From c853ffb5b2f75f5a889ed2e3ef89b818a736e87a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:50:49 +0200 Subject: Adding upstream version 1.3+ds. Signed-off-by: Daniel Baumann --- src/async/background-progress.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/async/background-progress.h (limited to 'src/async/background-progress.h') 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 +#include "channel.h" +#include "progress.h" + +namespace Inkscape { +namespace Async { + +template +class BackgroundProgress final + : public Progress +{ +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 &onprogress) + : channel(&channel) + , onprogress(std::move(onprogress)) {} + +private: + Channel::Source *channel; + std::function 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 -- cgit v1.2.3