]>
Commit | Line | Data |
---|---|---|
1 | // | |
2 | // windows/basic_handle.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2018 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 | ||
11 | #ifndef BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP | |
12 | #define BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
20 | #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) | |
21 | ||
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) | |
26 | ||
27 | #include <boost/asio/basic_io_object.hpp> | |
28 | #include <boost/asio/detail/throw_error.hpp> | |
29 | #include <boost/asio/error.hpp> | |
30 | ||
31 | #include <boost/asio/detail/push_options.hpp> | |
32 | ||
33 | namespace boost { | |
34 | namespace asio { | |
35 | namespace windows { | |
36 | ||
37 | /// Provides Windows handle functionality. | |
38 | /** | |
39 | * The windows::basic_handle class template provides the ability to wrap a | |
40 | * Windows handle. | |
41 | * | |
42 | * @par Thread Safety | |
43 | * @e Distinct @e objects: Safe.@n | |
44 | * @e Shared @e objects: Unsafe. | |
45 | */ | |
46 | template <typename HandleService> | |
47 | class basic_handle | |
48 | : public basic_io_object<HandleService> | |
49 | { | |
50 | public: | |
51 | /// The native representation of a handle. | |
52 | typedef typename HandleService::native_handle_type native_handle_type; | |
53 | ||
54 | /// A basic_handle is always the lowest layer. | |
55 | typedef basic_handle<HandleService> lowest_layer_type; | |
56 | ||
57 | /// Construct a basic_handle without opening it. | |
58 | /** | |
59 | * This constructor creates a handle without opening it. | |
60 | * | |
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. | |
63 | */ | |
64 | explicit basic_handle(boost::asio::io_context& io_context) | |
65 | : basic_io_object<HandleService>(io_context) | |
66 | { | |
67 | } | |
68 | ||
69 | /// Construct a basic_handle on an existing native handle. | |
70 | /** | |
71 | * This constructor creates a handle object to hold an existing native handle. | |
72 | * | |
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. | |
75 | * | |
76 | * @param handle A native handle. | |
77 | * | |
78 | * @throws boost::system::system_error Thrown on failure. | |
79 | */ | |
80 | basic_handle(boost::asio::io_context& io_context, | |
81 | const native_handle_type& handle) | |
82 | : basic_io_object<HandleService>(io_context) | |
83 | { | |
84 | boost::system::error_code ec; | |
85 | this->get_service().assign(this->get_implementation(), handle, ec); | |
86 | boost::asio::detail::throw_error(ec, "assign"); | |
87 | } | |
88 | ||
89 | #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
90 | /// Move-construct a basic_handle from another. | |
91 | /** | |
92 | * This constructor moves a handle from one object to another. | |
93 | * | |
94 | * @param other The other basic_handle object from which the move will occur. | |
95 | * | |
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. | |
98 | */ | |
99 | basic_handle(basic_handle&& other) | |
100 | : basic_io_object<HandleService>( | |
101 | BOOST_ASIO_MOVE_CAST(basic_handle)(other)) | |
102 | { | |
103 | } | |
104 | ||
105 | /// Move-assign a basic_handle from another. | |
106 | /** | |
107 | * This assignment operator moves a handle from one object to another. | |
108 | * | |
109 | * @param other The other basic_handle object from which the move will occur. | |
110 | * | |
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. | |
113 | */ | |
114 | basic_handle& operator=(basic_handle&& other) | |
115 | { | |
116 | basic_io_object<HandleService>::operator=( | |
117 | BOOST_ASIO_MOVE_CAST(basic_handle)(other)); | |
118 | return *this; | |
119 | } | |
120 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
121 | ||
122 | /// Get a reference to the lowest layer. | |
123 | /** | |
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. | |
127 | * | |
128 | * @return A reference to the lowest layer in the stack of layers. Ownership | |
129 | * is not transferred to the caller. | |
130 | */ | |
131 | lowest_layer_type& lowest_layer() | |
132 | { | |
133 | return *this; | |
134 | } | |
135 | ||
136 | /// Get a const reference to the lowest layer. | |
137 | /** | |
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. | |
141 | * | |
142 | * @return A const reference to the lowest layer in the stack of layers. | |
143 | * Ownership is not transferred to the caller. | |
144 | */ | |
145 | const lowest_layer_type& lowest_layer() const | |
146 | { | |
147 | return *this; | |
148 | } | |
149 | ||
150 | /// Assign an existing native handle to the handle. | |
151 | /* | |
152 | * This function opens the handle to hold an existing native handle. | |
153 | * | |
154 | * @param handle A native handle. | |
155 | * | |
156 | * @throws boost::system::system_error Thrown on failure. | |
157 | */ | |
158 | void assign(const native_handle_type& handle) | |
159 | { | |
160 | boost::system::error_code ec; | |
161 | this->get_service().assign(this->get_implementation(), handle, ec); | |
162 | boost::asio::detail::throw_error(ec, "assign"); | |
163 | } | |
164 | ||
165 | /// Assign an existing native handle to the handle. | |
166 | /* | |
167 | * This function opens the handle to hold an existing native handle. | |
168 | * | |
169 | * @param handle A native handle. | |
170 | * | |
171 | * @param ec Set to indicate what error occurred, if any. | |
172 | */ | |
173 | BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& handle, | |
174 | boost::system::error_code& ec) | |
175 | { | |
176 | this->get_service().assign(this->get_implementation(), handle, ec); | |
177 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
178 | } | |
179 | ||
180 | /// Determine whether the handle is open. | |
181 | bool is_open() const | |
182 | { | |
183 | return this->get_service().is_open(this->get_implementation()); | |
184 | } | |
185 | ||
186 | /// Close the handle. | |
187 | /** | |
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. | |
191 | * | |
192 | * @throws boost::system::system_error Thrown on failure. | |
193 | */ | |
194 | void close() | |
195 | { | |
196 | boost::system::error_code ec; | |
197 | this->get_service().close(this->get_implementation(), ec); | |
198 | boost::asio::detail::throw_error(ec, "close"); | |
199 | } | |
200 | ||
201 | /// Close the handle. | |
202 | /** | |
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. | |
206 | * | |
207 | * @param ec Set to indicate what error occurred, if any. | |
208 | */ | |
209 | BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec) | |
210 | { | |
211 | this->get_service().close(this->get_implementation(), ec); | |
212 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
213 | } | |
214 | ||
215 | /// Get the native handle representation. | |
216 | /** | |
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. | |
220 | */ | |
221 | native_handle_type native_handle() | |
222 | { | |
223 | return this->get_service().native_handle(this->get_implementation()); | |
224 | } | |
225 | ||
226 | /// Cancel all asynchronous operations associated with the handle. | |
227 | /** | |
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. | |
231 | * | |
232 | * @throws boost::system::system_error Thrown on failure. | |
233 | */ | |
234 | void cancel() | |
235 | { | |
236 | boost::system::error_code ec; | |
237 | this->get_service().cancel(this->get_implementation(), ec); | |
238 | boost::asio::detail::throw_error(ec, "cancel"); | |
239 | } | |
240 | ||
241 | /// Cancel all asynchronous operations associated with the handle. | |
242 | /** | |
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. | |
246 | * | |
247 | * @param ec Set to indicate what error occurred, if any. | |
248 | */ | |
249 | BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec) | |
250 | { | |
251 | this->get_service().cancel(this->get_implementation(), ec); | |
252 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
253 | } | |
254 | ||
255 | protected: | |
256 | /// Protected destructor to prevent deletion through this type. | |
257 | ~basic_handle() | |
258 | { | |
259 | } | |
260 | }; | |
261 | ||
262 | } // namespace windows | |
263 | } // namespace asio | |
264 | } // namespace boost | |
265 | ||
266 | #include <boost/asio/detail/pop_options.hpp> | |
267 | ||
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) | |
272 | ||
273 | #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) | |
274 | ||
275 | #endif // BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP |