summaryrefslogtreecommitdiff
path: root/src/database/include/future_fetcher.hpp
diff options
context:
space:
mode:
authorailurux <ailuruxx@gmail.com>2023-07-08 12:58:07 +1000
committerailurux <ailuruxx@gmail.com>2023-07-08 12:58:07 +1000
commit3de310f6e4c170c4c4bfb789cb07ca10e5ab17b8 (patch)
tree0d13d6efa758b8c029a35c73405529dcadde3788 /src/database/include/future_fetcher.hpp
parentdaa3013836d619d920db3a9dc1f9cc988047a4b4 (diff)
parent8f8bc1f088b389a683735d626cbce9adb1f6dc17 (diff)
downloadtangara-fw-3de310f6e4c170c4c4bfb789cb07ca10e5ab17b8.tar.gz
Merge branch 'main' of git.sr.ht:~jacqueline/tangara-fw
Diffstat (limited to 'src/database/include/future_fetcher.hpp')
-rw-r--r--src/database/include/future_fetcher.hpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/database/include/future_fetcher.hpp b/src/database/include/future_fetcher.hpp
new file mode 100644
index 00000000..e8ce9729
--- /dev/null
+++ b/src/database/include/future_fetcher.hpp
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#pragma once
+
+#include <memory>
+#include <utility>
+
+#include "database.hpp"
+
+namespace database {
+
+/*
+ * Utility to simplify waiting for a std::future to complete without blocking.
+ * Each instance is good for a single future, and does not directly own anything
+ * other than the future itself.
+ */
+template <typename T>
+class FutureFetcher {
+ public:
+ explicit FutureFetcher(std::future<T>&& fut)
+ : is_consumed_(false), fut_(std::move(fut)) {}
+
+ /*
+ * Returns whether or not the underlying future is still awaiting async work.
+ */
+ auto Finished() -> bool {
+ if (!fut_.valid()) {
+ return true;
+ }
+ if (fut_.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
+ return false;
+ }
+ return true;
+ }
+
+ /*
+ * Returns the result of the future, and releases ownership of the underling
+ * resource. Will return an absent value if the future became invalid (e.g.
+ * the promise associated with it was destroyed.)
+ */
+ auto Result() -> std::optional<T> {
+ assert(!is_consumed_);
+ if (is_consumed_) {
+ return {};
+ }
+ is_consumed_ = true;
+ if (!fut_.valid()) {
+ return {};
+ }
+ return fut_.get();
+ }
+
+ private:
+ bool is_consumed_;
+ std::future<T> fut_;
+};
+
+} // namespace database