poco/Foundation/src/NotificationCenter.cpp
Roger Meier b0581433a7 LICENSE: add info about SPDX-License-Identifier usage and use it
fix: remove executable flag and change back to 100644 (was 100755)

Signed-off-by: Roger Meier <r.meier@siemens.com>
2014-05-14 08:38:09 +02:00

112 lines
2.0 KiB
C++

//
// NotificationCenter.cpp
//
// $Id: //poco/1.4/Foundation/src/NotificationCenter.cpp#2 $
//
// 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