lemon/error.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 16 Mar 2008 07:32:43 +0000
changeset 84 8161012eaa61
child 108 889d0c289d19
permissions -rw-r--r--
Merge
kpeter@66
     1
/* -*- C++ -*-
kpeter@66
     2
 *
kpeter@66
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@66
     4
 *
kpeter@66
     5
 * Copyright (C) 2003-2008
kpeter@66
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@66
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@66
     8
 *
kpeter@66
     9
 * Permission to use, modify and distribute this software is granted
kpeter@66
    10
 * provided that this copyright notice appears in all copies. For
kpeter@66
    11
 * precise terms see the accompanying LICENSE file.
kpeter@66
    12
 *
kpeter@66
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@66
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@66
    15
 * purpose.
kpeter@66
    16
 *
kpeter@66
    17
 */
kpeter@66
    18
kpeter@66
    19
#ifndef LEMON_ERROR_H
kpeter@66
    20
#define LEMON_ERROR_H
kpeter@66
    21
kpeter@66
    22
/// \ingroup exceptions
kpeter@66
    23
/// \file
kpeter@66
    24
/// \brief Basic exception classes and error handling.
kpeter@66
    25
kpeter@66
    26
#include <exception>
kpeter@66
    27
#include <string>
kpeter@66
    28
#include <sstream>
kpeter@66
    29
#include <iostream>
kpeter@66
    30
#include <cstdlib>
kpeter@66
    31
#include <memory>
kpeter@66
    32
kpeter@66
    33
