mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
@@ -40,11 +40,6 @@ jobs:
|
|||||||
- name: Install ninja
|
- name: Install ninja
|
||||||
run: brew install ninja
|
run: brew install ninja
|
||||||
if: matrix.os == 'macos-latest'
|
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
|
- name: Compile
|
||||||
env:
|
env:
|
||||||
CC: clang
|
CC: clang
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ namespace rfl {
|
|||||||
namespace internal {
|
namespace internal {
|
||||||
namespace enums {
|
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 {
|
struct Names {
|
||||||
/// Contains a collection of enums as compile-time strings.
|
/// Contains a collection of enums as compile-time strings.
|
||||||
using Literal = LiteralType;
|
using Literal = LiteralType;
|
||||||
@@ -34,36 +35,41 @@ struct Names {
|
|||||||
|
|
||||||
template <class NewLiteral, auto _new_enum>
|
template <class NewLiteral, auto _new_enum>
|
||||||
using AddOneType = std::conditional_t<
|
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,
|
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(
|
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}...);
|
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(
|
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>>{
|
return make_named_tuple(Field<_names, std::underlying_type_t<EnumType>>{
|
||||||
static_cast<std::underlying_type_t<EnumType>>(_enums)}...);
|
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>
|
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 {
|
return {
|
||||||
std::make_pair(LiteralHelper<_names>::name_.string_view(), _enums)...};
|
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<
|
constexpr std::array<
|
||||||
std::pair<std::string_view, std::underlying_type_t<EnumType>>, N>
|
std::pair<std::string_view, std::underlying_type_t<EnumType>>, N>
|
||||||
names_to_underlying_enumerator_array(
|
names_to_underlying_enumerator_array(
|
||||||
Names<EnumType, Literal<_names...>, N, _enums...>) {
|
Names<EnumType, Literal<_names...>, N, _is_flag, _enums...>) {
|
||||||
return {
|
return {
|
||||||
std::make_pair(LiteralHelper<_names>::name_.string_view(),
|
std::make_pair(LiteralHelper<_names>::name_.string_view(),
|
||||||
static_cast<std::underlying_type_t<EnumType>>(_enums))...};
|
static_cast<std::underlying_type_t<EnumType>>(_enums))...};
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#if __has_include(<source_location>)
|
#if __has_include(<source_location>)
|
||||||
#include <source_location>
|
#include <source_location>
|
||||||
@@ -38,9 +39,7 @@
|
|||||||
// use a scoped integer, static_cast<MyEnum>(some_integer_value) will always be
|
// use a scoped integer, static_cast<MyEnum>(some_integer_value) will always be
|
||||||
// defined.
|
// defined.
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::internal::enums {
|
||||||
namespace internal {
|
|
||||||
namespace enums {
|
|
||||||
|
|
||||||
template <auto e>
|
template <auto e>
|
||||||
consteval auto get_enum_name_str_view() {
|
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()>{});
|
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>
|
template <class T, bool _is_flag>
|
||||||
consteval T get_max() {
|
consteval T get_max() {
|
||||||
if constexpr (_is_flag) {
|
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 {
|
} else {
|
||||||
return std::numeric_limits<T>::max() > 127 ? static_cast<T>(127)
|
return std::numeric_limits<T>::max() > 127 ? static_cast<T>(127)
|
||||||
: std::numeric_limits<T>::max();
|
: 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>
|
template <class EnumType, class LiteralType, size_t N, bool _is_flag, int _i,
|
||||||
consteval auto get_enum_names_impl() {
|
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>;
|
using T = std::underlying_type_t<EnumType>;
|
||||||
|
|
||||||
constexpr T j = calc_j<T, _is_flag, _i>();
|
constexpr T j = calc_j<T, _is_flag, _i>();
|
||||||
|
|
||||||
constexpr auto name = get_enum_name<static_cast<EnumType>(j)>();
|
constexpr auto name = get_enum_name<static_cast<EnumType>(j)>();
|
||||||
|
|
||||||
if constexpr (std::get<0>(name.arr_) == '(') {
|
if constexpr (std::get<0>(name.arr_) == '(') {
|
||||||
if constexpr (j == _max) {
|
return NamesType{};
|
||||||
return NamesType{};
|
|
||||||
} else {
|
|
||||||
return get_enum_names_impl<EnumType, NamesType, _max, _is_flag, _i + 1>();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
using NewNames = typename NamesType::template AddOneType<
|
return typename NamesType::template AddOneType<
|
||||||
Literal<remove_namespaces<name>()>, static_cast<EnumType>(j)>;
|
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>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,15 +126,15 @@ consteval auto get_enum_names() {
|
|||||||
static_assert(std::is_integral_v<std::underlying_type_t<EnumType>>,
|
static_assert(std::is_integral_v<std::underlying_type_t<EnumType>>,
|
||||||
"The underlying type of any Enum must be integral!");
|
"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>();
|
constexpr auto max = get_max<std::underlying_type_t<EnumType>, _is_flag>();
|
||||||
|
|
||||||
using EmptyNames = Names<EnumType, rfl::Literal<"">, 0>;
|
return []<int... _is>(std::integer_sequence<int, _is...>) {
|
||||||
|
return (EmptyNames{} | ... | std::integral_constant<int, _is>());
|
||||||
return get_enum_names_impl<EnumType, EmptyNames, max, _is_flag, 0>();
|
}(std::make_integer_sequence<int, max>());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace enums
|
} // namespace rfl::internal::enums
|
||||||
} // namespace internal
|
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user