Skip to main content

TypeIndexedDoubleBuffer Class Template

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

Declaration

template <typename Indexer> class helios::engine::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:

 // Define a message
 struct CollisionMessage {
  EntityId a;
  EntityId b;
  vec3f contact;
 };
 
 // In collision system
 
 // 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 59 of file TypeIndexedDoubleBuffer.ixx.

Public Constructors

TypeIndexedDoubleBuffer()

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

Default constructor.

Definition at line 88 of file TypeIndexedDoubleBuffer.ixx.

Public Destructor

~TypeIndexedDoubleBuffer()

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

Default destructor.

Definition at line 83 of file TypeIndexedDoubleBuffer.ixx.

Public Member Functions

clearAll()

template <typename Indexer>
void helios::engine::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 207 of file TypeIndexedDoubleBuffer.ixx.

207 void clearAll() {
210 }

References helios::engine::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::clearReadBuffers and helios::engine::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::clearWriteBuffers.

Referenced by helios::engine::runtime::gameloop::GameLoop::phaseCommit.

clearReadBuffers()

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

Clears all read buffers.

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

Definition at line 180 of file TypeIndexedDoubleBuffer.ixx.

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

Referenced by helios::engine::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::clearAll.

clearWriteBuffers()

template <typename Indexer>
void helios::engine::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 193 of file TypeIndexedDoubleBuffer.ixx.

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

Referenced by helios::engine::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::clearAll.

getOrCreateBuffer()

template <typename T>
ReadWriteDoubleBuffer< T > & helios::engine::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 147 of file TypeIndexedDoubleBuffer.ixx.

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

Reference helios::engine::core::registerComponents.

push()

template <typename T, typename... Args>
void helios::engine::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 99 of file TypeIndexedDoubleBuffer.ixx.

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

Reference helios::engine::core::registerComponents.

read()

template <typename T>
std::span< const T > helios::engine::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 114 of file TypeIndexedDoubleBuffer.ixx.

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

References helios::engine::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::read and helios::engine::core::registerComponents.

Referenced by helios::engine::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::read.

readSource()

template <typename Indexer>
ReadSource helios::engine::core::container::buffer::TypeIndexedDoubleBuffer< Indexer >::readSource ()
inline noexcept

Creates a ReadSource handle for this buffer.

Returns

A ReadSource that can read messages from this buffer.

Definition at line 308 of file TypeIndexedDoubleBuffer.ixx.

309 return ReadSource(*this);
310 }

reserve()

template <typename T>
void helios::engine::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 135 of file TypeIndexedDoubleBuffer.ixx.

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

Reference helios::engine::core::registerComponents.

swapBuffers()

template <typename Indexer>
void helios::engine::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 167 of file TypeIndexedDoubleBuffer.ixx.

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

Referenced by helios::engine::runtime::gameloop::GameLoop::onPassCommit, helios::engine::runtime::gameloop::GameLoop::phaseCommit and helios::engine::runtime::gameloop::GameLoop::update.

writeSink()

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

Creates a WriteSink handle for this buffer.

Returns

A WriteSink that can push messages to this buffer.

Definition at line 299 of file TypeIndexedDoubleBuffer.ixx.

300 return WriteSink(*this);
301 }

Private Member Attributes

buffers_

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

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

Definition at line 64 of file TypeIndexedDoubleBuffer.ixx.

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

Private Static Functions

index()

template <typename T>
size_t helios::engine::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 74 of file TypeIndexedDoubleBuffer.ixx.

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

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.