Skip to main content

TypeIndexedReadWriteBuffer.ixx File

Type-indexed single-buffered message system for immediate-access patterns. More...

Included Headers

#include <memory> #include <span> #include <vector> #include <helios.core.container.buffer.ReadWriteBuffer> #include <helios.core.container.buffer.Buffer> #include <helios.core.data.TypeIndexer>

Namespaces Index

namespacehelios
namespacecore

Core utilities shared across the helios engine. More...

namespacecontainer

Generic container types shared across the engine. More...

namespacebuffer

Double-buffering infrastructure for thread-safe message passing. More...

Classes Index

classTypeIndexedReadWriteBuffer<Indexer>

Type-indexed container for immediate-access message buffers. More...

Description

Type-indexed single-buffered message system for immediate-access patterns.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file TypeIndexedReadWriteBuffer.ixx
3 * @brief Type-indexed single-buffered message system for immediate-access patterns.
4 */
5module;
6
7#include <memory>
8#include <span>
9#include <vector>
10
11export module helios.core.container.buffer.TypeIndexedReadWriteBuffer;
12
13import helios.core.data.TypeIndexer;
14
15import helios.core.container.buffer.Buffer;
16import helios.core.container.buffer.ReadWriteBuffer;
17
18
19export namespace helios::core::container::buffer {
20
21 /**
22 * @brief Type-indexed container for immediate-access message buffers.
23 *
24 * @details Unlike TypeIndexedDoubleBuffer, this container provides immediate
25 * visibility of pushed messages within the same frame. Each message type T
26 * gets its own dedicated ReadWriteBuffer, indexed via TypeIndexer for O(1) access.
27 *
28 * Use this when message producers and consumers operate in the same phase.
29 * For cross-phase communication with frame-boundary swapping, prefer
30 * TypeIndexedDoubleBuffer.
31 *
32 * @tparam Indexer The TypeIndexer used for mapping message types to buffer indices.
33 */
34 template<typename Indexer>
35
37
38 /**
39 * @brief Type-erased storage for message buffers, indexed by type.
40 */
41 std::vector<std::unique_ptr<Buffer>> buffers_;
42
43 /**
44 * @brief Returns the buffer index for message type T.
45 *
46 * @tparam T The message type.
47 *
48 * @return The index assigned to type T by the Indexer.
49 */
50 template<typename T>
51 static size_t index() {
52 return Indexer::template typeIndex<T>();
53 }
54
55 public:
56
57 /**
58 * @brief Default destructor.
59 */
61
62 /**
63 * @brief Default constructor.
64 */
66
67 /**
68 * @brief Pushes a message of type T to the buffer.
69 *
70 * @tparam T The message type.
71 * @tparam Args Constructor argument types for T.
72 *
73 * @param args Arguments forwarded to T's constructor.
74 */
75 template<typename T, typename... Args>
76 void push(Args&&... args) {
77 getOrCreateBuffer<T>().push(std::forward<Args>(args)...);
78 }
79
80 /**
81 * @brief Returns a read-only view of all messages of type T.
82 *
83 * @details Returns messages that were written to the buffer.
84 * If no messages of type T exist, returns an empty span.
85 *
86 * @tparam T The message type to read.
87 *
88 * @return A span over all messages of type T in the read buffer.
89 */
90 template<typename T>
91 std::span<const T> read() const noexcept {
92
93 const size_t idx = index<T>();
94
95 if (buffers_.size() <= idx || !buffers_[idx]) {
96 return {};
97 }
98
99 return static_cast<ReadWriteBuffer<T>*>(buffers_[idx].get())->read();
100 }
101
102 /**
103 * @brief Pre-allocates capacity for messages of type T.
104 *
105 * @details Call during initialization to avoid allocations during gameplay.
106 *
107 * @tparam T The message type.
108 *
109 * @param size The number of messages to reserve capacity for.
110 */
111 template<typename T>
112 void reserve(size_t size) {
113 getOrCreateBuffer<T>().reserve(size);
114 }
115
116 /**
117 * @brief Gets or creates the buffer for message type T.
118 *
119 * @tparam T The message type.
120 *
121 * @return Reference to the ReadWriteBuffer for type T.
122 */
123 template<typename T>
125 const size_t idx = index<T>();
126
127 if (buffers_.size() <= idx) {
128 buffers_.resize(idx + 1);
129 }
130
131 if (!buffers_[idx]) {
132 buffers_[idx] = std::make_unique<ReadWriteBuffer<T>>();
133 }
134
135 return *static_cast<ReadWriteBuffer<T>*>(buffers_[idx].get());
136 }
137
138
139 /**
140 * @brief Clears all buffers.
141 */
142 void clear() {
143 for (auto& buffer : buffers_) {
144 if (buffer) {
145 buffer->clear();
146 }
147 }
148 }
149
150 };
151
152
153}
154

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.