Boost logo

Boost Users :

From: Kevin Jenkins (gameprogrammer_at_[hidden])
Date: 2008-07-22 03:05:20


Thanks!

Jeremy Pack wrote:
> Hi Kevin!
>
> On Mon, Jul 21, 2008 at 9:03 PM, Kevin Jenkins
> <gameprogrammer_at_[hidden] <mailto:gameprogrammer_at_[hidden]>> wrote:
> > Is there any way to automate this, to support from 0 to n parameters?
> >
> > template <class P1, class P2, class P3, class P4, class P5>
> > bool DoIt(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
>
> Boost.Preprocessor would do that.
>
> Create two files, which I'll call:
>
> main.hpp
> implementation.hpp
>
> In main.hpp:
>
> #include <boost/preprocessor/repetition.hpp>
> #include <boost/preprocessor/iteration/iterate.hpp>
>
> #define NUM_FUNCTION_PARAMS 8 // Or however many you need.
>
> // Define the function for 1 to 8 arguments.
> #define BOOST_PP_ITERATION_LIMITS(1, NUM_FUNCTION_PARAMS);
> #define BOOST_PP_FILENAME_1 "implementation.hpp"
> #include BOOST_PP_ITERATE()
>
> And then in implementation.hpp:
>
> // Note: If you want to support 0 arguments, you'll need to use
> BOOST_PP_IF
> // to only optionally include the template declaration. I'd recommend
> just putting the
> // argless version into main.hpp, unless the function is quite complex.
> template <BOOST_PP_ENUM_PARAMS(N, class Param)>
> inline void MyFunc(BOST_PP_ENUM_BINARY_PARAMS(N, Param, p)) {
> #define DO_THAT(n) DoThat<BOOST_PP_CAT(P, n)>(BOOST_PP_CAT(p, n));
> BOOST_PP_REPEAT(N, DO_THAT,)
> #undef DO_THAT
> }
>
> Hope that helps - and can't promise it will immediately compile!
>
> I do this in the proposed Extension and Reflection libraries a few
> times. The best example is probably in the adaptable_factory class,
> where I wanted to support calling a factory function without knowing
> its arguments:
>
> http://svn.boost.org/trac/boost/browser/sandbox/boost/extension/impl/adaptable_factory_free_functions.hpp
> http://svn.boost.org/trac/boost/browser/sandbox/boost/extension/adaptable_factory.hpp
>
> Let me know if you have problems getting your function to compile.
> Also, with Boost.Preprocessor, "gcc -E" is your best friend.
>
> Good luck!
>
> Jeremy Pack
> http://boost-extension.blogspot.com
> /boost-users


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