#include <listener.h>
Inheritance diagram for listener::ListenerForEvent< EVENT >:
Public Member Functions | |
ListenerForEvent (unsigned int priority=0) | |
ListenerForEvent (Publisher *p, unsigned int priority=0) | |
Protected Member Functions | |
virtual bool | process_event (EVENT *e)=0 |
Client code override this method when receiving an event of type EVENT. | |
virtual bool | process_event (Event *e) |
Client code does not override this method anymore, nor it should be used. |
When overriding process_event, your are totally sure that the passed Event if of type EVENT. This class uses a filter of type EventFiltering as a permanent filter.
Client code does not override the process_event(Event*), but instead the specialized one for EVENT type: process_event(EVENT*). In any case, you should have a compiler error if you use Event as template parameter, since 2 same methods will be defined.
class MouseEvent: public Event { public: MouseEvent(int x, int y): x_(x), y_(y) {} double x() const {return x_ ;} double y() const {return y_ ;} private: double x_, y_ ; } ; // -------------------------------- // Use with the method add_listener // -------------------------------- class MouseListener: public ListenerForEvent<MouseEvent> { public: typedef basic::function2<void,double,double> fct ; MouseListener(Publisher* p, const fct& f): ListenerForEvent<MouseEvent>(p), f_(f) {} protected: virtual bool process_event(MouseEvent* e) { f_(e->x(), e->y()) ; } private: fct f_ ; } ; class MySubscriber: public Subscriber { public: void mouse_move(double, double) {...} } ; MySubscriber s ; s.add_listener(new MouseListener(&p, basic::bind(&MySubscriber::mouse_move, &s))) ; // ------------------ // or when inheriting // ------------------ class MySubscriber: public Subscriber, public ListenerForEvent<MouseEvent> { public: MySubscriber(Publisher* p): ListenerForEvent<MouseEvent>(p) { register_listener<ListenerForEvent<MouseEvent> >() ; } protected: virtual bool process_event(MouseEvent* e) { ... } } ;