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-2023 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 <type_traits>
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  template<class Name> \
70  word typedName(Name name) const { return (type() + ':') + name; }
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  template<class Name> \
77  word typedName(Name name) const { return (type() + ':') + name; }
78 
79 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 namespace Foam
83 {
84 
85 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
86 
87 //- Reference type cast template function,
88 // wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
89 template<class To, class From>
90 inline To& dynamicCast(From& r)
91 {
92  try
93  {
94  return dynamic_cast<To&>(r);
95  }
96  catch (const std::bad_cast&)
97  {
99  << "Attempt to cast type " << typeid(r).name()
100  << " to type " << typeid(To).name()
101  << abort(FatalError);
102 
103  return dynamic_cast<To&>(r);
104  }
105 }
106 
107 
108 //- Reference type cast template function.
109 // As per dynamicCast, but handles type names via the virtual type() method.
110 template<class To, class From>
111 inline To& refCast(From& r)
112 {
113  try
114  {
115  return dynamic_cast<To&>(r);
116  }
117  catch (const std::bad_cast&)
118  {
120  << "Attempt to cast type " << r.type()
121  << " to type " << To::typeName
122  << abort(FatalError);
123 
124  return dynamic_cast<To&>(r);
125  }
126 }
127 
128 
129 //- Check the typeid
130 template<class TestType, class Type>
131 inline bool isType(const Type& t)
132 {
133  return typeid(t) == typeid(TestType);
134 }
135 
136 
137 //- Check if a dynamic_cast to typeid is possible
138 template<class TestType, class Type>
139 inline bool isA(const Type& t)
140 {
141  const Type* tPtr = &t;
142  return dynamic_cast<const TestType*>(tPtr);
143 }
144 
145 
146 //- Return the name of the object within the given type
147 // as <typeName>:<name>
148 template<class TypeName, class Name>
149 inline word typedName(Name name)
150 {
151  return (TypeName::typeName + ':') + name;
152 }
153 
154 
155 //- Determine whether a type is a reference
156 template<class Type>
157 constexpr bool isRef = std::is_reference<Type>::value;
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 } // End namespace Foam
163 
164 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
165 
166 #endif
167 
168 // ************************************************************************* //
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:306
Namespace for OpenFOAM.
To & refCast(From &r)
Reference type cast template function.
Definition: typeInfo.H:111
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:131
To & dynamicCast(From &r)
Reference type cast template function,.
Definition: typeInfo.H:90
bool isA(const Type &t)
Check if a dynamic_cast to typeid is possible.
Definition: typeInfo.H:139
constexpr bool isRef
Determine whether a type is a reference.
Definition: typeInfo.H:157
word typedName(Name name)
Return the name of the object within the given type.
Definition: typeInfo.H:149
error FatalError