-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMetaEnumerator.hpp
executable file
·1662 lines (1338 loc) · 47 KB
/
MetaEnumerator.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
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 ELIGT_METAENUMERATOR_H
#define ELIGT_METAENUMERATOR_H
#include <stddef.h>
#include <sstream>
#include <type_traits>
#include <climits>
#include <limits>
#ifdef METAENUMERATOR_NAMESPACE
namespace METAENUMERATOR_NAMESPACE
{
#endif
template <size_t BitLength>
class EnumeratorDataContainer
{
public:
template <typename IntType = short int, typename ByteType = char>
inline constexpr static bool isBigEndian()
{
static_assert(sizeof(IntType) > sizeof(ByteType), "isBigEndian() : IntType must be of size greater than ByteType.");
IntType number = 0x1;
ByteType *numPtr = (ByteType*)&number;
return (numPtr[0] != 1);
}
public:
using MemoryType = unsigned char;
using OperandType = unsigned int;
static constexpr const size_t MEMORY_BITS = sizeof(MemoryType) * CHAR_BIT;
static constexpr const MemoryType MEMORY_MASK = std::numeric_limits<MemoryType>::max();
static constexpr const size_t OPERAND_BITS = sizeof(OperandType) * CHAR_BIT;
static constexpr const size_t OPERAND_COUNT = (BitLength / OPERAND_BITS) + (BitLength % OPERAND_BITS != 0 ? 1 : 0);
static constexpr const size_t ROUNDED_BITLENGTH = OPERAND_COUNT * OPERAND_BITS;
static constexpr const size_t ARRAY_SIZE = (ROUNDED_BITLENGTH / MEMORY_BITS) + (ROUNDED_BITLENGTH % MEMORY_BITS != 0 ? 1 : 0);
static inline constexpr EnumeratorDataContainer max()
{
EnumeratorDataContainer c;
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
c._data[i] = std::numeric_limits<OperandType>::max();
}
return c;
}
public:
inline constexpr EnumeratorDataContainer() noexcept = default;
inline constexpr EnumeratorDataContainer(const EnumeratorDataContainer& other) noexcept = default;
inline constexpr EnumeratorDataContainer(OperandType value) noexcept : _data{value} { }
inline explicit constexpr operator bool() const
{
return !equals(0);
}
inline constexpr bool equals(const EnumeratorDataContainer& other) const
{
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
if (_data[i] != other._data[i])
return false;
}
return true;
}
inline constexpr bool equals(OperandType other) const
{
if (_data[0] != other)
return false;
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
if (_data[i] != 0)
return false;
}
return true;
}
inline constexpr bool operator==(const EnumeratorDataContainer& other) const
{
return equals(other);
}
inline constexpr bool operator==(OperandType other) const
{
return equals(other);
}
inline constexpr bool operator!=(const EnumeratorDataContainer& other) const
{
return !equals(other);
}
inline constexpr bool operator!=(OperandType other) const
{
return !equals(other);
}
inline constexpr bool has_bit(size_t bit) const
{
const size_t offset = bit / OPERAND_BITS;
const size_t rest = bit % OPERAND_BITS;
if (offset < OPERAND_COUNT)
return _data[offset] & (static_cast<OperandType>(1) << rest);
return false;
}
inline constexpr EnumeratorDataContainer operator&(const EnumeratorDataContainer& other) const
{
EnumeratorDataContainer r{};
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
r._data[i] = _data[i] & other._data[i];
}
return r;
}
inline constexpr EnumeratorDataContainer operator&(OperandType other) const
{
EnumeratorDataContainer r{};
r._data[0] = _data[0] & other;
for (size_t i = 1; i < OPERAND_COUNT; ++i)
{
r._data[i] = 0;
}
return r;
}
inline constexpr EnumeratorDataContainer operator&=(const EnumeratorDataContainer& other)
{
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
_data[i] = _data[i] & other._data[i];
}
return *this;
}
inline constexpr EnumeratorDataContainer operator&=(OperandType other)
{
_data[0] = _data[0] & other;
for (size_t i = 1; i < OPERAND_COUNT; ++i)
{
_data[i] = 0;
}
return *this;
}
inline constexpr EnumeratorDataContainer operator|(const EnumeratorDataContainer& other) const
{
EnumeratorDataContainer r{};
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
r._data[i] = _data[i] | other._data[i];
}
return r;
}
inline constexpr EnumeratorDataContainer operator|(OperandType other) const
{
EnumeratorDataContainer r{};
r._data[0] = _data[0] | other;
for (size_t i = 1; i < OPERAND_COUNT; ++i)
{
r._data[i] = _data[i];
}
return r;
}
inline constexpr EnumeratorDataContainer operator|=(const EnumeratorDataContainer& other)
{
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
_data[i] = _data[i] | other._data[i];
}
return *this;
}
inline constexpr EnumeratorDataContainer operator|=(OperandType other)
{
_data[0] = _data[0] | other;
return *this;
}
inline constexpr EnumeratorDataContainer operator^(const EnumeratorDataContainer& other) const
{
EnumeratorDataContainer r{};
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
r._data[i] = _data[i] ^ other._data[i];
}
return r;
}
inline constexpr EnumeratorDataContainer operator^(OperandType other) const
{
EnumeratorDataContainer r{};
r._data[0] = _data[0] ^ other;
for (size_t i = 1; i < OPERAND_COUNT; ++i)
{
r._data[i] = _data[i] ^ 0;
}
return r;
}
inline constexpr EnumeratorDataContainer operator^=(const EnumeratorDataContainer& other)
{
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
_data[i] = _data[i] ^ other._data[i];
}
return *this;
}
inline constexpr EnumeratorDataContainer operator^=(OperandType other)
{
_data[0] = _data[0] ^ other;
for (size_t i = 1; i < OPERAND_COUNT; ++i)
{
_data[i] = _data[i] ^ 0;
}
return *this;
}
inline constexpr EnumeratorDataContainer operator~() const
{
EnumeratorDataContainer r{};
for (size_t i = 0; i < OPERAND_COUNT; ++i)
{
r._data[i] = ~_data[i];
}
return r;
}
inline constexpr EnumeratorDataContainer operator<<(size_t bits) const
{
if (bits == 0)
return *this;
if (bits == OPERAND_BITS * OPERAND_COUNT)
return {};
EnumeratorDataContainer ret{};
const size_t offset = bits / OPERAND_BITS;
const size_t rest = (bits % OPERAND_BITS);
if (rest > 0)
{
OperandType carry = 0;
for (size_t indexRead = 0, indexWrite = offset; indexRead < OPERAND_COUNT - offset; ++indexRead, ++indexWrite)
{
OperandType value = _data[indexRead];
ret._data[indexWrite] = (value << rest) | carry;
carry = value >> (OPERAND_BITS - rest);
}
}
else
{
for (size_t indexRead = 0, indexWrite = offset; indexRead < OPERAND_COUNT - offset; ++indexRead, ++indexWrite)
ret._data[indexWrite] = _data[indexRead];
}
return ret;
}
inline constexpr EnumeratorDataContainer operator>>(size_t bits) const
{
if (bits == 0)
return *this;
if (bits == OPERAND_BITS * OPERAND_COUNT)
return {};
EnumeratorDataContainer ret{};
const int offset = bits / OPERAND_BITS;
const int rest = (bits % OPERAND_BITS);
if (rest > 0)
{
OperandType carry = 0;
for (int indexRead = OPERAND_COUNT - 1, indexWrite = indexRead - offset; indexRead >= offset; --indexRead, --indexWrite)
{
OperandType value = _data[indexRead];
ret._data[indexWrite] = (value >> rest) | carry;
carry = value << (OPERAND_BITS - rest);
}
}
else
{
for (int indexRead = OPERAND_COUNT - 1, indexWrite = indexRead - offset; indexRead >= offset; --indexRead, --indexWrite)
ret._data[indexWrite] = _data[indexRead];
}
return ret;
}
public:
OperandType _data[OPERAND_COUNT];
};
template <typename EnumType, typename DataType, size_t bit_length, bool isFlags>
class EnumeratorConverter
{
public:
static inline constexpr DataType get_data(EnumType value)
{
auto data_value = static_cast<typename std::make_unsigned<EnumType>::type>(value);
if (data_value > bit_length)
return static_cast<DataType>(0);
if (data_value == 0)
return 0;
return static_cast<DataType>(1) << (data_value - 1);
}
static inline constexpr EnumType get_value(DataType data)
{
if (data == 0)
return static_cast<EnumType>(0);
DataType i=0;
while( !((data >> i++) & 0x01) ) { ; }
return static_cast<EnumType>(i);
}
static inline constexpr EnumType get_bit(size_t index)
{
return static_cast<EnumType>(index);
}
};
template <typename EnumType, typename DataType, size_t bit_length>
class EnumeratorConverter<EnumType, DataType, bit_length, true>
{
public:
static inline constexpr DataType get_data(EnumType value)
{
static_cast<DataType>(value);
}
static inline constexpr EnumType get_value(DataType data)
{
static_cast<EnumType>(data);
}
static inline constexpr EnumType get_bit(size_t index)
{
return static_cast<EnumType>(1 << index);
}
};
template <typename EnumType>
class EnumeratorMeta
{
};
template <typename EnumType>
class EnumeratorInherited
{
public:
using InheritedType = EnumType;
};
template<typename T, typename = void>
struct enumerator_has_extension_meta : std::false_type { };
template<typename T>
struct enumerator_has_extension_meta<T, decltype((void)EnumeratorMeta<T>::enum_extension, void())> : std::true_type { };
template<typename T, typename = void>
struct enumerator_has_extension_value : std::false_type { };
template<typename T>
struct enumerator_has_extension_value<T, decltype((void)T::EXTENSION, void())> : std::true_type { };
template<typename T>
struct enumerator_has_extension : std::integral_constant<bool, enumerator_has_extension_meta<T>::value || enumerator_has_extension_value<T>::value> { };
template<typename T, typename = void>
struct enumerator_has_inheritance_meta : std::false_type { };
template<typename T>
struct enumerator_has_inheritance_meta<T, decltype((void)EnumeratorMeta<T>::enum_inheritance, void())> : std::true_type { };
template<typename T, typename = void>
struct enumerator_has_inheritance_value : std::false_type { };
template<typename T>
struct enumerator_has_inheritance_value<T, decltype((void)T::INHERITANCE, void())> : std::true_type { };
template<typename T>
struct enumerator_has_inheritance : std::integral_constant<bool, enumerator_has_inheritance_meta<T>::value || enumerator_has_inheritance_value<T>::value> { };
template<typename T, typename = void>
struct enumerator_has_base_type : std::false_type { };
template<typename T>
struct enumerator_has_base_type<T, decltype((void)typename EnumeratorMeta<T>::BaseEnumType(), void())> : std::true_type { };
template <typename EnumType>
class EnumeratorInfo
{
template <typename>
friend class EnumeratorInfo;
protected:
using Meta = EnumeratorMeta<EnumType>;
using DataType = typename Meta::DataType;
using Extender = typename Meta::Extender;
using Inheritor = typename Meta::Inheritor;
public:
using EntryType = typename std::remove_const< typename std::remove_reference< typename std::remove_pointer< decltype(Meta::enum_entries[0]) >::type >::type >::type;
static constexpr const size_t ENTRY_COUNT = sizeof(EnumeratorMeta<EnumType>::enum_entries) / sizeof(EntryType);
struct Result
{
friend class EnumeratorInfo;
public:
constexpr Result() = default;
constexpr explicit operator bool() const { return _ptr != nullptr; }
constexpr const EntryType& operator*() const { return *_ptr; }
constexpr const EntryType* operator->() { return _ptr; }
constexpr const EntryType* pointer() { return _ptr; }
protected:
constexpr Result(const EntryType* entry) : _ptr{entry} {}
protected:
const EntryType* _ptr{};
};
struct Iterator
{
public:
using iterator_category = std::input_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = EntryType;
using pointer = const EntryType*;
using reference = const EntryType&;
public:
constexpr Iterator(pointer ptr) : _ptr{ptr} { }
reference operator*() const { return *_ptr; }
pointer operator->() { return _ptr; }
Iterator& operator++() { _ptr++; return *this; }
Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }
friend bool operator== (const Iterator& a, const Iterator& b) { return a._ptr == b._ptr; };
friend bool operator!= (const Iterator& a, const Iterator& b) { return a._ptr != b._ptr; };
private:
pointer _ptr;
};
static constexpr Iterator cbegin() { return Iterator(EnumeratorMeta<EnumType>::enum_entries); }
static constexpr Iterator cend() { return Iterator(EnumeratorMeta<EnumType>::enum_entries + ENTRY_COUNT);}
public:
static constexpr Result find(EnumType value)
{
auto result = findQuick(value);
if (result)
return result;
return findSlow(value);
}
static constexpr Result find(const char* name)
{
Result result = findSelf(name);
if (result)
return result;
result = findInherited(name);
if (result)
return result;
result = findExtended(name);
if (result)
return result;
return {};
}
protected:
template <typename T = EnumType, typename std::enable_if<enumerator_has_base_type<T>::value, int>::type = 0>
static constexpr size_t getQuickIndex(EnumType value)
{
using BaseMeta = EnumeratorMeta<typename EnumeratorMeta<T>::BaseEnumType>;
using BaseInheritor = typename BaseMeta::Inheritor;
auto dataValue = static_cast<DataType>(value);
auto dataMin = static_cast<DataType>(BaseInheritor::get_inheritance());
auto index = static_cast<size_t>(dataValue - dataMin) + 1;
return index;
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_base_type<T>::value, int>::type = 0>
static constexpr size_t getQuickIndex(EnumType value)
{
auto dataValue = static_cast<DataType>(value);
auto dataMin = static_cast<DataType>(Meta::MIN_VALUE);
auto index = static_cast<size_t>(dataValue - dataMin);
return index;
}
static constexpr Result findQuickSelf(EnumType value)
{
auto index = getQuickIndex(value);
if (index < ENTRY_COUNT)
{
auto&& entry = EnumeratorMeta<EnumType>::enum_entries[index];
if (entry.get_value() == value)
return {&entry};
}
// Special case if enum_entries skips the first 0-valued entry
if (index > 0 && index <= ENTRY_COUNT)
{
auto&& entry = EnumeratorMeta<EnumType>::enum_entries[index - 1];
if (entry.get_value() == value)
return {&entry};
}
return {};
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_inheritance<T>::value && !std::is_same<T, typename EnumeratorInherited<T>::InheritedType>::value, int>::type = 0>
static constexpr Result findQuickInherited(EnumType value)
{
using InheritedType = typename EnumeratorInherited<T>::InheritedType;
using InheritedInfo = EnumeratorInfo<InheritedType>;
auto inheritance = Inheritor::get_inheritance();
auto dataValue = static_cast<DataType>(value);
auto dataInheritance = static_cast<DataType>(inheritance);
if (dataValue >= dataInheritance)
{
return { InheritedInfo::find(static_cast<InheritedType>(value)).pointer() };
}
return {};
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_inheritance<T>::value || std::is_same<T, typename EnumeratorInherited<T>::InheritedType>::value, int>::type = 0>
static constexpr Result findQuickInherited(EnumType)
{
return {};
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_extension<T>::value, int>::type = 0>
static constexpr Result findQuickExtended(EnumType value)
{
auto extension = Extender::get_extension();
auto dataValue = static_cast<DataType>(value);
auto dataExtension = static_cast<DataType>(extension);
if (dataValue >= dataExtension)
{
auto index = static_cast<size_t>(dataValue - dataExtension);
auto&& container = Extender::get_container();
// Note: because of how enum extensions work, we don't need an equivalent findSlow() for extensions
if (index < container.ENTRY_COUNT)
{
auto&& entry = container.enum_entries[index];
if (entry.get_value() == value)
return {&entry};
}
}
return {};
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_extension<T>::value, int>::type = 0>
static constexpr Result findQuickExtended(EnumType)
{
return {};
}
static constexpr Result findQuick(EnumType value)
{
// Return if the enum values aren't sequential, i.e. their values are already bitflags (isFlags parameter passed to EnumeratorMetaDefault was true)
if (!EnumeratorMeta<EnumType>::bitwise_conversion)
return {};
Result result = findQuickSelf(value);
if (result)
return result;
result = findQuickInherited(value);
if (result)
return result;
result = findQuickExtended(value);
if (result)
return result;
return {};
}
static constexpr Result findSlow(EnumType value)
{
auto it = cbegin();
auto end = cend();
for (; it != end; ++it)
{
auto&& entry = *it;
if (entry.get_value() == value)
return {&entry};
}
return {};
}
static constexpr bool matchEntry(const EntryType& entry, const char* name)
{
if (entry.get_name() == name)
return true;
else
{
if (name != nullptr && entry.get_name() != nullptr)
{
auto nameP = name;
auto itName = entry.get_name();
while (*itName != '\0' && *nameP != '\0' && *itName == *nameP)
{
itName++;
nameP++;
}
if (*itName == '\0' && *nameP == '\0')
return true;
}
}
return false;
}
static constexpr Result findSelf(const char* name)
{
auto it = cbegin();
auto end = cend();
for (; it != end; ++it)
{
auto&& entry = *it;
if (matchEntry(entry, name))
return {&entry};
}
return {};
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_inheritance<T>::value && !std::is_same<T, typename EnumeratorInherited<T>::InheritedType>::value, int>::type = 0>
static constexpr Result findInherited(const char* name)
{
using InheritedType = typename EnumeratorInherited<T>::InheritedType;
using InheritedInfo = EnumeratorInfo<InheritedType>;
return { InheritedInfo::find(name).pointer() };
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_inheritance<T>::value || std::is_same<T, typename EnumeratorInherited<T>::InheritedType>::value, int>::type = 0>
static constexpr Result findInherited(const char*)
{
return {};
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_extension<T>::value, int>::type = 0>
static constexpr Result findExtended(const char* name)
{
auto&& container = Extender::get_container();
Iterator it(container.enum_entries);
Iterator end(container.enum_entries + container.ENTRY_COUNT);
for (; it != end; ++it)
{
auto&& entry = *it;
if (matchEntry(entry, name))
return {&entry};
}
return {};
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_extension<T>::value, int>::type = 0>
static constexpr Result findExtended(const char*)
{
return {};
}
};
template <typename EnumType>
class EnumeratorSerializer
{
using Info = EnumeratorInfo<EnumType>;
using EntryType = typename Info::EntryType;
public:
static constexpr decltype(std::declval<EntryType>().get_name()) get_name(EnumType value)
{
auto it = Info::find(value);
if (it)
return it->get_name();
return nullptr;
}
static constexpr decltype(std::declval<EntryType>().get_label()) get_label(EnumType value)
{
auto it = Info::find(value);
if (it)
return it->get_label();
return nullptr;
}
static constexpr EnumType get_value(const char* name)
{
auto it = Info::find(name);
if (it)
return it->get_value();
return EnumeratorMeta<EnumType>::Converter::get_value(0);
}
};
template <typename EnumType>
class EnumeratorExtender
{
protected:
using Meta = EnumeratorMeta<EnumType>;
using Info = EnumeratorInfo<EnumType>;
using EntryType = typename Info::EntryType;
using DataType = typename Meta::DataType;
static constexpr const DataType max_enum_value = static_cast<DataType>(Meta::MAX_VALUE);
public:
template <typename T = EnumType, typename std::enable_if<enumerator_has_extension<T>::value, int>::type = 0>
static constexpr bool has_extension()
{
return true;
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_extension<T>::value, int>::type = 0>
static constexpr bool has_extension()
{
return false;
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_extension_meta<T>::value, int>::type = 0>
static constexpr T get_extension()
{
return static_cast<T>(Meta::enum_extension);
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_extension_value<T>::value && !enumerator_has_extension_meta<T>::value, int>::type = 0>
static constexpr T get_extension()
{
return static_cast<T>(EnumType::EXTENSION);
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_extension<T>::value, int>::type = 0>
static constexpr EnumType get_extension()
{
static_assert(sizeof(T) == 0, "EnumeratorExtender requires the EnumeratorMeta to define an enum_extension variable or the enum to contain an EXTENSION value.");
}
template<EnumType extension>
struct Container
{
static constexpr const size_t ENTRY_COUNT = max_enum_value - static_cast<DataType>(extension);
EntryType enum_entries[ENTRY_COUNT]{};
EnumType enum_extension = extension;
};
// Making this a templated function is an unfortunately required hack to build on MSVC
template<EnumType extension = get_extension()>
static Container<extension>& get_container()
{
static_assert (extension == get_extension(), "Do not pass a template parameter to this function, read comment above.");
static Container<extension> container{};
return container;
}
template <typename... Types>
static EnumType extend(Types... entries)
{
auto&& container = get_container<get_extension()>();
if (static_cast<DataType>(container.enum_extension) <= max_enum_value)
{
auto return_enum = container.enum_extension;
auto entry_index = static_cast<DataType>(return_enum) - static_cast<DataType>(get_extension());
container.enum_entries[entry_index] = EntryType{return_enum, entries...};
container.enum_extension = static_cast<EnumType>(static_cast<DataType>(return_enum) + 1);
return return_enum;
}
// This is not ideal, if extensions overflow we should throw? or at least assert?
return static_cast<EnumType>(0);
}
};
template <typename EnumType>
class EnumeratorInheritor
{
protected:
using Meta = EnumeratorMeta<EnumType>;
using DataType = typename Meta::DataType;
static constexpr const DataType max_enum_value = static_cast<DataType>(Meta::MAX_VALUE);
public:
template <typename T = EnumType, typename std::enable_if<enumerator_has_inheritance<T>::value, int>::type = 0>
static constexpr bool has_inheritance()
{
return true;
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_inheritance<T>::value, int>::type = 0>
static constexpr bool has_inheritance()
{
return false;
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_inheritance_meta<T>::value, int>::type = 0>
static constexpr T get_inheritance()
{
return static_cast<T>(Meta::enum_inheritance);
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_inheritance_value<T>::value && !enumerator_has_inheritance_meta<T>::value, int>::type = 0>
static constexpr T get_inheritance()
{
return static_cast<T>(EnumType::INHERITANCE);
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_inheritance<T>::value, int>::type = 0>
static constexpr EnumType get_enum_inheritance()
{
static_assert(sizeof(T) == 0, "EnumeratorInheritor requires the EnumeratorMeta to define an enum_inheritance variable or the enum to contain an INHERITANCE value.");
}
static constexpr DataType inherit()
{
return static_cast<DataType>(get_inheritance());
}
static constexpr DataType inheritMaximum()
{
return max_enum_value;
}
template <typename T = EnumType, typename std::enable_if<enumerator_has_extension<T>::value, int>::type = 0>
static constexpr DataType inheritExtension()
{
return static_cast<DataType>(Meta::Extender::get_extension());
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_extension<T>::value, int>::type = 0>
static constexpr DataType inheritExtension()
{
return inheritMaximum();
}
};
template <typename EnumType>
class EnumeratorSpecializer
{
protected:
using Meta = EnumeratorMeta<EnumType>;
using DataType = typename Meta::DataType;
public:
template <typename T = EnumType, typename std::enable_if<enumerator_has_base_type<T>::value, int>::type = 0>
static constexpr bool has_base()
{
return true;
}
template <typename T = EnumType, typename std::enable_if<!enumerator_has_base_type<T>::value, int>::type = 0>
static constexpr bool has_base()
{
return false;
}
};
template <typename EnumType_>
class SmartEnumerator
{
protected:
using Meta = EnumeratorMeta<EnumType_>;
public:
using EnumType = EnumType_;
using DataType = typename Meta::DataType;
public:
constexpr SmartEnumerator(EnumType value) : m_data(value) { }
template<typename EnumT, typename std::enable_if<std::is_enum<EnumT>::value && (std::is_same<EnumT, typename Meta::BaseEnumType>::value || std::is_same<EnumType, typename EnumeratorMeta<EnumT>::BaseEnumType>::value), int>::type = 0>
constexpr SmartEnumerator(EnumT value) : SmartEnumerator(static_cast<DataType>(value)) { }
protected:
constexpr SmartEnumerator(DataType data) : m_data(static_cast<EnumType>(data)) { }
public:
constexpr DataType data() const
{
return m_data;
}
constexpr bool operator==(SmartEnumerator a) const
{
return m_data == a.m_data;
}
constexpr bool operator==(EnumType a) const
{
return m_data == Meta::Converter::get_data(a);
}
constexpr bool operator!=(SmartEnumerator a) const
{
return m_data != a.m_data;
}
constexpr bool operator!=(EnumType a) const
{
return m_data != Meta::Converter::get_data(a);
}
constexpr operator bool() const
{
return m_data != 0;
}
protected:
EnumType m_data;
};
template <typename EnumType, size_t bit_length = std::numeric_limits<typename std::make_unsigned<EnumType>::type>::digits>
class EnumeratorMask
{
protected:
using Meta = EnumeratorMeta<EnumType>;
public:
using InnerType = EnumType;