TypeIndexedDoubleBuffer Class Template
Central hub for publishing and consuming typed messages. More...
Declaration
Public Constructors Index
template <typename Indexer> | |
| TypeIndexedDoubleBuffer ()=default | |
|
Default constructor. More... | |
Public Destructor Index
template <typename Indexer> | |
| ~TypeIndexedDoubleBuffer ()=default | |
|
Default destructor. More... | |
Public Member Functions Index
template <typename T, typename... Args> | |
| void | push (Args &&... args) |
|
Pushes a message of type T to the write buffer. More... | |
template <typename T> | |
| auto | read () const noexcept -> std::span< const T > |
|
Returns a read-only view of all messages of type T. More... | |
template <typename T> | |
| void | reserve (size_t size) |
|
Pre-allocates capacity for messages of type T. More... | |
template <typename T> | |
| auto | getOrCreateBuffer () -> ReadWriteDoubleBuffer< T > & |
|
Gets or creates the buffer for message type T. More... | |
template <typename Indexer> | |
| void | swapBuffers () |
|
Swaps read and write buffers for all message types. More... | |
template <typename Indexer> | |
| void | clearReadBuffers () |
|
Clears all read buffers. More... | |
template <typename Indexer> | |
| void | clearWriteBuffers () |
|
Clears all write buffers without swapping. More... | |
template <typename Indexer> | |
| void | clearAll () |
|
Clears both read and write buffers for all message types. More... | |
template <typename Indexer> | |
| WriteSink | writeSink () noexcept |
template <typename Indexer> | |
| ReadSource | readSource () noexcept |
|
Creates a ReadSource handle for this buffer. More... | |
Private Member Attributes Index
template <typename Indexer> | |
| std::vector< std::unique_ptr< DoubleBuffer > > | buffers_ |
|
Type-erased storage for message buffers, indexed by type. More... | |
Private Static Functions Index
template <typename T> | |
| static size_t | index () |
|
Returns the buffer index for message type T. More... | |
Description
Central hub for publishing and consuming typed messages.
TypeIndexedDoubleBuffer provides a double-buffered, type-indexed message system for decoupled inter-system communication. Systems push messages during their update phase, then at frame boundaries `swapBuffers()` is called to make those messages available for reading.
Each message type T gets its own dedicated ReadWriteDoubleBuffer, indexed via TypeIndexer for O(1) access. This design enables efficient, allocation-friendly inter-system communication.
Example usage: ```cpp // Define a message struct CollisionMessage { EntityId a; EntityId b; vec3f contact; };
// In collision system messageBuffer.push<CollisionMessage>(entityA, entityB, contactPoint);
// At frame boundary messageBuffer.swapBuffers();
// In damage system (next phase or frame) for (const auto& msg : messageBuffer.read<CollisionMessage>()) { applyDamage(msg.a, msg.b); } ```
- Template Parameters
-
Indexer The TypeIndexer used for mapping message types to buffer indices.
Definition at line 60 of file TypeIndexedDoubleBuffer.ixx.
Public Constructors
TypeIndexedDoubleBuffer()
| default |
Default constructor.
Definition at line 89 of file TypeIndexedDoubleBuffer.ixx.
Referenced by helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::ReadSource::ReadSource and helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::WriteSink::WriteSink.
Public Destructor
~TypeIndexedDoubleBuffer()
| default |
Default destructor.
Definition at line 84 of file TypeIndexedDoubleBuffer.ixx.
Public Member Functions
clearAll()
| inline |
Clears both read and write buffers for all message types.
Completely resets the buffer state, discarding all messages.
Definition at line 208 of file TypeIndexedDoubleBuffer.ixx.
clearReadBuffers()
| inline |
Clears all read buffers.
Typically not needed as `swapBuffers()` clears read buffers automatically.
Definition at line 181 of file TypeIndexedDoubleBuffer.ixx.
Referenced by helios::core::container::buffer::TypeIndexedDoubleBuffer< helios::core::data::TypeIndexer< GameLoopEventBusGroup > >::clearAll.
clearWriteBuffers()
| inline |
Clears all write buffers without swapping.
Use to discard messages that were pushed but should not be processed.
Definition at line 194 of file TypeIndexedDoubleBuffer.ixx.
Referenced by helios::core::container::buffer::TypeIndexedDoubleBuffer< helios::core::data::TypeIndexer< GameLoopEventBusGroup > >::clearAll.
getOrCreateBuffer()
| inline |
Gets or creates the buffer for message type T.
- Template Parameters
-
T The message type.
- Returns
Reference to the ReadWriteDoubleBuffer for type T.
Definition at line 148 of file TypeIndexedDoubleBuffer.ixx.
Referenced by helios::core::container::buffer::TypeIndexedDoubleBuffer< helios::core::data::TypeIndexer< GameLoopEventBusGroup > >::push and helios::core::container::buffer::TypeIndexedDoubleBuffer< helios::core::data::TypeIndexer< GameLoopEventBusGroup > >::reserve.
push()
| inline |
Pushes a message of type T to the write buffer.
- Template Parameters
-
T The message type.
Args Constructor argument types for T.
- Parameters
-
args Arguments forwarded to T's constructor.
Definition at line 100 of file TypeIndexedDoubleBuffer.ixx.
read()
| inline noexcept |
Returns a read-only view of all messages of type T.
Returns messages that were written before the last `swapBuffers()` call. If no messages of type T exist, returns an empty span.
- Template Parameters
-
T The message type to read.
- Returns
A span over all messages of type T in the read buffer.
Definition at line 115 of file TypeIndexedDoubleBuffer.ixx.
readSource()
| inline nodiscard noexcept |
Creates a ReadSource handle for this buffer.
- Returns
A ReadSource that can read messages from this buffer.
Definition at line 309 of file TypeIndexedDoubleBuffer.ixx.
reserve()
| inline |
Pre-allocates capacity for messages of type T.
Call during initialization to avoid allocations during gameplay.
- Template Parameters
-
T The message type.
- Parameters
-
size The number of messages to reserve capacity for.
Definition at line 136 of file TypeIndexedDoubleBuffer.ixx.
swapBuffers()
| inline |
Swaps read and write buffers for all message types.
Call once per frame at a consistent point (e.g., end of update phase). After this call, messages pushed in the current frame become readable.
Definition at line 168 of file TypeIndexedDoubleBuffer.ixx.
writeSink()
| inline nodiscard noexcept |
Private Member Attributes
buffers_
|
Type-erased storage for message buffers, indexed by type.
Definition at line 65 of file TypeIndexedDoubleBuffer.ixx.
Private Static Functions
index()
| inline static |
Returns the buffer index for message type T.
- Template Parameters
-
T The message type.
- Returns
The index assigned to type T by the Indexer.
Definition at line 75 of file TypeIndexedDoubleBuffer.ixx.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.