write_at.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. //
  2. // write_at.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/cstdint.hpp>
  20. #include <boost/asio/detail/pop_options.hpp>
  21. #include <boost/asio/basic_streambuf.hpp>
  22. #include <boost/asio/error.hpp>
  23. namespace boost {
  24. namespace asio {
  25. /**
  26. * @defgroup write_at boost::asio::write_at
  27. *
  28. * @brief Write a certain amount of data at a specified offset before returning.
  29. */
  30. /*@{*/
  31. /// Write all of the supplied data at the specified offset before returning.
  32. /**
  33. * This function is used to write a certain number of bytes of data to a random
  34. * access device at a specified offset. The call will block until one of the
  35. * following conditions is true:
  36. *
  37. * @li All of the data in the supplied buffers has been written. That is, the
  38. * bytes transferred is equal to the sum of the buffer sizes.
  39. *
  40. * @li An error occurred.
  41. *
  42. * This operation is implemented in terms of zero or more calls to the device's
  43. * write_some_at function.
  44. *
  45. * @param d The device to which the data is to be written. The type must support
  46. * the SyncRandomAccessWriteDevice concept.
  47. *
  48. * @param offset The offset at which the data will be written.
  49. *
  50. * @param buffers One or more buffers containing the data to be written. The sum
  51. * of the buffer sizes indicates the maximum number of bytes to write to the
  52. * device.
  53. *
  54. * @returns The number of bytes transferred.
  55. *
  56. * @throws boost::system::system_error Thrown on failure.
  57. *
  58. * @par Example
  59. * To write a single data buffer use the @ref buffer function as follows:
  60. * @code boost::asio::write_at(d, 42, boost::asio::buffer(data, size)); @endcode
  61. * See the @ref buffer documentation for information on writing multiple
  62. * buffers in one go, and how to use it with arrays, boost::array or
  63. * std::vector.
  64. *
  65. * @note This overload is equivalent to calling:
  66. * @code boost::asio::write_at(
  67. * d, offset, buffers,
  68. * boost::asio::transfer_all()); @endcode
  69. */
  70. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  71. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  72. boost::uint64_t offset, const ConstBufferSequence& buffers);
  73. /// Write a certain amount of data at a specified offset before returning.
  74. /**
  75. * This function is used to write a certain number of bytes of data to a random
  76. * access device at a specified offset. The call will block until one of the
  77. * following conditions is true:
  78. *
  79. * @li All of the data in the supplied buffers has been written. That is, the
  80. * bytes transferred is equal to the sum of the buffer sizes.
  81. *
  82. * @li The completion_condition function object returns 0.
  83. *
  84. * This operation is implemented in terms of zero or more calls to the device's
  85. * write_some_at function.
  86. *
  87. * @param d The device to which the data is to be written. The type must support
  88. * the SyncRandomAccessWriteDevice concept.
  89. *
  90. * @param offset The offset at which the data will be written.
  91. *
  92. * @param buffers One or more buffers containing the data to be written. The sum
  93. * of the buffer sizes indicates the maximum number of bytes to write to the
  94. * device.
  95. *
  96. * @param completion_condition The function object to be called to determine
  97. * whether the write operation is complete. The signature of the function object
  98. * must be:
  99. * @code std::size_t completion_condition(
  100. * // Result of latest write_some_at operation.
  101. * const boost::system::error_code& error,
  102. *
  103. * // Number of bytes transferred so far.
  104. * std::size_t bytes_transferred
  105. * ); @endcode
  106. * A return value of 0 indicates that the write operation is complete. A
  107. * non-zero return value indicates the maximum number of bytes to be written on
  108. * the next call to the device's write_some_at function.
  109. *
  110. * @returns The number of bytes transferred.
  111. *
  112. * @throws boost::system::system_error Thrown on failure.
  113. *
  114. * @par Example
  115. * To write a single data buffer use the @ref buffer function as follows:
  116. * @code boost::asio::write_at(d, 42, boost::asio::buffer(data, size),
  117. * boost::asio::transfer_at_least(32)); @endcode
  118. * See the @ref buffer documentation for information on writing multiple
  119. * buffers in one go, and how to use it with arrays, boost::array or
  120. * std::vector.
  121. */
  122. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  123. typename CompletionCondition>
  124. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  125. boost::uint64_t offset, const ConstBufferSequence& buffers,
  126. CompletionCondition completion_condition);
  127. /// Write a certain amount of data at a specified offset before returning.
  128. /**
  129. * This function is used to write a certain number of bytes of data to a random
  130. * access device at a specified offset. The call will block until one of the
  131. * following conditions is true:
  132. *
  133. * @li All of the data in the supplied buffers has been written. That is, the
  134. * bytes transferred is equal to the sum of the buffer sizes.
  135. *
  136. * @li The completion_condition function object returns 0.
  137. *
  138. * This operation is implemented in terms of zero or more calls to the device's
  139. * write_some_at function.
  140. *
  141. * @param d The device to which the data is to be written. The type must support
  142. * the SyncRandomAccessWriteDevice concept.
  143. *
  144. * @param offset The offset at which the data will be written.
  145. *
  146. * @param buffers One or more buffers containing the data to be written. The sum
  147. * of the buffer sizes indicates the maximum number of bytes to write to the
  148. * device.
  149. *
  150. * @param completion_condition The function object to be called to determine
  151. * whether the write operation is complete. The signature of the function object
  152. * must be:
  153. * @code std::size_t completion_condition(
  154. * // Result of latest write_some_at operation.
  155. * const boost::system::error_code& error,
  156. *
  157. * // Number of bytes transferred so far.
  158. * std::size_t bytes_transferred
  159. * ); @endcode
  160. * A return value of 0 indicates that the write operation is complete. A
  161. * non-zero return value indicates the maximum number of bytes to be written on
  162. * the next call to the device's write_some_at function.
  163. *
  164. * @param ec Set to indicate what error occurred, if any.
  165. *
  166. * @returns The number of bytes written. If an error occurs, returns the total
  167. * number of bytes successfully transferred prior to the error.
  168. */
  169. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  170. typename CompletionCondition>
  171. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  172. boost::uint64_t offset, const ConstBufferSequence& buffers,
  173. CompletionCondition completion_condition, boost::system::error_code& ec);
  174. /// Write all of the supplied data at the specified offset before returning.
  175. /**
  176. * This function is used to write a certain number of bytes of data to a random
  177. * access device at a specified offset. The call will block until one of the
  178. * following conditions is true:
  179. *
  180. * @li All of the data in the supplied basic_streambuf has been written.
  181. *
  182. * @li An error occurred.
  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 b The basic_streambuf object from which data will be written.
  193. *
  194. * @returns The number of bytes transferred.
  195. *
  196. * @throws boost::system::system_error Thrown on failure.
  197. *
  198. * @note This overload is equivalent to calling:
  199. * @code boost::asio::write_at(
  200. * d, 42, b,
  201. * boost::asio::transfer_all()); @endcode
  202. */
  203. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  204. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  205. boost::uint64_t offset, basic_streambuf<Allocator>& b);
  206. /// Write a certain amount of data at a specified offset before returning.
  207. /**
  208. * This function is used to write a certain number of bytes of data to a random
  209. * access device at a specified offset. The call will block until one of the
  210. * following conditions is true:
  211. *
  212. * @li All of the data in the supplied basic_streambuf has been written.
  213. *
  214. * @li The completion_condition function object returns 0.
  215. *
  216. * This operation is implemented in terms of zero or more calls to the device's
  217. * write_some_at function.
  218. *
  219. * @param d The device to which the data is to be written. The type must support
  220. * the SyncRandomAccessWriteDevice concept.
  221. *
  222. * @param offset The offset at which the data will be written.
  223. *
  224. * @param b The basic_streambuf object from which data will be written.
  225. *
  226. * @param completion_condition The function object to be called to determine
  227. * whether the write operation is complete. The signature of the function object
  228. * must be:
  229. * @code std::size_t completion_condition(
  230. * // Result of latest write_some_at operation.
  231. * const boost::system::error_code& error,
  232. *
  233. * // Number of bytes transferred so far.
  234. * std::size_t bytes_transferred
  235. * ); @endcode
  236. * A return value of 0 indicates that the write operation is complete. A
  237. * non-zero return value indicates the maximum number of bytes to be written on
  238. * the next call to the device's write_some_at function.
  239. *
  240. * @returns The number of bytes transferred.
  241. *
  242. * @throws boost::system::system_error Thrown on failure.
  243. */
  244. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  245. typename CompletionCondition>
  246. std::size_t write_at(SyncRandomAccessWriteDevice& d, boost::uint64_t offset,
  247. basic_streambuf<Allocator>& b, CompletionCondition completion_condition);
  248. /// Write a certain amount of data at a specified offset before returning.
  249. /**
  250. * This function is used to write a certain number of bytes of data to a random
  251. * access device at a specified offset. The call will block until one of the
  252. * following conditions is true:
  253. *
  254. * @li All of the data in the supplied basic_streambuf has been written.
  255. *
  256. * @li The completion_condition function object returns 0.
  257. *
  258. * This operation is implemented in terms of zero or more calls to the device's
  259. * write_some_at function.
  260. *
  261. * @param d The device to which the data is to be written. The type must support
  262. * the SyncRandomAccessWriteDevice concept.
  263. *
  264. * @param offset The offset at which the data will be written.
  265. *
  266. * @param b The basic_streambuf object from which data will be written.
  267. *
  268. * @param completion_condition The function object to be called to determine
  269. * whether the write operation is complete. The signature of the function object
  270. * must be:
  271. * @code std::size_t completion_condition(
  272. * // Result of latest write_some_at operation.
  273. * const boost::system::error_code& error,
  274. *
  275. * // Number of bytes transferred so far.
  276. * std::size_t bytes_transferred
  277. * ); @endcode
  278. * A return value of 0 indicates that the write operation is complete. A
  279. * non-zero return value indicates the maximum number of bytes to be written on
  280. * the next call to the device's write_some_at function.
  281. *
  282. * @param ec Set to indicate what error occurred, if any.
  283. *
  284. * @returns The number of bytes written. If an error occurs, returns the total
  285. * number of bytes successfully transferred prior to the error.
  286. */
  287. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  288. typename CompletionCondition>
  289. std::size_t write_at(SyncRandomAccessWriteDevice& d, boost::uint64_t offset,
  290. basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
  291. boost::system::error_code& ec);
  292. /*@}*/
  293. /**
  294. * @defgroup async_write_at boost::asio::async_write_at
  295. *
  296. * @brief Start an asynchronous operation to write a certain amount of data at
  297. * the specified offset.
  298. */
  299. /*@{*/
  300. /// Start an asynchronous operation to write all of the supplied data at the
  301. /// specified offset.
  302. /**
  303. * This function is used to asynchronously write a certain number of bytes of
  304. * data to a random access device at a specified offset. The function call
  305. * always returns immediately. The asynchronous operation will continue until
  306. * one of the following conditions is true:
  307. *
  308. * @li All of the data in the supplied buffers has been written. That is, the
  309. * bytes transferred is equal to the sum of the buffer sizes.
  310. *
  311. * @li An error occurred.
  312. *
  313. * This operation is implemented in terms of zero or more calls to the device's
  314. * async_write_some_at function.
  315. *
  316. * @param d The device to which the data is to be written. The type must support
  317. * the AsyncRandomAccessWriteDevice concept.
  318. *
  319. * @param offset The offset at which the data will be written.
  320. *
  321. * @param buffers One or more buffers containing the data to be written.
  322. * Although the buffers object may be copied as necessary, ownership of the
  323. * underlying memory blocks is retained by the caller, which must guarantee
  324. * that they remain valid until the handler is called.
  325. *
  326. * @param handler The handler to be called when the write operation completes.
  327. * Copies will be made of the handler as required. The function signature of
  328. * the handler must be:
  329. * @code void handler(
  330. * // Result of operation.
  331. * const boost::system::error_code& error,
  332. *
  333. * // Number of bytes written from the buffers. If an error
  334. * // occurred, this will be less than the sum of the buffer sizes.
  335. * std::size_t bytes_transferred
  336. * ); @endcode
  337. * Regardless of whether the asynchronous operation completes immediately or
  338. * not, the handler will not be invoked from within this function. Invocation of
  339. * the handler will be performed in a manner equivalent to using
  340. * boost::asio::io_service::post().
  341. *
  342. * @par Example
  343. * To write a single data buffer use the @ref buffer function as follows:
  344. * @code
  345. * boost::asio::async_write_at(d, 42, boost::asio::buffer(data, size), handler);
  346. * @endcode
  347. * See the @ref buffer documentation for information on writing multiple
  348. * buffers in one go, and how to use it with arrays, boost::array or
  349. * std::vector.
  350. */
  351. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  352. typename WriteHandler>
  353. void async_write_at(AsyncRandomAccessWriteDevice& d, boost::uint64_t offset,
  354. const ConstBufferSequence& buffers, WriteHandler handler);
  355. /// Start an asynchronous operation to write a certain amount of data at the
  356. /// specified offset.
  357. /**
  358. * This function is used to asynchronously write a certain number of bytes of
  359. * data to a random access device at a specified offset. The function call
  360. * always returns immediately. The asynchronous operation will continue until
  361. * one of the following conditions is true:
  362. *
  363. * @li All of the data in the supplied buffers has been written. That is, the
  364. * bytes transferred is equal to the sum of the buffer sizes.
  365. *
  366. * @li The completion_condition function object returns 0.
  367. *
  368. * This operation is implemented in terms of zero or more calls to the device's
  369. * async_write_some_at function.
  370. *
  371. * @param d The device to which the data is to be written. The type must support
  372. * the AsyncRandomAccessWriteDevice concept.
  373. *
  374. * @param offset The offset at which the data will be written.
  375. *
  376. * @param buffers One or more buffers containing the data to be written.
  377. * Although the buffers object may be copied as necessary, ownership of the
  378. * underlying memory blocks is retained by the caller, which must guarantee
  379. * that they remain valid until the handler is called.
  380. *
  381. * @param completion_condition The function object to be called to determine
  382. * whether the write operation is complete. The signature of the function object
  383. * must be:
  384. * @code std::size_t completion_condition(
  385. * // Result of latest async_write_some_at operation.
  386. * const boost::system::error_code& error,
  387. *
  388. * // Number of bytes transferred so far.
  389. * std::size_t bytes_transferred
  390. * ); @endcode
  391. * A return value of 0 indicates that the write operation is complete. A
  392. * non-zero return value indicates the maximum number of bytes to be written on
  393. * the next call to the device's async_write_some_at function.
  394. *
  395. * @param handler The handler to be called when the write operation completes.
  396. * Copies will be made of the handler as required. The function signature of the
  397. * handler must be:
  398. * @code void handler(
  399. * // Result of operation.
  400. * const boost::system::error_code& error,
  401. *
  402. * // Number of bytes written from the buffers. If an error
  403. * // occurred, this will be less than the sum of the buffer sizes.
  404. * std::size_t bytes_transferred
  405. * ); @endcode
  406. * Regardless of whether the asynchronous operation completes immediately or
  407. * not, the handler will not be invoked from within this function. Invocation of
  408. * the handler will be performed in a manner equivalent to using
  409. * boost::asio::io_service::post().
  410. *
  411. * @par Example
  412. * To write a single data buffer use the @ref buffer function as follows:
  413. * @code boost::asio::async_write_at(d, 42,
  414. * boost::asio::buffer(data, size),
  415. * boost::asio::transfer_at_least(32),
  416. * handler); @endcode
  417. * See the @ref buffer documentation for information on writing multiple
  418. * buffers in one go, and how to use it with arrays, boost::array or
  419. * std::vector.
  420. */
  421. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  422. typename CompletionCondition, typename WriteHandler>
  423. void async_write_at(AsyncRandomAccessWriteDevice& d,
  424. boost::uint64_t offset, const ConstBufferSequence& buffers,
  425. CompletionCondition completion_condition, WriteHandler handler);
  426. /// Start an asynchronous operation to write all of the supplied data at the
  427. /// specified offset.
  428. /**
  429. * This function is used to asynchronously write a certain number of bytes of
  430. * data to a random access device at a specified offset. The function call
  431. * always returns immediately. The asynchronous operation will continue until
  432. * one of the following conditions is true:
  433. *
  434. * @li All of the data in the supplied basic_streambuf has been written.
  435. *
  436. * @li An error occurred.
  437. *
  438. * This operation is implemented in terms of zero or more calls to the device's
  439. * async_write_some_at function.
  440. *
  441. * @param d The device to which the data is to be written. The type must support
  442. * the AsyncRandomAccessWriteDevice concept.
  443. *
  444. * @param offset The offset at which the data will be written.
  445. *
  446. * @param b A basic_streambuf object from which data will be written. Ownership
  447. * of the streambuf is retained by the caller, which must guarantee that it
  448. * remains valid until the handler is called.
  449. *
  450. * @param handler The handler to be called when the write operation completes.
  451. * Copies will be made of the handler as required. The function signature of the
  452. * handler must be:
  453. * @code void handler(
  454. * // Result of operation.
  455. * const boost::system::error_code& error,
  456. *
  457. * // Number of bytes written from the buffers. If an error
  458. * // occurred, this will be less than the sum of the buffer sizes.
  459. * std::size_t bytes_transferred
  460. * ); @endcode
  461. * Regardless of whether the asynchronous operation completes immediately or
  462. * not, the handler will not be invoked from within this function. Invocation of
  463. * the handler will be performed in a manner equivalent to using
  464. * boost::asio::io_service::post().
  465. */
  466. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  467. typename WriteHandler>
  468. void async_write_at(AsyncRandomAccessWriteDevice& d, boost::uint64_t offset,
  469. basic_streambuf<Allocator>& b, WriteHandler handler);
  470. /// Start an asynchronous operation to write a certain amount of data at the
  471. /// specified offset.
  472. /**
  473. * This function is used to asynchronously write a certain number of bytes of
  474. * data to a random access device at a specified offset. The function call
  475. * always returns immediately. The asynchronous operation will continue until
  476. * one of the following conditions is true:
  477. *
  478. * @li All of the data in the supplied basic_streambuf has been written.
  479. *
  480. * @li The completion_condition function object returns 0.
  481. *
  482. * This operation is implemented in terms of zero or more calls to the device's
  483. * async_write_some_at function.
  484. *
  485. * @param d The device to which the data is to be written. The type must support
  486. * the AsyncRandomAccessWriteDevice concept.
  487. *
  488. * @param offset The offset at which the data will be written.
  489. *
  490. * @param b A basic_streambuf object from which data will be written. Ownership
  491. * of the streambuf is retained by the caller, which must guarantee that it
  492. * remains valid until the handler is called.
  493. *
  494. * @param completion_condition The function object to be called to determine
  495. * whether the write operation is complete. The signature of the function object
  496. * must be:
  497. * @code std::size_t completion_condition(
  498. * // Result of latest async_write_some_at operation.
  499. * const boost::system::error_code& error,
  500. *
  501. * // Number of bytes transferred so far.
  502. * std::size_t bytes_transferred
  503. * ); @endcode
  504. * A return value of 0 indicates that the write operation is complete. A
  505. * non-zero return value indicates the maximum number of bytes to be written on
  506. * the next call to the device's async_write_some_at function.
  507. *
  508. * @param handler The handler to be called when the write operation completes.
  509. * Copies will be made of the handler as required. The function signature of the
  510. * handler must be:
  511. * @code void handler(
  512. * // Result of operation.
  513. * const boost::system::error_code& error,
  514. *
  515. * // Number of bytes written from the buffers. If an error
  516. * // occurred, this will be less than the sum of the buffer sizes.
  517. * std::size_t bytes_transferred
  518. * ); @endcode
  519. * Regardless of whether the asynchronous operation completes immediately or
  520. * not, the handler will not be invoked from within this function. Invocation of
  521. * the handler will be performed in a manner equivalent to using
  522. * boost::asio::io_service::post().
  523. */
  524. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  525. typename CompletionCondition, typename WriteHandler>
  526. void async_write_at(AsyncRandomAccessWriteDevice& d, boost::uint64_t offset,
  527. basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
  528. WriteHandler handler);
  529. /*@}*/
  530. } // namespace asio
  531. } // namespace boost
  532. #include <boost/asio/impl/write_at.ipp>
  533. #include <boost/asio/detail/pop_options.hpp>
  534. #endif // BOOST_ASIO_WRITE_AT_HPP