SharedPromise in OutputBufferingHandler
[folly.git] / folly / futures / SharedPromise.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/futures/Promise.h>
20
21 namespace folly {
22
23 /*
24  * SharedPromise provides the same interface as Promise, but you can extract
25  * multiple Futures from it, i.e. you can call getFuture() as many times as
26  * you'd like. When the SharedPromise is fulfilled, all of the Futures will be
27  * called back. Calls to getFuture() after the SharedPromise is fulfilled return
28  * a completed Future. If you find yourself constructing collections of Promises
29  * and fulfilling them simultaneously with the same value, consider this
30  * utility instead. Likewise, if you find yourself in need of setting multiple
31  * callbacks on the same Future (which is indefinitely unsupported), consider
32  * refactoring to use SharedPromise to "split" the Future.
33  */
34 template <class T>
35 class SharedPromise {
36 public:
37   SharedPromise() = default;
38   ~SharedPromise() = default;
39
40   // not copyable
41   SharedPromise(SharedPromise const&) = delete;
42   SharedPromise& operator=(SharedPromise const&) = delete;
43
44   // movable
45   SharedPromise(SharedPromise<T>&&) noexcept;
46   SharedPromise& operator=(SharedPromise<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   /** Return the number of Futures associated with this SharedPromise */
53   size_t size();
54
55   /** Fulfill the SharedPromise with an exception_wrapper */
56   void setException(exception_wrapper ew);
57
58   /** Fulfill the SharedPromise with an exception_ptr, e.g.
59     try {
60       ...
61     } catch (...) {
62       p.setException(std::current_exception());
63     }
64     */
65   void setException(std::exception_ptr const&) DEPRECATED;
66
67   /** Fulfill the SharedPromise with an exception type E, which can be passed to
68     std::make_exception_ptr(). Useful for originating exceptions. If you
69     caught an exception the exception_wrapper form is more appropriate.
70     */
71   template <class E>
72   typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
73   setException(E const&);
74
75   /// Set an interrupt handler to handle interrupts. See the documentation for
76   /// Future::raise(). Your handler can do whatever it wants, but if you
77   /// bother to set one then you probably will want to fulfill the SharedPromise with
78   /// an exception (or special value) indicating how the interrupt was
79   /// handled.
80   void setInterruptHandler(std::function<void(exception_wrapper const&)>);
81
82   /// Fulfill this SharedPromise<void>
83   template <class B = T>
84   typename std::enable_if<std::is_void<B>::value, void>::type
85   setValue() {
86     setTry(Try<T>());
87   }
88
89   /// Sugar to fulfill this SharedPromise<Unit>
90   template <class B = T>
91   typename std::enable_if<std::is_same<Unit, B>::value, void>::type
92   setValue() {
93     setTry(Try<T>(T()));
94   }
95
96   /** Set the value (use perfect forwarding for both move and copy) */
97   template <class M>
98   void setValue(M&& value);
99
100   void setTry(Try<T>&& t);
101
102   /** Fulfill this SharedPromise with the result of a function that takes no
103     arguments and returns something implicitly convertible to T.
104     Captures exceptions. e.g.
105
106     p.setWith([] { do something that may throw; return a T; });
107   */
108   template <class F>
109   void setWith(F&& func);
110
111 private:
112   std::mutex mutex_;
113   size_t size_{0};
114   bool hasValue_{false};
115   Try<T> try_;
116   std::vector<Promise<T>> promises_;
117 };
118
119 }
120
121 #include <folly/futures/Future.h>
122 #include <folly/futures/SharedPromise-inl.h>