You.i Engine
CYIOptional< T > Class Template Reference

Detailed Description

template<typename T>
class CYIOptional< T >

A container class that either contains a value or 'nothing'.

The typical use case for this class is as return value for functions, but it may also be used to specify default arguments to functions, and to make lazily-initialized class members.

When a CYIOptional object is received, it must always be tested before its value is accessed. Failure to do so would result in a runtime crash if the optional does not contain a value. To check if the optional object contains a value, either call CYIOptional::HasValue() or use the optional object in a boolean context (e.g. if (optionalObject)).

Alternatively, CYIOptional::GetValueOr() can be used to use a specific default value when the optional object does not contain a value. When using CYIOptional::GetValueOr(), it isn't necessary to verify that the optional object contains a value.

Values in a CYIOptional object are always stored without making additional heap allocations (although the value itself may perform heap allocations in its constructor).

Follows is an example of how a CYIOptional object can be returned from a function:

CYIOptional<CYIString> GetSubtitle()
{
auto it = m_attributes.find("subtitle");
if (it != m_attributes.end())
{
const CYIString &subtitle = it->second;
return subtitle;
}
// No subtitle available
return {};
}

This is an example of how a function that returns a CYIOptional value can be used:

if (auto subtitle = dataObject.GetSubtitle())
{
m_pSubtitleNode->SetText(std::move(subtitle.GetValue()));
}

#include <utility/YiOptional.h>

Public Member Functions

constexpr CYIOptional ()
 
constexpr CYIOptional (CYIEmptyOptionalType)
 
YI_CONSTEXPR CYIOptional (const CYIOptional &other)
 
template<typename U >
YI_CONSTEXPR CYIOptional (const CYIOptional< U > &other)
 
YI_CONSTEXPR CYIOptional (CYIOptional &&other) noexcept(std::is_nothrow_move_constructible< T >::value)
 
template<typename U >
YI_CONSTEXPR CYIOptional (CYIOptional< U > &&other)
 
constexpr CYIOptional (const T &value)
 
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
constexpr CYIOptional (const U &value)
 
constexpr CYIOptional (T &&value)
 
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
constexpr CYIOptional (U &&value)
 
 ~CYIOptional ()
 
YI_CONSTEXPR CYIOptionaloperator= (const CYIOptional &other)
 
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
YI_CONSTEXPR CYIOptionaloperator= (const U &value)
 
YI_CONSTEXPR CYIOptionaloperator= (CYIOptional &&other)
 
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
YI_CONSTEXPR CYIOptionaloperator= (U &&value)
 
template<typename... Args>
T & Emplace (Args &&... args)
 
constexpr bool HasValue () const
 
constexpr operator bool () const
 
T * operator-> ()
 
const T * operator-> () const
 
T & GetValue () &
 
const T & GetValue () const &
 
T && GetValue () &&
 
T & operator* () &
 
const T & operator* () const &
 
T && operator* () &&
 
template<typename U >
constexpr T GetValueOr (U &&alternative) const &
 
template<typename U >
constexpr T GetValueOr (U &&alternative) &&
 
YI_CONSTEXPR void Reset ()
 
YI_CONSTEXPR void Swap (CYIOptional &other)
 

Friends

template<typename U >
class CYIOptional
 

Constructor & Destructor Documentation

◆ CYIOptional() [1/10]

template<typename T>
constexpr CYIOptional< T >::CYIOptional ( )

Creates an empty optional instance.

◆ CYIOptional() [2/10]

template<typename T>
constexpr CYIOptional< T >::CYIOptional ( CYIEmptyOptionalType  )

◆ CYIOptional() [3/10]

template<typename T>
YI_CONSTEXPR CYIOptional< T >::CYIOptional ( const CYIOptional< T > &  other)

Copy constructor. Copies other and its contained value (if any) into a new CYIOptional object.

◆ CYIOptional() [4/10]

template<typename T>
template<typename U >
YI_CONSTEXPR CYIOptional< T >::CYIOptional ( const CYIOptional< U > &  other)

◆ CYIOptional() [5/10]

template<typename T>
YI_CONSTEXPR CYIOptional< T >::CYIOptional ( CYIOptional< T > &&  other) const
noexcept

Move constructor. Moves the value contained in other (if any) into a new CYIOptional object.

◆ CYIOptional() [6/10]

template<typename T>
template<typename U >
YI_CONSTEXPR CYIOptional< T >::CYIOptional ( CYIOptional< U > &&  other)

◆ CYIOptional() [7/10]

