lemon/error.h
author Alpar Juttner <alpar@cs.elte.hu>
Sat, 27 Sep 2008 14:33:28 +0200
changeset 287 bb40b6db0a58
parent 212 1ae84dea7d09
child 290 f6899946c1ac
permissions -rw-r--r--
Merge
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@66
     2
 *
alpar@209
     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 {
alpar@209
    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 {
alpar@209
    54
        ptr.reset(new Type());
alpar@209
    55
        if (ptr.get() == 0) return;
alpar@209
    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 {
alpar@209
    62
        if (!copy.valid()) return;
alpar@209
    63
        ptr.reset(new Type());
alpar@209
    64
        if (ptr.get() == 0) return;
alpar@209
    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 {
alpar@209
    72
        if (!copy.valid()) return;
kpeter@212
    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 {
alpar@209
    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
deba@108
    96
  /// Exception-safe convenient error message builder 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
    mutable std::auto_ptr<std::ostringstream> buf;
kpeter@66
   106
kpeter@66
   107
    ///\e
kpeter@66
   108
    bool init() throw() {
kpeter@66
   109
      try {
alpar@209
   110
        buf.reset(new std::ostringstream);
kpeter@66
   111
      }
kpeter@66
   112
      catch(...) {
alpar@209
   113
        buf.reset();
kpeter@66
   114
      }
kpeter@66
   115
      return buf.get();
kpeter@66
   116
    }
kpeter@66
   117
kpeter@66
   118
  public:
kpeter@66
   119
kpeter@66
   120
    ///\e
kpeter@66
   121
    ErrorMessage() throw() { init(); }
kpeter@66
   122
kpeter@66
   123
    ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
kpeter@66
   124
kpeter@66
   125
    ///\e
kpeter@66
   126
    ErrorMessage(const char *msg) throw() {
kpeter@66
   127
      init();
kpeter@66
   128
      *this << msg;
kpeter@66
   129
    }
kpeter@66
   130
kpeter@66
   131
    ///\e
kpeter@66
   132
    ErrorMessage(const std::string &msg) throw() {
kpeter@66
   133
      init();
kpeter@66
   134
      *this << msg;
kpeter@66
   135
    }
kpeter@66
   136
kpeter@66
   137
    ///\e
kpeter@66
   138
    template <typename T>
kpeter@66
   139
    ErrorMessage& operator<<(const T &t) throw() {
kpeter@66
   140
      if( ! buf.get() ) return *this;
kpeter@66
   141
kpeter@66
   142
      try {
alpar@209
   143
        *buf << t;
kpeter@66
   144
      }
kpeter@66
   145
      catch(...) {
alpar@209
   146
        buf.reset();
kpeter@66
   147
      }
kpeter@66
   148
      return *this;
kpeter@66
   149
    }
kpeter@66
   150
kpeter@66
   151
    ///\e
kpeter@66
   152
    const char* message() throw() {
kpeter@66
   153
      if( ! buf.get() ) return 0;
kpeter@66
   154
kpeter@66
   155
      const char* mes = 0;
kpeter@66
   156
      try {
alpar@209
   157
        mes = buf->str().c_str();
kpeter@66
   158
      }
kpeter@66
   159
      catch(...) {}
kpeter@66
   160
      return mes;
kpeter@66
   161
    }
kpeter@66
   162
kpeter@66
   163
  };
kpeter@66
   164
kpeter@66
   165
  /// Generic exception class.
kpeter@66
   166
kpeter@66
   167
  /// Base class for exceptions used in LEMON.
kpeter@66
   168
  ///
kpeter@66
   169
  class Exception : public std::exception {
kpeter@66
   170
  public:
kpeter@66
   171
    ///\e
kpeter@66
   172
    Exception() {}
kpeter@66
   173
    ///\e
kpeter@66
   174
    virtual ~Exception() throw() {}
kpeter@66
   175
    ///\e
kpeter@66
   176
    virtual const char* what() const throw() {
kpeter@66
   177
      return "lemon::Exception";
kpeter@66
   178
    }
kpeter@66
   179
  };
kpeter@66
   180
kpeter@66
   181
  /// One of the two main subclasses of \ref Exception.
kpeter@66
   182
kpeter@66
   183
  /// Logic errors represent problems in the internal logic of a program;
kpeter@66
   184
  /// in theory, these are preventable, and even detectable before the
kpeter@66
   185
  /// program runs (e.g. violations of class invariants).
kpeter@66
   186
  ///
kpeter@66
   187
  /// A typical example for this is \ref UninitializedParameter.
kpeter@66
   188
  class LogicError : public Exception {
kpeter@66
   189
  public:
kpeter@66
   190
    virtual const char* what() const throw() {
kpeter@66
   191
      return "lemon::LogicError";
kpeter@66
   192
    }
kpeter@66
   193
  };
kpeter@66
   194
kpeter@66
   195
  /// \ref Exception for uninitialized parameters.
kpeter@66
   196
kpeter@66
   197
  /// This error represents problems in the initialization
kpeter@66
   198
  /// of the parameters of the algorithms.
kpeter@66
   199
  class UninitializedParameter : public LogicError {
kpeter@66
   200
  public:
kpeter@66
   201
    virtual const char* what() const throw() {
kpeter@66
   202
      return "lemon::UninitializedParameter";
kpeter@66
   203
    }
kpeter@66
   204
  };
kpeter@66
   205
kpeter@66
   206
kpeter@66
   207
  /// One of the two main subclasses of \ref Exception.
kpeter@66
   208
kpeter@66
   209
  /// Runtime errors represent problems outside the scope of a program;
kpeter@66
   210
  /// they cannot be easily predicted and can generally only be caught
kpeter@66
   211
  /// as the program executes.
kpeter@66
   212
  class RuntimeError : public Exception {
kpeter@66
   213
  public:
kpeter@66
   214
    virtual const char* what() const throw() {
kpeter@66
   215
      return "lemon::RuntimeError";
kpeter@66
   216
    }
kpeter@66
   217
  };
kpeter@66
   218
kpeter@66
   219
  ///\e
kpeter@66
   220
  class RangeError : public RuntimeError {
kpeter@66
   221
  public:
kpeter@66
   222
    virtual const char* what() const throw() {
kpeter@66
   223
      return "lemon::RangeError";
kpeter@66
   224
    }
kpeter@66
   225
  };
kpeter@66
   226
kpeter@66
   227
  ///\e
kpeter@66
   228
  class IoError : public RuntimeError {
kpeter@66
   229
  public:
kpeter@66
   230
    virtual const char* what() const throw() {
kpeter@66
   231
      return "lemon::IoError";
kpeter@66
   232
    }
kpeter@66
   233
  };
kpeter@66
   234
kpeter@66
   235
  ///\e
kpeter@66
   236
  class DataFormatError : public IoError {
kpeter@66
   237
  protected:
kpeter@66
   238
    ExceptionMember<std::string> _message;
kpeter@66
   239
    ExceptionMember<std::string> _file;
kpeter@66
   240
    int _line;
kpeter@66
   241
kpeter@66
   242
    mutable ExceptionMember<std::string> _message_holder;
kpeter@66
   243
  public:
kpeter@66
   244
kpeter@66
   245
    DataFormatError(const DataFormatError &dfe) :
kpeter@66
   246
      IoError(dfe), _message(dfe._message), _file(dfe._file),
kpeter@66
   247
      _line(dfe._line) {}
kpeter@66
   248
kpeter@66
   249
    ///\e
kpeter@66
   250
    explicit DataFormatError(const char *the_message)
kpeter@66
   251
      : _message(the_message), _line(0) {}
kpeter@66
   252
kpeter@66
   253
    ///\e
kpeter@66
   254
    DataFormatError(const std::string &file_name, int line_num,
alpar@209
   255
                    const char *the_message)
kpeter@66
   256
      : _message(the_message), _line(line_num) { file(file_name); }
kpeter@66
   257
kpeter@66
   258
    ///\e
kpeter@66
   259
    void line(int ln) { _line = ln; }
kpeter@66
   260
    ///\e
kpeter@66
   261
    void message(const std::string& msg) { _message.set(msg); }
kpeter@66
   262
    ///\e
kpeter@66
   263
    void file(const std::string &fl) { _file.set(fl); }
kpeter@66
   264
kpeter@66
   265
    ///\e
kpeter@66
   266
    int line() const { return _line; }
kpeter@66
   267
    ///\e
kpeter@66
   268
    const char* message() const {
kpeter@66
   269
      if (_message.valid() && !_message.get().empty()) {
alpar@209
   270
        return _message.get().c_str();
kpeter@66
   271
      } else {
alpar@209
   272
        return 0;
kpeter@66
   273
      }
kpeter@66
   274
    }
kpeter@66
   275
kpeter@66
   276
    /// \brief Returns the filename.
kpeter@66
   277
    ///
kpeter@66
   278
    /// Returns \e null if the filename was not specified.
kpeter@66
   279
    const char* file() const {
kpeter@66
   280
      if (_file.valid() && !_file.get().empty()) {
alpar@209
   281
        return _file.get().c_str();
kpeter@66
   282
      } else {
alpar@209
   283
        return 0;
kpeter@66
   284
      }
kpeter@66
   285
    }
kpeter@66
   286
kpeter@66
   287
    ///\e
kpeter@66
   288
    virtual const char* what() const throw() {
kpeter@66
   289
      try {
alpar@209
   290
        std::ostringstream ostr;
alpar@209
   291
        ostr << "lemon:DataFormatError" << ": ";
alpar@209
   292
        if (message()) ostr << message();
alpar@209
   293
        if( file() || line() != 0 ) {
alpar@209
   294
          ostr << " (";
alpar@209
   295
          if( file() ) ostr << "in file '" << file() << "'";
alpar@209
   296
          if( file() && line() != 0 ) ostr << " ";
alpar@209
   297
          if( line() != 0 ) ostr << "at line " << line();
alpar@209
   298
          ostr << ")";
alpar@209
   299
        }
alpar@209
   300
        _message_holder.set(ostr.str());
kpeter@66
   301
      }
kpeter@66
   302
      catch (...) {}
kpeter@66
   303
      if( _message_holder.valid()) return _message_holder.get().c_str();
kpeter@66
   304
      return "lemon:DataFormatError";
kpeter@66
   305
    }
kpeter@66
   306
kpeter@66
   307
    virtual ~DataFormatError() throw() {}
kpeter@66
   308
  };
kpeter@66
   309
kpeter@66
   310
  ///\e
kpeter@66
   311
  class FileOpenError : public IoError {
kpeter@66
   312
  protected:
kpeter@66
   313
    ExceptionMember<std::string> _file;
kpeter@66
   314
kpeter@66
   315
    mutable ExceptionMember<std::string> _message_holder;
kpeter@66
   316
  public:
kpeter@66
   317
kpeter@66
   318
    FileOpenError(const FileOpenError &foe) :
kpeter@66
   319
      IoError(foe), _file(foe._file) {}
kpeter@66
   320
kpeter@66
   321
    ///\e
kpeter@66
   322
    explicit FileOpenError(const std::string& fl)
kpeter@66
   323
      : _file(fl) {}
kpeter@66
   324
kpeter@66
   325
kpeter@66
   326
    ///\e
kpeter@66
   327
    void file(const std::string &fl) { _file.set(fl); }
kpeter@66
   328
kpeter@66
   329
    /// \brief Returns the filename.
kpeter@66
   330
    ///
kpeter@66
   331
    /// Returns \e null if the filename was not specified.
kpeter@66
   332
    const char* file() const {
kpeter@66
   333
      if (_file.valid() && !_file.get().empty()) {
alpar@209
   334
        return _file.get().c_str();
kpeter@66
   335
      } else {
alpar@209
   336
        return 0;
kpeter@66
   337
      }
kpeter@66
   338
    }
kpeter@66
   339
kpeter@66
   340
    ///\e
kpeter@66
   341
    virtual const char* what() const throw() {
kpeter@66
   342
      try {
alpar@209
   343
        std::ostringstream ostr;
alpar@209
   344
        ostr << "lemon::FileOpenError" << ": ";
alpar@209
   345
        ostr << "Cannot open file - " << file();
alpar@209
   346
        _message_holder.set(ostr.str());
kpeter@66
   347
      }
kpeter@66
   348
      catch (...) {}
kpeter@66
   349
      if( _message_holder.valid()) return _message_holder.get().c_str();
kpeter@66
   350
      return "lemon::FileOpenError";
kpeter@66
   351
    }
kpeter@66
   352
    virtual ~FileOpenError() throw() {}
kpeter@66
   353
  };
kpeter@66
   354
kpeter@66
   355
  class IoParameterError : public IoError {
kpeter@66
   356
  protected:
kpeter@66
   357
    ExceptionMember<std::string> _message;
kpeter@66
   358
    ExceptionMember<std::string> _file;
kpeter@66
   359
kpeter@66
   360
    mutable ExceptionMember<std::string> _message_holder;
kpeter@66
   361
  public:
kpeter@66
   362
kpeter@66
   363
    IoParameterError(const IoParameterError &ile) :
kpeter@66
   364
      IoError(ile), _message(ile._message), _file(ile._file) {}
kpeter@66
   365
kpeter@66
   366
    ///\e
kpeter@66
   367
    explicit IoParameterError(const char *the_message)
kpeter@66
   368
      : _message(the_message) {}
kpeter@66
   369
kpeter@66
   370
    ///\e
kpeter@66
   371
    IoParameterError(const char *file_name, const char *the_message)
kpeter@66
   372
      : _message(the_message), _file(file_name) {}
kpeter@66
   373
kpeter@66
   374
     ///\e
kpeter@66
   375
    void message(const std::string& msg) { _message.set(msg); }
kpeter@66
   376
    ///\e
kpeter@66
   377
    void file(const std::string &fl) { _file.set(fl); }
kpeter@66
   378
kpeter@66
   379
     ///\e
kpeter@66
   380
    const char* message() const {
kpeter@66
   381
      if (_message.valid()) {
alpar@209
   382
        return _message.get().c_str();
kpeter@66
   383
      } else {
alpar@209
   384
        return 0;
kpeter@66
   385
      }
kpeter@66
   386
    }
kpeter@66
   387
kpeter@66
   388
    /// \brief Returns the filename.
kpeter@66
   389
    ///
kpeter@66
   390
    /// Returns \c 0 if the filename was not specified.
kpeter@66
   391
    const char* file() const {
kpeter@66
   392
      if (_file.valid()) {
alpar@209
   393
        return _file.get().c_str();
kpeter@66
   394
      } else {
alpar@209
   395
        return 0;
kpeter@66
   396
      }
kpeter@66
   397
    }
kpeter@66
   398
kpeter@66
   399
    ///\e
kpeter@66
   400
    virtual const char* what() const throw() {
kpeter@66
   401
      try {
alpar@209
   402
        std::ostringstream ostr;
alpar@209
   403
        if (message()) ostr << message();
alpar@209
   404
        if (file()) ostr << "(when reading file '" << file() << "')";
alpar@209
   405
        _message_holder.set(ostr.str());
kpeter@66
   406
      }
kpeter@66
   407
      catch (...) {}
kpeter@66
   408
      if( _message_holder.valid() ) return _message_holder.get().c_str();
kpeter@66
   409
      return "lemon:IoParameterError";
kpeter@66
   410
    }
kpeter@66
   411
    virtual ~IoParameterError() throw() {}
kpeter@66
   412
  };
kpeter@66
   413
deba@108
   414
  /// @}
kpeter@66
   415
kpeter@66
   416
}
deba@108
   417
kpeter@66
   418
#endif // LEMON_ERROR_H