Ticket #2557: mythobservable.patch

File mythobservable.patch, 4.5 KB (added by myth@…, 17 years ago)

possible patch

  • libs/libmyth/output.cpp

     
    2222
    2323
    2424void OutputListeners::error(const QString &e) {
    25     QObject *object = firstListener();
    26     while (object) {
    27         QApplication::postEvent(object, new OutputEvent(e));
    28         object = nextListener();
    29     }
     25    OutputEvent ev (e);
     26    dispatch (ev);
    3027}
    3128
    3229void OutputListeners::addVisual(MythTV::Visual *v)
  • libs/libmyth/mythobservable.cpp

     
    1212
    1313void MythObservable::addListener(QObject *listener)
    1414{
     15    QMutexLocker locked (&mutex);
    1516    if (m_listeners.find(listener) == -1)
    1617        m_listeners.append(listener);
    1718}
    1819
    1920void MythObservable::removeListener(QObject *listener)
    2021{
     22    QMutexLocker locked (&mutex);
    2123    if (m_listeners.find(listener) != -1)
    2224        m_listeners.remove(listener);
    2325}
    2426
    25 QObject* MythObservable::firstListener()
    26 {
    27     return m_listeners.first();
    28 }
    29 
    30 QObject* MythObservable::nextListener()
    31 {
    32     return m_listeners.next();
    33 }
    34 
    35 QPtrList<QObject> MythObservable::getListeners()
    36 {
    37     return m_listeners;
    38 }
    39 
    4027void MythObservable::dispatch(MythEvent &event)
    4128{
    42     QObject *listener = firstListener();
     29    QMutexLocker locked (&mutex);
     30    QObject *listener = m_listeners.first();
    4331    while (listener)
    4432    {
    4533        QApplication::postEvent(listener, event.clone());
    46         listener = nextListener();
     34        listener = m_listeners.next();
    4735    }
    4836}
    4937
    5038void MythObservable::dispatchNow(MythEvent &event)
    5139{
    52     QObject *listener = firstListener();
     40    QMutexLocker locked (&mutex);
     41    QObject *listener = m_listeners.first();
    5342    while (listener)
    5443    {
    5544        QApplication::sendEvent(listener, event.clone());
    56         listener = nextListener();
     45        listener = m_listeners.next();
    5746    }
    5847}
    5948
  • libs/libmyth/mythobservable.h

     
    22#define MYTHOBSERVABLE_H_
    33
    44#include <qptrlist.h>
     5#include <qmutex.h>
    56#include "mythexp.h"
    67#include "mythevent.h"
    78
     
    3637    the observers as listeners (ie. addListener), however,
    3738    MythListenable just doesn't sound right, and fixing all the calls
    3839    to addListener was too big a patch.
     40   
     41    All public methods in MythObservable are reentrant.
    3942*/
    4043class MPUBLIC MythObservable
    4144{
     
    6164    */
    6265    void removeListener(QObject *listener);
    6366
    64     /** \brief Begin iteration across listeners
    65 
    66         If you simply need to iterate across the listeners, use \p
    67         firstListener and \p nextListener to iterate across the
    68         listeners. Ie. instead of
    69 
    70         \code
    71         {
    72             QPtrList<QObject> listeners = getListeners();
    73             QObject *listener = listeners.first();
    74             while (listener) {
    75                 // use listener...
    76                 listener = listeners.next();
    77             }
    78         }
    79         \endcode
    80 
    81         you can avoid the copy and just do
    82 
    83         \code
    84         {
    85             QObject *listener = firstListener();
    86             while (listener) {
    87                 // use listener...
    88                 listener = nextListener();
    89             }
    90         }
    91         \endcode
    92 
    93         \returns pointer to the first listener, NULL if there are no listeners
    94     */
    95     QObject* firstListener();
    96 
    97     /** \brief Continue iteration to the next listener
    98 
    99         See firstListener. Returns NULL if there are no more listeners.
    100 
    101         \returns pointer to the next listener, NULL if there are no more listeners
    102     */
    103     QObject* nextListener();
    104 
    105     /** \brief Get a copy of the list of listener
    106 
    107         If you need access to more than just iteration via
    108         firstListener/nextListerner, you can call this to obtain a
    109         QPtrList with all the listeners.
    110 
    111         \returns a copy of the list of listener
    112     */
    113     QPtrList<QObject> getListeners(void);
    114 
    11567    /** \brief Dispatch an event to all listeners
    11668                       
    11769        Makes a copy of the event on the heap by calling
     
    13486    void dispatchNow(MythEvent &event);
    13587
    13688  private:
     89
    13790    QPtrList<QObject> m_listeners;
     91    QMutex mutex;
    13892};
    13993
    14094#endif /* MYTHOBSERVABLE_H */