write_at.hpp 22 KB

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