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:
This is an example of how a function that returns a CYIOptional value can be used:
#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) |
template<typename U = T, typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type> | |
constexpr | CYIOptional (U &&value) |
~CYIOptional () | |
YI_CONSTEXPR CYIOptional & | operator= (const CYIOptional &other) |
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type> | |
YI_CONSTEXPR CYIOptional & | operator= (const U &value) |
YI_CONSTEXPR CYIOptional & | operator= (CYIOptional &&other) |
template<typename U , typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type> | |
YI_CONSTEXPR CYIOptional & | operator= (U &&value) |
template<typename... Args> | |
T & | Emplace (Args &&... args) |
YI_CONSTEXPR bool | HasValue () const |
YI_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 > | |
YI_CONSTEXPR T | GetValueOr (U &&alternative) const & |
template<typename U > | |
YI_CONSTEXPR T | GetValueOr (U &&alternative) && |
YI_CONSTEXPR void | Reset () |
YI_CONSTEXPR void | Swap (CYIOptional &other) |
Friends | |
template<typename U > | |
class | CYIOptional |
constexpr CYIOptional< T >::CYIOptional | ( | ) |
Creates an empty optional instance.
constexpr CYIOptional< T >::CYIOptional | ( | CYIEmptyOptionalType | ) |
YI_CONSTEXPR CYIOptional< T >::CYIOptional | ( | const CYIOptional< T > & | other | ) |
Copy constructor. Copies other and its contained value (if any) into a new CYIOptional object.
YI_CONSTEXPR CYIOptional< T >::CYIOptional | ( | const CYIOptional< U > & | other | ) |
|
noexcept |
Move constructor. Moves the value contained in other (if any) into a new CYIOptional object.
YI_CONSTEXPR CYIOptional< T >::CYIOptional | ( | CYIOptional< U > && | other | ) |
constexpr CYIOptional< T >::CYIOptional | ( | U && | value | ) |
Value copy/move constructor. Copies or moves value into a new CYIOptional object.
CYIOptional< T >::~CYIOptional | ( | ) |
Emplaces a value into this object, passing args to the appropriate constructor of T.
T& CYIOptional< T >::GetValue | ( | ) | & |
Returns the value contained in this object.
Example:
const T& CYIOptional< T >::GetValue | ( | ) | const & |
T&& CYIOptional< T >::GetValue | ( | ) | && |
YI_CONSTEXPR T CYIOptional< T >::GetValueOr | ( | U && | alternative | ) | const & |
If this object contains a value, this function returns that value. Otherwise, alternative is returned.
YI_CONSTEXPR T CYIOptional< T >::GetValueOr | ( | U && | alternative | ) | && |
YI_CONSTEXPR bool CYIOptional< T >::HasValue | ( | ) | const |
Returns true if this object contains a value, false otherwise.
|
explicit |
T& CYIOptional< T >::operator* | ( | ) | & |
const T& CYIOptional< T >::operator* | ( | ) | const & |
T&& CYIOptional< T >::operator* | ( | ) | && |
T* CYIOptional< T >::operator-> | ( | ) |
const T* CYIOptional< T >::operator-> | ( | ) | const |
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.
YI_CONSTEXPR CYIOptional& CYIOptional< T >::operator= | ( | const U & | value | ) |
Value copy assignment operator. Copies the value value into this object.
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.
YI_CONSTEXPR CYIOptional& CYIOptional< T >::operator= | ( | U && | value | ) |
Value move assignment operator. Moves the value value into this object.
YI_CONSTEXPR void CYIOptional< T >::Reset | ( | ) |
Makes this object empty. If this object contains a value, it is destroyed.
YI_CONSTEXPR void CYIOptional< T >::Swap | ( | CYIOptional< T > & | other | ) |
Swaps the content of this object with that of other.
char CYIOptional< T >::m_emptyValue |
T CYIOptional< T >::m_value |