|
| 1 | +// include/beman/execution26/detail/then.hpp -*-C++-*- |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#ifndef INCLUDED_BEMAN_EXECUTION26_DETAIL_THEN |
| 5 | +#define INCLUDED_BEMAN_EXECUTION26_DETAIL_THEN |
| 6 | + |
| 7 | +#include <beman/execution26/detail/call_result_t.hpp> |
| 8 | +#include <beman/execution26/detail/completion_signatures.hpp> |
| 9 | +#include <beman/execution26/detail/completion_signatures_for.hpp> |
| 10 | +#include <beman/execution26/detail/completion_signatures_of_t.hpp> |
| 11 | +#include <beman/execution26/detail/default_impls.hpp> |
| 12 | +#include <beman/execution26/detail/get_domain_early.hpp> |
| 13 | +#include <beman/execution26/detail/impls_for.hpp> |
| 14 | +#include <beman/execution26/detail/make_sender.hpp> |
| 15 | +#include <beman/execution26/detail/meta_transform.hpp> |
| 16 | +#include <beman/execution26/detail/meta_unique.hpp> |
| 17 | +#include <beman/execution26/detail/movable_value.hpp> |
| 18 | +#include <beman/execution26/detail/sender.hpp> |
| 19 | +#include <beman/execution26/detail/sender_adaptor.hpp> |
| 20 | +#include <beman/execution26/detail/sender_adaptor_closure.hpp> |
| 21 | +#include <beman/execution26/detail/set_error.hpp> |
| 22 | +#include <beman/execution26/detail/set_stopped.hpp> |
| 23 | +#include <beman/execution26/detail/set_value.hpp> |
| 24 | +#include <beman/execution26/detail/transform_sender.hpp> |
| 25 | +#include <concepts> |
| 26 | +#include <exception> |
| 27 | +#include <functional> |
| 28 | +#include <utility> |
| 29 | + |
| 30 | +// ---------------------------------------------------------------------------- |
| 31 | + |
| 32 | +namespace beman::execution26::detail |
| 33 | +{ |
| 34 | + template <typename Completion> |
| 35 | + struct then_t |
| 36 | + : ::beman::execution26::sender_adaptor_closure<then_t<Completion>> |
| 37 | + { |
| 38 | + template <::beman::execution26::detail::movable_value Fun> |
| 39 | + auto operator()(Fun&& fun) const |
| 40 | + { |
| 41 | + return ::beman::execution26::detail::sender_adaptor{ |
| 42 | + *this, ::std::forward<Fun>(fun) |
| 43 | + }; |
| 44 | + } |
| 45 | + template <::beman::execution26::sender Sender, |
| 46 | + ::beman::execution26::detail::movable_value Fun> |
| 47 | + auto operator()(Sender&& sender, Fun&& fun) const |
| 48 | + { |
| 49 | + auto domain{::beman::execution26::detail::get_domain_early(sender)}; |
| 50 | + return ::beman::execution26::transform_sender( |
| 51 | + domain, |
| 52 | + ::beman::execution26::detail::make_sender( |
| 53 | + *this, |
| 54 | + ::std::forward<Fun>(fun), |
| 55 | + ::std::forward<Sender>(sender) |
| 56 | + ) |
| 57 | + ); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + template <typename Completion> |
| 62 | + struct impls_for<then_t<Completion>> |
| 63 | + : ::beman::execution26::detail::default_impls |
| 64 | + { |
| 65 | + static constexpr auto complete |
| 66 | + = []<typename Tag, typename... Args> |
| 67 | + (auto, auto& fun, auto& receiver, Tag, Args&&... args) |
| 68 | + noexcept |
| 69 | + -> void |
| 70 | + { |
| 71 | + if constexpr (::std::same_as<Completion, Tag>) |
| 72 | + { |
| 73 | + try |
| 74 | + { |
| 75 | + auto invoke = [&]{ |
| 76 | + return ::std::invoke(::std::move(fun), |
| 77 | + ::std::forward<Args>(args)...); |
| 78 | + }; |
| 79 | + if constexpr (::std::same_as<void, decltype(invoke())>) |
| 80 | + { |
| 81 | + invoke(); |
| 82 | + ::beman::execution26::set_value( |
| 83 | + ::std::move(receiver) |
| 84 | + ); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + ::beman::execution26::set_value( |
| 89 | + ::std::move(receiver), |
| 90 | + invoke() |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + catch (...) |
| 95 | + { |
| 96 | + ::beman::execution26::set_error( |
| 97 | + ::std::move(receiver), |
| 98 | + ::std::current_exception() |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + Tag()(::std::move(receiver), ::std::forward<Args>(args)...); |
| 105 | + } |
| 106 | + }; |
| 107 | + }; |
| 108 | + |
| 109 | + template <typename T> |
| 110 | + struct then_set_value |
| 111 | + { |
| 112 | + using type = ::beman::execution26::set_value_t(T); |
| 113 | + }; |
| 114 | + template <> |
| 115 | + struct then_set_value<void> |
| 116 | + { |
| 117 | + using type = ::beman::execution26::set_value_t(); |
| 118 | + }; |
| 119 | + |
| 120 | + template <typename, typename, typename Completion> |
| 121 | + struct then_transform |
| 122 | + { |
| 123 | + using type = Completion; |
| 124 | + }; |
| 125 | + |
| 126 | + template <typename Fun, typename Replace> |
| 127 | + struct then_transform_t |
| 128 | + { |
| 129 | + template <typename Completion> |
| 130 | + using transform = typename |
| 131 | + ::beman::execution26::detail::then_transform<Fun, Replace, Completion> |
| 132 | + ::type; |
| 133 | + }; |
| 134 | + |
| 135 | + template <typename Fun, typename Completion, typename... T> |
| 136 | + struct then_transform<Fun, Completion, Completion(T...)> |
| 137 | + { |
| 138 | + using type = typename ::beman::execution26::detail::then_set_value< |
| 139 | + ::beman::execution26::detail::call_result_t<Fun, T...> |
| 140 | + >::type; |
| 141 | + }; |
| 142 | + |
| 143 | + template <typename Completion, typename Fun, typename Sender, typename Env> |
| 144 | + struct completion_signatures_for_impl< |
| 145 | + ::beman::execution26::detail::basic_sender< |
| 146 | + ::beman::execution26::detail::then_t<Completion>, |
| 147 | + Fun, |
| 148 | + Sender |
| 149 | + >, |
| 150 | + Env |
| 151 | + > |
| 152 | + { |
| 153 | + using type = ::beman::execution26::detail::meta::unique< |
| 154 | + ::beman::execution26::detail::meta::transform< |
| 155 | + ::beman::execution26::detail::then_transform_t<Fun, Completion>::template transform, |
| 156 | + ::beman::execution26::completion_signatures_of_t<Sender, Env> |
| 157 | + > |
| 158 | + >; |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +namespace beman::execution26 |
| 163 | +{ |
| 164 | + using then_t = ::beman::execution26::detail::then_t<::beman::execution26::set_value_t>; |
| 165 | + using upon_error_t = ::beman::execution26::detail::then_t<::beman::execution26::set_error_t>; |
| 166 | + using upon_stopped_t = ::beman::execution26::detail::then_t<::beman::execution26::set_stopped_t>; |
| 167 | + |
| 168 | + inline constexpr ::beman::execution26::then_t then{}; |
| 169 | + inline constexpr ::beman::execution26::upon_error_t upon_error{}; |
| 170 | + inline constexpr ::beman::execution26::upon_stopped_t upon_stopped{}; |
| 171 | +} |
| 172 | + |
| 173 | +// ---------------------------------------------------------------------------- |
| 174 | + |
| 175 | +#endif |
0 commit comments