Poco

template < class C, class N >

class Observer

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 inline

Observer(
    const Observer & observer
);

Observer inline

Observer(
    C & object,
    Callback method
);

Destructor

~Observer virtual

~Observer() override = default;

Member Functions

accepts virtual inline

bool accepts(
    Notification * pNf,
    const char * pName
) const override;

accepts virtual inline

bool accepts(
    const Notification::Ptr & pNf
) const override;

clone virtual inline

AbstractObserver * clone() const override;

disable virtual inline

void disable() override;

equals virtual inline

bool equals(
    const AbstractObserver & abstractObserver
) const override;

notify virtual inline

void notify(
    Notification * pNf
) const override;

operator = inline

Observer & operator = (
    const Observer & observer
);