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-2019 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<T>(r)
42  \endcode
43 
44  wraps dynamic_cast to handle the bad_cast exception and generate a
45  FatalError.
46 
47  The isA function:
48  \code
49  isA<T>(r)
50  \endcode
51 
52  returns true if r is of type T or derived from type T.
53 
54 \*---------------------------------------------------------------------------*/
55 
56 #ifndef typeInfo_H
57 #define typeInfo_H
58 
59 #include "error.H"
60 #include "className.H"
61 #include <typeinfo>
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 // Declarations (for use in header files)
66 
67 //- Declare a ClassNameNoDebug() with extra virtual type info
68 #define TypeNameNoDebug(TypeNameString) \
69  ClassNameNoDebug(TypeNameString); \
70  virtual const word& type() const { return typeName; }
71 
72 //- Declare a ClassName() with extra virtual type info
73 #define TypeName(TypeNameString) \
74  ClassName(TypeNameString); \
75  virtual const word& type() const { return typeName; }
76 
77 
78 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
79 
80 namespace Foam
81 {
82 
83 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
84 
85 //- Reference type cast template function,
86 //- wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
87 template<class To, class From>
88 inline To& dynamicCast(From& r)
89 {
90  try
91  {
92  return dynamic_cast<To&>(r);
93  }
94  catch (const std::bad_cast&)
95  {
97  << "Attempt to cast type " << typeid(r).name()
98  << " to type " << typeid(To).name()
99  << abort(FatalError);
100 
101  return dynamic_cast<To&>(r);
102  }
103 }
104 
105 
106 //- Reference type cast template function,
107 //- wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
108 template<class To, class From>
109 inline To& dynamicCast(From& r, const dictionary& d)
110 {
111  try
112  {
113  return dynamic_cast<To&>(r);
114  }
115  catch (const std::bad_cast&)
116  {
118  << "Attempt to cast type " << typeid(r).name()
119  << " to type " << typeid(To).name()
120  << abort(FatalIOError);
121 
122  return dynamic_cast<To&>(r);
123  }
124 }
125 
126 
127 //- Reference type cast template function.
128 // As per dynamicCast, but handles type names for the error messages
129 // via the virtual type() method.
130 template<class To, class From>
131 inline To& refCast(From& r)
132 {
133  try
134  {
135  return dynamic_cast<To&>(r);
136  }
137  catch (const std::bad_cast&)
138  {
140  << "Attempt to cast type " << r.type()
141  << " to type " << To::typeName
142  << abort(FatalError);
143 
144  return dynamic_cast<To&>(r);
145  }
146 }
147 
148 
149 //- Reference type cast template function.
150 // As per dynamicCast, but handles type names for the error messages
151 // via the virtual type() method.
152 // Can use index to convey the context.
153 template<class To, class From>
154 inline To& refCast(From& r, const label index)
155 {
156  try
157  {
158  return dynamic_cast<To&>(r);
159  }
160  catch (const std::bad_cast&)
161  {
163  << "Attempt to cast type " << r.type()
164  << " to type " << To::typeName
165  << " at index " << index
166  << abort(FatalError);
167 
168  return dynamic_cast<To&>(r);
169  }
170 }
171 
172 
173 //- Reference type cast template function.
174 // As per dynamicCast, but handles type names for the error messages
175 // via the virtual type() method.
176 template<class To, class From>
177 inline To& refCast(From& r, const dictionary& d)
178 {
179  try
180  {
181  return dynamic_cast<To&>(r);
182  }
183  catch (const std::bad_cast&)
184  {
186  << "Attempt to cast type " << r.type()
187  << " to type " << To::typeName
188  << abort(FatalIOError);
189 
190  return dynamic_cast<To&>(r);
191  }
192 }
193 
194 
195 //- Check if dynamic_cast to TargetType is possible
196 template<class TargetType, class Type>
197 inline const TargetType* isA(const Type& t)
198 {
199  const Type* p = &t;
200  return dynamic_cast<const TargetType*>(p);
201 }
202 
203 
204 //- Check if dynamic_cast to TargetType is possible, as a functor
205 template<class TargetType>
206 struct isAOp
207 {
208  template<class Type>
209  inline bool operator()(const Type& t) const
210  {
211  return isA<TargetType,Type>(t);
212  }
213 };
214 
215 
216 //- Check is typeid is identical to the TargetType
217 template<class TargetType, class Type>
218 inline bool isType(const Type& t)
219 {
220  return typeid(TargetType) == typeid(t);
221 }
222 
223 
224 //- Check is typeid is identical to the TargetType, as a functor
225 template<class TargetType>
226 struct isTypeOp
227 {
228  template<class Type>
229  inline bool operator()(const Type& t) const
230  {
231  return isType<TargetType,Type>(t);
232  }
233 };
234 
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 } // End namespace Foam
239 
240 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 
242 #endif
243 
244 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::isTypeOp::operator()
bool operator()(const Type &t) const
Definition: typeInfo.H:229
Foam::FatalIOError
IOerror FatalIOError
Foam::isTypeOp
Check is typeid is identical to the TargetType, as a functor.
Definition: typeInfo.H:226
error.H
className.H
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Foam::isAOp::operator()
bool operator()(const Type &t) const
Definition: typeInfo.H:209
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::refCast
To & refCast(From &r)
Reference type cast template function.
Definition: typeInfo.H:131
Foam::isType
bool isType(const Type &t)
Check is typeid is identical to the TargetType.
Definition: typeInfo.H:218
Foam::isA
const TargetType * isA(const Type &t)
Check if dynamic_cast to TargetType is possible.
Definition: typeInfo.H:197
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::dynamicCast
To & dynamicCast(From &r)
Definition: typeInfo.H:88
Foam::isAOp
Check if dynamic_cast to TargetType is possible, as a functor.
Definition: typeInfo.H:206
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473