Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
progress.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5#ifndef INKSCAPE_ASYNC_PROGRESS_H
6#define INKSCAPE_ASYNC_PROGRESS_H
7
8#include <chrono>
9
10namespace Inkscape {
11namespace Async {
12
14
21template <typename... T>
23{
24public:
26 bool report(T const &... progress) { return _report(progress...); }
27
29 void report_or_throw(T const &... progress) { if (!_report(progress...)) throw CancelledException(); }
30
32 bool keepgoing() const { return _keepgoing(); }
33
35 void throw_if_cancelled() const { if (!_keepgoing()) throw CancelledException(); }
36
38 operator bool() const { return _keepgoing(); }
39
40protected:
41 ~Progress() = default;
42 virtual bool _keepgoing() const = 0;
43 virtual bool _report(T const &... progress) = 0;
44};
45
49template <typename T, typename... S>
50class SubProgress final
51 : public Progress<T, S...>
52{
53public:
56 {
57 if (auto p = dynamic_cast<SubProgress*>(&parent)) {
58 _root = p->_root;
59 _from = p->_from + p->_amount * from;
60 _amount = p->_amount * amount;
61 } else {
62 _root = &parent;
63 _from = from;
64 _amount = amount;
65 }
66 }
67
68private:
69 Progress<T, S...> *_root;
71
72 bool _keepgoing() const override { return _root->keepgoing(); }
73 bool _report(T const &progress, S const &... aux) override { return _root->report(_from + _amount * progress, aux...); }
74};
75
79template <typename T, typename... S>
81 : public Progress<T, S...>
82{
83public:
86
87private:
88 Progress<T, S...> *parent;
90 T last = 0;
91
92 bool _keepgoing() const override { return parent->keepgoing(); }
93
94 bool _report(T const &progress, S const &... aux) override
95 {
96 if (progress - last < step) {
97 return parent->keepgoing();
98 } else {
99 last = progress;
100 return parent->report(progress, aux...);
101 }
102 }
103};
104
108template <typename... T>
110 : public Progress<T...>
111{
112 using clock = std::chrono::steady_clock;
113 using time_point = clock::time_point;
114
115public:
116 using duration = clock::duration;
117
120
121private:
124 time_point last = clock::now();
125
126 bool _keepgoing() const override { return parent->keepgoing(); }
127
128 bool _report(T const &... progress) override
129 {
130 auto now = clock::now();
131 if (now - last < interval) {
132 return parent->keepgoing();
133 } else {
134 last = now;
135 return parent->report(progress...);
136 }
137 }
138};
139
143template <typename... T>
144class ProgressAlways final
145 : public Progress<T...>
146{
147private:
148 bool _keepgoing() const override { return true; }
149 bool _report(T const &...) override { return true; }
150};
151
152} // namespace Async
153} // namespace Inkscape
154
155#endif // INKSCAPE_ASYNC_PROGRESS_H
A dummy Progress object that never reports cancellation.
Definition progress.h:146
bool _keepgoing() const override
Definition progress.h:148
bool _report(T const &...) override
Definition progress.h:149
A Progress object that throttles reports to a given step size.
Definition progress.h:82
bool _keepgoing() const override
Definition progress.h:92
ProgressStepThrottler(Progress< T, S... > &parent, T step)
Definition progress.h:84
bool _report(T const &progress, S const &... aux) override
Definition progress.h:94
A Progress object that throttles reports to a given time interval.
Definition progress.h:111
ProgressTimeThrottler(Progress< T... > &parent, duration interval)
Definition progress.h:118
bool _keepgoing() const override
Definition progress.h:126
bool _report(T const &... progress) override
Definition progress.h:128
std::chrono::steady_clock clock
Definition progress.h:112
An interface for tasks to report progress and check for cancellation.
Definition progress.h:23
bool keepgoing() const
Return whether not cancelled.
Definition progress.h:32
void report_or_throw(T const &... progress)
Report a progress value, throwing CancelledException if cancelled.
Definition progress.h:29
void throw_if_cancelled() const
Throw CancelledException if cancelled.
Definition progress.h:35
bool report(T const &... progress)
Report a progress value, returning false if cancelled.
Definition progress.h:26
virtual bool _report(T const &... progress)=0
virtual bool _keepgoing() const =0
A Progress object representing a sub-task of another Progress.
Definition progress.h:52
SubProgress(Progress< T, S... > &parent, T from, T amount)
Construct a progress object for a sub-task.
Definition progress.h:55
Progress< T, S... > * _root
Definition progress.h:69
bool _report(T const &progress, S const &... aux) override
Definition progress.h:73
bool _keepgoing() const override
Definition progress.h:72
static char const *const parent
Definition dir-util.cpp:70
Helper class to stream background task notifications as a series of messages.