Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Option< T > Class Template Reference

#include <elm/util/Option.h>

Public Member Functions

 Option ()
 
 Option (const OptionalNone &none)
 
 Option (t::in< T > value)
 
 Option (const Option< T > &opt)
 
bool some () const
 
bool none () const
 
t::ret< T > value () const
 
t::ret< T > operator* () const
 
template<class F >
const Option< T > & if_one (const F &f) const
 
template<class F >
const Option< T > & if_else (const F &f) const
 
Option< T > & operator= (const Option< T > &opt)
 
Option< T > & operator= (t::in< T > value)
 
bool equals (const OptionalNone &_) const
 
bool equals (const Option< T > &opt) const
 
bool operator== (const OptionalNone &_) const
 
bool operator!= (const OptionalNone &_) const
 
bool operator== (const Option< T > &o) const
 
bool operator!= (const Option< T > &o) const
 
 operator bool (void) const
 
 operator t::ret< T > () const
 
bool isOne () const
 
bool isNone () const
 

Detailed Description

template<class T>
class elm::Option< T >

This class is very handy to manage parameters or return value that may be optional. This class records there is no value or there is a value and the instance of the value.

To build it, you may be easier to use the special value elm::none or the special function elm::some() instead of the class constructors.
if(cannot_compute)
return none;
else
return some(result);
Please consider the conversion operator allowing to test of the availability of a value by putting it straight-forward in a condition or accessing the value by putting the optional value in place where the value may be used.
Option<int> val = f();
if(val)
x += val;
Parameters
TType of the stored value.

Constructor & Destructor Documentation

◆ Option() [1/4]

Option ( )
inline

◆ Option() [2/4]

Option ( const OptionalNone none)
inline
@fn Option::Option();

Build an empty optional value recording there is no value.

For nternal use only.

◆ Option() [3/4]

Option ( t::in< T >  value)
inline

Build an optional value containing the given one.

Parameters
valueValue to store.

◆ Option() [4/4]

Option ( const Option< T > &  opt)
inline

Build an optional value by copying an existing one.

Parameters
optOptional value to clone.

Member Function Documentation

◆ equals() [1/2]

bool equals ( const Option< T > &  opt) const
inline

◆ equals() [2/2]

bool equals ( const OptionalNone _) const
inline

◆ if_else()

const Option<T>& if_else ( const F &  f) const
inline

◆ if_one()

const Option<T>& if_one ( const F &  f) const
inline

◆ isNone()

bool isNone ( ) const
inline

Test if no value is available.

Returns
True if no value is available, false else.

Referenced by StrictMapDelegate< C >::get().

◆ isOne()

bool isOne ( ) const
inline

Test if a value is available.

Returns
True if a value is available, false else.

◆ none()

◆ operator bool()

operator bool ( void  ) const
inline
Returns
Evaluates to true if a value is available, false else.

References Option< T >::some().

◆ operator t::ret< T >()

operator t::ret< T > ( ) const
inline

Convert to the stored value.

Deprecated:

References Option< T >::value().

◆ operator!=() [1/2]

bool operator!= ( const Option< T > &  o) const
inline

References Option< T >::equals().

◆ operator!=() [2/2]

bool operator!= ( const OptionalNone _) const
inline

References Option< T >::none().

◆ operator*()

t::ret< T > operator* ( ) const
inline
Returns
The stored value.

References Option< T >::value().

◆ operator=() [1/2]

Option< T > & operator= ( const Option< T > &  opt)
inline

Provides assignment support.

Parameters
optOptional value to assign.
Returns
Current object.

◆ operator=() [2/2]

Option<T>& operator= ( t::in< T >  value)
inline

References type_info< T >::put().

◆ operator==() [1/2]

bool operator== ( const Option< T > &  o) const
inline

References Option< T >::equals().

◆ operator==() [2/2]

bool operator== ( const OptionalNone _) const
inline

References Option< T >::none().

◆ some()

◆ value()

t::ret< T > value ( ) const
inline

Get the value.

Returns
The stored value.
Warning
It is an error to call this method if no value is available.

Referenced by Option< T >::operator t::ret< T >(), and Option< T >::operator*().


The documentation for this class was generated from the following files:
elm::Option::none
bool none() const
Definition: Option.h:43
elm::Option::some
bool some() const
Definition: Option.h:42