From a1fb54f4ac7c269ce6456a716cb5f3ffd3b13753 Mon Sep 17 00:00:00 2001 From: stu Date: Thu, 22 Feb 2024 13:45:26 +0000 Subject: [PATCH 1/7] add rfl::enum_to_string and rfl::string_to_enum --- include/rfl.hpp | 1 + include/rfl/enums.hpp | 24 ++++++++++++++++++++++++ tests/json/test_enum.cpp | 17 +++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 include/rfl/enums.hpp 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..d4bf6ba9 --- /dev/null +++ b/include/rfl/enums.hpp @@ -0,0 +1,24 @@ +#ifndef RFL_ENUMS_HPP_ +#define RFL_ENUMS_HPP_ + +#include + +#include "Result.hpp" +#include "internal/enums/StringConverter.hpp" +#include "internal/enums/is_scoped_enum.hpp" + +namespace rfl { + +template +std::string enum_to_string(EnumType _enum) { + return rfl::internal::enums::StringConverter::enum_to_string(_enum); +} + +template +rfl::Result string_to_enum(const std::string& _str) { + return rfl::internal::enums::StringConverter::string_to_enum(_str); +} + +} // namespace rfl + +#endif // RFL_ENUMS_HPP_ diff --git a/tests/json/test_enum.cpp b/tests/json/test_enum.cpp index 21010052..d2273b7e 100644 --- a/tests/json/test_enum.cpp +++ b/tests/json/test_enum.cpp @@ -25,6 +25,23 @@ 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{.radius = 2.0, .color = Color::green}; + 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"})"); } } // namespace test_enum From c76e7a647155aedfb26e68bf9ba9402e25911b98 Mon Sep 17 00:00:00 2001 From: stu Date: Thu, 22 Feb 2024 14:30:04 +0000 Subject: [PATCH 2/7] add rfl::get_enumerators() and rfl::get_enumerator_array() --- include/rfl/enums.hpp | 22 ++++++++++++++++++++++ include/rfl/internal/enums/Names.hpp | 16 ++++++++++++++++ tests/json/test_enum.cpp | 19 ++++++++++++++++++- tests/json/test_flag_enum.cpp | 8 ++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) 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 From cdca9ac73ac3185e023db937c197b583e9c8eacd Mon Sep 17 00:00:00 2001 From: stu Date: Fri, 1 Mar 2024 18:58:42 +0000 Subject: [PATCH 3/7] add get_enumerators_underlying and get_enumerator_array_underlying --- include/rfl/enums.hpp | 18 ++++++++++++++++++ include/rfl/internal/enums/Names.hpp | 17 +++++++++++++++++ tests/json/test_enum.cpp | 17 +++++++++++++++++ 3 files changed, 52 insertions(+) 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 From 617226cd1debc958a0514a33d8c07d24ec5032d6 Mon Sep 17 00:00:00 2001 From: stu Date: Fri, 1 Mar 2024 19:02:51 +0000 Subject: [PATCH 4/7] rename to get_underlying_enumerators --- include/rfl/enums.hpp | 8 ++++---- include/rfl/internal/enums/Names.hpp | 4 ++-- tests/json/test_enum.cpp | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp index b5ccec9b..3b1b4eb9 100644 --- a/include/rfl/enums.hpp +++ b/include/rfl/enums.hpp @@ -35,10 +35,10 @@ auto get_enumerators() { // Returns a named tuple mapping names of enumerators of the given enum type to // their underlying values. template -auto get_enumerators_underlying() { +auto get_underlying_enumerators() { constexpr auto names = internal::enums::get_enum_names< EnumType, internal::enums::is_flag_enum>(); - return internal::enums::names_to_enumerator_named_tuple_underlying(names); + return internal::enums::names_to_underlying_enumerator_named_tuple(names); } // Returns an std::array containing pairs of enumerator names (as @@ -53,10 +53,10 @@ constexpr auto get_enumerator_array() { // 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 get_underlying_enumerator_array() { constexpr auto names = internal::enums::get_enum_names< EnumType, internal::enums::is_flag_enum>(); - return internal::enums::names_to_enumerator_array_underlying(names); + return internal::enums::names_to_underlying_enumerator_array(names); } } // namespace rfl diff --git a/include/rfl/internal/enums/Names.hpp b/include/rfl/internal/enums/Names.hpp index 46b1b5b2..39bb9f13 100644 --- a/include/rfl/internal/enums/Names.hpp +++ b/include/rfl/internal/enums/Names.hpp @@ -46,7 +46,7 @@ auto names_to_enumerator_named_tuple( } template -auto names_to_enumerator_named_tuple_underlying( +auto names_to_underlying_enumerator_named_tuple( Names, N, _enums...>) { return make_named_tuple(Field<_names, std::underlying_type_t>{ static_cast>(_enums)}...); @@ -62,7 +62,7 @@ names_to_enumerator_array(Names, N, _enums...>) { template constexpr std::array< std::pair>, N> -names_to_enumerator_array_underlying( +names_to_underlying_enumerator_array( Names, N, _enums...>) { return { std::make_pair(LiteralHelper<_names>::field_.string_view(), diff --git a/tests/json/test_enum.cpp b/tests/json/test_enum.cpp index 7d7a542f..752555f5 100644 --- a/tests/json/test_enum.cpp +++ b/tests/json/test_enum.cpp @@ -50,7 +50,7 @@ void test() { }); write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})"); - rfl::get_enumerators_underlying().apply([&](auto field) { + rfl::get_underlying_enumerators().apply([&](auto field) { if constexpr (decltype(field)::name() == "blue") { mutable_circle.color = static_cast(field.value()); } @@ -67,7 +67,7 @@ void test() { 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(); + 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"); From ec437c51f2ec0f5d092d0eb4d6b372c95e4bef13 Mon Sep 17 00:00:00 2001 From: stu Date: Fri, 1 Mar 2024 19:19:34 +0000 Subject: [PATCH 5/7] add documentation for new enumerator functionality --- docs/enums.md | 36 ++++++++++++++++++++++++++++++++++++ tests/json/test_enum.cpp | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/enums.md b/docs/enums.md index 2c6ad6dd..b13a37cf 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 enumeration 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/tests/json/test_enum.cpp b/tests/json/test_enum.cpp index 752555f5..a968b1ec 100644 --- a/tests/json/test_enum.cpp +++ b/tests/json/test_enum.cpp @@ -50,8 +50,8 @@ void test() { }); write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})"); - rfl::get_underlying_enumerators().apply([&](auto field) { - if constexpr (decltype(field)::name() == "blue") { + rfl::get_underlying_enumerators().apply([&](const auto& field) { + if (field.name() == "blue") { mutable_circle.color = static_cast(field.value()); } }); From 1358223955d9c2b2b6b64f2199b7ebdcae362181 Mon Sep 17 00:00:00 2001 From: stu Date: Fri, 1 Mar 2024 19:23:32 +0000 Subject: [PATCH 6/7] add small section to README --- README.md | 3 +++ docs/enums.md | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) 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 b13a37cf..8de33da0 100644 --- a/docs/enums.md +++ b/docs/enums.md @@ -148,7 +148,7 @@ This works, because 16 + 256 + 512 + 1024 + 8192 = 10000. Flag enums are *always ## General-purpose enumeration utilities -reflect-cpp also allows you to directly convert between enumeration values and strings: +reflect-cpp also allows you to directly convert between enumerator values and strings: ```cpp enum class Color { red, green, blue }; From 0b2f5d2a135254716a9d9cc40cc69297b17ffc01 Mon Sep 17 00:00:00 2001 From: stu Date: Fri, 1 Mar 2024 19:25:46 +0000 Subject: [PATCH 7/7] clang-format --- tests/json/test_enum.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/json/test_enum.cpp b/tests/json/test_enum.cpp index a968b1ec..8484e34a 100644 --- a/tests/json/test_enum.cpp +++ b/tests/json/test_enum.cpp @@ -67,7 +67,8 @@ void test() { 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(); + 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");