diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp index b12ac1ed..b5ccec9b 100644 --- a/include/rfl/enums.hpp +++ b/include/rfl/enums.hpp @@ -32,6 +32,15 @@ auto get_enumerators() { return internal::enums::names_to_enumerator_named_tuple(names); } +// Returns a named tuple mapping names of enumerators of the given enum type to +// their underlying values. +template +auto get_enumerators_underlying() { + constexpr auto names = internal::enums::get_enum_names< + EnumType, internal::enums::is_flag_enum>(); + return internal::enums::names_to_enumerator_named_tuple_underlying(names); +} + // Returns an std::array containing pairs of enumerator names (as // std::string_view) and values. template @@ -41,6 +50,15 @@ constexpr auto get_enumerator_array() { return internal::enums::names_to_enumerator_array(names); } +// Returns an std::array containing pairs of enumerator names (as +// std::string_view) and underlying values. +template +constexpr auto get_enumerator_array_underlying() { + constexpr auto names = internal::enums::get_enum_names< + EnumType, internal::enums::is_flag_enum>(); + return internal::enums::names_to_enumerator_array_underlying(names); +} + } // namespace rfl #endif // RFL_ENUMS_HPP_ diff --git a/include/rfl/internal/enums/Names.hpp b/include/rfl/internal/enums/Names.hpp index b836b971..46b1b5b2 100644 --- a/include/rfl/internal/enums/Names.hpp +++ b/include/rfl/internal/enums/Names.hpp @@ -45,6 +45,13 @@ auto names_to_enumerator_named_tuple( return make_named_tuple(Field<_names, EnumType>{_enums}...); } +template +auto names_to_enumerator_named_tuple_underlying( + Names, N, _enums...>) { + return make_named_tuple(Field<_names, std::underlying_type_t>{ + static_cast>(_enums)}...); +} + template constexpr std::array, N> names_to_enumerator_array(Names, N, _enums...>) { @@ -52,6 +59,16 @@ names_to_enumerator_array(Names, N, _enums...>) { std::make_pair(LiteralHelper<_names>::field_.string_view(), _enums)...}; } +template +constexpr std::array< + std::pair>, N> +names_to_enumerator_array_underlying( + Names, N, _enums...>) { + return { + std::make_pair(LiteralHelper<_names>::field_.string_view(), + static_cast>(_enums))...}; +} + } // namespace enums } // namespace internal } // namespace rfl diff --git a/tests/json/test_enum.cpp b/tests/json/test_enum.cpp index ac0a7156..7d7a542f 100644 --- a/tests/json/test_enum.cpp +++ b/tests/json/test_enum.cpp @@ -50,6 +50,13 @@ void test() { }); write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})"); + rfl::get_enumerators_underlying().apply([&](auto field) { + if constexpr (decltype(field)::name() == "blue") { + mutable_circle.color = static_cast(field.value()); + } + }); + write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})"); + constexpr auto enumerator_array = rfl::get_enumerator_array(); static_assert(enumerator_array[0].first == "red"); static_assert(enumerator_array[1].first == "green"); @@ -59,6 +66,16 @@ void test() { static_assert(enumerator_array[1].second == Color::green); static_assert(enumerator_array[2].second == Color::blue); static_assert(enumerator_array[3].second == Color::yellow); + + constexpr auto enumerator_array_underlying = rfl::get_enumerator_array_underlying(); + static_assert(enumerator_array_underlying[0].first == "red"); + static_assert(enumerator_array_underlying[1].first == "green"); + static_assert(enumerator_array_underlying[2].first == "blue"); + static_assert(enumerator_array_underlying[3].first == "yellow"); + static_assert(enumerator_array_underlying[0].second == 0); + static_assert(enumerator_array_underlying[1].second == 1); + static_assert(enumerator_array_underlying[2].second == 2); + static_assert(enumerator_array_underlying[3].second == 3); } } // namespace test_enum