add rfl::get_enumerators() and rfl::get_enumerator_array()

This commit is contained in:
stu
2024-02-22 15:24:20 +00:00
parent a1fb54f4ac
commit c76e7a6471
4 changed files with 64 additions and 1 deletions
+22
View File
@@ -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 <internal::enums::is_scoped_enum EnumType>
std::string enum_to_string(EnumType _enum) {
return rfl::internal::enums::StringConverter<EnumType>::enum_to_string(_enum);
}
// Converts a string to a value of the given enum type.
template <internal::enums::is_scoped_enum EnumType>
rfl::Result<EnumType> string_to_enum(const std::string& _str) {
return rfl::internal::enums::StringConverter<EnumType>::string_to_enum(_str);
}
// Returns a named tuple mapping names of enumerators of the given enum type to
// their values.
template <internal::enums::is_scoped_enum EnumType>
auto get_enumerators() {
constexpr auto names = internal::enums::get_enum_names<
EnumType, internal::enums::is_flag_enum<EnumType>>();
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 <internal::enums::is_scoped_enum EnumType>
constexpr auto get_enumerator_array() {
constexpr auto names = internal::enums::get_enum_names<
EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_enumerator_array(names);
}
} // namespace rfl
#endif // RFL_ENUMS_HPP_
+16
View File
@@ -6,9 +6,12 @@
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#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 <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
auto names_to_enumerator_named_tuple(
Names<EnumType, Literal<_names...>, N, _enums...>) {
return make_named_tuple(Field<_names, EnumType>{_enums}...);
}
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
constexpr std::array<std::pair<std::string_view, EnumType>, N>
names_to_enumerator_array(Names<EnumType, Literal<_names...>, N, _enums...>) {
return {
std::make_pair(LiteralHelper<_names>::field_.string_view(), _enums)...};
}
} // namespace enums
} // namespace internal
} // namespace rfl
+18 -1
View File
@@ -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<Color>("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<Color>().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<Color>();
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
+8
View File
@@ -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<Color>().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