Reputation: 67234
Expanding on this question, it looks like I did not provide enough detail.
I have an object called CallbackObject
that is designed to contain an object instance, and the information of what function to invoke, and the parameters to pass at invokation time.
template <typename objectType, typename memberFunctionPtrType, typename memberFcnArg1Type> struct CallbackObject1 { objectType obj ; memberFunctionPtrType fcnPtr ; memberFcnArg1Type arg1 ; CallbackObject1(objectType iObj, memberFunctionPtrType iFcnPtr, memberFcnArg1Type iArg1 ) { obj = iObj ; fcnPtr = iFcnPtr ; arg1 = iArg1 ; } void exec() { (obj.*fcnPtr)( arg1 ) ; } } ;
struct Point { float x,y ; void print( int numTimes ) { for( int i = 0 ; i < numTimes ; i++ ) printf( "%f %f\n", x, y ) ; } } ; typedef void (Point::* PointPrintFcnType)( int ) ; int main() { Point p ; p.x=p.y=1; CallbackObject1<Point, PointPrintFcnType, int> ocall( p, &Point::print, 5 ); ocall.exec() ; }
The only problem I'm having is if objectType
is a pointer type, then (obj.*fcnPtr)
fails, since it should really read ( (*obj).*fcnPtr)
or (obj->*fcnPtr)
if obj is a pointer.
Now I have one solution, where I define another CallbackObject class like so:
template <typename pObjectType, typename memberFunctionPtrType, typename memberFcnArg1Type> struct CallbackPObject1 { pObjectType obj ; memberFunctionPtrType fcnPtr ; memberFcnArg1Type arg1 ; CallbackPObject1(pObjectType iObj, memberFunctionPtrType iFcnPtr, memberFcnArg1Type iArg1 ) { obj = iObj ; fcnPtr = iFcnPtr ; arg1 = iArg1 ; } void exec() { (obj->*fcnPtr)( arg1 ) ; } } ;
But this is crufty at best, and difficult to use at worst, if someone else is using this code, they will have to create a different kind of Callback object if the object type being used is actually a pointer.
Is there any way around this?
Upvotes: 0
Views: 104
Reputation: 36497
Here's an example helper function, assuming void return, one argument, and no need to handle more than one level of indirection:
template <typename T, typename F, typename A>
inline void invoke(T& obj, F func, const A& arg)
{
(obj.*func)(arg);
}
template <typename T, typename F, typename A>
inline void invoke(T* obj, F func, const A& arg)
{
(obj->*func)(arg);
}
If you need to handle more than one level of indirection, you can replace the second function with this:
template <typename T, typename F, typename A>
inline void invoke(T* obj, F func, const A& arg)
{
invoke(*obj, func, arg);
}
This will recursively strip off levels of indirection until you end up with something that you can invoke your member function on.
You can then write your exec()
function like so:
void exec()
{
invoke(obj, fcnPtr, arg1);
}
Upvotes: 1
Reputation: 67234
Ok, applying the "overload" approach on the previous question I get
template <typename objectType, typename memberFunctionPtrType, typename memberFcnArg1Type> struct CallbackObject1 : public Callback { objectType obj ; objectType *pObj ; memberFunctionPtrType fcnPtr ; memberFcnArg1Type arg1 ; CallbackObject1(objectType iObj, memberFunctionPtrType iFcnPtr, memberFcnArg1Type iArg1 ) { obj = iObj ; pObj = 0 ; fcnPtr = iFcnPtr ; arg1 = iArg1 ; } CallbackObject1(objectType *iObj, memberFunctionPtrType iFcnPtr, memberFcnArg1Type iArg1 ) { pObj = iObj ; fcnPtr = iFcnPtr ; arg1 = iArg1 ; } void exec() { if( pObj ) (pObj->*fcnPtr)( arg1 ) ; else (obj.*fcnPtr)( arg1 ) ; } } ;
typedef void (Point::* PointPrintFcnType)( int ) ; Point p ; p.x=p.y=1; CallbackObject1<Point, PointPrintFcnType, int> ocall( p, &Point::print, 5 ); ocall.exec() ; CallbackPObject1<Point*, PointPrintFcnType, int> ocallP( &p, &Point::print, 2 ); ocallP.exec() ;
It works reasonably ok, but its not as clean inside as I would like.
Upvotes: 0
Reputation: 146930
Use a polymorphic function object such as boost::function
in combination with functor-producing functions like boost::bind
. These are a far superior solution- the genius happens when the functor is produced, not when it's called, and any function object that can be called with the correct signature may be called.
Upvotes: 0