OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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