basic_handle.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // basic_handle.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_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/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #include "asio/detail/pop_options.hpp"
  19. #include "asio/basic_io_object.hpp"
  20. #include "asio/error.hpp"
  21. #include "asio/detail/throw_error.hpp"
  22. namespace asio {
  23. namespace windows {
  24. /// Provides Windows handle functionality.
  25. /**
  26. * The windows::basic_handle class template provides the ability to wrap a
  27. * Windows handle.
  28. *
  29. * @par Thread Safety
  30. * @e Distinct @e objects: Safe.@n
  31. * @e Shared @e objects: Unsafe.
  32. */
  33. template <typename HandleService>
  34. class basic_handle
  35. : public basic_io_object<HandleService>
  36. {
  37. public:
  38. /// The native representation of a handle.
  39. typedef typename HandleService::native_type native_type;
  40. /// A basic_handle is always the lowest layer.
  41. typedef basic_handle<HandleService> lowest_layer_type;
  42. /// Construct a basic_handle without opening it.
  43. /**
  44. * This constructor creates a handle without opening it.
  45. *
  46. * @param io_service The io_service object that the handle will use to
  47. * dispatch handlers for any asynchronous operations performed on the handle.
  48. */
  49. explicit basic_handle(asio::io_service& io_service)
  50. : basic_io_object<HandleService>(io_service)
  51. {
  52. }
  53. /// Construct a basic_handle on an existing native handle.
  54. /**
  55. * This constructor creates a handle object to hold an existing native handle.
  56. *
  57. * @param io_service The io_service object that the handle will use to
  58. * dispatch handlers for any asynchronous operations performed on the handle.
  59. *
  60. * @param native_handle A native handle.
  61. *
  62. * @throws asio::system_error Thrown on failure.
  63. */
  64. basic_handle(asio::io_service& io_service,
  65. const native_type& native_handle)
  66. : basic_io_object<HandleService>(io_service)
  67. {
  68. asio::error_code ec;
  69. this->service.assign(this->implementation, native_handle, ec);
  70. asio::detail::throw_error(ec);
  71. }
  72. /// Get a reference to the lowest layer.
  73. /**
  74. * This function returns a reference to the lowest layer in a stack of
  75. * layers. Since a basic_handle cannot contain any further layers, it simply
  76. * returns a reference to itself.
  77. *
  78. * @return A reference to the lowest layer in the stack of layers. Ownership
  79. * is not transferred to the caller.
  80. */
  81. lowest_layer_type& lowest_layer()
  82. {
  83. return *this;
  84. }
  85. /// Get a const reference to the lowest layer.
  86. /**
  87. * This function returns a const reference to the lowest layer in a stack of
  88. * layers. Since a basic_handle cannot contain any further layers, it simply
  89. * returns a reference to itself.
  90. *
  91. * @return A const reference to the lowest layer in the stack of layers.
  92. * Ownership is not transferred to the caller.
  93. */
  94. const lowest_layer_type& lowest_layer() const
  95. {
  96. return *this;
  97. }
  98. /// Assign an existing native handle to the handle.
  99. /*
  100. * This function opens the handle to hold an existing native handle.
  101. *
  102. * @param native_handle A native handle.
  103. *
  104. * @throws asio::system_error Thrown on failure.
  105. */
  106. void assign(const native_type& native_handle)
  107. {
  108. asio::error_code ec;
  109. this->service.assign(this->implementation, native_handle, ec);
  110. asio::detail::throw_error(ec);
  111. }
  112. /// Assign an existing native handle to the handle.
  113. /*
  114. * This function opens the handle to hold an existing native handle.
  115. *
  116. * @param native_handle A native handle.
  117. *
  118. * @param ec Set to indicate what error occurred, if any.
  119. */
  120. asio::error_code assign(const native_type& native_handle,
  121. asio::error_code& ec)
  122. {
  123. return this->service.assign(this->implementation, native_handle, ec);
  124. }
  125. /// Determine whether the handle is open.
  126. bool is_open() const
  127. {
  128. return this->service.is_open(this->implementation);
  129. }
  130. /// Close the handle.
  131. /**
  132. * This function is used to close the handle. Any asynchronous read or write
  133. * operations will be cancelled immediately, and will complete with the
  134. * asio::error::operation_aborted error.
  135. *
  136. * @throws asio::system_error Thrown on failure.
  137. */
  138. void close()
  139. {
  140. asio::error_code ec;
  141. this->service.close(this->implementation, ec);
  142. asio::detail::throw_error(ec);
  143. }
  144. /// Close the handle.
  145. /**
  146. * This function is used to close the handle. Any asynchronous read or write
  147. * operations will be cancelled immediately, and will complete with the
  148. * asio::error::operation_aborted error.
  149. *
  150. * @param ec Set to indicate what error occurred, if any.
  151. */
  152. asio::error_code close(asio::error_code& ec)
  153. {
  154. return this->service.close(this->implementation, ec);
  155. }
  156. /// Get the native handle representation.
  157. /**
  158. * This function may be used to obtain the underlying representation of the
  159. * handle. This is intended to allow access to native handle functionality
  160. * that is not otherwise provided.
  161. */
  162. native_type native()
  163. {
  164. return this->service.native(this->implementation);
  165. }
  166. /// Cancel all asynchronous operations associated with the handle.
  167. /**
  168. * This function causes all outstanding asynchronous read or write operations
  169. * to finish immediately, and the handlers for cancelled operations will be
  170. * passed the asio::error::operation_aborted error.
  171. *
  172. * @throws asio::system_error Thrown on failure.
  173. */
  174. void cancel()
  175. {
  176. asio::error_code ec;
  177. this->service.cancel(this->implementation, ec);
  178. asio::detail::throw_error(ec);
  179. }
  180. /// Cancel all asynchronous operations associated with the handle.
  181. /**
  182. * This function causes all outstanding asynchronous read or write operations
  183. * to finish immediately, and the handlers for cancelled operations will be
  184. * passed the asio::error::operation_aborted error.
  185. *
  186. * @param ec Set to indicate what error occurred, if any.
  187. */
  188. asio::error_code cancel(asio::error_code& ec)
  189. {
  190. return this->service.cancel(this->implementation, ec);
  191. }
  192. protected:
  193. /// Protected destructor to prevent deletion through this type.
  194. ~basic_handle()
  195. {
  196. }
  197. };
  198. } // namespace windows
  199. } // namespace asio
  200. #include "asio/detail/pop_options.hpp"
  201. #endif // ASIO_WINDOWS_BASIC_HANDLE_HPP