|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <async/completion_tags.hpp> |
| 4 | +#include <async/concepts.hpp> |
| 5 | +#include <async/connect.hpp> |
| 6 | +#include <async/schedulers/trigger_scheduler.hpp> |
| 7 | +#include <async/start.hpp> |
| 8 | +#include <async/then.hpp> |
| 9 | + |
| 10 | +#include <stdx/concepts.hpp> |
| 11 | +#include <stdx/ct_string.hpp> |
| 12 | + |
| 13 | +#include <type_traits> |
| 14 | +#include <utility> |
| 15 | + |
| 16 | +namespace msg { |
| 17 | +namespace _send_recv { |
| 18 | +template <typename Sched> |
| 19 | +using scheduler_sender = decltype(std::declval<Sched>().schedule()); |
| 20 | + |
| 21 | +template <typename S> |
| 22 | +concept valid_send_action = |
| 23 | + requires { typename std::remove_cvref_t<S>::is_send_action; }; |
| 24 | + |
| 25 | +template <valid_send_action SA, typename Sched, typename Rcvr> |
| 26 | +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) |
| 27 | +struct op_state { |
| 28 | + template <stdx::same_as_unqualified<SA> S, typename Sch, typename R> |
| 29 | + constexpr op_state(S &&s, Sch &&sch, R &&r) |
| 30 | + : send(s), ops{async::connect(std::forward<Sch>(sch).schedule(), |
| 31 | + std::forward<R>(r))} {} |
| 32 | + constexpr op_state(op_state &&) = delete; |
| 33 | + |
| 34 | + using ops_t = async::connect_result_t<scheduler_sender<Sched>, Rcvr>; |
| 35 | + |
| 36 | + constexpr auto start() & -> void { |
| 37 | + async::start(ops); |
| 38 | + send(); |
| 39 | + } |
| 40 | + |
| 41 | + SA send; |
| 42 | + ops_t ops; |
| 43 | +}; |
| 44 | + |
| 45 | +template <valid_send_action SA, typename Sched> struct sender { |
| 46 | + using is_sender = void; |
| 47 | + |
| 48 | + [[no_unique_address]] SA send; |
| 49 | + [[no_unique_address]] Sched sched; |
| 50 | + |
| 51 | + public: |
| 52 | + template <async::receiver R> |
| 53 | + [[nodiscard]] constexpr auto |
| 54 | + connect(R &&r) && -> op_state<SA, Sched, std::remove_cvref_t<R>> { |
| 55 | + async::check_connect<sender &&, R>(); |
| 56 | + return {std::move(send), std::move(sched), std::forward<R>(r)}; |
| 57 | + } |
| 58 | + |
| 59 | + template <async::receiver R> |
| 60 | + [[nodiscard]] constexpr auto |
| 61 | + connect(R &&r) const & -> op_state<SA, Sched, std::remove_cvref_t<R>> { |
| 62 | + async::check_connect<sender, R>(); |
| 63 | + return {send, sched, std::forward<R>(r)}; |
| 64 | + } |
| 65 | + |
| 66 | + template <typename Env> |
| 67 | + [[nodiscard]] constexpr static auto get_completion_signatures(Env const &) |
| 68 | + -> async::completion_signatures_of_t<scheduler_sender<Sched>, Env> { |
| 69 | + return {}; |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +template <typename Sndr, typename Sched> |
| 74 | +sender(Sndr, Sched) -> sender<Sndr, Sched>; |
| 75 | + |
| 76 | +template <typename F> struct send_action : F { |
| 77 | + using is_send_action = void; |
| 78 | +}; |
| 79 | +template <typename F> send_action(F) -> send_action<F>; |
| 80 | + |
| 81 | +template <stdx::ct_string Name, typename... Args> struct pipeable { |
| 82 | + template <typename Adaptor> struct type { |
| 83 | + [[no_unique_address]] Adaptor a; |
| 84 | + |
| 85 | + private: |
| 86 | + template <valid_send_action S, stdx::same_as_unqualified<type> Self> |
| 87 | + friend constexpr auto operator|(S &&s, Self &&self) -> async::sender |
| 88 | + auto { |
| 89 | + return _send_recv::sender{ |
| 90 | + std::forward<S>(s), |
| 91 | + async::trigger_scheduler<Name, Args...>{}} | |
| 92 | + std::forward<Self>(self).a; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + template <typename T> type(T) -> type<T>; |
| 97 | +}; |
| 98 | +} // namespace _send_recv |
| 99 | + |
| 100 | +template <typename F, typename... Args> |
| 101 | +constexpr auto send(F &&f, Args &&...args) { |
| 102 | + return _send_recv::send_action{ |
| 103 | + [f = std::forward<F>(f), ... args = std::forward<Args>(args)]() { |
| 104 | + return f(args...); |
| 105 | + }}; |
| 106 | +} |
| 107 | + |
| 108 | +template <stdx::ct_string Name, typename... RecvArgs, typename F, |
| 109 | + typename... Args> |
| 110 | +[[nodiscard]] constexpr auto then_receive(F &&f, Args &&...args) { |
| 111 | + return typename _send_recv::pipeable<Name, RecvArgs...>::type{async::then( |
| 112 | + [f = std::forward<F>(f), ... args = std::forward<Args>(args)]( |
| 113 | + RecvArgs const &...as) { return f(as..., args...); })}; |
| 114 | +} |
| 115 | + |
| 116 | +template <stdx::ct_string Name, _send_recv::valid_send_action S, typename F, |
| 117 | + typename... Args> |
| 118 | +[[nodiscard]] constexpr auto then_receive(S &&s, F &&f, |
| 119 | + Args &&...args) -> async::sender |
| 120 | + auto { |
| 121 | + return std::forward<S>(s) | |
| 122 | + then_receive<Name>(std::forward<F>(f), std::forward<Args>(args)...); |
| 123 | +} |
| 124 | +} // namespace msg |
0 commit comments