2 * Copyright 2017 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 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();
60 /** Fulfill the Promise with an exception_wrapper */
61 void setException(exception_wrapper ew);
63 /** Fulfill the Promise with an exception_ptr, e.g.
67 p.setException(std::current_exception());
70 FOLLY_DEPRECATED("use setException(exception_wrapper)")
71 void setException(std::exception_ptr const&);
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.
78 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
79 setException(E const&);
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
86 void setInterruptHandler(std::function<void(exception_wrapper const&)>);
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
95 /** Set the value (use perfect forwarding for both move and copy) */
97 void setValue(M&& value);
99 void setTry(Try<T>&& t);
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.
105 p.setWith([] { do something that may throw; return a T; });
108 void setWith(F&& func);
110 bool isFulfilled() const noexcept;
113 typedef typename Future<T>::corePtr corePtr;
115 friend class futures::detail::FutureBase;
117 friend class SemiFuture;
120 template <class, class>
121 friend class futures::detail::CoreCallbackState;
123 // Whether the Future has been retrieved (a one-time operation).
126 // shared core state object
129 explicit Promise(futures::detail::EmptyConstruct) noexcept;
131 void throwIfFulfilled();
132 void throwIfRetrieved();
138 #include <folly/futures/Future.h>
139 #include <folly/futures/Promise-inl.h>