|
Boost : |
From: Philippe A. Bouchard (philippe_at_[hidden])
Date: 2003-08-03 17:04:49
How about this one:
#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;
template <typename U>
void connect(Object & o, T U::* s)
{
push_back(value_type(& o, static_cast<T Object::*>(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, & B::slot_bip);
a.sigdone.connect(b, & 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