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;
32 struct EmptyConstruct {};
33 template <typename T, typename F>
34 class CoreCallbackState;
36 } // namespace futures
41 static Promise<T> makeEmpty() noexcept; // equivalent to moved-from
47 Promise(Promise const&) = delete;
48 Promise& operator=(Promise const&) = delete;
51 Promise(Promise<T>&&) noexcept;
52 Promise& operator=(Promise<T>&&) noexcept;
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();
58 /** Fulfill the Promise with an exception_wrapper */
59 void setException(exception_wrapper ew);
61 /** Fulfill the Promise with an exception_ptr, e.g.
65 p.setException(std::current_exception());
68 FOLLY_DEPRECATED("use setException(exception_wrapper)")
69 void setException(std::exception_ptr const&);
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.
76 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
77 setException(E const&);
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
84 void setInterruptHandler(std::function<void(exception_wrapper const&)>);
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
93 /** Set the value (use perfect forwarding for both move and copy) */
95 void setValue(M&& value);
97 void setTry(Try<T>&& t);
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.
103 p.setWith([] { do something that may throw; return a T; });
106 void setWith(F&& func);
108 bool isFulfilled() const noexcept;
111 typedef typename Future<T>::corePtr corePtr;
113 friend class SemiFuture;
114 template <class> friend class Future;
115 template <class, class>
116 friend class futures::detail::CoreCallbackState;
118 // Whether the Future has been retrieved (a one-time operation).
121 // shared core state object
124 explicit Promise(futures::detail::EmptyConstruct) noexcept;
126 void throwIfFulfilled();
127 void throwIfRetrieved();
133 #include <folly/futures/Future.h>
134 #include <folly/futures/Promise-inl.h>