COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/klao/error.h @ 1061:e3433c024123

Last change on this file since 1061:e3433c024123 was 1061:e3433c024123, checked in by Alpar Juttner, 20 years ago
  • Empty doxygen comments
  • sconst -> const
File size: 4.3 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/error.h - Part of LEMON, a generic C++ optimization library
[906]3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
[883]16
[921]17#ifndef LEMON_ERROR_H
18#define LEMON_ERROR_H
[883]19
20//! \ingroup misc
21//! \file
22//! \brief Basic error handling (signaling) routines.
23
24#include <exception>
25#include <string>
26#include <sstream>
27
[1056]28#include <boost/shared_ptr.hpp>
[883]29
[921]30namespace lemon {
[883]31
[1056]32  /// Exception-safe convenient "error message" class.
33  class ErrorMessage {
34  protected:
[1061]35  ///\e
[1056]36    boost::shared_ptr<std::ostringstream> buf;
37   
[1061]38  ///\e
[1056]39    bool init() throw() {
40      try {
41        buf.reset(new std::ostringstream);
42      }
43      catch(...) {
44        buf.reset();
45      }
46      return buf;
47    }
48
49  public:
50
[1061]51  ///\e
[1056]52    ErrorMessage() throw() { init(); }
53
[1061]54  ///\e
[1056]55    ErrorMessage(const char *message) throw() {
56      init();
57      *this << message;
58    }
59
[1061]60  ///\e
[1056]61    ErrorMessage(const std::string &message) throw() {
62      init();
63      *this << message;
64    }
65
[1061]66  ///\e
[1056]67    template <typename T>
68    ErrorMessage& operator<<(const T &t) throw() {
69      if( !buf ) return *this;
70
71      try {
72        *buf << t;
73      }
74      catch(...) {
75        buf.reset();
76      }
77    }
78
[1061]79  ///\e
[1056]80    const char* message() throw() {
81      if( !buf ) return 0;
82
83      const char* mes = 0;
84      try {
85        mes = buf->str().c_str();
86      }
87      catch(...) {}
88      return mes;
89    }
90   
91  };
92
[883]93  /**
94   * \brief Generic exception class.
95   *
[1056]96   * Base class for exceptions used in LEMON.
[883]97   */
[1056]98  class Exception : public std::exception, public ErrorMessage {
[883]99  public:
[1061]100  ///\e
[1056]101    Exception() throw() {}
[1061]102  ///\e
[1056]103    explicit Exception(const std::string &s) throw()
104      : ErrorMessage(s) {}
[1061]105  ///\e
[883]106    virtual ~Exception() throw() {}
107   
[1061]108  ///\e
[883]109    virtual const char* what() const throw() {
[1056]110      const char *mes = message();
111      if( mes ) return mes;
112      return "lemon::Exception";
113    }
114  };
115
[1061]116  ///\e
[1056]117  class LogicError : public Exception {
[1061]118  ///\e
[1056]119    explicit LogicError(const std::string &s)
120      : Exception(s) {}
121  };
122
[1061]123  ///\e
[1056]124  class RuntimeError : public Exception {
[1061]125  ///\e
[1056]126    explicit RuntimeError(const std::string &s)
127      : Exception(s) {}
128  };
129
[1061]130  ///\e
[1056]131  class RangeError : public RuntimeError {
[1061]132  ///\e
[1056]133    explicit RangeError(const std::string &s)
134      : RuntimeError(s) {}
135  };
136
[1061]137  ///\e
[1056]138  class IOError : public RuntimeError {
[1061]139  ///\e
[1056]140    explicit IOError(const std::string &s)
141      : RuntimeError(s) {}
142  };
143
[1061]144  ///\e
[1056]145  class DataFormatError : public IOError {
[1061]146  ///\e
[1056]147    explicit DataFormatError(const std::string &message)
148      : IOError(message) : line(0) {}
[1061]149  ///\e
[1056]150    DataFormatError(const std::string &file_name, int line_num,
[1061]151                    const std::string &message)
[1056]152      : IOError(message), line(line_num) { set_file(file_name); }
153
[1061]154  ///\e
[1056]155    void set_line(int line_num) { line=line_num; }
[1061]156  ///\e
[1056]157    void set_file(const std::string &file_name) {
158      try {
159        file.reset(new std::string);
160        *file = file_name;
161      }
162      catch(...) {
163        file.reset();
164      }
[883]165    }
166
[1061]167  ///\e
[1056]168    virtual const char* what() const {
169      const char *mes = 0;
170      try {
171        std::ostringstream ostr;
172        ostr << IOError::what();
173        if( file || line ) {
174          ostr << " (";
175          if( file ) ostr << "in file" << *file;
176          if( line ) ostr << " at line" << line;
177        }
178        mes = ostr.str().c_str();
179      }
180      catch(...) {}
181      if( mes ) return mes;
182      return "lemon::DataFormatError";
183    }
[883]184  };
185
[1056]186
187
188  /****************  Macros  ****************/
189
190
[883]191  /**
[1056]192   * \brief Macro for assertions with customizable message
[883]193   */
[1056]194
195# define lemon_assert(exp, msg) \
196    if(!(exp)) { \
197      std::cerr << __FILE__ ":" << __LINE__ << ": " << (msg) << std::endl; \
198      abort; \
199    }
200
[883]201
202  /**
203   * \brief Macro for mark not yet implemented features.
204   *
205   * \todo Is this the right place for this? It should be used only in
206   * modules under development.
207   */
208
[1056]209# define FIXME(msg) lemon_assert(0, "FIXME: " msg)
[883]210
211}
[921]212#endif // LEMON_ERROR_H
Note: See TracBrowser for help on using the repository browser.