I have an Event class. In AddHandler(thisPtr, callback) I use tr1::bind on thisPtr and callback and eventually I have a list:
typedef std::tr1::function<void( int& )> CallbackFunction;
std::list< CallbackFunction > m_handlers;
But how can I implement a remove handler method if tr1::function is non-comparable?
Should I keep a map from object pointers and callbacks to tr1::function and remove the one having equal object pointer and callback pointer to the arguments in RemoveHandler? Is this a safe approach?
Since you’re using an std::list, which never invalidates iterators, as long as the element stays in the container, you can use an iterator to keep track of the functions you insert. Example:
Now, you can call that function to insert a callback, and it will return an iterator to it, which you can use to remove it, like this: