1 /*
2 * Copyright 2020 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 #pragma once
18
19 #include <future>
20 #include <type_traits>
21 #include <utility>
22
23 namespace android::ftl {
24
25 // Creates a future that defers a function call until its result is queried.
26 //
27 // auto future = ftl::defer([](int x) { return x + 1; }, 99);
28 // assert(future.get() == 100);
29 //
30 template <typename F, typename... Args>
defer(F && f,Args &&...args)31 inline auto defer(F&& f, Args&&... args) {
32 return std::async(std::launch::deferred, std::forward<F>(f), std::forward<Args>(args)...);
33 }
34
35 // Creates a future that wraps a value.
36 //
37 // auto future = ftl::yield(42);
38 // assert(future.get() == 42);
39 //
40 // auto ptr = std::make_unique<char>('!');
41 // auto future = ftl::yield(std::move(ptr));
42 // assert(*future.get() == '!');
43 //
44 template <typename T>
yield(T && v)45 inline std::future<T> yield(T&& v) {
46 return defer([](T&& v) { return std::forward<T>(v); }, std::forward<T>(v));
47 }
48
49 namespace details {
50
51 template <typename T>
52 struct future_result {
53 using type = T;
54 };
55
56 template <typename T>
57 struct future_result<std::future<T>> {
58 using type = T;
59 };
60
61 template <typename T>
62 using future_result_t = typename future_result<T>::type;
63
64 // Attaches a continuation to a future. The continuation is a function that maps T to either R or
65 // std::future<R>. In the former case, the chain wraps the result in a future as if by ftl::yield.
66 //
67 // auto future = ftl::yield(123);
68 // std::future<char> futures[] = {ftl::yield('a'), ftl::yield('b')};
69 //
70 // std::future<char> chain =
71 // ftl::chain(std::move(future))
72 // .then([](int x) { return static_cast<std::size_t>(x % 2); })
73 // .then([&futures](std::size_t i) { return std::move(futures[i]); });
74 //
75 // assert(chain.get() == 'b');
76 //
77 template <typename T>
78 struct Chain {
79 // Implicit conversion.
80 Chain(std::future<T>&& f) : future(std::move(f)) {}
81 operator std::future<T>&&() && { return std::move(future); }
82
83 T get() && { return future.get(); }
84
85 template <typename F, typename R = std::invoke_result_t<F, T>>
86 auto then(F&& op) && -> Chain<future_result_t<R>> {
87 return defer(
88 [](auto&& f, F&& op) {
89 R r = op(f.get());
90 if constexpr (std::is_same_v<R, future_result_t<R>>) {
91 return r;
92 } else {
93 return r.get();
94 }
95 },
96 std::move(future), std::forward<F>(op));
97 }
98
99 std::future<T> future;
100 };
101
102 } // namespace details
103
104 template <typename T>
105 inline auto chain(std::future<T>&& f) -> details::Chain<T> {
106 return std::move(f);
107 }
108
109 } // namespace android::ftl
110