Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/magic_enum/include/magic_enum.hpp
2 views
1
// __ __ _ ______ _____
2
// | \/ | (_) | ____| / ____|_ _
3
// | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
4
// | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
5
// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
6
// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
7
// __/ | https://github.com/Neargye/magic_enum
8
// |___/ version 0.7.3
9
//
10
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
11
// SPDX-License-Identifier: MIT
12
// Copyright (c) 2019 - 2021 Daniil Goncharov <[email protected]>.
13
//
14
// Permission is hereby granted, free of charge, to any person obtaining a copy
15
// of this software and associated documentation files (the "Software"), to deal
16
// in the Software without restriction, including without limitation the rights
17
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
// copies of the Software, and to permit persons to whom the Software is
19
// furnished to do so, subject to the following conditions:
20
//
21
// The above copyright notice and this permission notice shall be included in all
22
// copies or substantial portions of the Software.
23
//
24
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
// SOFTWARE.
31
32
#ifndef NEARGYE_MAGIC_ENUM_HPP
33
#define NEARGYE_MAGIC_ENUM_HPP
34
35
#define MAGIC_ENUM_VERSION_MAJOR 0
36
#define MAGIC_ENUM_VERSION_MINOR 7
37
#define MAGIC_ENUM_VERSION_PATCH 3
38
39
#include <array>
40
#include <cassert>
41
#include <cstdint>
42
#include <cstddef>
43
#include <iosfwd>
44
#include <limits>
45
#include <type_traits>
46
#include <utility>
47
48
#if defined(MAGIC_ENUM_CONFIG_FILE)
49
#include MAGIC_ENUM_CONFIG_FILE
50
#endif
51
52
#if !defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL)
53
#include <optional>
54
#endif
55
#if !defined(MAGIC_ENUM_USING_ALIAS_STRING)
56
#include <string>
57
#endif
58
#if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
59
#include <string_view>
60
#endif
61
62
#if defined(__clang__)
63
# pragma clang diagnostic push
64
#elif defined(__GNUC__)
65
# pragma GCC diagnostic push
66
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'.
67
#elif defined(_MSC_VER)
68
# pragma warning(push)
69
# pragma warning(disable : 26495) // Variable 'static_string<N>::chars_' is uninitialized.
70
# pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value.
71
# pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call.
72
#endif
73
74
// Checks magic_enum compiler compatibility.
75
#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910
76
# undef MAGIC_ENUM_SUPPORTED
77
# define MAGIC_ENUM_SUPPORTED 1
78
#endif
79
80
// Checks magic_enum compiler aliases compatibility.
81
#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920
82
# undef MAGIC_ENUM_SUPPORTED_ALIASES
83
# define MAGIC_ENUM_SUPPORTED_ALIASES 1
84
#endif
85
86
// Enum value must be greater or equals than MAGIC_ENUM_RANGE_MIN. By default MAGIC_ENUM_RANGE_MIN = -128.
87
// If need another min range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN.
88
#if !defined(MAGIC_ENUM_RANGE_MIN)
89
# define MAGIC_ENUM_RANGE_MIN -128
90
#endif
91
92
// Enum value must be less or equals than MAGIC_ENUM_RANGE_MAX. By default MAGIC_ENUM_RANGE_MAX = 128.
93
// If need another max range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX.
94
#if !defined(MAGIC_ENUM_RANGE_MAX)
95
# define MAGIC_ENUM_RANGE_MAX 128
96
#endif
97
98
namespace magic_enum {
99
100
// If need another optional type, define the macro MAGIC_ENUM_USING_ALIAS_OPTIONAL.
101
#if defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL)
102
MAGIC_ENUM_USING_ALIAS_OPTIONAL
103
#else
104
using std::optional;
105
#endif
106
107
// If need another string_view type, define the macro MAGIC_ENUM_USING_ALIAS_STRING_VIEW.
108
#if defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
109
MAGIC_ENUM_USING_ALIAS_STRING_VIEW
110
#else
111
using std::string_view;
112
#endif
113
114
// If need another string type, define the macro MAGIC_ENUM_USING_ALIAS_STRING.
115
#if defined(MAGIC_ENUM_USING_ALIAS_STRING)
116
MAGIC_ENUM_USING_ALIAS_STRING
117
#else
118
using std::string;
119
#endif
120
121
namespace customize {
122
123
// Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128.
124
// If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX.
125
// If need another range for specific enum type, add specialization enum_range for necessary enum type.
126
template <typename E>
127
struct enum_range {
128
static_assert(std::is_enum_v<E>, "magic_enum::customize::enum_range requires enum type.");
129
inline static constexpr int min = MAGIC_ENUM_RANGE_MIN;
130
inline static constexpr int max = MAGIC_ENUM_RANGE_MAX;
131
static_assert(max > min, "magic_enum::customize::enum_range requires max > min.");
132
};
133
134
static_assert(MAGIC_ENUM_RANGE_MIN <= 0, "MAGIC_ENUM_RANGE_MIN must be less or equals than 0.");
135
static_assert(MAGIC_ENUM_RANGE_MIN > (std::numeric_limits<std::int16_t>::min)(), "MAGIC_ENUM_RANGE_MIN must be greater than INT16_MIN.");
136
137
static_assert(MAGIC_ENUM_RANGE_MAX > 0, "MAGIC_ENUM_RANGE_MAX must be greater than 0.");
138
static_assert(MAGIC_ENUM_RANGE_MAX < (std::numeric_limits<std::int16_t>::max)(), "MAGIC_ENUM_RANGE_MAX must be less than INT16_MAX.");
139
140
static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN.");
141
142
// If need custom names for enum, add specialization enum_name for necessary enum type.
143
template <typename E>
144
constexpr string_view enum_name(E) noexcept {
145
static_assert(std::is_enum_v<E>, "magic_enum::customize::enum_name requires enum type.");
146
147
return {};
148
}
149
150
} // namespace magic_enum::customize
151
152
namespace detail {
153
154
template <typename T>
155
struct supported
156
#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT)
157
: std::true_type {};
158
#else
159
: std::false_type {};
160
#endif
161
162
struct char_equal_to {
163
constexpr bool operator()(char lhs, char rhs) const noexcept {
164
return lhs == rhs;
165
}
166
};
167
168
template <std::size_t N>
169
class static_string {
170
public:
171
constexpr explicit static_string(string_view str) noexcept : static_string{str, std::make_index_sequence<N>{}} {
172
assert(str.size() == N);
173
}
174
175
constexpr const char* data() const noexcept { return chars_; }
176
177
constexpr std::size_t size() const noexcept { return N; }
178
179
constexpr operator string_view() const noexcept { return {data(), size()}; }
180
181
private:
182
template <std::size_t... I>
183
constexpr static_string(string_view str, std::index_sequence<I...>) noexcept : chars_{str[I]..., '\0'} {}
184
185
char chars_[N + 1];
186
};
187
188
template <>
189
class static_string<0> {
190
public:
191
constexpr explicit static_string(string_view) noexcept {}
192
193
constexpr const char* data() const noexcept { return nullptr; }
194
195
constexpr std::size_t size() const noexcept { return 0; }
196
197
constexpr operator string_view() const noexcept { return {}; }
198
};
199
200
constexpr string_view pretty_name(string_view name) noexcept {
201
for (std::size_t i = name.size(); i > 0; --i) {
202
if (!((name[i - 1] >= '0' && name[i - 1] <= '9') ||
203
(name[i - 1] >= 'a' && name[i - 1] <= 'z') ||
204
(name[i - 1] >= 'A' && name[i - 1] <= 'Z') ||
205
(name[i - 1] == '_'))) {
206
name.remove_prefix(i);
207
break;
208
}
209
}
210
211
if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') ||
212
(name.front() >= 'A' && name.front() <= 'Z') ||
213
(name.front() == '_'))) {
214
return name;
215
}
216
217
return {}; // Invalid name.
218
}
219
220
constexpr std::size_t find(string_view str, char c) noexcept {
221
#if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__)
222
// https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc
223
// https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html
224
constexpr bool workaround = true;
225
#else
226
constexpr bool workaround = false;
227
#endif
228
if constexpr (workaround) {
229
for (std::size_t i = 0; i < str.size(); ++i) {
230
if (str[i] == c) {
231
return i;
232
}
233
}
234
235
return string_view::npos;
236
} else {
237
return str.find_first_of(c);
238
}
239
}
240
241
template <typename T, std::size_t N, std::size_t... I>
242
constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N], std::index_sequence<I...>) {
243
return {{a[I]...}};
244
}
245
246
template <typename BinaryPredicate>
247
constexpr bool cmp_equal(string_view lhs, string_view rhs, BinaryPredicate&& p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) {
248
#if defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__)
249
// https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html
250
// https://developercommunity.visualstudio.com/content/problem/232218/c-constexpr-string-view.html
251
constexpr bool workaround = true;
252
#else
253
constexpr bool workaround = false;
254
#endif
255
constexpr bool default_predicate = std::is_same_v<std::decay_t<BinaryPredicate>, char_equal_to>;
256
257
if constexpr (default_predicate && !workaround) {
258
static_cast<void>(p);
259
return lhs == rhs;
260
} else {
261
if (lhs.size() != rhs.size()) {
262
return false;
263
}
264
265
const auto size = lhs.size();
266
for (std::size_t i = 0; i < size; ++i) {
267
if (!p(lhs[i], rhs[i])) {
268
return false;
269
}
270
}
271
272
return true;
273
}
274
}
275
276
template <typename L, typename R>
277
constexpr bool cmp_less(L lhs, R rhs) noexcept {
278
static_assert(std::is_integral_v<L> && std::is_integral_v<R>, "magic_enum::detail::cmp_less requires integral type.");
279
280
if constexpr (std::is_signed_v<L> == std::is_signed_v<R>) {
281
// If same signedness (both signed or both unsigned).
282
return lhs < rhs;
283
} else if constexpr (std::is_signed_v<R>) {
284
// If 'right' is negative, then result is 'false', otherwise cast & compare.
285
return rhs > 0 && lhs < static_cast<std::make_unsigned_t<R>>(rhs);
286
} else {
287
// If 'left' is negative, then result is 'true', otherwise cast & compare.
288
return lhs < 0 || static_cast<std::make_unsigned_t<L>>(lhs) < rhs;
289
}
290
}
291
292
template <typename I>
293
constexpr I log2(I value) noexcept {
294
static_assert(std::is_integral_v<I>, "magic_enum::detail::log2 requires integral type.");
295
296
auto ret = I{0};
297
for (; value > I{1}; value >>= I{1}, ++ret) {}
298
299
return ret;
300
}
301
302
template <typename I>
303
constexpr bool is_pow2(I x) noexcept {
304
static_assert(std::is_integral_v<I>, "magic_enum::detail::is_pow2 requires integral type.");
305
306
return x != 0 && (x & (x - 1)) == 0;
307
}
308
309
template <typename T>
310
inline constexpr bool is_enum_v = std::is_enum_v<T> && std::is_same_v<T, std::decay_t<T>>;
311
312
template <typename E>
313
constexpr auto n() noexcept {
314
static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
315
#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
316
# if defined(__clang__)
317
constexpr string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36};
318
# elif defined(__GNUC__)
319
constexpr string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51};
320
# elif defined(_MSC_VER)
321
constexpr string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57};
322
# endif
323
return static_string<name.size()>{name};
324
#else
325
return string_view{}; // Unsupported compiler.
326
#endif
327
}
328
329
template <typename E>
330
inline constexpr auto type_name_v = n<E>();
331
332
template <typename E, E V>
333
constexpr auto n() noexcept {
334
static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
335
constexpr auto custom_name = customize::enum_name<E>(V);
336
337
if constexpr (custom_name.empty()) {
338
static_cast<void>(custom_name);
339
#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
340
# if defined(__clang__) || defined(__GNUC__)
341
constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
342
# elif defined(_MSC_VER)
343
constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
344
# endif
345
return static_string<name.size()>{name};
346
#else
347
return string_view{}; // Unsupported compiler.
348
#endif
349
} else {
350
return static_string<custom_name.size()>{custom_name};
351
}
352
}
353
354
template <typename E, E V>
355
inline constexpr auto enum_name_v = n<E, V>();
356
357
template <typename E, auto V>
358
constexpr bool is_valid() noexcept {
359
static_assert(is_enum_v<E>, "magic_enum::detail::is_valid requires enum type.");
360
361
return n<E, static_cast<E>(V)>().size() != 0;
362
}
363
364
template <typename E, int O, bool IsFlags = false, typename U = std::underlying_type_t<E>>
365
constexpr E value(std::size_t i) noexcept {
366
static_assert(is_enum_v<E>, "magic_enum::detail::value requires enum type.");
367
368
if constexpr (IsFlags) {
369
return static_cast<E>(U{1} << static_cast<U>(static_cast<int>(i) + O));
370
} else {
371
return static_cast<E>(static_cast<int>(i) + O);
372
}
373
}
374
375
template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
376
constexpr int reflected_min() noexcept {
377
static_assert(is_enum_v<E>, "magic_enum::detail::reflected_min requires enum type.");
378
379
if constexpr (IsFlags) {
380
return 0;
381
} else {
382
constexpr auto lhs = customize::enum_range<E>::min;
383
static_assert(lhs > (std::numeric_limits<std::int16_t>::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN.");
384
constexpr auto rhs = (std::numeric_limits<U>::min)();
385
386
if constexpr (cmp_less(lhs, rhs)) {
387
return rhs;
388
} else {
389
static_assert(!is_valid<E, value<E, lhs - 1, IsFlags>(0)>(), "magic_enum::enum_range detects enum value smaller than min range size.");
390
return lhs;
391
}
392
}
393
}
394
395
template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
396
constexpr int reflected_max() noexcept {
397
static_assert(is_enum_v<E>, "magic_enum::detail::reflected_max requires enum type.");
398
399
if constexpr (IsFlags) {
400
return std::numeric_limits<U>::digits - 1;
401
} else {
402
constexpr auto lhs = customize::enum_range<E>::max;
403
static_assert(lhs < (std::numeric_limits<std::int16_t>::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX.");
404
constexpr auto rhs = (std::numeric_limits<U>::max)();
405
406
if constexpr (cmp_less(lhs, rhs)) {
407
static_assert(!is_valid<E, value<E, lhs + 1, IsFlags>(0)>(), "magic_enum::enum_range detects enum value larger than max range size.");
408
return lhs;
409
} else {
410
return rhs;
411
}
412
}
413
}
414
415
template <typename E, bool IsFlags = false>
416
inline constexpr auto reflected_min_v = reflected_min<E, IsFlags>();
417
418
template <typename E, bool IsFlags = false>
419
inline constexpr auto reflected_max_v = reflected_max<E, IsFlags>();
420
421
template <std::size_t N>
422
constexpr std::size_t values_count(const bool (&valid)[N]) noexcept {
423
auto count = std::size_t{0};
424
for (std::size_t i = 0; i < N; ++i) {
425
if (valid[i]) {
426
++count;
427
}
428
}
429
430
return count;
431
}
432
433
template <typename E, bool IsFlags, int Min, std::size_t... I>
434
constexpr auto values(std::index_sequence<I...>) noexcept {
435
static_assert(is_enum_v<E>, "magic_enum::detail::values requires enum type.");
436
constexpr bool valid[sizeof...(I)] = {is_valid<E, value<E, Min, IsFlags>(I)>()...};
437
constexpr std::size_t count = values_count(valid);
438
439
if constexpr (count > 0) {
440
E values[count] = {};
441
for (std::size_t i = 0, v = 0; v < count; ++i) {
442
if (valid[i]) {
443
values[v++] = value<E, Min, IsFlags>(i);
444
}
445
}
446
447
return to_array(values, std::make_index_sequence<count>{});
448
} else {
449
return std::array<E, 0>{};
450
}
451
}
452
453
template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
454
constexpr auto values() noexcept {
455
static_assert(is_enum_v<E>, "magic_enum::detail::values requires enum type.");
456
constexpr auto min = reflected_min_v<E, IsFlags>;
457
constexpr auto max = reflected_max_v<E, IsFlags>;
458
constexpr auto range_size = max - min + 1;
459
static_assert(range_size > 0, "magic_enum::enum_range requires valid size.");
460
static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "magic_enum::enum_range requires valid size.");
461
462
return values<E, IsFlags, reflected_min_v<E, IsFlags>>(std::make_index_sequence<range_size>{});
463
}
464
465
template <typename E, bool IsFlags = false>
466
inline constexpr auto values_v = values<E, IsFlags>();
467
468
template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
469
using values_t = decltype((values_v<D, IsFlags>));
470
471
template <typename E, bool IsFlags = false>
472
inline constexpr auto count_v = values_v<E, IsFlags>.size();
473
474
template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
475
inline constexpr auto min_v = (count_v<E, IsFlags> > 0) ? static_cast<U>(values_v<E, IsFlags>.front()) : U{0};
476
477
template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
478
inline constexpr auto max_v = (count_v<E, IsFlags> > 0) ? static_cast<U>(values_v<E, IsFlags>.back()) : U{0};
479
480
template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
481
constexpr std::size_t range_size() noexcept {
482
static_assert(is_enum_v<E>, "magic_enum::detail::range_size requires enum type.");
483
constexpr auto max = IsFlags ? log2(max_v<E, IsFlags>) : max_v<E, IsFlags>;
484
constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
485
constexpr auto range_size = max - min + U{1};
486
static_assert(range_size > 0, "magic_enum::enum_range requires valid size.");
487
static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "magic_enum::enum_range requires valid size.");
488
489
return static_cast<std::size_t>(range_size);
490
}
491
492
template <typename E, bool IsFlags = false>
493
inline constexpr auto range_size_v = range_size<E, IsFlags>();
494
495
template <typename E, bool IsFlags = false>
496
using index_t = std::conditional_t<range_size_v<E, IsFlags> < (std::numeric_limits<std::uint8_t>::max)(), std::uint8_t, std::uint16_t>;
497
498
template <typename E, bool IsFlags = false>
499
inline constexpr auto invalid_index_v = (std::numeric_limits<index_t<E, IsFlags>>::max)();
500
501
template <typename E, bool IsFlags, std::size_t... I>
502
constexpr auto indexes(std::index_sequence<I...>) noexcept {
503
static_assert(is_enum_v<E>, "magic_enum::detail::indexes requires enum type.");
504
constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
505
[[maybe_unused]] auto i = index_t<E, IsFlags>{0};
506
507
return std::array<decltype(i), sizeof...(I)>{{(is_valid<E, value<E, min, IsFlags>(I)>() ? i++ : invalid_index_v<E, IsFlags>)...}};
508
}
509
510
template <typename E, bool IsFlags = false>
511
inline constexpr auto indexes_v = indexes<E, IsFlags>(std::make_index_sequence<range_size_v<E, IsFlags>>{});
512
513
template <typename E, bool IsFlags, std::size_t... I>
514
constexpr auto names(std::index_sequence<I...>) noexcept {
515
static_assert(is_enum_v<E>, "magic_enum::detail::names requires enum type.");
516
517
return std::array<string_view, sizeof...(I)>{{enum_name_v<E, values_v<E, IsFlags>[I]>...}};
518
}
519
520
template <typename E, bool IsFlags = false>
521
inline constexpr auto names_v = names<E, IsFlags>(std::make_index_sequence<count_v<E, IsFlags>>{});
522
523
template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
524
using names_t = decltype((names_v<D, IsFlags>));
525
526
template <typename E, bool IsFlags, std::size_t... I>
527
constexpr auto entries(std::index_sequence<I...>) noexcept {
528
static_assert(is_enum_v<E>, "magic_enum::detail::entries requires enum type.");
529
530
return std::array<std::pair<E, string_view>, sizeof...(I)>{{{values_v<E, IsFlags>[I], enum_name_v<E, values_v<E, IsFlags>[I]>}...}};
531
}
532
533
template <typename E, bool IsFlags = false>
534
inline constexpr auto entries_v = entries<E, IsFlags>(std::make_index_sequence<count_v<E, IsFlags>>{});
535
536
template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
537
using entries_t = decltype((entries_v<D, IsFlags>));
538
539
template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
540
constexpr bool is_sparse() noexcept {
541
static_assert(is_enum_v<E>, "magic_enum::detail::is_sparse requires enum type.");
542
543
return range_size_v<E, IsFlags> != count_v<E, IsFlags>;
544
}
545
546
template <typename E, bool IsFlags = false>
547
inline constexpr bool is_sparse_v = is_sparse<E, IsFlags>();
548
549
template <typename E, typename U = std::underlying_type_t<E>>
550
constexpr std::size_t undex(U value) noexcept {
551
static_assert(is_enum_v<E>, "magic_enum::detail::undex requires enum type.");
552
553
if (const auto i = static_cast<std::size_t>(value - min_v<E>); value >= min_v<E> && value <= max_v<E>) {
554
if constexpr (is_sparse_v<E>) {
555
if (const auto idx = indexes_v<E>[i]; idx != invalid_index_v<E>) {
556
return idx;
557
}
558
} else {
559
return i;
560
}
561
}
562
563
return invalid_index_v<E>; // Value out of range.
564
}
565
566
template <typename E, typename U = std::underlying_type_t<E>>
567
constexpr std::size_t endex(E value) noexcept {
568
static_assert(is_enum_v<E>, "magic_enum::detail::endex requires enum type.");
569
570
return undex<E>(static_cast<U>(value));
571
}
572
573
template <typename E, typename U = std::underlying_type_t<E>>
574
constexpr U value_ors() noexcept {
575
static_assert(is_enum_v<E>, "magic_enum::detail::endex requires enum type.");
576
577
auto value = U{0};
578
for (std::size_t i = 0; i < count_v<E, true>; ++i) {
579
value |= static_cast<U>(values_v<E, true>[i]);
580
}
581
582
return value;
583
}
584
585
template <bool, typename T, typename R>
586
struct enable_if_enum {};
587
588
template <typename T, typename R>
589
struct enable_if_enum<true, T, R> {
590
using type = R;
591
using D = std::decay_t<T>;
592
static_assert(supported<D>::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
593
};
594
595
template <typename T, typename R = void>
596
using enable_if_enum_t = std::enable_if_t<std::is_enum_v<std::decay_t<T>>, R>;
597
598
template <typename T, typename Enable = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
599
using enum_concept = T;
600
601
template <typename T, bool = std::is_enum_v<T>>
602
struct is_scoped_enum : std::false_type {};
603
604
template <typename T>
605
struct is_scoped_enum<T, true> : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
606
607
template <typename T, bool = std::is_enum_v<T>>
608
struct is_unscoped_enum : std::false_type {};
609
610
template <typename T>
611
struct is_unscoped_enum<T, true> : std::bool_constant<std::is_convertible_v<T, std::underlying_type_t<T>>> {};
612
613
template <typename T, bool = std::is_enum_v<std::decay_t<T>>>
614
struct underlying_type {};
615
616
template <typename T>
617
struct underlying_type<T, true> : std::underlying_type<std::decay_t<T>> {};
618
619
} // namespace magic_enum::detail
620
621
// Checks is magic_enum supported compiler.
622
inline constexpr bool is_magic_enum_supported = detail::supported<void>::value;
623
624
template <typename T>
625
using Enum = detail::enum_concept<T>;
626
627
// Checks whether T is an Unscoped enumeration type.
628
// Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. Otherwise, value is equal to false.
629
template <typename T>
630
struct is_unscoped_enum : detail::is_unscoped_enum<T> {};
631
632
template <typename T>
633
inline constexpr bool is_unscoped_enum_v = is_unscoped_enum<T>::value;
634
635
// Checks whether T is an Scoped enumeration type.
636
// Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. Otherwise, value is equal to false.
637
template <typename T>
638
struct is_scoped_enum : detail::is_scoped_enum<T> {};
639
640
template <typename T>
641
inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
642
643
// If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.
644
// Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed.
645
template <typename T>
646
struct underlying_type : detail::underlying_type<T> {};
647
648
template <typename T>
649
using underlying_type_t = typename underlying_type<T>::type;
650
651
// Returns type name of enum.
652
template <typename E>
653
[[nodiscard]] constexpr auto enum_type_name() noexcept -> detail::enable_if_enum_t<E, string_view> {
654
using D = std::decay_t<E>;
655
constexpr string_view name = detail::type_name_v<D>;
656
static_assert(name.size() > 0, "Enum type does not have a name.");
657
658
return name;
659
}
660
661
// Returns number of enum values.
662
template <typename E>
663
[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t<E, std::size_t> {
664
using D = std::decay_t<E>;
665
666
return detail::count_v<D>;
667
}
668
669
// Returns enum value at specified index.
670
// No bounds checking is performed: the behavior is undefined if index >= number of enum values.
671
template <typename E>
672
[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t<E, std::decay_t<E>> {
673
using D = std::decay_t<E>;
674
static_assert(detail::count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
675
676
if constexpr (detail::is_sparse_v<D>) {
677
return assert((index < detail::count_v<D>)), detail::values_v<D>[index];
678
} else {
679
return assert((index < detail::count_v<D>)), detail::value<D, detail::min_v<D>>(index);
680
}
681
}
682
683
// Returns std::array with enum values, sorted by enum value.
684
template <typename E>
685
[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t<E, detail::values_t<E>> {
686
using D = std::decay_t<E>;
687
static_assert(detail::count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
688
689
return detail::values_v<D>;
690
}
691
692
// Returns name from static storage enum variable.
693
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
694
template <auto V>
695
[[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_enum_t<decltype(V), string_view> {
696
using D = std::decay_t<decltype(V)>;
697
constexpr string_view name = detail::enum_name_v<D, V>;
698
static_assert(name.size() > 0, "Enum value does not have a name.");
699
700
return name;
701
}
702
703
// Returns name from enum value.
704
// If enum value does not have name or value out of range, returns empty string.
705
template <typename E>
706
[[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_enum_t<E, string_view> {
707
using D = std::decay_t<E>;
708
709
if (const auto i = detail::endex<D>(value); i != detail::invalid_index_v<D>) {
710
return detail::names_v<D>[i];
711
}
712
713
return {}; // Invalid value or out of range.
714
}
715
716
// Returns std::array with names, sorted by enum value.
717
template <typename E>
718
[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t<E, detail::names_t<E>> {
719
using D = std::decay_t<E>;
720
static_assert(detail::count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
721
722
return detail::names_v<D>;
723
}
724
725
// Returns std::array with pairs (value, name), sorted by enum value.
726
template <typename E>
727
[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t<E, detail::entries_t<E>> {
728
using D = std::decay_t<E>;
729
static_assert(detail::count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
730
731
return detail::entries_v<D>;
732
}
733
734
// Obtains enum value from integer value.
735
// Returns optional with enum value.
736
template <typename E>
737
[[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
738
using D = std::decay_t<E>;
739
740
if (detail::undex<D>(value) != detail::invalid_index_v<D>) {
741
return static_cast<D>(value);
742
}
743
744
return {}; // Invalid value or out of range.
745
}
746
747
// Obtains enum value from name.
748
// Returns optional with enum value.
749
template <typename E, typename BinaryPredicate>
750
[[nodiscard]] constexpr auto enum_cast(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
751
static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::enum_cast requires bool(char, char) invocable predicate.");
752
using D = std::decay_t<E>;
753
754
for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
755
if (detail::cmp_equal(value, detail::names_v<D>[i], p)) {
756
return enum_value<D>(i);
757
}
758
}
759
760
return {}; // Invalid value or out of range.
761
}
762
763
// Obtains enum value from name.
764
// Returns optional with enum value.
765
template <typename E>
766
[[nodiscard]] constexpr auto enum_cast(string_view value) noexcept -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
767
using D = std::decay_t<E>;
768
769
return enum_cast<D>(value, detail::char_equal_to{});
770
}
771
772
// Returns integer value from enum value.
773
template <typename E>
774
[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_t<E, underlying_type_t<E>> {
775
return static_cast<underlying_type_t<E>>(value);
776
}
777
778
// Obtains index in enum values from enum value.
779
// Returns optional with index.
780
template <typename E>
781
[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t<E, optional<std::size_t>> {
782
using D = std::decay_t<E>;
783
784
if (const auto i = detail::endex<D>(value); i != detail::invalid_index_v<D>) {
785
return i;
786
}
787
788
return {}; // Invalid value or out of range.
789
}
790
791
// Checks whether enum contains enumerator with such enum value.
792
template <typename E>
793
[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t<E, bool> {
794
using D = std::decay_t<E>;
795
796
return detail::endex<D>(value) != detail::invalid_index_v<D>;
797
}
798
799
// Checks whether enum contains enumerator with such integer value.
800
template <typename E>
801
[[nodiscard]] constexpr auto enum_contains(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, bool> {
802
using D = std::decay_t<E>;
803
804
return detail::undex<D>(value) != detail::invalid_index_v<D>;
805
}
806
807
// Checks whether enum contains enumerator with such name.
808
template <typename E, typename BinaryPredicate>
809
[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, bool> {
810
static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::enum_contains requires bool(char, char) invocable predicate.");
811
using D = std::decay_t<E>;
812
813
return enum_cast<D>(value, std::move_if_noexcept(p)).has_value();
814
}
815
816
// Checks whether enum contains enumerator with such name.
817
template <typename E>
818
[[nodiscard]] constexpr auto enum_contains(string_view value) noexcept -> detail::enable_if_enum_t<E, bool> {
819
using D = std::decay_t<E>;
820
821
return enum_cast<D>(value).has_value();
822
}
823
824
namespace ostream_operators {
825
826
template <typename Char, typename Traits, typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
827
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, E value) {
828
using D = std::decay_t<E>;
829
using U = underlying_type_t<D>;
830
#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
831
if (const auto name = magic_enum::enum_name<D>(value); !name.empty()) {
832
for (const auto c : name) {
833
os.put(c);
834
}
835
return os;
836
}
837
#endif
838
return (os << static_cast<U>(value));
839
}
840
841
template <typename Char, typename Traits, typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
842
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, optional<E> value) {
843
return value.has_value() ? (os << value.value()) : os;
844
}
845
846
} // namespace magic_enum::ostream_operators
847
848
namespace bitwise_operators {
849
850
template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
851
constexpr E operator~(E rhs) noexcept {
852
return static_cast<E>(~static_cast<underlying_type_t<E>>(rhs));
853
}
854
855
template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
856
constexpr E operator|(E lhs, E rhs) noexcept {
857
return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) | static_cast<underlying_type_t<E>>(rhs));
858
}
859
860
template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
861
constexpr E operator&(E lhs, E rhs) noexcept {
862
return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) & static_cast<underlying_type_t<E>>(rhs));
863
}
864
865
template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
866
constexpr E operator^(E lhs, E rhs) noexcept {
867
return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) ^ static_cast<underlying_type_t<E>>(rhs));
868
}
869
870
template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
871
constexpr E& operator|=(E& lhs, E rhs) noexcept {
872
return lhs = (lhs | rhs);
873
}
874
875
template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
876
constexpr E& operator&=(E& lhs, E rhs) noexcept {
877
return lhs = (lhs & rhs);
878
}
879
880
template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
881
constexpr E& operator^=(E& lhs, E rhs) noexcept {
882
return lhs = (lhs ^ rhs);
883
}
884
885
} // namespace magic_enum::bitwise_operators
886
887
namespace flags {
888
889
// Returns type name of enum.
890
using magic_enum::enum_type_name;
891
892
// Returns number of enum-flags values.
893
template <typename E>
894
[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t<E, std::size_t> {
895
using D = std::decay_t<E>;
896
897
return detail::count_v<D, true>;
898
}
899
900
// Returns enum-flags value at specified index.
901
// No bounds checking is performed: the behavior is undefined if index >= number of enum-flags values.
902
template <typename E>
903
[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t<E, std::decay_t<E>> {
904
using D = std::decay_t<E>;
905
static_assert(detail::count_v<D, true> > 0, "magic_enum::flags requires enum-flags implementation.");
906
907
if constexpr (detail::is_sparse_v<D, true>) {
908
return assert((index < detail::count_v<D, true>)), detail::values_v<D, true>[index];
909
} else {
910
constexpr auto min = detail::log2(detail::min_v<D, true>);
911
912
return assert((index < detail::count_v<D, true>)), detail::value<D, min, true>(index);
913
}
914
}
915
916
// Returns std::array with enum-flags values, sorted by enum-flags value.
917
template <typename E>
918
[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t<E, detail::values_t<E, true>> {
919
using D = std::decay_t<E>;
920
static_assert(detail::count_v<D, true> > 0, "magic_enum::flags requires enum-flags implementation.");
921
922
return detail::values_v<D, true>;
923
}
924
925
// Returns name from enum-flags value.
926
// If enum-flags value does not have name or value out of range, returns empty string.
927
template <typename E>
928
[[nodiscard]] auto enum_name(E value) -> detail::enable_if_enum_t<E, string> {
929
using D = std::decay_t<E>;
930
using U = underlying_type_t<D>;
931
932
string name;
933
auto check_value = U{0};
934
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
935
if (const auto v = static_cast<U>(enum_value<D>(i)); (static_cast<U>(value) & v) != 0) {
936
check_value |= v;
937
const auto n = detail::names_v<D, true>[i];
938
if (!name.empty()) {
939
name.append(1, '|');
940
}
941
name.append(n.data(), n.size());
942
}
943
}
944
945
if (check_value != 0 && check_value == static_cast<U>(value)) {
946
return name;
947
}
948
949
return {}; // Invalid value or out of range.
950
}
951
952
// Returns std::array with string names, sorted by enum-flags value.
953
template <typename E>
954
[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t<E, detail::names_t<E, true>> {
955
using D = std::decay_t<E>;
956
static_assert(detail::count_v<D, true> > 0, "magic_enum::flags requires enum-flags implementation.");
957
958
return detail::names_v<D, true>;
959
}
960
961
// Returns std::array with pairs (value, name), sorted by enum-flags value.
962
template <typename E>
963
[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t<E, detail::entries_t<E, true>> {
964
using D = std::decay_t<E>;
965
static_assert(detail::count_v<D, true> > 0, "magic_enum::flags requires enum-flags implementation.");
966
967
return detail::entries_v<D, true>;
968
}
969
970
// Obtains enum-flags value from integer value.
971
// Returns optional with enum-flags value.
972
template <typename E>
973
[[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
974
using D = std::decay_t<E>;
975
using U = underlying_type_t<D>;
976
977
if constexpr (detail::is_sparse_v<D, true>) {
978
auto check_value = U{0};
979
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
980
if (const auto v = static_cast<U>(enum_value<D>(i)); (value & v) != 0) {
981
check_value |= v;
982
}
983
}
984
985
if (check_value != 0 && check_value == value) {
986
return static_cast<D>(value);
987
}
988
} else {
989
constexpr auto min = detail::min_v<D, true>;
990
constexpr auto max = detail::value_ors<D>();
991
992
if (value >= min && value <= max) {
993
return static_cast<D>(value);
994
}
995
}
996
997
return {}; // Invalid value or out of range.
998
}
999
1000
// Obtains enum-flags value from name.
1001
// Returns optional with enum-flags value.
1002
template <typename E, typename BinaryPredicate>
1003
[[nodiscard]] constexpr auto enum_cast(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
1004
static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::flags::enum_cast requires bool(char, char) invocable predicate.");
1005
using D = std::decay_t<E>;
1006
using U = underlying_type_t<D>;
1007
1008
auto result = U{0};
1009
while (!value.empty()) {
1010
const auto d = detail::find(value, '|');
1011
const auto s = (d == string_view::npos) ? value : value.substr(0, d);
1012
auto f = U{0};
1013
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
1014
if (detail::cmp_equal(s, detail::names_v<D, true>[i], p)) {
1015
f = static_cast<U>(enum_value<D>(i));
1016
result |= f;
1017
break;
1018
}
1019
}
1020
if (f == U{0}) {
1021
return {}; // Invalid value or out of range.
1022
}
1023
value.remove_prefix((d == string_view::npos) ? value.size() : d + 1);
1024
}
1025
1026
if (result == U{0}) {
1027
return {}; // Invalid value or out of range.
1028
} else {
1029
return static_cast<D>(result);
1030
}
1031
}
1032
1033
// Obtains enum-flags value from name.
1034
// Returns optional with enum-flags value.
1035
template <typename E>
1036
[[nodiscard]] constexpr auto enum_cast(string_view value) noexcept -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
1037
using D = std::decay_t<E>;
1038
1039
return enum_cast<D>(value, detail::char_equal_to{});
1040
}
1041
1042
// Returns integer value from enum value.
1043
using magic_enum::enum_integer;
1044
1045
// Obtains index in enum-flags values from enum-flags value.
1046
// Returns optional with index.
1047
template <typename E>
1048
[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t<E, optional<std::size_t>> {
1049
using D = std::decay_t<E>;
1050
using U = underlying_type_t<D>;
1051
1052
if (detail::is_pow2(static_cast<U>(value))) {
1053
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
1054
if (enum_value<D>(i) == value) {
1055
return i;
1056
}
1057
}
1058
}
1059
1060
return {}; // Invalid value or out of range.
1061
}
1062
1063
// Checks whether enum-flags contains enumerator with such enum-flags value.
1064
template <typename E>
1065
[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t<E, bool> {
1066
using D = std::decay_t<E>;
1067
using U = underlying_type_t<D>;
1068
1069
return enum_cast<D>(static_cast<U>(value)).has_value();
1070
}
1071
1072
// Checks whether enum-flags contains enumerator with such integer value.
1073
template <typename E>
1074
[[nodiscard]] constexpr auto enum_contains(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, bool> {
1075
using D = std::decay_t<E>;
1076
1077
return enum_cast<D>(value).has_value();
1078
}
1079
1080
// Checks whether enum-flags contains enumerator with such name.
1081
template <typename E, typename BinaryPredicate>
1082
[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, bool> {
1083
static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::flags::enum_contains requires bool(char, char) invocable predicate.");
1084
using D = std::decay_t<E>;
1085
1086
return enum_cast<D>(value, std::move_if_noexcept(p)).has_value();
1087
}
1088
1089
// Checks whether enum-flags contains enumerator with such name.
1090
template <typename E>
1091
[[nodiscard]] constexpr auto enum_contains(string_view value) noexcept -> detail::enable_if_enum_t<E, bool> {
1092
using D = std::decay_t<E>;
1093
1094
return enum_cast<D>(value).has_value();
1095
}
1096
1097
} // namespace magic_enum::flags
1098
1099
namespace flags::ostream_operators {
1100
1101
template <typename Char, typename Traits, typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
1102
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, E value) {
1103
using D = std::decay_t<E>;
1104
using U = underlying_type_t<D>;
1105
#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
1106
if (const auto name = magic_enum::flags::enum_name<D>(value); !name.empty()) {
1107
for (const auto c : name) {
1108
os.put(c);
1109
}
1110
return os;
1111
}
1112
#endif
1113
return (os << static_cast<U>(value));
1114
}
1115
1116
template <typename Char, typename Traits, typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
1117
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, optional<E> value) {
1118
return value.has_value() ? (os << value.value()) : os;
1119
}
1120
1121
} // namespace magic_enum::flags::ostream_operators
1122
1123
namespace flags::bitwise_operators {
1124
1125
using namespace magic_enum::bitwise_operators;
1126
1127
} // namespace magic_enum::flags::bitwise_operators
1128
1129
} // namespace magic_enum
1130
1131
#if defined(__clang__)
1132
# pragma clang diagnostic pop
1133
#elif defined(__GNUC__)
1134
# pragma GCC diagnostic pop
1135
#elif defined(_MSC_VER)
1136
# pragma warning(pop)
1137
#endif
1138
1139
#endif // NEARGYE_MAGIC_ENUM_HPP
1140
1141