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