2 * Copyright 2015 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/futures/Deprecated.h>
20 #include <folly/futures/Try.h>
25 // forward declaration
26 template <class T> class Future;
35 Promise(Promise const&) = delete;
36 Promise& operator=(Promise const&) = delete;
39 Promise(Promise<T>&&) noexcept;
40 Promise& operator=(Promise<T>&&) noexcept;
42 /** Return a Future tied to the shared core state. This can be called only
43 once, thereafter Future already retrieved exception will be raised. */
44 Future<T> getFuture();
46 /** Fulfill the Promise with an exception_wrapper */
47 void setException(exception_wrapper ew);
49 /** Fulfill the Promise with an exception_ptr, e.g.
53 p.setException(std::current_exception());
56 void setException(std::exception_ptr const&) DEPRECATED;
58 /** Fulfill the Promise with an exception type E, which can be passed to
59 std::make_exception_ptr(). Useful for originating exceptions. If you
60 caught an exception the exception_wrapper form is more appropriate.
63 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
64 setException(E const&);
66 /// Set an interrupt handler to handle interrupts. See the documentation for
67 /// Future::raise(). Your handler can do whatever it wants, but if you
68 /// bother to set one then you probably will want to fulfill the promise with
69 /// an exception (or special value) indicating how the interrupt was
71 void setInterruptHandler(std::function<void(exception_wrapper const&)>);
73 /// Fulfill this Promise<void>
74 template <class B = T>
75 typename std::enable_if<std::is_void<B>::value, void>::type
80 /// Sugar to fulfill this Promise<Unit>
81 template <class B = T>
82 typename std::enable_if<std::is_same<Unit, B>::value, void>::type
87 /** Set the value (use perfect forwarding for both move and copy) */
89 void setValue(M&& value);
91 void setTry(Try<T>&& t);
93 /** Fulfill this Promise with the result of a function that takes no
94 arguments and returns something implicitly convertible to T.
95 Captures exceptions. e.g.
97 p.setWith([] { do something that may throw; return a T; });
100 void setWith(F&& func);
103 typedef typename Future<T>::corePtr corePtr;
105 // Whether the Future has been retrieved (a one-time operation).
108 // shared core state object
111 void throwIfFulfilled();
112 void throwIfRetrieved();
118 #include <folly/futures/Future.h>
119 #include <folly/futures/Promise-inl.h>