/Src/Dependencies/Boost/boost/asio/windows/basic_random_access_handle.hpp

http://hadesmem.googlecode.com/ · C++ Header · 374 lines · 107 code · 25 blank · 242 comment · 3 complexity · 28fb9f4bc06a5aa574f8e9b9f02d3551 MD5 · raw file

  1. //
  2. // windows/basic_random_access_handle.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 BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
  11. #define BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_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. #if defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <cstddef>
  19. #include <boost/asio/detail/handler_type_requirements.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/windows/basic_handle.hpp>
  23. #include <boost/asio/windows/random_access_handle_service.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace windows {
  28. /// Provides random-access handle functionality.
  29. /**
  30. * The windows::basic_random_access_handle class template provides asynchronous
  31. * and blocking random-access handle functionality.
  32. *
  33. * @par Thread Safety
  34. * @e Distinct @e objects: Safe.@n
  35. * @e Shared @e objects: Unsafe.
  36. */
  37. template <typename RandomAccessHandleService = random_access_handle_service>
  38. class basic_random_access_handle
  39. : public basic_handle<RandomAccessHandleService>
  40. {
  41. public:
  42. /// (Deprecated: Use native_handle_type.) The native representation of a
  43. /// handle.
  44. typedef typename RandomAccessHandleService::native_handle_type native_type;
  45. /// The native representation of a handle.
  46. typedef typename RandomAccessHandleService::native_handle_type
  47. native_handle_type;
  48. /// Construct a basic_random_access_handle without opening it.
  49. /**
  50. * This constructor creates a random-access handle without opening it. The
  51. * handle needs to be opened before data can be written to or or read from it.
  52. *
  53. * @param io_service The io_service object that the random-access handle will
  54. * use to dispatch handlers for any asynchronous operations performed on the
  55. * handle.
  56. */
  57. explicit basic_random_access_handle(boost::asio::io_service& io_service)
  58. : basic_handle<RandomAccessHandleService>(io_service)
  59. {
  60. }
  61. /// Construct a basic_random_access_handle on an existing native handle.
  62. /**
  63. * This constructor creates a random-access handle object to hold an existing
  64. * native handle.
  65. *
  66. * @param io_service The io_service object that the random-access handle will
  67. * use to dispatch handlers for any asynchronous operations performed on the
  68. * handle.
  69. *
  70. * @param handle The new underlying handle implementation.
  71. *
  72. * @throws boost::system::system_error Thrown on failure.
  73. */
  74. basic_random_access_handle(boost::asio::io_service& io_service,
  75. const native_handle_type& handle)
  76. : basic_handle<RandomAccessHandleService>(io_service, handle)
  77. {
  78. }
  79. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  80. /// Move-construct a basic_random_access_handle from another.
  81. /**
  82. * This constructor moves a random-access handle from one object to another.
  83. *
  84. * @param other The other basic_random_access_handle object from which the
  85. * move will occur.
  86. *
  87. * @note Following the move, the moved-from object is in the same state as if
  88. * constructed using the @c basic_random_access_handle(io_service&)
  89. * constructor.
  90. */
  91. basic_random_access_handle(basic_random_access_handle&& other)
  92. : basic_handle<RandomAccessHandleService>(
  93. BOOST_ASIO_MOVE_CAST(basic_random_access_handle)(other))
  94. {
  95. }
  96. /// Move-assign a basic_random_access_handle from another.
  97. /**
  98. * This assignment operator moves a random-access handle from one object to
  99. * another.
  100. *
  101. * @param other The other basic_random_access_handle object from which the
  102. * move will occur.
  103. *
  104. * @note Following the move, the moved-from object is in the same state as if
  105. * constructed using the @c basic_random_access_handle(io_service&)
  106. * constructor.
  107. */
  108. basic_random_access_handle& operator=(basic_random_access_handle&& other)
  109. {
  110. basic_handle<RandomAccessHandleService>::operator=(
  111. BOOST_ASIO_MOVE_CAST(basic_random_access_handle)(other));
  112. return *this;
  113. }
  114. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  115. /// Write some data to the handle at the specified offset.
  116. /**
  117. * This function is used to write data to the random-access handle. The
  118. * function call will block until one or more bytes of the data has been
  119. * written successfully, or until an error occurs.
  120. *
  121. * @param offset The offset at which the data will be written.
  122. *
  123. * @param buffers One or more data buffers to be written to the handle.
  124. *
  125. * @returns The number of bytes written.
  126. *
  127. * @throws boost::system::system_error Thrown on failure. An error code of
  128. * boost::asio::error::eof indicates that the connection was closed by the
  129. * peer.
  130. *
  131. * @note The write_some_at operation may not write all of the data. Consider
  132. * using the @ref write_at function if you need to ensure that all data is
  133. * written before the blocking operation completes.
  134. *
  135. * @par Example
  136. * To write a single data buffer use the @ref buffer function as follows:
  137. * @code
  138. * handle.write_some_at(42, boost::asio::buffer(data, size));
  139. * @endcode
  140. * See the @ref buffer documentation for information on writing multiple
  141. * buffers in one go, and how to use it with arrays, boost::array or
  142. * std::vector.
  143. */
  144. template <typename ConstBufferSequence>
  145. std::size_t write_some_at(boost::uint64_t offset,
  146. const ConstBufferSequence& buffers)
  147. {
  148. boost::system::error_code ec;
  149. std::size_t s = this->get_service().write_some_at(
  150. this->get_implementation(), offset, buffers, ec);
  151. boost::asio::detail::throw_error(ec, "write_some_at");
  152. return s;
  153. }
  154. /// Write some data to the handle at the specified offset.
  155. /**
  156. * This function is used to write data to the random-access handle. The
  157. * function call will block until one or more bytes of the data has been
  158. * written successfully, or until an error occurs.
  159. *
  160. * @param offset The offset at which the data will be written.
  161. *
  162. * @param buffers One or more data buffers to be written to the handle.
  163. *
  164. * @param ec Set to indicate what error occurred, if any.
  165. *
  166. * @returns The number of bytes written. Returns 0 if an error occurred.
  167. *
  168. * @note The write_some operation may not transmit all of the data to the
  169. * peer. Consider using the @ref write_at function if you need to ensure that
  170. * all data is written before the blocking operation completes.
  171. */
  172. template <typename ConstBufferSequence>
  173. std::size_t write_some_at(boost::uint64_t offset,
  174. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  175. {
  176. return this->get_service().write_some_at(
  177. this->get_implementation(), offset, buffers, ec);
  178. }
  179. /// Start an asynchronous write at the specified offset.
  180. /**
  181. * This function is used to asynchronously write data to the random-access
  182. * handle. The function call always returns immediately.
  183. *
  184. * @param offset The offset at which the data will be written.
  185. *
  186. * @param buffers One or more data buffers to be written to the handle.
  187. * Although the buffers object may be copied as necessary, ownership of the
  188. * underlying memory blocks is retained by the caller, which must guarantee
  189. * that they remain valid until the handler is called.
  190. *
  191. * @param handler The handler to be called when the write operation completes.
  192. * Copies will be made of the handler as required. The function signature of
  193. * the handler must be:
  194. * @code void handler(
  195. * const boost::system::error_code& error, // Result of operation.
  196. * std::size_t bytes_transferred // Number of bytes written.
  197. * ); @endcode
  198. * Regardless of whether the asynchronous operation completes immediately or
  199. * not, the handler will not be invoked from within this function. Invocation
  200. * of the handler will be performed in a manner equivalent to using
  201. * boost::asio::io_service::post().
  202. *
  203. * @note The write operation may not transmit all of the data to the peer.
  204. * Consider using the @ref async_write_at function if you need to ensure that
  205. * all data is written before the asynchronous operation completes.
  206. *
  207. * @par Example
  208. * To write a single data buffer use the @ref buffer function as follows:
  209. * @code
  210. * handle.async_write_some_at(42, boost::asio::buffer(data, size), handler);
  211. * @endcode
  212. * See the @ref buffer documentation for information on writing multiple
  213. * buffers in one go, and how to use it with arrays, boost::array or
  214. * std::vector.
  215. */
  216. template <typename ConstBufferSequence, typename WriteHandler>
  217. void async_write_some_at(boost::uint64_t offset,
  218. const ConstBufferSequence& buffers,
  219. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  220. {
  221. // If you get an error on the following line it means that your handler does
  222. // not meet the documented type requirements for a WriteHandler.
  223. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  224. this->get_service().async_write_some_at(this->get_implementation(),
  225. offset, buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  226. }
  227. /// Read some data from the handle at the specified offset.
  228. /**
  229. * This function is used to read data from the random-access handle. The
  230. * function call will block until one or more bytes of data has been read
  231. * successfully, or until an error occurs.
  232. *
  233. * @param offset The offset at which the data will be read.
  234. *
  235. * @param buffers One or more buffers into which the data will be read.
  236. *
  237. * @returns The number of bytes read.
  238. *
  239. * @throws boost::system::system_error Thrown on failure. An error code of
  240. * boost::asio::error::eof indicates that the connection was closed by the
  241. * peer.
  242. *
  243. * @note The read_some operation may not read all of the requested number of
  244. * bytes. Consider using the @ref read_at function if you need to ensure that
  245. * the requested amount of data is read before the blocking operation
  246. * completes.
  247. *
  248. * @par Example
  249. * To read into a single data buffer use the @ref buffer function as follows:
  250. * @code
  251. * handle.read_some_at(42, boost::asio::buffer(data, size));
  252. * @endcode
  253. * See the @ref buffer documentation for information on reading into multiple
  254. * buffers in one go, and how to use it with arrays, boost::array or
  255. * std::vector.
  256. */
  257. template <typename MutableBufferSequence>
  258. std::size_t read_some_at(boost::uint64_t offset,
  259. const MutableBufferSequence& buffers)
  260. {
  261. boost::system::error_code ec;
  262. std::size_t s = this->get_service().read_some_at(
  263. this->get_implementation(), offset, buffers, ec);
  264. boost::asio::detail::throw_error(ec, "read_some_at");
  265. return s;
  266. }
  267. /// Read some data from the handle at the specified offset.
  268. /**
  269. * This function is used to read data from the random-access handle. The
  270. * function call will block until one or more bytes of data has been read
  271. * successfully, or until an error occurs.
  272. *
  273. * @param offset The offset at which the data will be read.
  274. *
  275. * @param buffers One or more buffers into which the data will be read.
  276. *
  277. * @param ec Set to indicate what error occurred, if any.
  278. *
  279. * @returns The number of bytes read. Returns 0 if an error occurred.
  280. *
  281. * @note The read_some operation may not read all of the requested number of
  282. * bytes. Consider using the @ref read_at function if you need to ensure that
  283. * the requested amount of data is read before the blocking operation
  284. * completes.
  285. */
  286. template <typename MutableBufferSequence>
  287. std::size_t read_some_at(boost::uint64_t offset,
  288. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  289. {
  290. return this->get_service().read_some_at(
  291. this->get_implementation(), offset, buffers, ec);
  292. }
  293. /// Start an asynchronous read at the specified offset.
  294. /**
  295. * This function is used to asynchronously read data from the random-access
  296. * handle. The function call always returns immediately.
  297. *
  298. * @param offset The offset at which the data will be read.
  299. *
  300. * @param buffers One or more buffers into which the data will be read.
  301. * Although the buffers object may be copied as necessary, ownership of the
  302. * underlying memory blocks is retained by the caller, which must guarantee
  303. * that they remain valid until the handler is called.
  304. *
  305. * @param handler The handler to be called when the read operation completes.
  306. * Copies will be made of the handler as required. The function signature of
  307. * the handler must be:
  308. * @code void handler(
  309. * const boost::system::error_code& error, // Result of operation.
  310. * std::size_t bytes_transferred // Number of bytes read.
  311. * ); @endcode
  312. * Regardless of whether the asynchronous operation completes immediately or
  313. * not, the handler will not be invoked from within this function. Invocation
  314. * of the handler will be performed in a manner equivalent to using
  315. * boost::asio::io_service::post().
  316. *
  317. * @note The read operation may not read all of the requested number of bytes.
  318. * Consider using the @ref async_read_at function if you need to ensure that
  319. * the requested amount of data is read before the asynchronous operation
  320. * completes.
  321. *
  322. * @par Example
  323. * To read into a single data buffer use the @ref buffer function as follows:
  324. * @code
  325. * handle.async_read_some_at(42, boost::asio::buffer(data, size), handler);
  326. * @endcode
  327. * See the @ref buffer documentation for information on reading into multiple
  328. * buffers in one go, and how to use it with arrays, boost::array or
  329. * std::vector.
  330. */
  331. template <typename MutableBufferSequence, typename ReadHandler>
  332. void async_read_some_at(boost::uint64_t offset,
  333. const MutableBufferSequence& buffers,
  334. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  335. {
  336. // If you get an error on the following line it means that your handler does
  337. // not meet the documented type requirements for a ReadHandler.
  338. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  339. this->get_service().async_read_some_at(this->get_implementation(),
  340. offset, buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  341. }
  342. };
  343. } // namespace windows
  344. } // namespace asio
  345. } // namespace boost
  346. #include <boost/asio/detail/pop_options.hpp>
  347. #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  348. // || defined(GENERATING_DOCUMENTATION)
  349. #endif // BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP