write_at.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. constraint_t<
  181. is_completion_condition<CompletionCondition>::value
  182. > = 0);
  183. /// Write a certain amount of data at a specified offset before returning.
  184. /**
  185. * This function is used to write a certain number of bytes of data to a random
  186. * access device at a specified offset. The call will block until one of the
  187. * following conditions is true:
  188. *
  189. * @li All of the data in the supplied buffers has been written. That is, the
  190. * bytes transferred is equal to the sum of the buffer sizes.
  191. *
  192. * @li The completion_condition function object returns 0.
  193. *
  194. * This operation is implemented in terms of zero or more calls to the device's
  195. * write_some_at function.
  196. *
  197. * @param d The device to which the data is to be written. The type must support
  198. * the SyncRandomAccessWriteDevice concept.
  199. *
  200. * @param offset The offset at which the data will be written.
  201. *
  202. * @param buffers One or more buffers containing the data to be written. The sum
  203. * of the buffer sizes indicates the maximum number of bytes to write to the
  204. * device.
  205. *
  206. * @param completion_condition The function object to be called to determine
  207. * whether the write operation is complete. The signature of the function object
  208. * must be:
  209. * @code std::size_t completion_condition(
  210. * // Result of latest write_some_at operation.
  211. * const boost::system::error_code& error,
  212. *
  213. * // Number of bytes transferred so far.
  214. * std::size_t bytes_transferred
  215. * ); @endcode
  216. * A return value of 0 indicates that the write operation is complete. A
  217. * non-zero return value indicates the maximum number of bytes to be written on
  218. * the next call to the device's write_some_at function.
  219. *
  220. * @param ec Set to indicate what error occurred, if any.
  221. *
  222. * @returns The number of bytes written. If an error occurs, returns the total
  223. * number of bytes successfully transferred prior to the error.
  224. */
  225. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  226. typename CompletionCondition>
  227. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  228. uint64_t offset, const ConstBufferSequence& buffers,
  229. CompletionCondition completion_condition, boost::system::error_code& ec,
  230. constraint_t<
  231. is_completion_condition<CompletionCondition>::value
  232. > = 0);
  233. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  234. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  235. /// Write all of the supplied data at the specified offset before returning.
  236. /**
  237. * This function is used to write a certain number of bytes of data to a random
  238. * access device at a specified offset. The call will block until one of the
  239. * following conditions is true:
  240. *
  241. * @li All of the data in the supplied basic_streambuf has been written.
  242. *
  243. * @li An error occurred.
  244. *
  245. * This operation is implemented in terms of zero or more calls to the device's
  246. * write_some_at function.
  247. *
  248. * @param d The device to which the data is to be written. The type must support
  249. * the SyncRandomAccessWriteDevice concept.
  250. *
  251. * @param offset The offset at which the data will be written.
  252. *
  253. * @param b The basic_streambuf object from which data will be written.
  254. *
  255. * @returns The number of bytes transferred.
  256. *
  257. * @throws boost::system::system_error Thrown on failure.
  258. *
  259. * @note This overload is equivalent to calling:
  260. * @code boost::asio::write_at(
  261. * d, 42, b,
  262. * boost::asio::transfer_all()); @endcode
  263. */
  264. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  265. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  266. uint64_t offset, basic_streambuf<Allocator>& b);
  267. /// Write all of the supplied data at the specified offset before returning.
  268. /**
  269. * This function is used to write a certain number of bytes of data to a random
  270. * access device at a specified offset. The call will block until one of the
  271. * following conditions is true:
  272. *
  273. * @li All of the data in the supplied basic_streambuf has been written.
  274. *
  275. * @li An error occurred.
  276. *
  277. * This operation is implemented in terms of zero or more calls to the device's
  278. * write_some_at function.
  279. *
  280. * @param d The device to which the data is to be written. The type must support
  281. * the SyncRandomAccessWriteDevice concept.
  282. *
  283. * @param offset The offset at which the data will be written.
  284. *
  285. * @param b The basic_streambuf object from which data will be written.
  286. *
  287. * @param ec Set to indicate what error occurred, if any.
  288. *
  289. * @returns The number of bytes transferred.
  290. *
  291. * @note This overload is equivalent to calling:
  292. * @code boost::asio::write_at(
  293. * d, 42, b,
  294. * boost::asio::transfer_all(), ec); @endcode
  295. */
  296. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  297. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  298. uint64_t offset, basic_streambuf<Allocator>& b,
  299. boost::system::error_code& ec);
  300. /// Write a certain amount of data at a specified offset before returning.
  301. /**
  302. * This function is used to write a certain number of bytes of data to a random
  303. * access device at a specified offset. The call will block until one of the
  304. * following conditions is true:
  305. *
  306. * @li All of the data in the supplied basic_streambuf has been written.
  307. *
  308. * @li The completion_condition function object returns 0.
  309. *
  310. * This operation is implemented in terms of zero or more calls to the device's
  311. * write_some_at function.
  312. *
  313. * @param d The device to which the data is to be written. The type must support
  314. * the SyncRandomAccessWriteDevice concept.
  315. *
  316. * @param offset The offset at which the data will be written.
  317. *
  318. * @param b The basic_streambuf object from which data will be written.
  319. *
  320. * @param completion_condition The function object to be called to determine
  321. * whether the write operation is complete. The signature of the function object
  322. * must be:
  323. * @code std::size_t completion_condition(
  324. * // Result of latest write_some_at operation.
  325. * const boost::system::error_code& error,
  326. *
  327. * // Number of bytes transferred so far.
  328. * std::size_t bytes_transferred
  329. * ); @endcode
  330. * A return value of 0 indicates that the write operation is complete. A
  331. * non-zero return value indicates the maximum number of bytes to be written on
  332. * the next call to the device's write_some_at function.
  333. *
  334. * @returns The number of bytes transferred.
  335. *
  336. * @throws boost::system::system_error Thrown on failure.
  337. */
  338. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  339. typename CompletionCondition>
  340. std::size_t write_at(SyncRandomAccessWriteDevice& d, uint64_t offset,
  341. basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
  342. constraint_t<
  343. is_completion_condition<CompletionCondition>::value
  344. > = 0);
  345. /// Write a certain amount of data at a specified offset before returning.
  346. /**
  347. * This function is used to write a certain number of bytes of data to a random
  348. * access device at a specified offset. The call will block until one of the
  349. * following conditions is true:
  350. *
  351. * @li All of the data in the supplied basic_streambuf has been written.
  352. *
  353. * @li The completion_condition function object returns 0.
  354. *
  355. * This operation is implemented in terms of zero or more calls to the device's
  356. * write_some_at function.
  357. *
  358. * @param d The device to which the data is to be written. The type must support
  359. * the SyncRandomAccessWriteDevice concept.
  360. *
  361. * @param offset The offset at which the data will be written.
  362. *
  363. * @param b The basic_streambuf object from which data will be written.
  364. *
  365. * @param completion_condition The function object to be called to determine
  366. * whether the write operation is complete. The signature of the function object
  367. * must be:
  368. * @code std::size_t completion_condition(
  369. * // Result of latest write_some_at operation.
  370. * const boost::system::error_code& error,
  371. *
  372. * // Number of bytes transferred so far.
  373. * std::size_t bytes_transferred
  374. * ); @endcode
  375. * A return value of 0 indicates that the write operation is complete. A
  376. * non-zero return value indicates the maximum number of bytes to be written on
  377. * the next call to the device's write_some_at function.
  378. *
  379. * @param ec Set to indicate what error occurred, if any.
  380. *
  381. * @returns The number of bytes written. If an error occurs, returns the total
  382. * number of bytes successfully transferred prior to the error.
  383. */
  384. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  385. typename CompletionCondition>
  386. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  387. uint64_t offset, basic_streambuf<Allocator>& b,
  388. CompletionCondition completion_condition, boost::system::error_code& ec,
  389. constraint_t<
  390. is_completion_condition<CompletionCondition>::value
  391. > = 0);
  392. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  393. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  394. /*@}*/
  395. /**
  396. * @defgroup async_write_at boost::asio::async_write_at
  397. *
  398. * @brief The @c async_write_at function is a composed asynchronous operation
  399. * that writes a certain amount of data at the specified offset before
  400. * completion.
  401. */
  402. /*@{*/
  403. /// Start an asynchronous operation to write all of the supplied data at the
  404. /// specified offset.
  405. /**
  406. * This function is used to asynchronously write a certain number of bytes of
  407. * data to a random access device at a specified offset. It is an initiating
  408. * function for an @ref asynchronous_operation, and always returns immediately.
  409. * The asynchronous operation will continue until one of the following
  410. * conditions is true:
  411. *
  412. * @li All of the data in the supplied buffers has been written. That is, the
  413. * bytes transferred is equal to the sum of the buffer sizes.
  414. *
  415. * @li An error occurred.
  416. *
  417. * This operation is implemented in terms of zero or more calls to the device's
  418. * async_write_some_at function, and is known as a <em>composed operation</em>.
  419. * The program must ensure that the device performs no <em>overlapping</em>
  420. * write operations (such as async_write_at, the device's async_write_some_at
  421. * function, or any other composed operations that perform writes) until this
  422. * operation completes. Operations are overlapping if the regions defined by
  423. * their offsets, and the numbers of bytes to write, intersect.
  424. *
  425. * @param d The device to which the data is to be written. The type must support
  426. * the AsyncRandomAccessWriteDevice concept.
  427. *
  428. * @param offset The offset at which the data will be written.
  429. *
  430. * @param buffers One or more buffers containing the data to be written.
  431. * Although the buffers object may be copied as necessary, ownership of the
  432. * underlying memory blocks is retained by the caller, which must guarantee
  433. * that they remain valid until the completion handler is called.
  434. *
  435. * @param token The @ref completion_token that will be used to produce a
  436. * completion handler, which will be called when the write completes.
  437. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  438. * @ref yield_context, or a function object with the correct completion
  439. * signature. The function signature of the completion handler must be:
  440. * @code void handler(
  441. * // Result of operation.
  442. * const boost::system::error_code& error,
  443. *
  444. * // Number of bytes written from the buffers. If an error
  445. * // occurred, this will be less than the sum of the buffer sizes.
  446. * std::size_t bytes_transferred
  447. * ); @endcode
  448. * Regardless of whether the asynchronous operation completes immediately or
  449. * not, the completion handler will not be invoked from within this function.
  450. * On immediate completion, invocation of the handler will be performed in a
  451. * manner equivalent to using boost::asio::async_immediate().
  452. *
  453. * @par Completion Signature
  454. * @code void(boost::system::error_code, std::size_t) @endcode
  455. *
  456. * @par Example
  457. * To write a single data buffer use the @ref buffer function as follows:
  458. * @code
  459. * boost::asio::async_write_at(d, 42, boost::asio::buffer(data, size), handler);
  460. * @endcode
  461. * See the @ref buffer documentation for information on writing multiple
  462. * buffers in one go, and how to use it with arrays, boost::array or
  463. * std::vector.
  464. *
  465. * @par Per-Operation Cancellation
  466. * This asynchronous operation supports cancellation for the following
  467. * boost::asio::cancellation_type values:
  468. *
  469. * @li @c cancellation_type::terminal
  470. *
  471. * @li @c cancellation_type::partial
  472. *
  473. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  474. * async_write_some_at operation.
  475. */
  476. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  477. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  478. std::size_t)) WriteToken = default_completion_token_t<
  479. typename AsyncRandomAccessWriteDevice::executor_type>>
  480. inline auto async_write_at(AsyncRandomAccessWriteDevice& d,
  481. uint64_t offset, const ConstBufferSequence& buffers,
  482. WriteToken&& token = default_completion_token_t<
  483. typename AsyncRandomAccessWriteDevice::executor_type>(),
  484. constraint_t<
  485. !is_completion_condition<WriteToken>::value
  486. > = 0)
  487. -> decltype(
  488. async_initiate<WriteToken,
  489. void (boost::system::error_code, std::size_t)>(
  490. declval<detail::initiate_async_write_at<
  491. AsyncRandomAccessWriteDevice>>(),
  492. token, offset, buffers, transfer_all()))
  493. {
  494. return async_initiate<WriteToken,
  495. void (boost::system::error_code, std::size_t)>(
  496. detail::initiate_async_write_at<AsyncRandomAccessWriteDevice>(d),
  497. token, offset, buffers, transfer_all());
  498. }
  499. /// Start an asynchronous operation to write a certain amount of data at the
  500. /// specified offset.
  501. /**
  502. * This function is used to asynchronously write a certain number of bytes of
  503. * data to a random access device at a specified offset. It is an initiating
  504. * function for an @ref asynchronous_operation, and always returns immediately.
  505. * The asynchronous operation will continue until one of the following
  506. * conditions is true:
  507. *
  508. * @li All of the data in the supplied buffers has been written. That is, the
  509. * bytes transferred is equal to the sum of the buffer sizes.
  510. *
  511. * @li The completion_condition function object returns 0.
  512. *
  513. * This operation is implemented in terms of zero or more calls to the device's
  514. * async_write_some_at function, and is known as a <em>composed operation</em>.
  515. * The program must ensure that the device performs no <em>overlapping</em>
  516. * write operations (such as async_write_at, the device's async_write_some_at
  517. * function, or any other composed operations that perform writes) until this
  518. * operation completes. Operations are overlapping if the regions defined by
  519. * their offsets, and the numbers of bytes to write, intersect.
  520. *
  521. * @param d The device to which the data is to be written. The type must support
  522. * the AsyncRandomAccessWriteDevice concept.
  523. *
  524. * @param offset The offset at which the data will be written.
  525. *
  526. * @param buffers One or more buffers containing the data to be written.
  527. * Although the buffers object may be copied as necessary, ownership of the
  528. * underlying memory blocks is retained by the caller, which must guarantee
  529. * that they remain valid until the completion handler is called.
  530. *
  531. * @param completion_condition The function object to be called to determine
  532. * whether the write operation is complete. The signature of the function object
  533. * must be:
  534. * @code std::size_t completion_condition(
  535. * // Result of latest async_write_some_at operation.
  536. * const boost::system::error_code& error,
  537. *
  538. * // Number of bytes transferred so far.
  539. * std::size_t bytes_transferred
  540. * ); @endcode
  541. * A return value of 0 indicates that the write operation is complete. A
  542. * non-zero return value indicates the maximum number of bytes to be written on
  543. * the next call to the device's async_write_some_at function.
  544. *
  545. * @param token The @ref completion_token that will be used to produce a
  546. * completion handler, which will be called when the write completes.
  547. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  548. * @ref yield_context, or a function object with the correct completion
  549. * signature. The function signature of the completion handler must be:
  550. * @code void handler(
  551. * // Result of operation.
  552. * const boost::system::error_code& error,
  553. *
  554. * // Number of bytes written from the buffers. If an error
  555. * // occurred, this will be less than the sum of the buffer sizes.
  556. * std::size_t bytes_transferred
  557. * ); @endcode
  558. * Regardless of whether the asynchronous operation completes immediately or
  559. * not, the completion handler will not be invoked from within this function.
  560. * On immediate completion, invocation of the handler will be performed in a
  561. * manner equivalent to using boost::asio::async_immediate().
  562. *
  563. * @par Completion Signature
  564. * @code void(boost::system::error_code, std::size_t) @endcode
  565. *
  566. * @par Example
  567. * To write a single data buffer use the @ref buffer function as follows:
  568. * @code boost::asio::async_write_at(d, 42,
  569. * boost::asio::buffer(data, size),
  570. * boost::asio::transfer_at_least(32),
  571. * handler); @endcode
  572. * See the @ref buffer documentation for information on writing multiple
  573. * buffers in one go, and how to use it with arrays, boost::array or
  574. * std::vector.
  575. *
  576. * @par Per-Operation Cancellation
  577. * This asynchronous operation supports cancellation for the following
  578. * boost::asio::cancellation_type values:
  579. *
  580. * @li @c cancellation_type::terminal
  581. *
  582. * @li @c cancellation_type::partial
  583. *
  584. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  585. * async_write_some_at operation.
  586. */
  587. template <typename AsyncRandomAccessWriteDevice,
  588. typename ConstBufferSequence, typename CompletionCondition,
  589. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  590. std::size_t)) WriteToken = default_completion_token_t<
  591. typename AsyncRandomAccessWriteDevice::executor_type>>
  592. inline auto async_write_at(AsyncRandomAccessWriteDevice& d,
  593. uint64_t offset, const ConstBufferSequence& buffers,
  594. CompletionCondition completion_condition,
  595. WriteToken&& token = default_completion_token_t<
  596. typename AsyncRandomAccessWriteDevice::executor_type>(),
  597. constraint_t<
  598. is_completion_condition<CompletionCondition>::value
  599. > = 0)
  600. -> decltype(
  601. async_initiate<WriteToken,
  602. void (boost::system::error_code, std::size_t)>(
  603. declval<detail::initiate_async_write_at<
  604. AsyncRandomAccessWriteDevice>>(),
  605. token, offset, buffers,
  606. static_cast<CompletionCondition&&>(completion_condition)))
  607. {
  608. return async_initiate<WriteToken,
  609. void (boost::system::error_code, std::size_t)>(
  610. detail::initiate_async_write_at<AsyncRandomAccessWriteDevice>(d),
  611. token, offset, buffers,
  612. static_cast<CompletionCondition&&>(completion_condition));
  613. }
  614. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  615. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  616. /// Start an asynchronous operation to write all of the supplied data at the
  617. /// specified offset.
  618. /**
  619. * This function is used to asynchronously write a certain number of bytes of
  620. * data to a random access device at a specified offset. It is an initiating
  621. * function for an @ref asynchronous_operation, and always returns immediately.
  622. * The asynchronous operation will continue until one of the following
  623. * conditions is true:
  624. *
  625. * @li All of the data in the supplied basic_streambuf has been written.
  626. *
  627. * @li An error occurred.
  628. *
  629. * This operation is implemented in terms of zero or more calls to the device's
  630. * async_write_some_at function, and is known as a <em>composed operation</em>.
  631. * The program must ensure that the device performs no <em>overlapping</em>
  632. * write operations (such as async_write_at, the device's async_write_some_at
  633. * function, or any other composed operations that perform writes) until this
  634. * operation completes. Operations are overlapping if the regions defined by
  635. * their offsets, and the numbers of bytes to write, intersect.
  636. *
  637. * @param d The device to which the data is to be written. The type must support
  638. * the AsyncRandomAccessWriteDevice concept.
  639. *
  640. * @param offset The offset at which the data will be written.
  641. *
  642. * @param b A basic_streambuf object from which data will be written. Ownership
  643. * of the streambuf is retained by the caller, which must guarantee that it
  644. * remains valid until the completion handler is called.
  645. *
  646. * @param token The @ref completion_token that will be used to produce a
  647. * completion handler, which will be called when the write completes.
  648. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  649. * @ref yield_context, or a function object with the correct completion
  650. * signature. The function signature of the completion handler must be:
  651. * @code void handler(
  652. * // Result of operation.
  653. * const boost::system::error_code& error,
  654. *
  655. * // Number of bytes written from the buffers. If an error
  656. * // occurred, this will be less than the sum of the buffer sizes.
  657. * std::size_t bytes_transferred
  658. * ); @endcode
  659. * Regardless of whether the asynchronous operation completes immediately or
  660. * not, the completion handler will not be invoked from within this function.
  661. * On immediate completion, invocation of the handler will be performed in a
  662. * manner equivalent to using boost::asio::async_immediate().
  663. *
  664. * @par Completion Signature
  665. * @code void(boost::system::error_code, std::size_t) @endcode
  666. *
  667. * @par Per-Operation Cancellation
  668. * This asynchronous operation supports cancellation for the following
  669. * boost::asio::cancellation_type values:
  670. *
  671. * @li @c cancellation_type::terminal
  672. *
  673. * @li @c cancellation_type::partial
  674. *
  675. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  676. * async_write_some_at operation.
  677. */
  678. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  679. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  680. std::size_t)) WriteToken = default_completion_token_t<
  681. typename AsyncRandomAccessWriteDevice::executor_type>>
  682. inline auto async_write_at(AsyncRandomAccessWriteDevice& d,
  683. uint64_t offset, basic_streambuf<Allocator>& b,
  684. WriteToken&& token = default_completion_token_t<
  685. typename AsyncRandomAccessWriteDevice::executor_type>(),
  686. constraint_t<
  687. !is_completion_condition<WriteToken>::value
  688. > = 0)
  689. -> decltype(
  690. async_initiate<WriteToken,
  691. void (boost::system::error_code, std::size_t)>(
  692. declval<detail::initiate_async_write_at_streambuf<
  693. AsyncRandomAccessWriteDevice>>(),
  694. token, offset, &b, transfer_all()))
  695. {
  696. return async_initiate<WriteToken,
  697. void (boost::system::error_code, std::size_t)>(
  698. detail::initiate_async_write_at_streambuf<
  699. AsyncRandomAccessWriteDevice>(d),
  700. token, offset, &b, transfer_all());
  701. }
  702. /// Start an asynchronous operation to write a certain amount of data at the
  703. /// specified offset.
  704. /**
  705. * This function is used to asynchronously write a certain number of bytes of
  706. * data to a random access device at a specified offset. It is an initiating
  707. * function for an @ref asynchronous_operation, and always returns immediately.
  708. * The asynchronous operation will continue until one of the following
  709. * conditions is true:
  710. *
  711. * @li All of the data in the supplied basic_streambuf has been written.
  712. *
  713. * @li The completion_condition function object returns 0.
  714. *
  715. * This operation is implemented in terms of zero or more calls to the device's
  716. * async_write_some_at function, and is known as a <em>composed operation</em>.
  717. * The program must ensure that the device performs no <em>overlapping</em>
  718. * write operations (such as async_write_at, the device's async_write_some_at
  719. * function, or any other composed operations that perform writes) until this
  720. * operation completes. Operations are overlapping if the regions defined by
  721. * their offsets, and the numbers of bytes to write, intersect.
  722. *
  723. * @param d The device to which the data is to be written. The type must support
  724. * the AsyncRandomAccessWriteDevice concept.
  725. *
  726. * @param offset The offset at which the data will be written.
  727. *
  728. * @param b A basic_streambuf object from which data will be written. Ownership
  729. * of the streambuf is retained by the caller, which must guarantee that it
  730. * remains valid until the completion handler is called.
  731. *
  732. * @param completion_condition The function object to be called to determine
  733. * whether the write operation is complete. The signature of the function object
  734. * must be:
  735. * @code std::size_t completion_condition(
  736. * // Result of latest async_write_some_at operation.
  737. * const boost::system::error_code& error,
  738. *
  739. * // Number of bytes transferred so far.
  740. * std::size_t bytes_transferred
  741. * ); @endcode
  742. * A return value of 0 indicates that the write operation is complete. A
  743. * non-zero return value indicates the maximum number of bytes to be written on
  744. * the next call to the device's async_write_some_at function.
  745. *
  746. * @param token The @ref completion_token that will be used to produce a
  747. * completion handler, which will be called when the write completes.
  748. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  749. * @ref yield_context, or a function object with the correct completion
  750. * signature. The function signature of the completion handler must be:
  751. * @code void handler(
  752. * // Result of operation.
  753. * const boost::system::error_code& error,
  754. *
  755. * // Number of bytes written from the buffers. If an error
  756. * // occurred, this will be less than the sum of the buffer sizes.
  757. * std::size_t bytes_transferred
  758. * ); @endcode
  759. * Regardless of whether the asynchronous operation completes immediately or
  760. * not, the completion handler will not be invoked from within this function.
  761. * On immediate completion, invocation of the handler will be performed in a
  762. * manner equivalent to using boost::asio::async_immediate().
  763. *
  764. * @par Completion Signature
  765. * @code void(boost::system::error_code, std::size_t) @endcode
  766. *
  767. * @par Per-Operation Cancellation
  768. * This asynchronous operation supports cancellation for the following
  769. * boost::asio::cancellation_type values:
  770. *
  771. * @li @c cancellation_type::terminal
  772. *
  773. * @li @c cancellation_type::partial
  774. *
  775. * if they are also supported by the @c AsyncRandomAccessWriteDevice type's
  776. * async_write_some_at operation.
  777. */
  778. template <typename AsyncRandomAccessWriteDevice,
  779. typename Allocator, typename CompletionCondition,
  780. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  781. std::size_t)) WriteToken = default_completion_token_t<
  782. typename AsyncRandomAccessWriteDevice::executor_type>>
  783. inline auto async_write_at(AsyncRandomAccessWriteDevice& d, uint64_t offset,
  784. basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
  785. WriteToken&& token = default_completion_token_t<
  786. typename AsyncRandomAccessWriteDevice::executor_type>(),
  787. constraint_t<
  788. is_completion_condition<CompletionCondition>::value
  789. > = 0)
  790. -> decltype(
  791. async_initiate<WriteToken,
  792. void (boost::system::error_code, std::size_t)>(
  793. declval<detail::initiate_async_write_at_streambuf<
  794. AsyncRandomAccessWriteDevice>>(),
  795. token, offset, &b,
  796. static_cast<CompletionCondition&&>(completion_condition)))
  797. {
  798. return async_initiate<WriteToken,
  799. void (boost::system::error_code, std::size_t)>(
  800. detail::initiate_async_write_at_streambuf<
  801. AsyncRandomAccessWriteDevice>(d),
  802. token, offset, &b,
  803. static_cast<CompletionCondition&&>(completion_condition));
  804. }
  805. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  806. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  807. /*@}*/
  808. } // namespace asio
  809. } // namespace boost
  810. #include <boost/asio/detail/pop_options.hpp>
  811. #include <boost/asio/impl/write_at.hpp>
  812. #endif // BOOST_ASIO_WRITE_AT_HPP