Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members

listener::ListenerForEvent< EVENT > Class Template Reference

Listener class for only one special Event of type EVENT. More...

#include <listener.h>

Inheritance diagram for listener::ListenerForEvent< EVENT >:

listener::Listener List of all members.

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.

Detailed Description

template<typename EVENT>
class listener::ListenerForEvent< EVENT >

Listener class for only one special Event of type EVENT.

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.

Note:
You cannot use the Event class as template parameter. If so, you will have an assertion.
WARNING: Using this class is 4 times slower that using Listener
See also:
testing/test_event_filtering for an example of use.
 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) {
     ...
   }
 } ;


The documentation for this class was generated from the following file:
Generated on Mon Jan 30 11:57:50 2006 for EventListener by  doxygen 1.3.9.1