write_at.hpp 32 KB

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