template<typename T>
constexpr CYIOptional< T >::CYIOptional ( const T &  value)

\ details Value copy constructor. Copies value into a new CYIOptional object.

◆ CYIOptional() [8/10]

template<typename T>
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
constexpr CYIOptional< T >::CYIOptional ( const U &  value)

◆ CYIOptional() [9/10]

template<typename T>
constexpr CYIOptional< T >::CYIOptional ( T &&  value)

Value move constructor. Moves value into a new CYIOptional object.

◆ CYIOptional() [10/10]

template<typename T>
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
constexpr CYIOptional< T >::CYIOptional ( U &&  value)

◆ ~CYIOptional()

template<typename T>
CYIOptional< T >::~CYIOptional ( )

Member Function Documentation

◆ Emplace()

template<typename T>
template<typename... Args>
T& CYIOptional< T >::Emplace ( Args &&...  args)

Emplaces a value into this object, passing args to the appropriate constructor of T.

Note
If this object already contained a value, it is destroyed prior to emplacing a new value.

◆ GetValue() [1/3]

template<typename T>
T& CYIOptional< T >::GetValue ( ) &

Returns the value contained in this object.

Example:

if (auto optional = GetSubtitle())
{
subtitle = std::move(optional.GetValue());
}
Warning
It is necessary to verify that this object contains a value using CYIOptional::HasValue before calling this function.

◆ GetValue() [2/3]

template<typename T>
const T& CYIOptional< T >::GetValue ( ) const &

◆ GetValue() [3/3]

template<typename T>
T&& CYIOptional< T >::GetValue ( ) &&

◆ GetValueOr() [1/2]

template<typename T>
template<typename U >
constexpr T CYIOptional< T >::GetValueOr ( U &&  alternative) const &

If this object contains a value, this function returns that value. Otherwise, alternative is returned.

◆ GetValueOr() [2/2]

template<typename T>
template<typename U >
constexpr T CYIOptional< T >::GetValueOr ( U &&  alternative) &&

◆ HasValue()

template<typename T>
constexpr bool CYIOptional< T >::HasValue ( ) const

Returns true if this object contains a value, false otherwise.

Note
This function must be used prior to accessing the value contained within this optional object (unless the value is accessed using CYIOptional::GetValueOr).

◆ operator bool()

template<typename T>
constexpr CYIOptional< T >::operator bool ( ) const
explicit

◆ operator*() [1/3]

template<typename T>
T& CYIOptional< T >::operator* ( ) &

◆ operator*() [2/3]

template<typename T>
const T& CYIOptional< T >::operator* ( ) const &

◆ operator*() [3/3]

template<typename T>
T&& CYIOptional< T >::operator* ( ) &&

◆ operator->() [1/2]

template<typename T>
T* CYIOptional< T >::operator-> ( )

◆ operator->() [2/2]

template<typename T>
const T* CYIOptional< T >::operator-> ( ) const

◆ operator=() [1/4]

template<typename T>
YI_CONSTEXPR CYIOptional& CYIOptional< T >::operator= ( const CYIOptional< T > &  other)

Copy assignment operator. Copies the value contained in other into this object. If other is empty, this object is made empty.

◆ operator=() [2/4]

template<typename T>
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
YI_CONSTEXPR CYIOptional& CYIOptional< T >::operator= ( const U &  value)

Value copy assignment operator. Copies the value value into this object.

◆ operator=() [3/4]

template<typename T>
YI_CONSTEXPR CYIOptional& CYIOptional< T >::operator= ( CYIOptional< T > &&  other)

Move assignment operator. Moves the value contained in other into this object. If other is empty, this object is made empty.

Note
If other was non-empty, it remains non-empty (but contains an unspecified moved-from value).

◆ operator=() [4/4]

template<typename T>
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
YI_CONSTEXPR CYIOptional& CYIOptional< T >::operator= ( U &&  value)

Value move assignment operator. Moves the value value into this object.

◆ Reset()

template<typename T>
YI_CONSTEXPR void CYIOptional< T >::Reset ( )

Makes this object empty. If this object contains a value, it is destroyed.

◆ Swap()

template<typename T>
YI_CONSTEXPR void CYIOptional< T >::Swap ( CYIOptional< T > &  other)

Swaps the content of this object with that of other.

Friends And Related Function Documentation

◆ CYIOptional

template<typename T>
template<typename U >
friend class CYIOptional
friend

Member Data Documentation

◆ m_emptyValue

template<typename T>
char CYIOptional< T >::m_emptyValue

◆ m_value

template<typename T>
T CYIOptional< T >::m_value

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