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