Boost logo

Boost :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2003-10-14 04:22:37


> From: boost-bounces_at_[hidden]
> [mailto:boost-bounces_at_[hidden]] On Behalf Of Darren Cook

[...] (all of the above noted)

> If anyone wants to take my below example and show be how I'd use
> Boost.Preprocessor to simplify it I'd greatly appreciate it.
> Maybe it could
> then be copied and pasted directly into a FAQ page of the docs :-).

Hi Darren, the following implements the overloads up to ARITY pairs of
parameters:

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/iteration/local.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>

#define ARITY 3

class some_class {
    public:
        void add_ifthere(int d, int v) { /**/ }
        void add_normal(int d, int v) { /**/ }
        void add_only(int d, int v) { /**/ }

        // generates n-th parameter pair, as in:
        // int d0, int v0

        #define params(z, n, _) \
            int BOOST_PP_CAT(d, n), int BOOST_PP_CAT(v, n) \
            /**/

        // generates the n-th call to "func", as in:
        // add_ifthere(d0, v0);

        #define call(z, n, func) \
            func(BOOST_PP_CAT(d, n), BOOST_PP_CAT(v, n)); \
            /**/

        // generates a function definition of "func"
        // with "arity" parameter pairs, as in:
        // void add_ifthere(int d0, int v0, int d1, int v1) {
        // add_ifthere(d0, v0);
        // add_ifthere(d1, v1);
        // }

        #define make_function(arity, func) \
            void func(BOOST_PP_ENUM(arity, params, ~)) { \
                BOOST_PP_REPEAT(arity, call, func); \
            } \
            /**/

        // [macro repeated by local iteration]
        // makes a copy of each function for logical arity n

        #define BOOST_PP_LOCAL_MACRO(n) \
            make_function(n, add_ifthere) \
            make_function(n, add_normal) \
            make_function(n, add_only) \
            /**/

        // [boundaries of local iteration]

        #define BOOST_PP_LOCAL_LIMITS \
            (2, (ARITY) < 2 ? 2 : (ARITY)) \
            /**/

        // repeat BOOST_PP_LOCAL_MACRO with 'n' ranging
        // from 2 to ARITY

        #include BOOST_PP_LOCAL_ITERATE()

        // undefine temporary macros
        // (BOOST_PP_ macros are automatically undefined)

        #undef params
        #undef call
        #undef make_function
};

Regards,
Paul Mensonides


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk