5
// Copyright (c) 2003-2008 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 ASIO_WINDOWS_BASIC_HANDLE_HPP
12
#define ASIO_WINDOWS_BASIC_HANDLE_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
21
#include <boost/config.hpp>
22
#include "asio/detail/pop_options.hpp"
24
#include "asio/basic_io_object.hpp"
25
#include "asio/error.hpp"
26
#include "asio/detail/throw_error.hpp"
31
/// Provides Windows handle functionality.
33
* The windows::basic_handle class template provides the ability to wrap a
37
* @e Distinct @e objects: Safe.@n
38
* @e Shared @e objects: Unsafe.
40
template <typename HandleService>
42
: public basic_io_object<HandleService>
45
/// The native representation of a handle.
46
typedef typename HandleService::native_type native_type;
48
/// A basic_handle is always the lowest layer.
49
typedef basic_handle<HandleService> lowest_layer_type;
51
/// Construct a basic_handle without opening it.
53
* This constructor creates a handle without opening it.
55
* @param io_service The io_service object that the handle will use to
56
* dispatch handlers for any asynchronous operations performed on the handle.
58
explicit basic_handle(asio::io_service& io_service)
59
: basic_io_object<HandleService>(io_service)
63
/// Construct a basic_handle on an existing native handle.
65
* This constructor creates a handle object to hold an existing native handle.
67
* @param io_service The io_service object that the handle will use to
68
* dispatch handlers for any asynchronous operations performed on the handle.
70
* @param native_handle A native handle.
72
* @throws asio::system_error Thrown on failure.
74
basic_handle(asio::io_service& io_service,
75
const native_type& native_handle)
76
: basic_io_object<HandleService>(io_service)
79
this->service.assign(this->implementation, native_handle, ec);
80
asio::detail::throw_error(ec);
83
/// Get a reference to the lowest layer.
85
* This function returns a reference to the lowest layer in a stack of
86
* layers. Since a basic_handle cannot contain any further layers, it simply
87
* returns a reference to itself.
89
* @return A reference to the lowest layer in the stack of layers. Ownership
90
* is not transferred to the caller.
92
lowest_layer_type& lowest_layer()
97
/// Get a const reference to the lowest layer.
99
* This function returns a const reference to the lowest layer in a stack of
100
* layers. Since a basic_handle cannot contain any further layers, it simply
101
* returns a reference to itself.
103
* @return A const reference to the lowest layer in the stack of layers.
104
* Ownership is not transferred to the caller.
106
const lowest_layer_type& lowest_layer() const
111
/// Assign an existing native handle to the handle.
113
* This function opens the handle to hold an existing native handle.
115
* @param native_handle A native handle.
117
* @throws asio::system_error Thrown on failure.
119
void assign(const native_type& native_handle)
122
this->service.assign(this->implementation, native_handle, ec);
123
asio::detail::throw_error(ec);
126
/// Assign an existing native handle to the handle.
128
* This function opens the handle to hold an existing native handle.
130
* @param native_handle A native handle.
132
* @param ec Set to indicate what error occurred, if any.
134
asio::error_code assign(const native_type& native_handle,
135
asio::error_code& ec)
137
return this->service.assign(this->implementation, native_handle, ec);
140
/// Determine whether the handle is open.
143
return this->service.is_open(this->implementation);
146
/// Close the handle.
148
* This function is used to close the handle. Any asynchronous read or write
149
* operations will be cancelled immediately, and will complete with the
150
* asio::error::operation_aborted error.
152
* @throws asio::system_error Thrown on failure.
157
this->service.close(this->implementation, ec);
158
asio::detail::throw_error(ec);
161
/// Close the handle.
163
* This function is used to close the handle. Any asynchronous read or write
164
* operations will be cancelled immediately, and will complete with the
165
* asio::error::operation_aborted error.
167
* @param ec Set to indicate what error occurred, if any.
169
asio::error_code close(asio::error_code& ec)
171
return this->service.close(this->implementation, ec);
174
/// Get the native handle representation.
176
* This function may be used to obtain the underlying representation of the
177
* handle. This is intended to allow access to native handle functionality
178
* that is not otherwise provided.
182
return this->service.native(this->implementation);
185
/// Cancel all asynchronous operations associated with the handle.
187
* This function causes all outstanding asynchronous read or write operations
188
* to finish immediately, and the handlers for cancelled operations will be
189
* passed the asio::error::operation_aborted error.
191
* @throws asio::system_error Thrown on failure.
196
this->service.cancel(this->implementation, ec);
197
asio::detail::throw_error(ec);
200
/// Cancel all asynchronous operations associated with the handle.
202
* This function causes all outstanding asynchronous read or write operations
203
* to finish immediately, and the handlers for cancelled operations will be
204
* passed the asio::error::operation_aborted error.
206
* @param ec Set to indicate what error occurred, if any.
208
asio::error_code cancel(asio::error_code& ec)
210
return this->service.cancel(this->implementation, ec);
214
/// Protected destructor to prevent deletion through this type.
220
} // namespace windows
223
#include "asio/detail/pop_options.hpp"
225
#endif // ASIO_WINDOWS_BASIC_HANDLE_HPP