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 | Copyright (C) 2011 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 Typedef
25  Foam::typeInfo
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 <typeinfo>
59 
60 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 
62 // declarations (for use in header files)
63 
64 //- Declare a ClassNameNoDebug() with extra virtual type info
65 #define TypeNameNoDebug(TypeNameString) \
66  ClassNameNoDebug(TypeNameString); \
67  virtual const word& type() const { return typeName; }
68 
69 //- Declare a ClassName() with extra virtual type info
70 #define TypeName(TypeNameString) \
71  ClassName(TypeNameString); \
72  virtual const word& type() const { return typeName; }
73 
74 
75 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76 
77 namespace Foam
78 {
79 
80 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
81 
82 //- Reference type cast template function,
83 // wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
84 template<class To, class From>
85 inline To& dynamicCast(From& r)
86 {
87  try
88  {
89  return dynamic_cast<To&>(r);
90  }
91  catch (std::bad_cast)
92  {
93  FatalErrorIn("dynamicCast<To>(From&)")
94  << "Attempt to cast type " << typeid(r).name()
95  << " to type " << typeid(To).name()
96  << abort(FatalError);
97 
98  return dynamic_cast<To&>(r);
99  }
100 }
101 
102 
103 //- Reference type cast template function.
104 // As per dynamicCast, but handles type names via the virtual type() method.
105 template<class To, class From>
106 inline To& refCast(From& r)
107 {
108  try
109  {
110  return dynamic_cast<To&>(r);
111  }
112  catch (std::bad_cast)
113  {
114  FatalErrorIn("refCast<To>(From&)")
115  << "Attempt to cast type " << r.type()
116  << " to type " << To::typeName
117  << abort(FatalError);
118 
119  return dynamic_cast<To&>(r);
120  }
121 }
122 
123 
124 //- Check the typeid
125 template<class TestType, class Type>
126 inline bool isType(const Type& t)
127 {
128  return typeid(t) == typeid(TestType);
129 }
130 
131 
132 //- Check if a dynamic_cast to typeid is possible
133 template<class TestType, class Type>
134 inline bool isA(const Type& t)
135 {
136  return dynamic_cast<const TestType*>(&t);
137 }
138 
139 
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
141 
142 } // End namespace Foam
143 
144 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
145 
146 #endif
147 
148 // ************************************************************************* //
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Namespace for OpenFOAM.
Macro definitions for declaring ClassName(), NamespaceName(), etc.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bool isType(const Type &t)
Check the typeid.
Definition: typeInfo.H:126
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
To & dynamicCast(From &r)
Reference type cast template function,.
Definition: typeInfo.H:85
To & refCast(From &r)
Reference type cast template function.
Definition: typeInfo.H:106
bool isA(const Type &t)
Check if a dynamic_cast to typeid is possible.
Definition: typeInfo.H:134