/* name: stk_callback.h info: component's callback functions (global or methods), using templates - header file author: Dusan Halicky (dvh.tosomja@post.sk) licence: GNU GPL history: 2005-09-26 - start of developement todo: - bugs: - */ #ifndef STK_CALLBACK_H #define STK_CALLBACK_H // basic callback class, abstract, parent of global-function/class-method class StkBasicCallback { public: StkBasicCallback() { }; virtual ~StkBasicCallback() { }; virtual void Execute () = 0; // virtual bool Equal (StkBasicCallback * cb) = 0; }; // template for method callback template < class T > class StkCallback : public StkBasicCallback { typedef void (T::*StkCallbackFuncPtr)(); // internal type "StkCallbackFuncPtr" is pointer on method of any object private: T* objekt; // to make call the method (of some object) possible, we need to know the object, we store it here, it will be "this" StkCallbackFuncPtr funkcia; // declaration of variable which point on method of some object public: StkCallback(T* o,StkCallbackFuncPtr f) : objekt(o), funkcia(f) {}; // constructor void Execute() { (objekt->*funkcia)(); }; // call the method of "object" on which point variable "funkcia" }; // special template (descendant of StkBasicCallback) which can be used for using global functions as callbacks /* note: why it is not created via template? Because StkBasicCallback is abstract class designed to cover more "stuff" (in the order of OOP). StkCallback is for objects (classes) where we need to remember parameter "this" of that object which call it. In global functions, there is no parameter "this" so we don't need template for constructing callback. */ class StkGlobalCallback : public StkBasicCallback { // definition of internal type which point on the function typedef void (*StkCallbackFuncPtr)(); // pointer type which point to function which do not return any value private: StkCallbackFuncPtr funkcia; // definition of variable which point to the function public: StkGlobalCallback(StkCallbackFuncPtr f) : funkcia(f) {}; // constructor, content only one parameter - callback void Execute() { (*funkcia)(); }; // calling that funcion }; #endif // STK_CALLBACK_H