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;
}
maxon::Int Int
Definition: ge_sys_math.h:64
char ** ptr
Definition: pycore_dtoa.h:13
#define iferr_return
Definition: resultbase.h:1519

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 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
 
T * GetPointer () const
 
const ResultPtr< T > & operator& () const
 

Private Attributes

T * _value
 

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.

◆ GetPointer()

T* GetPointer ( ) const

Returns the result pointer.

Returns
Result pointer.

◆ 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

◆ _value

T* _value
private