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