#include #include #include #include #include "MacroUtils.hpp" #define ENUM_CASE(x, name) \ case name::x: \ return #x; # #define ENUM_PARSE_CASE(x, name) \ case ConstHash(#x): \ return name::x; # #define ENUM_TRY_PARSE_CASE(x, name) \ case ConstHash(#x): \ out = name::x; \ return true; # #define ENUM_PARSE_CASE_INSENSITIVE(x, name) \ case ConstHashCI(#x): \ return name::x; # #define ENUM_TRY_PARSE_CASE_INSENSITIVE(x, name) \ case ConstHashCI(#x): \ out = name::x; \ return true; # #define ARRAY_NAME(x, name) name::x, /*! \def ENUM(name, type, values...) Generates an enum class inheriting from specified type with specified name. Also generates a useful helper class with static methods for use with the enum. */ #define ENUM(name, type, values...) \ enum class name : type { values }; \ class name##Helper { \ inline static uint32_t constexpr ConstHash(char const* input) { \ return *input ? static_cast(*input) + 33 * ConstHash(input + 1) : 5381; \ } \ inline static constexpr char charToLower(const char c) { \ return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; \ } \ inline static uint32_t constexpr ConstHashCI(char const* input) { \ return charToLower(*input) ? static_cast(charToLower(*input)) + 33 * ConstHashCI(input + 1) \ : 5381; \ } \ \ public: \ constexpr static const char* ToString(name value) { \ switch (value) { FOR_EACH(ENUM_CASE, name, values) } \ throw std::logic_error("Not reachable"); \ } \ constexpr static name Parse(const char* input, bool caseInsensitive = false) { \ if (caseInsensitive) \ return ParseCaseInsensitive(input); \ switch (ConstHash(input)) { FOR_EACH(ENUM_PARSE_CASE, name, values) } \ throw std::runtime_error("Invalid " #name " string."); \ } \ constexpr static bool TryParse(const char* input, name& out, bool caseInsensitive = false) { \ if (caseInsensitive) \ return TryParseCaseInsensitive(input, out); \ switch (ConstHash(input)) { FOR_EACH(ENUM_TRY_PARSE_CASE, name, values) } \ return false; \ } \ static std::vector GetValues() { return {FOR_EACH(ARRAY_NAME, name, values)}; } \ \ private: \ static name ParseCaseInsensitive(const char* input) { \ switch (ConstHashCI(input)) { FOR_EACH(ENUM_PARSE_CASE_INSENSITIVE, name, values) } \ throw std::runtime_error("Invalid " #name " string."); \ } \ static bool TryParseCaseInsensitive(const char* input, name& out) { \ switch (ConstHashCI(input)) { FOR_EACH(ENUM_TRY_PARSE_CASE_INSENSITIVE, name, values) } \ return false; \ } \ };