42477263fabc6c75862304807543668f8ba06c89
[folly.git] / folly / futures / Promise.h
1 /*
2  * Copyright 2017 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
19 #include <folly/Portability.h>
20 #include <folly/Try.h>
21 #include <functional>
22
23 namespace folly {
24
25 // forward declaration
26 template <class T> class Future;
27
28 namespace futures {
29 namespace detail {
30 struct EmptyConstruct {};
31 template <typename T, typename F>
32 class CoreCallbackState;
33 } // namespace detail
34 } // namespace futures
35
36 template <class T>
37 class Promise {
38  public:
39   static Promise<T> makeEmpty() noexcept; // equivalent to moved-from
40
41   Promise();
42   ~Promise();
43
44   // not copyable
45   Promise(Promise const&) = delete;
46   Promise& operator=(Promise const&) = delete;
47
48   // movable
49   Promise(Promise<T>&&) noexcept;
50   Promise& operator=(Promise<T>&&) noexcept;
51
52   /** Return a Future tied to the shared core state. This can be called only
53     once, thereafter Future already retrieved exception will be raised. */
54   Future<T> getFuture();
55
56   /** Fulfill the Promise with an exception_wrapper */
57   void setException(exception_wrapper ew);
58
59   /** Fulfill the Promise with an exception_ptr, e.g.
60     try {
61       ...
62     } catch (...) {
63       p.setException(std::current_exception());
64     }
65     */
66   FOLLY_DEPRECATED("use setException(exception_wrapper)")
67   void setException(std::exception_ptr const&);
68
69   /** Fulfill the Promise with an exception type E, which can be passed to
70     std::make_exception_ptr(). Useful for originating exceptions. If you
71     caught an exception the exception_wrapper form is more appropriate.
72     */
73   template <class E>
74   typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
75   setException(E const&);
76
77   /// Set an interrupt handler to handle interrupts. See the documentation for
78   /// Future::raise(). Your handler can do whatever it wants, but if you
79   /// bother to set one then you probably will want to fulfill the promise with
80   /// an exception (or special value) indicating how the interrupt was
81   /// handled.
82   void setInterruptHandler(std::function<void(exception_wrapper const&)>);
83
84   /// Sugar to fulfill this Promise<Unit>
85   template <class B = T>
86   typename std::enable_if<std::is_same<Unit, B>::value, void>::type
87   setValue() {
88     setTry(Try<T>(T()));
89   }
90
91   /** Set the value (use perfect forwarding for both move and copy) */
92   template <class M>
93   void setValue(M&& value);
94
95   void setTry(Try<T>&& t);
96
97   /** Fulfill this Promise with the result of a function that takes no
98     arguments and returns something implicitly convertible to T.
99     Captures exceptions. e.g.
100
101     p.setWith([] { do something that may throw; return a T; });
102   */
103   template <class F>
104   void setWith(F&& func);
105
106   bool isFulfilled() const noexcept;
107
108  private:
109   typedef typename Future<T>::corePtr corePtr;
110   template <class> friend class Future;
111   template <class, class>
112   friend class futures::detail::CoreCallbackState;
113
114   // Whether the Future has been retrieved (a one-time operation).
115   bool retrieved_;
116
117   // shared core state object
118   corePtr core_;
119
120   explicit Promise(futures::detail::EmptyConstruct) noexcept;
121
122   void throwIfFulfilled();
123   void throwIfRetrieved();
124   void detach();
125 };
126
127 }
128
129 #include <folly/futures/Future.h>
130 #include <folly/futures/Promise-inl.h>