lemon/error.h
author alpar
Thu, 28 Jul 2005 19:04:43 +0000
changeset 1603 5ad84fbadf2b
parent 1435 8e85e6bbefdf
child 1746 874e4bc21435
permissions -rw-r--r--
More docs
     1 /* -*- C++ -*-
     2  * lemon/error.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi
     5  * Kutatocsoport (Egervary Research Group on Combinatorial Optimization,
     6  * EGRES).
     7  *
     8  * Permission to use, modify and distribute this software is granted
     9  * provided that this copyright notice appears in all copies. For
    10  * precise terms see the accompanying LICENSE file.
    11  *
    12  * This software is provided "AS IS" with no warranty of any kind,
    13  * express or implied, and with no claim as to its suitability for any
    14  * purpose.
    15  *
    16  */
    17 
    18 #ifndef LEMON_ERROR_H
    19 #define LEMON_ERROR_H
    20 
    21 //! \ingroup exceptions
    22 //! \file
    23 //! \brief Basic exception classes and error handling.
    24 
    25 #include <exception>
    26 #include <string>
    27 #include <sstream>
    28 #include <iostream>
    29 #include <cstdlib>
    30 #include <memory>
    31 
    32 namespace lemon {
    33 
    34   /// \addtogroup exceptions
    35   /// @{
    36   
    37   /// \brief Exception safe wrapper class.
    38   ///
    39   /// Exception safe wrapper class to implement the members of exceptions.
    40   template <typename _Type>
    41   class ExceptionMember {
    42   public:
    43     typedef _Type Type;
    44 
    45     ExceptionMember() throw () {
    46       try {
    47 	ptr.reset(new Type());
    48       } catch (...) {}
    49     }
    50 
    51     ExceptionMember(const Type& type) throw () {
    52       try {
    53 	ptr.reset(new Type());
    54 	if (ptr.get() == 0) return;
    55 	*ptr = type;
    56       } catch (...) {}
    57     }
    58 
    59     ExceptionMember(const ExceptionMember& copy) throw() {
    60       try {
    61 	if (!copy.valid()) return;
    62 	ptr.reset(new Type());
    63 	if (ptr.get() == 0) return;
    64 	*ptr = copy.get();
    65       } catch (...) {}
    66     }
    67 
    68     ExceptionMember& operator=(const ExceptionMember& copy) {
    69       if (ptr.get() == 0) return;
    70       try {
    71 	if (!copy.valid()) return;
    72  	*ptr = copy.get();
    73       } catch (...) {}
    74     }
    75 
    76     void set(const Type& type) {
    77       if (ptr.get() == 0) return;
    78       try {
    79 	*ptr = type;
    80       } catch (...) {}
    81     }
    82 
    83     const Type& get() const {
    84       return *ptr;
    85     }
    86 
    87     bool valid() const {
    88       return ptr.get() != 0;
    89     }
    90     
    91   private:
    92     std::auto_ptr<_Type> ptr;
    93   };
    94 
    95   /// Exception-safe convenient "error message" class.
    96 
    97   /// Helper class which provides a convenient ostream-like (operator <<
    98   /// based) interface to create a string message. Mostly useful in
    99   /// exception classes (therefore the name).
   100   class ErrorMessage {
   101   protected:
   102     ///\e 
   103 
   104     ///\todo The good solution is boost::shared_ptr...
   105     ///
   106     mutable
   107     std::auto_ptr<std::ostringstream> buf;
   108     
   109     ///\e 
   110     bool init() throw() {
   111       try {
   112 	buf.reset(new std::ostringstream);
   113       }
   114       catch(...) {
   115 	buf.reset();
   116       }
   117       return buf.get();
   118     }
   119 
   120   public:
   121 
   122     ///\e 
   123     ErrorMessage() throw() { init(); }
   124 
   125     ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
   126 
   127     ///\e 
   128     ErrorMessage(const char *message) throw() {
   129       init();
   130       *this << message;
   131     }
   132 
   133     ///\e 
   134     ErrorMessage(const std::string &message) throw() {
   135       init();
   136       *this << message;
   137     }
   138 
   139     ///\e 
   140     template <typename T>
   141     ErrorMessage& operator<<(const T &t) throw() {
   142       if( ! buf.get() ) return *this;
   143 
   144       try {
   145 	*buf << t;
   146       }
   147       catch(...) {
   148 	buf.reset();
   149       }
   150       return *this;
   151     }
   152 
   153     ///\e 
   154     const char* message() throw() {
   155       if( ! buf.get() ) return 0;
   156 
   157       const char* mes = 0;
   158       try {
   159 	mes = buf->str().c_str();
   160       }
   161       catch(...) {}
   162       return mes;
   163     }
   164     
   165   };
   166 
   167   /**
   168    * \brief Generic exception class.
   169    *
   170    * Base class for exceptions used in LEMON.
   171    */
   172   class Exception : public std::exception {
   173   public:
   174     ///\e 
   175     Exception() {}
   176     ///\e 
   177     virtual ~Exception() throw() {}
   178 
   179     ///\e
   180     virtual const char* exceptionName() const {
   181       return "lemon::Exception";
   182     }
   183     
   184     ///\e 
   185     virtual const char* what() const throw() {
   186       return exceptionName();
   187     }
   188   };
   189 
   190   /**
   191    * \brief One of the two main subclasses of \ref Exception.
   192    *
   193    * Logic errors represent problems in the internal logic of a program;
   194    * in theory, these are preventable, and even detectable before the
   195    * program runs (e.g., violations of class invariants).
   196    *
   197    * A typical example for this is \ref UninitializedParameter.
   198    */
   199   class LogicError : public Exception {
   200   public:
   201     virtual const char* exceptionName() const {
   202       return "lemon::LogicError";
   203     }
   204   };
   205 
   206   /**
   207    * \brief \ref Exception for uninitialized parameters.
   208    *
   209    * This error represents problems in the initialization
   210    * of the parameters of the algorithms.
   211    */
   212   class UninitializedParameter : public LogicError {
   213   public:
   214     virtual const char* exceptionName() const {
   215       return "lemon::UninitializedParameter";
   216     }
   217   };
   218 
   219   
   220   /**
   221    * \brief One of the two main subclasses of \ref Exception.
   222    *
   223    * Runtime errors represent problems outside the scope of a program;
   224    * they cannot be easily predicted and can generally only be caught as
   225    * the program executes.
   226    */
   227   class RuntimeError : public Exception {
   228   public:
   229     virtual const char* exceptionName() const {
   230       return "lemon::RuntimeError";
   231     }
   232   };
   233 
   234   ///\e
   235   class RangeError : public RuntimeError {
   236   public:
   237     virtual const char* exceptionName() const {
   238       return "lemon::RangeError";
   239     }
   240   };
   241 
   242   ///\e 
   243   class IOError : public RuntimeError {
   244   public:
   245     virtual const char* exceptionName() const {
   246       return "lemon::IOError";
   247     }
   248   };
   249 
   250   ///\e 
   251   class DataFormatError : public IOError {
   252   protected:
   253     ExceptionMember<std::string> _message;
   254     ExceptionMember<std::string> _file;
   255     int _line;
   256 
   257     mutable ExceptionMember<std::string> _message_holder;
   258   public:
   259 
   260     DataFormatError(const DataFormatError &dfe) : 
   261       IOError(dfe), _message(dfe._message), _file(dfe._file),
   262       _line(dfe._line) {}
   263 
   264     ///\e 
   265     explicit DataFormatError(const char *the_message)
   266       : _message(the_message), _line(0) {}
   267 
   268     ///\e 
   269     DataFormatError(const std::string &file_name, int line_num,
   270 		    const char *the_message)
   271       : _message(the_message), _line(line_num) { file(file_name); }
   272 
   273     ///\e 
   274     void line(int line) { _line = line; }
   275     ///\e 
   276     void message(const std::string& message) { _message.set(message); }
   277     ///\e 
   278     void file(const std::string &file) { _file.set(file); }
   279  
   280     ///\e
   281     int line() const { return _line; }
   282     ///\e
   283     const char* message() const { 
   284       if (_message.valid() && !_message.get().empty()) {
   285 	return _message.get().c_str();
   286       } else {
   287 	return 0;
   288       }
   289     }
   290 
   291     /// \brief Returns the filename.
   292     ///
   293     /// Returns \e null if the filename was not specified.
   294     const char* file() const {
   295       if (_file.valid() && !_file.get().empty()) {
   296 	return _file.get().c_str();
   297       } else {
   298 	return 0;
   299       }
   300     }
   301 
   302     ///\e 
   303     virtual const char* what() const throw() {
   304       try {
   305 	std::ostringstream ostr;
   306 	ostr << exceptionName() << ": ";
   307 	if (message()) ostr << message();
   308 	if( file() || line() != 0 ) {
   309 	  ostr << " (";
   310 	  if( file() ) ostr << "in file '" << file() << "'";
   311 	  if( file() && line() != 0 ) ostr << " ";
   312 	  if( line() != 0 ) ostr << "at line " << line();
   313 	  ostr << ")";
   314 	}
   315 	_message_holder.set(ostr.str());
   316       }
   317       catch (...) {}
   318       if( _message_holder.valid()) return _message_holder.get().c_str();
   319       return exceptionName();
   320     }
   321 
   322     virtual const char* exceptionName() const {
   323       return "lemon::DataFormatError";
   324     }
   325 
   326     virtual ~DataFormatError() throw() {}
   327   };
   328 
   329   class IOParameterError : public LogicError {
   330   protected:
   331     ExceptionMember<std::string> _message;
   332     ExceptionMember<std::string> _file;
   333 
   334     mutable ExceptionMember<std::string> _message_holder;
   335   public:
   336 
   337     IOParameterError(const IOParameterError &ile) : 
   338       LogicError(ile), _message(ile._message), _file(ile._file) {}
   339 
   340     ///\e 
   341     explicit IOParameterError(const char *the_message)
   342       : _message(the_message) {}
   343 
   344     ///\e 
   345     IOParameterError(const char *file_name, const char *the_message)
   346       : _message(the_message), _file(file_name) {}
   347 
   348      ///\e 
   349     void message(const std::string& message) { _message.set(message); }
   350     ///\e 
   351     void file(const std::string &file) { _file.set(file); }
   352  
   353      ///\e
   354     const char* message() const { 
   355       if (_message.valid()) {
   356 	return _message.get().c_str();
   357       } else {
   358 	return 0;
   359       }
   360     }
   361 
   362     /// \brief Returns the filename.
   363     ///
   364     /// Returns \e null if the filename was not specified.
   365     const char* file() const {
   366       if (_file.valid()) {
   367 	return _file.get().c_str();
   368       } else {
   369 	return 0;
   370       }
   371     }
   372 
   373     ///\e 
   374     virtual const char* what() const throw() {
   375       try {
   376 	std::ostringstream ostr;
   377 	if (message()) ostr << message();
   378 	if (file()) ostr << "(when reading file '" << file() << "')";
   379 	_message_holder.set(ostr.str());
   380       }
   381       catch (...) {}
   382       if( _message_holder.valid() ) return _message_holder.get().c_str();
   383       return exceptionName();
   384     }
   385 
   386     virtual const char* exceptionName() const {
   387       return "lemon::IOParameterError";
   388     }
   389 
   390     virtual ~IOParameterError() throw() {}
   391   };
   392 
   393 
   394   ///\e
   395   class AssertionFailedError : public LogicError {
   396   protected:
   397     const char *assertion;
   398     const char *file;
   399     int line;
   400     const char *function;
   401     const char *message;
   402 
   403     mutable ExceptionMember<std::string> _message_holder;
   404   public:
   405     ///\e
   406     AssertionFailedError(const char *_file, int _line, const char *func,
   407 			 const char *msg, const char *_assertion = 0) :
   408       assertion(_assertion), file(_file), line(_line), function(func),
   409       message(msg) {}
   410 
   411     ///\e
   412     const char* get_assertion() const { return assertion; }
   413     ///\e
   414     const char* get_message() const { return message; }
   415     ///\e
   416     const char* get_file() const { return file; }
   417     ///\e
   418     const char* get_function() const { return function; }
   419     ///\e
   420     int get_line() const { return line; }
   421 
   422 
   423     virtual const char* what() const throw() {
   424       try {
   425 	std::ostringstream ostr;
   426 	ostr << file << ":" << line << ": ";
   427 	if( function )
   428 	  ostr << function << ": ";
   429 	ostr << message;
   430 	if( assertion )
   431 	   ostr << " (assertion '" << assertion << "' failed)";
   432 	_message_holder.set(ostr.str());
   433 	return ostr.str().c_str();
   434       }
   435       catch(...) {}
   436       if( _message_holder.valid() ) return _message_holder.get().c_str();
   437       return exceptionName();
   438     }
   439 
   440     virtual const char* exceptionName() const {
   441       return "lemon::AssertionFailedError";
   442     }
   443 
   444     virtual ~AssertionFailedError() throw() {}
   445   };
   446 
   447 
   448   /****************  Macros  ****************/
   449 
   450 
   451   inline
   452   void assert_fail(const char *file, int line, const char *func,
   453 		   const char *message, const char *assertion = 0,
   454 		   bool do_abort=true)
   455   {
   456     using namespace std;
   457     cerr << file << ":" << line << ": ";
   458     if( func )
   459       cerr << func << ": ";
   460     cerr << message;
   461     if( assertion )
   462       cerr << " (assertion '" << assertion << "' failed)";
   463     cerr << endl;
   464     if(do_abort)
   465       abort();
   466   }
   467 
   468   inline
   469   void assert_fail_throw(const char *file, int line, const char *func,
   470 		   const char *message, const char *assertion = 0,
   471 		   bool = true)
   472   {
   473     throw AssertionFailedError(file, line, func, message, assertion);
   474   }
   475 
   476 /// @}
   477 
   478 }
   479 #endif // LEMON_ERROR_H
   480 
   481 #undef LEMON_ASSERT
   482 #undef LEMON_FIXME
   483 
   484 #ifndef LEMON_ASSERT_ABORT
   485 #  define LEMON_ASSERT_ABORT 1
   486 #endif
   487 
   488 #ifndef LEMON_ASSERT_HANDLER
   489 #  ifdef LEMON_ASSERT_EXCEPTION
   490 #    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
   491 #  else
   492 #    define LEMON_ASSERT_HANDLER ::lemon::assert_fail
   493 #  endif
   494 #endif
   495 
   496 #if defined(NDEBUG) || defined(LEMON_DISABLE_ASSERTS)
   497 
   498 #  define LEMON_ASSERT(exp, msg)  (static_cast<void> (0))
   499 
   500 #else
   501 
   502 /**
   503  * \brief Macro for assertions with customizable message
   504  *
   505  * Macro for assertions with customizable message.
   506  *
   507  * The behaviour can be customized with LEMON_ASSERT_HANDLER,
   508  * LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be
   509  * disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros.
   510  *
   511  * \todo We should provide some way to reset to the default behaviour,
   512  * shouldn't we?
   513  *
   514  * \todo This whole 'assert' business should be placed in a separate
   515  * include file.
   516  *
   517  * \todo __PRETTY_FUNCTION__ should be replaced by something
   518  * compiler-independent, like BOOST_CURRENT_FUNCTION
   519  */
   520 
   521 #  define LEMON_ASSERT(exp, msg)                 \
   522      (static_cast<void> (!!(exp) ? 0 : (         \
   523        LEMON_ASSERT_HANDLER(__FILE__, __LINE__,  \
   524                             __PRETTY_FUNCTION__, \
   525 			    (msg), #exp, LEMON_ASSERT_ABORT), 0)))
   526 
   527 #endif // NDEBUG || LEMON_DISABLE_ASSERTS
   528 
   529 /**
   530  * \brief Macro for mark not yet implemented features.
   531  *
   532  * \todo Is this the right place for this? It should be used only in
   533  * modules under development.
   534  *
   535  * \todo __PRETTY_FUNCTION__ should be replaced by something
   536  * compiler-independent, like BOOST_CURRENT_FUNCTION
   537  */
   538 
   539 # define LEMON_FIXME(msg) \
   540     (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
   541 			  "FIXME: " msg))