src/hugo/error.h
author alpar
Thu, 16 Sep 2004 19:18:18 +0000
changeset 872 c010b38ea35b
parent 508 95f8f7171bae
permissions -rw-r--r--
Cross references turned off.
klao@489
     1
// -*- C++ -*- //
klao@489
     2
klao@489
     3
#ifndef HUGO_ERROR_H
klao@489
     4
#define HUGO_ERROR_H
klao@489
     5
klao@491
     6
//! \ingroup misc
klao@489
     7
//! \file
klao@489
     8
//! \brief Basic error handling (signaling) routines.
klao@489
     9
klao@490
    10
#include <exception>
klao@489
    11
#include <string>
klao@489
    12
#include <sstream>
klao@489
    13
klao@489
    14
klao@489
    15
namespace hugo {
klao@489
    16
klao@489
    17
  /**
klao@489
    18
   * \brief Generic exception class.
klao@489
    19
   *
klao@489
    20
   * \todo Do we need this?
klao@489
    21
   *
klao@489
    22
   * \todo Don't we need different kind of exceptions for different kind
klao@489
    23
   * of errors?
alpar@508
    24
   * Shouldn't we use \<stdexcept\> instead?
klao@489
    25
   */
klao@489
    26
  class Exception : public std::exception {
klao@489
    27
  protected:
klao@489
    28
    std::ostringstream buf;
klao@489
    29
  public:
klao@489
    30
    Exception() {}
klao@489
    31
    explicit Exception(const std::string &s) { buf << s; }
klao@490
    32
    Exception(const Exception &e) : std::exception() {
klao@490
    33
      buf << e.buf.str();
klao@490
    34
    }
klao@489
    35
    virtual ~Exception() throw() {}
klao@489
    36
    
klao@489
    37
    virtual const char* what() const throw() {
klao@489
    38
      return buf.str().c_str();
klao@489
    39
    }
klao@489
    40
klao@489
    41
    Exception& operator<<(std::string const& s) { buf << s; return *this; }
klao@489
    42
    Exception& operator<<(char const *s) { buf << s; return *this; }
klao@489
    43
    Exception& operator<<(int i) { buf << i; return *this; }
klao@489
    44
  };
klao@489
    45
klao@489
    46
  /**
klao@489
    47
   * \brief Generic error signaling function.
klao@489
    48
   *
klao@489
    49
   * \todo Do we really need this? Is it helpful?
klao@489
    50
   */
klao@489
    51
  inline void fault(const std::string &msg) {
klao@489
    52
    throw Exception(msg);
klao@489
    53
  }
klao@489
    54
klao@489
    55
  /**
klao@489
    56
   * \brief Macro for mark not yet implemented features.
klao@489
    57
   *
klao@489
    58
   * \todo Is this the right place for this? It should be used only in
klao@489
    59
   * modules under development.
klao@489
    60
   */
klao@489
    61
klao@489
    62
# define FIXME(msg) \
klao@490
    63
    do { throw ::hugo::Exception() << "FIXME: " msg " (in: "    \
klao@490
    64
      __FILE__ ", " << __LINE__ << ")";                         \
klao@489
    65
    } while(false)
klao@489
    66
klao@489
    67
}
klao@489
    68
#endif // HUGO_ERROR_H