sparrow-ipc 0.2.0
Loading...
Searching...
No Matches
sparrow_ipc::any_output_stream Class Reference

Type-erased wrapper for any stream-like object. More...

#include <any_output_stream.hpp>

Public Member Functions

template<writable_stream TStream>
 any_output_stream (TStream &stream)
 Constructs a type-erased stream from any stream-like object.
 
 ~any_output_stream ()=default
 Default destructor.
 
 any_output_stream (any_output_stream &&) noexcept=default
 
any_output_streamoperator= (any_output_stream &&) noexcept=default
 
 any_output_stream (const any_output_stream &)=delete
 
any_output_streamoperator= (const any_output_stream &)=delete
 
void write (std::span< const std::uint8_t > span)
 Writes a span of bytes to the underlying stream.
 
void write (uint8_t value, std::size_t count=1)
 Writes a single byte value multiple times.
 
void add_padding ()
 Adds padding to align to 8-byte boundary.
 
void reserve (std::size_t size)
 Reserves capacity if supported by the underlying stream.
 
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 stream.
 
template<typename TStream>
TStream & get ()
 Gets a reference to the underlying stream cast to the specified type.
 
template<typename TStream>
const TStream & get () const
 Gets a const reference to the underlying stream cast to the specified type.
 

Detailed Description

Type-erased wrapper for any stream-like object.

This class provides type erasure for ANY type that supports stream operations. It uses the concept-based type erasure pattern to wrap any stream-like object polymorphically.

This implementation uses the classic type erasure pattern with:

  • An abstract base class (stream_concept) defining the interface
  • A templated model class that adapts any stream type to the interface
  • A wrapper class that stores the model polymorphically

Usage:

std::vector<uint8_t> buffer;
any_output_stream stream1(mem_stream);
// Also works with standard streams
std::ostringstream oss;
any_output_stream stream2(oss);
// Or any custom type with a write method
my_custom_stream custom;
any_output_stream stream3(custom);
any_output_stream(TStream &stream)
Constructs a type-erased stream from any stream-like object.
An output stream that writes data to a contiguous memory buffer.

The class provides a common interface that works with any stream type.

Definition at line 54 of file any_output_stream.hpp.

Constructor & Destructor Documentation

◆ any_output_stream() [1/3]

template<writable_stream TStream>
sparrow_ipc::any_output_stream::any_output_stream ( TStream & stream)

Constructs a type-erased stream from any stream-like object.

Template Parameters
TStreamThe concrete stream type (must satisfy writable_stream concept)
Parameters
streamThe stream object to wrap

The stream is stored by reference, so the caller must ensure the stream lifetime exceeds that of the any_output_stream object.

Definition at line 207 of file any_output_stream.hpp.

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

◆ ~any_output_stream()

sparrow_ipc::any_output_stream::~any_output_stream ( )
default

Default destructor.

◆ any_output_stream() [2/3]

sparrow_ipc::any_output_stream::any_output_stream ( any_output_stream && )
defaultnoexcept
Here is the call graph for this function:

◆ any_output_stream() [3/3]

sparrow_ipc::any_output_stream::any_output_stream ( const any_output_stream & )
delete
Here is the call graph for this function:

Member Function Documentation

◆ add_padding()

void sparrow_ipc::any_output_stream::add_padding ( )

Adds padding to align to 8-byte boundary.

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

◆ get() [1/2]

template<typename TStream>
TStream & sparrow_ipc::any_output_stream::get ( )

Gets a reference to the underlying stream cast to the specified type.

Template Parameters
TStreamThe expected concrete type of the underlying stream
Returns
Reference to the underlying stream as TStream
Exceptions
std::bad_castif the underlying stream is not of type TStream

Definition at line 213 of file any_output_stream.hpp.

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

◆ get() [2/2]

template<typename TStream>
const TStream & sparrow_ipc::any_output_stream::get ( ) const

Gets a const reference to the underlying stream cast to the specified type.

Template Parameters
TStreamThe expected concrete type of the underlying stream
Returns
Const reference to the underlying stream as TStream
Exceptions
std::bad_castif the underlying stream is not of type TStream

Definition at line 224 of file any_output_stream.hpp.

Here is the call graph for this function:

◆ operator=() [1/2]

any_output_stream & sparrow_ipc::any_output_stream::operator= ( any_output_stream && )
defaultnoexcept
Here is the call graph for this function:

◆ operator=() [2/2]

any_output_stream & sparrow_ipc::any_output_stream::operator= ( const any_output_stream & )
delete
Here is the call graph for this function:

◆ reserve() [1/2]

void sparrow_ipc::any_output_stream::reserve ( const std::function< std::size_t()> & calculate_reserve_size)

Reserves capacity using a lazy calculation function.

Parameters
calculate_reserve_sizeFunction that calculates the size to reserve
Here is the call graph for this function:

◆ reserve() [2/2]

void sparrow_ipc::any_output_stream::reserve ( std::size_t size)

Reserves capacity if supported by the underlying stream.

Parameters
sizeThe number of bytes to reserve
Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

size_t sparrow_ipc::any_output_stream::size ( ) const
nodiscard

Gets the current size of the stream.

Returns
The current number of bytes written
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write() [1/2]

void sparrow_ipc::any_output_stream::write ( std::span< const std::uint8_t > span)

Writes a span of bytes to the underlying stream.

Parameters
spanThe bytes to write
Returns
The number of bytes written
Exceptions
std::runtime_errorif write operation fails
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write() [2/2]

void sparrow_ipc::any_output_stream::write ( uint8_t value,
std::size_t count = 1 )

Writes a single byte value multiple times.

Parameters
valueThe byte value to write
countNumber of times to write the value (default: 1)
Returns
The number of bytes written
Here is the call graph for this function:

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