src/lemon/error.h
author deba
Mon, 07 Feb 2005 15:40:34 +0000
changeset 1139 f59038affc7e
parent 1122 3ce7fc516cca
child 1151 b217fc69f913
permissions -rw-r--r--
Changing first to iterators.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/error.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@883
    16
alpar@921
    17
#ifndef LEMON_ERROR_H
alpar@921
    18
#define LEMON_ERROR_H
alpar@883
    19
alpar@883
    20
//! \ingroup misc
alpar@883
    21
//! \file
klao@1067
    22
//! \brief Basic exception classes and error handling.
alpar@883
    23
alpar@883
    24
#include <exception>
alpar@883
    25
#include <string>
alpar@883
    26
#include <sstream>
klao@1067
    27
#include <iostream>
klao@1067
    28
#include <cstdlib>
alpar@883
    29
klao@1056
    30
#include <boost/shared_ptr.hpp>
alpar@883
    31
alpar@921
    32
namespace lemon {
alpar@883
    33
klao@1056
    34
  /// Exception-safe convenient "error message" class.
klao@1067
    35
klao@1067
    36
  /// Helper class which provides a convenient ostream-like (operator <<
klao@1067
    37
  /// based) interface to create a string message. Mostly useful in
klao@1067
    38
  /// exception classes (therefore the name).
klao@1056
    39
  class ErrorMessage {
klao@1056
    40
  protected:
klao@1067
    41
    ///\e 
klao@1056
    42
    boost::shared_ptr<std::ostringstream> buf;
klao@1056
    43
    
klao@1067
    44
    ///\e 
klao@1056
    45
    bool init() throw() {
klao@1056
    46
      try {
klao@1056
    47
	buf.reset(new std::ostringstream);
klao@1056
    48
      }
klao@1056
    49
      catch(...) {
klao@1056
    50
	buf.reset();
klao@1056
    51
      }
klao@1056
    52
      return buf;
klao@1056
    53
    }
klao@1056
    54
klao@1056
    55
  public:
klao@1056
    56
klao@1067
    57
    ///\e 
klao@1056
    58
    ErrorMessage() throw() { init(); }
klao@1056
    59
klao@1067
    60
    ///\e 
klao@1056
    61
    ErrorMessage(const char *message) throw() {
klao@1056
    62
      init();
klao@1056
    63
      *this << message;
klao@1056
    64
    }
klao@1056
    65
klao@1067
    66
    ///\e 
klao@1056
    67
    ErrorMessage(const std::string &message) throw() {
klao@1056
    68
      init();
klao@1056
    69
      *this << message;
klao@1056
    70
    }
klao@1056
    71
klao@1067
    72
    ///\e 
klao@1056
    73
    template <typename T>
klao@1056
    74
    ErrorMessage& operator<<(const T &t) throw() {
klao@1056
    75
      if( !buf ) return *this;
klao@1056
    76
klao@1056
    77
      try {
klao@1056
    78
	*buf << t;
klao@1056
    79
      }
klao@1056
    80
      catch(...) {
klao@1056
    81
	buf.reset();
klao@1056
    82
      }
klao@1056
    83
    }
klao@1056
    84
klao@1067
    85
    ///\e 
klao@1056
    86
    const char* message() throw() {
klao@1056
    87
      if( !buf ) return 0;
klao@1056
    88
klao@1056
    89
      const char* mes = 0;
klao@1056
    90
      try {
klao@1056
    91
	mes = buf->str().c_str();
klao@1056
    92
      }
klao@1056
    93
      catch(...) {}
klao@1056
    94
      return mes;
klao@1056
    95
    }
klao@1056
    96
    
klao@1056
    97
  };
klao@1056
    98
alpar@883
    99
  /**
alpar@883
   100
   * \brief Generic exception class.
alpar@883
   101
   *
klao@1056
   102
   * Base class for exceptions used in LEMON.
alpar@883
   103
   */
klao@1067
   104
  class Exception : public std::exception {
alpar@883
   105
  public:
klao@1067
   106
    ///\e 
klao@1120
   107
    Exception() {}
klao@1067
   108
    ///\e 
alpar@883
   109
    virtual ~Exception() throw() {}
klao@1120
   110
klao@1120
   111
    ///\e
klao@1120
   112
    virtual const char* exceptionName() const {
klao@1120
   113
      return "lemon::Exception";
klao@1120
   114
    }
alpar@883
   115
    
klao@1067
   116
    ///\e 
alpar@883
   117
    virtual const char* what() const throw() {
klao@1120
   118
      return exceptionName();
klao@1056
   119
    }
klao@1056
   120
  };
klao@1056
   121
klao@1120
   122
  /**
klao@1120
   123
   * \brief One of the two main subclasses of \ref Exception.
klao@1120
   124
   *
klao@1120
   125
   * Logic errors represent problems in the internal logic of a program;
klao@1120
   126
   * in theory, these are preventable, and even detectable before the
klao@1120
   127
   * program runs (e.g., violations of class invariants).
klao@1120
   128
   *
alpar@1125
   129
   * A typical example for this is \ref UninitializedParameter.
klao@1120
   130
   */
klao@1056
   131
  class LogicError : public Exception {
klao@1067
   132
  public:
klao@1120
   133
    virtual const char* exceptionName() const {
klao@1120
   134
      return "lemon::LogicError";
klao@1120
   135
    }
klao@1056
   136
  };
klao@1056
   137
alpar@1125
   138
  /**
alpar@1125
   139
   * \brief \ref Exception for uninitialized parameters.
alpar@1125
   140
   *
alpar@1125
   141
   * This error represents problems in the initialization
alpar@1125
   142
   * of the parameters of the algorithms.
alpar@1125
   143
   */
alpar@1125
   144
  class UninitializedParameter : public LogicError {
alpar@1125
   145
  public:
alpar@1125
   146
    virtual const char* exceptionName() const {
alpar@1125
   147
      return "lemon::UninitializedParameter";
alpar@1125
   148
    }
alpar@1125
   149
  };
alpar@1125
   150
klao@1120
   151
  
klao@1120
   152
  /**
klao@1120
   153
   * \brief One of the two main subclasses of \ref Exception.
klao@1120
   154
   *
klao@1120
   155
   * Runtime errors represent problems outside the scope of a program;
klao@1120
   156
   * they cannot be easily predicted and can generally only be caught as
klao@1120
   157
   * the program executes.
klao@1120
   158
   */
klao@1056
   159
  class RuntimeError : public Exception {
klao@1067
   160
  public:
klao@1120
   161
    virtual const char* exceptionName() const {
klao@1120
   162
      return "lemon::RuntimeError";
klao@1120
   163
    }
klao@1056
   164
  };
klao@1056
   165
klao@1067
   166
  ///\e
klao@1056
   167
  class RangeError : public RuntimeError {
klao@1067
   168
  public:
klao@1120
   169
    virtual const char* exceptionName() const {
klao@1120
   170
      return "lemon::RangeError";
klao@1120
   171
    }
klao@1056
   172
  };
klao@1056
   173
alpar@1061
   174
  ///\e 
klao@1056
   175
  class IOError : public RuntimeError {
klao@1067
   176
  public:
klao@1120
   177
    virtual const char* exceptionName() const {
klao@1120
   178
      return "lemon::IOError";
klao@1120
   179
    }
klao@1056
   180
  };
klao@1056
   181
alpar@1061
   182
  ///\e 
klao@1056
   183
  class DataFormatError : public IOError {
klao@1067
   184
  protected:
klao@1120
   185
    const char *_message;
klao@1120
   186
    int _line;
klao@1120
   187
    boost::shared_ptr<std::string> _file;
klao@1067
   188
klao@1067
   189
  public:
klao@1067
   190
    ///\e 
klao@1120
   191
    explicit DataFormatError(const char *the_message)
klao@1120
   192
      : _message(the_message), _line(0) {}
klao@1067
   193
    ///\e 
klao@1056
   194
    DataFormatError(const std::string &file_name, int line_num,
klao@1120
   195
		    const char *the_message)
klao@1120
   196
      : _message(the_message), _line(line_num) { file(file_name); }
klao@1056
   197
klao@1067
   198
    ///\e 
klao@1120
   199
    void line(int line_num) { _line=line_num; }
klao@1067
   200
    ///\e 
klao@1120
   201
    void message(char *the_message) { _message=the_message; }
klao@1120
   202
    ///\e 
klao@1120
   203
    void file(const std::string &file_name) {
klao@1056
   204
      try {
klao@1120
   205
	_file.reset(new std::string);
klao@1120
   206
	*_file = file_name;
klao@1056
   207
      }
klao@1056
   208
      catch(...) {
klao@1120
   209
	_file.reset();
klao@1056
   210
      }
alpar@883
   211
    }
alpar@883
   212
klao@1067
   213
    ///\e
klao@1120
   214
    int line() const { return _line; }
klao@1120
   215
    ///\e
klao@1120
   216
    const char* message() const { return _message; }
klao@1067
   217
klao@1067
   218
    /// \brief Returns the filename.
klao@1067
   219
    ///
klao@1068
   220
    /// Returns \e "(unknown)" if the filename was not specified.
klao@1120
   221
    const char* file() const {
klao@1120
   222
      if( _file )
klao@1120
   223
	return _file->c_str();
klao@1067
   224
      else
klao@1067
   225
	return "(unknown)";
klao@1067
   226
    }
klao@1067
   227
klao@1067
   228
    ///\e 
klao@1067
   229
    virtual const char* what() const throw() {
klao@1056
   230
      const char *mes = 0;
klao@1056
   231
      try {
klao@1056
   232
	std::ostringstream ostr;
klao@1120
   233
	ostr << _message;
klao@1120
   234
	if( _file || _line ) {
klao@1056
   235
	  ostr << " (";
klao@1120
   236
	  if( _file ) ostr << "in file '" << *_file << "'";
klao@1120
   237
	  if( _file && _line ) ostr << " ";
klao@1120
   238
	  if( _line ) ostr << "at line " << _line;
klao@1067
   239
	  ostr << ")";
klao@1056
   240
	}
klao@1056
   241
	mes = ostr.str().c_str();
klao@1056
   242
      }
klao@1056
   243
      catch(...) {}
klao@1056
   244
      if( mes ) return mes;
klao@1120
   245
      return exceptionName();
klao@1120
   246
    }
klao@1120
   247
klao@1120
   248
    virtual const char* exceptionName() const {
klao@1056
   249
      return "lemon::DataFormatError";
klao@1056
   250
    }
klao@1067
   251
klao@1067
   252
    virtual ~DataFormatError() throw() {}
alpar@883
   253
  };
alpar@883
   254
klao@1056
   255
klao@1068
   256
  ///\e
klao@1067
   257
  class AssertionFailedError : public LogicError {
klao@1067
   258
  protected:
klao@1067
   259
    const char *assertion;
klao@1067
   260
    const char *file;
klao@1067
   261
    int line;
klao@1067
   262
    const char *function;
klao@1067
   263
    const char *message;
klao@1067
   264
  public:
klao@1067
   265
    ///\e
klao@1067
   266
    AssertionFailedError(const char *_file, int _line, const char *func,
klao@1067
   267
			 const char *msg, const char *_assertion = 0) :
klao@1067
   268
      assertion(_assertion), file(_file), line(_line), function(func),
klao@1067
   269
      message(msg) {}
klao@1067
   270
klao@1067
   271
    ///\e
klao@1067
   272
    const char* get_assertion() const { return assertion; }
klao@1067
   273
    ///\e
klao@1067
   274
    const char* get_message() const { return message; }
klao@1067
   275
    ///\e
klao@1067
   276
    const char* get_file() const { return file; }
klao@1067
   277
    ///\e
klao@1067
   278
    const char* get_function() const { return function; }
klao@1067
   279
    ///\e
klao@1067
   280
    int get_line() const { return line; }
klao@1067
   281
klao@1067
   282
klao@1067
   283
    virtual const char* what() const throw() {
klao@1067
   284
      const char *mes = 0;
klao@1067
   285
      try {
klao@1067
   286
	std::ostringstream ostr;
klao@1067
   287
	ostr << file << ":" << line << ": ";
klao@1067
   288
	if( function )
klao@1067
   289
	  ostr << function << ": ";
klao@1067
   290
	ostr << message;
klao@1067
   291
	if( assertion )
klao@1067
   292
	  ostr << " (assertion '" << assertion << "' failed)";
klao@1067
   293
	mes = ostr.str().c_str();
klao@1067
   294
      }
klao@1067
   295
      catch(...) {}
klao@1067
   296
      if( mes ) return mes;
klao@1120
   297
      return exceptionName();
klao@1120
   298
    }
klao@1120
   299
klao@1120
   300
    virtual const char* exceptionName() const {
klao@1067
   301
      return "lemon::AssertionFailedError";
klao@1067
   302
    }
klao@1067
   303
klao@1067
   304
    virtual ~AssertionFailedError() throw() {}
klao@1067
   305
  };
klao@1067
   306
klao@1056
   307
klao@1056
   308
  /****************  Macros  ****************/
klao@1056
   309
klao@1056
   310
klao@1067
   311
  inline
klao@1067
   312
  void assert_fail(const char *file, int line, const char *func,
klao@1067
   313
		   const char *message, const char *assertion = 0,
klao@1067
   314
		   bool do_abort=true)
klao@1067
   315
  {
klao@1067
   316
    using namespace std;
klao@1067
   317
    cerr << file << ":" << line << ": ";
klao@1067
   318
    if( func )
klao@1067
   319
      cerr << func << ": ";
klao@1067
   320
    cerr << message;
klao@1067
   321
    if( assertion )
klao@1067
   322
      cerr << " (assertion '" << assertion << "' failed)";
klao@1067
   323
    cerr << endl;
klao@1067
   324
    if(do_abort)
klao@1067
   325
      abort();
klao@1067
   326
  }
klao@1056
   327
klao@1067
   328
  inline
klao@1067
   329
  void assert_fail_throw(const char *file, int line, const char *func,
klao@1067
   330
		   const char *message, const char *assertion = 0,
klao@1067
   331
		   bool = true)
klao@1067
   332
  {
klao@1067
   333
    throw AssertionFailedError(file, line, func, message, assertion);
klao@1067
   334
  }
klao@1056
   335
alpar@883
   336
alpar@883
   337
}
alpar@921
   338
