From 51a79f9545ec8b49758c63bbdcb90d50fd301e18 Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 30 Apr 2020 19:46:46 +0500 Subject: [PATCH] remove duplicated code --- include/nameof.hpp | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index a7782f0..6cec454 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -403,6 +403,16 @@ constexpr bool cmp_less(L lhs, R rhs) noexcept { } } +template +constexpr std::size_t range_size() noexcept { + static_assert(is_enum_v, "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::max)(), "nameof::enum_range requires valid size."); + + return static_cast(size); +} + template constexpr int reflected_min() noexcept { static_assert(is_enum_v, "nameof::detail::reflected_min requires enum type."); @@ -429,17 +439,6 @@ inline constexpr int reflected_min_v = reflected_min(); template inline constexpr int reflected_max_v = reflected_max(); -template -constexpr std::size_t reflected_size() noexcept { - static_assert(is_enum_v, "nameof::detail::reflected_size requires enum type."); - static_assert(reflected_max_v > reflected_min_v, "nameof::enum_range requires max > min."); - constexpr auto size = reflected_max_v - reflected_min_v + 1; - static_assert(size > 0, "nameof::enum_range requires valid size."); - static_assert(size < (std::numeric_limits::max)(), "nameof::enum_range requires valid size."); - - return static_cast(size); -} - template constexpr auto values(std::integer_sequence) noexcept { static_assert(is_enum_v, "nameof::detail::values requires enum type."); @@ -457,7 +456,7 @@ constexpr auto values(std::integer_sequence) noexcept { } template -inline constexpr auto values_v = values(std::make_integer_sequence()>{}); +inline constexpr auto values_v = values(std::make_integer_sequence, reflected_max_v>()>{}); template inline constexpr std::size_t count_v = values_v.size(); @@ -469,17 +468,7 @@ template inline constexpr int max_v = values_v.empty() ? 0 : static_cast(values_v.back()); template -constexpr std::size_t range_size() noexcept { - static_assert(is_enum_v, "nameof::detail::range_size requires enum type."); - constexpr auto size = max_v - min_v + 1; - static_assert(size > 0, "nameof::enum_range requires valid size."); - static_assert(size < (std::numeric_limits::max)(), "nameof::enum_range requires valid size."); - - return static_cast(size); -} - -template -inline constexpr std::size_t range_size_v = range_size(); +inline constexpr std::size_t range_size_v = range_size, max_v>(); template using index_t = std::conditional_t < (std::numeric_limits::max)(), std::uint8_t, std::uint16_t>;