Boost pre-processor: accessing elements in a sequence of tuples

I am trying to write some pre-processor macros that generate some logging functionality. I currently have a working example that works with simple sequences (i.e. FUNC( (a)(b)(c)(d) )

)

#include <boost/preprocessor.hpp>
#include <iostream>
#include <string>

#define X_DEFINE_ELEMENT_DISPLAY( r, data, elem ) \
    std::cerr << "( " << BOOST_PP_STRINGIZE( elem ) << " : " << v.elem << " ) ";

#define DEFINE_DISPLAY_FUNC( type, params )                 \
    void display( const type& v )                           \
    {                                                       \
        std::cerr << BOOST_PP_STRINGIZE( type ) << "{ ";    \
        BOOST_PP_SEQ_FOR_EACH(                              \
                X_DEFINE_ELEMENT_DISPLAY,                   \
                type, params )                              \
        std::cerr << " }\n";                                \
    }

struct MyStruct
{
    int   p1_;
    std::string p2_;
    unsigned    p3_;
};

DEFINE_DISPLAY_FUNC( MyStruct, (p1_)(p2_)(p3_) )

int main( int, char** )
{
    MyStruct s{ 23, "hello", 5467u };

    display( s );
    return 0;
}

      

If I modify the code to use a sequence of tuples (similarly BOOST_FUSION_ADAPT_STRUCT

) then the macro fails:

DEFINE_DISPLAY_FUNC( MyStruct, (int, p1_)(std::string, p2_)(unsigned, p3_) )

      

I understand this is because the elements are now "tuples", which causes the macro to BOOST_PP_SEQ_FOR_EACH

fail to expand correctly.

I am new to the pre-processor metaprogram and I have no idea what I should be using to properly expand / extract the required data.

+3


source to share





All Articles