initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / error / errorManip.H
bloba3989e629df5f25fe8a2dbc9b09ac9f04c65426a
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 Class
26     Foam::errorManip
28 Description
29     Error stream manipulators for exit and abort which may terminate the
30     program or throw an exception depending if the exception
31     handling has been switched on (off by default).
33 Usage
34     @code
35         error << "message1" << "message2" << FoamDataType << exit(error, errNo);
36         error << "message1" << "message2" << FoamDataType << abort(error);
37     @endcode
39 \*---------------------------------------------------------------------------*/
41 #ifndef errorManip_H
42 #define errorManip_H
44 #include "error.H"
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 namespace Foam
51 // Forward declaration of friend functions and operators
53 template<class Err> class errorManip;
54 template<class Err> Ostream& operator<<(Ostream&, errorManip<Err>);
56 template<class Err, class T> class errorManipArg;
57 template<class Err, class T>
58 Ostream& operator<<(Ostream&, errorManipArg<Err, T>);
61 /*---------------------------------------------------------------------------*\
62                            Class errorManip Declaration
63 \*---------------------------------------------------------------------------*/
65 template<class Err>
66 class errorManip
68     void (Err::*fPtr_)();
69     Err& err_;
71 public:
73     errorManip(void (Err::*fPtr)(), Err& t)
74     :
75         fPtr_(fPtr),
76         err_(t)
77     {}
79     friend Ostream& operator<< <Err>(Ostream& os, errorManip<Err> m);
83 template<class Err>
84 inline Ostream& operator<<(Ostream& os, errorManip<Err> m)
86     (m.err_.*m.fPtr_)();
87     return os;
91 /*---------------------------------------------------------------------------*\
92                            Class errorManipArg Declaration
93 \*---------------------------------------------------------------------------*/
95 //- errorManipArg
96 template<class Err, class T>
97 class errorManipArg
99     void (Err::*fPtr_)(const T);
100     Err& err_;
101     T arg_;
103 public:
105     errorManipArg(void (Err::*fPtr)(const T), Err& t, const T i)
106     :
107         fPtr_(fPtr),
108         err_(t),
109         arg_(i)
110     {}
112     friend Ostream& operator<< <Err, T>(Ostream& os, errorManipArg<Err, T> m);
116 template<class Err, class T>
117 inline Ostream& operator<<(Ostream& os, errorManipArg<Err, T> m)
119     (m.err_.*m.fPtr_)(m.arg_);
120     return os;
124 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
126 inline errorManipArg<error, int> exit(error& err, const int errNo = 1)
128     return errorManipArg<error, int>(&error::exit, err, errNo);
131 inline errorManip<error> abort(error& err)
133     return errorManip<error>(&error::abort, err);
137 inline errorManipArg<IOerror, int> exit(IOerror& err, const int errNo = 1)
139     return errorManipArg<IOerror, int>(&IOerror::exit, err, errNo);
142 inline errorManip<IOerror> abort(IOerror& err)
144     return errorManip<IOerror>(&IOerror::abort, err);
148 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
150 } // End namespace Foam
152 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
154 #endif
156 // ************************************************************************* //