Chris@16: // Chris@16: // serial_port_service.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_SERIAL_PORT_SERVICE_HPP Chris@16: #define BOOST_ASIO_SERIAL_PORT_SERVICE_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_SERIAL_PORT) \ Chris@16: || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include 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: Chris@16: /// Default service implementation for a serial port. Chris@16: class serial_port_service Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: : public boost::asio::io_service::service Chris@16: #else Chris@16: : public boost::asio::detail::service_base Chris@16: #endif Chris@16: { Chris@16: public: Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: /// The unique service identifier. Chris@16: static boost::asio::io_service::id id; Chris@16: #endif Chris@16: Chris@16: private: Chris@16: // The type of the platform-specific implementation. Chris@16: #if defined(BOOST_ASIO_HAS_IOCP) Chris@16: typedef detail::win_iocp_serial_port_service service_impl_type; Chris@16: #else Chris@16: typedef detail::reactive_serial_port_service service_impl_type; Chris@16: #endif Chris@16: Chris@16: public: Chris@16: /// The type of a serial port implementation. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined implementation_type; Chris@16: #else Chris@16: typedef service_impl_type::implementation_type implementation_type; Chris@16: #endif Chris@16: Chris@16: /// (Deprecated: Use native_handle_type.) The native handle type. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined native_type; Chris@16: #else Chris@16: typedef service_impl_type::native_handle_type native_type; Chris@16: #endif Chris@16: Chris@16: /// The native handle type. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined native_handle_type; Chris@16: #else Chris@16: typedef service_impl_type::native_handle_type native_handle_type; Chris@16: #endif Chris@16: Chris@16: /// Construct a new serial port service for the specified io_service. Chris@16: explicit serial_port_service(boost::asio::io_service& io_service) Chris@16: : boost::asio::detail::service_base(io_service), Chris@16: service_impl_(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a new serial port implementation. Chris@16: void construct(implementation_type& impl) Chris@16: { Chris@16: service_impl_.construct(impl); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a new serial port implementation. Chris@16: void move_construct(implementation_type& impl, Chris@16: implementation_type& other_impl) Chris@16: { Chris@16: service_impl_.move_construct(impl, other_impl); Chris@16: } Chris@16: Chris@16: /// Move-assign from another serial port implementation. Chris@16: void move_assign(implementation_type& impl, Chris@16: serial_port_service& other_service, Chris@16: implementation_type& other_impl) Chris@16: { Chris@16: service_impl_.move_assign(impl, other_service.service_impl_, other_impl); Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Destroy a serial port implementation. Chris@16: void destroy(implementation_type& impl) Chris@16: { Chris@16: service_impl_.destroy(impl); Chris@16: } Chris@16: Chris@16: /// Open a serial port. Chris@16: boost::system::error_code open(implementation_type& impl, Chris@16: const std::string& device, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.open(impl, device, ec); Chris@16: } Chris@16: Chris@16: /// Assign an existing native handle to a serial port. Chris@16: boost::system::error_code assign(implementation_type& impl, Chris@16: const native_handle_type& handle, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.assign(impl, handle, ec); Chris@16: } Chris@16: Chris@16: /// Determine whether the handle is open. Chris@16: bool is_open(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.is_open(impl); Chris@16: } Chris@16: Chris@16: /// Close a serial port implementation. Chris@16: boost::system::error_code close(implementation_type& impl, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.close(impl, ec); Chris@16: } Chris@16: Chris@16: /// (Deprecated: Use native_handle().) Get the native handle implementation. Chris@16: native_type native(implementation_type& impl) Chris@16: { Chris@16: return service_impl_.native_handle(impl); Chris@16: } Chris@16: Chris@16: /// Get the native handle implementation. Chris@16: native_handle_type native_handle(implementation_type& impl) Chris@16: { Chris@16: return service_impl_.native_handle(impl); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the handle. Chris@16: boost::system::error_code cancel(implementation_type& impl, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.cancel(impl, ec); Chris@16: } Chris@16: Chris@16: /// Set a serial port option. Chris@16: template Chris@16: boost::system::error_code set_option(implementation_type& impl, Chris@16: const SettableSerialPortOption& option, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.set_option(impl, option, ec); Chris@16: } Chris@16: Chris@16: /// Get a serial port option. Chris@16: template Chris@16: boost::system::error_code get_option(const implementation_type& impl, Chris@16: GettableSerialPortOption& option, boost::system::error_code& ec) const Chris@16: { Chris@16: return service_impl_.get_option(impl, option, ec); Chris@16: } Chris@16: Chris@16: /// Send a break sequence to the serial port. Chris@16: boost::system::error_code send_break(implementation_type& impl, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.send_break(impl, ec); Chris@16: } Chris@16: Chris@16: /// Write the given data to the stream. Chris@16: template Chris@16: std::size_t write_some(implementation_type& impl, Chris@16: const ConstBufferSequence& buffers, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.write_some(impl, buffers, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous write. Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_write_some(implementation_type& impl, Chris@16: const ConstBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: detail::async_result_init< Chris@16: WriteHandler, void (boost::system::error_code, std::size_t)> init( Chris@16: BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: Chris@16: service_impl_.async_write_some(impl, buffers, init.handler); Chris@16: Chris@16: return init.result.get(); Chris@16: } Chris@16: Chris@16: /// Read some data from the stream. Chris@16: template Chris@16: std::size_t read_some(implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.read_some(impl, buffers, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous read. Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_read_some(implementation_type& impl, Chris@16: const MutableBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler) Chris@16: { Chris@16: detail::async_result_init< Chris@16: ReadHandler, void (boost::system::error_code, std::size_t)> init( Chris@16: BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: Chris@16: service_impl_.async_read_some(impl, buffers, init.handler); Chris@16: Chris@16: return init.result.get(); Chris@16: } Chris@16: Chris@16: private: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: void shutdown_service() Chris@16: { Chris@16: service_impl_.shutdown_service(); Chris@16: } Chris@16: Chris@16: // The platform-specific implementation. Chris@16: service_impl_type service_impl_; Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT) Chris@16: // || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #endif // BOOST_ASIO_SERIAL_PORT_SERVICE_HPP