/Src/Dependencies/Boost/boost/asio/windows/basic_handle.hpp

http://hadesmem.googlecode.com/ · C++ Header · 281 lines · 111 code · 30 blank · 140 comment · 4 complexity · 5cfb11a204586d9b04ddb98780c7a92b MD5 · raw file

  1. //
  2. // windows/basic_handle.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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(GENERATING_DOCUMENTATION)
  19. #include <boost/asio/basic_io_object.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  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(boost::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 boost::system::system_error Thrown on failure.
  68. */
  69. basic_handle(boost::asio::io_service& io_service,
  70. const native_handle_type& handle)
  71. : basic_io_object<HandleService>(io_service)
  72. {
  73. boost::system::error_code ec;
  74. this->get_service().assign(this->get_implementation(), handle, ec);
  75. boost::asio::detail::throw_error(ec, "assign");
  76. }
  77. #if defined(BOOST_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. BOOST_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. BOOST_ASIO_MOVE_CAST(basic_handle)(other));
  105. return *this;
  106. }
  107. #endif // defined(BOOST_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 boost::system::system_error Thrown on failure.
  141. */
  142. void assign(const native_handle_type& handle)
  143. {
  144. boost::system::error_code ec;
  145. this->get_service().assign(this->get_implementation(), handle, ec);
  146. boost::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. boost::system::error_code assign(const native_handle_type& handle,
  157. boost::system::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. * 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::system::error_code close(boost::system::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 boost::asio::error::operation_aborted error.
  217. *
  218. * @throws boost::system::system_error Thrown on failure.
  219. */
  220. void cancel()
  221. {
  222. boost::system::error_code ec;
  223. this->get_service().cancel(this->get_implementation(), ec);
  224. boost::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 boost::asio::error::operation_aborted error.
  231. *
  232. * @param ec Set to indicate what error occurred, if any.
  233. */
  234. boost::system::error_code cancel(boost::system::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. } // namespace boost
  247. #include <boost/asio/detail/pop_options.hpp>
  248. #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  249. // || defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  250. // || defined(GENERATING_DOCUMENTATION)
  251. #endif // BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP