Fix copyright lines
[folly.git] / folly / futures / Promise.h
1 /*
2  * Copyright 2014-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
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 SemiFuture tied to the shared core state. This can be called only
57     once, thereafter FutureAlreadyRetrieved exception will be raised. */
58   SemiFuture<T> getSemiFuture();
59
60   /** Return a Future tied to the shared core state. This can be called only
61     once, thereafter FutureAlreadyRetrieved exception will be raised.
62     NOTE: This function is deprecated. Please use getSemiFuture and pass the
63           appropriate executor to .via on the returned SemiFuture to get a
64           valid Future where necessary. */
65   Future<T> getFuture();
66
67   /** Fulfill the Promise with an exception_wrapper */
68   void setException(exception_wrapper ew);
69
70   /** Fulfill the Promise with an exception_ptr, e.g.
71     try {
72       ...
73     } catch (...) {
74       p.setException(std::current_exception());
75     }
76     */
77   FOLLY_DEPRECATED("use setException(exception_wrapper)")
78   void setException(std::exception_ptr const&);
79
80   /** Fulfill the Promise with an exception type E, which can be passed to
81     std::make_exception_ptr(). Useful for originating exceptions. If you
82     caught an exception the exception_wrapper form is more appropriate.
83     */
84   template <class E>
85   typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
86   setException(E const&);
87
88   /// Set an interrupt handler to handle interrupts. See the documentation for
89   /// Future::raise(). Your handler can do whatever it wants, but if you
90   /// bother to set one then you probably will want to fulfill the promise with
91   /// an exception (or special value) indicating how the interrupt was
92   /// handled.
93   void setInterruptHandler(std::function<void(exception_wrapper const&)>);
94
95   /// Sugar to fulfill this Promise<Unit>
96   template <class B = T>
97   typename std::enable_if<std::is_same<Unit, B>::value, void>::type
98   setValue() {
99     setTry(Try<T>(T()));
100   }
101
102   /** Set the value (use perfect forwarding for both move and copy) */
103   template <class M>
104   void setValue(M&& value);
105
106   void setTry(Try<T>&& t);
107
108   /** Fulfill this Promise with the result of a function that takes no
109     arguments and returns something implicitly convertible to T.
110     Captures exceptions. e.g.
111
112     p.setWith([] { do something that may throw; return a T; });
113   */
114   template <class F>
115   void setWith(F&& func);
116
117   bool isFulfilled() const noexcept;
118
119  private:
120   typedef typename Future<T>::corePtr corePtr;
121   template <class>
122   friend class futures::detail::FutureBase;
123   template <class>
124   friend class SemiFuture;
125   template <class>
126   friend class Future;
127   template <class, class>
128   friend class futures::detail::CoreCallbackState;
129
130   // Whether the Future has been retrieved (a one-time operation).
131   bool retrieved_;
132
133   // shared core state object
134   corePtr core_;
135
136   explicit Promise(futures::detail::EmptyConstruct) noexcept;
137
138   void throwIfFulfilled();
139   void throwIfRetrieved();
140   void detach();
141 };
142
143 } // namespace folly
144
145 #include <folly/futures/Future.h>
146 #include <folly/futures/Promise-inl.h>