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  if (isNull(r))
116  {
117  return NullObjectNonConstRef<To>();
118  }
119 
120  try
121  {
122  return dynamic_cast<To&>(r);
123  }
124  catch (const std::bad_cast&)
125  {
126  return NullObjectNonConstRef<To>();
127  }
128 }
129 
130 
131 //- Reference type cast template function.
132 // As per dynamicCast, but handles type names via the virtual type() method.
133 template<class To, class From>
134 inline To& refCast(From& r)
135 {
136  try
137  {
138  return dynamic_cast<To&>(r);
139  }
140  catch (const std::bad_cast&)
141  {
143  << "Attempt to cast type " << r.type()
144  << " to type " << To::typeName
145  << abort(FatalError);
146 
147  return dynamic_cast<To&>(r);
148  }
149 }
150 
151 
152 //- Reference type cast template function,
153 // As dynamicCastNull.
154 template<class To, class From>
155 inline To& refCastNull(From& r)
156 {
157  return dynamicCastNull<To, From>(r);
158 }
159 
160 
161 //- Check the typeid
162 template<class TestType, class Type>
163 inline bool isType(const Type& t)
164 {
165  return typeid(t) == typeid(TestType);
166 }
167 
168 
169 //- Check if a dynamic_cast to typeid is possible
170 template<class TestType, class Type>
171 inline bool isA(const Type& t)
172 {
173  const Type* tPtr = &t;
174  return dynamic_cast<const TestType*>(tPtr);
175 }
176 
177 
178 //- Return the name of the object within the given type
179 // as <typeName>:<name>
180 template<class TypeName, class Name>
181 inline word typedName(Name name)
182 {
183  return (TypeName::typeName + ':') + name;
184 }
185 
186 
187 //- Determine whether a type is a reference
188 template<class Type>
189 constexpr bool isRef = std::is_reference<Type>::value;
190 
191 
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 
194 } // End namespace Foam
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 #endif
199 
200 // ************************************************************************* //
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:155
To & refCast(From &r)
Reference type cast template function.
Definition: typeInfo.H:134
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bool isType(const Type &t)
Check the typeid.
Definition: typeInfo.H:163
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:171
constexpr bool isRef
Determine whether a type is a reference.
Definition: typeInfo.H:189
word typedName(Name name)
Return the name of the object within the given type.
Definition: typeInfo.H:181
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
bool isNull(const T &t)
Return true if t is a reference to the nullObject of type T.
Definition: nullObjectI.H:58
To & dynamicCastNull(From &r)
Reference type cast template function,.
Definition: typeInfo.H:113
error FatalError