C++ Utilities
5.36.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Toggle main menu visibility
Loading...
Searching...
No Matches
io
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
11
namespace
CppUtilities
{
12
13
class
CPP_UTILITIES_EXPORT
BitReader
{
14
public
:
15
BitReader
(
const
char
*buffer, std::size_t bufferSize);
16
BitReader
(
const
char
*buffer,
const
char
*end);
17
18
template
<
typename
int
Type> intType
readBits
(std::uint8_t bitCount);
19
std::uint8_t
readBit
();
20
template
<
typename
int
Type> intType
readUnsignedExpGolombCodedBits
();
21
template
<
typename
int
Type> intType
readSignedExpGolombCodedBits
();
22
template
<
typename
int
Type> 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
29
private
:
30
const
std::uint8_t *m_buffer;
31
const
std::uint8_t *m_end;
32
std::uint8_t m_bitsAvail;
33
};
34
41
inline
BitReader::BitReader
(
const
char
*buffer, std::size_t bufferSize)
42
:
BitReader
(buffer, buffer + bufferSize)
43
{
44
}
45
52
inline
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
67
template
<
typename
int
Type> 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
89
inline
std::uint8_t
BitReader::readBit
()
90
{
91
return
readBits<std::uint8_t>
(1) == 1;
92
}
93
102
template
<
typename
int
Type> intType
BitReader::readUnsignedExpGolombCodedBits
()
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
119
template
<
typename
int
Type> 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
>
(-
static_cast<
intType
>
(value / 2));
123
}
124
128
template
<
typename
int
Type> intType
BitReader::showBits
(std::uint8_t 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
std::uint8_t *
>
(buffer);
151
m_end =
reinterpret_cast<
const
std::uint8_t *
>
(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
std::uint8_t *
>
(buffer);
164
m_end =
reinterpret_cast<
const
std::uint8_t *
>
(end);
165
m_bitsAvail = 8;
166
}
167
171
inline
void
BitReader::align
()
172
{
173
skipBits
(m_bitsAvail);
174
}
175
176
}
// namespace CppUtilities
177
178
#endif
// IOUTILITIES_BITREADER_H
CppUtilities::BitReader::reset
void reset(const char *buffer, std::size_t bufferSize)
Resets the reader.
Definition
bitreader.h:148
CppUtilities::BitReader::readBit
std::uint8_t readBit()
Reads the one bit from the buffer advancing the current position by one bit.
Definition
bitreader.h:89
CppUtilities::BitReader::skipBits
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
Definition
bitreader.cpp:43
CppUtilities::BitReader::align
void align()
Re-establishes alignment.
Definition
bitreader.h:171
CppUtilities::BitReader::bitsAvailable
std::size_t bitsAvailable()
Returns the number of bits which are still available to read.
Definition
bitreader.h:137
CppUtilities::BitReader::BitReader
BitReader(const char *buffer, std::size_t bufferSize)
Constructs a new BitReader.
Definition
bitreader.h:41
CppUtilities::BitReader::readSignedExpGolombCodedBits
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
Definition
bitreader.h:119
CppUtilities::BitReader::readUnsignedExpGolombCodedBits
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
Definition
bitreader.h:102
CppUtilities::BitReader::readBits
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
CppUtilities::BitReader::showBits
intType showBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer without advancing the current position.
Definition
bitreader.h:128
global.h
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition
global.h:14
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition
argumentparser.h:18
std
STL namespace.
Generated on
for C++ Utilities by
1.18.0