2 * Copyright 2014-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/Portability.h>
20 #include <folly/Try.h>
25 // forward declaration
28 template <class T> class Future;
34 struct EmptyConstruct {};
35 template <typename T, typename F>
36 class CoreCallbackState;
38 } // namespace futures
43 static Promise<T> makeEmpty() noexcept; // equivalent to moved-from
49 Promise(Promise const&) = delete;
50 Promise& operator=(Promise const&) = delete;
53 Promise(Promise<T>&&) noexcept;
54 Promise& operator=(Promise<T>&&) noexcept;
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();
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();
67 /** Fulfill the Promise with an exception_wrapper */
68 void setException(exception_wrapper ew);
70 /** Fulfill the Promise with an exception_ptr, e.g.
74 p.setException(std::current_exception());
77 FOLLY_DEPRECATED("use setException(exception_wrapper)")
78 void setException(std::exception_ptr const&);
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.
85 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
86 setException(E const&);
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
93 void setInterruptHandler(std::function<void(exception_wrapper const&)>);
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
102 /** Set the value (use perfect forwarding for both move and copy) */
104 void setValue(M&& value);
106 void setTry(Try<T>&& t);
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.
112 p.setWith([] { do something that may throw; return a T; });
115 void setWith(F&& func);
117 bool isFulfilled() const noexcept;
120 typedef typename Future<T>::corePtr corePtr;
122 friend class futures::detail::FutureBase;
124 friend class SemiFuture;
127 template <class, class>
128 friend class futures::detail::CoreCallbackState;
130 // Whether the Future has been retrieved (a one-time operation).
133 // shared core state object
136 explicit Promise(futures::detail::EmptyConstruct) noexcept;
138 void throwIfFulfilled();
139 void throwIfRetrieved();
145 #include <folly/futures/Future.h>
146 #include <folly/futures/Promise-inl.h>