OpenFOAM logo
Open Source CFD Toolkit

typeInfo.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Type
00026     typeInfo
00027 
00028 Description
00029     Basic run-time type information using word as the type's name. 
00030     Used to enhance the standard RTTI to cover I/O.
00031 
00032     The user can get the type's type name using the type info access function
00033 
00034         type()
00035 
00036     The reference type cast template function:
00037 
00038         refCast<T>(r)
00039 
00040     wraps dynamic_cast to handle the bad_cast exception and generate a
00041     FatalError.
00042 
00043     The isA function:
00044 
00045         isA<T>(r)
00046 
00047     returns true if r is of type T or derived from type T.
00048 
00049 \*---------------------------------------------------------------------------*/
00050 
00051 #ifndef typeInfo_H
00052 #define typeInfo_H
00053 
00054 #include "error.H"
00055 #include "className.H"
00056 #include <typeinfo>
00057 
00058 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00059 
00060 namespace Foam
00061 {
00062 
00063 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00064 
00065 #define TypeName(TypeNameString)                              \
00066     ClassName(TypeNameString);                                \
00067     virtual const word& type() const { return typeName; }
00068 
00069 
00070 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00071 
00072 // Reference type cast template function wraps dynamic_cast to handle the
00073 // bad_cast exception and generate a FatalError.
00074 
00075 template<class To, class From>
00076 inline To& dynamicCast(From& r)
00077 {
00078     try
00079     {
00080         return dynamic_cast<To&>(r);
00081     }
00082     catch (std::bad_cast)
00083     {
00084         FatalErrorIn("dynamicCast<To>(From&)")
00085             << "Attempt to cast type " << typeid(r).name()
00086             << " to type " << typeid(To).name()
00087             << abort(FatalError);
00088 
00089         return dynamic_cast<To&>(r);
00090     }
00091 }
00092 
00093 
00094 template<class To, class From>
00095 inline To& refCast(From& r)
00096 {
00097     try
00098     {
00099         return dynamic_cast<To&>(r);
00100     }
00101     catch (std::bad_cast)
00102     {
00103         FatalErrorIn("refCast<To>(From&)")
00104             << "Attempt to cast type " << r.type()
00105             << " to type " << To::typeName
00106             << abort(FatalError);
00107 
00108         return dynamic_cast<To&>(r);
00109     }
00110 }
00111 
00112 
00113 template<class TestType, class Type>
00114 inline bool isType(const Type& t)
00115 {
00116     return typeid(t) == typeid(TestType);
00117 }
00118 
00119 
00120 template<class TestType, class Type>
00121 inline bool isA(const Type& t)
00122 {
00123     return dynamic_cast<const TestType*>(&t);
00124 }
00125 
00126 
00127 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00128 
00129 } // End namespace Foam
00130 
00131 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00132 
00133 #endif
00134 
00135 // ************************************************************************* //
For further information go to www.openfoam.org