Skip to content

Commit aa45d44

Browse files
committed
add NAMEOF_ENUM_SUPPORTED and NAMEOF_TYPE_SUPPORTED
1 parent 67c27ef commit aa45d44

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

example/example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int main() {
8888
constexpr auto name = NAMEOF(structvar);
8989
static_assert("structvar" == name);
9090

91-
#if defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER)
91+
#if defined(NAMEOF_ENUM_SUPPORTED)
9292
// Nameof enum variable.
9393
auto color = Color::RED;
9494
std::cout << nameof::nameof_enum(color) << std::endl; // 'RED'

include/nameof.hpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
66
// |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
77
// https://github.com/Neargye/nameof
8-
// vesion 0.9.0
8+
// vesion 0.9.1
99
//
1010
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
1111
// SPDX-License-Identifier: MIT
@@ -40,6 +40,16 @@
4040
#include <type_traits>
4141
#include <utility>
4242

43+
// Checks nameof_type compiler compatibility.
44+
#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
45+
# define NAMEOF_TYPE_SUPPORTED 1
46+
#endif
47+
48+
// Checks nameof_enum compiler compatibility.
49+
#if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 || defined(_MSC_VER)
50+
# define NAMEOF_ENUM_SUPPORTED 1
51+
#endif
52+
4353
// Enum value must be greater or equals than NAMEOF_ENUM_RANGE_MIN. By default NAMEOF_ENUM_RANGE_MIN = -128.
4454
// If need another min range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN.
4555
#if !defined(NAMEOF_ENUM_RANGE_MIN)
@@ -82,15 +92,15 @@ struct identity final {
8292

8393
template <typename... T>
8494
struct nameof_type_supported final
85-
#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
95+
#if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED
8696
: std::true_type {};
8797
#else
8898
: std::false_type {};
8999
#endif
90100

91101
template <typename T>
92102
struct nameof_enum_supported final
93-
#if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 || defined(_MSC_VER) || defined(NAMEOF_ENUM_NO_CHECK_SUPPORT)
103+
#if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED
94104
: std::true_type {};
95105
#else
96106
: std::false_type {};
@@ -320,18 +330,17 @@ constexpr std::string_view pretty_name(std::string_view name, bool remove_templa
320330
template <typename E, E V>
321331
constexpr auto n() noexcept {
322332
static_assert(is_enum_v<E>, "nameof::detail::n requires enum type.");
323-
#if defined(__clang__) || defined(__GNUC__)
333+
#if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED
334+
# if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9
324335
constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
325-
#elif defined(_MSC_VER)
336+
# elif defined(_MSC_VER)
326337
constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
338+
# endif
339+
return static_string<name.size()>{name};
340+
#else
341+
static_assert(nameof_enum_supported<E>::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
342+
return std::string_view{}; // Unsupported compiler.
327343
#endif
328-
329-
if constexpr (nameof_enum_supported<E>::value) {
330-
return static_string<name.size()>{name};
331-
} else {
332-
static_assert(nameof_enum_supported<E>::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
333-
return std::string_view{}; // Unsupported compiler.
334-
}
335344
}
336345

337346
template <typename E, E V>
@@ -465,7 +474,7 @@ constexpr auto strings() noexcept {
465474
static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type.");
466475

467476
if constexpr (sparsity_v<E>) {
468-
return strings<E>(std::make_index_sequence<count_v<E>>{});
477+
return strings<E>(sequence_v<E>);
469478
} else {
470479
return strings<E>(range_v<E>);
471480
}
@@ -502,20 +511,19 @@ class enum_traits final {
502511

503512
template <typename... T>
504513
constexpr auto n() noexcept {
505-
#if defined(__clang__)
514+
#if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED
515+
# if defined(__clang__)
506516
constexpr std::string_view name{__PRETTY_FUNCTION__ + 31, sizeof(__PRETTY_FUNCTION__) - 34};
507-
#elif defined(__GNUC__)
517+
# elif defined(__GNUC__)
508518
constexpr std::string_view name{__PRETTY_FUNCTION__ + 46, sizeof(__PRETTY_FUNCTION__) - 49};
509-
#elif defined(_MSC_VER)
519+
# elif defined(_MSC_VER)
510520
constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)};
521+
# endif
522+
return static_string<name.size()>{name};
523+
#else
524+
static_assert(nameof_type_supported<T...>::value, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
525+
return std::string_view{}; // Unsupported compiler.
511526
#endif
512-
513-
if constexpr (nameof_type_supported<T...>::value) {
514-
return static_string<name.size()>{name};
515-
} else {
516-
static_assert(nameof_type_supported<T...>::value, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
517-
return std::string_view{}; // Unsupported compiler.
518-
}
519527
}
520528

521529
} // namespace nameof::detail

test/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ TEST_CASE("NAMEOF_RAW") {
225225
}
226226
}
227227

228-
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER)
228+
#if defined(NAMEOF_ENUM_SUPPORTED)
229229

230230
static_assert(nameof::is_nameof_enum_supported, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
231231

@@ -369,7 +369,7 @@ TEST_CASE("nameof_enum") {
369369

370370
#endif
371371

372-
#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
372+
#if defined(NAMEOF_TYPE_SUPPORTED)
373373

374374
static_assert(nameof::is_nameof_type_supported, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
375375

0 commit comments

Comments
 (0)