|
Boost Users : |
Subject: Re: [Boost-users] [fusion?] [Phoenix?] Calling arbitrary method with collection of args
From: Kim Kuen Tang (kuentang_at_[hidden])
Date: 2010-03-19 10:33:42
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 <boost/cstdlib.hpp>
# include <boost/type_traits/function_traits.hpp>
# include <boost/preprocessor/repetition.hpp>
template<int n>
struct callme
{
typedef int result_type;
BOOST_STATIC_CONSTANT(int, arity = n);
int operator()() const{return 0;}
# define CALLME_params(z,n,data) BOOST_PP_CAT(T,n) const& BOOST_PP_CAT(t,n)
# define CALLME_op(z,n,data) \
template<BOOST_PP_ENUM_PARAMS(n,typename T)> \
int operator()(BOOST_PP_ENUM(n,CALLME_params,~)) const \
{ \
return n; \
}
BOOST_PP_REPEAT_FROM_TO(1, 3, CALLME_op, ~)
# undef CALLME_op
# undef CALLME_params
};
template <typename CALLABLE, typename LLSD>
typename 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 (typename CALLABLE::arity == 0)
{
return callable();
}
if (typename CALLABLE::arity == 1)
{
return callable(args[0]);
}
if (typename CALLABLE::arity == 2)
{
return callable(args[0], args[1]);
}
}
template <typename CALLABLE, typename LLSD>
typename CALLABLE::result_type
apply1(const CALLABLE& callable, const LLSD& args)
{
# define APPLY_params(z,n,data) args[n]
# define APPLY_func(z,n,data) \
if (typename CALLABLE::arity == n) \
{ \
return callable(BOOST_PP_ENUM(n,APPLY_params,~) ); \
}
BOOST_PP_REPEAT_FROM_TO(0,3,APPLY_func,~)
}
int main()
{
callme<2> c;
int ar[3];
int a = apply(c,ar);
int b = apply1(c,ar);
return boost::exit_success;
}
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