|
|
The Notifier template base class is an implementation of the observer design pattern (GoF book).
Objects that need to send out specific callback events derive from this base class. Each Notifier class may have any number of callback events, and each of these events may take any number (and type) of parameters. The callback system is type safe and easy to use.
Events sent from the Notifier class are received by objects of the Listener class.
A separate interface class detailing each callback event and the type of Notifier source class must be written, for example:
class Exhibitionist; class ExhibitionistListener { public: typedef Exhibitionist notifier_type; virtual void eventOne(Exhibitionist *) = 0; virtual void eventTwo(Exhibitionist *, int a) = 0; }; |
The first parameter of callback events must always be a pointer to the source object.
You may choose to provide a default implementation for the callback methods. This will save you having to reimplement every callback, only the ones for the events you are interested in.
You can now declare a Notifier class thus:
class Exhibitionist : public Notifier |
and implement your functionality.
The Exhibitionist class can send callbacks to any attached listener by calling the notify member function. For example:
notify(&ExhibitionistListener::eventTwo, 1); |
The source object pointer parameter is automatically provided and does not need to be specified.
To receive events from a Notifier class, you must write a class that derives from Listener. The documentation for Listener describes how to do this.
See also: Listener
typedef Listener | listener_type |
The type of Listener that this Notifier class works with.
friend class listener_type | listener_type |
Notifier ()
| Notifier |
[protected]
Creates a Notifier with no listeners.
Use Listener::attachTo to add listeners.
You can only subclass this type, not instanitate it directly.
typedef interface_type::notifier_type c_notifier_type | c_notifier_type |
[protected]
The concrete Notifier type (i.e. the class that will
be subclassing Notifier
template | notify |
[protected]
There is a family of notify
functions. The allow a Notifier
to multicast an event to every attached Listener object.
They are overloaded on the different number of parameters each listener event function takes.
You call a notify method specifying first the member function of
the interface_type
to call, and then the parameters to call
it with.
Every listener function has a first parameter which is a pointer to this (source) object. This is automatically provided by the notify function, so you need not supply it. If a funciton takes any extra parameters then you must specify them.
The first notify function is for events with no extra parameters.
template | notify |
[protected]
Notify function for events with one extra parameter.
template | notify |
[protected]
Notify function for events with two extra parameters.
template | notify |
[protected]
Notify function for events with three extra parameters.
~Notifier ()
| ~Notifier |
[protected virtual]
The dtor tells every attached Listener that this object is being deleted.
unsigned int numListeners ()
| numListeners |
[protected const]