|
| 1 | +// include/beman/execution/detail/store_receiver.hpp -*-C++-*- |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#ifndef INCLUDED_INCLUDE_BEMAN_EXECUTION_DETAIL_STORE_RECEIVER |
| 5 | +#define INCLUDED_INCLUDE_BEMAN_EXECUTION_DETAIL_STORE_RECEIVER |
| 6 | + |
| 7 | +#include <beman/execution/detail/common.hpp> |
| 8 | +#ifdef BEMAN_HAS_IMPORT_STD |
| 9 | +import std; |
| 10 | +#else |
| 11 | +#include <memory> |
| 12 | +#include <utility> |
| 13 | +#include <type_traits> |
| 14 | +#endif |
| 15 | +#ifdef BEMAN_HAS_MODULES |
| 16 | +import beman.execution.detail.connect; |
| 17 | +import beman.execution.detail.connect_result_t; |
| 18 | +import beman.execution.detail.env_of_t; |
| 19 | +import beman.execution.detail.get_completion_signatures; |
| 20 | +import beman.execution.detail.get_env; |
| 21 | +import beman.execution.detail.operation_state; |
| 22 | +import beman.execution.detail.receiver; |
| 23 | +import beman.execution.detail.sender; |
| 24 | +import beman.execution.detail.set_error; |
| 25 | +import beman.execution.detail.set_stopped; |
| 26 | +import beman.execution.detail.set_value; |
| 27 | +import beman.execution.detail.start; |
| 28 | +#else |
| 29 | +#include <beman/execution/detail/connect.hpp> |
| 30 | +#include <beman/execution/detail/connect_result_t.hpp> |
| 31 | +#include <beman/execution/detail/env_of_t.hpp> |
| 32 | +#include <beman/execution/detail/get_completion_signatures.hpp> |
| 33 | +#include <beman/execution/detail/get_env.hpp> |
| 34 | +#include <beman/execution/detail/sender.hpp> |
| 35 | +#include <beman/execution/detail/receiver.hpp> |
| 36 | +#include <beman/execution/detail/set_error.hpp> |
| 37 | +#include <beman/execution/detail/set_stopped.hpp> |
| 38 | +#include <beman/execution/detail/set_value.hpp> |
| 39 | +#include <beman/execution/detail/operation_state.hpp> |
| 40 | +#include <beman/execution/detail/start.hpp> |
| 41 | +#endif |
| 42 | + |
| 43 | +// ---------------------------------------------------------------------------- |
| 44 | + |
| 45 | +namespace beman::execution::detail { |
| 46 | +struct store_receiver_t { |
| 47 | + template <::beman::execution::receiver Rcvr> |
| 48 | + struct receiver { |
| 49 | + using receiver_concept = ::beman::execution::receiver_t; |
| 50 | + Rcvr* rcvr; |
| 51 | + template <typename... Args> |
| 52 | + auto set_value(Args&&... args) && noexcept -> void { |
| 53 | + ::beman::execution::set_value(::std::move(*this->rcvr), ::std::forward<Args>(args)...); |
| 54 | + } |
| 55 | + template <typename Error> |
| 56 | + auto set_error(Error&& error) && noexcept -> void { |
| 57 | + ::beman::execution::set_error(::std::move(*this->rcvr), ::std::forward<Error>(error)); |
| 58 | + } |
| 59 | + auto set_stopped() && noexcept -> void { ::beman::execution::set_stopped(::std::move(*this->rcvr)); } |
| 60 | + auto get_env() const noexcept { return ::beman::execution::get_env(*this->rcvr); } |
| 61 | + }; |
| 62 | + template <::beman::execution::sender Sndr, typename Trans, ::beman::execution::receiver Rcvr> |
| 63 | + struct state { |
| 64 | + using operation_state_concept = ::beman::execution::operation_state_t; |
| 65 | + using env_t = ::beman::execution::env_of_t<Rcvr>; |
| 66 | + using state_t = ::beman::execution::connect_result_t<decltype(::std::declval<Trans>()( |
| 67 | + ::std::declval<Sndr>(), ::std::declval<env_t>())), |
| 68 | + receiver<Rcvr>>; |
| 69 | + Rcvr rcvr; |
| 70 | + state_t op_state; |
| 71 | + template <::beman::execution::sender S, typename T, ::beman::execution::receiver R> |
| 72 | + state(S&& sndr, T&& trans, R&& r) |
| 73 | + : rcvr(::std::forward<R>(r)), |
| 74 | + op_state(::beman::execution::connect( |
| 75 | + ::std::forward<T>(trans)(::std::forward<S>(sndr), ::beman::execution::get_env(this->rcvr)), |
| 76 | + receiver<Rcvr>{::std::addressof(this->rcvr)})) {} |
| 77 | + auto start() & noexcept { ::beman::execution::start(this->op_state); } |
| 78 | + }; |
| 79 | + template <::beman::execution::sender Sndr, typename Trans> |
| 80 | + struct sender { |
| 81 | + using sender_concept = ::beman::execution::sender_t; |
| 82 | + template <typename... Env> |
| 83 | + static consteval auto get_completion_signatures(Env&&... env) noexcept { |
| 84 | + return ::beman::execution:: |
| 85 | + get_completion_signatures<decltype(::std::declval<Trans>()(::std::declval<Sndr>())), Env...>(); |
| 86 | + } |
| 87 | + ::std::remove_cvref_t<Sndr> sndr; |
| 88 | + ::std::remove_cvref_t<Trans> trans; |
| 89 | + |
| 90 | + template <::beman::execution::receiver Receiver> |
| 91 | + auto connect(Receiver&& r) && { |
| 92 | + static_assert(::beman::execution::operation_state<state<Sndr, Trans, ::std::remove_cvref_t<Receiver>>>); |
| 93 | + return state<Sndr, Trans, ::std::remove_cvref_t<Receiver>>( |
| 94 | + ::std::move(this->sndr), ::std::move(this->trans), ::std::forward<Receiver>(r)); |
| 95 | + } |
| 96 | + template <::beman::execution::receiver Receiver> |
| 97 | + auto connect(Receiver&& r) const& { |
| 98 | + static_assert(::beman::execution::operation_state<state<Sndr, Trans, ::std::remove_cvref_t<Receiver>>>); |
| 99 | + return state<Sndr, Trans, ::std::remove_cvref_t<Receiver>>( |
| 100 | + this->sndr, this->trans, ::std::forward<Receiver>(r)); |
| 101 | + } |
| 102 | + }; |
| 103 | + template <::beman::execution::sender Sndr, typename Trans> |
| 104 | + auto operator()(Sndr&& sndr, Trans&& trans) const { |
| 105 | + static_assert(::beman::execution::sender<sender<Sndr, Trans>>); |
| 106 | + return sender<Sndr, Trans>{::std::forward<Sndr>(sndr), ::std::forward<Trans>(trans)}; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +inline constexpr store_receiver_t store_receiver{}; |
| 111 | +} // namespace beman::execution::detail |
| 112 | + |
| 113 | +// ---------------------------------------------------------------------------- |
| 114 | + |
| 115 | +#endif |
0 commit comments