namespace lemon {
kpeter@66
    34
kpeter@66
    35
  /// \addtogroup exceptions
kpeter@66
    36
  /// @{
kpeter@66
    37
kpeter@66
    38
  /// \brief Exception safe wrapper class.
kpeter@66
    39
  ///
kpeter@66
    40
  /// Exception safe wrapper class to implement the members of exceptions.
kpeter@66
    41
  template <typename _Type>
kpeter@66
    42
  class ExceptionMember {
kpeter@66
    43
  public:
kpeter@66
    44
    typedef _Type Type;
kpeter@66
    45
kpeter@66
    46
    ExceptionMember() throw() {
kpeter@66
    47
      try {
kpeter@66
    48
	ptr.reset(new Type());
kpeter@66
    49
      } catch (...) {}
kpeter@66
    50
    }
kpeter@66
    51
kpeter@66
    52
    ExceptionMember(const Type& type) throw() {
kpeter@66
    53
      try {
kpeter@66
    54
	ptr.reset(new Type());
kpeter@66
    55
	if (ptr.get() == 0) return;
kpeter@66
    56
	*ptr = type;
kpeter@66
    57
      } catch (...) {}
kpeter@66
    58
    }
kpeter@66
    59
kpeter@66
    60
    ExceptionMember(const ExceptionMember& copy) throw() {
kpeter@66
    61
      try {
kpeter@66
    62
	if (!copy.valid()) return;
kpeter@66
    63
	ptr.reset(new Type());
kpeter@66
    64
	if (ptr.get() == 0) return;
kpeter@66
    65
	*ptr = copy.get();
kpeter@66
    66
      } catch (...) {}
kpeter@66
    67
    }
kpeter@66
    68
kpeter@66
    69
    ExceptionMember& operator=(const ExceptionMember& copy) throw() {
kpeter@66
    70
      if (ptr.get() == 0) return;
kpeter@66
    71
      try {
kpeter@66
    72
	if (!copy.valid()) return;
kpeter@66
    73
 	*ptr = copy.get();
kpeter@66
    74
      } catch (...) {}
kpeter@66
    75
    }
kpeter@66
    76
kpeter@66
    77
    void set(const Type& type) throw() {
kpeter@66
    78
      if (ptr.get() == 0) return;
kpeter@66
    79
      try {
kpeter@66
    80
	*ptr = type;
kpeter@66
    81
      } catch (...) {}
kpeter@66
    82
    }
kpeter@66
    83
kpeter@66
    84
    const Type& get() const {
kpeter@66
    85
      return *ptr;
kpeter@66
    86
    }
kpeter@66
    87
kpeter@66
    88
    bool valid() const throw() {
kpeter@66
    89
      return ptr.get() != 0;
kpeter@66
    90
    }
kpeter@66
    91
kpeter@66
    92
  private:
kpeter@66
    93
    std::auto_ptr<_Type> ptr;
kpeter@66
    94
  };
kpeter@66
    95
kpeter@66
    96
  /// Exception-safe convenient "error message" class.
kpeter@66
    97
kpeter@66
    98
  /// Helper class which provides a convenient ostream-like (operator <<
kpeter@66
    99
  /// based) interface to create a string message. Mostly useful in
kpeter@66
   100
  /// exception classes (therefore the name).
kpeter@66
   101
  class ErrorMessage {
kpeter@66
   102
  protected:
kpeter@66
   103
    ///\e
kpeter@66
   104
kpeter@66
   105
    ///\todo The good solution is boost::shared_ptr...
kpeter@66
   106
    ///
kpeter@66
   107
    mutable std::auto_ptr<std::ostringstream> buf;
kpeter@66
   108
kpeter@66
   109
    ///\e
kpeter@66
   110
    bool init() throw() {
kpeter@66
   111
      try {
kpeter@66
   112
	buf.reset(new std::ostringstream);
kpeter@66
   113
      }
kpeter@66
   114
      catch(...) {
kpeter@66
   115
	buf.reset();
kpeter@66
   116
      }
kpeter@66
   117
      return buf.get();
kpeter@66
   118
    }
kpeter@66
   119
kpeter@66
   120
  public:
kpeter@66
   121
kpeter@66
   122
    ///\e
kpeter@66
   123
    ErrorMessage() throw() { init(); }
kpeter@66
   124
kpeter@66
   125
    ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
kpeter@66
   126
kpeter@66
   127
    ///\e
kpeter@66
   128
    ErrorMessage(const char *msg) throw() {
kpeter@66
   129
      init();
kpeter@66
   130
      *this << msg;
kpeter@66
   131
    }
kpeter@66
   132
kpeter@66
   133
    ///\e
kpeter@66
   134
    ErrorMessage(const std::string &msg) throw() {
kpeter@66
   135
      init();
kpeter@66
   136
      *this << msg;
kpeter@66
   137
    }
kpeter@66
   138
kpeter@66
   139
    ///\e
kpeter@66
   140
    template <typename T>
kpeter@66
   141
    ErrorMessage& operator<<(const T &t) throw() {
kpeter@66
   142
      if( ! buf.get() ) return *this;
kpeter@66
   143
kpeter@66
   144
      try {
kpeter@66
   145
	*buf << t;
kpeter@66
   146
      }
kpeter@66
   147
      catch(...) {
kpeter@66
   148
	buf.reset();
kpeter@66
   149
      }
kpeter@66
   150
      return *this;
kpeter@66
   151
    }
kpeter@66
   152
kpeter@66
   153
    ///\e
kpeter@66
   154
    const char* message() throw() {
kpeter@66
   155
      if( ! buf.get() ) return 0;
kpeter@66
   156
kpeter@66
   157
      const char* mes = 0;
kpeter@66
   158
      try {
kpeter@66
   159
	mes = buf->str().c_str();
kpeter@66
   160
      }
kpeter@66
   161
      catch(...) {}
kpeter@66
   162
      return mes;
kpeter@66
   163
    }
kpeter@66
   164
kpeter@66
   165
  };
kpeter@66
   166
kpeter@66
   167
  /// Generic exception class.
kpeter@66
   168
kpeter@66
   169
  /// Base class for exceptions used in LEMON.
kpeter@66
   170
  ///
kpeter@66
   171
  class Exception : public std::exception {
kpeter@66
   172
  public:
kpeter@66
   173
    ///\e
kpeter@66
   174
    Exception() {}
kpeter@66
   175
    ///\e
kpeter@66
   176
    virtual ~Exception() throw() {}
kpeter@66
   177
    ///\e
kpeter@66
   178
    virtual const char* what() const throw() {
kpeter@66
   179
      return "lemon::Exception";
kpeter@66
   180
    }
kpeter@66
   181
  };
kpeter@66
   182
kpeter@66
   183
  /// One of the two main subclasses of \ref Exception.
kpeter@66
   184
kpeter@66
   185
  /// Logic errors represent problems in the internal logic of a program;
kpeter@66
   186
  /// in theory, these are preventable, and even detectable before the
kpeter@66
   187
  /// program runs (e.g. violations of class invariants).
kpeter@66
   188
  ///
kpeter@66
   189
  /// A typical example for this is \ref UninitializedParameter.
kpeter@66
   190
  class LogicError : public Exception {
kpeter@66
   191
  public:
kpeter@66
   192
    virtual const char* what() const throw() {
kpeter@66
   193
      return "lemon::LogicError";
kpeter@66
   194
    }
kpeter@66
   195
  };
kpeter@66
   196
kpeter@66
   197
  /// \ref Exception for uninitialized parameters.
kpeter@66
   198
kpeter@66
   199
  /// This error represents problems in the initialization
kpeter@66
   200
  /// of the parameters of the algorithms.
kpeter@66
   201
  class UninitializedParameter : public LogicError {
kpeter@66
   202
  public:
kpeter@66
   203
    virtual const char* what() const throw() {
kpeter@66
   204
      return "lemon::UninitializedParameter";
kpeter@66
   205
    }
kpeter@66
   206
  };
kpeter@66
   207
kpeter@66
   208
kpeter@66
   209
  /// One of the two main subclasses of \ref Exception.
kpeter@66
   210
kpeter@66
   211
  /// Runtime errors represent problems outside the scope of a program;
kpeter@66
   212
  /// they cannot be easily predicted and can generally only be caught
kpeter@66
   213
  /// as the program executes.
kpeter@66
   214
  class RuntimeError : public Exception {
kpeter@66
   215
  public:
kpeter@66
   216
    virtual const char* what() const throw() {
kpeter@66
   217
      return "lemon::RuntimeError";
kpeter@66
   218
    }
kpeter@66
   219
  };
kpeter@66
   220
kpeter@66
   221
  ///\e
kpeter@66
   222
  class RangeError : public RuntimeError {
kpeter@66
   223
  public:
kpeter@66
   224
    virtual const char* what() const throw() {
kpeter@66
   225
      return "lemon::RangeError";
kpeter@66
   226
    }
kpeter@66
   227
  };
kpeter@66
   228
kpeter@66
   229
  ///\e
kpeter@66
   230
  class IoError : public RuntimeError {
kpeter@66
   231
  public:
kpeter@66
   232
    virtual const char* what() const throw() {
kpeter@66
   233
      return "lemon::IoError";
kpeter@66
   234
    }
kpeter@66
   235
  };
kpeter@66
   236
kpeter@66
   237
  ///\e
kpeter@66
   238
  class DataFormatError : public IoError {
kpeter@66
   239
  protected:
kpeter@66
   240
    ExceptionMember<std::string> _message;
kpeter@66
   241
    ExceptionMember<std::string> _file;
kpeter@66
   242
    int _line;
kpeter@66
   243
kpeter@66
   244
    mutable ExceptionMember<std::string> _message_holder;
kpeter@66
   245
  public:
kpeter@66
   246
kpeter@66
   247
    DataFormatError(const DataFormatError &dfe) :
kpeter@66
   248
      IoError(dfe), _message(dfe._message), _file(dfe._file),
kpeter@66
   249
      _line(dfe._line) {}
kpeter@66
   250
kpeter@66
   251
    ///\e
kpeter@66
   252
    explicit DataFormatError(const char *the_message)
kpeter@66
   253
      : _message(the_message), _line(0) {}
kpeter@66
   254
kpeter@66
   255
    ///\e
kpeter@66
   256
    DataFormatError(const std::string &file_name, int line_num,
kpeter@66
   257
		    const char *the_message)
kpeter@66
   258
      : _message(the_message), _line(line_num) { file(file_name); }
kpeter@66
   259
kpeter@66
   260
    ///\e
kpeter@66
   261
    void line(int ln) { _line = ln; }
kpeter@66
   262
    ///\e
kpeter@66
   263
    void message(const std::string& msg) { _message.set(msg); }
kpeter@66
   264
    ///\e
kpeter@66
   265
    void file(const std::string &fl) { _file.set(fl); }
kpeter@66
   266
kpeter@66
   267
    ///\e
kpeter@66
   268
    int line() const { return _line; }
kpeter@66
   269
    ///\e
kpeter@66
   270
    const char* message() const {
kpeter@66
   271
      if (_message.valid() && !_message.get().empty()) {
kpeter@66
   272
	return _message.get().c_str();
kpeter@66
   273
      } else {
kpeter@66
   274
	return 0;
kpeter@66
   275
      }
kpeter@66
   276
    }
kpeter@66
   277
kpeter@66
   278
    /// \brief Returns the filename.
kpeter@66
   279
    ///
kpeter@66
   280
    /// Returns \e null if the filename was not specified.
kpeter@66
   281
    const char* file() const {
kpeter@66
   282
      if (_file.valid() && !_file.get().empty()) {
kpeter@66
   283
	return _file.get().c_str();
kpeter@66
   284
      } else {
kpeter@66
   285
	return 0;
kpeter@66
   286
      }
kpeter@66
   287
    }
kpeter@66
   288
kpeter@66
   289
    ///\e
kpeter@66
   290
    virtual const char* what() const throw() {
kpeter@66
   291
      try {
kpeter@66
   292
	std::ostringstream ostr;
kpeter@66
   293
	ostr << "lemon:DataFormatError" << ": ";
kpeter@66
   294
	if (message()) ostr << message();
kpeter@66
   295
	if( file() || line() != 0 ) {
kpeter@66
   296
	  ostr << " (";
kpeter@66
   297
	  if( file() ) ostr << "in file '" << file() << "'";
kpeter@66
   298
	  if( file() && line() != 0 ) ostr << " ";
kpeter@66
   299
	  if( line() != 0 ) ostr << "at line " << line();
kpeter@66
   300
	  ostr << ")";
kpeter@66
   301
	}
kpeter@66
   302
	_message_holder.set(ostr.str());
kpeter@66
   303
      }
kpeter@66
   304
      catch (...) {}
kpeter@66
   305
      if( _message_holder.valid()) return _message_holder.get().c_str();
kpeter@66
   306
      return "lemon:DataFormatError";
kpeter@66
   307
    }
kpeter@66
   308
kpeter@66
   309
    virtual ~DataFormatError() throw() {}
kpeter@66
   310
  };
kpeter@66
   311
kpeter@66
   312
  ///\e
kpeter@66
   313
  class FileOpenError : public IoError {
kpeter@66
   314
  protected:
kpeter@66
   315
    ExceptionMember<std::string> _file;
kpeter@66
   316
kpeter@66
   317
    mutable ExceptionMember<std::string> _message_holder;
kpeter@66
   318
  public:
kpeter@66
   319
kpeter@66
   320
    FileOpenError(const FileOpenError &foe) :
kpeter@66
   321
      IoError(foe), _file(foe._file) {}
kpeter@66
   322
kpeter@66
   323
    ///\e
kpeter@66
   324
    explicit FileOpenError(const std::string& fl)
kpeter@66
   325
      : _file(fl) {}
kpeter@66
   326
kpeter@66
   327
kpeter@66
   328
    ///\e
kpeter@66
   329
    void file(const std::string &fl) { _file.set(fl); }
kpeter@66
   330
kpeter@66
   331
    /// \brief Returns the filename.
kpeter@66
   332
    ///
kpeter@66
   333
    /// Returns \e null if the filename was not specified.
kpeter@66
   334
    const char* file() const {
kpeter@66
   335
      if (_file.valid() && !_file.get().empty()) {
kpeter@66
   336
	return _file.get().c_str();
kpeter@66
   337
      } else {
kpeter@66
   338
	return 0;
kpeter@66
   339
      }
kpeter@66
   340
    }
kpeter@66
   341
kpeter@66
   342
    ///\e
kpeter@66
   343
    virtual const char* what() const throw() {
kpeter@66
   344
      try {
kpeter@66
   345
	std::ostringstream ostr;
kpeter@66
   346
	ostr << "lemon::FileOpenError" << ": ";
kpeter@66
   347
	ostr << "Cannot open file - " << file();
kpeter@66
   348
	_message_holder.set(ostr.str());
kpeter@66
   349
      }
kpeter@66
   350
      catch (...) {}
kpeter@66
   351
      if( _message_holder.valid()) return _message_holder.get().c_str();
kpeter@66
   352
      return "lemon::FileOpenError";
kpeter@66
   353
    }
kpeter@66
   354
    virtual ~FileOpenError() throw() {}
kpeter@66
   355
  };
kpeter@66
   356
kpeter@66
   357
  class IoParameterError : public IoError {
kpeter@66
   358
  protected:
kpeter@66
   359
    ExceptionMember<std::string> _message;
kpeter@66
   360
    ExceptionMember<std::string> _file;
kpeter@66
   361
kpeter@66
   362
    mutable ExceptionMember<std::string> _message_holder;
kpeter@66
   363
  public:
kpeter@66
   364
kpeter@66
   365
    IoParameterError(const IoParameterError &ile) :
kpeter@66
   366
      IoError(ile), _message(ile._message), _file(ile._file) {}
kpeter@66
   367
kpeter@66
   368
    ///\e
kpeter@66
   369
    explicit IoParameterError(const char *the_message)
kpeter@66
   370
      : _message(the_message) {}
kpeter@66
   371
kpeter@66
   372
    ///\e
kpeter@66
   373
    IoParameterError(const char *file_name, const char *the_message)
kpeter@66
   374
      : _message(the_message), _file(file_name) {}
kpeter@66
   375
kpeter@66
   376
     ///\e
kpeter@66
   377
    void message(const std::string& msg) { _message.set(msg); }
kpeter@66
   378
    ///\e
kpeter@66
   379
    void file(const std::string &fl) { _file.set(fl); }
kpeter@66
   380
kpeter@66
   381
     ///\e
kpeter@66
   382
    const char* message() const {
kpeter@66
   383
      if (_message.valid()) {
kpeter@66
   384
	return _message.get().c_str();
kpeter@66
   385
      } else {
kpeter@66
   386
	return 0;
kpeter@66
   387
      }
kpeter@66
   388
    }
kpeter@66
   389
kpeter@66
   390
    /// \brief Returns the filename.
kpeter@66
   391
    ///
kpeter@66
   392
    /// Returns \c 0 if the filename was not specified.
kpeter@66
   393
    const char* file() const {
kpeter@66
   394
      if (_file.valid()) {
kpeter@66
   395
	return _file.get().c_str();
kpeter@66
   396
      } else {
kpeter@66
   397
	return 0;
kpeter@66
   398
      }
kpeter@66
   399
    }
kpeter@66
   400
kpeter@66
   401
    ///\e
kpeter@66
   402
    virtual const char* what() const throw() {
kpeter@66
   403
      try {
kpeter@66
   404
	std::ostringstream ostr;
kpeter@66
   405
	if (message()) ostr << message();
kpeter@66
   406
	if (file()) ostr << "(when reading file '" << file() << "')";
kpeter@66
   407
	_message_holder.set(ostr.str());
kpeter@66
   408
      }
kpeter@66
   409
      catch (...) {}
kpeter@66
   410
      if( _message_holder.valid() ) return _message_holder.get().c_str();
kpeter@66
   411
      return "lemon:IoParameterError";
kpeter@66
   412
    }
kpeter@66
   413
    virtual ~IoParameterError() throw() {}
kpeter@66
   414
  };
kpeter@66
   415
kpeter@66
   416
kpeter@66
   417
  ///\e
kpeter@66
   418
  class AssertionFailedError : public LogicError {
kpeter@66
   419
  protected:
kpeter@66
   420
    const char *assertion;
kpeter@66
   421
    const char *file;
kpeter@66
   422
    int line;
kpeter@66
   423
    const char *function;
kpeter@66
   424
    const char *message;
kpeter@66
   425
kpeter@66
   426
    mutable ExceptionMember<std::string> _message_holder;
kpeter@66
   427
  public:
kpeter@66
   428
    ///\e
kpeter@66
   429
    AssertionFailedError(const char *_file, int _line, const char *func,
kpeter@66
   430
			 const char *msg, const char *_assertion = 0) :
kpeter@66
   431
      assertion(_assertion), file(_file), line(_line), function(func),
kpeter@66
   432
      message(msg) {}
kpeter@66
   433
kpeter@66
   434
    ///\e
kpeter@66
   435
    const char* get_assertion() const { return assertion; }
kpeter@66
   436
    ///\e
kpeter@66
   437
    const char* get_message() const { return message; }
kpeter@66
   438
    ///\e
kpeter@66
   439
    const char* get_file() const { return file; }
kpeter@66
   440
    ///\e
kpeter@66
   441
    const char* get_function() const { return function; }
kpeter@66
   442
    ///\e
kpeter@66
   443
    int get_line() const { return line; }
kpeter@66
   444
kpeter@66
   445
kpeter@66
   446
    virtual const char* what() const throw() {
kpeter@66
   447
      try {
kpeter@66
   448
	std::ostringstream ostr;
kpeter@66
   449
	ostr << file << ":" << line << ": ";
kpeter@66
   450
	if( function )
kpeter@66
   451
	  ostr << function << ": ";
kpeter@66
   452
	ostr << message;
kpeter@66
   453
	if( assertion )
kpeter@66
   454
	   ostr << " (assertion '" << assertion << "' failed)";
kpeter@66
   455
	_message_holder.set(ostr.str());
kpeter@66
   456
	return ostr.str().c_str();
kpeter@66
   457
      }
kpeter@66
   458
      catch(...) {}
kpeter@66
   459
      if( _message_holder.valid() ) return _message_holder.get().c_str();
kpeter@66
   460
      return "lemon::AssertionFailedError";
kpeter@66
   461
    }
kpeter@66
   462
   virtual ~AssertionFailedError() throw() {}
kpeter@66
   463
  };
kpeter@66
   464
kpeter@66
   465
kpeter@66
   466
  /****************  Macros  ****************/
kpeter@66
   467
kpeter@66
   468
kpeter@66
   469
  template <typename Exception>
kpeter@66
   470
  inline void assert_fail(const char *file, int line,
kpeter@66
   471
                          const char *func,
kpeter@66
   472
                          Exception exception,
kpeter@66
   473
                          const char *assertion = 0,
kpeter@66
   474
                          bool do_abort=true)
kpeter@66
   475
  {
kpeter@66
   476
    using namespace std;
kpeter@66
   477
    cerr << file << ":" << line << ": ";
kpeter@66
   478
    if (func)
kpeter@66
   479
      cerr << func << ": ";
kpeter@66
   480
    cerr << exception.what();
kpeter@66
   481
    if (assertion)
kpeter@66
   482
      cerr << " (assertion '" << assertion << "' failed)";
kpeter@66
   483
    cerr << endl;
kpeter@66
   484
    if (do_abort)
kpeter@66
   485
      abort();
kpeter@66
   486
  }
kpeter@66
   487
kpeter@66
   488
  template <>
kpeter@66
   489
  inline void assert_fail<const char *>(const char *file, int line,
kpeter@66
   490
                                        const char *func,
kpeter@66
   491
                                        const char *message,
kpeter@66
   492
                                        const char *assertion,
kpeter@66
   493
                                        bool do_abort)
kpeter@66
   494
  {
kpeter@66
   495
    using namespace std;
kpeter@66
   496
    cerr << file << ":" << line << ": ";
kpeter@66
   497
    if (func)
kpeter@66
   498
      cerr << func << ": ";
kpeter@66
   499
    cerr << message;
kpeter@66
   500
    if (assertion)
kpeter@66
   501
      cerr << " (assertion '" << assertion << "' failed)";
kpeter@66
   502
    cerr << endl;
kpeter@66
   503
    if (do_abort)
kpeter@66
   504
      abort();
kpeter@66
   505
  }
kpeter@66
   506
kpeter@66
   507
  template <>
kpeter@66
   508
  inline void assert_fail<std::string>(const char *file, int line,
kpeter@66
   509
                                       const char *func,
kpeter@66
   510
                                       std::string message,
kpeter@66
   511
                                       const char *assertion,
kpeter@66
   512
                                       bool do_abort)
kpeter@66
   513
  {
kpeter@66
   514
    assert_fail(file, line, func, message.c_str(), assertion, do_abort);
kpeter@66
   515
  }
kpeter@66
   516
kpeter@66
   517
  template <typename Exception>
kpeter@66
   518
  inline void assert_fail_failure(const char *file, int line, const char *func,
kpeter@66
   519
			   Exception exception,
kpeter@66
   520
			   const char *assertion = 0,
kpeter@66
   521
			   bool = true)
kpeter@66
   522
  {
kpeter@66
   523
    throw AssertionFailedError(file, line, func, exception.what(), assertion);
kpeter@66
   524
  }
kpeter@66
   525
kpeter@66
   526
  template <>
kpeter@66
   527
  inline void assert_fail_failure<const char *>(const char *file, int line,
kpeter@66
   528
                                                const char *func,
kpeter@66
   529
                                                const char *message,
kpeter@66
   530
                                                const char *assertion,
kpeter@66
   531
                                                bool)
kpeter@66
   532
  {
kpeter@66
   533
    throw AssertionFailedError(file, line, func, message, assertion);
kpeter@66
   534
  }
kpeter@66
   535
kpeter@66
   536
  template <>
kpeter@66
   537
  inline void assert_fail_failure<std::string>(const char *file, int line,
kpeter@66
   538
                                               const char *func,
kpeter@66
   539
                                               std::string message,
kpeter@66
   540
                                               const char *assertion,
kpeter@66
   541
                                               bool)
kpeter@66
   542
  {
kpeter@66
   543
    assert_fail_failure(file, line, func, message.c_str(), assertion, true);
kpeter@66
   544
  }
kpeter@66
   545
kpeter@66
   546
  template <typename Exception>
kpeter@66
   547
  inline void assert_fail_exception(const char *file, int line, const char *func,
kpeter@66
   548
			     Exception exception,
kpeter@66
   549
			     const char *assertion = 0, bool = true)
kpeter@66
   550
  {
kpeter@66
   551
    throw exception;
kpeter@66
   552
  }
kpeter@66
   553
kpeter@66
   554
  template <>
kpeter@66
   555
  inline void assert_fail_exception<const char *>(const char *file, int line,
kpeter@66
   556
					   const char *func,
kpeter@66
   557
					   const char *message,
kpeter@66
   558
					   const char *assertion,
kpeter@66
   559
					   bool)
kpeter@66
   560
  {
kpeter@66
   561
    throw AssertionFailedError(file, line, func, message, assertion);
kpeter@66
   562
  }
kpeter@66
   563
kpeter@66
   564
  template <>
kpeter@66
   565
  inline void assert_fail_exception<std::string>(const char *file, int line,
kpeter@66
   566
                                                 const char *func,
kpeter@66
   567
                                                 std::string message,
kpeter@66
   568
                                                 const char *assertion,
kpeter@66
   569
                                                 bool)
kpeter@66
   570
  {
kpeter@66
   571
    assert_fail_exception(file, line, func, message.c_str(), assertion, true);
kpeter@66
   572
  }
kpeter@66
   573
kpeter@66
   574
/// @}
kpeter@66
   575
kpeter@66
   576
}
kpeter@66
   577
