Logo Search packages:      
Sourcecode: poco version File versions  Download package

Poco::NotificationCenter Class Reference

#include <NotificationCenter.h>

List of all members.

Detailed Description

A NotificationCenter is essentially a notification dispatcher. It notifies all observers of notifications meeting specific criteria. This information is encapsulated in Notification objects. Client objects register themselves with the notification center as observers of specific notifications posted by other objects. When an event occurs, an object posts an appropriate notification to the notification center. The notification center invokes the registered method on each matching observer, passing the notification as argument.

The order in which observers receive notifications is undefined. It is possible for the posting object and the observing object to be the same. The NotificationCenter delivers notifications to observers synchronously. In other words the postNotification() method does not return until all observers have received and processed the notification. If an observer throws an exception while handling a notification, the NotificationCenter stops dispatching the notification and postNotification() rethrows the exception.

In a multithreaded scenario, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

The NotificationCenter class is basically a C++ implementation of the NSNotificationCenter class found in Apple's Cocoa (or OpenStep).

While handling a notification, an observer can unregister itself from the notification center, or it can register or unregister other observers. Observers added during a dispatch cycle will not receive the current notification.

The method receiving the notification must be implemented as void handleNotification(MyNotification* pNf); The handler method gets co-ownership of the Notification object and must release it when done. This is best done with an AutoPtr: void MyClass::handleNotification(MyNotification* pNf) { AutoPtr<MyNotification> nf(pNf); ... }

Alternatively, the NObserver class template can be used to register a callback method. In this case, the callback method receives the Notification in an AutoPtr and thus does not have to deal with object ownership issues: void MyClass::handleNotification(const AutoPtr<MyNotification>& pNf) { ... }

Definition at line 55 of file NotificationCenter.h.

Public Member Functions

void addObserver (const AbstractObserver &observer)
 Destroys the NotificationCenter.
bool hasObservers () const
void postNotification (Notification *pNotification)
 Unregisters an observer with the NotificationCenter.
void removeObserver (const AbstractObserver &observer)
 ~NotificationCenter ()
 Creates the NotificationCenter.

Static Public Member Functions

static NotificationCenterdefaultCenter ()

Private Types

typedef std::list
< AbstractObserver * > 

Private Attributes

Mutex _mutex
ObserverList _observers

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

Generated by  Doxygen 1.6.0   Back to index