Search code examples
c++boostboost-preprocessor

Repeat generation of macro


I would like to generate the following preprocessor pragmas

#pragma blabla column("0030")
#pragma blabla column("0130")
#pragma blabla column("0230")
...
#pragma blabla column("2330")

the hour changing from 0 to 23

Is that possible with BOOST_PP_LOCAL_LIMITS/ITERATE?


Solution

  • Yeah.

    #include <boost/preprocessor/repeat.hpp>
    #include <boost/preprocessor/stringize.hpp>
    #include <boost/preprocessor/control/if.hpp>
    #include <boost/preprocessor/comparison/greater.hpp>
    
    #define blabla(z, n, data)                          \
        _Pragma(BOOST_PP_STRINGIZE(                     \
            blabla column(                              \
                BOOST_PP_STRINGIZE(                     \
                    BOOST_PP_CAT(                       \
                        BOOST_PP_CAT(                   \
                            BOOST_PP_IF(                \
                                BOOST_PP_GREATER(n, 9), \
                                ,                       \
                                0                       \
                            ),                          \
                            n                           \
                        ),                              \
                        30                              \
                    )                                   \
                )                                       \
            )                                           \
        ))
    
    BOOST_PP_REPEAT(24, blabla, ~)
    

    _Pragma saved us there because there is no way to generate preprocessor directives such as #pragma, however it is very picky about what it accepts. In particular, it doesn't do string concatenation, so _Pragma("some" "thing") does not work, we have to concatenate everything in token-land then stringize as the last step.