Skip to main content

TypeIndexedDoubleBuffer Class Template

Central hub for publishing and consuming typed messages. More...

Declaration

template <typename Indexer> class helios::core::container::buffer::TypeIndexedDoubleBuffer<Indexer> { ... }

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>
voidpush (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>
voidreserve (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>
voidswapBuffers ()

Swaps read and write buffers for all message types. More...

template <typename Indexer>
voidclearReadBuffers ()

Clears all read buffers. More...

template <typename Indexer>
voidclearWriteBuffers ()

Clears all write buffers without swapping. More...

template <typename Indexer>
voidclearAll ()

Clears both read and write buffers for all message types. More...

template <typename Indexer>
WriteSinkwriteSink () noexcept

Creates a WriteSink handle for this buffer. More...

template <typename Indexer>
ReadSourcereadSource () 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_tindex ()

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()

template <typename Indexer>
helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::TypeIndexedDoubleBuffer ()
default

Public Destructor

~TypeIndexedDoubleBuffer()

template <typename Indexer>
helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::~TypeIndexedDoubleBuffer ()
default

Default destructor.

Definition at line 84 of file TypeIndexedDoubleBuffer.ixx.

Public Member Functions

clearAll()

template <typename Indexer>
void helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::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.

208 void clearAll() {
211 }

clearReadBuffers()

template <typename Indexer>
void helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::clearReadBuffers ()
inline

Clears all read buffers.

Typically not needed as `swapBuffers()` clears read buffers automatically.

Definition at line 181 of file TypeIndexedDoubleBuffer.ixx.

182 for (auto& buffer : buffers_) {
183 if (buffer) {
184 buffer->clearReadBuffer();
185 }
186 }
187 }

Referenced by helios::core::container::buffer::TypeIndexedDoubleBuffer< helios::core::data::TypeIndexer< GameLoopEventBusGroup > >::clearAll.

clearWriteBuffers()

template <typename Indexer>
void helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::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.

195 for (auto& buffer : buffers_) {
196 if (buffer) {
197 buffer->clearWriteBuffer();
198 }
199
200 }
201 }

Referenced by helios::core::container::buffer::TypeIndexedDoubleBuffer< helios::core::data::TypeIndexer< GameLoopEventBusGroup > >::clearAll.

getOrCreateBuffer()

template <typename T>
ReadWriteDoubleBuffer< T > & helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::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.

149 const size_t idx = index<T>();
150
151 if (buffers_.size() <= idx) {
152 buffers_.resize(idx + 1);
153 }
154
155 if (!buffers_[idx]) {
156 buffers_[idx] = std::make_unique<ReadWriteDoubleBuffer<T>>();
157 }
158
159 return *static_cast<ReadWriteDoubleBuffer<T>*>(buffers_[idx].get());
160 }

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()

template <typename T, typename... Args>
void helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::push (Args &&... args)
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.

100 void push(Args&&... args) {
101 getOrCreateBuffer<T>().push(std::forward<Args>(args)...);
102 }

read()

template <typename T>
std::span< const T > helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::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.

115 std::span<const T> read() const noexcept {
116
117 const size_t idx = index<T>();
118
119 if (buffers_.size() <= idx || !buffers_[idx]) {
120 return {};
121 }
122
123 return static_cast<ReadWriteDoubleBuffer<T>*>(buffers_[idx].get())->read();
124 }

readSource()

template <typename Indexer>
ReadSource helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::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.

309 [[nodiscard]] ReadSource readSource() noexcept {
310 return ReadSource(*this);
311 }

reserve()

template <typename T>
void helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::reserve (size_t size)
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.

136 void reserve(size_t size) {
137 getOrCreateBuffer<T>().reserve(size);
138 }

swapBuffers()

template <typename Indexer>
void helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::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.

168 void swapBuffers() {
169 for (auto& buffer : buffers_) {
170 if (buffer) {
171 buffer->swap();
172 }
173 }
174 }

writeSink()

template <typename Indexer>
WriteSink helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::writeSink ()
inline nodiscard noexcept

Creates a WriteSink handle for this buffer.

Returns

A WriteSink that can push messages to this buffer.

Definition at line 300 of file TypeIndexedDoubleBuffer.ixx.

300 [[nodiscard]] WriteSink writeSink() noexcept {
301 return WriteSink(*this);
302 }

Private Member Attributes

buffers_

template <typename Indexer>
std::vector<std::unique_ptr<DoubleBuffer> > helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::buffers_

Type-erased storage for message buffers, indexed by type.

Definition at line 65 of file TypeIndexedDoubleBuffer.ixx.

65 std::vector<std::unique_ptr<DoubleBuffer>> buffers_;

Private Static Functions

index()

template <typename T>
size_t helios::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::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.

75 static size_t index() {
76 return Indexer::template typeIndex<T>();
77 }

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.