ClickHouse/base/poco/Foundation/src/NotificationCenter.cpp
Robert Schulze b79ead9c84
Move poco to base/poco/ (#46075)
* Replicate poco into base/poco/

* De-register poco submodule

* Build poco from ClickHouse

* Exclude poco from stylecheck

* Exclude poco from whitespace check

* Exclude poco from typo check

* Remove x bit from sources/headers (the style check complained)

* Exclude poco from duplicate include check

* Fix fasttest

* Remove contrib/poco-cmake/*

* Simplify poco build descriptions

* Remove poco stuff not used by ClickHouse

* Glob poco sources

* Exclude poco from clang-tidy
2023-02-08 12:04:11 +01:00

110 lines
1.9 KiB
C++

//
// NotificationCenter.cpp
//
// Library: Foundation
// Package: Notifications
// Module: NotificationCenter
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/NotificationCenter.h"
#include "Poco/Notification.h"
#include "Poco/Observer.h"
#include "Poco/AutoPtr.h"
#include "Poco/SingletonHolder.h"
namespace Poco {
NotificationCenter::NotificationCenter()
{
}
NotificationCenter::~NotificationCenter()
{
}
void NotificationCenter::addObserver(const AbstractObserver& observer)
{
Mutex::ScopedLock lock(_mutex);
_observers.push_back(observer.clone());
}
void NotificationCenter::removeObserver(const AbstractObserver& observer)
{
Mutex::ScopedLock lock(_mutex);
for (ObserverList::iterator it = _observers.begin(); it != _observers.end(); ++it)
{
if (observer.equals(**it))
{
(*it)->disable();
_observers.erase(it);
return;
}
}
}
bool NotificationCenter::hasObserver(const AbstractObserver& observer) const
{
Mutex::ScopedLock lock(_mutex);
for (ObserverList::const_iterator it = _observers.begin(); it != _observers.end(); ++it)
if (observer.equals(**it)) return true;
return false;
}
void NotificationCenter::postNotification(Notification::Ptr pNotification)
{
poco_check_ptr (pNotification);
ScopedLockWithUnlock<Mutex> lock(_mutex);
ObserverList observersToNotify(_observers);
lock.unlock();
for (ObserverList::iterator it = observersToNotify.begin(); it != observersToNotify.end(); ++it)
{
(*it)->notify(pNotification);
}
}
bool NotificationCenter::hasObservers() const
{
Mutex::ScopedLock lock(_mutex);
return !_observers.empty();
}
std::size_t NotificationCenter::countObservers() const
{
Mutex::ScopedLock lock(_mutex);
return _observers.size();
}
namespace
{
static SingletonHolder<NotificationCenter> sh;
}
NotificationCenter& NotificationCenter::defaultCenter()
{
return *sh.get();
}
} // namespace Poco