fix compiler warnings from gcc-4.9 + -Wunused-variable
[folly.git] / folly / experimental / fibers / 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 #pragma once
17
18 #include <folly/experimental/fibers/traits.h>
19 #include <folly/futures/Try.h>
20
21 namespace folly { namespace fibers {
22
23 class Baton;
24
25 template <typename F>
26 typename FirstArgOf<F>::type::value_type
27 inline await(F&& func);
28
29 template <typename T>
30 class Promise {
31  public:
32   typedef T value_type;
33
34   ~Promise();
35
36   // not copyable
37   Promise(const Promise&) = delete;
38   Promise& operator=(const Promise&) = delete;
39
40   // movable
41   Promise(Promise&&) noexcept;
42   Promise& operator=(Promise&&);
43
44   /** Fulfill this promise (only for Promise<void>) */
45   void setValue();
46
47   /** Set the value (use perfect forwarding for both move and copy) */
48   template <class M>
49   void setValue(M&& value);
50
51   /**
52    * Fulfill the promise with a given try
53    *
54    * @param t
55    */
56   void setTry(folly::Try<T>&& t);
57
58   /** Fulfill this promise with the result of a function that takes no
59     arguments and returns something implicitly convertible to T.
60     Captures exceptions. e.g.
61
62     p.setWith([] { do something that may throw; return a T; });
63   */
64   template <class F>
65   void setWith(F&& func);
66
67   /** Fulfill the Promise with an exception_wrapper, e.g.
68     auto ew = folly::try_and_catch<std::exception>([]{ ... });
69     if (ew) {
70       p.setException(std::move(ew));
71     }
72     */
73   void setException(folly::exception_wrapper);
74
75  private:
76   template <typename F>
77   friend typename FirstArgOf<F>::type::value_type await(F&&);
78
79   Promise(folly::Try<T>& value, Baton& baton);
80   folly::Try<T>* value_;
81   Baton* baton_;
82
83   void throwIfFulfilled() const;
84
85   template <class F>
86   typename std::enable_if<
87     std::is_convertible<typename std::result_of<F()>::type, T>::value &&
88     !std::is_same<T, void>::value>::type
89   fulfilHelper(F&& func);
90
91   template <class F>
92   typename std::enable_if<
93     std::is_same<typename std::result_of<F()>::type, void>::value &&
94     std::is_same<T, void>::value>::type
95   fulfilHelper(F&& func);
96 };
97
98 }}
99
100 #include <folly/experimental/fibers/Promise-inl.h>