src/lemon/error.h
author deba
Wed, 09 Mar 2005 14:15:22 +0000
changeset 1208 f486d30e4e7b
parent 1164 80bb73097736
child 1209 dc9fdf77007f
permissions -rw-r--r--
Easy input-output function for common graphs.
Modified Exception handling in graph_reader.
     1 /* -*- C++ -*-
     2  *
     3  * src/lemon/error.h - Part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi
     6  * Kutatocsoport (Egervary Combinatorial Optimization Research Group,
     7  * EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_ERROR_H
    20 #define LEMON_ERROR_H
    21 
    22 //! \ingroup exceptions
    23 //! \file
    24 //! \brief Basic exception classes and error handling.
    25 
    26 #include <exception>
    27 #include <string>
    28 #include <sstream>
    29 #include <iostream>
    30 #include <cstdlib>
    31 #include <memory>
    32 
    33 namespace lemon {
    34 
    35   /// \addtogroup exceptions
    36   /// @{
    37   
    38   /// \brief Exception safe wrapper class.
    39   ///
    40   /// Exception safe wrapper class to implement the members of exceptions.
    41   template <typename _Type>
    42   class ExceptionMember {
    43   public:
    44     typedef _Type Type;
    45 
    46     ExceptionMember() throw () {
    47       try {
    48 	ptr.reset(new Type());
    49       } catch (...) {}
    50     }
    51 
    52     ExceptionMember(const Type& type) throw () {
    53       try {
    54 	ptr.reset(new Type());
    55 	if (ptr.get() == 0) return;
    56 	*ptr = type;
    57       } catch (...) {}
    58     }
    59 
    60     ExceptionMember(const ExceptionMember& copy) throw() {
    61       try {
    62 	if (!copy.valid()) return;
    63 	ptr.reset(new Type());
    64 	if (ptr.get() == 0) return;
    65 	*ptr = copy.get();
    66       } catch (...) {}
    67     }
    68 
    69     ExceptionMember& operator=(const ExceptionMember& copy) {
    70       if (ptr.get() == 0) return;
    71       try {
    72 	if (!copy.valid()) return;
    73  	*ptr = copy.get();
    74       } catch (...) {}
    75     }
    76 
    77     void set(const Type& type) {
    78       if (ptr.get() == 0) return;
    79       try {
    80 	*ptr = type;
    81       } catch (...) {}
    82     }
    83 
    84     const Type& get() const {
    85       return *ptr;
    86     }
    87 
    88     bool valid() const {
    89       return ptr.get() != 0;
    90     }
    91     
    92   private:
    93     std::auto_ptr<_Type> ptr;
    94   };
    95 
    96   /// Exception-safe convenient "error message" class.
    97 
    98   /// Helper class which provides a convenient ostream-like (operator <<
    99   /// based) interface to create a string message. Mostly useful in
   100   /// exception classes (therefore the name).
   101   class ErrorMessage {
   102   protected:
   103     ///\e 
   104     ///\todo The good solution is boost:shared_ptr...
   105     mutable
   106     std::auto_ptr<std::ostringstream> buf;
   107     
   108     ///\e 
   109     bool init() throw() {
   110       try {
   111 	buf.reset(new std::ostringstream);
   112       }
   113       catch(...) {
   114 	buf.reset();
   115       }
   116       return buf.get();
   117     }
   118 
   119   public:
   120 
   121     ///\e 
   122     ErrorMessage() throw() { init(); }
   123 
   124     ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
   125 
   126     ///\e 
   127     ErrorMessage(const char *message) throw() {
   128       init();
   129       *this << message;
   130     }
   131 
   132     ///\e 
   133     ErrorMessage(const std::string &message) throw() {
   134       init();
   135       *this << message;
   136     }
   137 
   138     ///\e 
   139     template <typename T>
   140     ErrorMessage& operator<<(const T &t) throw() {
   141       if( ! buf.get() ) return *this;
   142 
   143       try {
   144 	*buf << t;
   145       }
   146       catch(...) {
   147 	buf.reset();
   148       }
   149       return *this;
   150     }
   151 
   152     ///\e 
   153     const char* message() throw() {
   154       if( ! buf.get() ) return 0;
   155 
   156       const char* mes = 0;
   157       try {
   158 	mes = buf->str().c_str();
   159       }
   160       catch(...) {}
   161       return mes;
   162     }
   163     
   164   };
   165 
   166   /**
   167    * \brief Generic exception class.
   168    *
   169    * Base class for exceptions used in LEMON.
   170    */
   171   class Exception : public std::exception {
   172   public:
   173     ///\e 
   174     Exception() {}
   175     ///\e 
   176     virtual ~Exception() throw() {}
   177 
   178     ///\e
   179     virtual const char* exceptionName() const {
   180       return "lemon::Exception";
   181     }
   182     
   183     ///\e 
   184     virtual const char* what() const throw() {
   185       return exceptionName();
   186     }
   187   };
   188 
   189   /**
   190    * \brief One of the two main subclasses of \ref Exception.
   191    *
   192    * Logic errors represent problems in the internal logic of a program;
   193    * in theory, these are preventable, and even detectable before the
   194    * program runs (e.g., violations of class invariants).
   195    *
   196    * A typical example for this is \ref UninitializedParameter.
   197    */
   198   class LogicError : public Exception {
   199   public:
   200     virtual const char* exceptionName() const {
   201       return "lemon::LogicError";
   202     }
   203   };
   204 
   205   /**
   206    * \brief \ref Exception for uninitialized parameters.
   207    *
   208    * This error represents problems in the initialization
   209    * of the parameters of the algorithms.
   210    */
   211   class UninitializedParameter : public LogicError {
   212   public:
   213     virtual const char* exceptionName() const {
   214       return "lemon::UninitializedParameter";
   215     }
   216   };
   217 
   218   
   219   /**
   220    * \brief One of the two main subclasses of \ref Exception.
   221    *
   222    * Runtime errors represent problems outside the scope of a program;
   223    * they cannot be easily predicted and can generally only be caught as
   224    * the program executes.
   225    */
   226   class RuntimeError : public Exception {
   227   public:
   228     virtual const char* exceptionName() const {
   229       return "lemon::RuntimeError";
   230     }
   231   };
   232 
   233   ///\e
   234   class RangeError : public RuntimeError {
   235   public:
   236     virtual const char* exceptionName() const {
   237       return "lemon::RangeError";
   238     }
   239   };
   240 
   241   ///\e 
   242   class IOError : public RuntimeError {
   243   public:
   244     virtual const char* exceptionName() const {
   245       return "lemon::IOError";
   246     }
   247   };
   248 
   249   ///\e 
   250   class DataFormatError : public IOError {
   251   protected:
   252     ExceptionMember<std::string> _message;
   253     ExceptionMember<std::string> _file;
   254     int _line;
   255 
   256     mutable ExceptionMember<std::string> _message_holder;
   257   public:
   258 
   259     DataFormatError(const DataFormatError &dfe) : 
   260       IOError(dfe), _message(dfe._message), _file(dfe._file),
   261       _line(dfe._line) {}
   262 
   263     ///\e 
   264     explicit DataFormatError(const char *the_message)
   265       : _message(the_message), _line(0) {}
   266 
   267     ///\e 
   268     DataFormatError(const std::string &file_name, int line_num,
   269 		    const char *the_message)
   270       : _message(the_message), _line(line_num) { file(file_name); }
   271 
   272     ///\e 
   273     void line(int line) { _line = line; }
   274     ///\e 
   275     void message(const std::string& message) { _message.set(message); }
   276     ///\e 
   277     void file(const std::string &file) { _file.set(file); }
   278  
   279     ///\e
   280     int line() const { return _line; }
   281     ///\e
   282     const char* message() const { 
   283       if (_message.valid() && !_message.get().empty()) {
   284 	return _message.get().c_str();
   285       } else {
   286 	return 0;
   287       }
   288     }
   289 
   290     /// \brief Returns the filename.
   291     ///
   292     /// Returns \e null if the filename was not specified.
   293     const char* file() const {
   294       if (_file.valid() && !_file.get().empty()) {
   295 	return _file.get().c_str();
   296       } else {
   297 	return 0;
   298       }
   299     }
   300 
   301     ///\e 
   302     virtual const char* what() const throw() {
   303       try {
   304 	std::ostringstream ostr;
   305 	if (message()) ostr << message();
   306 	if( file() || line() != 0 ) {
   307 	  ostr << " (";
   308 	  if( file() ) ostr << "in file '" << file() << "'";
   309 	  if( file() && line() != 0 ) ostr << " ";
   310 	  if( line() != 0 ) ostr << "at line " << line();
   311 	  ostr << ")";
   312 	}
   313 	_message_holder.set(ostr.str());
   314       }
   315       catch (...) {}
   316       if( _message_holder.valid()) return _message_holder.get().c_str();
   317       return exceptionName();
   318     }
   319 
   320     virtual const char* exceptionName() const {
   321       return "lemon::DataFormatError";
   322     }
   323 
   324     virtual ~DataFormatError() throw() {}
   325   };
   326 
   327   class IOLogicError : public IOError, public LogicError {
   328   protected:
   329     ExceptionMember<std::string> _message;
   330     ExceptionMember<std::string> _file;
   331     int _line;
   332 
   333     mutable ExceptionMember<std::string> _message_holder;
   334   public:
   335 
   336     IOLogicError(const IOLogicError &ile) : 
   337       IOError(ile), LogicError(ile), 
   338       _message(ile._message), _file(ile._file) {}
   339 
   340     ///\e 
   341     explicit IOLogicError(const char *the_message)
   342       : _message(the_message), _line(0) {}
   343 
   344     ///\e 
   345     IOLogicError(const char *file_name, const char *the_message)
   346       : _message(file_name), _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::IOLogicError";
   388     }
   389 
   390     virtual ~IOLogicError() 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   public:
   403     ///\e
   404     AssertionFailedError(const char *_file, int _line, const char *func,
   405 			 const char *msg, const char *_assertion = 0) :
   406       assertion(_assertion), file(_file), line(_line), function(func),
   407       message(msg) {}
   408 
   409     ///\e
   410     const char* get_assertion() const { return assertion; }
   411     ///\e
   412     const char* get_message() const { return message; }
   413     ///\e
   414     const char* get_file() const { return file; }
   415     ///\e
   416     const char* get_function() const { return function; }
   417     ///\e
   418     int get_line() const { return line; }
   419 
   420 
   421     virtual const char* what() const throw() {
   422       const char *mes = 0;
   423       try {
   424 	std::ostringstream ostr;
   425 	ostr << file << ":" << line << ": ";
   426 	if( function )
   427 	  ostr << function << ": ";
   428 	ostr << message;
   429 	if( assertion )
   430 	  ostr << " (assertion '" << assertion << "' failed)";
   431 	mes = ostr.str().c_str();
   432 	/// \bug Szerintem a 'mes'-re nem szabad hivatkozni, mert
   433 	/// az elobb felszabadul. 
   434       }
   435       catch(...) {}
   436       if( mes ) return mes;
   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-independant, 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-independant, like BOOST_CURRENT_FUNCTION
   537  */
   538 
   539 # define LEMON_FIXME(msg) \
   540     (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
   541 			  "FIXME: " msg))