LiMaL callback interface base class. More...
#include <CallbackBase.hpp>
Public Member Functions | |
CallbackBase () | |
virtual | ~CallbackBase () |
virtual Result * | call (const Request *request) |
Protected Member Functions | |
virtual Result * | callback (const Request *request)=0 |
Private Member Functions | |
CallbackBase (const CallbackBase &) | |
CallbackBase & | operator= (const CallbackBase &) |
LiMaL callback interface base class.
The abstract CallbackBase template provides the callback interface supported in LiMaL.
The interface uses only one callback function signature (see callback() method), but with definable return code and input parameter data-types (Request,Result). Both data-types have the requirement to provide a default and (deep) copy constructors. The default constructor can be used to signal default or invalid Request/Result.
The usage of one function signature and the copy constructor make it possible to implement reusable support wrapper templates allowing to implement the callback method in languages other than C++ (e.g. perl or python).
To implement a function (or class) using a callback, following steps are required:
class DoitCBMsg { private: int foo; public: DoitCBMsg(int arg=0): foo(arg) {} DoitCBMsg(const DoitCBMsg &msg): foo(msg.foo) {} ~DoitCBMsg() {} int getFoo() { return foo; } };
// specialize the callback interface for Request/Result typedef CallbackBase<DoitCBMsg,DoitCBMsg> DoitCB; // use the specialized callback type in the caller function int doit(DoitCB *cb) { if(cb) { DoitCBMsg request(42); DoitCBMsg *result = NULL; try { result = cb->call(&request); } // ... } return 1; }
// implement callback derived from specialized class class MyDoitCB: public DoitCB { private: int m_data; // some internal data if / as required public: MyDoitCB(int data): DoitCB(), m_data(data) {} ~MyDoitCB() {} // implement the callback method and functionality virtual Result * callback(const Request *request) { if( request) return new Result(request->getFoo() * m_data); else return new Result(); } }; int main(void) { // create callback instance MyDoitCB cb(2); // pass it to the function using it int ret = doit(&cb); return 0; }
Implement a CallbackRef holding a reference counted (specialized?) Callback object...
Implement a call() method variant using Request/Result references?
limal::CallbackBase< Request, Result >::CallbackBase | ( | ) | [inline] |
Default constructor.
virtual limal::CallbackBase< Request, Result >::~CallbackBase | ( | ) | [inline, virtual] |
Destructor.
limal::CallbackBase< Request, Result >::CallbackBase | ( | const CallbackBase< Request, Result > & | ) | [private] |
Copying not allowed.
virtual Result* limal::CallbackBase< Request, Result >::call | ( | const Request * | request | ) | [inline, virtual] |
Call method for the caller function executing a callback.
request | Read-Only pointer to the callback request. |
std::bad_alloc | and maybe other, callback() method implementation specific exceptions. |
virtual Result* limal::CallbackBase< Request, Result >::callback | ( | const Request * | request | ) | [protected, pure virtual] |
Callback method that has to be implemented by the user and delivers the result for to the request back to the caller.
request | Read-Only pointer to the callback request. |
std::bad_alloc | and maybe other, implementation specific exceptions. |
CallbackBase& limal::CallbackBase< Request, Result >::operator= | ( | const CallbackBase< Request, Result > & | ) | [private] |
Copying not allowed.