OBT::EventSource< TListener, TEvent > Class Template Reference

Abstract base class of all event sources. More...

#include <OBTEventSource.h>

Collaboration diagram for OBT::EventSource< TListener, TEvent >:
[legend]

List of all members.

Public Member Functions

 EventSource (ProcessEventFunc processEvent)
 constructor
virtual ~EventSource ()
 destructor
void addListener (TListener &listener)
 subscribe a listener to the publication of the event
void removeListener (TListener &listener)
 unsubscribe a listener to the publication of the event
void publishEvent (const TEvent &event)
 publish the event to the subscribed listeners

Private Types

typedef void(TListener::* ProcessEventFunc )(const TEvent &)
 pointer to the callback implemented by a listener to process a published event.

Private Attributes

std::vector< TListener * > _listeners
 pointers to the added listeners interface
ProcessEventFunc _processEvent
 pointer to the TListener method processing the event

Detailed Description

template<class TListener, class TEvent>
class OBT::EventSource< TListener, TEvent >

Abstract base class of all event sources.

Author:
Michaël Rouillé <michael.rouille@gmail.com>

An event source (i.e., publisher) has two responsibilities: 1. Register listener objects. 2. Notify listener objects when an event happens by sending them a message.

Definition at line 19 of file OBTEventSource.h.


Member Typedef Documentation

template<class TListener , class TEvent >
typedef void( TListener ::* OBT::EventSource< TListener, TEvent >::ProcessEventFunc)(const TEvent &) [private]

pointer to the callback implemented by a listener to process a published event.

Definition at line 22 of file OBTEventSource.h.


Constructor & Destructor Documentation

template<class TListener , class TEvent >
OBT::EventSource< TListener, TEvent >::EventSource ( ProcessEventFunc  processEvent  )  [inline]

constructor

Parameters:
processEvent pointer to the TListener method processing the event

Definition at line 68 of file OBTEventSource.h.

References OBT::EventSource< TListener, TEvent >::_processEvent.

00069         {
00070                 _processEvent = processEvent ;
00071         }

template<class TListener , class TEvent >
OBT::EventSource< TListener, TEvent >::~EventSource (  )  [inline, virtual]

destructor

Definition at line 77 of file OBTEventSource.h.

00078         {
00079         }


Member Function Documentation

template<class TListener , class TEvent >
void OBT::EventSource< TListener, TEvent >::addListener ( TListener &  listener  )  [inline]

subscribe a listener to the publication of the event

Parameters:
listener reference to the listener instance

Definition at line 86 of file OBTEventSource.h.

References OBT::EventSource< TListener, TEvent >::_listeners.

00087         {
00088                 _listeners.push_back( &listener );
00089         }

template<class TListener , class TEvent >
void OBT::EventSource< TListener, TEvent >::publishEvent ( const TEvent &  event  )  [inline]

publish the event to the subscribed listeners

Parameters:
event a const reference to the event to publish while processing a published event in its ProcessEventFunc callback.

Definition at line 118 of file OBTEventSource.h.

References OBT::EventSource< TListener, TEvent >::_listeners, and OBT::EventSource< TListener, TEvent >::_processEvent.

00119         {
00120                 std::vector< TListener* >::iterator ite( _listeners.begin() ) ;
00121                 for ( ; ite != _listeners.end() ; ++ite )
00122                 {
00123                         ( ( *ite )->*_processEvent )( event );
00124                 }
00125         }

template<class TListener , class TEvent >
void OBT::EventSource< TListener, TEvent >::removeListener ( TListener &  listener  )  [inline]

unsubscribe a listener to the publication of the event

Parameters:
listener reference to the listener instance

Definition at line 96 of file OBTEventSource.h.

References OBT::EventSource< TListener, TEvent >::_listeners.

00097         {
00098                 std::vector< TListener* >::iterator ite( _listeners.begin() ) ;
00099                 while ( ite != _listeners.end() )
00100                 {
00101                         if ( &listener == *ite )
00102                         {
00103                                 _listeners.erase( ite ) ;
00104                                 ite = _listeners.end() ;
00105                         }
00106                         else
00107                         {
00108                                 ++ite ;
00109                         }
00110                 }
00111         }


Member Data Documentation

template<class TListener , class TEvent >
std::vector< TListener* > OBT::EventSource< TListener, TEvent >::_listeners [private]
template<class TListener , class TEvent >
ProcessEventFunc OBT::EventSource< TListener, TEvent >::_processEvent [private]

pointer to the TListener method processing the event

Definition at line 61 of file OBTEventSource.h.

Referenced by OBT::EventSource< TListener, TEvent >::EventSource(), and OBT::EventSource< TListener, TEvent >::publishEvent().


Generated on 1 Jan 2010 for OBT by  doxygen 1.6.1