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 
22 MythObservable::MythObservable() : m_lock(new QMutex())
23 {
24 }
25 
27 {
28  m_lock->lock();
29  m_listeners.clear();
30  m_lock->unlock();
31  delete m_lock;
32  m_lock = nullptr;
33 }
34 
42 void MythObservable::addListener(QObject *listener)
43 {
44  if (listener)
45  {
46  QMutexLocker locker(m_lock);
47  m_listeners.insert(listener);
48  }
49 }
50 
51 
59 void MythObservable::removeListener(QObject *listener)
60 {
61  if (listener)
62  {
63  QMutexLocker locker(m_lock);
64  m_listeners.remove(listener);
65  QCoreApplication::removePostedEvents(listener);
66  }
67 }
68 
78 {
79  QMutexLocker locker(m_lock);
80 
81  QSet<QObject*>::const_iterator it = m_listeners.begin();
82  for (; it != m_listeners.end() ; ++it)
83  QCoreApplication::postEvent(*it, event.clone());
84 }
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:15
void dispatch(const MythEvent &event)
Dispatch an event to all listeners.
QSet< QObject * > m_listeners
virtual MythEvent * clone() const
Definition: mythevent.h:62
virtual ~MythObservable()