FreeFOAM The Cross-Platform CFD Toolkit
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 | Copyright (C) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/error.H>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward declaration of friend functions and operators
51 
52 template<class Err> class errorManip;
53 template<class Err> Ostream& operator<<(Ostream&, errorManip<Err>);
54 
55 template<class Err, class T> class errorManipArg;
56 template<class Err, class T>
57 Ostream& operator<<(Ostream&, errorManipArg<Err, T>);
58 
59 
60 /*---------------------------------------------------------------------------*\
61  Class errorManip Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 template<class Err>
66 {
67  void (Err::*fPtr_)();
68  Err& err_;
69 
70 public:
71 
72  errorManip(void (Err::*fPtr)(), Err& t)
73  :
74  fPtr_(fPtr),
75  err_(t)
76  {}
77 
78  friend Ostream& operator<< <Err>(Ostream& os, errorManip<Err> m);
79 };
80 
81 
82 template<class Err>
83 inline Ostream& operator<<(Ostream& os, errorManip<Err> m)
84 {
85  (m.err_.*m.fPtr_)();
86  return os;
87 }
88 
89 
90 /*---------------------------------------------------------------------------*\
91  Class errorManipArg Declaration
92 \*---------------------------------------------------------------------------*/
93 
94 //- errorManipArg
95 template<class Err, class T>
97 {
98  void (Err::*fPtr_)(const T);
99  Err& err_;
100  T arg_;
101 
102 public:
103 
104  errorManipArg(void (Err::*fPtr)(const T), Err& t, const T i)
105  :
106  fPtr_(fPtr),
107  err_(t),
108  arg_(i)
109  {}
110 
111  friend Ostream& operator<< <Err, T>(Ostream& os, errorManipArg<Err, T> m);
112 };
113 
114 
115 template<class Err, class T>
116 inline Ostream& operator<<(Ostream& os, errorManipArg<Err, T> m)
117 {
118  (m.err_.*m.fPtr_)(m.arg_);
119  return os;
120 }
121 
122 
123 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
124 
125 inline errorManipArg<error, int> exit(error& err, const int errNo = 1)
126 {
127  return errorManipArg<error, int>(&error::exit, err, errNo);
128 }
129 
131 {
132  return errorManip<error>(&error::abort, err);
133 }
134 
135 
136 inline errorManipArg<IOerror, int> exit(IOerror& err, const int errNo = 1)
137 {
138  return errorManipArg<IOerror, int>(&IOerror::exit, err, errNo);
139 }
140 
142 {
143  return errorManip<IOerror>(&IOerror::abort, err);
144 }
145 
146 
147 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
148 
149 } // End namespace Foam
150 
151 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
152 
153 #endif
154 
155 // ************************ vim: set sw=4 sts=4 et: ************************ //