basic_object_handle.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // windows/basic_object_handle.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2011 Boris Schaeling (boris@highscore.de)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP
  12. #define BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  18. #if defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) \
  19. || defined(GENERATING_DOCUMENTATION)
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/windows/basic_handle.hpp>
  23. #include <boost/asio/windows/object_handle_service.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace windows {
  28. /// Provides object-oriented handle functionality.
  29. /**
  30. * The windows::basic_object_handle class template provides asynchronous and
  31. * blocking object-oriented handle functionality.
  32. *
  33. * @par Thread Safety
  34. * @e Distinct @e objects: Safe.@n
  35. * @e Shared @e objects: Unsafe.
  36. */
  37. template <typename ObjectHandleService = object_handle_service>
  38. class basic_object_handle
  39. : public basic_handle<ObjectHandleService>
  40. {
  41. public:
  42. /// The native representation of a handle.
  43. typedef typename ObjectHandleService::native_handle_type native_handle_type;
  44. /// Construct a basic_object_handle without opening it.
  45. /**
  46. * This constructor creates an object handle without opening it.
  47. *
  48. * @param io_context The io_context object that the object handle will use to
  49. * dispatch handlers for any asynchronous operations performed on the handle.
  50. */
  51. explicit basic_object_handle(boost::asio::io_context& io_context)
  52. : basic_handle<ObjectHandleService>(io_context)
  53. {
  54. }
  55. /// Construct a basic_object_handle on an existing native handle.
  56. /**
  57. * This constructor creates an object handle object to hold an existing native
  58. * handle.
  59. *
  60. * @param io_context The io_context object that the object handle will use to
  61. * dispatch handlers for any asynchronous operations performed on the handle.
  62. *
  63. * @param native_handle The new underlying handle implementation.
  64. *
  65. * @throws boost::system::system_error Thrown on failure.
  66. */
  67. basic_object_handle(boost::asio::io_context& io_context,
  68. const native_handle_type& native_handle)
  69. : basic_handle<ObjectHandleService>(io_context, native_handle)
  70. {
  71. }
  72. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  73. /// Move-construct a basic_object_handle from another.
  74. /**
  75. * This constructor moves an object handle from one object to another.
  76. *
  77. * @param other The other basic_object_handle object from which the move will
  78. * occur.
  79. *
  80. * @note Following the move, the moved-from object is in the same state as if
  81. * constructed using the @c basic_object_handle(io_context&) constructor.
  82. */
  83. basic_object_handle(basic_object_handle&& other)
  84. : basic_handle<ObjectHandleService>(
  85. BOOST_ASIO_MOVE_CAST(basic_object_handle)(other))
  86. {
  87. }
  88. /// Move-assign a basic_object_handle from another.
  89. /**
  90. * This assignment operator moves an object handle from one object to another.
  91. *
  92. * @param other The other basic_object_handle object from which the move will
  93. * occur.
  94. *
  95. * @note Following the move, the moved-from object is in the same state as if
  96. * constructed using the @c basic_object_handle(io_context&) constructor.
  97. */
  98. basic_object_handle& operator=(basic_object_handle&& other)
  99. {
  100. basic_handle<ObjectHandleService>::operator=(
  101. BOOST_ASIO_MOVE_CAST(basic_object_handle)(other));
  102. return *this;
  103. }
  104. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  105. /// Perform a blocking wait on the object handle.
  106. /**
  107. * This function is used to wait for the object handle to be set to the
  108. * signalled state. This function blocks and does not return until the object
  109. * handle has been set to the signalled state.
  110. *
  111. * @throws boost::system::system_error Thrown on failure.
  112. */
  113. void wait()
  114. {
  115. boost::system::error_code ec;
  116. this->get_service().wait(this->get_implementation(), ec);
  117. boost::asio::detail::throw_error(ec, "wait");
  118. }
  119. /// Perform a blocking wait on the object handle.
  120. /**
  121. * This function is used to wait for the object handle to be set to the
  122. * signalled state. This function blocks and does not return until the object
  123. * handle has been set to the signalled state.
  124. *
  125. * @param ec Set to indicate what error occurred, if any.
  126. */
  127. void wait(boost::system::error_code& ec)
  128. {
  129. this->get_service().wait(this->get_implementation(), ec);
  130. }
  131. /// Start an asynchronous wait on the object handle.
  132. /**
  133. * This function is be used to initiate an asynchronous wait against the
  134. * object handle. It always returns immediately.
  135. *
  136. * @param handler The handler to be called when the object handle is set to
  137. * the signalled state. Copies will be made of the handler as required. The
  138. * function signature of the handler must be:
  139. * @code void handler(
  140. * const boost::system::error_code& error // Result of operation.
  141. * ); @endcode
  142. * Regardless of whether the asynchronous operation completes immediately or
  143. * not, the handler will not be invoked from within this function. Invocation
  144. * of the handler will be performed in a manner equivalent to using
  145. * boost::asio::io_context::post().
  146. */
  147. template <typename WaitHandler>
  148. BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
  149. void (boost::system::error_code))
  150. async_wait(BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
  151. {
  152. return this->get_service().async_wait(this->get_implementation(),
  153. BOOST_ASIO_MOVE_CAST(WaitHandler)(handler));
  154. }
  155. };
  156. } // namespace windows
  157. } // namespace asio
  158. } // namespace boost
  159. #include <boost/asio/detail/pop_options.hpp>
  160. #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
  161. // || defined(GENERATING_DOCUMENTATION)
  162. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  163. #endif // BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP