AsyncObserver (#4444)

* feat(AsyncObserver): Improve NotificationCenter speed and usability #4414

* fix(Notification): add missing header

* feat(Any): add checkers for holding nullptr #4447

* feat(NotificationCenter): g++ build and refactoring #4414

* fix(Observer): compile errors on some compilers #4414

* fix(NotificationCenter): compile errors #4414

* chore(ParallelSocketAcceptor): remove unnecessary include and using from header

* feat(AsyncNotificationCenter): add #4414

* test(AsyncNotificationCenter): add mixed observer types to the test #4414

* fix(AsyncNotificationCenter): hangs on program exit #4414

* fix(dev): friend not honored, temporarily make private members public

* fix(AsyncNotificationCenter); remove default #4414
This commit is contained in:
Aleksandar Fabijanic
2024-02-16 09:34:19 +01:00
committed by GitHub
parent 30a0a06bac
commit 88be66972a
24 changed files with 882 additions and 139 deletions

View File

@@ -79,10 +79,11 @@ class Foundation_API NotificationCenter
/// }
{
public:
NotificationCenter();
/// Creates the NotificationCenter.
~NotificationCenter();
virtual ~NotificationCenter();
/// Destroys the NotificationCenter.
void addObserver(const AbstractObserver& observer);
@@ -99,7 +100,7 @@ public:
bool hasObserver(const AbstractObserver& observer) const;
/// Returns true if the observer is registered with this NotificationCenter.
void postNotification(Notification::Ptr pNotification);
virtual void postNotification(Notification::Ptr pNotification);
/// Posts a notification to the NotificationCenter.
/// The NotificationCenter then delivers the notification
/// to all interested observers.
@@ -120,13 +121,29 @@ public:
std::size_t countObservers() const;
/// Returns the number of registered observers.
virtual int backlog() const;
/// Returns the sum of queued notifications
/// for all observers (applies only to asynchronous observers,
/// regular observers post notifications syncronously and
/// never have a backlog).
static NotificationCenter& defaultCenter();
/// Returns a reference to the default
/// NotificationCenter.
protected:
using AbstractObserverPtr = SharedPtr<AbstractObserver>;
using ObserverList = std::vector<AbstractObserverPtr>;
Mutex& mutex()
{
return _mutex;
}
ObserverList observersToNotify(const Notification::Ptr& pNotification) const;
void notifyObservers(Notification::Ptr& pNotification);
private:
typedef SharedPtr<AbstractObserver> AbstractObserverPtr;
typedef std::vector<AbstractObserverPtr> ObserverList;
ObserverList _observers;
mutable Mutex _mutex;