Poco

template < class C, class N >

class NObserver

Library: Foundation
Package: Notifications
Header: Poco/NObserver.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.

This class template is quite similar to the Observer class template. The differences are:

  • NObserver expects the callback function to accept a const AutoPtr& instead of a plain pointer as argument, thus simplifying memory management.

  • In addition to dispatching notifications based on the Notification runtime type, NObserver can also notify subscribers based on the Notification name. To enable this functionality, a matcher function must be provided. Null matcher means no matching is performed and all notificiations of the type subscribed to are dispatched.

Thread Safety:

The handle() 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, acceptsSync, clone, disable, equals, handle, handleSync, hasMatcher, match, notify, notifySync, operator =

Inherited Functions: accepts, acceptsSync, backlog, clone, disable, equals, notify, notifySync, operator =, start

Types Aliases

Handler

using Handler = void (C::*)(const NotificationPtr &);

Matcher

using Matcher = bool (C::*)(const std::string &)const;

MatcherFunc

using MatcherFunc = std::function < bool (const std::string &)>;

NotificationPtr

using NotificationPtr = AutoPtr < N >;

SyncHandler

using SyncHandler = NotificationResult (C::*)(const NotificationPtr &);

Type

using Type = NObserver < C, N >;

Constructors

NObserver

NObserver() = delete;

NObserver inline

NObserver(
    const NObserver & observer
);

NObserver inline

NObserver(
    NObserver && observer
);

NObserver inline

NObserver(
    C & object,
    Handler method,
    Matcher matcher = nullptr,
    SyncHandler syncMethod = nullptr
);

NObserver inline

NObserver(
    C & object,
    Handler method,
    MatcherFunc matcherFunc,
    SyncHandler syncMethod = nullptr
);

Destructor

~NObserver virtual

~NObserver() 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;

acceptsSync virtual inline

bool acceptsSync() 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;

notifySync virtual inline

NotificationResult notifySync(
    Notification * pNf
) const override;

operator = inline

NObserver & operator = (
    const NObserver & observer
);

operator = inline

NObserver & operator = (
    NObserver && observer
);

handle protected inline

void handle(
    const NotificationPtr & ptr
) const;

handleSync protected inline

NotificationResult handleSync(
    const NotificationPtr & ptr
) const;

hasMatcher protected inline

bool hasMatcher() const;

match protected inline

bool match(
    const Notification::Ptr & ptr
) const;