# 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

NObserver(
const NObserver & observer
);

### NObserver

NObserver(
NObserver && observer
);

### NObserver

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

### NObserver

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

## Destructor

### ~NObserver

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

### acceptsSync

bool acceptsSync() const override;

See also: Poco::AbstractObserver::acceptsSync()

### 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()

### notifySync

NotificationResult notifySync(
Notification * pNf
) const override;

See also: Poco::AbstractObserver::notifySync()

### operator =

NObserver & operator = (
const NObserver & observer
);

### operator =

NObserver & operator = (
NObserver && observer
);

### handle

void handle(
const NotificationPtr & ptr
) const;

### handleSync

NotificationResult handleSync(
const NotificationPtr & ptr
) const;

### hasMatcher

bool hasMatcher() const;

### match

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