waitVia(e).value();
}
-namespace futures {
+template <class T>
+Future<bool> Future<T>::willEqual(Future<T>& f) {
+ return whenAll(*this, f).then([](const std::tuple<Try<T>, Try<T>>& t) {
+ if (std::get<0>(t).hasValue() && std::get<1>(t).hasValue()) {
+ return std::get<0>(t).value() == std::get<1>(t).value();
+ } else {
+ return false;
+ }
+ });
+}
+namespace futures {
namespace {
template <class Z>
Future<Z> chainHelper(Future<Z> f) {
/// Overload of waitVia() for rvalue Futures
Future<T>&& waitVia(DrivableExecutor* e) &&;
- protected:
+ /// If the value in this Future is equal to the given Future, when they have
+ /// both completed, the value of the resulting Future<bool> will be true. It
+ /// will be false otherwise (including when one or both Futures have an
+ /// exception)
+ Future<bool> willEqual(Future<T>&);
+
typedef detail::Core<T>* corePtr;
// shared core state object
EXPECT_THROW(f.get(), std::runtime_error);
EXPECT_EQ(2, count);
}
+
+TEST(Future, willEqual) {
+ //both p1 and p2 already fulfilled
+ {
+ Promise<int> p1;
+ Promise<int> p2;
+ p1.setValue(27);
+ p2.setValue(27);
+ auto f1 = p1.getFuture();
+ auto f2 = p2.getFuture();
+ EXPECT_TRUE(f1.willEqual(f2).get());
+ }{
+ Promise<int> p1;
+ Promise<int> p2;
+ p1.setValue(27);
+ p2.setValue(36);
+ auto f1 = p1.getFuture();
+ auto f2 = p2.getFuture();
+ EXPECT_FALSE(f1.willEqual(f2).get());
+ }
+ //both p1 and p2 not yet fulfilled
+ {
+ Promise<int> p1;
+ Promise<int> p2;
+ auto f1 = p1.getFuture();
+ auto f2 = p2.getFuture();
+ auto f3 = f1.willEqual(f2);
+ p1.setValue(27);
+ p2.setValue(27);
+ EXPECT_TRUE(f3.get());
+ }{
+ Promise<int> p1;
+ Promise<int> p2;
+ auto f1 = p1.getFuture();
+ auto f2 = p2.getFuture();
+ auto f3 = f1.willEqual(f2);
+ p1.setValue(27);
+ p2.setValue(36);
+ EXPECT_FALSE(f3.get());
+ }
+ //p1 already fulfilled, p2 not yet fulfilled
+ {
+ Promise<int> p1;
+ Promise<int> p2;
+ p1.setValue(27);
+ auto f1 = p1.getFuture();
+ auto f2 = p2.getFuture();
+ auto f3 = f1.willEqual(f2);
+ p2.setValue(27);
+ EXPECT_TRUE(f3.get());
+ }{
+ Promise<int> p1;
+ Promise<int> p2;
+ p1.setValue(27);
+ auto f1 = p1.getFuture();
+ auto f2 = p2.getFuture();
+ auto f3 = f1.willEqual(f2);
+ p2.setValue(36);
+ EXPECT_FALSE(f3.get());
+ }
+ //p2 already fulfilled, p1 not yet fulfilled
+ {
+ Promise<int> p1;
+ Promise<int> p2;
+ p2.setValue(27);
+ auto f1 = p1.getFuture();
+ auto f2 = p2.getFuture();
+ auto f3 = f1.willEqual(f2);
+ p1.setValue(27);
+ EXPECT_TRUE(f3.get());
+ }{
+ Promise<int> p1;
+ Promise<int> p2;
+ p2.setValue(36);
+ auto f1 = p1.getFuture();
+ auto f2 = p2.getFuture();
+ auto f3 = f1.willEqual(f2);
+ p1.setValue(27);
+ EXPECT_FALSE(f3.get());
+ }
+}