FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::errorManip
00026 
00027 Description
00028     Error stream manipulators for exit and abort which may terminate the
00029     program or throw an exception depending if the exception
00030     handling has been switched on (off by default).
00031 
00032 Usage
00033     @code
00034         error << "message1" << "message2" << FoamDataType << exit(error, errNo);
00035         error << "message1" << "message2" << FoamDataType << abort(error);
00036     @endcode
00037 
00038 \*---------------------------------------------------------------------------*/
00039 
00040 #ifndef errorManip_H
00041 #define errorManip_H
00042 
00043 #include <OpenFOAM/error.H>
00044 
00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00046 
00047 namespace Foam
00048 {
00049 
00050 // Forward declaration of friend functions and operators
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 //- errorManipArg
00095 template<class Err, class T>
00096 class errorManipArg
00097 {
00098     void (Err::*fPtr_)(const T);
00099     Err& err_;
00100     T arg_;
00101 
00102 public:
00103 
00104     errorManipArg(void (Err::*fPtr)(const T), Err& t, const T i)
00105     :
00106         fPtr_(fPtr),
00107         err_(t),
00108         arg_(i)
00109     {}
00110 
00111     friend Ostream& operator<< <Err, T>(Ostream& os, errorManipArg<Err, T> m);
00112 };
00113 
00114 
00115 template<class Err, class T>
00116 inline Ostream& operator<<(Ostream& os, errorManipArg<Err, T> m)
00117 {
00118     (m.err_.*m.fPtr_)(m.arg_);
00119     return os;
00120 }
00121 
00122 
00123 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00124 
00125 inline errorManipArg<error, int> exit(error& err, const int errNo = 1)
00126 {
00127     return errorManipArg<error, int>(&error::exit, err, errNo);
00128 }
00129 
00130 inline errorManip<error> abort(error& err)
00131 {
00132     return errorManip<error>(&error::abort, err);
00133 }
00134 
00135 
00136 inline errorManipArg<IOerror, int> exit(IOerror& err, const int errNo = 1)
00137 {
00138     return errorManipArg<IOerror, int>(&IOerror::exit, err, errNo);
00139 }
00140 
00141 inline errorManip<IOerror> abort(IOerror& err)
00142 {
00143     return errorManip<IOerror>(&IOerror::abort, err);
00144 }
00145 
00146 
00147 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00148 
00149 } // End namespace Foam
00150 
00151 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00152 
00153 #endif
00154 
00155 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines