[ VIGRA Homepage | Class Index | Function Index | File Index | Main Page ]
![]() |
vigra/error.hxx | ![]() |
---|
00001 /************************************************************************/ 00002 /* */ 00003 /* Copyright 1998-2002 by Ullrich Koethe */ 00004 /* Cognitive Systems Group, University of Hamburg, Germany */ 00005 /* */ 00006 /* This file is part of the VIGRA computer vision library. */ 00007 /* ( Version 1.2.0, Aug 07 2003 ) */ 00008 /* You may use, modify, and distribute this software according */ 00009 /* to the terms stated in the LICENSE file included in */ 00010 /* the VIGRA distribution. */ 00011 /* */ 00012 /* The VIGRA Website is */ 00013 /* http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ */ 00014 /* Please direct questions, bug reports, and contributions to */ 00015 /* koethe@informatik.uni-hamburg.de */ 00016 /* */ 00017 /* THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR */ 00018 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ 00019 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ 00020 /* */ 00021 /************************************************************************/ 00022 00023 00024 #ifndef VIGRA_ERROR_HXX 00025 #define VIGRA_ERROR_HXX 00026 00027 #include <stdexcept> 00028 #include <stdio.h> 00029 #include "vigra/config.hxx" 00030 00031 /*! \page ErrorReporting Error Reporting 00032 Exceptions and assertions provided by VIGRA 00033 00034 <b>\#include</b> "<a href="error_8hxx-source.html">vigra/error.hxx</a>" 00035 00036 VIGRA defines the following exception classes: 00037 00038 \code 00039 namespace vigra { 00040 class ContractViolation : public std::exception; 00041 class PreconditionViolation : public ContractViolation; 00042 class PostconditionViolation : public ContractViolation; 00043 class InvariantViolation : public ContractViolation; 00044 } 00045 \endcode 00046 00047 The following associated macros throw the corresponding exception if 00048 their PREDICATE evaluates to '<TT>false</TT>': 00049 00050 \code 00051 vigra_precondition(PREDICATE, MESSAGE); 00052 vigra_postcondition(PREDICATE, MESSAGE); 00053 vigra_invariant(PREDICATE, MESSAGE); 00054 \endcode 00055 00056 The MESSAGE is passed to the exception and can be retrieved via 00057 the overloaded member function '<TT>exception.what()</TT>'. If the compiler 00058 flag '<TT>NDEBUG</TT>' is <em>not</em> defined, the file name and line number of 00059 the error are automatically included in the message. 00060 00061 The following macro 00062 00063 \code 00064 vigra_fail(MESSAGE); 00065 \endcode 00066 00067 unconditionally throws a '<TT>std::runtime_error</TT>' constructed from the message 00068 (along with file name and line number, if NDEBUG is not set). 00069 00070 <b> Usage:</b> 00071 00072 Include-File: 00073 "<a href="error_8hxx-source.html">vigra/error.hxx</a>" 00074 <p> 00075 Namespace: vigra (except for the macros, of course) 00076 00077 \code 00078 int main(int argc, char ** argv) 00079 { 00080 try 00081 { 00082 const char* input_file_name = argv[1]; 00083 00084 // read input image 00085 vigra::ImageImportInfo info(input_file_name); 00086 00087 // fail if input image is not grayscale 00088 vigra_precondition(info.isGrayscale(), "Input image must be grayscale"); 00089 00090 ...// process image 00091 } 00092 catch (std::exception & e) 00093 { 00094 std::cerr << e.what() << std::endl; // print message 00095 return 1; 00096 } 00097 00098 return 0; 00099 } 00100 \endcode 00101 **/ 00102 00103 namespace vigra { 00104 00105 class ContractViolation : public StdException 00106 { 00107 public: 00108 ContractViolation(char const * prefix, char const * message, 00109 char const * file, int line) 00110 { 00111 sprintf(what_, "\n%.30s\n%.900s\n(%.100s:%d)\n", prefix, message, file, line); 00112 } 00113 00114 ContractViolation(char const * prefix, char const * message) 00115 { 00116 sprintf(what_, "\n%.30s\n%.900s\n", prefix, message); 00117 } 00118 00119 virtual const char * what() const throw() 00120 { 00121 return what_; 00122 } 00123 00124 private: 00125 enum { bufsize_ = 1100 }; 00126 char what_[bufsize_]; 00127 }; 00128 00129 class PreconditionViolation : public ContractViolation 00130 { 00131 public: 00132 PreconditionViolation(char const * message, const char * file, int line) 00133 : ContractViolation("Precondition violation!", message, file, line) 00134 {} 00135 00136 PreconditionViolation(char const * message) 00137 : ContractViolation("Precondition violation!", message) 00138 {} 00139 }; 00140 00141 class PostconditionViolation : public ContractViolation 00142 { 00143 public: 00144 PostconditionViolation(char const * message, const char * file, int line) 00145 : ContractViolation("Postcondition violation!", message, file, line) 00146 {} 00147 00148 PostconditionViolation(char const * message) 00149 : ContractViolation("Postcondition violation!", message) 00150 {} 00151 }; 00152 00153 class InvariantViolation : public ContractViolation 00154 { 00155 public: 00156 InvariantViolation(char const * message, const char * file, int line) 00157 : ContractViolation("Invariant violation!", message, file, line) 00158 {} 00159 00160 InvariantViolation(char const * message) 00161 : ContractViolation("Invariant violation!", message) 00162 {} 00163 }; 00164 00165 #ifndef NDEBUG 00166 00167 inline 00168 void throw_invariant_error(bool predicate, char const * message, char const * file, int line) 00169 { 00170 if(!predicate) 00171 throw vigra::InvariantViolation(message, file, line); 00172 } 00173 00174 inline 00175 void throw_precondition_error(bool predicate, char const * message, char const * file, int line) 00176 { 00177 if(!predicate) 00178 throw vigra::PreconditionViolation(message, file, line); 00179 } 00180 00181 inline 00182 void throw_postcondition_error(bool predicate, char const * message, char const * file, int line) 00183 { 00184 if(!predicate) 00185 throw vigra::PostconditionViolation(message, file, line); 00186 } 00187 00188 inline 00189 void throw_runtime_error(char const * message, char const * file, int line) 00190 { 00191 char what_[1100]; 00192 sprintf(what_, "\n%.900s\n(%.100s:%d)\n", message, file, line); 00193 throw std::runtime_error(what_); 00194 } 00195 00196 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__) 00197 00198 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__) 00199 00200 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE, __FILE__, __LINE__) 00201 00202 #define vigra_fail(MESSAGE) vigra::throw_runtime_error(MESSAGE, __FILE__, __LINE__) 00203 00204 #else // NDEBUG 00205 00206 inline 00207 void throw_invariant_error(bool predicate, char const * message) 00208 { 00209 if(!predicate) 00210 throw vigra::InvariantViolation(message); 00211 } 00212 00213 inline 00214 void throw_precondition_error(bool predicate, char const * message) 00215 { 00216 if(!predicate) 00217 throw vigra::PreconditionViolation(message); 00218 } 00219 00220 inline 00221 void throw_postcondition_error(bool predicate, char const * message) 00222 { 00223 if(!predicate) 00224 throw vigra::PostconditionViolation(message); 00225 } 00226 00227 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE) 00228 00229 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE) 00230 00231 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE) 00232 00233 #define vigra_fail(MESSAGE) throw std::runtime_error(MESSAGE) 00234 00235 #endif // NDEBUG 00236 00237 } // namespace vigra 00238 00239 #endif // VIGRA_ERROR_HXX
© Ullrich Köthe (koethe@informatik.uni-hamburg.de) |
html generated using doxygen and Python
|