/*
- * 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/Memory.h>
#include <folly/futures/Deprecated.h>
#include <folly/futures/FutureException.h>
+#include <folly/futures/Unit.h>
namespace folly {
* context. Exceptions are stored as exception_wrappers so that the user can
* minimize rethrows if so desired.
*
- * There is a specialization, Try<void>, which represents either success
- * or an exception.
+ * To represent success or a captured exception, use Try<Unit>
*/
template <class T>
class Try {
*/
explicit Try(T&& v) : contains_(Contains::VALUE), value_(std::move(v)) {}
+ /// Implicit conversion from Try<void> to Try<Unit>
+ template <class T2 = T>
+ /* implicit */
+ Try(typename std::enable_if<std::is_same<Unit, T2>::value,
+ Try<void> const&>::type t);
+
/*
* Construct a Try with an exception_wrapper
*
}
// 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();
return *e_;
}
+ const exception_wrapper& exception() const {
+ if (UNLIKELY(!hasException())) {
+ throw FutureException("exception(): Try does not contain an exception");
+ }
+ return *e_;
+ }
+
/*
* If the Try contains an exception and it is of type Ex, execute func(Ex)
*
return *e_;
}
+ const exception_wrapper& exception() const {
+ if (UNLIKELY(!hasException())) {
+ throw FutureException("exception(): Try does not contain an exception");
+ }
+ return *e_;
+ }
+
/*
* If the Try contains an exception and it is of type Ex, execute func(Ex)
*
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 return
*
* @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