demangle_util.h
Go to the documentation of this file.
1 #ifndef DEMANGLE_UTIL_H
2 #define DEMANGLE_UTIL_H
3 
4 #include <string>
5 
6 
7 #if defined( __clang__ ) && defined( __has_include )
8 # if __has_include(<cxxabi.h>)
9 # define HAS_CXXABI_H
10 # endif
11 #elif defined( __GLIBCXX__ ) || defined( __GLIBCPP__ )
12 # define HAS_CXXABI_H
13 #endif
14 
15 #if defined( HAS_CXXABI_H )
16 # include <cxxabi.h>
17 # include <cstdlib>
18 # include <cstddef>
19 #endif
20 
21 namespace BT
22 {
23 
24 inline char const * demangle_alloc( char const * name ) noexcept;
25 inline void demangle_free( char const * name ) noexcept;
26 
28 {
29 private:
30  char const * m_p;
31 
32 public:
33  explicit scoped_demangled_name( char const * name ) noexcept :
34  m_p( demangle_alloc( name ) )
35  {
36  }
37 
39  {
40  demangle_free( m_p );
41  }
42 
43  char const * get() const noexcept
44  {
45  return m_p;
46  }
47 
50 };
51 
52 
53 #if defined( HAS_CXXABI_H )
54 
55 inline char const * demangle_alloc( char const * name ) noexcept
56 {
57  int status = 0;
58  std::size_t size = 0;
59  return abi::__cxa_demangle( name, NULL, &size, &status );
60 }
61 
62 inline void demangle_free( char const * name ) noexcept
63 {
64  std::free( const_cast< char* >( name ) );
65 }
66 
67 #else
68 
69 inline char const * demangle_alloc( char const * name ) noexcept
70 {
71  return name;
72 }
73 
74 inline void demangle_free( char const * ) noexcept
75 {
76 }
77 
78 inline std::string demangle( char const * name )
79 {
80  return name;
81 }
82 
83 #endif
84 
85 inline std::string demangle(const std::type_info* info)
86 {
87  if (!info)
88  {
89  return "void";
90  }
91  if (info == &typeid(std::string))
92  {
93  return "std::string";
94  }
95  scoped_demangled_name demangled_name(info->name());
96  char const* const p = demangled_name.get();
97  if (p)
98  {
99  return p;
100  }
101  else
102  {
103  return info->name();
104  }
105 }
106 
107 inline std::string demangle(const std::type_info& info)
108 {
109  return demangle(&info);
110 }
111 
112 } // namespace BT
113 
114 #undef HAS_CXXABI_H
115 
116 #endif // DEMANGLE_UTIL_H
char const * get() const noexcept
Definition: demangle_util.h:43
scoped_demangled_name & operator=(scoped_demangled_name const &)=delete
scoped_demangled_name(char const *name) noexcept
Definition: demangle_util.h:33
std::string demangle(char const *name)
Definition: demangle_util.h:78
char const * demangle_alloc(char const *name) noexcept
Definition: demangle_util.h:69
~scoped_demangled_name() noexcept
Definition: demangle_util.h:38
void demangle_free(char const *name) noexcept
Definition: demangle_util.h:74


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04