Tag Parser  7.1.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 <c++utilities/conversion/types.h>
7 
8 #include <atomic>
9 #include <functional>
10 #include <string>
11 
12 namespace TagParser {
13 
14 template <typename ActualProgressFeedback> class BasicProgressFeedback {
15 public:
16  using Callback = std::function<void(ActualProgressFeedback &feedback)>;
17 
18  BasicProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback = Callback());
19  BasicProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback = Callback());
20 
21  const std::string &step() const;
22  byte stepPercentage() const;
23  byte overallPercentage() const;
24  void updateStep(const std::string &step, byte stepPercentage = 0);
25  void updateStep(std::string &&step, byte stepPercentage = 0);
29 
30 private:
31  Callback m_callback;
32  Callback m_percentageOnlyCallback;
33  std::string m_step;
34  byte m_stepPercentage;
35  byte m_overallPercentage;
36 };
37 
43 template <typename ActualProgressFeedback>
44 inline BasicProgressFeedback<ActualProgressFeedback>::BasicProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
45  : m_callback(callback)
46  , m_percentageOnlyCallback(percentageOnlyCallback)
47  , m_stepPercentage(0)
48  , m_overallPercentage(0)
49 {
50 }
51 
57 template <typename ActualProgressFeedback>
59  : m_callback(callback)
60  , m_percentageOnlyCallback(percentageOnlyCallback)
61  , m_stepPercentage(0)
62  , m_overallPercentage(0)
63 {
64 }
65 
69 template <typename ActualProgressFeedback> inline const std::string &BasicProgressFeedback<ActualProgressFeedback>::step() const
70 {
71  return m_step;
72 }
73 
78 template <typename ActualProgressFeedback> inline byte BasicProgressFeedback<ActualProgressFeedback>::stepPercentage() const
79 {
80  return m_stepPercentage;
81 }
82 
87 template <typename ActualProgressFeedback> inline byte BasicProgressFeedback<ActualProgressFeedback>::overallPercentage() const
88 {
89  return m_overallPercentage;
90 }
91 
96 template <typename ActualProgressFeedback>
97 inline void BasicProgressFeedback<ActualProgressFeedback>::updateStep(const std::string &step, byte stepPercentage)
98 {
99  m_step = step;
100  m_stepPercentage = stepPercentage;
101  if (m_callback) {
102  m_callback(*static_cast<ActualProgressFeedback *>(this));
103  }
104 }
105 
110 template <typename ActualProgressFeedback>
111 inline void BasicProgressFeedback<ActualProgressFeedback>::updateStep(std::string &&step, byte stepPercentage)
112 {
113  m_step = step;
114  m_stepPercentage = stepPercentage;
115  if (m_callback) {
116  m_callback(*static_cast<ActualProgressFeedback *>(this));
117  }
118 }
119 
124 template <typename ActualProgressFeedback> inline void BasicProgressFeedback<ActualProgressFeedback>::updateStepPercentage(byte 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>
141 {
142  updateStepPercentage(static_cast<byte>(stepPercentage * 100.0));
143 }
144 
149 template <typename ActualProgressFeedback> inline void BasicProgressFeedback<ActualProgressFeedback>::updateOverallPercentage(byte overallPercentage)
150 {
151  m_overallPercentage = overallPercentage;
152  if (m_percentageOnlyCallback) {
153  m_percentageOnlyCallback(*static_cast<ActualProgressFeedback *>(this));
154  } else if (m_callback) {
155  m_callback(*static_cast<ActualProgressFeedback *>(this));
156  }
157 }
158 
159 class ProgressFeedback : public BasicProgressFeedback<ProgressFeedback> {
160 public:
161  ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback = Callback());
162  ProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback = Callback());
163 };
164 
170 inline ProgressFeedback::ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
171  : BasicProgressFeedback<ProgressFeedback>(callback, percentageOnlyCallback)
172 {
173 }
174 
180 inline ProgressFeedback::ProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback)
181  : BasicProgressFeedback<ProgressFeedback>(callback, percentageOnlyCallback)
182 {
183 }
184 
185 class AbortableProgressFeedback : public BasicProgressFeedback<AbortableProgressFeedback> {
186 public:
187  AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback = Callback());
188  AbortableProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback = Callback());
189 
190  bool isAborted() const;
191  void tryToAbort();
192  void stopIfAborted() const;
193  void nextStepOrStop(const std::string &step, byte stepPercentage = 0);
194  void nextStepOrStop(std::string &&step, byte stepPercentage = 0);
195 
196 private:
197  std::atomic_bool m_aborted;
198 };
199 
205 inline AbortableProgressFeedback::AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback)
206  : BasicProgressFeedback<AbortableProgressFeedback>(callback, percentageOnlyCallback)
207  , m_aborted(false)
208 {
209 }
210 
216 inline AbortableProgressFeedback::AbortableProgressFeedback(Callback &&callback, Callback &&percentageOnlyCallback)
217  : BasicProgressFeedback<AbortableProgressFeedback>(callback, percentageOnlyCallback)
218  , m_aborted(false)
219 {
220 }
221 
226 {
227  return m_aborted.load();
228 }
229 
236 {
237  return m_aborted.store(true);
238 }
239 
245 {
246  if (isAborted()) {
248  }
249 }
250 
255 inline void AbortableProgressFeedback::nextStepOrStop(const std::string &status, byte percentage)
256 {
257  if (isAborted()) {
259  }
260  updateStep(status, percentage);
261 }
262 
267 inline void AbortableProgressFeedback::nextStepOrStop(std::string &&status, byte percentage)
268 {
269  if (isAborted()) {
271  }
272  updateStep(status, percentage);
273 }
274 
275 } // namespace TagParser
276 
277 #endif // TAGPARSER_PROGRESS_FEEDBACK_H
void updateStepPercentage(byte stepPercentage)
Updates the current step percentage and invokes the second callback specified on construction (or the...
std::function< void(AbortableProgressFeedback &feedback)> Callback
The ProgressFeedback class provides feedback about an ongoing operation via callbacks.
void stopIfAborted() const
Throws an OperationAbortedException if aborted.
BasicProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new BasicProgressFeedback.
The BasicProgressFeedback class provides the base for ProgressFeedback and AbortableProgressFeedback...
ProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new ProgressFeedback.
void updateStepPercentageFromFraction(double stepPercentage)
Updates the current step percentage and invokes the second callback specified on construction (or the...
bool isAborted() const
Returns whether the operation has been aborted via tryToAbort().
byte stepPercentage() const
Returns the percentage of the current step (initially 0, supposed to be a value from 0 to 100)...
byte overallPercentage() const
Returns the overall percentage (initially 0, supposed to be a value from 0 to 100).
void tryToAbort()
Aborts the operation.
AbortableProgressFeedback(const Callback &callback, const Callback &percentageOnlyCallback=Callback())
Constructs a new AbortableProgressFeedback.
void updateOverallPercentage(byte overallPercentage)
Updates the overall percentage and invokes the second callback specified on construction (or the firs...
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks...
void updateStep(const std::string &step, byte stepPercentage=0)
Updates the current step and invokes the first callback specified on construction.
const std::string & step() const
Returns the name of the current step (initially empty).
The exception that is thrown when an operation has been stopped and thus not successfully completed b...
Definition: exceptions.h:39
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
void nextStepOrStop(const std::string &step, byte stepPercentage=0)
Throws an OperationAbortedException if aborted; otherwise the data for the next step is set...