return *this;
}
+template <class T>
+SharedPromise<T>::SharedPromise(Future<T> future) {
+ future.then(&SharedPromise<T>::setTry, this);
+}
+
template <class T>
size_t SharedPromise<T>::size() {
std::lock_guard<std::mutex> g(mutex_);
SharedPromise(SharedPromise<T>&&) noexcept;
SharedPromise& operator=(SharedPromise<T>&&) noexcept;
+ /**
+ * Provide a way to split a Future<T>. Note that while the Futures from
+ * `getFuture()' depend on the completion of the parameter Future they do not
+ * inherit any other properties such as Executor's passed to `via' etc.
+ */
+ explicit SharedPromise(Future<T>);
+
/**
* Return a Future tied to the shared core state. Unlike Promise::getFuture,
* this can be called an unlimited number of times per SharedPromise.
f.cancel();
EXPECT_TRUE(flag);
}
+
+TEST(SharedPromise, splitFutureSuccess) {
+ Promise<int> p;
+ SharedPromise<int> sp(p.getFuture());
+ auto f1 = sp.getFuture();
+ EXPECT_FALSE(f1.isReady());
+ p.setValue(1);
+ EXPECT_TRUE(f1.isReady());
+ EXPECT_TRUE(f1.hasValue());
+ auto f2 = sp.getFuture();
+ EXPECT_TRUE(f2.isReady());
+ EXPECT_TRUE(f2.hasValue());
+}
+
+TEST(SharedPromise, splitFutureFailure) {
+ Promise<int> p;
+ SharedPromise<int> sp(p.getFuture());
+ auto f1 = sp.getFuture();
+ EXPECT_FALSE(f1.isReady());
+ try {
+ throw std::runtime_error("Oops");
+ } catch (...) {
+ p.setException(exception_wrapper(std::current_exception()));
+ }
+ EXPECT_TRUE(f1.isReady());
+ EXPECT_TRUE(f1.hasException());
+ auto f2 = sp.getFuture();
+ EXPECT_TRUE(f2.isReady());
+ EXPECT_TRUE(f2.hasException());
+}