Use a Boost preprocessor for a sequence of Parse elements

I have a macro that

#define TYPES (height,int,10)(width,int,20)

How to extend this macro using Boost Preprocessor, something like this?

int height = 10;
int width = 20;

I can get maximum height, int, 10 and width, int, 20 as a string, but I cannot parse a single element.

+3
source share
1 answer

Using a pinch of custom macros to turn TYPESin ((height,int,10))((width,int,20))before processing, so it BOOST_PP_SEQ_FOR_EACHwon't strangle him:

#define GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_0(...) \
     ((__VA_ARGS__)) GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_1

#define GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_1(...) \
     ((__VA_ARGS__)) GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_0

#define GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_0_END
#define GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_1_END

// Double the parentheses of a Boost.PP sequence
// I.e. (a, b)(c, d) becomes ((a, b))((c, d))
#define GLK_PP_SEQ_DOUBLE_PARENS(seq) \
    BOOST_PP_CAT(GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_0 seq, _END)


#define MAKE_ONE_VARIABLE(r, data, elem) \
    BOOST_PP_TUPLE_ELEM(1, elem) BOOST_PP_TUPLE_ELEM(0, elem) = BOOST_PP_TUPLE_ELEM(2, elem);

#define MAKE_VARIABLES(seq) \
    BOOST_PP_SEQ_FOR_EACH(MAKE_ONE_VARIABLE, ~, GLK_PP_SEQ_DOUBLE_PARENS(seq))

Using:

#define TYPES (height,int,10)(width,int,20)

int main() {
    MAKE_VARIABLES(TYPES)
}

Pre-processed:

int main() {
    int height = 10; int width = 20;
}

Watch live on Coliru

+4
source

Source: https://habr.com/ru/post/1677471/


All Articles