7d8609bcc81deea2aeffd57323c171c3808daa08
[folly.git] / folly / wangle / futures / Promise.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <folly/wangle/futures/Try.h>
20 #include <functional>
21
22 namespace folly { namespace wangle {
23
24 // forward declaration
25 template <class T> class Future;
26
27 template <class T>
28 class Promise {
29 public:
30   Promise();
31   ~Promise();
32
33   // not copyable
34   Promise(Promise const&) = delete;
35   Promise& operator=(Promise const&) = delete;
36
37   // movable
38   Promise(Promise<T>&&);
39   Promise& operator=(Promise<T>&&);
40
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();
44
45   /** Fulfil the Promise with an exception_ptr, e.g.
46     try {
47       ...
48     } catch (...) {
49       p.setException(std::current_exception());
50     }
51     */
52   void setException(std::exception_ptr const&);
53
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.
57     */
58   template <class E> void setException(E const&);
59
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
64   /// handled.
65   void setInterruptHandler(std::function<void(std::exception_ptr const&)>);
66
67   /** Fulfil this Promise (only for Promise<void>) */
68   void setValue();
69
70   /** Set the value (use perfect forwarding for both move and copy) */
71   template <class M>
72   void setValue(M&& value);
73
74   void fulfilTry(Try<T>&& t);
75
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.
79
80     p.fulfil([] { do something that may throw; return a T; });
81   */
82   template <class F>
83   void fulfil(F&& func);
84
85 private:
86   typedef typename Future<T>::corePtr corePtr;
87
88   // Whether the Future has been retrieved (a one-time operation).
89   bool retrieved_;
90
91   // shared core state object
92   corePtr core_;
93
94   void throwIfFulfilled();
95   void throwIfRetrieved();
96   void detach();
97 };
98
99 }}
100
101 #include <folly/wangle/futures/Future.h>
102 #include <folly/wangle/futures/Promise-inl.h>