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
|
#ifndef __TYPEINFO_H
#define __TYPEINFO_H
#include <typeinfo>
#include <string>
#include <stdexcept>
#include <cstdlib>
#if defined( __GNUG__ ) && defined( __GLIBCXX__ )
#include <cxxabi.h>
std::string demangle( const std::type_info &info )
{
enum { BUFLEN = 200 };
char *buf;
std::size_t buflen = BUFLEN;
int status;
char *res;
// pass a malloced buffer (required by cxa_demangle)
buf = (char *)malloc( BUFLEN );
// res may point to buf or be realloced depending on whether the
// demangling had enough space or not
res = __cxxabiv1::__cxa_demangle( info.name( ), buf, &buflen, &status );
// throw exception if this goes wrong, because we don't want to have
// have code reacting on failed demangling or wrong type information!
if( status != 0 || res == NULL ) {
throw std::runtime_error( "__cxa_demangle failed!" );
}
// construct string on stack and return it, free original buffer
std::string s( res );
free( res );
return s;
}
#else
#ifdef _WIN32
// TODO: maybe extract into a generic stringutils module
void replaceAll( std::string &s, const std::string &from, const std::string &to )
{
if( from.empty( ) ) {
return;
}
size_t pos = 0;
while( ( pos = s.find( from, pos ) ) != std::string::npos ) {
s.replace( pos, from.length( ), to );
pos += to.length( );
}
}
std::string demangle( const std::type_info &info )
{
std::string name = info.name( );
// MSVC marks metatypes, nice, but gcc doesn't, falling
// back as we can't do the same for gcc
replaceAll( name, "class ", "" );
// TODO: much more to follow, this is currently just enough
// for the module registry structure with base class
// signature only
return name;
}
#else
#error "type.name() demangling not implemented!"
#endif // _WIN32
#endif // defined( __GNUG__ ) && defined( __GLIBCXX__ )
#endif // __TYPEINFO_H
|