This commit is contained in:
Neargye 2018-04-04 21:36:18 +05:00
parent d7573e556e
commit 32c4cf483f
6 changed files with 24 additions and 20 deletions

View file

@ -1,6 +1,6 @@
# nameof() c++11 # nameof() c++11
C++ alternative to [nameof](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof) operator in C#. C++ alternative to [nameof](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof) operator in [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)).
Branch | Linux/OSX | Windows Branch | Linux/OSX | Windows
-------|-----------|--------- -------|-----------|---------

View file

@ -1,7 +1,7 @@
include_directories(${CMAKE_SOURCE_DIR}/src) include_directories(${CMAKE_SOURCE_DIR}/include)
set(SOURCE_EXAMPLE set(SOURCE_EXAMPLE
${CMAKE_SOURCE_DIR}/src/nameof.hpp ${CMAKE_SOURCE_DIR}/include/nameof.hpp
example.cpp) example.cpp)
add_executable(${PROJECT_NAME}_example ${SOURCE_EXAMPLE}) add_executable(${PROJECT_NAME}_example ${SOURCE_EXAMPLE})

View file

@ -1,5 +1,5 @@
// nameof() c++11 https://github.com/Neargye/nameof // nameof() c++11 https://github.com/Neargye/nameof
// Vesion 0.2.0 // Vesion 0.2.1
// //
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// Copyright (c) 2016, 2018 Daniil Goncharov <neargye@gmail.com>. // Copyright (c) 2016, 2018 Daniil Goncharov <neargye@gmail.com>.
@ -30,30 +30,34 @@
namespace nameof { namespace nameof {
namespace detail { namespace detail {
inline constexpr bool IsLexeme(const char s) {
inline constexpr bool IsLexeme(const char s) noexcept {
return (s == '.' || s == '>' || s == ':' || s == '&' || s == '*' || return (s == '.' || s == '>' || s == ':' || s == '&' || s == '*' ||
s == '+' || s == '~' || s == '-' || s == '!'); s == '+' || s == '~' || s == '-' || s == '!');
} }
}
} } // namespace detail
} // namespace nameof
#if defined(__GXX_RTTI) || defined(_CPPRTTI) || defined(__RTTI) || defined(__INTEL_RTTI__) #if defined(__GXX_RTTI) || defined(_CPPRTTI) || defined(__RTTI) || defined(__INTEL_RTTI__)
#include <typeinfo> #include <typeinfo>
namespace nameof { namespace nameof {
inline constexpr const char* Nameof(const char* name, const std::size_t length, const std::size_t) {
inline constexpr const char* Nameof(const char* name, const std::size_t length, const std::size_t) noexcept {
return length == 0 ? name : detail::IsLexeme(name[length - 1]) return length == 0 ? name : detail::IsLexeme(name[length - 1])
? &name[length] ? &name[length]
: Nameof(name, length - 1, 0); : Nameof(name, length - 1, 0);
} }
}
} // namespace nameof
// Used to obtain the string name of a variable, type, function and etc. // Used to obtain the string name of a variable, type, function and etc.
#define NAMEOF(name) nameof::Nameof(NAMEOF_RAW(name), sizeof(NAMEOF_RAW(name)) / sizeof(char) - 1, sizeof(typeid(name))) #define NAMEOF(name) ::nameof::Nameof(NAMEOF_RAW(name), sizeof(NAMEOF_RAW(name)) / sizeof(char) - 1, sizeof(typeid(name)))
// Used to obtain the string full name of a variable, type, function and etc. // Used to obtain the string full name of a variable, type, function and etc.
#define NAMEOF_FULL(name) nameof::Nameof(NAMEOF_RAW(name), 0, sizeof(typeid(name))) #define NAMEOF_FULL(name) ::nameof::Nameof(NAMEOF_RAW(name), 0, sizeof(typeid(name)))
// Alias // Alias
#define NAMEOF_TYPE(type) NAMEOF(type) #define NAMEOF_TYPE(type) NAMEOF(type)
@ -62,24 +66,26 @@ inline constexpr const char* Nameof(const char* name, const std::size_t length,
#else #else
namespace nameof { namespace nameof {
template <typename T> template <typename T>
inline constexpr const char* Nameof(const char* name, const std::size_t length) { inline constexpr const char* Nameof(const char* name, const std::size_t length) noexcept {
return length == 0 ? name : detail::IsLexeme(name[length - 1]) return length == 0 ? name : detail::IsLexeme(name[length - 1])
? &name[length] ? &name[length]
: Nameof<T>(name, length - 1); : Nameof<T>(name, length - 1);
} }
}
} // namespace nameof
// Used to obtain the string name of a variable, function and etc. // Used to obtain the string name of a variable, function and etc.
#define NAMEOF(name) nameof::Nameof<decltype(name)>(NAMEOF_RAW(name), sizeof(NAMEOF_RAW(name)) / sizeof(char) - 1) #define NAMEOF(name) ::nameof::Nameof<decltype(name)>(NAMEOF_RAW(name), sizeof(NAMEOF_RAW(name)) / sizeof(char) - 1)
// Used to obtain the string full name of a variable, function and etc. // Used to obtain the string full name of a variable, function and etc.
#define NAMEOF_FULL(name) nameof::Nameof<decltype(name)>(NAMEOF_RAW(name), 0) #define NAMEOF_FULL(name) ::nameof::Nameof<decltype(name)>(NAMEOF_RAW(name), 0)
// Used to obtain the string name of a type. // Used to obtain the string name of a type.
#define NAMEOF_TYPE(type) nameof::Nameof<type>(NAMEOF_RAW(type), sizeof(NAMEOF_RAW(type)) / sizeof(char) - 1) #define NAMEOF_TYPE(type) ::nameof::Nameof<type>(NAMEOF_RAW(type), sizeof(NAMEOF_RAW(type)) / sizeof(char) - 1)
// Used to obtain the string full name of a type. // Used to obtain the string full name of a type.
#define NAMEOF_TYPE_FULL(type) nameof::Nameof<type>(NAMEOF_RAW(type), 0) #define NAMEOF_TYPE_FULL(type) ::nameof::Nameof<type>(NAMEOF_RAW(type), 0)
#endif #endif

View file

@ -1,5 +1,5 @@
include_directories(3rdparty/Catch2) include_directories(3rdparty/Catch2)
include_directories(${CMAKE_SOURCE_DIR}/src) include_directories(${CMAKE_SOURCE_DIR}/include)
add_executable(${PROJECT_NAME}_test test.cpp) add_executable(${PROJECT_NAME}_test test.cpp)
add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test) add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test)

View file

@ -188,5 +188,4 @@ TEST_CASE("NAMEOF_FULL") {
REQUIRE(std::strcmp(NAMEOF_FULL(Color::RED), "Color::RED") == 0); REQUIRE(std::strcmp(NAMEOF_FULL(Color::RED), "Color::RED") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(Color::BLUE), "Color::BLUE") == 0); REQUIRE(std::strcmp(NAMEOF_FULL(Color::BLUE), "Color::BLUE") == 0);
} }
} }

View file

@ -169,5 +169,4 @@ TEST_CASE("NAMEOF_FULL") {
REQUIRE(std::strcmp(NAMEOF_FULL(Color::RED), "Color::RED") == 0); REQUIRE(std::strcmp(NAMEOF_FULL(Color::RED), "Color::RED") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(Color::BLUE), "Color::BLUE") == 0); REQUIRE(std::strcmp(NAMEOF_FULL(Color::BLUE), "Color::BLUE") == 0);
} }
} }