1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
28 Basic run-time type information using word as the type's name.
29 Used to enhance the standard RTTI to cover I/O.
31 The user can get the type's type name using the type info access function
36 The reference type cast template function:
41 wraps dynamic_cast to handle the bad_cast exception and generate a
49 returns true if r is of type T or derived from type T.
51 \*---------------------------------------------------------------------------*/
57 #include "className.H"
60 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
62 // declarations (for use in header files)
64 //- Declare a ClassNameNoDebug() with extra virtual type info
65 #define TypeNameNoDebug(TypeNameString) \
66 ClassNameNoDebug(TypeNameString); \
67 virtual const word& type() const { return typeName; }
69 //- Declare a ClassName() with extra virtual type info
70 #define TypeName(TypeNameString) \
71 ClassName(TypeNameString); \
72 virtual const word& type() const { return typeName; }
75 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
80 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
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)
89 return dynamic_cast<To&>(r);
93 FatalErrorIn("dynamicCast<To>(From&)")
94 << "Attempt to cast type " << typeid(r).name()
95 << " to type " << typeid(To).name()
98 return dynamic_cast<To&>(r);
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)
110 return dynamic_cast<To&>(r);
112 catch (std::bad_cast)
114 FatalErrorIn("refCast<To>(From&)")
115 << "Attempt to cast type " << r.type()
116 << " to type " << To::typeName
117 << abort(FatalError);
119 return dynamic_cast<To&>(r);
125 template<class TestType, class Type>
126 inline bool isType(const Type& t)
128 return typeid(t) == typeid(TestType);
132 //- Check if a dynamic_cast to typeid is possible
133 template<class TestType, class Type>
134 inline bool isA(const Type& t)
136 return dynamic_cast<const TestType*>(&t);
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
142 } // End namespace Foam
144 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
148 // ************************************************************************* //