BitStream
bitstream::bit_writer< Policy > Class Template Reference

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

#include <bit_writer.h>

Public Member Functions

template<typename... Ts, typename = std::enable_if_t<std::is_constructible_v<Policy, Ts...>>>
 bit_writer (Ts &&... args) noexcept(std::is_nothrow_constructible_v< Policy, Ts... >)
 Construct a writer with the parameters passed to the underlying policy. More...
 
 bit_writer (const bit_writer &)=delete
 
 bit_writer (bit_writer &&other) noexcept
 
bit_writeroperator= (const bit_writer &)=delete
 
bit_writeroperator= (bit_writer &&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...
 
uint32_t flush () noexcept
 Flushes any remaining bits into the buffer. Use this when you no longer intend to write anything to the buffer. 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...
 
bool serialize_into (bit_writer &writer) const noexcept
 Writes the contents of the buffer into the given writer. Essentially copies the entire buffer without modifying it. More...
 
template<typename Trait , typename... Args, typename = utility::has_serialize_t<Trait, bit_writer, Args...>>
bool serialize (Args &&... args) noexcept(utility::is_serialize_noexcept_v< Trait, bit_writer, Args... >)
 Writes to the buffer, using the given Trait. More...
 
template<typename... Args, typename Trait , typename = utility::has_deduce_serialize_t<Trait, bit_writer, Args...>>
bool serialize (Trait &&arg, Args &&... args) noexcept(utility::is_deduce_serialize_noexcept_v< Trait, bit_writer, 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

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

A stream for writing objects tightly into a buffer.

Template Parameters
PolicyThe underlying representation of the buffer

Constructor & Destructor Documentation

◆ bit_writer() [1/3]

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

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

Parameters
...argsThe arguments to pass to the policy

◆ bit_writer() [2/3]

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

◆ bit_writer() [3/3]

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

Member Function Documentation

◆ align()

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

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

Returns
Success

◆ can_serialize_bits()

template<typename Policy >
bool bitstream::bit_writer< Policy >::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

◆ flush()

template<typename Policy >
uint32_t bitstream::bit_writer< Policy >::flush ( )
inlinenoexcept

Flushes any remaining bits into the buffer. Use this when you no longer intend to write anything to the buffer.

Returns
The number of bytes written to the buffer

◆ get_buffer()

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

Returns the buffer that this writer is currently serializing into.

Returns
The buffer

◆ get_num_bits_serialized()

template<typename Policy >
uint32_t bitstream::bit_writer< Policy >::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()

template<typename Policy >
uint32_t bitstream::bit_writer< Policy >::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()

template<typename Policy >
uint32_t bitstream::bit_writer< Policy >::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()

template<typename Policy >
uint32_t bitstream::bit_writer< 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_writer& bitstream::bit_writer< Policy >::operator= ( bit_writer< Policy > &&  rhs)
inlinenoexcept

◆ operator=() [2/2]

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

◆ pad()

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

template<typename Policy >
bool bitstream::bit_writer< Policy >::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 Policy >
template<typename Trait , typename... Args, typename = utility::has_serialize_t<Trait, bit_writer, Args...>>
bool bitstream::bit_writer< Policy >::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 Policy >
template<typename... Args, typename Trait , typename = utility::has_deduce_serialize_t<Trait, bit_writer, Args...>>
bool bitstream::bit_writer< Policy >::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()

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

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

template<typename Policy >
uint32_t bitstream::bit_writer< Policy >::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

◆ serialize_into()

template<typename Policy >
bool bitstream::bit_writer< Policy >::serialize_into ( bit_writer< Policy > &  writer) const
inlinenoexcept

Writes the contents of the buffer into the given writer. Essentially copies the entire buffer without modifying it.

Parameters
writerThe writer to copy into
Returns
Returns false if writing would overflow the buffer

Member Data Documentation

◆ reading

template<typename Policy >
constexpr bool bitstream::bit_writer< Policy >::reading = false
staticconstexpr

◆ writing

template<typename Policy >
constexpr bool bitstream::bit_writer< Policy >::writing = true
staticconstexpr

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