vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::EventBus Class Reference

Typed pub/sub event dispatcher. More...

#include <event_bus.h>

Public Member Functions

template<typename EventT>
ListenerId subscribe (std::function< void(const EventT &)> callback)
 Register a callback for events of type EventT.
template<typename EventT>
bool unsubscribe (ListenerId id)
 Remove a registered listener.
template<typename EventT>
void emit (const EventT &event)
 Fire event synchronously to every listener now.
template<typename EventT>
void enqueue (EventT event)
 Queue event for delivery on the next flush().
void flush ()
 Drain every per-type queue to its listeners.
void dropIdleBuses ()
 Drop every bus nothing listens to any more.
std::size_t busCount () const
 How many event types have a bus right now.

Detailed Description

Typed pub/sub event dispatcher.

Owned by the Engine and carried on FrameContext. Main-thread only. Subscribing and unsubscribing inside a callback are allowed; docs/reference/events.md has the delivery rules.

Member Function Documentation

◆ subscribe()

template<typename EventT>
ListenerId Vkm::Engine::EventBus::subscribe ( std::function< void(const EventT &)> callback)
inline

Register a callback for events of type EventT.

Template Parameters
EventTEvent type listened for.
Parameters
callbackCalled with each EventT emitted or flushed from now on.
Returns
ListenerId for a later unsubscribe().

◆ unsubscribe()

template<typename EventT>
bool Vkm::Engine::EventBus::unsubscribe ( ListenerId id)
inline

Remove a registered listener.

Template Parameters
EventTEvent type it was subscribed to.
Parameters
idWhat subscribe() returned.
Returns
true if it was found and removed.

◆ emit()

template<typename EventT>
void Vkm::Engine::EventBus::emit ( const EventT & event)
inline

Fire event synchronously to every listener now.

Template Parameters
EventTEvent type; no bus is created when nothing listens.
Parameters
eventPassed to each listener.

◆ enqueue()

template<typename EventT>
void Vkm::Engine::EventBus::enqueue ( EventT event)
inline

Queue event for delivery on the next flush().

Template Parameters
EventTEvent type.
Parameters
eventEvent to queue; moved in.

◆ flush()

void Vkm::Engine::EventBus::flush ( )

Drain every per-type queue to its listeners.

Engine::run calls it at the top of Simulation, once per tick and once per frame. A nested call from a listener is ignored.

◆ dropIdleBuses()

void Vkm::Engine::EventBus::dropIdleBuses ( )

Drop every bus nothing listens to any more.

For just before a gameplay module is unmapped (see ScriptModule::releaseRegistrations): a Bus<EventT> for a module's event holds the module's vtable, so a flush() after reload would call into unmapped memory. A bus with a listener stays, so drop those first; an idle bus has nowhere to deliver, so nothing is lost.

◆ busCount()

std::size_t Vkm::Engine::EventBus::busCount ( ) const
inline

How many event types have a bus right now.

Returns
The number of live buses.

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