【问题标题】:RXcpp stream data from function continuouslyRXcpp 从函数连续流数据
【发布时间】:2016-07-10 04:19:21
【问题描述】:

我正在学习 C++ 的响应式,我正在寻找一些指导。我创建了一个函数来等待事件然后返回它。我想捕捉反应式异步发生的所有事件并在它们发生时处理它们。到目前为止,这是我所拥有的:

int Engine::Initialize()
{   
    al_init();

    display = al_create_display(1200, 800);

    eventQueue = al_create_event_queue();

    al_install_mouse();
    al_install_keyboard();

    al_register_event_source(eventQueue, al_get_keyboard_event_source());
    al_register_event_source(eventQueue, al_get_mouse_event_source());

//test: wait for 2 events to happen
    auto events = rxcpp::observable<>::create([](rxcpp::subscriber<ALLEGRO_EVENT> e) 
    {
        e.on_next(WaitForEvent);
        e.on_next(WaitForEvent);
        e.on_completed();
    });

    events.subscribe([](ALLEGRO_EVENT e)
    {
        printf("We have an Event: %d \n", e.type);
    },

    []()
    {
        printf("Done\n");
    });

    return 0;
}

ALLEGRO_EVENT Engine::WaitForEvent()
{
    ALLEGRO_EVENT event;

    al_wait_for_event(eventQueue, &event);

    return event;
}

我似乎得到了错误:没有函数模板实例“rxcpp::observable::create”与参数列表匹配。我是否需要制作自己的模板或其他东西才能观察 ALLEGRO_EVENT?

【问题讨论】:

    标签: c++ reactive-programming allegro rxcpp


    【解决方案1】:

    the docs 看来,create 使用显式模板参数作为返回类型,因此您需要提供它。你的听众没有返回任何东西,所以void 可以工作。

    auto events = rxcpp::observable<>::create<void>(...
    

    【讨论】:

    • 现在我得到这个错误:没有函数模板实例 "rxcpp::observable::create" 匹配参数列表
    • 这很奇怪,因为当我从文档中粘贴示例时,它说了同样的话
    • 嗯。这很奇怪。这可能是一个错误。不幸的是,我没有任何使用这个库的经验。我建议在他们的discussion forum 上发帖。
    【解决方案2】:

    @tim 的想法是对的。

    我正在通过我们的discussion 发布答案

    create&lt;&gt;() 需要知道将传递给on_next() 的类型。 此外,WaitForEvent 是一个函数,但 on_next() 期待的是 ALLEGRO_EVENT,因此请确保调用 WaitForEvent() 并将结果传递给 on_next()

    auto events = rxcpp::observable<>::create<ALLEGRO_EVENT>([this](rxcpp::subscriber<ALLEGRO_EVENT> e) 
    {
        e.on_next(this->WaitForEvent());
        e.on_next(this->WaitForEvent());
        e.on_completed();
    });
    

    【讨论】:

      猜你喜欢
      • 2018-10-19
      • 1970-01-01
      • 2021-06-13
      • 2017-10-16
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      相关资源
      最近更新 更多