#endif // LEMON_ERROR_H
kpeter@66
   578
kpeter@66
   579
#undef LEMON_ASSERT
kpeter@66
   580
#undef LEMON_FIXME
kpeter@66
   581
kpeter@66
   582
#ifdef LEMON_ENABLE_ASSERTS
kpeter@66
   583
#  define LEMON_ASSERT_ABORT
kpeter@66
   584
#endif
kpeter@66
   585
kpeter@66
   586
#ifndef LEMON_ASSERT_DO_ABORT
kpeter@66
   587
#  define LEMON_ASSERT_DO_ABORT 1
kpeter@66
   588
#endif
kpeter@66
   589
kpeter@66
   590
#ifndef LEMON_ASSERT_HANDLER
kpeter@66
   591
#  if defined LEMON_ASSERT_EXCEPTION
kpeter@66
   592
#    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_exception
kpeter@66
   593
#  elif defined LEMON_ASSERT_FAILURE
kpeter@66
   594
#    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_failure
kpeter@66
   595
#  elif defined LEMON_ASSERT_ABORT
kpeter@66
   596
#    define LEMON_ASSERT_HANDLER ::lemon::assert_fail
kpeter@66
   597
#  else
kpeter@66
   598
#    define LEMON_DISABLE_ASSERTS
kpeter@66
   599
