Skip to content

Commit 15e32b5

Browse files
authored
Merge pull request #12 from KredeGC/dev
More generic traits
2 parents f7f7e8b + c4ad4ec commit 15e32b5

File tree

12 files changed

+329
-97
lines changed

12 files changed

+329
-97
lines changed

include/bitstream/bitstream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "traits/array_traits.h"
1616
#include "traits/bool_trait.h"
1717
#include "traits/enum_trait.h"
18+
#include "traits/float_trait.h"
1819
#include "traits/integral_traits.h"
1920
#include "traits/quantization_traits.h"
2021
#include "traits/string_traits.h"

include/bitstream/stream/bit_reader.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ namespace bitstream
200200
return true;
201201
}
202202

203+
/**
204+
* @brief Pads the buffer up with the given number of bytes
205+
* @param num_bytes The amount of bytes to pad
206+
* @return Returns false if the current size of the buffer is bigger than @p num_bytes or if the padded bits are not zeros.
207+
*/
208+
[[nodiscard]] bool pad(uint32_t num_bytes) noexcept
209+
{
210+
return pad_to_size(get_num_bytes_serialized() + num_bytes);
211+
}
212+
203213
/**
204214
* @brief Pads the buffer with up to 8 zeros, so that the next read is byte-aligned
205215
* @notes Return false if the padded bits are not zeros
@@ -235,7 +245,7 @@ namespace bitstream
235245
{
236246
const uint32_t* ptr = m_Buffer + m_WordIndex;
237247

238-
uint64_t ptr_value = static_cast<uint64_t>(utility::endian_swap_32(*ptr)) << (32U - m_ScratchBits);
248+
uint64_t ptr_value = static_cast<uint64_t>(utility::to_big_endian32(*ptr)) << (32U - m_ScratchBits);
239249
m_Scratch |= ptr_value;
240250
m_ScratchBits += 32U;
241251
m_WordIndex++;
@@ -285,7 +295,7 @@ namespace bitstream
285295

286296
// Casting a byte-array to an int is wrong on little-endian systems
287297
// We have to swap the bytes around
288-
word_buffer[i] = utility::endian_swap_32(value);
298+
word_buffer[i] = utility::to_big_endian32(value);
289299
}
290300
}
291301

include/bitstream/stream/bit_writer.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace bitstream
153153
{
154154
uint32_t* ptr = m_Buffer + m_WordIndex;
155155
uint32_t ptr_value = static_cast<uint32_t>(m_Scratch >> 32U);
156-
*ptr = utility::endian_swap_32(ptr_value);
156+
*ptr = utility::to_big_endian32(ptr_value);
157157

158158
m_Scratch = 0U;
159159
m_ScratchBits = 0U;
@@ -241,6 +241,16 @@ namespace bitstream
241241
return true;
242242
}
243243

244+
/**
245+
* @brief Pads the buffer up with the given number of bytes
246+
* @param num_bytes The amount of bytes to pad
247+
* @return Returns false if the current size of the buffer is bigger than @p num_bytes or if the padded bits are not zeros.
248+
*/
249+
[[nodiscard]] bool pad(uint32_t num_bytes) noexcept
250+
{
251+
return pad_to_size(get_num_bytes_serialized() + num_bytes);
252+
}
253+
244254
/**
245255
* @brief Pads the buffer with up to 8 zeros, so that the next write is byte-aligned
246256
* @return Success
@@ -281,7 +291,7 @@ namespace bitstream
281291
{
282292
uint32_t* ptr = m_Buffer + m_WordIndex;
283293
uint32_t ptr_value = static_cast<uint32_t>(m_Scratch >> 32U);
284-
*ptr = utility::endian_swap_32(ptr_value);
294+
*ptr = utility::to_big_endian32(ptr_value);
285295
m_Scratch <<= 32ULL;
286296
m_ScratchBits -= 32U;
287297
m_WordIndex++;
@@ -321,7 +331,7 @@ namespace bitstream
321331
{
322332
// Casting a byte-array to an int is wrong on little-endian systems
323333
// We have to swap the bytes around
324-
uint32_t value = utility::endian_swap_32(word_buffer[i]);
334+
uint32_t value = utility::to_big_endian32(word_buffer[i]);
325335
BS_ASSERT(serialize_bits(value, 32U));
326336
}
327337
}

include/bitstream/traits/bool_trait.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22
#include "../utility/assert.h"
3+
#include "../utility/meta.h"
4+
#include "../utility/parameter.h"
35

46
#include "../stream/serialize_traits.h"
5-
#include "../stream/bit_reader.h"
6-
#include "../stream/bit_writer.h"
77

88
namespace bitstream
99
{
@@ -13,14 +13,18 @@ namespace bitstream
1313
template<>
1414
struct serialize_traits<bool>
1515
{
16-
static bool serialize(bit_writer& writer, bool value) noexcept
16+
template<typename Stream>
17+
typename utility::is_writing_t<Stream>
18+
static serialize(Stream& writer, in<bool> value) noexcept
1719
{
1820
uint32_t unsigned_value = value;
1921

2022
return writer.serialize_bits(unsigned_value, 1U);
2123
}
2224

23-
static bool serialize(bit_reader& reader, bool& value) noexcept
25+
template<typename Stream>
26+
typename utility::is_reading_t<Stream>
27+
static serialize(Stream& reader, out<bool> value) noexcept
2428
{
2529
uint32_t unsigned_value;
2630

@@ -38,7 +42,9 @@ namespace bitstream
3842
template<size_t Size>
3943
struct serialize_traits<bool[Size]>
4044
{
41-
static bool serialize(bit_writer& writer, const bool* values) noexcept
45+
template<typename Stream>
46+
typename utility::is_writing_t<Stream>
47+
static serialize(Stream& writer, const bool* values) noexcept
4248
{
4349
uint32_t unsigned_value;
4450
for (size_t i = 0; i < Size; i++)
@@ -50,7 +56,9 @@ namespace bitstream
5056
return writer.serialize_bits(unsigned_value, 1U);
5157
}
5258

53-
static bool serialize(bit_reader& reader, bool* values) noexcept
59+
template<typename Stream>
60+
typename utility::is_reading_t<Stream>
61+
static serialize(Stream& reader, bool* values) noexcept
5462
{
5563
uint32_t unsigned_value;
5664
for (size_t i = 0; i < Size; i++)

include/bitstream/traits/enum_trait.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22
#include "../utility/assert.h"
3+
#include "../utility/meta.h"
4+
#include "../utility/parameter.h"
35

46
#include "../stream/serialize_traits.h"
5-
#include "../stream/bit_reader.h"
6-
#include "../stream/bit_writer.h"
77

88
#include "../traits/integral_traits.h"
99

@@ -23,19 +23,23 @@ namespace bitstream
2323
struct serialize_traits<T, typename std::enable_if_t<std::is_enum_v<T>>>
2424
{
2525
using value_type = std::underlying_type_t<T>;
26-
27-
static bool serialize(bit_writer& writer, T value, value_type min = 0, value_type max = (std::numeric_limits<value_type>::max)()) noexcept
26+
27+
template<typename Stream>
28+
typename utility::is_writing_t<Stream>
29+
static serialize(Stream& writer, T value, value_type min = 0, value_type max = (std::numeric_limits<value_type>::max)()) noexcept
2830
{
2931
value_type unsigned_value = static_cast<value_type>(value);
3032

31-
return writer.serialize<value_type>(unsigned_value, min, max);
33+
return writer.template serialize<value_type>(unsigned_value, min, max);
3234
}
3335

34-
static bool serialize(bit_reader& reader, T& value, value_type min = 0, value_type max = (std::numeric_limits<value_type>::max)()) noexcept
36+
template<typename Stream>
37+
typename utility::is_reading_t<Stream>
38+
static serialize(Stream& reader, T& value, value_type min = 0, value_type max = (std::numeric_limits<value_type>::max)()) noexcept
3539
{
3640
value_type unsigned_value;
3741

38-
BS_ASSERT(reader.serialize<value_type>(unsigned_value, min, max));
42+
BS_ASSERT(reader.template serialize<value_type>(unsigned_value, min, max));
3943

4044
value = static_cast<T>(unsigned_value);
4145

@@ -52,18 +56,22 @@ namespace bitstream
5256
using value_type = std::underlying_type_t<T>;
5357
using bound_type = bounded_int<value_type, Min, Max>;
5458

55-
static bool serialize(bit_writer& writer, T value) noexcept
59+
template<typename Stream>
60+
typename utility::is_writing_t<Stream>
61+
static serialize(Stream& writer, T value) noexcept
5662
{
5763
value_type unsigned_value = static_cast<value_type>(value);
5864

59-
return writer.serialize<bound_type>(unsigned_value);
65+
return writer.template serialize<bound_type>(unsigned_value);
6066
}
6167

62-
static bool serialize(bit_reader& reader, T& value) noexcept
68+
template<typename Stream>
69+
typename utility::is_reading_t<Stream>
70+
static serialize(Stream& reader, T& value) noexcept
6371
{
6472
value_type unsigned_value;
6573

66-
BS_ASSERT(reader.serialize<bound_type>(unsigned_value));
74+
BS_ASSERT(reader.template serialize<bound_type>(unsigned_value));
6775

6876
value = static_cast<T>(unsigned_value);
6977

include/bitstream/traits/float_trait.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22
#include "../utility/assert.h"
3+
#include "../utility/meta.h"
4+
#include "../utility/parameter.h"
35

46
#include "../stream/serialize_traits.h"
5-
#include "../stream/bit_reader.h"
6-
#include "../stream/bit_writer.h"
77

88
#include <cstdint>
99
#include <cstring>
@@ -22,7 +22,9 @@ namespace bitstream
2222
* @param value The float to serialize
2323
* @return Success
2424
*/
25-
static bool serialize(bit_writer& writer, float value) noexcept
25+
template<typename Stream>
26+
typename utility::is_writing_t<Stream>
27+
static serialize(Stream& writer, in<float> value) noexcept
2628
{
2729
uint32_t tmp;
2830
std::memcpy(&tmp, &value, sizeof(float));
@@ -38,7 +40,9 @@ namespace bitstream
3840
* @param value The float to serialize to
3941
* @return Success
4042
*/
41-
static bool serialize(bit_reader& reader, float& value) noexcept
43+
template<typename Stream>
44+
typename utility::is_reading_t<Stream>
45+
static serialize(Stream& reader, float& value) noexcept
4246
{
4347
uint32_t tmp;
4448

@@ -62,7 +66,9 @@ namespace bitstream
6266
* @param value The double to serialize
6367
* @return Success
6468
*/
65-
static bool serialize(bit_writer& writer, double value) noexcept
69+
template<typename Stream>
70+
typename utility::is_writing_t<Stream>
71+
static serialize(Stream& writer, in<double> value) noexcept
6672
{
6773
uint32_t tmp[2];
6874
std::memcpy(tmp, &value, sizeof(double));
@@ -79,7 +85,9 @@ namespace bitstream
7985
* @param value The double to serialize to
8086
* @return Success
8187
*/
82-
static bool serialize(bit_reader& reader, double& value) noexcept
88+
template<typename Stream>
89+
typename utility::is_reading_t<Stream>
90+
static serialize(Stream& reader, double& value) noexcept
8391
{
8492
uint32_t tmp[2];
8593

include/bitstream/traits/integral_traits.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22
#include "../utility/assert.h"
33
#include "../utility/bits.h"
4+
#include "../utility/meta.h"
5+
#include "../utility/parameter.h"
46

57
#include "../stream/serialize_traits.h"
6-
#include "../stream/bit_reader.h"
7-
#include "../stream/bit_writer.h"
88

99
#include <cstdint>
1010
#include <limits>
@@ -37,7 +37,9 @@ namespace bitstream
3737
* @param max The maximum bound that @p value can be. Inclusive
3838
* @return Success
3939
*/
40-
static bool serialize(bit_writer& writer, T value, T min = (std::numeric_limits<T>::min)(), T max = (std::numeric_limits<T>::max)()) noexcept
40+
template<typename Stream>
41+
typename utility::is_writing_t<Stream>
42+
static serialize(Stream& writer, in<T> value, T min = (std::numeric_limits<T>::min)(), T max = (std::numeric_limits<T>::max)()) noexcept
4143
{
4244
BS_ASSERT(min < max);
4345

@@ -77,7 +79,9 @@ namespace bitstream
7779
* @param max The maximum bound that @p value can be. Inclusive
7880
* @return Success
7981
*/
80-
static bool serialize(bit_reader& reader, T& value, T min = (std::numeric_limits<T>::min)(), T max = (std::numeric_limits<T>::max)()) noexcept
82+
template<typename Stream>
83+
typename utility::is_reading_t<Stream>
84+
static serialize(Stream& reader, T& value, T min = (std::numeric_limits<T>::min)(), T max = (std::numeric_limits<T>::max)()) noexcept
8185
{
8286
BS_ASSERT(min < max);
8387

@@ -138,7 +142,9 @@ namespace bitstream
138142
* @param value The value to serialize
139143
* @return Success
140144
*/
141-
static bool serialize(bit_writer& writer, T value) noexcept
145+
template<typename Stream>
146+
typename utility::is_writing_t<Stream>
147+
static serialize(Stream& writer, in<T> value) noexcept
142148
{
143149
static_assert(Min < Max);
144150

@@ -173,7 +179,9 @@ namespace bitstream
173179
* @param value The value to serialize
174180
* @return Success
175181
*/
176-
static bool serialize(bit_reader& reader, T& value) noexcept
182+
template<typename Stream>
183+
typename utility::is_reading_t<Stream>
184+
static serialize(Stream& reader, T& value) noexcept
177185
{
178186
static_assert(Min < Max);
179187

0 commit comments

Comments
 (0)