bhawesh
bhawesh

Reputation: 1320

Use Boost Preprocessor to Parse sequence of elements

I have a macro defined which is

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

How to expand this macro using Boost Preprocessor something like this?

int height = 10;
int width = 20;

at most i am able to get is height,int,10 and width,int,20 as string but can't parse individual element.

Upvotes: 4

Views: 1426

Answers (1)

Quentin
Quentin

Reputation: 63124

Using BOOST_PP_VARIADIC_SEQ_TO_SEQ to turn TYPES into ((height,int,10))((width,int,20)) before processing, so that BOOST_PP_SEQ_FOR_EACH doesn't choke on it:

#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, ~, BOOST_PP_VARIADIC_SEQ_TO_SEQ(seq))

Usage:

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

int main() {
    MAKE_VARIABLES(TYPES)
}

Is preprocessed into:

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

See it live on Coliru

Upvotes: 7

Related Questions