write_at.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. //
  2. // write_at.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_WRITE_AT_HPP
  11. #define ASIO_WRITE_AT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include "asio/async_result.hpp"
  18. #include "asio/detail/cstdint.hpp"
  19. #include "asio/error.hpp"
  20. #if !defined(ASIO_NO_EXTENSIONS)
  21. # include "asio/basic_streambuf_fwd.hpp"
  22. #endif // !defined(ASIO_NO_EXTENSIONS)
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. /**
  26. * @defgroup write_at asio::write_at
  27. *
  28. * @brief The @c write_at function is a composed operation that writes a
  29. * certain amount of data at a specified offset before returning.
  30. */
  31. /*@{*/
  32. /// Write all of the supplied data at the specified offset before returning.
  33. /**
  34. * This function is used to write a certain number of bytes of data to a random
  35. * access device at a specified offset. The call will block until one of the
  36. * following conditions is true:
  37. *
  38. * @li All of the data in the supplied buffers has been written. That is, the
  39. * bytes transferred is equal to the sum of the buffer sizes.
  40. *
  41. * @li An error occurred.
  42. *
  43. * This operation is implemented in terms of zero or more calls to the device's
  44. * write_some_at function.
  45. *
  46. * @param d The device to which the data is to be written. The type must support
  47. * the SyncRandomAccessWriteDevice concept.
  48. *
  49. * @param offset The offset at which the data will be written.
  50. *
  51. * @param buffers One or more buffers containing the data to be written. The sum
  52. * of the buffer sizes indicates the maximum number of bytes to write to the
  53. * device.
  54. *
  55. * @returns The number of bytes transferred.
  56. *
  57. * @throws asio::system_error Thrown on failure.
  58. *
  59. * @par Example
  60. * To write a single data buffer use the @ref buffer function as follows:
  61. * @code asio::write_at(d, 42, asio::buffer(data, size)); @endcode
  62. * See the @ref buffer documentation for information on writing multiple
  63. * buffers in one go, and how to use it with arrays, boost::array or
  64. * std::vector.
  65. *
  66. * @note This overload is equivalent to calling:
  67. * @code asio::write_at(
  68. * d, offset, buffers,
  69. * asio::transfer_all()); @endcode
  70. */
  71. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  72. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  73. uint64_t offset, const ConstBufferSequence& buffers);
  74. /// Write all of the supplied data at the specified offset before returning.
  75. /**
  76. * This function is used to write a certain number of bytes of data to a random
  77. * access device at a specified offset. The call will block until one of the
  78. * following conditions is true:
  79. *
  80. * @li All of the data in the supplied buffers has been written. That is, the
  81. * bytes transferred is equal to the sum of the buffer sizes.
  82. *
  83. * @li An error occurred.
  84. *
  85. * This operation is implemented in terms of zero or more calls to the device's
  86. * write_some_at function.
  87. *
  88. * @param d The device to which the data is to be written. The type must support
  89. * the SyncRandomAccessWriteDevice concept.
  90. *
  91. * @param offset The offset at which the data will be written.
  92. *
  93. * @param buffers One or more buffers containing the data to be written. The sum
  94. * of the buffer sizes indicates the maximum number of bytes to write to the
  95. * device.
  96. *
  97. * @param ec Set to indicate what error occurred, if any.
  98. *
  99. * @returns The number of bytes transferred.
  100. *
  101. * @par Example
  102. * To write a single data buffer use the @ref buffer function as follows:
  103. * @code asio::write_at(d, 42,
  104. * asio::buffer(data, size), ec); @endcode
  105. * See the @ref buffer documentation for information on writing multiple
  106. * buffers in one go, and how to use it with arrays, boost::array or
  107. * std::vector.
  108. *
  109. * @note This overload is equivalent to calling:
  110. * @code asio::write_at(
  111. * d, offset, buffers,
  112. * asio::transfer_all(), ec); @endcode
  113. */
  114. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  115. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  116. uint64_t offset, const ConstBufferSequence& buffers,
  117. asio::error_code& ec);
  118. /// Write a certain amount of data at a specified offset before returning.
  119. /**
  120. * This function is used to write a certain number of bytes of data to a random
  121. * access device at a specified offset. The call will block until one of the
  122. * following conditions is true:
  123. *
  124. * @li All of the data in the supplied buffers has been written. That is, the
  125. * bytes transferred is equal to the sum of the buffer sizes.
  126. *
  127. * @li The completion_condition function object returns 0.
  128. *
  129. * This operation is implemented in terms of zero or more calls to the device's
  130. * write_some_at function.
  131. *
  132. * @param d The device to which the data is to be written. The type must support
  133. * the SyncRandomAccessWriteDevice concept.
  134. *
  135. * @param offset The offset at which the data will be written.
  136. *
  137. * @param buffers One or more buffers containing the data to be written. The sum
  138. * of the buffer sizes indicates the maximum number of bytes to write to the
  139. * device.
  140. *
  141. * @param completion_condition The function object to be called to determine
  142. * whether the write operation is complete. The signature of the function object
  143. * must be:
  144. * @code std::size_t completion_condition(
  145. * // Result of latest write_some_at operation.
  146. * const asio::error_code& error,
  147. *
  148. * // Number of bytes transferred so far.
  149. * std::size_t bytes_transferred
  150. * ); @endcode
  151. * A return value of 0 indicates that the write operation is complete. A
  152. * non-zero return value indicates the maximum number of bytes to be written on
  153. * the next call to the device's write_some_at function.
  154. *
  155. * @returns The number of bytes transferred.
  156. *
  157. * @throws asio::system_error Thrown on failure.
  158. *
  159. * @par Example
  160. * To write a single data buffer use the @ref buffer function as follows:
  161. * @code asio::write_at(d, 42, asio::buffer(data, size),
  162. * asio::transfer_at_least(32)); @endcode
  163. * See the @ref buffer documentation for information on writing multiple
  164. * buffers in one go, and how to use it with arrays, boost::array or
  165. * std::vector.
  166. */
  167. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  168. typename CompletionCondition>
  169. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  170. uint64_t offset, const ConstBufferSequence& buffers,
  171. CompletionCondition completion_condition);
  172. /// Write a certain amount of data at a specified offset before returning.
  173. /**
  174. * This function is used to write a certain number of bytes of data to a random
  175. * access device at a specified offset. The call will block until one of the
  176. * following conditions is true:
  177. *
  178. * @li All of the data in the supplied buffers has been written. That is, the
  179. * bytes transferred is equal to the sum of the buffer sizes.
  180. *
  181. * @li The completion_condition function object returns 0.
  182. *
  183. * This operation is implemented in terms of zero or more calls to the device's
  184. * write_some_at function.
  185. *
  186. * @param d The device to which the data is to be written. The type must support
  187. * the SyncRandomAccessWriteDevice concept.
  188. *
  189. * @param offset The offset at which the data will be written.
  190. *
  191. * @param buffers One or more buffers containing the data to be written. The sum
  192. * of the buffer sizes indicates the maximum number of bytes to write to the
  193. * device.
  194. *
  195. * @param completion_condition The function object to be called to determine
  196. * whether the write operation is complete. The signature of the function object
  197. * must be:
  198. * @code std::size_t completion_condition(
  199. * // Result of latest write_some_at operation.
  200. * const asio::error_code& error,
  201. *
  202. * // Number of bytes transferred so far.
  203. * std::size_t bytes_transferred
  204. * ); @endcode
  205. * A return value of 0 indicates that the write operation is complete. A
  206. * non-zero return value indicates the maximum number of bytes to be written on
  207. * the next call to the device's write_some_at function.
  208. *
  209. * @param ec Set to indicate what error occurred, if any.
  210. *
  211. * @returns The number of bytes written. If an error occurs, returns the total
  212. * number of bytes successfully transferred prior to the error.
  213. */
  214. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  215. typename CompletionCondition>
  216. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  217. uint64_t offset, const ConstBufferSequence& buffers,
  218. CompletionCondition completion_condition, asio::error_code& ec);
  219. #if !defined(ASIO_NO_EXTENSIONS)
  220. #if !defined(ASIO_NO_IOSTREAM)
  221. /// Write all of the supplied data at the specified offset before returning.
  222. /**
  223. * This function is used to write a certain number of bytes of data to a random
  224. * access device at a specified offset. The call will block until one of the
  225. * following conditions is true:
  226. *
  227. * @li All of the data in the supplied basic_streambuf has been written.
  228. *
  229. * @li An error occurred.
  230. *
  231. * This operation is implemented in terms of zero or more calls to the device's
  232. * write_some_at function.
  233. *
  234. * @param d The device to which the data is to be written. The type must support
  235. * the SyncRandomAccessWriteDevice concept.
  236. *
  237. * @param offset The offset at which the data will be written.
  238. *
  239. * @param b The basic_streambuf object from which data will be written.
  240. *
  241. * @returns The number of bytes transferred.
  242. *
  243. * @throws asio::system_error Thrown on failure.
  244. *
  245. * @note This overload is equivalent to calling:
  246. * @code asio::write_at(
  247. * d, 42, b,
  248. * asio::transfer_all()); @endcode
  249. */
  250. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  251. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  252. uint64_t offset, basic_streambuf<Allocator>& b);
  253. /// Write all of the supplied data at the specified offset before returning.
  254. /**
  255. * This function is used to write a certain number of bytes of data to a random
  256. * access device at a specified offset. The call will block until one of the
  257. * following conditions is true:
  258. *
  259. * @li All of the data in the supplied basic_streambuf has been written.
  260. *
  261. * @li An error occurred.
  262. *
  263. * This operation is implemented in terms of zero or more calls to the device's
  264. * write_some_at function.
  265. *
  266. * @param d The device to which the data is to be written. The type must support
  267. * the SyncRandomAccessWriteDevice concept.
  268. *
  269. * @param offset The offset at which the data will be written.
  270. *
  271. * @param b The basic_streambuf object from which data will be written.
  272. *
  273. * @param ec Set to indicate what error occurred, if any.
  274. *
  275. * @returns The number of bytes transferred.
  276. *
  277. * @note This overload is equivalent to calling:
  278. * @code asio::write_at(
  279. * d, 42, b,
  280. * asio::transfer_all(), ec); @endcode
  281. */
  282. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  283. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  284. uint64_t offset, basic_streambuf<Allocator>& b,
  285. asio::error_code& ec);
  286. /// Write a certain amount of data at a specified offset before returning.
  287. /**
  288. * This function is used to write a certain number of bytes of data to a random
  289. * access device at a specified offset. The call will block until one of the
  290. * following conditions is true:
  291. *
  292. * @li All of the data in the supplied basic_streambuf has been written.
  293. *
  294. * @li The completion_condition function object returns 0.
  295. *
  296. * This operation is implemented in terms of zero or more calls to the device's
  297. * write_some_at function.
  298. *
  299. * @param d The device to which the data is to be written. The type must support
  300. * the SyncRandomAccessWriteDevice concept.
  301. *
  302. * @param offset The offset at which the data will be written.
  303. *
  304. * @param b The basic_streambuf object from which data will be written.
  305. *
  306. * @param completion_condition The function object to be called to determine
  307. * whether the write operation is complete. The signature of the function object
  308. * must be:
  309. * @code std::size_t completion_condition(
  310. * // Result of latest write_some_at operation.
  311. * const asio::error_code& error,
  312. *
  313. * // Number of bytes transferred so far.
  314. * std::size_t bytes_transferred
  315. * ); @endcode
  316. * A return value of 0 indicates that the write operation is complete. A
  317. * non-zero return value indicates the maximum number of bytes to be written on
  318. * the next call to the device's write_some_at function.
  319. *
  320. * @returns The number of bytes transferred.
  321. *
  322. * @throws asio::system_error Thrown on failure.
  323. */
  324. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  325. typename CompletionCondition>
  326. std::size_t write_at(SyncRandomAccessWriteDevice& d, uint64_t offset,
  327. basic_streambuf<Allocator>& b, CompletionCondition completion_condition);
  328. /// Write a certain amount of data at a specified offset before returning.
  329. /**
  330. * This function is used to write a certain number of bytes of data to a random
  331. * access device at a specified offset. The call will block until one of the
  332. * following conditions is true:
  333. *
  334. * @li All of the data in the supplied basic_streambuf has been written.
  335. *
  336. * @li The completion_condition function object returns 0.
  337. *
  338. * This operation is implemented in terms of zero or more calls to the device's
  339. * write_some_at function.
  340. *
  341. * @param d The device to which the data is to be written. The type must support
  342. * the SyncRandomAccessWriteDevice concept.
  343. *
  344. * @param offset The offset at which the data will be written.
  345. *
  346. * @param b The basic_streambuf object from which data will be written.
  347. *
  348. * @param completion_condition The function object to be called to determine
  349. * whether the write operation is complete. The signature of the function object
  350. * must be:
  351. * @code std::size_t completion_condition(
  352. * // Result of latest write_some_at operation.
  353. * const asio::error_code& error,
  354. *
  355. * // Number of bytes transferred so far.
  356. * std::size_t bytes_transferred
  357. * ); @endcode
  358. * A return value of 0 indicates that the write operation is complete. A
  359. * non-zero return value indicates the maximum number of bytes to be written on
  360. * the next call to the device's write_some_at function.
  361. *
  362. * @param ec Set to indicate what error occurred, if any.
  363. *
  364. * @returns The number of bytes written. If an error occurs, returns the total
  365. * number of bytes successfully transferred prior to the error.
  366. */
  367. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  368. typename CompletionCondition>
  369. std::size_t write_at(SyncRandomAccessWriteDevice& d, uint64_t offset,
  370. basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
  371. asio::error_code& ec);
  372. #endif // !defined(ASIO_NO_IOSTREAM)
  373. #endif // !defined(ASIO_NO_EXTENSIONS)
  374. /*@}*/
  375. /**
  376. * @defgroup async_write_at asio::async_write_at
  377. *
  378. * @brief The @c async_write_at function is a composed asynchronous operation
  379. * that writes a certain amount of data at the specified offset before
  380. * completion.
  381. */
  382. /*@{*/
  383. /// Start an asynchronous operation to write all of the supplied data at the
  384. /// specified offset.
  385. /**
  386. * This function is used to asynchronously write a certain number of bytes of
  387. * data to a random access device at a specified offset. It is an initiating
  388. * function for an @ref asynchronous_operation, and always returns immediately.
  389. * The asynchronous operation will continue until one of the following
  390. * conditions is true:
  391. *
  392. * @li All of the data in the supplied buffers has been written. That is, the
  393. * bytes transferred is equal to the sum of the buffer sizes.
  394. *
  395. * @li An error occurred.
  396. *
  397. * This operation is implemented in terms of zero or more calls to the device's
  398. * async_write_some_at function, and is known as a <em>composed operation</em>.
  399. * The program must ensure that the device performs no <em>overlapping</em>
  400. * write operations (such as async_write_at, the device's async_write_some_at
  401. * function, or any other composed operations that perform writes) until this
  402. * operation completes. Operations are overlapping if the regions defined by
  403. * their offsets, and the numbers of bytes to write, intersect.
  404. *
  405. * @param d The device to which the data is to be written. The type must support
  406. * the AsyncRandomAccessWriteDevice concept.
  407. *
  408. * @param offset The offset at which the data will be written.
  409. *
  410. * @param buffers One or more buffers containing the data to be written.
  411. * Although the buffers object may be copied as necessary, ownership of the
  412. * underlying memory blocks is retained by the caller, which must guarantee
  413. * that they remain valid until the completion handler is called.
  414. *
  415. * @param token The @ref completion_token that will be used to produce a
  416. * completion handler, which will be called when the write completes.
  417. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  418. * @ref yield_context, or a function object with the correct completion
  419. * signature. The function signature of the completion handler must be:
  420. * @code void handler(
  421. * // Result of operation.
  422. * const asio::error_code& error,
  423. *
  424. * // Number of bytes written from the buffers. If an error
  425. * // occurred, this will be less than the sum of the buffer sizes.
  426. * std::size_t bytes_transferred
  427. * ); @endcode
  428. * Regardless of whether the asynchronous operation completes immediately or
  429. * not, the completion handler will not be invoked from within this function.
  430. * On immediate completion, invocation of the handler will be performed in a
  431. * manner equivalent to using asio::post().
  432. *
  433. * @par Completion Signature
  434. * @code void(asio::error_code, std::size_t) @endcode
  435. *
  436. * @par Example
  437. * To write a single data buffer use the @ref buffer function as follows:
  438. * @code
  439. * asio::async_write_at(d, 42, asio::buffer(data, size), handler);
  440. * @endcode
  441. * See the @ref buffer documentation for information on writing multiple
  442. * buffers in one go, and how to use it with arrays, boost::array or
  443. * std::vector.
  444. *
  445. * @par Per-Operation Cancellation
  446. * This asynchronous operation supports cancellation for the following
  447. * asio::cancellation_type values:
  448. *
  449. * @li @c cancellation_type::terminal
  450. *
  451. * @li @c cancellation_type::partial
  452. *
  453. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  454. * async_write_some_at operation.
  455. */
  456. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  457. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  458. std::size_t)) WriteToken
  459. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  460. typename AsyncRandomAccessWriteDevice::executor_type)>
  461. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  462. void (asio::error_code, std::size_t))
  463. async_write_at(AsyncRandomAccessWriteDevice& d, uint64_t offset,
  464. const ConstBufferSequence& buffers,
  465. ASIO_MOVE_ARG(WriteToken) token
  466. ASIO_DEFAULT_COMPLETION_TOKEN(
  467. typename AsyncRandomAccessWriteDevice::executor_type));
  468. /// Start an asynchronous operation to write a certain amount of data at the
  469. /// specified offset.
  470. /**
  471. * This function is used to asynchronously write a certain number of bytes of
  472. * data to a random access device at a specified offset. It is an initiating
  473. * function for an @ref asynchronous_operation, and always returns immediately.
  474. * The asynchronous operation will continue until one of the following
  475. * conditions is true:
  476. *
  477. * @li All of the data in the supplied buffers has been written. That is, the
  478. * bytes transferred is equal to the sum of the buffer sizes.
  479. *
  480. * @li The completion_condition function object returns 0.
  481. *
  482. * This operation is implemented in terms of zero or more calls to the device's
  483. * async_write_some_at function, and is known as a <em>composed operation</em>.
  484. * The program must ensure that the device performs no <em>overlapping</em>
  485. * write operations (such as async_write_at, the device's async_write_some_at
  486. * function, or any other composed operations that perform writes) until this
  487. * operation completes. Operations are overlapping if the regions defined by
  488. * their offsets, and the numbers of bytes to write, intersect.
  489. *
  490. * @param d The device to which the data is to be written. The type must support
  491. * the AsyncRandomAccessWriteDevice concept.
  492. *
  493. * @param offset The offset at which the data will be written.
  494. *
  495. * @param buffers One or more buffers containing the data to be written.
  496. * Although the buffers object may be copied as necessary, ownership of the
  497. * underlying memory blocks is retained by the caller, which must guarantee
  498. * that they remain valid until the completion handler is called.
  499. *
  500. * @param completion_condition The function object to be called to determine
  501. * whether the write operation is complete. The signature of the function object
  502. * must be:
  503. * @code std::size_t completion_condition(
  504. * // Result of latest async_write_some_at operation.
  505. * const asio::error_code& error,
  506. *
  507. * // Number of bytes transferred so far.
  508. * std::size_t bytes_transferred
  509. * ); @endcode
  510. * A return value of 0 indicates that the write operation is complete. A
  511. * non-zero return value indicates the maximum number of bytes to be written on
  512. * the next call to the device's async_write_some_at function.
  513. *
  514. * @param token The @ref completion_token that will be used to produce a
  515. * completion handler, which will be called when the write completes.
  516. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  517. * @ref yield_context, or a function object with the correct completion
  518. * signature. The function signature of the completion handler must be:
  519. * @code void handler(
  520. * // Result of operation.
  521. * const asio::error_code& error,
  522. *
  523. * // Number of bytes written from the buffers. If an error
  524. * // occurred, this will be less than the sum of the buffer sizes.
  525. * std::size_t bytes_transferred
  526. * ); @endcode
  527. * Regardless of whether the asynchronous operation completes immediately or
  528. * not, the completion handler will not be invoked from within this function.
  529. * On immediate completion, invocation of the handler will be performed in a
  530. * manner equivalent to using asio::post().
  531. *
  532. * @par Completion Signature
  533. * @code void(asio::error_code, std::size_t) @endcode
  534. *
  535. * @par Example
  536. * To write a single data buffer use the @ref buffer function as follows:
  537. * @code asio::async_write_at(d, 42,
  538. * asio::buffer(data, size),
  539. * asio::transfer_at_least(32),
  540. * handler); @endcode
  541. * See the @ref buffer documentation for information on writing multiple
  542. * buffers in one go, and how to use it with arrays, boost::array or
  543. * std::vector.
  544. *
  545. * @par Per-Operation Cancellation
  546. * This asynchronous operation supports cancellation for the following
  547. * asio::cancellation_type values:
  548. *
  549. * @li @c cancellation_type::terminal
  550. *
  551. * @li @c cancellation_type::partial
  552. *
  553. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  554. * async_write_some_at operation.
  555. */
  556. template <typename AsyncRandomAccessWriteDevice,
  557. typename ConstBufferSequence, typename CompletionCondition,
  558. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  559. std::size_t)) WriteToken
  560. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  561. typename AsyncRandomAccessWriteDevice::executor_type)>
  562. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  563. void (asio::error_code, std::size_t))
  564. async_write_at(AsyncRandomAccessWriteDevice& d,
  565. uint64_t offset, const ConstBufferSequence& buffers,
  566. CompletionCondition completion_condition,
  567. ASIO_MOVE_ARG(WriteToken) token
  568. ASIO_DEFAULT_COMPLETION_TOKEN(
  569. typename AsyncRandomAccessWriteDevice::executor_type));
  570. #if !defined(ASIO_NO_EXTENSIONS)
  571. #if !defined(ASIO_NO_IOSTREAM)
  572. /// Start an asynchronous operation to write all of the supplied data at the
  573. /// specified offset.
  574. /**
  575. * This function is used to asynchronously write a certain number of bytes of
  576. * data to a random access device at a specified offset. It is an initiating
  577. * function for an @ref asynchronous_operation, and always returns immediately.
  578. * The asynchronous operation will continue until one of the following
  579. * conditions is true:
  580. *
  581. * @li All of the data in the supplied basic_streambuf has been written.
  582. *
  583. * @li An error occurred.
  584. *
  585. * This operation is implemented in terms of zero or more calls to the device's
  586. * async_write_some_at function, and is known as a <em>composed operation</em>.
  587. * The program must ensure that the device performs no <em>overlapping</em>
  588. * write operations (such as async_write_at, the device's async_write_some_at
  589. * function, or any other composed operations that perform writes) until this
  590. * operation completes. Operations are overlapping if the regions defined by
  591. * their offsets, and the numbers of bytes to write, intersect.
  592. *
  593. * @param d The device to which the data is to be written. The type must support
  594. * the AsyncRandomAccessWriteDevice concept.
  595. *
  596. * @param offset The offset at which the data will be written.
  597. *
  598. * @param b A basic_streambuf object from which data will be written. Ownership
  599. * of the streambuf is retained by the caller, which must guarantee that it
  600. * remains valid until the completion handler is called.
  601. *
  602. * @param token The @ref completion_token that will be used to produce a
  603. * completion handler, which will be called when the write completes.
  604. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  605. * @ref yield_context, or a function object with the correct completion
  606. * signature. The function signature of the completion handler must be:
  607. * @code void handler(
  608. * // Result of operation.
  609. * const asio::error_code& error,
  610. *
  611. * // Number of bytes written from the buffers. If an error
  612. * // occurred, this will be less than the sum of the buffer sizes.
  613. * std::size_t bytes_transferred
  614. * ); @endcode
  615. * Regardless of whether the asynchronous operation completes immediately or
  616. * not, the completion handler will not be invoked from within this function.
  617. * On immediate completion, invocation of the handler will be performed in a
  618. * manner equivalent to using asio::post().
  619. *
  620. * @par Completion Signature
  621. * @code void(asio::error_code, std::size_t) @endcode
  622. *
  623. * @par Per-Operation Cancellation
  624. * This asynchronous operation supports cancellation for the following
  625. * asio::cancellation_type values:
  626. *
  627. * @li @c cancellation_type::terminal
  628. *
  629. * @li @c cancellation_type::partial
  630. *
  631. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  632. * async_write_some_at operation.
  633. */
  634. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  635. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  636. std::size_t)) WriteToken
  637. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  638. typename AsyncRandomAccessWriteDevice::executor_type)>
  639. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  640. void (asio::error_code, std::size_t))
  641. async_write_at(AsyncRandomAccessWriteDevice& d,
  642. uint64_t offset, basic_streambuf<Allocator>& b,
  643. ASIO_MOVE_ARG(WriteToken) token
  644. ASIO_DEFAULT_COMPLETION_TOKEN(
  645. typename AsyncRandomAccessWriteDevice::executor_type));
  646. /// Start an asynchronous operation to write a certain amount of data at the
  647. /// specified offset.
  648. /**
  649. * This function is used to asynchronously write a certain number of bytes of
  650. * data to a random access device at a specified offset. It is an initiating
  651. * function for an @ref asynchronous_operation, and always returns immediately.
  652. * The asynchronous operation will continue until one of the following
  653. * conditions is true:
  654. *
  655. * @li All of the data in the supplied basic_streambuf has been written.
  656. *
  657. * @li The completion_condition function object returns 0.
  658. *
  659. * This operation is implemented in terms of zero or more calls to the device's
  660. * async_write_some_at function, and is known as a <em>composed operation</em>.
  661. * The program must ensure that the device performs no <em>overlapping</em>
  662. * write operations (such as async_write_at, the device's async_write_some_at
  663. * function, or any other composed operations that perform writes) until this
  664. * operation completes. Operations are overlapping if the regions defined by
  665. * their offsets, and the numbers of bytes to write, intersect.
  666. *
  667. * @param d The device to which the data is to be written. The type must support
  668. * the AsyncRandomAccessWriteDevice concept.
  669. *
  670. * @param offset The offset at which the data will be written.
  671. *
  672. * @param b A basic_streambuf object from which data will be written. Ownership
  673. * of the streambuf is retained by the caller, which must guarantee that it
  674. * remains valid until the completion handler is called.
  675. *
  676. * @param completion_condition The function object to be called to determine
  677. * whether the write operation is complete. The signature of the function object
  678. * must be:
  679. * @code std::size_t completion_condition(
  680. * // Result of latest async_write_some_at operation.
  681. * const asio::error_code& error,
  682. *
  683. * // Number of bytes transferred so far.
  684. * std::size_t bytes_transferred
  685. * ); @endcode
  686. * A return value of 0 indicates that the write operation is complete. A
  687. * non-zero return value indicates the maximum number of bytes to be written on
  688. * the next call to the device's async_write_some_at function.
  689. *
  690. * @param token The @ref completion_token that will be used to produce a
  691. * completion handler, which will be called when the write completes.
  692. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  693. * @ref yield_context, or a function object with the correct completion
  694. * signature. The function signature of the completion handler must be:
  695. * @code void handler(
  696. * // Result of operation.
  697. * const asio::error_code& error,
  698. *
  699. * // Number of bytes written from the buffers. If an error
  700. * // occurred, this will be less than the sum of the buffer sizes.
  701. * std::size_t bytes_transferred
  702. * ); @endcode
  703. * Regardless of whether the asynchronous operation completes immediately or
  704. * not, the completion handler will not be invoked from within this function.
  705. * On immediate completion, invocation of the handler will be performed in a
  706. * manner equivalent to using asio::post().
  707. *
  708. * @par Completion Signature
  709. * @code void(asio::error_code, std::size_t) @endcode
  710. *
  711. * @par Per-Operation Cancellation
  712. * This asynchronous operation supports cancellation for the following
  713. * asio::cancellation_type values:
  714. *
  715. * @li @c cancellation_type::terminal
  716. *
  717. * @li @c cancellation_type::partial
  718. *
  719. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  720. * async_write_some_at operation.
  721. */
  722. template <typename AsyncRandomAccessWriteDevice,
  723. typename Allocator, typename CompletionCondition,
  724. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  725. std::size_t)) WriteToken
  726. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  727. typename AsyncRandomAccessWriteDevice::executor_type)>
  728. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  729. void (asio::error_code, std::size_t))
  730. async_write_at(AsyncRandomAccessWriteDevice& d, uint64_t offset,
  731. basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
  732. ASIO_MOVE_ARG(WriteToken) token
  733. ASIO_DEFAULT_COMPLETION_TOKEN(
  734. typename AsyncRandomAccessWriteDevice::executor_type));
  735. #endif // !defined(ASIO_NO_IOSTREAM)
  736. #endif // !defined(ASIO_NO_EXTENSIONS)
  737. /*@}*/
  738. } // namespace asio
  739. #include "asio/detail/pop_options.hpp"
  740. #include "asio/impl/write_at.hpp"
  741. #endif // ASIO_WRITE_AT_HPP