Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
8.1KB

  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. /// The native representation of a handle.
  41. typedef typename HandleService::native_handle_type native_handle_type;
  42. /// A basic_handle is always the lowest layer.
  43. typedef basic_handle<HandleService> lowest_layer_type;
  44. /// Construct a basic_handle without opening it.
  45. /**
  46. * This constructor creates a handle without opening it.
  47. *
  48. * @param io_context The io_context object that the handle will use to
  49. * dispatch handlers for any asynchronous operations performed on the handle.
  50. */
  51. explicit basic_handle(asio::io_context& io_context)
  52. : basic_io_object<HandleService>(io_context)
  53. {
  54. }
  55. /// Construct a basic_handle on an existing native handle.
  56. /**
  57. * This constructor creates a handle object to hold an existing native handle.
  58. *
  59. * @param io_context The io_context object that the handle will use to
  60. * dispatch handlers for any asynchronous operations performed on the handle.
  61. *
  62. * @param handle A native handle.
  63. *
  64. * @throws asio::system_error Thrown on failure.
  65. */
  66. basic_handle(asio::io_context& io_context,
  67. const native_handle_type& handle)
  68. : basic_io_object<HandleService>(io_context)
  69. {
  70. asio::error_code ec;
  71. this->get_service().assign(this->get_implementation(), handle, ec);
  72. asio::detail::throw_error(ec, "assign");
  73. }
  74. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  75. /// Move-construct a basic_handle from another.
  76. /**
  77. * This constructor moves a handle from one object to another.
  78. *
  79. * @param other The other basic_handle object from which the move will occur.
  80. *
  81. * @note Following the move, the moved-from object is in the same state as if
  82. * constructed using the @c basic_handle(io_context&) constructor.
  83. */
  84. basic_handle(basic_handle&& other)
  85. : basic_io_object<HandleService>(
  86. ASIO_MOVE_CAST(basic_handle)(other))
  87. {
  88. }
  89. /// Move-assign a basic_handle from another.
  90. /**
  91. * This assignment operator moves a handle from one object to another.
  92. *
  93. * @param other The other basic_handle object from which the move will occur.
  94. *
  95. * @note Following the move, the moved-from object is in the same state as if
  96. * constructed using the @c basic_handle(io_context&) constructor.
  97. */
  98. basic_handle& operator=(basic_handle&& other)
  99. {
  100. basic_io_object<HandleService>::operator=(
  101. ASIO_MOVE_CAST(basic_handle)(other));
  102. return *this;
  103. }
  104. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  105. /// Get a reference to the lowest layer.
  106. /**
  107. * This function returns a reference to the lowest layer in a stack of
  108. * layers. Since a basic_handle cannot contain any further layers, it simply
  109. * returns a reference to itself.
  110. *
  111. * @return A reference to the lowest layer in the stack of layers. Ownership
  112. * is not transferred to the caller.
  113. */
  114. lowest_layer_type& lowest_layer()
  115. {
  116. return *this;
  117. }
  118. /// Get a const reference to the lowest layer.
  119. /**
  120. * This function returns a const reference to the lowest layer in a stack of
  121. * layers. Since a basic_handle cannot contain any further layers, it simply
  122. * returns a reference to itself.
  123. *
  124. * @return A const reference to the lowest layer in the stack of layers.
  125. * Ownership is not transferred to the caller.
  126. */
  127. const lowest_layer_type& lowest_layer() const
  128. {
  129. return *this;
  130. }
  131. /// Assign an existing native handle to the handle.
  132. /*
  133. * This function opens the handle to hold an existing native handle.
  134. *
  135. * @param handle A native handle.
  136. *
  137. * @throws asio::system_error Thrown on failure.
  138. */
  139. void assign(const native_handle_type& handle)
  140. {
  141. asio::error_code ec;
  142. this->get_service().assign(this->get_implementation(), handle, ec);
  143. asio::detail::throw_error(ec, "assign");
  144. }
  145. /// Assign an existing native handle to the handle.
  146. /*
  147. * This function opens the handle to hold an existing native handle.
  148. *
  149. * @param handle A native handle.
  150. *
  151. * @param ec Set to indicate what error occurred, if any.
  152. */
  153. asio::error_code assign(const native_handle_type& handle,
  154. asio::error_code& ec)
  155. {
  156. return this->get_service().assign(this->get_implementation(), handle, ec);
  157. }
  158. /// Determine whether the handle is open.
  159. bool is_open() const
  160. {
  161. return this->get_service().is_open(this->get_implementation());
  162. }
  163. /// Close the handle.
  164. /**
  165. * This function is used to close the handle. Any asynchronous read or write
  166. * operations will be cancelled immediately, and will complete with the
  167. * asio::error::operation_aborted error.
  168. *
  169. * @throws asio::system_error Thrown on failure.
  170. */
  171. void close()
  172. {
  173. asio::error_code ec;
  174. this->get_service().close(this->get_implementation(), ec);
  175. asio::detail::throw_error(ec, "close");
  176. }
  177. /// Close the handle.
  178. /**
  179. * This function is used to close the handle. Any asynchronous read or write
  180. * operations will be cancelled immediately, and will complete with the
  181. * asio::error::operation_aborted error.
  182. *
  183. * @param ec Set to indicate what error occurred, if any.
  184. */
  185. asio::error_code close(asio::error_code& ec)
  186. {
  187. return this->get_service().close(this->get_implementation(), ec);
  188. }
  189. /// Get the native handle representation.
  190. /**
  191. * This function may be used to obtain the underlying representation of the
  192. * handle. This is intended to allow access to native handle functionality
  193. * that is not otherwise provided.
  194. */
  195. native_handle_type native_handle()
  196. {
  197. return this->get_service().native_handle(this->get_implementation());
  198. }
  199. /// Cancel all asynchronous operations associated with the handle.
  200. /**
  201. * This function causes all outstanding asynchronous read or write operations
  202. * to finish immediately, and the handlers for cancelled operations will be
  203. * passed the asio::error::operation_aborted error.
  204. *
  205. * @throws asio::system_error Thrown on failure.
  206. */
  207. void cancel()
  208. {
  209. asio::error_code ec;
  210. this->get_service().cancel(this->get_implementation(), ec);
  211. asio::detail::throw_error(ec, "cancel");
  212. }
  213. /// Cancel all asynchronous operations associated with the handle.
  214. /**
  215. * This function causes all outstanding asynchronous read or write operations
  216. * to finish immediately, and the handlers for cancelled operations will be
  217. * passed the asio::error::operation_aborted error.
  218. *
  219. * @param ec Set to indicate what error occurred, if any.
  220. */
  221. asio::error_code cancel(asio::error_code& ec)
  222. {
  223. return this->get_service().cancel(this->get_implementation(), ec);
  224. }
  225. protected:
  226. /// Protected destructor to prevent deletion through this type.
  227. ~basic_handle()
  228. {
  229. }
  230. };
  231. } // namespace windows
  232. } // namespace asio
  233. #include "asio/detail/pop_options.hpp"
  234. #endif // defined(ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  235. // || defined(ASIO_HAS_WINDOWS_STREAM_HANDLE)
  236. // || defined(ASIO_HAS_WINDOWS_OBJECT_HANDLE)
  237. // || defined(GENERATING_DOCUMENTATION)
  238. #endif // ASIO_WINDOWS_BASIC_HANDLE_HPP