[Preprocessor] Is it possible to expand recursively?

Hello, I'd like to have a macro like: BUILD_HIERARCHY (a, (b,) (c, (d,) (e,) ) ) which should expand to: struct a {}; struct b: a {}; struct c: a {}; struct d: c {}; struct e: c {}; Is this possible at all? I have tried but with no luck, if this is impossible, please tell me so. TIA

On 7/17/2011 9:21 AM, TONGARI wrote:
Hello,
I'd like to have a macro like:
BUILD_HIERARCHY (a, (b,) (c, (d,) (e,) ) )
which should expand to:
struct a {}; struct b: a {}; struct c: a {}; struct d: c {}; struct e: c {};
Is this possible at all? I have tried but with no luck, if this is impossible, please tell me so.
I think you want above: (a, (b) (c, (d) (e) ) ) This is a tuple with a possible second element as a seq. Each element of your seq is a tuple with the same rule as the top tuple. Following internal logic, perhaps with nested BOOST_PP_WHILEs, you should be able to cycle through each tuple and nested tuple forming the relationships you have created for your structs above. I can not guarantee this will be doable but at least you should have some idea of what to do from the description of what you have.

On 7/17/2011 1:29 PM, Edward Diener wrote:
On 7/17/2011 9:21 AM, TONGARI wrote:
Hello,
I'd like to have a macro like:
BUILD_HIERARCHY (a, (b,) (c, (d,) (e,) ) )
which should expand to:
struct a {}; struct b: a {}; struct c: a {}; struct d: c {}; struct e: c {};
Is this possible at all? I have tried but with no luck, if this is impossible, please tell me so.
I think you want above:
(a, (b) (c, (d) (e) ) )
This is a tuple with a possible second element as a seq. Each element of your seq is a tuple with the same rule as the top tuple.
Following internal logic, perhaps with nested BOOST_PP_WHILEs, you should be able to cycle through each tuple and nested tuple forming the relationships you have created for your structs above. I can not guarantee this will be doable but at least you should have some idea of what to do from the description of what you have.
I have to correct the above. The syntax for what I described must be: (a, ((b)) ((c, (d) (e)) ) )

2011/7/18 Edward Diener
On 7/17/2011 1:29 PM, Edward Diener wrote:
On 7/17/2011 9:21 AM, TONGARI wrote:
Hello,
I'd like to have a macro like:
BUILD_HIERARCHY (a, (b,) (c, (d,) (e,) ) )
which should expand to:
struct a {}; struct b: a {}; struct c: a {}; struct d: c {}; struct e: c {};
Is this possible at all? I have tried but with no luck, if this is impossible, please tell me so.
I think you want above:
(a, (b) (c, (d) (e) ) )
This is a tuple with a possible second element as a seq. Each element of your seq is a tuple with the same rule as the top tuple.
Following internal logic, perhaps with nested BOOST_PP_WHILEs, you should be able to cycle through each tuple and nested tuple forming the relationships you have created for your structs above. I can not guarantee this will be doable but at least you should have some idea of what to do from the description of what you have.
I have to correct the above. The syntax for what I described must be:
(a, ((b)) ((c, (d) (e)) )
)
I think I need the comma since I don't assume variadic macro. This is what I had (not working, of course): #define PACK_2A( a1, a2 ) ((a1, a2)) PACK_2B #define PACK_2B( a1, a2 ) ((a1, a2)) PACK_2A #define PACK_2A_END #define PACK_2B_END #define BUILD_SUB( x, parent, child ) \ struct BOOST_PP_TUPLE_ELEM(2, 0, child) : parent {};\ BOOST_PP_SEQ_FOR_EACH\ (\ BUILD_SUB, BOOST_PP_TUPLE_ELEM(2, 0, child)\ , BOOST_PP_CAT(BOOST_PP_EXPAND(PACK_2A BOOST_PP_TUPLE_ELEM(2, 1, child)), _END)\ ) #define BUILD_HIERARCHY( root, children ) \ struct root {};\ BOOST_PP_SEQ_FOR_EACH\ (\ BUILD_SUB, root\ , BOOST_PP_CAT(PACK_2A children, _END)\ ) Obviously BUILD_SUB cannot be that way since the preprocessor disallows recursion. But even if I replace the recursive BUILD_SUB with something else the BOOST_PP_SEQ_FOR_EACH inside BUILD_SUB still fails to expand. I have no ideas :/ Thanks for your time.

