Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
TypesUtils.hpp
1 #pragma once
2 
3 #include <Core/CoreMacros.hpp>
4 
5 #ifndef _WIN32
6 # include <cxxabi.h>
7 # include <memory>
8 #else
9 # include <typeinfo>
10 #endif
11 
12 #include <string>
13 
14 #include <Core/Utils/StringUtils.hpp>
15 
16 namespace Ra {
17 namespace Core {
18 namespace Utils {
19 
21 template <typename T>
22 const char* demangleType() noexcept;
23 
25 template <typename T>
26 const char* demangleType( const T& ) noexcept;
27 
28 // TypeList taken and adapted from
29 // https://github.com/AcademySoftwareFoundation/openvdb/blob/master/openvdb/openvdb/TypeList.h
30 // Only took small part of TypeList utilities
31 
32 // forward declarations
33 template <typename... Ts>
34 struct TypeList;
35 
36 namespace TypeListInternal {
37 
42 template <typename ListT, typename... Ts>
43 struct TSAppendImpl;
44 
49 template <typename... Ts, typename... OtherTs>
50 struct TSAppendImpl<TypeList<Ts...>, OtherTs...> {
51  using type = TypeList<Ts..., OtherTs...>;
52 };
53 
58 template <typename... Ts, typename... OtherTs>
59 struct TSAppendImpl<TypeList<Ts...>, TypeList<OtherTs...>> {
60  using type = TypeList<Ts..., OtherTs...>;
61 };
62 
63 } // namespace TypeListInternal
64 
65 template <typename... Ts>
66 struct TypeList {
68  using Self = TypeList;
70  static constexpr size_t Size = sizeof...( Ts );
71 
87  template <typename... TypesToAppend>
88  using Append = typename TypeListInternal::TSAppendImpl<Self, TypesToAppend...>::type;
89 };
90 
91 #ifdef _WIN32
92 // On windows (since MSVC 2019), typeid( T ).name() returns the demangled name
93 template <typename T>
94 const char* demangleType() noexcept {
95  static auto demangled_name = []() {
96  std::string retval { typeid( T ).name() };
97  removeAllInString( retval, "class " );
98  removeAllInString( retval, "struct " );
99  replaceAllInString( retval, ",", ", " );
100  replaceAllInString( retval, "> >", ">>" );
101  return retval;
102  }();
103 
104  return demangled_name.data();
105 }
106 #else
107 template <typename T>
108 const char* demangleType() noexcept {
109  // once per one type
110  static auto demangled_name = []() {
111  int error = 0;
112  std::string retval;
113  char* name = abi::__cxa_demangle( typeid( T ).name(), 0, 0, &error );
114 
115  switch ( error ) {
116  case 0:
117  retval = name;
118  break;
119  case -1:
120  retval = "memory allocation failed";
121  break;
122  case -2:
123  retval = "not a valid mangled name";
124  break;
125  default:
126  retval = "__cxa_demangle failed";
127  break;
128  }
129  std::free( name );
130  removeAllInString( retval, "__1::" ); // or "::__1" ?
131  replaceAllInString( retval, "> >", ">>" );
132  return retval;
133  }();
134 
135  return demangled_name.data();
136 }
137 #endif
138 // calling with instances
139 template <typename T>
140 const char* demangleType( const T& ) noexcept {
141  return demangleType<T>();
142 }
143 
144 } // namespace Utils
145 } // namespace Core
146 } // namespace Ra
Definition: Cage.cpp:3
Append any number of types to a TypeList.
Definition: TypesUtils.hpp:43