X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ffutures%2FPromise-inl.h;h=620890ee2da41a14f0847f0ab675c6e292d114cc;hb=96791c4516497b4ea2ad08af12e4267bd1c4e796;hp=044f94f1934739c92867ce87ca607c5ac4eb4412;hpb=502c8e4b9f082df107bd46170c3d38ce47a61a07;p=folly.git diff --git a/folly/futures/Promise-inl.h b/folly/futures/Promise-inl.h index 044f94f1..620890ee 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. @@ -19,7 +19,7 @@ #include #include -#include +#include #include namespace folly { @@ -29,12 +29,14 @@ Promise::Promise() : retrieved_(false), core_(new 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,16 +44,19 @@ Promise& Promise::operator=(Promise&& other) { template void Promise::throwIfFulfilled() { - if (!core_) + if (UNLIKELY(!core_)) { throw NoState(); - if (core_->ready()) + } + if (UNLIKELY(core_->ready())) { throw PromiseAlreadySatisfied(); + } } template void Promise::throwIfRetrieved() { - if (retrieved_) + if (UNLIKELY(retrieved_)) { throw FutureAlreadyRetrieved(); + } } template @@ -107,7 +112,7 @@ void Promise::setInterruptHandler( } template -void Promise::fulfilTry(Try&& t) { +void Promise::setTry(Try&& t) { throwIfFulfilled(); core_->setResult(std::move(t)); } @@ -118,22 +123,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() { + if (core_) { + return core_->hasResult(); + } + return true; } }