Commit 75f97748 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot 7

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
parent fdf0474b
......@@ -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") {}
......
......@@ -18,8 +18,6 @@
#include <stdexcept>
#include <folly/futures/FutureException.h>
namespace folly {
template <class T>
......
......@@ -23,19 +23,42 @@
#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_;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment