This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypenameof.cpp
133 lines (98 loc) · 2.95 KB
/
typenameof.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef JDECOMPILER_TYPENAME_OF_CPP
#define JDECOMPILER_TYPENAME_OF_CPP
#include <regex>
namespace jdecompiler {
using std::regex;
using std::regex_replace;
static inline string undecorateTypeName(const char* name) {
#ifdef __GNUC__
static const regex
namespaceStartRegex("^N\\d+"),
namespaceEndRegex("E$"),
namespaceRegex("\\d+"),
startRegex("^\\d+");
if(name[0] == 'N' && isdigit(name[1]))
return regex_replace(regex_replace(regex_replace(name, namespaceStartRegex, ""), namespaceEndRegex, ""), namespaceRegex, "::");
else
return regex_replace(name, startRegex, "");
#else
return name;
#endif
}
template<typename T>
static inline constexpr string typenameof() {
if constexpr(is_pointer<T>())
return typenameof<remove_pointer_t<T>>() + '*';
else if constexpr(is_const<T>())
return "const " + typenameof<remove_const_t<T>>();
else if constexpr(is_volatile<T>())
return "volatile " + typenameof<remove_volatile_t<T>>();
else
return undecorateTypeName(typeid(T).name());
}
template<> inline string typenameof<void>() {
return "void";
}
template<> inline string typenameof<char>() {
return "char";
}
template<> inline string typenameof<short>() {
return "short";
}
template<> inline string typenameof<int>() {
return "int";
}
template<> inline string typenameof<long>() {
return "long";
}
template<> inline string typenameof<long long>() {
return "long long";
}
template<> inline string typenameof<signed char>() {
return "signed char";
}
template<> inline string typenameof<unsigned char>() {
return "unsigned char";
}
template<> inline string typenameof<unsigned short>() {
return "unsigned short";
}
template<> inline string typenameof<unsigned int>() {
return "unsigned int";
}
template<> inline string typenameof<unsigned long>() {
return "unsigned long";
}
template<> inline string typenameof<unsigned long long>() {
return "unsigned long long";
}
template<> inline string typenameof<float>() {
return "float";
}
template<> inline string typenameof<double>() {
return "double";
}
template<> inline string typenameof<long double>() {
return "long double";
}
template<> inline string typenameof<void*>() {
return "void*";
}
template<> inline string typenameof<const void*>() {
return "const void*";
}
template<class T>
static inline string typenameof(T& value) {
if constexpr(is_fundamental<T>() || is_same<remove_cv_t<remove_pointer_t<T>>, void>() /* const or volatile void* */)
return typenameof<T>();
else if constexpr(is_pointer<T>())
return typenameof<remove_pointer_t<T>>(*value) + '*';
else if constexpr(is_const<T>())
return "const " + typenameof<remove_const_t<T>>(const_cast<remove_const_t<T>&>(value));
else if constexpr(is_volatile<T>())
return "volatile " + typenameof<remove_volatile_t<T>>(const_cast<remove_volatile_t<T>&>(value));
else
return undecorateTypeName(typeid(value).name());
}
}
#endif