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>
21 namespace folly { namespace wangle {
23 // forward declaration
24 template <class T> class Future;
33 Promise(Promise const&) = delete;
34 Promise& operator=(Promise const&) = delete;
37 Promise(Promise<T>&&);
38 Promise& operator=(Promise<T>&&);
40 /** Return a Future tied to the shared core state. This can be called only
41 once, thereafter Future already retrieved exception will be raised. */
42 Future<T> getFuture();
44 /** Fulfil the Promise with an exception_ptr, e.g.
48 p.setException(std::current_exception());
51 void setException(std::exception_ptr const&);
53 /** Fulfil the Promise with an exception type E, which can be passed to
54 std::make_exception_ptr(). Useful for originating exceptions. If you
55 caught an exception the exception_ptr form is more appropriate.
57 template <class E> void setException(E const&);
59 /// Set an interrupt handler to handle interrupts. See the documentation for
60 /// Future::raise(). Your handler can do whatever it wants, but if you
61 /// bother to set one then you probably will want to fulfil the promise with
62 /// an exception (or special value) indicating how the interrupt was
64 void setInterruptHandler(std::function<void(std::exception_ptr const&)>);
66 /** Fulfil this Promise (only for Promise<void>) */
69 /** Set the value (use perfect forwarding for both move and copy) */
71 void setValue(M&& value);
73 void fulfilTry(Try<T>&& t);
75 /** Fulfil this Promise with the result of a function that takes no
76 arguments and returns something implicitly convertible to T.
77 Captures exceptions. e.g.
79 p.fulfil([] { do something that may throw; return a T; });
82 void fulfil(F&& func);
85 typedef typename Future<T>::corePtr corePtr;
87 // Whether the Future has been retrieved (a one-time operation).
90 // shared core state object
93 void throwIfFulfilled();
94 void throwIfRetrieved();
100 #include <folly/wangle/futures/Future.h>
101 #include <folly/wangle/futures/Promise-inl.h>