From aa45d44f05bc59d4a19d7be55fc15029e040f289 Mon Sep 17 00:00:00 2001 From: neargye Date: Wed, 2 Oct 2019 15:23:54 +0500 Subject: [PATCH] add NAMEOF_ENUM_SUPPORTED and NAMEOF_TYPE_SUPPORTED --- example/example.cpp | 2 +- include/nameof.hpp | 54 ++++++++++++++++++++++++++------------------- test/test.cpp | 4 ++-- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 8433ccd..101611b 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -88,7 +88,7 @@ int main() { constexpr auto name = NAMEOF(structvar); static_assert("structvar" == name); -#if defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) +#if defined(NAMEOF_ENUM_SUPPORTED) // Nameof enum variable. auto color = Color::RED; std::cout << nameof::nameof_enum(color) << std::endl; // 'RED' diff --git a/include/nameof.hpp b/include/nameof.hpp index a59bfa0..fc72a3e 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -5,7 +5,7 @@ // | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_| // |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____| // https://github.com/Neargye/nameof -// vesion 0.9.0 +// vesion 0.9.1 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT @@ -40,6 +40,16 @@ #include #include +// Checks nameof_type compiler compatibility. +#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) +# define NAMEOF_TYPE_SUPPORTED 1 +#endif + +// Checks nameof_enum compiler compatibility. +#if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 || defined(_MSC_VER) +# define NAMEOF_ENUM_SUPPORTED 1 +#endif + // Enum value must be greater or equals than NAMEOF_ENUM_RANGE_MIN. By default NAMEOF_ENUM_RANGE_MIN = -128. // If need another min range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN. #if !defined(NAMEOF_ENUM_RANGE_MIN) @@ -82,7 +92,7 @@ struct identity final { template struct nameof_type_supported final -#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT) +#if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED : std::true_type {}; #else : std::false_type {}; @@ -90,7 +100,7 @@ struct nameof_type_supported final template struct nameof_enum_supported final -#if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 || defined(_MSC_VER) || defined(NAMEOF_ENUM_NO_CHECK_SUPPORT) +#if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED : std::true_type {}; #else : std::false_type {}; @@ -320,18 +330,17 @@ constexpr std::string_view pretty_name(std::string_view name, bool remove_templa template constexpr auto n() noexcept { static_assert(is_enum_v, "nameof::detail::n requires enum type."); -#if defined(__clang__) || defined(__GNUC__) +#if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED +# if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); -#elif defined(_MSC_VER) +# elif defined(_MSC_VER) constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); +# endif + return static_string{name}; +#else + static_assert(nameof_enum_supported::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); + return std::string_view{}; // Unsupported compiler. #endif - - if constexpr (nameof_enum_supported::value) { - return static_string{name}; - } else { - static_assert(nameof_enum_supported::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); - return std::string_view{}; // Unsupported compiler. - } } template @@ -465,7 +474,7 @@ constexpr auto strings() noexcept { static_assert(is_enum_v, "nameof::detail::strings requires enum type."); if constexpr (sparsity_v) { - return strings(std::make_index_sequence>{}); + return strings(sequence_v); } else { return strings(range_v); } @@ -502,20 +511,19 @@ class enum_traits final { template constexpr auto n() noexcept { -#if defined(__clang__) +#if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED +# if defined(__clang__) constexpr std::string_view name{__PRETTY_FUNCTION__ + 31, sizeof(__PRETTY_FUNCTION__) - 34}; -#elif defined(__GNUC__) +# elif defined(__GNUC__) constexpr std::string_view name{__PRETTY_FUNCTION__ + 46, sizeof(__PRETTY_FUNCTION__) - 49}; -#elif defined(_MSC_VER) +# elif defined(_MSC_VER) constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)}; +# endif + return static_string{name}; +#else + static_assert(nameof_type_supported::value, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); + return std::string_view{}; // Unsupported compiler. #endif - - if constexpr (nameof_type_supported::value) { - return static_string{name}; - } else { - static_assert(nameof_type_supported::value, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); - return std::string_view{}; // Unsupported compiler. - } } } // namespace nameof::detail diff --git a/test/test.cpp b/test/test.cpp index 0cb2160..a115b16 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -225,7 +225,7 @@ TEST_CASE("NAMEOF_RAW") { } } -#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) +#if defined(NAMEOF_ENUM_SUPPORTED) static_assert(nameof::is_nameof_enum_supported, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); @@ -369,7 +369,7 @@ TEST_CASE("nameof_enum") { #endif -#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) +#if defined(NAMEOF_TYPE_SUPPORTED) static_assert(nameof::is_nameof_type_supported, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");