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>
64 Ostream& operator<<(Ostream&, errorManipArg<Err, T>);
65 
66 
67 /*---------------------------------------------------------------------------*\
68  Class errorManip Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 template<class Err>
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>
90 inline Ostream& operator<<(Ostream& os, errorManip<Err> m)
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>
122 inline Ostream& operator<<(Ostream& os, errorManipArg<Err, T> m)
123 {
124  (m.err_.*m.fPtr_)(m.arg_);
125  return os;
126 }
127 
128 
129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 
131 inline errorManipArg<error, int>
132 exit(error& err, const int errNo = 1)
133 {
134  return errorManipArg<error, int>(&error::exit, err, errNo);
135 }
136 
137 
138 inline errorManipArg<IOerror, int>
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>
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 // ************************************************************************* //
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:245
Error stream manipulators for functions with an argument.
Definition: errorManip.H:55
errorManip(void(Err::*fPtr)(), Err &t)
Definition: errorManip.H:72
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:343
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:70
errorManipArg(void(Err::*fPtr)(const T), Err &t, const T i)
Definition: errorManip.H:103
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:349
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Report an I/O error.
Definition: error.H:370
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:239
OBJstream os(runTime.globalPath()/outputName)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Error stream manipulators for exit and abort which may terminate the program or throw an exception de...
Definition: errorManip.H:52
Namespace for OpenFOAM.