-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprogressfeedback.h
288 lines (255 loc) · 10.2 KB
/
progressfeedback.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#ifndef TAGPARSER_PROGRESS_FEEDBACK_H
#define TAGPARSER_PROGRESS_FEEDBACK_H
#include "./exceptions.h"
#include <atomic>
#include <cstdint>
#include <functional>
#include <string>
namespace TagParser {
template <typename ActualProgressFeedback> class BasicProgressFeedback {
public:
using Callback = std::function<void(ActualProgressFeedback &feedback)>;
explicit BasicProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback = Callback());
explicit BasicProgressFeedback(Callback &&callback = Callback(), Callback &&percentageOnlyCallback = Callback());
const std::string &step() const;
std::uint8_t stepPercentage() const;
std::uint8_t overallPercentage() const;
void updateStep(const std::string &step, std::uint8_t stepPercentage = 0);
void updateStep(std::string &&step, std::uint8_t stepPercentage = 0);
void updateStepPercentage(std::uint8_t stepPercentage);
void updateStepPercentageFromFraction(double stepPercentage);
void updateOverallPercentage(std::uint8_t overallPercentage);
private:
Callback m_callback;
Callback m_percentageOnlyCallback;
std::string m_step;
std::uint8_t m_stepPercentage;
std::uint8_t m_overallPercentage;
};
/*!
* \brief Constructs a new BasicProgressFeedback.
*
* It will call \a callback on the next step and \a percentageOnlyCallback when only the percentage changes.
*/
template <typename ActualProgressFeedback>
inline BasicProgressFeedback<ActualProgressFeedback>::BasicProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
: m_callback(callback)
, m_percentageOnlyCallback(percentageOnlyCallback)
, m_stepPercentage(0)
, m_overallPercentage(0)
{
}
/*!
* \brief Constructs a new BasicProgressFeedback.
*
* It will call \a callback on the next step and \a percentageOnlyCallback when only the percentage changes.
*/
template <typename ActualProgressFeedback>
inline BasicProgressFeedback<ActualProgressFeedback>::BasicProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback)
: m_callback(callback)
, m_percentageOnlyCallback(percentageOnlyCallback)
, m_stepPercentage(0)
, m_overallPercentage(0)
{
}
/*!
* \brief Returns the name of the current step (initially empty).
*/
template <typename ActualProgressFeedback> inline const std::string &BasicProgressFeedback<ActualProgressFeedback>::step() const
{
return m_step;
}
/*!
* \brief Returns the percentage of the current step (initially 0, supposed to be a value from 0 to 100).
* \remarks A percentage of 0 means that the percentage is currently unknown; 100 means finished.
*/
template <typename ActualProgressFeedback> inline std::uint8_t BasicProgressFeedback<ActualProgressFeedback>::stepPercentage() const
{
return m_stepPercentage;
}
/*!
* \brief Returns the overall percentage (initially 0, supposed to be a value from 0 to 100).
* \remarks A percentage of 0 means that the percentage is currently unknown; 100 means finished.
*/
template <typename ActualProgressFeedback> inline std::uint8_t BasicProgressFeedback<ActualProgressFeedback>::overallPercentage() const
{
return m_overallPercentage;
}
/*!
* \brief Updates the current step and invokes the first callback specified on construction.
* \remarks Supposed to be called only by the operation itself.
*/
template <typename ActualProgressFeedback>
inline void BasicProgressFeedback<ActualProgressFeedback>::updateStep(const std::string &step, std::uint8_t stepPercentage)
{
m_step = step;
m_stepPercentage = stepPercentage;
if (m_callback) {
m_callback(*static_cast<ActualProgressFeedback *>(this));
}
}
/*!
* \brief Updates the current step and invokes the first callback specified on construction.
* \remarks Supposed to be called only by the operation itself.
*/
template <typename ActualProgressFeedback>
inline void BasicProgressFeedback<ActualProgressFeedback>::updateStep(std::string &&step, std::uint8_t stepPercentage)
{
m_step = step;
m_stepPercentage = stepPercentage;
if (m_callback) {
m_callback(*static_cast<ActualProgressFeedback *>(this));
}
}
/*!
* \brief Updates the current step percentage and invokes the second callback specified on construction (or the first if only one has been specified).
* \remarks Supposed to be called only by the operation itself.
*/
template <typename ActualProgressFeedback>
inline void BasicProgressFeedback<ActualProgressFeedback>::updateStepPercentage(std::uint8_t stepPercentage)
{
m_stepPercentage = stepPercentage;
if (m_percentageOnlyCallback) {
m_percentageOnlyCallback(*static_cast<ActualProgressFeedback *>(this));
} else if (m_callback) {
m_callback(*static_cast<ActualProgressFeedback *>(this));
}
}
/*!
* \brief Updates the current step percentage and invokes the second callback specified on construction (or the first if only one has been specified).
* \param stepPercentage Specifies the percentage which is supposed to be a value from 0.0 to 1.0.
* \remarks Supposed to be called only by the operation itself.
*/
template <typename ActualProgressFeedback>
inline void BasicProgressFeedback<ActualProgressFeedback>::updateStepPercentageFromFraction(double stepPercentage)
{
updateStepPercentage(static_cast<std::uint8_t>(stepPercentage * 100.0));
}
/*!
* \brief Updates the overall percentage and invokes the second callback specified on construction (or the first if only one has been specified).
* \remarks Supposed to be called only by the operation itself.
*/
template <typename ActualProgressFeedback>
inline void BasicProgressFeedback<ActualProgressFeedback>::updateOverallPercentage(std::uint8_t overallPercentage)
{
m_overallPercentage = overallPercentage;
if (m_percentageOnlyCallback) {
m_percentageOnlyCallback(*static_cast<ActualProgressFeedback *>(this));
} else if (m_callback) {
m_callback(*static_cast<ActualProgressFeedback *>(this));
}
}
class ProgressFeedback : public BasicProgressFeedback<ProgressFeedback> {
public:
explicit ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback = Callback());
explicit ProgressFeedback(Callback &&callback = Callback(), Callback &&percentageOnlyCallback = Callback());
};
/*!
* \brief Constructs a new ProgressFeedback.
*
* It will call \a callback on the next step and \a percentageOnlyCallback when only the percentage changes.
*/
inline ProgressFeedback::ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
: BasicProgressFeedback<ProgressFeedback>(callback, percentageOnlyCallback)
{
}
/*!
* \brief Constructs a new ProgressFeedback.
*
* It will call \a callback on the next step and \a percentageOnlyCallback when only the percentage changes.
*/
inline ProgressFeedback::ProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback)
: BasicProgressFeedback<ProgressFeedback>(callback, percentageOnlyCallback)
{
}
class AbortableProgressFeedback : public BasicProgressFeedback<AbortableProgressFeedback> {
public:
explicit AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback = Callback());
explicit AbortableProgressFeedback(Callback &&callback = Callback(), Callback &&percentageOnlyCallback = Callback());
AbortableProgressFeedback(const AbortableProgressFeedback &);
bool isAborted() const;
void tryToAbort();
void stopIfAborted() const;
void nextStepOrStop(const std::string &step, std::uint8_t stepPercentage = 0);
void nextStepOrStop(std::string &&step, std::uint8_t stepPercentage = 0);
private:
std::atomic_bool m_aborted;
};
/*!
* \brief Constructs a new AbortableProgressFeedback.
*
* It will call \a callback on the next step and \a percentageOnlyCallback when only the percentage changes.
*/
inline AbortableProgressFeedback::AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
: BasicProgressFeedback<AbortableProgressFeedback>(callback, percentageOnlyCallback)
, m_aborted(false)
{
}
/*!
* \brief Constructs a new AbortableProgressFeedback.
*
* It will call \a callback on the next step and \a percentageOnlyCallback when only the percentage changes.
*/
inline AbortableProgressFeedback::AbortableProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback)
: BasicProgressFeedback<AbortableProgressFeedback>(callback, percentageOnlyCallback)
, m_aborted(false)
{
}
/*!
* \brief Constructs a new AbortableProgressFeedback based on \a other.
*/
inline AbortableProgressFeedback::AbortableProgressFeedback(const AbortableProgressFeedback &other)
: BasicProgressFeedback<AbortableProgressFeedback>(other)
, m_aborted(other.isAborted())
{
}
/*!
* \brief Returns whether the operation has been aborted via tryToAbort().
*/
inline bool AbortableProgressFeedback::isAborted() const
{
return m_aborted.load();
}
/*!
* \brief Aborts the operation.
* \remarks The operation will not be killed forcefully. It will be aborted at the next point where it makes sense or even
* finish if it makes no sense to abort.
*/
inline void AbortableProgressFeedback::tryToAbort()
{
return m_aborted.store(true);
}
/*!
* \brief Throws an OperationAbortedException if aborted.
* \remarks Supposed to be called only by the operation itself.
*/
inline void AbortableProgressFeedback::stopIfAborted() const
{
if (isAborted()) {
throw OperationAbortedException();
}
}
/*!
* \brief Throws an OperationAbortedException if aborted; otherwise the data for the next step is set.
* \remarks Supposed to be called only by the operation itself.
*/
inline void AbortableProgressFeedback::nextStepOrStop(const std::string &status, std::uint8_t percentage)
{
if (isAborted()) {
throw OperationAbortedException();
}
updateStep(status, percentage);
}
/*!
* \brief Throws an OperationAbortedException if aborted; otherwise the data for the next step is set.
* \remarks Supposed to be called only by the operation itself.
*/
inline void AbortableProgressFeedback::nextStepOrStop(std::string &&status, std::uint8_t percentage)
{
if (isAborted()) {
throw OperationAbortedException();
}
updateStep(status, percentage);
}
} // namespace TagParser
#endif // TAGPARSER_PROGRESS_FEEDBACK_H