M
Martin Herbert Dietze
Hi,
in a project I am using callbacks which are called like
ordinary functions but in fact are Loki::Functor's
encapsulating calls to non-static member functions on instances
of different classes.
The approach using Loki::Functor looks like this:
| class X {
| public:
| void foo(int) { }
| };
|
| X x;
|
| typedef Loki::Functor<void, TYPELIST_1(int)> Functor1;
| Functor1 f1(&x, &X::foo);
|
| f1(42); // x disappears, f can be called like a function
The nice thing about it is you do not need to template the
actual type of the object on which you want to call the
member function.
Using boost::function1 and boost::bind you can come close to
this:
| template <class C, typename R, typename A1> class MyFunction
| : public boost::function1 <R, A1> {
| public:
| MyFunction(C *c, R (C::*f)(A1)) {
| boost::function1 <R, A1> t = boost::bind(f, c, _1);
| swap(t);
| }
| };
|
| template<class C>struct A {
| typedef MyFunction<C, void, int> f2(x, &X::foo) Functor2;
| };
|
| A::Functor2<X> f2(&x, &X::foo);
This does not only look more complicated, it is also less
flexible. While with Loki::Functor I can use my function object
on any member function of any class provided the signature
fits, my Functor2 template will only work with class X.
I would like to avoid this dependency since if using the
current boost-based approach I would have to make a large number
of classes using this functor class templates, too.
On the other hand I would like to reduce the number of
third-party libraries my project depends on, and from the
Loki package I am currently only using the Functor class, so
that I would actually prefer to replace its functionality by
something else.
Any idea?
Cheers,
Martin
in a project I am using callbacks which are called like
ordinary functions but in fact are Loki::Functor's
encapsulating calls to non-static member functions on instances
of different classes.
The approach using Loki::Functor looks like this:
| class X {
| public:
| void foo(int) { }
| };
|
| X x;
|
| typedef Loki::Functor<void, TYPELIST_1(int)> Functor1;
| Functor1 f1(&x, &X::foo);
|
| f1(42); // x disappears, f can be called like a function
The nice thing about it is you do not need to template the
actual type of the object on which you want to call the
member function.
Using boost::function1 and boost::bind you can come close to
this:
| template <class C, typename R, typename A1> class MyFunction
| : public boost::function1 <R, A1> {
| public:
| MyFunction(C *c, R (C::*f)(A1)) {
| boost::function1 <R, A1> t = boost::bind(f, c, _1);
| swap(t);
| }
| };
|
| template<class C>struct A {
| typedef MyFunction<C, void, int> f2(x, &X::foo) Functor2;
| };
|
| A::Functor2<X> f2(&x, &X::foo);
This does not only look more complicated, it is also less
flexible. While with Loki::Functor I can use my function object
on any member function of any class provided the signature
fits, my Functor2 template will only work with class X.
I would like to avoid this dependency since if using the
current boost-based approach I would have to make a large number
of classes using this functor class templates, too.
On the other hand I would like to reduce the number of
third-party libraries my project depends on, and from the
Loki package I am currently only using the Functor class, so
that I would actually prefer to replace its functionality by
something else.
Any idea?
Cheers,
Martin