Tag Parser 12.1.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
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
11namespace TagParser {
12
13template <typename ActualProgressFeedback> class BasicProgressFeedback {
14public:
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);
28
29private:
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
42template <typename ActualProgressFeedback>
44 : m_callback(callback)
45 , m_percentageOnlyCallback(percentageOnlyCallback)
46 , m_stepPercentage(0)
47 , m_overallPercentage(0)
48{
49}
50
56template <typename ActualProgressFeedback>
58 : m_callback(callback)
59 , m_percentageOnlyCallback(percentageOnlyCallback)
60 , m_stepPercentage(0)
61 , m_overallPercentage(0)
62{
63}
64
68template <typename ActualProgressFeedback> inline const std::string &BasicProgressFeedback<ActualProgressFeedback>::step() const
69{
70 return m_step;
71}
72
77template <typename ActualProgressFeedback> inline std::uint8_t BasicProgressFeedback<ActualProgressFeedback>::stepPercentage() const
78{
79 return m_stepPercentage;
80}
81
86template <typename ActualProgressFeedback> inline std::uint8_t BasicProgressFeedback<ActualProgressFeedback>::overallPercentage() const
87{
88 return m_overallPercentage;
89}
90
95template <typename ActualProgressFeedback>
96inline 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
109template <typename ActualProgressFeedback>
110inline 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
123template <typename ActualProgressFeedback>
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
139template <typename ActualProgressFeedback>
141{
142 updateStepPercentage(static_cast<std::uint8_t>(stepPercentage * 100.0));
143}
144
149template <typename ActualProgressFeedback>
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
160class ProgressFeedback : public BasicProgressFeedback<ProgressFeedback> {
161public:
162 explicit ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback = Callback());
163 explicit ProgressFeedback(Callback &&callback = Callback(), Callback &&percentageOnlyCallback = Callback());
164};
165
171inline ProgressFeedback::ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
172 : BasicProgressFeedback<ProgressFeedback>(callback, percentageOnlyCallback)
173{
174}
175
181inline ProgressFeedback::ProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback)
182 : BasicProgressFeedback<ProgressFeedback>(callback, percentageOnlyCallback)
183{
184}
185
186class AbortableProgressFeedback : public BasicProgressFeedback<AbortableProgressFeedback> {
187public:
188 explicit AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback = Callback());
189 explicit AbortableProgressFeedback(Callback &&callback = Callback(), Callback &&percentageOnlyCallback = Callback());
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
198private:
199 std::atomic_bool m_aborted;
200};
201
207inline AbortableProgressFeedback::AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
208 : BasicProgressFeedback<AbortableProgressFeedback>(callback, percentageOnlyCallback)
209 , m_aborted(false)
210{
211}
212
218inline AbortableProgressFeedback::AbortableProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback)
219 : BasicProgressFeedback<AbortableProgressFeedback>(callback, percentageOnlyCallback)
220 , m_aborted(false)
221{
222}
223
232
237{
238 return m_aborted.load();
239}
240
247{
248 return m_aborted.store(true);
249}
250
256{
257 if (isAborted()) {
259 }
260}
261
266inline void AbortableProgressFeedback::nextStepOrStop(const std::string &status, std::uint8_t percentage)
267{
268 if (isAborted()) {
270 }
271 updateStep(status, percentage);
272}
273
278inline void AbortableProgressFeedback::nextStepOrStop(std::string &&status, std::uint8_t percentage)
279{
280 if (isAborted()) {
282 }
283 updateStep(status, percentage);
284}
285
286} // namespace TagParser
287
288#endif // TAGPARSER_PROGRESS_FEEDBACK_H
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks.
bool isAborted() const
Returns whether the operation has been aborted via tryToAbort().
void stopIfAborted() const
Throws an OperationAbortedException if aborted.
AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new AbortableProgressFeedback.
void tryToAbort()
Aborts the operation.
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.
The BasicProgressFeedback class provides the base for ProgressFeedback and AbortableProgressFeedback.
std::uint8_t stepPercentage() const
Returns the percentage of the current step (initially 0, supposed to be a value from 0 to 100).
void updateStep(std::string &&step, std::uint8_t stepPercentage=0)
Updates the current step and invokes the first callback specified on construction.
void updateStep(const std::string &step, std::uint8_t stepPercentage=0)
Updates the current step and invokes the first callback specified on construction.
BasicProgressFeedback(Callback &&callback=Callback(), Callback &&percentageOnlyCallback=Callback())
Constructs a new BasicProgressFeedback.
void updateStepPercentage(std::uint8_t stepPercentage)
Updates the current step percentage and invokes the second callback specified on construction (or the...
const std::string & step() const
Returns the name of the current step (initially empty).
void updateStepPercentageFromFraction(double stepPercentage)
Updates the current step percentage and invokes the second callback specified on construction (or the...
std::function< void(ActualProgressFeedback &feedback)> Callback
BasicProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new BasicProgressFeedback.
std::uint8_t overallPercentage() const
Returns the overall percentage (initially 0, supposed to be a value from 0 to 100).
void updateOverallPercentage(std::uint8_t overallPercentage)
Updates the overall percentage and invokes the second callback specified on construction (or the firs...
The exception that is thrown when an operation has been stopped and thus not successfully completed b...
Definition exceptions.h:46
The ProgressFeedback class provides feedback about an ongoing operation via callbacks.
ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new ProgressFeedback.
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10