Skip to content

Instantly share code, notes, and snippets.

@xkikeg
Created March 26, 2012 17:37
Show Gist options
  • Save xkikeg/2207211 to your computer and use it in GitHub Desktop.
Save xkikeg/2207211 to your computer and use it in GitHub Desktop.
Example of Definition of Value Specified ENUM with Boost.PP
#include <iostream>
#include <boost/preprocessor.hpp>
#define VALUED_ENUM(unused, index, seq) \
BOOST_PP_TUPLE_ELEM(2, 0, BOOST_PP_SEQ_ELEM(index, seq)) \
= BOOST_PP_TUPLE_ELEM(2, 1, BOOST_PP_SEQ_ELEM(index, seq))
#define INSERT_TUPLE_HEAD(s, state, elem) \
BOOST_PP_SEQ_PUSH_BACK(state, \
BOOST_PP_TUPLE_ELEM(2, 0, elem))
#define ENUM_TO_STR(unused, data, elem) \
case BOOST_PP_CAT(data, elem): \
std::cerr << BOOST_PP_STRINGIZE(elem) << "\n"; \
break;
#define ENUM_KEY_VALUE ((VAL_A,0x3))((VAL_B,0x1))((VAL_C,0x4))
#define ENUM_KEY_TMP BOOST_PP_SEQ_FOLD_LEFT(INSERT_TUPLE_HEAD, \
(), \
ENUM_KEY_VALUE)
#define ENUM_KEY BOOST_PP_SEQ_REST_N(1, ENUM_KEY_TMP)
enum TestEnum {
BOOST_PP_ENUM(BOOST_PP_SEQ_SIZE(ENUM_KEY_VALUE), \
VALUED_ENUM, ENUM_KEY_VALUE)
};
int main()
{
for(int i=1; i < 5; ++i)
{
switch(i)
{
BOOST_PP_SEQ_FOR_EACH(ENUM_TO_STR, , ENUM_KEY);
default:
std::cerr << "(no hit)" << "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment