MLIB
|
Read and write bit fields. More...
#include <bitstream.h>
Public Member Functions | |
bitstream (std::iostream &str, unsigned int pack) | |
bool | read () |
Read the next bit. | |
void | write (int val) |
Write a bit to stream. | |
void | flush () |
If there are any leftover bits, encode them in one character and write them out to stream. | |
int | mread (unsigned int sz, bool is_signed=false) |
Read a variable number of bits. | |
void | mwrite (int val, unsigned int sz) |
Write a variable number of bits. | |
Protected Member Functions | |
virtual void | encode (unsigned char bits, char &chr) |
Encode bit field in a byte. | |
virtual void | decode (unsigned char &bits, char chr) |
Decode bit field from a byte. | |
Protected Attributes | |
const unsigned int | packing |
std::iostream & | s |
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 | ||
) |
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.
bits | resulting bit field |
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 |
chr | encoded byte |
int mlib::bitstream::mread | ( | unsigned int | sz, |
bool | sign = false |
||
) |
Read a variable number of bits.
Invokes read function repeatedly to read all bits.
sz | Width of bit field |
sign | "true" if bit field should be interpreted as a signed value. |
void mlib::bitstream::mwrite | ( | int | val, |
unsigned int | sz | ||
) |
Write a variable number of bits.
Invokes write function repeatedly to write each bit.
val | Value to write (0 to 2sz-1 ) |
sz | Width of bit field |
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.
void mlib::bitstream::write | ( | int | 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 |