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