mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
Thanks to @bryceschober for pointing this out and drafting a fix.
This commit is contained in:
@@ -84,7 +84,9 @@ consteval auto get_range_min() {
|
|||||||
if constexpr (_is_flag) {
|
if constexpr (_is_flag) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return std::max(std::numeric_limits<U>::min(), range_min<T>::value);
|
return std::max(static_cast<decltype(range_min<T>::value)>(
|
||||||
|
std::numeric_limits<U>::min()),
|
||||||
|
range_min<T>::value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
template <class T, bool _is_flag>
|
template <class T, bool _is_flag>
|
||||||
@@ -97,7 +99,9 @@ consteval auto get_range_max() {
|
|||||||
return (sizeof(U) * 8 - 1);
|
return (sizeof(U) * 8 - 1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return std::min(std::numeric_limits<U>::max(), range_max<T>::value);
|
return std::min(static_cast<decltype(range_max<T>::value)>(
|
||||||
|
std::numeric_limits<U>::max()),
|
||||||
|
range_max<T>::value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +130,8 @@ consteval auto operator|(Names<EnumType, LiteralType, N, _is_flag, _enums...>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class EnumType, class LiteralType, size_t N, bool _is_flag, auto... _enums>
|
template <class EnumType, class LiteralType, size_t N, bool _is_flag,
|
||||||
|
auto... _enums>
|
||||||
struct NamesCombiner {
|
struct NamesCombiner {
|
||||||
template <int... Is>
|
template <int... Is>
|
||||||
static consteval auto combine(std::integer_sequence<int, Is...>) {
|
static consteval auto combine(std::integer_sequence<int, Is...>) {
|
||||||
@@ -145,8 +150,10 @@ struct CombineNames {
|
|||||||
}(std::make_integer_sequence<int, End - Start + 1>{});
|
}(std::make_integer_sequence<int, End - Start + 1>{});
|
||||||
} else {
|
} else {
|
||||||
constexpr int Mid = Start + (End - Start) / 2;
|
constexpr int Mid = Start + (End - Start) / 2;
|
||||||
constexpr auto left = CombineNames<ChunkSize>::template apply<NamesType, Start, Mid>();
|
constexpr auto left =
|
||||||
constexpr auto right = CombineNames<ChunkSize>::template apply<NamesType, Mid + 1, End>();
|
CombineNames<ChunkSize>::template apply<NamesType, Start, Mid>();
|
||||||
|
constexpr auto right =
|
||||||
|
CombineNames<ChunkSize>::template apply<NamesType, Mid + 1, End>();
|
||||||
return left | right;
|
return left | right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,16 +162,18 @@ struct CombineNames {
|
|||||||
template <class EnumType, bool _is_flag>
|
template <class EnumType, bool _is_flag>
|
||||||
consteval auto get_enum_names() {
|
consteval auto get_enum_names() {
|
||||||
static_assert(is_scoped_enum<EnumType>,
|
static_assert(is_scoped_enum<EnumType>,
|
||||||
"You must use scoped enums (using class or struct) for the parsing to work!");
|
"You must use scoped enums (using class or struct) for the "
|
||||||
|
"parsing to work!");
|
||||||
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!");
|
||||||
|
|
||||||
constexpr auto max = get_range_max<EnumType, _is_flag>();
|
constexpr auto max = get_range_max<EnumType, _is_flag>();
|
||||||
constexpr auto min = get_range_min<EnumType, _is_flag>();
|
constexpr auto min = get_range_min<EnumType, _is_flag>();
|
||||||
constexpr auto range_size = max - min + 1;
|
constexpr auto range_size = max - min + 1;
|
||||||
|
|
||||||
static_assert(range_size > 0,
|
static_assert(range_size > 0,
|
||||||
"enum_range requires a valid range size. Ensure that max is greater than min.");
|
"enum_range requires a valid range size. Ensure that max is "
|
||||||
|
"greater than min.");
|
||||||
|
|
||||||
using EmptyNames = Names<EnumType, rfl::Literal<"">, 0, _is_flag>;
|
using EmptyNames = Names<EnumType, rfl::Literal<"">, 0, _is_flag>;
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
namespace test_enum_range_min_max {
|
namespace test_enum_range_min_max {
|
||||||
|
|
||||||
enum class InnerColor { none = -128, red = -50, green = 0, blue = 128 };
|
enum class InnerColor { none = -128, red = -50, green = 0, blue = 128 };
|
||||||
enum class LineColor { yellow = 200, purple = 300, orange = 400 };
|
enum class LineColor : uint16_t { yellow = 200, purple = 300, orange = 400 };
|
||||||
|
|
||||||
} // namespace test_enum_range_min_max
|
} // namespace test_enum_range_min_max
|
||||||
|
|
||||||
@@ -30,4 +30,4 @@ TEST(generic, test_enum_range_min_max) {
|
|||||||
EXPECT_EQ(line_max, 400);
|
EXPECT_EQ(line_max, 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace test_enum_range_min_max
|
} // namespace test_enum_range_min_max
|
||||||
|
|||||||
Reference in New Issue
Block a user