iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
iox::cxx::expected< ValueType, ErrorType > Class Template Reference

specialization of the expected class which can contain an error as well as a success value More...

#include <iceoryx_hoofs/cxx/expected.hpp>

Public Member Functions

 expected ()=delete
 default ctor is deleted since you have to clearly state if the expected contains a success value or an error value
 
 expected (const expected &) noexcept=default
 the copy constructor calls the copy constructor of the contained success value or the error value - depending on what is stored in the expected
 
 expected (expected &&rhs) noexcept
 the move constructor calls the move constructor of the contained success value or the error value - depending on what is stored in the expected
 
 ~expected () noexcept=default
 calls the destructor of the success value or error value - depending on what is stored in the expected
 
expectedoperator= (const expected &) noexcept
 calls the copy assignment operator of the contained success value or the error value - depending on what is stored in the expected
 
expectedoperator= (expected &&rhs) noexcept
 calls the move assignment operator of the contained success value or the error value - depending on what is stored in the expected
 
 expected (const success< ValueType > &successValue) noexcept
 constructs an expected which is signaling success and uses the value provided by successValue to copy construct its success value
 
 expected (success< ValueType > &&successValue) noexcept
 constructs an expected which is signaling success and uses the value provided by successValue to move construct its success value
 
 expected (const error< ErrorType > &errorValue) noexcept
 constructs an expected which is signaling an error and stores the error value provided by errorValue
 
 expected (error< ErrorType > &&errorValue) noexcept
 constructs an expected which is signaling an error and stores the error value provided by errorValue
 
 operator bool () const noexcept
 returns true if the expected contains an error otherwise false
 
bool has_error () const noexcept
 returns true if the expected contains an error otherwise false
 
ErrorType & get_error () &noexcept
 returns a reference to the contained error value, if the expected does not contain an error this is undefined behavior
 
const ErrorType & get_error () const &noexcept
 returns a const reference to the contained error value, if the expected does not contain an error this is undefined behavior
 
ErrorType && get_error () &&noexcept
 returns a rvalue reference to the contained error value, if the expected does not contain an error this is undefined behavior
 
ValueType & value () &noexcept
 returns a reference to the contained success value, if the expected does not contain a success value this is undefined behavior
 
const ValueType & value () const &noexcept
 returns a const reference to the contained success value, if the expected does not contain a success value this is undefined behavior
 
ValueType && value () &&noexcept
 returns a reference to the contained success value, if the expected does not contain a success value this is undefined behavior
 
ValueType value_or (const ValueType &value) const noexcept
 returns a copy of the contained success value if the expected does contain a success value, otherwise it returns a copy of value
 
ValueType value_or (const ValueType &value) noexcept
 returns a copy of the contained success value if the expected does contain a success value, otherwise it returns a copy of value
 
ValueType & operator* () noexcept
 dereferencing operator which returns a reference to the contained success value. if the expected contains an error the behavior is undefined.
 
const ValueType & operator* () const noexcept
 dereferencing operator which returns a reference to the contained success value. if the expected contains an error the behavior is undefined.
 
ValueType * operator-> () noexcept
 arrow operator which returns the pointer to the contained success value. if the expected contains an error the behavior is undefined.
 
const ValueType * operator-> () const noexcept
 arrow operator which returns the pointer to the contained success value. if the expected contains an error the behavior is undefined.
 
template<typename T >
 operator expected< T > () noexcept
 conversion operator to an error only expected which can be useful if you would like to return only the success of a function
 
template<typename T >
 operator expected< T > () const noexcept
 conversion operator to an error only expected which can be useful if you would like to return only the success of a function
 
const expectedor_else (const cxx::function_ref< void(ErrorType &)> &callable) const noexcept
 if the expected does contain an error the given callable is called and a reference to the ErrorType is given as an argument to the callable
 
expectedor_else (const cxx::function_ref< void(ErrorType &)> &callable) noexcept
 if the expected does contain an error the given callable is called and a reference to the ErrorType is given as an argument to the callable
 
