BitStream
Loading...
Searching...
No Matches
bit_reader.h
Go to the documentation of this file.
1#pragma once
2#include "../utility/assert.h"
3#include "../utility/crc.h"
4#include "../utility/endian.h"
5#include "../utility/meta.h"
6
7#include "byte_buffer.h"
8#include "serialize_traits.h"
9#include "stream_traits.h"
10
11#include <cstdint>
12#include <cstring>
13#include <string>
14#include <type_traits>
15
16namespace bitstream
17{
22 template<typename Policy>
24 {
25 public:
26 static constexpr bool writing = false;
27 static constexpr bool reading = true;
28
33 template<typename... Ts,
34 typename = std::enable_if_t<std::is_constructible_v<Policy, Ts...>>>
35 bit_reader(Ts&&... args)
36 noexcept(std::is_nothrow_constructible_v<Policy, Ts...>) :
37 m_Policy(std::forward<Ts>(args) ...),
38 m_Scratch(0),
39 m_ScratchBits(0),
40 m_WordIndex(0) {}
41
42 bit_reader(const bit_reader&) = delete;
43
44 bit_reader(bit_reader&& other) noexcept :
45 m_Policy(std::move(other.m_Policy)),
46 m_Scratch(other.m_Scratch),
47 m_ScratchBits(other.m_ScratchBits),
48 m_WordIndex(other.m_WordIndex)
49 {
50 other.m_Scratch = 0;
51 other.m_ScratchBits = 0;
52 other.m_WordIndex = 0;
53 }
54
55 bit_reader& operator=(const bit_reader&) = delete;
56
58 {
59 m_Policy = std::move(rhs.m_Policy);
60 m_Scratch = rhs.m_Scratch;
61 m_ScratchBits = rhs.m_ScratchBits;
62 m_WordIndex = rhs.m_WordIndex;
63
64 rhs.m_Scratch = 0;
65 rhs.m_ScratchBits = 0;
66 rhs.m_WordIndex = 0;
67
68 return *this;
69 }
70
75 [[nodiscard]] const uint8_t* get_buffer() const noexcept { return reinterpret_cast<const uint8_t*>(m_Policy.get_buffer()); }
76
81 [[nodiscard]] uint32_t get_num_bits_serialized() const noexcept { return m_Policy.get_num_bits_serialized(); }
82
87 [[nodiscard]] uint32_t get_num_bytes_serialized() const noexcept { return get_num_bits_serialized() > 0U ? ((get_num_bits_serialized() - 1U) / 8U + 1U) : 0U; }
88
94 [[nodiscard]] bool can_serialize_bits(uint32_t num_bits) const noexcept { return m_Policy.can_serialize_bits(num_bits); }
95
101 [[nodiscard]] uint32_t get_remaining_bits() const noexcept { return get_total_bits() - get_num_bits_serialized(); }
102
107 [[nodiscard]] uint32_t get_total_bits() const noexcept { return m_Policy.get_total_bits(); }
108
114 [[nodiscard]] bool pad_to_size(uint32_t num_bytes) noexcept
115 {
116 uint32_t num_bits_read = get_num_bits_serialized();
117
118 BS_ASSERT(num_bytes * 8U >= num_bits_read);
119
120 BS_ASSERT(can_serialize_bits(num_bytes * 8U - num_bits_read));
121
122 uint32_t remainder = (num_bytes * 8U - num_bits_read) % 32U;
123 uint32_t zero;
124
125 // Test the last word more carefully, as it may have data
126 if (remainder != 0U)
127 {
128 bool status = serialize_bits(zero, remainder);
129 BS_ASSERT(status && zero == 0);
130 }
131
132 uint32_t offset = get_num_bits_serialized() / 32;
133 uint32_t max = num_bytes / 4;
134
135 // Test for zeros in padding
136 for (uint32_t i = offset; i < max; i++)
137 {
138 bool status = serialize_bits(zero, 32);
139 BS_ASSERT(status && zero == 0);
140 }
141
142 return true;
143 }
144
150 [[nodiscard]] bool pad(uint32_t num_bytes) noexcept
151 {
152 return pad_to_size(get_num_bytes_serialized() + num_bytes);
153 }
154
160 [[nodiscard]] bool align() noexcept
161 {
162 uint32_t remainder = get_num_bits_serialized() % 8U;
163 if (remainder != 0U)
164 {
165 uint32_t zero;
166 bool status = serialize_bits(zero, 8U - remainder);
167
168 BS_ASSERT(status && zero == 0U && get_num_bits_serialized() % 8U == 0U);
169 }
170
171 return true;
172 }
173
180 [[nodiscard]] bool serialize_bits(uint32_t& value, uint32_t num_bits) noexcept
181 {
182 BS_ASSERT(num_bits > 0U && num_bits <= 32U);
183
184 BS_ASSERT(m_Policy.extend(num_bits));
185
186 // This is actually slower
187 // Possibly due to unlikely branching
188 /*if (num_bits == 32U && m_ScratchBits == 0U)
189 {
190 const uint32_t* ptr = m_Policy.get_buffer() + m_WordIndex;
191
192 value = utility::to_big_endian32(*ptr);
193
194 m_WordIndex++;
195
196 return true;
197 }*/
198
199 if (m_ScratchBits < num_bits)
200 {
201 const uint32_t* ptr = m_Policy.get_buffer() + m_WordIndex;
202
203 uint64_t ptr_value = static_cast<uint64_t>(utility::to_big_endian32(*ptr)) << (32U - m_ScratchBits);
204 m_Scratch |= ptr_value;
205 m_ScratchBits += 32U;
206 m_WordIndex++;
207 }
208
209 uint32_t offset = 64U - num_bits;
210 value = static_cast<uint32_t>(m_Scratch >> offset);
211
212 m_Scratch <<= num_bits;
213 m_ScratchBits -= num_bits;
214
215 return true;
216 }
217
224 [[nodiscard]] bool serialize_bytes(uint8_t* bytes, uint32_t num_bits) noexcept
225 {
226 BS_ASSERT(num_bits > 0U);
227
228 BS_ASSERT(can_serialize_bits(num_bits));
229
230 // Read the byte array as words
231 uint32_t* word_buffer = reinterpret_cast<uint32_t*>(bytes);
232 uint32_t num_words = num_bits / 32U;
233
234 if (m_ScratchBits % 32U == 0U && num_words > 0U)
235 {
236 BS_ASSERT(m_Policy.extend(num_words * 32U));
237
238 // If the read buffer is word-aligned, just memcpy it
239 std::memcpy(word_buffer, m_Policy.get_buffer() + m_WordIndex, num_words * 4U);
240
241 m_WordIndex += num_words;
242 }
243 else
244 {
245 // If the buffer is not word-aligned, serialize a word at a time
246 for (uint32_t i = 0U; i < num_words; i++)
247 {
248 uint32_t value;
249 BS_ASSERT(serialize_bits(value, 32U));
250
251 // Casting a byte-array to an int is wrong on little-endian systems
252 // We have to swap the bytes around
253 word_buffer[i] = utility::to_big_endian32(value);
254 }
255 }
256
257 // Early exit if the word-count matches
258 if (num_bits % 32 == 0)
259 return true;
260
261 uint32_t remaining_bits = num_bits - num_words * 32U;
262
263 uint32_t num_bytes = (remaining_bits - 1U) / 8U + 1U;
264 for (uint32_t i = 0; i < num_bytes; i++)
265 {
266 uint32_t value;
267 BS_ASSERT(serialize_bits(value, (std::min)(remaining_bits - i * 8U, 8U)));
268
269 bytes[num_words * 4 + i] = static_cast<uint8_t>(value);
270 }
271
272 return true;
273 }
274
283 template<typename Trait, typename... Args, typename = utility::has_serialize_t<Trait, bit_reader, Args...>>
284 [[nodiscard]] bool serialize(Args&&... args) noexcept(utility::is_serialize_noexcept_v<Trait, bit_reader, Args...>)
285 {
286 return serialize_traits<Trait>::serialize(*this, std::forward<Args>(args)...);
287 }
288
299 template<typename... Args, typename Trait, typename = utility::has_deduce_serialize_t<Trait, bit_reader, Args...>>
300 [[nodiscard]] bool serialize(Trait&& arg, Args&&... args) noexcept(utility::is_deduce_serialize_noexcept_v<Trait, bit_reader, Args...>)
301 {
302 return serialize_traits<utility::deduce_trait_t<Trait, bit_reader, Args...>>::serialize(*this, std::forward<Trait>(arg), std::forward<Args>(args)...);
303 }
304
305 private:
306 Policy m_Policy;
307
308 uint64_t m_Scratch;
309 uint32_t m_ScratchBits;
310 uint32_t m_WordIndex;
311 };
312
314}
#define BS_ASSERT(...)
Definition assert.h:15
A stream for reading objects from a tightly packed buffer.
Definition bit_reader.h:24
static constexpr bool writing
Definition bit_reader.h:26
bool serialize_bytes(uint8_t *bytes, uint32_t num_bits) noexcept
Reads the first num_bits bits of the given byte array, 32 bits at a time.
Definition bit_reader.h:224
uint32_t get_num_bits_serialized() const noexcept
Returns the number of bits which have been read from the buffer.
Definition bit_reader.h:81
uint32_t get_remaining_bits() const noexcept
Returns the number of bits which have not been read yet.
Definition bit_reader.h:101
bool serialize(Trait &&arg, Args &&... args) noexcept(utility::is_deduce_serialize_noexcept_v< Trait, bit_reader, Args... >)
Reads from the buffer, by trying to deduce the trait.
Definition bit_reader.h:300
uint32_t get_num_bytes_serialized() const noexcept
Returns the number of bytes which have been read from the buffer.
Definition bit_reader.h:87
bool can_serialize_bits(uint32_t num_bits) const noexcept
Returns whether the num_bits be read from the buffer.
Definition bit_reader.h:94
bit_reader(Ts &&... args) noexcept(std::is_nothrow_constructible_v< Policy, Ts... >)
Construct a reader with the parameters passed to the underlying policy.
Definition bit_reader.h:35
const uint8_t * get_buffer() const noexcept
Returns the buffer that this reader is currently serializing from.
Definition bit_reader.h:75
bit_reader & operator=(bit_reader &&rhs) noexcept
Definition bit_reader.h:57
bit_reader & operator=(const bit_reader &)=delete
bool pad(uint32_t num_bytes) noexcept
Pads the buffer up with the given number of bytes.
Definition bit_reader.h:150
bool serialize(Args &&... args) noexcept(utility::is_serialize_noexcept_v< Trait, bit_reader, Args... >)
Reads from the buffer, using the given Trait.
Definition bit_reader.h:284
static constexpr bool reading
Definition bit_reader.h:27
bit_reader(const bit_reader &)=delete
bool align() noexcept
Pads the buffer with up to 8 zeros, so that the next read is byte-aligned @notes Return false if the ...
Definition bit_reader.h:160
bool serialize_bits(uint32_t &value, uint32_t num_bits) noexcept
Reads the first num_bits bits of value from the buffer.
Definition bit_reader.h:180
uint32_t get_total_bits() const noexcept
Returns the size of the buffer, in bits.
Definition bit_reader.h:107
bool pad_to_size(uint32_t num_bytes) noexcept
Pads the buffer up to the given number of bytes.
Definition bit_reader.h:114
bit_reader(bit_reader &&other) noexcept
Definition bit_reader.h:44
constexpr bool is_deduce_serialize_noexcept_v
Definition meta.h:104
has_serialize_t< deduce_trait_t< Trait, Stream, Args... >, Stream, Trait, Args... > has_deduce_serialize_t
Definition meta.h:101
constexpr bool is_serialize_noexcept_v
Definition meta.h:40
BS_CONSTEXPR uint32_t to_big_endian32(uint32_t value)
Definition endian.h:104
typename deduce_trait< void, Trait, Stream, Args... >::type deduce_trait_t
Definition meta.h:96
std::void_t< decltype(serialize_traits< T >::serialize(std::declval< Stream & >(), std::declval< Args >()...))> has_serialize_t
Definition meta.h:17
Definition bounded_range.h:28
A class for specializing trait serialization functions.
Definition serialize_traits.h:11