Short example of what I use:
#include
#include
#include "boost/preprocessor.hpp"
#include "boost/algorithm/string/predicate.hpp"
#define ENUMIFY_FOREACH( r, data, elem ) \
BOOST_PP_STRINGIZE( elem ) BOOST_PP_COMMA()
#define ENUMIFY( name, values ) \
struct name \
{ \
static const unsigned int Size = BOOST_PP_SEQ_SIZE( values ); \
typedef enum{ \
BOOST_PP_SEQ_ENUM( values ) \
} Values; \
static const char* (&Mappings())[ Size ] \
{ \
static const char* mappings[] = \
{ \
BOOST_PP_SEQ_FOR_EACH( ENUMIFY_FOREACH, _, values ) \
}; \
return mappings; \
}; \
static const char* String( Values a_Val ) \
{ \
return Mappings()[ static_cast< unsigned int >( a_Val ) ]; \
} \
static Values Value( const char* a_Key ) \
{ \
for( unsigned int i = 0; i < Size; ++i ) \
if( boost::iequals( a_Key, Mappings()[i] ) ) return static_cast< Values >( i ); \
assert( 0 && "Didn't find the value of string " ); \
return static_cast< Values >( 0 ); \
} \
\
};
ENUMIFY( SomeEnum, (Long)(Short)(Etc) );
int main()
{
std::cout << SomeEnum::String( SomeEnum::Long ) << std::endl; // Outputs Long
}