basic_handle.hpp 8.6 KB

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