proxygen
FutureExecutor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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 
17 #pragma once
18 
20 #include <folly/futures/Future.h>
21 
22 namespace folly {
23 
24 template <typename ExecutorImpl>
25 class FutureExecutor : public ExecutorImpl {
26  public:
27  template <typename... Args>
28  explicit FutureExecutor(Args&&... args)
29  : ExecutorImpl(std::forward<Args>(args)...) {}
30 
31  /*
32  * Given a function func that returns a Future<T>, adds that function to the
33  * contained Executor and returns a Future<T> which will be fulfilled with
34  * func's result once it has been executed.
35  *
36  * For example: auto f = futureExecutor.addFuture([](){
37  * return doAsyncWorkAndReturnAFuture();
38  * });
39  */
40  template <typename F>
41  typename std::enable_if<
44  addFuture(F func) {
45  using T = typename invoke_result_t<F>::value_type;
46  folly::Promise<T> promise;
47  auto future = promise.getFuture();
48  ExecutorImpl::add([promise = std::move(promise),
49  func = std::move(func)]() mutable {
50  func().then([promise = std::move(promise)](folly::Try<T>&& t) mutable {
51  promise.setTry(std::move(t));
52  });
53  });
54  return future;
55  }
56 
57  /*
58  * Similar to addFuture above, but takes a func that returns some non-Future
59  * type T.
60  *
61  * For example: auto f = futureExecutor.addFuture([]() {
62  * return 42;
63  * });
64  */
65  template <typename F>
66  typename std::enable_if<
69  addFuture(F func) {
71  folly::Promise<T> promise;
72  auto future = promise.getFuture();
74  [promise = std::move(promise), func = std::move(func)]() mutable {
75  promise.setWith(std::move(func));
76  });
77  return future;
78  }
79 };
80 
81 } // namespace folly
typename invoke_result< F, Args... >::type invoke_result_t
Definition: Invoke.h:142
auto add
Definition: BaseTest.cpp:70
PskType type
void setWith(F &&func)
Definition: Promise-inl.h:137
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::enable_if< folly::isFuture< invoke_result_t< F > >::value, invoke_result_t< F > >::type addFuture(F func)
STL namespace.
folly::std T
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void setTry(Try< T > &&t)
Definition: Promise-inl.h:122
Future< T > getFuture()
Definition: Promise-inl.h:97
Definition: Try.h:51
FutureExecutor(Args &&...args)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
std::enable_if< !folly::isFuture< invoke_result_t< F > >::value, folly::Future< typename folly::lift_unit< invoke_result_t< F > >::type > >::type addFuture(F func)