#include "stdafx.h" #include #include "NotificationManager.h" void NotificationManager::RegisterNotificationListener(shared_ptr notificationListener) { auto lock = _lock.AcquireSafe(); for(weak_ptr 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(); //Remove expired listeners _listeners.erase( std::remove_if( _listeners.begin(), _listeners.end(), [](weak_ptr ptr) { return ptr.expired(); } ), _listeners.end() ); } void NotificationManager::SendNotification(ConsoleNotificationType type, void* parameter) { vector> listeners; { auto lock = _lock.AcquireSafe(); CleanupNotificationListeners(); listeners = _listeners; } //Iterate on a copy without using a lock for(weak_ptr notificationListener : listeners) { shared_ptr listener = notificationListener.lock(); if(listener) { listener->ProcessNotification(type, parameter); } } }