write_at.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 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
  467. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  468. typename AsyncRandomAccessWriteDevice::executor_type)>
  469. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(WriteToken,
  470. void (boost::system::error_code, std::size_t))
  471. async_write_at(AsyncRandomAccessWriteDevice& d, uint64_t offset,
  472. const ConstBufferSequence& buffers,
  473. BOOST_ASIO_MOVE_ARG(WriteToken) token
  474. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  475. typename AsyncRandomAccessWriteDevice::executor_type))
  476. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  477. async_initiate<WriteToken,
  478. void (boost::system::error_code, std::size_t)>(
  479. declval<detail::initiate_async_write_at<
  480. AsyncRandomAccessWriteDevice> >(),
  481. token, offset, buffers, transfer_all())));
  482. /// Start an asynchronous operation to write a certain amount of data at the
  483. /// specified offset.
  484. /**
  485. * This function is used to asynchronously write a certain number of bytes of
  486. * data to a random access device at a specified offset. It is an initiating
  487. * function for an @ref asynchronous_operation, and always returns immediately.
  488. * The asynchronous operation will continue until one of the following
  489. * conditions is true:
  490. *
  491. * @li All of the data in the supplied buffers has been written. That is, the
  492. * bytes transferred is equal to the sum of the buffer sizes.
  493. *
  494. * @li The completion_condition function object returns 0.
  495. *
  496. * This operation is implemented in terms of zero or more calls to the device's
  497. * async_write_some_at function, and is known as a <em>composed operation</em>.
  498. * The program must ensure that the device performs no <em>overlapping</em>
  499. * write operations (such as async_write_at, the device's async_write_some_at
  500. * function, or any other composed operations that perform writes) until this
  501. * operation completes. Operations are overlapping if the regions defined by
  502. * their offsets, and the numbers of bytes to write, intersect.
  503. *
  504. * @param d The device to which the data is to be written. The type must support
  505. * the AsyncRandomAccessWriteDevice concept.
  506. *
  507. * @param offset The offset at which the data will be written.
  508. *
  509. * @param buffers One or more buffers containing the data to be written.
  510. * Although the buffers object may be copied as necessary, ownership of the
  511. * underlying memory blocks is retained by the caller, which must guarantee
  512. * that they remain valid until the completion handler is called.
  513. *
  514. * @param completion_condition The function object to be called to determine
  515. * whether the write operation is complete. The signature of the function object
  516. * must be:
  517. * @code std::size_t completion_condition(
  518. * // Result of latest async_write_some_at operation.
  519. * const boost::system::error_code& error,
  520. *
  521. * // Number of bytes transferred so far.
  522. * std::size_t bytes_transferred
  523. * ); @endcode
  524. * A return value of 0 indicates that the write operation is complete. A
  525. * non-zero return value indicates the maximum number of bytes to be written on
  526. * the next call to the device's async_write_some_at function.
  527. *
  528. * @param token The @ref completion_token that will be used to produce a
  529. * completion handler, which will be called when the write completes.
  530. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  531. * @ref yield_context, or a function object with the correct completion
  532. * signature. The function signature of the completion handler must be:
  533. * @code void handler(
  534. * // Result of operation.
  535. * const boost::system::error_code& error,
  536. *
  537. * // Number of bytes written from the buffers. If an error
  538. * // occurred, this will be less than the sum of the buffer sizes.
  539. * std::size_t bytes_transferred
  540. * ); @endcode
  541. * Regardless of whether the asynchronous operation completes immediately or
  542. * not, the completion handler will not be invoked from within this function.
  543. * On immediate completion, invocation of the handler will be performed in a
  544. * manner equivalent to using boost::asio::post().
  545. *
  546. * @par Completion Signature
  547. * @code void(boost::system::error_code, std::size_t) @endcode
  548. *
  549. * @par Example
  550. * To write a single data buffer use the @ref buffer function as follows:
  551. * @code boost::asio::async_write_at(d, 42,
  552. * boost::asio::buffer(data, size),
  553. * boost::asio::transfer_at_least(32),
  554. * handler); @endcode
  555. * See the @ref buffer documentation for information on writing multiple
  556. * buffers in one go, and how to use it with arrays, boost::array or
  557. * std::vector.
  558. *
  559. * @par Per-Operation Cancellation
  560. * This asynchronous operation supports cancellation for the following
  561. * boost::asio::cancellation_type values:
  562. *
  563. * @li @c cancellation_type::terminal
  564. *
  565. * @li @c cancellation_type::partial
  566. *
  567. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  568. * async_write_some_at operation.
  569. */
  570. template <typename AsyncRandomAccessWriteDevice,
  571. typename ConstBufferSequence, typename CompletionCondition,
  572. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  573. std::size_t)) WriteToken
  574. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  575. typename AsyncRandomAccessWriteDevice::executor_type)>
  576. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(WriteToken,
  577. void (boost::system::error_code, std::size_t))
  578. async_write_at(AsyncRandomAccessWriteDevice& d,
  579. uint64_t offset, const ConstBufferSequence& buffers,
  580. CompletionCondition completion_condition,
  581. BOOST_ASIO_MOVE_ARG(WriteToken) token
  582. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  583. typename AsyncRandomAccessWriteDevice::executor_type))
  584. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  585. async_initiate<WriteToken,
  586. void (boost::system::error_code, std::size_t)>(
  587. declval<detail::initiate_async_write_at<
  588. AsyncRandomAccessWriteDevice> >(),
  589. token, offset, buffers,
  590. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition))));
  591. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  592. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  593. /// Start an asynchronous operation to write all of the supplied data at the
  594. /// specified offset.
  595. /**
  596. * This function is used to asynchronously write a certain number of bytes of
  597. * data to a random access device at a specified offset. It is an initiating
  598. * function for an @ref asynchronous_operation, and always returns immediately.
  599. * The asynchronous operation will continue until one of the following
  600. * conditions is true:
  601. *
  602. * @li All of the data in the supplied basic_streambuf has been written.
  603. *
  604. * @li An error occurred.
  605. *
  606. * This operation is implemented in terms of zero or more calls to the device's
  607. * async_write_some_at function, and is known as a <em>composed operation</em>.
  608. * The program must ensure that the device performs no <em>overlapping</em>
  609. * write operations (such as async_write_at, the device's async_write_some_at
  610. * function, or any other composed operations that perform writes) until this
  611. * operation completes. Operations are overlapping if the regions defined by
  612. * their offsets, and the numbers of bytes to write, intersect.
  613. *
  614. * @param d The device to which the data is to be written. The type must support
  615. * the AsyncRandomAccessWriteDevice concept.
  616. *
  617. * @param offset The offset at which the data will be written.
  618. *
  619. * @param b A basic_streambuf object from which data will be written. Ownership
  620. * of the streambuf is retained by the caller, which must guarantee that it
  621. * remains valid until the completion handler is called.
  622. *
  623. * @param token The @ref completion_token that will be used to produce a
  624. * completion handler, which will be called when the write completes.
  625. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  626. * @ref yield_context, or a function object with the correct completion
  627. * signature. The function signature of the completion handler must be:
  628. * @code void handler(
  629. * // Result of operation.
  630. * const boost::system::error_code& error,
  631. *
  632. * // Number of bytes written from the buffers. If an error
  633. * // occurred, this will be less than the sum of the buffer sizes.
  634. * std::size_t bytes_transferred
  635. * ); @endcode
  636. * Regardless of whether the asynchronous operation completes immediately or
  637. * not, the completion handler will not be invoked from within this function.
  638. * On immediate completion, invocation of the handler will be performed in a
  639. * manner equivalent to using boost::asio::post().
  640. *
  641. * @par Completion Signature
  642. * @code void(boost::system::error_code, std::size_t) @endcode
  643. *
  644. * @par Per-Operation Cancellation
  645. * This asynchronous operation supports cancellation for the following
  646. * boost::asio::cancellation_type values:
  647. *
  648. * @li @c cancellation_type::terminal
  649. *
  650. * @li @c cancellation_type::partial
  651. *
  652. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  653. * async_write_some_at operation.
  654. */
  655. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  656. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  657. std::size_t)) WriteToken
  658. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  659. typename AsyncRandomAccessWriteDevice::executor_type)>
  660. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(WriteToken,
  661. void (boost::system::error_code, std::size_t))
  662. async_write_at(AsyncRandomAccessWriteDevice& d,
  663. uint64_t offset, basic_streambuf<Allocator>& b,
  664. BOOST_ASIO_MOVE_ARG(WriteToken) token
  665. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  666. typename AsyncRandomAccessWriteDevice::executor_type))
  667. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  668. async_initiate<WriteToken,
  669. void (boost::system::error_code, std::size_t)>(
  670. declval<detail::initiate_async_write_at_streambuf<
  671. AsyncRandomAccessWriteDevice> >(),
  672. token, offset, &b, transfer_all())));
  673. /// Start an asynchronous operation to write a certain amount of data at the
  674. /// specified offset.
  675. /**
  676. * This function is used to asynchronously write a certain number of bytes of
  677. * data to a random access device at a specified offset. It is an initiating
  678. * function for an @ref asynchronous_operation, and always returns immediately.
  679. * The asynchronous operation will continue until one of the following
  680. * conditions is true:
  681. *
  682. * @li All of the data in the supplied basic_streambuf has been written.
  683. *
  684. * @li The completion_condition function object returns 0.
  685. *
  686. * This operation is implemented in terms of zero or more calls to the device's
  687. * async_write_some_at function, and is known as a <em>composed operation</em>.
  688. * The program must ensure that the device performs no <em>overlapping</em>
  689. * write operations (such as async_write_at, the device's async_write_some_at
  690. * function, or any other composed operations that perform writes) until this
  691. * operation completes. Operations are overlapping if the regions defined by
  692. * their offsets, and the numbers of bytes to write, intersect.
  693. *
  694. * @param d The device to which the data is to be written. The type must support
  695. * the AsyncRandomAccessWriteDevice concept.
  696. *
  697. * @param offset The offset at which the data will be written.
  698. *
  699. * @param b A basic_streambuf object from which data will be written. Ownership
  700. * of the streambuf is retained by the caller, which must guarantee that it
  701. * remains valid until the completion handler is called.
  702. *
  703. * @param completion_condition The function object to be called to determine
  704. * whether the write operation is complete. The signature of the function object
  705. * must be:
  706. * @code std::size_t completion_condition(
  707. * // Result of latest async_write_some_at operation.
  708. * const boost::system::error_code& error,
  709. *
  710. * // Number of bytes transferred so far.
  711. * std::size_t bytes_transferred
  712. * ); @endcode
  713. * A return value of 0 indicates that the write operation is complete. A
  714. * non-zero return value indicates the maximum number of bytes to be written on
  715. * the next call to the device's async_write_some_at function.
  716. *
  717. * @param token The @ref completion_token that will be used to produce a
  718. * completion handler, which will be called when the write completes.
  719. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  720. * @ref yield_context, or a function object with the correct completion
  721. * signature. The function signature of the completion handler must be:
  722. * @code void handler(
  723. * // Result of operation.
  724. * const boost::system::error_code& error,
  725. *
  726. * // Number of bytes written from the buffers. If an error
  727. * // occurred, this will be less than the sum of the buffer sizes.
  728. * std::size_t bytes_transferred
  729. * ); @endcode
  730. * Regardless of whether the asynchronous operation completes immediately or
  731. * not, the completion handler will not be invoked from within this function.
  732. * On immediate completion, invocation of the handler will be performed in a
  733. * manner equivalent to using boost::asio::post().
  734. *
  735. * @par Completion Signature
  736. * @code void(boost::system::error_code, std::size_t) @endcode
  737. *
  738. * @par Per-Operation Cancellation
  739. * This asynchronous operation supports cancellation for the following
  740. * boost::asio::cancellation_type values:
  741. *
  742. * @li @c cancellation_type::terminal
  743. *
  744. * @li @c cancellation_type::partial
  745. *
  746. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  747. * async_write_some_at operation.
  748. */
  749. template <typename AsyncRandomAccessWriteDevice,
  750. typename Allocator, typename CompletionCondition,
  751. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  752. std::size_t)) WriteToken
  753. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  754. typename AsyncRandomAccessWriteDevice::executor_type)>
  755. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(WriteToken,
  756. void (boost::system::error_code, std::size_t))
  757. async_write_at(AsyncRandomAccessWriteDevice& d, uint64_t offset,
  758. basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
  759. BOOST_ASIO_MOVE_ARG(WriteToken) token
  760. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  761. typename AsyncRandomAccessWriteDevice::executor_type))
  762. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  763. async_initiate<WriteToken,
  764. void (boost::system::error_code, std::size_t)>(
  765. declval<detail::initiate_async_write_at_streambuf<
  766. AsyncRandomAccessWriteDevice> >(),
  767. token, offset, &b,
  768. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition))));
  769. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  770. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  771. /*@}*/
  772. } // namespace asio
  773. } // namespace boost
  774. #include <boost/asio/detail/pop_options.hpp>
  775. #include <boost/asio/impl/write_at.hpp>
  776. #endif // BOOST_ASIO_WRITE_AT_HPP