Minecraft Community Edition 0.1.0
Loading...
Searching...
No Matches
mce::QEventBus Class Reference

Thread-safe event bus. More...

#include <QEventBus.hpp>

Public Types

template<typename EventType>
using Handler = eastl::function<void(const EventType&)>
 Alias for an event handler function.

Public Member Functions

 QEventBus (const std::string_view &eventNamespace)
template<typename EventType>
SubscriptionToken subscribeRAII (Handler< EventType > handler)
 Subscribe a handler to EventType and receive a RAII token.
template<typename EventType>
void subscribe (Handler< EventType > handler)
 Subscribe a handler to EventType without RAII token.
template<typename EventType>
void post (EventType &&event)
 Post an event instance to the bus for later processing.
void process ()
 Process and dispatch all currently queued events.
void runAsync ()
 Start asynchronous processing of queued events.
void stop ()
 Stop asynchronous processing and join the worker thread.
size_t getQueueSize () const
 ~QEventBus ()
 Destructor.
const std::string_view & getNamespace () const

Detailed Description

Thread-safe event bus.

QEventBus allows subscribing handlers to event types, posting events to a queue, and dispatching queued events to all registered handlers. It supports both synchronous processing via process() and asynchronous processing via runAsync()/stop().

Handlers are stored per event type (std::type_index of the concrete event) and receive events by const-reference to the concrete event type.

Note
All public subscribe/post/process operations are designed to be thread-safe, but handlers themselves are invoked on the processing thread/context and must be safe to execute there.

Member Typedef Documentation

◆ Handler

template<typename EventType>
using mce::QEventBus::Handler = eastl::function<void(const EventType&)>

Alias for an event handler function.

Template Parameters
EventTypeConcrete event type handled by the function.

The handler receives a const reference to the event instance.

Constructor & Destructor Documentation

◆ ~QEventBus()

mce::QEventBus::~QEventBus ( )

Destructor.

Ensures asynchronous processing is stopped and resources are cleaned up. Any remaining queued events will be destroyed.

Member Function Documentation

◆ post()

template<typename EventType>
void mce::QEventBus::post ( EventType && event)
inline

Post an event instance to the bus for later processing.

Template Parameters
EventTypeThe concrete event type being posted.
Parameters
eventThe event instance to enqueue (moved or copied as needed).

The event is stored as a heap-allocated IEvent-derived instance and will be dispatched during process() or by the asynchronous thread if runAsync() is active.

◆ process()

void mce::QEventBus::process ( )

Process and dispatch all currently queued events.

This method will dequeue all pending events and invoke their registered handlers synchronously in the caller's thread.

◆ runAsync()

void mce::QEventBus::runAsync ( )

Start asynchronous processing of queued events.

This creates a background thread that waits for posted events and calls dispatch on them as they arrive.

◆ stop()

void mce::QEventBus::stop ( )

Stop asynchronous processing and join the worker thread.

Blocks until the background processing thread has exited. After stop() returns, it is safe to destroy the QEventBus or call runAsync() again.

◆ subscribe()

template<typename EventType>
void mce::QEventBus::subscribe ( Handler< EventType > handler)
inline

Subscribe a handler to EventType without RAII token.

Template Parameters
EventTypeThe event type to subscribe to.
Parameters
handlerThe handler called when an EventType is dispatched.

Use this if you do not need automated unsubscription. To remove the handler manually you must extend this class or implement additional removal logic.

◆ subscribeRAII()

template<typename EventType>
SubscriptionToken mce::QEventBus::subscribeRAII ( Handler< EventType > handler)
inline

Subscribe a handler to EventType and receive a RAII token.

Template Parameters
EventTypeThe event type to subscribe to.
Parameters
handlerThe handler called when an EventType is dispatched.
Returns
SubscriptionToken RAII object that will unsubscribe on destruction.

The returned token ensures the handler is removed when the token is destroyed. The unsubscribe action captured in the token is safe to call from any thread.


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