Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 3 | #include "errors.hpp" |
| 4 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 5 | #include <sdbusplus/exception.hpp> |
| 6 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <array> |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 9 | #include <stdexcept> |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 10 | #include <string> |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 11 | |
| 12 | namespace utils |
| 13 | { |
| 14 | |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 15 | template <size_t N> |
| 16 | struct ConstexprString |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 17 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 18 | constexpr ConstexprString(const char (&data)[N]) : s() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 19 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 20 | for (size_t i = 0; i < N; ++i) |
| 21 | { |
| 22 | s[i] = data[i]; |
| 23 | } |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 24 | } |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 25 | |
| 26 | constexpr operator std::string_view() const |
| 27 | { |
| 28 | return {s.data(), s.size()}; |
| 29 | } |
| 30 | |
| 31 | std::array<char, N> s; |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 34 | [[noreturn]] inline void throwConversionError(std::string_view propertyName) |
| 35 | { |
| 36 | throw errors::InvalidArgument(propertyName, "Cannot convert."); |
| 37 | } |
| 38 | |
| 39 | template <class T> |
| 40 | struct EnumTraits; |
| 41 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 42 | template <class T, T first, T last> |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 43 | inline T toEnum(std::underlying_type_t<T> x) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 44 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 45 | if (x < static_cast<std::underlying_type_t<T>>(first) || |
| 46 | x > static_cast<std::underlying_type_t<T>>(last)) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 47 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 48 | throwConversionError(EnumTraits<T>::propertyName); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 49 | } |
| 50 | return static_cast<T>(x); |
| 51 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 52 | |
| 53 | template <class T> |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 54 | constexpr inline std::underlying_type_t<T> toUnderlying(T value) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 55 | { |
| 56 | return static_cast<std::underlying_type_t<T>>(value); |
| 57 | } |
| 58 | |
| 59 | template <class T, size_t N> |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 60 | constexpr inline T |
| 61 | minEnumValue(std::array<std::pair<std::string_view, T>, N> data) |
| 62 | { |
| 63 | auto min = data[0].second; |
| 64 | for (auto [key, value] : data) |
| 65 | { |
| 66 | if (toUnderlying(min) > toUnderlying(value)) |
| 67 | { |
| 68 | min = value; |
| 69 | } |
| 70 | } |
| 71 | return min; |
| 72 | } |
| 73 | |
| 74 | template <class T, size_t N> |
| 75 | constexpr inline T |
| 76 | maxEnumValue(std::array<std::pair<std::string_view, T>, N> data) |
| 77 | { |
| 78 | auto max = data[0].second; |
| 79 | for (auto [key, value] : data) |
| 80 | { |
| 81 | if (toUnderlying(max) < toUnderlying(value)) |
| 82 | { |
| 83 | max = value; |
| 84 | } |
| 85 | } |
| 86 | return max; |
| 87 | } |
| 88 | |
| 89 | template <class T, size_t N> |
| 90 | inline T toEnum(const std::array<std::pair<std::string_view, T>, N>& data, |
| 91 | const std::string& value) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 92 | { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 93 | auto it = std::find_if( |
| 94 | std::begin(data), std::end(data), |
| 95 | [&value](const auto& item) { return item.first == value; }); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 96 | if (it == std::end(data)) |
| 97 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 98 | throwConversionError(EnumTraits<T>::propertyName); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 99 | } |
| 100 | return it->second; |
| 101 | } |
| 102 | |
| 103 | template <class T, size_t N> |
| 104 | inline std::string_view |
| 105 | enumToString(const std::array<std::pair<std::string_view, T>, N>& data, |
| 106 | T value) |
| 107 | { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 108 | auto it = std::find_if( |
| 109 | std::begin(data), std::end(data), |
| 110 | [value](const auto& item) { return item.second == value; }); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 111 | if (it == std::end(data)) |
| 112 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 113 | throwConversionError(EnumTraits<T>::propertyName); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 114 | } |
| 115 | return it->first; |
| 116 | } |
| 117 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 118 | } // namespace utils |