basic_handle.hpp 6.8 KB

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