#  endif
kpeter@66
   600
#endif
kpeter@66
   601
kpeter@66
   602
#ifdef DOXYGEN
kpeter@66
   603
kpeter@66
   604
/// \brief Macro for assertions with customizable message
kpeter@66
   605
///
kpeter@66
   606
/// Macro for assertions with customizable message.
kpeter@66
   607
///
kpeter@66
   608
/// The assertions are disabled in the default behaviour. You can
kpeter@66
   609
/// enable the assertions with the
kpeter@66
   610
/// \code
kpeter@66
   611
/// #define LEMON_ENABLE_ASSERTS
kpeter@66
   612
/// \endcode
kpeter@66
   613
/// Then an assert
kpeter@66
   614
/// provides a log on the standard error about the assertion and aborts
kpeter@66
   615
/// the program if LEMON_ASSERT_DO_ABORT is also defined (otherwise the
kpeter@66
   616
/// program keeps on running).
kpeter@66
   617
/// By defining LEMON_ASSERT_FAILURE or
kpeter@66
   618
/// LEMON_ASSERT_EXCEPTION, you can set other behaviour to the
kpeter@66
   619
/// assertions. In case LEMON_ASSERT_FAILURE is given, LEMON_ASSERT
kpeter@66
   620
/// will always throw an \c AssertionFailedError exception with
kpeter@66
   621
