lemon/error.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 15 Jun 2008 22:05:23 +0200
changeset 171 02f4d5d9bfd7
parent 66 5f7a8570687d
child 209 765619b7cbb2
permissions -rw-r--r--
Improve and redesign test programs + unify their output (ticket #25)
- Move graph related utilities form test_tools.h to graph_test.h.
- Move the contents of graph_utils_test.h to graph_utils_test.cc.
- Rename map_test.h -> graph_maps_test.h.
- Rename digraph_test.h -> graph_test.h.
- Many improvements in the following files:
* digraph_test.cc
* graph_test.cc
* graph_test.h
* graph_maps_test.h
* graph_utils_test.cc
* bfs_test.cc
* dfs_test.cc
* counter_test.cc
- Test programs print messages only if it really seems necessary.
- Remove \file commands form .cc test files.
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
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
    ///\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
deba@108
   416
  /// @}
kpeter@66
   417
kpeter@66
   418
}
deba@108
   419
kpeter@66
   420
#endif // LEMON_ERROR_H