16#include "pw_async2/dispatcher.h"
17#include "pw_async2/future.h"
28template <
typename... Pendables>
31 static constexpr auto kTupleIndexSequence =
32 std::make_index_sequence<
sizeof...(Pendables)>();
33 using TupleOfOutputRvalues = std::tuple<PendOutputOf<Pendables>&&...>;
37 explicit Join(Pendables&&... pendables)
38 : pendables_(std::move(pendables)...),
39 outputs_(
Poll<PendOutputOf<Pendables>>(
Pending())...) {}
44 if (!PendElements(cx, kTupleIndexSequence)) {
47 return TakeOutputs(kTupleIndexSequence);
54 template <
size_t... Is>
55 bool PendElements(
Context& cx, std::index_sequence<Is...>) {
56 return (... && PendElement<Is>(cx));
60 template <
size_t... Is>
61 Poll<TupleOfOutputRvalues> TakeOutputs(std::index_sequence<Is...>) {
62 return Poll<TupleOfOutputRvalues>(
63 std::forward_as_tuple<PendOutputOf<Pendables>...>(TakeOutput<Is>()...));
71 template <
size_t kTupleIndex>
72 bool PendElement(Context& cx) {
73 auto& output = std::get<kTupleIndex>(outputs_);
74 if (output.IsReady()) {
77 output = std::get<kTupleIndex>(pendables_).Pend(cx);
78 return output.IsReady();
82 template <
size_t kTupleIndex>
83 PendOutputOf<
typename std::tuple_element<kTupleIndex,
84 std::tuple<Pendables...>>::type>&&
86 return std::move(std::get<kTupleIndex>(outputs_).value());
89 std::tuple<Pendables...> pendables_;
90 std::tuple<Poll<PendOutputOf<Pendables>>...> outputs_;
93template <
typename... Pendables>
94Join(Pendables&&...) -> Join<Pendables...>;
98namespace experimental {
100template <
typename... Futures>
102 :
public Future<JoinFuture<Futures...>,
103 std::tuple<typename Futures::value_type&&...>> {
105 static constexpr auto kTupleIndexSequence =
106 std::make_index_sequence<
sizeof...(Futures)>();
107 using TupleOfOutputRvalues = std::tuple<
typename Futures::value_type&&...>;
112 template <
typename... Fs>
113 friend constexpr auto Join(Fs&&...);
115 explicit constexpr JoinFuture(Futures&&... futures)
116 : futures_(std::in_place, std::move(futures)...),
122 if (!PendElements(cx, kTupleIndexSequence)) {
125 return TakeOutputs(kTupleIndexSequence);
128 void DoMarkComplete() { futures_.reset(); }
129 bool DoIsComplete()
const {
return !futures_.has_value(); }
134 template <
size_t... Is>
135 bool PendElements(
Context& cx, std::index_sequence<Is...>) {
136 (PendElement<Is>(cx), ...);
137 return (std::get<Is>(outputs_).IsReady() && ...);
142 template <
size_t kTupleIndex>
143 void PendElement(
Context& cx) {
144 auto& output = std::get<kTupleIndex>(outputs_);
145 if (!output.IsReady()) {
146 output = std::get<kTupleIndex>(*futures_).Pend(cx);
151 template <
size_t... Is>
154 std::forward_as_tuple<PendOutputOf<Futures>...>(TakeOutput<Is>()...));
158 template <
size_t kTupleIndex>
160 typename std::tuple_element<kTupleIndex, std::tuple<Futures...>>::type>&&
162 return std::move(std::get<kTupleIndex>(outputs_).value());
165 static_assert(std::conjunction_v<is_future<Futures>...>,
166 "All types in JoinFuture must be Future types");
167 std::optional<std::tuple<Futures...>> futures_;
168 std::tuple<Poll<PendOutputOf<Futures>>...> outputs_;
171template <
typename... Futures>
179template <
typename... Futures>
180constexpr auto Join(Futures&&... futures) {
181 static_assert(std::conjunction_v<is_future<Futures>...>,
182 "All arguments to Join must be Future types");
183 return JoinFuture(std::forward<Futures>(futures)...);
Join(Pendables &&... pendables)
Creates a Join from a series of pendable values.
Definition: join.h:37
Poll< TupleOfOutputRvalues > Pend(Context &cx)
Definition: join.h:43
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271