Boost C++ 库

...是世界上备受推崇且设计精湛的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ Coding Standards

async_write_at (4 of 4 overloads) - Boost C++ 函数库
PrevUpHomeNext

启动异步操作,在指定偏移量处写入一定量的数据。

template<
    typename AsyncRandomAccessWriteDevice,
    typename Allocator,
    typename CompletionCondition,
    typename WriteToken = default_completion_token_t<        typename AsyncRandomAccessWriteDevice::executor_type>>
DEDUCED async_write_at(
    AsyncRandomAccessWriteDevice & d,
    uint64_t offset,
    basic_streambuf< Allocator > & b,
    CompletionCondition completion_condition,
    WriteToken && token = default_completion_token_t< typename AsyncRandomAccessWriteDevice::executor_type >(),
    constraint_t< is_completion_condition< CompletionCondition >::value >  = 0);

此函数用于将指定数量的字节数据异步写入到随机访问设备上的指定偏移量处。它是一个 异步操作 的发起函数,并且总是立即返回。异步操作将一直进行,直到满足以下条件之一:

  • 已写入提供的 basic_streambuf 中的所有数据。
  • completion_condition 函数对象返回 0。

此操作是通过零次或多次调用设备的 async_write_some_at 函数来实现的,被称为 组合操作。程序必须确保设备在此操作完成之前不执行 重叠 的写操作(例如 async_write_at、设备的 async_write_some_at 函数,或其他执行写的组合操作)。如果操作定义的区域(由它们的偏移量和要写入的字节数决定)相交,则这些操作是重叠的。

参数

d

要写入_数据的设备_。类型必须支持 AsyncRandomAccessWriteDevice 概念。

offset

将要写入_数据的偏移量_。

b

一个 basic_streambuf 对象,数据将从中写入。streambuf 的所有权由调用者保留,调用者必须保证它在完成处理程序被调用之前一直有效。

completion_condition

用于确定写入操作是否完成的函数对象。函数对象的签名必须是:

std::size_t completion_condition(
  // Result of latest async_write_some_at operation.
  const boost::system::error_code& error,

  // Number of bytes transferred so far.
  std::size_t bytes_transferred
);

返回值为 0 表示写操作已完成。非零返回值表示下一次调用设备的 async_write_some_at 函数时要写入的最大字节数。

标记

将用于生成完成处理程序的 完成令牌,当写操作完成时将调用此处理程序。可能的完成令牌包括 use_futureuse_awaitableyield_context,或者一个具有正确完成签名的函数对象。完成处理程序的函数签名必须是:

void handler(
  // Result of operation.
  const boost::system::error_code& error,

  // Number of bytes written from the buffers. If an error
  // occurred, this will be less than the sum of the buffer sizes.
  std::size_t bytes_transferred
);

无论异步操作是立即完成还是尚未完成,都不会在此函数内调用完成处理程序。在立即完成的情况下,处理程序的调用将通过等效于使用 async_immediate 的方式进行。

完成签名
void(boost::system::error_code, std::size_t)
按操作取消

此异步操作支持以下 cancellation_type 值的取消

  • cancellation_type::terminal
  • cancellation_type::partial

如果它们也由 AsyncRandomAccessWriteDevice 类型的 async_write_some_at 操作支持。


PrevUpHomeNext