Tag Parser  6.2.1
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
statusprovider.h
Go to the documentation of this file.
1 #ifndef STATUSPROVIDER_H
2 #define STATUSPROVIDER_H
3 
4 #include "./notification.h"
5 
6 #include <functional>
7 #include <vector>
8 
9 namespace Media {
10 
12 {
13 public:
14  typedef std::function<void (StatusProvider &sender)> CallbackFunction;
15  typedef std::vector<CallbackFunction> CallbackVector;
16  typedef std::pair<int, CallbackFunction> CallbackPair;
17 
18  const NotificationList &notifications() const;
19  bool hasNotifications() const;
20  bool hasCriticalNotifications() const;
21  NotificationType worstNotificationType() const;
22  const std::string &currentStatus() const;
23  double currentPercentage() const;
24  size_t registerCallback(CallbackFunction callback);
25  void unregisterCallback(size_t id);
26  void unregisterAllCallbacks();
27  void forwardStatusUpdateCalls(StatusProvider *other = nullptr);
28  inline StatusProvider *usedProvider();
29  void tryToAbort();
30  bool isAborted() const;
31  void invalidateStatus();
32  void invalidateNotifications();
33  void updateStatus(const std::string &status);
34  void updateStatus(const std::string &status, double percentage);
35  void updatePercentage(double percentage);
36  void addNotification(const Notification &notification);
37  void addNotification(NotificationType type, const std::string &message, const std::string &context);
38  void addNotifications(const StatusProvider &from);
39  void addNotifications(const std::string &higherContext, const StatusProvider &from);
40  void addNotifications(const NotificationList &notifications);
41 
42 protected:
44 
45 private:
46  inline void invokeCallbacks();
47  inline void updateWorstNotificationType(NotificationType notificationType);
48 
49  NotificationList m_notifications;
50  NotificationType m_worstNotificationType;
51  std::string m_status;
52  double m_percentage;
53  CallbackVector m_callbacks;
54  bool m_abort;
55  StatusProvider *m_forward;
56 };
57 
61 inline void StatusProvider::updateStatus(const std::string &status)
62 {
63  m_status = status;
64  invokeCallbacks();
65 }
66 
72 inline void StatusProvider::updateStatus(const std::string &status, double percentage)
73 {
74  m_status = status;
75  m_percentage = percentage;
76  invokeCallbacks();
77 }
78 
84 inline void StatusProvider::updatePercentage(double percentage)
85 {
86  m_percentage = percentage;
87  invokeCallbacks();
88 }
89 
96 {
97  return m_forward ? m_forward->usedProvider() : this;
98 }
99 
104 {
105  return m_notifications;
106 }
107 
112 {
113  return m_notifications.size();
114 }
115 
120 {
121  return m_worstNotificationType == NotificationType::Critical;
122 }
123 
128 {
129  return m_worstNotificationType;
130 }
131 
135 inline const std::string &StatusProvider::currentStatus() const
136 {
137  if(m_status.empty() && m_forward) {
138  return m_forward->currentStatus();
139  }
140  return m_status;
141 }
142 
147 {
148  if(m_percentage == 0.0 && m_forward) {
149  return m_forward->currentPercentage();
150  }
151  return m_percentage;
152 }
153 
160 inline bool StatusProvider::isAborted() const
161 {
162  return m_abort || (m_forward && m_forward->isAborted());
163 }
164 
172 {
173  if(id < m_callbacks.size()) {
174  m_callbacks[id] = nullptr;
175  }
176 }
177 
184 {
185  m_callbacks.clear();
186 }
187 
209 {
210  m_forward = other;
211 }
212 
221 {
222  m_abort = true;
223 }
224 
235 {
236  m_status.clear();
237  m_percentage = 0.0;
238  m_abort = false;
239 }
240 
247 {
248  m_notifications.clear();
249  m_worstNotificationType = NotificationType::Information;
250 }
251 
255 inline void StatusProvider::invokeCallbacks()
256 {
257  for(std::function<void (StatusProvider &sender)> &callback : usedProvider()->m_callbacks) {
258  if(callback) {
259  callback(*this);
260  }
261  }
262 }
263 
267 inline void StatusProvider::updateWorstNotificationType(NotificationType notificationType)
268 {
269  if(static_cast<int>(m_worstNotificationType) < static_cast<int>(notificationType)) {
270  m_worstNotificationType = notificationType;
271  }
272 }
273 
274 }
275 
276 
277 #endif // STATUSPROVIDER_H
void unregisterAllCallbacks()
Unregisters all callback functions.
bool hasCriticalNotifications() const
Returns an indication whether there are critical notifications for the current object.
void invalidateStatus()
Invalidates the current status.
bool isAborted() const
Returns an indication whether the current operation should be aborted.
const NotificationList & notifications() const
Returns notifications for the current object.
NotificationType
Specifies the notification type.
Definition: notification.h:18
double currentPercentage() const
Returns the progress percentage of the current object.
std::vector< CallbackFunction > CallbackVector
NotificationType worstNotificationType() const
Returns the worst notification type.
void unregisterCallback(size_t id)
Unregisters a previously registered callback function whith the specified id.
void updatePercentage(double percentage)
This method is meant to be called by the derived class to report updated progress percentage only...
bool hasNotifications() const
Returns an indication whether there are notifications for the current object.
void tryToAbort()
Commands the object to abort the current operation.
std::function< void(StatusProvider &sender)> CallbackFunction
std::list< Notification > NotificationList
Definition: notification.h:39
void invalidateNotifications()
Invalidates the object&#39;s notifications.
The Notification class holds a notification message of a certain notification type.
Definition: notification.h:43
StatusProvider * usedProvider()
Returns the provider which callback functions will be called when the status or the percentage is upd...
const std::string & currentStatus() const
Returns a status information for the current object.
void updateStatus(const std::string &status)
This method is meant to be called by the derived class to report updated status information.
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
std::pair< int, CallbackFunction > CallbackPair
The StatusProvider class acts as a base class for objects providing status information.
void forwardStatusUpdateCalls(StatusProvider *other=nullptr)
Forwards all status updates calls to the specified statusProvider.
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.