X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Ffutures%2FFuture-inl.h;h=56b98410abcfe59d24cfcdbe89caecf08a0d8ba2;hp=70b0f892de00320d9798cd1ff017ee938fc85c6d;hb=ff18deaf720fbe59551a7ff275b09003a61c4351;hpb=a674aa6cee470969ddecd340154002cf1d3efc1c diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 70b0f892..56b98410 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2017-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #pragma once #include @@ -174,30 +173,50 @@ FutureBase::~FutureBase() { template T& FutureBase::value() & { + return result().value(); +} + +template +T const& FutureBase::value() const& { + return result().value(); +} + +template +T&& FutureBase::value() && { + return std::move(result().value()); +} + +template +T const&& FutureBase::value() const&& { + return std::move(result().value()); +} + +template +Try& FutureBase::result() & { throwIfInvalid(); - return core_->getTry().value(); + return core_->getTry(); } template -T const& FutureBase::value() const& { +Try const& FutureBase::result() const& { throwIfInvalid(); - return core_->getTry().value(); + return core_->getTry(); } template -T&& FutureBase::value() && { +Try&& FutureBase::result() && { throwIfInvalid(); - return std::move(core_->getTry().value()); + return std::move(core_->getTry()); } template -T const&& FutureBase::value() const&& { +Try const&& FutureBase::result() const&& { throwIfInvalid(); - return std::move(core_->getTry().value()); + return std::move(core_->getTry()); } template @@ -208,12 +227,12 @@ bool FutureBase::isReady() const { template bool FutureBase::hasValue() { - return getTry().hasValue(); + return core_->getTry().hasValue(); } template bool FutureBase::hasException() { - return getTry().hasException(); + return core_->getTry().hasException(); } template @@ -224,13 +243,6 @@ void FutureBase::detach() { } } -template -Try& FutureBase::getTry() { - throwIfInvalid(); - - return core_->getTry(); -} - template void FutureBase::throwIfInvalid() const { if (!core_) { @@ -795,11 +807,6 @@ Future::onError(F&& func) { return f; } -template -Try& Future::getTryVia(DrivableExecutor* e) { - return waitVia(e).getTry(); -} - template auto via(Executor* x, Func&& func) -> Future()())>::Inner> { @@ -1399,6 +1406,21 @@ void waitViaImpl(Future& f, DrivableExecutor* e) { assert(f.isReady()); } +template +void waitViaImpl(SemiFuture& f, DrivableExecutor* e) { + // Set callback so to ensure that the via executor has something on it + // so that once the preceding future triggers this callback, drive will + // always have a callback to satisfy it + if (f.isReady()) { + return; + } + f = std::move(f).via(e).then([](T&& t) { return std::move(t); }); + while (!f.isReady()) { + e->drive(); + } + assert(f.isReady()); +} + } // namespace detail } // namespace futures @@ -1426,9 +1448,21 @@ SemiFuture&& SemiFuture::wait(Duration dur) && { return std::move(*this); } +template +SemiFuture& SemiFuture::waitVia(DrivableExecutor* e) & { + futures::detail::waitViaImpl(*this, e); + return *this; +} + +template +SemiFuture&& SemiFuture::waitVia(DrivableExecutor* e) && { + futures::detail::waitViaImpl(*this, e); + return std::move(*this); +} + template T SemiFuture::get() && { - return std::move(wait().value()); + return std::move(wait()).value(); } template @@ -1441,6 +1475,31 @@ T SemiFuture::get(Duration dur) && { } } +template +Try SemiFuture::getTry() && { + return std::move(wait()).result(); +} + +template +Try SemiFuture::getTry(Duration dur) && { + wait(dur); + if (this->isReady()) { + return std::move(this->result()); + } else { + throwTimedOut(); + } +} + +template +T SemiFuture::getVia(DrivableExecutor* e) && { + return std::move(waitVia(e)).value(); +} + +template +Try SemiFuture::getTryVia(DrivableExecutor* e) && { + return std::move(waitVia(e)).result(); +} + template Future& Future::wait() & { futures::detail::waitImpl(*this); @@ -1492,11 +1551,21 @@ T Future::get(Duration dur) { } } +template +Try& Future::getTry() { + return result(); +} + template T Future::getVia(DrivableExecutor* e) { return std::move(waitVia(e).value()); } +template +Try& Future::getTryVia(DrivableExecutor* e) { + return waitVia(e).getTry(); +} + namespace futures { namespace detail { template