Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <array> |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 5 | #include <stdexcept> |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 6 | #include <string> |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 7 | |
| 8 | namespace utils |
| 9 | { |
| 10 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame^] | 11 | template <class T> |
| 12 | struct EnumTraits |
| 13 | { |
| 14 | [[noreturn]] static void throwConversionError() |
| 15 | { |
| 16 | throw std::out_of_range("Value is not in range of enum"); |
| 17 | } |
| 18 | }; |
| 19 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 20 | template <class T, T first, T last> |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 21 | inline T toEnum(std::underlying_type_t<T> x) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 22 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 23 | if (x < static_cast<std::underlying_type_t<T>>(first) || |
| 24 | x > static_cast<std::underlying_type_t<T>>(last)) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 25 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame^] | 26 | EnumTraits<T>::throwConversionError(); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 27 | } |
| 28 | return static_cast<T>(x); |
| 29 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 30 | |
| 31 | template <class T> |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame^] | 32 | constexpr inline std::underlying_type_t<T> toUnderlying(T value) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 33 | { |
| 34 | return static_cast<std::underlying_type_t<T>>(value); |
| 35 | } |
| 36 | |
| 37 | template <class T, size_t N> |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame^] | 38 | constexpr inline T |
| 39 | minEnumValue(std::array<std::pair<std::string_view, T>, N> data) |
| 40 | { |
| 41 | auto min = data[0].second; |
| 42 | for (auto [key, value] : data) |
| 43 | { |
| 44 | if (toUnderlying(min) > toUnderlying(value)) |
| 45 | { |
| 46 | min = value; |
| 47 | } |
| 48 | } |
| 49 | return min; |
| 50 | } |
| 51 | |
| 52 | template <class T, size_t N> |
| 53 | constexpr inline T |
| 54 | maxEnumValue(std::array<std::pair<std::string_view, T>, N> data) |
| 55 | { |
| 56 | auto max = data[0].second; |
| 57 | for (auto [key, value] : data) |
| 58 | { |
| 59 | if (toUnderlying(max) < toUnderlying(value)) |
| 60 | { |
| 61 | max = value; |
| 62 | } |
| 63 | } |
| 64 | return max; |
| 65 | } |
| 66 | |
| 67 | template <class T, size_t N> |
| 68 | inline T toEnum(const std::array<std::pair<std::string_view, T>, N>& data, |
| 69 | const std::string& value) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 70 | { |
| 71 | auto it = std::find_if( |
| 72 | std::begin(data), std::end(data), |
| 73 | [&value](const auto& item) { return item.first == value; }); |
| 74 | if (it == std::end(data)) |
| 75 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame^] | 76 | EnumTraits<T>::throwConversionError(); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 77 | } |
| 78 | return it->second; |
| 79 | } |
| 80 | |
| 81 | template <class T, size_t N> |
| 82 | inline std::string_view |
| 83 | enumToString(const std::array<std::pair<std::string_view, T>, N>& data, |
| 84 | T value) |
| 85 | { |
| 86 | auto it = std::find_if( |
| 87 | std::begin(data), std::end(data), |
| 88 | [value](const auto& item) { return item.second == value; }); |
| 89 | if (it == std::end(data)) |
| 90 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame^] | 91 | EnumTraits<T>::throwConversionError(); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 92 | } |
| 93 | return it->first; |
| 94 | } |
| 95 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 96 | } // namespace utils |