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