c – rxcpp:嵌套while循环或类似程序的“经典”命令式结构
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c – rxcpp:嵌套while循环或类似程序的“经典”命令式结构,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3889字,纯文字阅读大概需要6分钟。
内容图文
![c – rxcpp:嵌套while循环或类似程序的“经典”命令式结构](/upload/InfoBanner/zyjiaocheng/735/a063e6dd38c54a55a81b0e292450e7a4.jpg)
我有一个设备可以传输一些事件.我想使用反应式扩展来模拟以下行为:
>检测用户何时连接加密狗(我的程序检查连接加密狗的事件).
>连接加密狗后,开始从加密狗捕获数据流.
>能够检测到加密狗何时断开连接并返回1.以后,如果用户再次连接加密狗,我想转到2.如果键盘被击中我流数据的状态,则程序结束.
我知道如何等待加密狗连接(1.):
auto waitForDongle = events.take_while([](auto const & event) {
return event == DongleConnected
}).subscribe([](auto) {});
我知道如何捕获流(2.):
auto streamMotionData = events.take_while([](auto const &) { return !keyboardPressed(); })
.map([](auto const & evt) -> boost::optional<std::vector<double>> {
...
return data;
}).subscribe([](vector<double> const &) { ...});
我的问题是我不知道如何组合流以便回到1.以及之后2.我只知道如何一次又一次地执行此操作.但我想要上述行为.
解决方法:
这与Rx中的常见UX拖动示例有关.在这种情况下,印刷机是加密狗连接,释放是加密狗移除.
该解决方案要求一次只能连接一个加密狗(在下一次连接之前预期移除).事件中必须有更多信息才能允许多个连接重叠.
这是解决方案的核心.整个计划如下.
auto DatasFromConnectedDongle = DongleConnects. // when connected
map([=](DongleEvent const & event){
assert(event == DongleEvent::Connected);
cout << "Connected - " << flush;
return DongleDatas. // return all the datas
take_until(DongleRemoves). // stop when removed
finally([](){
cout << "- Removed" << endl;
});
}).
switch_on_next(). // only listen to datas from the most recent connected dongle
take_until(Exits); // stop everything when key is pressed
我最终使用了repeat(),但只生成了测试事件数据.
#include <rxcpp/rx.hpp>
namespace Rx {
using namespace rxcpp;
using namespace rxcpp::sources;
using namespace rxcpp::operators;
using namespace rxcpp::util;
}
using namespace Rx;
#include <cassert>
using namespace std;
using namespace std::chrono;
int main()
{
//
// test code
//
auto keyboardPressed = [](){
return false;
};
enum class DongleEvent {
Connected,
Removed,
Data,
Other
};
auto events = from(
DongleEvent::Data, DongleEvent::Other,
DongleEvent::Connected, DongleEvent::Data,
DongleEvent::Other, DongleEvent::Other,
DongleEvent::Data, DongleEvent::Removed,
DongleEvent::Other, DongleEvent::Data).
repeat(5). // send the above events five times over
zip(take_at<0>(), interval(milliseconds(200))). // pace our test data
publish().
ref_count(); // publish and ref_count make the events sharable
//
// the solution
//
// fires when connected
auto DongleConnects = events.
filter([](DongleEvent const & event) {
return event == DongleEvent::Connected;
});
// fires when data arrives
auto DongleDatas = events.
filter([](DongleEvent const & event) {
return event == DongleEvent::Data;
});
// fires when removed
auto DongleRemoves = events.
filter([](DongleEvent const & event) {
return event == DongleEvent::Removed;
});
// fires when key pressed
auto Exits = interval(milliseconds(200)).
filter([=](long){
return keyboardPressed();
});
auto DatasFromConnectedDongle = DongleConnects.
map([=](DongleEvent const & event){
assert(event == DongleEvent::Connected);
cout << "Connected - " << flush;
return DongleDatas. // return all the datas
take_until(DongleRemoves). // stop when removed
finally([](){
cout << "- Removed" << endl;
});
}).
switch_on_next(). // only listen to datas from the most recent connected dongle
take_until(Exits); // stop everything when key is pressed
DatasFromConnectedDongle.subscribe([](DongleEvent const & event){
assert(event == DongleEvent::Data);
cout << "Data " << flush;
});
return 0;
}
产生
~/source/rxcpp/Rx/v2/examples/dongle (master)$cmake .
...
~/source/rxcpp/Rx/v2/examples/dongle (master)$make
Scanning dependencies of target dongle
[ 50%] Building CXX object CMakeFiles/dongle.dir/main.cpp.o
[100%] Linking CXX executable dongle
[100%] Built target dongle
~/source/rxcpp/Rx/v2/examples/dongle (master)$./dongle
Connected - Data Data - Removed
Connected - Data Data - Removed
Connected - Data Data - Removed
Connected - Data Data - Removed
Connected - Data Data - Removed
~/source/rxcpp/Rx/v2/examples/dongle (master)$
内容总结
以上是互联网集市为您收集整理的c – rxcpp:嵌套while循环或类似程序的“经典”命令式结构全部内容,希望文章能够帮你解决c – rxcpp:嵌套while循环或类似程序的“经典”命令式结构所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。