Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

Event-handling library: simple use

  1. Create your event type by creating a struct containing the event data. Typically the data members are all public and const. Rarely should your event struct require methods, except for a constructor to initialize the data. Example:
           // 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) {}
           }; 
    
  2. Make one of your classes a listener of that type of event by inheriting from Listener<YourEvent> and overriding 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...
           }; 
    
  3. Add a call to your listener class' 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...
           }
    
  4. Instantiate a YourEvent and call EventSender<YourEvent >send(const YourEvent&). E.g., the following will cause a tic event to be heard by all Listener<TicEvent> objects that have called listenForEvents():
           void f() {
               static time_t oldTime = time();
               time_t newTime = time();
               
               const float dt = newTime - oldTime; 
               EventSender<TicEvent>::send( TicEvent(dt) );
               
               oldTime = newTime;
           } 
    

Generated on Sun Feb 16 16:56:24 2003 for C++ Event Handling Template Library by doxygen1.2.18