From 370f6b824a29e3517d1c850f400a7ecbb7053260 Mon Sep 17 00:00:00 2001 From: Terik23 Date: Sat, 17 Mar 2018 14:12:59 +0500 Subject: [PATCH] Add new case --- example/example.cpp | 57 +++++++++++++++++++++------------------------ src/nameof.hpp | 13 +++++++---- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index f65815f..ffbe8f1 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -31,43 +31,40 @@ struct SomeStruct { void SomeMethod() { std::cout << "No called!" << std::endl; } }; -struct Long -{ - struct LongLong - { - int LongLongLongField; +struct Long { + struct LL { + int LLLField; }; - LongLong LongLongField; + LL LLField; }; int someVar = 0; int main() { SomeStruct someVar{1}; - Long otherVar{}; + Long otherVar{2}; + int innValue{3}; SomeStruct* ptrVar = &someVar; - constexpr auto a = NAMEOF_VAR(someVar.SomeField); - constexpr auto b = NAMEOF_VAR((&someVar)->SomeField); - constexpr auto c = NAMEOF_VAR(someVar); - constexpr auto d = NAMEOF_VAR(::someVar); - constexpr auto e = NAMEOF_VAR(&SomeStruct::SomeMethod); - constexpr auto f = NAMEOF_FUN(someVar.SomeMethod()); - constexpr auto g = NAMEOF_TYPE(SomeStruct); - constexpr auto h = NAMEOF_VAR(otherVar.LongLongField.LongLongLongField); - constexpr auto i = NAMEOF_VAR(&someVar); - constexpr auto j = NAMEOF_VAR(*ptrVar); - constexpr auto k = NAMEOF_TYPE(Long::LongLong); + constexpr auto constexpr_work_fine = NAMEOF_VAR(innValue); + std::cout << constexpr_work_fine << std::endl; - std::cout << a << std::endl; // SomeField - std::cout << b << std::endl; // SomeField - std::cout << c << std::endl; // someVar - std::cout << d << std::endl; // someVar - std::cout << e << std::endl; // SomeMethod - std::cout << f << std::endl; // SomeMethod() - std::cout << g << std::endl; // SomeStruct - std::cout << h << std::endl; // LongLongLongField - std::cout << i << std::endl; // someVar - std::cout << j << std::endl; // ptrVar - std::cout << k << std::endl; // LongLong -} + std::cout << NAMEOF_TYPE(int[]) << std::endl; // int[] + std::cout << NAMEOF_TYPE(SomeStruct) << std::endl; // SomeStruct + std::cout << NAMEOF_TYPE(Long::LL) << std::endl; // LL + + std::cout << NAMEOF_FUN(someVar.SomeMethod()) << std::endl; // SomeMethod() + + std::cout << NAMEOF_VAR(someVar) << std::endl; // someVar + std::cout << NAMEOF_VAR(someVar.SomeField) << std::endl; // SomeField + std::cout << NAMEOF_VAR((&someVar)->SomeField) << std::endl; // SomeField + std::cout << NAMEOF_VAR(::someVar) << std::endl; // someVar + std::cout << NAMEOF_VAR(&SomeStruct::SomeMethod) << std::endl; // SomeMethod + std::cout << NAMEOF_VAR(otherVar.LLField.LLLField) << std::endl; // LLLField + std::cout << NAMEOF_VAR(&someVar) << std::endl; // someVar + std::cout << NAMEOF_VAR(*ptrVar) << std::endl; // ptrVar + std::cout << NAMEOF_VAR(+innValue) << std::endl; // innValue + std::cout << NAMEOF_VAR(-innValue) << std::endl; // innValue + std::cout << NAMEOF_VAR(~innValue) << std::endl; // innValue + std::cout << NAMEOF_VAR(!innValue) << std::endl; // unsignedValue +} \ No newline at end of file diff --git a/src/nameof.hpp b/src/nameof.hpp index 40d9d84..4e24da9 100644 --- a/src/nameof.hpp +++ b/src/nameof.hpp @@ -26,18 +26,21 @@ inline constexpr const char* template_nameof(const char* name, const size_t length) { return length == 0 ? name - : (name[length - 1] == '.' || + : (name[length - 1] == ' ' || + name[length - 1] == '.' || name[length - 1] == '>' || name[length - 1] == ':' || name[length - 1] == '&' || - name[length - 1] == '*') + name[length - 1] == '*' || + name[length - 1] == '+' || + name[length - 1] == '~' || + name[length - 1] == '-' || + name[length - 1] == '!') ? &name[length] : template_nameof(name, length - 1); } -#define NAMEOF(x) template_nameof(#x, sizeof(#x) / sizeof(char) - 1) - -#define RAW_NAMEOF(x) #x +#define NAMEOF(x) #x // Used to obtain the string name of a variable. #define NAMEOF_VARIABLE(variable) template_nameof_variable(#variable, sizeof(#variable) / sizeof(char) - 1)