MLIB
|
Read and write bit fields. More...
#include <bitstream.h>
Public Member Functions | |
bitstream (std::iostream &str, unsigned int pack) | |
Constructor. | |
bool | read () |
Read the next bit. | |
int | read (size_t sz, bool is_signed=false) |
Read a number of bits. | |
void | write (bool val) |
Write a bit to stream. | |
void | write (int val, size_t sz) |
Write a number of bits. | |
void | flush () |
If there are any leftover bits, encode them in one character and write them out to stream. | |
Protected Member Functions | |
virtual char | encode (unsigned char bits) |
Encode bit field in a byte. | |
virtual unsigned char | decode (char chr) |
Decode bit field from a byte. | |
Protected Attributes | |
const unsigned int | packing |
number of used bits per byte | |
std::iostream & | s |
underlining (byte) stream | |
Read and write bit fields.
This class allows reading and writing of bit fields from or to a byte stream.
It allows for different byte encodings using the protected encode/decode functions.
mlib::bitstream::bitstream | ( | std::iostream & | str, |
unsigned int | pack ) |
Constructor.
str | Underlining iostream |
pack | Number of bits per byte |
|
inlineprotectedvirtual |
Decode bit field from a byte.
The default implementation unpacks the bit field from the LSB of byte.
chr | encoded byte |
|
inlineprotectedvirtual |
Encode bit field in a byte.
The default implementation packs the bit field in the LSB of byte.
bits | bit field to encode |
bool mlib::bitstream::read | ( | ) |
Read the next bit.
Read a single bit from stream.
If there are no more bits available in current byte, tries to get a new byte and decodes it using decode() function.
int mlib::bitstream::read | ( | size_t | sz, |
bool | sign = false ) |
Read a number of bits.
Invokes bit read function repeatedly to read all bits.
sz | Width of bit field |
sign | true if first bit should be interpreted as a sign value. |
void mlib::bitstream::write | ( | bool | val | ) |
Write a bit to stream.
If it has accumulated enough bits calls encode() function to encode them and writes a new byte to underlining stream.
val | Bit value to write |
void mlib::bitstream::write | ( | int | val, |
size_t | sz ) |
Write a number of bits.
Invokes bit write function repeatedly to write each bit.
val | Value to write (0 to 2sz-1 ) |
sz | Width of bit field |