iceoryx_hoofs 2.0.3
Public Types | Public Member Functions | List of all members
iox::cxx::optional< T > Class Template Reference

Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 standard and it can be used in factory functions which can fail. More...

#include <iceoryx_hoofs/cxx/optional.hpp>

Public Types

using type = T
 

Public Member Functions

 optional () noexcept
 Creates an optional which has no value. If you access such an optional via .value() or the arrow operator the behavior is undefined.
 
 optional (const nullopt_t &) noexcept
 Creates an optional which has no value. If you access such an optional via .value() or the arrow operator the behavior is defined in the cxx::Expects handling.
 
 optional (T &&value) noexcept
 Creates an optional by forwarding value to the constructor of T. This optional has a value. More...
 
 optional (const T &value) noexcept
 Creates an optional by using the copy constructor of T. More...
 
template<typename... Targs>
 optional (in_place_t, Targs &&... args) noexcept
 Creates an optional and an object inside the optional on construction by perfectly forwarding args to the constructor of T. Could be used e.g. when T is not copyable/movable. More...
 
 ~optional () noexcept
 The destructor will call the destructor of T if a value is set.
 
 optional (const optional &rhs) noexcept
 Constructs a value with the copy constructor if rhs has a value. Otherwise it contains no value. More...
 
 optional (optional &&rhs) noexcept
 Constructs a value with the move constructor if rhs has a value. Otherwise it contains no value. More...
 
optionaloperator= (const optional &rhs) noexcept
 Copies an optional. If the optional has a value then the copy assignment of that value is called. If the optional has no value a new value is constructed with the copy constructor. More...
 
optionaloperator= (optional &&rhs) noexcept
 Moves an optional. If the optional has a value then the move assignment of that value is called. If the optional has no value a new value is constructed with the move constructor. More...
 
constexpr bool operator== (const optional< T > &rhs) const noexcept
 If the optionals have values it compares these values by using their comparison operator. More...
 
constexpr bool operator== (const nullopt_t &) const noexcept
 Comparison with nullopt_t for easier unset optional comparison. More...
 
constexpr bool operator!= (const optional< T > &rhs) const noexcept
 If the optionals have values it compares these values by using their comparison operator. More...
 
constexpr bool operator!= (const nullopt_t &) const noexcept
 Comparision with nullopt_t for easier unset optional comparison. More...
 
template<typename U = T>
std::enable_if<!std::is_same< U, optional< T > & >::value, optional >::type & operator= (U &&value) noexcept
 Direct assignment of the underlying value. If the optional has no value then a new T is constructed by forwarding the assignment to T's constructor. If the optional has a value the assignment operator of T is called. More...
 
const T * operator-> () const noexcept
 Returns a pointer to the underlying value. If the optional has no value the behavior is undefined. You need to verify that the optional has a value by calling has_value() before using it. More...
 
const T & operator* () const noexcept
 Returns a reference to the underlying value. If the optional has no value the behavior is undefined. You need to verify that the optional has a value by calling has_value() before using it. More...
 
T * operator-> () noexcept
 Returns a pointer to the underlying value. If the optional has no value the behavior is undefined. You need to verify that the optional has a value by calling has_value() before using it. More...
 
T & operator* () noexcept
 Returns a reference to the underlying value. If the optional has no value the behavior is undefined. You need to verify that the optional has a value by calling has_value() before using it. More...
 
constexpr operator bool () const noexcept
 Will return true if the optional contains a value, otherwise false. More...
 
constexpr bool has_value () const noexcept
 Will return true if the optional contains a value, otherwise false. More...
 
template<typename... Targs>
T & emplace (Targs &&... args) noexcept
 A new element is constructed by forwarding the arguments to the constructor of T. If the optional has a value then the destructor of T is called. More...
 
void reset () noexcept
 Calls the destructor of T if the optional has a value. If the optional has no value, nothing happens. After that call the optional has no more value.
 
T & value () &noexcept
 Returns a reference to the underlying value. If the optional has no value the application terminates. You need to verify that the optional has a value by calling has_value() before using it. More...
 
const T & value () const &noexcept
 Returns a const reference to the underlying value. If the optional has no value the application terminates. You need to verify that the optional has a value by calling has_value() before using it. More...
 
T && value () &&noexcept
 Returns a rvalue reference to the underlying value. If the optional has no value the application terminates. You need to verify that the optional has a value by calling has_value() before using it. More...
 
const T && value () const &&noexcept
 Returns a const rvalue reference to the underlying value. If the optional has no value the application terminates. You need to verify that the optional has a value by calling has_value() before using it. More...
 
template<typename U >
constexpr T value_or (U &&default_value) const noexcept
 If the optional contains a value a copy of that value is returned, otherwise the default_value is returned. More...
 
optionaland_then (const cxx::function_ref< void(T &)> &callable) noexcept
 calls the provided callable with the optional value as arguments if the optional contains a value More...
 
