-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_name.hpp
78 lines (71 loc) · 2.13 KB
/
type_name.hpp
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
#pragma once
#include <string_view>
#ifndef _MSC_VER
#include <cxxabi.h>
#endif
namespace misc {
template<typename T>
constexpr std::basic_string_view<char> type_name() {
#ifdef MISC_CUSTOM_TYPE_NAME
MISC_CUSTOM_TYPE_NAME
#else
# ifdef __clang__
std::string_view result{__PRETTY_FUNCTION__};
result.remove_prefix(std::string_view{
"std::basic_string_view<char> misc::type_name() [T = "
}.size());
result.remove_suffix(std::string_view{
"]"
}.size());
return result;
# elif defined(__GNUC__)
std::string_view result{__PRETTY_FUNCTION__};
result.remove_prefix(std::string_view{
"constexpr std::basic_string_view<char> misc::type_name() [with T = "
}.size());
result.remove_suffix(std::string_view{
"]"
}.size());
return result;
# elif defined(_MSC_VER)
std::string_view result{__FUNCSIG__};
result.remove_prefix(std::string_view{
"class std::basic_string_view<char,struct std::char_traits<char> > __cdecl misc::type_name<"
}.size());
result.remove_suffix(std::string_view{
">(void)"
}.size());
if (result.starts_with("struct "))
result.remove_prefix(7);
else if (result.starts_with("class "))
result.remove_prefix(6);
return result;
# else
# error "not implemented, you might want to define MISC_CUSTOM_TYPE_NAME"
# endif
#endif
}
inline std::string demangle(const char *name_from_typeid) {
#ifndef _MSC_VER
int status = 0;
std::size_t size = 0;
char *p = abi::__cxa_demangle(name_from_typeid, NULL, &size, &status); // NOLINT(modernize-use-nullptr)
if (!p)
return name_from_typeid;
std::string result(p);
std::free(p);
return result;
#else
std::string result = name_from_typeid;
if (result.substr(0, 6) == "class ")
result = result.substr(6);
if (result.substr(0, 7) == "struct ")
result = result.substr(7);
if (result.substr(0, 5) == "enum ")
result = result.substr(5);
return result;
#endif
}
}
// can be put in class, might require type_traits
#define MISC_CLASS_NAME misc::type_name<std::remove_cvref_t<decltype(*this)>>()