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/futures/WangleException.h>
23 #include <folly/futures/detail/Core.h>
28 Promise<T>::Promise() : retrieved_(false), core_(new detail::Core<T>())
32 Promise<T>::Promise(Promise<T>&& other) : core_(nullptr) {
33 *this = std::move(other);
37 Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
38 std::swap(core_, other.core_);
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 core_->detachFuture();
67 core_->detachPromise();
73 Future<T> Promise<T>::getFuture() {
76 return Future<T>(core_);
81 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
82 Promise<T>::setException(E const& e) {
83 setException(make_exception_wrapper<E>(e));
87 void Promise<T>::setException(std::exception_ptr const& ep) {
89 std::rethrow_exception(ep);
90 } catch (const std::exception& e) {
91 setException(exception_wrapper(std::current_exception(), e));
93 setException(exception_wrapper(std::current_exception()));
98 void Promise<T>::setException(exception_wrapper ew) {
100 core_->setResult(Try<T>(std::move(ew)));
104 void Promise<T>::setInterruptHandler(
105 std::function<void(exception_wrapper const&)> fn) {
106 core_->setInterruptHandler(std::move(fn));
110 void Promise<T>::fulfilTry(Try<T>&& t) {
112 core_->setResult(std::move(t));
117 void Promise<T>::setValue(M&& v) {
118 static_assert(!std::is_same<T, void>::value,
119 "Use setValue() instead");
121 fulfilTry(Try<T>(std::forward<M>(v)));
125 void Promise<T>::setValue() {
126 static_assert(std::is_same<T, void>::value,
127 "Use setValue(value) instead");
129 fulfilTry(Try<void>());
134 void Promise<T>::fulfil(F&& func) {
136 fulfilTry(makeTryFunction(std::forward<F>(func)));