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-2024 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 
81 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
82 
83 namespace Foam
84 {
85 
86 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
87 
88 //- Reference type cast template function,
89 // wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
90 template<class To, class From>
91 inline To& dynamicCast(From& r)
92 {
93  try
94  {
95  return dynamic_cast<To&>(r);
96  }
97  catch (const std::bad_cast&)
98  {
100  << "Attempt to cast type " << typeid(r).name()
101  << " to type " << typeid(To).name()
102  << abort(FatalError);
103 
104  return dynamic_cast<To&>(r);
105  }
106 }
107 
108 
109 //- Reference type cast template function,
110 // wraps dynamic_cast to handle bad_cast exception and generate a null
111 // reference.
112 template<class To, class From>
113 inline To& dynamicCastNull(From& r)
114 {
115  try
116  {
117  return dynamic_cast<To&>(r);
118  }
119  catch (const std::bad_cast&)
120  {
121  return NullObjectNonConstRef<To>();
122  }
123 }
124 
125 
126 //- Reference type cast template function.
127 // As per dynamicCast, but handles type names via the virtual type() method.
128 template<class To, class From>
129 inline To& refCast(From& r)
130 {
131  try
132  {
133  return dynamic_cast<To&>(r);
134  }
135  catch (const std::bad_cast&)
136  {
138  << "Attempt to cast type " << r.type()
139  << " to type " << To::typeName
140  << abort(FatalError);
141 
142  return dynamic_cast<To&>(r);
143  }
144 }
145 
146 
147 //- Reference type cast template function,
148 // As dynamicCastNull.
149 template<class To, class From>
150 inline To& refCastNull(From& r)
151 {
152  return dynamicCastNull<To, From>(r);
153 }
154 
155 
156 //- Check the typeid
157 template<class TestType, class Type>
158 inline bool isType(const Type& t)
159 {
160  return typeid(t) == typeid(TestType);
161 }
162 
163 
164 //- Check if a dynamic_cast to typeid is possible
165 template<class TestType, class Type>
166 inline bool isA(const Type& t)
167 {
168  const Type* tPtr = &t;
169  return dynamic_cast<const TestType*>(tPtr);
170 }
171 
172 
173 //- Return the name of the object within the given type
174 // as <typeName>:<name>
175 template<class TypeName, class Name>
176 inline word typedName(Name name)
177 {
178  return (TypeName::typeName + ':') + name;
179 }
180 
181 
182 //- Determine whether a type is a reference
183 template<class Type>
184 constexpr bool isRef = std::is_reference<Type>::value;
185 
186 
187 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188 
189 } // End namespace Foam
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 #endif
194 
195 // ************************************************************************* //
A class for handling words, derived from string.
Definition: word.H:62
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:150
To & refCast(From &r)
Reference type cast template function.
Definition: typeInfo.H:129
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bool isType(const Type &t)
Check the typeid.
Definition: typeInfo.H:158
To & dynamicCast(From &r)
Reference type cast template function,.
Definition: typeInfo.H:91
bool isA(const Type &t)
Check if a dynamic_cast to typeid is possible.
Definition: typeInfo.H:166
constexpr bool isRef
Determine whether a type is a reference.
Definition: typeInfo.H:184
word typedName(Name name)
Return the name of the object within the given type.
Definition: typeInfo.H:176
To & dynamicCastNull(From &r)
Reference type cast template function,.
Definition: typeInfo.H:113
error FatalError