LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
plugin::PluginEventBus Class Reference

Central event bus for plugin lifecycle notifications. More...

#include <PluginEventBus.hpp>

Collaboration diagram for plugin::PluginEventBus:
Collaboration graph

Public Types

using ObserverPtr = std::shared_ptr< IPluginEventObserver >
 Shared pointer to observer.
 

Public Member Functions

 PluginEventBus (const PluginEventBus &)=delete
 
PluginEventBus & operator= (const PluginEventBus &)=delete
 
void Subscribe (ObserverPtr observer)
 Subscribes observer to plugin events.
 
void Unsubscribe (const IPluginEventObserver *observer)
 Unsubscribes observer from plugin events.
 
void PublishEvent (const PluginEvent &event)
 Publishes an event to all subscribed observers.
 
size_t GetObserverCount () const
 Gets number of active observers.
 

Static Public Member Functions

static PluginEventBus & GetInstance ()
 Gets singleton instance.
 

Private Member Functions

 PluginEventBus ()=default
 
void LogEvent (const PluginEvent &event) const
 Logs event details for debugging.
 
void CleanupDeadObserversUnlocked ()
 Removes expired weak_ptr observers.
 

Private Attributes

std::shared_mutex m_observersMutex
 
std::vector< std::weak_ptr< IPluginEventObserver > > m_observers
 

Detailed Description

Central event bus for plugin lifecycle notifications.

Manages subscriptions to plugin events and notifies all interested observers when events occur. Uses weak_ptr to automatically clean up destroyed observers.

Thread Safety
Thread-safe with shared_mutex for concurrent observer access.
Usage Example
class MyObserver : public IPluginEventObserver {
void OnPluginEvent(const PluginEvent& event) override {
if (event.type == PluginEventType::Loaded) {
Logger::Info("Plugin loaded: {}", event.pluginId);
}
}
};
auto busextra = PluginEventBus::GetInstance();
auto observer = std::make_shared<MyObserver>();
bus.Subscribe(observer);
// Later, plugins or system publishes events
PluginEvent evt{PluginEventType::Loaded, "plugin-id", ...};
bus.PublishEvent(evt);
Interface for observing plugin events.
Definition PluginEventBus.hpp:70
static PluginEventBus & GetInstance()
Gets singleton instance.
Definition PluginEventBus.hpp:122
@ Loaded
Basic lifecycle events.
PluginEvent
Definition IPluginObserver.hpp:10
Detailed information about plugin state change.
Definition PluginEventBus.hpp:56
std::string pluginId
Plugin ID.
Definition PluginEventBus.hpp:58
PluginEventType type
Event type.
Definition PluginEventBus.hpp:57

Member Typedef Documentation

◆ ObserverPtr

Shared pointer to observer.

Constructor & Destructor Documentation

◆ PluginEventBus() [1/2]

plugin::PluginEventBus::PluginEventBus ( const PluginEventBus &  )
delete

◆ PluginEventBus() [2/2]

plugin::PluginEventBus::PluginEventBus ( )
privatedefault

Member Function Documentation

◆ CleanupDeadObserversUnlocked()

void plugin::PluginEventBus::CleanupDeadObserversUnlocked ( )
inlineprivate

Removes expired weak_ptr observers.

Called with lock held to clean up dead observers.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetInstance()

static PluginEventBus & plugin::PluginEventBus::GetInstance ( )
inlinestatic

Gets singleton instance.

Returns
Reference to PluginEventBus singleton

◆ GetObserverCount()

size_t plugin::PluginEventBus::GetObserverCount ( ) const
inline

Gets number of active observers.

Returns
Count of subscribed observers

◆ LogEvent()

void plugin::PluginEventBus::LogEvent ( const PluginEvent &  event) const
inlineprivate

Logs event details for debugging.

Parameters
eventEvent to log
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

PluginEventBus & plugin::PluginEventBus::operator= ( const PluginEventBus &  )
delete

◆ PublishEvent()

void plugin::PluginEventBus::PublishEvent ( const PluginEvent &  event)
inline

Publishes an event to all subscribed observers.

Parameters
eventThe plugin event to publish

Dead observers are automatically detected and removed. If an observer throws, the exception is logged but other observers are still notified (best-effort delivery).

Here is the call graph for this function:

◆ Subscribe()

void plugin::PluginEventBus::Subscribe ( ObserverPtr  observer)
inline

Subscribes observer to plugin events.

Parameters
observerShared pointer to observer

Observer will receive OnPluginEvent callbacks until destroyed or explicitly unsubscribed.

Here is the call graph for this function:

◆ Unsubscribe()

void plugin::PluginEventBus::Unsubscribe ( const IPluginEventObserver *  observer)
inline

Unsubscribes observer from plugin events.

Parameters
observerObserver to unsubscribe

Safe if observer not subscribed or already destroyed.

Here is the call graph for this function:

Member Data Documentation

◆ m_observers

std::vector<std::weak_ptr<IPluginEventObserver> > plugin::PluginEventBus::m_observers
private

◆ m_observersMutex

std::shared_mutex plugin::PluginEventBus::m_observersMutex
mutableprivate

The documentation for this class was generated from the following file: