Commit 2006de6b authored by Dave Watson's avatar Dave Watson Committed by Andrew Cox

Future constructor

Summary:
As a replacement for makeFuture().  The main advantage is for non-void futures, implicit conversion works pretty well.

See unittest for examples

Test Plan: fbconfig -r folly/futures; fbmake runtests

Reviewed By: hannesr@fb.com

Subscribers: yfeldblum, trunkagent, doug, folly-diffs@, jsedgwick

FB internal diff: D1806575

Signature: t1:1806575:1422465608:6099f791591b70ce1bcda439b49307b8f3187d89
parent e7e16f3d
......@@ -42,6 +42,36 @@ Future<T>& Future<T>::operator=(Future<T>&& other) {
return *this;
}
template <class T>
template <class F>
Future<T>::Future(
const typename std::enable_if<!std::is_void<F>::value, F>::type& val)
: core_(nullptr) {
Promise<F> p;
p.setValue(val);
*this = p.getFuture();
}
template <class T>
template <class F>
Future<T>::Future(
typename std::enable_if<!std::is_void<F>::value, F>::type&& val)
: core_(nullptr) {
Promise<F> p;
p.setValue(std::forward<F>(val));
*this = p.getFuture();
}
template <>
template <class F,
typename std::enable_if<std::is_void<F>::value, int>::type>
Future<void>::Future() : core_(nullptr) {
Promise<void> p;
p.setValue();
*this = p.getFuture();
}
template <class T>
Future<T>::~Future() {
detach();
......
......@@ -181,6 +181,19 @@ class Future {
Future(Future&&) noexcept;
Future& operator=(Future&&);
// makeFuture
template <class F = T>
/* implicit */
Future(const typename std::enable_if<!std::is_void<F>::value, F>::type& val);
template <class F = T>
/* implicit */
Future(typename std::enable_if<!std::is_void<F>::value, F>::type&& val);
template <class F = T,
typename std::enable_if<std::is_void<F>::value, int>::type = 0>
Future();
~Future();
/** Return the reference to result. Should not be called if !isReady().
......
......@@ -1311,3 +1311,18 @@ TEST(Future, CircularDependencySharedPtrSelfReset) {
promise.fulfil([]{return 1l;});
}
TEST(Future, Constructor) {
auto f1 = []() -> Future<int> { return Future<int>(3); }();
EXPECT_EQ(f1.value(), 3);
auto f2 = []() -> Future<void> { return Future<void>(); }();
EXPECT_NO_THROW(f2.value());
}
TEST(Future, ImplicitConstructor) {
auto f1 = []() -> Future<int> { return 3; }();
EXPECT_EQ(f1.value(), 3);
// Unfortunately, the C++ standard does not allow the
// following implicit conversion to work:
//auto f2 = []() -> Future<void> { }();
}
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