Debian Codegraph @

src/core/lib/promise/seq.h

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef GRPC_CORE_LIB_PROMISE_SEQ_H
16 #define GRPC_CORE_LIB_PROMISE_SEQ_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <utility>
21
22 #include "src/core/lib/promise/detail/basic_seq.h"
23 #include "src/core/lib/promise/detail/promise_like.h"
24 #include "src/core/lib/promise/poll.h"
25
26 namespace grpc_core {
27
28 namespace promise_detail {
29
30 template <typename T>
31 struct SeqTraits {
32 using UnwrappedType = T;
33 using WrappedType = T;
34 template <typename Next>
35 static auto CallFactory(Next* next, T&& value) {
36 return next->Make(std::forward<T>(value));
37 }
38 template <typename F, typename Elem>
39 static auto CallSeqFactory(F& f, Elem&& elem, T&& value) {
40 return f(std::forward<Elem>(elem), std::forward<T>(value));
41 }
42 template <typename Result, typename PriorResult, typename RunNext>
43 static Poll<Result> CheckResultAndRunNext(PriorResult prior,
44 RunNext run_next) {
45 return run_next(std::move(prior));
46 }
47 };
48
49 template <typename... Fs>
50 using Seq = BasicSeq<SeqTraits, Fs...>;
51
52 template <typename I, typename F, typename Arg>
53 struct SeqIterTraits {
54 using Iter = I;
55 using Factory = F;
56 using Argument = Arg;
57 using IterValue = decltype(*std::declval<Iter>());
58 using StateCreated = decltype(std::declval<F>()(std::declval<IterValue>(),
59 std::declval<Arg>()));
60 using State = PromiseLike<StateCreated>;
61 using Wrapped = typename State::Result;
62
63 using Traits = SeqTraits<Wrapped>;
64 };
65
66 template <typename Iter, typename Factory, typename Argument>
67 struct SeqIterResultTraits {
68 using IterTraits = SeqIterTraits<Iter, Factory, Argument>;
69 using Result = BasicSeqIter<IterTraits>;
70 };
71
72 } // namespace promise_detail
73
74 // Sequencing combinator.
75 // Run the first promise.
76 // Pass its result to the second, and run the returned promise.
77 // Pass its result to the third, and run the returned promise.
78 // etc
79 // Return the final value.
80 template <typename... Functors>
81 promise_detail::Seq<Functors...> Seq(Functors... functors) {
82 return promise_detail::Seq<Functors...>(std::move(functors)...);
83 }
84
85 template <typename F>
86 F Seq(F functor) {
87 return functor;
88 }
89
90 // Execute a sequence of operations of unknown length.
91 // Asynchronously:
92 // for (element in (begin, end)) {
93 // argument = wait_for factory(element, argument);
94 // }
95 // return argument;
96 template <typename Iter, typename Factory, typename Argument>
97 typename promise_detail::SeqIterResultTraits<Iter, Factory, Argument>::Result
98 SeqIter(Iter begin, Iter end, Argument argument, Factory factory) {
99 using Result = typename promise_detail::SeqIterResultTraits<Iter, Factory,
100 Argument>::Result;
101 return Result(begin, end, std::move(factory), std::move(argument));
102 }
103
104 } // namespace grpc_core
105
106 #endif // GRPC_CORE_LIB_PROMISE_SEQ_H