1 | /* |
2 | * Copyright 2015-present Facebook, Inc. |
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 | #pragma once |
17 | |
18 | // included by Future.h, do not include directly. |
19 | |
20 | namespace folly { |
21 | |
22 | template <class> |
23 | class Promise; |
24 | |
25 | template <class T> |
26 | class SemiFuture; |
27 | |
28 | template <typename T> |
29 | struct isSemiFuture : std::false_type { |
30 | using Inner = lift_unit_t<T>; |
31 | }; |
32 | |
33 | template <typename T> |
34 | struct isSemiFuture<SemiFuture<T>> : std::true_type { |
35 | typedef T Inner; |
36 | }; |
37 | |
38 | template <typename T> |
39 | struct isFuture : std::false_type { |
40 | using Inner = lift_unit_t<T>; |
41 | }; |
42 | |
43 | template <typename T> |
44 | struct isFuture<Future<T>> : std::true_type { |
45 | typedef T Inner; |
46 | }; |
47 | |
48 | template <typename T> |
49 | struct isFutureOrSemiFuture : std::false_type { |
50 | using Inner = lift_unit_t<T>; |
51 | using Return = Inner; |
52 | }; |
53 | |
54 | template <typename T> |
55 | struct isFutureOrSemiFuture<Future<T>> : std::true_type { |
56 | typedef T Inner; |
57 | using Return = Future<Inner>; |
58 | }; |
59 | |
60 | template <typename T> |
61 | struct isFutureOrSemiFuture<SemiFuture<T>> : std::true_type { |
62 | typedef T Inner; |
63 | using Return = SemiFuture<Inner>; |
64 | }; |
65 | |
66 | template <typename T> |
67 | struct isTry : std::false_type {}; |
68 | |
69 | template <typename T> |
70 | struct isTry<Try<T>> : std::true_type {}; |
71 | |
72 | namespace futures { |
73 | namespace detail { |
74 | |
75 | template <class> |
76 | class Core; |
77 | |
78 | template <typename...> |
79 | struct ArgType; |
80 | |
81 | template <typename Arg, typename... Args> |
82 | struct ArgType<Arg, Args...> { |
83 | typedef Arg FirstArg; |
84 | }; |
85 | |
86 | template <> |
87 | struct ArgType<> { |
88 | typedef void FirstArg; |
89 | }; |
90 | |
91 | template <bool isTry_, typename F, typename... Args> |
92 | struct argResult { |
93 | using Function = F; |
94 | using ArgList = ArgType<Args...>; |
95 | using Result = invoke_result_t<F, Args...>; |
96 | using ArgsSize = index_constant<sizeof...(Args)>; |
97 | static constexpr bool isTry() { |
98 | return isTry_; |
99 | } |
100 | }; |
101 | |
102 | template <typename T, typename F> |
103 | struct callableResult { |
104 | typedef typename std::conditional< |
105 | is_invocable<F>::value, |
106 | detail::argResult<false, F>, |
107 | typename std::conditional< |
108 | is_invocable<F, T&&>::value, |
109 | detail::argResult<false, F, T&&>, |
110 | detail::argResult<true, F, Try<T>&&>>::type>::type Arg; |
111 | typedef isFutureOrSemiFuture<typename Arg::Result> ReturnsFuture; |
112 | typedef Future<typename ReturnsFuture::Inner> Return; |
113 | }; |
114 | |
115 | template < |
116 | typename T, |
117 | typename F, |
118 | typename = std::enable_if_t<is_invocable<F, Try<T>&&>::value>> |
119 | struct tryCallableResult { |
120 | typedef detail::argResult<true, F, Try<T>&&> Arg; |
121 | typedef isFutureOrSemiFuture<typename Arg::Result> ReturnsFuture; |
122 | typedef typename ReturnsFuture::Inner value_type; |
123 | typedef Future<value_type> Return; |
124 | }; |
125 | |
126 | template < |
127 | typename T, |
128 | typename F, |
129 | typename = std::enable_if_t<is_invocable<F, Executor*, Try<T>&&>::value>> |
130 | struct tryExecutorCallableResult { |
131 | typedef detail::argResult<true, F, Executor*, Try<T>&&> Arg; |
132 | typedef isFutureOrSemiFuture<typename Arg::Result> ReturnsFuture; |
133 | typedef typename ReturnsFuture::Inner value_type; |
134 | typedef Future<value_type> Return; |
135 | }; |
136 | |
137 | template <typename T, typename F> |
138 | struct valueCallableResult { |
139 | typedef detail::argResult<false, F, T&&> Arg; |
140 | typedef isFutureOrSemiFuture<typename Arg::Result> ReturnsFuture; |
141 | typedef typename ReturnsFuture::Inner value_type; |
142 | typedef typename Arg::ArgList::FirstArg FirstArg; |
143 | typedef Future<value_type> Return; |
144 | }; |
145 | |
146 | template <typename L> |
147 | struct : Extract<decltype(&L::operator())> {}; |
148 | |
149 | template <typename Class, typename R, typename... Args> |
150 | struct <R (Class::*)(Args...) const> { |
151 | typedef isFutureOrSemiFuture<R> ; |
152 | typedef Future<typename ReturnsFuture::Inner> ; |
153 | typedef typename ReturnsFuture::Inner ; |
154 | typedef typename ArgType<Args...>::FirstArg ; |
155 | }; |
156 | |
157 | template <typename Class, typename R, typename... Args> |
158 | struct <R (Class::*)(Args...)> { |
159 | typedef isFutureOrSemiFuture<R> ; |
160 | typedef Future<typename ReturnsFuture::Inner> ; |
161 | typedef typename ReturnsFuture::Inner ; |
162 | typedef typename ArgType<Args...>::FirstArg ; |
163 | }; |
164 | |
165 | template <typename R, typename... Args> |
166 | struct <R (*)(Args...)> { |
167 | typedef isFutureOrSemiFuture<R> ; |
168 | typedef Future<typename ReturnsFuture::Inner> ; |
169 | typedef typename ReturnsFuture::Inner ; |
170 | typedef typename ArgType<Args...>::FirstArg ; |
171 | }; |
172 | |
173 | template <typename R, typename... Args> |
174 | struct <R (&)(Args...)> { |
175 | typedef isFutureOrSemiFuture<R> ; |
176 | typedef Future<typename ReturnsFuture::Inner> ; |
177 | typedef typename ReturnsFuture::Inner ; |
178 | typedef typename ArgType<Args...>::FirstArg ; |
179 | }; |
180 | |
181 | class DeferredExecutor; |
182 | |
183 | } // namespace detail |
184 | } // namespace futures |
185 | |
186 | class Timekeeper; |
187 | |
188 | } // namespace folly |
189 | |