4dee6099c22e2176b969a6763e42c0dc8759466f
[folly.git] / folly / experimental / fibers / Promise.h
1 /*
2  * Copyright 2016 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 <folly/experimental/fibers/traits.h>
19 #include <folly/futures/Try.h>
20
21 namespace folly {
22 namespace fibers {
23
24 class Baton;
25
26 template <typename T>
27 class Promise {
28  public:
29   typedef T value_type;
30
31   ~Promise();
32
33   // not copyable
34   Promise(const Promise&) = delete;
35   Promise& operator=(const Promise&) = delete;
36
37   // movable
38   Promise(Promise&&) noexcept;
39   Promise& operator=(Promise&&);
40
41   /** Fulfill this promise (only for Promise<void>) */
42   void setValue();
43
44   /** Set the value (use perfect forwarding for both move and copy) */
45   template <class M>
46   void setValue(M&& value);
47
48   /**
49    * Fulfill the promise with a given try
50    *
51    * @param t
52    */
53   void setTry(folly::Try<T>&& t);
54
55   /** Fulfill this promise with the result of a function that takes no
56     arguments and returns something implicitly convertible to T.
57     Captures exceptions. e.g.
58
59     p.setWith([] { do something that may throw; return a T; });
60   */
61   template <class F>
62   void setWith(F&& func);
63
64   /** Fulfill the Promise with an exception_wrapper, e.g.
65     auto ew = folly::try_and_catch<std::exception>([]{ ... });
66     if (ew) {
67       p.setException(std::move(ew));
68     }
69     */
70   void setException(folly::exception_wrapper);
71
72   /**
73    * Blocks task execution until given promise is fulfilled.
74    *
75    * Calls function passing in a Promise<T>, which has to be fulfilled.
76    *
77    * @return data which was used to fulfill the promise.
78    */
79   template <class F>
80   static value_type await(F&& func);
81
82  private:
83   Promise(folly::Try<T>& value, Baton& baton);
84   folly::Try<T>* value_;
85   Baton* baton_;
86
87   void throwIfFulfilled() const;
88
89   template <class F>
90   typename std::enable_if<
91       std::is_convertible<typename std::result_of<F()>::type, T>::value &&
92       !std::is_same<T, void>::value>::type
93   fulfilHelper(F&& func);
94
95   template <class F>
96   typename std::enable_if<
97       std::is_same<typename std::result_of<F()>::type, void>::value &&
98       std::is_same<T, void>::value>::type
99   fulfilHelper(F&& func);
100 };
101 }
102 }
103
104 #include <folly/experimental/fibers/Promise-inl.h>