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 
22 #ifndef __typeInfo_hpp__
23 #define __typeInfo_hpp__
24 
25 #include <typeinfo>
26 #include <cxxabi.h>
27 
28 #include "bTypes.hpp"
29 
30 
31 #define has_static_member(name) \
32  template<typename, typename> \
33  struct has_static_member_##name; \
34  \
35  template<typename testType, typename Ret, typename... Args> \
36  struct has_static_member_##name<testType, Ret(Args...)> { \
37  template<typename U, U> 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 
47 
48 #define TypeInfo(tName) \
49  inline static word TYPENAME() {return tName; } \
50  virtual word typeName() const {return TYPENAME();}
51 
52 #define TypeInfoNV(tName) \
53  inline static word TYPENAME() {return tName; } \
54  word typeName() const {return TYPENAME();}
55 
56 
57 #define TypeInfoTemplate(tName, Type) \
58  has_static_member(TYPENAME); \
59  inline static word TYPENAME() \
60  { \
61  if constexpr( has_static_member_TYPENAME<Type,word(void)>::value) \
62  { return word(tName)+"<"+Type::TYPENAME()+">";} \
63  else \
64  return word(tName)+"<"+basicTypeName<Type>()+">"; \
65  return "noTYPE"; \
66  } \
67  virtual word typeName() const { return TYPENAME();}
68 
69 #define TypeInfoTemplate2(tName, Type1, Type2) \
70  has_static_member(TYPENAME); \
71  inline static word TYPENAME() \
72  { \
73  if constexpr( has_static_member_TYPENAME<Type1,word(void)>::value) \
74  { return word(tName)+"<"+Type1::TYPENAME()+","+Type2::TYPENAME()+">";} \
75  else \
76  return word(tName)+"<"+basicTypeName<Type1>()+","+Type2::TYPENAME()+">";\
77  return "noTYPE"; \
78  } \
79  virtual word typeName() const { return TYPENAME();}
80 
81 #define TypeInfoTemplate3(tName, Type1, Type2, Type3) \
82  inline static word TYPENAME() \
83  { \
84  return word(tName)+"<"+Type1::TYPENAME()+","+Type2::TYPENAME()+","+Type3::TYPENAME()+">";\
85  } \
86  virtual word typeName() const { return TYPENAME();}
87 
88 // this is the non-virtual version
89 #define TypeInfoTemplateNV(tName, Type) \
90  has_static_member(TYPENAME); \
91  inline static word TYPENAME() \
92  { \
93  if constexpr( has_static_member_TYPENAME<Type,word(void)>::value) \
94  { return word(tName)+"<"+Type::TYPENAME()+">";} \
95  else \
96  return word(tName)+"<"+basicTypeName<Type>()+">"; \
97  return "noTYPE"; \
98  } \
99  inline word typeName() const { return TYPENAME();}
100 
101 
102 #define TypeInfoTemplateNV2(tName, Type, tName2) \
103  has_static_member(TYPENAME); \
104  inline static word TYPENAME() \
105  { \
106  if constexpr ( has_static_member_TYPENAME<Type,word(void)>::value) \
107  { return word(tName)+"<"+Type::TYPENAME()+","+word(tName2)+">";} \
108  else \
109  return word(tName)+"<"+basicTypeName<Type>()+","+word(tName2)+">"; \
110  return "noTYPE"; \
111  } \
112  inline word typeName() const { return TYPENAME();}
113 
114 
115 
116 
117 namespace pFlow
118 {
119 
120  template <typename T>
122  {
123  int status;
124  auto& ti = typeid(T);
125  char* realname = abi::__cxa_demangle(ti.name(), 0, 0, &status);
126  word name(realname);
127  free(realname);
128  return name;
129  }
130 
131  template<>
132  inline word basicTypeName<word>(){ return "word"; }
133 
134  template<>
135  inline word basicTypeName<int64>(){ return "int64"; }
136 
137  template<>
138  inline word basicTypeName<int32>(){ return "int32"; }
139 
140  template<>
141  inline word basicTypeName<int16>(){ return "int16"; }
142 
143  template<>
144  inline word basicTypeName<int8>(){ return "int8"; }
145 
146  template<>
147  inline word basicTypeName<label>(){ return "label"; }
148 
149  template<>
150  inline word basicTypeName<uint32>(){ return "uint32"; }
151 
152  template<>
153  inline word basicTypeName<real>(){ return "real"; }
154 
155 
156  // compare the overriden typeName of object with concrete TYPENAME
157  // of Type1
158  template <typename Type1, typename Type2>
159  bool checkType(Type2* object)
160  {
161  return word(Type1::TYPENAME()) == object->typeName();
162  }
163 
164  template <typename Type1, typename Type2>
165  bool checkType(Type2& object)
166  {
167  return word(Type1::TYPENAME()) == object.typeName();
168  }
169 
170 
171 
172 } // pFlow
173 
174 #endif
pFlow::checkType
bool checkType(Type2 *object)
Definition: typeInfo.hpp:159
pFlow::basicTypeName
word basicTypeName()
Definition: typeInfo.hpp:121
pFlow::basicTypeName< uint32 >
word basicTypeName< uint32 >()
Definition: typeInfo.hpp:150
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::basicTypeName< int8 >
word basicTypeName< int8 >()
Definition: typeInfo.hpp:144
bTypes.hpp
pFlow
Definition: demComponent.hpp:28
pFlow::basicTypeName< int64 >
word basicTypeName< int64 >()
Definition: typeInfo.hpp:135
pFlow::basicTypeName< label >
word basicTypeName< label >()
Definition: typeInfo.hpp:147
pFlow::basicTypeName< int16 >
word basicTypeName< int16 >()
Definition: typeInfo.hpp:141
pFlow::basicTypeName< real >
word basicTypeName< real >()
Definition: typeInfo.hpp:153
pFlow::basicTypeName< word >
word basicTypeName< word >()
Definition: typeInfo.hpp:132
pFlow::basicTypeName< int32 >
word basicTypeName< int32 >()
Definition: typeInfo.hpp:138