basic_object_handle.hpp 6.1 KB

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