Listener< EvType > Class Template Reference

#include <Listener.h>

List of all members.

Public Member Functions

 Listener ()
 By default, listener not registered.
virtual ~Listener ()
 Automatically deregister ourselves.
void ignoreEvents ()
void listenForEvents ()
void ignoreThisEvent ()
void processEventPublic (const EvType &)
bool isRegistered () const

Protected Member Functions

virtual void processEvent (const EvType &event)=0


Detailed Description

template<typename EvType>
class Listener< EvType >

Derive your class from a Listener<EvType>, to give it the ability to hear (i.e. receive, listen to) events of type EvType. EvType is a fundamental type (bool, int, float, etc), a struct or a class of your choice. You must define Listener<EvType>::processEvent() in your Listener<EvType> subclass. You call listenForEvents() on your listener instance (at least once) for it to be able to hear (i.e. receive) any EvType event, and you call ignoreEvents() on the instance so it will no longer receive events of type EvType.

Note:
  • Do not assume that the Listeners will receive the generated event in the same order as they registered.
  • A class can listen to more than one type of event by inheriting from more than one type of listener. Calling listenForEvents() then is done by specifying which listenForEvents() to call, e.g.
          class YourListener: 
              public Listener<TicEvent>, public Listener<TacEvent> 
          {
              public:
                  YourListener() {
                      Listener<TicEvent>::listenForEvents();
                      Listener<TacEvent>::listenForEvents();
                  }
              protected: 
                  virtual void processEvent(const TicEvent& event) {...}
                  virtual void processEvent(const TacEvent& event) {...}
          };
    
  • If a subclass of your class must also process an event, remember that because its processEvent is virtual, it will be the first one called. Therefore it should call its base class method:
          struct TicEvent {...}; 
          class YourListener: public Listener<TicEvent> {...};
          class DerivedListener: public YourListener
          {
              public: 
                  // ...
              protected:
                  virtual void processEvent(const TicEvent& event) 
                  {
                      // give parent class a chance to process event
                      YourListener::processEvent(event);
                      // do our stuff ...
                  }
          };
    
  • ignoreThisEvent(): You can call it (from inside processEvent()) to tell EventSender<EvType> that you will be ignoring the event received. This can be a useful way to signify an error in the data sent by the event generator. Just keep in mind that the generator of the event may not actually check whether any listener ignored the event (it may not care).

Definition at line 23 of file Listener.h.


Constructor & Destructor Documentation

template<typename EvType>
Listener< EvType >::Listener (  )  [inline]

By default, listener not registered.

Definition at line 27 of file Listener.h.

template<typename EvType>
virtual Listener< EvType >::~Listener (  )  [inline, virtual]

Automatically deregister ourselves.

Definition at line 31 of file Listener.h.


Member Function Documentation

template<typename EvType>
void Listener< EvType >::ignoreEvents (  )  [inline]

Tell EventSender<EvType> that this listener is not interested in hearing EvType events. This does nothing if already ignoring them.

Definition at line 61 of file Listener.cpp.

template<typename EvType>
void Listener< EvType >::listenForEvents (  )  [inline]

Tell EventSender<EvType> that this listener wants to hear EvType events. This does nothing if already listening to them.

Definition at line 77 of file Listener.cpp.

template<typename EvType>
void Listener< EvType >::ignoreThisEvent (  )  [inline]

Tell EventSender<EvType> that this listener is ignoring the event received. This will increment a counter in EventSender<EvType>, such that a call to EventSender<EvType>::getNumIgnored() will return how many listeners ignored the event. Note however that the listener is not required to notify EventSender<EvType> that it is ignoring the event, hence this number is only a minimum. This method can be called more than once without screwing up the count. If called from outside a call to Listener<EvType>::processEvent(), nothing is done.

Definition at line 41 of file Listener.cpp.

template<typename EvType>
void Listener< EvType >::processEventPublic ( const EvType &  event  )  [inline]

Process this event. This public method can be called directly with an event to simulate an event having been generated and heard by this listener. It is also called by EventSender<EvType>::send(). This public method is a "template pattern" method, in that it calls several private methods but most importantly calls the protected, virtual processEvent(). Since the latter is virtual it is the definition in the most derived class of this Listener<EvType> that will get called. The event is passed as const, so listeners can't change the data carried by the event.

Parameters:
event event to process

Definition at line 100 of file Listener.h.

template<typename EvType>
bool Listener< EvType >::isRegistered (  )  const [inline]

Is this instance of listener registered? If not, it will not receive events of type EvType, processEvent() will not be called. To register it, call listenForEvents().

Returns:
true if *this is registered

Definition at line 44 of file Listener.h.

template<typename EvType>
virtual void Listener< EvType >::processEvent ( const EvType &  event  )  [protected, pure virtual]

Classes that inherit from Listener must override this method. This is where you react to the event heard.

Parameters:
event the event heard.

Implemented in mdCliever, mdCliever, mdCliever, mdCliever, mdCliever, mdCliever, mdEmbedded, mdEmbedded, mdEmbedded, mdEmbedded, mdEmbedded, mdEmbedded, masterDaemon, masterDaemon, masterDaemon, masterDaemon, masterDaemon, masterDaemon, masterDaemon, and masterDaemon.


The documentation for this class was generated from the following files:

Generated on Mon Jan 10 22:33:44 2011 by  doxygen 1.5.6