17#ifndef IOX_HOOFS_CXX_EXPECTED_HPP
18#define IOX_HOOFS_CXX_EXPECTED_HPP
20#include "iceoryx_hoofs/cxx/attributes.hpp"
21#include "iceoryx_hoofs/cxx/function_ref.hpp"
22#include "iceoryx_hoofs/cxx/helplets.hpp"
23#include "iceoryx_hoofs/cxx/optional.hpp"
24#include "iceoryx_hoofs/cxx/variant.hpp"
47template <
typename T =
void>
59 template <
typename... Targs>
107 template <
typename... Targs>
113template <
typename... T>
114class IOX_NO_DISCARD expected;
145template <
typename ErrorType>
146class IOX_NO_DISCARD expected<ErrorType>
164 template <
typename ValueType>
169 template <
typename ValueType>
178 expected& operator=(const expected&) noexcept;
182 expected& operator=(expected&& rhs) noexcept;
187 template <
typename ValueType>
192 template <
typename ValueType>
218 template <typename... Targs>
219 static expected create_error(Targs&&... args) noexcept;
223 explicit operator
bool() const noexcept;
227 bool has_error() const noexcept;
232 ErrorType& get_error() & noexcept;
237 const ErrorType& get_error() const& noexcept;
242 ErrorType&& get_error() && noexcept;
253 const expected& or_else(const cxx::
function_ref<
void(ErrorType&)>& callable) const noexcept;
264 expected& or_else(const cxx::
function_ref<
void(ErrorType&)>& callable) noexcept;
275 const expected& and_then(const cxx::
function_ref<
void()>& callable) const noexcept;
286 expected& and_then(const cxx::
function_ref<
void()>& callable) noexcept;
289 expected(
variant<ErrorType>&& store, const
bool hasError) noexcept;
292 static constexpr uint64_t ERROR_INDEX = 0U;
298template <typename ValueType, typename ErrorType>
299class IOX_NO_DISCARD expected<ValueType, ErrorType>
320 expected& operator=(const expected&) noexcept;
324 expected& operator=(expected&& rhs) noexcept;
329 expected(const
success<ValueType>& successValue) noexcept;
334 expected(
success<ValueType>&& successValue) noexcept;
339 expected(const
error<ErrorType>& errorValue) noexcept;
344 expected(
error<ErrorType>&& errorValue) noexcept;
350 template <typename... Targs>
351 static expected create_value(Targs&&... args) noexcept;
357 template <typename... Targs>
358 static expected create_error(Targs&&... args) noexcept;
362 explicit operator
bool() const noexcept;
366 bool has_error() const noexcept;
371 ErrorType& get_error() & noexcept;
376 const ErrorType& get_error() const& noexcept;
381 ErrorType&& get_error() && noexcept;
386 ValueType& value() & noexcept;
391 const ValueType& value() const& noexcept;
396 ValueType&& value() && noexcept;
401 ValueType value_or(const ValueType& value) const noexcept;
406 ValueType value_or(const ValueType& value) noexcept;
418 ValueType& operator*() noexcept;
429 const ValueType& operator*() const noexcept;
438 ValueType* operator->() noexcept;
447 const ValueType* operator->() const noexcept;
460 template <typename T>
461 operator expected<T>() noexcept;
474 template <typename T>
475 operator expected<T>() const noexcept;
486 const expected& or_else(const cxx::
function_ref<
void(ErrorType&)>& callable) const noexcept;
497 expected& or_else(const cxx::
function_ref<
void(ErrorType&)>& callable) noexcept;
508 const expected& and_then(const cxx::
function_ref<
void(ValueType&)>& callable) const noexcept;
519 expected& and_then(const cxx::
function_ref<
void(ValueType&)>& callable) noexcept;
532 template <typename Optional = ValueType,
533 typename std::enable_if<internal::IsOptional<Optional>::value,
int>::type = 0>
534 const expected& and_then(const cxx::
function_ref<
void(typename Optional::type&)>& callable) const noexcept;
547 template <typename Optional = ValueType,
548 typename std::enable_if<internal::IsOptional<Optional>::value,
int>::type = 0>
549 expected& and_then(const cxx::
function_ref<
void(typename Optional::type&)>& callable) noexcept;
564 template <typename Optional = ValueType,
565 typename std::enable_if<internal::IsOptional<Optional>::value,
int>::type = 0>
566 [[deprecated]] const expected& if_empty(const cxx::
function_ref<
void()>& callable) const noexcept;
581 template <typename Optional = ValueType,
582 typename std::enable_if<internal::IsOptional<Optional>::value,
int>::type = 0>
583 [[deprecated]] expected& if_empty(const cxx::
function_ref<
void()>& callable) noexcept;
585 optional<ValueType> to_optional() const noexcept;
588 expected(
variant<ValueType, ErrorType>&& f_store, const
bool hasError) noexcept;
589 variant<ValueType, ErrorType> m_store;
591 static constexpr uint64_t VALUE_INDEX = 0U;
592 static constexpr uint64_t ERROR_INDEX = 1U;
595template <typename ErrorType>
596class IOX_NO_DISCARD expected<
void, ErrorType> : public expected<ErrorType>
599 using expected<ErrorType>::expected;
606#include "iceoryx_hoofs/internal/cxx/expected.inl"
expected(const success< void > &successValue) noexcept
constructs an expected which is signaling success
expected(error< ErrorType > &&errorValue) noexcept
constructs an expected which is signaling an error and stores the error value provided by value
static expected create_value() noexcept
creates an expected which is signaling success
~expected() noexcept=default
calls the destructor of the success value or error value - depending on what is stored in the expecte...
expected(const expected &) noexcept=default
the copy constructor calls the copy constructor of the contained success value or the error value - d...
expected(expected &&rhs) noexcept
the move constructor calls the move constructor of the contained success value or the error value - d...
expected(const error< ErrorType > &errorValue) noexcept
constructs an expected which is signaling an error and stores the error value provided by errorValue
expected()=delete
default ctor is deleted since you have to clearly state if the expected contains a success value or a...
specialization of the expected class which can contain an error as well as a success value
Definition: expected.hpp:300
expected(expected &&rhs) noexcept
the move constructor calls the move constructor of the contained success value or the error value - d...
expected(const expected &) noexcept=default
the copy constructor calls the copy constructor of the contained success value or the error value - d...
~expected() noexcept=default
calls the destructor of the success value or error value - depending on what is stored in the expecte...
expected()=delete
default ctor is deleted since you have to clearly state if the expected contains a success value or a...
Definition: function_ref.hpp:34
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:69
Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 sta...
Definition: variant.hpp:107
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29
helper struct to create an expected which is signalling an error more easily
Definition: expected.hpp:92
error(T &&t) noexcept
constructor which creates a error helper class by moving the value of t
error(const T &t) noexcept
constructor which creates a error helper class by copying the value of t
error(Targs &&... args) noexcept
constructor which creates a error helper class by forwarding arguments to the constructor of T
helper struct to create an error only expected which is signalling success more easily
Definition: expected.hpp:79
helper struct to create an expected which is signalling success more easily
Definition: expected.hpp:49
success(const T &t) noexcept
constructor which creates a success helper class by copying the value of t
success(T &&t) noexcept
constructor which creates a success helper class by moving the value of t
success(Targs &&... args) noexcept
constructor which creates a success helper class by forwarding arguments to the constructor of T