
The event framework relies on three classes: the event that is passed out, a dispatcher that sends the events and listeners that receive them. The former two are based on the Event and EventDispatcher base classes. In order to create a new event type, developers should write subclasses of these two classes and provide an appropriate listener interface. A typical implementation is listed below.
class FooDispatcher; class FooEvent : public Event<FooDispatcher> { private: //event data public: FooEvent(FooDispatcher* src) : Event<FooDispatcher>(src) {} //data accessors }; class FooListener { public: virtual ~FooListener() {} virtual void processFooEvent(FooEvent&) = 0; }; class FooDispatcher : public EventDispatcher<FooEvent,FooListener,struct FooRelay> { public: inline void addFooListener(const LPtr& l) { add(l); } inline void removeFooListener(const LPtr& l) { remove(l); } }; struct FooRelay { inline static void forward(FooEvent& e, const FooDispatcher::LPtr& l) { l->processFooEvent(e); } };
EventDispatcher documentation for details on the forward function. EventDispatcher template is necessary for each type of event, very similar actions should be combined in one event class. However, too general events will lead to excessive listener notification. For example, pressing and releasing a mouse button is combined in one event class, while a keystroke would be considered a different event.
| Disp | the dispatcher class for this event |
Public Member Functions | |
| Event (Disp *src) | |
| creates a new event. | |
| Disp * | getSource () const |
| provides access to the source of the event. | |
| virtual | ~Event () |
|
||||||||||
|
creates a new event.
|
|
|||||||||
|
|
|
|||||||||
|
provides access to the source of the event.
The source of an event is the
|
1.4.3