BitStream
bitstream::bit_measure Class Reference

A stream for writing objects tightly into a buffer. More...

#include <bit_measure.h>

Public Member Functions

 bit_measure () noexcept
 Default construct a writer pointing to a null buffer. More...
 
 bit_measure (uint32_t num_bytes) noexcept
 Construct a writer pointing to the given byte array with num_bytes size. More...
 
 bit_measure (const bit_measure &)=delete
 
 bit_measure (bit_measure &&other) noexcept
 
bit_measureoperator= (const bit_measure &)=delete
 
bit_measureoperator= (bit_measure &&rhs) noexcept
 
uint8_t * get_buffer () const noexcept
 Returns the buffer that this writer is currently serializing into. More...
 
uint32_t get_num_bits_serialized () const noexcept
 Returns the number of bits which have been written to the buffer. More...
 
uint32_t get_num_bytes_serialized () const noexcept
 Returns the number of bytes which have been written to the buffer. More...
 
bool can_serialize_bits (uint32_t num_bits) const noexcept
 Returns whether the num_bits can fit in the buffer. More...
 
uint32_t get_remaining_bits () const noexcept
 Returns the number of bits which have not been written yet. More...
 
uint32_t get_total_bits () const noexcept
 Returns the size of the buffer, in bits. More...
 
bool prepend_checksum () noexcept
 Instructs the writer that you intend to use serialize_checksum() later on, and to reserve the first 32 bits. More...
 
uint32_t serialize_checksum (uint32_t protocol_version) noexcept
 Writes a checksum of the protocol_version and the rest of the buffer as the first 32 bits. More...
 
bool pad_to_size (uint32_t num_bytes) noexcept
 Pads the buffer up to the given number of bytes with zeros. More...
 
bool pad (uint32_t num_bytes) noexcept
 Pads the buffer up with the given number of bytes. More...
 
bool align () noexcept
 Pads the buffer with up to 8 zeros, so that the next write is byte-aligned. More...
 
bool serialize_bits (uint32_t value, uint32_t num_bits) noexcept
 Writes the first num_bits bits of value into the buffer. More...
 
bool serialize_bytes (const uint8_t *bytes, uint32_t num_bits) noexcept
 Writes the first num_bits bits of the given byte array, 32 bits at a time. More...
 
template<typename Trait , typename... Args, typename = utility::has_serialize_t<Trait, bit_measure, Args...>>
bool serialize (Args &&... args) noexcept(utility::is_serialize_noexcept_v< Trait, bit_measure, Args... >)
 Writes to the buffer, using the given Trait. More...
 
template<typename... Args, typename Trait , typename = utility::has_deduce_serialize_t<Trait, bit_measure, Args...>>
bool serialize (Trait &&arg, Args &&... args) noexcept(utility::is_deduce_serialize_noexcept_v< Trait, bit_measure, Args... >)
 Writes to the buffer, by trying to deduce the trait. More...
 

Static Public Attributes

static constexpr bool writing = true
 
static constexpr bool reading = false
 

Detailed Description

A stream for writing objects tightly into a buffer.

Note
Does not take ownership of the buffer

Constructor & Destructor Documentation

◆ bit_measure() [1/4]

bitstream::bit_measure::bit_measure ( )
inlinenoexcept

Default construct a writer pointing to a null buffer.

◆ bit_measure() [2/4]

bitstream::bit_measure::bit_measure ( uint32_t  num_bytes)
inlinenoexcept

Construct a writer pointing to the given byte array with num_bytes size.

Parameters
num_bytesThe number of bytes in the array

◆ bit_measure() [3/4]

bitstream::bit_measure::bit_measure ( const bit_measure )
delete

◆ bit_measure() [4/4]

bitstream::bit_measure::bit_measure ( bit_measure &&  other)
inlinenoexcept

Member Function Documentation

◆ align()

bool bitstream::bit_measure::align ( )
inlinenoexcept

Pads the buffer with up to 8 zeros, so that the next write is byte-aligned.

Returns
Success

◆ can_serialize_bits()

bool bitstream::bit_measure::can_serialize_bits ( uint32_t  num_bits) const
inlinenoexcept

Returns whether the num_bits can fit in the buffer.

Parameters
num_bitsThe number of bits to test
Returns
Whether the number of bits can fit in the buffer

◆ get_buffer()

uint8_t* bitstream::bit_measure::get_buffer ( ) const
inlinenoexcept

Returns the buffer that this writer is currently serializing into.

Returns
The buffer

◆ get_num_bits_serialized()

uint32_t bitstream::bit_measure::get_num_bits_serialized ( ) const
inlinenoexcept

