2 // windows/basic_handle.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP
12 #define BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
22 #if defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \
23 || defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
24 || defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) \
25 || defined(GENERATING_DOCUMENTATION)
27 #include <boost/asio/basic_io_object.hpp>
28 #include <boost/asio/detail/throw_error.hpp>
29 #include <boost/asio/error.hpp>
31 #include <boost/asio/detail/push_options.hpp>
37 /// Provides Windows handle functionality.
39 * The windows::basic_handle class template provides the ability to wrap a
43 * @e Distinct @e objects: Safe.@n
44 * @e Shared @e objects: Unsafe.
46 template <typename HandleService>
48 : public basic_io_object<HandleService>
51 /// The native representation of a handle.
52 typedef typename HandleService::native_handle_type native_handle_type;
54 /// A basic_handle is always the lowest layer.
55 typedef basic_handle<HandleService> lowest_layer_type;
57 /// Construct a basic_handle without opening it.
59 * This constructor creates a handle without opening it.
61 * @param io_context The io_context object that the handle will use to
62 * dispatch handlers for any asynchronous operations performed on the handle.
64 explicit basic_handle(boost::asio::io_context& io_context)
65 : basic_io_object<HandleService>(io_context)
69 /// Construct a basic_handle on an existing native handle.
71 * This constructor creates a handle object to hold an existing native handle.
73 * @param io_context The io_context object that the handle will use to
74 * dispatch handlers for any asynchronous operations performed on the handle.
76 * @param handle A native handle.
78 * @throws boost::system::system_error Thrown on failure.
80 basic_handle(boost::asio::io_context& io_context,
81 const native_handle_type& handle)
82 : basic_io_object<HandleService>(io_context)
84 boost::system::error_code ec;
85 this->get_service().assign(this->get_implementation(), handle, ec);
86 boost::asio::detail::throw_error(ec, "assign");
89 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
90 /// Move-construct a basic_handle from another.
92 * This constructor moves a handle from one object to another.
94 * @param other The other basic_handle object from which the move will occur.
96 * @note Following the move, the moved-from object is in the same state as if
97 * constructed using the @c basic_handle(io_context&) constructor.
99 basic_handle(basic_handle&& other)
100 : basic_io_object<HandleService>(
101 BOOST_ASIO_MOVE_CAST(basic_handle)(other))
105 /// Move-assign a basic_handle from another.
107 * This assignment operator moves a handle from one object to another.
109 * @param other The other basic_handle object from which the move will occur.
111 * @note Following the move, the moved-from object is in the same state as if
112 * constructed using the @c basic_handle(io_context&) constructor.
114 basic_handle& operator=(basic_handle&& other)
116 basic_io_object<HandleService>::operator=(
117 BOOST_ASIO_MOVE_CAST(basic_handle)(other));
120 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
122 /// Get a reference to the lowest layer.
124 * This function returns a reference to the lowest layer in a stack of
125 * layers. Since a basic_handle cannot contain any further layers, it simply
126 * returns a reference to itself.
128 * @return A reference to the lowest layer in the stack of layers. Ownership
129 * is not transferred to the caller.
131 lowest_layer_type& lowest_layer()
136 /// Get a const reference to the lowest layer.
138 * This function returns a const reference to the lowest layer in a stack of
139 * layers. Since a basic_handle cannot contain any further layers, it simply
140 * returns a reference to itself.
142 * @return A const reference to the lowest layer in the stack of layers.
143 * Ownership is not transferred to the caller.
145 const lowest_layer_type& lowest_layer() const
150 /// Assign an existing native handle to the handle.
152 * This function opens the handle to hold an existing native handle.
154 * @param handle A native handle.
156 * @throws boost::system::system_error Thrown on failure.
158 void assign(const native_handle_type& handle)
160 boost::system::error_code ec;
161 this->get_service().assign(this->get_implementation(), handle, ec);
162 boost::asio::detail::throw_error(ec, "assign");
165 /// Assign an existing native handle to the handle.
167 * This function opens the handle to hold an existing native handle.
169 * @param handle A native handle.
171 * @param ec Set to indicate what error occurred, if any.
173 BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& handle,
174 boost::system::error_code& ec)
176 this->get_service().assign(this->get_implementation(), handle, ec);
177 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
180 /// Determine whether the handle is open.
183 return this->get_service().is_open(this->get_implementation());
186 /// Close the handle.
188 * This function is used to close the handle. Any asynchronous read or write
189 * operations will be cancelled immediately, and will complete with the
190 * boost::asio::error::operation_aborted error.
192 * @throws boost::system::system_error Thrown on failure.
196 boost::system::error_code ec;
197 this->get_service().close(this->get_implementation(), ec);
198 boost::asio::detail::throw_error(ec, "close");
201 /// Close the handle.
203 * This function is used to close the handle. Any asynchronous read or write
204 * operations will be cancelled immediately, and will complete with the
205 * boost::asio::error::operation_aborted error.
207 * @param ec Set to indicate what error occurred, if any.
209 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
211 this->get_service().close(this->get_implementation(), ec);
212 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
215 /// Get the native handle representation.
217 * This function may be used to obtain the underlying representation of the
218 * handle. This is intended to allow access to native handle functionality
219 * that is not otherwise provided.
221 native_handle_type native_handle()
223 return this->get_service().native_handle(this->get_implementation());
226 /// Cancel all asynchronous operations associated with the handle.
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.
232 * @throws boost::system::system_error Thrown on failure.
236 boost::system::error_code ec;
237 this->get_service().cancel(this->get_implementation(), ec);
238 boost::asio::detail::throw_error(ec, "cancel");
241 /// Cancel all asynchronous operations associated with the handle.
243 * This function causes all outstanding asynchronous read or write operations
244 * to finish immediately, and the handlers for cancelled operations will be
245 * passed the boost::asio::error::operation_aborted error.
247 * @param ec Set to indicate what error occurred, if any.
249 BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
251 this->get_service().cancel(this->get_implementation(), ec);
252 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
256 /// Protected destructor to prevent deletion through this type.
262 } // namespace windows
266 #include <boost/asio/detail/pop_options.hpp>
268 #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
269 // || defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
270 // || defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
271 // || defined(GENERATING_DOCUMENTATION)
273 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
275 #endif // BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP