demangle.h
Go to the documentation of this file.
1 
2 #ifndef TTG_DEMANGLE_H_INCLUDED
3 #define TTG_DEMANGLE_H_INCLUDED
4 
5 #include <cxxabi.h>
6 #include <string>
7 #include <typeinfo>
8 #include <memory>
9 #define HAVE_CXA_DEMANGLE
10 #ifdef HAVE_CXA_DEMANGLE
11 
12 namespace ttg {
13  namespace detail {
14  template <typename T>
15  static std::string demangled_type_name(T *x = nullptr) {
16  const char *name = nullptr;
17  if constexpr (std::is_void_v<T>)
18  name = "void";
19  else
20  name = (x != nullptr) ? typeid(*x).name() : // this works for polymorphic types
21  typeid(T).name();
22  static size_t buf_size = 1024;
23  static std::unique_ptr<char, decltype(std::free) *> buf{reinterpret_cast<char *>(malloc(sizeof(char) * buf_size)),
24  std::free};
25  int status;
26  char *res = abi::__cxa_demangle(name, buf.get(), &buf_size, &status);
27  if (status != 0 || res == nullptr) {
28  return name;
29  } else {
30  if (res != buf.get()) {
31  buf.release();
32  buf = std::unique_ptr<char, decltype(std::free) *>{res, std::free};
33  }
34  return res;
35  }
36  }
37 #else
38 template <typename T>
39 std::string demangled_type_name(T* x = nullptr) {
40  const char* name = (x != nullptr) ? typeid(*x).name() : // this works for polymorphic types
41  typeid(T).name();
42  return std::string(name);
43 }
44 #endif
45  } // namespace detail
46 } // namespace ttg
47 
48 #endif // TTG_DEMANGLE_H_INCLUDED
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8