Lodestar
An integrated real-time control package in C++
TypeName.hpp
1 //
2 // Created by Hamza El-Kebir on 6/12/21.
3 //
4 
5 #ifndef LODESTAR_TYPENAME_HPP
6 #define LODESTAR_TYPENAME_HPP
7 
8 #include <type_traits>
9 #include <typeinfo>
10 #ifndef _MSC_VER
11 # include <cxxabi.h>
12 #endif
13 #include <memory>
14 #include <string>
15 #include <cstdlib>
16 
17 template <class T>
18 std::string
19 type_name()
20 {
21  typedef typename std::remove_reference<T>::type TR;
22  std::unique_ptr<char, void(*)(void*)> own
23  (
24 #ifndef _MSC_VER
25  abi::__cxa_demangle(typeid(TR).name(), nullptr,
26  nullptr, nullptr),
27 #else
28  nullptr,
29 #endif
30  std::free
31  );
32  std::string r = own != nullptr ? own.get() : typeid(TR).name();
33  if (std::is_const<TR>::value)
34  r += " const";
35  if (std::is_volatile<TR>::value)
36  r += " volatile";
37  if (std::is_lvalue_reference<T>::value)
38  r += "&";
39  else if (std::is_rvalue_reference<T>::value)
40  r += "&&";
41  return r;
42 }
43 
44 #endif //LODESTAR_TYPENAME_HPP