boost::signals2 Annoyance

Started by
2 comments, last by krippy2k8 11 years, 7 months ago
My Scene has a collection of Objects. Object has a signal called SelectionChanged. For every Object in the Scene, the Scene subscribes to the SelectionChanged signal.

My annoyance is that if an object is removed from the scene, I need to disconnect the subscription. In C#, this would just be something like

obj.SelectionChanged -= OnSelectionChanged;

boost::signals2 does not seem to support this. Do I really have to maintain a collection of boost::signals2::connection for every object so that I can disconnect? There seems to be a lot of extra boilerplate code for signals compared to events in C#.
-----Quat
Advertisement
Well in C# you would use delegates, and C++ doesn't have delegates. OnSelectionChanged doesn't have enough meaning to C++ to be able to use it to connect or disconnect to a signal. A slot has to be a combination of the function pointer and the object pointer, plus optional information about additional arguments.

You could create your own delegate class that holds a function object and a connection object, but that wouldn't really save you any work.

You could also have a look at Poco which does have some Event and Delegate classes that are designed to be similar to the C# events and delegates, with a similar syntax.
Or if you want a simple delegate implementation (simple as in one header file), try fastdelegate:

http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

The C++11 standard library and Boost both essentially have delegates as well (std::function and boost::function), they just don't have an event/signal mechanism that makes use of them with the same type of syntax that C# users are accustomed to.

This topic is closed to new replies.

Advertisement