src/lemon/error.h
author alpar
Fri, 28 Jan 2005 08:53:48 +0000
changeset 1101 9286569c3749
parent 921 818510fa3d99
permissions -rw-r--r--
Wrap a long line
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/error.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@883
    16
alpar@921
    17
#ifndef LEMON_ERROR_H
alpar@921
    18
#define LEMON_ERROR_H
alpar@883
    19
alpar@883
    20
//! \ingroup misc
alpar@883
    21
//! \file
alpar@883
    22
//! \brief Basic error handling (signaling) routines.
alpar@883
    23
alpar@883
    24
#include <exception>
alpar@883
    25
#include <string>
alpar@883
    26
#include <sstream>
alpar@883
    27
alpar@883
    28
alpar@921
    29
namespace lemon {
alpar@883
    30
alpar@883
    31
  /**
alpar@883
    32
   * \brief Generic exception class.
alpar@883
    33
   *
alpar@883
    34
   * \todo Do we need this?
alpar@883
    35
   *
alpar@883
    36
   * \todo Don't we need different kind of exceptions for different kind
alpar@883
    37
   * of errors?
alpar@883
    38
   * Shouldn't we use \<stdexcept\> instead?
alpar@883
    39
   */
alpar@883
    40
  class Exception : public std::exception {
alpar@883
    41
  protected:
alpar@883
    42
    std::ostringstream buf;
alpar@883
    43
  public:
alpar@883
    44
    Exception() {}
alpar@883
    45
    explicit Exception(const std::string &s) { buf << s; }
alpar@883
    46
    Exception(const Exception &e) : std::exception() {
alpar@883
    47
      buf << e.buf.str();
alpar@883
    48
    }
alpar@883
    49
    virtual ~Exception() throw() {}
alpar@883
    50
    
alpar@883
    51
    virtual const char* what() const throw() {
alpar@883
    52
      return buf.str().c_str();
alpar@883
    53
    }
alpar@883
    54
klao@993
    55
    template <typename T>
klao@993
    56
    Exception& operator<<(T const& t) { buf << t; return *this; }
alpar@883
    57
  };
alpar@883
    58
alpar@883
    59
  /**
alpar@883
    60
   * \brief Generic error signaling function.
alpar@883
    61
   *
alpar@883
    62
   * \todo Do we really need this? Is it helpful?
alpar@883
    63
   */
alpar@883
    64
  inline void fault(const std::string &msg) {
alpar@883
    65
    throw Exception(msg);
alpar@883
    66
  }
alpar@883
    67
alpar@883
    68
  /**
alpar@883
    69
   * \brief Macro for mark not yet implemented features.
alpar@883
    70
   *
alpar@883
    71
   * \todo Is this the right place for this? It should be used only in
alpar@883
    72
   * modules under development.
alpar@883
    73
   */
alpar@883
    74
alpar@883
    75
# define FIXME(msg) \
alpar@921
    76
    do { throw ::lemon::Exception() << "FIXME: " msg " (in: "    \
klao@993
    77
      __FILE__ ", " << __LINE__ << ")";                          \
alpar@883
    78
    } while(false)
alpar@883
    79
alpar@883
    80
}
alpar@921
    81
#endif // LEMON_ERROR_H