Honeycomb  0.1
Android / iOS 3D Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Friends | List of all members
honey::option< T > Class Template Reference

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

#include <Option.h>

Public Member Functions

 option ()
 Uninitialized by default. More...
 
 option (optnull_t)
 
 option (const option &rhs)
 
 option (option &rhs)
 
 option (option &&rhs)
 
template<class U >
 option (const option< U > &rhs)
 
template<class U >
 option (option< U > &rhs)
 
template<class U >
 option (option< U > &&rhs)
 
template<class U >
 option (U &&rhs)
 
 ~option ()
 
optionoperator= (optnull_t)
 Reset the option to an uninitialized state. More...
 
optionoperator= (const option &rhs)
 Assign wrapped object. More...
 
optionoperator= (option &rhs)
 
optionoperator= (option &&rhs)
 
template<class U >
optionoperator= (const option< U > &rhs)
 
template<class U >
optionoperator= (option< U > &rhs)
 
template<class U >
optionoperator= (option< U > &&rhs)
 
template<class U >
optionoperator= (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 option is initialized. More...
 
 operator bool ()
 
const T & get () const
 Get wrapped object. Option 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 option
 
StringStreamoperator<< (StringStream &os, const option &rhs)
 To string. More...
 

Detailed Description

template<class T>
class honey::option< T >

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

An option 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 option to an uninitialized state, destructing any instance.

Supports wrapped const/ref types.

See Also
option<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 option can be used instead of a pointer to make the behavior explicit and encourage stack allocation. Option syntax is also clearer for function calls:

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

Example:

func(option<char> o = optnull)
{
option<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; //Options implicitly convert to their wrapped object
}

Constructor & Destructor Documentation

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

Uninitialized by default.

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

Member Function Documentation

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

Get wrapped object. Option must be initialized.

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

Test whether option is initialized.

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

Reset the option to an uninitialized state.

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

Assign wrapped object.

template<class T>
option& honey::option< T >::operator= ( option< T > &  rhs)
inline
template<class T>
option& honey::option< T >::operator= ( option< T > &&  rhs)
inline
template<class T>
template<class U >
option& honey::option< T >::operator= ( const option< U > &  rhs)
inline
template<class T>
template<class U >
option& honey::option< T >::operator= ( option< U > &  rhs)
inline
template<class T>
template<class U >
option& honey::option< T >::operator= ( option< U > &&  rhs)
inline
template<class T>
template<class U >
option& honey::option< 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 option to be assignable to rhs it must also be constructible with rhs, this is necessary for allowing the option to be nullable.

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

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

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

Friends And Related Function Documentation

template<class T>
StringStream& operator<< ( StringStream os,
const option< T > &  rhs 
)
friend

To string.

template<class T>
template<class T_ >
friend class option
friend

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