diff --git a/LICENSE b/LICENSE index c6b0653..ec52865 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016 - 2022 Daniil Goncharov +Copyright (c) 2016 - 2023 Daniil Goncharov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 01b18c7..7cb8552 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Header-only C++17 library provides nameof macros and functions to simply obtain * Compile-time * Name of variable, member variable * Name of type, variable type +* Name of member, pointer * Name of function, member function * Name of enum, enum variable * Name of macro @@ -149,9 +150,6 @@ CPMAddPackage( ## Compiler compatibility -* Clang/LLVM >= 6 -* MSVC++ >= 14.11 / Visual Studio >= 2017 -* Xcode >= 10 -* GCC >= 7 (GCC >= 9 for NAMEOF_ENUM) +Check in the [reference](doc/reference.md) for each features. ## Licensed under the [MIT License](LICENSE) diff --git a/doc/limitations.md b/doc/limitations.md index 04a37ca..97189f2 100644 --- a/doc/limitations.md +++ b/doc/limitations.md @@ -16,6 +16,12 @@ * To check if nameof_type_rtti is supported by your compiler use macro `NAMEOF_TYPE_RTTI_SUPPORTED` or constexpr constant `nameof::is_nameof_type_rtti_supported`.
If nameof_type_rtti is used on an unsupported compiler, a compilation error occurs. To suppress the error define the macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`. +* To check is nameof_member supported compiler use macro `NAMEOF_MEMBER_SUPPORTED` or constexpr constant `nameof::is_nameof_member_supported`.
+ If nameof_member used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`. + +* To check is nameof_pointer supported compiler use macro `NAMEOF_POINTER_SUPPORTED` or constexpr constant `nameof::is_nameof_pointer_supported`.
+ If nameof_pointer used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`. + ## Nameof Enum * This library uses a compiler-specific hack (based on `__PRETTY_FUNCTION__` / `__FUNCSIG__`), which works on Clang >= 5, MSVC >= 15.3 and GCC >= 9. diff --git a/doc/reference.md b/doc/reference.md index cfcea5b..c374d7e 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -16,19 +16,27 @@ * [`NAMEOF_TYPE_RTTI` obtains type name, using RTTI.](#nameof_type_rtti) * [`NAMEOF_FULL_TYPE_RTTI` obtains short type name, using RTTI.](#nameof_full_type_rtti) * [`NAMEOF_SHORT_TYPE_RTTI` obtains short type name, using RTTI.](#nameof_short_type_rtti) +* [`NAMEOF_MEMBER` obtains name of member.](#nameof_member) +* [`NAMEOF_POINTER` obtains name of a function, a global or class static variable.](#nameof_pointer) ## Synopsis * Before use, read the [limitations](limitations.md) of functionality. +* To check is nameof_enum supported compiler use macro `NAMEOF_ENUM_SUPPORTED` or constexpr constant `nameof::is_nameof_enum_supported`.
+ If nameof_enum used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_ENUM_NO_CHECK_SUPPORT`. + * To check is nameof_type supported compiler use macro `NAMEOF_TYPE_SUPPORTED` or constexpr constant `nameof::is_nameof_type_supported`.
If nameof_type used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`. * To check is nameof_type_rtti supported compiler use macro `NAMEOF_TYPE_RTTI_SUPPORTED` or constexpr constant `nameof::is_nameof_type_rtti_supported`.
If nameof_type used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`. -* To check is nameof_enum supported compiler use macro `NAMEOF_ENUM_SUPPORTED` or constexpr constant `nameof::is_nameof_enum_supported`.
- If nameof_enum used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_ENUM_NO_CHECK_SUPPORT`. +* To check is nameof_member supported compiler use macro `NAMEOF_MEMBER_SUPPORTED` or constexpr constant `nameof::is_nameof_member_supported`.
+ If nameof_member used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`. + +* To check is nameof_pointer supported compiler use macro `NAMEOF_POINTER_SUPPORTED` or constexpr constant `nameof::is_nameof_pointer_supported`.
+ If nameof_pointer used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`. * To add custom enum or type names see the [example](../example/example_custom_name.cpp). @@ -71,6 +79,11 @@ NAMEOF(NAMEOF(structvar)) -> "NAMEOF" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
+ ## `NAMEOF_FULL` * Obtains full (with template suffix) name of variable, function, macro. @@ -89,6 +102,11 @@ NAMEOF_FULL(somevar.some_method()) -> "some_method" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
+ ## `NAMEOF_RAW` * Obtains raw name of variable, function, macro. @@ -104,6 +122,10 @@ NAMEOF_RAW(&some_class::some_method) -> "&some_class::some_method" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
## `NAMEOF_ENUM` @@ -121,6 +143,11 @@ nameof::nameof_enum(color) -> "RED" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 9 and C++ >= 17
+ # `NAMEOF_ENUM_OR` * Obtains name of enum variable or default value if enum variable out of range. @@ -129,6 +156,19 @@ * If argument does not have name or [out of range](limitations.md#nameof-enum), returns `default_value`. + ```cpp + auto color = Color::RED; + NAMEOF_ENUM_OR(color, "none") -> "RED" + NAMEOF_ENUM_OR((Color)-1, "none") -> "none" + nameof::nameof_enum_or(color, "none") -> "RED" + nameof::nameof_enum_or((Color)-1, "none") -> "none" + ``` + +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 9 and C++ >= 17
+ ## `NAMEOF_ENUM_CONST` * Obtains name of static storage enum variable. @@ -146,6 +186,11 @@ nameof::nameof_enum() -> "GREEN" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 9 and C++ >= 17
+ ## `NAMEOF_ENUM_FLAG` * Obtains name of enum flag variable. @@ -171,6 +216,11 @@ nameof_enum(HasClaws | CanFly) -> "" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 9 and C++ >= 17
+ ## `NAMEOF_TYPE` * Obtains type name, reference and cv-qualifiers are ignored. @@ -192,6 +242,11 @@ nameof::nameof_type() -> "int" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
+ ## `NAMEOF_FULL_TYPE` * Obtains full type name, with reference and cv-qualifiers. @@ -210,6 +265,11 @@ nameof::nameof_full_type() -> "const int&" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
+ ## `NAMEOF_SHORT_TYPE` * Obtains short type name. @@ -228,6 +288,11 @@ nameof::nameof_short_type() -> "SomeClass" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
+ ## `NAMEOF_TYPE_EXPR` * Obtains string name type of expression, reference and cv-qualifiers are ignored. @@ -249,6 +314,11 @@ nameof::nameof_type() -> "int" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
+ ## `NAMEOF_FULL_TYPE_EXPR` * Obtains full type name of expression, with reference and cv-qualifiers. @@ -268,6 +338,11 @@ nameof::nameof_full_type() -> "const int&" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
+ ## `NAMEOF_SHORT_TYPE_EXPR` * Obtains short type name of expression. @@ -286,6 +361,11 @@ nameof::nameof_short_type() -> "SomeClass" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2017 and C++ >= 17
+ GCC >= 7 and C++ >= 17
+ ## `NAMEOF_TYPE_RTTI` * Obtains type name, using RTTI. @@ -299,6 +379,11 @@ NAMEOF_TYPE_RTTI(*ptr) -> "my::detail::Derived" ``` +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17 and RTTI enabled
+ Visual Studio >= 2017 and C++ >= 17 and RTTI enabled
+ GCC >= 7 and C++ >= 17 and RTTI enabled
+ ## `NAMEOF_FULL_TYPE_RTTI` * Obtains full type name, using RTTI. @@ -324,3 +409,53 @@ volatile const my::detail::Base* ptr = new my::detail::Derived(); NAMEOF_SHORT_TYPE_RTTI(*ptr) -> "Derived" ``` + +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17 and RTTI enabled
+ Visual Studio >= 2017 and C++ >= 17 and RTTI enabled
+ GCC >= 7 and C++ >= 17 and RTTI enabled
+ +## `NAMEOF_MEMBER` + +* Obtains name of member. + +* Returns `string_view`. + +* Examples + + ```cpp + struct A { + int this_is_the_name; + }; + // .. + NAMEOF_MEMBER(&A::this_is_the_name) -> "this_is_the_name" + nameof::nameof_member(&A::this_is_the_name) -> "this_is_the_name" + ``` + +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2022 and C++ >= 20
+ GCC >= 7 and C++ >= 17
+ +## `NAMEOF_POINTER` + +* Obtains name of a function, a global or class static variable. + +* Returns `string_view`. + +* Examples + ```cpp + int someglobalvariable = 0; + // .. + NAMEOF_POINTER(&someglobalconstvariable) == "someglobalconstvariable" + nameof::nameof_pointer(&someglobalconstvariable) == "someglobalconstvariable" + + constexpr auto global_ptr = &someglobalvariable; + NAMEOF_POINTER(global_ptr) == "someglobalconstvariable" + nameof::nameof_pointer(global_ptr) == "someglobalconstvariable" + ``` + +* Compiler compatibility + Clang/LLVM >= 5 and C++ >= 17
+ Visual Studio >= 2022 and C++ >= 20
+ GCC >= 7 and C++ >= 17
diff --git a/example/example.cpp b/example/example.cpp index 90e6efd..d897adc 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -1,6 +1,6 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2018 - 2022 Daniil Goncharov . +// Copyright (c) 2018 - 2023 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/example/example_custom_name.cpp b/example/example_custom_name.cpp index e8d4c3f..7306d38 100644 --- a/example/example_custom_name.cpp +++ b/example/example_custom_name.cpp @@ -1,6 +1,6 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2020 - 2022 Daniil Goncharov . +// Copyright (c) 2020 - 2023 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/include/nameof.hpp b/include/nameof.hpp index 565d667..e5ade6f 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -9,7 +9,7 @@ // // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2016 - 2022 Daniil Goncharov . +// Copyright (c) 2016 - 2023 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -1009,7 +1009,7 @@ constexpr auto get_member_name() noexcept { return n(); } else { constexpr bool is_defined = sizeof(decltype(get_base_type(V))) != 0; - static_assert(is_defined, "Member name can use only if the struct is already fully defined. Please use NAMEOF macro, or separate definition and declaration."); + static_assert(is_defined, "nameof::nameof_member member name can use only if the struct is already fully defined. Please use NAMEOF macro, or separate definition and declaration."); if constexpr (is_defined) { return n::value.f.*V)>(); } else { @@ -1025,19 +1025,18 @@ template inline constexpr auto member_name_v = cstring<0>{}; #endif -template +template struct is_same : std::false_type {}; -template +template struct is_same : std::true_type {}; -template +template constexpr bool is_nullptr_v = is_same>(nullptr)>::value; template constexpr auto p() noexcept { - [[maybe_unused]] constexpr auto custom_name = - customize::pointer_name().empty() && is_nullptr_v ? "nullptr" : customize::pointer_name(); + [[maybe_unused]] constexpr auto custom_name = customize::pointer_name().empty() && is_nullptr_v ? "nullptr" : customize::pointer_name(); if constexpr (custom_name.empty() && nameof_pointer_supported::value) { #if defined(__clang__) diff --git a/test/test.cpp b/test/test.cpp index 24b3727..9173914 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,6 +1,6 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2018 - 2022 Daniil Goncharov . +// Copyright (c) 2018 - 2023 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -975,4 +975,4 @@ TEST_CASE("nameof_pointer") { REQUIRE(nameof::nameof_pointer<&somefunction>() == "somefunction"); } -#endif \ No newline at end of file +#endif diff --git a/test/test_aliases.cpp b/test/test_aliases.cpp index 0f1d417..f41fcb3 100644 --- a/test/test_aliases.cpp +++ b/test/test_aliases.cpp @@ -1,6 +1,6 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2019 - 2022 Daniil Goncharov . +// Copyright (c) 2019 - 2023 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal