Files
MesenCE/Core/Shared/NotificationManager.cpp
SourMesen e391fd8f9a Code: Add auto-formatting rules for C++ (clang-format) and enforce in CI via "clang-format" and "dotnet format" (#78)
* Code: Add auto-formatting rules for C++ projects (clang-format) and enforce code style via "clang-format" and "dotnet format"

* fix ui format warnings
2026-04-30 06:03:55 -07:00

46 lines
1.3 KiB
C++

#include "pch.h"
#include <algorithm>
#include "Shared/NotificationManager.h"
void NotificationManager::RegisterNotificationListener(shared_ptr<INotificationListener> notificationListener)
{
auto lock = _lock.AcquireSafe();
for(weak_ptr<INotificationListener> listener : _listeners) {
if(listener.lock() == notificationListener) {
//This listener is already registered, do nothing
return;
}
}
_listeners.push_back(notificationListener);
}
void NotificationManager::CleanupNotificationListeners()
{
auto lock = _lock.AcquireSafe();
auto predicate = [](weak_ptr<INotificationListener> ptr) { return ptr.expired(); };
//Remove expired listeners
_listeners.erase(std::remove_if(_listeners.begin(), _listeners.end(), predicate), _listeners.end());
}
void NotificationManager::SendNotification(ConsoleNotificationType type, void* parameter)
{
vector<weak_ptr<INotificationListener>> listeners;
{
auto lock = _lock.AcquireSafe();
CleanupNotificationListeners();
listeners = _listeners;
}
//Iterate on a copy without using a lock
for(weak_ptr<INotificationListener> notificationListener : listeners) {
shared_ptr<INotificationListener> listener = notificationListener.lock();
if(listener) {
listener->ProcessNotification(type, parameter);
}
}
}