[xpressive] Calling a placeholder method from within an action?

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'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? Regards, Ami.

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

On Thu, Oct 8, 2009 at 4:43 PM, Eric Niebler
I'm not exactly sure what _placeholder is here, or what you mean by "using an object as a placeholder".
Thanks. I just meant that I was using the placeholder template to pass in an external object. But you seem to have gotten the gist of what I was trying to say anyway :-).
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:
Yeah, I think I've settled on something like that. And I'm going to
resort to a C-style pre-processor define to cut down on the
boilerplate code. Something like (untested):
#define makeFunc( METHOD, PARAM ) \
struct METHOD##func \
{ \
typedef void result_type; \
void operator()(Link *l, PARAM p) const \
{ \
l->METHOD(p); \
} \
} \
function
participants (2)
-
Ami Ganguli
-
Eric Niebler