fix range check

This commit is contained in:
neargye 2021-04-22 12:41:25 +03:00
parent d091ca081e
commit a9813bd7a1

View file

@ -498,6 +498,17 @@ constexpr bool is_valid() noexcept {
return n<E, static_cast<E>(V)>().size() != 0; return n<E, static_cast<E>(V)>().size() != 0;
} }
template <typename E, int O, bool IsFlags = false, typename U = std::underlying_type_t<E>>
constexpr E value(std::size_t i) noexcept {
static_assert(is_enum_v<E>, "nameof::detail::value requires enum type.");
if constexpr (IsFlags) {
return static_cast<E>(U{1} << static_cast<U>(static_cast<int>(i) + O));
} else {
return static_cast<E>(static_cast<int>(i) + O);
}
}
template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>> template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
constexpr int reflected_min() noexcept { constexpr int reflected_min() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::reflected_min requires enum type."); static_assert(is_enum_v<E>, "nameof::detail::reflected_min requires enum type.");
@ -512,6 +523,7 @@ constexpr int reflected_min() noexcept {
if constexpr (cmp_less(lhs, rhs)) { if constexpr (cmp_less(lhs, rhs)) {
return rhs; return rhs;
} else { } else {
static_assert(!is_valid<E, value<E, lhs - 1, IsFlags>(0)>(), "nameof::enum_range detects enum value smaller than min range size.");
return lhs; return lhs;
} }
} }
@ -529,6 +541,7 @@ constexpr int reflected_max() noexcept {
constexpr auto rhs = (std::numeric_limits<U>::max)(); constexpr auto rhs = (std::numeric_limits<U>::max)();
if constexpr (cmp_less(lhs, rhs)) { if constexpr (cmp_less(lhs, rhs)) {
static_assert(!is_valid<E, value<E, lhs + 1, IsFlags>(0)>(), "nameof::enum_range detects enum value larger than max range size.");
return lhs; return lhs;
} else { } else {
return rhs; return rhs;
@ -542,17 +555,6 @@ inline constexpr auto reflected_min_v = reflected_min<E, IsFlags>();
template <typename E, bool IsFlags = false> template <typename E, bool IsFlags = false>
inline constexpr auto reflected_max_v = reflected_max<E, IsFlags>(); inline constexpr auto reflected_max_v = reflected_max<E, IsFlags>();
template <typename E, int O, bool IsFlags = false, typename U = std::underlying_type_t<E>>
constexpr E value(std::size_t i) noexcept {
static_assert(is_enum_v<E>, "nameof::detail::value requires enum type.");
if constexpr (IsFlags) {
return static_cast<E>(U{1} << static_cast<U>(static_cast<int>(i) + O));
} else {
return static_cast<E>(static_cast<int>(i) + O);
}
}
template <std::size_t N> template <std::size_t N>
constexpr std::size_t values_count(const bool (&valid)[N]) noexcept { constexpr std::size_t values_count(const bool (&valid)[N]) noexcept {
auto count = std::size_t{0}; auto count = std::size_t{0};
@ -593,12 +595,6 @@ constexpr auto values() noexcept {
constexpr auto range_size = max - min + 1; constexpr auto range_size = max - min + 1;
static_assert(range_size > 0, "nameof::enum_range requires valid size."); static_assert(range_size > 0, "nameof::enum_range requires valid size.");
static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size."); static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size.");
if constexpr (cmp_less((std::numeric_limits<U>::min)(), min) && !IsFlags) {
static_assert(!is_valid<E, value<E, min - 1, IsFlags>(0)>(), "nameof::enum_range detects enum value smaller than min range size.");
}
if constexpr (cmp_less(range_size, (std::numeric_limits<U>::max)()) && !IsFlags) {
static_assert(!is_valid<E, value<E, min, IsFlags>(range_size + 1)>(), "nameof::enum_range detects enum value larger than max range size.");
}
return values<E, IsFlags, reflected_min_v<E, IsFlags>>(std::make_index_sequence<range_size>{}); return values<E, IsFlags, reflected_min_v<E, IsFlags>>(std::make_index_sequence<range_size>{});
} }