5#include <Common/assertion.h>
21 explicit ByteBuffer(std::size_t numExpectedWrittenBytes);
27 void read(T* out_data, std::size_t numElements);
32 void write(
const T& data);
37 void write(
const T* data, std::size_t numElements);
46 void fill(std::byte filledByte);
75 std::vector<std::byte> m_buffer;
76 std::size_t m_readHead;
77 std::size_t m_writeHead;
93 m_buffer.reserve(numExpectedWrittenBytes);
107 static_assert(std::is_trivially_copyable_v<T>,
108 "reading the type from ByteBuffer is not allowed");
111 PH_ASSERT_LE(m_readHead, m_buffer.size());
113 const std::size_t numRemainingBytes = m_buffer.size() - m_readHead;
114 const std::size_t numDataBytes = std::min(
sizeof(T) * numElements, numRemainingBytes);
117 m_buffer.data() + m_readHead,
119 reinterpret_cast<std::byte*
>(out_data));
121 m_readHead += numDataBytes;
123 PH_ASSERT_LE(m_readHead, m_buffer.size());
135 static_assert(std::is_trivially_copyable_v<T>,
136 "writing the type into ByteBuffer is not allowed");
139 PH_ASSERT_LE(m_writeHead, m_buffer.size());
141 const std::size_t numDataBytes =
sizeof(T) * numElements;
142 const std::size_t numAvailableBytes = m_buffer.size() - m_writeHead;
145 if(numAvailableBytes >= numDataBytes)
148 reinterpret_cast<const std::byte*
>(data),
150 m_buffer.data() + m_writeHead);
156 const std::size_t numShortedBytes = numDataBytes - numAvailableBytes;
160 reinterpret_cast<const std::byte*
>(data),
162 m_buffer.data() + m_writeHead);
167 reinterpret_cast<const std::byte*
>(data) + numAvailableBytes,
168 reinterpret_cast<const std::byte*
>(data) + numDataBytes);
171 m_writeHead += numDataBytes;
173 PH_ASSERT_LE(m_writeHead, m_buffer.size());
196 std::fill(m_buffer.begin(), m_buffer.end(), filledByte);
198 m_writeHead = m_buffer.size();
203 PH_ASSERT_LE(m_writeHead, m_buffer.size());
205 m_buffer.resize(m_writeHead);
207 if(m_readHead > m_writeHead)
215 m_buffer.shrink_to_fit();
220 PH_ASSERT_LE(indexOfByte, m_buffer.size());
222 m_readHead = indexOfByte;
227 PH_ASSERT_LE(indexOfByte, m_buffer.size());
229 m_writeHead = indexOfByte;
269 return m_buffer.size();
274 return m_buffer.empty();
An auto-resizable byte storage.
Definition ByteBuffer.h:18
void rewindWrite()
Definition ByteBuffer.h:181
TSpan< std::byte > getBytes()
Direct access to all the bytes in the buffer.
Definition ByteBuffer.h:257
std::size_t getReadPosition() const
Definition ByteBuffer.h:247
bool hasMoreToRead() const
Definition ByteBuffer.h:277
bool isEmpty() const
Definition ByteBuffer.h:272
void write(const T &data)
Writes data to the buffer. Grows the buffer if no space left.
Definition ByteBuffer.h:127
void truncate()
Definition ByteBuffer.h:201
ByteBuffer()
Definition ByteBuffer.h:82
T read()
Definition ByteBuffer.h:97
void setReadPosition(std::size_t indexOfByte)
Definition ByteBuffer.h:218
void setNumBytes(std::size_t numBytes)
Definition ByteBuffer.h:232
std::size_t getWritePosition() const
Definition ByteBuffer.h:252
void clear()
Clear all data and reset reader and writer positions.
Definition ByteBuffer.h:186
void rewindRead()
Definition ByteBuffer.h:176
void fit()
Definition ByteBuffer.h:213
std::size_t numBytes() const
Definition ByteBuffer.h:267
void fill(std::byte filledByte)
Definition ByteBuffer.h:194
void setWritePosition(std::size_t indexOfByte)
Definition ByteBuffer.h:225
The root for all renderer implementations.
Definition EEngineProject.h:6
std::span< const T, EXTENT > TSpanView
Same as TSpan, except that the objects are const-qualified. Note that for pointer types,...
Definition TSpan.h:19
std::span< T, EXTENT > TSpan
A contiguous sequence of objects of type T. Effectively the same as std::span.
Definition TSpan.h:12