diff options
author | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-03-24 13:58:08 +0300 |
---|---|---|
committer | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-03-24 13:58:08 +0300 |
commit | 271ac8fd7cc6c125b34b2bce83590db2b2c7e2d7 (patch) | |
tree | 78acf3dc1d8f60371fb158e6d731d22b49afcf56 /contrib/libs/cxxsupp/libcxx/include/optional | |
parent | d9fd72590079fc40db118f7993267f5fc8d1ce58 (diff) | |
download | ydb-271ac8fd7cc6c125b34b2bce83590db2b2c7e2d7.tar.gz |
intermediate changes
ref:9eb06e5e31eb4c81bc7f55fa295da8edd4e644bb
Diffstat (limited to 'contrib/libs/cxxsupp/libcxx/include/optional')
-rw-r--r-- | contrib/libs/cxxsupp/libcxx/include/optional | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/contrib/libs/cxxsupp/libcxx/include/optional b/contrib/libs/cxxsupp/libcxx/include/optional index 1d16767dcf..805e2e6d04 100644 --- a/contrib/libs/cxxsupp/libcxx/include/optional +++ b/contrib/libs/cxxsupp/libcxx/include/optional @@ -132,6 +132,18 @@ namespace std { template <class U> constexpr T value_or(U &&) const &; template <class U> constexpr T value_or(U &&) &&; + // [optional.monadic], monadic operations + template<class F> constexpr auto and_then(F&& f) &; // since C++23 + template<class F> constexpr auto and_then(F&& f) &&; // since C++23 + template<class F> constexpr auto and_then(F&& f) const&; // since C++23 + template<class F> constexpr auto and_then(F&& f) const&&; // since C++23 + template<class F> constexpr auto transform(F&& f) &; // since C++23 + template<class F> constexpr auto transform(F&& f) &&; // since C++23 + template<class F> constexpr auto transform(F&& f) const&; // since C++23 + template<class F> constexpr auto transform(F&& f) const&&; // since C++23 + template<class F> constexpr optional or_else(F&& f) &&; // since C++23 + template<class F> constexpr optional or_else(F&& f) const&; // since C++23 + // 23.6.3.6, modifiers void reset() noexcept; // constexpr in C++20 @@ -147,6 +159,7 @@ template<class T> */ #include <__availability> +#include <__concepts/invocable.h> #include <__config> #include <__debug> #include <__functional_base> @@ -200,6 +213,8 @@ struct nullopt_t inline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}}; +struct __optional_construct_from_invoke_tag {}; + template <class _Tp, bool = is_trivially_destructible<_Tp>::value> struct __optional_destruct_base; @@ -234,6 +249,13 @@ struct __optional_destruct_base<_Tp, false> : __val_(_VSTD::forward<_Args>(__args)...), __engaged_(true) {} +#if _LIBCPP_STD_VER > 20 + template <class _Fp, class... _Args> + _LIBCPP_HIDE_FROM_ABI + constexpr __optional_destruct_base(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) + : __val_(_VSTD::invoke(_VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...)), __engaged_(true) {} +#endif + _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept { @@ -269,6 +291,13 @@ struct __optional_destruct_base<_Tp, true> : __val_(_VSTD::forward<_Args>(__args)...), __engaged_(true) {} +#if _LIBCPP_STD_VER > 20 + template <class _Fp, class... _Args> + _LIBCPP_HIDE_FROM_ABI + constexpr __optional_destruct_base(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) + : __val_(_VSTD::invoke(_VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...)), __engaged_(true) {} +#endif + _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept { @@ -582,6 +611,12 @@ using __optional_sfinae_assign_base_t = __sfinae_assign_base< (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value) >; +template<class _Tp> +class optional; +template <class _Tp> +struct __is_std_optional : false_type {}; +template <class _Tp> struct __is_std_optional<optional<_Tp>> : true_type {}; + template <class _Tp> class optional : private __optional_move_assign_base<_Tp> @@ -679,6 +714,7 @@ private: _CheckOptionalLikeConstructor<_QualUp>, __check_tuple_constructor_fail >; + public: _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {} @@ -754,6 +790,14 @@ public: this->__construct_from(_VSTD::move(__v)); } +#if _LIBCPP_STD_VER > 20 + template<class _Fp, class... _Args> + _LIBCPP_HIDE_FROM_ABI + constexpr explicit optional(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) + : __base(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...) { + } +#endif + _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& operator=(nullopt_t) noexcept { @@ -988,6 +1032,132 @@ public: static_cast<value_type>(_VSTD::forward<_Up>(__v)); } +#if _LIBCPP_STD_VER > 20 + template<class _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr auto and_then(_Func&& __f) & { + using _Up = invoke_result_t<_Func, value_type&>; + static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, + "Result of f(value()) must be a specialization of std::optional"); + if (*this) + return _VSTD::invoke(_VSTD::forward<_Func>(__f), value()); + return remove_cvref_t<_Up>(); + } + + template<class _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr auto and_then(_Func&& __f) const& { + using _Up = invoke_result_t<_Func, const value_type&>; + static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, + "Result of f(value()) must be a specialization of std::optional"); + if (*this) + return _VSTD::invoke(_VSTD::forward<_Func>(__f), value()); + return remove_cvref_t<_Up>(); + } + + template<class _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr auto and_then(_Func&& __f) && { + using _Up = invoke_result_t<_Func, value_type&&>; + static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, + "Result of f(std::move(value())) must be a specialization of std::optional"); + if (*this) + return _VSTD::invoke(_VSTD::forward<_Func>(__f), _VSTD::move(value())); + return remove_cvref_t<_Up>(); + } + + template<class _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr auto and_then(_Func&& __f) const&& { + using _Up = invoke_result_t<_Func, const value_type&&>; + static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, + "Result of f(std::move(value())) must be a specialization of std::optional"); + if (*this) + return _VSTD::invoke(_VSTD::forward<_Func>(__f), _VSTD::move(value())); + return remove_cvref_t<_Up>(); + } + + template<class _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr auto transform(_Func&& __f) & { + using _Up = remove_cv_t<invoke_result_t<_Func, value_type&>>; + static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array"); + static_assert(!is_same_v<_Up, in_place_t>, + "Result of f(value()) should not be std::in_place_t"); + static_assert(!is_same_v<_Up, nullopt_t>, + "Result of f(value()) should not be std::nullopt_t"); + static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type"); + if (*this) + return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), value()); + return optional<_Up>(); + } + + template<class _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr auto transform(_Func&& __f) const& { + using _Up = remove_cv_t<invoke_result_t<_Func, const value_type&>>; + static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array"); + static_assert(!is_same_v<_Up, in_place_t>, + "Result of f(value()) should not be std::in_place_t"); + static_assert(!is_same_v<_Up, nullopt_t>, + "Result of f(value()) should not be std::nullopt_t"); + static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type"); + if (*this) + return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), value()); + return optional<_Up>(); + } + + template<class _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr auto transform(_Func&& __f) && { + using _Up = remove_cv_t<invoke_result_t<_Func, value_type&&>>; + static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array"); + static_assert(!is_same_v<_Up, in_place_t>, + "Result of f(std::move(value())) should not be std::in_place_t"); + static_assert(!is_same_v<_Up, nullopt_t>, + "Result of f(std::move(value())) should not be std::nullopt_t"); + static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type"); + if (*this) + return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), _VSTD::move(value())); + return optional<_Up>(); + } + + template<class _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr auto transform(_Func&& __f) const&& { + using _Up = remove_cvref_t<invoke_result_t<_Func, const value_type&&>>; + static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array"); + static_assert(!is_same_v<_Up, in_place_t>, + "Result of f(std::move(value())) should not be std::in_place_t"); + static_assert(!is_same_v<_Up, nullopt_t>, + "Result of f(std::move(value())) should not be std::nullopt_t"); + static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type"); + if (*this) + return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), _VSTD::move(value())); + return optional<_Up>(); + } + + template<invocable _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr optional or_else(_Func&& __f) const& requires is_copy_constructible_v<value_type> { + static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>, + "Result of f() should be the same type as this optional"); + if (*this) + return *this; + return _VSTD::forward<_Func>(__f)(); + } + + template<invocable _Func> + _LIBCPP_HIDE_FROM_ABI + constexpr optional or_else(_Func&& __f) && requires is_move_constructible_v<value_type> { + static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>, + "Result of f() should be the same type as this optional"); + if (*this) + return _VSTD::move(*this); + return _VSTD::forward<_Func>(__f)(); + } +#endif // _LIBCPP_STD_VER > 20 + using __base::reset; }; |