#endif // LEMON_ERROR_H
klao@1067
   339
klao@1067
   340
#undef LEMON_ASSERT
klao@1067
   341
#undef LEMON_FIXME
klao@1067
   342
klao@1067
   343
#ifndef LEMON_ASSERT_ABORT
klao@1067
   344
#  define LEMON_ASSERT_ABORT 1
klao@1067
   345
#endif
klao@1067
   346
klao@1067
   347
#ifndef LEMON_ASSERT_HANDLER
klao@1120
   348
#  ifdef LEMON_ASSERT_EXCEPTION
klao@1120
   349
#    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
klao@1120
   350
#  else
klao@1120
   351
#    define LEMON_ASSERT_HANDLER ::lemon::assert_fail
klao@1120
   352
#  endif
klao@1067
   353
#endif
klao@1067
   354
klao@1067
   355
#if defined(NDEBUG) || defined(LEMON_DISABLE_ASSERTS)
klao@1067
   356
klao@1067
   357
#  define LEMON_ASSERT(exp, msg)  (static_cast<void> (0))
klao@1067
   358
klao@1067
   359
#else
klao@1067
   360
klao@1067
   361
/**
klao@1067
   362
 * \brief Macro for assertions with customizable message
klao@1067
   363
 *
klao@1120
   364
 * Macro for assertions with customizable message.
klao@1120
   365
 *
klao@1120
   366
 * The behaviour can be customized with LEMON_ASSERT_HANDLER,
klao@1120
   367
 * LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be
klao@1120
   368
 * disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros.
klao@1120
   369
 *
klao@1120
   370
 * \todo We should provide some way to reset to the default behaviour,
klao@1120
   371
 * shouldn't we?
klao@1120
   372
 *
klao@1120
   373
 * \todo This whole 'assert' business should be placed in a separate
klao@1120
   374
 * include file.
klao@1120
   375
 *
klao@1067
   376
 * \todo __PRETTY_FUNCTION__ should be replaced by something
klao@1067
   377
 * compiler-independant, like BOOST_CURRENT_FUNCTION
klao@1067
   378
 */
klao@1067
   379
klao@1067
   380
#  define LEMON_ASSERT(exp, msg)                 \
klao@1067
   381
     (static_cast<void> (!!(exp) ? 0 : (         \
klao@1067
   382
       LEMON_ASSERT_HANDLER(__FILE__, __LINE__,  \
klao@1067
   383
                            __PRETTY_FUNCTION__, \
klao@1067
   384
			    (msg), #exp, LEMON_ASSERT_ABORT), 0)))
klao@1067
   385
klao@1120
   386
#endif // NDEBUG || LEMON_DISABLE_ASSERTS
klao@1067
   387
klao@1067
   388
/**
klao@1067
   389
 * \brief Macro for mark not yet implemented features.
klao@1067
   390
 *
klao@1067
   391
 * \todo Is this the right place for this? It should be used only in
klao@1067
   392
 * modules under development.
klao@1067
   393
 *
klao@1067
   394
 * \todo __PRETTY_FUNCTION__ should be replaced by something
klao@1067
   395
 * compiler-independant, like BOOST_CURRENT_FUNCTION
klao@1067
   396
 */
klao@1067
   397
klao@1067
   398
# define LEMON_FIXME(msg) \
klao@1067
   399
    (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
klao@1067
   400
			  "FIXME: " msg))