template<class Request, class Result>
class ca_mgm::CallbackBase< Request, Result >
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:
- Declare and implement one or two classes that will be used as Request (input parameters) and the Result (return value) types by the callback method. For example:
* class DoitCBMsg
* {
* private:
* int foo;
* public:
* DoitCBMsg(int arg=0): foo(arg) {}
* DoitCBMsg(const DoitCBMsg &msg): foo(msg.foo) {}
* ~DoitCBMsg() {}
* int getFoo() { return foo; }
* };
*
- Declare and implement your function using a callback with the previously declared Request and Result types:
*
* typedef CallbackBase<DoitCBMsg,DoitCBMsg> DoitCB;
*
*
* int doit(DoitCB *cb)
* {
* if(cb)
* {
* DoitCBMsg request(42);
* DoitCBMsg *result = NULL;
* try
* {
* result = cb->call(&request);
* }
*
* }
* return 1;
* }
*
- The user of the function using a callback has to inherit from the specialized callback class (DoitCB in the example) and implement the abstract callback() method and pass an instance of the derived class to the function. For example:
*
* class MyDoitCB: public DoitCB
* {
* private:
* int m_data;
*
* public:
* MyDoitCB(int data): DoitCB(), m_data(data) {}
* ~MyDoitCB() {}
*
*
* virtual Result *
* {
* if( request)
* return new Result(request->getFoo() * m_data);
* else
* return new Result();
* }
* };
*
* int main(void)
* {
*
* MyDoitCB cb(2);
*
*
* int ret = doit(&cb);
*
* return 0;
* }
*
- Todo:
Implement a CallbackRef holding a reference counted (specialized?) Callback object...
Implement a call() method variant using Request/Result references?