ADTF  3.15.2
repo/src/libraries/a_utils/include/a_utils/core/result.h
Go to the documentation of this file.
1 
8 #ifndef _A_UTILS_BASE_RESULT_H_
9 #define _A_UTILS_BASE_RESULT_H_
10 
11 namespace A_UTILS_NS
12 {
13 
17 class cResult
18 {
19 public: //construction
29  cResult();
30 
38  cResult(decltype(ERR_NOERROR));
39 
44  template<tErrorCode::error_code_type ErrorCode>
45  cResult(const tError<ErrorCode>& i_oError);
46 
51  cResult(tErrorCode i_oError);
52 
62  template<tErrorCode::error_code_type ErrorCode>
63  explicit cResult(tError<ErrorCode> i_oError,
64  const tChar* i_strErrorDescription,
65  tInt32 i_nLine,
66  const tChar* i_strFile,
67  const tChar* i_strFunction);
68 
73  explicit cResult(tErrorCode::error_code_type i_nErrorCode);
74 
83  explicit cResult(tErrorCode::error_code_type i_nErrorCode,
84  const tChar* i_strErrorDescription,
85  tInt32 i_nLine,
86  const tChar* i_strFile,
87  const tChar* i_strFunction);
88 
97  explicit cResult(const cResult& i_oResult,
98  const tChar* i_strErrorDescription,
99  tInt32 i_nLine,
100  const tChar* i_strFile,
101  const tChar* i_strFunction);
102 
107 
114  cResult(const cResult& i_oOther);
115 
124  cResult& operator=(decltype(ERR_NOERROR));
125 
132  cResult& operator=(cResult i_oOther);
133 
138  cResult(cResult&& i_oOther);
139 
140 public: //access
146 
151  const tChar* GetErrorString() const;
152 
157  const tChar* GetDescription() const;
158 
163  tInt32 GetLine() const;
164 
169  const tChar* GetFile() const;
170 
175  const tChar* GetFunction() const;
176 
183  template<tErrorCode::error_code_type ErrorCode>
184  tBool IsEqual(const tError<ErrorCode>& i_oErrorVal) const;
185 
192  template<typename ... ErrorTypes>
193  tBool IsEqual(const ErrorTypes&... i_oErrorVals) const;
194 
199  tBool IsOk() const;
200 
205  tBool IsFailed() const;
206 
212  friend void swap(cResult& i_oLHS, cResult& i_oRHS);
213 
220  friend tBool operator==(const cResult& i_oLHS, const cResult& i_oRHS);
221 
222 private: //private constructor
232  explicit cResult(tErrorCode i_nErrorCode,
233  const tChar* i_strErrorDescription,
234  tInt32 i_nLine,
235  const tChar* i_strFile,
236  const tChar* i_strFunction,
237  const tChar* i_strErrorCode);
238 
249  const tChar* i_strErrorDescription,
250  tInt32 i_nLine,
251  const tChar* i_strFile,
252  const tChar* i_strFunction);
253 
260  static tErrorString GetErrorString(const tErrorCode& i_oErrorCode);
261 
262 private: //private member data
265 
266 private: //helper types
272  template<typename ErrorType, typename ... ErrorTypes>
273  struct sErrorEval;
274 
275  template<typename ErrorType>
276  struct sErrorEval < ErrorType > ;
277 };//class cResult
278 
285 tBool operator!=(const cResult& i_oLHS, const cResult& i_oRHS);
286 
288 {
289 }
290 
291 inline cResult::cResult(decltype(ERR_NOERROR)) : cResult()
292 {
293 }
294 
295 template<tErrorCode::error_code_type ErrorCode>
296 inline cResult::cResult(const tError<ErrorCode>& i_oError)
297  : cResult(static_cast<tErrorCode>(i_oError))
298 {
299 }
300 
301 inline cResult::cResult(tErrorCode i_oError)
302  : m_oResultHandler(description_type::make_result_description(i_oError))
303 {
304 }
305 
306 template<tErrorCode::error_code_type ErrorCode>
308  const tChar* i_strErrorDescription,
309  tInt32 i_nLine,
310  const tChar* i_strFile,
311  const tChar* i_strFunction)
312  : cResult(static_cast<tErrorCode>(i_oError),
313  i_strErrorDescription,
314  i_nLine,
315  i_strFile,
316  i_strFunction,
317  tError<ErrorCode>::str_error_code)
318 {
319 }
320 
322  : cResult(tErrorCode{ i_nErrorCode })
323 {
324 }
325 
327  const tChar* i_strErrorDescription,
328  tInt32 i_nLine,
329  const tChar* i_strFile,
330  const tChar* i_strFunction) :
331  cResult(CreateFrom(i_nErrorCode, i_strErrorDescription, i_nLine, i_strFile, i_strFunction))
332 {
333 }
334 
335 inline cResult::cResult(const cResult& i_oResult,
336  const tChar* i_strErrorDescription,
337  tInt32 i_nLine,
338  const tChar* i_strFile,
339  const tChar* i_strFunction) :
340  cResult(i_oResult.GetErrorCode(),
341  i_strErrorDescription,
342  i_nLine,
343  i_strFile,
344  i_strFunction,
345  i_oResult.GetErrorString())
346 {
347 }
348 
350 {
351  swap(*this, i_oOther);
352  return *this;
353 }
354 
355 inline cResult& cResult::operator=(decltype(ERR_NOERROR))
356 {
357  return cResult::operator=(cResult());
358 }
359 
360 inline cResult::cResult(cResult&& i_oOther) : cResult()
361 {
362  swap(*this, i_oOther);
363  if (i_oOther.IsFailed())
364  {
365  i_oOther = cResult();
366  }
367 }
368 
369 inline tBool operator==(const cResult& i_oLHS, const cResult& i_oRHS)
370 {
371  return i_oLHS.m_oResultHandler == i_oRHS.m_oResultHandler;
372 }
373 
374 inline tBool operator!=(const cResult& i_oLHS, const cResult& i_oRHS)
375 {
376  return !(i_oLHS == i_oRHS);
377 }
378 
379 inline tBool cResult::IsOk() const
380 {
381  return tErrorCode{} == GetErrorCode();
382 }
383 
384 inline tBool cResult::IsFailed() const
385 {
386  return !IsOk();
387 }
388 
389 template<tErrorCode::error_code_type ErrorCode>
390 inline tBool cResult::IsEqual(const tError<ErrorCode>& i_oErrorVal) const
391 {
392  return GetErrorCode() == static_cast<tErrorCode>(i_oErrorVal);
393 }
394 
395 template<typename ... ErrorTypes>
396 inline tBool cResult::IsEqual(const ErrorTypes&... i_oErrorVals) const
397 {
398  return sErrorEval<ErrorTypes...>::dispatch(GetErrorCode(), i_oErrorVals...);
399 }
400 
408 template<tErrorCode::error_code_type ErrorCode>
409 inline tBool operator==(const cResult& oResult, const tError<ErrorCode>& sCode)
410 {
411  return oResult.IsEqual(sCode);
412 }
413 
421 template<tErrorCode::error_code_type ErrorCode>
422 inline tBool operator!=(const cResult& oResult, const tError<ErrorCode>& sCode)
423 {
424  return !oResult.IsEqual(sCode);
425 }
426 
434 template<tErrorCode::error_code_type ErrorCode>
435 inline tBool operator==(const tError<ErrorCode>& sCode, const cResult& oResult)
436 {
437  return oResult.IsEqual(sCode);
438 }
439 
447 template<tErrorCode::error_code_type ErrorCode>
448 inline tBool operator!=(const tError<ErrorCode>& sCode, const cResult& oResult)
449 {
450  return !oResult.IsEqual(sCode);
451 }
452 
453 inline void swap(cResult& i_oLHS, cResult& i_oRHS)
454 { //enable ADL
455  using std::swap;
456  swap(i_oLHS.m_oResultHandler, i_oRHS.m_oResultHandler);
457 }
458 
459 template<typename ErrorType, typename ... ErrorTypes>
461 {
469  static tBool dispatch(tErrorCode i_oErrorCode,
470  const ErrorType& i_oErrorVal,
471  const ErrorTypes&... i_oErrorVals)
472  {
473  if (static_cast<tErrorCode>(i_oErrorVal) == i_oErrorCode)
474  {
475  return tTrue;
476  }
477  else
478  {
479  return sErrorEval<ErrorTypes...>::dispatch(i_oErrorCode, i_oErrorVals...);
480  }
481  }
482 };//template<typename ErrorType, typename ... ErrorTypes> struct sErrorEval
483 
488 template<typename ErrorType>
489 struct cResult::sErrorEval<ErrorType>
490 {
497  static tBool dispatch(tErrorCode i_oErrorCode, const ErrorType& i_oErrorVal)
498  {
499  return static_cast<tErrorCode>(i_oErrorVal) == i_oErrorCode;
500  }
501 };//template<typename ErrorType> struct sErrorEval<ErrorType>
502 
503 }//ns A_UTILS_NS
504 
505 
508 
509 namespace std
510 {
516  template<>
517  inline void swap<A_UTILS_NS::cResult>(A_UTILS_NS::cResult& i_oLHS, A_UTILS_NS::cResult& i_oRHS)
518  {
519  A_UTILS_NS::swap(i_oLHS, i_oRHS);
520  }
521 }
522 
523 #endif // _A_UTILS_BASE_RESULT_H_
A common result class usable as return value throughout.
~cResult()
Destructor.
const tChar * GetDescription() const
Get user provided error description.
tBool IsOk() const
Check whether this result object contains ERR_NOERROR.
cResult(tErrorCode i_nErrorCode, const tChar *i_strErrorDescription, tInt32 i_nLine, const tChar *i_strFile, const tChar *i_strFunction, const tChar *i_strErrorCode)
Construct result object with complete set of error information.
cResult()
Construct empty which basically means no error occurred.
static tErrorString GetErrorString(const tErrorCode &i_oErrorCode)
Query the string representation of an error code.
const tChar * GetErrorString() const
Get error code as string representation.
friend tBool operator==(const cResult &i_oLHS, const cResult &i_oRHS)
Comparison operator (shallow comparison)
tInt32 GetLine() const
Get line in source file where the error was reported.
static cResult CreateFrom(tErrorCode::error_code_type i_nErrorCode, const tChar *i_strErrorDescription, tInt32 i_nLine, const tChar *i_strFile, const tChar *i_strFunction)
Create a detailed object of type cResult on the heap from an error code.
detail::result_description< detail::IErrorDescription > description_type
Abbreviation typedef.
tErrorCode GetErrorCode() const
Get error code.
const tChar * GetFunction() const
Get name of the function the error was reported in.
tBool IsFailed() const
Check whether this result object contains an error != ERR_NOERROR.
cResult & operator=(decltype(ERR_NOERROR))
Assign with ERR_NOERROR.
description_type m_oResultHandler
The result handler wrapper.
const tChar * GetFile() const
Get name of the file the error was reported in.
cResult(const cResult &i_oOther)
Copy constructor.
tBool IsEqual(const tError< ErrorCode > &i_oErrorVal) const
Check whether an error code is equal to the contained one.
friend void swap(cResult &i_oLHS, cResult &i_oRHS)
Specialized friend swap function to enable ADL in std::swap later.
#define tTrue
Value for tBool.
Definition: constants.h:62
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
tBool operator==(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.
tBool operator!=(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.
void swap(cResult &i_oLHS, cResult &i_oRHS)
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).
static tBool dispatch(tErrorCode i_oErrorCode, const ErrorType &i_oErrorVal)
Exit point for compile time dispatching of error codes.
Used as entry point for comparison on the error codes used in IsEqual()
static tBool dispatch(tErrorCode i_oErrorCode, const ErrorType &i_oErrorVal, const ErrorTypes &... i_oErrorVals)
Compile time dispatching of error codes.
C++11 POD type to distribute error codes between binary boundaries.
Basic error type template, specialized by _MAKE_ERROR() to create concrete error types.
Wrapper for stringification of error codes - for usage by struct tError.