diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp index d4bf6ba9..b12ac1ed 100644 --- a/include/rfl/enums.hpp +++ b/include/rfl/enums.hpp @@ -5,20 +5,42 @@ #include "Result.hpp" #include "internal/enums/StringConverter.hpp" +#include "internal/enums/get_enum_names.hpp" +#include "internal/enums/is_flag_enum.hpp" #include "internal/enums/is_scoped_enum.hpp" namespace rfl { +// Converts an enum value to a string. template std::string enum_to_string(EnumType _enum) { return rfl::internal::enums::StringConverter::enum_to_string(_enum); } +// Converts a string to a value of the given enum type. template rfl::Result string_to_enum(const std::string& _str) { return rfl::internal::enums::StringConverter::string_to_enum(_str); } +// Returns a named tuple mapping names of enumerators of the given enum type to +// their values. +template +auto get_enumerators() { + constexpr auto names = internal::enums::get_enum_names< + EnumType, internal::enums::is_flag_enum>(); + return internal::enums::names_to_enumerator_named_tuple(names); +} + +// Returns an std::array containing pairs of enumerator names (as +// std::string_view) and values. +template +constexpr auto get_enumerator_array() { + constexpr auto names = internal::enums::get_enum_names< + EnumType, internal::enums::is_flag_enum>(); + return internal::enums::names_to_enumerator_array(names); +} + } // namespace rfl #endif // RFL_ENUMS_HPP_ diff --git a/include/rfl/internal/enums/Names.hpp b/include/rfl/internal/enums/Names.hpp index 79c10cbb..b836b971 100644 --- a/include/rfl/internal/enums/Names.hpp +++ b/include/rfl/internal/enums/Names.hpp @@ -6,9 +6,12 @@ #include #include #include +#include #include "../../Literal.hpp" #include "../../define_literal.hpp" +#include "../../make_named_tuple.hpp" +#include "../StringLiteral.hpp" namespace rfl { namespace internal { @@ -36,6 +39,19 @@ struct Names { _enums..., _new_enum>>; }; +template +auto names_to_enumerator_named_tuple( + Names, N, _enums...>) { + return make_named_tuple(Field<_names, EnumType>{_enums}...); +} + +template +constexpr std::array, N> +names_to_enumerator_array(Names, N, _enums...>) { + return { + std::make_pair(LiteralHelper<_names>::field_.string_view(), _enums)...}; +} + } // namespace enums } // namespace internal } // namespace rfl diff --git a/tests/json/test_enum.cpp b/tests/json/test_enum.cpp index d2273b7e..ac0a7156 100644 --- a/tests/json/test_enum.cpp +++ b/tests/json/test_enum.cpp @@ -26,7 +26,7 @@ void test() { write_and_read(circle, R"({"radius":2.0,"color":"green"})"); - auto mutable_circle = Circle{.radius = 2.0, .color = Color::green}; + auto mutable_circle = circle; if (auto color = rfl::string_to_enum("red"); color) { mutable_circle.color = *color; } @@ -42,6 +42,23 @@ void test() { mutable_circle.color = *color; } write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})"); + + rfl::get_enumerators().apply([&](auto field) { + if constexpr (decltype(field)::name() == "green") { + mutable_circle.color = field.value(); + } + }); + write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})"); + + constexpr auto enumerator_array = rfl::get_enumerator_array(); + static_assert(enumerator_array[0].first == "red"); + static_assert(enumerator_array[1].first == "green"); + static_assert(enumerator_array[2].first == "blue"); + static_assert(enumerator_array[3].first == "yellow"); + static_assert(enumerator_array[0].second == Color::red); + static_assert(enumerator_array[1].second == Color::green); + static_assert(enumerator_array[2].second == Color::blue); + static_assert(enumerator_array[3].second == Color::yellow); } } // namespace test_enum diff --git a/tests/json/test_flag_enum.cpp b/tests/json/test_flag_enum.cpp index 5f8e61a7..2275b558 100644 --- a/tests/json/test_flag_enum.cpp +++ b/tests/json/test_flag_enum.cpp @@ -35,6 +35,14 @@ void test() { Circle{.radius = 2.0, .color = Color::blue | Color::orange}; write_and_read(circle, R"({"radius":2.0,"color":"red|blue|yellow"})"); + + auto mutable_circle = circle; + rfl::get_enumerators().apply([&](auto field) { + if constexpr (decltype(field)::name() == "green") { + mutable_circle.color = field.value(); + } + }); + write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})"); } } // namespace test_flag_enum