Boost logo

Boost Users :

Subject: Re: [Boost-users] [Preprocessor] how to use REPEAT_FROM_TO for 2 arguments
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-01-06 15:14:22


AMDG

Kai Liu wrote:
> I am trying to use the preprocessor library to simplify the code I have.
>
> The problem:
> I have many lines of the following assignments:
>
> mybfclass x_0_mybf;
> mybfclass x_1_mybf;
> ...
> mybfclass1 x_0_mybf1;
> mybfclass1 x_1_mybf1;
> ...
> mybfclass2 x_0_mybf2;
> mybfclass2 x_1_mybf2;
>
> then I defined several vectors:
>
> vector<mybfclass*> x_mybf;
> vector<mybfclass1*> x_mybf1;
> vector<mybfclass2*> x_mybf2;
> *
> use the boost preprocessor library, I write the following code:
>
> #define* DECLX_mybf(z, n, text) BOOST_PP_CAT(text, BOOST_PP_CAT(_,
> mybf)).push_back(&BOOST_PP_CAT(text, BOOST_PP_CAT(BOOST_PP_CAT(_, n),
> BOOST_PP_CAT(_, mybf))));
> *#define* DECLX_mybf1(z, n, text) BOOST_PP_CAT(text, BOOST_PP_CAT(_,
> mybf)).push_back(&BOOST_PP_CAT(text, BOOST_PP_CAT(BOOST_PP_CAT(_, n),
> BOOST_PP_CAT(_, mybf))));
> *#define* DECLX_mybf2(z, n, text) BOOST_PP_CAT(text, BOOST_PP_CAT(_,
> mybf)).push_back(&BOOST_PP_CAT(text, BOOST_PP_CAT(BOOST_PP_CAT(_, n),
> BOOST_PP_CAT(_, mybf))));
>
> BOOST_PP_REPEAT_FROM_TO(0, 2, DECLX_mybf, x);
> *// generated: x_mybf.push_back(&x_0_mybf); x_mybf.push_back(&x_1_mybf);
> * BOOST_PP_REPEAT_FROM_TO(0, 2, DECLX_mybf1, x);
> *// generated: x_mybf1.push_back(&x_0_mybf1); x_mybf1.push_back(&x_1_mybf);
> * BOOST_PP_REPEAT_FROM_TO(0, 2, DECLX_mybf2, x);
> *// generated: x_mybf2.push_back(&x_0_mybf2); x_mybf2.push_back(&x_1_mybf);
> *
> but I think it is too much coding here, since I have to write a macro for
> each mybf occurrence, DECLX_mybf
>
> I have difficulty to use the BOOST_PP_REPEAT_FROM_TO for two argument, since
> the macro(z, x, data) can only accept one argument data, however, I need to
> pass two arguments x and mybf to the macro,
> in order to generate a mybfclass1 x_0_mybf.
>
> does anyone know how to apply macro to generate the above structure?
>

Wrap the arguments in a tuple:

#define DECLX(z, n, data) \
    BOOST_PP_CAT(\
        BOOST_PP_TUPLE_ELEM(2, 1, data),\
        BOOST_PP_CAT(\
            _,\
            BOOST_PP_TUPLE_ELEM(2, 0, data))\
    ).push_back(\
        &BOOST_PP_CAT(\
            BOOST_PP_TUPLE_ELEM(2, 1, data),\
            BOOST_PP_CAT(\
                BOOST_PP_CAT(_, n),\
                BOOST_PP_CAT(\
                    _,\
                    BOOST_PP_TUPLE_ELEM(2, 0, data)))));

BOOST_PP_REPEAT_FROM_TO(0, 2, DECLX, (mybf, x));

In Christ,
Steven Watanabe


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