2

Possible Duplicate:
Which C++ signals/slots library should I choose?

I've been using boost signals/signals 2 for a while to allow event hookups/function dispatching.

It works fine but I have two issues with it.

-This is the more minor one, but handling disconnects can be a pain. I use the trackable class to help with this, but it doesn't solve everything. Having to keep track of the connection if I want to disconnect the signal at an arbitrary time is a bit of nuisance as well

-This is the bigger one. There's no way to copy the signals, meaning I need to implement a copy constructor every time I add a signal to a class, and if I do copy the class, it will no longer have any events hooked up. The obvious solution is to make the held Signal a pointer, then it's copyable and shareable, but this feels dirty and of bad form.

Given the above I started looking for alternatives, but the above issues seem common to signal libraries. I was wondering if anyone had suggestions of getting around the above problems; perhaps a way to deal with them directly.

I was also considering going back to my original solution before I started using boost signals. Something like this:

boost::unordered_map<std::string, boost::function>

the boost::function will take the place of slots/callbacks, then I add a Dispatch function to the class holding this map that will loop trough and call each boost::function. With this approach, I can remove callbacks easily at anytime, and I can copy the map. I was told this wasn't a good way to do it, but looking at it now, I can't really see the problem here.

Further, now that there's variadic templates, it should be an easy task to make a small class around this map to deal with the Dispatch/Add/Remove callbacks calls to the map.

Is this a bad approach?

Community
  • 1
  • 1
Megatron
  • 2,871
  • 4
  • 40
  • 58
  • Are you running in a multithreading environment? If so, beware of threading issues with the solution you decide to use. – David Rodríguez - dribeas Oct 02 '12 at 15:14
  • real problem with copying the object is that the target of connection is described with pointers. You'd need to add some way to register objects to global container and fetch them with id to be able to copy the object correctly. – tp1 Oct 02 '12 at 21:58

0 Answers0