const optionaland_then (const cxx::function_ref< void(const T &)> &callable) const noexcept
 calls the provided callable with the optional value as arguments if the optional contains a value More...
 
optionalor_else (const cxx::function_ref< void()> &callable) noexcept
 calls the provided callable if the optional does not contain a value More...
 
const optionalor_else (const cxx::function_ref< void()> &callable) const noexcept
 calls the provided callable if the optional does not contain a value More...
 

Detailed Description

template<typename T>
class iox::cxx::optional< T >

Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 standard and it can be used in factory functions which can fail.

#include "iceoryx_hoofs/cxx/optional.hpp"
cxx::optional<void*> SomeFactory() {
void *memory = malloc(1234);
if ( memory == nullptr )
return cxx::nullopt_t();
else
return cxx::make_optional<void*>(memory);
}
int main() {
auto var = SomeFactory();
// never forget the has_value call before working with an optional
if ( var.has_value() ) {
// do stuff with var
}
}
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:69
Helper struct which is used to signal an empty optional. It is equivalent to no value.
Definition: optional.hpp:34

Constructor & Destructor Documentation

◆ optional() [1/5]

template<typename T >
iox::cxx::optional< T >::optional ( T &&  value)
noexcept

Creates an optional by forwarding value to the constructor of T. This optional has a value.

Parameters
[in]valuervalue of type T which will be moved into the optional

◆ optional() [2/5]

template<typename T >
iox::cxx::optional< T >::optional ( const T &  value)
noexcept

Creates an optional by using the copy constructor of T.

Parameters
[in]valuelvalue of type T which will be copy constructed into the optional

◆ optional() [3/5]

template<typename T >
template<typename... Targs>
iox::cxx::optional< T >::optional ( in_place_t  ,
Targs &&...  args 
)
noexcept

Creates an optional and an object inside the optional on construction by perfectly forwarding args to the constructor of T. Could be used e.g. when T is not copyable/movable.

Template Parameters
Targsis the template parameter pack for the perfectly forwarded arguments
Parameters
[in]in_place_tcompile time variable to distinguish between constructors with certain behavior

◆ optional() [4/5]

template<typename T >
iox::cxx::optional< T >::optional ( const optional< T > &  rhs)
noexcept

Constructs a value with the copy constructor if rhs has a value. Otherwise it contains no value.

Parameters
[in]rhssource of the copy

◆ optional() [5/5]

template<typename T >
iox::cxx::optional< T >::optional ( optional< T > &&  rhs)
noexcept

Constructs a value with the move constructor if rhs has a value. Otherwise it contains no value.

Parameters
[in]rhssource of the move

Member Function Documentation

◆ and_then() [1/2]

template<typename T >
const optional & iox::cxx::optional< T >::and_then ( const cxx::function_ref< void(const T &)> &  callable) const
noexcept

calls the provided callable with the optional value as arguments if the optional contains a value

Parameters
[in]callablewhich has T as argument
Returns
reference to this

◆ and_then() [2/2]

template<typename T >
optional & iox::cxx::optional< T >::and_then ( const cxx::function_ref< void(T &)> &  callable)
noexcept

calls the provided callable with the optional value as arguments if the optional contains a value

Parameters
[in]callablewhich has T as argument
Returns
reference to this

◆ emplace()

template<typename T >
template<typename... Targs>
T & iox::cxx::optional< T >::emplace ( Targs &&...  args)
noexcept

A new element is constructed by forwarding the arguments to the constructor of T. If the optional has a value then the destructor of T is called.

Parameters
[in]perfectlyforwards args to the constructor of T to perform a placement new
Returns
reference to the underlying type

◆ has_value()

template<typename T >
constexpr bool iox::cxx::optional< T >::has_value ( ) const
constexprnoexcept

Will return true if the optional contains a value, otherwise false.

Returns
true if optional contains a value, otherwise false

◆ operator bool()

template<typename T >
constexpr iox::cxx::optional< T >::operator bool ( ) const
explicitconstexprnoexcept

Will return true if the optional contains a value, otherwise false.

Returns
true if optional contains a value, otherwise false

◆ operator!=() [1/2]

template<typename T >
constexpr bool iox::cxx::optional< T >::operator!= ( const nullopt_t ) const
constexprnoexcept

Comparision with nullopt_t for easier unset optional comparison.

Returns
true if the optional is set, otherwise false

◆ operator!=() [2/2]

template<typename T >
constexpr bool iox::cxx::optional< T >::operator!= ( const optional< T > &  rhs) const
constexprnoexcept

If the optionals have values it compares these values by using their comparison operator.

Parameters
[in]rhsvalue to which this optional should be compared to
Returns
true if the contained values are not equal, otherwise false

◆ operator*() [1/2]

template<typename T >
const T & iox::cxx::optional< T >::operator* ( ) const
noexcept

