X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Ffutures%2FPromise-inl.h;h=7471228c4e2719db43c6af234a40b6994a9db3f4;hp=b347486a4faa0d10999e3d852462bb98fbb68985;hb=7ebe7c2a249de44209e8bcc453e1a70bafd4ed19;hpb=7b5d03d60aaeeb217fb33d3d3f9be24cc3d0b6b9 diff --git a/folly/futures/Promise-inl.h b/folly/futures/Promise-inl.h index b347486a..7471228c 100644 --- a/folly/futures/Promise-inl.h +++ b/folly/futures/Promise-inl.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,16 +25,23 @@ namespace folly { template -Promise::Promise() : retrieved_(false), core_(new detail::Core()) -{} +Promise Promise::makeEmpty() noexcept { + return Promise(futures::detail::EmptyConstruct{}); +} + +template +Promise::Promise() + : retrieved_(false), core_(new futures::detail::Core()) {} template -Promise::Promise(Promise&& other) : core_(nullptr) { - *this = std::move(other); +Promise::Promise(Promise&& other) noexcept + : retrieved_(other.retrieved_), core_(other.core_) { + other.core_ = nullptr; + other.retrieved_ = false; } template -Promise& Promise::operator=(Promise&& other) { +Promise& Promise::operator=(Promise&& other) noexcept { std::swap(core_, other.core_); std::swap(retrieved_, other.retrieved_); return *this; @@ -42,18 +49,25 @@ Promise& Promise::operator=(Promise&& other) { template void Promise::throwIfFulfilled() { - if (!core_) - throw NoState(); - if (core_->ready()) - throw PromiseAlreadySatisfied(); + if (!core_) { + throwNoState(); + } + if (core_->ready()) { + throwPromiseAlreadySatisfied(); + } } template void Promise::throwIfRetrieved() { - if (retrieved_) - throw FutureAlreadyRetrieved(); + if (retrieved_) { + throwFutureAlreadyRetrieved(); + } } +template +Promise::Promise(futures::detail::EmptyConstruct) noexcept + : retrieved_(false), core_(nullptr) {} + template Promise::~Promise() { detach(); @@ -62,8 +76,9 @@ Promise::~Promise() { template void Promise::detach() { if (core_) { - if (!retrieved_) + if (!retrieved_) { core_->detachFuture(); + } core_->detachPromise(); core_ = nullptr; } @@ -85,13 +100,7 @@ Promise::setException(E const& e) { template void Promise::setException(std::exception_ptr const& ep) { - try { - std::rethrow_exception(ep); - } catch (const std::exception& e) { - setException(exception_wrapper(std::current_exception(), e)); - } catch (...) { - setException(exception_wrapper(std::current_exception())); - } + setException(exception_wrapper::from_exception_ptr(ep)); } template @@ -107,7 +116,7 @@ void Promise::setInterruptHandler( } template -void Promise::fulfilTry(Try t) { +void Promise::setTry(Try&& t) { throwIfFulfilled(); core_->setResult(std::move(t)); } @@ -118,22 +127,22 @@ void Promise::setValue(M&& v) { static_assert(!std::is_same::value, "Use setValue() instead"); - fulfilTry(Try(std::forward(v))); + setTry(Try(std::forward(v))); } template -void Promise::setValue() { - static_assert(std::is_same::value, - "Use setValue(value) instead"); - - fulfilTry(Try()); +template +void Promise::setWith(F&& func) { + throwIfFulfilled(); + setTry(makeTryWith(std::forward(func))); } template -template -void Promise::fulfil(F&& func) { - throwIfFulfilled(); - fulfilTry(makeTryFunction(std::forward(func))); +bool Promise::isFulfilled() const noexcept { + if (core_) { + return core_->hasResult(); + } + return true; } }