BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
demangle_util.h
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 <cstddef>
18#include <cstdlib>
19
20#include <cxxabi.h>
21#endif
22
23namespace BT
24{
25inline char const* demangle_alloc(char const* name) noexcept;
26inline void demangle_free(char const* name) noexcept;
27
29{
30private:
31 char const* m_p;
32
33public:
34 explicit scoped_demangled_name(char const* name) noexcept : m_p(demangle_alloc(name))
35 {}
36
37 ~scoped_demangled_name() noexcept
38 {
39 demangle_free(m_p);
40 }
41
42 char const* get() const noexcept
43 {
44 return m_p;
45 }
46
47 scoped_demangled_name(scoped_demangled_name const&) = delete;
48 scoped_demangled_name& operator=(scoped_demangled_name const&) = delete;
49 scoped_demangled_name(scoped_demangled_name&&) = delete;
50 scoped_demangled_name& operator=(scoped_demangled_name&&) = delete;
51};
52
53#if defined(HAS_CXXABI_H)
54
55inline 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
62inline void demangle_free(char const* name) noexcept
63{
64 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
65 std::free(const_cast<char*>(name));
66}
67
68#else
69
70inline char const* demangle_alloc(char const* name) noexcept
71{
72 return name;
73}
74
75inline void demangle_free(char const*) noexcept
76{}
77
78inline std::string demangle(char const* name)
79{
80 return name;
81}
82
83#endif
84
85inline std::string demangle(const std::type_index& index)
86{
87 if(index == typeid(std::string))
88 {
89 return "std::string";
90 }
91 if(index == typeid(std::string_view))
92 {
93 return "std::string_view";
94 }
95 if(index == typeid(std::chrono::seconds))
96 {
97 return "std::chrono::seconds";
98 }
99 if(index == typeid(std::chrono::milliseconds))
100 {
101 return "std::chrono::milliseconds";
102 }
103 if(index == typeid(std::chrono::microseconds))
104 {
105 return "std::chrono::microseconds";
106 }
107
108 scoped_demangled_name demangled_name(index.name());
109 char const* const p = demangled_name.get();
110 if(p != nullptr)
111 {
112 return p;
113 }
114 return index.name();
115}
116
117inline std::string demangle(const std::type_info& info)
118{
119 return demangle(std::type_index(info));
120}
121
122} // namespace BT
123
124#undef HAS_CXXABI_H
125
126#endif // DEMANGLE_UTIL_H
Definition: demangle_util.h:29
Definition: action_node.h:24