Type-erased wrapper for any stream-like object.
More...
#include <any_output_stream.hpp>
|
| 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_stream & | operator= (any_output_stream &&) noexcept=default |
| |
| | any_output_stream (const any_output_stream &)=delete |
| |
| any_output_stream & | operator= (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.
|
| |
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;
std::ostringstream oss;
my_custom_stream 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.
◆ 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
-
- Parameters
-
| stream | The 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.
◆ ~any_output_stream()
| sparrow_ipc::any_output_stream::~any_output_stream |
( |
| ) |
|
|
default |
◆ any_output_stream() [2/3]
◆ any_output_stream() [3/3]
◆ add_padding()
| void sparrow_ipc::any_output_stream::add_padding |
( |
| ) |
|
Adds padding to align to 8-byte boundary.
◆ 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
-
| TStream | The expected concrete type of the underlying stream |
- Returns
- Reference to the underlying stream as TStream
- Exceptions
-
| std::bad_cast | if the underlying stream is not of type TStream |
Definition at line 213 of file any_output_stream.hpp.
◆ 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
-
| TStream | The expected concrete type of the underlying stream |
- Returns
- Const reference to the underlying stream as TStream
- Exceptions
-
| std::bad_cast | if the underlying stream is not of type TStream |
Definition at line 224 of file any_output_stream.hpp.
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ 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_size | Function that calculates the size to reserve |
◆ reserve() [2/2]
| void sparrow_ipc::any_output_stream::reserve |
( |
std::size_t | size | ) |
|
Reserves capacity if supported by the underlying stream.
- Parameters
-
| size | The number of bytes to reserve |
◆ 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
◆ 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
-
- Returns
- The number of bytes written
- Exceptions
-
| std::runtime_error | if write operation fails |
◆ 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
-
| value | The byte value to write |
| count | Number of times to write the value (default: 1) |
- Returns
- The number of bytes written
The documentation for this class was generated from the following file: