Loki-like functors with boost::function?

Discussion in 'C++' started by Martin Herbert Dietze, Mar 30, 2006.

  1. 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

    --
    Ruft man einen Hund, dann kommt er. Ruft man eine Katze, dann nimmt
    sie das zur Kenntnis, und kommt gelegentlich darauf zurueck.
    -=-=- -=-=-=-=- --=-=-
    Martin Dietze -=-=- http://www.the-little-red-haired-girl.org
     
    Martin Herbert Dietze, Mar 30, 2006
    #1
    1. Advertising

  2. Martin Herbert Dietze

    Jeff Flinn Guest

    Martin Herbert Dietze wrote:
    > 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:


    Why not just:

    typdef boost::function1<void,int> Functor1;

    Functor1 f1( boost::bind( &x::for, &x ) );

    f1(42);

    Jeff
     
    Jeff Flinn, Mar 30, 2006
    #2
    1. Advertising

  3. Jeff Flinn <> wrote:

    > Why not just:
    >
    > typdef boost::function1<void,int> Functor1;
    >
    > Functor1 f1( boost::bind( &x::for, &x ) );
    >
    > f1(42);


    This would add boost-specific code to dozens of modules
    throughout the project. I would like to encapsulate this in a
    class which does that magic for the user. However I have not
    got around explicitly having to specify the class X as a
    template argument.

    Cheers,

    Martin

    --
    There's only one serious question. And that is: Who knows how to make love stay?
    Answer me that and I will tell you whether or not to kill yourself. Answer me
    that and I will ease your mind about the beginning and the end of time. Answer
    me that and I will reveal to you the purpose of the moon. -- Tom Robbins
    -=-=- -=-=-=-=- --=-=-
    Martin Dietze -=-=- http://www.the-little-red-haired-girl.org
     
    Martin Herbert Dietze, Mar 30, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sebastian Faust

    Loki::Factory and Loki::TList

    Sebastian Faust, Oct 30, 2003, in forum: C++
    Replies:
    0
    Views:
    694
    Sebastian Faust
    Oct 30, 2003
  2. Loki and BOOST

    , Jan 12, 2006, in forum: C++
    Replies:
    1
    Views:
    410
    Pavel Vozenilek
    Jan 13, 2006
  3. leaf
    Replies:
    2
    Views:
    322
  4. mlt

    Function functors?

    mlt, Aug 28, 2008, in forum: C++
    Replies:
    3
    Views:
    295
    Kai-Uwe Bux
    Aug 28, 2008
  5. Christopher
    Replies:
    1
    Views:
    839
    Yakov Gerlovin
    Oct 5, 2011
Loading...

Share This Page