|
|
|
typeInfo.HGo 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-2009 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 Typedef 00026 Foam::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 @code 00034 type() 00035 @endcode 00036 00037 The reference type cast template function: 00038 @code 00039 refCast<T>(r) 00040 @endcode 00041 00042 wraps dynamic_cast to handle the bad_cast exception and generate a 00043 FatalError. 00044 00045 The isA function: 00046 @code 00047 isA<T>(r) 00048 @endcode 00049 00050 returns true if r is of type T or derived from type T. 00051 00052 \*---------------------------------------------------------------------------*/ 00053 00054 #ifndef typeInfo_H 00055 #define typeInfo_H 00056 00057 #include "error.H" 00058 #include "className.H" 00059 #include <typeinfo> 00060 00061 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00062 00063 // declarations (for use in header files) 00064 00065 //- Declare a ClassNameNoDebug() with extra virtual type info 00066 #define TypeNameNoDebug(TypeNameString) \ 00067 ClassNameNoDebug(TypeNameString); \ 00068 virtual const word& type() const { return typeName; } 00069 00070 //- Declare a ClassName() with extra virtual type info 00071 #define TypeName(TypeNameString) \ 00072 ClassName(TypeNameString); \ 00073 virtual const word& type() const { return typeName; } 00074 00075 00076 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00077 00078 namespace Foam 00079 { 00080 00081 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // 00082 00083 //- Reference type cast template function, 00084 // wraps dynamic_cast to handle bad_cast exception and generate a FatalError. 00085 template<class To, class From> 00086 inline To& dynamicCast(From& r) 00087 { 00088 try 00089 { 00090 return dynamic_cast<To&>(r); 00091 } 00092 catch (std::bad_cast) 00093 { 00094 FatalErrorIn("dynamicCast<To>(From&)") 00095 << "Attempt to cast type " << typeid(r).name() 00096 << " to type " << typeid(To).name() 00097 << abort(FatalError); 00098 00099 return dynamic_cast<To&>(r); 00100 } 00101 } 00102 00103 00104 //- Reference type cast template function. 00105 // As per dynamicCast, but handles type names via the virtual type() method. 00106 template<class To, class From> 00107 inline To& refCast(From& r) 00108 { 00109 try 00110 { 00111 return dynamic_cast<To&>(r); 00112 } 00113 catch (std::bad_cast) 00114 { 00115 FatalErrorIn("refCast<To>(From&)") 00116 << "Attempt to cast type " << r.type() 00117 << " to type " << To::typeName 00118 << abort(FatalError); 00119 00120 return dynamic_cast<To&>(r); 00121 } 00122 } 00123 00124 00125 //- Check the typeid 00126 template<class TestType, class Type> 00127 inline bool isType(const Type& t) 00128 { 00129 return typeid(t) == typeid(TestType); 00130 } 00131 00132 00133 //- Check if a dynamic_cast to typeid is possible 00134 template<class TestType, class Type> 00135 inline bool isA(const Type& t) 00136 { 00137 return dynamic_cast<const TestType*>(&t); 00138 } 00139 00140 00141 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00142 00143 } // End namespace Foam 00144 00145 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00146 00147 #endif 00148 00149 // ************************************************************************* // |