Pigweed
 
Loading...
Searching...
No Matches
pw::ByteBuilder Class Reference

#include <byte_builder.h>

Inheritance diagram for pw::ByteBuilder:
pw::ByteBuffer< kSizeBytes >

Classes

class  iterator
 

Public Types

using element_type = const std::byte
 
using value_type = std::byte
 
using pointer = std::byte *
 
using reference = std::byte &
 
using iterator = iterator
 
using const_iterator = iterator
 

Public Member Functions

constexpr ByteBuilder (ByteSpan buffer)
 Creates an empty ByteBuilder.
 
 ByteBuilder (const ByteBuilder &)=delete
 
ByteBuilderoperator= (const ByteBuilder &)=delete
 
const std::byte * data () const
 Returns the contents of the bytes buffer.
 
Status status () const
 
StatusWithSize status_with_size () const
 Returns status() and size() as a StatusWithSize.
 
bool ok () const
 True if status() is OkStatus().
 
bool empty () const
 True if the bytes builder is empty.
 
size_t size () const
 Returns the current length of the bytes.
 
size_t max_size () const
 Returns the maximum length of the bytes.
 
void clear ()
 Clears the bytes and resets its error state.
 
void clear_status ()
 Sets the statuses to OkStatus();.
 
void push_back (std::byte b)
 
void pop_back ()
 
const_iterator begin () const
 Root of bytebuffer wrapped in iterator type.
 
const_iterator cbegin () const
 
const_iterator end () const
 End of bytebuffer wrapped in iterator type.
 
const_iterator cend () const
 
const std::byte & front () const
 Front and Back C++ container functions.
 
const std::byte & back () const
 
ByteBuilderappend (size_t count, std::byte b)
 Appends the provided byte count times.
 
ByteBuilderappend (const void *bytes, size_t count)
 
ByteBuilderappend (ConstByteSpan bytes)
 Appends bytes from a byte span that calls the pointer/length version.
 
void resize (size_t new_size)
 
ByteBuilderPutUint8 (uint8_t val)
 Put methods for inserting different 8-bit ints.
 
ByteBuilderPutInt8 (int8_t val)
 
ByteBuilderPutUint16 (uint16_t value, endian order=endian::little)
 Put methods for inserting different 16-bit ints.
 
ByteBuilderPutInt16 (int16_t value, endian order=endian::little)
 
ByteBuilderPutUint32 (uint32_t value, endian order=endian::little)
 Put methods for inserting different 32-bit ints.
 
ByteBuilderPutInt32 (int32_t value, endian order=endian::little)
 
ByteBuilderPutUint64 (uint64_t value, endian order=endian::little)
 Put methods for inserting different 64-bit ints.
 
ByteBuilderPutInt64 (int64_t value, endian order=endian::little)
 

Protected Member Functions

constexpr ByteBuilder (const ByteSpan &buffer, const ByteBuilder &other)
 Functions to support ByteBuffer copies.
 
void CopySizeAndStatus (const ByteBuilder &other)
 

Detailed Description

ByteBuilder facilitates building bytes in a fixed-size buffer. BytesBuilders never overflow. Status is tracked for each operation and an overall status is maintained, which reflects the most recent error.

A ByteBuilder does not own the buffer it writes to. It can be used to write bytes to any buffer. The ByteBuffer template class, defined below, allocates a buffer alongside a ByteBuilder.

Constructor & Destructor Documentation

◆ ByteBuilder()

pw::ByteBuilder::ByteBuilder ( const ByteBuilder )
delete

Disallow copy/assign to avoid confusion about where the bytes is actually stored. ByteBuffers may be copied into one another.

Member Function Documentation

◆ append()

ByteBuilder & pw::ByteBuilder::append ( const void *  bytes,
size_t  count 
)

Appends count bytes from 'bytes' to the end of the ByteBuilder. If count exceeds the remaining space in the ByteBuffer, no bytes will be appended and the status is set to RESOURCE_EXHAUSTED.

◆ pop_back()

void pw::ByteBuilder::pop_back ( )
inline

Removes the last byte. Sets the status to OUT_OF_RANGE if the buffer is empty (in which case the unsigned overflow is intentional).

◆ push_back()

void pw::ByteBuilder::push_back ( std::byte  b)
inline

Appends a single byte. Sets the status to RESOURCE_EXHAUSTED if the byte cannot be added because the buffer is full.

◆ resize()

void pw::ByteBuilder::resize ( size_t  new_size)

Sets the ByteBuilder's size. This function only truncates; if new_size > size(), it sets status to OUT_OF_RANGE and does nothing.

◆ status()

Status pw::ByteBuilder::status ( ) const
inline

Returns the ByteBuilder's status, which reflects the most recent error that occurred while updating the bytes. After an update fails, the status remains non-OK until it is cleared with clear() or clear_status().

Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: No errors have occurred.
* 
*     RESOURCE_EXHAUSTED: Output to the ``ByteBuilder`` was truncated.
* 
*     INVALID_ARGUMENT: ``printf``-style formatting failed.
* 
*     OUT_OF_RANGE: An operation outside the buffer was attempted.
* 
*  

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