8 #ifndef _A_UTILS_BASE_RESULT_H_
9 #define _A_UTILS_BASE_RESULT_H_
12 #include <a_utils/a_utils_version.h>
26 #include <string_view>
30 namespace A_UTILS_NS::detail
32 static inline std::string filename(std::string_view strPath)
34 auto nLastSlash = strPath.rfind(
'/');
35 if (nLastSlash == std::string::npos)
37 nLastSlash = strPath.rfind(
'\\');
39 if (nLastSlash == std::string_view::npos)
41 return std::string(strPath);
43 return std::string(strPath.substr(nLastSlash + 1));
46 static inline std::string filename_with_thread_id(std::string_view strPath)
48 using namespace std::literals;
49 std::ostringstream strBuffer;
50 strBuffer <<
"tid="sv << std::this_thread::get_id() <<
" "sv << filename(strPath);
51 return strBuffer.str();
54 static inline std::string
56 __attribute__((format(printf, 1, 2)))
60 _In_z_ _Printf_format_string_
62 const char* strFormat,
65 va_list args, args_copy;
66 va_start(args, strFormat);
67 va_copy(args_copy, args);
68 std::string strBuffer;
69 strBuffer.resize(
static_cast<size_t>(::std::vsnprintf(
nullptr, 0, strFormat, args_copy)));
71 ::std::vsnprintf(strBuffer.data(), strBuffer.size() + 1, strFormat, args);
78 #define IS_OK(s) ((s).IsOk())
81 #define IS_FAILED(s) ((s).IsFailed())
84 #define DETAILED_RESULT(_errcode, ...) \
85 tResult(_errcode, A_UTILS_NS::detail::format(__VA_ARGS__).c_str(), __LINE__, \
86 A_UTILS_NS::detail::filename_with_thread_id(__FILE__).c_str(), __FUNC__)
90 #define RETURN_NOERROR return ERR_NOERROR
92 #if (__UCOM_ASSERT_WHEN_LOGGING_ERRORS == 1)
94 #define RETURN_ERROR(_code) \
95 assert(IS_OK(_code)); \
98 #define RETURN_ERROR_DESC(_code, ...) \
99 assert(IS_OK(_code)); \
100 return DETAILED_RESULT(_code, __VA_ARGS__)
103 #define RETURN_ERROR(code) return (code)
105 #define RETURN_ERROR_DESC(_code, ...) return DETAILED_RESULT(_code, __VA_ARGS__)
110 #define RETURN_IF_FAILED(s) \
112 tResult _errcode(s); \
113 if (_errcode.IsFailed()) \
119 #define RETURN_IF_FAILED_DESC(s, ...) \
121 tResult _errcode(s); \
122 if (_errcode.IsFailed()) \
124 return DETAILED_RESULT(_errcode.GetErrorCode().value, __VA_ARGS__); \
129 #define RETURN_IF_POINTER_NULL(_ptr) \
130 if (nullptr == _ptr) \
132 RETURN_ERROR(ERR_POINTER); \
137 #define RETURN_IF_POINTER_NULL_DESC(_ptr, ...) \
138 if (nullptr == _ptr) \
140 return DETAILED_RESULT(ERR_POINTER, __VA_ARGS__); \
170 constexpr
cResult(decltype(ERR_NOERROR)) noexcept;
176 template<tErrorCode::error_code_type ErrorCode>
194 template<tErrorCode::error_code_type ErrorCode>
196 const tChar* i_strErrorDescription,
198 const tChar* i_strFile,
199 const tChar* i_strFunction) noexcept;
216 const tChar* i_strErrorDescription,
218 const tChar* i_strFile,
219 const tChar* i_strFunction) noexcept;
230 const tChar* i_strErrorDescription,
232 const tChar* i_strFile,
233 const tChar* i_strFunction) noexcept;
256 cResult& operator=(decltype(ERR_NOERROR)) noexcept;
283 const
tChar* GetErrorString() const noexcept;
289 const
tChar* GetDescription() const noexcept;
301 const
tChar* GetFile() const noexcept;
307 const
tChar* GetFunction() const noexcept;
315 template<
tErrorCode::error_code_type ErrorCode>
316 tBool IsEqual(const
tError<ErrorCode>& i_oErrorVal) const noexcept;
324 template<typename... ErrorTypes>
325 tBool IsEqual(const ErrorTypes&... i_oErrorVals) const noexcept;
331 tBool IsOk() const noexcept;
337 tBool IsFailed() const noexcept;
365 const
tChar* i_strErrorDescription,
367 const
tChar* i_strFile,
368 const
tChar* i_strFunction,
369 const
tChar* i_strErrorCode) noexcept;
381 const
tChar* i_strErrorDescription,
383 const
tChar* i_strFile,
384 const
tChar* i_strFunction) noexcept;
404 template<typename ErrorType, typename... ErrorTypes>
407 template<typename ErrorType>
427 template<tErrorCode::error_code_type ErrorCode>
434 m_oResultHandler(description_type::make_result_description(i_oError))
438 template<tErrorCode::error_code_type ErrorCode>
440 const tChar* i_strErrorDescription,
442 const tChar* i_strFile,
443 const tChar* i_strFunction) noexcept:
445 i_strErrorDescription,
458 const tChar* i_strErrorDescription,
460 const tChar* i_strFile,
461 const tChar* i_strFunction) noexcept:
462 cResult(CreateFrom(i_nErrorCode, i_strErrorDescription, i_nLine, i_strFile, i_strFunction))
467 const tChar* i_strErrorDescription,
469 const tChar* i_strFile,
470 const tChar* i_strFunction) noexcept:
478 swap(*
this, i_oOther);
493 return i_oLHS.m_oResultHandler == i_oRHS.m_oResultHandler;
498 return !(i_oLHS == i_oRHS);
511 template<tErrorCode::error_code_type ErrorCode>
514 return GetErrorCode() ==
static_cast<tErrorCode>(i_oErrorVal);
517 template<
typename... ErrorTypes>
530 template<tErrorCode::error_code_type ErrorCode>
533 return oResult.IsEqual(sCode);
543 template<tErrorCode::error_code_type ErrorCode>
546 return !oResult.IsEqual(sCode);
556 template<tErrorCode::error_code_type ErrorCode>
559 return oResult.IsEqual(sCode);
569 template<tErrorCode::error_code_type ErrorCode>
572 return !oResult.IsEqual(sCode);
578 swap(i_oLHS.m_oResultHandler, i_oRHS.m_oResultHandler);
581 template<
typename ErrorType,
typename... ErrorTypes>
591 static constexpr
bool
592 dispatch(
tErrorCode i_oErrorCode,
const ErrorType& i_oErrorVal,
const ErrorTypes&... i_oErrorVals) noexcept
594 if (
static_cast<tErrorCode>(i_oErrorVal) == i_oErrorCode)
609 template<
typename ErrorType>
620 return static_cast<tErrorCode>(i_oErrorVal) == i_oErrorCode;
Copyright © Audi Electronics Venture GmbH.
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).
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
A common result class usable as return value throughout.
~cResult() noexcept
Destructor.
tErrorCode GetErrorCode() const noexcept
Get error code.
const tChar * GetErrorString() const noexcept
Get error code as string representation.
cResult & operator=(decltype(ERR_NOERROR)) noexcept
Assign with ERR_NOERROR.
constexpr cResult() noexcept
Construct empty which basically means no error occurred.
tBool IsFailed() const noexcept
Check whether this result object contains an error != ERR_NOERROR.
tBool IsOk() const noexcept
Check whether this result object contains ERR_NOERROR.
tBool IsEqual(const tError< ErrorCode > &i_oErrorVal) const noexcept
Check whether an error code is equal to the contained one.
Copyright © Audi Electronics Venture GmbH.
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
tBool operator==(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.
cString to_string(const tResult &i_oResult, eResultFormatFlags i_eFormatFlags=eResultFormatFlags::RFF_DisableNone, const tChar *i_strFormat=nullptr)
Copy all information of an assigned result object to a (formatted) string.
tBool operator!=(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.
void swap(cResult &i_oLHS, cResult &i_oRHS) noexcept
Copyright © Audi Electronics Venture GmbH.
static constexpr bool dispatch(tErrorCode i_oErrorCode, const ErrorType &i_oErrorVal) noexcept
Exit point for compile time dispatching of error codes.
Used as entry point for comparison on the error codes used in IsEqual()
static constexpr bool dispatch(tErrorCode i_oErrorCode, const ErrorType &i_oErrorVal, const ErrorTypes &... i_oErrorVals) noexcept
Compile time dispatching of error codes.
C++11 POD type to distribute error codes between binary boundaries.
tInt32 error_code_type
Type of the 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.