0
votes

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 ) ;
  }
} ;

Example of use:

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?

3
Any of the answers provided in your first question would've worked, including partial specialization (your second class could've been a partial specialization of the first) and using an overloaded function as a helper function to actually perform the call.John Calsbeek
How will they work for a class?bobobobo
Exactly the same way. Your problem is that it's difficult to write the expression in exec(). If you use Johannes' or my answer, it provides a helper function that you can use to write that statement in exec() generically. Said helper functions could just be global functions or private member functions.John Calsbeek
Yeah, it is difficult to write exec(). I copped out with an if statement, see my answer below. How else could you write exec?bobobobo

3 Answers

1
votes

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);
}
0
votes

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.

0
votes

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 ) ;
  }
} ;

Use

  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.