add get_enumerators_underlying and get_enumerator_array_underlying

This commit is contained in:
stu
2024-03-01 18:59:47 +00:00
parent c76e7a6471
commit cdca9ac73a
3 changed files with 52 additions and 0 deletions
+18
View File
@@ -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 <internal::enums::is_scoped_enum EnumType>
auto get_enumerators_underlying() {
constexpr auto names = internal::enums::get_enum_names<
EnumType, internal::enums::is_flag_enum<EnumType>>();
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 <internal::enums::is_scoped_enum EnumType>
@@ -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 <internal::enums::is_scoped_enum EnumType>
constexpr auto get_enumerator_array_underlying() {
constexpr auto names = internal::enums::get_enum_names<
EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_enumerator_array_underlying(names);
}
} // namespace rfl
#endif // RFL_ENUMS_HPP_
+17
View File
@@ -45,6 +45,13 @@ auto names_to_enumerator_named_tuple(
return make_named_tuple(Field<_names, EnumType>{_enums}...);
}
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
auto names_to_enumerator_named_tuple_underlying(
Names<EnumType, Literal<_names...>, N, _enums...>) {
return make_named_tuple(Field<_names, std::underlying_type_t<EnumType>>{
static_cast<std::underlying_type_t<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...>) {
@@ -52,6 +59,16 @@ names_to_enumerator_array(Names<EnumType, Literal<_names...>, N, _enums...>) {
std::make_pair(LiteralHelper<_names>::field_.string_view(), _enums)...};
}
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
constexpr std::array<
std::pair<std::string_view, std::underlying_type_t<EnumType>>, N>
names_to_enumerator_array_underlying(
Names<EnumType, Literal<_names...>, N, _enums...>) {
return {
std::make_pair(LiteralHelper<_names>::field_.string_view(),
static_cast<std::underlying_type_t<EnumType>>(_enums))...};
}
} // namespace enums
} // namespace internal
} // namespace rfl
+17
View File
@@ -50,6 +50,13 @@ void test() {
});
write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})");
rfl::get_enumerators_underlying<Color>().apply([&](auto field) {
if constexpr (decltype(field)::name() == "blue") {
mutable_circle.color = static_cast<Color>(field.value());
}
});
write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})");
constexpr auto enumerator_array = rfl::get_enumerator_array<Color>();
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<Color>();
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