typeInfo.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 InNamespace
25  Foam
26 
27 Description
28  Basic run-time type information using word as the type's name.
29  Used to enhance the standard RTTI to cover I/O.
30 
31  The user can get the type's type name using the type info access function
32  \code
33  type()
34  \endcode
35 
36  The reference type cast template function:
37  \code
38  refCast<T>(r)
39  \endcode
40 
41  wraps dynamic_cast to handle the bad_cast exception and generate a
42  FatalError.
43 
44  The isA function:
45  \code
46  isA<T>(r)
47  \endcode
48 
49  returns true if r is of type T or derived from type T.
50 
51 \*---------------------------------------------------------------------------*/
52 
53 #ifndef typeInfo_H
54 #define typeInfo_H
55 
56 #include "error.H"
57 #include "className.H"
58 #include "nullObject.H"
59 #include <type_traits>
60 #include <typeinfo>
61 
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 
64 // Declarations (for use in header files)
65 
66 //- Declare a ClassNameNoDebug() with extra virtual type info
67 #define TypeNameNoDebug(TypeNameString) \
68  ClassNameNoDebug(TypeNameString); \
69  virtual const word& type() const { return typeName; } \
70  template<class Name> \
71  word typedName(Name name) const { return (type() + ':') + name; }
72 
73 //- Declare a ClassName() with extra virtual type info
74 #define TypeName(TypeNameString) \
75  ClassName(TypeNameString); \
76  virtual const word& type() const { return typeName; } \
77  template<class Name> \
78  word typedName(Name name) const { return (type() + ':') + name; }
79 
80 //- Declare a FunctionName() with extra virtual type info
81 #define FunctionTypeName(TypeNameString) \
82  FunctionName(TypeNameString); \
83  virtual const word& type() const { return typeName; } \
84  template<class Name> \
85  word typedName(Name name) const { return (type() + ':') + name; }
86 
87 
88 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
89 
90 namespace Foam
91 {
92 
93 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
94 
95 //- Reference type cast template function,
96 // wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
97 template<class To, class From>
98 inline To& dynamicCast(From& r)
99 {
100  try
101  {
102  return dynamic_cast<To&>(r);
103  }
104  catch (const std::bad_cast&)
105  {
107  << "Attempt to cast type " << typeid(r).name()
108  << " to type " << typeid(To).name()
109  << abort(FatalError);
110 
111  return dynamic_cast<To&>(r);
112  }
113 }
114 
115 
116 //- Reference type cast template function,
117 // wraps dynamic_cast to handle bad_cast exception and generate a null
118 // reference.
119 template<class To, class From>
120 inline To& dynamicCastNull(From& r)
121 {
122  if (isNull(r))
123  {
124  return NullObjectNonConstRef<To>();
125  }
126 
127  try
128  {
129  return dynamic_cast<To&>(r);
130  }
131  catch (const std::bad_cast&)
132  {
133  return NullObjectNonConstRef<To>();
134  }
135 }
136 
137 
138 //- Reference type cast template function.
139 // As per dynamicCast, but handles type names via the virtual type() method.
140 template<class To, class From>
141 inline To& refCast(From& r)
142 {
143  try
144  {
145  return dynamic_cast<To&>(r);
146  }
147  catch (const std::bad_cast&)
148  {
150  << "Attempt to cast type " << r.type()
151  << " to type " << To::typeName
152  << abort(FatalError);
153 
154  return dynamic_cast<To&>(r);
155  }
156 }
157 
158 
159 //- Reference type cast template function,
160 // As dynamicCastNull.
161 template<class To, class From>
162 inline To& refCastNull(From& r)
163 {
164  return dynamicCastNull<To, From>(r);
165 }
166 
167 
168 //- Check the typeid
169 template<class TestType, class Type>
170 inline bool isType(const Type& t)
171 {
172  return typeid(t) == typeid(TestType);
173 }
174 
175 
176 //- Check if a dynamic_cast to typeid is possible
177 template<class TestType, class Type>
178 inline bool isA(const Type& t)
179 {
180  const Type* tPtr = &t;
181  return dynamic_cast<const TestType*>(tPtr);
182 }
183 
184 
185 //- Return the name of the object within the given type
186 // as <typeName>:<name>
187 template<class TypeName, class Name>
188 inline word typedName(Name name)
189 {
190  return (TypeName::typeName + ':') + name;
191 }
192 
193 
194 //- Determine whether a type is a reference
195 template<class Type>
196 constexpr bool isRef = std::is_reference<Type>::value;
197 
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 } // End namespace Foam
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 
205 #endif
206 
207 // ************************************************************************* //
A class for handling words, derived from string.
Definition: word.H:63
Macro definitions for declaring ClassName(), NamespaceName(), etc.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
Namespace for OpenFOAM.
To & refCastNull(From &r)
Reference type cast template function,.
Definition: typeInfo.H:162
To & refCast(From &r)
Reference type cast template function.
Definition: typeInfo.H:141
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bool isType(const Type &t)
Check the typeid.
Definition: typeInfo.H:170
To & dynamicCast(From &r)
Reference type cast template function,.
Definition: typeInfo.H:98
bool isA(const Type &t)
Check if a dynamic_cast to typeid is possible.
Definition: typeInfo.H:178
constexpr bool isRef
Determine whether a type is a reference.
Definition: typeInfo.H:196
word typedName(Name name)
Return the name of the object within the given type.
Definition: typeInfo.H:188
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
bool isNull(const T &t)
Return true if t is a reference to the nullObject of type T.
Definition: nullObjectI.H:58
To & dynamicCastNull(From &r)
Reference type cast template function,.
Definition: typeInfo.H:120
error FatalError