Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- // expansion macro for enum value definition
- #define ENUM_VALUE( name, assign ) name assign,
- // expansion macro for enum to string conversion
- #define ENUM_CASE( name, assign ) case name: return #name;
- #define ENUM_STRCMP( name, assign ) if( !strcmp( str, #name ) ) return name;
- // declare the access function and enum values
- #define DECLARE_ENUM( EnumType, ENUM_DEF ) \
- typedef enum _##EnumType { \
- ENUM_DEF( ENUM_VALUE ) \
- }EnumType;\
- static inline const char* get_##EnumType##_string( EnumType blah )\
- {\
- switch( blah ) {\
- ENUM_DEF(ENUM_CASE)\
- default: return "";\
- }\
- }\
- static inline EnumType get_##EnumType##_value( const char* str )\
- {\
- ENUM_DEF(ENUM_STRCMP)\
- return (EnumType)0;\
- }
- #define DECLARE_ENUM_NONINLINE( EnumType, ENUM_DEF )\
- typedef enum _##EnumType { \
- ENUM_DEF( ENUM_VALUE ) \
- }EnumType;\
- const char* get_##EnumType##_string( EnumType blah );\
- EnumType get_##EnumType##_value( const char* str );
- #define DEFINE_ENUM_STRINGIFY( EnumType, ENUM_DEF )\
- const char* get_##EnumType##_string( EnumType blah )\
- {\
- switch( blah ) {\
- ENUM_DEF(ENUM_CASE)\
- default: return "";\
- }\
- }\
- EnumType get_##EnumType##_value( const char* str )\
- {\
- ENUM_DEF(ENUM_STRCMP)\
- return (EnumType)0;\
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement