|
Aegis2D
Modular C++26 2D game engine for networked games
|
Reads values written by BitWriter from untrusted bytes, never past their end. More...
#include <aegis2d/net/bits.hpp>
Public Member Functions | |
| BitReader (std::span< const std::byte > bytes) noexcept | |
Starts reading at the first bit of bytes. | |
| std::optional< std::uint64_t > | read_bits (unsigned width) noexcept |
| Reads an unsigned value. | |
| std::optional< std::int64_t > | read_signed (unsigned width) noexcept |
| Reads a signed value in two's complement. | |
| std::optional< bool > | read_bool () noexcept |
| Reads a boolean, one bit. | |
| bool | failed () const noexcept |
| Tells whether a read has failed. | |
| bool | at_end () const noexcept |
| Tells whether everything was read without error. | |
| std::size_t | bit_position () const noexcept |
| Number of bits read so far. | |
| std::size_t | bits_remaining () const noexcept |
| Number of bits not read yet, the padding of the last byte included. | |
Reads values written by BitWriter from untrusted bytes, never past their end.
When not enough bits remain or a width is invalid, the read returns an empty std::optional, moves nothing and the reader fails: every later read fails too, so a truncated or forged payload is never half-parsed with shifted fields.
The reader does not copy the bytes: they must outlive it.
|
explicitnoexcept |
Starts reading at the first bit of bytes.
| bytes | Received data, for instance the payload of a datagram. |
|
noexcept |
Tells whether everything was read without error.
Parsers check it last, to reject payloads with trailing data.
true if no read failed and what remains is only the 0 bits BitWriter puts at the end of the last byte.
|
noexcept |
Number of bits read so far.
|
noexcept |
Number of bits not read yet, the padding of the last byte included.
|
noexcept |
Tells whether a read has failed.
true once a read returned nothing; it stays true.
|
noexcept |
Reads an unsigned value.
| width | Number of bits, from 0 to BitWriter::max_width; 0 reads the value 0. |
width is above BitWriter::max_width or fewer than width bits remain.
|
noexcept |
Reads a boolean, one bit.
|
noexcept |
Reads a signed value in two's complement.
| width | Number of bits, sign included, from 1 to BitWriter::max_width. |
width is out of range or fewer than width bits remain.