Honeycomb  0.1
Component-Model Framework
Public Member Functions | Friends | List of all members
honey::optional< T > Class Template Reference

Enables any type to be optional so it can exist in an uninitialized null state. More...

#include <Optional.h>

Public Member Functions

 optional ()
 Uninitialized by default. More...
 
 optional (optnull_t)
 
 optional (const optional &rhs)
 
 optional (optional &rhs)
 
 optional (optional &&rhs)
 
template<class U >
 optional (const optional< U > &rhs)
 
template<class U >
 optional (optional< U > &rhs)
 
template<class U >
 optional (optional< U > &&rhs)
 
template<class U >
 optional (U &&rhs)
 
 ~optional ()
 
optionaloperator= (optnull_t)
 Reset the optional to an uninitialized state. More...
 
optionaloperator= (const optional &rhs)
 Assign wrapped object. More...
 
optionaloperator= (optional &rhs)
 
optionaloperator= (optional &&rhs)
 
template<class U >
optionaloperator= (const optional< U > &rhs)
 
template<class U >
optionaloperator= (optional< U > &rhs)
 
template<class U >
optionaloperator= (optional< U > &&rhs)
 
template<class U >
optionaloperator= (U &&rhs)
 Assign object. On the first assignment the instance is copy/move constructed, assignments thereafter use copy/move assign. More...
 
bool operator== (optnull_t) const
 
bool operator!= (optnull_t) const
 
const T & operator* () const
 
T & operator* ()
 
const T * operator-> () const
 
T * operator-> ()
 
 operator const T & () const
 
 operator T & ()
 
 operator bool () const
 Test whether optional is initialized. More...
 
 operator bool ()
 
const T & get () const
 Get wrapped object. Optional must be initialized. More...
 
T & get ()
 
const T * ptr () const
 Get pointer to wrapped object. Returns null if not initialized. More...
 
T * ptr ()
 

Friends

template<class T_ >
class optional
 
ostream & operator<< (ostream &os, const optional &rhs)
 

Detailed Description

template<class T>
class honey::optional< T >

Enables any type to be optional so it can exist in an uninitialized null state.

An optional is large enough to hold an instance of its wrapped type. On construction or first assignment the instance is copy/move constructed, assignments thereafter use copy/move assign. Assigning to optnull will reset the optional to an uninitialized state, destructing any instance.

Supports wrapped const/ref types.

See also
optional<T&> for wrapped ref types.

Variables are commonly defined as pointers for the sole reason that pointers can exist in a null state. In this case, an optional can be used instead of a pointer to make the behavior explicit and encourage stack allocation. Optional syntax is also clearer for function calls:

func(iterator* optIter = nullptr);  func(&iter)     ---->  func(optional<iterator> optIter = optnull);  func(iter)
func(int* retVal = nullptr);        func(&retInt)   ---->  func(optional<int&> retVal = optnull);       func(retInt)
See also
optnull

Example:

func(optional<char> o = optnull)
{
optional<int&> rInt;
int i, j;
if (o == optnull) o = 'a'; //Set a default value if caller didn't specify a char
rInt.bind(i); //Bind the reference before use
rInt = 2; //Assignment to bound reference: i = 2
rInt.bind(&j); //Rebind to j from pointer
rInt = o; //j = the wrapped char in 'o'
rInt = optnull; //Reset to null unbound reference
int a = *o; //The * and -> operators can be used to retrieve the wrapped object
a = o + 1; //Optionals implicitly convert to their wrapped object
}

Constructor & Destructor Documentation

template<class T>
honey::optional< T >::optional ( )
inline

Uninitialized by default.

template<class T>
honey::optional< T >::optional ( optnull_t  )
inline
template<class T>
honey::optional< T >::optional ( const optional< T > &  rhs)
inline
template<class T>
honey::optional< T >::optional ( optional< T > &  rhs)
inline
template<class T>
honey::optional< T >::optional ( optional< T > &&  rhs)
inline
template<class T>
template<class U >
honey::optional< T >::optional ( const optional< U > &  rhs)
inline
template<class T>
template<class U >
honey::optional< T >::optional ( optional< U > &  rhs)
inline
template<class T>
template<class U >
honey::optional< T >::optional ( optional< U > &&  rhs)
inline
template<class T>
template<class U >
honey::optional< T >::optional ( U &&  rhs)
inline
template<class T>
honey::optional< T >::~optional ( )
inline

Member Function Documentation

template<class T>
const T& honey::optional< T >::get ( ) const
inline

Get wrapped object. Optional must be initialized.

template<class T>
T& honey::optional< T >::get ( )
inline
template<class T>
honey::optional< T >::operator bool ( ) const
inlineexplicit

Test whether optional is initialized.

template<class T>
honey::optional< T >::operator bool ( )
inlineexplicit
template<class T>
honey::optional< T >::operator const T & ( ) const
inline
template<class T>
honey::optional< T >::operator T & ( )
inline
template<class T>
bool honey::optional< T >::operator!= ( optnull_t  ) const
inline
template<class T>
const T& honey::optional< T >::operator* ( ) const
inline
template<class T>
T& honey::optional< T >::operator* ( )
inline
template<class T>
const T* honey::optional< T >::operator-> ( ) const
inline
template<class T>
T* honey::optional< T >::operator-> ( )
inline
template<class T>
optional& honey::optional< T >::operator= ( optnull_t  )
inline

Reset the optional to an uninitialized state.

template<class T>
optional& honey::optional< T >::operator= ( const optional< T > &  rhs)
inline

Assign wrapped object.

template<class T>
optional& honey::optional< T >::operator= ( optional< T > &  rhs)
inline
template<class T>
optional& honey::optional< T >::operator= ( optional< T > &&  rhs)
inline
template<class T>
template<class U >
optional& honey::optional< T >::operator= ( const optional< U > &  rhs)
inline
template<class T>
template<class U >
optional& honey::optional< T >::operator= ( optional< U > &  rhs)
inline
template<class T>
template<class U >
optional& honey::optional< T >::operator= ( optional< U > &&  rhs)
inline
template<class T>
template<class U >
optional& honey::optional< T >::operator= ( U &&  rhs)
inline

Assign object. On the first assignment the instance is copy/move constructed, assignments thereafter use copy/move assign.

For an optional to be assignable to rhs it must also be constructible with rhs, this is necessary for allowing the optional to be nullable.

template<class T>
bool honey::optional< T >::operator== ( optnull_t  ) const
inline
template<class T>
const T* honey::optional< T >::ptr ( ) const
inline

Get pointer to wrapped object. Returns null if not initialized.

template<class T>
T* honey::optional< T >::ptr ( )
inline

Friends And Related Function Documentation

template<class T>
ostream& operator<< ( ostream &  os,
const optional< T > &  rhs 
)
friend
template<class T>
template<class T_ >
friend class optional
friend

The documentation for this class was generated from the following file: