vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::Bus< EventT > Class Template Reference

Listener list plus deferred-event queue for a single event type. More...

#include <bus.h>

Inheritance diagram for Vkm::Engine::Bus< EventT >:
Vkm::Engine::IBus

Public Member Functions

ListenerId subscribe (std::function< void(const EventT &)> cb)
 Append a listener and return its new id.
bool remove (ListenerId id)
 Erase the listener with id.
void emit (const EventT &event)
 Dispatch event to every current listener synchronously.
void enqueue (EventT event)
 Buffer event for delivery on the next flush().
void takeQueue () override
 Swap the queue aside so what a listener enqueues cannot join it.
void deliver () override
 Deliver the batch takeQueue set aside to every current listener.
bool hasListeners () const override
 Whether any live listener remains, mid-dispatch removals aside.

Detailed Description

template<typename EventT>
class Vkm::Engine::Bus< EventT >

Listener list plus deferred-event queue for a single event type.

Driven only through EventBus.

Member Function Documentation

◆ subscribe()

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

Append a listener and return its new id.

Mid-dispatch, the entry joins once the outermost dispatch unwinds: appending could reallocate the list and destroy the std::function whose operator() is on the stack.

Parameters
cbCalled with each event of this type delivered from now on.
Returns
The id remove() takes.

◆ remove()

template<typename EventT>
bool Vkm::Engine::Bus< EventT >::remove ( ListenerId id)
inline

Erase the listener with id.

Callable from a listener, on itself too. Mid-dispatch the entry is only marked dead, reaped by admitPending: erasing would shift the index walk and skip a listener, and clearing the std::function would free the captures of the callback still running. An entry still in m_pending is erased outright, since nothing walks it.

Parameters
idWhat subscribe() returned.
Returns
true if it was found - erased, or marked dead mid-dispatch.

◆ emit()

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

Dispatch event to every current listener synchronously.

Parameters
eventPassed to each listener.

◆ enqueue()

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

Buffer event for delivery on the next flush().

Parameters
eventEvent to queue; moved in.

◆ takeQueue()

template<typename EventT>
void Vkm::Engine::Bus< EventT >::takeQueue ( )
inlineoverridevirtual

Swap the queue aside so what a listener enqueues cannot join it.

Swaps with a retained member, so a steady frame allocates nothing.

Implements Vkm::Engine::IBus.

◆ deliver()

template<typename EventT>
void Vkm::Engine::Bus< EventT >::deliver ( )
inlineoverridevirtual

Deliver the batch takeQueue set aside to every current listener.

An event a listener enqueues now fires on the next flush, whatever its type.

Implements Vkm::Engine::IBus.

◆ hasListeners()

template<typename EventT>
bool Vkm::Engine::Bus< EventT >::hasListeners ( ) const
inlineoverridevirtual

Whether any live listener remains, mid-dispatch removals aside.

Returns
True while at least one live entry remains.

Implements Vkm::Engine::IBus.


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