add NAMEOF_ENUM_SUPPORTED and NAMEOF_TYPE_SUPPORTED

This commit is contained in:
neargye 2019-10-02 15:23:54 +05:00
parent 67c27efd61
commit aa45d44f05
3 changed files with 34 additions and 26 deletions

View file

@ -88,7 +88,7 @@ int main() {
constexpr auto name = NAMEOF(structvar); constexpr auto name = NAMEOF(structvar);
static_assert("structvar" == name); static_assert("structvar" == name);
#if defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) #if defined(NAMEOF_ENUM_SUPPORTED)
// Nameof enum variable. // Nameof enum variable.
auto color = Color::RED; auto color = Color::RED;
std::cout << nameof::nameof_enum(color) << std::endl; // 'RED' std::cout << nameof::nameof_enum(color) << std::endl; // 'RED'

View file

@ -5,7 +5,7 @@
// | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_| // | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
// |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____| // |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
// https://github.com/Neargye/nameof // https://github.com/Neargye/nameof
// vesion 0.9.0 // vesion 0.9.1
// //
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
@ -40,6 +40,16 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
// 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. // 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 need another min range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN.
#if !defined(NAMEOF_ENUM_RANGE_MIN) #if !defined(NAMEOF_ENUM_RANGE_MIN)
@ -82,7 +92,7 @@ struct identity final {
template <typename... T> template <typename... T>
struct nameof_type_supported final 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 {}; : std::true_type {};
#else #else
: std::false_type {}; : std::false_type {};
@ -90,7 +100,7 @@ struct nameof_type_supported final
template <typename T> template <typename T>
struct nameof_enum_supported final 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 {}; : std::true_type {};
#else #else
: std::false_type {}; : std::false_type {};
@ -320,18 +330,17 @@ constexpr std::string_view pretty_name(std::string_view name, bool remove_templa
template <typename E, E V> template <typename E, E V>
constexpr auto n() noexcept { constexpr auto n() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::n requires enum type."); static_assert(is_enum_v<E>, "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}); 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}); constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
# endif
return static_string<name.size()>{name};
#else
static_assert(nameof_enum_supported<E>::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
return std::string_view{}; // Unsupported compiler.
#endif #endif
if constexpr (nameof_enum_supported<E>::value) {
return static_string<name.size()>{name};
} else {
static_assert(nameof_enum_supported<E>::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
return std::string_view{}; // Unsupported compiler.
}
} }
template <typename E, E V> template <typename E, E V>
@ -465,7 +474,7 @@ constexpr auto strings() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type."); static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type.");
if constexpr (sparsity_v<E>) { if constexpr (sparsity_v<E>) {
return strings<E>(std::make_index_sequence<count_v<E>>{}); return strings<E>(sequence_v<E>);
} else { } else {
return strings<E>(range_v<E>); return strings<E>(range_v<E>);
} }
@ -502,20 +511,19 @@ class enum_traits final {
template <typename... T> template <typename... T>
constexpr auto n() noexcept { 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}; 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}; 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)}; constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)};
# endif
return static_string<name.size()>{name};
#else
static_assert(nameof_type_supported<T...>::value, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
return std::string_view{}; // Unsupported compiler.
#endif #endif
if constexpr (nameof_type_supported<T...>::value) {
return static_string<name.size()>{name};
} else {
static_assert(nameof_type_supported<T...>::value, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
return std::string_view{}; // Unsupported compiler.
}
} }
} // namespace nameof::detail } // namespace nameof::detail

View file

@ -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)."); 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 #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)."); static_assert(nameof::is_nameof_type_supported, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");