boost::capy::any_read_stream

Type‐erased wrapper for any ReadStream.

Synopsis

class any_read_stream;

Description

This class provides type erasure for any type satisfying the ReadStream concept, enabling runtime polymorphism for read 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

When the underlying stream's awaitable reports ready immediately (e.g. buffered data already available), the wrapper skips coroutine suspension entirely and returns the result inline.

Thread Safety

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

Example

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

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

mutable_buffer buf(data, size);
auto [ec, n] = co_await stream.read_some(buf);

Member Functions

Name

Description

any_read_stream [constructor]

Constructors

~any_read_stream [destructor]

Destructor.

operator= [deleted]

Move assignment operator.

has_value

Check if the wrapper contains a valid stream.

read_some

Initiate an asynchronous read 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_write_stream, any_stream, ReadStream

Created with MrDocs