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