From: Yedidya Feldblum Date: Thu, 19 May 2016 00:39:53 +0000 (-0700) Subject: Make Try independent of Future X-Git-Tag: 2016.07.26~219 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=75f97748ba727ad5762910e10939632f4ad4990b;p=folly.git Make Try independent of Future 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 --- diff --git a/folly/futures/FutureException.h b/folly/futures/FutureException.h index c8cc8644..8e10746a 100644 --- a/folly/futures/FutureException.h +++ b/folly/futures/FutureException.h @@ -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") {} diff --git a/folly/futures/Try-inl.h b/folly/futures/Try-inl.h index 499dfa44..ba988e3d 100644 --- a/folly/futures/Try-inl.h +++ b/folly/futures/Try-inl.h @@ -18,8 +18,6 @@ #include -#include - namespace folly { template diff --git a/folly/futures/Try.h b/folly/futures/Try.h index 8d112a60..ab37a596 100644 --- a/folly/futures/Try.h +++ b/folly/futures/Try.h @@ -23,19 +23,42 @@ #include #include #include -#include #include 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 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 + * To represent success or a captured exception, use Try. */ template 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 { } /* - * @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_; }