[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]
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.6.0, Aug 13 2008 ) */ 00008 /* The VIGRA Website is */ 00009 /* http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ */ 00010 /* Please direct questions, bug reports, and contributions to */ 00011 /* ullrich.koethe@iwr.uni-heidelberg.de or */ 00012 /* vigra@informatik.uni-hamburg.de */ 00013 /* */ 00014 /* Permission is hereby granted, free of charge, to any person */ 00015 /* obtaining a copy of this software and associated documentation */ 00016 /* files (the "Software"), to deal in the Software without */ 00017 /* restriction, including without limitation the rights to use, */ 00018 /* copy, modify, merge, publish, distribute, sublicense, and/or */ 00019 /* sell copies of the Software, and to permit persons to whom the */ 00020 /* Software is furnished to do so, subject to the following */ 00021 /* conditions: */ 00022 /* */ 00023 /* The above copyright notice and this permission notice shall be */ 00024 /* included in all copies or substantial portions of the */ 00025 /* Software. */ 00026 /* */ 00027 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */ 00028 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */ 00029 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 00030 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */ 00031 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */ 00032 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */ 00033 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */ 00034 /* OTHER DEALINGS IN THE SOFTWARE. */ 00035 /* */ 00036 /************************************************************************/ 00037 00038 00039 #ifndef VIGRA_ERROR_HXX 00040 #define VIGRA_ERROR_HXX 00041 00042 #include <stdexcept> 00043 #include <stdio.h> 00044 #include <string> 00045 #include "config.hxx" 00046 00047 /*! \page ErrorReporting Error Reporting 00048 Exceptions and assertions provided by VIGRA 00049 00050 <b>\#include</b> <<a href="error_8hxx-source.html">vigra/error.hxx</a>> 00051 00052 VIGRA defines the following exception classes: 00053 00054 \code 00055 namespace vigra { 00056 class ContractViolation : public std::exception; 00057 class PreconditionViolation : public ContractViolation; 00058 class PostconditionViolation : public ContractViolation; 00059 class InvariantViolation : public ContractViolation; 00060 } 00061 \endcode 00062 00063 The following associated macros throw the corresponding exception if 00064 their PREDICATE evaluates to '<TT>false</TT>': 00065 00066 \code 00067 vigra_precondition(PREDICATE, MESSAGE); 00068 vigra_postcondition(PREDICATE, MESSAGE); 00069 vigra_invariant(PREDICATE, MESSAGE); 00070 \endcode 00071 00072 The MESSAGE is passed to the exception and can be retrieved via 00073 the overloaded member function '<TT>exception.what()</TT>'. If the compiler 00074 flag '<TT>NDEBUG</TT>' is <em>not</em> defined, the file name and line number of 00075 the error are automatically included in the message. The macro 00076 00077 \code 00078 vigra_assert(PREDICATE, MESSAGE); 00079 \endcode 00080 00081 is identical to <tt>vigra_precondition()</tt> except that it is completely removed 00082 when '<TT>NDEBUG</TT>' is defined. This is useful for test that are only needed during 00083 debugging, such as array index bound checking. The following macro 00084 00085 \code 00086 vigra_fail(MESSAGE); 00087 \endcode 00088 00089 unconditionally throws a '<TT>std::runtime_error</TT>' constructed from the message 00090 (along with file name and line number, if NDEBUG is not set). 00091 00092 <b> Usage:</b> 00093 00094 Include-File: 00095 <<a href="error_8hxx-source.html">vigra/error.hxx</a>> 00096 <p> 00097 Namespace: vigra (except for the macros, of course) 00098 00099 \code 00100 int main(int argc, char ** argv) 00101 { 00102 try 00103 { 00104 const char* input_file_name = argv[1]; 00105 00106 // read input image 00107 vigra::ImageImportInfo info(input_file_name); 00108 00109 // fail if input image is not grayscale 00110 vigra_precondition(info.isGrayscale(), "Input image must be grayscale"); 00111 00112 ...// process image 00113 } 00114 catch (std::exception & e) 00115 { 00116 std::cerr << e.what() << std::endl; // print message 00117 return 1; 00118 } 00119 00120 return 0; 00121 } 00122 \endcode 00123 **/ 00124 00125 namespace vigra { 00126 00127 class ContractViolation : public StdException 00128 { 00129 public: 00130 ContractViolation(char const * prefix, char const * message, 00131 char const * file, int line) 00132 { 00133 sprintf(what_, "\n%.30s\n%.900s\n(%.100s:%d)\n", prefix, message, file, line); 00134 } 00135 00136 ContractViolation(char const * prefix, char const * message) 00137 { 00138 sprintf(what_, "\n%.30s\n%.900s\n", prefix, message); 00139 } 00140 00141 virtual const char * what() const throw() 00142 { 00143 return what_; 00144 } 00145 00146 private: 00147 enum { bufsize_ = 1100 }; 00148 char what_[bufsize_]; 00149 }; 00150 00151 class PreconditionViolation : public ContractViolation 00152 { 00153 public: 00154 PreconditionViolation(char const * message, const char * file, int line) 00155 : ContractViolation("Precondition violation!", message, file, line) 00156 {} 00157 00158 PreconditionViolation(char const * message) 00159 : ContractViolation("Precondition violation!", message) 00160 {} 00161 }; 00162 00163 class PostconditionViolation : public ContractViolation 00164 { 00165 public: 00166 PostconditionViolation(char const * message, const char * file, int line) 00167 : ContractViolation("Postcondition violation!", message, file, line) 00168 {} 00169 00170 PostconditionViolation(char const * message) 00171 : ContractViolation("Postcondition violation!", message) 00172 {} 00173 }; 00174 00175 class InvariantViolation : public ContractViolation 00176 { 00177 public: 00178 InvariantViolation(char const * message, const char * file, int line) 00179 : ContractViolation("Invariant violation!", message, file, line) 00180 {} 00181 00182 InvariantViolation(char const * message) 00183 : ContractViolation("Invariant violation!", message) 00184 {} 00185 }; 00186 00187 #ifndef NDEBUG 00188 00189 inline 00190 void throw_invariant_error(bool predicate, char const * message, char const * file, int line) 00191 { 00192 if(!predicate) 00193 throw vigra::InvariantViolation(message, file, line); 00194 } 00195 00196 inline 00197 void throw_invariant_error(bool predicate, std::string message, char const * file, int line) 00198 { 00199 if(!predicate) 00200 throw vigra::InvariantViolation(message.c_str(), file, line); 00201 } 00202 00203 inline 00204 void throw_precondition_error(bool predicate, char const * message, char const * file, int line) 00205 { 00206 if(!predicate) 00207 throw vigra::PreconditionViolation(message, file, line); 00208 } 00209 00210 inline 00211 void throw_precondition_error(bool predicate, std::string message, char const * file, int line) 00212 { 00213 if(!predicate) 00214 throw vigra::PreconditionViolation(message.c_str(), file, line); 00215 } 00216 00217 inline 00218 void throw_postcondition_error(bool predicate, char const * message, char const * file, int line) 00219 { 00220 if(!predicate) 00221 throw vigra::PostconditionViolation(message, file, line); 00222 } 00223 00224 inline 00225 void throw_postcondition_error(bool predicate, std::string message, char const * file, int line) 00226 { 00227 if(!predicate) 00228 throw vigra::PostconditionViolation(message.c_str(), file, line); 00229 } 00230 00231 inline 00232 void throw_runtime_error(char const * message, char const * file, int line) 00233 { 00234 char what_[1100]; 00235 sprintf(what_, "\n%.900s\n(%.100s:%d)\n", message, file, line); 00236 throw std::runtime_error(what_); 00237 } 00238 00239 inline 00240 void throw_runtime_error(std::string message, char const * file, int line) 00241 { 00242 char what_[1100]; 00243 sprintf(what_, "\n%.900s\n(%.100s:%d)\n", message.c_str(), file, line); 00244 throw std::runtime_error(what_); 00245 } 00246 00247 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__) 00248 00249 #define vigra_assert(PREDICATE, MESSAGE) vigra_precondition(PREDICATE, MESSAGE) 00250 00251 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__) 00252 00253 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE, __FILE__, __LINE__) 00254 00255 #define vigra_fail(MESSAGE) vigra::throw_runtime_error(MESSAGE, __FILE__, __LINE__) 00256 00257 #else // NDEBUG 00258 00259 inline 00260 void throw_invariant_error(bool predicate, char const * message) 00261 { 00262 if(!predicate) 00263 throw vigra::InvariantViolation(message); 00264 } 00265 00266 inline 00267 void throw_precondition_error(bool predicate, char const * message) 00268 { 00269 if(!predicate) 00270 throw vigra::PreconditionViolation(message); 00271 } 00272 00273 inline 00274 void throw_postcondition_error(bool predicate, char const * message) 00275 { 00276 if(!predicate) 00277 throw vigra::PostconditionViolation(message); 00278 } 00279 00280 inline 00281 void throw_invariant_error(bool predicate, std::string message) 00282 { 00283 if(!predicate) 00284 throw vigra::InvariantViolation(message.c_str()); 00285 } 00286 00287 inline 00288 void throw_precondition_error(bool predicate, std::string message) 00289 { 00290 if(!predicate) 00291 throw vigra::PreconditionViolation(message.c_str()); 00292 } 00293 00294 inline 00295 void throw_postcondition_error(bool predicate, std::string message) 00296 { 00297 if(!predicate) 00298 throw vigra::PostconditionViolation(message.c_str()); 00299 } 00300 00301 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE) 00302 00303 #define vigra_assert(PREDICATE, MESSAGE) 00304 00305 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE) 00306 00307 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE) 00308 00309 #define vigra_fail(MESSAGE) throw std::runtime_error(MESSAGE) 00310 00311 #endif // NDEBUG 00312 00313 } // namespace vigra 00314 00315 #endif // VIGRA_ERROR_HXX
© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) |
html generated using doxygen and Python
|