const expectedand_then (const cxx::function_ref< void(ValueType &)> &callable) const noexcept
 if the expected does contain a success value the given callable is called and a reference to the result is given as an argument to the callable
 
expectedand_then (const cxx::function_ref< void(ValueType &)> &callable) noexcept
 if the expected does contain a success value the given callable is called and a reference to the result is given as an argument to the callable
 
template<typename Optional = ValueType, typename std::enable_if< internal::IsOptional< Optional >::value, int >::type = 0>
const expectedand_then (const cxx::function_ref< void(typename Optional::type &)> &callable) const noexcept
 if the expected contains a success value and its type is a non-empty optional, retrieve the value from the optional and provide it as the argument to the provided callable
 
template<typename Optional = ValueType, typename std::enable_if< internal::IsOptional< Optional >::value, int >::type = 0>
expectedand_then (const cxx::function_ref< void(typename Optional::type &)> &callable) noexcept
 if the expected contains a success value and its type is a non-empty optional, retrieve the value from the optional and provide it as the argument to the provided callable
 
template<typename Optional = ValueType, typename std::enable_if< internal::IsOptional< Optional >::value, int >::type = 0>
const expectedif_empty (const cxx::function_ref< void()> &callable) const noexcept
 if the expected contains a success value and its type is an empty optional, calls the provided callable
 
template<typename Optional = ValueType, typename std::enable_if< internal::IsOptional< Optional >::value, int >::type = 0>
expectedif_empty (const cxx::function_ref< void()> &callable) noexcept
 if the expected contains a success value and its type is an empty optional, calls the provided callable
 
optional< ValueType > to_optional () const noexcept
 

Static Public Member Functions

template<typename... Targs>
static expected create_value (Targs &&... args) noexcept
 creates an expected which is signaling success and perfectly forwards the args to the constructor of ValueType
 
template<typename... Targs>
static expected create_error (Targs &&... args) noexcept
 creates an expected which is signaling an error and perfectly forwards the args to the constructor of ErrorType
 

Detailed Description

template<typename ValueType, typename ErrorType>
class iox::cxx::expected< ValueType, ErrorType >

specialization of the expected class which can contain an error as well as a success value

Parameters
ValueTypetype of the value which can be stored in the expected
ErrorTypetype of the error which can be stored in the expected

Constructor & Destructor Documentation

◆ expected() [1/4]

template<typename ValueType , typename ErrorType >
iox::cxx::expected< ValueType, ErrorType >::expected ( const success< ValueType > &  successValue)
noexcept

constructs an expected which is signaling success and uses the value provided by successValue to copy construct its success value

Parameters
[in]successValuevalue which will be stored in the expected

◆ expected() [2/4]

template<typename ValueType , typename ErrorType >
iox::cxx::expected< ValueType, ErrorType >::expected ( success< ValueType > &&  successValue)
noexcept

constructs an expected which is signaling success and uses the value provided by successValue to move construct its success value

Parameters
[in]successValuevalue which will be moved into the expected

◆ expected() [3/4]

template<typename ValueType , typename ErrorType >
iox::cxx::expected< ValueType, ErrorType >::expected ( const error< ErrorType > &  errorValue)
noexcept

constructs an expected which is signaling an error and stores the error value provided by errorValue

Parameters
[in]errorValueerror value which will be stored in the expected

◆ expected() [4/4]

template<typename ValueType , typename ErrorType >
iox::cxx::expected< ValueType, ErrorType >::expected ( error< ErrorType > &&  errorValue)
noexcept

constructs an expected which is signaling an error and stores the error value provided by errorValue

Parameters
[in]errorValueerror value which will be moved into the expected

Member Function Documentation

◆ and_then() [1/4]

