Passing an object's method as a callback

Z

zzorozz

I often find that I need to pass a non-static method as a call-back
instead of a functor or a function. The problem with that is the
implicit 'this' parameter. So I created an adaptor that given an
object and a unary method creates a unary_function-defived functor
bound to that object and method. Code is below.

// Method-to-functor adapter


template<class TClass, typename TArg, typename TRet>
class unary_method : public unary_function<TArg, TRet>
{
public:
unary_method(TClass& object,
TRet (TClass::*method)(TArg)) :
_object(object),
_method(method)
{}

TRet operator()(TArg arg) {
(_object.*_method)(arg);
}

private:
TClass& _object;
TRet (TClass::*_method)(TArg);
};

Questions:
Is this the best (simplest, most efficient) way to do it?
Isn't this a common enough problem for the solution to be available? I
searched, but found nothing in stl, boost or anywhere else.

Here's a test driver, in case you want to give it a try:
<code>
#include <iostream>
#include <functional>

using namespace std;

// Method-to-functor adapter


template<class TClass, typename TArg, typename TRet>
class unary_method : public unary_function<TArg, TRet>
{
public:
unary_method(TClass& object,
TRet (TClass::*method)(TArg)) :
_object(object),
_method(method)
{}

TRet operator()(TArg arg) {
(_object.*_method)(arg);
}

private:
TClass& _object;
TRet (TClass::*_method)(TArg);
};


class MyClass
{
public:
void MyMethod(int a) {
cout << a << endl;
};

static void MyStaticMethod(int a) {
cout << a << endl;
};
};


main () {
void (*foo)(int) = &MyClass::MyStaticMethod;
foo(3);

MyClass my_object;
unary_method<MyClass, int, void> bar(my_object,
&MyClass::MyMethod);
bar(5);
}
</code>

Regards,
Zori
 
R

red floyd

I often find that I need to pass a non-static method as a call-back
instead of a functor or a function. The problem with that is the
implicit 'this' parameter. So I created an adaptor that given an
object and a unary method creates a unary_function-defived functor
bound to that object and method. Code is below.

[code redacted]

Questions:
Is this the best (simplest, most efficient) way to do it?
Isn't this a common enough problem for the solution to be available? I
searched, but found nothing in stl, boost or anywhere else.

have you looked at std::mem_fun and std::mem_fun_ptr?
 
T

Thomas J. Gritzan

I often find that I need to pass a non-static method as a call-back
instead of a functor or a function. The problem with that is the
implicit 'this' parameter. So I created an adaptor that given an
object and a unary method creates a unary_function-defived functor
bound to that object and method. Code is below.

// Method-to-functor adapter [...]
Questions:
Is this the best (simplest, most efficient) way to do it?
Isn't this a common enough problem for the solution to be available? I
searched, but found nothing in stl, boost or anywhere else.

boost::bind and boost::function, which will be in the next standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top