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 "WangleException.h"
25 namespace folly { namespace wangle {
28 Promise<T>::Promise() : retrieved_(false), obj_(new detail::FutureObject<T>())
32 Promise<T>::Promise(Promise<T>&& other) :
33 retrieved_(other.retrieved_), obj_(other.obj_) {
38 Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
39 std::swap(obj_, other.obj_);
40 std::swap(retrieved_, other.retrieved_);
45 void Promise<T>::throwIfFulfilled() {
47 throw PromiseAlreadySatisfied();
51 void Promise<T>::throwIfRetrieved() {
53 throw FutureAlreadyRetrieved();
57 Promise<T>::~Promise() {
59 setException(BrokenPromise());
64 Future<T> Promise<T>::getFuture() {
68 return Future<T>(obj_);
73 void Promise<T>::setException(E const& e) {
75 setException(std::make_exception_ptr<E>(e));
79 void Promise<T>::setException(std::exception_ptr const& e) {
81 obj_->setException(e);
89 void Promise<T>::fulfilTry(Try<T>&& t) {
91 obj_->fulfil(std::move(t));
100 void Promise<T>::setValue(M&& v) {
101 static_assert(!std::is_same<T, void>::value,
102 "Use setValue() instead");
105 obj_->fulfil(Try<T>(std::forward<M>(v)));
113 void Promise<T>::setValue() {
114 static_assert(std::is_same<T, void>::value,
115 "Use setValue(value) instead");
118 obj_->fulfil(Try<void>());
127 void Promise<T>::fulfil(const F& func) {
133 typename std::enable_if<
134 std::is_convertible<typename std::result_of<F()>::type, T>::value &&
135 !std::is_same<T, void>::value>::type
136 inline Promise<T>::fulfilHelper(const F& func) {
141 setException(std::current_exception());
147 typename std::enable_if<
148 std::is_same<typename std::result_of<F()>::type, void>::value &&
149 std::is_same<T, void>::value>::type
150 inline Promise<T>::fulfilHelper(const F& func) {
156 setException(std::current_exception());