Result< T > Class Template Reference

#include <resultbase.h>

Inheritance diagram for Result< T >:

Detailed Description

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

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);
}

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);
}

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
TType of result value.
See also
$ref error_result

Public Types

using Super = typename ResultSuper< T, 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_void< T >::value, 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_void< T >::value, ResultOk< T >, DummyParamType &&>::type value)
 
template<typename T2 >
MAXON_IMPLICIT Result (const ResultMemT< T2 > &value, typename std::enable_if< std::is_void< typename SFINAEHelper< T, T2 >::type >::value, 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_void< typename SFINAEHelper< T, T2 >::type >::value, 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_void< T >::value, 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)
 
template<typename T2 , typename = typename std::enable_if<!maxon::HasBase<typename std::decay<T2>::type::ReferencedType, ErrorInterface>::value, T>::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_void< typename SFINAEHelper< T, T2 >::type >::value, 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::HasBase::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 E >
 Result (CopyType value, E &&error, typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBase::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::HasBase::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), DummyParamType * >::type=(DummyParamType *) nullptr)
 
 Result (typename std::conditional< std::is_same< typename std::decay< T >::type, Bool >::value, volatile DummyParamType &&, Bool >::type)=delete
 
const Error & GetError () const
 
template<typename E >
std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBase::template Check< ErrorInterface >::value &&MAXON_IS_COW_KIND(std::remove_reference< E >::type::Handler::KIND), const E &>::type GetError () const
 
Bool operator== (ERROR_OK ok) const
 
Bool operator== (ERROR_FAILED failed) const
 
void operator= (typename std::conditional< std::is_reference< T >::value, T, 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 T & GetValue ()
 
MAXON_ATTRIBUTE_FORCE_INLINE const T & UncheckedGetValue () const
 
MAXON_ATTRIBUTE_FORCE_INLINE T & UncheckedGetValue ()
 
MAXON_ATTRIBUTE_FORCE_INLINE const T & GetValueAndError (ThreadReferencedError &error) const
 
MAXON_ATTRIBUTE_FORCE_INLINE T && MoveValueGetError (ThreadReferencedError &error)
 
MAXON_ATTRIBUTE_FORCE_INLINE const T & GetValueAndError (Error &error) const
 
MAXON_ATTRIBUTE_FORCE_INLINE T && MoveValueGetError (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 const 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_reference< CopyType >::value, T &&, DummyParamType & >::type
 
template<typename T2 >
using ConstructibleFrom = std::enable_if< std::is_constructible< T, T2 >::value >
 
- Protected Attributes inherited from ResultBase< T >
ThreadReferencedError _error
 
_value
 

Member Typedef Documentation

◆ Super

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

Constructor & Destructor Documentation

◆ Result() [1/27]

Result ( )

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

◆ Result() [2/27]

Constructs a Result object with the given error. The result value will be initialized as a null 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/27]

◆ Result() [4/27]

MAXON_IMPLICIT Result ( const ErrorPtr error)

◆ Result() [5/27]

◆ Result() [6/27]

MAXON_IMPLICIT Result ( typename std::conditional< std::is_void< T >::value, 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/27]

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/27]

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/27]

MAXON_IMPLICIT Result ( typename std::conditional< std::is_void< T >::value, ResultOk< T >, 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/27]

MAXON_IMPLICIT Result ( const ResultMemT< T2 > &  value,
typename std::enable_if< std::is_void< typename SFINAEHelper< T, T2 >::type >::value, 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/27]

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/27]

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/27]

MAXON_IMPLICIT Result ( const ResultRef< T2 > &  value,
typename std::enable_if< std::is_void< typename SFINAEHelper< T, T2 >::type >::value, 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/27]

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/27]

MAXON_IMPLICIT Result ( typename std::conditional< std::is_void< T >::value, 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/27]

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/27]

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/27]

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

Result ( const Result< T2 > &  src,
typename std::enable_if< std::is_void< typename SFINAEHelper< T, T2 >::type >::value, 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() [20/27]

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

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

MAXON_IMPLICIT Result ( E &&  error,
typename std::enable_if<!T_IS_ERROR, typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBase::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 null value.

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

◆ Result() [23/27]

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

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

Result ( CopyType  value,
E &&  error,
typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBase::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() [26/27]

Result ( MoveType  value,
E &&  error,
typename std::enable_if< std::remove_reference< E >::type::DirectlyReferencedType::HasBase::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() [27/27]

Result ( typename std::conditional< std::is_same< typename std::decay< T >::type, Bool >::value, volatile DummyParamType &&, Bool >::type  )
explicitdelete

Member Function Documentation

◆ GetError() [1/2]

const Error& GetError ( void  ) const

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

Returns
Error object, may be a null reference.

◆ GetError() [2/2]

std::enable_if<std::remove_reference<E>::type::DirectlyReferencedType::HasBase::template Check<ErrorInterface>::value && MAXON_IS_COW_KIND(std::remove_reference<E>::type::Handler::KIND), const E& >::type GetError ( void  ) 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
result iferr_return;
}
Template Parameters
EThe error reference class to check for.
Returns
Error object of type E, may be a null reference.

◆ operator==() [1/2]

Bool operator== ( ERROR_OK  ok) const

Checks if this 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 has no error.

◆ operator==() [2/2]

Bool operator== ( ERROR_FAILED  failed) const

Checks if this 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 has an error.

◆ operator=()

void operator= ( typename std::conditional< std::is_reference< T >::value, T, 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