vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::BitWriter Class Reference

Packs values to the bit into a byte buffer. More...

#include <bit_stream.h>

Public Member Functions

 BitWriter (std::vector< uint8_t > &bytes, size_t capacity)
 Write into bytes, filling at most capacity of them.
void bits (uint32_t value, uint32_t width)
 Write the low width bits of value.
void u64 (uint64_t value)
 Two words, high first, because the field width is capped at 32.
void f32 (float value)
 A float as its exact bits, for the values quantisation would spoil.
void append (const uint8_t *bytes, size_t count)
 Write count bits from bytes, starting at its first bit.
size_t bitCount () const
 Bits written so far.
size_t byteCount () const
 Whole bytes needed to carry what has been written.
bool overflowed () const
 True when a write did not fit.
void finish ()
 Flush the last partial byte into the buffer.

Detailed Description

Packs values to the bit into a byte buffer.

A field costs what it needs, not its type's size: a flag is one bit.

Bounded by the packet's size: a write past it sets a sticky overflow flag instead of growing, so a caller asks once whether it all fit. Bits gather in a 64-bit word; the last partial byte stays there until finish().

Constructor & Destructor Documentation

◆ BitWriter()

Vkm::Engine::BitWriter::BitWriter ( std::vector< uint8_t > & bytes,
size_t capacity )
inline

Write into bytes, filling at most capacity of them.

Parameters
bytesBuffer to fill; emptied, then grown as bytes complete. Holds the whole stream only after finish().
capacityThe packet's ceiling, in bytes.

Member Function Documentation

◆ bits()

void Vkm::Engine::BitWriter::bits ( uint32_t value,
uint32_t width )
inline

Write the low width bits of value.

Parameters
valueValue to write; bits above width are ignored.
width1 to 32.

◆ append()

void Vkm::Engine::BitWriter::append ( const uint8_t * bytes,
size_t count )
inline

Write count bits from bytes, starting at its first bit.

For splicing something already encoded, which is not a whole number of bytes.

Parameters
bytesBuffer holding the encoded bits, first bit first.
countBits to take from it.

◆ overflowed()

bool Vkm::Engine::BitWriter::overflowed ( ) const
inline

True when a write did not fit.

Sticky, so a caller tests once at the end.

Returns
Whether any write since construction was refused.

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