iceoryx_hoofs 2.0.3
Public Member Functions | Static Public Member Functions | List of all members
iox::cxx::expected< ErrorType > Class Template Reference

expected implementation from the C++20 proposal with C++11. The interface is inspired by the proposal but it has changes since we are not allowed to throw an exception. More...

#include <iceoryx_hoofs/cxx/expected.hpp>

Inheritance diagram for iox::cxx::expected< ErrorType >:
Inheritance graph
[legend]

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< void > &successValue) noexcept
 constructs an expected which is signaling success More...
 
 expected (const error< ErrorType > &errorValue) noexcept
 constructs an expected which is signaling an error and stores the error value provided by errorValue More...
 
 expected (error< ErrorType > &&errorValue) noexcept
 constructs an expected which is signaling an error and stores the error value provided by value More...
 
 operator bool () const noexcept
 returns true if the expected contains an error otherwise false More...
 
bool has_error () const noexcept
 returns true if the expected contains an error otherwise false More...
 
ErrorType & get_error () &noexcept
 returns a reference to the contained error value, if the expected does not contain an error this is undefined behavior More...
 
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 More...
 
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 More...
 
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 More...
 
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 More...
 
const expectedand_then (const cxx::function_ref< void()> &callable) const noexcept
 if the expected does contain a success value the given callable is called and a reference to the expected is given as an argument to the callable More...
 
expectedand_then (const cxx::function_ref< void()> &callable) noexcept
 if the expected does contain a success value the given callable is called and a reference to the expected is given as an argument to the callable More...
 

Static Public Member Functions

static expected create_value () noexcept
 creates an expected which is signaling success More...
 
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 lErrorType More...
 

Detailed Description

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

expected implementation from the C++20 proposal with C++11. The interface is inspired by the proposal but it has changes since we are not allowed to throw an exception.

Parameters
ErrorTypetype of the error which can be stored in the expected
cxx::expected<int, float> callMe() {
bool l_errorOccured;
// ... do stuff
if ( l_errorOccured ) {
return cxx::error<float>(55.1f);
} else if ( !l_errorOccured ) {
return cxx::success<int>(123);
}
}
cxx::expected<float> errorOnlyMethod() {
return callMe().or_else([]{
std::cerr << "Error Occured\n";
}).and_then([](cxx::expected<int, float> & result){
std::cout << "Success, got " << result.value() << std::endl;
});
}
cxx::expected<std::vector<int>, int> allHailHypnotoad(success<std::vector<int>>({6,6,6}));
allHailHypnotoad->push_back(7);
const expected & and_then(const cxx::function_ref< void()> &callable) const noexcept
if the expected does contain a success value the given callable is called and a reference to the expe...
helper struct to create an expected which is signalling an error more easily
Definition: expected.hpp:92
helper struct to create an expected which is signalling success more easily
Definition: expected.hpp:49

Constructor & Destructor Documentation

◆ expected() [1/3]

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

constructs an expected which is signaling success

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

◆ expected() [2/3]

template<typename ErrorType >
iox::cxx::expected< 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() [3/3]

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

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

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

Member Function Documentation

◆ and_then() [1/2]

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

if the expected does contain a success value the given callable is called and a reference to the expected 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 itself
someExpected.and_then([]{
std::cout << "we are successful!" << std::endl;
})

◆ and_then() [2/2]

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

if the expected does contain a success value the given callable is called and a reference to the expected 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 itself
someExpected.and_then([]{
std::cout << "we are successful!" << std::endl;
})

◆ create_error()

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

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

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

◆ create_value()

template<typename ErrorType >
static expected iox::cxx::expected< ErrorType >::create_value ( )
staticnoexcept

creates an expected which is signaling success

Returns
expected signalling success

◆ get_error() [1/3]

template<typename ErrorType >
ErrorType && iox::cxx::expected< 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 ErrorType >
ErrorType & iox::cxx::expected< 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 ErrorType >
const ErrorType & iox::cxx::expected< 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 ErrorType >
bool iox::cxx::expected< 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

◆ operator bool()

template<typename ErrorType >
iox::cxx::expected< 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

◆ or_else() [1/2]

template<typename ErrorType >
const expected & iox::cxx::expected< 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& error){
std::cout << "error occured : " << error << std::endl;
})

◆ or_else() [2/2]

template<typename ErrorType >
expected & iox::cxx::expected< 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
const reference to the expected itself
someExpected.or_else([](float& error){
std::cout << "error occured : " << error << std::endl;
})

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