Boost logo

Boost Users :

Subject: Re: [Boost-users] [xpressive] Calling a placeholder method from within an action?
From: Eric Niebler (eric_at_[hidden])
Date: 2009-10-08 09:43:48


Ami Ganguli wrote:
> Hi all,
>
> I'm using an object as a placeholder, and would like to call various
> methods to the object from within my actions. Something along the
> lines of:
>
>
> port = (s1= *digit)
> [ _placeholder->setPort( as<int>(s1) ) ];

I'm not exactly sure what _placeholder is here, or what you mean by
"using an object as a placeholder".

> I've found a way to do something equivalent using method pointers, but
> the resulting code is really opaque. I'm afraid nobody else will ever
> be able to figure out what I've done.
>
> I also know I can do this by creating separate function objects for
> each method that will be called. This is less opaque, but really
> verbose. I end up with hundreds of lines of boilerplate code.
>
> Is there a "right" way to achieve something similar?

I'm afraid not. But if you go the route of creating function object
wrappers, then you can get a nice syntax at the call site:

struct setPortFun
{
   typedef X result_type;
   X operator()(Object & obj, int arg) const
   { return obj.setPort(arg); }
};

xpr::function<setPortFun>::type setPort const = {{}};

    port = (s1= *digit)
       [ xpr::ref(obj)->*setPort( as<int>(s1) ) ];

Xpressive recognizes this use of ->* and treats it as:

   setPort( xpr::ref(obj), as<int>(s1) )

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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