c++响应式编程 rxcpp之cep样例解析
#include "rxcpp/rx.hpp" #include <thread> // create alias' to simplify code // these are owned by the user so that // conflicts can be managed by the user. //创建别名以简化代码 //这一步由用户处理 用户可以处理因此带来的冲突 namespace rx = rxcpp; namespace rxsub = rxcpp::subjects; namespace rxu = rxcpp::util; #include <cctype> #include <clocale> // At this time, RxCpp will fail to compile if the contents // of the std namespace are merged into the global namespace // DO NOT USE: 'using namespace std;' // 不要用'using namespace std;',反之,Rxcpp将编译失败 int main() { auto keys = rx::observable<>::create<int>(//创建一个简单的可观察数字集合 [](rx::subscriber<int> dest) { for (;;) {//被观察者循环检测用户输入字符 std::cout << "threadid observable: " << std::this_thread::get_id() << std::endl; int key = std::cin.get(); dest.on_next(key);//通知观察者处理字符 } }). publish();//发布订阅 auto a = keys. filter([](int key) {return std::tolower(key) == 'a'; });//过滤器 过滤字符'a' auto g = keys. filter([](int key) {return std::tolower(key) == 'g'; });//过滤器 过滤字符'b' auto observer = a.merge(g).//过滤器合并 subscribe([](int key) {//观察者使用 Observable 类的 Subscribe 方法订阅可观察集合 std::cout << "threadid observer: " << std::this_thread::get_id() << std::endl; std::cout << key << std::endl; }); //observer.unsubscribe(); //可取消订阅 // run the loop in create //开启循环 keys.connect(); return 0; }