DUNE-ACFEM (unstable)

typestring.hh
1 #ifndef __DUNE_ACFEM_COMMON_TYPESTRING_HH__
2 #define __DUNE_ACFEM_COMMON_TYPESTRING_HH__
3 
4 #if HAVE_CONFIG_H
5 # include <config.h>
6 #endif
7 
8 #include <limits>
9 #include <regex>
10 #include <iostream>
11 #if HAVE_CXA_DEMANGLE
12 # include <typeinfo>
13 # include <cxxabi.h>
14 # include <cassert>
15 #else
16 # warning Pretty-printing of class-names will not work, C++ demangler not available.
17 #endif
18 
19 #include "types.hh"
20 
21 namespace Dune {
22 
23  namespace ACFem {
24 
29  template<class T>
30  std::string cvString(T&& t)
31  {
32  std::string result;
33  if (std::is_lvalue_reference<T>::value) {
34  result += "&";
35  }
36  if (std::is_rvalue_reference<T>::value) {
37  result += "&&";
38  }
39  if (std::is_pointer<T>::value) {
40  result += "*";
41  }
42  if (std::is_const<T>::value) {
43  result += "const";
44  }
45  return result;
46  }
47 
48  template<class T>
49  std::string demangle(T&& t)
50  {
51 #if HAVE_CXA_DEMANGLE
52  auto *cname = typeid(std::forward<T>(t)).name();
53  int status;
54  auto *demangled = abi::__cxa_demangle(cname, 0, 0, &status);
55  assert(status == 0);
56  auto name = std::string(demangled);
57  delete demangled;
58 #else
59  name = std::string("Don't know how to generate pretty type-names with this compiler.");
60 #endif
61  return name;
62  }
63 
65  template<class T>
66  class TypeString
67  {
68  public:
69  TypeString(const std::decay_t<T>* arg = nullptr, bool isRValueRef = false)
70  {
71 #if HAVE_CXA_DEMANGLE
72  auto *name = typeid(arg).name();
73  int status;
74  auto *demangled = abi::__cxa_demangle(name, 0, 0, &status);
75  if (status != 0) {
76  std::clog << "*** Unable to demangle symbol(" << status << ")" << std::endl
77  << name << std::endl
78  << "***" << std::endl;
79  }
80  assert(status == 0);
81  name_ = std::string(demangled);
82  delete demangled;
83  name_ = name_.substr(0, name_.size() - strlen(" const*"));
84  if (std::is_rvalue_reference<T>::value) {
85  name_ += "&&";
86  } else if (std::is_lvalue_reference<T>::value) {
87  name_ += "&";
88  }
89  if (std::is_const<std::remove_reference_t<T> >::value) {
90  name_ += " const";
91  }
92 #else
93  name_ = std::string("Don't know how to generate pretty type-names with this compiler.");
94 #endif
95  }
96  TypeString(T&& arg)
97  : TypeString(&arg, std::is_rvalue_reference<decltype(arg)>::value)
98  {}
99 
106  std::string name(bool postProcess = true) const
107  {
108  if (postProcess) {
109  auto result = name_;
110 
111  // strip name space and SFINAE dummy parameters
112  result = std::regex_replace(result, std::regex("ACFem::|Dune::|Fem::|Expressions::|MPL::|FunctionExpressions::|GridFunction::|std::|Tensor::|TypedValue::|PDEModel::|ModelInstrospection::|[(]anonymous namespace[)]::|impl::|__[0-9]+::|(>)(,( )void)+(>)|, void"), "$1$3$4");
113 
114  // also strip suffixes indicating the integer type.
115  result = std::regex_replace(result, std::regex("([0-9]+)[uU]?[lL]?"), "$1");
116 
117  // short cut for canonical ALU leaf grid
118  result = std::regex_replace(result, std::regex("AdaptiveLeafGridPart<(ALUGrid)<([0-9]), ([0-9]), [(]ALUGridElementType[)]0, [(]ALUGridRefinementType[)]0, ALUGridMPIComm>, [(]PartitionIteratorType[)][0-9]+, (false|true)>"), "$1<$2,$3>");
119 
120  // Short cut for lagrange space. Omit grid and storage and just keep degree and dimensions.
121  result = std::regex_replace(result,
122  std::regex("LagrangeDiscreteFunctionSpace<FunctionSpace<double, double, ([0-9]+), ([0-9]+)>,.+?, ([0-9]+), CachingStorage>"),
123  "LagrangeSpace<$3, <$1,$2> >");
124 
125  // return the entire mess
126  return result;
127  } else {
128  return name_;
129  }
130  }
131  protected:
132  std::string name_;
133  };
134 
143  template<class T>
144  auto typeString(T&& t, bool stripNameSpaces = true)
145  {
146  return TypeString<T>(std::forward<T>(t)).name(stripNameSpaces);
147  }
148 
149  template<class T>
150  auto typeString(bool stripNameSpaces = true, const std::remove_reference_t<T>* arg = nullptr)
151  {
152  return TypeString<T>{}.name(stripNameSpaces);
153  }
154 
156 
157  } // ACFem::
158 
159 } // Dune::
160 
161 #endif // __DUNE_ACFEM_COMMON_TYPESTRING_HH__
A class constructing the name of another type.
Definition: typestring.hh:67
std::string name(bool postProcess=true) const
If implemented for the current compiler return the name of the given type.
Definition: typestring.hh:106
auto typeString(T &&t, bool stripNameSpaces=true)
Generator for TypeString.
Definition: typestring.hh:144
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (Apr 26, 22:29, 2024)