Library: Foundation
Package: Notifications
Header: Poco/Observer.h
Description
This template class implements an adapter that sits between a NotificationCenter and an object receiving notifications from it. It is quite similar in concept to the RunnableAdapter, but provides some NotificationCenter specific additional methods. See the NotificationCenter class for information on how to use this template class.
Instead of the Observer class template, you might want to use the NObserver class template, which uses an AutoPtr to pass the Notification to the callback function, thus freeing you from memory management issues.
Thread Safety:
The notify() method uses atomic load for _pObject and does not hold any lock during callback invocation. This prevents lock-order-inversion deadlocks when handlers call back into NotificationCenter (e.g., to remove themselves during disconnect).
Callers must ensure:
- The observed object outlives all pending notifications. This is inherent to the observer pattern: if object C is destroyed while notifications are in flight (e.g., queued in AsyncObserver), undefined behavior occurs.
- Assignment operators should not be used concurrently with notification delivery. Observer instances are typically created once and registered; concurrent modification during active notification dispatch is not supported.
Inheritance
Direct Base Classes: AbstractObserver
All Base Classes: AbstractObserver
Member Summary
Member Functions: accepts, clone, disable, equals, notify, operator =
Inherited Functions: accepts, acceptsSync, backlog, clone, disable, equals, notify, notifySync, operator =, start
Types Aliases
Callback
using Callback = void (C::*)(N *);
Constructors
Observer
Observer() = delete;
Observer
Observer(
const Observer & observer
);
Observer
Observer(
C & object,
Callback method
);
Destructor
~Observer
~Observer() override = default;
Member Functions
accepts
bool accepts(
Notification * pNf,
const char * pName
) const override;
See also: Poco::AbstractObserver::accepts()
accepts
bool accepts(
const Notification::Ptr & pNf
) const override;
See also: Poco::AbstractObserver::accepts()
clone
AbstractObserver * clone() const override;
See also: Poco::AbstractObserver::clone()
disable
void disable() override;
See also: Poco::AbstractObserver::disable()
equals
bool equals(
const AbstractObserver & abstractObserver
) const override;
See also: Poco::AbstractObserver::equals()
notify
void notify(
Notification * pNf
) const override;
See also: Poco::AbstractObserver::notify()
operator =
Observer & operator = (
const Observer & observer
);