|
Minecraft Community Edition 0.1.0
|
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 |
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.
| using mce::QEventBus::Handler = eastl::function<void(const EventType&)> |
Alias for an event handler function.
| EventType | Concrete event type handled by the function. |
The handler receives a const reference to the event instance.
| mce::QEventBus::~QEventBus | ( | ) |
Destructor.
Ensures asynchronous processing is stopped and resources are cleaned up. Any remaining queued events will be destroyed.
|
inline |
Post an event instance to the bus for later processing.
| EventType | The concrete event type being posted. |
| event | The 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.
| 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.
| 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.
| 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.
|
inline |
Subscribe a handler to EventType without RAII token.
| EventType | The event type to subscribe to. |
| handler | The 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.
|
inline |
Subscribe a handler to EventType and receive a RAII token.
| EventType | The event type to subscribe to. |
| handler | The handler called when an EventType is dispatched. |
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.