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/Core.h>
25 namespace folly { namespace wangle {
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 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 core_->setResult(Try<T>(e));
92 void Promise<T>::setInterruptHandler(
93 std::function<void(std::exception_ptr const&)> fn) {
94 core_->setInterruptHandler(std::move(fn));
98 void Promise<T>::fulfilTry(Try<T>&& t) {
100 core_->setResult(std::move(t));
105 void Promise<T>::setValue(M&& v) {
106 static_assert(!std::is_same<T, void>::value,
107 "Use setValue() instead");
109 fulfilTry(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");
117 fulfilTry(Try<void>());
122 void Promise<T>::fulfil(F&& func) {
124 fulfilTry(makeTryFunction(std::forward<F>(func)));