1 | /* -*- C++ -*- |
---|
2 | * src/hugo/error.h - Part of HUGOlib, a generic C++ optimization library |
---|
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 | */ |
---|
16 | |
---|
17 | #ifndef HUGO_ERROR_H |
---|
18 | #define HUGO_ERROR_H |
---|
19 | |
---|
20 | //! \ingroup misc |
---|
21 | //! \file |
---|
22 | //! \brief Basic error handling (signaling) routines. |
---|
23 | |
---|
24 | #include <exception> |
---|
25 | #include <string> |
---|
26 | #include <sstream> |
---|
27 | |
---|
28 | |
---|
29 | namespace hugo { |
---|
30 | |
---|
31 | /** |
---|
32 | * \brief Generic exception class. |
---|
33 | * |
---|
34 | * \todo Do we need this? |
---|
35 | * |
---|
36 | * \todo Don't we need different kind of exceptions for different kind |
---|
37 | * of errors? |
---|
38 | * Shouldn't we use \<stdexcept\> instead? |
---|
39 | */ |
---|
40 | class Exception : public std::exception { |
---|
41 | protected: |
---|
42 | std::ostringstream buf; |
---|
43 | public: |
---|
44 | Exception() {} |
---|
45 | explicit Exception(const std::string &s) { buf << s; } |
---|
46 | Exception(const Exception &e) : std::exception() { |
---|
47 | buf << e.buf.str(); |
---|
48 | } |
---|
49 | virtual ~Exception() throw() {} |
---|
50 | |
---|
51 | virtual const char* what() const throw() { |
---|
52 | return buf.str().c_str(); |
---|
53 | } |
---|
54 | |
---|
55 | Exception& operator<<(std::string const& s) { buf << s; return *this; } |
---|
56 | Exception& operator<<(char const *s) { buf << s; return *this; } |
---|
57 | Exception& operator<<(int i) { buf << i; return *this; } |
---|
58 | }; |
---|
59 | |
---|
60 | /** |
---|
61 | * \brief Generic error signaling function. |
---|
62 | * |
---|
63 | * \todo Do we really need this? Is it helpful? |
---|
64 | */ |
---|
65 | inline void fault(const std::string &msg) { |
---|
66 | throw Exception(msg); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * \brief Macro for mark not yet implemented features. |
---|
71 | * |
---|
72 | * \todo Is this the right place for this? It should be used only in |
---|
73 | * modules under development. |
---|
74 | */ |
---|
75 | |
---|
76 | # define FIXME(msg) \ |
---|
77 | do { throw ::hugo::Exception() << "FIXME: " msg " (in: " \ |
---|
78 | __FILE__ ", " << __LINE__ << ")"; \ |
---|
79 | } while(false) |
---|
80 | |
---|
81 | } |
---|
82 | #endif // HUGO_ERROR_H |
---|