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