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>
149 template<enum_t... values>
150 static const AMap<enum_t, AString, enum_less>& mapNameByValue(
const Values<values...>& v) {
151 static AMap<enum_t, AString, enum_less> map = {
158template<
typename enum_t>
161template<
typename enum_t>
166 return mapValueByName(v);
169namespace aui::enumerate {
177 constexpr enum_t ITEMS[] = {values...};
178 return std::to_array(ITEMS);
182template<
typename enum_t>
187 return mapNameByValue(v);
208#define AUI_ENUM_VALUES(enum_t, ...) template<> \
209struct AEnumerateAllValues<enum_t>{ \
210 static inline constexpr AEnumerate<enum_t>::Values<__VA_ARGS__> get() {return {}; } \
212namespace std { inline AString to_wstring(enum_t v) { return AEnumerate<enum_t>::valueToNameMap().optional(v).valueOr("<unknown enum value {}>"_format(int(v))); } } \
213inline std::ostream& operator<<(std::ostream& o, enum_t v) { return o << std::to_wstring(v); }
215template <
typename T>
struct fmt::formatter<T, char, std::enable_if_t<aui::is_complete<AEnumerateAllValues<T>>>>: formatter<std::string> {
217 template <
typename FormatContext>
218 auto format(T c, FormatContext& ctx)
const {
225#define AUI_ENUM_FLAG(name) enum class name: int; \
226 constexpr inline name operator|(name a, name b) {return static_cast<name>(static_cast<int>(a) | static_cast<int>(b));} \
227 constexpr inline name operator&(name a, name b) {return static_cast<name>(static_cast<int>(a) & static_cast<int>(b));} \
228 constexpr inline name operator^(name a, name b) {return static_cast<name>(static_cast<int>(a) ^ static_cast<int>(b));} \
229 constexpr inline name operator|=(name& a, name b) {return a = static_cast<name>(static_cast<int>(a) | static_cast<int>(b));} \
230 constexpr inline name operator&=(name& a, name b) {return a = static_cast<name>(static_cast<int>(a) & static_cast<int>(b));} \
231 constexpr inline name operator^=(name& a, name b) {return a = static_cast<name>(static_cast<int>(a) ^ static_cast<int>(b));} \
232 constexpr inline name operator~(const name& a) {return static_cast<name>(~static_cast<int>(a));} \
233 constexpr inline bool operator!(const name& a) {return a == static_cast<name>(0);} \
235 constexpr inline bool operator&&(const name& a, bool v) {return static_cast<int>(a) && v;} \
236 constexpr inline bool operator||(const name& a, bool v) {return static_cast<int>(a) || v;} \
240#define AUI_ENUM_INT(name) enum class name: int; \
241 constexpr inline bool operator<(name a, name b) {return static_cast<int>(a) < static_cast<int>(b);} \
242 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:28
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
constexpr bool is_complete
Determines whether T is complete or not.
Definition types.h:23
Definition AEnumerate.h:159
Definition AEnumerate.h:91
Definition AEnumerate.h:69
Definition AEnumerate.h:30