template<typename ValueType , typename ErrorType >
template<typename Optional = ValueType, typename std::enable_if< internal::IsOptional< Optional >::value, int >::type = 0>
const expected & iox::cxx::expected< ValueType, ErrorType >::and_then ( const cxx::function_ref< void(typename Optional::type &)> &  callable) const
noexcept

if the expected contains a success value and its type is a non-empty optional, retrieve the value from the optional and provide it as the argument to the provided callable

Parameters
[in]callablethe callable to be called with the contents of the optional
Returns
reference to the expected
anExpectedOptional.and_then([](int& value){
std::cout << "the optional contains the value: " << result << std::endl;
})
ValueType & value() &noexcept
returns a reference to the contained success value, if the expected does not contain a success value ...

◆ and_then() [2/4]

template<typename ValueType , typename ErrorType >
template<typename Optional = ValueType, typename std::enable_if< internal::IsOptional< Optional >::value, int >::type = 0>
expected & iox::cxx::expected< ValueType, ErrorType >::and_then ( const cxx::function_ref< void(typename Optional::type &)> &  callable)
noexcept

if the expected contains a success value and its type is a non-empty optional, retrieve the value from the optional and provide it as the argument to the provided callable

Parameters
[in]callablethe callable to be called with the contents of the optional
Returns
reference to the expected
anExpectedOptional.and_then([](int& value){
std::cout << "the optional contains the value: " << result << std::endl;
})

◆ and_then() [3/4]

template<typename ValueType , typename ErrorType >
const expected & iox::cxx::expected< ValueType, ErrorType >::and_then ( const cxx::function_ref< void(ValueType &)> &  callable) const
noexcept

if the expected does contain a success value the given callable is called and a reference to the result is given as an argument to the callable

Parameters
[in]callablecallable which will be called if the expected contains a success value
Returns
const reference to the expected
someExpected.and_then([](int& result){
std::cout << "we have a result : " << result << std::endl;
})

◆ and_then() [4/4]

template<typename ValueType , typename ErrorType >
expected & iox::cxx::expected< ValueType, ErrorType >::and_then ( const cxx::function_ref< void(ValueType &)> &  callable)
noexcept

if the expected does contain a success value the given callable is called and a reference to the result is given as an argument to the callable

Parameters
[in]callablecallable which will be called if the expected contains a success value
Returns
reference to the expected
someExpected.and_then([](int& result){
std::cout << "we have a result : " << result << std::endl;
})

◆ create_error()

template<typename ValueType , typename ErrorType >
template<typename... Targs>
static expected iox::cxx::expected< ValueType, ErrorType >::create_error ( Targs &&...  args)
staticnoexcept

creates an expected which is signaling an error and perfectly forwards the args to the constructor of ErrorType

Parameters
[in]args...arguments which will be forwarded to the ErrorType constructor
Returns
expected signalling error

◆ create_value()

template<typename ValueType , typename ErrorType >
template<typename... Targs>
static expected iox::cxx::expected< ValueType, ErrorType >::create_value ( Targs &&...  args)
staticnoexcept

creates an expected which is signaling success and perfectly forwards the args to the constructor of ValueType

Parameters
[in]args...arguments which will be forwarded to the ValueType constructor
Returns
expected signalling success

◆ get_error() [1/3]

template<typename ValueType , typename ErrorType >
ErrorType && iox::cxx::expected< ValueType, ErrorType >::get_error ( ) &&
noexcept

returns a rvalue reference to the contained error value, if the expected does not contain an error this is undefined behavior

Returns
rvalue reference to the internally contained error

◆ get_error() [2/3]

template<typename ValueType , typename ErrorType >
ErrorType & iox::cxx::expected< ValueType, ErrorType >::get_error ( ) &
noexcept

returns a reference to the contained error value, if the expected does not contain an error this is undefined behavior

Returns
reference to the internally contained error

◆ get_error() [3/3]

template<typename ValueType , typename ErrorType >
const ErrorType & iox::cxx::expected< ValueType, ErrorType >::get_error ( ) const &
noexcept

