2020-02-26 11:57:18 +00:00
|
|
|
#include <algorithm>
|
2020-04-10 18:02:41 +00:00
|
|
|
#include <cmath>
|
2020-02-26 11:57:18 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
|
|
|
#include "MacroUtils.hpp"
|
2020-06-26 13:56:00 +00:00
|
|
|
#include "StringView.hpp"
|
2020-02-26 11:57:18 +00:00
|
|
|
|
2020-04-18 12:57:23 +00:00
|
|
|
#define ENUM_VALUE(x, value) x = value,
|
|
|
|
#
|
2020-02-26 11:57:18 +00:00
|
|
|
#define ENUM_CASE(x, name) \
|
|
|
|
case name::x: \
|
2020-06-26 13:56:00 +00:00
|
|
|
return ArbUt::StringViewLiteral(#x);
|
2020-02-26 11:57:18 +00:00
|
|
|
#
|
|
|
|
#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;
|
|
|
|
#
|
2020-03-12 18:28:26 +00:00
|
|
|
#define ENUM_GET_HIGHEST(x, name) \
|
|
|
|
if ((int64_t)name::x > highest) { \
|
|
|
|
highest = (int64_t)name::x; \
|
|
|
|
}
|
|
|
|
#
|
|
|
|
#define ENUM_GET_LOWEST(x, name) \
|
|
|
|
if ((int64_t)name::x < lowest) { \
|
|
|
|
lowest = (int64_t)name::x; \
|
|
|
|
}
|
|
|
|
#
|
2020-02-26 11:57:18 +00:00
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 12:57:23 +00:00
|
|
|
#define ENUM_WITH_START_VALUE(name, type, startValue, values...) \
|
2020-04-18 13:12:34 +00:00
|
|
|
enum class name : type { \
|
|
|
|
MACRO_UTILS_FOR_EACH_WITH_VALUE(ENUM_VALUE, startValue + ___MACRO_UTILS_NARGS(values) - 1, values) \
|
|
|
|
}; \
|
2020-02-26 11:57:18 +00:00
|
|
|
class name##Helper { \
|
2020-06-20 15:45:41 +00:00
|
|
|
inline static uint32_t constexpr ConstHash(char const* input) noexcept { \
|
2020-02-26 11:57:18 +00:00
|
|
|
return *input ? static_cast<uint32_t>(*input) + 33 * ConstHash(input + 1) : 5381; \
|
|
|
|
} \
|
2020-06-20 15:45:41 +00:00
|
|
|
inline static constexpr char charToLower(const char c) noexcept { \
|
2020-02-26 11:57:18 +00:00
|
|
|
return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; \
|
|
|
|
} \
|
2020-06-20 15:45:41 +00:00
|
|
|
inline static uint32_t constexpr ConstHashCI(char const* input) noexcept { \
|
2020-02-26 11:57:18 +00:00
|
|
|
return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * ConstHashCI(input + 1) \
|
|
|
|
: 5381; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
public: \
|
2020-06-26 13:56:00 +00:00
|
|
|
constexpr static ArbUt::StringViewLiteral ToString(name value) noexcept { \
|
2020-03-11 10:49:56 +00:00
|
|
|
switch (value) { MACRO_UTILS_FOR_EACH(ENUM_CASE, name, values) } \
|
2020-06-26 13:56:00 +00:00
|
|
|
return "out of bounds"_cnc; \
|
2020-02-26 11:57:18 +00:00
|
|
|
} \
|
|
|
|
constexpr static name Parse(const char* input, bool caseInsensitive = false) { \
|
|
|
|
if (caseInsensitive) \
|
|
|
|
return ParseCaseInsensitive(input); \
|
2020-03-11 10:49:56 +00:00
|
|
|
switch (ConstHash(input)) { MACRO_UTILS_FOR_EACH(ENUM_PARSE_CASE, name, values) } \
|
2020-03-11 10:38:36 +00:00
|
|
|
throw std::runtime_error("Invalid " #name " string."); \
|
2020-02-26 11:57:18 +00:00
|
|
|
} \
|
2020-06-20 15:45:41 +00:00
|
|
|
constexpr static bool TryParse(const char* input, name& out, bool caseInsensitive = false) noexcept { \
|
2020-02-26 11:57:18 +00:00
|
|
|
if (caseInsensitive) \
|
|
|
|
return TryParseCaseInsensitive(input, out); \
|
2020-03-11 10:49:56 +00:00
|
|
|
switch (ConstHash(input)) { MACRO_UTILS_FOR_EACH(ENUM_TRY_PARSE_CASE, name, values) } \
|
2020-02-26 11:57:18 +00:00
|
|
|
return false; \
|
|
|
|
} \
|
2020-06-20 15:45:41 +00:00
|
|
|
static std::vector<name> GetValues() noexcept { return {MACRO_UTILS_FOR_EACH(ARRAY_NAME, name, values)}; } \
|
|
|
|
constexpr static name Last() noexcept { return name::MACRO_UTILS_GET_LAST(values); } \
|
|
|
|
constexpr static name First() noexcept { return name::MACRO_UTILS_GET_FIRST(values); } \
|
|
|
|
constexpr static name Highest() noexcept { \
|
2020-03-12 18:28:26 +00:00
|
|
|
int64_t highest = -9223372036854775807; \
|
|
|
|
MACRO_UTILS_FOR_EACH(ENUM_GET_HIGHEST, name, values) \
|
|
|
|
return (name)highest; \
|
|
|
|
} \
|
2020-06-20 15:45:41 +00:00
|
|
|
constexpr static name Lowest() noexcept { \
|
2020-03-12 18:28:26 +00:00
|
|
|
int64_t lowest = 9223372036854775807; \
|
|
|
|
MACRO_UTILS_FOR_EACH(ENUM_GET_LOWEST, name, values) \
|
|
|
|
return (name)lowest; \
|
|
|
|
} \
|
2020-02-26 11:57:18 +00:00
|
|
|
\
|
|
|
|
private: \
|
2020-03-11 10:38:36 +00:00
|
|
|
constexpr static name ParseCaseInsensitive(const char* input) { \
|
2020-03-11 10:49:56 +00:00
|
|
|
switch (ConstHashCI(input)) { MACRO_UTILS_FOR_EACH(ENUM_PARSE_CASE_INSENSITIVE, name, values) } \
|
2020-03-11 10:38:36 +00:00
|
|
|
throw std::runtime_error("Invalid " #name " string."); \
|
2020-02-26 11:57:18 +00:00
|
|
|
} \
|
2020-06-20 15:45:41 +00:00
|
|
|
constexpr static bool TryParseCaseInsensitive(const char* input, name& out) noexcept { \
|
2020-03-11 10:49:56 +00:00
|
|
|
switch (ConstHashCI(input)) { MACRO_UTILS_FOR_EACH(ENUM_TRY_PARSE_CASE_INSENSITIVE, name, values) } \
|
2020-02-26 11:57:18 +00:00
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
};
|
2020-04-18 12:57:23 +00:00
|
|
|
|
|
|
|
#define ENUM(name, type, values...) ENUM_WITH_START_VALUE(name, type, 0, values);
|