Pandora
Pandora source code navigator
Loading...
Searching...
No Matches
StatusCodes.h
Go to the documentation of this file.
1
8#ifndef PANDORA_STATUS_CODES_H
9#define PANDORA_STATUS_CODES_H 1
10
11#include <exception>
12#include <string>
13
14#if defined(__GNUC__) && defined(BACKTRACE)
15 #include <cstdlib>
16 #include <execinfo.h>
17#endif
18
19#define PANDORA_RETURN_RESULT_IF(StatusCode1, Operator, Command) \
20{ \
21 const pandora::StatusCode statusCode(Command); \
22 if (statusCode Operator StatusCode1) \
23 { \
24 std::cout << #Command << " return " << StatusCodeToString(statusCode) << std::endl; \
25 std::cout << " in function: " << __FUNCTION__ << std::endl; \
26 std::cout << " in file: " << __FILE__ << " line#: " << __LINE__ << std::endl; \
27 return statusCode; \
28 } \
29}
30
31#define PANDORA_RETURN_RESULT_IF_AND_IF(StatusCode1, StatusCode2, Operator, Command) \
32{ \
33 const pandora::StatusCode statusCode(Command); \
34 if ((statusCode Operator StatusCode1) && (statusCode Operator StatusCode2)) \
35 { \
36 std::cout << #Command << " return " << StatusCodeToString(statusCode) << std::endl; \
37 std::cout << " in function: " << __FUNCTION__ << std::endl; \
38 std::cout << " in file: " << __FILE__ << " line#: " << __LINE__ << std::endl; \
39 return statusCode; \
40 } \
41}
42
43#define PANDORA_THROW_RESULT_IF(StatusCode1, Operator, Command) \
44{ \
45 const pandora::StatusCode statusCode(Command); \
46 if (statusCode Operator StatusCode1) \
47 { \
48 std::cout << #Command << " throw " << StatusCodeToString(statusCode) << std::endl; \
49 std::cout << " in function: " << __FUNCTION__ << std::endl; \
50 std::cout << " in file: " << __FILE__ << " line#: " << __LINE__ << std::endl; \
51 throw pandora::StatusCodeException(statusCode); \
52 } \
53}
54
55#define PANDORA_THROW_RESULT_IF_AND_IF(StatusCode1, StatusCode2, Operator, Command) \
56{ \
57 const pandora::StatusCode statusCode(Command); \
58 if ((statusCode Operator StatusCode1) && (statusCode Operator StatusCode2)) \
59 { \
60 std::cout << #Command << " throw " << StatusCodeToString(statusCode) << std::endl; \
61 std::cout << " in function: " << __FUNCTION__ << std::endl; \
62 std::cout << " in file: " << __FILE__ << " line#: " << __LINE__ << std::endl; \
63 throw pandora::StatusCodeException(statusCode); \
64 } \
65}
66
67//------------------------------------------------------------------------------------------------------------------------------------------
68
69namespace pandora
70{
71
72#define STATUS_CODE_TABLE(d) \
73 d(STATUS_CODE_SUCCESS, "STATUS_CODE_SUCCESS" ) \
74 d(STATUS_CODE_FAILURE, "STATUS_CODE_FAILURE" ) \
75 d(STATUS_CODE_NOT_FOUND, "STATUS_CODE_NOT_FOUND" ) \
76 d(STATUS_CODE_NOT_INITIALIZED, "STATUS_CODE_NOT_INITIALIZED" ) \
77 d(STATUS_CODE_ALREADY_INITIALIZED, "STATUS_CODE_ALREADY_INITIALIZED" ) \
78 d(STATUS_CODE_ALREADY_PRESENT, "STATUS_CODE_ALREADY_PRESENT" ) \
79 d(STATUS_CODE_OUT_OF_RANGE, "STATUS_CODE_OUT_OF_RANGE" ) \
80 d(STATUS_CODE_NOT_ALLOWED, "STATUS_CODE_NOT_ALLOWED" ) \
81 d(STATUS_CODE_INVALID_PARAMETER, "STATUS_CODE_INVALID_PARAMETER" ) \
82 d(STATUS_CODE_UNCHANGED, "STATUS_CODE_UNCHANGED" )
83
87#define GET_STATUS_CODE_ENUM_ENTRY(a, b) \
88 a,
89
93#define GET_STATUS_CODE_NAME_SWITCH(a, b) \
94 case a : return b;
95
104
110std::string StatusCodeToString(const StatusCode statusCode);
111
112//------------------------------------------------------------------------------------------------------------------------------------------
113//------------------------------------------------------------------------------------------------------------------------------------------
114
119{
120public:
126 StatusCodeException(const StatusCode statusCode);
127
131 ~StatusCodeException() throw();
132
139
145 std::string ToString() const;
146
152 const std::string &GetBackTrace() const;
153
154private:
156 std::string m_backTrace;
157};
158
159//------------------------------------------------------------------------------------------------------------------------------------------
160
162 m_statusCode(statusCode)
163{
164#if defined(__GNUC__) && defined(BACKTRACE)
165 const size_t maxDepth = 100;
166 void *stackAddresses[maxDepth];
167
168 size_t stackDepth = backtrace(stackAddresses, maxDepth);
169 char **stackStrings = backtrace_symbols(stackAddresses, stackDepth);
170
171 m_backTrace = "\nBackTrace\n ";
172
173 for (size_t i = 0; i < stackDepth; ++i)
174 {
175 m_backTrace += stackStrings[i];
176 m_backTrace += "\n ";
177 }
178
179 free(stackStrings); // malloc()ed by backtrace_symbols
180#endif
181}
182
183//------------------------------------------------------------------------------------------------------------------------------------------
184
188
189//------------------------------------------------------------------------------------------------------------------------------------------
190
192{
193 return m_statusCode;
194}
195
196//------------------------------------------------------------------------------------------------------------------------------------------
197
198inline std::string StatusCodeException::ToString() const
199{
201}
202
203//------------------------------------------------------------------------------------------------------------------------------------------
204
205inline const std::string &StatusCodeException::GetBackTrace() const
206{
207 return m_backTrace;
208}
209
210//------------------------------------------------------------------------------------------------------------------------------------------
211//------------------------------------------------------------------------------------------------------------------------------------------
212
213inline std::string StatusCodeToString(const StatusCode statusCode)
214{
215 switch (statusCode)
216 {
218 default : return "UNKNOWN";
219 }
220}
221
222} // namespace pandora
223
224#endif // #ifndef PANDORA_STATUS_CODES_H
#define GET_STATUS_CODE_ENUM_ENTRY(a, b)
The status code enum entry macro.
Definition StatusCodes.h:87
#define GET_STATUS_CODE_NAME_SWITCH(a, b)
The status code name switch statement macro.
Definition StatusCodes.h:93
#define STATUS_CODE_TABLE(d)
Definition StatusCodes.h:72
StatusCodeException class.
std::string m_backTrace
The back trace.
~StatusCodeException()
Constructor.
const StatusCode m_statusCode
The status code.
std::string ToString() const
Get status code as a string.
StatusCodeException(const StatusCode statusCode)
Constructor.
StatusCode GetStatusCode() const
Get status code.
const std::string & GetBackTrace() const
Get back trace at point of exception construction (gcc only)
std::string StatusCodeToString(const StatusCode statusCode)
Get status code as a string.
StatusCode
The StatusCode enum.
@ NUMBER_OF_STATUS_CODES