boost::capy::any_stream

Type‐erased wrapper for bidirectional streams.

Synopsis

class any_stream
    : public any_read_stream
    , public any_write_stream

Description

This class provides type erasure for any type satisfying both the ReadStream and WriteStream concepts, enabling runtime polymorphism for bidirectional I/O operations.

Inherits from both any_read_stream and any_write_stream, providing read_some and write_some operations. Each base maintains its own cached awaitable storage, allowing concurrent read and write operations.

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.

Implicit Conversion

This class implicitly converts to any_read_stream& or any_write_stream&, allowing it to be passed to functions that accept only one capability. However, do not move through a base reference as this would leave the other base in an invalid state.

Thread Safety

Not thread‐safe. Concurrent operations of the same type (two reads or two writes) are undefined behavior. One read and one write may be in flight simultaneously.

Example

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

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

// Use read_some from any_read_stream base
mutable_buffer rbuf(rdata, rsize);
auto [ec1, n1] = co_await stream.read_some(std::span(&rbuf, 1));

// Use write_some from any_write_stream base
const_buffer wbuf(wdata, wsize);
auto [ec2, n2] = co_await stream.write_some(std::span(&wbuf, 1));

// Pass to functions expecting one capability
void reader(any_read_stream&);
void writer(any_write_stream&);
reader(stream);  // Implicit upcast
writer(stream);  // Implicit upcast

Base Classes

Name Description

any_read_stream

Type‐erased wrapper for any ReadStream.

any_write_stream

Type‐erased wrapper for any WriteStream.

Member Functions

Name

Description

any_stream [constructor]

Constructors

~any_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.

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.

See Also

any_read_stream, any_write_stream, ReadStream, WriteStream

Created with MrDocs