errorManip.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::errorManip
28 
29 Description
30  Error stream manipulators for exit and abort which may terminate the
31  program or throw an exception depending if the exception
32  handling has been switched on (off by default).
33 
34 Usage
35  \code
36  error << "message " << someType << abort(error);
37  error << "message " << someType << exit(error, errNo);
38  \endcode
39 
40 Class
41  Foam::errorManipArg
42 
43 Description
44  Error stream manipulators for functions with an argument.
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef errorManip_H
49 #define errorManip_H
50 
51 #include "error.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward Declarations
59 template<class Err> class errorManip;
60 template<class Err> Ostream& operator<<(Ostream&, errorManip<Err>);
61 
62 template<class Err, class T> class errorManipArg;
63 template<class Err, class T>
65 
66 
67 /*---------------------------------------------------------------------------*\
68  Class errorManip Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 template<class Err>
72 class errorManip
73 {
74  void (Err::*fPtr_)();
75  Err& err_;
76 
77 public:
78 
79  errorManip(void (Err::*fPtr)(), Err& t)
80  :
81  fPtr_(fPtr),
82  err_(t)
83  {}
84 
85  friend Ostream& operator<< <Err>(Ostream& os, errorManip<Err> m);
86 };
87 
88 
89 template<class Err>
91 {
92  (m.err_.*m.fPtr_)();
93  return os;
94 }
95 
96 
97 /*---------------------------------------------------------------------------*\
98  Class errorManipArg Declaration
99 \*---------------------------------------------------------------------------*/
100 
101 template<class Err, class T>
102 class errorManipArg
103 {
104  void (Err::*fPtr_)(const T);
105  Err& err_;
106  T arg_;
107 
108 public:
109 
110  errorManipArg(void (Err::*fPtr)(const T), Err& t, const T i)
111  :
112  fPtr_(fPtr),
113  err_(t),
114  arg_(i)
115  {}
116 
117  friend Ostream& operator<< <Err, T>(Ostream& os, errorManipArg<Err, T> m);
118 };
119 
120 
121 template<class Err, class T>
123 {
124  (m.err_.*m.fPtr_)(m.arg_);
125  return os;
126 }
127 
128 
129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 
132 exit(error& err, const int errNo = 1)
133 {
134  return errorManipArg<error, int>(&error::exit, err, errNo);
135 }
136 
137 
139 exit(IOerror& err, const int errNo = 1)
140 {
141  return errorManipArg<IOerror, int>(&IOerror::exit, err, errNo);
142 }
143 
144 
145 inline errorManip<error>
146 abort(error& err)
147 {
148  return errorManip<error>(&error::abort, err);
149 }
150 
151 
152 inline errorManip<IOerror>
153 abort(IOerror& err)
154 {
155  return errorManip<IOerror>(&IOerror::abort, err);
156 }
157 
158 
159 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
160 
161 } // End namespace Foam
162 
163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
164 
165 #endif
166 
167 // ************************************************************************* //
Foam::IOerror::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:249
Foam::errorManip
Error stream manipulators for exit and abort which may terminate the program or throw an exception de...
Definition: errorManip.H:57
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
error.H
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::error::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:331
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:243
Foam::errorManip::errorManip
errorManip(void(Err::*fPtr)(), Err &t)
Definition: errorManip.H:77
Foam::error::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:337
Foam::errorManipArg::errorManipArg
errorManipArg(void(Err::*fPtr)(const T), Err &t, const T i)
Definition: errorManip.H:108
Foam::IOerror
Report an I/O error.
Definition: error.H:279
Foam::errorManipArg
Error stream manipulators for functions with an argument.
Definition: errorManip.H:60
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::error
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:73