MythObservable Class Reference

#include <mythobservable.h>

Inheritance diagram for MythObservable:

Decoder MythContext OutputListeners SSDPCache aacDecoder avfDecoder CdDecoder FlacDecoder MadDecoder VorbisDecoder AudioOutput AudioOutputBase AudioReencodeBuffer AudioOutputALSA AudioOutputARTS AudioOutputCA AudioOutputDX AudioOutputJACK AudioOutputNULL AudioOutputOSS AudioOutputWin

List of all members.


Detailed Description

Superclass for making an object have a set of listeners.

This superclass provides the basic API for adding and removing listeners and iterating across them. It is typically used to post events to listening QObjects.

For example, to post a custom event with event id 100 to all listeners :

       void dispatch()
       {
           QObject *listener = firstListener();
               while (listener) {
                   QApplication::postEvent (listener, new QCustomEvent(100));
               listener = nextListener();  
           }
       }

MythEvents can be dispatched to all listeners by calling dispatch or dispatchNow. The former is much preferred as it uses QApplication::postEvent() while the latter uses the blocking QApplication::sendEvent().

The name MythObservable is 'wrong', since all the methods refer to the observers as listeners (ie. addListener), however, MythListenable just doesn't sound right, and fixing all the calls to addListener was too big a patch.

Definition at line 40 of file mythobservable.h.


Public Member Functions

 MythObservable ()
virtual ~MythObservable ()
void addListener (QObject *listener)
 Add a listener to the observable.
void removeListener (QObject *listener)
 Remove a listener to the observable.
QObject * firstListener ()
 Begin iteration across listeners.
QObject * nextListener ()
 Continue iteration to the next listener.
QPtrList< QObject > getListeners (void)
 Get a copy of the list of listener.
void dispatch (MythEvent &event)
 Dispatch an event to all listeners.
void dispatchNow (MythEvent &event)
 Dispatch an event to all listeners.

Private Attributes

QPtrList< QObject > m_listeners

Constructor & Destructor Documentation

MythObservable::MythObservable (  ) 

Definition at line 5 of file mythobservable.cpp.

MythObservable::~MythObservable (  )  [virtual]

Definition at line 9 of file mythobservable.cpp.


Member Function Documentation

void MythObservable::addListener ( QObject *  listener  ) 

void MythObservable::removeListener ( QObject *  listener  ) 

QObject * MythObservable::firstListener (  ) 

Begin iteration across listeners.

If you simply need to iterate across the listeners, use firstListener and nextListener to iterate across the listeners. Ie. instead of

        {
            QPtrList<QObject> listeners = getListeners();
            QObject *listener = listeners.first();
            while (listener) {
                // use listener...
                listener = listeners.next();
            }
        }

you can avoid the copy and just do

        {
            QObject *listener = firstListener();
            while (listener) {
                // use listener...
                listener = nextListener();
            }
        } 

Returns:
pointer to the first listener, NULL if there are no listeners

Definition at line 25 of file mythobservable.cpp.

Referenced by dispatch(), dispatchNow(), and OutputListeners::error().

QObject * MythObservable::nextListener (  ) 

Continue iteration to the next listener.

See firstListener. Returns NULL if there are no more listeners.

Returns:
pointer to the next listener, NULL if there are no more listeners

Definition at line 30 of file mythobservable.cpp.

Referenced by dispatch(), dispatchNow(), and OutputListeners::error().

QPtrList< QObject > MythObservable::getListeners ( void   ) 

Get a copy of the list of listener.

If you need access to more than just iteration via firstListener/nextListerner, you can call this to obtain a QPtrList with all the listeners.

Returns:
a copy of the list of listener

Definition at line 35 of file mythobservable.cpp.

void MythObservable::dispatch ( MythEvent event  ) 

Dispatch an event to all listeners.

Makes a copy of the event on the heap by calling MythEvent::clone and dispatches is by calling QApplication::postEvent.

Parameters:
event a MythEvent to dispatch.

Reimplemented in MythContext.

Definition at line 40 of file mythobservable.cpp.

Referenced by MythContext::dispatch(), Decoder::error(), SSDPCache::NotifyAdd(), SSDPCache::NotifyRemove(), AudioOutputBase::OutputAudioLoop(), VorbisDecoder::run(), MadDecoder::run(), FlacDecoder::run(), avfDecoder::run(), aacDecoder::run(), and AudioOutputBase::Status().

void MythObservable::dispatchNow ( MythEvent event  ) 

Dispatch an event to all listeners.

See dispatch.

Note:
This uses QApplication::sendEvent, which is blocking. It's preferred to use dispatch instead.
Parameters:
event a MythEvent to dispatch.

Reimplemented in MythContext.

Definition at line 50 of file mythobservable.cpp.

Referenced by MythContext::dispatchNow().


Member Data Documentation

QPtrList<QObject> MythObservable::m_listeners [private]


The documentation for this class was generated from the following files:

Generated on Sat Sep 4 05:16:06 2010 for MythTV by  doxygen 1.5.5