Help with boost::function and boost::bind
I need to make a registry of callback functions, which are in the form of member functions of objects of various types. ----------- code snippet ----------- class T1 { public: void CallBackFcn(); }; ... class Registry { public: template <typename T> void registerFcn (void (T::*updFcn)(), T* obj ); private: vector< boost::function<void>* > fcnvec; }; template <typename T> void Registry::registerFcn(void (T::*updFcn)(), T* obj) { boost::function<void> *fp = new boost::function<void> ( boost::bind(updFcn,obj) ); fcnvec.push_back( fp ); } main() { .... T1 *pt = new T1; p_Registry->registerFcn(&T1::CallBackFcn, pt); } ----------------------------------------------------- However, before adding the boost::function to the vector, I want to make sure that the callback function isn't already in there. Unfortunately, I can't just look at value of the pointer fp - I need to get to the address of the actual callback function that is to be executed. But I can't figure out how to get there. Any help appreciated.
On Wednesday 13 November 2002 06:10 pm, electricsnark wrote:
I need to make a registry of callback functions, which are in the form of member functions of objects of various types. [snip code snippet] However, before adding the boost::function to the vector, I want to make sure that the callback function isn't already in there. Unfortunately, I can't just look at value of the pointer fp - I need to get to the address of the actual callback function that is to be executed. But I can't figure out how to get there.
Unfortunately, you can't get the address or type of the underlying function object from a boost::function object. But even if you could, it wouldn't help much, because you can't compare the function objects returned by Boost.Bind. One common workaround is to explicitly keep "tokens" around that represent a callback function that is in the list. The token could be, for instance, an iterator into the callback list (not for a vector!). The Signals library does this with the "signals::connection" class, which can be used to query the status of the connection and/or disconnect the connection (e.g., remove the callback function from the vector). You might also want to take a look at the Signals library. It may do what you need. Doug
participants (2)
-
Douglas Gregor
-
electricsnark