C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
bitreader.h
Go to the documentation of this file.
1 #ifndef IOUTILITIES_BITREADER_H
2 #define IOUTILITIES_BITREADER_H
3 
4 #include "../conversion/types.h"
5 #include "../global.h"
6 #include "../io/catchiofailure.h"
7 
8 #include <ios>
9 #include <iostream>
10 #include <type_traits>
11 
12 namespace IoUtilities {
13 
15 public:
16  BitReader(const char *buffer, std::size_t bufferSize);
17  BitReader(const char *buffer, const char *end);
18 
19  template <typename intType> intType readBits(byte bitCount);
20  byte readBit();
21  template <typename intType> intType readUnsignedExpGolombCodedBits();
22  template <typename intType> intType readSignedExpGolombCodedBits();
23  template <typename intType> intType showBits(byte bitCount);
24  void skipBits(std::size_t bitCount);
25  void align();
26  std::size_t bitsAvailable();
27  void reset(const char *buffer, std::size_t bufferSize);
28  void reset(const char *buffer, const char *end);
29 
30 private:
31  const byte *m_buffer;
32  const byte *m_end;
33  byte m_bitsAvail;
34 };
35 
42 inline BitReader::BitReader(const char *buffer, std::size_t bufferSize)
43  : BitReader(buffer, buffer + bufferSize)
44 {
45 }
46 
53 inline BitReader::BitReader(const char *buffer, const char *end)
54  : m_buffer(reinterpret_cast<const byte *>(buffer))
55  , m_end(reinterpret_cast<const byte *>(end))
56  , m_bitsAvail(8)
57 {
58 }
59 
68 template <typename intType> intType BitReader::readBits(byte bitCount)
69 {
70  intType val = 0;
71  for (byte readAtOnce; bitCount; bitCount -= readAtOnce) {
72  if (!m_bitsAvail) {
73  if (++m_buffer >= m_end) {
74  throwIoFailure("end of buffer exceeded");
75  }
76  m_bitsAvail = 8;
77  }
78  readAtOnce = std::min(bitCount, m_bitsAvail);
79  val = static_cast<intType>((val << readAtOnce) | (((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce))));
80  }
81  return val;
82 }
83 
89 inline byte BitReader::readBit()
90 {
91  return readBits<byte>(1) == 1;
92 }
93 
102 template <typename intType> intType BitReader::readUnsignedExpGolombCodedBits()
103 {
104  byte count = 0;
105  while (!readBit()) {
106  ++count;
107  }
108  return count ? (((1 << count) | readBits<intType>(count)) - 1) : 0;
109 }
110 
119 template <typename intType> intType BitReader::readSignedExpGolombCodedBits()
120 {
121  auto value = readUnsignedExpGolombCodedBits<typename std::make_unsigned<intType>::type>();
122  return (value % 2) ? static_cast<intType>((value + 1) / 2) : (-static_cast<intType>(value / 2));
123 }
124 
128 template <typename intType> intType BitReader::showBits(byte bitCount)
129 {
130  auto tmp = *this;
131  return tmp.readBits<intType>(bitCount);
132 }
133 
137 inline std::size_t BitReader::bitsAvailable()
138 {
139  return m_buffer != m_end ? static_cast<std::size_t>(((m_end - m_buffer - 1) * 8) + m_bitsAvail) : static_cast<std::size_t>(0);
140 }
141 
148 inline void BitReader::reset(const char *buffer, std::size_t bufferSize)
149 {
150  m_buffer = reinterpret_cast<const byte *>(buffer);
151  m_end = reinterpret_cast<const byte *>(buffer + bufferSize);
152  m_bitsAvail = 8;
153 }
154 
161 inline void BitReader::reset(const char *buffer, const char *end)
162 {
163  m_buffer = reinterpret_cast<const byte *>(buffer);
164  m_end = reinterpret_cast<const byte *>(end);
165  m_bitsAvail = 8;
166 }
167 
171 inline void BitReader::align()
172 {
173  skipBits(m_bitsAvail);
174 }
175 
176 } // namespace IoUtilities
177 
178 #endif // IOUTILITIES_BITREADER_H
CPP_UTILITIES_EXPORT void throwIoFailure(const char *what)
Throws an std::ios_base::failure with the specified message.
#define CPP_UTILITIES_EXPORT
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
Definition: bitreader.h:119
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
Definition: bitreader.h:102
The BitReader class provides bitwise reading of buffered data.
Definition: bitreader.h:14
std::size_t bitsAvailable()
Returns the number of bits which are still available to read.
Definition: bitreader.h:137
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition: math.h:17
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
Definition: bitreader.cpp:19
intType readBits(byte bitCount)
Reads the specified number of bits from the buffer advancing the current position by bitCount bits.
Definition: bitreader.h:68
BitReader(const char *buffer, std::size_t bufferSize)
Constructs a new BitReader.
Definition: bitreader.h:42
std::uint8_t byte
unsigned byte
Definition: types.h:14
void reset(const char *buffer, std::size_t bufferSize)
Resets the reader.
Definition: bitreader.h:148
void align()
Re-establishes alignment.
Definition: bitreader.h:171
byte readBit()
Reads the one bit from the buffer advancing the current position by one bit.
Definition: bitreader.h:89
intType showBits(byte bitCount)
Reads the specified number of bits from the buffer without advancing the current position.
Definition: bitreader.h:128