20#include <AUI/Common/AString.h>
21#include <AUI/Common/AMap.h>
22#include <AUI/Traits/types.h>
23#include <fmt/format.h>
28namespace aui::enumerate::basic {
29 template<
typename enum_t>
31 template<enum_t value>
35 AString::iterator end = s.begin() + s.rfind(
'>');
36 AString::iterator begin = (std::find_if(std::make_reverse_iterator(end), s.rend(), [](
wchar_t c) {
37 return c ==
':' || c ==
'<';
42 AString s = __PRETTY_FUNCTION__;
44 auto end = s.rfind(
']');
46 auto end = s.rfind(
';');
48 size_t begin = s.rfind(
"value =", end);
49 if (begin == AString::NPOS) {
50 begin = s.rfind(
'[', end) + 1;
54 AString result = {s.begin() + begin, s.begin() + end};
57 for (
size_t p; (p = result.find(
"::")) != AString::NPOS;) {
58 result = result.substr(p + 2);
67namespace aui::enumerate {
68 template<
typename enum_t>
78template<
typename enum_t>
82 constexpr bool operator()(enum_t l, enum_t r)
const {
83 return static_cast<std::underlying_type_t<enum_t>
>(l) <
static_cast<std::underlying_type_t<enum_t>
>(r);
88 static_assert(std::is_enum_v<enum_t>,
"AEnumerate accepts only enums");
90 template<enum_t... values>
102 template<enum_t value>
128 throw AException(
"unknown enum value: \"{}\""_format(name));
138 throw AException(
"unknown enum value: \"(int){}\""_format((std::underlying_type_t<enum_t>)value));
142 template<enum_t... values>
145 {valueName<values>(), values}...
149 template<enum_t... values>
152 {values, valueName<values>() }...
158template<
typename enum_t>
161template<
typename enum_t>
163 static_assert(aui::is_complete<AEnumerateAllValues<enum_t>>,
"AUI_ENUM_VALUES is not defined for this enum type");
166 return mapValueByName(v);
169namespace aui::enumerate {
175 template<
typename enum_t>
requires aui::is_complete<AEnumerateAllValues<enum_t>>
177 constexpr enum_t ITEMS[] = {values...};
178 return std::to_array(ITEMS);
182template<
typename enum_t>
184 static_assert(aui::is_complete<AEnumerateAllValues<enum_t>>,
"AUI_ENUM_VALUES is not defined for this enum type");
187 return mapNameByValue(v);
193#define AUI_ENUM_VALUES(enum_t, ...) template<> \
194struct AEnumerateAllValues<enum_t>{ \
195 static inline constexpr AEnumerate<enum_t>::Values<__VA_ARGS__> get() {return {}; } \
197namespace std { inline AString to_wstring(enum_t v) { return AEnumerate<enum_t>::valueToNameMap().optional(v).valueOr("<unknown enum value {}>"_format(int(v))); } } \
198inline std::ostream& operator<<(std::ostream& o, enum_t v) { return o << std::to_wstring(v); }
200template <
typename T>
struct fmt::formatter<T, char, std::enable_if_t<aui::is_complete<AEnumerateAllValues<T>>>>: formatter<std::string> {
202 template <
typename FormatContext>
203 auto format(T c, FormatContext& ctx)
const {
210#define AUI_ENUM_FLAG(name) enum class name: int; \
211 constexpr inline name operator|(name a, name b) {return static_cast<name>(static_cast<int>(a) | static_cast<int>(b));} \
212 constexpr inline name operator&(name a, name b) {return static_cast<name>(static_cast<int>(a) & static_cast<int>(b));} \
213 constexpr inline name operator^(name a, name b) {return static_cast<name>(static_cast<int>(a) ^ static_cast<int>(b));} \
214 constexpr inline name operator|=(name& a, name b) {return a = static_cast<name>(static_cast<int>(a) | static_cast<int>(b));} \
215 constexpr inline name operator&=(name& a, name b) {return a = static_cast<name>(static_cast<int>(a) & static_cast<int>(b));} \
216 constexpr inline name operator^=(name& a, name b) {return a = static_cast<name>(static_cast<int>(a) ^ static_cast<int>(b));} \
217 constexpr inline name operator~(const name& a) {return static_cast<name>(~static_cast<int>(a));} \
218 constexpr inline bool operator!(const name& a) {return a == static_cast<name>(0);} \
220 constexpr inline bool operator&&(const name& a, bool v) {return static_cast<int>(a) && v;} \
221 constexpr inline bool operator||(const name& a, bool v) {return static_cast<int>(a) || v;} \
225#define AUI_ENUM_INT(name) enum class name: int; \
226 constexpr inline bool operator<(name a, name b) {return static_cast<int>(a) < static_cast<int>(b);} \
227 constexpr inline bool operator>(name a, name b) {return static_cast<int>(a) > static_cast<int>(b);} \
Enum trait to transform enum to name, name to enum, list all enums and vise versa.
Definition: AEnumerate.h:79
static const AMap< AString, enum_t > & nameToValueMap()
Get runtime name to enum value mapping.
Definition: AEnumerate.h:162
static AString valueName()
Maps compile-time specified enum value to name.
Definition: AEnumerate.h:103
static enum_t byName(const AString &name)
Map runtime name to enum value. Transforms name to uppercase as a fallback. Throws an exception if no...
Definition: AEnumerate.h:121
static const AMap< enum_t, AString, enum_less > & valueToNameMap()
Map runtime enum value to name.
Definition: AEnumerate.h:183
static const AString & toName(enum_t value)
Map runtime enum value to name. Throws an exception if no such value.
Definition: AEnumerate.h:134
Abstract AUI exception.
Definition: AException.h:29
A std::map with AUI extensions.
Definition: AMap.h:218
Represents a Unicode character string.
Definition: AString.h:37
constexpr auto ALL_VALUES
constexpr std::array of all possible enum values is the order they've been passed to AUI_ENUM_VALUES.
Definition: AEnumerate.h:176
bool contains(const Container &c, const typename Container::const_reference value) noexcept
Definition: containers.h:153
Definition: AEnumerate.h:159
Definition: AEnumerate.h:91
Definition: AEnumerate.h:69
Definition: AEnumerate.h:30