next up previous contents
Next: Example 1: Templated events Up: MultiplexerListener Previous: MultiplexerListener   Contents

igs_connect and igs_disconnect

Using the igs_connect and igs_disconnect global template functions facilitate the processus of connection and disconnection.
The interfaces are:
  template <typename E>
  Listener::ID igs_connect(Publisher* publisher, 
                           Subscriber* susbscriber, 
                           const basic::function1<void, E*>& f, 
                           unsigned int priority=0) ;
  template <typename E>
  bool igs_disconnect(Publisher* puslisher,
                      Subscriber* subscriber,
                      const basic::function1<void, E*>& f) ;
and the related macros are:
  igs_functor(SUBSCRIBER, METHOD) ;
  igs_function(FUNCTION) ;
Example of use:
  class Event1: public Event {} ;
  class Event2: public Event {} ;
  class Event3: public Event {} ;

  class S: public Subscriber {
  public:
    void event1(Event1*) {std::cerr << "1\n" ;}
    void event2(Event2*) {std::cerr << "2\n" ;}
    void event3(Event3*) {std::cerr << "3\n" ;}
  } ;
  
  int main() {
    Publisher p ;
    S         s ;
    igs_connect(&p, &s, igs_functor(&s, S::event1)) ;
    igs_connect(&p, &s, igs_functor(&s, S::event2)) ;
    igs_connect(&p, &s, igs_functor(&s, S::event3)) ;
    p.igs_emit(Event1) ;
    p.igs_emit(Event2) ;
    p.igs_emit(Event3) ;
    igs_disconnect(&p, &s, igs_functor(&s, S::event3)) ;
    p.igs_emit(Event3) ; // never received
  }
Here, we see that no special Listener for each Event is created, and that the MultiplexerListener class is used through the igs_connect function to listen to the special events defined by the S member functions. This class automatically recognize the Event type argument and do the necessary connections.
Note that the igs_connect returns the created Listener, which allows you block/unlock, add EventFilter and remove it from the Subscriber.

The reason why the igs_connect takes a Subscriber pointer and the repeated Subscriber pointer within the functor definition, is because you can use a functor from another class, or a C function as in the following code:
  class S {
  public:
    void event1(Event1*) {}
    void event2(Event2*) {}
  } ;

  void event3(Event3*) {}
  
  int main() {
    Publisher* pub = new Publisher ;
    Subscriber sub ;
    S* s = new S ;
 
    igs_connect(pub, sub, igs_functor(s, S::event1)) ;
    igs_connect(pub, sub, igs_functor(s, S::event2)) ;
    igs_connect(pub, sub, igs_function(event3)) ;
    
    pub->igs_emit(Event3) ;
    ...
  }



Frantz Maerten 2006-01-30