demangle_util.h
Go to the documentation of this file.
1 #ifndef DEMANGLE_UTIL_H
2 #define DEMANGLE_UTIL_H
3 
4 #include <chrono>
5 #include <string>
6 #include <typeindex>
7 
8 #if defined(__clang__) && defined(__has_include)
9 #if __has_include(<cxxabi.h>)
10 #define HAS_CXXABI_H
11 #endif
12 #elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
13 #define HAS_CXXABI_H
14 #endif
15 
16 #if defined(HAS_CXXABI_H)
17 #include <cxxabi.h>
18 #include <cstdlib>
19 #include <cstddef>
20 #endif
21 
22 namespace BT
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 : m_p(demangle_alloc(name))
34  {}
35 
37  {
39  }
40 
41  char const* get() const noexcept
42  {
43  return m_p;
44  }
45 
48 };
49 
50 #if defined(HAS_CXXABI_H)
51 
52 inline char const* demangle_alloc(char const* name) noexcept
53 {
54  int status = 0;
55  std::size_t size = 0;
56  return abi::__cxa_demangle(name, NULL, &size, &status);
57 }
58 
59 inline void demangle_free(char const* name) noexcept
60 {
61  std::free(const_cast<char*>(name));
62 }
63 
64 #else
65 
66 inline char const* demangle_alloc(char const* name) noexcept
67 {
68  return name;
69 }
70 
71 inline void demangle_free(char const*) noexcept
72 {}
73 
74 inline std::string demangle(char const* name)
75 {
76  return name;
77 }
78 
79 #endif
80 
81 inline std::string demangle(const std::type_index& index)
82 {
83  if(index == typeid(std::string))
84  {
85  return "std::string";
86  }
87  if(index == typeid(std::string_view))
88  {
89  return "std::string_view";
90  }
91  if(index == typeid(std::chrono::seconds))
92  {
93  return "std::chrono::seconds";
94  }
95  if(index == typeid(std::chrono::milliseconds))
96  {
97  return "std::chrono::milliseconds";
98  }
99  if(index == typeid(std::chrono::microseconds))
100  {
101  return "std::chrono::microseconds";
102  }
103 
104  scoped_demangled_name demangled_name(index.name());
105  char const* const p = demangled_name.get();
106  if(p)
107  {
108  return p;
109  }
110  else
111  {
112  return index.name();
113  }
114 }
115 
116 inline std::string demangle(const std::type_info& info)
117 {
118  return demangle(std::type_index(info));
119 }
120 
121 } // namespace BT
122 
123 #undef HAS_CXXABI_H
124 
125 #endif // DEMANGLE_UTIL_H
cx::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: wildcards.hpp:636
BT
Definition: ex01_wrap_legacy.cpp:29
BT::demangle
std::string demangle(char const *name)
Definition: demangle_util.h:74
BT::scoped_demangled_name::get
char const * get() const noexcept
Definition: demangle_util.h:41
BT::scoped_demangled_name::operator=
scoped_demangled_name & operator=(scoped_demangled_name const &)=delete
BT::scoped_demangled_name
Definition: demangle_util.h:27
BT::demangle_alloc
char const * demangle_alloc(char const *name) noexcept
Definition: demangle_util.h:66
BT::scoped_demangled_name::scoped_demangled_name
scoped_demangled_name(char const *name) noexcept
Definition: demangle_util.h:33
BT::demangle_free
void demangle_free(char const *name) noexcept
Definition: demangle_util.h:71
BT::scoped_demangled_name::~scoped_demangled_name
~scoped_demangled_name() noexcept
Definition: demangle_util.h:36
BT::scoped_demangled_name::m_p
char const * m_p
Definition: demangle_util.h:30
lexyd::p
constexpr auto p
Parses the production.
Definition: production.hpp:127
lexy::_detail::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:184


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07