On 7/18/2011 4:04 AM, TONGARI wrote:
2011/7/18 Edward Diener
mailto:eldiener@tropicsoft.com> On 7/17/2011 1:29 PM, Edward Diener wrote:
On 7/17/2011 9:21 AM, TONGARI wrote:
Hello,
I'd like to have a macro like:
BUILD_HIERARCHY (a, (b,) (c, (d,) (e,) ) )
which should expand to:
struct a {}; struct b: a {}; struct c: a {}; struct d: c {}; struct e: c {};
Is this possible at all? I have tried but with no luck, if this is impossible, please tell me so.
I think you want above:
(a, (b) (c, (d) (e) ) )
This is a tuple with a possible second element as a seq. Each element of your seq is a tuple with the same rule as the top tuple.
Following internal logic, perhaps with nested BOOST_PP_WHILEs, you should be able to cycle through each tuple and nested tuple forming the relationships you have created for your structs above. I can not guarantee this will be doable but at least you should have some idea of what to do from the description of what you have.
I have to correct the above. The syntax for what I described must be:
(a, ((b)) ((c, (d) (e)) )
)
I think I need the comma since I don't assume variadic macro.
I do not understand what you mean by that, and what some extra comma has to do with variadic macros I still do not have the above right. It should be: (a, ((b)) ((c, ((d)) ((e)) ) ) ) In other words, each base-class/derived-classes relationship is a tuple where the first element is the base class name and where each optional second element is a sequence of derived classes in the form of your tuple again. If you can not use variadic macros you can change your pp-tuples above to pp-arrays so you have the tuple size of either 1 or 2. So without variadic macros your relationship above would be: (2,(a, ((1,(b))) ((2,(c, ((1,(d))) ((1,(e))) ) ) ) ))
This is what I had (not working, of course):
#define PACK_2A( a1, a2 ) ((a1, a2)) PACK_2B #define PACK_2B( a1, a2 ) ((a1, a2)) PACK_2A #define PACK_2A_END #define PACK_2B_END
#define BUILD_SUB( x, parent, child ) \ struct BOOST_PP_TUPLE_ELEM(2, 0, child) : parent {};\ BOOST_PP_SEQ_FOR_EACH\ (\ BUILD_SUB, BOOST_PP_TUPLE_ELEM(2, 0, child)\ , BOOST_PP_CAT(BOOST_PP_EXPAND(PACK_2A BOOST_PP_TUPLE_ELEM(2, 1, child)), _END)\ )
#define BUILD_HIERARCHY( root, children ) \ struct root {};\ BOOST_PP_SEQ_FOR_EACH\ (\ BUILD_SUB, root\ , BOOST_PP_CAT(PACK_2A children, _END)\ )
Obviously BUILD_SUB cannot be that way since the preprocessor disallows recursion. But even if I replace the recursive BUILD_SUB with something else the BOOST_PP_SEQ_FOR_EACH inside BUILD_SUB still fails to expand.
I have no ideas :/
Just in case you do not know, you can use the Wave trace facility to see how your macros above are expanding when you invoke BUILD_HIERARCHY with your two parameters.

2011/7/19 Edward Diener
On 7/18/2011 4:04 AM, TONGARI wrote:
2011/7/18 Edward Diener
>> On 7/17/2011 1:29 PM, Edward Diener wrote:
On 7/17/2011 9:21 AM, TONGARI wrote:
Hello,
I'd like to have a macro like:
BUILD_HIERARCHY (a, (b,) (c, (d,) (e,) ) )
which should expand to:
struct a {}; struct b: a {}; struct c: a {}; struct d: c {}; struct e: c {};
Is this possible at all? I have tried but with no luck, if this is impossible, please tell me so.
I think you want above:
(a, (b) (c, (d) (e) ) )
This is a tuple with a possible second element as a seq. Each element of your seq is a tuple with the same rule as the top tuple.
Following internal logic, perhaps with nested BOOST_PP_WHILEs, you should be able to cycle through each tuple and nested tuple forming the relationships you have created for your structs above. I can not guarantee this will be doable but at least you should have some idea of what to do from the description of what you have.
I have to correct the above. The syntax for what I described must be:
(a, ((b)) ((c, (d) (e)) )
)
I think I need the comma since I don't assume variadic macro.
I do not understand what you mean by that, and what some extra comma has to do with variadic macros
Without variadic macro we can just leave the 2nd arg empty (e.g. MACRO(x, ) where MACRO requires 2 args) as no derived classes.
I still do not have the above right. It should be:
(a, ((b)) ((c, ((d)) ((e)) ) ) )
IIUC, your surrounding ()s here are automatically packed by what I had: #define PACK_2A( a1, a2 ) ((a1, a2)) PACK_2B #define PACK_2B( a1, a2 ) ((a1, a2)) PACK_2A #define PACK_2A_END #define PACK_2B_END and used like: BOOST_PP_CAT(PACK_2A children, _END) BTW, if variadic macro available, they'd be #define PACK_A( VARG... ) (( VARG )) PACK_B #define PACK_B( VARG... ) (( VARG )) PACK_A #define PACK_A_END #define PACK_B_END [...] Just in case you do not know, you can use the Wave trace facility to see how
your macros above are expanding when you invoke BUILD_HIERARCHY with your two parameters.
I used to use BOOST_PP_STRINGIZE to show me the result. It turns out that BOOST_PP_SEQ_FOR_EACH cannot be used recursively. ...and I have to say I must give up on this temporarily, if I have time to back to this again, I'll see if Wave can help me figure out what happened. Thanks for your patience.
participants (2)
-
Edward Diener
-
TONGARI