[fusion?] [Phoenix?] Calling arbitrary method with collection of args

We have a data type equivalent to a recursive variant that can hold a few scalar types or a vector of such types. My colleague proposes that we build a template function that will accept a free function or class method (that is, any callable whose signature we can infer) plus a vector of the variant type. For each formal parameter type in the callable's signature, this function would ensure that the vector is long enough to fill it, and then that the runtime type of the appropriate vector entry is compatible with the static type of the parameter. Finally it would call the callable, filling the formal parameters from the variant vector. I strongly suspect that this function already exists in Boost. I simply don't know which library to research. (OvermindDL1?)

Hi Nat, You can use push_back from phoenix to fill in your data type. See http://www.boost.org/doc/libs/1_42_0/libs/spirit/phoenix/doc/html/phoenix/co... Perhaps you can show us your data type so we can go into detail. Cheers, Kim Nat Goodspeed schrieb:
We have a data type equivalent to a recursive variant that can hold a few scalar types or a vector of such types.
My colleague proposes that we build a template function that will accept a free function or class method (that is, any callable whose signature we can infer) plus a vector of the variant type. For each formal parameter type in the callable's signature, this function would ensure that the vector is long enough to fill it, and then that the runtime type of the appropriate vector entry is compatible with the static type of the parameter. Finally it would call the callable, filling the formal parameters from the variant vector.
I strongly suspect that this function already exists in Boost. I simply don't know which library to research. (OvermindDL1?) _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Kim Kuen Tang wrote:
You can use push_back from phoenix to fill in your data type. See http://www.boost.org/doc/libs/1_42_0/libs/spirit/phoenix/doc/html/phoenix/co...
Perhaps you can show us your data type so we can go into detail.
I'm talking about the LLSD class: http://svn.secondlife.com/trac/linden/browser/trunk/indra/llcommon/llsd.h I'm confused by your answer, though, so I probably asked the question badly. I'm looking for something equivalent to this pseudocode: template <typename CALLABLE> typename boost::function_traits<CALLABLE>::result_type apply(const CALLABLE& callable, const LLSD& args) { // 'args' is already filled // for present purposes, assume we know it contains an array // call 'callable', supplying its args from that array if (boost::function_traits<CALLABLE>::arity == 0) { return callable(); } if (boost::function_traits<CALLABLE>::arity == 1) { return callable(args[0]); } if (boost::function_traits<CALLABLE>::arity == 2) { return callable(args[0], args[1]); } // ...etc. to some max arity... } I could possibly write such code, using partial template specialization on boost::function_traits<CALLABLE>::arity instead of a sequence of 'if' statements. My point is that I assume Boost already contains something like this. I'm just not sure where to look. Thanks for any pointers to the appropriate documentation.

Hi Nat, it's me, who doesnt understand your question. Nat Goodspeed schrieb:
I'm confused by your answer, though, so I probably asked the question badly.
This badly question is better to me.
I'm looking for something equivalent to this pseudocode:
I could possibly write such code, using partial template specialization on boost::function_traits<CALLABLE>::arity instead of a sequence of 'if' statements.
My point is that I assume Boost already contains something like this. I'm just not sure where to look. Thanks for any pointers to the appropriate documentation.
From my opinion i 'll say that there is no ready-to-use function in boost.
You can give preprocessor a try:
# include

Kim Kuen Tang wrote:
From my opinion i 'll say that there is no ready-to-use function in boost.
Hmm! Further digging turned up boost::fusion::invoke(). The following
short program builds and runs as expected using Boost 1.39.0 with gcc 4.0.1.
The Fusion documentation says that support for boost::array is entirely
implemented with the non-intrusive Fusion extension mechanism. Hopefully
it won't be too hard to adapt the boost::array extension for our own
container. (I'm a bit surprised not to find existing support for
std::vector; presumably that's been done a few times by individuals.)
#include <iostream>
#include

Nat Goodspeed schrieb:
I'm a bit surprised not to find existing support for std::vector; presumably that's been done a few times by individuals.
The size of std:::vector is not an mpl integral constant ( not known in compile time). So std::vector is not a sequence in the sense of fusion. See http://boost-spirit.com/dl_more/fusion_v2/libs/fusion/doc/html/fusion/sequen... Cheers, Kim

Kim Kuen Tang wrote:
Nat Goodspeed schrieb:
I'm a bit surprised not to find existing support for std::vector; presumably that's been done a few times by individuals.
The size of std:::vector is not an mpl integral constant ( not known in compile time). So std::vector is not a sequence in the sense of fusion. See http://boost-spirit.com/dl_more/fusion_v2/libs/fusion/doc/html/fusion/sequen...
Then maybe boost::fusion::invoke() isn't what I'm looking for. Nonetheless it comes frustratingly close: the preprocessor iteration to handle various arities is already done. (In fact handling some number of C++ arities is (re)implemented in a surprising number of different Boost libraries.) Presumably Boost.Python has internal machinery very like what I want, receiving an arbitrary sequence of Python variant objects and using them to populate the parameter list of some C++ callable. Of course I don't want to start basing production code on an undocumented implementation detail, though. Still looking for a Boost feature to apply (e.g.) a std::vector of parameter values to a C++ callable... thanks for additional suggestions.

On Fri, Mar 19, 2010 at 11:24 AM, Nat Goodspeed
Kim Kuen Tang wrote:
Nat Goodspeed schrieb:
I'm a bit surprised not to find existing support for std::vector; presumably that's been done a few times by individuals.
The size of std:::vector is not an mpl integral constant ( not known in compile time). So std::vector is not a sequence in the sense of fusion. See
http://boost-spirit.com/dl_more/fusion_v2/libs/fusion/doc/html/fusion/sequen...
Then maybe boost::fusion::invoke() isn't what I'm looking for.
Nonetheless it comes frustratingly close: the preprocessor iteration to handle various arities is already done.
(In fact handling some number of C++ arities is (re)implemented in a surprising number of different Boost libraries.)
Presumably Boost.Python has internal machinery very like what I want, receiving an arbitrary sequence of Python variant objects and using them to populate the parameter list of some C++ callable. Of course I don't want to start basing production code on an undocumented implementation detail, though.
Still looking for a Boost feature to apply (e.g.) a std::vector of parameter values to a C++ callable... thanks for additional suggestions.
Actually I have done that same kind of thing as what you are wanting with my RPC library using Boost::Fusion. Statically you build up a recursive functor that creates the fusion sequence that matches the function, and dynamically you populate it with the vector elements and invoke it (or throw an exception if you have too many or too few vector elements?). It is actually quite easy and Boost has an example of that in the boost::function_types examples.
participants (3)
-
Kim Kuen Tang
-
Nat Goodspeed
-
OvermindDL1