Skip to main content

ReadWriteDoubleBuffer.ixx File

Type-safe double-buffered container for producer-consumer patterns. More...

Included Headers

#include <memory> #include <span> #include <vector> #include <helios.core.container.buffer.ReadBuffer> #include <helios.core.container.buffer.WriteBuffer> #include <helios.core.container.buffer.DoubleBuffer>

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

classReadWriteDoubleBuffer<T>

Type-safe double-buffered container for messages of type T. More...

Description

Type-safe double-buffered container for producer-consumer patterns.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file ReadWriteDoubleBuffer.ixx
3 * @brief Type-safe double-buffered container for producer-consumer patterns.
4 */
5module;
6
7#include <memory>
8#include <span>
9#include <vector>
10
11export module helios.core.container.buffer.ReadWriteDoubleBuffer;
12
13import helios.core.container.buffer.DoubleBuffer;
14import helios.core.container.buffer.WriteBuffer;
15import helios.core.container.buffer.ReadBuffer;
16
17
18export namespace helios::core::container::buffer {
19 /**
20 * @class ReadWriteDoubleBuffer
21 * @brief Type-safe double-buffered container for messages of type T.
22 *
23 * @details Implements a producer-consumer pattern where messages are written
24 * to one buffer while consumers read from another. This decouples message
25 * production from consumption and allows lock-free operation in single-threaded
26 * game loops.
27 *
28 * @tparam T The message type stored in this buffer. Must be movable.
29 */
30 template<typename T>
32
33 /**
34 * @brief Buffer containing messages available for reading.
35 */
36 ReadBuffer<T> readBuffer_;
37
38 /**
39 * @brief Buffer where new messages are pushed.
40 */
41 WriteBuffer<T> writeBuffer_;
42
43
44 public:
45
46 /**
47 * @brief Pre-allocates memory for both buffers.
48 *
49 * @param size The number of messages to reserve capacity for.
50 */
51 void reserve(size_t size) {
52 readBuffer_.reserve(size);
53 writeBuffer_.reserve(size);
54 }
55
56 /**
57 * @brief Constructs and pushes a message to the write buffer.
58 *
59 * @tparam Args Constructor argument types for T.
60 *
61 * @param args Arguments forwarded to T's constructor.
62 */
63 template<typename... Args>
64 void push(Args&&... args) {
65 writeBuffer_.push(std::forward<Args>(args)...);
66 }
67
68 /**
69 * @brief Returns a read-only view of messages in the read buffer.
70 *
71 * @return A span over all messages written in the previous frame.
72 */
73 std::span<const T> read() const noexcept {
74 return readBuffer_.read();
75 }
76
77 /**
78 * @brief Swaps read with write buffer.
79 *
80 * @details Swaps the read- with the write-buffer. The write buffer is empty after
81 * this operation, while the read buffer contains the contents of the write buffer.
82 */
83 void swap() override {
84 readBuffer_.clear().bufferData().swap(writeBuffer_.bufferData());
85 }
86
87 /**
88 * @brief Clears the read buffer.
89 */
90 void clearReadBuffer() override {
91 readBuffer_.clear();
92 }
93
94 /**
95 * @brief Clears the write buffer.
96 */
97 void clearWriteBuffer() override {
98 writeBuffer_.clear();
99 }
100 };
101}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.