Move getTry to subclasses.
authorLee Howes <lwh@fb.com>
Wed, 27 Dec 2017 20:54:44 +0000 (12:54 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 27 Dec 2017 21:05:09 +0000 (13:05 -0800)
Summary:
Move getTry from FutureBase to Future and SemiFuture.

Make SemiFuture version move the result out for consistency with get.

Reviewed By: yfeldblum

Differential Revision: D6638561

fbshipit-source-id: 551c0f06ed52ef6d8976a5971a5e90b3ab793da0

folly/futures/Future-inl.h
folly/futures/Future.h

index 70b0f892de00320d9798cd1ff017ee938fc85c6d..c7c66b589b7e2c397f40d3d5e51280f0b18fed34 100644 (file)
@@ -208,12 +208,12 @@ bool FutureBase<T>::isReady() const {
 
 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>
@@ -224,13 +224,6 @@ void FutureBase<T>::detach() {
   }
 }
 
-template <class T>
-Try<T>& FutureBase<T>::getTry() {
-  throwIfInvalid();
-
-  return core_->getTry();
-}
-
 template <class T>
 void FutureBase<T>::throwIfInvalid() const {
   if (!core_) {
@@ -1441,6 +1434,12 @@ T SemiFuture<T>::get(Duration dur) && {
   }
 }
 
+template <class T>
+Try<T> SemiFuture<T>::getTry() && {
+  wait();
+  return std::move(this->core_->getTry());
+}
+
 template <class T>
 Future<T>& Future<T>::wait() & {
   futures::detail::waitImpl(*this);
@@ -1492,6 +1491,13 @@ T Future<T>::get(Duration dur) {
   }
 }
 
+template <class T>
+Try<T>& Future<T>::getTry() {
+  throwIfInvalid();
+
+  return this->core_->getTry();
+}
+
 template <class T>
 T Future<T>::getVia(DrivableExecutor* e) {
   return std::move(waitVia(e).value());
index 2179623b593db88ac2fa92cbaa55d213182a6bc5..20a56b7e05aa612751979cc87f7a80b58a10eff1 100644 (file)
@@ -106,9 +106,6 @@ class FutureBase {
   /// sugar for getTry().hasException()
   bool hasException();
 
-  /** A reference to the Try of the value */
-  Try<T>& getTry();
-
   /// If the promise has been fulfilled, return an Optional with the Try<T>.
   /// Otherwise return an empty Optional.
   /// Note that this moves the Try<T> out.
@@ -233,7 +230,6 @@ class SemiFuture : private futures::detail::FutureBase<T> {
   /* implicit */ SemiFuture(Future<T>&&) noexcept;
 
   using Base::cancel;
-  using Base::getTry;
   using Base::hasException;
   using Base::hasValue;
   using Base::isActive;
@@ -256,6 +252,10 @@ class SemiFuture : private futures::detail::FutureBase<T> {
   /// exception).
   T get(Duration dur) &&;
 
+  /// Block until the future is fulfilled, or until timed out. Returns the
+  /// Try of the value (moved out).
+  Try<T> getTry() &&;
+
   /// Block until this Future is complete. Returns a reference to this Future.
   SemiFuture<T>& wait() &;
 
@@ -392,7 +392,6 @@ class Future : private futures::detail::FutureBase<T> {
   Future& operator=(Future<T2>&&);
 
   using Base::cancel;
-  using Base::getTry;
   using Base::hasException;
   using Base::hasValue;
   using Base::isActive;
@@ -646,6 +645,9 @@ class Future : private futures::detail::FutureBase<T> {
   /// exception).
   T get(Duration dur);
 
+  /** A reference to the Try of the value */
+  Try<T>& getTry();
+
   /// Block until this Future is complete. Returns a reference to this Future.
   Future<T>& wait() &;