Skip to content

Commit 2fa61ad

Browse files
committed
Add byte_buffer<Size>
Change tests to use byte_buffer Add move and copy operator to bit_reader
1 parent eebddf0 commit 2fa61ad

File tree

8 files changed

+76
-53
lines changed

8 files changed

+76
-53
lines changed

include/bitstream/stream/bit_reader.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace bitstream
3535

3636
/**
3737
* @brief Construct a reader pointing to the given byte array with @p num_bytes size
38-
* @param bytes The byte array to read from
38+
* @param bytes The byte array to read from. Should be 4-byte aligned if possible. The size of the array must be a multiple of 4
3939
* @param num_bytes The maximum number of bytes that we can read
4040
*/
4141
bit_reader(const void* bytes, uint32_t num_bytes) noexcept :
@@ -78,6 +78,27 @@ namespace bitstream
7878
other.m_WordIndex = 0;
7979
}
8080

81+
bit_reader& operator=(const bit_reader&) = delete;
82+
83+
bit_reader& operator=(bit_reader&& rhs) noexcept
84+
{
85+
m_Buffer = rhs.m_Buffer;
86+
m_NumBitsRead = rhs.m_NumBitsRead;
87+
m_TotalBits = rhs.m_TotalBits;
88+
m_Scratch = rhs.m_Scratch;
89+
m_ScratchBits = rhs.m_ScratchBits;
90+
m_WordIndex = rhs.m_WordIndex;
91+
92+
rhs.m_Buffer = nullptr;
93+
rhs.m_NumBitsRead = 0;
94+
rhs.m_TotalBits = 0;
95+
rhs.m_Scratch = 0;
96+
rhs.m_ScratchBits = 0;
97+
rhs.m_WordIndex = 0;
98+
99+
return *this;
100+
}
101+
81102
/**
82103
* @brief Returns the buffer that this reader is currently serializing from
83104
* @return The buffer

include/bitstream/stream/bit_writer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ namespace bitstream
4040

4141
/**
4242
* @brief Construct a writer pointing to the given byte array with @p num_bytes size
43-
* @param bytes The byte array to write to
44-
* @param num_bytes The number of bytes in the array
43+
* @param bytes The byte array to write to. Should be 4-byte aligned if possible
44+
* @param num_bytes The number of bytes in the array. Must be a multiple of 4
4545
*/
4646
bit_writer(void* bytes, uint32_t num_bytes) noexcept :
4747
m_Buffer(static_cast<uint32_t*>(bytes)),
@@ -105,6 +105,8 @@ namespace bitstream
105105
rhs.m_Scratch = 0;
106106
rhs.m_ScratchBits = 0;
107107
rhs.m_WordIndex = 0;
108+
109+
return *this;
108110
}
109111

