basic_handle.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // windows/basic_handle.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_HANDLE_HPP
  11. #define BOOST_ASIO_WINDOWS_BASIC_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(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
  18. || defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) \
  19. || defined(GENERATING_DOCUMENTATION)
  20. #include <boost/asio/basic_io_object.hpp>
  21. #include <boost/asio/detail/throw_error.hpp>
  22. #include <boost/asio/error.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace windows {
  27. /// Provides Windows handle functionality.
  28. /**
  29. * The windows::basic_handle class template provides the ability to wrap a
  30. * Windows handle.
  31. *
  32. * @par Thread Safety
  33. * @e Distinct @e objects: Safe.@n
  34. * @e Shared @e objects: Unsafe.
  35. */
  36. template <typename HandleService>
  37. class basic_handle
  38. : public basic_io_object<HandleService>
  39. {
  40. public:
  41. /// (Deprecated: Use native_handle_type.) The native representation of a
  42. /// handle.
  43. typedef typename HandleService::native_handle_type native_type;
  44. /// The native representation of a handle.
  45. typedef typename HandleService::native_handle_type native_handle_type;
  46. /// A basic_handle is always the lowest layer.
  47. typedef basic_handle<HandleService> lowest_layer_type;
  48. /// Construct a basic_handle without opening it.
  49. /**
  50. * This constructor creates a handle without opening it.
  51. *
  52. * @param io_service The io_service object that the handle will use to
  53. * dispatch handlers for any asynchronous operations performed on the handle.
  54. */
  55. explicit basic_handle(boost::asio::io_service& io_service)
  56. : basic_io_object<HandleService>(io_service)
  57. {
  58. }
  59. /// Construct a basic_handle on an existing native handle.
  60. /**
  61. * This constructor creates a handle object to hold an existing native handle.
  62. *
  63. * @param io_service The io_service object that the handle will use to
  64. * dispatch handlers for any asynchronous operations performed on the handle.
  65. *
  66. * @param handle A native handle.
  67. *
  68. * @throws boost::system::system_error Thrown on failure.
  69. */
  70. basic_handle(boost::asio::io_service& io_service,
  71. const native_handle_type& handle)
  72. : basic_io_object<HandleService>(io_service)
  73. {
  74. boost::system::error_code ec;
  75. this->get_service().assign(this->get_implementation(), handle, ec);
  76. boost::asio::detail::throw_error(ec, "assign");
  77. }
  78. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  79. /// Move-construct a basic_handle from another.
  80. /**
  81. * This constructor moves a handle from one object to another.
  82. *
  83. * @param other The other basic_handle object from which the move will occur.
  84. *
  85. * @note Following the move, the moved-from object is in the same state as if
  86. * constructed using the @c basic_handle(io_service&) constructor.
  87. */
  88. basic_handle(basic_handle&& other)
  89. : basic_io_object<HandleService>(
  90. BOOST_ASIO_MOVE_CAST(basic_handle)(other))
  91. {
  92. }
  93. /// Move-assign a basic_handle from another.
  94. /**
  95. * This assignment operator moves a handle from one object to another.
  96. *
  97. * @param other The other basic_handle object from which the move will occur.
  98. *
  99. * @note Following the move, the moved-from object is in the same state as if
  100. * constructed using the @c basic_handle(io_service&) constructor.
  101. */
  102. basic_handle& operator=(basic_handle&& other)
  103. {
  104. basic_io_object<HandleService>::operator=(
  105. BOOST_ASIO_MOVE_CAST(basic_handle)(other));
  106. return *this;
  107. }
  108. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  109. /// Get a reference to the lowest layer.
  110. /**
  111. * This function returns a reference to the lowest layer in a stack of
  112. * layers. Since a basic_handle cannot contain any further layers, it simply
  113. * returns a reference to itself.
  114. *
  115. * @return A reference to the lowest layer in the stack of layers. Ownership
  116. * is not transferred to the caller.
  117. */
  118. lowest_layer_type& lowest_layer()
  119. {
  120. return *this;
  121. }
  122. /// Get a const reference to the lowest layer.
  123. /**
  124. * This function returns a const reference to the lowest layer in a stack of
  125. * layers. Since a basic_handle cannot contain any further layers, it simply
  126. * returns a reference to itself.
  127. *
  128. * @return A const reference to the lowest layer in the stack of layers.
  129. * Ownership is not transferred to the caller.
  130. */
  131. const lowest_layer_type& lowest_layer() const
  132. {
  133. return *this;
  134. }
  135. /// Assign an existing native handle to the handle.
  136. /*
  137. * This function opens the handle to hold an existing native handle.
  138. *
  139. * @param handle A native handle.
  140. *
  141. * @throws boost::system::system_error Thrown on failure.
  142. */
  143. void assign(const native_handle_type& handle)
  144. {
  145. boost::system::error_code ec;
  146. this->get_service().assign(this->get_implementation(), handle, ec);
  147. boost::asio::detail::throw_error(ec, "assign");
  148. }
  149. /// Assign an existing native handle to the handle.
  150. /*
  151. * This function opens the handle to hold an existing native handle.
  152. *
  153. * @param handle A native handle.
  154. *
  155. * @param ec Set to indicate what error occurred, if any.
  156. */
  157. boost::system::error_code assign(const native_handle_type& handle,
  158. boost::system::error_code& ec)
  159. {
  160. return this->get_service().assign(this->get_implementation(), handle, ec);
  161. }
  162. /// Determine whether the handle is open.
  163. bool is_open() const
  164. {
  165. return this->get_service().is_open(this->get_implementation());
  166. }
  167. /// Close the handle.
  168. /**
  169. * This function is used to close the handle. Any asynchronous read or write
  170. * operations will be cancelled immediately, and will complete with the
  171. * boost::asio::error::operation_aborted error.
  172. *
  173. * @throws boost::system::system_error Thrown on failure.
  174. */
  175. void close()
  176. {
  177. boost::system::error_code ec;
  178. this->get_service().close(this->get_implementation(), ec);
  179. boost::asio::detail::throw_error(ec, "close");
  180. }
  181. /// Close the handle.
  182. /**
  183. * This function is used to close the handle. Any asynchronous read or write
  184. * operations will be cancelled immediately, and will complete with the
  185. * boost::asio::error::operation_aborted error.
  186. *
  187. * @param ec Set to indicate what error occurred, if any.
  188. */
  189. boost::system::error_code close(boost::system::error_code& ec)
  190. {
  191. return this->get_service().close(this->get_implementation(), ec);
  192. }
  193. /// (Deprecated: Use native_handle().) Get the native handle representation.
  194. /**
  195. * This function may be used to obtain the underlying representation of the
  196. * handle. This is intended to allow access to native handle functionality
  197. * that is not otherwise provided.
  198. */
  199. native_type native()
  200. {
  201. return this->get_service().native_handle(this->get_implementation());
  202. }
  203. /// Get the native handle representation.
  204. /**
  205. * This function may be used to obtain the underlying representation of the
  206. * handle. This is intended to allow access to native handle functionality
  207. * that is not otherwise provided.
  208. */
  209. native_handle_type native_handle()
  210. {
  211. return this->get_service().native_handle(this->get_implementation());
  212. }
  213. /// Cancel all asynchronous operations associated with the handle.
  214. /**
  215. * This function causes all outstanding asynchronous read or write operations
  216. * to finish immediately, and the handlers for cancelled operations will be
  217. * passed the boost::asio::error::operation_aborted error.
  218. *
  219. * @throws boost::system::system_error Thrown on failure.
  220. */
  221. void cancel()
  222. {
  223. boost::system::error_code ec;
  224. this->get_service().cancel(this->get_implementation(), ec);
  225. boost::asio::detail::throw_error(ec, "cancel");
  226. }
  227. /// Cancel all asynchronous operations associated with the handle.
  228. /**
  229. * This function causes all outstanding asynchronous read or write operations
  230. * to finish immediately, and the handlers for cancelled operations will be
  231. * passed the boost::asio::error::operation_aborted error.
  232. *
  233. * @param ec Set to indicate what error occurred, if any.
  234. */
  235. boost::system::error_code cancel(boost::system::error_code& ec)
  236. {
  237. return this->get_service().cancel(this->get_implementation(), ec);
  238. }
  239. protected:
  240. /// Protected destructor to prevent deletion through this type.
  241. ~basic_handle()
  242. {
  243. }
  244. };
  245. } // namespace windows
  246. } // namespace asio
  247. } // namespace boost
  248. #include <boost/asio/detail/pop_options.hpp>
  249. #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  250. // || defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  251. // || defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
  252. // || defined(GENERATING_DOCUMENTATION)
  253. #endif // BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP