2 * Copyright 2016 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.
16 #include <folly/experimental/fibers/Baton.h>
18 namespace folly { namespace fibers {
21 Promise<T>::Promise(folly::Try<T>& value, Baton& baton) :
22 value_(&value), baton_(&baton)
26 Promise<T>::Promise(Promise&& other) noexcept :
27 value_(other.value_), baton_(other.baton_) {
28 other.value_ = nullptr;
29 other.baton_ = nullptr;
33 Promise<T>& Promise<T>::operator=(Promise&& other) {
34 std::swap(value_, other.value_);
35 std::swap(baton_, other.baton_);
40 void Promise<T>::throwIfFulfilled() const {
42 throw std::logic_error("promise already fulfilled");
47 Promise<T>::~Promise() {
49 setException(folly::make_exception_wrapper<std::logic_error>(
50 "promise not fulfilled"));
55 void Promise<T>::setException(folly::exception_wrapper e) {
56 setTry(folly::Try<T>(e));
60 void Promise<T>::setTry(folly::Try<T>&& t) {
63 *value_ = std::move(t);
72 void Promise<T>::setValue(M&& v) {
73 static_assert(!std::is_same<T, void>::value,
74 "Use setValue() instead");
76 setTry(folly::Try<T>(std::forward<M>(v)));
80 void Promise<T>::setValue() {
81 static_assert(std::is_same<T, void>::value,
82 "Use setValue(value) instead");
84 setTry(folly::Try<void>());
89 void Promise<T>::setWith(F&& func) {
90 setTry(makeTryWith(std::forward<F>(func)));