boost::capy::any_write_stream

Type‐erased wrapper for any WriteStream.

Synopsis

class any_write_stream;

Description

This class provides type erasure for any type satisfying the WriteStream concept, enabling runtime polymorphism for write operations. It uses cached awaitable storage to achieve zero steady‐state allocation after construction.

The wrapper supports two construction modes: ‐ Owning: Pass by value to transfer ownership. The wrapper allocates storage and owns the stream. ‐ Reference: Pass a pointer to wrap without ownership. The pointed‐to stream must outlive this wrapper.

Awaitable Preallocation

The constructor preallocates storage for the type‐erased awaitable. This reserves all virtual address space at server startup so memory usage can be measured up front, rather than allocating piecemeal as traffic arrives.

Immediate Completion

Operations complete immediately without suspending when the buffer sequence is empty, or when the underlying stream's awaitable reports readiness via await_ready.

Thread Safety

Not thread‐safe. Concurrent operations on the same wrapper are undefined behavior.

Example

// Owning - takes ownership of the stream
any_write_stream stream(socket{ioc});

// Reference - wraps without ownership
socket sock(ioc);
any_write_stream stream(&sock);

const_buffer buf(data, size);
auto [ec, n] = co_await stream.write_some(std::span(&buf, 1));

Member Functions

Name

Description

any_write_stream [constructor]

Constructors

~any_write_stream [destructor]

Destructor.

operator= [deleted]

Move assignment operator.

has_value

Check if the wrapper contains a valid stream.

write_some

Initiate an asynchronous write operation.

operator bool

Check if the wrapper contains a valid stream.

Protected Member Functions

Name

Description

rebind

Rebind to a new stream after move.

Derived Classes

Name Description

any_stream

Type‐erased wrapper for bidirectional streams.

See Also

any_read_stream, any_stream, WriteStream

Created with MrDocs