SHARE
TWEET

Untitled

a guest Sep 17th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string.h>
  2.  
  3. // expansion macro for enum value definition
  4. #define ENUM_VALUE( name, assign ) name assign,
  5.  
  6. // expansion macro for enum to string conversion
  7. #define ENUM_CASE( name, assign ) case name: return #name;
  8.  
  9. #define ENUM_STRCMP( name, assign ) if( !strcmp( str, #name ) ) return name;
  10.  
  11. // declare the access function and enum values
  12. #define DECLARE_ENUM( EnumType, ENUM_DEF ) \
  13.   typedef enum _##EnumType { \
  14.     ENUM_DEF( ENUM_VALUE ) \
  15.   }EnumType;\
  16.   static inline const char* get_##EnumType##_string( EnumType blah )\
  17.   {\
  18.      switch( blah ) {\
  19.        ENUM_DEF(ENUM_CASE)\
  20.        default: return "";\
  21.      }\
  22.   }\
  23.   static inline EnumType get_##EnumType##_value( const char* str )\
  24.   {\
  25.      ENUM_DEF(ENUM_STRCMP)\
  26.      return (EnumType)0;\
  27.   }
  28.  
  29. #define DECLARE_ENUM_NONINLINE( EnumType, ENUM_DEF )\
  30.   typedef enum _##EnumType { \
  31.     ENUM_DEF( ENUM_VALUE ) \
  32.   }EnumType;\
  33.   const char* get_##EnumType##_string( EnumType blah );\
  34.   EnumType get_##EnumType##_value( const char* str );
  35.  
  36. #define DEFINE_ENUM_STRINGIFY( EnumType, ENUM_DEF )\
  37.   const char* get_##EnumType##_string( EnumType blah )\
  38.   {\
  39.      switch( blah ) {\
  40.        ENUM_DEF(ENUM_CASE)\
  41.        default: return "";\
  42.      }\
  43.   }\
  44.   EnumType get_##EnumType##_value( const char* str )\
  45.   {\
  46.      ENUM_DEF(ENUM_STRCMP)\
  47.      return (EnumType)0;\
  48.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top