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