BitStream
stream_traits.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "byte_buffer.h"
4 
5 #include <cstddef>
6 #include <cstdint>
7 #include <limits>
8 #include <type_traits>
9 
10 namespace bitstream
11 {
12  struct fixed_policy
13  {
19  fixed_policy(void* buffer, uint32_t num_bits) noexcept :
20  m_Buffer(static_cast<uint32_t*>(buffer)),
22  m_TotalBits(num_bits) {}
23 
29  template<size_t Size>
30  fixed_policy(byte_buffer<Size>& buffer, uint32_t num_bits) noexcept :
31  m_Buffer(reinterpret_cast<uint32_t*>(buffer.Bytes)),
33  m_TotalBits(num_bits) {}
34 
39  template<size_t Size>
40  fixed_policy(byte_buffer<Size>& buffer) noexcept :
41  m_Buffer(reinterpret_cast<uint32_t*>(buffer.Bytes)),
43  m_TotalBits(Size * 8) {}
44 
45  uint32_t* get_buffer() const noexcept { return m_Buffer; }
46 
47  // TODO: Transition sizes to size_t
48  uint32_t get_num_bits_serialized() const noexcept { return m_NumBitsSerialized; }
49 
50  bool can_serialize_bits(uint32_t num_bits) const noexcept { return m_NumBitsSerialized + num_bits <= m_TotalBits; }
51 
52  uint32_t get_total_bits() const noexcept { return m_TotalBits; }
53 
54  bool extend(uint32_t num_bits)
55  {
56  if (!can_serialize_bits(num_bits))
57  return false;
58 
59  m_NumBitsSerialized += num_bits;
60  return true;
61  }
62 
63  uint32_t* m_Buffer;
64  // TODO: Transition sizes to size_t
66  uint32_t m_TotalBits;
67  };
68 
69  template<typename T>
71  {
72  growing_policy(T& container) noexcept :
73  m_Buffer(container),
75 
76  uint32_t* get_buffer() const noexcept { return m_Buffer.data(); }
77 
78  uint32_t get_num_bits_serialized() const noexcept { return m_NumBitsSerialized; }
79 
80  bool can_serialize_bits(uint32_t num_bits) const noexcept { return true; }
81 
82  uint32_t get_total_bits() const noexcept { return (std::numeric_limits<uint32_t>::max)(); }
83 
84  bool extend(uint32_t num_bits)
85  {
86  m_NumBitsSerialized += num_bits;
87  uint32_t num_bytes = (m_NumBitsSerialized - 1) / 8U + 1;
88  m_Buffer.resize(num_bytes);
89  return true;
90  }
91 
93 
95  };
96 }
Definition: bounded_range.h:28
A byte buffer aligned to 4 bytes. Can be used with bit_reader and bit_writer.
Definition: byte_buffer.h:15
Definition: stream_traits.h:13
uint32_t m_NumBitsSerialized
Definition: stream_traits.h:65
fixed_policy(byte_buffer< Size > &buffer) noexcept
Construct a stream pointing to the given buffer.
Definition: stream_traits.h:40
fixed_policy(void *buffer, uint32_t num_bits) noexcept
Construct a stream pointing to the given byte array with num_bytes size.
Definition: stream_traits.h:19
uint32_t * m_Buffer
Definition: stream_traits.h:63
uint32_t * get_buffer() const noexcept
Definition: stream_traits.h:45
uint32_t m_TotalBits
Definition: stream_traits.h:66
bool can_serialize_bits(uint32_t num_bits) const noexcept
Definition: stream_traits.h:50
uint32_t get_total_bits() const noexcept
Definition: stream_traits.h:52
bool extend(uint32_t num_bits)
Definition: stream_traits.h:54
fixed_policy(byte_buffer< Size > &buffer, uint32_t num_bits) noexcept
Construct a stream pointing to the given buffer.
Definition: stream_traits.h:30
uint32_t get_num_bits_serialized() const noexcept
Definition: stream_traits.h:48
Definition: stream_traits.h:71
bool extend(uint32_t num_bits)
Definition: stream_traits.h:84
uint32_t get_num_bits_serialized() const noexcept
Definition: stream_traits.h:78
bool can_serialize_bits(uint32_t num_bits) const noexcept
Definition: stream_traits.h:80
T & m_Buffer
Definition: stream_traits.h:92
uint32_t get_total_bits() const noexcept
Definition: stream_traits.h:82
uint32_t * get_buffer() const noexcept
Definition: stream_traits.h:76
uint32_t m_NumBitsSerialized
Definition: stream_traits.h:94
growing_policy(T &container) noexcept
Definition: stream_traits.h:72