Boost logo

Boost Users :

Subject: [Boost-users] PP: how to use automatic recursion
From: Hicham Mouline (hicham_at_[hidden])
Date: 2010-02-01 02:27:21


Hello,

I am trying to generate many types from 1 struct (for full background, please see thread "generate structs with all combinations of members from a given struct"), with the help of Boost.PP

#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/seq/size.hpp>
#include <boost/preprocessor/seq/seq.hpp>
#include <boost/preprocessor/seq/elem.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/seq/pop_front.hpp>
#include <boost/preprocessor/seq/push_back.hpp>

#define STRUCTMEMBERS ((double,m1))((double,m2))((double,m3))((double,m4))((double,m5))

#define SEQ_LAST_ELEM(seq) \
  BOOST_PP_SEQ_ELEM( BOOST_PP_SUB(BOOST_PP_SEQ_SIZE(seq), 1), seq)

#define UPDATE_MAIN_SEQ(mainseq, globalseq)\
  BOOST_PP_IF( BOOST_PP_SEQ_SIZE(mainseq),\
               BOOST_PP_SEQ_PUSH_BACK(\
                mainseq,\
                ((SEQ_LAST_ELEM(mainseq))(BOOST_PP_SEQ_HEAD(globalseq)))\
        ),\
               ((BOOST_PP_SEQ_HEAD(globalseq)))\
      )

#define COMBINATIONS(seq, combseq)\
  BOOST_PP_IF( BOOST_PP_SEQ_SIZE(seq),\
               COMBINATIONS( UPDATE_MAIN_SEQ(combseq,seq), BOOST_PP_SEQ_POP_FRONT(seq) ), \
               combseq\
             )

#define COMBOS COMBINATIONS(STRUCTMEMBERS, BOOST_PP_SEQ_NIL)

COMBOS

Reading PP docs, I just realized that the preprocessor disallows recursion ( the COMBINATIONS macro calls itself in my case), but I didn't understand the reentrancy topic page.

How can I change the above code to make the recursive COMBINATIONS possible?

regards,



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