110112
/**

src/test/serialize_array_traits.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace bitstream::test::traits
2727

2828
auto compare = [](uint32_t value) { return value != 21 && value != 42 && value != 99; };
2929

30-
uint8_t buffer[16]{ 0 };
31-
bit_writer writer(buffer, 16);
30+
byte_buffer<16> buffer;
31+
bit_writer writer(buffer);
3232

3333
BS_TEST_ASSERT(writer.serialize<trait>(values_in, 6, compare)); // Use bounded_int for writing
3434
uint32_t num_bytes = writer.flush();
@@ -37,7 +37,7 @@ namespace bitstream::test::traits
3737

3838

3939
uint32_t values_out[6];
40-
bit_reader reader(std::move(writer));
40+
bit_reader reader(buffer, num_bytes);
4141

4242
BS_TEST_ASSERT(reader.serialize<array_subset<uint32_t>>(values_out, 6, compare, 0U, 2048U)); // Use min, max arguments for reading
4343

src/test/serialize_bool_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace bitstream::test::traits
1414
bool value = true;
1515

1616
// Write a char array, but make sure the word count isn't whole
17-
uint8_t buffer[4];
18-
bit_writer writer(buffer, 4);
17+
byte_buffer<4> buffer;
18+
bit_writer writer(buffer);
1919

2020
BS_TEST_ASSERT(writer.serialize<bool>(value));
2121
uint32_t num_bytes = writer.flush();
@@ -24,7 +24,7 @@ namespace bitstream::test::traits
2424

2525
// Read the bool back and validate
2626
bool out_value;
27-
bit_reader reader(std::move(writer));
27+
bit_reader reader(buffer, num_bytes);
2828

2929
BS_TEST_ASSERT(reader.serialize<bool>(out_value));
3030

@@ -38,8 +38,8 @@ namespace bitstream::test::traits
3838
bool value = true;
3939

4040
// Write a char array, but make sure the word count isn't whole
41-
uint8_t buffer[4];
42-
bit_writer writer(buffer, 4);
41+
byte_buffer<4> buffer;
42+
bit_writer writer(buffer);
4343

4444
BS_TEST_ASSERT(writer.serialize_bits(padding, 5U));
4545
BS_TEST_ASSERT(writer.serialize<bool>(value));
@@ -50,7 +50,7 @@ namespace bitstream::test::traits
5050
// Read the bool back and validate
5151
uint32_t out_padding;
5252
bool out_value;
53-
bit_reader reader(std::move(writer));
53+
bit_reader reader(buffer, num_bytes);
5454

5555
BS_TEST_ASSERT(reader.serialize_bits(out_padding, 5U));
5656
BS_TEST_ASSERT(reader.serialize<bool>(out_value));

src/test/serialize_integral_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ namespace bitstream::test::traits
1212
void test_integral(T value)
1313
{
1414
// Test integral numbers
15-
uint8_t buffer[8]{ 0 };
16-
bit_writer writer(buffer, 8);
15+
byte_buffer<8> buffer;
16+
bit_writer writer(buffer);
1717

1818
BS_TEST_ASSERT(writer.serialize<T>(value, Min, Max));
1919
uint32_t num_bytes = writer.flush();
@@ -36,8 +36,8 @@ namespace bitstream::test::traits
3636
using trait = bounded_int<T, Min, Max>;
3737

3838
// Test integral numbers
39-
uint8_t buffer[8]{ 0 };
40-
bit_writer writer(buffer, 8);
39+
byte_buffer<8> buffer;
40+
bit_writer writer(buffer);
4141

4242
BS_TEST_ASSERT(writer.serialize<trait>(value));
4343
uint32_t num_bytes = writer.flush();

src/test/serialize_quantization_test.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ namespace bitstream::test::traits
1616
// Test float
1717
float value_in = 3.141592f;
1818

19-
uint8_t buffer[16]{ 0 };
20-
bit_writer writer(buffer, 16);
19+
byte_buffer<16> buffer;
20+
bit_writer writer(buffer);
2121

2222
BS_TEST_ASSERT(writer.serialize<float>(value_in));
2323
uint32_t num_bytes = writer.flush();
@@ -38,8 +38,8 @@ namespace bitstream::test::traits
3838
// Test half precision float
3939
float value_in = 3.141592f;
4040

41-
uint8_t buffer[16]{ 0 };
42-
bit_writer writer(buffer, 16);
41+
byte_buffer<16> buffer;
42+
bit_writer writer(buffer);
4343

4444
BS_TEST_ASSERT(writer.serialize<half_precision>(value_in));
4545
uint32_t num_bytes = writer.flush();
@@ -63,8 +63,8 @@ namespace bitstream::test::traits
6363
float value_in = 3.141592f;
6464
bounded_range range(0.0f, 5.0f, 0.0001f);
6565

66-
uint8_t buffer[16]{ 0 };
67-
bit_writer writer(buffer, 16);
66+
byte_buffer<16> buffer;
67+
bit_writer writer(buffer);
6868

6969
BS_TEST_ASSERT(writer.serialize<bounded_range>(range, value_in));
7070
uint32_t num_bytes = writer.flush();
@@ -88,8 +88,8 @@ namespace bitstream::test::traits
8888
// Test smallest three
8989
quaternion value_in(0.0f, std::sin(2.0f), std::cos(2.0f), 0.0f);
9090

91-
uint8_t buffer[16]{ 0 };
92-
bit_writer writer(buffer, 16);
91+
byte_buffer<16> buffer;
92+
bit_writer writer(buffer);
9393

9494
BS_TEST_ASSERT(writer.serialize<trait>(value_in));
9595
uint32_t num_bytes = writer.flush();

src/test/serialize_string_test.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ namespace bitstream::test::traits
1616
const char* value = "Hello, world!";
1717

1818
// Write a char array, but make sure the word count isn't whole
19-
uint8_t buffer[32];
20-
bit_writer writer(buffer, 32);
19+
byte_buffer<32> buffer;
20+
bit_writer writer(buffer);
2121

2222
BS_TEST_ASSERT(writer.serialize_bits(padding, 26));
2323
BS_TEST_ASSERT(writer.serialize<const char*>(value, 32U));
@@ -44,8 +44,8 @@ namespace bitstream::test::traits
4444
const char* value = "Hello, world!";
4545

4646
// Write a char array, but with an uneven bit offset
47-
uint8_t buffer[32];
48-
bit_writer writer(buffer, 32);
47+
byte_buffer<32> buffer;
48+
bit_writer writer(buffer);
4949

5050
BS_TEST_ASSERT(writer.serialize_bits(padding, 6));
5151
BS_TEST_ASSERT(writer.serialize<const char*>(value, 32U));
@@ -74,8 +74,8 @@ namespace bitstream::test::traits
7474
std::string value = "Hello, world!";
7575

7676
// Write a string, but make sure the word count isn't whole
77-
uint8_t buffer[32];
78-
bit_writer writer(buffer, 32);
77+
byte_buffer<32> buffer;
78+
bit_writer writer(buffer);
7979

8080
BS_TEST_ASSERT(writer.serialize_bits(padding, 26));
8181
BS_TEST_ASSERT(writer.serialize<std::string>(value, 32U));
@@ -101,8 +101,8 @@ namespace bitstream::test::traits
101101
std::wstring value = L"Hello, world!";
102102

103103
// Write a widechar string, but make sure the word count isn't whole
104-
uint8_t buffer[64];
105-
bit_writer writer(buffer, 64); // wchar_t is apparently platform dependent and can be 16 or 32 bits
104+
byte_buffer<64> buffer; // wchar_t is apparently platform dependent and can be 16 or 32 bits
105+
bit_writer writer(buffer);
106106

107107
BS_TEST_ASSERT(writer.serialize<std::wstring>(value, 32U));
108108
uint32_t num_bytes = writer.flush();

src/test/stream_test.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ namespace bitstream::test::stream
104104
uint32_t in_value2 = 6;
105105

106106
// Write some initial value, pad it, and then write another value
107-
uint8_t buffer[32];
108-
bit_writer writer(buffer, 32);
107+
byte_buffer<32> buffer;
108+
bit_writer writer(buffer);
109109
BS_TEST_ASSERT(writer.serialize_bits(in_value1, 3));
110110
BS_TEST_ASSERT(writer.pad_to_size(31));
111111
BS_TEST_ASSERT(writer.serialize_bits(in_value2, 5));
@@ -116,7 +116,7 @@ namespace bitstream::test::stream
116116
// Read the values and validate padding
117117
uint32_t out_value1;
118118
uint32_t out_value2;
119-
bit_reader reader(std::move(writer));
119+
bit_reader reader(buffer, num_bytes);
120120

121121
BS_TEST_ASSERT(reader.serialize_bits(out_value1, 3));
122122
BS_TEST_ASSERT(reader.pad_to_size(31));
@@ -132,8 +132,8 @@ namespace bitstream::test::stream
132132
uint32_t value = 3;
133133

134134
// Write a bit offset and the align to byte
135-
uint8_t buffer[8]{ 0 };
136-
bit_writer writer(buffer, 8);
135+
byte_buffer<8> buffer;
136+
bit_writer writer(buffer);
137137

138138
BS_TEST_ASSERT(writer.serialize_bits(value, 2));
139139
BS_TEST_ASSERT(writer.align());
@@ -143,7 +143,7 @@ namespace bitstream::test::stream
143143

144144
// Read back the the bit offset and validate the alignment
145145
uint32_t out_value;
146-
bit_reader reader(std::move(writer));
146+
bit_reader reader(buffer, num_bytes);
147147

148148
BS_TEST_ASSERT(reader.serialize_bits(out_value, 2));
149149
BS_TEST_ASSERT(reader.get_remaining_bits() > 0);
@@ -161,8 +161,8 @@ namespace bitstream::test::stream
161161
uint32_t num_bits = 2 * 8 - 2;
162162

163163
// Write some values with a few bits
164-
uint8_t buffer[8]{ 0 };
165-
bit_writer writer(buffer, 8);
164+
byte_buffer<8> buffer;
165+
bit_writer writer(buffer);
166166

167167
BS_TEST_ASSERT(writer.serialize_bits(in_padding, 5));
168168
BS_TEST_ASSERT(writer.serialize_bytes(in_value, num_bits));
@@ -173,7 +173,7 @@ namespace bitstream::test::stream
173173
// Read the values back and validate
174174
uint8_t out_value[2];
175175
uint32_t out_padding;
176-
bit_reader reader(std::move(writer));
176+
bit_reader reader(buffer, num_bytes);
177177

178178
BS_TEST_ASSERT(reader.serialize_bits(out_padding, 5));
179179
BS_TEST_ASSERT(reader.serialize_bytes(out_value, num_bits));
@@ -192,8 +192,8 @@ namespace bitstream::test::stream
192192
uint32_t num_bits = 5 * 8 - 1;
193193

194194
// Write some values with a few bits
195-
uint8_t buffer[8]{ 0 };
196-
bit_writer writer(buffer, 8);
195+
byte_buffer<8> buffer;
196+
bit_writer writer(buffer);
197197

198198
BS_TEST_ASSERT(writer.serialize_bits(in_padding, 5));
199199
BS_TEST_ASSERT(writer.serialize_bytes(in_value, num_bits));
@@ -204,7 +204,7 @@ namespace bitstream::test::stream
204204
// Read the values back and validate
205205
uint8_t out_value[5];
206206
uint32_t out_padding;
207-
bit_reader reader(std::move(writer));
207+
bit_reader reader(buffer, num_bytes);
208208

209209
BS_TEST_ASSERT(reader.serialize_bits(out_padding, 5));
210210
BS_TEST_ASSERT(reader.serialize_bytes(out_value, num_bits));
@@ -223,8 +223,8 @@ namespace bitstream::test::stream
223223
uint32_t num_bits = 10 * 8 - 3;
224224

225225
// Write some values with a few bits
226-
uint8_t buffer[16]{ 0 };
227-
bit_writer writer(buffer, 16);
226+
byte_buffer<16> buffer;
227+
bit_writer writer(buffer);
228228

229229
BS_TEST_ASSERT(writer.serialize_bits(in_padding, 5));
230230
BS_TEST_ASSERT(writer.serialize_bytes(in_value, num_bits));
@@ -235,7 +235,7 @@ namespace bitstream::test::stream
235235
// Read the values back and validate
236236
uint8_t out_value[10];
237237
uint32_t out_padding;
238-
bit_reader reader(std::move(writer));
238+
bit_reader reader(buffer, num_bytes);
239239

240240
BS_TEST_ASSERT(reader.serialize_bits(out_padding, 5));
241241
BS_TEST_ASSERT(reader.serialize_bytes(out_value, num_bits));
@@ -253,16 +253,16 @@ namespace bitstream::test::stream
253253
uint32_t nested_value = 511;
254254

255255
// Write some initial value with a bit offset
256-
uint8_t buffer[8]{ 0 };
257-
bit_writer writer(buffer, 8);
256+
byte_buffer<8> buffer;
257+
bit_writer writer(buffer);
258258

259259
BS_TEST_ASSERT(writer.serialize_bits(value, 2));
260260
BS_TEST_ASSERT(writer.serialize_bits(value, 3));
261261

262262
{
263263
// Write nested values
264-
uint8_t nested_buffer[8]{ 0 };
265-
bit_writer nested_writer(nested_buffer, 8);
264+
byte_buffer<8> nested_buffer;
265+
bit_writer nested_writer(nested_buffer);
266266

267267
BS_TEST_ASSERT(nested_writer.serialize_bits(nested_value, 11));
268268
BS_TEST_ASSERT(nested_writer.serialize_bits(nested_value, 13));
@@ -313,8 +313,8 @@ namespace bitstream::test::stream
313313

314314
{
315315
// Write nested values
316-
uint8_t nested_buffer[8]{ 0 };
317-
bit_writer nested_writer(nested_buffer, 8);
316+
byte_buffer<8> nested_buffer;
317+
bit_writer nested_writer(nested_buffer);
318318

319319
BS_TEST_ASSERT(nested_writer.serialize_bits(nested_value, 11));
320320
BS_TEST_ASSERT(nested_writer.serialize_bits(nested_value, 13));

0 commit comments

Comments
 (0)