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  QSet<QObject*>::const_iterator it = m_listeners.begin();
78  for (; it != m_listeners.end() ; ++it)
79  QCoreApplication::postEvent(*it, event.clone());
80 }
void removeListener(QObject *listener)
Remove a listener to the observable.
void addListener(QObject *listener)
Add a listener to the observable.
This class is used as a container for messages.
Definition: mythevent.h:16
void dispatch(const MythEvent &event)
Dispatch an event to all listeners.
QSet< QObject * > m_listeners
virtual MythEvent * clone() const
Definition: mythevent.h:63
virtual ~MythObservable()