write_at.hpp 32 KB

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