3 #include "wise_enum_detail.h"
4 #include "wise_enum_generated.h"
14 The first argument to any macro enum is either the name of the enum, or a
15 parenthesized pair, (name, storage). The first form results in unfixed
16 underlying type for the enum, while the second form defines it explicitly.
17 After that, the macros take a variadic enumerator list. Each entry in the list
18 can be either a legal identifier name, or it can be a parenthesized pair where
19 the first entry is the identifier and the second entry is the initializer.
22 - A semi-colon after the macro invocation is not necessary, and your
23 compiler may warn for it
26 // Declare an enum at namespace scope
27 #define WISE_ENUM(name, ...) WISE_ENUM_IMPL(enum, name, , __VA_ARGS__)
29 // Declare an enum class at namespace scope
30 #define WISE_ENUM_CLASS(name, ...) \
31 WISE_ENUM_IMPL(enum class, name, , __VA_ARGS__)
33 // Declare an enum at class scope
34 #define WISE_ENUM_MEMBER(name, ...) \
35 WISE_ENUM_IMPL(enum, name, friend, __VA_ARGS__)
37 // Declare an enum class at class scope
38 #define WISE_ENUM_CLASS_MEMBER(name, ...) \
39 WISE_ENUM_IMPL(enum class, name, friend, __VA_ARGS__)
42 Adapt an existing enum into the wise enum API. This macro must be used at
43 global scope. The first argument must be the name of the enum (qualified),
44 followed by all the enumerators of the enum.
46 #define WISE_ENUM_ADAPT(name, ...) WISE_ENUM_IMPL_ADAPT(name, __VA_ARGS__)
50 // Returns the string representation of an enumerator
52 constexpr string_type
to_string(T t
) {
53 return wise_enum_detail_to_string(t
, detail::Tag
<T
>{});
56 // Enumerators trait class. Each value is also available as a template variable
60 // For a given wise enum type, this variable allows iteration over enumerators
61 // and their string names in the declared order. Each iterated object is a
62 // struct with members { T value; string_type name; }
63 static constexpr decltype(wise_enum_detail_array(detail::Tag
<T
>{})) range
=
64 wise_enum_detail_array(detail::Tag
<T
>{});
66 // This variable is equal to the number of enumerators for the wise enum type.
67 static constexpr std::size_t size
= range
.size();
72 wise_enum_detail_array(detail::Tag
<T
>{})) enumerators
<T
>::range
;
75 constexpr std::size_t enumerators
<T
>::size
;
77 #if __cplusplus >= 201402
79 constexpr auto &range
= enumerators
<T
>::range
;
82 constexpr std::size_t size
= enumerators
<T
>::size
;
85 // A type trait; this allows checking if a type is a wise_enum in generic code
87 using is_wise_enum
= detail::is_wise_enum
<T
>;
89 #if __cplusplus >= 201402
91 static constexpr bool is_wise_enum_v
= is_wise_enum
<T
>::value
;
94 // Converts a string literal into a wise enum. Returns an optional<T>; if no
95 // enumerator has name matching the string, the optional is returned empty.
97 WISE_ENUM_CONSTEXPR_14 optional_type
<T
> from_string(string_type s
) {
99 std::find_if(enumerators
<T
>::range
.begin(), enumerators
<T
>::range
.end(),
100 [=](const detail::value_and_name
<T
> &x
) {
101 return ::wise_enum::detail::compare(x
.name
, s
);
103 if (it
== enumerators
<T
>::range
.end())
108 } // namespace wise_enum