jubilant-funicular
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
nta::utils::Option< T > Class Template Reference

#include <Option.h>

Collaboration diagram for nta::utils::Option< T >:
Collaboration graph
[legend]

Classes

struct  Nop
 Only exists to avoid a warning when using Option<T&> More...
 

Public Member Functions

 Option ()
 Defaults to none variant.
 
 Option (const Option &)=default
 
template<typename S >
 Option (const Option< S > &)
 
 operator bool () const
 Same as is_some.
 
bool is_some () const
 Does this hold some data.
 
bool is_none () const
 Does this hold nothing?
 
get () const
 
unwrap () const
 unwrap and get are the same thing
 
get_or (const T &optb)
 Return the data held by this Option or optb if it's None.
 
unwrap_or (const T &optb)
 
void destroy ()
 Turns this into None variant, calling destructor if necessary.
 
template<typename S >
Option< S > map (std::function< S(T)> func)
 
template<typename S >
Option< S > map (std::function< Option< S >(T)> func)
 
template<typename S >
map_or (std::function< S(T)> func, const S &def)
 
void map (std::function< void(T)> func)
 

Static Public Member Functions

static Option some (const T &data)
 Creates an Option holding some data.
 
static Option none ()
 Creates a None variant Option.
 

Private Types

using type = typename std::remove_reference< T >::type
 
using storage_type = typename std::conditional< std::is_reference< T >::value, void *, T >::type
 
using placement_type = typename std::conditional< std::is_reference< T >::value, Nop, type >::type
 

Private Member Functions

 Option (const T &d)
 Private constructor (use some or none instead)
 

Private Attributes

std::aligned_storage_t< sizeof(storage_type), alignof(storage_type)> m_data
 
bool m_some
 

Detailed Description

template<typename T>
class nta::utils::Option< T >

A class that optionally holds some data

Used to signal that a value may or may not be present

T can be a reference type (e.g. Option<std::string&> works as expected)

Definition at line 17 of file Option.h.

Member Function Documentation

◆ get()

template<typename T >
T nta::utils::Option< T >::get

Retrieve the underlying data

You should always call is_some/is_none beforehand

Todo:
Push an error to ErrorManager instead

Definition at line 86 of file Option.h.

Referenced by nta::utils::Option< T >::get_or(), and nta::utils::Option< T >::unwrap().

◆ map()

template<typename T >
template<typename S >
Option< S > nta::utils::Option< T >::map ( std::function< S(T)>  func)

Returns an Option holding the result of applying func to data or None if this == None

Definition at line 104 of file Option.h.


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