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