mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Fixed minor issues in the enum name extraction
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
#ifndef RFL_INTERNAL_ENUMS_LITERAL_CONVERTER_HPP_
|
||||
#define RFL_INTERNAL_ENUMS_LITERAL_CONVERTER_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/Literal.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
namespace enums {
|
||||
|
||||
template <class EnumType, class LiteralType, size_t N>
|
||||
struct Names {
|
||||
/// Contains a collection of enums as compile-time strings.
|
||||
using Names = LiteralType;
|
||||
|
||||
/// The number of possible values
|
||||
constexpr static size_t size = N;
|
||||
|
||||
/// A list of all the possible enums
|
||||
std::array<EnumType, N> enums_;
|
||||
|
||||
/// The integer values underlying the enum.
|
||||
std::array<std::underlying_type_t<EnumType>, N> integers_;
|
||||
|
||||
static_assert(LiteralType::size() == N,
|
||||
"Size of literal and enum do not match.");
|
||||
};
|
||||
|
||||
} // namespace enums
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -44,6 +44,7 @@ struct LiteralConverter {
|
||||
}
|
||||
|
||||
static EnumType literal_to_enum(const NamesLiteral _lit) {
|
||||
static_assert(names_.size != 0, "This does not work for empty enums.");
|
||||
return names_.enums_[_lit.value()];
|
||||
}
|
||||
|
||||
|
||||
@@ -24,9 +24,6 @@ struct Names {
|
||||
/// A list of all the possible enums
|
||||
std::array<EnumType, N> enums_;
|
||||
|
||||
/// The integer values underlying the enum.
|
||||
std::array<std::underlying_type_t<EnumType>, N> integers_;
|
||||
|
||||
static_assert(N == 0 || LiteralType::size() == N,
|
||||
"Size of literal and enum do not match.");
|
||||
};
|
||||
|
||||
@@ -68,9 +68,8 @@ consteval auto get_enum_name() {
|
||||
}
|
||||
|
||||
template <class EnumType>
|
||||
constexpr auto start_value = Names<EnumType, rfl::Literal<"">, 0>{
|
||||
.enums_ = std::array<EnumType, 0>{},
|
||||
.integers_ = std::array<std::underlying_type_t<EnumType>, 0>{}};
|
||||
constexpr auto start_value =
|
||||
Names<EnumType, rfl::Literal<"">, 0>{.enums_ = std::array<EnumType, 0>{}};
|
||||
|
||||
template <class EnumType, auto _names = start_value<EnumType>, int _i = 0>
|
||||
consteval auto get_enum_names() {
|
||||
@@ -98,12 +97,6 @@ consteval auto get_enum_names() {
|
||||
_names.enums_[Ns]..., static_cast<EnumType>(_i)};
|
||||
};
|
||||
|
||||
const auto update_integers = [&]<auto... Ns>(std::index_sequence<Ns...>) {
|
||||
return std::array<std::underlying_type_t<EnumType>, sizeof...(Ns) + 1>{
|
||||
_names.integers_[Ns]...,
|
||||
static_cast<std::underlying_type_t<EnumType>>(_i)};
|
||||
};
|
||||
|
||||
using NewNames = std::conditional_t<
|
||||
decltype(_names)::size == 0,
|
||||
Names<EnumType, Literal<remove_namespaces<name>()>, 1>,
|
||||
@@ -112,11 +105,9 @@ consteval auto get_enum_names() {
|
||||
Literal<remove_namespaces<name>()>>,
|
||||
decltype(_names)::size + 1>>;
|
||||
|
||||
constexpr auto new_names = NewNames{
|
||||
.enums_ =
|
||||
update_enums(std::make_index_sequence<decltype(_names)::size>{}),
|
||||
.integers_ = update_integers(
|
||||
std::make_index_sequence<decltype(_names)::size>{})};
|
||||
constexpr auto new_names =
|
||||
NewNames{.enums_ = update_enums(
|
||||
std::make_index_sequence<decltype(_names)::size>{})};
|
||||
|
||||
return get_enum_names<EnumType, new_names, _i + 1>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user