-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwise_enum.h
108 lines (86 loc) · 3.59 KB
/
wise_enum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include "wise_enum_detail.h"
#include "wise_enum_generated.h"
#include <algorithm>
#include <array>
#include <type_traits>
#include <utility>
/*
Macro interface
The first argument to any macro enum is either the name of the enum, or a
parenthesized pair, (name, storage). The first form results in unfixed
underlying type for the enum, while the second form defines it explicitly.
After that, the macros take a variadic enumerator list. Each entry in the list
can be either a legal identifier name, or it can be a parenthesized pair where
the first entry is the identifier and the second entry is the initializer.
Usage notes:
- A semi-colon after the macro invocation is not necessary, and your
compiler may warn for it
*/
// Declare an enum at namespace scope
#define WISE_ENUM(name, ...) WISE_ENUM_IMPL(enum, name, , __VA_ARGS__)
// Declare an enum class at namespace scope
#define WISE_ENUM_CLASS(name, ...) \
WISE_ENUM_IMPL(enum class, name, , __VA_ARGS__)
// Declare an enum at class scope
#define WISE_ENUM_MEMBER(name, ...) \
WISE_ENUM_IMPL(enum, name, friend, __VA_ARGS__)
// Declare an enum class at class scope
#define WISE_ENUM_CLASS_MEMBER(name, ...) \
WISE_ENUM_IMPL(enum class, name, friend, __VA_ARGS__)
/*
Adapt an existing enum into the wise enum API. This macro must be used at
global scope. The first argument must be the name of the enum (qualified),
followed by all the enumerators of the enum.
*/
#define WISE_ENUM_ADAPT(name, ...) WISE_ENUM_IMPL_ADAPT(name, __VA_ARGS__)
namespace wise_enum {
// Returns the string representation of an enumerator
template <class T>
constexpr string_type to_string(T t) {
return wise_enum_detail_to_string(t, detail::Tag<T>{});
}
// Enumerators trait class. Each value is also available as a template variable
// for C++14 and on
template <class T>
struct enumerators {
// For a given wise enum type, this variable allows iteration over enumerators
// and their string names in the declared order. Each iterated object is a
// struct with members { T value; string_type name; }
static constexpr decltype(wise_enum_detail_array(detail::Tag<T>{})) range =
wise_enum_detail_array(detail::Tag<T>{});
// This variable is equal to the number of enumerators for the wise enum type.
static constexpr std::size_t size = range.size();
};
template <class T>
constexpr decltype(
wise_enum_detail_array(detail::Tag<T>{})) enumerators<T>::range;
template <class T>
constexpr std::size_t enumerators<T>::size;
#if __cplusplus >= 201402
template <class T>
constexpr auto &range = enumerators<T>::range;
template <class T>
constexpr std::size_t size = enumerators<T>::size;
#endif
// A type trait; this allows checking if a type is a wise_enum in generic code
template <class T>
using is_wise_enum = detail::is_wise_enum<T>;
#if __cplusplus >= 201402
template <class T>
static constexpr bool is_wise_enum_v = is_wise_enum<T>::value;
#endif
// Converts a string literal into a wise enum. Returns an optional<T>; if no
// enumerator has name matching the string, the optional is returned empty.
template <class T>
WISE_ENUM_CONSTEXPR_14 optional_type<T> from_string(string_type s) {
auto it =
std::find_if(enumerators<T>::range.begin(), enumerators<T>::range.end(),
[=](const detail::value_and_name<T> &x) {
return ::wise_enum::detail::compare(x.name, s);
});
if (it == enumerators<T>::range.end())
return {};
return it->value;
}
} // namespace wise_enum