-
Notifications
You must be signed in to change notification settings - Fork 37
/
enums_test_common.hpp
284 lines (241 loc) · 7.78 KB
/
enums_test_common.hpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#pragma once
#include "fixed_containers/enum_map.hpp"
#include "fixed_containers/enum_utils.hpp"
#include <magic_enum.hpp>
#include <array>
#include <cstddef>
#include <string_view>
namespace example
{
// DEFINITION
namespace detail
{
enum class ColorBackingEnum
{
RED,
YELLOW,
BLUE,
GREEN,
};
} // namespace detail
// SkeletalRichEnum base class automatically provides all general-purpose functionality.
class Color : public fixed_containers::rich_enums::SkeletalRichEnum<Color, detail::ColorBackingEnum>
{
friend SkeletalRichEnum::ValuesFriend;
using SkeletalRichEnum::SkeletalRichEnum;
public:
// Forwarders for the enum constants. Macro not strictly required, see subsequent examples.
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(Color, RED)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(Color, YELLOW)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(Color, BLUE)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(Color, GREEN)
// Custom member function
[[nodiscard]] constexpr bool is_primary() const
{
return backing_enum() == detail::ColorBackingEnum::RED ||
backing_enum() == detail::ColorBackingEnum::YELLOW ||
backing_enum() == detail::ColorBackingEnum::BLUE;
}
};
// USAGE
static_assert(fixed_containers::rich_enums::is_rich_enum<Color>); // Type-trait `concept`
inline constexpr Color COLOR = Color::RED(); // Note the parens
static_assert("RED" == COLOR.to_string()); // auto-provided member
static_assert(COLOR.is_primary()); // Custom member
static_assert(COLOR == Color::value_of("RED").value()); // auto-provided
static_assert(4 == Color::count()); // auto-provided
} // namespace example
namespace fixed_containers::rich_enums
{
enum class TestEnum1
{
ONE = 0,
TWO,
THREE,
FOUR,
};
enum class EnumWithNoConstants
{
};
namespace detail
{
enum class TestRichEnum1BackingEnum : std::uint32_t
{
C_ONE = 19,
C_FOUR = 25,
C_TWO = 21,
C_THREE = 23,
};
// If we have data to associate with each enum constant, we can put them in a map here, then provide
// them with rich enum member functions.
struct TestRichEnum1Data
{
std::size_t value;
double double_value;
};
inline constexpr auto TEST_RICH_ENUM_1_DATA =
EnumMap<TestRichEnum1BackingEnum, TestRichEnum1Data>::create_with_all_entries({
{TestRichEnum1BackingEnum::C_ONE, {1, 1.0}},
{TestRichEnum1BackingEnum::C_TWO, {2, 2.0}},
{TestRichEnum1BackingEnum::C_THREE, {3, 3.0}},
{TestRichEnum1BackingEnum::C_FOUR, {4, 4.0}},
});
} // namespace detail
class TestRichEnum1 : public SkeletalRichEnum<TestRichEnum1, detail::TestRichEnum1BackingEnum>
{
friend SkeletalRichEnum::ValuesFriend;
using SkeletalRichEnum::SkeletalRichEnum;
public:
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnum1, C_ONE)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnum1, C_TWO)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnum1, C_THREE)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnum1, C_FOUR)
public:
// Provide associated data with member functions.
[[nodiscard]] constexpr std::size_t value() const
{
return detail::TEST_RICH_ENUM_1_DATA.at(backing_enum()).value;
}
[[nodiscard]] constexpr double double_value() const
{
return detail::TEST_RICH_ENUM_1_DATA.at(backing_enum()).double_value;
}
};
// Example with infused data
namespace detail
{
enum class TestRichEnum2BackingEnum
{
C_ONE,
C_TWO,
C_THREE,
C_FOUR,
};
struct TestRichEnum2Data
{
std::size_t value;
};
struct TestRichEnum2InfusedData
{
using BE = TestRichEnum2BackingEnum;
static constexpr auto VALUES = EnumMap<BE, TestRichEnum2Data>::create_with_all_entries({
{BE::C_ONE, {1}},
{BE::C_TWO, {2}},
{BE::C_THREE, {3}},
{BE::C_FOUR, {4}},
});
};
} // namespace detail
class TestRichEnum2
: public NonDefaultConstructibleSkeletalRichEnum<TestRichEnum2, detail::TestRichEnum2BackingEnum>
{
friend SkeletalRichEnum::ValuesFriend;
using NonDefaultConstructibleSkeletalRichEnum::NonDefaultConstructibleSkeletalRichEnum;
public:
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnum2, C_ONE)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnum2, C_TWO)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnum2, C_THREE)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnum2, C_FOUR)
public:
[[nodiscard]] constexpr std::size_t value() const
{
return detail::TestRichEnum2InfusedData::VALUES.at(backing_enum()).value;
}
};
// This class does not have ordinal() and count()
// Instead it has differently named functions
class NonConformingTestRichEnum1
{
friend class NonConformingTestRichEnum1Values;
private:
enum class BackingEnum
{
NC_ONE,
NC_TWO,
};
public:
static constexpr std::size_t size() { return magic_enum::enum_count<BackingEnum>(); }
static constexpr const std::array<NonConformingTestRichEnum1,
magic_enum::enum_count<BackingEnum>()>&
all_values();
private:
static constexpr NonConformingTestRichEnum1 at(const BackingEnum& backing_enum)
{
return all_values().at(magic_enum::enum_index(backing_enum).value());
}
public:
static constexpr NonConformingTestRichEnum1 NC_ONE() // NOLINT(readability-identifier-naming)
{
return at(BackingEnum::NC_ONE);
}
static constexpr NonConformingTestRichEnum1 NC_TWO() // NOLINT(readability-identifier-naming)
{
return at(BackingEnum::NC_TWO);
}
private:
BackingEnum backing_enum_;
constexpr NonConformingTestRichEnum1(BackingEnum backing_enum)
: backing_enum_(backing_enum)
{
}
public:
[[nodiscard]] constexpr std::size_t index() const
{
return static_cast<std::size_t>(magic_enum::enum_integer(backing_enum_));
}
[[nodiscard]] constexpr std::string_view str() const
{
return magic_enum::enum_name(backing_enum_);
}
};
class NonConformingTestRichEnum1Values
{
friend class NonConformingTestRichEnum1;
using BackingEnum = NonConformingTestRichEnum1::BackingEnum;
static constexpr std::array<NonConformingTestRichEnum1, NonConformingTestRichEnum1::size()>
VALUES{
NonConformingTestRichEnum1{BackingEnum::NC_ONE},
{BackingEnum::NC_TWO},
};
};
constexpr const std::array<NonConformingTestRichEnum1, NonConformingTestRichEnum1::size()>&
NonConformingTestRichEnum1::all_values()
{
return NonConformingTestRichEnum1Values::VALUES;
}
template <>
struct EnumAdapter<NonConformingTestRichEnum1>
{
using Enum = NonConformingTestRichEnum1;
static constexpr std::size_t count() { return NonConformingTestRichEnum1::size(); }
static constexpr const std::array<NonConformingTestRichEnum1, 2>& values()
{
return NonConformingTestRichEnum1::all_values();
}
static constexpr std::size_t ordinal(const NonConformingTestRichEnum1& key)
{
return key.index();
}
static constexpr std::string_view to_string(const NonConformingTestRichEnum1& key)
{
return key.str();
}
};
namespace detail
{
enum class TestRichEnumBoolBackingEnum : bool
{
FALSE_VALUE = false,
TRUE_VALUE = true,
};
} // namespace detail
class TestRichEnumBool
: public SkeletalRichEnum<TestRichEnumBool, detail::TestRichEnumBoolBackingEnum>
{
friend SkeletalRichEnum::ValuesFriend;
using SkeletalRichEnum::SkeletalRichEnum;
public:
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnumBool, FALSE_VALUE)
FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(TestRichEnumBool, TRUE_VALUE)
};
} // namespace fixed_containers::rich_enums