17 #ifndef IOX_UTILS_CXX_EXPECTED_HPP
18 #define IOX_UTILS_CXX_EXPECTED_HPP
20 #include "iceoryx_utils/cxx/attributes.hpp"
21 #include "iceoryx_utils/cxx/function_ref.hpp"
22 #include "iceoryx_utils/cxx/optional.hpp"
23 #include "iceoryx_utils/cxx/variant.hpp"
35 template <
typename,
typename =
void>
36 struct HasInvalidStateMember;
39 template <
typename...>
49 "T must have an INVALID_STATE value/member. Alternatively write an ErrorTypeAdapter specialisation "
52 static T getInvalidState() noexcept;
63 template <
typename T =
void>
75 template <
typename... Targs>
81 success(Targs&&... args) noexcept;
106 template <
typename T>
112 error(
const T& t) noexcept;
117 error(T&& t) noexcept;
123 template <
typename... Targs>
124 error(Targs&&... args) noexcept;
129 template <
typename... T>
130 class IOX_NO_DISCARD expected;
161 template <
typename ErrorType>
162 class IOX_NO_DISCARD expected<ErrorType>
180 template <
typename ValueType>
185 template <
typename ValueType>
203 template <
typename ValueType>
208 template <
typename ValueType>
228 static expected create_value() noexcept;
234 template <typename... Targs>
235 static expected create_error(Targs&&... args) noexcept;
239 explicit operator
bool() const noexcept;
243 bool has_error() const noexcept;
248 ErrorType& get_error() & noexcept;
253 const ErrorType& get_error() const& noexcept;
258 ErrorType&& get_error() && noexcept;
269 const expected& or_else(const cxx::
function_ref<
void(ErrorType&)>& callable) const noexcept;
280 expected& or_else(const cxx::
function_ref<
void(ErrorType&)>& callable) noexcept;
291 const expected& and_then(const cxx::
function_ref<
void()>& callable) const noexcept;
302 expected& and_then(const cxx::
function_ref<
void()>& callable) noexcept;
305 expected(
variant<ErrorType>&& store, const
bool hasError) noexcept;
308 static constexpr uint64_t ERROR_INDEX = 0U;
314 template <typename ValueType, typename ErrorType>
315 class IOX_NO_DISCARD expected<ValueType, ErrorType>
336 expected& operator=(const expected&) noexcept;
340 expected& operator=(expected&& rhs) noexcept;
345 expected(const
success<ValueType>& successValue) noexcept;
350 expected(
success<ValueType>&& successValue) noexcept;
355 expected(const
error<ErrorType>& errorValue) noexcept;
360 expected(
error<ErrorType>&& errorValue) noexcept;
366 template <typename... Targs>
367 static expected create_value(Targs&&... args) noexcept;
373 template <typename... Targs>
374 static expected create_error(Targs&&... args) noexcept;
378 explicit operator
bool() const noexcept;
382 bool has_error() const noexcept;
387 ErrorType& get_error() & noexcept;
392 const ErrorType& get_error() const& noexcept;
397 ErrorType&& get_error() && noexcept;
402 ValueType& value() & noexcept;
407 const ValueType& value() const& noexcept;
412 ValueType&& value() && noexcept;
417 ValueType value_or(const ValueType& value) const noexcept;
422 ValueType value_or(const ValueType& value) noexcept;
434 ValueType& operator*() noexcept;
445 const ValueType& operator*() const noexcept;
454 ValueType* operator->() noexcept;
463 const ValueType* operator->() const noexcept;
476 template <typename T>
477 operator expected<T>() noexcept;
490 template <typename T>
491 operator expected<T>() const noexcept;
502 const expected& or_else(const cxx::
function_ref<
void(ErrorType&)>& callable) const noexcept;
513 expected& or_else(const cxx::
function_ref<
void(ErrorType&)>& callable) noexcept;
524 const expected& and_then(const cxx::
function_ref<
void(ValueType&)>& callable) const noexcept;
535 expected& and_then(const cxx::
function_ref<
void(ValueType&)>& callable) noexcept;
548 template <typename Optional = ValueType,
549 typename std::enable_if<internal::IsOptional<Optional>::value,
int>::type = 0>
550 const expected& and_then(const cxx::
function_ref<
void(typename Optional::type&)>& callable) const noexcept;
563 template <typename Optional = ValueType,
564 typename std::enable_if<internal::IsOptional<Optional>::value,
int>::type = 0>
565 expected& and_then(const cxx::
function_ref<
void(typename Optional::type&)>& callable) noexcept;
580 template <typename Optional = ValueType,
581 typename std::enable_if<internal::IsOptional<Optional>::value,
int>::type = 0>
582 [[deprecated]] const expected& if_empty(const cxx::
function_ref<
void()>& callable) const noexcept;
597 template <typename Optional = ValueType,
598 typename std::enable_if<internal::IsOptional<Optional>::value,
int>::type = 0>
599 [[deprecated]] expected& if_empty(const cxx::
function_ref<
void()>& callable) noexcept;
601 optional<ValueType> to_optional() const noexcept;
604 expected(
variant<ValueType, ErrorType>&& f_store, const
bool hasError) noexcept;
605 variant<ValueType, ErrorType> m_store;
607 static constexpr uint64_t VALUE_INDEX = 0U;
608 static constexpr uint64_t ERROR_INDEX = 1U;
611 template <typename ErrorType>
612 class IOX_NO_DISCARD expected<
void, ErrorType> : public expected<ErrorType>
622 #include "iceoryx_utils/internal/cxx/expected.inl"
expected(const expected &) noexcept=default
the copy constructor calls the copy constructor of the contained success value or the error value - d...
~expected()=default
calls the destructor of the success value or error value - depending on what is stored in the expecte...
expected & operator=(expected &&rhs) noexcept
calls the move assignment operator of the contained success value or the error value - depending on w...
expected(expected &&rhs) noexcept
the move constructor calls the move constructor of the contained success value or the error value - d...
expected & operator=(const expected &)
calls the copy assignment operator of the contained success value or the error value - depending on w...
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:316
expected(expected &&rhs) noexcept
the move constructor calls the move constructor of the contained success value or the error value - d...
expected(const expected &)=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:32
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:63
Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 sta...
Definition: variant.hpp:105
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Generic adapter to access INVALID_STATE member or value.
Definition: expected.hpp:47
helper struct to create an expected which is signalling an error more easily
Definition: expected.hpp:108
error(const T &t) noexcept
constructor which creates a error helper class by copying the value of t
Definition: expected.inl:71
Type trait which verifies whether the passed type T has INVALID_STATE std::true_type overload chosen ...
Definition: expected.inl:30
helper struct to create an error only expected which is signalling success more easily
Definition: expected.hpp:95
helper struct to create an expected which is signalling success more easily
Definition: expected.hpp:65