00001 /*---------------------------------------------------------------------------*\ 00002 ========= | 00003 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox 00004 \\ / O peration | 00005 \\ / A nd | Copyright (C) 1991-2010 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 00013 the Free Software Foundation, either version 3 of the License, or 00014 (at your 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, see <http://www.gnu.org/licenses/>. 00023 00024 Typedef 00025 Foam::typeInfo 00026 00027 Description 00028 Basic run-time type information using word as the type's name. 00029 Used to enhance the standard RTTI to cover I/O. 00030 00031 The user can get the type's type name using the type info access function 00032 @code 00033 type() 00034 @endcode 00035 00036 The reference type cast template function: 00037 @code 00038 refCast<T>(r) 00039 @endcode 00040 00041 wraps dynamic_cast to handle the bad_cast exception and generate a 00042 FatalError. 00043 00044 The isA function: 00045 @code 00046 isA<T>(r) 00047 @endcode 00048 00049 returns true if r is of type T or derived from type T. 00050 00051 \*---------------------------------------------------------------------------*/ 00052 00053 #ifndef typeInfo_H 00054 #define typeInfo_H 00055 00056 #include <OpenFOAM/error.H> 00057 #include "className.H" 00058 #include <typeinfo> 00059 00060 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00061 00062 // declarations (for use in header files) 00063 00064 //- Declare a ClassNameNoDebug() with extra virtual type info 00065 #define TypeNameNoDebug(TypeNameString) \ 00066 ClassNameNoDebug(TypeNameString); \ 00067 virtual const word& type() const { return typeName; } 00068 00069 //- Declare a ClassName() with extra virtual type info 00070 #define TypeName(TypeNameString) \ 00071 ClassName(TypeNameString); \ 00072 virtual const word& type() const { return typeName; } 00073 00074 00075 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00076 00077 namespace Foam 00078 { 00079 00080 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // 00081 00082 //- Reference type cast template function, 00083 // wraps dynamic_cast to handle bad_cast exception and generate a FatalError. 00084 template<class To, class From> 00085 inline To& dynamicCast(From& r) 00086 { 00087 try 00088 { 00089 return dynamic_cast<To&>(r); 00090 } 00091 catch (std::bad_cast) 00092 { 00093 FatalErrorIn("dynamicCast<To>(From&)") 00094 << "Attempt to cast type " << typeid(r).name() 00095 << " to type " << typeid(To).name() 00096 << abort(FatalError); 00097 00098 return dynamic_cast<To&>(r); 00099 } 00100 } 00101 00102 00103 //- Reference type cast template function. 00104 // As per dynamicCast, but handles type names via the virtual type() method. 00105 template<class To, class From> 00106 inline To& refCast(From& r) 00107 { 00108 try 00109 { 00110 return dynamic_cast<To&>(r); 00111 } 00112 catch (std::bad_cast) 00113 { 00114 FatalErrorIn("refCast<To>(From&)") 00115 << "Attempt to cast type " << r.type() 00116 << " to type " << To::typeName 00117 << abort(FatalError); 00118 00119 return dynamic_cast<To&>(r); 00120 } 00121 } 00122 00123 00124 //- Check the typeid 00125 template<class TestType, class Type> 00126 inline bool isType(const Type& t) 00127 { 00128 return typeid(t) == typeid(TestType); 00129 } 00130 00131 00132 //- Check if a dynamic_cast to typeid is possible 00133 template<class TestType, class Type> 00134 inline bool isA(const Type& t) 00135 { 00136 return dynamic_cast<const TestType*>(&t); 00137 } 00138 00139 00140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00141 00142 } // End namespace Foam 00143 00144 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00145 00146 #endif 00147 00148 // ************************ vim: set sw=4 sts=4 et: ************************ //