Skip to content

Commit

Permalink
fix range check
Browse files Browse the repository at this point in the history
  • Loading branch information
Neargye committed Apr 22, 2021
1 parent d091ca0 commit a9813bd
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions include/nameof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,17 @@ constexpr bool is_valid() noexcept {
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>>
constexpr int reflected_min() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::reflected_min requires enum type.");
Expand All @@ -512,6 +523,7 @@ constexpr int reflected_min() noexcept {
if constexpr (cmp_less(lhs, rhs)) {
return rhs;
} 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;
}
}
Expand All @@ -529,6 +541,7 @@ constexpr int reflected_max() noexcept {
constexpr auto rhs = (std::numeric_limits<U>::max)();

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;
} else {
return rhs;
Expand All @@ -542,17 +555,6 @@ inline constexpr auto reflected_min_v = reflected_min<E, IsFlags>();
template <typename E, bool IsFlags = false>
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>
constexpr std::size_t values_count(const bool (&valid)[N]) noexcept {
auto count = std::size_t{0};
Expand Down Expand Up @@ -593,12 +595,6 @@ constexpr auto values() noexcept {
constexpr auto range_size = max - min + 1;
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.");
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>{});
}
Expand Down

0 comments on commit a9813bd

Please sign in to comment.