Boost logo

Boost :

From: Philippe A. Bouchard (philippe_at_[hidden])
Date: 2003-08-03 16:42:40


... or the following with the "emit" keyword defined as the only macro. The
function connect() could be simplified but I won't go further because it is
not pure C++. You take the decisions from here:

#include <vector>
#include <utility>
#include <iostream>

using namespace std;

struct Object
{
};

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

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

#define emit(SIGNAL) for (size_t i = 0; i != SIGNAL.size(); ++ i)
(SIGNAL[i].first->*SIGNAL[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);

 emit (a.sigdone)(99);
}

Philippe


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