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-2022 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 // \returns const pointer to cast object, nullptr if cast is not possible
85 template<class Type, class U>
86 inline const Type* isA(const U& obj)
87 {
88  const U* p = &obj;
89  return dynamic_cast<const Type*>(p);
90 }
91 
92 
93 //- Check if typeid of the object and \c Type are identical
94 template<class Type, class U>
95 inline bool isType(const U& obj)
96 {
97  return typeid(Type) == typeid(obj);
98 }
99 
100 
101 //- A dynamic_cast (for references) that generates FatalError on failed casts
102 template<class Type, class U>
103 inline Type& dynamicCast(U& obj)
104 {
105  U* p = &obj;
106  Type* casted = dynamic_cast<Type*>(p);
107 
108  if (!casted)
109  {
111  << "Attempt to cast type " << typeid(U).name()
112  << " to type " << typeid(Type).name()
113  << abort(FatalError);
114  }
115 
116  return *casted;
117 }
118 
119 
120 //- A dynamic_cast (for references) that generates FatalIOError on failed casts
121 template<class Type, class U>
122 inline Type& dynamicCast(U& obj, const dictionary& dict)
123 {
124  U* p = &obj;
125  Type* casted = dynamic_cast<Type*>(p);
126 
127  if (!casted)
128  {
130  << "Attempt to cast type " << typeid(U).name()
131  << " to type " << typeid(Type).name()
132  << abort(FatalIOError);
133  }
134 
135  return *casted;
136 }
137 
138 
139 //- A dynamic_cast (for references) that generates FatalError on failed casts,
140 //- uses the virtual type() method for error messages.
141 template<class Type, class U>
142 inline Type& refCast(U& obj)
143 {
144  U* p = &obj;
145  Type* casted = dynamic_cast<Type*>(p);
146 
147  if (!casted)
148  {
150  << "Attempt to cast type " << obj.type()
151  << " to type " << Type::typeName
152  << abort(FatalError);
153  }
154 
155  return *casted;
156 }
157 
158 
159 //- A dynamic_cast (for references) that generates FatalIOError on failed casts,
160 //- uses the virtual type() method for error messages.
161 template<class Type, class U>
162 inline Type& refCast(U& obj, const dictionary& dict)
163 {
164  U* p = &obj;
165  Type* casted = dynamic_cast<Type*>(p);
166 
167  if (!casted)
168  {
170  << "Attempt to cast type " << obj.type()
171  << " to type " << Type::typeName
172  << abort(FatalIOError);
173  }
174 
175  return *casted;
176 }
177 
178 
179 //- A dynamic_cast (for references) that generates FatalError on failed casts,
180 //- uses the virtual type() method for error messages.
181 //- The index can be used to convey additional context.
182 template<class Type, class U>
183 inline Type& refCast(U& obj, const label index)
184 {
185  U* p = &obj;
186  Type* casted = dynamic_cast<Type*>(p);
187 
188  if (!casted)
189  {
191  << "Attempt to cast type " << obj.type()
192  << " to type " << Type::typeName
193  << " at index " << index
194  << abort(FatalError);
195  }
197  return *casted;
198 }
199 
200 
201 //- Test if dynamic_cast to Type is possible, as a functor
202 template<class Type>
203 struct isAOp
204 {
205  template<class U>
206  bool operator()(const U& obj) const
207  {
208  return isA<Type, U>(obj);
209  }
210 };
211 
212 
213 //- Test if typeid is identical to the Type, as a functor
214 template<class Type>
215 struct isTypeOp
216 {
217  template<class U>
218  bool operator()(const U& obj) const
219  {
220  return isType<Type, U>(obj);
221  }
222 };
223 
224 
225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226 
227 } // End namespace Foam
228 
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
230 
231 #endif
233 // ************************************************************************* //
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:578
Type & refCast(U &obj)
A dynamic_cast (for references) that generates FatalError on failed casts, uses the virtual type() me...
Definition: typeInfo.H:151
bool operator()(const U &obj) const
Definition: typeInfo.H:235
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
bool isType(const U &obj)
Check if typeid of the object and Type are identical.
Definition: typeInfo.H:98
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:108
U
Definition: pEqn.H:72
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
Macro definitions for declaring ClassName(), NamespaceName(), etc.
const Type * isA(const U &obj)
Check if dynamic_cast to Type is possible.
Definition: typeInfo.H:87
volScalarField & p
bool operator()(const U &obj) const
Definition: typeInfo.H:221
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...