BitStream
bitstream::bit_reader< Policy > Class Template Reference

A stream for reading objects from a tightly packed buffer. More...

#include <bit_reader.h>

Public Member Functions

template<typename... Ts, typename = std::enable_if_t<std::is_constructible_v<Policy, Ts...>>>
 bit_reader (Ts &&... args) noexcept(std::is_nothrow_constructible_v< Policy, Ts... >)
 Construct a reader with the parameters passed to the underlying policy. More...
 
 bit_reader (const bit_reader &)=delete
 
 bit_reader (bit_reader &&other) noexcept
 
bit_readeroperator= (const bit_reader &)=delete
 
bit_readeroperator= (bit_reader &&rhs) noexcept
 
const uint8_t * get_buffer () const noexcept
 Returns the buffer that this reader is currently serializing from. More...
 
uint32_t get_num_bits_serialized () const noexcept
 Returns the number of bits which have been read from the buffer. More...
 
uint32_t get_num_bytes_serialized () const noexcept
 Returns the number of bytes which have been read from the buffer. More...
 
bool can_serialize_bits (uint32_t num_bits) const noexcept
 Returns whether the num_bits be read from the buffer. More...
 
uint32_t get_remaining_bits () const noexcept
 Returns the number of bits which have not been read yet. More...
 
uint32_t get_total_bits () const noexcept
 Returns the size of the buffer, in bits. More...
 
bool serialize_checksum (uint32_t protocol_version) noexcept
 Reads the first 32 bits of the buffer and compares it to a checksum of the protocol_version and the rest of the buffer. More...
 
bool pad_to_size (uint32_t num_bytes) noexcept
 Pads the buffer up to the given number of bytes. 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 read is byte-aligned @notes Return false if the padded bits are not zeros. More...
 
bool serialize_bits (uint32_t &value, uint32_t num_bits) noexcept
 Reads the first num_bits bits of value from the buffer. More...
 
bool serialize_bytes (uint8_t *bytes, uint32_t num_bits) noexcept
 Reads 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_reader, Args...>>
bool serialize (Args &&... args) noexcept(utility::is_serialize_noexcept_v< Trait, bit_reader, Args... >)
 Reads from the buffer, using the given Trait. More...
 
template<typename... Args, typename Trait , typename = utility::has_deduce_serialize_t<Trait, bit_reader, Args...>>
bool serialize (Trait &&arg, Args &&... args) noexcept(utility::is_deduce_serialize_noexcept_v< Trait, bit_reader, Args... >)
 Reads from the buffer, by trying to deduce the trait. More...
 

Static Public Attributes

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

Detailed Description

template<typename Policy>
class bitstream::bit_reader< Policy >

A stream for reading objects from a tightly packed buffer.

Template Parameters
PolicyThe underlying representation of the buffer

Constructor & Destructor Documentation

◆ bit_reader() [1/3]

template<typename Policy >
template<typename... Ts, typename = std::enable_if_t<std::is_constructible_v<Policy, Ts...>>>
bitstream::bit_reader< Policy >::bit_reader ( Ts &&...  args)
inlinenoexcept

Construct a reader with the parameters passed to the underlying policy.

Parameters
...argsThe arguments to pass to the policy

◆ bit_reader() [2/3]

template<typename Policy >
bitstream::bit_reader< Policy >::bit_reader ( const bit_reader< Policy > &  )
delete

◆ bit_reader() [3/3]

template<typename Policy >
bitstream::bit_reader< Policy >::bit_reader ( bit_reader< Policy > &&  other)
inlinenoexcept

Member Function Documentation

◆ align()

template<typename Policy >
bool bitstream::bit_reader< Policy >::align ( )
inlinenoexcept

Pads the buffer with up to 8 zeros, so that the next read is byte-aligned @notes Return false if the padded bits are not zeros.

Returns
Returns false if the padded bits are not zeros

◆ can_serialize_bits()

template<typename Policy >
bool bitstream::bit_reader< Policy >::can_serialize_bits ( uint32_t  num_bits) const
inlinenoexcept

Returns whether the num_bits be read from the buffer.

Parameters
num_bitsThe number of bits to test
Returns
Whether the number of bits can be read from the buffer

◆ get_buffer()

template<typename Policy >
const uint8_t* bitstream::bit_reader< Policy >::get_buffer ( ) const
inlinenoexcept

Returns the buffer that this reader is currently serializing from.

Returns
The buffer

◆ get_num_bits_serialized()

template<typename Policy >
uint32_t bitstream::bit_reader< Policy >::get_num_bits_serialized ( ) const
inlinenoexcept

Returns the number of bits which have been read from the buffer.

Returns
The number of bits which have been read

◆ get_num_bytes_serialized()

template<typename Policy >
uint32_t bitstream::bit_reader< Policy >::get_num_bytes_serialized ( ) const
inlinenoexcept

Returns the number of bytes which have been read from the buffer.

Returns
The number of bytes which have been read

◆ get_remaining_bits()

template<typename Policy >
uint32_t bitstream::bit_reader< Policy >::get_remaining_bits ( ) const
inlinenoexcept

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

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

◆ get_total_bits()

template<typename Policy >
uint32_t bitstream::bit_reader< Policy >::get_total_bits ( ) const
inlinenoexcept

Returns the size of the buffer, in bits.

Returns
The size of the buffer, in bits

◆ operator=() [1/2]

template<typename Policy >
bit_reader& bitstream::bit_reader< Policy >::operator= ( bit_reader< Policy > &&  rhs)
inlinenoexcept

◆ operator=() [2/2]

template<typename Policy >
bit_reader& bitstream::bit_reader< Policy >::operator= ( const bit_reader< Policy > &  )
delete

◆ pad()

template<typename Policy >
bool bitstream::bit_reader< Policy >::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()

template<typename Policy >
bool bitstream::bit_reader< Policy >::pad_to_size ( uint32_t  num_bytes)
inlinenoexcept

Pads the buffer up to the given number of bytes.

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

◆ serialize() [1/2]

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

Reads from 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 Policy >
template<typename... Args, typename Trait , typename = utility::has_deduce_serialize_t<Trait, bit_reader, Args...>>
bool bitstream::bit_reader< Policy >::serialize ( Trait &&  arg,
Args &&...  args 
)
inlinenoexcept

Reads from 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()

template<typename Policy >
bool bitstream::bit_reader< Policy >::serialize_bits ( uint32_t &  value,
uint32_t  num_bits 
)
inlinenoexcept

Reads the first num_bits bits of value from 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 reading the given number of bits would overflow the buffer

◆ serialize_bytes()

template<typename Policy >
bool bitstream::bit_reader< Policy >::serialize_bytes ( uint8_t *  bytes,
uint32_t  num_bits 
)
inlinenoexcept

Reads 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 reading the given number of bits would overflow the buffer

◆ serialize_checksum()

template<typename Policy >
bool bitstream::bit_reader< Policy >::serialize_checksum ( uint32_t  protocol_version)
inlinenoexcept

Reads the first 32 bits of the buffer and compares it to a checksum of the protocol_version and the rest of the buffer.

Parameters
protocol_versionA unique version number
Returns
Whether the checksum matches what was written

Member Data Documentation

◆ reading

template<typename Policy >
constexpr bool bitstream::bit_reader< Policy >::reading = true
staticconstexpr

◆ writing

template<typename Policy >
constexpr bool bitstream::bit_reader< Policy >::writing = false
staticconstexpr

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