summaryrefslogtreecommitdiffstats
path: root/src/async/background-progress.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/async/background-progress.h')
-rw-r--r--src/async/background-progress.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/async/background-progress.h b/src/async/background-progress.h
new file mode 100644
index 0000000..49fe3d7
--- /dev/null
+++ b/src/async/background-progress.h
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/** \file Background-progress
+ * A Progress object that reports progress thread-safely over a Channel.
+ */
+#ifndef INKSCAPE_ASYNC_BACKGROUND_PROGRESS_H
+#define INKSCAPE_ASYNC_BACKGROUND_PROGRESS_H
+
+#include <functional>
+#include "channel.h"
+#include "progress.h"
+
+namespace Inkscape {
+namespace Async {
+
+template <typename... T>
+class BackgroundProgress final
+ : public Progress<T...>
+{
+public:
+ /**
+ * Construct a Progress object which becomes cancelled as soon as \a channel is closed,
+ * and reports progress by calling \a onprogress over \a channel.
+ *
+ * The result can only be used within the lifetime of \a channel.
+ */
+ BackgroundProgress(Channel::Source &channel, std::function<void(T...)> &onprogress)
+ : channel(&channel)
+ , onprogress(std::move(onprogress)) {}
+
+private:
+ Channel::Source *channel;
+ std::function<void(T...)> onprogress;
+
+ bool _keepgoing() const override { return channel->operator bool(); }
+ bool _report(T const &... progress) override { return channel->run(std::bind(onprogress, progress...)); }
+};
+
+} // namespace Async
+} // namespace Inkscape
+
+#endif // INKSCAPE_ASYNC_BACKGROUND_PROGRESS_H