Skip to content

Commit eebddf0

Browse files
committed
Add byte_buffer
1 parent 5292326 commit eebddf0

File tree

6 files changed

+95
-29
lines changed

6 files changed

+95
-29
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
if: matrix.machine.os == 'windows-latest' && matrix.machine.toolset == 'msc'
4949
uses: microsoft/[email protected]
5050
- name: Install 32-bit gcc libs
51-
if: matrix.machine.os == 'ubuntu-latest'
51+
if: matrix.architecture == 'x86' && matrix.machine.os == 'ubuntu-latest'
5252
run: sudo apt-get install gcc-multilib g++-multilib
5353
- name: Run premake
5454
run: premake5 ${{ matrix.machine.action }} --toolset=${{ matrix.machine.toolset }} --dialect=${{ matrix.dialect }}

include/bitstream/bitstream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// Stream
99
#include "stream/bit_reader.h"
1010
#include "stream/bit_writer.h"
11+
#include "stream/byte_buffer.h"
1112
#include "stream/serialize_traits.h"
1213

1314
// Traits

include/bitstream/stream/bit_reader.h

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "../utility/crc.h"
44
#include "../utility/endian.h"
55

6-
#include "bit_writer.h"
6+
#include "byte_buffer.h"
77
#include "serialize_traits.h"
88

99
#include <cstdint>
@@ -22,6 +22,9 @@ namespace bitstream
2222
static constexpr bool writing = false;
2323
static constexpr bool reading = true;
2424

25+
/**
26+
* @brief Default construct a reader pointing to a null buffer
27+
*/
2528
bit_reader() noexcept :
2629
m_Buffer(nullptr),
2730
m_NumBitsRead(0),
@@ -30,29 +33,32 @@ namespace bitstream
3033
m_ScratchBits(0),
3134
m_WordIndex(0) {}
3235

36+
/**
37+
* @brief Construct a reader pointing to the given byte array with @p num_bytes size
38+
* @param bytes The byte array to read from
39+
* @param num_bytes The maximum number of bytes that we can read
40+
*/
3341
bit_reader(const void* bytes, uint32_t num_bytes) noexcept :
3442
m_Buffer(static_cast<const uint32_t*>(bytes)),
3543
m_NumBitsRead(0),
3644
m_TotalBits(num_bytes * 8),
3745
m_Scratch(0),
3846
m_ScratchBits(0),
3947
m_WordIndex(0) {}
40-
41-
explicit bit_reader(bit_writer&& other) noexcept :
42-
m_Buffer(other.m_Buffer),
43-
m_NumBitsRead(0),
44-
m_TotalBits(other.m_NumBitsWritten),
45-
m_Scratch(0),
46-
m_ScratchBits(0),
47-
m_WordIndex(0)
48-
{
49-
other.m_Buffer = nullptr;
50-
other.m_NumBitsWritten = 0;
51-
other.m_TotalBits = 0;
52-
other.m_Scratch = 0;
53-
other.m_ScratchBits = 0;
54-
other.m_WordIndex = 0;
55-
}
48+
49+
/**
50+
* @brief Construct a reader pointing to the given @p buffer
51+
* @param buffer The buffer to read from
52+
* @param num_bytes The maximum number of bytes that we can read
53+
*/
54+
template<size_t Size>
55+
explicit bit_reader(byte_buffer<Size>& buffer, uint32_t num_bytes) noexcept :
56+
m_Buffer(reinterpret_cast<uint32_t*>(buffer.Bytes)),
57+
m_NumBitsRead(0),
58+
m_TotalBits(num_bytes * 8),
59+
m_Scratch(0),
60+
m_ScratchBits(0),
61+
m_WordIndex(0) {}
5662

5763
bit_reader(const bit_reader&) = delete;
5864

include/bitstream/stream/bit_writer.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../utility/crc.h"
44
#include "../utility/endian.h"
55

6+
#include "byte_buffer.h"
67
#include "serialize_traits.h"
78

89
#include <cstdint>
@@ -26,6 +27,9 @@ namespace bitstream
2627
static constexpr bool writing = true;
2728
static constexpr bool reading = false;
2829

30+
/**
31+
* @brief Default construct a writer pointing to a null buffer
32+
*/
2933
bit_writer() noexcept :
3034
m_Buffer(nullptr),
3135
m_NumBitsWritten(0),
@@ -34,6 +38,11 @@ namespace bitstream
3438
m_ScratchBits(0),
3539
m_WordIndex(0) {}
3640

41+
/**
42+
* @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
45+
*/
3746
bit_writer(void* bytes, uint32_t num_bytes) noexcept :
3847
m_Buffer(static_cast<uint32_t*>(bytes)),
3948
m_NumBitsWritten(0),
@@ -48,6 +57,19 @@ namespace bitstream
4857
#endif
4958
}
5059

60+
/**
61+
* @brief Construct a writer pointing to the given @p buffer
62+
* @param buffer The buffer to write to
63+
*/
64+
template<size_t Size>
65+
explicit bit_writer(byte_buffer<Size>& buffer) noexcept :
66+
m_Buffer(reinterpret_cast<uint32_t*>(buffer.Bytes)),
67+
m_NumBitsWritten(0),
68+
m_TotalBits(Size * 8),
69+
m_Scratch(0),
70+
m_ScratchBits(0),
71+
m_WordIndex(0) {}
72+
5173
bit_writer(const bit_writer&) = delete;
5274

