Skip to main content

TypeIndexedDoubleBuffer.ixx File

Double-buffered, type-indexed message system for decoupled inter-system communication. More...

Included Headers

#include <memory> #include <span> #include <vector> #include <cstddef> #include <helios.engine.core.container.buffer:ReadWriteDoubleBuffer> #include <helios.engine.core.container.buffer.concepts.IsTypeIndexerLike>

Namespaces Index

namespacehelios
namespaceengine
namespacecore
namespacecontainer
namespacebuffer

Classes Index

classTypeIndexedDoubleBuffer<Indexer>

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

classWriteSink

Lightweight handle for pushing messages to a TypeIndexedDoubleBuffer. More...

classReadSource

Lightweight handle for reading messages from a TypeIndexedDoubleBuffer. More...

Description

Double-buffered, type-indexed message system for decoupled inter-system communication.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <memory>
8#include <span>
9#include <vector>
10#include <cstddef>
11
12export module helios.engine.core.container.buffer:TypeIndexedDoubleBuffer;
13
14import helios.engine.core.container.buffer.concepts.IsTypeIndexerLike;
15import :DoubleBuffer;
16import :WriteBuffer;
17import :ReadBuffer;
18import :ReadWriteDoubleBuffer;
19
20using namespace helios::engine::core::container::buffer::concepts;
22
57 template<typename Indexer>
60
64 std::vector<std::unique_ptr<DoubleBuffer>> buffers_;
65
73 template<typename T>
74 static size_t index() {
75 return Indexer::template typeIndex<T>();
76 }
77
78 public:
79
84
89
98 template<typename T, typename... Args>
99 void push(Args&&... args) {
100 getOrCreateBuffer<T>().push(std::forward<Args>(args)...);
101 }
102
113 template<typename T>
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 }
124
134 template<typename T>
135 void reserve(size_t size) {
136 getOrCreateBuffer<T>().reserve(size);
137 }
138
146 template<typename T>
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 }
160
167 void swapBuffers() {
168 for (auto& buffer : buffers_) {
169 if (buffer) {
170 buffer->swap();
171 }
172 }
173 }
174
181 for (auto& buffer : buffers_) {
182 if (buffer) {
183 buffer->clearReadBuffer();
184 }
185 }
186 }
187
194 for (auto& buffer : buffers_) {
195 if (buffer) {
196 buffer->clearWriteBuffer();
197 }
198
199 }
200 }
201
207 void clearAll() {
210 }
211
224 class WriteSink {
225
229 TypeIndexedDoubleBuffer* db_ = nullptr;
230
231 public:
232
239
248 template<typename E, typename... Args>
249 void push(Args&&... args) noexcept {
250 db_->template push <E>(std::forward<Args>(args)...);
251 }
252 };
253
265 class ReadSource {
266
270 TypeIndexedDoubleBuffer* db_ = nullptr;
271
272 public:
273
280
288 template<typename E>
289 std::span<const E> read() const noexcept {
290 return db_->template read<E>();
291 }
292 };
293
300 return WriteSink(*this);
301 }
302
309 return ReadSource(*this);
310 }
311
312 };
313
314
315}
316

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.