Use fold expressions to handle enums; #389 (#395)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-03-26 10:41:27 +08:00
committed by GitHub
parent 69ab371b03
commit e6299dc4e0
3 changed files with 38 additions and 53 deletions
-5
View File
@@ -40,11 +40,6 @@ jobs:
- name: Install ninja
run: brew install ninja
if: matrix.os == 'macos-latest'
- name: Make sure the library compiles with Conan
env:
CC: clang
CXX: clang++
run: conan build . --build=missing -s compiler.cppstd=gnu20 -o *:with_capnproto=True -o *:with_cbor=True -o *:with_flatbuffers=True -o *:with_msgpack=True -o *:with_toml=True -o *:with_ubjson=True -o *:with_xml=True -o *:with_yaml=True
- name: Compile
env:
CC: clang
+17 -11
View File
@@ -17,7 +17,8 @@ namespace rfl {
namespace internal {
namespace enums {
template <class EnumType, class LiteralType, size_t N, auto... _enums>
template <class EnumType, class LiteralType, size_t N, bool _is_flag,
auto... _enums>
struct Names {
/// Contains a collection of enums as compile-time strings.
using Literal = LiteralType;
@@ -34,36 +35,41 @@ struct Names {
template <class NewLiteral, auto _new_enum>
using AddOneType = std::conditional_t<
N == 0, Names<EnumType, NewLiteral, 1, _new_enum>,
N == 0, Names<EnumType, NewLiteral, 1, _is_flag, _new_enum>,
Names<EnumType, define_literal_t<LiteralType, NewLiteral>, N + 1,
_enums..., _new_enum>>;
_is_flag, _enums..., _new_enum>>;
};
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
template <class EnumType, size_t N, bool _is_flag, StringLiteral... _names,
auto... _enums>
auto names_to_enumerator_named_tuple(
Names<EnumType, Literal<_names...>, N, _enums...>) {
Names<EnumType, Literal<_names...>, N, _is_flag, _enums...>) {
return make_named_tuple(Field<_names, EnumType>{_enums}...);
}
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
template <class EnumType, size_t N, bool _is_flag, StringLiteral... _names,
auto... _enums>
auto names_to_underlying_enumerator_named_tuple(
Names<EnumType, Literal<_names...>, N, _enums...>) {
Names<EnumType, Literal<_names...>, N, _is_flag, _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>
template <class EnumType, size_t N, bool _is_flag, StringLiteral... _names,
auto... _enums>
constexpr std::array<std::pair<std::string_view, EnumType>, N>
names_to_enumerator_array(Names<EnumType, Literal<_names...>, N, _enums...>) {
names_to_enumerator_array(
Names<EnumType, Literal<_names...>, N, _is_flag, _enums...>) {
return {
std::make_pair(LiteralHelper<_names>::name_.string_view(), _enums)...};
}
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
template <class EnumType, size_t N, bool _is_flag, StringLiteral... _names,
auto... _enums>
constexpr std::array<
std::pair<std::string_view, std::underlying_type_t<EnumType>>, N>
names_to_underlying_enumerator_array(
Names<EnumType, Literal<_names...>, N, _enums...>) {
Names<EnumType, Literal<_names...>, N, _is_flag, _enums...>) {
return {
std::make_pair(LiteralHelper<_names>::name_.string_view(),
static_cast<std::underlying_type_t<EnumType>>(_enums))...};
+21 -37
View File
@@ -3,6 +3,7 @@
#include <limits>
#include <type_traits>
#include <utility>
#if __has_include(<source_location>)
#include <source_location>
@@ -38,9 +39,7 @@
// use a scoped integer, static_cast<MyEnum>(some_integer_value) will always be
// defined.
namespace rfl {
namespace internal {
namespace enums {
namespace rfl::internal::enums {
template <auto e>
consteval auto get_enum_name_str_view() {
@@ -79,19 +78,14 @@ consteval auto get_enum_name() {
return to_str_lit(std::make_index_sequence<name.size()>{});
}
template <class T>
consteval T calc_greatest_power_of_two() {
if constexpr (std::is_signed_v<T>) {
return static_cast<T>(1) << (sizeof(T) * 8 - 2);
} else {
return static_cast<T>(1) << (sizeof(T) * 8 - 1);
}
}
template <class T, bool _is_flag>
consteval T get_max() {
if constexpr (_is_flag) {
return calc_greatest_power_of_two<T>();
if constexpr (std::is_signed_v<T>) {
return (sizeof(T) * 8 - 2);
} else {
return (sizeof(T) * 8 - 1);
}
} else {
return std::numeric_limits<T>::max() > 127 ? static_cast<T>(127)
: std::numeric_limits<T>::max();
@@ -107,29 +101,19 @@ consteval T calc_j() {
}
}
template <class EnumType, class NamesType, auto _max, bool _is_flag, int _i>
consteval auto get_enum_names_impl() {
template <class EnumType, class LiteralType, size_t N, bool _is_flag, int _i,
auto... _enums>
consteval auto operator|(Names<EnumType, LiteralType, N, _is_flag, _enums...>,
std::integral_constant<int, _i>) {
using NamesType = Names<EnumType, LiteralType, N, _is_flag, _enums...>;
using T = std::underlying_type_t<EnumType>;
constexpr T j = calc_j<T, _is_flag, _i>();
constexpr auto name = get_enum_name<static_cast<EnumType>(j)>();
if constexpr (std::get<0>(name.arr_) == '(') {
if constexpr (j == _max) {
return NamesType{};
} else {
return get_enum_names_impl<EnumType, NamesType, _max, _is_flag, _i + 1>();
}
return NamesType{};
} else {
using NewNames = typename NamesType::template AddOneType<
Literal<remove_namespaces<name>()>, static_cast<EnumType>(j)>;
if constexpr (j == _max) {
return NewNames{};
} else {
return get_enum_names_impl<EnumType, NewNames, _max, _is_flag, _i + 1>();
}
return typename NamesType::template AddOneType<
Literal<remove_namespaces<name>()>, static_cast<EnumType>(j)>{};
}
}
@@ -142,15 +126,15 @@ consteval auto get_enum_names() {
static_assert(std::is_integral_v<std::underlying_type_t<EnumType>>,
"The underlying type of any Enum must be integral!");
using EmptyNames = Names<EnumType, rfl::Literal<"">, 0, _is_flag>;
constexpr auto max = get_max<std::underlying_type_t<EnumType>, _is_flag>();
using EmptyNames = Names<EnumType, rfl::Literal<"">, 0>;
return get_enum_names_impl<EnumType, EmptyNames, max, _is_flag, 0>();
return []<int... _is>(std::integer_sequence<int, _is...>) {
return (EmptyNames{} | ... | std::integral_constant<int, _is>());
}(std::make_integer_sequence<int, max>());
}
} // namespace enums
} // namespace internal
} // namespace rfl
} // namespace rfl::internal::enums
#endif