1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INCLUDE_PERFETTO_EXT_BASE_THREADING_FUTURE_COMBINATORS_H_ 18 #define INCLUDE_PERFETTO_EXT_BASE_THREADING_FUTURE_COMBINATORS_H_ 19 20 #include <optional> 21 #include <utility> 22 #include <vector> 23 24 #include "perfetto/base/status.h" 25 #include "perfetto/ext/base/threading/poll.h" 26 27 namespace perfetto { 28 namespace base { 29 30 template <typename T> 31 class Future; 32 33 // For a Function which Future<U>, returns the U. 34 template <typename Function, typename T> 35 using FutureReturn = typename std::invoke_result<Function, T>::type::PollT; 36 37 // Implementation of FuturePollable for creating a Future<T> from a T. 38 template <typename T> 39 class ImmediateImpl : public FuturePollable<T> { 40 public: ImmediateImpl(T value)41 explicit ImmediateImpl(T value) : value_(std::move(value)) {} 42 Poll(PollContext *)43 FuturePollResult<T> Poll(PollContext*) override { return std::move(value_); } 44 45 private: 46 T value_; 47 }; 48 49 // Implementation of FuturePollable backing Future::ContinueWith. 50 template <typename Function, typename A, typename B = FutureReturn<Function, A>> 51 class ContinueWithImpl : public FuturePollable<B> { 52 public: ContinueWithImpl(Future<A> first,Function second_fn)53 ContinueWithImpl(Future<A> first, Function second_fn) 54 : first_(std::move(first)), second_fn_(std::move(second_fn)) {} 55 Poll(PollContext * context)56 FuturePollResult<B> Poll(PollContext* context) override { 57 PERFETTO_CHECK((first_ && second_fn_) || second_); 58 if (first_) { 59 ASSIGN_OR_RETURN_IF_PENDING_FUTURE(res, first_->Poll(context)); 60 first_ = std::nullopt; 61 second_ = (*second_fn_)(std::move(res)); 62 second_fn_ = std::nullopt; 63 } 64 return second_->Poll(context); 65 } 66 67 private: 68 std::optional<Future<A>> first_; 69 std::optional<Function> second_fn_; 70 std::optional<Future<B>> second_; 71 }; 72 73 } // namespace base 74 } // namespace perfetto 75 76 #endif // INCLUDE_PERFETTO_EXT_BASE_THREADING_FUTURE_COMBINATORS_H_ 77