2 * Copyright 2014 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/wangle/futures/Try.h>
22 namespace folly { namespace wangle {
24 // forward declaration
25 template <class T> class Future;
34 Promise(Promise const&) = delete;
35 Promise& operator=(Promise const&) = delete;
38 Promise(Promise<T>&&);
39 Promise& operator=(Promise<T>&&);
41 /** Return a Future tied to the shared core state. This can be called only
42 once, thereafter Future already retrieved exception will be raised. */
43 Future<T> getFuture();
45 /** Fulfil the Promise with an exception_ptr, e.g.
49 p.setException(std::current_exception());
52 void setException(std::exception_ptr const&);
54 /** Fulfil the Promise with an exception type E, which can be passed to
55 std::make_exception_ptr(). Useful for originating exceptions. If you
56 caught an exception the exception_ptr form is more appropriate.
58 template <class E> void setException(E const&);
60 /// Set an interrupt handler to handle interrupts. See the documentation for
61 /// Future::raise(). Your handler can do whatever it wants, but if you
62 /// bother to set one then you probably will want to fulfil the promise with
63 /// an exception (or special value) indicating how the interrupt was
65 void setInterruptHandler(std::function<void(std::exception_ptr const&)>);
67 /** Fulfil this Promise (only for Promise<void>) */
70 /** Set the value (use perfect forwarding for both move and copy) */
72 void setValue(M&& value);
74 void fulfilTry(Try<T>&& t);
76 /** Fulfil this Promise with the result of a function that takes no
77 arguments and returns something implicitly convertible to T.
78 Captures exceptions. e.g.
80 p.fulfil([] { do something that may throw; return a T; });
83 void fulfil(F&& func);
86 typedef typename Future<T>::corePtr corePtr;
88 // Whether the Future has been retrieved (a one-time operation).
91 // shared core state object
94 void throwIfFulfilled();
95 void throwIfRetrieved();
101 #include <folly/wangle/futures/Future.h>
102 #include <folly/wangle/futures/Promise-inl.h>