enum_codegen.h 1.7 KB

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. /// see enum_codegen_ut.cpp for examples
  3. #define ENUM_VALUE_GEN(name, value, ...) name = value,
  4. #define ENUM_VALUE_GEN_NO_VALUE(name, ...) name,
  5. #define ENUM_TO_STRING_IMPL_ITEM(name, ...) \
  6. case name: \
  7. return #name;
  8. #define ENUM_LTLT_IMPL_ITEM(name, ...) \
  9. case name: \
  10. os << #name; \
  11. break;
  12. #define ENUM_TO_STRING(type, MAP) \
  13. static inline const char* ToCString(type value) { \
  14. switch (value) { \
  15. MAP(ENUM_TO_STRING_IMPL_ITEM) \
  16. default: \
  17. return "UNKNOWN"; \
  18. } \
  19. } \
  20. \
  21. static inline IOutputStream& operator<<(IOutputStream& os, type value) { \
  22. switch (value) { \
  23. MAP(ENUM_LTLT_IMPL_ITEM) \
  24. default: \
  25. os << int(value); \
  26. break; \
  27. } \
  28. return os; \
  29. }