I want to implement a class that uses boost::signal for callbacks. Other objects can register their callback functions using AddHandler() and RemoveHandler() methods.
In one SO answer it is suggested that we track the boost::signal::connection objects returned from connect(), but in another it is suggested that we don't! I'm not clear on how to manage the connections.
How would I be able to look up which connection to disconnect later when I only have the slot_type?
class MyClass {
public:
typedef void Handler();
void AddHandler(const boost::signal<Handler>::slot_type& aHandler) {
mSignal.connect(aHandler);
}
void RemoveHandler(const boost::signal<Handler>::slot_type& aHandler) {
mSignal.disconnect(aHandler); // compiler error!
}
private:
boost::signal<Handler> mSignal;
};
I want a caller to be able to do something like:
MyObject lMyObject;
lMySignaler.AddHandler(boost::bind(&MyObject::OnEvent, lMyObject));
...
lMySignaler.RemoveHandler(boost::bind(&MyObject::OnEvent, lMyObject));
Rather than using
const boost::signal<Handler>::slot_type&as my parameter type, I changed them to function pointers. Then I used the function pointers as the keys in astd::mapto track theboost::signals::connections. When theRemoveHandleris called I just calleddisconnecton the connection and erase the entry in the map.