From 1cbe42756bb1e85efd07fb9f80498640c6cbbfa7 Mon Sep 17 00:00:00 2001 From: Terik23 Date: Sat, 17 Mar 2018 13:04:35 +0500 Subject: [PATCH] Add new case --- example/example.cpp | 19 +++++++++++++++++++ src/nameof.hpp | 24 ++++++++++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index f8996d4..f65815f 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -31,10 +31,21 @@ struct SomeStruct { void SomeMethod() { std::cout << "No called!" << std::endl; } }; +struct Long +{ + struct LongLong + { + int LongLongLongField; + }; + LongLong LongLongField; +}; + int someVar = 0; int main() { SomeStruct someVar{1}; + Long otherVar{}; + SomeStruct* ptrVar = &someVar; constexpr auto a = NAMEOF_VAR(someVar.SomeField); constexpr auto b = NAMEOF_VAR((&someVar)->SomeField); @@ -43,6 +54,10 @@ int main() { 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); std::cout << a << std::endl; // SomeField std::cout << b << std::endl; // SomeField @@ -51,4 +66,8 @@ int main() { 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 } diff --git a/src/nameof.hpp b/src/nameof.hpp index 4c00fec..40d9d84 100644 --- a/src/nameof.hpp +++ b/src/nameof.hpp @@ -24,26 +24,34 @@ #pragma once #include -inline constexpr const char* template_nameof_(const char* name, size_t length) { +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] - : template_nameof_(name, length - 1); + : template_nameof(name, length - 1); } +#define NAMEOF(x) template_nameof(#x, sizeof(#x) / sizeof(char) - 1) + +#define RAW_NAMEOF(x) #x + // Used to obtain the string name of a variable. -#define NAMEOF_VARIABLE(variable) template_nameof_variable(variable, #variable, sizeof(#variable) / sizeof(char) - 1) +#define NAMEOF_VARIABLE(variable) template_nameof_variable(#variable, sizeof(#variable) / sizeof(char) - 1) template -inline constexpr const char* template_nameof_variable(const T& validate_type, const char* name, size_t length) { return template_nameof_(name, length); } +inline constexpr const char* template_nameof_variable(const char* name, const size_t length) { return template_nameof(name, length); } #define NAMEOF_VAR(var) NAMEOF_VARIABLE(var) // Used to obtain the string name of a type. -#define NAMEOF_TYPE(type) template_nameof_type(#type) +#define NAMEOF_TYPE(type) template_nameof_type(#type, sizeof(#type) / sizeof(char) - 1) template -inline constexpr const char* template_nameof_type(const char* name) { return name; } +inline constexpr const char* template_nameof_type(const char* name, const size_t length) { return template_nameof(name, length); } // Used to obtain the string name of a function. #define NAMEOF_FUNCTION(function) template_nameof_function(#function, sizeof(#function) / sizeof(char) - 1) template -inline constexpr const char* template_nameof_function(const char* name, size_t length) { return template_nameof_(name, length); } +inline constexpr const char* template_nameof_function(const char* name, const size_t length) { return template_nameof(name, length); } #define NAMEOF_FUN(fun) NAMEOF_FUNCTION(fun)