// Say TicEvent is a type of event generated // to mark the tics of a system clock. It tells // listeners how long it has been since last tic. struct TicEvent { const float dt; TicEvent(float dt): dt(dt) {} };
processEvent(const
YourEvent&). This is where you add code to react to the event and use its data. Example: // Following class is one of your classes that does // something useful in your application, but in particular // is interested in tic events: class YourClassInterestedInTicEvents: public Listener<TicEvent> { public: YourClassInterestedInTicEvents(); // ...your constructors etc... // and the Listener override: virtual processEvent(const TicEvent& event) { // do something with tic event cout << "Event received, time since last=" << event.dt << endl; } private: // ...your private data etc... };
listenForEvents()
method, which will tell EventSender<YourEvent> that it is interested in getting events of type YourEvent. You make this call somewhere in your program, presumably from wherever you instantiate your listener object, but you could also make the call in the constructor of your listener. Example: // Say you do the call in the constructor: this will automatically // register the instance as soon as created YourClassInterestedInTicEvents::YourClassInterestedInTicEvents() { listenForEvents(); // ...do other stuff you need to do... }
listenForEvents()
: void f() { static time_t oldTime = time(); time_t newTime = time(); const float dt = newTime - oldTime; EventSender<TicEvent>::send( TicEvent(dt) ); oldTime = newTime; }