5375
bit_writer(bit_writer&& other) noexcept :
@@ -66,6 +88,25 @@ namespace bitstream
6688
other.m_WordIndex = 0;
6789
}
6890

91+
bit_writer& operator=(const bit_writer&) = delete;
92+
93+
bit_writer& operator=(bit_writer&& rhs) noexcept
94+
{
95+
m_Buffer = rhs.m_Buffer;
96+
m_NumBitsWritten = rhs.m_NumBitsWritten;
97+
m_TotalBits = rhs.m_TotalBits;
98+
m_Scratch = rhs.m_Scratch;
99+
m_ScratchBits = rhs.m_ScratchBits;
100+
m_WordIndex = rhs.m_WordIndex;
101+
102+
rhs.m_Buffer = nullptr;
103+
rhs.m_NumBitsWritten = 0;
104+
rhs.m_TotalBits = 0;
105+
rhs.m_Scratch = 0;
106+
rhs.m_ScratchBits = 0;
107+
rhs.m_WordIndex = 0;
108+
}
109+
69110
/**
70111
* @brief Returns the buffer that this writer is currently serializing into
71112
* @return The buffer
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace bitstream
6+
{
7+
/**
8+
* @brief A byte buffer aligned to 4 bytes
9+
* @note Can be used with bit_reader and bit_writer
10+
*/
11+
template<size_t Size>
12+
struct byte_buffer
13+
{
14+
static_assert(Size % 4 == 0, "Buffer size must be a multiple of 4");
15+
16+
alignas(uint32_t) uint8_t Bytes[Size];
17+
};
18+
}

src/test/stream_test.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace bitstream::test::stream
1414
uint32_t in_value2 = 99;
1515

1616
// Write some values with too many bits
17-
uint8_t buffer[4]{ 0 };
18-
bit_writer writer(buffer, 4);
17+
byte_buffer<4> buffer;
18+
bit_writer writer(buffer);
1919

2020
BS_TEST_ASSERT(writer.serialize_bits(in_value1, 11));
2121
BS_TEST_ASSERT(writer.serialize_bits(in_value2, 11));
@@ -27,7 +27,7 @@ namespace bitstream::test::stream
2727
// Read the values back and validate
2828
uint32_t out_value1;
2929
uint32_t out_value2;
30-
bit_reader reader(std::move(writer));
30+
bit_reader reader(buffer, num_bytes);
3131

3232
BS_TEST_ASSERT(reader.serialize_bits(out_value1, 11));
3333
BS_TEST_ASSERT(reader.serialize_bits(out_value2, 11));
@@ -48,8 +48,8 @@ namespace bitstream::test::stream
4848
constexpr uint32_t bits_required = utility::bits_to_represent(1111);
4949

5050
// Write some values with a few bits
51-
uint8_t buffer[8]{ 0 };
52-
bit_writer writer(buffer, 8);
51+
byte_buffer<8> buffer;
52+
bit_writer writer(buffer);
5353

5454
BS_TEST_ASSERT(writer.serialize_bits(in_value1, bits_required));
5555
BS_TEST_ASSERT(writer.serialize_bits(in_value2, bits_required));
@@ -62,7 +62,7 @@ namespace bitstream::test::stream
6262
uint32_t out_value1;
6363
uint32_t out_value2;
6464
uint32_t out_value3;
65-
bit_reader reader(std::move(writer));
65+
bit_reader reader(buffer, num_bytes);
6666

6767
BS_TEST_ASSERT(reader.serialize_bits(out_value1, bits_required));
6868
BS_TEST_ASSERT(reader.serialize_bits(out_value2, bits_required));
@@ -80,16 +80,16 @@ namespace bitstream::test::stream
8080
uint32_t value = 3;
8181

8282
// Write some initial values and finish with a checksum
83-
uint8_t buffer[16]{ 0 };
84-
bit_writer writer(buffer, 16);
83+
byte_buffer<16> buffer;
84+
bit_writer writer(buffer);
8585

8686
writer.prepend_checksum();
8787
BS_TEST_ASSERT(writer.serialize_bits(value, 2));
8888
uint16_t num_bytes = writer.serialize_checksum(protocol_version);
8989

9090
// Read the checksum and validate
9191
uint32_t out_value;
92-
bit_reader reader(std::move(writer));
92+
bit_reader reader(buffer, num_bytes);
9393

9494
BS_TEST_ASSERT(reader.serialize_checksum(protocol_version));
9595
BS_TEST_ASSERT(reader.serialize_bits(out_value, 2));
@@ -305,8 +305,8 @@ namespace bitstream::test::stream
305305
uint32_t nested_value = 511;
306306

307307
// Write some initial value with a bit offset
308-
uint8_t buffer[8]{ 0 };
309-
bit_writer writer(buffer, 8);
308+
byte_buffer<8> buffer;
309+
bit_writer writer(buffer);
310310

311311
BS_TEST_ASSERT(writer.serialize_bits(value, 2));
312312
BS_TEST_ASSERT(writer.serialize_bits(value, 3));

0 commit comments

Comments
 (0)