boost::corosio::stream_file

An asynchronous sequential file for coroutine I/O.

Synopsis

class stream_file
    : public io_stream

Description

Provides asynchronous read and write operations on a regular file with an implicit position that advances after each operation.

Inherits from io_stream, so read_some and write_some are available and work with any algorithm that accepts an io_stream&.

On POSIX platforms, file I/O is dispatched to a thread pool (blocking preadv/`pwritev`) with completion posted back to the scheduler. On Windows, true overlapped I/O is used via IOCP.

Thread Safety

Distinct objects: Safe. Shared objects: Unsafe. Only one asynchronous operation may be in flight at a time.

Example

io_context ioc;
stream_file f(ioc);
f.open("data.bin", file_base::read_only);

char buf[4096];
auto [ec, n] = co_await f.read_some(
    capy::mutable_buffer(buf, sizeof(buf)));
if (ec == capy::cond::eof)
    // end of file

Base Classes

Name

Description

io_stream

Platform stream with read/write operations.

Types

Name

Description

handle

RAII wrapper for I/O object implementation lifetime.

implementation

Platform‐specific file implementation interface.

io_service

Service interface for I/O object lifecycle management.

Member Functions

Name

Description

stream_file [constructor] [deleted]

Constructors

~stream_file [destructor] [virtual]

Destructor.

operator= [deleted]

Move assignment operator.

assign

Adopt an existing native handle.

cancel

Cancel pending asynchronous operations.

close

Close the file.

context

Return the execution context.

is_open

Check if the file is open.

native_handle

Get the native file descriptor or handle.

open

Open a file.

read_some

Asynchronously read data from the stream.

release

Release ownership of the native handle.

resize

Resize the file to new_size bytes.

seek

Move the file position.

size

Return the file size in bytes.

sync_all

Synchronize file data and metadata to stable storage.

sync_data

Synchronize file data to stable storage.

write_some

Asynchronously write data to the stream.

Protected Types

Name

Description

read_some_awaitable

Awaitable for async read operations.

write_some_awaitable

Awaitable for async write operations.

Protected Member Functions

Name

Description

stream_file [constructor]

Constructors

operator= [deleted]

Move assign from another I/O object.

do_read_some [virtual]

do_read_some overloads

do_write_some [virtual]

do_write_some overloads

Protected Static Member Functions

Name

Description

create_handle

Create a handle bound to a service found in the context.

Protected Data Members

Name

Description

h_

The platform I/O handle owned by this object.

Derived Classes

Name

Description

native_stream_file

A sequential file with devirtualized async I/O operations.

Created with MrDocs