#include <thread>
#include <folly/Baton.h>
+#include <folly/Optional.h>
#include <folly/futures/detail/Core.h>
#include <folly/futures/Timekeeper.h>
return core_->getTry();
}
+template <class T>
+Optional<Try<T>> Future<T>::poll() {
+ Optional<Try<T>> o;
+ if (core_->ready()) {
+ o = std::move(core_->getTry());
+ }
+ return o;
+}
+
template <class T>
template <typename Executor>
inline Future<T> Future<T>::via(Executor* executor) && {
#include <type_traits>
#include <vector>
+#include <folly/Optional.h>
#include <folly/MoveWrapper.h>
#include <folly/futures/Deprecated.h>
#include <folly/futures/DrivableExecutor.h>
/** A reference to the Try of the value */
Try<T>& getTry();
+ /// If the promise has been fulfilled, return an Optional with the Try<T>.
+ /// Otherwise return an empty Optional.
+ /// Note that this moves the Try<T> out.
+ Optional<Try<T>> poll();
+
/// Block until the future is fulfilled. Returns the value (moved out), or
/// throws the exception. The future must not already have a callback.
T get();
--- /dev/null
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <folly/futures/Future.h>
+
+using namespace folly;
+
+TEST(Sugar, pollReady) {
+ Promise<int> p;
+ auto f = p.getFuture();
+ p.setValue(42);
+ EXPECT_EQ(42, f.poll().value().value());
+}
+
+TEST(SUGAR, pollNotReady) {
+ Promise<int> p;
+ auto f = p.getFuture();
+ EXPECT_FALSE(f.poll().hasValue());
+}
+
+TEST(SUGAR, pollException) {
+ Promise<void> p;
+ auto f = p.getFuture();
+ p.fulfil([] { throw std::runtime_error("Runtime"); });
+ EXPECT_TRUE(f.poll().value().hasException());
+}