Joos1W Compiler Framework
All Classes Functions Typedefs Pages
EnumMacros.h
1 #pragma once
2 
3 #define _MAKE_ENUM(VAR) VAR,
4 #define _MAKE_STRINGS(VAR) #VAR,
5 
6 /**
7  * @brief Declare an enum class with a list of members. First, you must declare
8  * the list of enum values through a macro. Like so:
9  * #define ENUM_NAME_LIST(F) F(First) F(Second) F(LAST_MEMBER)
10  * Then, use this macro to declare the enum class.
11  */
12 #define DECLARE_ENUM(ENUM_NAME, LIST)
13  enum class ENUM_NAME { LIST(_MAKE_ENUM) LAST_MEMBER };
14 
15 /**
16  * @brief Declare a string table for an enum class. First, you must declare the
17  * list of enum values through a macro (see DECLARE_ENUM). Then, use this
18  * macro to declare the string table. Use ENUM_NAME##_to_string to get
19  * the string representation of an enum value.
20  */
21 #define DECLARE_STRING_TABLE(ENUM_NAME, TABLE_NAME, LIST)
22  static constexpr char const* TABLE_NAME[]{LIST(_MAKE_STRINGS)};
23  static char const* ENUM_NAME##_to_string(ENUM_NAME type,
24  char const* default_value) {
25  /* Check if the type is within the range of the type string list */
26  if(static_cast<int>(type) >= static_cast<int>(ENUM_NAME::LAST_MEMBER))
27  return default_value;
28  return TABLE_NAME[static_cast<int>(type)];
29  }