use folly::Baton in waitWithSemaphore
[folly.git] / folly / wangle / 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/WangleException.h>
23 #include <folly/wangle/detail/State.h>
24
25 namespace folly { namespace wangle {
26
27 template <class T>
28 Promise<T>::Promise() : retrieved_(false), state_(new detail::State<T>())
29 {}
30
31 template <class T>
32 Promise<T>::Promise(Promise<T>&& other) : state_(nullptr) {
33   *this = std::move(other);
34 }
35
36 template <class T>
37 Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
38   std::swap(state_, other.state_);
39   std::swap(retrieved_, other.retrieved_);
40   return *this;
41 }
42
43 template <class T>
44 void Promise<T>::throwIfFulfilled() {
45   if (!state_)
46     throw NoState();
47   if (state_->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 (state_) {
65     if (!retrieved_)
66       state_->detachFuture();
67     state_->detachPromise();
68     state_ = nullptr;
69   }
70 }
71
72 template <class T>
73 Future<T> Promise<T>::getFuture() {
74   throwIfRetrieved();
75   retrieved_ = true;
76   return Future<T>(state_);
77 }
78
79 template <class T>
80 template <class E>
81 void Promise<T>::setException(E const& e) {
82   setException(std::make_exception_ptr<E>(e));
83 }
84
85 template <class T>
86 void Promise<T>::setException(std::exception_ptr const& e) {
87   throwIfFulfilled();
88   state_->setException(e);
89 }
90
91 template <class T>
92 void Promise<T>::fulfilTry(Try<T>&& t) {
93   throwIfFulfilled();
94   state_->fulfil(std::move(t));
95 }
96
97 template <class T>
98 template <class M>
99 void Promise<T>::setValue(M&& v) {
100   static_assert(!std::is_same<T, void>::value,
101                 "Use setValue() instead");
102
103   fulfilTry(Try<T>(std::forward<M>(v)));
104 }
105
106 template <class T>
107 void Promise<T>::setValue() {
108   static_assert(std::is_same<T, void>::value,
109                 "Use setValue(value) instead");
110
111   fulfilTry(Try<void>());
112 }
113
114 template <class T>
115 template <class F>
116 void Promise<T>::fulfil(F&& func) {
117   throwIfFulfilled();
118   fulfilTry(makeTryFunction(std::forward<F>(func)));
119 }
120
121 }}