/// the \c msg error message. By using
kpeter@66
   622
/// LEMON_ASSERT_EXCEPTION, one can define an arbitrary exception to be thrown.
kpeter@66
   623
///
kpeter@66
   624
/// The LEMON_ASSERT macro should be called with the \c exp parameter
kpeter@66
   625
/// which should be an expression convertible to bool. If the given
kpeter@66
   626
/// parameter is false the assertion is raised and one of the assertion
kpeter@66
   627
/// behaviour will be activated. The \c msg should be either a const
kpeter@66
   628
/// char* message or an exception. When the \c msg is an exception the
kpeter@66
   629
/// \ref lemon::Exception::what() "what()" function is called to retrieve and
kpeter@66
   630
/// display the error message.
kpeter@66
   631
///
kpeter@66
   632
/// \todo We should provide some way to reset to the default behaviour,
kpeter@66
   633
/// shouldn't we?
kpeter@66
   634
///
kpeter@66
   635
/// \todo This whole 'assert' business should be placed in a separate
kpeter@66
   636
/// include file. The boost assert is not guarded by header sentries
kpeter@66
   637
/// which may help to change the behaviour of the assertions in
kpeter@66
   638
/// the files.
kpeter@66
   639
///
kpeter@66
   640
/// \todo __PRETTY_FUNCTION__ should be replaced by something
kpeter@66
   641
