boost::capy::any_stream

Dispatches read_some and write_some through independent type‐erased vtables.

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

void reader(any_read_stream&);
void writer(any_write_stream&);

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

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

// Use read_some from the any_read_stream base
char rdata[1024];
mutable_buffer rbuf(rdata, sizeof(rdata));
auto [ec1, n1] = co_await owning_stream.read_some(std::span(&rbuf, 1));

// Use write_some from the any_write_stream base
char wdata[] = "hello";
const_buffer wbuf(wdata, sizeof(wdata));
auto [ec2, n2] = co_await owning_stream.write_some(std::span(&wbuf, 1));

// Pass to functions expecting one capability
reader(owning_stream);  // Implicit upcast
writer(owning_stream);  // Implicit upcast

Base Classes

Name

Description

any_read_stream

Dispatches read_some through a type‐erased vtable, using preallocated awaitable storage.

any_write_stream

Dispatches write_some through a type‐erased vtable, using preallocated awaitable storage.

Member Functions

Name

Description

any_stream [constructor]

Constructors

~any_stream [destructor]

Destructor.

operator= [deleted]

Assignment operators

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