returns a const reference to the contained error value, if the expected does not contain an error this is undefined behavior

Returns
const reference to the internally contained error

◆ has_error()

template<typename ValueType , typename ErrorType >
bool iox::cxx::expected< ValueType, ErrorType >::has_error ( ) const
noexcept

returns true if the expected contains an error otherwise false

Returns
bool which contains true if the expected contains an error

◆ if_empty() [1/2]

template<typename ValueType , typename ErrorType >
template<typename Optional = ValueType, typename std::enable_if< internal::IsOptional< Optional >::value, int >::type = 0>
const expected & iox::cxx::expected< ValueType, ErrorType >::if_empty ( const cxx::function_ref< void()> &  callable) const
noexcept

if the expected contains a success value and its type is an empty optional, calls the provided callable

Parameters
[in]callablethe callable to be called if the contained optional is empty
Returns
reference to the expected
anExpectedOptional.and_then([](SomeType& value){
std::cout << "we got something in the optional: " << value << std::endl;
})
.if_empty([](){
std::cout << "the optional was empty, but do something anyway!" << result << std::endl;
})
const expected & if_empty(const cxx::function_ref< void()> &callable) const noexcept
if the expected contains a success value and its type is an empty optional, calls the provided callab...

◆ if_empty() [2/2]

template<typename ValueType , typename ErrorType >
template<typename Optional = ValueType, typename std::enable_if< internal::IsOptional< Optional >::value, int >::type = 0>
expected & iox::cxx::expected< ValueType, ErrorType >::if_empty ( const cxx::function_ref< void()> &  callable)
noexcept

if the expected contains a success value and its type is an empty optional, calls the provided callable

Parameters
[in]callablethe callable to be called if the contained optional is empty
Returns
reference to the expected
anExpectedOptional.and_then([](SomeType& value){
std::cout << "we got something in the optional: " << value << std::endl;
})
.if_empty([](){
std::cout << "the optional was empty, but do something anyway!" << result << std::endl;
})

◆ operator bool()

template<typename ValueType , typename ErrorType >
iox::cxx::expected< ValueType, ErrorType >::operator bool ( ) const
explicitnoexcept

returns true if the expected contains an error otherwise false

Returns
bool which contains true if the expected contains an error

◆ operator expected< T >() [1/2]

template<typename ValueType , typename ErrorType >
template<typename T >
iox::cxx::expected< ValueType, ErrorType >::operator expected< T > ( ) const
noexcept

conversion operator to an error only expected which can be useful if you would like to return only the success of a function

Returns
converts an expected which can contain a value and an error to an expected which contains only an error
cxx::expected<int, int> someErrorProneFunction(){}
cxx::expected<int> isItSuccessful() {
return someErrorProneFunction();
}

◆ operator expected< T >() [2/2]

template<typename ValueType , typename ErrorType >
template<typename T >
iox::cxx::expected< ValueType, ErrorType >::operator expected< T > ( )
noexcept

conversion operator to an error only expected which can be useful if you would like to return only the success of a function

Returns
converts an expected which can contain a value and an error to an expected which contains only an error
cxx::expected<int, int> someErrorProneFunction(){}
cxx::expected<int> isItSuccessful() {
return someErrorProneFunction();
}

◆ operator*() [1/2]

template<typename ValueType , typename ErrorType >
const ValueType & iox::cxx::expected< ValueType, ErrorType >::operator* ( ) const
noexcept

dereferencing operator which returns a reference to the contained success value. if the expected contains an error the behavior is undefined.

Returns
const reference to the contained value
cxx::expected<int, float> frodo(success<int>(45));
*frodo += 12;
std::cout << *frodo << std::endl; // prints 57
helper struct to create an expected which is signalling success more easily
Definition expected.hpp:49

◆ operator*() [2/2]

template<typename ValueType , typename ErrorType >
ValueType & iox::cxx::expected< ValueType, ErrorType >::operator* ( )
noexcept

