Boost logo

Boost :

From: Philippe A. Bouchard (philippe_at_[hidden])
Date: 2003-08-03 15:55:32


Here is a better example. I am breaking standards when upcasting a pointer
to member though ((void (B::*)(int) -> (void (Object::*)(int))). This is
why I never proposed it ;)

#include <list>
#include <utility>
#include <iostream>

using namespace std;

struct Object
{
};

template <typename T>
 struct signal : list< pair<Object *, T Object::*> >
 {
  typedef typename list< pair<Object *, T Object::*> >::value_type
value_type;

  void connect(Object * p, T Object::* s)
  {
   push_back(value_type(p, s));
  }

  template <typename U, typename V>
   void emit(U const & u, V const & v)
   {
    for (typename list<value_type>::iterator i = begin(); i != end(); ++ i)
    {
     (i->first->*i->second)(u, v);
    }
   }

  template <typename U>
   void emit(U const & u)
   {
    for (typename list<value_type>::iterator i = begin(); i != end(); ++ i)
    {
     (i->first->*i->second)(u);
    }
   }

   void emit()
   {
    for (typename list<value_type>::iterator i = begin(); i != end(); ++ i)
    {
     (i->first->*i->second)();
    }
   }
 };

// Usage example:
struct A : Object
{
 signal<void (int)> sigdone;
};

struct B : Object
{
 void slot_bip(int a, ...)
 {
  cout << __PRETTY_FUNCTION__ << ": " << a << endl;
 }

 void slot_refresh(int a, ...)
 {
  cout << __PRETTY_FUNCTION__ << ": " << a << endl;
 }
};

int main()
{
 A a;
 B b;

 a.sigdone.connect(& b, (void (Object::*)(int)) & B::slot_bip);
 a.sigdone.connect(& b, (void (Object::*)(int)) & B::slot_refresh);

 a.sigdone.emit(99);
}

Philippe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk