Realistic 3D camera system
3D camera system components
win_iocp_serial_port_service.hpp
Go to the documentation of this file.
1 //
2 // detail/win_iocp_serial_port_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 //
11 
12 #ifndef ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP
13 #define ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP
14 
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 
19 #include "asio/detail/config.hpp"
20 
21 #if defined(ASIO_HAS_IOCP) && defined(ASIO_HAS_SERIAL_PORT)
22 
23 #include <string>
24 #include "asio/error.hpp"
25 #include "asio/io_service.hpp"
27 
29 
30 namespace asio {
31 namespace detail {
32 
33 // Extend win_iocp_handle_service to provide serial port support.
34 class win_iocp_serial_port_service
35 {
36 public:
37  // The native type of a serial port.
38  typedef win_iocp_handle_service::native_handle_type native_handle_type;
39 
40  // The implementation type of the serial port.
41  typedef win_iocp_handle_service::implementation_type implementation_type;
42 
43  // Constructor.
44  ASIO_DECL win_iocp_serial_port_service(
45  asio::io_service& io_service);
46 
47  // Destroy all user-defined handler objects owned by the service.
48  ASIO_DECL void shutdown_service();
49 
50  // Construct a new serial port implementation.
51  void construct(implementation_type& impl)
52  {
53  handle_service_.construct(impl);
54  }
55 
56  // Move-construct a new serial port implementation.
57  void move_construct(implementation_type& impl,
58  implementation_type& other_impl)
59  {
60  handle_service_.move_construct(impl, other_impl);
61  }
62 
63  // Move-assign from another serial port implementation.
64  void move_assign(implementation_type& impl,
65  win_iocp_serial_port_service& other_service,
66  implementation_type& other_impl)
67  {
68  handle_service_.move_assign(impl,
69  other_service.handle_service_, other_impl);
70  }
71 
72  // Destroy a serial port implementation.
73  void destroy(implementation_type& impl)
74  {
75  handle_service_.destroy(impl);
76  }
77 
78  // Open the serial port using the specified device name.
79  ASIO_DECL asio::error_code open(implementation_type& impl,
80  const std::string& device, asio::error_code& ec);
81 
82  // Assign a native handle to a serial port implementation.
83  asio::error_code assign(implementation_type& impl,
84  const native_handle_type& handle, asio::error_code& ec)
85  {
86  return handle_service_.assign(impl, handle, ec);
87  }
88 
89  // Determine whether the serial port is open.
90  bool is_open(const implementation_type& impl) const
91  {
92  return handle_service_.is_open(impl);
93  }
94 
95  // Destroy a serial port implementation.
96  asio::error_code close(implementation_type& impl,
97  asio::error_code& ec)
98  {
99  return handle_service_.close(impl, ec);
100  }
101 
102  // Get the native serial port representation.
103  native_handle_type native_handle(implementation_type& impl)
104  {
105  return handle_service_.native_handle(impl);
106  }
107 
108  // Cancel all operations associated with the handle.
109  asio::error_code cancel(implementation_type& impl,
110  asio::error_code& ec)
111  {
112  return handle_service_.cancel(impl, ec);
113  }
114 
115  // Set an option on the serial port.
116  template <typename SettableSerialPortOption>
117  asio::error_code set_option(implementation_type& impl,
118  const SettableSerialPortOption& option, asio::error_code& ec)
119  {
120  return do_set_option(impl,
121  &win_iocp_serial_port_service::store_option<SettableSerialPortOption>,
122  &option, ec);
123  }
124 
125  // Get an option from the serial port.
126  template <typename GettableSerialPortOption>
127  asio::error_code get_option(const implementation_type& impl,
128  GettableSerialPortOption& option, asio::error_code& ec) const
129  {
130  return do_get_option(impl,
131  &win_iocp_serial_port_service::load_option<GettableSerialPortOption>,
132  &option, ec);
133  }
134 
135  // Send a break sequence to the serial port.
136  asio::error_code send_break(implementation_type&,
137  asio::error_code& ec)
138  {
140  return ec;
141  }
142 
143  // Write the given data. Returns the number of bytes sent.
144  template <typename ConstBufferSequence>
145  size_t write_some(implementation_type& impl,
146  const ConstBufferSequence& buffers, asio::error_code& ec)
147  {
148  return handle_service_.write_some(impl, buffers, ec);
149  }
150 
151  // Start an asynchronous write. The data being written must be valid for the
152  // lifetime of the asynchronous operation.
153  template <typename ConstBufferSequence, typename Handler>
154  void async_write_some(implementation_type& impl,
155  const ConstBufferSequence& buffers, Handler& handler)
156  {
157  handle_service_.async_write_some(impl, buffers, handler);
158  }
159 
160  // Read some data. Returns the number of bytes received.
161  template <typename MutableBufferSequence>
162  size_t read_some(implementation_type& impl,
163  const MutableBufferSequence& buffers, asio::error_code& ec)
164  {
165  return handle_service_.read_some(impl, buffers, ec);
166  }
167 
168  // Start an asynchronous read. The buffer for the data being received must be
169  // valid for the lifetime of the asynchronous operation.
170  template <typename MutableBufferSequence, typename Handler>
171  void async_read_some(implementation_type& impl,
172  const MutableBufferSequence& buffers, Handler& handler)
173  {
174  handle_service_.async_read_some(impl, buffers, handler);
175  }
176 
177 private:
178  // Function pointer type for storing a serial port option.
179  typedef asio::error_code (*store_function_type)(
180  const void*, ::DCB&, asio::error_code&);
181 
182  // Helper function template to store a serial port option.
183  template <typename SettableSerialPortOption>
184  static asio::error_code store_option(const void* option,
185  ::DCB& storage, asio::error_code& ec)
186  {
187  return static_cast<const SettableSerialPortOption*>(option)->store(
188  storage, ec);
189  }
190 
191  // Helper function to set a serial port option.
192  ASIO_DECL asio::error_code do_set_option(
193  implementation_type& impl, store_function_type store,
194  const void* option, asio::error_code& ec);
195 
196  // Function pointer type for loading a serial port option.
197  typedef asio::error_code (*load_function_type)(
198  void*, const ::DCB&, asio::error_code&);
199 
200  // Helper function template to load a serial port option.
201  template <typename GettableSerialPortOption>
202  static asio::error_code load_option(void* option,
203  const ::DCB& storage, asio::error_code& ec)
204  {
205  return static_cast<GettableSerialPortOption*>(option)->load(storage, ec);
206  }
207 
208  // Helper function to get a serial port option.
209  ASIO_DECL asio::error_code do_get_option(
210  const implementation_type& impl, load_function_type load,
211  void* option, asio::error_code& ec) const;
212 
213  // The implementation used for initiating asynchronous operations.
214  win_iocp_handle_service handle_service_;
215 };
216 
217 } // namespace detail
218 } // namespace asio
219 
221 
222 #if defined(ASIO_HEADER_ONLY)
224 #endif // defined(ASIO_HEADER_ONLY)
225 
226 #endif // defined(ASIO_HAS_IOCP) && defined(ASIO_HAS_SERIAL_PORT)
227 
228 #endif // ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP
Provides core I/O functionality.
Definition: io_service.hpp:184
const MutableBufferSequence & buffers
Definition: read.hpp:521
Class to represent an error code value.
Definition: error_code.hpp:80
#define ASIO_DECL
Definition: config.hpp:43
ASIO_DECL int close(int d, state_type &state, asio::error_code &ec)
ASIO_DECL int open(const char *path, int flags, asio::error_code &ec)
Operation not supported.
Definition: error.hpp:166