|
| 1 | +// |
| 2 | +// Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// Official repository: https://github.com/cppalliance/capy |
| 8 | +// |
| 9 | + |
| 10 | +#ifndef BOOST_CAPY_EXAMPLE_SENDER_AWAITABLE_HPP |
| 11 | +#define BOOST_CAPY_EXAMPLE_SENDER_AWAITABLE_HPP |
| 12 | + |
| 13 | +#include <boost/capy/ex/io_env.hpp> |
| 14 | + |
| 15 | +#include <beman/execution/execution.hpp> |
| 16 | + |
| 17 | +#include <coroutine> |
| 18 | +#include <cstring> |
| 19 | +#include <exception> |
| 20 | +#include <new> |
| 21 | +#include <stop_token> |
| 22 | +#include <tuple> |
| 23 | +#include <type_traits> |
| 24 | +#include <variant> |
| 25 | + |
| 26 | +namespace boost::capy { |
| 27 | + |
| 28 | +namespace detail { |
| 29 | + |
| 30 | +struct stopped_t {}; |
| 31 | + |
| 32 | +// The receiver's environment exposes only the stop token. |
| 33 | +struct bridge_env |
| 34 | +{ |
| 35 | + std::stop_token st_; |
| 36 | + |
| 37 | + auto query( |
| 38 | + beman::execution::get_stop_token_t const&) const noexcept |
| 39 | + { |
| 40 | + return st_; |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +// Deduce the single value tuple type from a sender's completion |
| 45 | +// signatures using beman::execution::value_types_of_t. |
| 46 | +template<class Sender> |
| 47 | +using sender_single_value_t = |
| 48 | + beman::execution::value_types_of_t< |
| 49 | + Sender, |
| 50 | + bridge_env, |
| 51 | + std::tuple, |
| 52 | + std::type_identity_t>; |
| 53 | + |
| 54 | +// Bridge receiver that stores the sender's completion result |
| 55 | +// and posts the coroutine handle back through the Capy executor. |
| 56 | +template<class ValueTuple> |
| 57 | +struct bridge_receiver |
| 58 | +{ |
| 59 | + using receiver_concept = beman::execution::receiver_t; |
| 60 | + |
| 61 | + std::variant< |
| 62 | + std::monostate, |
| 63 | + ValueTuple, |
| 64 | + std::exception_ptr, |
| 65 | + stopped_t>* result_; |
| 66 | + std::coroutine_handle<> cont_; |
| 67 | + io_env const* env_; |
| 68 | + |
| 69 | + auto get_env() const noexcept -> bridge_env |
| 70 | + { |
| 71 | + return {env_->stop_token}; |
| 72 | + } |
| 73 | + |
| 74 | + template<class... Args> |
| 75 | + void set_value(Args&&... args) && noexcept |
| 76 | + { |
| 77 | + result_->template emplace<1>( |
| 78 | + std::forward<Args>(args)...); |
| 79 | + env_->executor.post(cont_); |
| 80 | + } |
| 81 | + |
| 82 | + template<class E> |
| 83 | + void set_error(E&& e) && noexcept |
| 84 | + { |
| 85 | + if constexpr ( |
| 86 | + std::is_same_v< |
| 87 | + std::decay_t<E>, std::exception_ptr>) |
| 88 | + result_->template emplace<2>( |
| 89 | + std::forward<E>(e)); |
| 90 | + else |
| 91 | + result_->template emplace<2>( |
| 92 | + std::make_exception_ptr( |
| 93 | + std::forward<E>(e))); |
| 94 | + env_->executor.post(cont_); |
| 95 | + } |
| 96 | + |
| 97 | + void set_stopped() && noexcept |
| 98 | + { |
| 99 | + result_->template emplace<3>(stopped_t{}); |
| 100 | + env_->executor.post(cont_); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +} // namespace detail |
| 105 | + |
| 106 | +/** Awaitable that bridges a beman::execution sender into a Capy coroutine. |
| 107 | +
|
| 108 | + Satisfies IoAwaitable. When co_awaited inside a capy::task, |
| 109 | + connects the sender to a bridge receiver, starts the operation, |
| 110 | + and resumes the coroutine on the caller's executor when the |
| 111 | + sender completes. |
| 112 | +
|
| 113 | + Stop token propagation: the Capy coroutine's stop_token is |
| 114 | + forwarded to the sender through the bridge receiver's |
| 115 | + environment. |
| 116 | +
|
| 117 | + @tparam Sender The beman::execution sender type. |
| 118 | +*/ |
| 119 | +template<class Sender> |
| 120 | +struct [[nodiscard]] sender_awaitable |
| 121 | +{ |
| 122 | + using value_tuple = detail::sender_single_value_t<Sender>; |
| 123 | + using receiver_type = detail::bridge_receiver<value_tuple>; |
| 124 | + using op_state_type = decltype( |
| 125 | + beman::execution::connect( |
| 126 | + std::declval<Sender>(), |
| 127 | + std::declval<receiver_type>())); |
| 128 | + |
| 129 | + Sender sndr_; |
| 130 | + |
| 131 | + std::variant< |
| 132 | + std::monostate, |
| 133 | + value_tuple, |
| 134 | + std::exception_ptr, |
| 135 | + detail::stopped_t> result_{}; |
| 136 | + |
| 137 | + alignas(op_state_type) |
| 138 | + unsigned char op_buf_[sizeof(op_state_type)]; |
| 139 | + bool op_constructed_ = false; |
| 140 | + |
| 141 | + explicit sender_awaitable(Sender sndr) |
| 142 | + : sndr_(std::move(sndr)) |
| 143 | + { |
| 144 | + } |
| 145 | + |
| 146 | + // Movable only before await_suspend (op_state not yet constructed) |
| 147 | + sender_awaitable(sender_awaitable&& o) noexcept( |
| 148 | + std::is_nothrow_move_constructible_v<Sender>) |
| 149 | + : sndr_(std::move(o.sndr_)) |
| 150 | + { |
| 151 | + } |
| 152 | + |
| 153 | + sender_awaitable(sender_awaitable const&) = delete; |
| 154 | + sender_awaitable& operator=(sender_awaitable const&) = delete; |
| 155 | + sender_awaitable& operator=(sender_awaitable&&) = delete; |
| 156 | + |
| 157 | + ~sender_awaitable() |
| 158 | + { |
| 159 | + if(op_constructed_) |
| 160 | + std::launder( |
| 161 | + reinterpret_cast<op_state_type*>( |
| 162 | + op_buf_))->~op_state_type(); |
| 163 | + } |
| 164 | + |
| 165 | + bool await_ready() const noexcept { return false; } |
| 166 | + |
| 167 | + std::coroutine_handle<> |
| 168 | + await_suspend( |
| 169 | + std::coroutine_handle<> h, |
| 170 | + io_env const* env) |
| 171 | + { |
| 172 | + ::new(op_buf_) op_state_type( |
| 173 | + beman::execution::connect( |
| 174 | + std::move(sndr_), |
| 175 | + receiver_type{&result_, h, env})); |
| 176 | + op_constructed_ = true; |
| 177 | + beman::execution::start( |
| 178 | + *std::launder( |
| 179 | + reinterpret_cast<op_state_type*>( |
| 180 | + op_buf_))); |
| 181 | + return std::noop_coroutine(); |
| 182 | + } |
| 183 | + |
| 184 | + auto await_resume() |
| 185 | + { |
| 186 | + if(result_.index() == 2) |
| 187 | + std::rethrow_exception( |
| 188 | + std::get<2>(result_)); |
| 189 | + if(result_.index() == 3) |
| 190 | + throw std::runtime_error( |
| 191 | + "sender completed with set_stopped"); |
| 192 | + |
| 193 | + if constexpr (std::tuple_size_v<value_tuple> == 0) |
| 194 | + return; |
| 195 | + else if constexpr (std::tuple_size_v<value_tuple> == 1) |
| 196 | + return std::get<0>( |
| 197 | + std::get<1>(std::move(result_))); |
| 198 | + else |
| 199 | + return std::get<1>(std::move(result_)); |
| 200 | + } |
| 201 | +}; |
| 202 | + |
| 203 | +/** Create an IoAwaitable from a beman::execution sender. |
| 204 | +
|
| 205 | + @par Example |
| 206 | + @code |
| 207 | + capy::task<int> compute(auto sched) |
| 208 | + { |
| 209 | + auto result = co_await await_sender( |
| 210 | + beman::execution::schedule(sched) |
| 211 | + | beman::execution::then( |
| 212 | + [] { return 42; })); |
| 213 | + co_return result; |
| 214 | + } |
| 215 | + @endcode |
| 216 | +
|
| 217 | + @param sndr The sender to bridge. |
| 218 | + @return An IoAwaitable that can be co_awaited in a capy::task. |
| 219 | +*/ |
| 220 | +template<class Sender> |
| 221 | +auto await_sender(Sender&& sndr) |
| 222 | +{ |
| 223 | + return sender_awaitable<std::decay_t<Sender>>( |
| 224 | + std::forward<Sender>(sndr)); |
| 225 | +} |
| 226 | + |
| 227 | +} // namespace boost::capy |
| 228 | + |
| 229 | +#endif |
0 commit comments