Pigweed
 
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
18namespace pw::async2 {
19
25template <typename... Pendables>
26class Join {
27 private:
28 static constexpr auto kTupleIndexSequence =
29 std::make_index_sequence<sizeof...(Pendables)>();
30 using TupleOfOutputRvalues = std::tuple<PendOutputOf<Pendables>&&...>;
31
32 public:
34 explicit Join(Pendables&&... pendables)
35 : pendables_(std::move(pendables)...),
36 outputs_(Poll<PendOutputOf<Pendables>>(Pending())...) {}
37
41 if (!PendElements(cx, kTupleIndexSequence)) {
42 return Pending();
43 }
44 return TakeOutputs(kTupleIndexSequence);
45 }
46
47 private:
51 template <size_t... Is>
52 bool PendElements(Context& cx, std::index_sequence<Is...>) {
53 return (... && PendElement<Is>(cx));
54 }
55
57 template <size_t... Is>
58 Poll<TupleOfOutputRvalues> TakeOutputs(std::index_sequence<Is...>) {
59 return Poll<TupleOfOutputRvalues>(
60 std::forward_as_tuple<PendOutputOf<Pendables>...>(TakeOutput<Is>()...));
61 }
62
68 template <size_t kTupleIndex>
69 bool PendElement(Context& cx) {
70 auto& output = std::get<kTupleIndex>(outputs_);
71 if (output.IsReady()) {
72 return true;
73 }
74 output = std::get<kTupleIndex>(pendables_).Pend(cx);
75 return output.IsReady();
76 }
77
79 template <size_t kTupleIndex>
80 PendOutputOf<typename std::tuple_element<kTupleIndex,
81 std::tuple<Pendables...>>::type>&&
82 TakeOutput() {
83 return std::move(std::get<kTupleIndex>(outputs_).value());
84 }
85
86 std::tuple<Pendables...> pendables_;
87 std::tuple<Poll<PendOutputOf<Pendables>>...> outputs_;
88};
89
90} // namespace pw::async2
Definition: dispatcher_base.h:52
Definition: join.h:26
Join(Pendables &&... pendables)
Creates a Join from a series of pendable values.
Definition: join.h:34
Poll< TupleOfOutputRvalues > Pend(Context &cx)
Definition: join.h:40
Definition: poll.h:54