ADTF  3.16.2
repo/src/libraries/a_utils/include/a_utils/base/error.h
Go to the documentation of this file.
1 
8 #ifndef A_UTILS_ERRORS_H
9 #define A_UTILS_ERRORS_H
10 
12 #define IS_OK(s) ( ( s ).IsOk() )
13 
15 #define IS_FAILED(s) ( ( s ).IsFailed() )
16 
18 #define DETAILED_RESULT(_errcode, ...) \
19  tResult(_errcode, \
20  A_UTILS_NS::cString::Format(__VA_ARGS__).GetPtr(), \
21  __LINE__, \
22  A_UTILS_NS::cFilename(__FILE__).GetName().GetPtr(), \
23  __FUNC__)
27 #define RETURN_NOERROR return ERR_NOERROR
28 
29 #if (__UCOM_ASSERT_WHEN_LOGGING_ERRORS == 1)
31 #define RETURN_ERROR(_code) assert(IS_OK(_code)); return (_code)
33 #define RETURN_ERROR_DESC(_code, ...) assert(IS_OK(_code)); return DETAILED_RESULT(_code, \
34  __VA_ARGS__)
35 #else
37 #define RETURN_ERROR(code) return (code)
39 #define RETURN_ERROR_DESC(_code, ...) return DETAILED_RESULT(_code, __VA_ARGS__)
40 
41 #endif
42 
44 #define RETURN_IF_FAILED(s) { tResult _errcode(s); if ( _errcode.IsFailed() ) { return (_errcode); } }
45 
46 #define RETURN_IF_FAILED_DESC(s, ...) { tResult _errcode(s); if ( _errcode.IsFailed() ) { return DETAILED_RESULT(_errcode.GetErrorCode().value, __VA_ARGS__); } }
47 
49 #define RETURN_IF_POINTER_NULL(_ptr) if ( nullptr == _ptr ) { RETURN_ERROR(ERR_POINTER); }
50 
52 #define RETURN_IF_POINTER_NULL_DESC(_ptr, ...) if ( nullptr == _ptr ) { return DETAILED_RESULT(ERR_POINTER, __VA_ARGS__); }
53 
55 #define LOG_RESULT(code) \
56 do\
57 { \
58  tResult _result = code; \
59  if (IS_OK(_result))\
60  {\
61  LOG_INFO(A_UTILS_NS::to_string(_result));\
62  } \
63  else \
64  {\
65  LOG_ERROR(A_UTILS_NS::to_string(_result));\
66  } \
67 } \
68 while(tFalse)
69 
72 #define RETURN_AND_LOG_ERROR(code) \
73 do\
74 { \
75  tResult _result = code; \
76  LOG_RESULT(_result); \
77  return (_result); \
78 }\
79 while(tFalse)
80 
83 #define RETURN_AND_LOG_ERROR_STR(code, ...) \
84 do\
85 { \
86  LOG_ERROR(__VA_ARGS__); \
87  return (code); \
88 }\
89 while(tFalse)
90 
93 #define RETURN_IF_FAILED_AND_LOG_ERROR_STR(code, ...) \
94 do\
95 { \
96  tResult _outer_result = code; \
97  if (IS_FAILED(_outer_result)) \
98  {\
99  LOG_ERROR(__VA_ARGS__); \
100  LOG_RESULT(_outer_result);\
101  return _outer_result;\
102  } \
103 }\
104 while(tFalse)
105 
107 #ifdef _DEBUG
108 #define ASSERT_IF_POINTER_NULL(_ptr) A_UTILS_ASSERT( nullptr != _ptr );
109 #else
110 #define ASSERT_IF_POINTER_NULL(_ptr)
111 #endif
112 
114 #ifdef WIN32
115  #define __FUNC__ __FUNCTION__
116 #else
117  #define __FUNC__ __func__
118 #endif //WIN32
119 
122 {
125 };//struct tErrorCode
126 
128 struct tErrorString : public tErrorCode
129 {
131  tErrorString() = default;
132 
138  tErrorString(const tErrorCode::error_code_type i_nErrorCode, const tChar* i_strErrorCode) :
139  tErrorCode{ i_nErrorCode }, strValue(i_strErrorCode) {}
140 
145  const tChar* strErrorCode() const { return strValue; }
146 
147 private:
148  const tChar* const strValue;
149 };//struct tErrorString
150 
156 template<tErrorCode::error_code_type ReturnValue>
157 struct tError;
158 
169 #define _MAKE_ERROR(_no, _strcode) /* (e.g. 2, ERR_UNKNOWN --> ERR_UNKNOWN = -2) */ \
170  template< > \
171  struct tError< -_no > \
172  { \
173  explicit operator tErrorCode() const \
174  { \
175  return tErrorCode { -_no }; \
176  } \
177  explicit operator tErrorString() const \
178  { \
179  return tErrorString { tError<-_no>::error_code, tError<-_no>::str_error_code }; \
180  } \
181  static constexpr const tChar* const str_error_code = #_strcode; \
182  static constexpr const tErrorCode::error_code_type error_code = -_no; \
183  }; \
184  static const tError<-_no> _strcode = tError<-_no>()
185 
187 _MAKE_ERROR( 0, ERR_NOERROR );
189 _MAKE_ERROR( 2, ERR_UNKNOWN );
191 _MAKE_ERROR( 3, ERR_UNEXPECTED );
193 _MAKE_ERROR( 4, ERR_POINTER );
195 _MAKE_ERROR( 5, ERR_INVALID_ARG );
197 _MAKE_ERROR( 6, ERR_INVALID_FUNCTION );
199 _MAKE_ERROR( 7, ERR_INVALID_ADDRESS );
201 _MAKE_ERROR( 8, ERR_INVALID_HANDLE );
203 _MAKE_ERROR( 9, ERR_INVALID_FLAGS );
205 _MAKE_ERROR(10, ERR_INVALID_INDEX );
207 _MAKE_ERROR(11, ERR_INVALID_FILE );
209 _MAKE_ERROR(12, ERR_MEMORY );
211 _MAKE_ERROR(13, ERR_TIMEOUT );
213 _MAKE_ERROR(14, ERR_OUT_OF_SYNC );
215 _MAKE_ERROR(15, ERR_RESOURCE_IN_USE );
217 _MAKE_ERROR(16, ERR_NOT_IMPL );
219 _MAKE_ERROR(17, ERR_NO_INTERFACE );
221 _MAKE_ERROR(18, ERR_NO_CLASS );
223 _MAKE_ERROR(19, ERR_NOT_SUPPORTED );
225 _MAKE_ERROR(20, ERR_NOT_FOUND );
227 _MAKE_ERROR(21, ERR_CANCELED );
229 _MAKE_ERROR(22, ERR_RETRY );
231 _MAKE_ERROR(23, ERR_FILE_NOT_FOUND );
233 _MAKE_ERROR(24, ERR_PATH_NOT_FOUND );
235 _MAKE_ERROR(25, ERR_ACCESS_DENIED );
237 _MAKE_ERROR(26, ERR_NOT_READY );
239 _MAKE_ERROR(27, ERR_OPEN_FAILED );
241 _MAKE_ERROR(28, ERR_IO_INCOMPLETE );
243 _MAKE_ERROR(29, ERR_IO_PENDING );
245 _MAKE_ERROR(30, ERR_NOACCESS );
247 _MAKE_ERROR(31, ERR_BAD_DEVICE );
249 _MAKE_ERROR(32, ERR_DEVICE_IO );
251 _MAKE_ERROR(33, ERR_DEVICE_NOT_READY );
253 _MAKE_ERROR(34, ERR_DEVICE_IN_USE );
255 _MAKE_ERROR(35, ERR_NOT_CONNECTED );
257 _MAKE_ERROR(36, ERR_UNKNOWN_FORMAT );
259 _MAKE_ERROR(37, ERR_NOT_INITIALIZED );
261 _MAKE_ERROR(38, ERR_FAILED );
263 _MAKE_ERROR(39, ERR_END_OF_FILE );
265 _MAKE_ERROR(40, ERR_INVALID_STATE );
267 _MAKE_ERROR(41, ERR_EXCEPTION_RAISED );
269 _MAKE_ERROR(42, ERR_INVALID_TYPE );
271 _MAKE_ERROR(43, ERR_EMPTY );
273 _MAKE_ERROR(44, ERR_INVALID_VERSION );
275 _MAKE_ERROR(45, ERR_INVALID_LICENSE );
277 _MAKE_ERROR(46, ERR_SERVICE_NOT_FOUND);
279 _MAKE_ERROR(47, ERR_DAU );
281 _MAKE_ERROR(48, ERR_IDLE_NOWAIT );
283 _MAKE_ERROR(49, ERR_OUT_OF_RANGE );
285 _MAKE_ERROR(50, ERR_KNOWN_PROBLEM );
287 _MAKE_ERROR(51, ERR_INVALID_INTERFACE);
288 
291 _MAKE_ERROR(52, ERR_INTERNAL );
292 
299 inline tBool operator== (const tErrorCode& lhs, const tErrorCode& rhs)
300 {
301  return lhs.value == rhs.value;
302 }
303 
310 inline tBool operator!= (const tErrorCode& lhs, const tErrorCode& rhs)
311 {
312  return !operator==(lhs, rhs);
313 }
314 
321 inline tBool operator< (const tErrorCode& lhs, const tErrorCode& rhs)
322 {
323  return lhs.value < rhs.value;
324 }
325 
332 inline tBool operator> (const tErrorCode& lhs, const tErrorCode& rhs)
333 {
334  return operator<(rhs, lhs);
335 }
336 
343 inline tBool operator<= (const tErrorCode& lhs, const tErrorCode& rhs)
344 {
345  return !operator>(lhs, rhs);
346 }
347 
354 inline tBool operator>= (const tErrorCode& lhs, const tErrorCode& rhs)
355 {
356  return !operator<(lhs, rhs);
357 }
358 
367 template<tErrorCode::error_code_type ErrorCodeLHS, tErrorCode::error_code_type ErrorCodeRHS>
369 {
370  return static_cast<tErrorCode>(lhs) == static_cast<tErrorCode>(rhs);
371 }
372 
381 template<tErrorCode::error_code_type ErrorCodeLHS, tErrorCode::error_code_type ErrorCodeRHS>
383 {
384  return !operator==(lhs, rhs);
385 }
386 
395 template<tErrorCode::error_code_type ErrorCodeLHS, tErrorCode::error_code_type ErrorCodeRHS>
397 {
398  return static_cast<tErrorCode>(lhs) < static_cast<tErrorCode>(rhs);
399 }
400 
409 template<tErrorCode::error_code_type ErrorCodeLHS, tErrorCode::error_code_type ErrorCodeRHS>
411 {
412  return operator<(rhs, lhs);
413 }
414 
423 template<tErrorCode::error_code_type ErrorCodeLHS, tErrorCode::error_code_type ErrorCodeRHS>
425 {
426  return !operator>(lhs, rhs);
427 }
428 
437 template<tErrorCode::error_code_type ErrorCodeLHS, tErrorCode::error_code_type ErrorCodeRHS>
439 {
440  return !operator<(lhs, rhs);
441 }
442 
443 #endif //A_UTILS_ERRORS_H
tBool operator<(const tErrorCode &lhs, const tErrorCode &rhs)
Less-than operator for POD error type.
#define _MAKE_ERROR(_no, _strcode)
Create an error type with its name and numeric representation.
tBool operator<=(const tErrorCode &lhs, const tErrorCode &rhs)
Less-than-or-equal operator for POD error type.
tBool operator>(const tErrorCode &lhs, const tErrorCode &rhs)
Greater-than operator for POD error type.
tBool operator!=(const tErrorCode &lhs, const tErrorCode &rhs)
Compare two POD error code types for inequality.
tBool operator==(const tErrorCode &lhs, const tErrorCode &rhs)
Compare two POD error code types for equality.
tBool operator>=(const tErrorCode &lhs, const tErrorCode &rhs)
Greater-than-or-equal operator for POD error type.
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
int32_t tInt32
type definition for signed integer values (32bit) (platform and compiler independent type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
C++11 POD type to distribute error codes between binary boundaries.
error_code_type value
Value of this error code.
Basic error type template, specialized by _MAKE_ERROR() to create concrete error types.
Wrapper for stringification of error codes - for usage by struct tError.
const tChar * strErrorCode() const
Get the error code as string representation.
const tChar *const strValue
Pointer to existing error code string.
tErrorString()=default
Default constructor.
tErrorString(const tErrorCode::error_code_type i_nErrorCode, const tChar *i_strErrorCode)
Construct with error code and error string.