Boost logo

Boost :

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


Terje Slettebø wrote:

[...]

> It's also possible to do the signal/slot without macros on wxWindows.
> See here (http://www.wxwindows.org/hworld2.txt) for an example. It's
> all done in standard C++, without any macros.

It is a lot better this way, I was not aware of this new syntax. But it
seems you still need global IDs to refer to the members... A signal is
really a list of pointer to members, maybe the following could simplify the
situation. This is an example, but the signal<>::emit should be overloaded
for N template parameters, N times:

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

using namespace std;

struct Object
{
};

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

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

struct A : Object
{
 signal<void (...)> sigdone;
};

struct B : Object
{
 void slot_bip()
 {
  cout << __PRETTY_FUNCTION__ << endl;
 }

 void slot_refresh()
 {
  cout << __PRETTY_FUNCTION__ << endl;
 }
};

int main()
{
 A a;
 B b;

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

 a.sigdone.emit();
}

> The syntax is similar to Qt's signal/slot mechanism, but without any
> preprocessor/intermediate compiler (MOC) needed.
>
> Here's a version using macros, which then resembles MFC
> (http://www.wxwindows.org/hworld.txt).

Yeah, this is what I was talking about.

Philippe


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