Tag Parser
12.5.3
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
Toggle main menu visibility
Loading...
Searching...
No Matches
progressfeedback.h
Go to the documentation of this file.
1
#ifndef TAGPARSER_PROGRESS_FEEDBACK_H
2
#define TAGPARSER_PROGRESS_FEEDBACK_H
3
4
#include "
./exceptions.h
"
5
6
#include <atomic>
7
#include <cstdint>
8
#include <functional>
9
#include <string>
10
11
namespace
TagParser
{
12
13
template
<
typename
ActualProgressFeedback>
class
BasicProgressFeedback
{
14
public
:
15
using
Callback
= std::function<void(ActualProgressFeedback &feedback)>;
16
17
explicit
BasicProgressFeedback
(
const
Callback
&callback,
const
Callback
&percentageOnlyCallback =
Callback
());
18
explicit
BasicProgressFeedback
(
Callback
&&callback =
Callback
(),
Callback
&&percentageOnlyCallback =
Callback
());
19
20
const
std::string &
step
()
const
;
21
std::uint8_t
stepPercentage
()
const
;
22
std::uint8_t
overallPercentage
()
const
;
23
void
updateStep
(
const
std::string &
step
, std::uint8_t
stepPercentage
= 0);
24
void
updateStep
(std::string &&
step
, std::uint8_t
stepPercentage
= 0);
25
void
updateStepPercentage
(std::uint8_t
stepPercentage
);
26
void
updateStepPercentageFromFraction
(
double
stepPercentage
);
27
void
updateOverallPercentage
(std::uint8_t
overallPercentage
);
28
29
private
:
30
Callback
m_callback;
31
Callback
m_percentageOnlyCallback;
32
std::string m_step;
33
std::uint8_t m_stepPercentage;
34
std::uint8_t m_overallPercentage;
35
};
36
42
template
<
typename
ActualProgressFeedback>
43
inline
BasicProgressFeedback<ActualProgressFeedback>::BasicProgressFeedback
(
const
Callback
&callback,
const
Callback
&percentageOnlyCallback)
44
: m_callback(callback)
45
, m_percentageOnlyCallback(percentageOnlyCallback)
46
, m_stepPercentage(0)
47
, m_overallPercentage(0)
48
{
49
}
50
56
template
<
typename
ActualProgressFeedback>
57
inline
BasicProgressFeedback<ActualProgressFeedback>::BasicProgressFeedback
(
Callback
&&callback,
Callback
&&percentageOnlyCallback)
58
: m_callback(callback)
59
, m_percentageOnlyCallback(percentageOnlyCallback)
60
, m_stepPercentage(0)
61
, m_overallPercentage(0)
62
{
63
}
64
68
template
<
typename
ActualProgressFeedback>
inline
const
std::string &
BasicProgressFeedback<ActualProgressFeedback>::step
()
const
69
{
70
return
m_step;
71
}
72
77
template
<
typename
ActualProgressFeedback>
inline
std::uint8_t
BasicProgressFeedback<ActualProgressFeedback>::stepPercentage
()
const
78
{
79
return
m_stepPercentage;
80
}
81
86
template
<
typename
ActualProgressFeedback>
inline
std::uint8_t
BasicProgressFeedback<ActualProgressFeedback>::overallPercentage
()
const
87
{
88
return
m_overallPercentage;
89
}
90
95
template
<
typename
ActualProgressFeedback>
96
inline
void
BasicProgressFeedback<ActualProgressFeedback>::updateStep
(
const
std::string &
step
, std::uint8_t
stepPercentage
)
97
{
98
m_step =
step
;
99
m_stepPercentage =
stepPercentage
;
100
if
(m_callback) {
101
m_callback(*
static_cast<
ActualProgressFeedback *
>
(
this
));
102
}
103
}
104
109
template
<
typename
ActualProgressFeedback>
110
inline
void
BasicProgressFeedback<ActualProgressFeedback>::updateStep
(std::string &&
step
, std::uint8_t
stepPercentage
)
111
{
112
m_step =
step
;
113
m_stepPercentage =
stepPercentage
;
114
if
(m_callback) {
115
m_callback(*
static_cast<
ActualProgressFeedback *
>
(
this
));
116
}
117
}
118
123
template
<
typename
ActualProgressFeedback>
124
inline
void
BasicProgressFeedback<ActualProgressFeedback>::updateStepPercentage
(std::uint8_t
stepPercentage
)
125
{
126
m_stepPercentage =
stepPercentage
;
127
if
(m_percentageOnlyCallback) {
128
m_percentageOnlyCallback(*
static_cast<
ActualProgressFeedback *
>
(
this
));
129
}
else
if
(m_callback) {
130
m_callback(*
static_cast<
ActualProgressFeedback *
>
(
this
));
131
}
132
}
133
139
template
<
typename
ActualProgressFeedback>
140
inline
void
BasicProgressFeedback<ActualProgressFeedback>::updateStepPercentageFromFraction
(
double
stepPercentage
)
141
{
142
updateStepPercentage
(
static_cast<
std::uint8_t
>
(
stepPercentage
* 100.0));
143
}
144
149
template
<
typename
ActualProgressFeedback>
150
inline
void
BasicProgressFeedback<ActualProgressFeedback>::updateOverallPercentage
(std::uint8_t
overallPercentage
)
151
{
152
m_overallPercentage =
overallPercentage
;
153
if
(m_percentageOnlyCallback) {
154
m_percentageOnlyCallback(*
static_cast<
ActualProgressFeedback *
>
(
this
));
155
}
else
if
(m_callback) {
156
m_callback(*
static_cast<
ActualProgressFeedback *
>
(
this
));
157
}
158
}
159
160
class
ProgressFeedback
:
public
BasicProgressFeedback
<ProgressFeedback> {
161
public
:
162
explicit
ProgressFeedback
(
const
Callback
&callback,
const
Callback
&percentageOnlyCallback =
Callback
());
163
explicit
ProgressFeedback
(
Callback
&&callback =
Callback
(),
Callback
&&percentageOnlyCallback =
Callback
());
164
};
165
171
inline
ProgressFeedback::ProgressFeedback
(
const
Callback
&callback,
const
Callback
&percentageOnlyCallback)
172
:
BasicProgressFeedback
<
ProgressFeedback
>(callback, percentageOnlyCallback)
173
{
174
}
175
181
inline
ProgressFeedback::ProgressFeedback
(
Callback
&&callback,
Callback
&&percentageOnlyCallback)
182
:
BasicProgressFeedback
<
ProgressFeedback
>(callback, percentageOnlyCallback)
183
{
184
}
185
186
class
AbortableProgressFeedback
:
public
BasicProgressFeedback
<AbortableProgressFeedback> {
187
public
:
188
explicit
AbortableProgressFeedback
(
const
Callback
&callback,
const
Callback
&percentageOnlyCallback =
Callback
());
189
explicit
AbortableProgressFeedback
(
Callback
&&callback =
Callback
(),
Callback
&&percentageOnlyCallback =
Callback
());
190
AbortableProgressFeedback
(
const
AbortableProgressFeedback
&);
191
192
bool
isAborted
()
const
;
193
void
tryToAbort
();
194
void
stopIfAborted
()
const
;
195
void
nextStepOrStop
(
const
std::string &
step
, std::uint8_t
stepPercentage
= 0);
196
void
nextStepOrStop
(std::string &&
step
, std::uint8_t
stepPercentage
= 0);
197
198
private
:
199
std::atomic_bool m_aborted;
200
};
201
207
inline
AbortableProgressFeedback::AbortableProgressFeedback
(
const
Callback
&callback,
const
Callback
&percentageOnlyCallback)
208
:
BasicProgressFeedback
<
AbortableProgressFeedback
>(callback, percentageOnlyCallback)
209
, m_aborted(false)
210
{
211
}
212
218
inline
AbortableProgressFeedback::AbortableProgressFeedback
(
Callback
&&callback,
Callback
&&percentageOnlyCallback)
219
:
BasicProgressFeedback
<
AbortableProgressFeedback
>(callback, percentageOnlyCallback)
220
, m_aborted(false)
221
{
222
}
223
227
inline
AbortableProgressFeedback::AbortableProgressFeedback
(
const
AbortableProgressFeedback
&other)
228
:
BasicProgressFeedback
<
AbortableProgressFeedback
>(other)
229
, m_aborted(other.
isAborted
())
230
{
231
}
232
236
inline
bool
AbortableProgressFeedback::isAborted
()
const
237
{
238
return
m_aborted.load();
239
}
240
246
inline
void
AbortableProgressFeedback::tryToAbort
()
247
{
248
return
m_aborted.store(
true
);
249
}
250
255
inline
void
AbortableProgressFeedback::stopIfAborted
()
const
256
{
257
if
(
isAborted
()) {
258
throw
OperationAbortedException
();
259
}
260
}
261
266
inline
void
AbortableProgressFeedback::nextStepOrStop
(
const
std::string &status, std::uint8_t percentage)
267
{
268
if
(
isAborted
()) {
269
throw
OperationAbortedException
();
270
}
271
updateStep
(status, percentage);
272
}
273
278
inline
void
AbortableProgressFeedback::nextStepOrStop
(std::string &&status, std::uint8_t percentage)
279
{
280
if
(
isAborted
()) {
281
throw
OperationAbortedException
();
282
}
283
updateStep
(status, percentage);
284
}
285
286
}
// namespace TagParser
287
288
#endif
// TAGPARSER_PROGRESS_FEEDBACK_H
TagParser::AbortableProgressFeedback::isAborted
bool isAborted() const
Returns whether the operation has been aborted via tryToAbort().
Definition
progressfeedback.h:236
TagParser::AbortableProgressFeedback::stopIfAborted
void stopIfAborted() const
Throws an OperationAbortedException if aborted.
Definition
progressfeedback.h:255
TagParser::AbortableProgressFeedback::AbortableProgressFeedback
AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new AbortableProgressFeedback.
Definition
progressfeedback.h:207
TagParser::AbortableProgressFeedback::tryToAbort
void tryToAbort()
Aborts the operation.
Definition
progressfeedback.h:246
TagParser::AbortableProgressFeedback::nextStepOrStop
void nextStepOrStop(const std::string &step, std::uint8_t stepPercentage=0)
Throws an OperationAbortedException if aborted; otherwise the data for the next step is set.
Definition
progressfeedback.h:266
TagParser::BasicProgressFeedback::stepPercentage
std::uint8_t stepPercentage() const
Returns the percentage of the current step (initially 0, supposed to be a value from 0 to 100).
Definition
progressfeedback.h:77
TagParser::BasicProgressFeedback::updateStep
void updateStep(const std::string &step, std::uint8_t stepPercentage=0)
Updates the current step and invokes the first callback specified on construction.
Definition
progressfeedback.h:96
TagParser::BasicProgressFeedback::updateStepPercentage
void updateStepPercentage(std::uint8_t stepPercentage)
Updates the current step percentage and invokes the second callback specified on construction (or the...
Definition
progressfeedback.h:124
TagParser::BasicProgressFeedback::step
const std::string & step() const
Returns the name of the current step (initially empty).
Definition
progressfeedback.h:68
TagParser::BasicProgressFeedback::updateStepPercentageFromFraction
void updateStepPercentageFromFraction(double stepPercentage)
Updates the current step percentage and invokes the second callback specified on construction (or the...
Definition
progressfeedback.h:140
TagParser::BasicProgressFeedback::Callback
std::function< void(ActualProgressFeedback &feedback)> Callback
Definition
progressfeedback.h:15
TagParser::BasicProgressFeedback::BasicProgressFeedback
BasicProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new BasicProgressFeedback.
Definition
progressfeedback.h:43
TagParser::BasicProgressFeedback::overallPercentage
std::uint8_t overallPercentage() const
Returns the overall percentage (initially 0, supposed to be a value from 0 to 100).
Definition
progressfeedback.h:86
TagParser::BasicProgressFeedback::updateOverallPercentage
void updateOverallPercentage(std::uint8_t overallPercentage)
Updates the overall percentage and invokes the second callback specified on construction (or the firs...
Definition
progressfeedback.h:150
TagParser::OperationAbortedException::OperationAbortedException
OperationAbortedException() noexcept
Constructs a new exception.
Definition
exceptions.cpp:157
TagParser::ProgressFeedback::ProgressFeedback
ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new ProgressFeedback.
Definition
progressfeedback.h:171
exceptions.h
TagParser
Contains all classes and functions of the TagInfo library.
Definition
aaccodebook.h:10
Generated on
for Tag Parser by
1.18.0