Make Try independent of Future
authorYedidya Feldblum <yfeldblum@fb.com>
Thu, 19 May 2016 00:39:53 +0000 (17:39 -0700)
committerFacebook Github Bot 7 <facebook-github-bot-7-bot@fb.com>
Thu, 19 May 2016 00:53:24 +0000 (17:53 -0700)
Summary:
[Folly] Make `Try` independent of `Future`.

`Try` is useful for futures, but it can be used independently from futures as well.

Reviewed By: djwatson

Differential Revision: D3319130

fbshipit-source-id: badfcaa482dee6e7bdef14a501b4efc91634fa51

folly/futures/FutureException.h
folly/futures/Try-inl.h
folly/futures/Try.h

index c8cc8644d6e373bfbb6ce0f6afcf8ca30bbc1b65..8e10746ae19d00c16d5e8f8763211b4ecaa4b821 100644 (file)
@@ -76,12 +76,6 @@ class FutureAlreadyRetrieved : public FutureException {
       FutureException("Future already retrieved") { }
 };
 
-class UsingUninitializedTry : public FutureException {
-  public:
-    explicit UsingUninitializedTry() :
-      FutureException("Using unitialized try") { }
-};
-
 class FutureCancellation : public FutureException {
  public:
   FutureCancellation() : FutureException("Future was cancelled") {}
index 499dfa446e3b068b63c46565b33d3fd2705dccaa..ba988e3dbc59a6b0615997fea025b3a99814974f 100644 (file)
@@ -18,8 +18,6 @@
 
 #include <stdexcept>
 
-#include <folly/futures/FutureException.h>
-
 namespace folly {
 
 template <class T>
index 8d112a6087ab35755c42998d1d84b11243b24d5e..ab37a596f69fb4024d374dfab07097ebb81f388f 100644 (file)
 #include <folly/Likely.h>
 #include <folly/Memory.h>
 #include <folly/Portability.h>
-#include <folly/futures/FutureException.h>
 #include <folly/Unit.h>
 
 namespace folly {
 
+class TryException : public std::exception {
+ public:
+  explicit TryException(std::string message_arg) noexcept
+      : message(std::move(message_arg)) {}
+
+  const char* what() const noexcept override {
+    return message.c_str();
+  }
+
+  bool operator==(const TryException& other) const noexcept {
+    return other.message == this->message;
+  }
+
+  bool operator!=(const TryException& other) const noexcept {
+    return !(*this == other);
+  }
+
+ protected:
+  std::string message;
+};
+
+class UsingUninitializedTry : public TryException {
+ public:
+  UsingUninitializedTry() noexcept : TryException("Using unitialized try") {}
+};
+
 /*
  * Try<T> is a wrapper that contains either an instance of T, an exception, or
- * nothing. Its primary use case is as a representation of a Promise or Future's
- * result and so it provides a number of methods that are useful in that
- * context. Exceptions are stored as exception_wrappers so that the user can
+ * nothing. Exceptions are stored as exception_wrappers so that the user can
  * minimize rethrows if so desired.
  *
- * To represent success or a captured exception, use Try<Unit>
+ * To represent success or a captured exception, use Try<Unit>.
  */
 template <class T>
 class Try {
@@ -192,14 +215,14 @@ class Try {
 
   exception_wrapper& exception() {
     if (UNLIKELY(!hasException())) {
-      throw FutureException("exception(): Try does not contain an exception");
+      throw TryException("exception(): Try does not contain an exception");
     }
     return *e_;
   }
 
   const exception_wrapper& exception() const {
     if (UNLIKELY(!hasException())) {
-      throw FutureException("exception(): Try does not contain an exception");
+      throw TryException("exception(): Try does not contain an exception");
     }
     return *e_;
   }
@@ -311,20 +334,20 @@ class Try<void> {
   }
 
   /*
-   * @throws FutureException if the Try doesn't contain an exception
+   * @throws TryException if the Try doesn't contain an exception
    *
    * @returns mutable reference to the exception contained by this Try
    */
   exception_wrapper& exception() {
     if (UNLIKELY(!hasException())) {
-      throw FutureException("exception(): Try does not contain an exception");
+      throw TryException("exception(): Try does not contain an exception");
     }
     return *e_;
   }
 
   const exception_wrapper& exception() const {
     if (UNLIKELY(!hasException())) {
-      throw FutureException("exception(): Try does not contain an exception");
+      throw TryException("exception(): Try does not contain an exception");
     }
     return *e_;
   }