src/generics/enums.h

changeset 1319
39d7a9642eea
child 1326
69a90bd2dba2
equal deleted inserted replaced
1318:568fcfc6da71 1319:39d7a9642eea
1 #pragma once
2 #include <type_traits>
3
4 template<typename T>
5 struct EnumLimits {};
6
7 //
8 // Iterates an enum
9 //
10 template<typename Enum>
11 struct EnumIterShell
12 {
13 struct Iterator
14 {
15 Iterator(typename std::underlying_type<Enum>::type i) :
16 i(i) {}
17
18 Iterator& operator++() { ++i; return *this; }
19 bool operator==(Iterator other) { return i == other.i; }
20 bool operator!=(Iterator other) { return i != other.i; }
21 Enum operator*() const { return Enum(i); }
22
23 typename std::underlying_type<Enum>::type i;
24 };
25
26 Iterator begin()
27 {
28 return Iterator(EnumLimits<Enum>::First);
29 };
30
31 Iterator end()
32 {
33 return Iterator(EnumLimits<Enum>::Last + 1);
34 }
35 };
36
37 template<typename Enum>
38 EnumIterShell<Enum> iterateEnum()
39 {
40 return EnumIterShell<Enum>();
41 }
42
43 /*
44 * Casts an enum into its underlying type.
45 */
46 template<typename T>
47 typename std::underlying_type<T>::type& enum_cast(T& enu)
48 {
49 return *reinterpret_cast<typename std::underlying_type<T>::type*>(&enu);
50 }
51
52 /*
53 * Returns whether an enum value is within proper bounds.
54 */
55 template<typename Enum>
56 bool valueInEnum(Enum enumerator)
57 {
58 auto index = enum_cast(enumerator);
59 return index >= EnumLimits<Enum>::First and index <= EnumLimits<Enum>::Last;
60 }
61
62 #define MAKE_ITERABLE_ENUM(T) \
63 template<> \
64 struct EnumLimits<T> \
65 {\
66 enum { First = 0, Last = static_cast<int>(T::_End) - 1, Count = Last + 1 };\
67 }; \
68 inline T operator++ (T& a) { a = static_cast<T>(enum_cast(a) + 1); return a; } \
69 inline T operator-- (T& a) { a = static_cast<T>(enum_cast(a) - 1); return a; } \
70 inline T operator++ (T& a, int) { T result = a; a = static_cast<T>(enum_cast(a) + 1); return result; } \
71 inline T operator-- (T& a, int) { T result = a; a = static_cast<T>(enum_cast(a) - 1); return result; }

mercurial