C/C++ API Reference
Loading...
Searching...
No Matches
join.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "pw_async2/dispatcher.h"
17#include "pw_async2/future.h"
18
19namespace pw::async2 {
20
22
28template <typename... Pendables>
29class Join {
30 private:
31 static constexpr auto kTupleIndexSequence =
32 std::make_index_sequence<sizeof...(Pendables)>();
33 using TupleOfOutputRvalues = std::tuple<PendOutputOf<Pendables>&&...>;
34
35 public:
37 explicit Join(Pendables&&... pendables)
38 : pendables_(std::move(pendables)...),
39 outputs_(Poll<PendOutputOf<Pendables>>(Pending())...) {}
40
44 if (!PendElements(cx, kTupleIndexSequence)) {
45 return Pending();
46 }
47 return TakeOutputs(kTupleIndexSequence);
48 }
49
50 private:
54 template <size_t... Is>
55 bool PendElements(Context& cx, std::index_sequence<Is...>) {
56 return (... && PendElement<Is>(cx));
57 }
58
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>()...));
64 }
65
71 template <size_t kTupleIndex>
72 bool PendElement(Context& cx) {
73 auto& output = std::get<kTupleIndex>(outputs_);
74 if (output.IsReady()) {
75 return true;
76 }
77 output = std::get<kTupleIndex>(pendables_).Pend(cx);
78 return output.IsReady();
79 }
80
82 template <size_t kTupleIndex>
83 PendOutputOf<typename std::tuple_element<kTupleIndex,
84 std::tuple<Pendables...>>::type>&&
85 TakeOutput() {
86 return std::move(std::get<kTupleIndex>(outputs_).value());
87 }
88
89 std::tuple<Pendables...> pendables_;
90 std::tuple<Poll<PendOutputOf<Pendables>>...> outputs_;
91};
92
93template <typename... Pendables>
94Join(Pendables&&...) -> Join<Pendables...>;
95
97
98namespace experimental {
99
100template <typename... Futures>
102 : public Future<JoinFuture<Futures...>,
103 std::tuple<typename Futures::value_type&&...>> {
104 private:
105 static constexpr auto kTupleIndexSequence =
106 std::make_index_sequence<sizeof...(Futures)>();
107 using TupleOfOutputRvalues = std::tuple<typename Futures::value_type&&...>;
108
109 using Base = Future<JoinFuture<Futures...>, TupleOfOutputRvalues>;
110 friend Base;
111
112 template <typename... Fs>
113 friend constexpr auto Join(Fs&&...);
114
115 explicit constexpr JoinFuture(Futures&&... futures)
116 : futures_(std::in_place, std::move(futures)...),
118
122 if (!PendElements(cx, kTupleIndexSequence)) {
123 return Pending();
124 }
125 return TakeOutputs(kTupleIndexSequence);
126 }
127
128 void DoMarkComplete() { futures_.reset(); }
129 bool DoIsComplete() const { return !futures_.has_value(); }
130
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() && ...);
138 }
139
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);
147 }
148 }
149
151 template <size_t... Is>
152 Poll<TupleOfOutputRvalues> TakeOutputs(std::index_sequence<Is...>) {
154 std::forward_as_tuple<PendOutputOf<Futures>...>(TakeOutput<Is>()...));
155 }
156
158 template <size_t kTupleIndex>
159 PendOutputOf<
160 typename std::tuple_element<kTupleIndex, std::tuple<Futures...>>::type>&&
161 TakeOutput() {
162 return std::move(std::get<kTupleIndex>(outputs_).value());
163 }
164
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_;
169};
170
171template <typename... Futures>
172JoinFuture(Futures&&...) -> JoinFuture<Futures...>;
173
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)...);
184}
185
186} // namespace experimental
187
188} // namespace pw::async2
Definition: context.h:55
Definition: join.h:29
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
Definition: poll.h:60
Definition: future.h:60
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271