Worldstone
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
WorldStone::BitStreamView Class Reference

Provides access to a variable bitsize values stream. More...

#include <BitStream.h>

Inheritance diagram for WorldStone::BitStreamView:
WorldStone::IOBase

Public Member Functions

 BitStreamView (const void *inputBuffer, size_t sizeInBits, size_t firstBitOffsetInBuffer=0)
 Creates a bitstream from raw memory.
 
BitStreamView createSubView (size_t newbufferSizeInBits) const
 
size_t tell () const
 Returns the current position in the stream in bits.
 
void setPosition (size_t newPosition)
 Set the current position, in bits.
 
size_t bitPositionInBuffer () const
 Returns the current position in the buffer (ignoring the first bit position) in bits.
 
void skip (size_t nbBits)
 Skips the next nbBits bits.
 
void alignToByte ()
 
size_t bufferSizeInBytes () const
 
size_t bufferSizeInBits () const
 Returns the total size of the current stream buffer in bits. More...
 
size_t sizeInBits () const
 Returns the total size of the current stream buffer in bytes.
 
bool readBool ()
 Reads a single bit from the stream.
 
uint32_t readBit ()
 Reads a single bit from the stream (uint32_t version)
 
template<typename RetType = uint32_t>
RetType readUnsigned (unsigned nbBits)
 Reads an unsigned value of variable bit size. More...
 
uint8_t readUnsigned8OrLess (const int nbBits)
 
uint32_t read0Bits ()
 Return 0u, used for member function tables as replacement for BitStream::readUnsigned<0>
 
template<unsigned NbBits, typename std::enable_if<(NbBits > 0>
int32_t readSigned ()
 Reads an signed value of variable bit size. More...
 
template<unsigned NbBits, typename std::enable_if<(NbBits==0)>::type * = nullptr>
int32_t readSigned ()
 Return 0 if reading less than 2 bits. More...
 
- Public Member Functions inherited from WorldStone::IOBase
 operator bool () const
 
bool operator! () const
 
bool good () const
 
bool eof () const
 
bool fail () const
 
bool bad () const
 

Additional Inherited Members

- Public Types inherited from WorldStone::IOBase
using Path = std::string
 
using iostate = int
 
- Protected Member Functions inherited from WorldStone::IOBase
void setstate (iostate state)
 
- Protected Attributes inherited from WorldStone::IOBase
iostate _state = goodbit
 
- Static Protected Attributes inherited from WorldStone::IOBase
static constexpr iostate goodbit = 0x0
 
static constexpr iostate eofbit = 0x1
 
static constexpr iostate failbit = 0x2
 
static constexpr iostate badbit = 0x4
 

Detailed Description

Provides access to a variable bitsize values stream.

The bitstream operates as a view on raw memory to get values of different sizes. This assumes that the data is ordered in a little endian fashion, and signed values are encoded using 2's complement.

Note
This is basicly the bitstream format used by the DCC format of Diablo 2
Does not inherit from IStream to avoid confusion since size is in bits and not bytes. Use MemoryStream instead.
Warning
As this class acts as a view, the buffer must outlive the usage of this class.
Todo:

Add some bounds checking and set io flags on error ?

Implement a MemoryStream class

Tests:
RO_bitstream

Definition at line 33 of file BitStream.h.

Member Function Documentation

size_t WorldStone::BitStreamView::bufferSizeInBits ( ) const
inline

Returns the total size of the current stream buffer in bits.

Note
This will always return a multiple of the size of a byte in bits.

Definition at line 91 of file BitStream.h.

template<unsigned NbBits, typename std::enable_if<(NbBits > 0>
int32_t WorldStone::BitStreamView::readSigned ( )
inline

Reads an signed value of variable bit size.

Values are using 2's complement.

Template Parameters
NbBitsThe number of bits to read from the stream
Note
The value is sign extended, hence for 1-bit values: 0b0 is 0 and 0b1 is -1.

Definition at line 161 of file BitStream.h.

template<unsigned NbBits, typename std::enable_if<(NbBits==0)>::type * = nullptr>
int32_t WorldStone::BitStreamView::readSigned ( )
inline

Return 0 if reading less than 2 bits.

Note
This specialization is only available because sometimes we want to read a number of bits choosen at runtime and use a method pointers table. But really, it'd be best to just not call it.

Definition at line 172 of file BitStream.h.

template<typename RetType = uint32_t>
RetType WorldStone::BitStreamView::readUnsigned ( unsigned  nbBits)
inline

Reads an unsigned value of variable bit size.

Template Parameters
RetTypeThe type of the value to return, must be at least NbBits bits big.
Parameters
nbBitsThe number of bits to read from the stream

Definition at line 112 of file BitStream.h.


The documentation for this class was generated from the following file: