[ 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 <stdio.h>
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> <<a href="error_8hxx-source.html">vigra/error.hxx</a>>
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  <<a href="error_8hxx-source.html">vigra/error.hxx</a>>
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(char const * prefix, char const * message,
129  char const * file, int line)
130  {
131  sprintf(what_, "\n%.30s\n%.900s\n(%.100s:%d)\n", prefix, message, file, line);
132  }
133 
134  ContractViolation(char const * prefix, char const * message)
135  {
136  sprintf(what_, "\n%.30s\n%.900s\n", prefix, message);
137  }
138 
139  virtual const char * what() const throw()
140  {
141  return what_;
142  }
143 
144  private:
145  enum { bufsize_ = 1100 };
146  char what_[bufsize_];
147 };
148 
149 class PreconditionViolation : public ContractViolation
150 {
151  public:
152  PreconditionViolation(char const * message, const char * file, int line)
153  : ContractViolation("Precondition violation!", message, file, line)
154  {}
155 
156  PreconditionViolation(char const * message)
157  : ContractViolation("Precondition violation!", message)
158  {}
159 };
160 
161 class PostconditionViolation : public ContractViolation
162 {
163  public:
164  PostconditionViolation(char const * message, const char * file, int line)
165  : ContractViolation("Postcondition violation!", message, file, line)
166  {}
167 
168  PostconditionViolation(char const * message)
169  : ContractViolation("Postcondition violation!", message)
170  {}
171 };
172 
173 class InvariantViolation : public ContractViolation
174 {
175  public:
176  InvariantViolation(char const * message, const char * file, int line)
177  : ContractViolation("Invariant violation!", message, file, line)
178  {}
179 
180  InvariantViolation(char const * message)
181  : ContractViolation("Invariant violation!", message)
182  {}
183 };
184 
185 #ifndef NDEBUG
186 
187 inline
188 void throw_invariant_error(bool predicate, char const * message, char const * file, int line)
189 {
190  if(!predicate)
191  throw vigra::InvariantViolation(message, file, line);
192 }
193 
194 inline
195 void throw_invariant_error(bool predicate, std::string message, char const * file, int line)
196 {
197  if(!predicate)
198  throw vigra::InvariantViolation(message.c_str(), file, line);
199 }
200 
201 inline
202 void throw_precondition_error(bool predicate, char const * message, char const * file, int line)
203 {
204  if(!predicate)
205  throw vigra::PreconditionViolation(message, file, line);
206 }
207 
208 inline
209 void throw_precondition_error(bool predicate, std::string message, char const * file, int line)
210 {
211  if(!predicate)
212  throw vigra::PreconditionViolation(message.c_str(), file, line);
213 }
214 
215 inline
216 void throw_postcondition_error(bool predicate, char const * message, char const * file, int line)
217 {
218  if(!predicate)
219  throw vigra::PostconditionViolation(message, file, line);
220 }
221 
222 inline
223 void throw_postcondition_error(bool predicate, std::string message, char const * file, int line)
224 {
225  if(!predicate)
226  throw vigra::PostconditionViolation(message.c_str(), file, line);
227 }
228 
229 inline
230 void throw_runtime_error(char const * message, char const * file, int line)
231 {
232  char what_[1100];
233  sprintf(what_, "\n%.900s\n(%.100s:%d)\n", message, file, line);
234  throw std::runtime_error(what_);
235 }
236 
237 inline
238 void throw_runtime_error(std::string message, char const * file, int line)
239 {
240  char what_[1100];
241  sprintf(what_, "\n%.900s\n(%.100s:%d)\n", message.c_str(), file, line);
242  throw std::runtime_error(what_);
243 }
244 
245 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
246 
247 #define vigra_assert(PREDICATE, MESSAGE) vigra_precondition(PREDICATE, MESSAGE)
248 
249 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
250 
251 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
252 
253 #define vigra_fail(MESSAGE) vigra::throw_runtime_error(MESSAGE, __FILE__, __LINE__)
254 
255 #else // NDEBUG
256 
257 inline
258 void throw_invariant_error(bool predicate, char const * message)
259 {
260  if(!predicate)
261  throw vigra::InvariantViolation(message);
262 }
263 
264 inline
265 void throw_precondition_error(bool predicate, char const * message)
266 {
267  if(!predicate)
268  throw vigra::PreconditionViolation(message);
269 }
270 
271 inline
272 void throw_postcondition_error(bool predicate, char const * message)
273 {
274  if(!predicate)
275  throw vigra::PostconditionViolation(message);
276 }
277 
278 inline
279 void throw_invariant_error(bool predicate, std::string message)
280 {
281  if(!predicate)
282  throw vigra::InvariantViolation(message.c_str());
283 }
284 
285 inline
286 void throw_precondition_error(bool predicate, std::string message)
287 {
288  if(!predicate)
289  throw vigra::PreconditionViolation(message.c_str());
290 }
291 
292 inline
293 void throw_postcondition_error(bool predicate, std::string message)
294 {
295  if(!predicate)
296  throw vigra::PostconditionViolation(message.c_str());
297 }
298 
299 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE)
300 
301 #define vigra_assert(PREDICATE, MESSAGE)
302 
303 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE)
304 
305 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE)
306 
307 #define vigra_fail(MESSAGE) throw std::runtime_error(MESSAGE)
308 
309 #endif // NDEBUG
310 
311 } // namespace vigra
312 
313 #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.7.1 (Wed Mar 12 2014)