Binary Serialize
Loading...
Searching...
No Matches
fixed_size_byte_array< N > Class Template Reference

Extract a sequence in network byte order from a std::byte buffer into the provided container. More...

#include <binary_serialize.hpp>

Public Member Functions

 fixed_size_byte_array () noexcept
 Default construct the @ fixed_size_byte_array.
 
std::size_t size () const noexcept
 Return the size of the data which has been written into the buffer.
 
void resize (std::size_t sz) noexcept
 Increase the logical size, allowing bytes to be appended.
 
std::byte * data () noexcept
 Return a pointer to the beginning of the buffer.
 
void clear () noexcept
 Logically reset so that new data can be written into the buffer at the beginning.
 

Detailed Description

template<std::size_t N>
class fixed_size_byte_array< N >

Extract a sequence in network byte order from a std::byte buffer into the provided container.

Fill a container with a sequence of elements. See append_sequence for additional comments on sequence.

Parameters
bufBuffer of std::bytes containing a sequence of objects to be extracted, with the count in front.
Template Parameters
CntType of the count value, which prepends the sequence of elements. The type determines the size (in bytes) of the count value (e.g. std::uint16_t would specify a 16-bit unsigned count).
ContainerContainer type, which must provide a value_type declaration, a default constructor, an emplace_back method, and a copy or move constructor.
Returns
A container with the element sequence.
Precondition
The buffer must contain at least sizeof(T) bytes.

Append a sequence of elements, including the count, into a std::byte buffer using the lower level append_val function.

A sequence is defined as an array of elements of type T, along with the number of elements. When appending to a stream of std::bytes, the count is first converted into network byte order and then appended, then the same performed for each element. The number of bits in the count is specified as the type of the count. For example a std::vector of 16-bit ints with an 8-bit count can be appended to the buffer as an 8-bit integer, and then successive 16-bit integers.

Parameters
bufBuffer of std::bytes containing an object of type T in network byte order.
cntNumber of elements in the sequence.
startIterator pointing to the start of the sequence.
endIterator pointing to the end of the sequence.
Returns
Total number of bytes appended to the std::byte buffer.
Precondition
The buffer must be large enough to contain the size of the count, plus all of the elements in the sequence.

Adapt a std::array so that a fixed size std::byte array can be used with the chops::marshall function template as the Buf template parameter.

This class provides three methods (size, resize, data) as required by the Buf parameter type in the chops::marshall function template.

The logical size of the buffer is tracked. If resize is called and there is not enough room in the buffer an assert is fired.

Member Function Documentation

◆ data()

template<std::size_t N>
std::byte * fixed_size_byte_array< N >::data ( )
inlinenoexcept

Return a pointer to the beginning of the buffer.

Returns
A pointer to the beginning of the buffer.

◆ resize()

template<std::size_t N>
void fixed_size_byte_array< N >::resize ( std::size_t sz)
inlinenoexcept

Increase the logical size, allowing bytes to be appended.

Parameters
szNew logical size.

◆ size()

template<std::size_t N>
std::size_t fixed_size_byte_array< N >::size ( ) const
inlinenoexcept

Return the size of the data which has been written into the buffer.

Returns
The size of the data which has been written into the buffer, which is modified through the resize method.

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