2 // windows/basic_object_handle.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2011 Boris Schaeling (boris@highscore.de)
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)
12 #ifndef BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP
13 #define BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
23 #if defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) \
24 || defined(GENERATING_DOCUMENTATION)
26 #include <boost/asio/detail/throw_error.hpp>
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/windows/basic_handle.hpp>
29 #include <boost/asio/windows/object_handle_service.hpp>
31 #include <boost/asio/detail/push_options.hpp>
37 /// Provides object-oriented handle functionality.
39 * The windows::basic_object_handle class template provides asynchronous and
40 * blocking object-oriented handle functionality.
43 * @e Distinct @e objects: Safe.@n
44 * @e Shared @e objects: Unsafe.
46 template <typename ObjectHandleService = object_handle_service>
47 class basic_object_handle
48 : public basic_handle<ObjectHandleService>
51 /// The native representation of a handle.
52 typedef typename ObjectHandleService::native_handle_type native_handle_type;
54 /// Construct a basic_object_handle without opening it.
56 * This constructor creates an object handle without opening it.
58 * @param io_context The io_context object that the object handle will use to
59 * dispatch handlers for any asynchronous operations performed on the handle.
61 explicit basic_object_handle(boost::asio::io_context& io_context)
62 : basic_handle<ObjectHandleService>(io_context)
66 /// Construct a basic_object_handle on an existing native handle.
68 * This constructor creates an object handle object to hold an existing native
71 * @param io_context The io_context object that the object handle will use to
72 * dispatch handlers for any asynchronous operations performed on the handle.
74 * @param native_handle The new underlying handle implementation.
76 * @throws boost::system::system_error Thrown on failure.
78 basic_object_handle(boost::asio::io_context& io_context,
79 const native_handle_type& native_handle)
80 : basic_handle<ObjectHandleService>(io_context, native_handle)
84 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
85 /// Move-construct a basic_object_handle from another.
87 * This constructor moves an object handle from one object to another.
89 * @param other The other basic_object_handle object from which the move will
92 * @note Following the move, the moved-from object is in the same state as if
93 * constructed using the @c basic_object_handle(io_context&) constructor.
95 basic_object_handle(basic_object_handle&& other)
96 : basic_handle<ObjectHandleService>(
97 BOOST_ASIO_MOVE_CAST(basic_object_handle)(other))
101 /// Move-assign a basic_object_handle from another.
103 * This assignment operator moves an object handle from one object to another.
105 * @param other The other basic_object_handle object from which the move will
108 * @note Following the move, the moved-from object is in the same state as if
109 * constructed using the @c basic_object_handle(io_context&) constructor.
111 basic_object_handle& operator=(basic_object_handle&& other)
113 basic_handle<ObjectHandleService>::operator=(
114 BOOST_ASIO_MOVE_CAST(basic_object_handle)(other));
117 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
119 /// Perform a blocking wait on the object handle.
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.
125 * @throws boost::system::system_error Thrown on failure.
129 boost::system::error_code ec;
130 this->get_service().wait(this->get_implementation(), ec);
131 boost::asio::detail::throw_error(ec, "wait");
134 /// Perform a blocking wait on the object handle.
136 * This function is used to wait for the object handle to be set to the
137 * signalled state. This function blocks and does not return until the object
138 * handle has been set to the signalled state.
140 * @param ec Set to indicate what error occurred, if any.
142 void wait(boost::system::error_code& ec)
144 this->get_service().wait(this->get_implementation(), ec);
147 /// Start an asynchronous wait on the object handle.
149 * This function is be used to initiate an asynchronous wait against the
150 * object handle. It always returns immediately.
152 * @param handler The handler to be called when the object handle is set to
153 * the signalled state. Copies will be made of the handler as required. The
154 * function signature of the handler must be:
155 * @code void handler(
156 * const boost::system::error_code& error // Result of operation.
158 * Regardless of whether the asynchronous operation completes immediately or
159 * not, the handler will not be invoked from within this function. Invocation
160 * of the handler will be performed in a manner equivalent to using
161 * boost::asio::io_context::post().
163 template <typename WaitHandler>
164 BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
165 void (boost::system::error_code))
166 async_wait(BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
168 return this->get_service().async_wait(this->get_implementation(),
169 BOOST_ASIO_MOVE_CAST(WaitHandler)(handler));
173 } // namespace windows
177 #include <boost/asio/detail/pop_options.hpp>
179 #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
180 // || defined(GENERATING_DOCUMENTATION)
182 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
184 #endif // BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP