157 lines
9.3 KiB
C++
157 lines
9.3 KiB
C++
#pragma once
|
|
|
|
#include <ostream>
|
|
|
|
#define ENUM_BASIC_MEMBERS(type) \
|
|
Value value; \
|
|
\
|
|
type() = default; \
|
|
/*NOLINTNEXTLINE*/ \
|
|
constexpr type(Value value) noexcept : value(value) {} \
|
|
\
|
|
type(const type &) noexcept = default; \
|
|
type(type &&) noexcept = default; \
|
|
type &operator=(const type &) noexcept = default; \
|
|
type &operator=(type &&) noexcept = default; \
|
|
\
|
|
~type() = default; \
|
|
\
|
|
static constexpr bool parse(const char *name, type *value) noexcept; \
|
|
static bool parse(const std::string &name, type *value) noexcept; \
|
|
\
|
|
[[nodiscard]] constexpr const char *name() const noexcept;
|
|
|
|
#define ENUM_VALUE_TO_NAME_CASE(type, v) \
|
|
case type::v: \
|
|
return #v;
|
|
|
|
#define ENUM_NAME_TO_VALUE_CLAUSE(type, v) \
|
|
if (name == #v) { \
|
|
*value = type::v; \
|
|
return true; \
|
|
}
|
|
|
|
#define ENUM_NAMEC_TO_VALUE_CLAUSE(type, v) \
|
|
if (std::string_view(name) == #v) { \
|
|
*value = type::v; \
|
|
return true; \
|
|
}
|
|
|
|
// clang-format off
|
|
#define ENUM_NAME_DEF(type, for_all) \
|
|
constexpr const char *type::name() const noexcept { \
|
|
switch (value) { \
|
|
for_all(ENUM_VALUE_TO_NAME_CASE) \
|
|
default: return "ENUM_UNKNOWN"; \
|
|
} \
|
|
}
|
|
// clang-format on
|
|
|
|
#define ENUM_PARSE_DEF(type, for_all) \
|
|
constexpr bool type::parse(const char *name, type *value) noexcept { \
|
|
for_all(ENUM_NAMEC_TO_VALUE_CLAUSE) return false; \
|
|
} \
|
|
inline bool type::parse(const std::string &name, type *value) noexcept { \
|
|
for_all(ENUM_NAME_TO_VALUE_CLAUSE) return false; \
|
|
}
|
|
|
|
#define ENUM_EQUALITY_OPS(type) \
|
|
constexpr bool operator==(type lhs, type rhs) noexcept { \
|
|
return lhs.value == rhs.value; \
|
|
} \
|
|
constexpr bool operator!=(type lhs, type rhs) noexcept { \
|
|
return lhs.value != rhs.value; \
|
|
} \
|
|
constexpr bool operator==(type lhs, type::Value rhs) noexcept { \
|
|
return lhs.value == rhs; \
|
|
} \
|
|
constexpr bool operator!=(type lhs, type::Value rhs) noexcept { \
|
|
return lhs.value != rhs; \
|
|
}
|
|
|
|
#define ENUM_OUTPUT_OPS(type) \
|
|
inline std::ostream &operator<<(std::ostream &out, type value) noexcept { \
|
|
out << value.name(); \
|
|
return out; \
|
|
} \
|
|
\
|
|
inline std::ostream &operator<<( \
|
|
std::ostream &out, type::Value value) noexcept { \
|
|
out << type{value}.name(); \
|
|
return out; \
|
|
}
|
|
|
|
#define ENUM_FROM_JSON(type) \
|
|
inline void from_json(const nlohmann::json &j, type &value) { \
|
|
std::string value_str; \
|
|
j.get_to(value_str); \
|
|
type::parse(value_str, &value); \
|
|
}
|
|
|
|
#define ENUM_TO_JSON(type) \
|
|
inline void to_json(nlohmann::json &j, const type &value) { \
|
|
j = value.name(); \
|
|
}
|
|
|
|
#define ENUM_DECLARE_VALUE(t, x) x,
|
|
|
|
#define ENUM_CLASS_DEF(type, for_all) \
|
|
class type { \
|
|
public: \
|
|
enum Value { for_all(ENUM_DECLARE_VALUE) }; \
|
|
\
|
|
ENUM_BASIC_MEMBERS(type) \
|
|
}; \
|
|
\
|
|
ENUM_OUTPUT_OPS(type) \
|
|
ENUM_NAME_DEF(type, for_all) \
|
|
ENUM_PARSE_DEF(type, for_all) \
|
|
ENUM_FROM_JSON(type) \
|
|
ENUM_TO_JSON(type) \
|
|
ENUM_EQUALITY_OPS(type)
|
|
|
|
#define ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, op) \
|
|
constexpr enum_type operator op(enum_type lhs, enum_type rhs) noexcept { \
|
|
return static_cast<enum_type>(static_cast<numeric_type>(lhs) \
|
|
op static_cast<numeric_type>(rhs)); \
|
|
}
|
|
|
|
#define ENUM_BITWISE_COMPARISON_OPS(enum_type, numeric_type) \
|
|
ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, |) \
|
|
ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, &) \
|
|
ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, ^)
|
|
|
|
#define ENUM_BITWISE_ASSIGNMENT_OP(enum_type, op) \
|
|
inline enum_type &operator op##=(enum_type &lhs, enum_type rhs) noexcept { \
|
|
lhs = lhs op rhs; \
|
|
return lhs; \
|
|
}
|
|
|
|
#define ENUM_BITWISE_ASSIGNMENT_OPS(enum_type) \
|
|
ENUM_BITWISE_ASSIGNMENT_OP(enum_type, |) \
|
|
ENUM_BITWISE_ASSIGNMENT_OP(enum_type, &) \
|
|
ENUM_BITWISE_ASSIGNMENT_OP(enum_type, ^)
|
|
|
|
#define ENUM_BITWISE_OPS(enum_type, numeric_type) \
|
|
ENUM_BITWISE_COMPARISON_OPS(enum_type, numeric_type) \
|
|
ENUM_BITWISE_ASSIGNMENT_OPS(enum_type)
|
|
|
|
#define ENUM_HAS_FLAG(enum_value, flag) (enum_value & flag) == flag
|
|
#define ENUM_STRIP_FLAG(enum_value, flag) (enum_value | flag) ^ flag
|
|
|
|
#define PB_ENUM_FROM_JSON(enum_type) \
|
|
inline void from_json(const nlohmann::json &j, enum_type &value) { \
|
|
io::JsonReader jr{&j}; \
|
|
std::string name; \
|
|
bool success = jr.read(name) && enum_type##_Parse(name, &value); \
|
|
if (!success) { \
|
|
const auto j_str = j.dump(2); \
|
|
LOG(Gen, Diag, "Failed to parse json to " #enum_type ":\n{}", j_str); \
|
|
} \
|
|
}
|
|
|
|
#define PB_ENUM_TO_JSON(enum_type) \
|
|
inline void to_json(nlohmann::json &j, const enum_type &value) { \
|
|
j = enum_type##_Name(value); \
|
|
}
|