|
| 1 | +// include/beman/execution/detail/stopped_as_optional.hpp -*-C++-*- |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_STOPPED_AS_OPTIONAL |
| 5 | +#define INCLUDED_BEMAN_EXECUTION_DETAIL_STOPPED_AS_OPTIONAL |
| 6 | + |
| 7 | +// ---------------------------------------------------------------------------- |
| 8 | + |
| 9 | +#include <beman/execution/detail/common.hpp> |
| 10 | +#ifdef BEMAN_HAS_IMPORT_STD |
| 11 | +import std; |
| 12 | +#else |
| 13 | +#include <optional> |
| 14 | +#include <type_traits> |
| 15 | +#include <utility> |
| 16 | +#endif |
| 17 | +#ifdef BEMAN_HAS_MODULES |
| 18 | +import beman.execution.detail.child_type; |
| 19 | +import beman.execution.detail.fwd_env; |
| 20 | +import beman.execution.detail.get_domain_early; |
| 21 | +import beman.execution.detail.just; |
| 22 | +import beman.execution.detail.let; |
| 23 | +import beman.execution.detail.make_sender; |
| 24 | +import beman.execution.detail.sender; |
| 25 | +import beman.execution.detail.sender_adaptor_closure; |
| 26 | +import beman.execution.detail.sender_for; |
| 27 | +import beman.execution.detail.single_sender; |
| 28 | +import beman.execution.detail.single_sender_value_type; |
| 29 | +import beman.execution.detail.then; |
| 30 | +import beman.execution.detail.transform_sender; |
| 31 | +#else |
| 32 | +#include <beman/execution/detail/child_type.hpp> |
| 33 | +#include <beman/execution/detail/fwd_env.hpp> |
| 34 | +#include <beman/execution/detail/get_domain_early.hpp> |
| 35 | +#include <beman/execution/detail/just.hpp> |
| 36 | +#include <beman/execution/detail/let.hpp> |
| 37 | +#include <beman/execution/detail/make_sender.hpp> |
| 38 | +#include <beman/execution/detail/sender.hpp> |
| 39 | +#include <beman/execution/detail/sender_adaptor_closure.hpp> |
| 40 | +#include <beman/execution/detail/sender_for.hpp> |
| 41 | +#include <beman/execution/detail/single_sender.hpp> |
| 42 | +#include <beman/execution/detail/single_sender_value_type.hpp> |
| 43 | +#include <beman/execution/detail/then.hpp> |
| 44 | +#include <beman/execution/detail/transform_sender.hpp> |
| 45 | +#endif |
| 46 | + |
| 47 | +namespace beman::execution::detail { |
| 48 | +struct stopped_as_optional_t : ::beman::execution::sender_adaptor_closure<stopped_as_optional_t> { |
| 49 | + template <::beman::execution::detail::sender_for<stopped_as_optional_t> Sndr, typename Env> |
| 50 | + auto transform_sender(Sndr&& sndr, Env&&) const noexcept { |
| 51 | + static_assert( |
| 52 | + ::beman::execution::detail::single_sender<child_type<Sndr>, ::beman::execution::detail::fwd_env<Env>>, |
| 53 | + "sender must be a single sender"); |
| 54 | + using value_type = |
| 55 | + ::beman::execution::detail::single_sender_value_type<child_type<Sndr>, |
| 56 | + ::beman::execution::detail::fwd_env<Env>>; |
| 57 | + static_assert(!::std::is_void_v<value_type>, "the value type of sender can't be empty"); |
| 58 | + return ::beman::execution::let_stopped( |
| 59 | + ::beman::execution::then( |
| 60 | + ::std::forward<Sndr>(sndr).template get<2>(), |
| 61 | + []<class... Args>(Args&&... args) noexcept(::std::is_nothrow_constructible_v<value_type, Args...>) { |
| 62 | + return ::std::optional<value_type>(::std::in_place, ::std::forward<Args>(args)...); |
| 63 | + }), |
| 64 | + []() noexcept { return ::beman::execution::just(::std::optional<value_type>()); }); |
| 65 | + } |
| 66 | + |
| 67 | + template <::beman::execution::sender Sndr> |
| 68 | + auto operator()(Sndr&& sndr) const { |
| 69 | + return ::beman::execution::transform_sender( |
| 70 | + ::beman::execution::detail::get_domain_early(sndr), |
| 71 | + ::beman::execution::detail::make_sender(stopped_as_optional_t{}, {}, ::std::forward<Sndr>(sndr))); |
| 72 | + } |
| 73 | + |
| 74 | + auto operator()() const noexcept { return ::beman::execution::detail::make_sender_adaptor(*this); } |
| 75 | +}; |
| 76 | + |
| 77 | +} // namespace beman::execution::detail |
| 78 | + |
| 79 | +namespace beman::execution { |
| 80 | +using stopped_as_optional_t = ::beman::execution::detail::stopped_as_optional_t; |
| 81 | +inline constexpr stopped_as_optional_t stopped_as_optional{}; |
| 82 | +} // namespace beman::execution |
| 83 | + |
| 84 | +// ---------------------------------------------------------------------------- |
| 85 | + |
| 86 | +#endif // INCLUDED_BEMAN_EXECUTION_DETAIL_STOPPED_AS_OPTIONAL |
0 commit comments