#include <Notifier.h>
Collaboration diagram for TSE3::Notifier< interface_type >:
Public Types | |
typedef Listener< interface_type > | listener_type |
Protected Types | |
typedef interface_type::notifier_type | c_notifier_type |
Protected Member Functions | |
Notifier () | |
template<typename func_type> void | notify (func_type func) |
template<typename func_type, typename p1_type> void | notify (func_type func, const p1_type &p1) |
template<typename func_type, typename p1_type, typename p2_type> void | notify (func_type func, const p1_type &p1, const p2_type &p2) |
template<typename func_type, typename p1_type, typename p2_type, typename p3_type> void | notify (func_type func, const p1_type &p1, const p2_type &p2, const p3_type &p3) |
virtual | ~Notifier () |
unsigned int | numListeners () const |
Friends | |
class | listener_type |
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.
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<ExhibtionistListener> { // Other contents.... };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.
|
|
The concrete Notifier type (i.e. the class that will be subclassing Notifier<interface_type>. |
|
Creates a Notifier with no listeners. Use Listener::attachTo to add listeners. You can only subclass this type, not instanitate it directly. |
|
The dtor tells every attached Listener that this object is being deleted. |
|
There is a family of 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 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. |
|
Notify function for events with one extra parameter. |
|
Notify function for events with two extra parameters. |
|
Notify function for events with three extra parameters. |
|
|
|
|