vigra - vigra/error.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

error.hxx |

1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2002 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36
37 #ifndef VIGRA_ERROR_HXX
38 #define VIGRA_ERROR_HXX
39
40 #include <stdexcept>
41 #include <sstream>
42 #include <string>
43 #include "config.hxx"
44
45 /** \page ErrorReporting Error Reporting
46 Exceptions and assertions provided by VIGRA
47
48 <b>\#include</b> <vigra/error.hxx>
49
50 VIGRA defines the following exception classes:
51
52 \code
53 namespace vigra {
54 class ContractViolation : public std::exception;
55 class PreconditionViolation : public ContractViolation;
56 class PostconditionViolation : public ContractViolation;
57 class InvariantViolation : public ContractViolation;
58 }
59 \endcode
60
61 The following associated macros throw the corresponding exception if
62 their PREDICATE evaluates to '<TT>false</TT>':
63
64 \code
65 vigra_precondition(PREDICATE, MESSAGE);
66 vigra_postcondition(PREDICATE, MESSAGE);
67 vigra_invariant(PREDICATE, MESSAGE);
68 \endcode
69
70 The MESSAGE is passed to the exception and can be retrieved via
71 the overloaded member function '<TT>exception.what()</TT>'. If the compiler
72 flag '<TT>NDEBUG</TT>' is <em>not</em> defined, the file name and line number of
73 the error are automatically included in the message. The macro
74
75 \code
76 vigra_assert(PREDICATE, MESSAGE);
77 \endcode
78
79 is identical to <tt>vigra_precondition()</tt> except that it is completely removed
80 when '<TT>NDEBUG</TT>' is defined. This is useful for test that are only needed during
81 debugging, such as array index bound checking. The following macro
82
83 \code
84 vigra_fail(MESSAGE);
85 \endcode
86
87 unconditionally throws a '<TT>std::runtime_error</TT>' constructed from the message
88 (along with file name and line number, if NDEBUG is not set).
89
90 <b> Usage:</b>
91
92 Include-File:
93 <vigra/error.hxx>
94 <p>
95 Namespace: vigra (except for the macros, of course)
96
97 \code
98 int main(int argc, char ** argv)
99 {
100 try
101 {
102 const char* input_file_name = argv[1];
103
104 // read input image
105 vigra::ImageImportInfo info(input_file_name);
106
107 // fail if input image is not grayscale
108 vigra_precondition(info.isGrayscale(), "Input image must be grayscale");
109
110 ...// process image
111 }
112 catch (std::exception & e)
113 {
114 std::cerr << e.what() << std::endl; // print message
115 return 1;
116 }
117
118 return 0;
119 }
120 \endcode
121 **/
122
123 namespace vigra {
124
125 class ContractViolation : public StdException
126 {
127 public:
128 ContractViolation()
129 {}
130
131 ContractViolation(char const * prefix, char const * message,
132 char const * file, int line)
133 {
134 (*this) << "\n" << prefix << "\n" << message << "\n("
135 << file << ":" << line << ")\n";
136 }
137
138 ContractViolation(char const * prefix, char const * message)
139 {
140 (*this) << "\n" << prefix << "\n" << message << "\n";
141 }
142
143 ~ContractViolation() throw()
144 {}
145
146 template<class T>
147 ContractViolation & operator<<(T const & data)
148 {
149 std::ostringstream what;
150 what << data;
151 what_ += what.str();
152 return *this;
153 }
154
155 virtual const char * what() const throw()
156 {
157 try
158 {
159 return what_.c_str();
160 }
161 catch(...)
162 {
163 return "vigra::ContractViolation: error message was lost, sorry.";
164 }
165 }
166
167 private:
168 std::string what_;
169 };
170
171 class PreconditionViolation : public ContractViolation
172 {
173 public:
174 PreconditionViolation(char const * message, const char * file, int line)
175 : ContractViolation("Precondition violation!", message, file, line)
176 {}
177
178 PreconditionViolation(char const * message)
179 : ContractViolation("Precondition violation!", message)
180 {}
181 };
182
183 class PostconditionViolation : public ContractViolation
184 {
185 public:
186 PostconditionViolation(char const * message, const char * file, int line)
187 : ContractViolation("Postcondition violation!", message, file, line)
188 {}
189
190 PostconditionViolation(char const * message)
191 : ContractViolation("Postcondition violation!", message)
192 {}
193 };
194
195 class InvariantViolation : public ContractViolation
196 {
197 public:
198 InvariantViolation(char const * message, const char * file, int line)
199 : ContractViolation("Invariant violation!", message, file, line)
200 {}
201
202 InvariantViolation(char const * message)
203 : ContractViolation("Invariant violation!", message)
204 {}
205 };
206
207 //#ifndef NDEBUG
208
209 #if 1
210
211 inline
212 void throw_invariant_error(bool predicate, char const * message, char const * file, int line)
213 {
214 if(!predicate)
215 throw vigra::InvariantViolation(message, file, line);
216 }
217
218 inline
219 void throw_invariant_error(bool predicate, std::string message, char const * file, int line)
220 {
221 if(!predicate)
222 throw vigra::InvariantViolation(message.c_str(), file, line);
223 }
224
225 inline
226 void throw_precondition_error(bool predicate, char const * message, char const * file, int line)
227 {
228 if(!predicate)
229 throw vigra::PreconditionViolation(message, file, line);
230 }
231
232 inline
233 void throw_precondition_error(bool predicate, std::string message, char const * file, int line)
234 {
235 if(!predicate)
236 throw vigra::PreconditionViolation(message.c_str(), file, line);
237 }
238
239 inline
240 void throw_postcondition_error(bool predicate, char const * message, char const * file, int line)
241 {
242 if(!predicate)
243 throw vigra::PostconditionViolation(message, file, line);
244 }
245
246 inline
247 void throw_postcondition_error(bool predicate, std::string message, char const * file, int line)
248 {
249 if(!predicate)
250 throw vigra::PostconditionViolation(message.c_str(), file, line);
251 }
252
253 inline
254 void throw_runtime_error(char const * message, char const * file, int line)
255 {
256 std::ostringstream what;
257 what << "\n" << message << "\n(" << file << ":" << line << ")\n";
258 throw std::runtime_error(what.str());
259 }
260
261 inline
262 void throw_runtime_error(std::string message, char const * file, int line)
263 {
264 std::ostringstream what;
265 what << "\n" << message << "\n(" << file << ":" << line << ")\n";
266 throw std::runtime_error(what.str());
267 }
268
269 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
270
271 #define vigra_assert(PREDICATE, MESSAGE) vigra_precondition(PREDICATE, MESSAGE)
272
273 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
274
275 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
276
277 #define vigra_fail(MESSAGE) vigra::throw_runtime_error(MESSAGE, __FILE__, __LINE__)
278
279 #else // NDEBUG
280
281 inline
282 void throw_invariant_error(bool predicate, char const * message)
283 {
284 if(!predicate)
285 throw vigra::InvariantViolation(message);
286 }
287
288 inline
289 void throw_precondition_error(bool predicate, char const * message)
290 {
291 if(!predicate)
292 throw vigra::PreconditionViolation(message);
293 }
294
295 inline
296 void throw_postcondition_error(bool predicate, char const * message)
297 {
298 if(!predicate)
299 throw vigra::PostconditionViolation(message);
300 }
301
302 inline
303 void throw_invariant_error(bool predicate, std::string message)
304 {
305 if(!predicate)
306 throw vigra::InvariantViolation(message.c_str());
307 }
308
309 inline
310 void throw_precondition_error(bool predicate, std::string message)
311 {
312 if(!predicate)
313 throw vigra::PreconditionViolation(message.c_str());
314 }
315
316 inline
317 void throw_postcondition_error(bool predicate, std::string message)
318 {
319 if(!predicate)
320 throw vigra::PostconditionViolation(message.c_str());
321 }
322
323 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE)
324
325 #define vigra_assert(PREDICATE, MESSAGE)
326
327 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE)
328
329 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE)
330
331 #define vigra_fail(MESSAGE) throw std::runtime_error(MESSAGE)
332
333 #endif // NDEBUG
334
335 } // namespace vigra
336
337 #endif // VIGRA_ERROR_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.10.0 (Mon Nov 18 2013)