-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_enum.hpp
More file actions
1446 lines (1271 loc) · 68.8 KB
/
nested_enum.hpp
File metadata and controls
1446 lines (1271 loc) · 68.8 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef NESTED_ENUM_HPP
#define NESTED_ENUM_HPP
#define NESTED_ENUM_VERSION_MAJOR 0
#define NESTED_ENUM_VERSION_MINOR 3
#define NESTED_ENUM_VERSION_PATCH 3
#include <cstdint>
#include <type_traits>
#include <utility>
#include <array>
#include <string_view>
#include <optional>
#include <tuple>
#ifndef NESTED_ENUM_DEFAULT_ENUM_TYPE
#define NESTED_ENUM_DEFAULT_ENUM_TYPE ::std::int32_t
#endif
#ifdef NESTED_ENUM_SUPPRESS_MULTIPLE_RESULTS_ASSERT
#define NESTED_ENUM_SUPPRESS_MULTIPLE_RESULTS_ASSERT 0
#endif
namespace nested_enum
{
template <std::size_t N>
struct fixed_string
{
constexpr fixed_string() = default;
constexpr fixed_string(const char(&array)[N + 1]) noexcept
{
for (std::size_t i = 0; i < N; ++i)
data[i] = array[i];
data[N] = '\0';
}
template<std::size_t M>
[[nodiscard]] constexpr auto append(fixed_string<M> other) const noexcept
{
fixed_string<N + M> result;
result.data[N + M] = '\0';
for (std::size_t i = 0; i < N; ++i)
result.data[i] = data[i];
for (std::size_t i = 0; i < M; ++i)
result.data[N + i] = other.data[i];
return result;
}
// concatenates with null terminator
template<std::size_t M>
[[nodiscard]] constexpr auto append_full(fixed_string<M> other) const noexcept
{
fixed_string<N + 1 + M> result;
for (std::size_t i = 0; i < N; ++i)
result.data[i] = data[i];
result.data[N] = '\0';
for (std::size_t i = 0; i < M; ++i)
result.data[N + 1 + i] = other.data[i];
result.data[N + 1 + M] = '\0';
return result;
}
[[nodiscard]] constexpr auto operator<=>(const fixed_string &) const = default;
[[nodiscard]] constexpr operator std::string_view() const noexcept { return { data.data(), N }; }
[[nodiscard]] static constexpr std::size_t size() noexcept { return N; }
std::array<char, N + 1> data;
};
template <std::size_t N>
fixed_string(const char(&)[N])->fixed_string<N - 1>;
namespace detail
{
template<typename T>
concept Enum = std::is_enum_v<T>;
template <class T>
inline constexpr auto is_complete_type_v = requires{ sizeof(T); };
template <typename ... Ts>
struct type_list
{
static constexpr auto size = sizeof...(Ts);
template <typename ... Us>
constexpr auto operator+(type_list<Us...>) const noexcept
{
return type_list<Ts..., Us...>{};
}
template <typename ... Us>
constexpr bool operator==(type_list<Us...>) const noexcept
{
return std::is_same_v<type_list<Ts...>, type_list<Us...>>;
}
};
template<typename T, typename ... Ts>
T get_first_type(type_list<T, Ts...>) { return T{}; }
template<typename T = int>
struct opt { bool isInitialised = false; T value = 0; };
inline constexpr fixed_string scopeResolution = "::";
template<typename T>
constexpr auto find_index(const auto &container, const T &value)
{
auto first = container.begin();
auto last = container.end();
if constexpr (std::is_same_v<typename std::remove_cvref_t<decltype(container)>::value_type, std::optional<std::remove_cvref_t<T>>>)
{
for (; first != last; ++first)
if (first->has_value() && first->value() == value)
return std::optional{ (std::size_t)(first - container.begin()) };
}
else
{
static_assert(std::is_same_v<typename std::remove_cvref_t<decltype(container)>::value_type, std::remove_cvref_t<T>>);
for (; first != last; ++first)
if (*first == value)
return std::optional{ (std::size_t)(first - container.begin()) };
}
return std::optional<std::size_t>{};
}
template<typename E, typename T, opt<T> ... values>
consteval auto get_array_of_values()
{
std::array<E, sizeof...(values)> enumValues{};
std::size_t index = 0;
T previous = 0;
for (auto current : std::initializer_list<opt<T>>{ values... })
{
if (current.isInitialised)
previous = current.value;
enumValues[index++] = (E)(previous++);
}
return enumValues;
}
template<fixed_string type, fixed_string value, fixed_string ... values>
consteval auto get_string_values()
{
static_assert(type.size() > 0);
constexpr auto current = type.append(scopeResolution.append(value));
if constexpr (sizeof...(values) == 0)
return current.append_full(fixed_string{ "" });
else
return current.append_full(get_string_values<type, values...>());
}
// bless their soul https://stackoverflow.com/a/54932626
namespace tuple_magic
{
template <typename T> struct is_tuple : std::false_type { };
template <typename... Ts> struct is_tuple<std::tuple<Ts...>> : std::true_type { };
template<typename T>
constexpr decltype(auto) as_tuple(T t)
{
return std::make_tuple(t);
}
template<typename ...Ts>
constexpr decltype(auto) as_tuple(std::tuple<Ts...> t)
{
return t;
}
constexpr decltype(auto) flatten(auto t)
{
return t;
}
template<typename T>
constexpr decltype(auto) flatten(std::tuple<T> t)
{
return flatten(std::get<0>(t));
}
template<typename ...Ts, std::enable_if_t<!(is_tuple<Ts>::value || ...), bool> = false>
constexpr decltype(auto) flatten(std::tuple<Ts...> t)
{
return t;
}
template<typename ...Ts, std::enable_if_t<(is_tuple<Ts>::value || ...), bool> = false>
constexpr decltype(auto) flatten(std::tuple<Ts...> t)
{
return std::apply([](auto...ts) { return flatten(std::tuple_cat(as_tuple(flatten(ts))...)); }, t);
}
}
template<typename A>
inline constexpr auto is_std_array_v = requires
{
requires std::is_same_v<A, std::array<typename A::value_type, std::tuple_size_v<A>>>;
};
template<class TupleOrArray>
constexpr auto tuple_of_arrays_to_array(TupleOrArray &&tupleOrArray)
{
if constexpr (is_std_array_v<TupleOrArray>)
return tupleOrArray;
else
{
auto function = []<typename T, std::size_t M, typename ... Args>(std::array<T, M> first, Args ... args)
{
if constexpr (sizeof...(Args) == 0)
return first;
else
{
constexpr std::size_t N = M + (args.size() + ...);
std::array<T, N> newArray;
std::size_t index = 0;
auto iterate = [&index, &newArray](auto current)
{
for (std::size_t i = 0; i < current.size(); i++)
newArray[index + i] = current[i];
index += current.size();
};
iterate(first);
(iterate(args), ...);
return newArray;
}
};
return [&]<std::size_t ... Indices>(std::index_sequence<Indices...>)
{
return function(std::get<Indices>(tupleOrArray)...);
}(std::make_index_sequence<std::tuple_size_v<TupleOrArray>>{});
}
}
constexpr std::string_view get_substring(std::string_view allStrings, std::size_t index, bool clean) noexcept
{
std::size_t end = 0;
std::size_t count = 0;
while (count < index)
if (allStrings[end++] == '\0')
++count;
std::string_view view = std::string_view(&allStrings[end]);
if (clean)
{
std::string_view scope = "::";
view.remove_prefix(view.rfind(scope) + scope.length());
}
return view;
}
template<fixed_string prefix = "">
constexpr auto get_prefix() noexcept { return prefix; }
template<typename T, typename E>
concept this_underlying_type = std::is_same_v<T, typename E::underlying_type>;
}
#define TEST_INCLUSIVENESS(selection, type) (selection == All || \
(!detail::is_complete_type_v<type> && selection == Outer) || \
(detail::is_complete_type_v<type> && ((type::isLeaf && selection == Outer) || (!type::isLeaf && selection == Inner))))
// Inner - enum values that are themselves enums
// Outer - enum values that are NOT enums
// All - both inner and outer enum values
enum InnerOuterAll { Inner, Outer, All };
template<class E, class P = E>
struct nested_enum
{
using type = E;
using parent = P;
template<class, class>
friend struct nested_enum;
using nested_enum_tag = void;
// returns the reflected type name of the enum
static constexpr auto name(bool clean = false) noexcept -> std::string_view
{
std::string_view name = E::internalName;
if (clean)
{
std::string_view scope = "::";
name.remove_prefix(name.rfind(scope) + scope.length());
}
return name;
}
// returns the id of the type name (if it has one)
static constexpr auto id() noexcept -> std::optional<std::string_view>
{
if constexpr (std::is_same_v<E, P>)
return {};
else
return P::enum_id(E::value());
}
// returns the integer of the type name (if it has one)
static constexpr auto integer() noexcept
{
return static_cast<typename E::underlying_type>(E::value());
}
// returns the global id of the topmost parent
static constexpr auto global_prefix() -> std::string_view
{
if constexpr (std::is_same_v<E, P>)
return std::string_view{ E::internalGlobalPrefix };
else
return parent::global_prefix();
}
// returns an instance of this type with the given value if it exist
template<typename T> requires std::is_integral_v<T> || std::is_floating_point_v<T>
static constexpr auto make_enum(T t) -> std::optional<E>
{
return enum_value(static_cast<typename E::underlying_type>(t));
}
// returns the string of the currently held value
constexpr auto enum_name(bool clean = false) const noexcept -> std::string_view
{
return enum_name(static_cast<const E &>(*this), clean).value();
}
// returns the id of the currently held value
constexpr auto enum_id() const noexcept -> std::optional<std::string_view>
{
return enum_id(static_cast<const E &>(*this));
}
// returns the string and id of the currently held value
constexpr auto enum_name_and_id(bool clean = false) const noexcept -> std::pair<std::string_view, std::optional<std::string_view>>
{
return enum_name_and_id(static_cast<const E &>(*this), clean).value();
}
// returns the integer representation of the currently held value
constexpr auto enum_integer() const noexcept
{
return (typename E::underlying_type)static_cast<const E &>(*this).internalValue;
}
protected:
template<std::size_t N>
static constexpr auto create_name(fixed_string<N> name)
{
if constexpr (std::is_same_v<E, P>)
{
if constexpr (E::internalGlobalPrefix.size() == 0)
return name;
else
{
auto fullName = E::internalGlobalPrefix.append(detail::scopeResolution.append(name));
return fullName;
}
}
else
{
auto fullName = P::internalName.append(detail::scopeResolution.append(name));
return fullName;
}
}
// returns a tuple of the enum subtypes that satisfy the selection
template<InnerOuterAll Selection, typename ... Ts>
static constexpr auto get_needed_values(detail::type_list<Ts...>) noexcept
{
std::size_t size = 0;
std::size_t index = 0;
std::array<bool, E::enumValues.size()> valuesNeeded{};
auto getValues = [&]<typename T>()
{
if constexpr (TEST_INCLUSIVENESS(Selection, T))
{
size++;
valuesNeeded[index] = true;
}
else
valuesNeeded[index] = false;
index++;
};
(getValues.template operator()<Ts>(), ...);
return std::pair{ valuesNeeded, size };
}
public:
// returns the values inside this enum that satisfy the Selection
template<InnerOuterAll Selection = All>
static constexpr auto enum_values() noexcept
{
if constexpr (E::enumValues.size() == 0 || Selection == All)
{
constexpr auto enumValues = []()
{
auto values = E::template internal_get_array_of_type<E::enumValues.size()>();
for (std::size_t i = 0; i < values.size(); ++i)
values[i] = E{ E::enumValues[i] };
return values;
}();
return enumValues;
}
else
{
constexpr auto result = []()
{
constexpr auto valuesNeeded = get_needed_values<Selection>(E::subtypes);
auto values = E::template internal_get_array_of_type<valuesNeeded.second>();
for (std::size_t i = 0, j = 0; i < valuesNeeded.first.size(); i++)
if (valuesNeeded.first[i])
values[j++] = E{ E::enumValues[i] };
return values;
}();
return result;
}
}
// returns the values count inside this enum that satisfy the selection
static constexpr auto enum_count(InnerOuterAll selection = All) noexcept -> std::size_t
{
if constexpr (E::enumValues.size() == 0)
return 0;
else
{
if (selection == All)
return E::enumValues.size();
return [&]<typename ... Ts>(detail::type_list<Ts...>)
{
std::size_t total = 0;
auto getTotal = [&]<typename T>()
{
if (TEST_INCLUSIVENESS(selection, T))
total++;
};
(getTotal.template operator()<Ts>(), ...);
return total;
}(E::subtypes);
}
}
// returns the underlying integers of enum values that satisfy the selection
template<InnerOuterAll Selection = All>
static constexpr auto enum_integers() noexcept
{
constexpr auto integerArray = []()
{
auto enumValues = enum_values<Selection>();
std::array<typename E::underlying_type, enumValues.size()> result;
for (std::size_t i = 0; i < result.size(); i++)
result[i] = (typename E::underlying_type)enumValues[i].internalValue;
return result;
}();
return integerArray;
}
// returns the ids of enum values that satisfy the selection
template<InnerOuterAll Selection = All>
static constexpr auto enum_ids() noexcept
{
if constexpr (E::enumIds.size() == 0 || Selection == All)
return E::enumIds;
else
{
constexpr auto result = []()
{
constexpr auto valuesNeeded = get_needed_values<Selection>(E::subtypes);
std::array<std::optional<std::string_view>, valuesNeeded.second> values;
for (std::size_t i = 0, j = 0; i < valuesNeeded.first.size(); i++)
if (valuesNeeded.first[i])
values[j++] = E::enumIds[i];
return values;
}();
return result;
}
}
// returns the reflected strings of enum values that satisfy the selection
// "clean" refers to whether only the enum value name or its entire path is returned
template<InnerOuterAll Selection = All>
static constexpr auto enum_names(bool clean = false) noexcept
{
if constexpr (enum_count(Selection) == 0)
{
// if this isn't assigned to a constexpr variable msvc won't recognise the function as constexpr
constexpr auto enumNames = std::array<std::string_view, 0>{};
return enumNames;
}
else
{
constexpr auto generateStrings = []<bool cleanString>()
{
constexpr auto valuesNeeded = get_needed_values<Selection>(E::subtypes);
std::array<std::string_view, valuesNeeded.second> values;
for (std::size_t i = 0, j = 0; i < valuesNeeded.first.size(); i++)
if (valuesNeeded.first[i])
values[j++] = detail::get_substring(std::string_view(E::enumNames), i, cleanString);
return values;
};
constexpr auto enumNames = generateStrings.template operator()<false>();
constexpr auto enumNamesClean = generateStrings.template operator()<true>();
if (!clean)
return enumNames;
else
return enumNamesClean;
}
}
// returns the reflected strings and ids of enum values that satisfy the selection
// "clean" refers to whether only the enum value name or its entire path is returned
template<InnerOuterAll Selection = All>
static constexpr auto enum_names_and_ids(bool clean = false) noexcept
{
if constexpr (enum_count(Selection) == 0)
return std::array<std::pair<std::string_view, std::optional<std::string_view>>, 0>{};
else
{
constexpr auto stringsAndIds = []()
{
constexpr auto valuesNeeded = get_needed_values<Selection>(E::subtypes);
std::array<std::pair<std::string_view, std::optional<std::string_view>>, valuesNeeded.second> values{};
for (std::size_t i = 0, j = 0; i < valuesNeeded.first.size(); i++)
if (valuesNeeded.first[i])
values[j++] = { detail::get_substring(E::enumNames, i, false), E::enumIds[i] };
return values;
}();
constexpr auto stringsAndIdsClean = [&]()
{
std::array<std::pair<std::string_view, std::optional<std::string_view>>, stringsAndIds.size()> values;
std::string_view scope = "::";
for (std::size_t i = 0; i < stringsAndIds.size(); ++i)
{
auto string = stringsAndIds[i].first;
string.remove_prefix(string.rfind(scope) + scope.length());
values[i] = std::pair{ string, stringsAndIds[i].second };
}
return values;
}();
if (!clean)
return stringsAndIds;
else
return stringsAndIdsClean;
}
}
// returns a tuple of the enum subtypes that satisfy the selection
template<InnerOuterAll Selection = All>
static constexpr auto enum_subtypes() noexcept
{
if constexpr (E::enumValues.size() == 0)
return std::tuple<>{};
else
{
constexpr auto list = []<typename ... Ts>(detail::type_list<Ts...>)
{
auto recurse = []<typename T>()
{
if constexpr (TEST_INCLUSIVENESS(Selection, T))
return detail::type_list<T>{};
else
return detail::type_list<>{};
};
auto list = (recurse.template operator()<Ts>() + ...);
return list;
}(E::subtypes);
return []<typename ... Ts>(detail::type_list<Ts...>)
{
return std::tuple<std::type_identity<Ts>...>{};
}(list);
}
}
private:
template<typename ... Ts>
static constexpr std::size_t enum_count_recursive_internal(InnerOuterAll selection, detail::type_list<Ts...>) noexcept
{
std::size_t count = (selection == Inner) || (selection == All);
if constexpr (sizeof...(Ts) == 0)
return count;
else
{
auto subtypesSizes = [&]<typename T>() -> std::size_t
{
if constexpr (detail::is_complete_type_v<T> && !T::isLeaf)
return T::enum_count_recursive_internal(selection, T::subtypes);
else
return (selection == Outer) || (selection == All);
};
return count + (subtypesSizes.template operator()<Ts>() + ...);
}
}
public:
// returns are recursive count of all enum values in the subtree that satisfy the selection
static constexpr std::size_t enum_count_recursive(InnerOuterAll selection = All) noexcept
{
std::size_t count = enum_count_recursive_internal(selection, E::subtypes);
if ((selection == Inner) || (selection == All))
return --count;
return count;
}
private:
template<auto Predicate, InnerOuterAll Selection>
static constexpr auto return_recursive_internal() noexcept
{
if constexpr (E::enumValues.size() == 0)
return std::tuple<>{};
else
{
constexpr auto values = Predicate.template operator()<E>();
// getting an array of values we need
constexpr auto subtypesToQuery = [&]<typename ... Ts>(detail::type_list<Ts...>)
{
// new tuple of only the inner enum nodes
auto checkQueries = []<typename T>()
{
// does the enum exist
if constexpr (!detail::is_complete_type_v<T>)
return detail::type_list<>{};
else
{
// does the enum have any values
if constexpr (T::subtypes.size == 0)
return detail::type_list<>{};
// different branches based on what we want
else if constexpr ((Selection == All) || (Selection == Outer))
return detail::type_list<T>{};
else
{
auto checkInner = []<typename ... Us>(detail::type_list<Us...>)
{
auto checkIndividual = []<typename U>() -> bool
{
if constexpr (detail::is_complete_type_v<U>)
return !U::isLeaf;
else
return false;
};
return false || (checkIndividual.template operator()<Us>() || ...);
};
if constexpr (checkInner(T::subtypes))
return detail::type_list<T>{};
else
return detail::type_list<>{};
}
}
};
return (checkQueries.template operator()<Ts>() + ...);
}(E::subtypes);
// returning only the values if we don't have any subtypes to query
if constexpr (subtypesToQuery.size == 0)
return std::make_tuple(values);
else
{
constexpr auto subtypesTuples = []<typename ... Ts>(detail::type_list<Ts...>)
{
auto expand = []<typename T>() { return T::template return_recursive_internal<Predicate, Selection>(); };
return std::make_tuple(expand.template operator()<Ts>()...);
}(subtypesToQuery);
if constexpr (values.size() == 0)
return subtypesTuples;
else
return std::tuple_cat(std::make_tuple(values), subtypesTuples);
}
}
}
public:
// returns std::tuple<std::array<Enum, ?>...>
// of all enum values in the subtree that satisfy the selection
template<InnerOuterAll Selection = All>
static constexpr auto enum_values_recursive() noexcept
{
constexpr auto predicate = []<typename T>()
{
return T::template enum_values<Selection>();
};
constexpr auto tuple = detail::tuple_magic::flatten(return_recursive_internal<predicate, Selection>());
return tuple;
}
// returns std::tuple<std::array<std::string_view, ?>...>
// of the reflected names of all enum values in the subtree that satisfy the selection
// if flattenTuple == true, the tuple of arrays will be flattened to a single array with all strings inside
template<InnerOuterAll Selection = All, bool flattenTuple = true, bool clean = false>
static constexpr auto enum_names_recursive() noexcept
{
constexpr auto predicate = []<typename T>()
{
return T::template enum_names<Selection>(clean);
};
if constexpr (flattenTuple)
{
constexpr auto flattenedTuple = detail::tuple_of_arrays_to_array(detail::tuple_magic::flatten(return_recursive_internal<predicate, Selection>()));
return flattenedTuple;
}
else
{
constexpr auto tuple = detail::tuple_magic::flatten(return_recursive_internal<predicate, Selection>());
return tuple;
}
}
// returns std::tuple<std::array<std::optional<std::string_view>, ?>...>
// of the ids of all enum values in the subtree that satisfy the selection
// if flattenTuple == true, the tuple of arrays will be flattened to a single array with all strings inside
template<InnerOuterAll Selection = All, bool flattenTuple = true>
static constexpr auto enum_ids_recursive() noexcept
{
constexpr auto predicate = []<typename T>()
{
return T::template enum_ids<Selection>();
};
if constexpr (flattenTuple)
{
constexpr auto flattenedTuple = detail::tuple_of_arrays_to_array(detail::tuple_magic::flatten(return_recursive_internal<predicate, Selection>()));
return flattenedTuple;
}
else
{
constexpr auto tuple = detail::tuple_magic::flatten(return_recursive_internal<predicate, Selection>());
return tuple;
}
}
// returns std::tuple<std::array<std::pair<std::string_view, std::optional<std::string_view>>, ?>...>
// of the ids and reflected strings of all enum values in the subtree that satisfy the selection
// if flattenTuple == true, the tuple of arrays will be flattened to a single array with all pairs of strings inside
template<InnerOuterAll Selection = All, bool flattenTuple = true, bool clean = false>
static constexpr auto enum_names_and_ids_recursive() noexcept
{
constexpr auto predicate = []<typename T>()
{
return T::template enum_names_and_ids<Selection>(clean);
};
if constexpr (flattenTuple)
{
constexpr auto flattenedTuple = detail::tuple_of_arrays_to_array(detail::tuple_magic::flatten(return_recursive_internal<predicate, Selection>()));
return flattenedTuple;
}
else
{
constexpr auto tuple = detail::tuple_magic::flatten(return_recursive_internal<predicate, Selection>());
return tuple;
}
}
// returns the reflected string of an enum value of this type
static constexpr auto enum_name(E value, bool clean = false) -> std::optional<std::string_view>
{
auto index = detail::find_index(E::enumValues, value.internalValue);
if (!index.has_value())
return {};
return enum_names<All>(clean)[index.value()];
}
// returns the reflected string of an enum value of this type, specified by its id
static constexpr auto enum_name_by_id(std::string_view id, bool clean = false) -> std::optional<std::string_view>
{
auto index = detail::find_index(E::enumIds, id);
if (!index.has_value())
return {};
return enum_names(clean)[index.value()];
}
// returns the id of an enum value of this type
static constexpr auto enum_id(E value) -> std::optional<std::string_view>
{
auto index = detail::find_index(E::enumValues, value.internalValue);
if (!index.has_value())
return {};
return enum_ids<All>()[index.value()];
}
// returns the id of an enum value of this type, specified by its reflected string
static constexpr auto enum_id(std::string_view enumName) -> std::optional<std::string_view>
{
constexpr auto strings = enum_names<All>();
auto index = detail::find_index(strings, enumName);
if (!index.has_value())
return {};
return enum_ids<All>()[index.value()];
}
// returns the reflected string and id of an enum value of this type
static constexpr auto enum_name_and_id(E value, bool clean = false) -> std::optional<std::pair<std::string_view, std::optional<std::string_view>>>
{
auto index = detail::find_index(E::enumValues, value.internalValue);
if (!index.has_value())
return {};
return enum_names_and_ids<All>(clean)[index.value()];
}
// returns the underlying integer of an enum value of this type
static constexpr auto enum_integer(E value)
{
return std::optional{ (typename E::underlying_type)value.internalValue };
}
// returns the underlying integer of an enum value of this type, specified by its reflected string
static constexpr auto enum_integer(std::string_view enumName)
{
constexpr auto enumNames = enum_names<All>(false);
auto index = detail::find_index(enumNames, enumName);
if (!index.has_value())
return std::optional<typename E::underlying_type>{};
return std::optional{ (typename E::underlying_type)E::enumValues[index.value()] };
}
// returns the underlying integer of an enum value of this type, specified by its id
static constexpr auto enum_integer_by_id(std::string_view id)
{
auto index = detail::find_index(E::enumIds, id);
if (!index.has_value())
return std::optional<typename E::underlying_type>{};
return std::optional{ (typename E::underlying_type)E::enumValues[index.value()] };
}
// returns the enum value of this type, specified by an integer
template<typename T> requires detail::this_underlying_type<T, E>
static constexpr auto enum_value(T integer)
{
static_assert(std::is_same_v<T, typename E::underlying_type>);
for (auto value : E::enumValues)
if (value == (typename E::underlying_type)integer)
return std::optional<E>{ value };
return std::optional<E>{};
}
// returns the enum value of this type, specified by its reflected string
static constexpr auto enum_value(std::string_view enumName)
{
constexpr auto enumNames = enum_names<All>(false);
auto index = detail::find_index(enumNames, enumName);
if (!index.has_value())
return std::optional<E>{};
return std::optional<E>{ E::enumValues[index.value()] };
}
// returns the enum value of this type, specified by its id
static constexpr auto enum_value_by_id(std::string_view id)
{
auto index = detail::find_index(E::enumIds, id);
if (!index.has_value())
return std::optional<E>{};
return std::optional<E>{ E::enumValues[index.value()] };
}
private:
// predicate is a lambda that takes the current nested_enum struct as type template parameter and
// returns a bool whether this is the searched for type (i.e. the enum value searched for is contained inside)
template<auto Predicate, typename ... Ts>
static constexpr auto find_type_recursive_internal(detail::type_list<Ts...>)
{
if constexpr (Predicate.template operator()<E>())
return detail::type_list<E>{};
else
{
if constexpr (sizeof...(Ts) == 0)
return detail::type_list<>{};
else
{
constexpr auto getResults = []<typename T>()
{
if constexpr (detail::is_complete_type_v<T>)
return T::template find_type_recursive_internal<Predicate>(T::subtypes);
else
return detail::type_list<>{};
};
// for some reason msvc expands this branch even if sizeof...(Ts) IS 0
// so we need to guard this expansion behind another function
// just msvc things i guess *sigh*
#ifdef _MSC_VER
constexpr auto subtypesResult = [&]<typename ... Us>()
{
if constexpr (sizeof...(Us) == 0)
return detail::type_list<>{};
else
return (getResults.template operator()<Us>() + ...);
}.template operator()<Ts...>();
#else
constexpr auto subtypesResult = (getResults.template operator()<Ts>() + ...);
#endif
if constexpr (subtypesResult.size > 0)
{
#if NESTED_ENUM_SUPPRESS_MULTIPLE_RESULTS_ASSERT == 0
static_assert(subtypesResult.size == 1, "Multiple results found for query, if this is expected define NESTED_ENUM_SUPPRESS_MULTIPLE_RESULTS_ASSERT to 1 before including the header");
#endif
return subtypesResult;
}
else
return detail::type_list<>{};
}
}
}
public:
// returns the reflected string of an enum value that is located somewhere in the subtree
static constexpr auto enum_name_recursive(detail::Enum auto value, bool clean = false) -> std::optional<std::string_view>
{
auto typeId = find_type_recursive_internal<[]<typename T>()
{
return std::is_same_v<decltype(value), typename T::Value>;
}>(E::subtypes);
if constexpr (typeId.size == 0)
return {};
else
{
using subType = decltype(detail::get_first_type(typeId));
return subType::enum_name(value, clean);
}
}
// returns the reflected string of an enum with the specified id that is located somewhere in the subtree
// the algorithm is a top-down DFS (in case you have repeating ids, which is not a good idea)
static constexpr auto enum_name_by_id_recursive(std::string_view id, bool clean = false) -> std::optional<std::string_view>
{
auto value = enum_name_by_id(id, clean);
if (value.has_value())
return value;
if constexpr (E::enumValues.size() == 0)
return {};
else
{
return []<typename ... Ts>(std::string_view id, bool clean, detail::type_list<Ts...>)
{
constexpr auto recurse = []<auto Self, typename U, typename ... Us>(std::string_view id, bool clean)
{
auto value = U::enum_name_by_id_recursive(id, clean);
if (value.has_value())
return value;
if constexpr (sizeof...(Us) > 0)
return Self.template operator()<Self, Us...>(id, clean);
else
return std::optional<std::string_view>{};
};
return recurse.template operator()<recurse, Ts...>(id, clean);
}(id, clean, E::subtypes);
}
}
// returns the id of an enum value that is located somewhere in the subtree
static constexpr auto enum_id_recursive(detail::Enum auto value) -> std::optional<std::string_view>
{
auto typeId = find_type_recursive_internal<[]<typename T>()
{
return std::is_same_v<decltype(value), typename T::Value>;
}>(E::subtypes);
if constexpr (typeId.size == 0)
return {};
else
{
using subType = decltype(detail::get_first_type(typeId));
return subType::enum_id(value);
}
}
// returns the id of an enum with the specified reflected string that is located somewhere in the subtree
static constexpr auto enum_id_recursive(std::string_view enumName) -> std::optional<std::string_view>
{