Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / src / OpenFOAM / db / typeInfo / typeInfo.H
blobcbb86b48396dd0533ccd9c4b1fe127e0f9053288
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 Typedef
26     Foam::typeInfo
28 Description
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
33     @code
34         type()
35     @endcode
37     The reference type cast template function:
38     @code
39         refCast<T>(r)
40     @endcode
42     wraps dynamic_cast to handle the bad_cast exception and generate a
43     FatalError.
45     The isA function:
46     @code
47         isA<T>(r)
48     @endcode
50     returns true if r is of type T or derived from type T.
52 \*---------------------------------------------------------------------------*/
54 #ifndef typeInfo_H
55 #define typeInfo_H
57 #include <OpenFOAM/error.H>
58 #include "className.H"
59 #include <typeinfo>
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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
78 namespace Foam
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)
88     try
89     {
90         return dynamic_cast<To&>(r);
91     }
92     catch (std::bad_cast)
93     {
94         FatalErrorIn("dynamicCast<To>(From&)")
95             << "Attempt to cast type " << typeid(r).name()
96             << " to type " << typeid(To).name()
97             << abort(FatalError);
99         return dynamic_cast<To&>(r);
100     }
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)
109     try
110     {
111         return dynamic_cast<To&>(r);
112     }
113     catch (std::bad_cast)
114     {
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);
121     }
125 //- Check the typeid
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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
147 #endif
149 // ************************ vim: set sw=4 sts=4 et: ************************ //