Rewritte and clean-up code
This commit is contained in:
parent
c1b30ac323
commit
c460858d85
2 changed files with 47 additions and 39 deletions
|
@ -28,9 +28,15 @@
|
|||
|
||||
struct SomeStruct {
|
||||
int SomeField;
|
||||
void SomeMethod() { std::cout << "No called!" << std::endl; }
|
||||
void SomeMethod1() { std::cout << "No called!" << std::endl; }
|
||||
int SomeMethod2() {
|
||||
std::cout << "No called!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
void SomeMethod3() { std::cout << "No called!" << std::endl; }
|
||||
|
||||
struct Long {
|
||||
struct LL {
|
||||
int LLLField;
|
||||
|
@ -47,34 +53,43 @@ int someVar = 0;
|
|||
int main() {
|
||||
SomeStruct someVar{1};
|
||||
Long otherVar{2};
|
||||
int innValue{3};
|
||||
int intValue{3};
|
||||
SomeStruct* ptrVar = &someVar;
|
||||
SomeStruct** ptrptrVar = &ptrVar;
|
||||
|
||||
constexpr auto constexpr_work_fine = NAMEOF_VAR(innValue);
|
||||
std::cout << constexpr_work_fine << std::endl;
|
||||
constexpr auto constexpr_work_fine = NAMEOF(intValue);
|
||||
std::cout << constexpr_work_fine << std::endl; // intValue
|
||||
|
||||
std::cout << NAMEOF(someVar) << std::endl; // someVar
|
||||
std::cout << NAMEOF(someVar.SomeField) << std::endl; // SomeField
|
||||
std::cout << NAMEOF((&someVar)->SomeField) << std::endl; // SomeField
|
||||
std::cout << NAMEOF(::someVar) << std::endl; // someVar
|
||||
std::cout << NAMEOF(otherVar.LLField.LLLField) << std::endl; // LLLField
|
||||
std::cout << NAMEOF(&someVar) << std::endl; // someVar
|
||||
std::cout << NAMEOF(ptrVar) << std::endl; // ptrVar
|
||||
std::cout << NAMEOF(*ptrVar) << std::endl; // ptrVar
|
||||
std::cout << NAMEOF(ptrptrVar) << std::endl; // ptrptrVar
|
||||
std::cout << NAMEOF(*ptrptrVar) << std::endl; // ptrptrVar
|
||||
std::cout << NAMEOF(**ptrptrVar) << std::endl; // ptrptrVar
|
||||
std::cout << NAMEOF(+intValue) << std::endl; // intValue
|
||||
std::cout << NAMEOF(-intValue) << std::endl; // intValue
|
||||
std::cout << NAMEOF(~intValue) << std::endl; // intValue
|
||||
std::cout << NAMEOF(!intValue) << std::endl; // intValue
|
||||
std::cout << NAMEOF(someVar.SomeMethod1()) << std::endl; // SomeMethod1()
|
||||
std::cout << NAMEOF(&someVar.SomeMethod2) << std::endl; // SomeMethod2
|
||||
std::cout << NAMEOF(SomeMethod3) << std::endl; // SomeMethod3
|
||||
|
||||
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_FUN(someVar.SomeMethod1()) << std::endl; // SomeMethod1()
|
||||
std::cout << NAMEOF_FUN(&someVar.SomeMethod2) << std::endl; // SomeMethod2
|
||||
std::cout << NAMEOF_FUN(SomeMethod3) << std::endl; // SomeMethod3
|
||||
|
||||
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(*ptrVar) << std::endl; // ptrVar
|
||||
std::cout << NAMEOF_VAR(ptrptrVar) << std::endl; // ptrptrVar
|
||||
std::cout << NAMEOF_VAR(*ptrptrVar) << std::endl; // ptrptrVar
|
||||
std::cout << NAMEOF_VAR(**ptrptrVar) << std::endl; // ptrptrVar
|
||||
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; // innValue
|
||||
std::cout << NAMEOF_VAR(TestRValue()) << std::endl; // TestRValue()
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -24,7 +24,9 @@
|
|||
#pragma once
|
||||
#include <cstddef>
|
||||
|
||||
inline constexpr const char* template_nameof(const char* name, const size_t length) {
|
||||
#define NAMEOF_RAW(x) (#x)
|
||||
|
||||
inline constexpr const char* nameof(const char* name, const size_t length) {
|
||||
return length == 0 ? name
|
||||
: (name[length - 1] == ' ' || name[length - 1] == '.' ||
|
||||
name[length - 1] == '>' || name[length - 1] == ':' ||
|
||||
|
@ -32,28 +34,19 @@ inline constexpr const char* template_nameof(const char* name, const size_t leng
|
|||
name[length - 1] == '+' || name[length - 1] == '~' ||
|
||||
name[length - 1] == '-' || name[length - 1] == '!')
|
||||
? &name[length]
|
||||
: template_nameof(name, length - 1);
|
||||
: nameof(name, length - 1);
|
||||
}
|
||||
|
||||
#define NAMEOF(x) #x
|
||||
// Used to obtain the string name of a variable, function and etc.
|
||||
template <typename T>
|
||||
inline constexpr const char* template_nameof(const char* name, const size_t length) { return nameof(name, length); }
|
||||
#define NAMEOF(name) template_nameof<decltype(name)>(NAMEOF_RAW(name), sizeof(NAMEOF_RAW(name)) / sizeof(char) - 1)
|
||||
|
||||
// Used to obtain the string name of a variable.
|
||||
#define NAMEOF_VARIABLE(variable) template_nameof_variable(variable, #variable, sizeof(#variable) / sizeof(char) - 1)
|
||||
template <typename T>
|
||||
inline constexpr const char* template_nameof_variable(const T& variable, const char* name, const size_t length) { return template_nameof(name, length); }
|
||||
template <typename T>
|
||||
inline constexpr const char* template_nameof_variable(const T* const variable, const char* name, const size_t length) { return template_nameof(name, length); }
|
||||
template <typename T>
|
||||
inline constexpr const char* template_nameof_variable(T&& variable, const char* name, const size_t length) { return template_nameof(name, length); }
|
||||
#define NAMEOF_VARIABLE(variable) NAMEOF(variable)
|
||||
#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, sizeof(#type) / sizeof(char) - 1)
|
||||
template <typename T>
|
||||
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, const size_t length) { return template_nameof(name, length); }
|
||||
#define NAMEOF_FUNCTION(function) NAMEOF(function)
|
||||
#define NAMEOF_FUN(fun) NAMEOF_FUNCTION(fun)
|
||||
|
||||
// Used to obtain the string name of a type.
|
||||
#define NAMEOF_TYPE(type) template_nameof<type>(NAMEOF_RAW(type), sizeof(NAMEOF_RAW(type)) / sizeof(char) - 1)
|
||||
|
|
Loading…
Reference in a new issue