write_at.hpp 22 KB

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