Returns the number of bits which have been written to the buffer.

Returns
The number of bits which have been written

◆ get_num_bytes_serialized()

uint32_t bitstream::bit_measure::get_num_bytes_serialized ( ) const
inlinenoexcept

Returns the number of bytes which have been written to the buffer.

Returns
The number of bytes which have been written

◆ get_remaining_bits()

uint32_t bitstream::bit_measure::get_remaining_bits ( ) const
inlinenoexcept

Returns the number of bits which have not been written yet.

Note
The same as get_total_bits() - get_num_bits_serialized()
Returns
The remaining space in the buffer

◆ get_total_bits()

uint32_t bitstream::bit_measure::get_total_bits ( ) const
inlinenoexcept

Returns the size of the buffer, in bits.

Returns
The size of the buffer, in bits

◆ operator=() [1/2]

bit_measure& bitstream::bit_measure::operator= ( bit_measure &&  rhs)
inlinenoexcept

◆ operator=() [2/2]

bit_measure& bitstream::bit_measure::operator= ( const bit_measure )
delete

◆ pad()

bool bitstream::bit_measure::pad ( uint32_t  num_bytes)
inlinenoexcept

Pads the buffer up with the given number of bytes.

Parameters
num_bytesThe amount of bytes to pad
Returns
Returns false if the current size of the buffer is bigger than num_bytes or if the padded bits are not zeros.

◆ pad_to_size()

bool bitstream::bit_measure::pad_to_size ( uint32_t  num_bytes)
inlinenoexcept

Pads the buffer up to the given number of bytes with zeros.

Parameters
num_bytesThe byte number to pad to
Returns
Returns false if the current size of the buffer is bigger than num_bytes

◆ prepend_checksum()

bool bitstream::bit_measure::prepend_checksum ( )
inlinenoexcept

Instructs the writer that you intend to use serialize_checksum() later on, and to reserve the first 32 bits.

Returns
Returns false if anything has already been written to the buffer or if there's no space to write the checksum

◆ serialize() [1/2]

template<typename Trait , typename... Args, typename = utility::has_serialize_t<Trait, bit_measure, Args...>>
bool bitstream::bit_measure::serialize ( Args &&...  args)
inlinenoexcept

Writes to the buffer, using the given Trait.

Note
The Trait type in this function must always be explicitly declared
Template Parameters
TraitA template specialization of serialize_trait<>
...ArgsThe types of the arguments to pass to the serialize function
Parameters
...argsThe arguments to pass to the serialize function
Returns
Whether successful or not

◆ serialize() [2/2]

template<typename... Args, typename Trait , typename = utility::has_deduce_serialize_t<Trait, bit_measure, Args...>>
bool bitstream::bit_measure::serialize ( Trait &&  arg,
Args &&...  args 
)
inlinenoexcept

Writes to the buffer, by trying to deduce the trait.

Note
The Trait type in this function is always implicit and will be deduced from the first argument if possible. If the trait cannot be deduced it will not compile.
Template Parameters
TraitThe type of the first argument, which will be used to deduce the trait specialization
...ArgsThe types of the arguments to pass to the serialize function
Parameters
argThe first argument to pass to the serialize function
...argsThe rest of the arguments to pass to the serialize function
Returns
Whether successful or not

◆ serialize_bits()

bool bitstream::bit_measure::serialize_bits ( uint32_t  value,
uint32_t  num_bits 
)
inlinenoexcept

Writes the first num_bits bits of value into the buffer.

Parameters
valueThe value to serialize
num_bitsThe number of bits of the value to serialize
Returns
Returns false if num_bits is less than 1 or greater than 32 or if writing the given number of bits would overflow the buffer

◆ serialize_bytes()

bool bitstream::bit_measure::serialize_bytes ( const uint8_t *  bytes,
uint32_t  num_bits 
)
inlinenoexcept

Writes the first num_bits bits of the given byte array, 32 bits at a time.

Parameters
bytesThe bytes to serialize
num_bitsThe number of bits of the bytes to serialize
Returns
Returns false if num_bits is less than 1 or if writing the given number of bits would overflow the buffer

◆ serialize_checksum()

uint32_t bitstream::bit_measure::serialize_checksum ( uint32_t  protocol_version)
inlinenoexcept

Writes a checksum of the protocol_version and the rest of the buffer as the first 32 bits.

Parameters
protocol_versionA unique version number
Returns
The number of bytes written to the buffer

Member Data Documentation

◆ reading

constexpr bool bitstream::bit_measure::reading = false
staticconstexpr

◆ writing

constexpr bool bitstream::bit_measure::writing = true
staticconstexpr

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