Tag Parser  10.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
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);
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>
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>
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>
141 {
142  updateStepPercentage(static_cast<std::uint8_t>(stepPercentage * 100.0));
143 }
144 
149 template <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 
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 
191  bool isAborted() const;
192  void tryToAbort();
193  void stopIfAborted() const;
194  void nextStepOrStop(const std::string &step, std::uint8_t stepPercentage = 0);
195  void nextStepOrStop(std::string &&step, std::uint8_t stepPercentage = 0);
196 
197 private:
198  std::atomic_bool m_aborted;
199 };
200 
206 inline AbortableProgressFeedback::AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
207  : BasicProgressFeedback<AbortableProgressFeedback>(callback, percentageOnlyCallback)
208  , m_aborted(false)
209 {
210 }
211 
217 inline AbortableProgressFeedback::AbortableProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback)
218  : BasicProgressFeedback<AbortableProgressFeedback>(callback, percentageOnlyCallback)
219  , m_aborted(false)
220 {
221 }
222 
227 {
228  return m_aborted.load();
229 }
230 
237 {
238  return m_aborted.store(true);
239 }
240 
246 {
247  if (isAborted()) {
249  }
250 }
251 
256 inline void AbortableProgressFeedback::nextStepOrStop(const std::string &status, std::uint8_t percentage)
257 {
258  if (isAborted()) {
260  }
261  updateStep(status, percentage);
262 }
263 
268 inline void AbortableProgressFeedback::nextStepOrStop(std::string &&status, std::uint8_t percentage)
269 {
270  if (isAborted()) {
272  }
273  updateStep(status, percentage);
274 }
275 
276 } // namespace TagParser
277 
278 #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