1 |
|
|
// -*- mode: c++ -*- |
2 |
|
|
// Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse, |
3 |
|
|
// JRL, CNRS/AIST. |
4 |
|
|
// |
5 |
|
|
|
6 |
|
|
#ifndef DYNAMIC_GRAPH_EXCEPTION_ABSTRACT_H |
7 |
|
|
#define DYNAMIC_GRAPH_EXCEPTION_ABSTRACT_H |
8 |
|
|
#include <dynamic-graph/dynamic-graph-api.h> |
9 |
|
|
|
10 |
|
|
#include <dynamic-graph/fwd.hh> |
11 |
|
|
#include <string> |
12 |
|
|
|
13 |
|
|
// Uncomment this macros to have lines parameter on the throw display |
14 |
|
|
// #define DYNAMIC-GRAPH_EXCEPTION_PASSING_PARAM |
15 |
|
|
|
16 |
|
|
#define DG_RETHROW \ |
17 |
|
|
(const ::dynamicgraph::ExceptionAbstract &err) { throw err; } |
18 |
|
|
|
19 |
|
|
#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM |
20 |
|
|
#define DG_THROW \ |
21 |
|
|
throw ::dynamicgraph::ExceptionAbstract::Param(__LINE__, __FUNCTION__, \ |
22 |
|
|
__FILE__) + |
23 |
|
|
#else |
24 |
|
|
#define DG_THROW throw |
25 |
|
|
#endif // DYNAMICGRAPH_EXCEPTION_PASSING_PARAM |
26 |
|
|
|
27 |
|
|
namespace dynamicgraph { |
28 |
|
|
/// \ingroup error |
29 |
|
|
/// |
30 |
|
|
/// \brief Abstract root class for all dynamic-graph exceptions. |
31 |
|
|
class DYNAMIC_GRAPH_DLLAPI ExceptionAbstract : public std::exception { |
32 |
|
|
public: |
33 |
|
|
/// \ingroup error |
34 |
|
|
/// |
35 |
|
|
/// \brief Class owned by exceptions to store error locations. |
36 |
|
|
class Param { |
37 |
|
|
public: |
38 |
|
|
static const int BUFFER_SIZE = 80; |
39 |
|
|
|
40 |
|
|
Param(const int &_line, const char *_function, const char *_file); |
41 |
|
1 |
Param() |
42 |
|
1 |
: functionPTR(), |
43 |
|
|
function(), |
44 |
|
|
line(), |
45 |
|
|
filePTR(), |
46 |
|
|
file(), |
47 |
|
|
pointersSet(false), |
48 |
✓✓✓✓
|
161 |
set(false) {} |
49 |
|
|
Param &initCopy(const Param &p); |
50 |
|
|
|
51 |
|
|
const char *functionPTR; |
52 |
|
|
char function[BUFFER_SIZE]; |
53 |
|
|
int line; |
54 |
|
|
const char *filePTR; |
55 |
|
|
char file[BUFFER_SIZE]; |
56 |
|
|
bool pointersSet; |
57 |
|
|
bool set; |
58 |
|
|
}; |
59 |
|
|
|
60 |
|
|
/// \brief Categories error code. |
61 |
|
|
/// |
62 |
|
|
/// Each value matches categories used by a subclass of |
63 |
|
|
/// ExceptionAbstract. |
64 |
|
|
/// |
65 |
|
|
/// This is the programmer responsibility to make sure there is |
66 |
|
|
/// enough room between two categories error code. |
67 |
|
|
enum ExceptionEnum { |
68 |
|
|
ABSTRACT = 0, |
69 |
|
|
SIGNAL = 100, |
70 |
|
|
FACTORY = 200, |
71 |
|
|
TRACES = 300, |
72 |
|
|
TOOLS = 700 |
73 |
|
|
}; |
74 |
|
|
|
75 |
|
|
static const std::string EXCEPTION_NAME; |
76 |
|
|
|
77 |
|
|
explicit ExceptionAbstract(const int &code, const std::string &msg = ""); |
78 |
|
128 |
virtual ~ExceptionAbstract() throw() {} |
79 |
|
|
|
80 |
|
2 |
virtual const std::string &getExceptionName() const { return EXCEPTION_NAME; } |
81 |
|
|
|
82 |
|
|
/// \brief Access to the error code. |
83 |
|
|
int getCode() const; |
84 |
|
|
|
85 |
|
|
/// \brief Reference access to the error message (can be empty). |
86 |
|
|
const std::string &getStringMessage() const; |
87 |
|
|
|
88 |
|
|
/// \brief Access to the pointer on the array of \e char related |
89 |
|
|
/// to the error string. |
90 |
|
|
/// |
91 |
|
|
/// Cannot be \e NULL. |
92 |
|
|
const char *getMessage() const; |
93 |
|
|
|
94 |
|
1 |
virtual const char *what() const throw() { |
95 |
✓✗ |
1 |
return getStringMessage().c_str(); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/// \brief Print the error structure. |
99 |
|
|
DYNAMIC_GRAPH_DLLAPI friend std::ostream &operator<<( |
100 |
|
|
std::ostream &os, const ExceptionAbstract &err); |
101 |
|
|
|
102 |
|
|
protected: |
103 |
|
|
/// \brief Error code. |
104 |
|
|
/// \sa ErrorCodeEnum |
105 |
|
|
int code; |
106 |
|
|
|
107 |
|
|
/// \brief Error message (can be empty). |
108 |
|
|
std::string message; |
109 |
|
|
|
110 |
|
|
#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM |
111 |
|
|
/// \brief Optional mutable attribute to store exception location. |
112 |
|
|
/// |
113 |
|
|
/// Only present if DYNAMICGRAPH_EXCEPTION_PASSING_PARAM |
114 |
|
|
/// preprocessor symbol exists. |
115 |
|
|
mutable Param p; |
116 |
|
|
|
117 |
|
|
template <class Exc> |
118 |
|
|
friend const Exc &operator+(const ExceptionAbstract::Param &p, const Exc &e) { |
119 |
|
|
e.p.initCopy(p); |
120 |
|
|
return e; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
template <class Exc> |
124 |
|
|
friend Exc &operator+(const ExceptionAbstract::Param &p, Exc &e) { |
125 |
|
|
e.p.initCopy(p); |
126 |
|
|
return e; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
#endif // DYNAMICGRAPH_EXCEPTION_PASSING_PARAM |
130 |
|
|
|
131 |
|
|
private: |
132 |
|
|
/// \brief Forbid the empty constructor (private). |
133 |
|
|
ExceptionAbstract(); |
134 |
|
|
}; |
135 |
|
|
} // end of namespace dynamicgraph |
136 |
|
|
|
137 |
|
|
#endif //! DYNAMIC_GRAPH_EXCEPTION_ABSTRACT_H |