#include <resultbase.h>
The Result class wraps a result value and may also hold an error value. It has to be used for functions or methods which have to indicate that an error happened during their execution so that no normal result value could be computed:
Functions which don't return a value on success use Result<void>
as result type and return OK;
as return statement for the successful case. Those functions can also use a ResultMem value in the return statement, this will set an OutOfMemoryError if the ResultMem is false:
The result value and error can be obtained via GetValue() and GetError(), respectively. To check if there was an error, you can simply write result == OK
or result == FAILED
. There are also the macros iferr and iferr_return which help to write error handling code.
In the following example the Result is a member of a class. The Result will keep the pointer to the Error object, but it does not keep it alive.
The alternative is to store the error object. The Error object is reference counted and your object of Foo will keep a reference to it, until the object itself is freed. As long as no error is set (GetResult() == OK) will return true.
RESULT_TYPE | Type of result value. |
Public Types | |
using | Super = typename ResultSuper< RESULT_TYPE, void >::type |
![]() | |
using | ValueType = T |
Public Member Functions | |
Result () | |
MAXON_IMPLICIT | Result (ErrorPtr &error) |
MAXON_IMPLICIT | Result (ErrorPtr &&error) |
MAXON_IMPLICIT | Result (const ErrorPtr &error) |
MAXON_IMPLICIT | Result (const ThreadReferencedError &error) |
MAXON_IMPLICIT | Result (typename std::conditional< STD_IS_REPLACEMENT(void, RESULT_TYPE), ResultMem, const volatile DummyParamType & >::type value) |
template<typename T2 , typename = typename Super::template ConstructibleFrom<typename std::add_const<T2>::type>::type> | |
MAXON_IMPLICIT | Result (const ResultOk< T2 > &value) |
template<typename T2 , typename = typename Super::template ConstructibleFrom<T2>::type> | |
MAXON_IMPLICIT | Result (ResultOk< T2 > &&value) |
MAXON_IMPLICIT | Result (typename std::conditional< STD_IS_REPLACEMENT(void, RESULT_TYPE), ResultOk< RESULT_TYPE >, DummyParamType && >::type value) |
template<typename T2 > | |
MAXON_IMPLICIT | Result (const ResultMemT< T2 > &value, typename std::enable_if< STD_IS_REPLACEMENT(void, typename SFINAEHelper< RESULT_TYPE, T2 >::type), DummyParamType * >::type=(DummyParamType *) nullptr) |
template<typename T2 , typename = typename Super::template ConstructibleFrom<typename std::add_const<T2>::type>::type> | |
MAXON_IMPLICIT | Result (const ResultMemT< T2 > &value) |
template<typename T2 , typename = typename Super::template ConstructibleFrom<T2>::type> | |
MAXON_IMPLICIT | Result (ResultMemT< T2 > &&value) |
template<typename T2 > | |
MAXON_IMPLICIT | Result (const ResultRef< T2 > &value, typename std::enable_if< STD_IS_REPLACEMENT(void, typename SFINAEHelper< RESULT_TYPE, T2 >::type), DummyParamType * >::type=(DummyParamType *) nullptr) |
template<typename T2 , typename = typename Super::template ConstructibleFrom<T2&>::type> | |
MAXON_IMPLICIT | Result (const ResultRef< T2 > &value) |
MAXON_IMPLICIT | Result (typename std::conditional< STD_IS_REPLACEMENT(void, RESULT_TYPE), ERROR_OK, volatile DummyParamType & >::type) |
MAXON_IMPLICIT | Result (typename std::conditional< T_IS_ERROR, const DummyParamType &, CopyType >::type value) |
MAXON_IMPLICIT | Result (typename std::conditional< T_IS_ERROR, DummyParamType &, MoveType >::type value) |
MAXON_IMPLICIT | Result (typename std::conditional< T_IS_ERROR, const DummyParamType &&, DeleteType >::type value)=delete |
template<typename T2 , typename = typename std::enable_if<!maxon::HasBaseDetector<typename std::decay<T2>::type::ReferencedType, ErrorInterface>::value, RESULT_TYPE>::type::ReferencedType, typename = typename Super::template ConstructibleFrom<T2>::type> | |
MAXON_IMPLICIT | Result (T2 &&value) |
template<typename T2 > | |
Result (const Result< T2 > &src, typename std::enable_if< STD_IS_REPLACEMENT(void, typename SFINAEHelper< RESULT_TYPE, T2 >::type), DummyParamType * >::type=(DummyParamType *) nullptr) | |
template<typename T2 , typename = typename Super::template ConstructibleFrom<typename std::add_const<T2>::type>::type> | |
MAXON_IMPLICIT | Result (const Result< T2 > &src) |
template<typename T2 , typename = typename Super::template ConstructibleFrom<T2>::type> | |
MAXON_IMPLICIT | Result (Result< T2 > &&src) |
template<typename E > | |
MAXON_IMPLICIT | Result (E &&error, typename std::enable_if<!T_IS_ERROR, typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), DummyParamType * >::type >::type=(DummyParamType *) nullptr) |
template<typename T2 > | |
Result (CopyType value, const Result< T2 > &result) | |
template<typename T2 > | |
Result (MoveType value, const Result< T2 > &result) | |
template<typename T2 > | |
Result (DeleteType value, const Result< T2 > &result)=delete | |
template<typename E > | |
Result (CopyType value, E &&error, typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), DummyParamType * >::type=(DummyParamType *) nullptr) | |
template<typename E > | |
Result (MoveType value, E &&error, typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), DummyParamType * >::type=(DummyParamType *) nullptr) | |
template<typename E > | |
Result (DeleteType value, E &&error, typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), DummyParamType * >::type=(DummyParamType *) nullptr)=delete | |
Result (typename std::conditional< STD_IS_REPLACEMENT(same, typename std::decay< RESULT_TYPE >::type, Bool), volatile DummyParamType &&, Bool >::type)=delete | |
SFINAEHelper< Error, RESULT_TYPE >::type | GetError () const |
MAXON_ATTRIBUTE_FORCE_INLINE const ErrorInterface * | GetErrorPointer () const |
template<typename E > | |
std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), E >::type | GetError () const |
template<typename E > | |
std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), Bool >::type | HasError () const |
Bool | operator== (ERROR_OK ok) const |
Bool | operator== (ERROR_FAILED failed) const |
void | operator= (typename std::conditional< STD_IS_REPLACEMENT(reference, RESULT_TYPE), RESULT_TYPE, const DummyParamType & >::type)=delete |
![]() | |
ResultBase () | |
ResultBase (CopyType value) | |
ResultBase (MoveType value) | |
MAXON_ATTRIBUTE_FORCE_INLINE const T & | GetValue () const & |
MAXON_ATTRIBUTE_FORCE_INLINE const T & | UncheckedGetValue () const & |
MAXON_MEMBER_ACCESSORS (T, GetValue) | |
MAXON_MEMBER_ACCESSORS (T, UncheckedGetValue) | |
MAXON_ATTRIBUTE_FORCE_INLINE const T & | GetValueAndError (ThreadReferencedError &error) const & |
MAXON_ATTRIBUTE_FORCE_INLINE T | GetValueAndError (ThreadReferencedError &error) && |
MAXON_ATTRIBUTE_FORCE_INLINE const T & | GetValueAndError (Error &error) const & |
MAXON_ATTRIBUTE_FORCE_INLINE T | GetValueAndError (Error &error) && |
MAXON_ATTRIBUTE_FORCE_INLINE const ThreadReferencedError & | GetErrorStorage () const |
MAXON_ATTRIBUTE_FORCE_INLINE ThreadReferencedError & | GetErrorStorage () |
Static Public Attributes | |
static const Bool | T_IS_ERROR |
![]() | |
static constexpr Bool | HAS_ERROR |
Private Member Functions | |
void | InitError (const Error &error) |
void | InitError (Error &&error) |
Additional Inherited Members | |
![]() | |
using | CopyType = typename ByValueParam< T >::type |
using | MoveType = typename std::conditional< STD_IS_REPLACEMENT(reference, CopyType), T &&, DummyParamType & >::type |
using | DeleteType = const DummyParamType && |
template<typename T2 > | |
using | ConstructibleFrom = std::enable_if< STD_IS_REPLACEMENT(constructible, T, T2)> |
![]() | |
ThreadReferencedError | _error |
T | _value |
using Super = typename ResultSuper<RESULT_TYPE, void>::type |
MAXON_IMPLICIT Result | ( | ErrorPtr & | error | ) |
MAXON_IMPLICIT Result | ( | ErrorPtr && | error | ) |
MAXON_IMPLICIT Result | ( | const ErrorPtr & | error | ) |
MAXON_IMPLICIT Result | ( | const ThreadReferencedError & | error | ) |
MAXON_IMPLICIT Result | ( | typename std::conditional< STD_IS_REPLACEMENT(void, RESULT_TYPE), ResultMem, const volatile DummyParamType & >::type | value | ) |
Constructs a Result<void>
object, indicating an OutOfMemoryError if value
is false
. This constructor is only available for Result<void>
and not for a general Result<T>
.
[in] | value | If false, set the error object to an OutOfMemoryError. |
MAXON_IMPLICIT Result | ( | const ResultOk< T2 > & | value | ) |
MAXON_IMPLICIT Result | ( | ResultOk< T2 > && | value | ) |
MAXON_IMPLICIT Result | ( | typename std::conditional< STD_IS_REPLACEMENT(void, RESULT_TYPE), ResultOk< RESULT_TYPE >, DummyParamType && >::type | value | ) |
MAXON_IMPLICIT Result | ( | const ResultMemT< T2 > & | value, |
typename std::enable_if< STD_IS_REPLACEMENT(void, typename SFINAEHelper< RESULT_TYPE, T2 >::type), DummyParamType * >::type | = (DummyParamType*) nullptr |
||
) |
Constructs a Result<void>
object, indicating an OutOfMemoryError if value
is nullptr
. This constructor is only available for Result<void>
and not for a general Result<T>
.
[in] | value | If nullptr, set the error object to an OutOfMemoryError. |
MAXON_IMPLICIT Result | ( | const ResultMemT< T2 > & | value | ) |
Constructs a Result
object with a pointer value from a ResultPtr.
[in] | value | The ResultPtr value to use. |
MAXON_IMPLICIT Result | ( | ResultMemT< T2 > && | value | ) |
Constructs a Result
object with a pointer value from a ResultPtr.
[in] | value | The ResultPtr value to use. |
MAXON_IMPLICIT Result | ( | const ResultRef< T2 > & | value, |
typename std::enable_if< STD_IS_REPLACEMENT(void, typename SFINAEHelper< RESULT_TYPE, T2 >::type), DummyParamType * >::type | = (DummyParamType*) nullptr |
||
) |
Constructs a Result<void>
object, indicating an OutOfMemoryError if value
is nullptr
. This constructor is only available for Result<void>
and not for a general Result<T>
.
[in] | value | If nullptr, set the error object to an OutOfMemoryError. |
MAXON_IMPLICIT Result | ( | const ResultRef< T2 > & | value | ) |
MAXON_IMPLICIT Result | ( | typename std::conditional< STD_IS_REPLACEMENT(void, RESULT_TYPE), ERROR_OK, volatile DummyParamType & >::type | ) |
Constructs a Result<void>
object without error. This indicates a successful operation, it is typically used implicitly by a return OK;
statement. This constructor is only available for Result<void>
and not for a general Result<T>
.
MAXON_IMPLICIT Result | ( | typename std::conditional< T_IS_ERROR, const DummyParamType &, CopyType >::type | value | ) |
Constructs a Result object with the given result value and without error.
[in] | value | The result value. |
MAXON_IMPLICIT Result | ( | typename std::conditional< T_IS_ERROR, DummyParamType &, MoveType >::type | value | ) |
Constructs a Result object with the given result value and without error.
[in] | value | The result value. |
|
delete |
MAXON_IMPLICIT Result | ( | T2 && | value | ) |
Constructs a Result object with the given result value and without error. This constructor is only available for reference classes.
[in] | value | The result value. |
Result | ( | const Result< T2 > & | src, |
typename std::enable_if< STD_IS_REPLACEMENT(void, typename SFINAEHelper< RESULT_TYPE, T2 >::type), DummyParamType * >::type | = (DummyParamType*) nullptr |
||
) |
Constructs a Result<void>
object from an existing Result<T2>
object src
by using the error of src
, if any. This constructor is only available for Result<void>
and not for a general Result<T>
.
MAXON_IMPLICIT Result | ( | const Result< T2 > & | src | ) |
MAXON_IMPLICIT Result | ( | Result< T2 > && | src | ) |
MAXON_IMPLICIT Result | ( | E && | error, |
typename std::enable_if<!T_IS_ERROR, typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), DummyParamType * >::type | , | ||
::type | = (DummyParamType*) nullptr |
||
) |
|
delete |
Result | ( | CopyType | value, |
E && | error, | ||
typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), DummyParamType * >::type | = (DummyParamType*) nullptr |
||
) |
Result | ( | MoveType | value, |
E && | error, | ||
typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBaseDetector::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), DummyParamType * >::type | = (DummyParamType*) nullptr |
||
) |
|
delete |
|
explicitdelete |
SFINAEHelper<Error, RESULT_TYPE>::type GetError | ( | ) | const |
Returns the error object. If there is no error, this will be a null reference.
MAXON_ATTRIBUTE_FORCE_INLINE const ErrorInterface* GetErrorPointer | ( | ) | const |
Returns the error object pointer. If there is no error, this will be nullptr.
std::enable_if<std::remove_reference<E>::type::DirectlyReferencedType::HasBaseDetector::template Check<ErrorInterface>::value && MAXON_IS_COW_KIND(std::remove_reference<E>::type::Handler::KIND), E >::type GetError | ( | ) | const |
Returns the error object if it is of type E. If there is no error or the error is not an instance of E, a null reference is returned. This function can be used for handling of specific errors as in the following example:
If you don't need the E reference, you can use the HasError() function.
E | The error reference class to check for. |
std::enable_if<std::remove_reference<E>::type::DirectlyReferencedType::HasBaseDetector::template Check<ErrorInterface>::value && MAXON_IS_COW_KIND(std::remove_reference<E>::type::Handler::KIND), Bool >::type HasError | ( | ) | const |
Checks if the error object is of type E.
E | The error reference class to check for. |
Checks if this @CLASS indicates a successful operation. Use this operator as in result == OK
. To check for a failed operation, you have to use result == FAILED
(!=
is not supported).
[in] | ok | Use the global constant OK. |
Bool operator== | ( | ERROR_FAILED | failed | ) | const |
Checks if this @CLASS indicates a failed operation. Use this operator as in result == FAILED
. To check for a successful operation, you have to use result == OK
(!=
is not supported).
[in] | failed | Use the global constant FAILED. |
|
delete |
|
private |
|
private |
|
static |