Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a general member function pointer

I have created a Timer class that must call a callback method when the timer has expired. Currently I have it working with normal function pointers (they are declared as void (*)(void), when the Elapsed event happens the function pointer is called.

Is possible to do the same thing with a member function that has also the signature void (AnyClass::*)(void)?

Thanks mates.

EDIT: This code has to work on Windows and also on a real-time OS (VxWorks) so not using external libraries would be great.

EDIT2: Just to be sure, what I need is to have a Timer class that take an argument at the Constructor of tipe "AnyClass.AnyMethod" without arguments and returning void. I have to store this argument and latter in a point of the code just execute the method pointed by this variable. Hope is clear.

like image 710
Ignacio Soler Garcia Avatar asked Jan 27 '10 14:01

Ignacio Soler Garcia


4 Answers

Dependencies, dependencies... yeah, sure boost is nice, so is mem_fn, but you don't need them. However, the syntax of calling member functions is evil, so a little template magic helps:

   class Callback
   {
   public:
      void operator()() { call(); };
      virtual void call() = 0;
   };

   class BasicCallback : public Callback
   {
      // pointer to member function
      void (*function)(void);
   public:
      BasicCallback(void(*_function)(void))
          : function( _function ) { };
      virtual void call()
      { 
          (*function)();
      };
   };   

   template <class AnyClass> 
   class ClassCallback : public Callback
   {
      // pointer to member function
      void (AnyClass::*function)(void);
      // pointer to object
      AnyClass* object;        
   public:
      ClassCallback(AnyClass* _object, void(AnyClass::*_function)(void))
          : object( _object ), function( _function ) { };
      virtual void call()
      { 
          (*object.*function)();
      };
   };

Now you can just use Callback as a callback storing mechanism so:

void set_callback( Callback* callback );
set_callback( new ClassCallback<MyClass>( my_class, &MyClass::timer ) );

And

Callback* callback = new ClassCallback<MyClass>( my_class, &MyClass::timer ) );

(*callback)();
// or...
callback->call();
like image 62
Kornel Kisielewicz Avatar answered Oct 16 '22 05:10

Kornel Kisielewicz


The best solution I have used for that same purpose was boost::signal or boost::function libraries (depending on whether you want a single callback or many of them), and boost::bind to actually register the callbacks.

class X {
public:
   void callback() {}
   void with_parameter( std::string const & x ) {}
};
int main()
{
   X x1, x2;
   boost::function< void () > callback1;

   callback1 = boost::bind( &X::callback, &x1 );
   callback1(); // will call x1.callback()

   boost::signal< void () > multiple_callbacks;
   multiple_callbacks.connect( boost::bind( &X::callback, &x1 ) );
   multiple_callbacks.connect( boost::bind( &X::callback, &x2 ) );
   // even inject parameters:
   multiple_callbacks.connect( boost::bind( &X::with_parameter, &x1, "Hi" ) );

   multiple_callbacks(); // will call x1.callback(), x2.callback and x1.with_parameter("Hi") in turn
}
like image 34
David Rodríguez - dribeas Avatar answered Oct 16 '22 06:10

David Rodríguez - dribeas


Maybe the standard mem_fun is already good enough for what you want. It's part of STL.

like image 35
Maurits Rijk Avatar answered Oct 16 '22 05:10

Maurits Rijk


boost::function looks like a perfect fit here.

like image 44
Nikolai Fetissov Avatar answered Oct 16 '22 05:10

Nikolai Fetissov