ReferenceWrapper< TYPE > Class Template Reference

#include <referencewrapper.h>

Detailed Description

template<typename TYPE>
class maxon::ReferenceWrapper< TYPE >

Wraps a pointer, but enforces reference semantics for assignment and hence cannot be null. Can be used just like a normal pointer (e.g. operator -> to call methods). However, even if it works, the use of operator * is discouraged and reference semantics should be used instead. Note that a ReferenceWrapper instance can be converted implicitly to a reference or a pointer of the same type (but not the other way around!). Similar in function to std::reference_wrapper<TYPE>.

Float value1 = 5.0;
Float value2 = 10.0;
ReferenceWrapper<Float> valueRef; // Doesn't compile! A reference cannot be null.
ReferenceWrapper<Float> valueRef = value1; // Doesn't compile! To improve readability, implicit conversions are disabled and an explicit call to Ref or CRef functions is enforced (as opposed to std::reference_wrapper<TYPE>).
ReferenceWrapper<Float> valueRef = Ref(value1); // valueRef now points to value1
ReferenceWrapper<Float> valueRef(Ref(value1)); // valueRef now points to value1 (equivalent to the previous statement)
valueRef = 0.0; // value1 now equals 0.0
valueRef = value2; // value1 now equals 10.0
valueRef = Ref(value2); // valueRef now points to value2
valueRef = 0.0; // value2 now equals 0.0
*valueRef = 5.0; // value2 now equals 5.0. Works, but isn't recommended, because valueRef now looks like a pointer.
maxon::Float Float
Definition: ge_sys_math.h:66
ReferenceWrapper< TYPE > Ref(TYPE &val) noexcept
Constructs a ReferenceWrapper instance from a reference type.
Definition: referencewrapper.h:112

Public Member Functions

 ReferenceWrapper ()=default
 
 ReferenceWrapper (const ReferenceWrapper &) noexcept=default
 
ReferenceWrapperoperator= (const ReferenceWrapper &) noexcept=default
 
void operator= (const TYPE &value)
 
UInt GetHashCode () const
 
Bool IsEqual (const ReferenceWrapper &ref) const
 
 operator TYPE * () const noexcept
 
 operator TYPE & () const noexcept
 
TYPE * operator-> () const noexcept
 
TYPE ** operator& () noexcept
 

Static Public Member Functions

static ReferenceWrapper< TYPE > Create (TYPE &val) noexcept
 
static ReferenceWrapper< const TYPE > CreateConst (TYPE &val) noexcept
 

Private Member Functions

MAXON_IMPLICIT ReferenceWrapper (TYPE &ref) noexcept
 
MAXON_IMPLICIT ReferenceWrapper (TYPE &&)=delete
 

Private Attributes

TYPE * _ptr
 

Constructor & Destructor Documentation

◆ ReferenceWrapper() [1/4]

MAXON_IMPLICIT ReferenceWrapper ( TYPE &  ref)
privatenoexcept

◆ ReferenceWrapper() [2/4]

MAXON_IMPLICIT ReferenceWrapper ( TYPE &&  )
privatedelete

◆ ReferenceWrapper() [3/4]

ReferenceWrapper ( )
default

◆ ReferenceWrapper() [4/4]

ReferenceWrapper ( const ReferenceWrapper< TYPE > &  )
defaultnoexcept

Member Function Documentation

◆ Create()

static ReferenceWrapper<TYPE> Create ( TYPE &  val)
staticnoexcept

◆ CreateConst()

static ReferenceWrapper<const TYPE> CreateConst ( TYPE &  val)
staticnoexcept

◆ operator=() [1/2]

ReferenceWrapper& operator= ( const ReferenceWrapper< TYPE > &  )
defaultnoexcept

◆ operator=() [2/2]

void operator= ( const TYPE &  value)

Used to change the value of the referenced object.

◆ GetHashCode()

UInt GetHashCode ( ) const

Hash function (for maxon::HashMap compatibility).

◆ IsEqual()

Bool IsEqual ( const ReferenceWrapper< TYPE > &  ref) const

Comparison function (for maxon::HashMap compatibility).

◆ operator TYPE *()

operator TYPE * ( ) const
noexcept

Implicit conversion to a raw pointer type. Makes it possible to pass the object directly to functions like Function(TYPE* t).

Returns
The internal pointer. The AutoAlloc instance owns the pointed object.

◆ operator TYPE &()

operator TYPE & ( ) const
noexcept

Implicit conversion to reference type. Makes it possible to pass the object directly to functions like Function(TYPE& t).

Returns
The dereferenced pointer.

◆ operator->()

TYPE* operator-> ( ) const
noexcept

Used for calls like referenceWrapper->Function().

Returns
The internal pointer.

◆ operator&()

TYPE** operator& ( )
noexcept

Used for expressions like &referenceWrapper. Extracts a pointer to the internal pointer.

Note
This means that it is impossible to get the address of the actual ReferenceWrapper instance.
Returns
A pointer to the internal pointer.

Member Data Documentation

◆ _ptr

TYPE* _ptr
private