MythTV  master
mythobservable.cpp
Go to the documentation of this file.
1 #include <QCoreApplication>
2 #include <QObject>
3 #include <QMutex>
4 
5 #include "mythobservable.h"
6 
23 {
24  m_lock->lock();
25  m_listeners.clear();
26  m_lock->unlock();
27  delete m_lock;
28  m_lock = nullptr;
29 }
30 
38 void MythObservable::addListener(QObject *listener)
39 {
40  if (listener)
41  {
42  QMutexLocker locker(m_lock);
43  m_listeners.insert(listener);
44  }
45 }
46 
47 
55 void MythObservable::removeListener(QObject *listener)
56 {
57  if (listener)
58  {
59  QMutexLocker locker(m_lock);
60  m_listeners.remove(listener);
61  QCoreApplication::removePostedEvents(listener);
62  }
63 }
64 
74 {
75  QMutexLocker locker(m_lock);
76 
77  for (auto *listener : qAsConst(m_listeners))
78  QCoreApplication::postEvent(listener, event.clone());
79 }
MythObservable::m_lock
QMutex * m_lock
Definition: mythobservable.h:29
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:17
MythObservable::dispatch
void dispatch(const MythEvent &event)
Dispatch an event to all listeners.
Definition: mythobservable.cpp:73
MythObservable::m_listeners
QSet< QObject * > m_listeners
Definition: mythobservable.h:30
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
MythObservable::~MythObservable
virtual ~MythObservable()
Definition: mythobservable.cpp:22
MythEvent::clone
virtual MythEvent * clone() const
Definition: mythevent.h:70
MythObservable::removeListener
void removeListener(QObject *listener)
Remove a listener to the observable.
Definition: mythobservable.cpp:55
mythobservable.h