ModErn Text Analysis
META Enumerates Textual Applications
Public Member Functions | Private Member Functions | Private Attributes | List of all members
meta::util::optional< T > Class Template Reference

A class for representing optional values. More...

#include <optional.h>

Inheritance diagram for meta::util::optional< T >:
meta::util::comparable< optional< T > >

Public Member Functions

 optional ()
 Default constructor, creates a disengaged optional value.
 
 optional (nullopt_t)
 Conversion constructor from nullopt, creates a disengaged optional value.
 
 optional (const T &value)
 Creates an optional value with the given contents. More...
 
 optional (T &&value)
 Creates an optional value with the given contents via move construction. More...
 
 optional (const optional &)
 Copy constructor.
 
 optional (optional &&)
 Move constructor.
 
optionaloperator= (optional)
 Assignment operator.
 
 ~optional ()
 Destructor. More...
 
void swap (optional &other)
 Swaps the current optional instance with the parameter. More...
 
const T & operator* () const
 Obtains the value contained in the optional. More...
 
T & operator* ()
 Obtains the value contained in the optional. More...
 
const T * operator-> () const
 Member access operator to the value contained in the optional. More...
 
T * operator-> ()
 Member access operator to the value contained in the optional. More...
 
 operator bool () const
 
void clear ()
 Empties the optional.
 
template<class U >
value_or (U &&default_value) const &
 
template<class U >
value_or (U &&default_value) &&
 

Private Member Functions

const T * dataptr () const
 Helper function to obtain the address of the contained value. More...
 
T * dataptr ()
 Helper function to obtain the address of the contained value. More...
 

Private Attributes

bool initialized_
 Whether or not this optional is engaged.
 
optional_storage< T > storage_
 The storage for this optional.
 

Detailed Description

template<class T>
class meta::util::optional< T >

A class for representing optional values.

This is a very naiive approach based on the design given in the WG21 paper and the reference implementation. Many features of the real optional<T> design there are not supported here—this is a lightweight replacement until C++14 is completed and implemented in compilers.

See also
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3527.html
https://github.com/akrzemi1/Optional/

Constructor & Destructor Documentation

§ optional() [1/2]

template<class T>
meta::util::optional< T >::optional ( const T &  value)

Creates an optional value with the given contents.

Parameters
valuethe desired value to be contained in the optional

§ optional() [2/2]

template<class T>
meta::util::optional< T >::optional ( T &&  value)

Creates an optional value with the given contents via move construction.

Parameters
valuethe desired value to be moved into the optional

§ ~optional()

template<class T >
meta::util::optional< T >::~optional ( )

Destructor.

Responsible for ensuring the proper destruction of the contained type, if it is engaged.

Member Function Documentation

§ swap()

template<class T >
void meta::util::optional< T >::swap ( optional< T > &  other)

Swaps the current optional instance with the parameter.

Parameters
otherthe optional to swap with

§ operator*() [1/2]

template<class T >
const T & meta::util::optional< T >::operator* ( ) const

Obtains the value contained in the optional.

Const version.

Returns
the value in the optional

§ operator*() [2/2]

template<class T >
T & meta::util::optional< T >::operator* ( )

Obtains the value contained in the optional.

Non-const version.

Returns
the value in the optional

§ operator->() [1/2]

template<class T >
const T * meta::util::optional< T >::operator-> ( ) const

Member access operator to the value contained in the optional.

Const version.

Returns
a pointer to the current value in the optional (member access)

§ operator->() [2/2]

template<class T >
T * meta::util::optional< T >::operator-> ( )

Member access operator to the value contained in the optional.

Non-const version.

Returns
a pointer to the current value in the optional (member access)

§ operator bool()

template<class T >
meta::util::optional< T >::operator bool ( ) const
explicit
Returns
whether the optional is engaged

§ value_or() [1/2]

template<class T >
template<class U >
T meta::util::optional< T >::value_or ( U &&  default_value) const
Parameters
default_valueThe value to return if this optional is empty
Returns
the contained value if there is on, or default_value otherwise

§ value_or() [2/2]

template<class T >
template<class U >
T meta::util::optional< T >::value_or ( U &&  default_value)
Parameters
default_valueThe value to return if this optional is empty
Returns
the contained value if there is on, or default_value otherwise

§ dataptr() [1/2]

template<class T >
const T * meta::util::optional< T >::dataptr ( ) const
private

Helper function to obtain the address of the contained value.

const version.

Returns
the address of the contained value

§ dataptr() [2/2]

template<class T >
T * meta::util::optional< T >::dataptr ( )
private

Helper function to obtain the address of the contained value.

non-const version.

Returns
the address of the contained value

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