-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDemangleCppName.cpp
More file actions
145 lines (114 loc) Β· 4.69 KB
/
DemangleCppName.cpp
File metadata and controls
145 lines (114 loc) Β· 4.69 KB
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
134
135
136
137
138
139
140
141
142
143
144
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// MLB Utility Library Module File
// ////////////////////////////////////////////////////////////////////////////
/*
File Name : DemangleCppName.cpp
File Description : Implementation of the portable path name functions.
Revision History : 2015-07-20 --- Original logic implemented in file
MlbDev/Utility/BackTrace.cpp.
Michael L. Brock
2016-08-07 --- Separated implementation into file
DemangleCppName.cpp.
Michael L. Brock
Copyright Michael L. Brock 2015 - 2018.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// Required include files...
// ////////////////////////////////////////////////////////////////////////////
#include <Utility/DemangleCppName.hpp>
#ifdef __linux__
# include <execinfo.h>
# include <cxxabi.h>
# include <boost/regex.hpp>
#endif // #ifdef __linux__
#include <boost/scoped_ptr.hpp>
// ////////////////////////////////////////////////////////////////////////////
namespace MLB {
namespace Utility {
// ////////////////////////////////////////////////////////////////////////////
std::string DemangleCppName(const char *src)
{
#if defined(__linux__)
int status_code;
boost::scoped_ptr<char> final_name(abi::__cxa_demangle(
src, NULL, NULL, &status_code));
return((!status_code) ? std::string(final_name.get()) :
std::string(src));
#elif defined(_MSC_VER)
return(src);
#else
return(src);
#endif // #if defined(__linux__)
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string DemangleCppName(const std::string &src)
{
return(DemangleCppName(src.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string DemangleTypeInfoName(const char *src)
{
return(DemangleCppName(src));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string DemangleTypeInfoName(const std::string &src)
{
return(DemangleTypeInfoName(src.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
} // namespace Utility
} // namespace MLB
// ////////////////////////////////////////////////////////////////////////////
// ****************************************************************************
// ****************************************************************************
// ****************************************************************************
// ////////////////////////////////////////////////////////////////////////////
#ifdef TEST_MAIN
// ////////////////////////////////////////////////////////////////////////////
#include <typeinfo>
// ////////////////////////////////////////////////////////////////////////////
using namespace MLB::Utility;
namespace {
// ////////////////////////////////////////////////////////////////////////////
struct MyStruct {
};
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
class MyClass {
public:
struct MyStructInner {
};
};
// ////////////////////////////////////////////////////////////////////////////
} // Anonymous namespace
// ////////////////////////////////////////////////////////////////////////////
int main()
{
int return_code = EXIT_SUCCESS;
try {
std::cout << DemangleTypeInfoName(typeid(std::string).name()) << "\n";
std::cout << DemangleTypeInfoName(typeid(std::ostream).name()) << "\n";
std::cout << DemangleTypeInfoName(typeid(main).name()) << "\n";
std::cout << DemangleTypeInfoName(typeid(MyStruct).name()) << "\n";
std::cout << DemangleTypeInfoName(typeid(MyClass).name()) << "\n";
std::cout << DemangleTypeInfoName(typeid(MyClass::MyStructInner).name())
<< "\n";
}
catch (const std::exception &except) {
std::cout << std::endl;
std::cout << "Regression test error: " << except.what() << std::endl;
return_code = EXIT_FAILURE;
}
return(return_code);
}
// ////////////////////////////////////////////////////////////////////////////
#endif // #ifdef TEST_MAIN