initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / error / error.H
blob639bd933c6e75b2af398044d75b402b337c43428
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::error
28 Description
29     Class to handle errors and exceptions in a simple, consistent stream-based
30     manner.
32     The error class is globaly instantiated with a title string. Errors,
33     messages and other data are piped to the messageStream class in the
34     standard manner.  Manipulators are supplied for exit and abort which may
35     terminate the program or throw an exception depending of if the exception
36     handling has beed switched on (off by default).
38 Usage
39     @code
40         error << "message1" << "message2" << FoamDataType << exit(errNo);
41         error << "message1" << "message2" << FoamDataType << abort();
42     @endcode
44 SourceFiles
45     error.C
47 \*---------------------------------------------------------------------------*/
49 #ifndef error_H
50 #define error_H
52 #include "messageStream.H"
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 namespace Foam
59 // Forward declaration of friend functions and operators
60 class error;
61 Ostream& operator<<(Ostream&, const error&);
64 /*---------------------------------------------------------------------------*\
65                            Class error Declaration
66 \*---------------------------------------------------------------------------*/
68 class error
70     public std::exception,
71     public messageStream
74 protected:
76     // Protected data
78         string functionName_;
79         string sourceFileName_;
80         label sourceFileLineNumber_;
82         bool abort_;
84         bool throwExceptions_;
85         OStringStream* messageStreamPtr_;
87 public:
89     // Constructors
91         //- Construct from title string
92         error(const string& title);
94         //- Construct from dictionary
95         error(const dictionary& errDict);
97         //- Construct as copy
98         error(const error& err);
101     // Destructor
103         virtual ~error() throw();
106     // Member functions
108         string message() const;
110         const string& functionName() const
111         {
112             return functionName_;
113         }
115         const string& sourceFileName() const
116         {
117             return sourceFileName_;
118         }
120         label sourceFileLineNumber() const
121         {
122             return sourceFileLineNumber_;
123         }
125         void throwExceptions()
126         {
127             throwExceptions_ = true;
128         }
130         void dontThrowExceptions()
131         {
132             throwExceptions_ = false;
133         }
135         //- Convert to Ostream
136         //  Prints basic message and then returns Ostream for further info.
137         OSstream& operator()
138         (
139             const char* functionName,
140             const char* sourceFileName,
141             const int sourceFileLineNumber = 0
142         );
144         OSstream& operator()
145         (
146             const string& functionName,
147             const char* sourceFileName,
148             const int sourceFileLineNumber = 0
149         );
151         //- Convert to Ostream
152         //  Prints basic message and then returns Ostream for further info.
153         operator OSstream&();
155         //- Explicitly convert to Ostream for << operations
156         OSstream& operator()()
157         {
158             return operator OSstream&();
159         }
161         //- Create and return a dictionary
162         operator dictionary() const;
165         //- Helper function to print a stack
166         static void printStack(Ostream& os);
168         //- Exit : can be called for any error to exit program. Prints stack
169         //  before exiting.
170         void exit(const int errNo = 1);
172         //- Abort : used to stop code for fatal errors. Prints stack before
173         //  exiting.
174         void abort();
177     // Ostream operator
179         friend Ostream& operator<<(Ostream&, const error&);
183 // Forward declaration of friend functions and operators
185 class IOerror;
187 Ostream& operator<<(Ostream&, const IOerror&);
190 /*---------------------------------------------------------------------------*\
191                            Class IOerror Declaration
192 \*---------------------------------------------------------------------------*/
194 //- Report an I/O error
195 class IOerror
197     public error
199     // Private data
201         string ioFileName_;
202         label ioStartLineNumber_;
203         label ioEndLineNumber_;
206 public:
208     // Constructors
210         //- Construct from title string
211         IOerror(const string& title);
213         //- Construct from dictionary
214         IOerror(const dictionary& errDict);
217     // Destructor
219         virtual ~IOerror() throw();
222     // Member functions
224         const string& ioFileName() const
225         {
226             return ioFileName_;
227         }
229         label ioStartLineNumber() const
230         {
231             return ioStartLineNumber_;
232         }
234         label ioEndLineNumber() const
235         {
236             return ioEndLineNumber_;
237         }
239         //- Convert to Ostream
240         //  Prints basic message and then returns Ostream for further info.
241         OSstream& operator()
242         (
243             const char* functionName,
244             const char* sourceFileName,
245             const int sourceFileLineNumber,
246             const string& ioFileName,
247             const label ioStartLineNumber = -1,
248             const label ioEndLineNumber = -1
249         );
251         //- Convert to Ostream
252         //  Prints basic message and then returns Ostream for further info.
253         OSstream& operator()
254         (
255             const char* functionName,
256             const char* sourceFileName,
257             const int sourceFileLineNumber,
258             const IOstream&
259         );
261         //- Convert to Ostream
262         //  Prints basic message and then returns Ostream for further info.
263         OSstream& operator()
264         (
265             const char* functionName,
266             const char* sourceFileName,
267             const int sourceFileLineNumber,
268             const dictionary&
269         );
271         //- Create and return a dictionary
272         operator dictionary() const;
275         //- Exit : can be called for any error to exit program
276         void exit(const int errNo = 1);
278         //- Abort : used to stop code for fatal errors
279         void abort();
282     // Ostream operator
284         friend Ostream& operator<<(Ostream&, const IOerror&);
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 // Global error declarations: defined in error.C
291 extern error FatalError;
292 extern IOerror FatalIOError;
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 // Convienient macros to add the file name and line number to the function name
297 #define FatalErrorIn(fn) FatalError(fn, __FILE__, __LINE__)
298 #define FatalIOErrorIn(fn, ios) FatalIOError(fn, __FILE__, __LINE__, ios)
300 // Call for functions which are not currently implemented.
301 // The functionName is printed and then abort is called.
302 #define notImplemented(fn) \
303     FatalErrorIn(fn) << "Not implemented" << Foam::abort(FatalError);
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
307 } // End namespace Foam
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
311 #include "errorManip.H"
313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315 #endif
317 // ************************************************************************* //