Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "utils/conversion.hpp" |
| 4 | |
| 5 | #include <sdbusplus/exception.hpp> |
| 6 | |
| 7 | #include <array> |
| 8 | #include <cstdint> |
| 9 | #include <string_view> |
| 10 | #include <type_traits> |
| 11 | |
| 12 | enum class ReportUpdates : uint32_t |
| 13 | { |
| 14 | overwrite, |
| 15 | appendStopsWhenFull, |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 16 | appendWrapsWhenFull |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | namespace utils |
| 20 | { |
| 21 | |
| 22 | template <> |
| 23 | struct EnumTraits<ReportUpdates> |
| 24 | { |
| 25 | [[noreturn]] static void throwConversionError() |
| 26 | { |
| 27 | throw sdbusplus::exception::SdBusError( |
| 28 | static_cast<int>(std::errc::invalid_argument), |
| 29 | "Invalid ReportUpdates"); |
| 30 | } |
| 31 | }; |
| 32 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 33 | constexpr auto convDataReportUpdates = |
| 34 | std::array{std::make_pair<std::string_view, ReportUpdates>( |
| 35 | "Overwrite", ReportUpdates::overwrite), |
| 36 | std::make_pair<std::string_view, ReportUpdates>( |
| 37 | "AppendStopsWhenFull", ReportUpdates::appendStopsWhenFull), |
| 38 | std::make_pair<std::string_view, ReportUpdates>( |
| 39 | "AppendWrapsWhenFull", ReportUpdates::appendWrapsWhenFull)}; |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 40 | |
| 41 | inline ReportUpdates |
| 42 | toReportUpdates(std::underlying_type_t<ReportUpdates> value) |
| 43 | { |
| 44 | return toEnum<ReportUpdates, minEnumValue(convDataReportUpdates), |
| 45 | maxEnumValue(convDataReportUpdates)>(value); |
| 46 | } |
| 47 | |
| 48 | inline ReportUpdates toReportUpdates(const std::string& value) |
| 49 | { |
| 50 | return toEnum(convDataReportUpdates, value); |
| 51 | } |
| 52 | |
| 53 | inline std::string enumToString(ReportUpdates value) |
| 54 | { |
| 55 | return std::string(enumToString(convDataReportUpdates, value)); |
| 56 | } |
| 57 | |
| 58 | } // namespace utils |