[ 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 inline
208 void throw_invariant_error(bool predicate, char const * message, char const * file, int line)
209{
210 if(!predicate)
211 throw vigra::InvariantViolation(message, file, line);
212}
213
214 inline
215 void throw_invariant_error(bool predicate, std::string message, char const * file, int line)
216{
217 if(!predicate)
218 throw vigra::InvariantViolation(message.c_str(), file, line);
219}
220
221 inline
222 void throw_precondition_error(bool predicate, char const * message, char const * file, int line)
223{
224 if(!predicate)
225 throw vigra::PreconditionViolation(message, file, line);
226}
227
228 inline
229 void throw_precondition_error(bool predicate, std::string message, char const * file, int line)
230{
231 if(!predicate)
232 throw vigra::PreconditionViolation(message.c_str(), file, line);
233}
234
235 inline
236 void throw_postcondition_error(bool predicate, char const * message, char const * file, int line)
237{
238 if(!predicate)
239 throw vigra::PostconditionViolation(message, file, line);
240}
241
242 inline
243 void throw_postcondition_error(bool predicate, std::string message, char const * file, int line)
244{
245 if(!predicate)
246 throw vigra::PostconditionViolation(message.c_str(), file, line);
247}
248
249 inline
250 void throw_runtime_error(char const * message, char const * file, int line)
251{
252 std::ostringstream what;
253 what << "\n" << message << "\n(" << file << ":" << line << ")\n";
254 throw std::runtime_error(what.str());
255}
256
257 inline
258 void throw_runtime_error(std::string message, char const * file, int line)
259{
260 std::ostringstream what;
261 what << "\n" << message << "\n(" << file << ":" << line << ")\n";
262 throw std::runtime_error(what.str());
263}
264
265#define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
266
267// Compile assertions only in debug mode
268#ifdef NDEBUG
269 #define vigra_assert(PREDICATE, MESSAGE)
270#else
271 #define vigra_assert(PREDICATE, MESSAGE) vigra_precondition(PREDICATE, MESSAGE)
272#endif
273
274#define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
275
276#define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
277
278#define vigra_fail(MESSAGE) vigra::throw_runtime_error(MESSAGE, __FILE__, __LINE__)
279
280} // namespace vigra
281
282#endif // VIGRA_ERROR_HXX
vigra

© 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.11.1