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