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