typeInfo.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Typedef
28  Foam::typeInfo
29 
30 Description
31  Basic run-time type information using word as the type's name.
32  Used to enhance the standard RTTI to cover I/O.
33 
34  The user can get the type's type name using the type info access function
35  \code
36  type()
37  \endcode
38 
39  The reference type cast template function:
40  \code
41  refCast<Type>(r)
42  \endcode
43  wraps dynamic_cast to handle failed casts and generate a FatalError.
44 
45  The isA function:
46  \code
47  isA<Type>(obj)
48  \endcode
49  returns const pointer to the cast object, nullptr if cast is not possible
50  (can be tested as a bool).
51 
52 \*---------------------------------------------------------------------------*/
53 
54 #ifndef Foam_typeInfo_H
55 #define Foam_typeInfo_H
56 
57 #include "error.H"
58 #include "className.H"
59 #include <typeinfo>
60 
61 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
62 
63 // Declarations (for use in header files)
64 
65 //- Declare a ClassNameNoDebug() with extra virtual type info
66 #define TypeNameNoDebug(TypeNameString) \
67  ClassNameNoDebug(TypeNameString); \
68  virtual const word& type() const { return typeName; }
69 
70 //- Declare a ClassName() with extra virtual type info
71 #define TypeName(TypeNameString) \
72  ClassName(TypeNameString); \
73  virtual const word& type() const { return typeName; }
74 
75 
76 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
77 
78 namespace Foam
79 {
80 
81 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
82 
83 //- Check if dynamic_cast to \c Type is possible.
84 // The template types should \em not include any \c const qualifier.
85 // \returns const pointer to cast object, nullptr if cast is not possible
86 template<class Type, class U>
87 inline const Type* isA(const U& obj)
88 {
89  const U* p = &obj;
90  return dynamic_cast<const Type*>(p);
91 }
92 
93 
94 //- Check if typeid of the object and \c Type are identical
95 template<class Type, class U>
96 inline bool isType(const U& obj)
97 {
98  return typeid(Type) == typeid(obj);
99 }
100 
101 
102 //- A dynamic_cast (for references) that generates FatalError on failed casts
103 // Respects the constness of the template types.
104 template<class Type, class U>
105 inline Type& dynamicCast(U& obj)
106 {
107  U* p = &obj;
108  Type* casted = dynamic_cast<Type*>(p);
109 
110  if (!casted)
111  {
113  << "Attempt to cast type " << typeid(U).name()
114  << " to type " << typeid(Type).name()
115  << abort(FatalError);
116  }
117 
118  return *casted;
119 }
120 
121 
122 //- A dynamic_cast (for references) that generates FatalIOError on failed casts
123 // Respects the constness of the template types.
124 template<class Type, class U>
125 inline Type& dynamicCast(U& obj, const dictionary& dict)
126 {
127  U* p = &obj;
128  Type* casted = dynamic_cast<Type*>(p);
129 
130  if (!casted)
131  {
133  << "Attempt to cast type " << typeid(U).name()
134  << " to type " << typeid(Type).name()
135  << abort(FatalIOError);
136  }
137 
138  return *casted;
139 }
140 
141 
142 //- A dynamic_cast (for references).
143 //- Generates a FatalError on failed casts and uses the virtual type()
144 //- method for error messages.
145 // Respects the constness of the template types.
146 template<class Type, class U>
147 inline Type& refCast(U& obj)
148 {
149  U* p = &obj;
150  Type* casted = dynamic_cast<Type*>(p);
151 
152  if (!casted)
153  {
155  << "Attempt to cast type " << obj.type()
156  << " to type " << Type::typeName
157  << abort(FatalError);
158  }
160  return *casted;
161 }
162 
163 
164 //- A dynamic_cast (for references) that generates FatalIOError on failed casts,
165 //- uses the virtual type() method for error messages.
166 // Respects the constness of the template types.
167 template<class Type, class U>
168 inline Type& refCast(U& obj, const dictionary& dict)
169 {
170  U* p = &obj;
171  Type* casted = dynamic_cast<Type*>(p);
172 
173  if (!casted)
174  {
176  << "Attempt to cast type " << obj.type()
177  << " to type " << Type::typeName
178  << abort(FatalIOError);
179  }
180 
181  return *casted;
182 }
184 
185 //- A dynamic_cast (for references) that generates FatalError on failed casts,
186 //- uses the virtual type() method for error messages.
187 //- The index can be used to convey additional context.
188 // Respects the constness of the template types.
189 template<class Type, class U>
190 inline Type& refCast(U& obj, const label index)
191 {
192  U* p = &obj;
193  Type* casted = dynamic_cast<Type*>(p);
194 
195  if (!casted)
196  {
198  << "Attempt to cast type " << obj.type()
199  << " to type " << Type::typeName
200  << " at index " << index
201  << abort(FatalError);
202  }
203 
204  return *casted;
205 }
206 
207 
208 //- Test if dynamic_cast to Type is possible, as a functor
209 template<class Type>
210 struct isAOp
211 {
212  template<class U>
213  bool operator()(const U& obj) const
214  {
215  return isA<Type, U>(obj);
216  }
217 };
218 
219 
220 //- Test if typeid is identical to the Type, as a functor
221 template<class Type>
222 struct isTypeOp
223 {
224  template<class U>
225  bool operator()(const U& obj) const
226  {
227  return isType<Type, U>(obj);
228  }
229 };
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
234 } // End namespace Foam
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 #endif
239 
240 // ************************************************************************* //
dictionary dict
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
Type & refCast(U &obj)
A dynamic_cast (for references). Generates a FatalError on failed casts and uses the virtual type() m...
Definition: typeInfo.H:159
bool operator()(const U &obj) const
Definition: typeInfo.H:247
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
bool isType(const U &obj)
Check if typeid of the object and Type are identical.
Definition: typeInfo.H:99
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Type & dynamicCast(U &obj)
A dynamic_cast (for references) that generates FatalError on failed casts.
Definition: typeInfo.H:111
U
Definition: pEqn.H:72
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Test if dynamic_cast to Type is possible, as a functor.
Definition: typeInfo.H:230
const Type * isA(const U &obj)
Check if dynamic_cast to Type is possible.
Definition: typeInfo.H:88
volScalarField & p
bool operator()(const U &obj) const
Definition: typeInfo.H:233
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...