[Preprocessor] how to use REPEAT_FROM_TO for 2 arguments
Hi,
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
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
x_mybf; vector x_mybf1; vector 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
Hi Steven,
thanks for your answer. It solves my puzzle.
BR,
/Kenny
2010/1/6 Steven Watanabe
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
x_mybf; vector x_mybf1; vector 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Kai Liu Wichernstr. 18 91052, Erlangen MSN: tjroamer@hotmail.com
participants (2)
-
Kai Liu
-
Steven Watanabe