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