diff --git a/README.md b/README.md index 843c5ce9..5bf0c1f6 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,9 @@ This results in the following JSON string: {"pos_x":2.0,"pos_y":3.0,"shape":"square","color":"red|blue"} ``` +You can also directly convert between enumerator values and strings with `rfl::enum_to_string()` and `rfl::string_to_enum()`, or +obtain list of enumerator name and value pairs with `rfl::get_enumerators()` or `rfl::get_enumerator_array()`. + ## Algebraic data types reflect-cpp supports Pydantic-style tagged unions, which allow you to form algebraic data types: diff --git a/docs/enums.md b/docs/enums.md index 2c6ad6dd..8de33da0 100644 --- a/docs/enums.md +++ b/docs/enums.md @@ -146,3 +146,39 @@ This will be represented as follows: This works, because 16 + 256 + 512 + 1024 + 8192 = 10000. Flag enums are *always* represented in terms of 2^N-numbers. +## General-purpose enumeration utilities + +reflect-cpp also allows you to directly convert between enumerator values and strings: + +```cpp +enum class Color { red, green, blue }; + +auto name = rfl::enum_to_string(Color::red); // "red" +auto value = rfl::string_to_enum("red"); // result containing Color::red +auto value = rfl::string_to_enum("greem"); // error result +``` + +This works with normal and flag enums, and behaves just like serialization of types containing enumerations as described above. + +You can also inspect the defined enumerators of an enum type (including at compile-time): + +```cpp +enum class Color { red, green, blue }; + +// This produces a named tuple where the keys are "red", "green", and "blue", with corresponding +// values Color::red, Color::green, and Color::blue. +auto enumerators_named_tuple = rfl::get_enumerators(); + +// You can iterate over the enumerators like this: +enumerators_named_tuple.apply([&](const auto& field) { + // field.name() will be "red", "green", "blue" + // field.value() will be Color::red, Color::green, Color::blue +}); + +// This produces the same data as an std::array containing std::pair, +// which can be inspected at compile-time. +constexpr auto enumerator_array = rfl::get_enumerator_array(); +``` + +In case it's more convenient to get the enumerator values as values of the enum's underlying type rather than values of the enum type itself, +there is also `rfl::get_underlying_enumerators()` and `rfl::get_underlying_enumerator_array()`. diff --git a/include/rfl.hpp b/include/rfl.hpp index 33d5ab28..de895b87 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -33,6 +33,7 @@ #include "rfl/define_named_tuple.hpp" #include "rfl/define_tagged_union.hpp" #include "rfl/define_variant.hpp" +#include "rfl/enums.hpp" #include "rfl/extract_discriminators.hpp" #include "rfl/field_type.hpp" #include "rfl/fields.hpp" diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp new file mode 100644 index 00000000..3b1b4eb9 --- /dev/null +++ b/include/rfl/enums.hpp @@ -0,0 +1,64 @@ +#ifndef RFL_ENUMS_HPP_ +#define RFL_ENUMS_HPP_ + +#include + +#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 a named tuple mapping names of enumerators of the given enum type to +// their underlying values. +template +auto get_underlying_enumerators() { + constexpr auto names = internal::enums::get_enum_names< + EnumType, internal::enums::is_flag_enum>(); + return internal::enums::names_to_underlying_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); +} + +// Returns an std::array containing pairs of enumerator names (as +// std::string_view) and underlying values. +template +constexpr auto get_underlying_enumerator_array() { + constexpr auto names = internal::enums::get_enum_names< + EnumType, internal::enums::is_flag_enum>(); + return internal::enums::names_to_underlying_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..39bb9f13 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,36 @@ struct Names { _enums..., _new_enum>>; }; +template +auto names_to_enumerator_named_tuple( + Names, N, _enums...>) { + return make_named_tuple(Field<_names, EnumType>{_enums}...); +} + +template +auto names_to_underlying_enumerator_named_tuple( + 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...>) { + return { + std::make_pair(LiteralHelper<_names>::field_.string_view(), _enums)...}; +} + +template +constexpr std::array< + std::pair>, N> +names_to_underlying_enumerator_array( + 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 21010052..8484e34a 100644 --- a/tests/json/test_enum.cpp +++ b/tests/json/test_enum.cpp @@ -25,6 +25,58 @@ void test() { const auto circle = Circle{.radius = 2.0, .color = Color::green}; write_and_read(circle, R"({"radius":2.0,"color":"green"})"); + + auto mutable_circle = circle; + if (auto color = rfl::string_to_enum("red"); color) { + mutable_circle.color = *color; + } + write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})"); + + if (auto color = rfl::string_to_enum("bart"); color) { + mutable_circle.color = *color; + } + write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})"); + + if (auto color = rfl::string_to_enum(rfl::enum_to_string(Color::blue)); + color) { + 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"})"); + + rfl::get_underlying_enumerators().apply([&](const auto& field) { + if (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"); + 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); + + constexpr auto enumerator_array_underlying = + rfl::get_underlying_enumerator_array(); + 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 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