boost::capy::any_stream
Dispatches read_some and write_some through independent type‐erased vtables.
Synopsis
Declared in <boost/capy/io/any_stream.hpp>
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 |
Dispatches |
|
Dispatches |
Member Functions
Name |
Description |
|
Constructors |
|
Destructor. |
|
Assignment operators |
Check if the wrapper contains a valid stream. |
|
Initiate an asynchronous read operation. |
|
Initiate an asynchronous write operation. |
|
Check if the wrapper contains a valid stream. |
See Also
any_read_stream, any_write_stream, ReadStream, WriteStream
Created with MrDocs