2 * Copyright 2017 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/FutureException.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) noexcept
33 : retrieved_(other.retrieved_), core_(other.core_) {
34 other.core_ = nullptr;
35 other.retrieved_ = false;
39 Promise<T>& Promise<T>::operator=(Promise<T>&& other) noexcept {
40 std::swap(core_, other.core_);
41 std::swap(retrieved_, other.retrieved_);
46 void Promise<T>::throwIfFulfilled() {
47 if (UNLIKELY(!core_)) {
50 if (UNLIKELY(core_->ready())) {
51 throw PromiseAlreadySatisfied();
56 void Promise<T>::throwIfRetrieved() {
57 if (UNLIKELY(retrieved_)) {
58 throw FutureAlreadyRetrieved();
63 Promise<T>::Promise(detail::EmptyConstruct) noexcept
64 : retrieved_(false), core_(nullptr) {}
67 Promise<T>::~Promise() {
72 void Promise<T>::detach() {
75 core_->detachFuture();
76 core_->detachPromise();
82 Future<T> Promise<T>::getFuture() {
85 return Future<T>(core_);
90 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
91 Promise<T>::setException(E const& e) {
92 setException(make_exception_wrapper<E>(e));
96 void Promise<T>::setException(std::exception_ptr const& ep) {
98 std::rethrow_exception(ep);
99 } catch (const std::exception& e) {
100 setException(exception_wrapper(std::current_exception(), e));
102 setException(exception_wrapper(std::current_exception()));
107 void Promise<T>::setException(exception_wrapper ew) {
109 core_->setResult(Try<T>(std::move(ew)));
113 void Promise<T>::setInterruptHandler(
114 std::function<void(exception_wrapper const&)> fn) {
115 core_->setInterruptHandler(std::move(fn));
119 void Promise<T>::setTry(Try<T>&& t) {
121 core_->setResult(std::move(t));
126 void Promise<T>::setValue(M&& v) {
127 static_assert(!std::is_same<T, void>::value,
128 "Use setValue() instead");
130 setTry(Try<T>(std::forward<M>(v)));
135 void Promise<T>::setWith(F&& func) {
137 setTry(makeTryWith(std::forward<F>(func)));
141 bool Promise<T>::isFulfilled() const noexcept {
143 return core_->hasResult();