-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_name.hpp
61 lines (57 loc) · 1.46 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
//
// Copyright Mathieu Champlon 2009
//
// 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)
//
#ifndef MOCK_TYPE_NAME_HPP_INCLUDED
#define MOCK_TYPE_NAME_HPP_INCLUDED
#include <stdexcept>
#include <typeinfo>
#ifdef __GNUC__
#include <cxxabi.h>
#include <cstdlib>
#endif
namespace mock
{
namespace detail
{
inline std::string type_full_name( const std::type_info& info )
{
const char* name = info.name();
#ifdef __GNUC__
size_t size = 0;
int status = 0;
char* result = abi::__cxa_demangle( name, NULL, &size, &status );
struct guard
{
explicit guard( char* p )
: p_( p )
{}
~guard()
{
free( p_ );
}
private:
char* p_;
} g( result );
if( result )
return result;
#endif
return name;
}
inline std::string type_name( const std::type_info& info )
{
const std::string name = type_full_name( info );
std::size_t p = name.rfind( "::" );
if( p != std::string::npos )
return name.substr( p + 2 );
p = name.rfind( " " );
if( p != std::string::npos )
return name.substr( p + 1 );
return name;
}
}
}
#endif // MOCK_TYPE_NAME_HPP_INCLUDED