CatapultServer  v0.5.0.1 (Elephant)
MacroBasedEnum.h
Go to the documentation of this file.
1 
21 #ifdef DEFINE_ENUM
22 #define ENUM_TYPE DEFINE_ENUM
23 #endif
24 
25 #if !defined(ENUM_LIST) || !defined(ENUM_TYPE)
26 #error "In order to use MacroBasedEnum.h ENUM_LIST and DEFINE_ENUM must be defined"
27 #endif
28 
29 #ifdef DEFINE_ENUM
30 #define CONCAT_SYMBOLS(LEFT, RIGHT) LEFT##RIGHT
31 #define QUOTE(NAME) #NAME
32 #define STR(MACRO) QUOTE(MACRO)
33 
34  namespace {
35  const char* CONCAT_SYMBOLS(ENUM_TYPE, ToString)(ENUM_TYPE value) {
36  switch (value) {
37 #ifdef EXPLICIT_VALUE_ENUM
38 #define ENUM_VALUE(LABEL, VALUE) case ENUM_TYPE::LABEL: return #LABEL;
39 #else
40 #define ENUM_VALUE(LABEL) case ENUM_TYPE::LABEL: return #LABEL;
41 #endif
42 
43  ENUM_LIST
44 
45 #undef ENUM_VALUE
46  }
47 
48  return nullptr;
49  }
50  }
51 
52  std::ostream& operator<<(std::ostream& out, ENUM_TYPE value) {
53  auto pLabel = CONCAT_SYMBOLS(ENUM_TYPE, ToString)(value);
54  if (pLabel)
55  out << pLabel;
56  else
57  out << STR(ENUM_TYPE) << "(0x" << utils::HexFormat(utils::to_underlying_type(value)) << ")";
58  return out;
59  }
60 
61 #undef STR
62 #undef QUOTE
63 #undef CONCAT_SYMBOLS
64 #endif
65 
66 #undef ENUM_TYPE
ENUM_LIST
#define ENUM_LIST
Definition: UnlockedAccounts.cpp:27
catapult::utils::HexFormat
constexpr void HexFormat(const ByteArray< N, TTag > &)
catapult::harvesting::operator<<
std::ostream & operator<<(std::ostream &out, UnlockedAccountsAddResult value)
Insertion operator for outputting value to out.
catapult::utils::to_underlying_type
constexpr std::underlying_type_t< TEnum > to_underlying_type(TEnum value)
Converts a strongly typed enumeration value to its underlying integral value.
Definition: Casting.h:37
STR
#define STR(SYMBOL)
Definition: EntityType.cpp:33