Returns a reference to the underlying value. If the optional has no value the behavior is undefined. You need to verify that the optional has a value by calling has_value() before using it.

Returns
reference of type const T to the underlying type

◆ operator*() [2/2]

template<typename T >
T & iox::cxx::optional< T >::operator* ( )
noexcept

Returns a reference to the underlying value. If the optional has no value the behavior is undefined. You need to verify that the optional has a value by calling has_value() before using it.

Returns
reference of type T to the underlying type

◆ operator->() [1/2]

template<typename T >
const T * iox::cxx::optional< T >::operator-> ( ) const
noexcept

Returns a pointer to the underlying value. If the optional has no value the behavior is undefined. You need to verify that the optional has a value by calling has_value() before using it.

Returns
pointer of type const T to the underlying type

◆ operator->() [2/2]

template<typename T >
T * iox::cxx::optional< T >::operator-> ( )
noexcept

Returns a pointer to the underlying value. If the optional has no value the behavior is undefined. You need to verify that the optional has a value by calling has_value() before using it.

Returns
pointer of type T to the underlying type

◆ operator=() [1/3]

template<typename T >
optional & iox::cxx::optional< T >::operator= ( const optional< T > &  rhs)
noexcept

Copies an optional. If the optional has a value then the copy assignment of that value is called. If the optional has no value a new value is constructed with the copy constructor.

Parameters
[in]rhssource of the copy
Returns
reference to the current optional

◆ operator=() [2/3]

template<typename T >
optional & iox::cxx::optional< T >::operator= ( optional< T > &&  rhs)
noexcept

Moves an optional. If the optional has a value then the move assignment of that value is called. If the optional has no value a new value is constructed with the move constructor.

Parameters
[in]rhssource of the move
Returns
reference to the current optional

◆ operator=() [3/3]

template<typename T >
template<typename U = T>
std::enable_if<!std::is_same< U, optional< T > & >::value, optional >::type & iox::cxx::optional< T >::operator= ( U &&  value)
noexcept

Direct assignment of the underlying value. If the optional has no value then a new T is constructed by forwarding the assignment to T's constructor. If the optional has a value the assignment operator of T is called.

Parameters
[in]valuevalue to assign to the underlying optional value
Returns
reference to the current optional

◆ operator==() [1/2]

template<typename T >
constexpr bool iox::cxx::optional< T >::operator== ( const nullopt_t ) const
constexprnoexcept

Comparison with nullopt_t for easier unset optional comparison.

Returns
true if the optional is unset, otherwise false

◆ operator==() [2/2]

template<typename T >
constexpr bool iox::cxx::optional< T >::operator== ( const optional< T > &  rhs) const
constexprnoexcept

If the optionals have values it compares these values by using their comparison operator.

Parameters
[in]rhsvalue to which this optional should be compared to
Returns
true if the contained values are equal, otherwise false

◆ or_else() [1/2]

template<typename T >
const optional & iox::cxx::optional< T >::or_else ( const cxx::function_ref< void()> &  callable) const
noexcept

calls the provided callable if the optional does not contain a value

Parameters
[in]callable
Returns
reference to this

◆ or_else() [2/2]

template<typename T >
optional & iox::cxx::optional< T >::or_else ( const cxx::function_ref< void()> &  callable)
noexcept

calls the provided callable if the optional does not contain a value

Parameters
[in]callable
Returns
reference to this

◆ value() [1/4]

template<typename T >
T && iox::cxx::optional< T >::value ( ) &&
noexcept

Returns a rvalue reference to the underlying value. If the optional has no value the application terminates. You need to verify that the optional has a value by calling has_value() before using it.

Returns
rvalue reference to the underlying type

◆ value() [2/4]

template<typename T >
T & iox::cxx::optional< T >::value ( ) &
noexcept

Returns a reference to the underlying value. If the optional has no value the application terminates. You need to verify that the optional has a value by calling has_value() before using it.

Returns
reference to the underlying type

◆ value() [3/4]

template<typename T >
const T && iox::cxx::optional< T >::value ( ) const &&
noexcept

Returns a const rvalue reference to the underlying value. If the optional has no value the application terminates. You need to verify that the optional has a value by calling has_value() before using it.

Returns
const rvalue reference to the underlying type

◆ value() [4/4]

template<typename T >
const T & iox::cxx::optional< T >::value ( ) const &
noexcept

Returns a const reference to the underlying value. If the optional has no value the application terminates. You need to verify that the optional has a value by calling has_value() before using it.

Returns
const reference to the underlying type

◆ value_or()

template<typename T >
template<typename U >
constexpr T iox::cxx::optional< T >::value_or ( U &&  default_value) const
constexprnoexcept

If the optional contains a value a copy of that value is returned, otherwise the default_value is returned.

Returns
copy of the underlying type if the optional has a value otherwise a copy of default_value

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