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, |
| 16 | appendWrapsWhenFull, |
| 17 | newReport |
| 18 | }; |
| 19 | |
| 20 | namespace utils |
| 21 | { |
| 22 | |
| 23 | template <> |
| 24 | struct EnumTraits<ReportUpdates> |
| 25 | { |
| 26 | [[noreturn]] static void throwConversionError() |
| 27 | { |
| 28 | throw sdbusplus::exception::SdBusError( |
| 29 | static_cast<int>(std::errc::invalid_argument), |
| 30 | "Invalid ReportUpdates"); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | constexpr std::array<std::pair<std::string_view, ReportUpdates>, 4> |
| 35 | convDataReportUpdates = { |
| 36 | {std::make_pair<std::string_view, ReportUpdates>( |
| 37 | "Overwrite", ReportUpdates::overwrite), |
| 38 | std::make_pair<std::string_view, ReportUpdates>( |
| 39 | "AppendStopsWhenFull", ReportUpdates::appendStopsWhenFull), |
| 40 | std::make_pair<std::string_view, ReportUpdates>( |
| 41 | "AppendWrapsWhenFull", ReportUpdates::appendWrapsWhenFull), |
| 42 | std::make_pair<std::string_view, ReportUpdates>( |
| 43 | "NewReport", ReportUpdates::newReport)}}; |
| 44 | |
| 45 | inline ReportUpdates |
| 46 | toReportUpdates(std::underlying_type_t<ReportUpdates> value) |
| 47 | { |
| 48 | return toEnum<ReportUpdates, minEnumValue(convDataReportUpdates), |
| 49 | maxEnumValue(convDataReportUpdates)>(value); |
| 50 | } |
| 51 | |
| 52 | inline ReportUpdates toReportUpdates(const std::string& value) |
| 53 | { |
| 54 | return toEnum(convDataReportUpdates, value); |
| 55 | } |
| 56 | |
| 57 | inline std::string enumToString(ReportUpdates value) |
| 58 | { |
| 59 | return std::string(enumToString(convDataReportUpdates, value)); |
| 60 | } |
| 61 | |
| 62 | } // namespace utils |