Boost logo

Boost Users :

From: Kerner, Jacques (jkerner_at_[hidden])
Date: 2005-11-24 20:27:03


Erratum : Please replace ReturnArg by AdaptToReturningFunction in the
AdaptToReturningFunction class definition.

JK

-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Kerner,
Jacques
Sent: Thursday, November 24, 2005 4:54 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Bind,lambda and how to chain relative
function calls

Hi,

My example got a little bit more complicated. The Car::GetRigidBody
function is :

void Car::GetRigidBody(RigidBody & rb)

instead of

RigidBody & Car::GetRigidBody()

So I have to write :

bind (
        &Position::GetX,
        bind (
                  &RigidBody::GetPosition,
 
AdaptToReturningFunction<RigidBody>(bind(&Car::GetRigidBody, &car))
              )
      )

With a functor named AdaptToReturningFunction that transforms the first
signature to the second :

template<typename T>
class AdaptToReturningFunction
{
public:
        ReturnArg(boost::function<void (T &)> f) : mF(f) {}
        T operator()() {T t; mF(t); return t;}
private:
        boost::function<void (T &)> mF;
};

Is there a simpler and better way of doing it without having to define
AdaptToReturningFunction ?

What if RigidBody was purely abstract (interface) ?

Thank you.

Jacques Kerner.

-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Peter Dimov
Sent: Thursday, November 24, 2005 3:41 AM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Bind,lambda and how to chain relative
function calls

Kerner, Jacques wrote:
> Hi,
>
> I would like to create a functor for calling functions in a chain like

> this :
>
> car.GetRigidBody().GetPosition().GetX()
>
> What is the best way to do that using bind (and lambda ?)
>
> The car.GetRigidBody() is easy : bind(&Car::GetRigidBody, &car) , but
> then I am a bit stuck. Note that I don't want to store the result of
> car.GetRigidBody(). I want the final functor to call all the functions

> everytime its operator() is called. So
>
>
> bind (
> &Position::GetX,
> &bind (
> &RigidBody::GetPosition,
> &bind (
>
> &Car::GetRigidBody,
> &car
> )()
> )()
> )
>
> would not be acceptable...

Just drop the invocations:

bind (
    &Position::GetX,
    bind (
        &RigidBody::GetPosition,
        bind( &Car::GetRigidBody, &car)
    )
)

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net