Index: ext/libicpf/src/callback.h =================================================================== diff -u -re17c80d36eaa0430313e7d1058aa7a301d1510af -re1fe275afd58c6df65f3ceac3b8959d39d3fbc3d --- ext/libicpf/src/callback.h (.../callback.h) (revision e17c80d36eaa0430313e7d1058aa7a301d1510af) +++ ext/libicpf/src/callback.h (.../callback.h) (revision e1fe275afd58c6df65f3ceac3b8959d39d3fbc3d) @@ -29,25 +29,41 @@ #include #include "mutex.h" -/// Callback1-type callback function -typedef void(*PFNCALLBACKPROC1)(ptr_t, ptr_t); -/// Callback2-type callback function -typedef void(*PFNCALLBACKPROC2)(ptr_t, ptr_t, ptr_t); - BEGIN_ICPF_NAMESPACE -/// Helper structure for callback1 class -struct _CALLBACKDATA1 +/// General function definition +typedef void(*PFNFUNC)(void); + +/// Helper structure for callback class +struct CLBDATA { - PFNCALLBACKPROC1 pfn; ///< Callback function that is to be called - ptr_t pParam; ///< The user parameter of a function to be called + PFNFUNC pfn; ///< General function definition + ptr_t param; }; -/// Helper structure for callback2 class -struct _CALLBACKDATA2 +class LIBICPF_API callback_list { - PFNCALLBACKPROC2 pfn; ///< Callback function that is to be called - ptr_t pParam; ///< The user parameter of a function to be called +public: +/** \name Construction/destruction */ +/**@{*/ + callback_list(); + ~callback_list(); +/**@}*/ + + void add(PFNFUNC pfn, ptr_t param); + bool remove(PFNFUNC pfn); + void clear(); + size_t size(); + CLBDATA* at(size_t tIndex); + + void lock(); + void unlock(); + +protected: + icpf::mutex m_lock; ///< A locking mechanism for the storage area + +private: + void* m_pStorage; ///< A pointer to a storage struct (a std::list probably), but must be inaccessible from outside }; /** \brief Callback class with one parameter. @@ -56,26 +72,55 @@ * function(s) registered by the user. Good for notifying user that something * had happened. */ -class LIBICPF_API callback1 +template +class /*LIBICPF_API*/ callback1 : public callback_list { +protected: + /// Callback1-type callback function + typedef R(*PFNCALLBACKPROC1)(ptr_t, P1); + public: /** \name Construction/destruction */ /**@{*/ - callback1(); ///< Standard constructor - ~callback1(); ///< Standard destructor + callback1() { }; ///< Standard constructor + ~callback1() { }; ///< Standard destructor /**@}*/ /** \name User interface */ /**@{*/ - void exec(ptr_t pData); ///< Executes registered callback functions with the pData as the first param + /** Executes a callback list associated with this object. + * \param[in] data - parameter that will be passed to a user callback function + */ + void exec(P1 data) + { + m_lock.lock(); + CLBDATA* pData; + for (size_t i=0;i != size();i++) + { + pData=at(i); + if (pData) + (*(PFNCALLBACKPROC1)(pData->pfn))(pData->param, data); + } + m_lock.unlock(); + } - void connect(PFNCALLBACKPROC1 pfn, ptr_t); ///< Connects the callback function to this callback class - void disconnect(PFNCALLBACKPROC1 pfn); ///< Disconnects the callback function from this callback class + /** Connects a user callback function to this object. + * \param[in] pfn - user callback function address + * \param[in] appParam - user parameter to pass to the callback function when executing + */ + void connect(PFNCALLBACKPROC1 pfn, ptr_t appParam) + { + add((PFNFUNC)pfn, appParam); + } + + /** Disconnects the user callback function if connected earlier. + * \param[in] pfn - address of a function to remove + */ + void disconnect(PFNCALLBACKPROC1 pfn) + { + remove((PFNFUNC)pfn); + } /**@}*/ - -protected: - std::list<_CALLBACKDATA1> m_lCalls; ///< List of the callback structures to execute - mutex m_lock; ///< Mutex for locking connect/disconnect calls }; /** \brief Callback class with two parameters. @@ -84,26 +129,115 @@ * function(s) registered by the user. Good for notifying user that something * had happened. */ -class LIBICPF_API callback2 +template +class /*LIBICPF_API*/ callback2 : public callback_list { +protected: + /// Callback2-type callback function + typedef R(*PFNCALLBACKPROC2)(ptr_t, P1, P2); + public: /** \name Construction/destruction */ /**@{*/ - callback2(); ///< Standard constructor - ~callback2(); ///< Standard destructor + callback2() { }; ///< Standard constructor + ~callback2() { }; ///< Standard destructor /**@}*/ /** \name User interface */ /**@{*/ - void exec(ptr_t pData, ptr_t pData2); ///< Executes registered callback functions with the pData as the first param + /** Executes a callback list associated with this object. + * \param[in] data1 - parameter that will be passed to a user callback function + * \param[in] data2 - parameter that will be passed to a user callback function + */ + void exec(P1 data1, P2 data2) + { + m_lock.lock(); + CLBDATA* pData; + for (size_t i=0;i != size();i++) + { + pData=at(i); + if (pData) + (*(PFNCALLBACKPROC2)(pData->pfn))(pData->param, data1, data2); + } + m_lock.unlock(); + } - void connect(PFNCALLBACKPROC2 pfn, ptr_t pParam); ///< Connects the callback function to this callback class - void disconnect(PFNCALLBACKPROC2 pfn); ///< Disconnects the callback function from this callback class + /** Connects a user callback function to this object. + * \param[in] pfn - user callback function address + * \param[in] appParam - user parameter to pass to the callback function when executing + */ + void connect(PFNCALLBACKPROC2 pfn, ptr_t appParam) + { + add((PFNFUNC)pfn, appParam); + } + + /** Disconnects the user callback function if connected earlier. + * \param[in] pfn - address of a function to remove + */ + void disconnect(PFNCALLBACKPROC2 pfn) + { + remove((PFNFUNC)pfn); + } /**@}*/ - +}; + +/** \brief Callback class with three parameters. + * + * Class provides a simple interface for user to call a specific callback + * function(s) registered by the user. Good for notifying user that something + * had happened. + */ +template +class /*LIBICPF_API*/ callback3 : public callback_list +{ protected: - std::list<_CALLBACKDATA2> m_lCalls; ///< List of the callback structures to execute - mutex m_lock; ///< Mutex for locking connect/disconnect calls + /// Callback2-type callback function + typedef R(*PFNCALLBACKPROC3)(ptr_t, P1, P2, P3); + +public: +/** \name Construction/destruction */ +/**@{*/ + callback3() { }; ///< Standard constructor + ~callback3() { }; ///< Standard destructor +/**@}*/ + +/** \name User interface */ +/**@{*/ + /** Executes a callback list associated with this object. + * \param[in] data1 - parameter that will be passed to a user callback function + * \param[in] data2 - parameter that will be passed to a user callback function + * \param[in] data3 - parameter that will be passed to a user callback function + */ + void exec(P1 data1, P2 data2, P3 data3) + { + m_lock.lock(); + CLBDATA* pData; + for (size_t i=0;i != size();i++) + { + pData=at(i); + if (pData) + (*(PFNCALLBACKPROC3)(pData->pfn))(pData->param, data1, data2, data3); + } + m_lock.unlock(); + } + + /** Connects a user callback function to this object. + * \param[in] pfn - user callback function address + * \param[in] appParam - user parameter to pass to the callback function when executing + */ + void connect(PFNCALLBACKPROC3 pfn, ptr_t appParam) + { + add((PFNFUNC)pfn, appParam); + } + + /** Disconnects the user callback function if connected earlier. + * \param[in] pfn - address of a function to remove + */ + void disconnect(PFNCALLBACKPROC3 pfn) + { + remove((PFNFUNC)pfn); + } +/**@}*/ }; END_ICPF_NAMESPACE