Result< RESULT_TYPE > Class Template Reference Data Types

#include <resultbase.h>

详细描述

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

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.

警告
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_TYPE Type of result value.
另请参阅
$ref error_result

Public Types

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

公共成员函数

  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::HasBase<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::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 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::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)
template<typename E >
  Result (DeleteType 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)=delete
  Result (typename std::conditional< STD_IS_REPLACEMENT (same, typename std::decay< RESULT_TYPE >::type, Bool ), 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_REPLACEMENT (reference, RESULT_TYPE), RESULT_TYPE, const DummyParamType & >::type)=delete

静态公共属性

static const Bool   T_IS_ERROR

私有成员函数

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

Member Typedef Documentation

◆  Super

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

构造函数 & 析构函数文档编制

◆  Result() [1/30]

Result ( )

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

◆  Result() [2/30]

MAXON_IMPLICIT Result ( ErrorPtr error )

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.

参数
[in] error Error object to use for the Result .

◆  Result() [3/30]

MAXON_IMPLICIT Result ( ErrorPtr &&  error )

◆  Result() [4/30]

MAXON_IMPLICIT Result ( const ErrorPtr error )

◆  Result() [5/30]

MAXON_IMPLICIT Result ( const ThreadReferencedError error )

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

参数
[in] value If 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 对象。

参数
[in] value The 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 对象。

参数
[in] value The 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 对象。

参数
[in] value The 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> .

参数
[in] value If 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.

参数
[in] value The ResultPtr value to use.

◆  Result() [12/30]

MAXON_IMPLICIT Result ( ResultMemT < T2 > &&  value )

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

参数
[in] value The 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> .

参数
[in] value If 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 .

参数
[in] value The 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.

参数
[in] value The 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.

参数
[in] value The 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.

参数
[in] value The 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::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.

参数
[in] error Error 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 .

参数
[in] value Result 值。
[in] result Result 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 .

参数
[in] value Result 值。
[in] result Result 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::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.

参数
[in] value Result 值。
[in] error Error object.

◆  Result() [28/30]

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.

参数
[in] value Result 值。
[in] error Error object.

◆  Result() [29/30]

Result ( DeleteType  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  
)
delete

◆  Result() [30/30]

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

成员函数文档编制

◆  GetError() [1/2]

const Error& GetError ( ) const

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

返回
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 ( ) 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
E The error reference class to check for.
返回
Error object of type E, may be a null reference.

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

参数
[in] ok Use the global constant OK.
返回
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).

参数
[in] failed Use the global constant FAILED.
返回
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
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
maxon::ResultMem
ResultMemT< Bool > ResultMem
定义: apibaseid.h:45
NewObj
#define NewObj(T,...)
定义: newobj.h:108