basic_handle.hpp 8.6 KB

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