2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <folly/wangle/WangleException.h>
23 #include <folly/wangle/detail/State.h>
25 namespace folly { namespace wangle {
28 Promise<T>::Promise() : retrieved_(false), state_(new detail::State<T>())
32 Promise<T>::Promise(Promise<T>&& other) : state_(nullptr) {
33 *this = std::move(other);
37 Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
38 std::swap(state_, other.state_);
39 std::swap(retrieved_, other.retrieved_);
44 void Promise<T>::throwIfFulfilled() {
48 throw PromiseAlreadySatisfied();
52 void Promise<T>::throwIfRetrieved() {
54 throw FutureAlreadyRetrieved();
58 Promise<T>::~Promise() {
63 void Promise<T>::detach() {
66 state_->detachFuture();
67 state_->detachPromise();
73 Future<T> Promise<T>::getFuture() {
76 return Future<T>(state_);
81 void Promise<T>::setException(E const& e) {
82 setException(std::make_exception_ptr<E>(e));
86 void Promise<T>::setException(std::exception_ptr const& e) {
88 state_->setException(e);
92 void Promise<T>::fulfilTry(Try<T>&& t) {
94 state_->fulfil(std::move(t));
99 void Promise<T>::setValue(M&& v) {
100 static_assert(!std::is_same<T, void>::value,
101 "Use setValue() instead");
103 fulfilTry(Try<T>(std::forward<M>(v)));
107 void Promise<T>::setValue() {
108 static_assert(std::is_same<T, void>::value,
109 "Use setValue(value) instead");
111 fulfilTry(Try<void>());
116 void Promise<T>::fulfil(F&& func) {
118 fulfilTry(makeTryFunction(std::forward<F>(func)));