Boost logo

Boost Users :

Subject: Re: [Boost-users] [lambda] Return types in lambda expressions
From: Johan Nilsson (r.johan.nilsson_at_[hidden])
Date: 2009-04-27 06:35:47


"Michael Crawford" <mbcrawfo_at_[hidden]> wrote in message
news:9f5be0740904260914sf375ddnfef0f1bb8bb62707_at_mail.gmail.com...
>I'm trying to use bind and lambda to create a functor that can receive an
>event generated by my GUI library, and dispatch an appropriate event into a
> >boost::statechart::state_machine. The GUI library requires the functor to
>have the signature bool (const CEGUI::EventArgs&). An example of what
>works:
>template<typename EventType>
>bool GuiEvent(const CEGUI::EventArgs&, GameStateMachine& gsm)
>{
> gsm.process_event(EventType( ));
> return true;
>}

>//usage:
>GameStateMachine sm;
>...
>boost::bind(&GuiEvent<EventExitGame>, _1, boost::ref(sm));
>But is it possible to use lambda to eliminate the need for the function?
>It looks like I would use something along the lines of
>boost::bind<bool>(boost::ref(sm).process_event(EventExitGame( )) /*somehow
>return true here*/, _1);

As you don't provide the call signature of the callback registration method
(which should be there somewhere) this is probably not entirely correct, but
something like this should be possible (beware of typos):

void foo_add_callback(::boost::function0<bool> const& cb)
{ ... }

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

...

namespace lambda = ::boost::lambda;

...

foo_add_callback((
  lambda::bind(&GameStateMachine::process_event<EventExitGame>,
lambda::var(sm), EventExitGame()),
  true
));

(note extra parens and the usage of the comma operator)

/ Johan


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