/*
- * 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
#pragma once
#include <algorithm>
template <class T>
T& FutureBase<T>::value() & {
+ return result().value();
+}
+
+template <class T>
+T const& FutureBase<T>::value() const& {
+ return result().value();
+}
+
+template <class T>
+T&& FutureBase<T>::value() && {
+ return std::move(result().value());
+}
+
+template <class T>
+T const&& FutureBase<T>::value() const&& {
+ return std::move(result().value());
+}
+
+template <class T>
+Try<T>& FutureBase<T>::result() & {
throwIfInvalid();
- return core_->getTry().value();
+ return core_->getTry();
}
template <class T>
-T const& FutureBase<T>::value() const& {
+Try<T> const& FutureBase<T>::result() const& {
throwIfInvalid();
- return core_->getTry().value();
+ return core_->getTry();
}
template <class T>
-T&& FutureBase<T>::value() && {
+Try<T>&& FutureBase<T>::result() && {
throwIfInvalid();
- return std::move(core_->getTry().value());
+ return std::move(core_->getTry());
}
template <class T>
-T const&& FutureBase<T>::value() const&& {
+Try<T> const&& FutureBase<T>::result() const&& {
throwIfInvalid();
- return std::move(core_->getTry().value());
+ return std::move(core_->getTry());
}
template <class T>
template <class T>
bool FutureBase<T>::hasValue() {
- return getTry().hasValue();
+ return core_->getTry().hasValue();
}
template <class T>
bool FutureBase<T>::hasException() {
- return getTry().hasException();
+ return core_->getTry().hasException();
}
template <class T>
}
}
-template <class T>
-Try<T>& FutureBase<T>::getTry() {
- throwIfInvalid();
-
- return core_->getTry();
-}
-
template <class T>
void FutureBase<T>::throwIfInvalid() const {
if (!core_) {
return f;
}
-template <class T>
-Try<T>& Future<T>::getTryVia(DrivableExecutor* e) {
- return waitVia(e).getTry();
-}
-
template <class Func>
auto via(Executor* x, Func&& func)
-> Future<typename isFuture<decltype(std::declval<Func>()())>::Inner> {
assert(f.isReady());
}
+template <class T>
+void waitViaImpl(SemiFuture<T>& 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
return std::move(*this);
}
+template <class T>
+SemiFuture<T>& SemiFuture<T>::waitVia(DrivableExecutor* e) & {
+ futures::detail::waitViaImpl(*this, e);
+ return *this;
+}
+
+template <class T>
+SemiFuture<T>&& SemiFuture<T>::waitVia(DrivableExecutor* e) && {
+ futures::detail::waitViaImpl(*this, e);
+ return std::move(*this);
+}
+
template <class T>
T SemiFuture<T>::get() && {
- return std::move(wait().value());
+ return std::move(wait()).value();
}
template <class T>
}
}
+template <class T>
+Try<T> SemiFuture<T>::getTry() && {
+ return std::move(wait()).result();
+}
+
+template <class T>
+Try<T> SemiFuture<T>::getTry(Duration dur) && {
+ wait(dur);
+ if (this->isReady()) {
+ return std::move(this->result());
+ } else {
+ throwTimedOut();
+ }
+}
+
+template <class T>
+T SemiFuture<T>::getVia(DrivableExecutor* e) && {
+ return std::move(waitVia(e)).value();
+}
+
+template <class T>
+Try<T> SemiFuture<T>::getTryVia(DrivableExecutor* e) && {
+ return std::move(waitVia(e)).result();
+}
+
template <class T>
Future<T>& Future<T>::wait() & {
futures::detail::waitImpl(*this);
}
}
+template <class T>
+Try<T>& Future<T>::getTry() {
+ return result();
+}
+
template <class T>
T Future<T>::getVia(DrivableExecutor* e) {
return std::move(waitVia(e).value());
}
+template <class T>
+Try<T>& Future<T>::getTryVia(DrivableExecutor* e) {
+ return waitVia(e).getTry();
+}
+
namespace futures {
namespace detail {
template <class T>