-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexceptions.h
98 lines (70 loc) · 2.18 KB
/
exceptions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef EXCEPTIONS_H__
#define EXCEPTIONS_H__
#include <vector>
#include <sstream>
#include <boost/exception/all.hpp>
/*
* EXCEPTIONS
*/
// top level
struct Exception : virtual boost::exception, virtual std::exception {};
// top level per module
struct GuiError : virtual Exception {};
struct SignalsError : virtual Exception {};
// general errors
struct Segfault : virtual Exception {};
struct IOError : virtual Exception {};
struct SizeMismatchError : virtual Exception {};
struct UsageError : virtual Exception {};
struct NullPointer : virtual Exception {};
struct NotYetImplemented : virtual Exception {};
/*
* THROW MACRO
*
* Usage:
*
* UTIL_THROW_EXCEPTION(ExceptionType, message)
*
* Example:
*
* UTIL_THROW_EXCEPTION(
* UsageError,
* "number of ordered pizzas is negative: " << numPizzas << " < 0");
*/
#define UTIL_THROW_EXCEPTION(exception, message) \
{ \
std::stringstream __util_messageStream; \
__util_messageStream << message; \
BOOST_THROW_EXCEPTION(exception() << error_message(__util_messageStream.str()) << STACK_TRACE); \
}
#define UTIL_RETHROW(e, message) \
{ \
std::stringstream __util_messageStream; \
__util_messageStream << *boost::get_error_info<error_message>(e) << message; \
BOOST_THROW_EXCEPTION(e << error_message(__util_messageStream.str())); \
}
/*
* TAGS
*/
class stack_trace_ {
public:
stack_trace_();
const std::vector<std::string>& get_stack_trace() const;
private:
const std::string& get_program_name();
std::string get_pid();
void initialise_program_name();
std::vector<std::string> _stack_trace;
std::string _program_name;
};
std::ostream& operator<<(std::ostream& out, const stack_trace_& trace);
typedef boost::error_info<struct tag_error_message, std::string> error_message;
typedef boost::error_info<struct tag_stack_trace, stack_trace_> stack_trace;
#define STACK_TRACE stack_trace(stack_trace_())
typedef boost::error_info<struct tag_mismatch_size1, int> mismatch_size1;
typedef boost::error_info<struct tag_mismatch_size2, int> mismatch_size2;
/*
* HELPER FUNCTIONS
*/
void handleException(const boost::exception& e, std::ostream& out);
#endif // EXCEPTIONS_H__