/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/Likely.h>
#include <folly/Memory.h>
#include <folly/futures/Deprecated.h>
-#include <folly/futures/WangleException.h>
+#include <folly/futures/FutureException.h>
+#include <folly/futures/Unit.h>
-namespace folly { namespace wangle {
+namespace folly {
/*
* Try<T> is a wrapper that contains either an instance of T, an exception, or
}
// Move constructor
- Try(Try<T>&& t);
+ Try(Try<T>&& t) noexcept;
// Move assigner
- Try& operator=(Try<T>&& t);
+ Try& operator=(Try<T>&& t) noexcept;
- // Non-copyable
- Try(const Try<T>& t) = delete;
- // Non-copyable
- Try& operator=(const Try<T>& t) = delete;
+ // Copy constructor
+ Try(const Try& t);
+ // Copy assigner
+ Try& operator=(const Try& t);
~Try();
exception_wrapper& exception() {
if (UNLIKELY(!hasException())) {
- throw WangleException("exception(): Try does not contain an exception");
+ throw FutureException("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");
}
return *e_;
}
return e_->with_exception<Ex>(std::move(func));
}
+ template <bool isTry, typename R>
+ typename std::enable_if<isTry, R>::type get() {
+ return std::forward<R>(*this);
+ }
+
+ template <bool isTry, typename R>
+ typename std::enable_if<!isTry, R>::type get() {
+ return std::forward<R>(value());
+ }
+
private:
Contains contains_;
union {
}
/*
- * @throws WangleException if the Try doesn't contain an exception
+ * @throws FutureException 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 WangleException("exception(): Try does not contain an exception");
+ throw FutureException("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");
}
return *e_;
}
return e_->with_exception<Ex>(std::move(func));
}
+ template <bool, typename R>
+ R get() {
+ return std::forward<R>(*this);
+ }
+
private:
bool hasValue_;
std::unique_ptr<exception_wrapper> e_{nullptr};
* @returns value contained in t
*/
template <typename T>
-T moveFromTry(wangle::Try<T>&& t);
+T moveFromTry(Try<T>&& t);
/*
* Throws if try contained an exception.
*
* @param t Try to move from
*/
-void moveFromTry(wangle::Try<void>&& t);
+void moveFromTry(Try<void>&& t);
/*
* @param f a function to execute and capture the result of (value or exception)
typename std::enable_if<
!std::is_same<typename std::result_of<F()>::type, void>::value,
Try<typename std::result_of<F()>::type>>::type
-makeTryFunction(F&& f);
+makeTryWith(F&& f);
/*
- * Specialization of makeTryFunction for void
+ * Specialization of makeTryWith for void
*
* @param f a function to execute and capture the result of
*
typename std::enable_if<
std::is_same<typename std::result_of<F()>::type, void>::value,
Try<void>>::type
-makeTryFunction(F&& f);
-
+makeTryWith(F&& f);
-}}
+} // folly
#include <folly/futures/Try-inl.h>