[ 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.3.2, Jan 27 2005 )                                    */
    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 <string>
    00030 #include "vigra/config.hxx"
    00031
    00032 /*! \page ErrorReporting Error Reporting
    00033     Exceptions and assertions provided by VIGRA
    00034
    00035     <b>\#include</b> "<a href="error_8hxx-source.html">vigra/error.hxx</a>"
    00036
    00037     VIGRA defines the following exception classes:
    00038
    00039     \code
    00040     namespace vigra {
    00041         class ContractViolation : public std::exception;
    00042         class PreconditionViolation : public ContractViolation;
    00043         class PostconditionViolation : public ContractViolation;
    00044         class InvariantViolation : public ContractViolation;
    00045     }
    00046     \endcode
    00047
    00048     The following associated macros throw the corresponding exception if
    00049     their PREDICATE evaluates to '<TT>false</TT>':
    00050
    00051     \code
    00052     vigra_precondition(PREDICATE, MESSAGE);
    00053     vigra_postcondition(PREDICATE, MESSAGE);
    00054     vigra_invariant(PREDICATE, MESSAGE);
    00055     \endcode
    00056
    00057     The MESSAGE is passed to the exception and can be retrieved via
    00058     the overloaded member function '<TT>exception.what()</TT>'. If the compiler
    00059     flag '<TT>NDEBUG</TT>' is <em>not</em> defined, the file name and line number of
    00060     the error are automatically included in the message. The macro
    00061
    00062     \code
    00063     vigra_assert(PREDICATE, MESSAGE);
    00064     \endcode
    00065
    00066     is identical to <tt>vigra_precondition()</tt> except that it is completely removed
    00067     when '<TT>NDEBUG</TT>' is defined. This is useful for test that are only needed during
    00068     debugging, such as array index bound checking. The following macro
    00069
    00070     \code
    00071     vigra_fail(MESSAGE);
    00072     \endcode
    00073
    00074     unconditionally throws a '<TT>std::runtime_error</TT>' constructed from the message
    00075     (along with file name and line number, if NDEBUG is not set).
    00076
    00077     <b> Usage:</b>
    00078
    00079     Include-File:
    00080     "<a href="error_8hxx-source.html">vigra/error.hxx</a>"
    00081     <p>
    00082     Namespace: vigra (except for the macros, of course)
    00083
    00084     \code
    00085     int main(int argc, char ** argv)
    00086     {
    00087         try
    00088         {
    00089             const char* input_file_name = argv[1];
    00090
    00091             // read input image
    00092             vigra::ImageImportInfo info(input_file_name);
    00093
    00094             // fail if input image is not grayscale
    00095             vigra_precondition(info.isGrayscale(), "Input image must be grayscale");
    00096
    00097             ...// process image
    00098         }
    00099         catch (std::exception & e)
    00100         {
    00101             std::cerr << e.what() << std::endl; // print message
    00102             return 1;
    00103         }
    00104
    00105         return 0;
    00106     }
    00107     \endcode
    00108 **/
    00109
    00110 namespace vigra {
    00111
    00112 class ContractViolation : public StdException
    00113 {
    00114   public:
    00115     ContractViolation(char const * prefix, char const * message,
    00116                       char const * file, int line)
    00117     {
    00118         sprintf(what_, "\n%.30s\n%.900s\n(%.100s:%d)\n", prefix, message, file, line);
    00119     }
    00120
    00121     ContractViolation(char const * prefix, char const * message)
    00122     {
    00123         sprintf(what_, "\n%.30s\n%.900s\n", prefix, message);
    00124     }
    00125
    00126     virtual const char * what() const throw()
    00127     {
    00128         return what_;
    00129     }
    00130
    00131   private:
    00132     enum { bufsize_ = 1100 };
    00133     char what_[bufsize_];
    00134 };
    00135
    00136 class PreconditionViolation : public ContractViolation
    00137 {
    00138   public:
    00139     PreconditionViolation(char const * message, const char * file, int line)
    00140     : ContractViolation("Precondition violation!", message, file, line)
    00141     {}
    00142
    00143     PreconditionViolation(char const * message)
    00144     : ContractViolation("Precondition violation!", message)
    00145     {}
    00146 };
    00147
    00148 class PostconditionViolation : public ContractViolation
    00149 {
    00150   public:
    00151     PostconditionViolation(char const * message, const char * file, int line)
    00152     : ContractViolation("Postcondition violation!", message, file, line)
    00153     {}
    00154
    00155     PostconditionViolation(char const * message)
    00156     : ContractViolation("Postcondition violation!", message)
    00157     {}
    00158 };
    00159
    00160 class InvariantViolation : public ContractViolation
    00161 {
    00162   public:
    00163     InvariantViolation(char const * message, const char * file, int line)
    00164     : ContractViolation("Invariant violation!", message, file, line)
    00165     {}
    00166
    00167     InvariantViolation(char const * message)
    00168     : ContractViolation("Invariant violation!", message)
    00169     {}
    00170 };
    00171
    00172 #ifndef NDEBUG
    00173
    00174 inline
    00175 void throw_invariant_error(bool predicate, char const * message, char const * file, int line)
    00176 {
    00177     if(!predicate)
    00178        throw vigra::InvariantViolation(message, file, line);
    00179 }
    00180
    00181 inline
    00182 void throw_invariant_error(bool predicate, std::string message, char const * file, int line)
    00183 {
    00184     if(!predicate)
    00185        throw vigra::InvariantViolation(message.c_str(), file, line);
    00186 }
    00187
    00188 inline
    00189 void throw_precondition_error(bool predicate, char const * message, char const * file, int line)
    00190 {
    00191     if(!predicate)
    00192        throw vigra::PreconditionViolation(message, file, line);
    00193 }
    00194
    00195 inline
    00196 void throw_precondition_error(bool predicate, std::string message, char const * file, int line)
    00197 {
    00198     if(!predicate)
    00199        throw vigra::PreconditionViolation(message.c_str(), file, line);
    00200 }
    00201
    00202 inline
    00203 void throw_postcondition_error(bool predicate, char const * message, char const * file, int line)
    00204 {
    00205     if(!predicate)
    00206        throw vigra::PostconditionViolation(message, file, line);
    00207 }
    00208
    00209 inline
    00210 void throw_postcondition_error(bool predicate, std::string message, char const * file, int line)
    00211 {
    00212     if(!predicate)
    00213        throw vigra::PostconditionViolation(message.c_str(), file, line);
    00214 }
    00215
    00216 inline
    00217 void throw_runtime_error(char const * message, char const * file, int line)
    00218 {
    00219     char what_[1100];
    00220     sprintf(what_, "\n%.900s\n(%.100s:%d)\n", message, file, line);
    00221     throw std::runtime_error(what_);
    00222 }
    00223
    00224 inline
    00225 void throw_runtime_error(std::string message, char const * file, int line)
    00226 {
    00227     char what_[1100];
    00228     sprintf(what_, "\n%.900s\n(%.100s:%d)\n", message.c_str(), file, line);
    00229     throw std::runtime_error(what_);
    00230 }
    00231
    00232 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
    00233
    00234 #define vigra_assert(PREDICATE, MESSAGE) vigra_precondition(PREDICATE, MESSAGE)
    00235
    00236 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
    00237
    00238 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
    00239
    00240 #define vigra_fail(MESSAGE) vigra::throw_runtime_error(MESSAGE, __FILE__, __LINE__)
    00241
    00242 #else // NDEBUG
    00243
    00244 inline
    00245 void throw_invariant_error(bool predicate, char const * message)
    00246 {
    00247     if(!predicate)
    00248        throw vigra::InvariantViolation(message);
    00249 }
    00250
    00251 inline
    00252 void throw_precondition_error(bool predicate, char const * message)
    00253 {
    00254     if(!predicate)
    00255        throw vigra::PreconditionViolation(message);
    00256 }
    00257
    00258 inline
    00259 void throw_postcondition_error(bool predicate, char const * message)
    00260 {
    00261     if(!predicate)
    00262        throw vigra::PostconditionViolation(message);
    00263 }
    00264
    00265 inline
    00266 void throw_invariant_error(bool predicate, std::string message)
    00267 {
    00268     if(!predicate)
    00269        throw vigra::InvariantViolation(message.c_str());
    00270 }
    00271
    00272 inline
    00273 void throw_precondition_error(bool predicate, std::string message)
    00274 {
    00275     if(!predicate)
    00276        throw vigra::PreconditionViolation(message.c_str());
    00277 }
    00278
    00279 inline
    00280 void throw_postcondition_error(bool predicate, std::string message)
    00281 {
    00282     if(!predicate)
    00283        throw vigra::PostconditionViolation(message.c_str());
    00284 }
    00285
    00286 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE)
    00287
    00288 #define vigra_assert(PREDICATE, MESSAGE)
    00289
    00290 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE)
    00291
    00292 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE)
    00293
    00294 #define vigra_fail(MESSAGE) throw std::runtime_error(MESSAGE)
    00295
    00296 #endif // NDEBUG
    00297
    00298 } // namespace vigra
    00299
    00300 #endif // VIGRA_ERROR_HXX

© Ullrich Köthe (koethe@informatik.uni-hamburg.de) Cognitive Systems Group, University of Hamburg, Germany | html generated using doxygen and Python VIGRA 1.3.2 (27 Jan 2005)