Consistently have the namespace closing comment
[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     }
82     core_->detachPromise();
83     core_ = nullptr;
84   }
85 }
86
87 template <class T>
88 Future<T> Promise<T>::getFuture() {
89   throwIfRetrieved();
90   retrieved_ = true;
91   return Future<T>(core_);
92 }
93
94 template <class T>
95 template <class E>
96 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
97 Promise<T>::setException(E const& e) {
98   setException(make_exception_wrapper<E>(e));
99 }
100
101 template <class T>
102 void Promise<T>::setException(std::exception_ptr const& ep) {
103   setException(exception_wrapper::from_exception_ptr(ep));
104 }
105
106 template <class T>
107 void Promise<T>::setException(exception_wrapper ew) {
108   throwIfFulfilled();
109   core_->setResult(Try<T>(std::move(ew)));
110 }
111
112 template <class T>
113 void Promise<T>::setInterruptHandler(
114   std::function<void(exception_wrapper const&)> fn) {
115   core_->setInterruptHandler(std::move(fn));
116 }
117
118 template <class T>
119 void Promise<T>::setTry(Try<T>&& t) {
120   throwIfFulfilled();
121   core_->setResult(std::move(t));
122 }
123
124 template <class T>
125 template <class M>
126 void Promise<T>::setValue(M&& v) {
127   static_assert(!std::is_same<T, void>::value,
128                 "Use setValue() instead");
129
130   setTry(Try<T>(std::forward<M>(v)));
131 }
132
133 template <class T>
134 template <class F>
135 void Promise<T>::setWith(F&& func) {
136   throwIfFulfilled();
137   setTry(makeTryWith(std::forward<F>(func)));
138 }
139
140 template <class T>
141 bool Promise<T>::isFulfilled() const noexcept {
142   if (core_) {
143     return core_->hasResult();
144   }
145   return true;
146 }
147
148 } // namespace folly