1 #ifndef IOUTILITIES_BITREADER_H 2 #define IOUTILITIES_BITREADER_H 4 #include "../conversion/types.h" 6 #include "../io/catchiofailure.h" 10 #include <type_traits> 16 BitReader(
const char *buffer, std::size_t bufferSize);
17 BitReader(
const char *buffer,
const char *end);
19 template <
typename intType> intType readBits(
byte bitCount);
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);
26 std::size_t bitsAvailable();
27 void reset(
const char *buffer, std::size_t bufferSize);
28 void reset(
const char *buffer,
const char *end);
54 : m_buffer(reinterpret_cast<const
byte *>(buffer))
55 , m_end(reinterpret_cast<const
byte *>(end))
71 for (
byte readAtOnce; bitCount; bitCount -= readAtOnce) {
73 if (++m_buffer >= m_end) {
78 readAtOnce =
std::min(bitCount, m_bitsAvail);
79 val = static_cast<intType>((val << readAtOnce) | (((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce))));
91 return readBits<byte>(1) == 1;
108 return count ? (((1 << count) | readBits<intType>(count)) - 1) : 0;
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));
131 return tmp.readBits<intType>(bitCount);
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);
150 m_buffer = reinterpret_cast<const byte *>(buffer);
151 m_end = reinterpret_cast<const byte *>(buffer + bufferSize);
163 m_buffer = reinterpret_cast<const byte *>(buffer);
164 m_end = reinterpret_cast<const byte *>(end);
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).
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.
constexpr T min(T first, T second)
Returns the smallest of the given items.
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.
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.