www.cemf.ir
typeInfo.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #ifndef __typeInfo_hpp__
22 #define __typeInfo_hpp__
23 
24 #include <cxxabi.h>
25 #include <typeinfo>
26 
27 #include "bTypes.hpp"
28 
29 #define has_static_member(name) \
30  template <typename, typename> \
31  struct has_static_member_##name; \
32  \
33  template <typename testType, typename Ret, typename... Args> \
34  struct has_static_member_##name<testType, Ret(Args...)> \
35  { \
36  template <typename U, U> \
37  struct Check; \
38  \
39  template <typename U> \
40  static std::true_type Test(Check<Ret (*)(Args...), &U::name> *); \
41  template <typename U> \
42  static std::false_type Test(...); \
43  static const bool value = decltype(Test<testType>(0))::value; \
44  };
45 
46 namespace pFlow
47 {
48 //
49 template <typename T>
51 {
52  has_static_member(TYPENAME);
53 
54  static constexpr bool hasMember()
55  {
56  return has_static_member_TYPENAME<T, word(void)>::value;
57  }
58 };
59 
60 template <typename T>
61 inline
63 {
64  int status;
65  auto &ti = typeid(T);
66  char *realname = abi::__cxa_demangle(ti.name(), nullptr, nullptr, &status);
67  word name(realname);
68  free(realname);
69  return name;
70 }
71 
72 template <>
74 {
75  return "word";
76 }
77 
78 template <>
80 {
81  return "int64";
82 }
83 
84 template <>
86 {
87  return "int32";
88 }
89 
90 template <>
92 {
93  return "int8";
94 }
95 
96 template <>
98 {
99  return "uint64";
100 }
101 
102 template <>
104 {
105  return "uint32";
106 }
107 
108 template <>
110 {
111  return "uint8";
112 }
113 
114 template <>
116 {
117  return "real";
118 }
119 
120 template <typename T>
121 inline
123 {
124  if constexpr (checkStatic<T>::hasMember())
125  {
126  return T::TYPENAME();
127  }
128  else
129  {
130  return basicTypeName<T>();
131  }
132 }
133 
134 template <typename T>
135 inline
136 word getTypeName(const T &)
137 {
138  if constexpr (checkStatic<T>::hasMember())
139  {
140  return T::TYPENAME();
141  }
142  else
143  {
144  return basicTypeName<T>();
145  }
146 }
147 
148 // compare the overriden typeName of object with concrete TYPENAME
149 // of Type1
150 template <typename Type1, typename Type2>
151 bool checkType(Type2 *object)
152 {
153  return getTypeName<Type1>() == object->typeName();
154 }
155 
156 template <typename Type1, typename Type2>
157 bool checkType(Type2 &object)
158 {
159  return getTypeName<Type1>() == object.typeName();
160 }
161 } // namespace pFlow
162 
163 #define QuadrupleTypeInfoNV(T) \
164  inline static word TYPENAME() \
165  { \
166  return getTypeName<T>()+"x4"; \
167  } \
168  word typeName() const \
169  { \
170  return TYPENAME(); \
171  }
172 
173 #define TripleTypeInfoNV(T) \
174  inline static word TYPENAME() \
175  { \
176  return getTypeName<T>()+"x3"; \
177  } \
178  word typeName() const \
179  { \
180  return TYPENAME(); \
181  }
182 
183 #define ClassInfo(tName) \
184  inline static word TYPENAME() \
185  { \
186  return tName; \
187  }
188 
189 #define TypeInfo(tName) \
190  inline static word TYPENAME() \
191  { \
192  return tName; \
193  } \
194  virtual word typeName() const \
195  { \
196  return TYPENAME(); \
197  }
198 
199 #define TypeInfoNV(tName) \
200  inline static word TYPENAME() \
201  { \
202  return tName; \
203  } \
204  word typeName() const \
205  { \
206  return TYPENAME(); \
207  }
208 
209 #define TypeInfoTemplate11(tName, Type) \
210  inline static word TYPENAME() \
211  { \
212  return word(tName) + "<" + getTypeName<Type>() + ">"; \
213  } \
214  virtual word typeName() const \
215  { \
216  return TYPENAME(); \
217  }
218 
219 #define TypeInfoTemplate12(tName, Type1, Type2) \
220  inline static word TYPENAME() \
221  { \
222  return word(tName) + "<" + getTypeName<Type1>() + "," + \
223  getTypeName<Type2>() + ">"; \
224  } \
225  virtual word typeName() const \
226  { \
227  return TYPENAME(); \
228  }
229 
230 #define TypeInfoTemplate13(tName, Type1, Type2, Type3) \
231  inline static word TYPENAME() \
232  { \
233  return word(tName) + "<" + getTypeName<Type1>() + "," + \
234  getTypeName<Type2>() + "," + getTypeName<Type3>() + ">"; \
235  } \
236  virtual word typeName() const \
237  { \
238  return TYPENAME(); \
239  }
240 
241 // this is the non-virtual version
242 #define TypeInfoTemplateNV11(tName, Type) \
243  inline static word TYPENAME() \
244  { \
245  return word(tName) + "<" + getTypeName<Type>() + ">"; \
246  } \
247  inline word typeName() const \
248  { \
249  return TYPENAME(); \
250  }
251 
252 #define TypeInfoTemplateNV111(tName, Type, tName2) \
253  inline static word TYPENAME() \
254  { \
255  return word(tName) + "<" + getTypeName<Type>() + "," + word(tName2) + \
256  ">"; \
257  } \
258  inline word typeName() const \
259  { \
260  return TYPENAME(); \
261  }
262 
263 #define TypeInfoTemplate111(tName, Type, tName2) \
264  inline static word TYPENAME() \
265  { \
266  return word(tName) + "<" + getTypeName<Type>() + "," + word(tName2) + \
267  ">"; \
268  } \
269  virtual word typeName() const \
270  { \
271  return TYPENAME(); \
272  }
273 
274 #define TypeInfoTemplate211(tBase, tName1, Type, tName3) \
275  inline static word TYPENAME() \
276  { \
277  return word(tBase) + "<" + word(tName1) + "," + getTypeName<Type>() + \
278  "," + word(tName3) + ">"; \
279  } \
280  virtual word typeName() const \
281  { \
282  return TYPENAME(); \
283  }
284 
285 #define TypeInfoTemplate22(tBase, tName1, Type1, Type2) \
286  inline static word TYPENAME() \
287  { \
288  return word(tBase) + "<" + word(tName1) + "," + getTypeName<Type1>() + \
289  "," + getTypeName<Type2>() + ">"; \
290  } \
291  virtual word typeName() const \
292  { \
293  return TYPENAME(); \
294  }
295 
296 #endif
pFlow::basicTypeName< uint8 >
word basicTypeName< uint8 >()
Definition: typeInfo.hpp:109
pFlow::checkType
bool checkType(Type2 *object)
Definition: typeInfo.hpp:151
pFlow::basicTypeName
word basicTypeName()
Definition: typeInfo.hpp:62
pFlow::basicTypeName< uint32 >
word basicTypeName< uint32 >()
Definition: typeInfo.hpp:103
pFlow::basicTypeName< uint64 >
word basicTypeName< uint64 >()
Definition: typeInfo.hpp:97
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::checkStatic::hasMember
static constexpr bool hasMember()
Definition: typeInfo.hpp:54
has_static_member
#define has_static_member(name)
Definition: typeInfo.hpp:29
pFlow::basicTypeName< int8 >
word basicTypeName< int8 >()
Definition: typeInfo.hpp:91
bTypes.hpp
pFlow
Definition: demGeometry.hpp:27
pFlow::basicTypeName< int64 >
word basicTypeName< int64 >()
Definition: typeInfo.hpp:79
pFlow::getTypeName
word getTypeName()
Definition: typeInfo.hpp:122
pFlow::basicTypeName< real >
word basicTypeName< real >()
Definition: typeInfo.hpp:115
pFlow::basicTypeName< word >
word basicTypeName< word >()
Definition: typeInfo.hpp:73
pFlow::basicTypeName< int32 >
word basicTypeName< int32 >()
Definition: typeInfo.hpp:85
pFlow::checkStatic::has_static_member_TYPENAME
Definition: typeInfo.hpp:52
pFlow::checkStatic
Definition: typeInfo.hpp:50