From 64499c3763aa1bb87112936c7e0e43510151a7c8 Mon Sep 17 00:00:00 2001 From: neargye Date: Sat, 19 Oct 2019 16:23:36 +0500 Subject: [PATCH] improve nameof_enum --- include/nameof.hpp | 51 ++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 113bf37..c31779b 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -6,6 +6,7 @@ // |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____| // https://github.com/Neargye/nameof // vesion 0.9.1 +// vesion 0.9.2 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT @@ -33,6 +34,7 @@ #define NEARGYE_NAMEOF_HPP #include +#include #include #include #include @@ -380,35 +382,39 @@ constexpr bool mixed_sign_less(L lhs, R rhs) noexcept { } } -template -constexpr int mixed_sign_min_as_int(L lhs, R rhs) noexcept { - static_assert(std::is_integral_v && std::is_integral_v, "nameof::detail::mixed_sign_min_as_int requires integral type."); +template +constexpr int reflected_min() noexcept { + static_assert(is_enum_v, "nameof::detail::reflected_min requires enum type."); + constexpr auto lhs = enum_range::min; + static_assert(lhs > (std::numeric_limits::min)(), "nameof::enum_range requires min must be greater than INT16_MIN."); + constexpr auto rhs = (std::numeric_limits>::min)(); - return mixed_sign_less(lhs, rhs) ? static_cast(lhs) : static_cast(rhs); -} - -template -constexpr int mixed_sign_max_as_int(L lhs, R rhs) noexcept { - static_assert(std::is_integral_v && std::is_integral_v, "nameof::detail::mixed_sign_max_as_int requires integral type."); - - return mixed_sign_less(lhs, rhs) ? static_cast(rhs) : static_cast(lhs); + return mixed_sign_less(lhs, rhs) ? rhs : lhs; } template -inline constexpr int reflected_min_v = mixed_sign_max_as_int(enum_range::min, (std::numeric_limits>::min)()); +constexpr int reflected_max() noexcept { + static_assert(is_enum_v, "nameof::detail::reflected_max requires enum type."); + constexpr auto lhs = enum_range::max; + static_assert(lhs < (std::numeric_limits::max)(), "nameof::enum_range requires max must be less than INT16_MAX."); + constexpr auto rhs = (std::numeric_limits>::max)(); + + return mixed_sign_less(lhs, rhs) ? lhs : rhs; +} template -inline constexpr int reflected_max_v = mixed_sign_min_as_int(enum_range::max, (std::numeric_limits>::max)()); +inline constexpr int reflected_min_v = reflected_min(); template -constexpr std::size_t reflected_size() { +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_min_v > (std::numeric_limits::min)(), "nameof::enum_range requires min must be greater than INT16_MIN."); - static_assert(reflected_max_v < (std::numeric_limits::max)(), "nameof::enum_range requires max must be less than INT16_MAX."); 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."); + static_assert(size < (std::numeric_limits::max)(), "nameof::enum_range requires valid size."); return static_cast(size); } @@ -417,12 +423,12 @@ template constexpr auto values(std::integer_sequence) noexcept { static_assert(is_enum_v, "nameof::detail::values requires enum type."); constexpr std::array valid{{(n(I + reflected_min_v)>().size() != 0)...}}; - constexpr std::size_t count = ((valid[I] ? 1 : 0) + ...); + constexpr int count = ((valid[I] ? 1 : 0) + ...); std::array values{}; - for (std::size_t i = 0, v = 0; v < count; ++i) { + for (int i = 0, v = 0; v < count; ++i) { if (valid[i]) { - values[v++] = static_cast(static_cast(i) + reflected_min_v); + values[v++] = static_cast(i + reflected_min_v); } } @@ -446,7 +452,7 @@ 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."); + static_assert(size < (std::numeric_limits::max)(), "nameof::enum_range requires valid size."); return static_cast(size); } @@ -499,9 +505,6 @@ constexpr auto strings() noexcept { template class enum_traits { static_assert(is_enum_v, "nameof::enum_traits requires enum type."); - static_assert(enum_range::min > (std::numeric_limits::min)(), "nameof::enum_range requires min must be greater than INT16_MIN."); - static_assert(enum_range::max < (std::numeric_limits::max)(), "nameof::enum_range requires max must be less than INT16_MAX."); - static_assert(enum_range::max > enum_range::min, "nameof::enum_range requires max > min."); static_assert(count_v > 0, "nameof::enum_range requires enum implementation or valid max and min."); using U = std::underlying_type_t; inline static constexpr auto strings_ = strings();