Sat, 15 Sep 2018 15:57:56 +0300
refactor
| 1326 | 1 | /* |
| 2 | * LDForge: LDraw parts authoring CAD | |
| 3 | * Copyright (C) 2013 - 2018 Teemu Piippo | |
| 4 | * | |
| 5 | * This program is free software: you can redistribute it and/or modify | |
| 6 | * it under the terms of the GNU General Public License as published by | |
| 7 | * the Free Software Foundation, either version 3 of the License, or | |
| 8 | * (at your option) any later version. | |
| 9 | * | |
| 10 | * This program is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 1319 | 19 | #pragma once |
| 20 | #include <type_traits> | |
| 21 | ||
| 22 | template<typename T> | |
| 23 | struct EnumLimits {}; | |
| 24 | ||
| 25 | // | |
| 26 | // Iterates an enum | |
| 27 | // | |
| 28 | template<typename Enum> | |
| 29 | struct EnumIterShell | |
| 30 | { | |
| 31 | struct Iterator | |
| 32 | { | |
| 33 | Iterator(typename std::underlying_type<Enum>::type i) : | |
| 34 | i(i) {} | |
| 35 | ||
| 36 | Iterator& operator++() { ++i; return *this; } | |
| 37 | bool operator==(Iterator other) { return i == other.i; } | |
| 38 | bool operator!=(Iterator other) { return i != other.i; } | |
| 39 | Enum operator*() const { return Enum(i); } | |
| 40 | ||
| 41 | typename std::underlying_type<Enum>::type i; | |
| 42 | }; | |
| 43 | ||
| 44 | Iterator begin() | |
| 45 | { | |
| 46 | return Iterator(EnumLimits<Enum>::First); | |
| 47 | }; | |
| 48 | ||
| 49 | Iterator end() | |
| 50 | { | |
| 51 | return Iterator(EnumLimits<Enum>::Last + 1); | |
| 52 | } | |
| 53 | }; | |
| 54 | ||
| 55 | template<typename Enum> | |
| 56 | EnumIterShell<Enum> iterateEnum() | |
| 57 | { | |
| 58 | return EnumIterShell<Enum>(); | |
| 59 | } | |
| 60 | ||
| 61 | /* | |
| 62 | * Casts an enum into its underlying type. | |
| 63 | */ | |
| 64 | template<typename T> | |
| 1429 | 65 | constexpr typename std::underlying_type<T>::type& enum_cast(T& enu) |
| 1319 | 66 | { |
| 67 | return *reinterpret_cast<typename std::underlying_type<T>::type*>(&enu); | |
| 68 | } | |
| 69 | ||
| 70 | /* | |
| 71 | * Returns whether an enum value is within proper bounds. | |
| 72 | */ | |
| 73 | template<typename Enum> | |
| 1429 | 74 | constexpr bool valueInEnum(Enum enumerator) |
| 1319 | 75 | { |
| 76 | auto index = enum_cast(enumerator); | |
| 77 | return index >= EnumLimits<Enum>::First and index <= EnumLimits<Enum>::Last; | |
| 78 | } | |
| 79 | ||
| 80 | #define MAKE_ITERABLE_ENUM(T) \ | |
| 81 | template<> \ | |
| 82 | struct EnumLimits<T> \ | |
| 83 | {\ | |
| 84 | enum { First = 0, Last = static_cast<int>(T::_End) - 1, Count = Last + 1 };\ | |
| 85 | }; \ | |
| 86 | inline T operator++ (T& a) { a = static_cast<T>(enum_cast(a) + 1); return a; } \ | |
| 87 | inline T operator-- (T& a) { a = static_cast<T>(enum_cast(a) - 1); return a; } \ | |
| 88 | inline T operator++ (T& a, int) { T result = a; a = static_cast<T>(enum_cast(a) + 1); return result; } \ | |
| 89 | inline T operator-- (T& a, int) { T result = a; a = static_cast<T>(enum_cast(a) - 1); return result; } |