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