Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::net::BitWriter Class Reference

Writes values of any width from 0 to 64 bits into a growing byte buffer. More...

#include <aegis2d/net/bits.hpp>

Public Member Functions

void write_bits (std::uint64_t value, unsigned width)
 Appends an unsigned value.
 
void write_signed (std::int64_t value, unsigned width)
 Appends a signed value in two's complement.
 
void write_bool (bool value)
 Appends a boolean as one bit.
 
std::span< const std::byte > data () const noexcept
 Bytes written so far, the last one completed with 0 bits.
 
std::size_t size () const noexcept
 Number of bytes written so far, the last one counted even when incomplete.
 
std::size_t bit_size () const noexcept
 Number of bits written so far.
 
void clear () noexcept
 Empties the buffer and keeps its memory, to write the next payload.
 

Static Public Attributes

static constexpr unsigned max_width = 64
 Widest value a single write accepts, in bits.
 

Detailed Description

Writes values of any width from 0 to 64 bits into a growing byte buffer.

Bits are packed from the least significant bit of each byte, in the order of the writes: bit n of the stream is bit n % 8 of byte n / 8, whatever the machine. The unused bits of the last byte are 0.

Member Function Documentation

◆ bit_size()

std::size_t aegis2d::net::BitWriter::bit_size ( ) const
noexcept

Number of bits written so far.

Returns
The offset of the next write, in bits.

◆ clear()

void aegis2d::net::BitWriter::clear ( )
noexcept

Empties the buffer and keeps its memory, to write the next payload.

◆ data()

std::span< const std::byte > aegis2d::net::BitWriter::data ( ) const
noexcept

Bytes written so far, the last one completed with 0 bits.

Returns
A view of the buffer, valid until the next write or clear().

◆ size()

std::size_t aegis2d::net::BitWriter::size ( ) const
noexcept

Number of bytes written so far, the last one counted even when incomplete.

Returns
The size of data().

◆ write_bits()

void aegis2d::net::BitWriter::write_bits ( std::uint64_t  value,
unsigned  width 
)

Appends an unsigned value.

Parameters
valueValue to write.
widthNumber of bits, from 0 to max_width; 0 writes nothing.
Exceptions
std::invalid_argumentif width is above max_width or value does not fit.

◆ write_bool()

void aegis2d::net::BitWriter::write_bool ( bool  value)

Appends a boolean as one bit.

Parameters
valueValue to write.

◆ write_signed()

void aegis2d::net::BitWriter::write_signed ( std::int64_t  value,
unsigned  width 
)

Appends a signed value in two's complement.

Parameters
valueValue to write.
widthNumber of bits, sign included, from 1 to max_width.
Exceptions
std::invalid_argumentif width is out of range or value does not fit.

Member Data Documentation

◆ max_width

constexpr unsigned aegis2d::net::BitWriter::max_width = 64
staticconstexpr

Widest value a single write accepts, in bits.


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