1

I'm learning reactive for c++ and I'm looking for some guidance. I've created a function to wait for an event then return it. I want to catch all the events that occur with reactive async and handle them as they come. Here's what I have so far:

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;
}

I seem to get the error: no instance of function template "rxcpp::observable::create" matches the argument list. Do I need to make my own template or something to be able to observe an ALLEGRO_EVENT?

Rook
  • 5,734
  • 3
  • 34
  • 43
shady
  • 1,076
  • 2
  • 13
  • 33

2 Answers2

0

From the docs, it looks like create uses an explicit template parameter for the return type, so you need to provide it. Your listener isn't returning anything, so void would work.

auto events = rxcpp::observable<>::create<void>(...
Tim
  • 1,517
  • 1
  • 9
  • 15
  • Now I get this error: no instance of function template "rxcpp::observable::create" matches the argument list – shady Jul 10 '16 at 14:05
  • its weird because when I paste in the sample from the docs it says the same thing – shady Jul 10 '16 at 15:48
  • Hmm. That is weird. It might be a bug. Unfortunately, I don't have any experience using this library. I would recommend posting on their [discussion forum](http://rxcpp.codeplex.com/discussions). – Tim Jul 10 '16 at 22:46
0

@tim had the right idea.

I am posting the answer from our discussion

create<>() needs to know the type that will be passed to on_next(). In addition, WaitForEvent is a function, but on_next() is expecting ALLEGRO_EVENT so make sure to call WaitForEvent() and pass the result to 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();
});
Kirk Shoop
  • 1,274
  • 9
  • 13