OpenFOAM logo
Open Source CFD Toolkit

errorManip.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 Class
00026     errorManip
00027 
00028 Description
00029     Error stream Manipulators for exit and abort which may terminate the
00030     program or throw an exception depending of if the exception
00031     handling has beed switched on (off by default).
00032 
00033 Usage
00034 
00035     error << "message1" << "message2" << FoamDataType << exit(error, errNo);
00036     error << "message1" << "message2" << FoamDataType << abort(error);
00037 
00038 \*---------------------------------------------------------------------------*/
00039 
00040 #ifndef errorManip_H
00041 #define errorManip_H
00042 
00043 #include "error.H"
00044 
00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00046 
00047 namespace Foam
00048 {
00049 
00050 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
00051 
00052 template<class Err> class errorManip;
00053 template<class Err> Ostream& operator<<(Ostream&, errorManip<Err>);
00054 
00055 template<class Err, class T> class errorManipArg;
00056 template<class Err, class T>
00057 Ostream& operator<<(Ostream&, errorManipArg<Err, T>);
00058 
00059 
00060 /*---------------------------------------------------------------------------*\
00061                            Class errorManip Declaration
00062 \*---------------------------------------------------------------------------*/
00063 
00064 template<class Err>
00065 class errorManip
00066 {
00067     void (Err::*fPtr_)();
00068     Err& err_;
00069 
00070 public:
00071 
00072     errorManip(void (Err::*fPtr)(), Err& t)
00073     :
00074         fPtr_(fPtr),
00075         err_(t)
00076     {}
00077 
00078     friend Ostream& operator<< <Err>(Ostream& os, errorManip<Err> m);
00079 };
00080 
00081 
00082 template<class Err>
00083 inline Ostream& operator<<(Ostream& os, errorManip<Err> m)
00084 {
00085     (m.err_.*m.fPtr_)();
00086     return os;
00087 }
00088 
00089 
00090 /*---------------------------------------------------------------------------*\
00091                            Class errorManipArg Declaration
00092 \*---------------------------------------------------------------------------*/
00093 
00094 template<class Err, class T>
00095 class errorManipArg
00096 {
00097     void (Err::*fPtr_)(const T);
00098     Err& err_;
00099     T arg_;
00100 
00101 public:
00102 
00103     errorManipArg(void (Err::*fPtr)(const T), Err& t, const T i)
00104     :
00105         fPtr_(fPtr),
00106         err_(t),
00107         arg_(i)
00108     {}
00109 
00110     friend Ostream& operator<< <Err, T>(Ostream& os, errorManipArg<Err, T> m);
00111 };
00112 
00113 
00114 template<class Err, class T>
00115 inline Ostream& operator<<(Ostream& os, errorManipArg<Err, T> m)
00116 {
00117     (m.err_.*m.fPtr_)(m.arg_);
00118     return os;
00119 }
00120 
00121 
00122 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00123 
00124 inline errorManipArg<error, int> exit(error& err, const int errNo = 1)
00125 {
00126     return errorManipArg<error, int>(&error::exit, err, errNo);
00127 }
00128 
00129 inline errorManip<error> abort(error& err)
00130 {
00131     return errorManip<error>(&error::abort, err);
00132 }
00133 
00134 
00135 inline errorManipArg<IOerror, int> exit(IOerror& err, const int errNo = 1)
00136 {
00137     return errorManipArg<IOerror, int>(&IOerror::exit, err, errNo);
00138 }
00139 
00140 inline errorManip<IOerror> abort(IOerror& err)
00141 {
00142     return errorManip<IOerror>(&IOerror::abort, err);
00143 }
00144 
00145 
00146 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00147 
00148 } // End namespace Foam
00149 
00150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00151 
00152 #endif
00153 
00154 // ************************************************************************* //
For further information go to www.openfoam.org