remove duplicated code

This commit is contained in:
neargye 2020-04-30 19:46:46 +05:00
parent dc127d3d4d
commit 51a79f9545

View file

@ -403,6 +403,16 @@ constexpr bool cmp_less(L lhs, R rhs) noexcept {
} }
} }
template <typename E, int Min, int Max>
constexpr std::size_t range_size() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::range_size requires enum type.");
constexpr auto size = Max - Min + 1;
static_assert(size > 0, "nameof::enum_range requires valid size.");
static_assert(size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size.");
return static_cast<std::size_t>(size);
}
template <typename E> template <typename 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.");
@ -429,17 +439,6 @@ inline constexpr int reflected_min_v = reflected_min<E>();
template <typename E> template <typename E>
inline constexpr int reflected_max_v = reflected_max<E>(); inline constexpr int reflected_max_v = reflected_max<E>();
template <typename E>
constexpr std::size_t reflected_size() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::reflected_size requires enum type.");
static_assert(reflected_max_v<E> > reflected_min_v<E>, "nameof::enum_range requires max > min.");
constexpr auto size = reflected_max_v<E> - reflected_min_v<E> + 1;
static_assert(size > 0, "nameof::enum_range requires valid size.");
static_assert(size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size.");
return static_cast<std::size_t>(size);
}
template <typename E, int... I> template <typename E, int... I>
constexpr auto values(std::integer_sequence<int, I...>) noexcept { constexpr auto values(std::integer_sequence<int, I...>) noexcept {
static_assert(is_enum_v<E>, "nameof::detail::values requires enum type."); static_assert(is_enum_v<E>, "nameof::detail::values requires enum type.");
@ -457,7 +456,7 @@ constexpr auto values(std::integer_sequence<int, I...>) noexcept {
} }
template <typename E> template <typename E>
inline constexpr auto values_v = values<E>(std::make_integer_sequence<int, reflected_size<E>()>{}); inline constexpr auto values_v = values<E>(std::make_integer_sequence<int, range_size<E, reflected_min_v<E>, reflected_max_v<E>>()>{});
template <typename E> template <typename E>
inline constexpr std::size_t count_v = values_v<E>.size(); inline constexpr std::size_t count_v = values_v<E>.size();
@ -469,17 +468,7 @@ template <typename E>
inline constexpr int max_v = values_v<E>.empty() ? 0 : static_cast<int>(values_v<E>.back()); inline constexpr int max_v = values_v<E>.empty() ? 0 : static_cast<int>(values_v<E>.back());
template <typename E> template <typename E>
constexpr std::size_t range_size() noexcept { inline constexpr std::size_t range_size_v = range_size<E, min_v<E>, max_v<E>>();
static_assert(is_enum_v<E>, "nameof::detail::range_size requires enum type.");
constexpr auto size = max_v<E> - min_v<E> + 1;
static_assert(size > 0, "nameof::enum_range requires valid size.");
static_assert(size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size.");
return static_cast<std::size_t>(size);
}
template <typename E>
inline constexpr std::size_t range_size_v = range_size<E>();
template <typename E> template <typename E>
using index_t = std::conditional_t<range_size_v<E> < (std::numeric_limits<std::uint8_t>::max)(), std::uint8_t, std::uint16_t>; using index_t = std::conditional_t<range_size_v<E> < (std::numeric_limits<std::uint8_t>::max)(), std::uint8_t, std::uint16_t>;