/// compiler-independent, like BOOST_CURRENT_FUNCTION
kpeter@66
   642
kpeter@66
   643
#  define LEMON_ASSERT(exp, msg)                 \
kpeter@66
   644
     (static_cast<void> (!!(exp) ? 0 : (         \
kpeter@66
   645
       LEMON_ASSERT_HANDLER(__FILE__, __LINE__,  \
kpeter@66
   646
                            __PRETTY_FUNCTION__, \
kpeter@66
   647
                            msg, #exp, LEMON_ASSERT_DO_ABORT), 0)))
kpeter@66
   648
kpeter@66
   649
#else
kpeter@66
   650
#  if defined LEMON_DISABLE_ASSERTS
kpeter@66
   651
kpeter@66
   652
#    define LEMON_ASSERT(exp, msg)  (static_cast<void> (0))
kpeter@66
   653
kpeter@66
   654
#  else
kpeter@66
   655
#    define LEMON_ASSERT(exp, msg)                 \
kpeter@66
   656
       (static_cast<void> (!!(exp) ? 0 : (         \
kpeter@66
   657
         LEMON_ASSERT_HANDLER(__FILE__, __LINE__,  \
kpeter@66
   658
                              __PRETTY_FUNCTION__, \
kpeter@66
   659
                              msg, #exp, LEMON_ASSERT_DO_ABORT), 0)))
kpeter@66
   660
#  endif
kpeter@66
   661
#endif
kpeter@66
   662
kpeter@66
   663
/**
kpeter@66
   664
 * \brief Macro for mark not yet implemented features.
kpeter@66
   665
 *
kpeter@66
   666
 * \todo Is this the right place for this? It should be used only in
kpeter@66
   667
 * modules under development.
kpeter@66
   668
 *
kpeter@66
   669
 * \todo __PRETTY_FUNCTION__ should be replaced by something
kpeter@66
   670
 * compiler-independent, like BOOST_CURRENT_FUNCTION
kpeter@66
   671
 */
kpeter@66
   672
kpeter@66
   673
#define LEMON_FIXME(msg) \
kpeter@66
   674
    (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
kpeter@66
   675
			  "FIXME: " msg))