proxygen
helpers.h
Go to the documentation of this file.
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 #include <atomic>
19 #include <tuple>
20 #include <utility>
21 
22 #include <folly/Portability.h>
23 #include <folly/Try.h>
25 #include <folly/futures/Future.h>
26 #include <folly/futures/Promise.h>
27 
28 namespace folly {
29 
36 namespace futures {
48 Future<Unit> sleep(Duration, Timekeeper* = nullptr);
49 
54 template <
55  class It,
56  class F,
57  class ItT = typename std::iterator_traits<It>::value_type,
58  class Result = typename decltype(
59  std::declval<ItT>().then(std::declval<F>()))::value_type>
60 std::vector<Future<Result>> map(It first, It last, F func);
61 
67 template <
68  class It,
69  class F,
70  class ItT = typename std::iterator_traits<It>::value_type,
71  class Result = typename decltype(std::move(std::declval<ItT>())
72  .via(std::declval<Executor*>())
73  .then(std::declval<F>()))::value_type>
74 std::vector<Future<Result>> map(Executor& exec, It first, It last, F func);
75 
76 // Sugar for the most common case
77 template <class Collection, class F>
78 auto map(Collection&& c, F&& func) -> decltype(map(c.begin(), c.end(), func)) {
79  return map(c.begin(), c.end(), std::forward<F>(func));
80 }
81 
82 // Sugar for the most common case
83 template <class Collection, class F>
84 auto map(Executor& exec, Collection&& c, F&& func)
85  -> decltype(map(exec, c.begin(), c.end(), func)) {
86  return map(exec, c.begin(), c.end(), std::forward<F>(func));
87 }
88 
89 } // namespace futures
90 
101 template <class T>
103 
106 
121 // makeSemiFutureWith(SemiFuture<T>()) -> SemiFuture<T>
122 template <class F>
123 typename std::enable_if<
126 makeSemiFutureWith(F&& func);
127 
128 // makeSemiFutureWith(T()) -> SemiFuture<T>
129 // makeSemiFutureWith(void()) -> SemiFuture<Unit>
130 template <class F>
131 typename std::enable_if<
134 makeSemiFutureWith(F&& func);
135 
140 template <class T>
141 [[deprecated("use makeSemiFuture(exception_wrapper)")]] SemiFuture<T>
142 makeSemiFuture(std::exception_ptr const& e);
143 
145 template <class T>
147 
150 template <class T, class E>
151 typename std::
153  makeSemiFuture(E const& e);
154 
156 template <class T>
158 
173 template <class T>
175 
184 
206 // makeFutureWith(Future<T>()) -> Future<T>
207 template <class F>
208 typename std::
209  enable_if<isFuture<invoke_result_t<F>>::value, invoke_result_t<F>>::type
210  makeFutureWith(F&& func);
211 
212 // makeFutureWith(T()) -> Future<T>
213 // makeFutureWith(void()) -> Future<Unit>
214 template <class F>
215 typename std::enable_if<
218 makeFutureWith(F&& func);
219 
224 template <class T>
225 [[deprecated("use makeSemiFuture(exception_wrapper)")]] Future<T> makeFuture(
226  std::exception_ptr const& e);
227 
232 template <class T>
234 
242 template <class T, class E>
244  type
245  makeFuture(E const& e);
246 
254 template <class T>
256 
257 /*
258  * Return a new Future that will call back on the given Executor.
259  * This is just syntactic sugar for makeFuture().via(executor)
260  *
261  * @param executor the Executor to call back on
262  * @param priority optionally, the priority to add with. Defaults to 0 which
263  * represents medium priority.
264  *
265  * @returns a void Future that will call back on the given executor
266  */
267 inline Future<Unit> via(
269  int8_t priority = Executor::MID_PRI);
270 
271 inline Future<Unit> via(
272  Executor::KeepAlive<> executor,
273  int8_t priority = Executor::MID_PRI);
274 
278 template <class Func>
279 auto via(Executor*, Func&& func) -> Future<
281 
282 template <class Func>
283 auto via(Executor::KeepAlive<>, Func&& func) -> Future<
285 
301 template <class InputIterator>
302 SemiFuture<std::vector<
304 collectAllSemiFuture(InputIterator first, InputIterator last);
305 
307 template <class Collection>
308 auto collectAllSemiFuture(Collection&& c)
309  -> decltype(collectAllSemiFuture(c.begin(), c.end())) {
310  return collectAllSemiFuture(c.begin(), c.end());
311 }
312 
313 template <class InputIterator>
314 Future<std::vector<
316 collectAll(InputIterator first, InputIterator last);
317 
318 template <class Collection>
319 auto collectAll(Collection&& c) -> decltype(collectAll(c.begin(), c.end())) {
320  return collectAll(c.begin(), c.end());
321 }
322 
327 template <typename... Fs>
329 collectAllSemiFuture(Fs&&... fs);
330 
331 template <typename... Fs>
333  Fs&&... fs);
337 template <class InputIterator>
338 Future<std::vector<
339  typename std::iterator_traits<InputIterator>::value_type::value_type>>
340 collect(InputIterator first, InputIterator last);
341 
343 template <class Collection>
344 auto collect(Collection&& c) -> decltype(collect(c.begin(), c.end())) {
345  return collect(c.begin(), c.end());
346 }
347 
351 template <typename... Fs>
353  Fs&&... fs);
354 
361 template <class InputIterator>
362 Future<std::pair<
363  size_t,
365 collectAny(InputIterator first, InputIterator last);
366 
368 template <class Collection>
369 auto collectAny(Collection&& c) -> decltype(collectAny(c.begin(), c.end())) {
370  return collectAny(c.begin(), c.end());
371 }
372 
377 template <class InputIterator>
378 SemiFuture<std::pair<
379  size_t,
380  typename std::iterator_traits<InputIterator>::value_type::value_type>>
381 collectAnyWithoutException(InputIterator first, InputIterator last);
382 
384 template <class Collection>
385 auto collectAnyWithoutException(Collection&& c)
386  -> decltype(collectAnyWithoutException(c.begin(), c.end())) {
387  return collectAnyWithoutException(c.begin(), c.end());
388 }
389 
396 template <class InputIterator>
397 SemiFuture<std::vector<std::pair<
398  size_t,
400 collectN(InputIterator first, InputIterator last, size_t n);
401 
403 template <class Collection>
404 auto collectN(Collection&& c, size_t n)
405  -> decltype(collectN(c.begin(), c.end(), n)) {
406  return collectN(c.begin(), c.end(), n);
407 }
408 
417 template <
418  class Collection,
419  class F,
420  class ItT = typename std::iterator_traits<
421  typename Collection::iterator>::value_type,
422  class Result = typename invoke_result_t<F, ItT&&>::value_type>
423 std::vector<Future<Result>> window(Collection input, F func, size_t n);
424 
425 template <
426  class Collection,
427  class F,
428  class ItT = typename std::iterator_traits<
429  typename Collection::iterator>::value_type,
430  class Result = typename invoke_result_t<F, ItT&&>::value_type>
431 std::vector<Future<Result>>
432 window(Executor* executor, Collection input, F func, size_t n);
433 
434 template <
435  class Collection,
436  class F,
437  class ItT = typename std::iterator_traits<
438  typename Collection::iterator>::value_type,
439  class Result = typename invoke_result_t<F, ItT&&>::value_type>
440 std::vector<Future<Result>>
441 window(Executor::KeepAlive<> executor, Collection input, F func, size_t n);
442 
443 template <typename F, typename T, typename ItT>
444 using MaybeTryArg = typename std::
445  conditional<is_invocable<F, T&&, Try<ItT>&&>::value, Try<ItT>, ItT>::type;
446 
457 template <class It, class T, class F>
458 Future<T> reduce(It first, It last, T&& initial, F&& func);
459 
461 template <class Collection, class T, class F>
462 auto reduce(Collection&& c, T&& initial, F&& func) -> decltype(reduce(
463  c.begin(),
464  c.end(),
465  std::forward<T>(initial),
466  std::forward<F>(func))) {
467  return reduce(
468  c.begin(), c.end(), std::forward<T>(initial), std::forward<F>(func));
469 }
470 
474 template <class It, class T, class F>
475 Future<T> unorderedReduce(It first, It last, T initial, F func);
476 
478 template <class Collection, class T, class F>
479 auto unorderedReduce(Collection&& c, T&& initial, F&& func)
480  -> decltype(unorderedReduce(
481  c.begin(),
482  c.end(),
483  std::forward<T>(initial),
484  std::forward<F>(func))) {
485  return unorderedReduce(
486  c.begin(), c.end(), std::forward<T>(initial), std::forward<F>(func));
487 }
488 } // namespace folly
static const int8_t MID_PRI
Definition: Executor.h:49
typename invoke_result< F, Args... >::type invoke_result_t
Definition: Invoke.h:142
PskType type
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
Future< T > unorderedReduce(It first, It last, T initial, F func)
Definition: Future-inl.h:1881
SemiFuture< std::tuple< Try< typename remove_cvref_t< Fs >::value_type >... > > collectAllSemiFuture(Fs &&...fs)
Definition: Future-inl.h:1441
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
SemiFuture< std::vector< std::pair< size_t, Try< typename std::iterator_traits< InputIterator >::value_type::value_type > > > > collectN(InputIterator first, InputIterator last, size_t n)
Definition: Future-inl.h:1692
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
std::vector< Future< Result > > map(It first, It last, F func)
Definition: Future-inl.h:2358
std::vector< Future< Result > > window(Collection input, F func, size_t n)
Definition: Future-inl.h:1789
SemiFuture< std::pair< size_t, typename std::iterator_traits< InputIterator >::value_type::value_type > > collectAnyWithoutException(InputIterator first, InputIterator last)
Definition: Future-inl.h:1644
std::enable_if< isFutureOrSemiFuture< invoke_result_t< F > >::value, SemiFuture< typename invoke_result_t< F >::value_type > >::type makeSemiFutureWith(F &&func)
Definition: Future-inl.h:721
Future< std::pair< size_t, Try< typename std::iterator_traits< InputIterator >::value_type::value_type > > > collectAny(InputIterator first, InputIterator last)
Definition: Future-inl.h:1618
Future< T > reduce(It first, It last, T &&initial, F &&func)
Definition: Future-inl.h:1753
Future< std::tuple< Try< typename remove_cvref_t< Fs >::value_type >... > > collectAll(Fs &&...fs)
Definition: Future-inl.h:1477
Definition: Traits.h:594
Definition: Traits.h:588
static const char *const value
Definition: Conv.cpp:50
Definition: Try.h:51
Future< std::vector< typename std::iterator_traits< InputIterator >::value_type::value_type > > collect(InputIterator first, InputIterator last)
Definition: Future-inl.h:1536
StatsClock::duration Duration
typename std::conditional< is_invocable< F, T &&, Try< ItT > && >::value, Try< ItT >, ItT >::type MaybeTryArg
Definition: helpers.h:445
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
auto via(Executor *x, Func &&func) -> Future< typename isFutureOrSemiFuture< decltype(std::declval< Func >()())>::Inner >
Definition: Future-inl.h:1290
char c
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
std::enable_if< isFuture< invoke_result_t< F > >::value, invoke_result_t< F > >::type makeFutureWith(F &&func)
Definition: Future-inl.h:1322
SemiFuture< typename std::decay< T >::type > makeSemiFuture(T &&t)
Definition: Future-inl.h:712
constexpr detail::First first
Definition: Base-inl.h:2553