exception_ptr -> exception_wrapper migration
[folly.git] / folly / wangle / futures / Promise-inl.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 <atomic>
20 #include <thread>
21
22 #include <folly/wangle/futures/WangleException.h>
23 #include <folly/wangle/futures/detail/Core.h>
24
25 namespace folly { namespace wangle {
26
27 template <class T>
28 Promise<T>::Promise() : retrieved_(false), core_(new detail::Core<T>())
29 {}
30
31 template <class T>
32 Promise<T>::Promise(Promise<T>&& other) : core_(nullptr) {
33   *this = std::move(other);
34 }
35
36 template <class T>
37 Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
38   std::swap(core_, other.core_);
39   std::swap(retrieved_, other.retrieved_);
40   return *this;
41 }
42
43 template <class T>
44 void Promise<T>::throwIfFulfilled() {
45   if (!core_)
46     throw NoState();
47   if (core_->ready())
48     throw PromiseAlreadySatisfied();
49 }
50
51 template <class T>
52 void Promise<T>::throwIfRetrieved() {
53   if (retrieved_)
54     throw FutureAlreadyRetrieved();
55 }
56
57 template <class T>
58 Promise<T>::~Promise() {
59   detach();
60 }
61
62 template <class T>
63 void Promise<T>::detach() {
64   if (core_) {
65     if (!retrieved_)
66       core_->detachFuture();
67     core_->detachPromise();
68     core_ = nullptr;
69   }
70 }
71
72 template <class T>
73 Future<T> Promise<T>::getFuture() {
74   throwIfRetrieved();
75   retrieved_ = true;
76   return Future<T>(core_);
77 }
78
79 template <class T>
80 template <class E>
81 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
82 Promise<T>::setException(E const& e) {
83   setException(make_exception_wrapper<E>(e));
84 }
85
86 template <class T>
87 void Promise<T>::setException(std::exception_ptr const& e) {
88   try {
89     std::rethrow_exception(e);
90   } catch (const std::exception& e) {
91     setException(exception_wrapper(std::current_exception(), e));
92   } catch (...) {
93     setException(exception_wrapper(std::current_exception()));
94   }
95 }
96
97 template <class T>
98 void Promise<T>::setException(exception_wrapper ew) {
99   throwIfFulfilled();
100   core_->setResult(Try<T>(std::move(ew)));
101 }
102
103 template <class T>
104 void Promise<T>::setInterruptHandler(
105   std::function<void(exception_wrapper const&)> fn) {
106   core_->setInterruptHandler(std::move(fn));
107 }
108
109 template <class T>
110 void Promise<T>::fulfilTry(Try<T>&& t) {
111   throwIfFulfilled();
112   core_->setResult(std::move(t));
113 }
114
115 template <class T>
116 template <class M>
117 void Promise<T>::setValue(M&& v) {
118   static_assert(!std::is_same<T, void>::value,
119                 "Use setValue() instead");
120
121   fulfilTry(Try<T>(std::forward<M>(v)));
122 }
123
124 template <class T>
125 void Promise<T>::setValue() {
126   static_assert(std::is_same<T, void>::value,
127                 "Use setValue(value) instead");
128
129   fulfilTry(Try<void>());
130 }
131
132 template <class T>
133 template <class F>
134 void Promise<T>::fulfil(F&& func) {
135   throwIfFulfilled();
136   fulfilTry(makeTryFunction(std::forward<F>(func)));
137 }
138
139 }}