Boost logo

Boost Users :

From: Gustavo Guerra (gustavobt_at_[hidden])
Date: 2001-12-17 22:00:13


I use the following macros in my code:

    #define MAKE_FLAG_TYPE1(TYPE_NAME, F1) \
      enum TYPE_NAME { F1 }; \
                                           \
    ::std::ostream& operator<<(::std::ostream& os, TYPE_NAME f) \
                                      \
      switch(f) \
      { \
        case F1: return os << #F1; \
      } \
      return os << "unknown"; \
    }

[2, 3 and 4 argument versions]

    #define MAKE_FLAG_TYPE5(TYPE_NAME, F1, F2, F3, F4, F5) \
      enum TYPE_NAME { F1, F2, F3, F4, F5 }; \
                                                            \
    ::std::ostream& operator<<(::std::ostream& os, TYPE_NAME f) \
    { \
      switch(f) \
      { \
        case F1: return os << #F1; \
        case F2: return os << #F2; \
        case F3: return os << #F3; \
        case F4: return os << #F4; \
        case F5: return os << #F5; \
      } \
      return os << "unknown"; \
    }

I wanted to try and do it with the Preprocessor library. It should be
something like this:

    #include <boost/preprocessor/comma_if.hpp>
    #include <boost/preprocessor/repeat.hpp>
    #include <boost/preprocessor/repeat_2nd.hpp>
    #include <boost/preprocessor/inc.hpp>
    #include <boost/preprocessor/cat.hpp>
    #include <boost/preprocessor/enum_params.hpp>

    #ifndef MAX_MAKE_FLAG_TYPE_PARAMS
      #define MAX_MAKE_FLAG_TYPE_PARAMS 10
    #endif

    BOOST_PREPROCESSOR_REPEAT(MAX_MAKE_FLAG_TYPE, MAKE_FLAG_TYPE_AUX, _)

    #define MAKE_FLAG_TYPE_AUX(N,_)
\
      #define MAKE_FLAG_TYPE##N(TYPE_NAME,BOOST_PREPROCESSOR_ENUM_PARAMS(N,
F)) \
          enum TYPE_NAME { BOOST_PREPROCESSOR_ENUM_PARAMS(N, F) };
\
    ::std::ostream& operator<<(::std::ostream& os, TYPE_NAME f)
\
    { \
      switch(f) \
      { \
        BOOST_PREPROCESSOR_REPEAT_2ND(N, MAKE_FLAG_TYPE_AUX2, _) \
      } \
      return os << "unknown"; \
    }

    #define MAKE_FLAG_TYPE_AUX2(N,_) \
      case F##N: return os << BOOST_PREPROCESSOR_CAT(#, F##N);

    #undef MAKE_FLAG_TYPE_AUX
    #undef MAKE_FLAG_TYPE_AUX2

But then I looked at my code and clearly saw this won't do it.
One of the problems is that by preprocessing I'll get things like:
    #define MAKE_FLAG_TYPE2(TYPE_NAME, F1, F2) enum [...]

That won't work because of the spaces between the commas.
And also I doubt BOOST_PREPROCESSOR_CAT(#, F##N) will work

So, does anyone has any suggestions on how to implement my MAKE_FLAG_TYPE<N>
macros
using the Preprocessor library? Or is it just not possible?

Thanks
Gustavo Guerra


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