1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 Basic run-time type information using word as the type's name.
30 Used to enhance the standard RTTI to cover I/O.
32 The user can get the type's type name using the type info access function
37 The reference type cast template function:
42 wraps dynamic_cast to handle the bad_cast exception and generate a
50 returns true if r is of type T or derived from type T.
52 \*---------------------------------------------------------------------------*/
57 #include <OpenFOAM/error.H>
58 #include "className.H"
61 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 // declarations (for use in header files)
65 //- Declare a ClassNameNoDebug() with extra virtual type info
66 #define TypeNameNoDebug(TypeNameString) \
67 ClassNameNoDebug(TypeNameString); \
68 virtual const word& type() const { return typeName; }
70 //- Declare a ClassName() with extra virtual type info
71 #define TypeName(TypeNameString) \
72 ClassName(TypeNameString); \
73 virtual const word& type() const { return typeName; }
76 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
83 //- Reference type cast template function,
84 // wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
85 template<class To, class From>
86 inline To& dynamicCast(From& r)
90 return dynamic_cast<To&>(r);
94 FatalErrorIn("dynamicCast<To>(From&)")
95 << "Attempt to cast type " << typeid(r).name()
96 << " to type " << typeid(To).name()
99 return dynamic_cast<To&>(r);
104 //- Reference type cast template function.
105 // As per dynamicCast, but handles type names via the virtual type() method.
106 template<class To, class From>
107 inline To& refCast(From& r)
111 return dynamic_cast<To&>(r);
113 catch (std::bad_cast)
115 FatalErrorIn("refCast<To>(From&)")
116 << "Attempt to cast type " << r.type()
117 << " to type " << To::typeName
118 << abort(FatalError);
120 return dynamic_cast<To&>(r);
126 template<class TestType, class Type>
127 inline bool isType(const Type& t)
129 return typeid(t) == typeid(TestType);
133 //- Check if a dynamic_cast to typeid is possible
134 template<class TestType, class Type>
135 inline bool isA(const Type& t)
137 return dynamic_cast<const TestType*>(&t);
141 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
143 } // End namespace Foam
145 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
149 // ************************ vim: set sw=4 sts=4 et: ************************ //