[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

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