261 typedef TValue value_type;
262 typedef TError error_type;
266 template <
typename U>
281 ETL_CONSTEXPR14
expected(
const value_type& value_) ETL_NOEXCEPT
300 : storage(other.storage)
309 : storage(etl::move(other.storage))
318 template <typename G, typename etl::enable_if< !etl::is_convertible<const G&, TError>::value,
bool>
::type =
false>
320 : storage(
etl::in_place_index_t<Error_Type>(), ue.error())
324 template <typename G, typename etl::enable_if< etl::is_convertible<const G&, TError>::value,
bool>::type =
false>
325 ETL_CONSTEXPR14
expected(
const etl::unexpected<G>& ue)
326 : storage(etl::in_place_index_t<Error_Type>(), ue.error())
330 template <
typename G>
331 explicit expected(
const etl::unexpected<G>& ue)
332 : storage(etl::in_place_index_t<Error_Type>(), ue.error())
341 template <typename G, typename etl::enable_if< !etl::is_convertible<const G&, TError>::value,
bool>::type =
false>
342 ETL_CONSTEXPR14
explicit expected(etl::unexpected<G>&& ue)
343 : storage(etl::in_place_index_t<Error_Type>(), etl::move(ue.error()))
347 template <typename G, typename etl::enable_if< etl::is_convertible<const G&, TError>::value,
bool>::type =
false>
348 ETL_CONSTEXPR14
expected(etl::unexpected<G>&& ue)
349 : storage(etl::in_place_index_t<Error_Type>(), etl::move(ue.error()))
358 : storage(value_type())
366 template <
typename... Args>
372 #if ETL_HAS_INITIALIZER_LIST
376 template <
typename U,
typename... Args>
378 : storage(
etl::in_place_index_t<Value_Type>(), il,
etl::forward<Args>(args)...)
386 template <
typename... Args>
387 ETL_CONSTEXPR14
explicit expected(etl::unexpect_t, Args&&... args)
388 : storage(error_type(etl::forward<Args>(args)...))
392 #if ETL_HAS_INITIALIZER_LIST
396 template <
typename U,
typename... Args>
397 ETL_CONSTEXPR14
explicit expected(etl::unexpect_t, std::initializer_list<U> il, Args&&... args)
398 : storage(error_type(il, etl::forward<Args>(args)...))
409 ETL_STATIC_ASSERT(etl::is_copy_constructible<TValue>::value && etl::is_copy_constructible<TError>::value,
"Not copy assignable");
411 storage = other.storage;
422 ETL_STATIC_ASSERT(etl::is_move_constructible<TValue>::value && etl::is_move_constructible<TError>::value,
"Not move assignable");
424 storage = etl::move(other.storage);
435 ETL_STATIC_ASSERT(etl::is_copy_constructible<TValue>::value,
"Value not copy assignable");
437 storage.template emplace<Value_Type>(
value);
448 ETL_STATIC_ASSERT(etl::is_move_constructible<TValue>::value,
"Value not move assignable");
450 storage.template emplace<Value_Type>(etl::move(value));
462 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value,
"Error not copy assignable");
465 storage.template emplace<Error_Type>(ue.
error());
476 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value,
"Error not move assignable");
478 storage.template emplace<Error_Type>(etl::move(ue.error()));
488 ETL_CONSTEXPR14 value_type&
value() &
496 ETL_CONSTEXPR14
const value_type&
value() const&
504 ETL_CONSTEXPR14 value_type&&
value() &&
512 ETL_CONSTEXPR14
const value_type&&
value() const&&
529 ETL_NODISCARD ETL_CONSTEXPR14
bool has_value() const ETL_NOEXCEPT
531 return (storage.
index() == Value_Type);
537 ETL_NODISCARD ETL_CONSTEXPR14 ETL_EXPLICIT
operator bool() const ETL_NOEXCEPT
546 template <
typename U>
547 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<U, value_type>::value, value_type> value_or(U&& default_value)
const&
555 return static_cast<value_type
>(etl::forward<U>(default_value));
562 template <
typename U>
563 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<U, value_type>::value, value_type> value_or(U&& default_value) &&
567 return etl::move(
value());
571 return static_cast<value_type
>(etl::forward<U>(default_value));
578 ETL_NODISCARD ETL_CONSTEXPR14 error_type& error() & ETL_NOEXCEPT
586 ETL_NODISCARD ETL_CONSTEXPR14
const error_type& error() const& ETL_NOEXCEPT
594 ETL_NODISCARD ETL_CONSTEXPR14 error_type&& error() && ETL_NOEXCEPT
602 ETL_NODISCARD ETL_CONSTEXPR14
const error_type&& error() const&& ETL_NOEXCEPT
610 template <
typename G>
611 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<G, error_type>::value, error_type>
error_or(G&& default_error)
const&
615 return static_cast<error_type
>(etl::forward<G>(default_error));
626 template <
typename G>
627 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<G, error_type>::value, error_type>
error_or(G&& default_error) &&
631 return static_cast<error_type
>(etl::forward<G>(default_error));
635 return etl::move(error());
642 void swap(this_type& other)
644 using ETL_OR_STD::swap;
646 swap(storage, other.storage);
652 template <
typename... Args>
653 ETL_CONSTEXPR14 value_type& emplace(Args&&... args) ETL_NOEXCEPT
655 storage.template emplace<value_type>(etl::forward<Args>(args)...);
663 #if ETL_HAS_INITIALIZER_LIST
664 template <
typename U,
typename... Args>
665 ETL_CONSTEXPR14 value_type& emplace(std::initializer_list<U> il, Args&&... args) ETL_NOEXCEPT
667 storage.template emplace<value_type>(il, etl::forward<Args>(args)...);
676 template <
typename U>
677 value_type value_or(
const U& default_value)
const
685 return default_value;
692 const error_type& error()
const
700 template <
typename G>
705 return static_cast<error_type
>(default_error);
719 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(
expected_invalid), ETL_NULLPTR);
727 const value_type* operator->()
const
729 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
737 value_type& operator*() ETL_LVALUE_REF_QUALIFIER
739 ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
747 const value_type& operator*() const ETL_LVALUE_REF_QUALIFIER
749 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
758 value_type&& operator*() &&
760 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
768 const value_type&& operator*() const&&
770 ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
780 ETL_NODISCARD ETL_CONSTEXPR14 value_type*
begin() ETL_NOEXCEPT
788 ETL_NODISCARD ETL_CONSTEXPR14 value_type*
end() ETL_NOEXCEPT
796 ETL_NODISCARD ETL_CONSTEXPR14
const value_type*
begin() const ETL_NOEXCEPT
804 ETL_NODISCARD ETL_CONSTEXPR14
const value_type*
end() const ETL_NOEXCEPT
810 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TValue&>::type>
::type>
813 return transform_impl<F, this_type&, U, TValue&>(etl::forward<F>(f), *
this);
816 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TValue&>::type>
::type>
819 return transform_impl<F, const this_type&, U, const TValue&>(etl::forward<F>(f), *
this);
822 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TValue&&>::type>::type>
825 return transform_impl<F, this_type&&, U, TValue&&>(etl::forward<F>(f), etl::move(*
this));
828 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TValue&&>::type>::type>
831 return transform_impl<F, const this_type&&, U, const TValue&&>(etl::forward<F>(f), etl::move(*
this));
834 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TValue&>::type>::type>
835 auto and_then(F&& f) & -> U
837 return and_then_impl<F, this_type&, U, TValue&>(etl::forward<F>(f), *
this);
840 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TValue&>::type>::type>
841 auto and_then(F&& f)
const& -> U
843 return and_then_impl<F, const this_type&, U, const TValue&>(etl::forward<F>(f), *
this);
846 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TValue&&>::type>::type>
847 auto and_then(F&& f) && -> U
849 return and_then_impl<F, this_type&&, U, TValue&&>(etl::forward<F>(f), etl::move(*
this));
852 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TValue&&>::type>::type>
853 auto and_then(F&& f)
const&& -> U
855 return and_then_impl<F, const this_type&&, U, const TValue&&>(etl::forward<F>(f), etl::move(*
this));
858 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&>::type>::type>
859 auto or_else(F&& f) & -> U
861 return or_else_impl<F, this_type&, U, TError&>(etl::forward<F>(f), *
this);
864 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&>::type>::type>
865 auto or_else(F&& f)
const& -> U
867 return or_else_impl<F, const this_type&, U, const TError&>(etl::forward<F>(f), *
this);
870 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&&>::type>::type>
871 auto or_else(F&& f) && -> U
873 return or_else_impl<F, this_type&&, U, TError&&>(etl::forward<F>(f), etl::move(*
this));
876 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&&>::type>::type>
877 auto or_else(F&& f)
const&& -> U
879 return or_else_impl<F, const this_type&&, U, const TError&&>(etl::forward<F>(f), etl::move(*
this));
882 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&>::type>::type>
885 return transform_error_impl<F, this_type&, U, TError&>(etl::forward<F>(f), *
this);
888 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&>::type>::type>
891 return transform_error_impl<F, const this_type&, U, const TError&>(etl::forward<F>(f), *
this);
894 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&&>::type>::type>
897 return transform_error_impl<F, this_type&&, U, TError&&>(etl::forward<F>(f), etl::move(*
this));
900 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&&>::type>::type>
903 return transform_error_impl<F, const this_type&&, U, const TError&&>(etl::forward<F>(f), etl::move(*
this));
915 typedef etl::variant<value_type, error_type> storage_type;
916 storage_type storage;
919 template < typename F, typename TExp, typename TRet, typename TValueRef, typename = typename etl::enable_if<!etl::is_void<TRet>::value>::type>
932 template < typename F, typename TExp, typename TRet, typename TValueRef, typename = typename etl::enable_if<etl::is_void<TRet>::value>::type>
946 template <
typename F,
typename TExp,
typename TRet,
typename TValueRef,
947 typename =
typename etl::enable_if< !etl::is_void<TRet>::value && etl::is_expected<TRet>::value
948 && etl::is_same<typename TRet::error_type, TError>::value>::type>
949 auto and_then_impl(F&& f, TExp&& exp)
const -> TRet
953 return etl::invoke(etl::forward<F>(f), etl::forward<TValueRef>(
etl::get<Value_Type>(exp.storage)));
957 return TRet(unexpected<TError>(etl::forward<TExp>(exp).error()));
961 template <
typename F,
typename TExp,
typename TRet,
typename TErrorRef,
962 typename =
typename etl::enable_if< !etl::is_void<TRet>::value && etl::is_expected<TRet>::value
963 && etl::is_same<typename TRet::value_type, TValue>::value>::type>
964 auto or_else_impl(F&& f, TExp&& exp)
const -> TRet
968 return TRet(etl::forward<TExp>(exp).
value());
972 return etl::invoke(etl::forward<F>(f), etl::forward<TErrorRef>(
etl::get<Error_Type>(exp.storage)));
976 template < typename F, typename TExp, typename TRet, typename TErrorRef, typename = typename etl::enable_if<!etl::is_void<TRet>::value>::type>
1000 typedef void value_type;
1001 typedef TError error_type;
1015 : storage(ue_.
error())
1023 ETL_CONSTEXPR14
expected(unexpected_type&& ue_)
1024 : storage(
etl::move(ue_.error()))
1033 : storage(other.storage)
1041 ETL_CONSTEXPR14
expected(this_type&& other)
1042 : storage(
etl::move(other.storage))
1052 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value,
"Not copy assignable");
1054 storage = other.storage;
1064 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value,
"Not move assignable");
1066 storage = etl::move(other.storage);
1077 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value,
"Error not copy assignable");
1080 storage.template emplace<Error_Type>(ue.
error());
1090 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value,
"Error not move assignable");
1092 storage.template emplace<Error_Type>(etl::move(ue.error()));
1100 ETL_NODISCARD ETL_CONSTEXPR14
bool has_value() const ETL_NOEXCEPT
1102 return (storage.index() != Error_Type);
1108 ETL_NODISCARD ETL_CONSTEXPR14 ETL_EXPLICIT
operator bool() const ETL_NOEXCEPT
1118 ETL_NODISCARD ETL_CONSTEXPR14 error_type& error() & ETL_NOEXCEPT
1127 ETL_NODISCARD ETL_CONSTEXPR14
const error_type& error() const& ETL_NOEXCEPT
1136 ETL_NODISCARD ETL_CONSTEXPR14 error_type&& error() && ETL_NOEXCEPT
1145 ETL_NODISCARD ETL_CONSTEXPR14
const error_type&& error() const&& ETL_NOEXCEPT
1153 template <
typename G>
1154 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<G, error_type>::value, error_type>
error_or(G&& default_error)
const&
1158 return static_cast<error_type
>(etl::forward<G>(default_error));
1169 template <
typename G>
1170 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<G, error_type>::value, error_type>
error_or(G&& default_error) &&
1174 return static_cast<error_type
>(etl::forward<G>(default_error));
1178 return etl::move(error());
1194 template <
typename G>
1199 return static_cast<error_type
>(default_error);
1213 using ETL_OR_STD::swap;
1215 swap(storage, other.storage);
1219 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>
::type>
1222 return transform_impl<F, this_type&, U>(etl::forward<F>(f), *
this);
1225 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>
::type>
1228 return transform_impl<F, const this_type&, U>(etl::forward<F>(f), *
this);
1231 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1234 return transform_impl<F, this_type&&, U>(etl::forward<F>(f), etl::move(*
this));
1237 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1240 return transform_impl<F, const this_type&&, U>(etl::forward<F>(f), etl::move(*
this));
1243 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1244 auto and_then(F&& f) & -> U
1246 return and_then_impl<F, this_type&, U>(etl::forward<F>(f), *
this);
1249 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1250 auto and_then(F&& f)
const& -> U
1252 return and_then_impl<F, const this_type&, U>(etl::forward<F>(f), *
this);
1255 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1256 auto and_then(F&& f) && -> U
1258 return and_then_impl<F, this_type&&, U>(etl::forward<F>(f), etl::move(*
this));
1261 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1262 auto and_then(F&& f)
const&& -> U
1264 return and_then_impl<F, const this_type&&, U>(etl::forward<F>(f), etl::move(*
this));
1267 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&>::type>::type>
1268 auto or_else(F&& f) & -> U
1270 return or_else_impl<F, this_type&, U, TError&>(etl::forward<F>(f), *
this);
1273 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&>::type>::type>
1274 auto or_else(F&& f)
const& -> U
1276 return or_else_impl<F, const this_type&, U, const TError&>(etl::forward<F>(f), *
this);
1279 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&&>::type>::type>
1280 auto or_else(F&& f) && -> U
1282 return or_else_impl<F, this_type&&, U, TError&&>(etl::forward<F>(f), etl::move(*
this));
1285 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&&>::type>::type>
1286 auto or_else(F&& f)
const&& -> U
1288 return or_else_impl<F, const this_type&&, U, const TError&&>(etl::forward<F>(f), etl::move(*
this));
1291 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&>::type>::type>
1294 return transform_error_impl<F, this_type&, U, TError&>(etl::forward<F>(f), *
this);
1297 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&>::type>::type>
1300 return transform_error_impl<F, const this_type&, U, const TError&>(etl::forward<F>(f), *
this);
1303 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&&>::type>::type>
1306 return transform_error_impl<F, this_type&&, U, TError&&>(etl::forward<F>(f), etl::move(*
this));
1309 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&&>::type>::type>
1312 return transform_error_impl<F, const this_type&&, U, const TError&&>(etl::forward<F>(f), etl::move(*
this));
1324 etl::variant<etl::monostate, error_type> storage;
1327 template < typename F, typename TExp, typename TRet, typename = typename etl::enable_if<!etl::is_void<TRet>::value>::type>
1330 if (exp.has_value())
1340 template < typename F, typename TExp, typename TRet, typename = typename etl::enable_if<etl::is_void<TRet>::value>::type>
1343 if (exp.has_value())
1345 etl::invoke(etl::forward<F>(f));
1354 template <
typename F,
typename TExp,
typename TRet,
1355 typename =
typename etl::enable_if< !etl::is_void<TRet>::value && etl::is_expected<TRet>::value
1356 && etl::is_same<typename TRet::error_type, TError>::value>::type>
1357 auto and_then_impl(F&& f, TExp&& exp)
const -> TRet
1359 if (exp.has_value())
1361 return etl::invoke(etl::forward<F>(f));
1365 return TRet(unexpected<TError>(etl::forward<TExp>(exp).error()));
1369 template <
typename F,
typename TExp,
typename TRet,
typename TErrorRef,
1370 typename =
typename etl::enable_if< !etl::is_void<TRet>::value && etl::is_expected<TRet>::value
1371 && etl::is_same<typename TRet::value_type, void>::value>::type>
1372 auto or_else_impl(F&& f, TExp&& exp)
const -> TRet
1374 if (exp.has_value())
1380 return etl::invoke(etl::forward<F>(f), etl::forward<TErrorRef>(
etl::get<Error_Type>(exp.storage)));
1384 template < typename F, typename TExp, typename TRet, typename TErrorRef, typename = typename etl::enable_if<!etl::is_void<TRet>::value>::type>
1387 if (exp.has_value())