From 32c4cf483fe5a0fefead9f2d4e63a57438833878 Mon Sep 17 00:00:00 2001 From: Neargye Date: Wed, 4 Apr 2018 21:36:18 +0500 Subject: [PATCH] v0.2.1 --- README.md | 2 +- example/CMakeLists.txt | 4 ++-- {src => include}/nameof.hpp | 34 ++++++++++++++++++++-------------- test/CMakeLists.txt | 2 +- test/test.cpp | 1 - test/test_no_rtti.cpp | 1 - 6 files changed, 24 insertions(+), 20 deletions(-) rename {src => include}/nameof.hpp (76%) diff --git a/README.md b/README.md index 020bedd..478bc89 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 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 -------|-----------|--------- diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index b00474b..ebb8165 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,7 +1,7 @@ -include_directories(${CMAKE_SOURCE_DIR}/src) +include_directories(${CMAKE_SOURCE_DIR}/include) set(SOURCE_EXAMPLE - ${CMAKE_SOURCE_DIR}/src/nameof.hpp + ${CMAKE_SOURCE_DIR}/include/nameof.hpp example.cpp) add_executable(${PROJECT_NAME}_example ${SOURCE_EXAMPLE}) \ No newline at end of file diff --git a/src/nameof.hpp b/include/nameof.hpp similarity index 76% rename from src/nameof.hpp rename to include/nameof.hpp index e94dc3a..85b1f07 100644 --- a/src/nameof.hpp +++ b/include/nameof.hpp @@ -1,5 +1,5 @@ // nameof() c++11 https://github.com/Neargye/nameof -// Vesion 0.2.0 +// Vesion 0.2.1 // // Licensed under the MIT License . // Copyright (c) 2016, 2018 Daniil Goncharov . @@ -30,30 +30,34 @@ namespace nameof { namespace detail { -inline constexpr bool IsLexeme(const char s) { + +inline constexpr bool IsLexeme(const char s) noexcept { return (s == '.' || s == '>' || s == ':' || s == '&' || s == '*' || s == '+' || s == '~' || s == '-' || s == '!'); } -} -} + +} // namespace detail +} // namespace nameof #if defined(__GXX_RTTI) || defined(_CPPRTTI) || defined(__RTTI) || defined(__INTEL_RTTI__) #include 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]) ? &name[length] : Nameof(name, length - 1, 0); } -} + +} // namespace nameof // 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. -#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 #define NAMEOF_TYPE(type) NAMEOF(type) @@ -62,24 +66,26 @@ inline constexpr const char* Nameof(const char* name, const std::size_t length, #else namespace nameof { + template -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]) ? &name[length] : Nameof(name, length - 1); } -} + +} // namespace nameof // Used to obtain the string name of a variable, function and etc. -#define NAMEOF(name) nameof::Nameof(NAMEOF_RAW(name), sizeof(NAMEOF_RAW(name)) / sizeof(char) - 1) +#define NAMEOF(name) ::nameof::Nameof(NAMEOF_RAW(name), sizeof(NAMEOF_RAW(name)) / sizeof(char) - 1) // Used to obtain the string full name of a variable, function and etc. -#define NAMEOF_FULL(name) nameof::Nameof(NAMEOF_RAW(name), 0) +#define NAMEOF_FULL(name) ::nameof::Nameof(NAMEOF_RAW(name), 0) // Used to obtain the string name of a type. -#define NAMEOF_TYPE(type) nameof::Nameof(NAMEOF_RAW(type), sizeof(NAMEOF_RAW(type)) / sizeof(char) - 1) +#define NAMEOF_TYPE(type) ::nameof::Nameof(NAMEOF_RAW(type), sizeof(NAMEOF_RAW(type)) / sizeof(char) - 1) // Used to obtain the string full name of a type. -#define NAMEOF_TYPE_FULL(type) nameof::Nameof(NAMEOF_RAW(type), 0) +#define NAMEOF_TYPE_FULL(type) ::nameof::Nameof(NAMEOF_RAW(type), 0) #endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 446db4e..4a51b03 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,5 @@ include_directories(3rdparty/Catch2) -include_directories(${CMAKE_SOURCE_DIR}/src) +include_directories(${CMAKE_SOURCE_DIR}/include) add_executable(${PROJECT_NAME}_test test.cpp) add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test) diff --git a/test/test.cpp b/test/test.cpp index 9a8c005..1cdf16a 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -188,5 +188,4 @@ TEST_CASE("NAMEOF_FULL") { REQUIRE(std::strcmp(NAMEOF_FULL(Color::RED), "Color::RED") == 0); REQUIRE(std::strcmp(NAMEOF_FULL(Color::BLUE), "Color::BLUE") == 0); } - } diff --git a/test/test_no_rtti.cpp b/test/test_no_rtti.cpp index b2819da..86db2b4 100644 --- a/test/test_no_rtti.cpp +++ b/test/test_no_rtti.cpp @@ -169,5 +169,4 @@ TEST_CASE("NAMEOF_FULL") { REQUIRE(std::strcmp(NAMEOF_FULL(Color::RED), "Color::RED") == 0); REQUIRE(std::strcmp(NAMEOF_FULL(Color::BLUE), "Color::BLUE") == 0); } - }