1 #ifndef IOUTILITIES_BITREADER_H 2 #define IOUTILITIES_BITREADER_H 4 #include "../conversion/types.h" 5 #include "../io/catchiofailure.h" 10 #include <type_traits> 17 BitReader(
const char *buffer, std::size_t bufferSize);
18 BitReader(
const char *buffer,
const char *end);
20 template<
typename intType> intType readBits(
byte bitCount);
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);
27 std::size_t bitsAvailable();
28 void reset(
const char *buffer, std::size_t bufferSize);
29 void reset(
const char *buffer,
const char *end);
54 m_buffer(reinterpret_cast<const
byte *>(buffer)),
55 m_end(reinterpret_cast<const
byte *>(end)),
67 template<
typename intType>
71 for(
byte readAtOnce; bitCount; bitCount -= readAtOnce) {
73 if(++m_buffer >= m_end) {
78 readAtOnce = std::min(bitCount, m_bitsAvail);
79 val = (val << readAtOnce) | (((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce)));
91 return readBits<byte>(1) == 1;
102 template<
typename intType>
109 return count ? (((1 << count) | readBits<intType>(count)) - 1) : 0;
120 template<
typename intType>
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));
130 template<
typename intType>
134 return tmp.readBits<intType>(bitCount);
142 return ((m_end - m_buffer) * 8) + m_bitsAvail;
153 m_buffer =
reinterpret_cast<const byte *
>(buffer);
154 m_end =
reinterpret_cast<const byte *
>(buffer + bufferSize);
166 m_buffer =
reinterpret_cast<const byte *
>(buffer);
167 m_end =
reinterpret_cast<const byte *
>(end);
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).
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
The BitReader class provides bitwise reading of buffered data.
std::size_t bitsAvailable()
Returns the number of bits which are still available to read.
Contains utility classes helping to read and write streams.
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
intType readBits(byte bitCount)
Reads the specified number of bits from the buffer advancing the current position by bitCount bits...
BitReader(const char *buffer, std::size_t bufferSize)
Constructs a new BitReader.
std::uint8_t byte
unsigned byte
void reset(const char *buffer, std::size_t bufferSize)
Resets the reader.
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
void align()
Re-establishes alignment.
byte readBit()
Reads the one bit from the buffer advancing the current position by one bit.
intType showBits(byte bitCount)
Reads the specified number of bits from the buffer without advancing the current position.