write_at.hpp 32 KB

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