|
| 1 | +// include/beman/execution/detail/bulk.hpp -*-C++-*- |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_BULK |
| 5 | +#define INCLUDED_BEMAN_EXECUTION_DETAIL_BULK |
| 6 | + |
| 7 | +#include <beman/execution/detail/get_completion_signatures.hpp> |
| 8 | +#include <beman/execution/detail/meta_combine.hpp> |
| 9 | +#include <beman/execution/detail/meta_unique.hpp> |
| 10 | +#include <beman/execution/detail/basic_sender.hpp> |
| 11 | +#include <beman/execution/detail/completion_signatures.hpp> |
| 12 | +#include <beman/execution/detail/completion_signatures_for.hpp> |
| 13 | +#include <beman/execution/detail/get_domain_early.hpp> |
| 14 | +#include <beman/execution/detail/make_sender.hpp> |
| 15 | +#include <beman/execution/detail/movable_value.hpp> |
| 16 | +#include <beman/execution/detail/product_type.hpp> |
| 17 | +#include <beman/execution/detail/sender.hpp> |
| 18 | +#include <beman/execution/detail/set_error.hpp> |
| 19 | +#include <beman/execution/detail/transform_sender.hpp> |
| 20 | +#include <beman/execution/detail/default_impls.hpp> |
| 21 | +#include <beman/execution/detail/impls_for.hpp> |
| 22 | +#include <beman/execution/detail/set_value.hpp> |
| 23 | +#include <algorithm> |
| 24 | +#include <concepts> |
| 25 | +#include <exception> |
| 26 | +#include <type_traits> |
| 27 | +#include <utility> |
| 28 | + |
| 29 | +#include <beman/execution/detail/suppress_push.hpp> |
| 30 | +namespace beman::execution::detail { |
| 31 | + |
| 32 | +struct bulk_t { |
| 33 | + |
| 34 | + template <class Sender, class Shape, class f> |
| 35 | + requires(::beman::execution::sender<Sender> && std::is_integral_v<Shape> && |
| 36 | + ::beman::execution::detail::movable_value<f>) |
| 37 | + auto operator()(Sender&& sndr, Shape&& shape, f&& fun) const { |
| 38 | + |
| 39 | + auto domain{::beman::execution::detail::get_domain_early(sndr)}; |
| 40 | + |
| 41 | + return ::beman::execution::transform_sender( |
| 42 | + domain, |
| 43 | + ::beman::execution::detail::make_sender( |
| 44 | + *this, ::beman::execution::detail::product_type<Shape, f>{shape, fun}, std::forward<Sender>(sndr))); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +template <> |
| 49 | +struct impls_for<bulk_t> : ::beman::execution::detail::default_impls { |
| 50 | + |
| 51 | + static constexpr auto complete = []<class Index, class Shape, class Fun, class Rcvr, class Tag, class... Args>( |
| 52 | + Index, |
| 53 | + ::beman::execution::detail::product_type<Shape, Fun>& state, |
| 54 | + Rcvr& rcvr, |
| 55 | + Tag, |
| 56 | + Args&&... args) noexcept -> void |
| 57 | + requires(not::std::same_as<Tag, set_value_t> || std::is_invocable_v<Fun, Shape, Args...>) |
| 58 | + { |
| 59 | + if constexpr (std::same_as<Tag, set_value_t>) { |
| 60 | + auto& [shape, f] = state; |
| 61 | + |
| 62 | + using s_type = std::remove_cvref_t<decltype(shape)>; |
| 63 | + |
| 64 | + constexpr bool nothrow = noexcept(f(s_type(shape), args...)); |
| 65 | + |
| 66 | + try { |
| 67 | + [&]() noexcept(nothrow) { |
| 68 | + for (decltype(s_type(shape)) i = 0; i < shape; i++) { |
| 69 | + f(s_type(i), args...); |
| 70 | + } |
| 71 | + Tag()(std::move(rcvr), std::forward<Args>(args)...); |
| 72 | + }(); |
| 73 | + |
| 74 | + } catch (...) { |
| 75 | + if constexpr (not nothrow) { |
| 76 | + ::beman::execution::set_error(std::move(rcvr), std::current_exception()); |
| 77 | + } |
| 78 | + } |
| 79 | + } else { |
| 80 | + Tag()(std::move(rcvr), std::forward<Args>(args)...); |
| 81 | + } |
| 82 | + }; |
| 83 | +}; |
| 84 | + |
| 85 | +template <typename, typename, typename> |
| 86 | +struct fixed_completions_helper; |
| 87 | + |
| 88 | +template <typename F, typename Shape, typename... Args> |
| 89 | +struct fixed_completions_helper<F, Shape, completion_signatures<Args...>> { |
| 90 | + |
| 91 | + template <typename, typename> |
| 92 | + struct may_throw; |
| 93 | + template <typename XF, typename Tag, typename... XArgs> |
| 94 | + struct may_throw<XF, Tag(XArgs...)> { |
| 95 | + static constexpr bool value = std::same_as<Tag, ::beman::execution::set_value_t> && |
| 96 | + not::std::is_nothrow_invocable<XF, Shape, XArgs...>(); |
| 97 | + }; |
| 98 | + template <typename XF, typename... Sigs> |
| 99 | + struct may_throw<XF, completion_signatures<Sigs...>> { |
| 100 | + static constexpr bool value = (false || ... || may_throw<XF, Sigs>::value); |
| 101 | + }; |
| 102 | + |
| 103 | + using type = std::conditional_t<!may_throw<F, Args...>::value, |
| 104 | + completion_signatures<Args...>, |
| 105 | + completion_signatures<Args..., set_error_t(std::exception_ptr)>>; |
| 106 | +}; |
| 107 | + |
| 108 | +template <typename F, typename Shape, typename Completions> |
| 109 | +using fixed_completions = typename fixed_completions_helper<F, Shape, Completions>::type; |
| 110 | + |
| 111 | +template <class Shape, class F, class Sender, class Env> |
| 112 | +struct completion_signatures_for_impl< |
| 113 | + ::beman::execution::detail:: |
| 114 | + basic_sender<::beman::execution::detail::bulk_t, ::beman::execution::detail::product_type<Shape, F>, Sender>, |
| 115 | + Env> { |
| 116 | + |
| 117 | + using completions = decltype(get_completion_signatures(std::declval<Sender>(), std::declval<Env>())); |
| 118 | + using type = ::beman::execution::detail::meta::unique< |
| 119 | + ::beman::execution::detail::meta::combine<fixed_completions<F, Shape, completions>>>; |
| 120 | +}; |
| 121 | + |
| 122 | +} // namespace beman::execution::detail |
| 123 | + |
| 124 | +#include <beman/execution/detail/suppress_pop.hpp> |
| 125 | + |
| 126 | +namespace beman::execution { |
| 127 | + |
| 128 | +using ::beman::execution::detail::bulk_t; |
| 129 | +inline constexpr ::beman::execution::bulk_t bulk{}; |
| 130 | + |
| 131 | +} // namespace beman::execution |
| 132 | + |
| 133 | +#endif |
0 commit comments