Mesen-SX/Core/NotificationManager.cpp

55 lines
1.3 KiB
C++
Raw Normal View History

2019-02-15 21:33:13 -05:00
#include "stdafx.h"
#include <algorithm>
#include "NotificationManager.h"
void NotificationManager::RegisterNotificationListener(shared_ptr<INotificationListener> notificationListener)
{
auto lock = _lock.AcquireSafe();
2020-12-19 23:30:09 +03:00
for (weak_ptr<INotificationListener> listener : _listeners)
{
if (listener.lock() == notificationListener)
{
2019-02-15 21:33:13 -05:00
//This listener is already registered, do nothing
return;
}
}
_listeners.push_back(notificationListener);
}
void NotificationManager::CleanupNotificationListeners()
{
auto lock = _lock.AcquireSafe();
//Remove expired listeners
_listeners.erase(
std::remove_if(
_listeners.begin(),
_listeners.end(),
[](weak_ptr<INotificationListener> ptr) { return ptr.expired(); }
),
_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
2020-12-19 23:30:09 +03:00
for (weak_ptr<INotificationListener> notificationListener : listeners)
{
2019-02-15 21:33:13 -05:00
shared_ptr<INotificationListener> listener = notificationListener.lock();
2020-12-19 23:30:09 +03:00
if (listener)
{
2019-02-15 21:33:13 -05:00
listener->ProcessNotification(type, parameter);
}
}
}