Add element construction/destruction hooks to IndexedMemPool
[folly.git] / folly / futures / Promise.h
1 /*
2  * Copyright 2017 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/Portability.h>
20 #include <folly/Try.h>
21 #include <functional>
22
23 namespace folly {
24
25 // forward declaration
26 template <class T> class Future;
27
28 namespace detail {
29 struct EmptyConstruct {};
30 template <typename T, typename F>
31 class CoreCallbackState;
32 }
33
34 template <class T>
35 class Promise {
36  public:
37   Promise();
38   ~Promise();
39
40   // not copyable
41   Promise(Promise const&) = delete;
42   Promise& operator=(Promise const&) = delete;
43
44   // movable
45   Promise(Promise<T>&&) noexcept;
46   Promise& operator=(Promise<T>&&) noexcept;
47
48   /** Return a Future tied to the shared core state. This can be called only
49     once, thereafter Future already retrieved exception will be raised. */
50   Future<T> getFuture();
51
52   /** Fulfill the Promise with an exception_wrapper */
53   void setException(exception_wrapper ew);
54
55   /** Fulfill the Promise with an exception_ptr, e.g.
56     try {
57       ...
58     } catch (...) {
59       p.setException(std::current_exception());
60     }
61     */
62   FOLLY_DEPRECATED("use setException(exception_wrapper)")
63   void setException(std::exception_ptr const&);
64
65   /** Fulfill the Promise with an exception type E, which can be passed to
66     std::make_exception_ptr(). Useful for originating exceptions. If you
67     caught an exception the exception_wrapper form is more appropriate.
68     */
69   template <class E>
70   typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
71   setException(E const&);
72
73   /// Set an interrupt handler to handle interrupts. See the documentation for
74   /// Future::raise(). Your handler can do whatever it wants, but if you
75   /// bother to set one then you probably will want to fulfill the promise with
76   /// an exception (or special value) indicating how the interrupt was
77   /// handled.
78   void setInterruptHandler(std::function<void(exception_wrapper const&)>);
79
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
83   setValue() {
84     setTry(Try<T>(T()));
85   }
86
87   /** Set the value (use perfect forwarding for both move and copy) */
88   template <class M>
89   void setValue(M&& value);
90
91   void setTry(Try<T>&& t);
92
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.
96
97     p.setWith([] { do something that may throw; return a T; });
98   */
99   template <class F>
100   void setWith(F&& func);
101
102   bool isFulfilled() const noexcept;
103
104  private:
105   typedef typename Future<T>::corePtr corePtr;
106   template <class> friend class Future;
107   template <class, class>
108   friend class detail::CoreCallbackState;
109
110   // Whether the Future has been retrieved (a one-time operation).
111   bool retrieved_;
112
113   // shared core state object
114   corePtr core_;
115
116   explicit Promise(detail::EmptyConstruct) noexcept;
117
118   void throwIfFulfilled();
119   void throwIfRetrieved();
120   void detach();
121 };
122
123 }
124
125 #include <folly/futures/Future.h>
126 #include <folly/futures/Promise-inl.h>