dereferencing operator which returns a reference to the contained success value. if the expected contains an error the behavior is undefined.

Returns
reference to the contained value
cxx::expected<int, float> frodo(success<int>(45));
*frodo += 12;
std::cout << *frodo << std::endl; // prints 57

◆ operator->() [1/2]

template<typename ValueType , typename ErrorType >
const ValueType * iox::cxx::expected< ValueType, ErrorType >::operator-> ( ) const
noexcept

arrow operator which returns the pointer to the contained success value. if the expected contains an error the behavior is undefined.

Returns
pointer of type const ValueType to the contained value
cxx::expected<std::vector<int>, int> holyPiotr(success<std::vector<int>>({1,2,3}));
holyPiotr->push_back(4);

◆ operator->() [2/2]

template<typename ValueType , typename ErrorType >
ValueType * iox::cxx::expected< ValueType, ErrorType >::operator-> ( )
noexcept

arrow operator which returns the pointer to the contained success value. if the expected contains an error the behavior is undefined.

Returns
pointer of type ValueType to the contained value
cxx::expected<std::vector<int>, int> holyPiotr(success<std::vector<int>>({1,2,3}));
holyPiotr->push_back(4);

◆ or_else() [1/2]

template<typename ValueType , typename ErrorType >
const expected & iox::cxx::expected< ValueType, ErrorType >::or_else ( const cxx::function_ref< void(ErrorType &)> &  callable) const
noexcept

if the expected does contain an error the given callable is called and a reference to the ErrorType is given as an argument to the callable

Parameters
[in]callablecallable which will be called if the expected contains an error
Returns
const reference to the expected itself
someExpected.or_else([](float& result){
std::cout << "error occured : " << error << std::endl;
})
helper struct to create an expected which is signalling an error more easily
Definition expected.hpp:92

◆ or_else() [2/2]

template<typename ValueType , typename ErrorType >
expected & iox::cxx::expected< ValueType, ErrorType >::or_else ( const cxx::function_ref< void(ErrorType &)> &  callable)
noexcept

if the expected does contain an error the given callable is called and a reference to the ErrorType is given as an argument to the callable

Parameters
[in]callablecallable which will be called if the expected contains an error
Returns
reference to the expected itself
someExpected.or_else([](float& error){
std::cout << "error occured : " << error << std::endl;
})

◆ value() [1/3]

template<typename ValueType , typename ErrorType >
ValueType && iox::cxx::expected< ValueType, ErrorType >::value ( ) &&
noexcept

returns a reference to the contained success value, if the expected does not contain a success value this is undefined behavior

Returns
rvalue reference to the internally contained value

◆ value() [2/3]

template<typename ValueType , typename ErrorType >
ValueType & iox::cxx::expected< ValueType, ErrorType >::value ( ) &
noexcept

returns a reference to the contained success value, if the expected does not contain a success value this is undefined behavior

Returns
reference to the internally contained value

◆ value() [3/3]

template<typename ValueType , typename ErrorType >
const ValueType & iox::cxx::expected< ValueType, ErrorType >::value ( ) const &
noexcept

returns a const reference to the contained success value, if the expected does not contain a success value this is undefined behavior

Returns
const reference to the internally contained value

◆ value_or() [1/2]

template<typename ValueType , typename ErrorType >
ValueType iox::cxx::expected< ValueType, ErrorType >::value_or ( const ValueType &  value) const
noexcept

returns a copy of the contained success value if the expected does contain a success value, otherwise it returns a copy of value

Returns
copy of the internally contained value or copy of value

◆ value_or() [2/2]

template<typename ValueType , typename ErrorType >
ValueType iox::cxx::expected< ValueType, ErrorType >::value_or ( const ValueType &  value)
noexcept

returns a copy of the contained success value if the expected does contain a success value, otherwise it returns a copy of value

Returns
copy of the internally contained value or copy of value

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