Result< RESULT_TYPE > Class Template Reference

#include <resultbase.h>

Inheritance diagram for Result< RESULT_TYPE >:

Detailed Description

template<typename RESULT_TYPE>
class maxon::Result< RESULT_TYPE >

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:

Result<Int> Func()
{
if (everything ok)
return 123;
else
return IllegalStateError(MAXON_SOURCE_LOCATION, "Illegal state in Func."_s);
}
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67

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:

Result<void> Func(MyObj*& ptr)
{
if (some condition)
return OK;
ptr = NewObj(MyObj);
return ResultMem(ptr != nullptr);
}
return OK
Definition: apibase.h:2735
ResultMemT< Bool > ResultMem
Definition: apibaseid.h:19
#define NewObj(T,...)
Definition: newobj.h:108
char ** ptr
Definition: pycore_dtoa.h:13

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.

Warning
This class is not thread-safe. The Result object just contains a pointer to an Error object and does not increment the reference counter. Instead of storing the result somewhere, use the direct Error class.

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.

class Foo
{
private:
Result<void> _result; // BAD!
public:
Result<void> GetResult()
{
return _result;
}
}

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.

class Foo
{
private:
Error _err; // FINE!
public:
Result<void> GetResult()
{
return _err;
}
}
Template Parameters
RESULT_TYPEType of result value.
See also
$ref error_result

Public Types

using Super = typename ResultSuper< RESULT_TYPE, void >::type
 
- Public Types inherited from ResultBase< T >
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 ErrorInterfaceGetErrorPointer () 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
 
- Public Member Functions inherited from ResultBase< T >
 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_INLINEGetValueAndError (ThreadReferencedError &error) &&
 
MAXON_ATTRIBUTE_FORCE_INLINE const T & GetValueAndError (Error &error) const &
 
MAXON_ATTRIBUTE_FORCE_INLINEGetValueAndError (Error &error) &&
 
MAXON_ATTRIBUTE_FORCE_INLINE const ThreadReferencedErrorGetErrorStorage () const
 
MAXON_ATTRIBUTE_FORCE_INLINE ThreadReferencedErrorGetErrorStorage ()
 

Static Public Attributes

static const Bool T_IS_ERROR
 
- Static Public Attributes inherited from ResultBase< T >
static constexpr Bool HAS_ERROR
 

Private Member Functions

void InitError (const Error &error)
 
void InitError (Error &&error)
 

Additional Inherited Members

- Protected Types inherited from ResultBase< T >
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)>
 
- Protected Attributes inherited from ResultBase< T >
ThreadReferencedError _error
 
_value
 

Member Typedef Documentation

◆ Super

using Super = typename ResultSuper<RESULT_TYPE, void>::type

Constructor & Destructor Documentation

◆ Result() [1/30]

Result ( )

Constructs a Result object with a default value and no error.

◆ Result() [2/30]

Constructs a Result object with the given error. The result value will be initialized as a default value. This constructor is needed for return err; statements within an iferr block. The extra constructors are required as workaround for MSVC.

Parameters
[in]errorError object to use for the Result.

◆ Result() [3/30]

◆ Result() [4/30]

MAXON_IMPLICIT Result ( const ErrorPtr error)

◆ Result() [5/30]

◆ Result() [6/30]

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>.

Parameters
[in]valueIf false, set the error object to an OutOfMemoryError.

◆ Result() [7/30]

MAXON_IMPLICIT Result ( const ResultOk< T2 > &  value)

Constructs a Result object without error from a ResultOk object.

Parameters
[in]valueThe ResultOk from which the value is taken.

◆ Result() [8/30]

MAXON_IMPLICIT Result ( ResultOk< T2 > &&  value)

Constructs a Result object without error from a ResultOk object.

Parameters
[in]valueThe ResultOk from which the value is taken.

◆ Result() [9/30]

MAXON_IMPLICIT Result ( typename std::conditional< STD_IS_REPLACEMENT(void, RESULT_TYPE), ResultOk< RESULT_TYPE >, DummyParamType && >::type  value)

Constructs a Result object without error from a ResultOk object.

Parameters
[in]valueThe ResultOk from which the value is taken.

◆ Result() [10/30]

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>.

Parameters
[in]valueIf nullptr, set the error object to an OutOfMemoryError.

◆ Result() [11/30]

MAXON_IMPLICIT Result ( const ResultMemT< T2 > &  value)

Constructs a Result object with a pointer value from a ResultPtr.

Parameters
[in]valueThe ResultPtr value to use.

◆ Result() [12/30]

MAXON_IMPLICIT Result ( ResultMemT< T2 > &&  value)

