boost::corosio::native_stream_file

Reads and writes a file sequentially, calling the backend directly.

Synopsis

template<auto Backend>
class native_stream_file
    : public stream_file

Description

This class template inherits from stream_file. It shadows read_some / write_some with versions that call the backend implementation directly. The compiler can then inline through the entire call chain.

Non‐async operations (open, close, size, resize, seek, sync_data, sync_all) remain unchanged and dispatch through the compiled library.

A native_stream_file IS‐A stream_file and can be passed to any function expecting stream_file& or io_stream&, in which case virtual dispatch is used transparently.

On POSIX platforms, file I/O is dispatched to a thread pool regardless of the chosen reactor backend. All three reactor tags (epoll, select, kqueue) therefore resolve to the same underlying implementation. The Backend template parameter exists for API symmetry with native_tcp_socket and friends. The vtable savings are smaller relative to the thread‐pool / overlapped‐I/O cost than they are for socket operations.

Thread Safety

Same as stream_file.

Example

capy::task<>
open_and_read()
{
    corosio::native_io_context<corosio::epoll> ctx;
    corosio::native_stream_file<corosio::epoll> f(ctx);
    if (auto ec = f.open("data.bin", corosio::file_base::read_only))
        co_return;

    char buf[4096];
    auto [ec, n] = co_await f.read_some(capy::mutable_buffer(buf, sizeof(buf)));
    if (ec)
        co_return;
}

Base Classes

Name

Description

stream_file

Reads and writes a file sequentially, from a coroutine.

Types

Name

Description

handle

Owns a platform‐specific I/O implementation and destroys it when the handle goes out of scope.

implementation

Defines the file operations a platform backend implements.

io_service

Constructs, closes, and destroys platform implementations on behalf of an I/O object. Platform backends implement this interface.

Member Functions

Name

Description

native_stream_file [constructor]

Constructors

operator=

Assignment operators

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 file.

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 file.

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

operator=

Assignment operators

do_read_some [virtual]

Dispatch read through implementation vtable.

do_write_some [virtual]

Dispatch write through implementation vtable.

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.

Template Parameters

Name

Description

Backend

A backend tag value (e.g., epoll, iocp).

See Also

stream_file, epoll_t, iocp_t

Created with MrDocs