ResultRef< T > Class Template Reference

#include <resultbase.h>

Detailed Description

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

ResultRef<T> wraps a pointer. When the pointer is nullptr, this has to be interpreted as an out-of-memory error condition, otherwise GetValue() returns the pointer as a C++ reference.

ResultRef<T> can be used instead of the more general Result<T&> when only out-of-memory conditions can occur in a function. It will lead to more efficient code.

Sometimes it is necessary to convert a ResultRef<T> to a ResultPtr<T>, for example if the variable to which the result shall be assigned already exists:

BaseArray<Int> array;
Int& ref = array.Append() iferr_return;
Int* ptr = ...;
if (some condition)
{
ptr = &array.Append() iferr_return;
}
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:196
#define iferr_return
Definition: resultbase.h:1771

The result of the second call to Append has to be converted from ResultRef<Int> to ResultPtr<Int> so that (in the successful case) #ptr can be set to a pointer to the new array element. For this purpose the address-of operator & is overloaded at ResultRef to do exactly this conversion.

All error handling features of the Maxon API (such as iferr, iferr_return and Result) support ResultRef.

Public Types

using IsResult = std::true_type
 
using ValueType = T &
 

Public Member Functions

MAXON_IMPLICIT ResultRef (T *value=nullptr)
 
MAXON_IMPLICIT ResultRef (T &value)
 
MAXON_IMPLICIT ResultRef (ERROR_FAILED)
 
void operator= (T &)=delete
 
template<typename T2 , typename = typename std::enable_if<(STD_IS_REPLACEMENT(same, const T, const T2) || STD_IS_REPLACEMENT(base_of, T, T2)) && (STD_IS_REPLACEMENT(const, T) >= STD_IS_REPLACEMENT(const, T2))>::type>
MAXON_IMPLICIT ResultRef (const ResultRef< T2 > &src)
 
template<typename T2 , typename = typename std::enable_if<(STD_IS_REPLACEMENT(same, const T, const T2) || STD_IS_REPLACEMENT(base_of, T, T2)) && (STD_IS_REPLACEMENT(const, T) >= STD_IS_REPLACEMENT(const, T2))>::type>
MAXON_IMPLICIT ResultRef (const ResultPtr< T2 > &src)
 
Bool operator== (ERROR_OK) const
 
Bool operator== (ERROR_FAILED) const
 
T & GetValue () const
 
T & UncheckedGetValue () const
 
const T & GetValueOr (const T &defaultValue) const
 
T GetValueOr (T &&defaultValue) const
 
const T & GetValueOrDefault () const
 
T * GetPointer () const
 
void IfErrIgnore (const char *) const
 
const ResultPtr< T > & operator& () const
 

Static Public Attributes

static constexpr Bool HAS_ERROR
 

Private Attributes

T * _value
 

Member Typedef Documentation

◆ IsResult

using IsResult = std::true_type

◆ ValueType

using ValueType = T&

Constructor & Destructor Documentation

◆ ResultRef() [1/5]

MAXON_IMPLICIT ResultRef ( T *  value = nullptr)

◆ ResultRef() [2/5]

MAXON_IMPLICIT ResultRef ( T &  value)

◆ ResultRef() [3/5]

◆ ResultRef() [4/5]

MAXON_IMPLICIT ResultRef ( const ResultRef< T2 > &  src)

◆ ResultRef() [5/5]

MAXON_IMPLICIT ResultRef ( const ResultPtr< T2 > &  src)

Member Function Documentation

◆ operator=()

void operator= ( T &  )
delete

◆ operator==() [1/2]

Bool operator== ( ERROR_OK  ) const

==(ERROR_OK)

==(ERROR_OK)

◆ operator==() [2/2]

Bool operator== ( ERROR_FAILED  ) const

==(ERROR_FAILED)

==(ERROR_FAILED)

◆ GetValue()

T& GetValue ( ) const

Returns the result value. This function must not be called when there is no value because an out-of-memory error occurred.

Returns
Result value.

◆ UncheckedGetValue()

T& UncheckedGetValue ( ) const

Returns the result value. The result of this function must not be used when there is no value (then it's an invalid C++ reference).

Returns
Result value.

◆ GetValueOr() [1/2]

const T& GetValueOr ( const T &  defaultValue) const

Returns the contained value, or a default value in case of an error.

Parameters
[in]defaultValueDefault value to be used in case of an error.

◆ GetValueOr() [2/2]

T GetValueOr ( T &&  defaultValue) const

Returns the contained value, or a default value in case of an error.

Parameters
[in]defaultValueDefault value to be used in case of an error.
Returns
Result value (or default in case of an error).

◆ GetValueOrDefault()

const T& GetValueOrDefault ( ) const

Returns the contained value, or a default value in case of an error.

Returns
Result value (or default in case of an error).

◆ GetPointer()

T* GetPointer ( ) const

Returns the result pointer.

Returns
Result pointer.

◆ IfErrIgnore()

void IfErrIgnore ( const char *  ) const

Allows to skip a returned error and to continue execution.

◆ operator&()

const ResultPtr<T>& operator& ( ) const

Converts from a ResultRef<T> to a ResultPtr<T>.

Note
Usually it is a really bad idea to overload the address-of operator. But in this case it can't do any harm because ResultRef isn't used in contexts where the normal address-of operator is used. And if & was applied to a ResultRef with the expectation of returning a pointer, the compiler would complain because ResultPtr doesn't mimic pointers at all.
Returns
This ResultRef, cast to a ResultPtr.

Member Data Documentation

◆ HAS_ERROR

constexpr Bool HAS_ERROR
staticconstexpr

◆ _value

T* _value
private