Add new case

This commit is contained in:
Terik23 2018-03-17 13:04:35 +05:00
parent cc8a598825
commit 1cbe42756b
2 changed files with 35 additions and 8 deletions

View file

@ -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
}

View file

@ -24,26 +24,34 @@
#pragma once
#include <cstddef>
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<decltype(variable)>(#variable, sizeof(#variable) / sizeof(char) - 1)
template <typename T>
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>(#type)
#define NAMEOF_TYPE(type) template_nameof_type<type>(#type, sizeof(#type) / sizeof(char) - 1)
template <typename T>
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<decltype(function)>(#function, sizeof(#function) / sizeof(char) - 1)
template <typename T>
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)