sparrow-ipc 0.2.0
Loading...
Searching...
No Matches
sparrow_ipc::memory_output_stream< R > Class Template Reference

An output stream that writes data to a contiguous memory buffer. More...

#include <memory_output_stream.hpp>

Public Member Functions

 memory_output_stream (R &buffer)
 Constructs a memory output stream with a reference to a buffer.
 
memory_output_streamwrite (const char *s, std::streamsize count)
 Writes character data to the buffer.
 
memory_output_streamwrite (std::span< const std::uint8_t > span)
 Writes a span of bytes to the buffer.
 
memory_output_streamwrite (uint8_t value, std::size_t count)
 Writes a byte value repeated a specified number of times.
 
memory_output_streamput (char value)
 Writes a single character to the buffer.
 
void reserve (std::size_t size)
 Reserves capacity in the underlying buffer.
 
void reserve (const std::function< std::size_t()> &calculate_reserve_size)
 Reserves capacity using a lazy calculation function.
 
size_t size () const
 Gets the current size of the buffer.
 

Detailed Description

template<typename R>
requires std::ranges::random_access_range<R> && std::same_as<typename R::value_type, uint8_t>
class sparrow_ipc::memory_output_stream< R >

An output stream that writes data to a contiguous memory buffer.

This template class implements an output_stream that appends data to a contiguous random-access range (typically std::vector<uint8_t>). All write operations append data to the end of the buffer, making it grow as needed.

Template Parameters
RA random access range type with uint8_t as its value type. Typically std::vector<uint8_t> or similar contiguous container types.

The memory output stream:

  • Supports efficient append operations
  • Can reserve capacity to minimize reallocations
  • Always operates on a contiguous memory buffer
  • Stores a non-owning reference to the buffer
Note
The caller must ensure the buffer remains valid for the lifetime of this stream

Definition at line 29 of file memory_output_stream.hpp.

Constructor & Destructor Documentation

◆ memory_output_stream()

template<typename R>
sparrow_ipc::memory_output_stream< R >::memory_output_stream ( R & buffer)
inline

Constructs a memory output stream with a reference to a buffer.

Parameters
bufferReference to the container that will store the written data. The stream stores a non-owning pointer to this buffer for write operations.
Note
The caller must ensure the buffer remains valid for the lifetime of this stream

Definition at line 41 of file memory_output_stream.hpp.

Here is the caller graph for this function:

Member Function Documentation

◆ put()

template<typename R>
requires std::ranges::random_access_range<R> && std::same_as<typename R::value_type, uint8_t>
memory_output_stream< R > & sparrow_ipc::memory_output_stream< R >::put ( char value)

Writes a single character to the buffer.

Appends a single byte to the end of the buffer. The character is cast to uint8_t.

Parameters
valueThe character value to write
Returns
Reference to this stream for method chaining

Definition at line 148 of file memory_output_stream.hpp.

Here is the call graph for this function:

◆ reserve() [1/2]

template<typename R>
requires std::ranges::random_access_range<R> && std::same_as<typename R::value_type, uint8_t>
void sparrow_ipc::memory_output_stream< R >::reserve ( const std::function< std::size_t()> & calculate_reserve_size)

Reserves capacity using a lazy calculation function.

Calls the provided function to determine the buffer size to reserve.

Parameters
calculate_reserve_sizeFunction that returns the number of bytes to reserve

Definition at line 163 of file memory_output_stream.hpp.

◆ reserve() [2/2]

template<typename R>
requires std::ranges::random_access_range<R> && std::same_as<typename R::value_type, uint8_t>
void sparrow_ipc::memory_output_stream< R >::reserve ( std::size_t size)

Reserves capacity in the underlying buffer.

Reserves space for at least the specified number of bytes in the buffer. This can help minimize reallocations during subsequent write operations.

Parameters
sizeNumber of bytes to reserve

Definition at line 156 of file memory_output_stream.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

template<typename R>
requires std::ranges::random_access_range<R> && std::same_as<typename R::value_type, uint8_t>
size_t sparrow_ipc::memory_output_stream< R >::size ( ) const
nodiscard

Gets the current size of the buffer.

Returns
The number of bytes currently in the buffer

Definition at line 170 of file memory_output_stream.hpp.

Here is the caller graph for this function:

◆ write() [1/3]

template<typename R>
requires std::ranges::random_access_range<R> && std::same_as<typename R::value_type, uint8_t>
memory_output_stream< R > & sparrow_ipc::memory_output_stream< R >::write ( const char * s,
std::streamsize count )

Writes character data to the buffer.

Appends the specified character data to the end of the buffer.

Parameters
sPointer to the character data to write
countNumber of characters to write
Returns
Reference to this stream for method chaining
Note
The characters are converted to uint8_t and appended to the buffer

Definition at line 124 of file memory_output_stream.hpp.

Here is the call graph for this function:

◆ write() [2/3]

template<typename R>
requires std::ranges::random_access_range<R> && std::same_as<typename R::value_type, uint8_t>
memory_output_stream< R > & sparrow_ipc::memory_output_stream< R >::write ( std::span< const std::uint8_t > span)

Writes a span of bytes to the buffer.

Appends the data from the provided span to the end of the buffer.

Parameters
spanA span of bytes to write
Returns
Reference to this stream for method chaining

Definition at line 132 of file memory_output_stream.hpp.

Here is the call graph for this function:

◆ write() [3/3]

template<typename R>
requires std::ranges::random_access_range<R> && std::same_as<typename R::value_type, uint8_t>
memory_output_stream< R > & sparrow_ipc::memory_output_stream< R >::write ( uint8_t value,
std::size_t count )

Writes a byte value repeated a specified number of times.

Appends the specified byte value repeated count times to the end of the buffer. This is useful for padding operations or filling with a specific value.

Parameters
valueThe byte value to write
countNumber of times to repeat the value
Returns
Reference to this stream for method chaining

Definition at line 140 of file memory_output_stream.hpp.

Here is the call graph for this function:

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