Macros.h 1006 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // Based on http://stackoverflow.com/a/202511
  3. //
  4. #pragma mark - Enum Factory Macros
  5. // expansion macro for enum value definition
  6. #define ENUM_VALUE(name,assign) name assign,
  7. // expansion macro for enum to string conversion
  8. #define ENUM_CASE(name,assign) case name: return @#name;
  9. // expansion macro for string to enum conversion
  10. #define ENUM_STRCMP(name,assign) if ([string isEqualToString:@#name]) return name;
  11. /// declare the access function and define enum values
  12. #define DECLARE_ENUM(EnumType,ENUM_DEF) \
  13. typedef enum EnumType { \
  14. ENUM_DEF(ENUM_VALUE) \
  15. }EnumType; \
  16. NSString *_Nonnull NSStringFrom##EnumType(EnumType value); \
  17. EnumType EnumType##FromNSString(NSString *_Nonnull string); \
  18. // Define Functions
  19. #define DEFINE_ENUM(EnumType, ENUM_DEF) \
  20. NSString *_Nonnull NSStringFrom##EnumType(EnumType value) \
  21. { \
  22. switch(value) \
  23. { \
  24. ENUM_DEF(ENUM_CASE) \
  25. default: return @""; \
  26. } \
  27. } \
  28. EnumType EnumType##FromNSString(NSString *string) \
  29. { \
  30. ENUM_DEF(ENUM_STRCMP) \
  31. return (EnumType)0; \
  32. }