C++ Utilities  4.6.1
Common C++ classes and routines used by my applications 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 "../io/catchiofailure.h"
6 #include "../global.h"
7 
8 #include <ios>
9 #include <iostream>
10 #include <type_traits>
11 
12 namespace IoUtilities {
13 
15 {
16 public:
17  BitReader(const char *buffer, std::size_t bufferSize);
18  BitReader(const char *buffer, const char *end);
19 
20  template<typename intType> intType readBits(byte bitCount);
21  byte readBit();
22  template<typename intType> intType readUnsignedExpGolombCodedBits();
23  template<typename intType> intType readSignedExpGolombCodedBits();
24  template<typename intType> intType showBits(byte bitCount);
25  void skipBits(std::size_t bitCount);
26  void align();
27  std::size_t bitsAvailable();
28  void reset(const char *buffer, std::size_t bufferSize);
29  void reset(const char *buffer, const char *end);
30 
31 private:
32  const byte *m_buffer;
33  const byte *m_end;
34  byte m_bitsAvail;
35 };
36 
43 inline BitReader::BitReader(const char *buffer, std::size_t bufferSize) :
44  BitReader(buffer, buffer + bufferSize)
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 
67 template<typename intType>
68 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 = (val << readAtOnce) | (((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce)));
80  }
81  return val;
82 }
83 
90 {
91  return readBits<byte>(1) == 1;
92 }
93 
102 template<typename intType>
104 {
105  byte count = 0;
106  while(!readBit()) {
107  ++count;
108  }
109  return count ? (((1 << count) | readBits<intType>(count)) - 1) : 0;
110 }
111 
120 template<typename intType>
122 {
123  auto value = readUnsignedExpGolombCodedBits<typename std::make_unsigned<intType>::type>();
124  return (value % 2) ? static_cast<intType>((value + 1) / 2) : (-static_cast<intType>(value / 2));
125 }
126 
130 template<typename intType>
131 intType BitReader::showBits(byte bitCount)
132 {
133  auto tmp = *this;
134  return tmp.readBits<intType>(bitCount);
135 }
136 
140 inline std::size_t BitReader::bitsAvailable()
141 {
142  return ((m_end - m_buffer) * 8) + m_bitsAvail;
143 }
144 
151 inline void BitReader::reset(const char *buffer, std::size_t bufferSize)
152 {
153  m_buffer = reinterpret_cast<const byte *>(buffer);
154  m_end = reinterpret_cast<const byte *>(buffer + bufferSize);
155  m_bitsAvail = 8;
156 }
157 
164 inline void BitReader::reset(const char *buffer, const char *end)
165 {
166  m_buffer = reinterpret_cast<const byte *>(buffer);
167  m_end = reinterpret_cast<const byte *>(end);
168  m_bitsAvail = 8;
169 }
170 
174 inline void BitReader::align()
175 {
176  skipBits(m_bitsAvail);
177 }
178 
179 } // namespace IoUtilities
180 
181 #endif // IOUTILITIES_BITREADER_H
CPP_UTILITIES_EXPORT void throwIoFailure(const char *what)
Throws a std::ios_base::failure with the specified message.
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
Definition: bitreader.h:121
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
Definition: bitreader.h:103
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:140
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10
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:43
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:151
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
void align()
Re-establishes alignment.
Definition: bitreader.h:174
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:131