Constructs a Result object with a pointer value from a ResultPtr.

Parameters
[in]valueThe ResultPtr value to use.

◆ Result() [13/30]

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>.

Parameters
[in]valueIf nullptr, set the error object to an OutOfMemoryError.

◆ Result() [14/30]

MAXON_IMPLICIT Result ( const ResultRef< T2 > &  value)

Constructs a Result object with a C++ reference value from a ResultRef.

Parameters
[in]valueThe ResultRef value to use.

◆ Result() [15/30]

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>.

◆ Result() [16/30]

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.

Parameters
[in]valueThe result value.

◆ Result() [17/30]

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.

Parameters
[in]valueThe result value.

◆ Result() [18/30]

MAXON_IMPLICIT Result ( typename std::conditional< T_IS_ERROR, const DummyParamType &&, DeleteType >::type  value)
delete

◆ Result() [19/30]

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.

Parameters
[in]valueThe result value.

◆ Result() [20/30]

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>.

◆ Result() [21/30]

MAXON_IMPLICIT Result ( const Result< T2 > &  src)

Constructs a Result object from an existing Result object src by using the value and/or error of src.

◆ Result() [22/30]

MAXON_IMPLICIT Result ( Result< T2 > &&  src)

Constructs a Result object from an existing Result object src by using the value and/or error of src.

◆ Result() [23/30]

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 
)

Constructs a Result object with the given error. The result value will be initialized as a default value.

Parameters
[in]errorError object to use for the Result.

◆ Result() [24/30]

Result ( CopyType  value,
const Result< T2 > &  result 
)

Constructs a Result object with both a value and an error taken from a Result.

Parameters
[in]valueResult value.
[in]resultResult object (from which the error is copied).

◆ Result() [25/30]

Result ( MoveType  value,
const Result< T2 > &  result 
)

Constructs a Result object with both a value and an error taken from a Result.

Parameters
[in]valueResult value.
[in]resultResult object (from which the error is copied).

◆ Result() [26/30]

Result ( DeleteType  value,
const Result< T2 > &  result 
)
delete

◆ Result() [27/30]

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 
)

Constructs a Result object with both a result value and an error.

Parameters
[in]valueResult value.
[in]errorError object.

◆ Result() [28/30]

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 
)

Constructs a Result object with both a result value and an error.

Parameters
[in]valueResult value.
[in]errorError object.

◆ Result() [29/30]

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() [30/30]

Result ( typename std::conditional< STD_IS_REPLACEMENT(same, typename std::decay< RESULT_TYPE >::type, Bool), volatile DummyParamType &&, Bool >::type  )
explicitdelete

Member Function Documentation

◆ GetError() [1/2]

SFINAEHelper<Error, RESULT_TYPE>::type GetError ( ) const

Returns the error object. If there is no error, this will be a null reference.

Returns
Error object, may be a null reference.

◆ GetErrorPointer()

MAXON_ATTRIBUTE_FORCE_INLINE const ErrorInterface* GetErrorPointer ( ) const

Returns the error object pointer. If there is no error, this will be nullptr.

Returns
Error object pointer, may be nullptr.

◆ GetError() [2/2]

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:

Result<void> result = MyFunc();
if (IllegalArgumentError e = result.GetError<IllegalArgumentError>())
{
// handle IllegalArgumentError
}
else
{
// all other errors (if any) are returned to the caller
}
PyObject PyObject * result
Definition: abstract.h:43
Py_ssize_t * e
Definition: longobject.h:89
#define iferr_return
Definition: resultbase.h:1524

If you don't need the E reference, you can use the HasError() function.

Template Parameters
EThe error reference class to check for.
Returns
Error object of type E, may be a null reference.

◆ HasError()

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.

Template Parameters
EThe error reference class to check for.
Returns
True if this Result holds an Error object of type E, false otherwise.

◆ operator==() [1/2]

Bool operator== ( ERROR_OK  ok) const

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).

Parameters
[in]okUse the global constant OK.
Returns
True if this @CLASS has no error.

◆ operator==() [2/2]

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).

Parameters
[in]failedUse the global constant FAILED.
Returns
True if this @CLASS has an error.

◆ operator=()

void operator= ( typename std::conditional< STD_IS_REPLACEMENT(reference, RESULT_TYPE), RESULT_TYPE, const DummyParamType & >::type  )
delete

◆ InitError() [1/2]

void InitError ( const Error &  error)
private

◆ InitError() [2/2]

void InitError ( Error &&  error)
private

Member Data Documentation

◆ T_IS_ERROR

const Bool T_IS_ERROR
static