Files
botw/lib/EventFlow/include/ore/EnumUtil.h
T
Léo Lam 18c60323a9 Switch to subrepos
git subrepo clone https://github.com/open-ead/sead lib/sead

subrepo:
  subdir:   "lib/sead"
  merged:   "1b66e825d"
upstream:
  origin:   "https://github.com/open-ead/sead"
  branch:   "master"
  commit:   "1b66e825d"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

git subrepo clone (merge) https://github.com/open-ead/nnheaders lib/NintendoSDK

subrepo:
  subdir:   "lib/NintendoSDK"
  merged:   "9ee21399f"
upstream:
  origin:   "https://github.com/open-ead/nnheaders"
  branch:   "master"
  commit:   "9ee21399f"
git-subrepo:
  version:  "0.4.3"
  origin:   "ssh://git@github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

git subrepo clone https://github.com/open-ead/agl lib/agl

subrepo:
  subdir:   "lib/agl"
  merged:   "7c063271b"
upstream:
  origin:   "https://github.com/open-ead/agl"
  branch:   "master"
  commit:   "7c063271b"
git-subrepo:
  version:  "0.4.3"
  origin:   "ssh://git@github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

git subrepo clone https://github.com/open-ead/EventFlow lib/EventFlow

subrepo:
  subdir:   "lib/EventFlow"
  merged:   "c35d21b34"
upstream:
  origin:   "https://github.com/open-ead/EventFlow"
  branch:   "master"
  commit:   "c35d21b34"
git-subrepo:
  version:  "0.4.3"
  origin:   "ssh://git@github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"
2022-03-21 21:31:42 +01:00

116 lines
5.4 KiB
C++

#pragma once
#include <iterator>
#include <ore/IterRange.h>
#include <ore/StringView.h>
#include <type_traits>
namespace ore {
namespace detail::EnumUtil {
int FindIndex(int value, const IterRange<const int*>& values);
void Parse(const IterRange<StringView*>& out, StringView definition);
constexpr int CountValues(const char* text_all, size_t text_all_len) {
int count = 1;
for (size_t i = 0; i < text_all_len; ++i) {
if (text_all[i] == ',')
++count;
}
return count;
}
} // namespace detail::EnumUtil
template <class T>
struct Enum {
public:
Enum() { T::Init(); }
static Enum<T>& Info() { return s_Info; }
StringView name{};
IterRange<StringView*> members{};
private:
static inline Enum<T> s_Info{};
};
#define ORE_ENUM(NAME, ...) \
class NAME { \
public: \
enum Type { __VA_ARGS__ }; \
\
static void Init() { \
static ore::StringView names[cCount]; \
ore::detail::EnumUtil::Parse(ore::IterRange<ore::StringView*>(names), cTextAll); \
ore::Enum<NAME>::Info().name = #NAME; \
ore::Enum<NAME>::Info().members = ore::IterRange<ore::StringView*>(names); \
} \
\
static constexpr int Size() { return cCount; } \
static constexpr Type Invalid() { return Type(Size()); } \
\
private: \
static constexpr const char* cTextAll = #__VA_ARGS__; \
static constexpr size_t cTextAllLen = sizeof(#__VA_ARGS__); \
static constexpr int cCount = ore::detail::EnumUtil::CountValues(cTextAll, cTextAllLen); \
};
// FIXME
template <class T>
class ValuedEnum {
public:
ValuedEnum() { T::Init(); }
static Enum<T>& Info() { return s_Info; }
StringView name{};
IterRange<StringView*> members{};
private:
static inline Enum<T> s_Info{};
};
#define ORE_VALUED_ENUM(NAME, ...) \
class NAME { \
public: \
enum Type { __VA_ARGS__ }; \
\
static void Init() { \
static ore::StringView names[cCount]; \
ore::detail::EnumUtil::Parse(ore::IterRange<ore::StringView*>(names), cTextAll); \
ore::ValuedEnum<NAME>::Info().name = #NAME; \
ore::ValuedEnum<NAME>::Info().members = ore::IterRange<ore::StringView*>(names); \
} \
\
static constexpr int Size() { return cCount; } \
static constexpr Type Invalid() { return Type(Size()); } \
\
private: \
static constexpr const char* cTextAll = #__VA_ARGS__; \
static constexpr size_t cTextAllLen = sizeof(#__VA_ARGS__); \
static constexpr int cCount = ore::detail::EnumUtil::CountValues(cTextAll, cTextAllLen); \
};
/// For storing an enum with a particular storage size when specifying the underlying type of the
/// enum is not an option.
template <typename Enum, typename Storage>
struct SizedEnum {
static_assert(std::is_enum<Enum>());
static_assert(!std::is_enum<Storage>());
constexpr SizedEnum() = default;
constexpr SizedEnum(Enum value) { *this = value; }
constexpr operator Enum() const { return static_cast<Enum>(mValue); }
constexpr SizedEnum& operator=(Enum value) {
mValue = static_cast<Storage>(value);
return *this;
}
Storage mValue;
};
} // namespace ore