Chris@16: // Chris@16: // windows/basic_handle.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP Chris@16: #define BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \ Chris@16: || defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \ Chris@16: || defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) \ Chris@16: || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace windows { Chris@16: Chris@16: /// Provides Windows handle functionality. Chris@16: /** Chris@16: * The windows::basic_handle class template provides the ability to wrap a Chris@16: * Windows handle. Chris@16: * Chris@16: * @par Thread Safety Chris@16: * @e Distinct @e objects: Safe.@n Chris@16: * @e Shared @e objects: Unsafe. Chris@16: */ Chris@16: template Chris@16: class basic_handle Chris@16: : public basic_io_object Chris@16: { Chris@16: public: Chris@16: /// (Deprecated: Use native_handle_type.) The native representation of a Chris@16: /// handle. Chris@16: typedef typename HandleService::native_handle_type native_type; Chris@16: Chris@16: /// The native representation of a handle. Chris@16: typedef typename HandleService::native_handle_type native_handle_type; Chris@16: Chris@16: /// A basic_handle is always the lowest layer. Chris@16: typedef basic_handle lowest_layer_type; Chris@16: Chris@16: /// Construct a basic_handle without opening it. Chris@16: /** Chris@16: * This constructor creates a handle without opening it. Chris@16: * Chris@16: * @param io_service The io_service object that the handle will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the handle. Chris@16: */ Chris@16: explicit basic_handle(boost::asio::io_service& io_service) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a basic_handle on an existing native handle. Chris@16: /** Chris@16: * This constructor creates a handle object to hold an existing native handle. Chris@16: * Chris@16: * @param io_service The io_service object that the handle will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the handle. Chris@16: * Chris@16: * @param handle A native handle. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_handle(boost::asio::io_service& io_service, Chris@16: const native_handle_type& handle) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().assign(this->get_implementation(), handle, ec); Chris@16: boost::asio::detail::throw_error(ec, "assign"); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a basic_handle from another. Chris@16: /** Chris@16: * This constructor moves a handle from one object to another. Chris@16: * Chris@16: * @param other The other basic_handle object from which the move will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_handle(io_service&) constructor. Chris@16: */ Chris@16: basic_handle(basic_handle&& other) Chris@16: : basic_io_object( Chris@16: BOOST_ASIO_MOVE_CAST(basic_handle)(other)) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Move-assign a basic_handle from another. Chris@16: /** Chris@16: * This assignment operator moves a handle from one object to another. Chris@16: * Chris@16: * @param other The other basic_handle object from which the move will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_handle(io_service&) constructor. Chris@16: */ Chris@16: basic_handle& operator=(basic_handle&& other) Chris@16: { Chris@16: basic_io_object::operator=( Chris@16: BOOST_ASIO_MOVE_CAST(basic_handle)(other)); Chris@16: return *this; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Get a reference to the lowest layer. Chris@16: /** Chris@16: * This function returns a reference to the lowest layer in a stack of Chris@16: * layers. Since a basic_handle cannot contain any further layers, it simply Chris@16: * returns a reference to itself. Chris@16: * Chris@16: * @return A reference to the lowest layer in the stack of layers. Ownership Chris@16: * is not transferred to the caller. Chris@16: */ Chris@16: lowest_layer_type& lowest_layer() Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /// Get a const reference to the lowest layer. Chris@16: /** Chris@16: * This function returns a const reference to the lowest layer in a stack of Chris@16: * layers. Since a basic_handle cannot contain any further layers, it simply Chris@16: * returns a reference to itself. Chris@16: * Chris@16: * @return A const reference to the lowest layer in the stack of layers. Chris@16: * Ownership is not transferred to the caller. Chris@16: */ Chris@16: const lowest_layer_type& lowest_layer() const Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /// Assign an existing native handle to the handle. Chris@16: /* Chris@16: * This function opens the handle to hold an existing native handle. Chris@16: * Chris@16: * @param handle A native handle. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void assign(const native_handle_type& handle) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().assign(this->get_implementation(), handle, ec); Chris@16: boost::asio::detail::throw_error(ec, "assign"); Chris@16: } Chris@16: Chris@16: /// Assign an existing native handle to the handle. Chris@16: /* Chris@16: * This function opens the handle to hold an existing native handle. Chris@16: * Chris@16: * @param handle A native handle. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code assign(const native_handle_type& handle, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().assign(this->get_implementation(), handle, ec); Chris@16: } Chris@16: Chris@16: /// Determine whether the handle is open. Chris@16: bool is_open() const Chris@16: { Chris@16: return this->get_service().is_open(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Close the handle. Chris@16: /** Chris@16: * This function is used to close the handle. Any asynchronous read or write Chris@16: * operations will be cancelled immediately, and will complete with the Chris@16: * boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void close() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().close(this->get_implementation(), ec); Chris@16: boost::asio::detail::throw_error(ec, "close"); Chris@16: } Chris@16: Chris@16: /// Close the handle. Chris@16: /** Chris@16: * This function is used to close the handle. Any asynchronous read or write Chris@16: * operations will be cancelled immediately, and will complete with the Chris@16: * boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code close(boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().close(this->get_implementation(), ec); Chris@16: } Chris@16: Chris@16: /// (Deprecated: Use native_handle().) Get the native handle representation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * handle. This is intended to allow access to native handle functionality Chris@16: * that is not otherwise provided. Chris@16: */ Chris@16: native_type native() Chris@16: { Chris@16: return this->get_service().native_handle(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Get the native handle representation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * handle. This is intended to allow access to native handle functionality Chris@16: * that is not otherwise provided. Chris@16: */ Chris@16: native_handle_type native_handle() Chris@16: { Chris@16: return this->get_service().native_handle(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the handle. Chris@16: /** Chris@16: * This function causes all outstanding asynchronous read or write operations Chris@16: * to finish immediately, and the handlers for cancelled operations will be Chris@16: * passed the boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void cancel() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().cancel(this->get_implementation(), ec); Chris@16: boost::asio::detail::throw_error(ec, "cancel"); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the handle. Chris@16: /** Chris@16: * This function causes all outstanding asynchronous read or write operations Chris@16: * to finish immediately, and the handlers for cancelled operations will be Chris@16: * passed the boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code cancel(boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().cancel(this->get_implementation(), ec); Chris@16: } Chris@16: Chris@16: protected: Chris@16: /// Protected destructor to prevent deletion through this type. Chris@16: ~basic_handle() Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace windows Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) Chris@16: // || defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) Chris@16: // || defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) Chris@16: // || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #endif // BOOST_ASIO_WINDOWS_BASIC_HANDLE_HPP