mockup_serial_port_service.hpp
1 //
2 // mockup_serial_port_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #ifndef BOOST_ASIO_MOCKUP_SERIAL_PORT_SERVICE_HPP
10 #define BOOST_ASIO_MOCKUP_SERIAL_PORT_SERVICE_HPP
11 
12 #include <boost/asio/detail/config.hpp>
13 
14 #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
15  || defined(GENERATING_DOCUMENTATION)
16 
17 #include <cstddef>
18 #include <string>
19 #include <boost/asio/async_result.hpp>
20 #include <pre/boost/asio/detail/mockup_serial_port_service.hpp>
21 #include <boost/asio/error.hpp>
22 #include <boost/asio/io_service.hpp>
23 #include <boost/asio/serial_port_base.hpp>
24 
25 namespace boost {
26 namespace asio {
27 
71  : public boost::asio::detail::service_base<mockup_serial_port_service>
72 {
73 public:
74 
75 private:
76  typedef detail::mockup_serial_port_service service_impl_type;
77 
78 public:
80  typedef service_impl_type::implementation_type implementation_type;
81 
83  typedef service_impl_type::native_handle_type native_type;
84 
86  typedef service_impl_type::native_handle_type native_handle_type;
87 
89  explicit mockup_serial_port_service(boost::asio::io_service& io_service)
90  : boost::asio::detail::service_base<mockup_serial_port_service>(io_service),
91  service_impl_(io_service)
92  {
93  }
94 
96  void construct(implementation_type& impl)
97  {
98  service_impl_.construct(impl);
99  }
100 
101 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
102  void move_construct(implementation_type& impl,
104  implementation_type& other_impl)
105  {
106  service_impl_.move_construct(impl, other_impl);
107  }
108 
109 // /// Move-assign from another serial port implementation.
110 // void move_assign(implementation_type& impl,
111 // mockup_serial_port_service& other_service,
112 // implementation_type& other_impl)
113 // {
114 // service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
115 // }
116 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
117 
119  void destroy(implementation_type& impl)
120  {
121  service_impl_.destroy(impl);
122  }
123 
125  boost::system::error_code open(implementation_type& impl,
126  const std::string& device, boost::system::error_code& ec)
127  {
128  return service_impl_.open(impl, device, ec);
129  }
130 
132  boost::system::error_code assign(implementation_type& impl,
133  const native_handle_type& handle, boost::system::error_code& ec)
134  {
135  return service_impl_.assign(impl, handle, ec);
136  }
137 
139  bool is_open(const implementation_type& impl) const
140  {
141  return service_impl_.is_open(impl);
142  }
143 
145  boost::system::error_code close(implementation_type& impl,
146  boost::system::error_code& ec)
147  {
148  return service_impl_.close(impl, ec);
149  }
150 
152  native_type native(implementation_type& impl)
153  {
154  return service_impl_.native_handle(impl);
155  }
156 
158  native_handle_type native_handle(implementation_type& impl)
159  {
160  return service_impl_.native_handle(impl);
161  }
162 
164  boost::system::error_code cancel(implementation_type& impl,
165  boost::system::error_code& ec)
166  {
167  return service_impl_.cancel(impl, ec);
168  }
169 
171  template <typename SettableSerialPortOption>
172  boost::system::error_code set_option(implementation_type& impl,
173  const SettableSerialPortOption& option, boost::system::error_code& ec)
174  {
175  return service_impl_.set_option(impl, option, ec);
176  }
177 
179  template <typename GettableSerialPortOption>
180  boost::system::error_code get_option(const implementation_type& impl,
181  GettableSerialPortOption& option, boost::system::error_code& ec) const
182  {
183  return service_impl_.get_option(impl, option, ec);
184  }
185 
187  boost::system::error_code send_break(implementation_type& impl,
188  boost::system::error_code& ec)
189  {
190  return service_impl_.send_break(impl, ec);
191  }
192 
194  template <typename ConstBufferSequence>
195  std::size_t write_some(implementation_type& impl,
196  const ConstBufferSequence& buffers, boost::system::error_code& ec)
197  {
198  return service_impl_.write_some(impl, buffers, ec);
199  }
200 
202  template <typename ConstBufferSequence, typename WriteHandler>
203  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
204  void (boost::system::error_code, std::size_t))
205  async_write_some(implementation_type& impl,
206  const ConstBufferSequence& buffers,
207  BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
208  {
209  detail::async_result_init<
210  WriteHandler, void (boost::system::error_code, std::size_t)> init(
211  BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
212 
213  service_impl_.async_write_some(impl, buffers, init.handler);
214 
215  return init.result.get();
216  }
217 
219  template <typename MutableBufferSequence>
220  std::size_t read_some(implementation_type& impl,
221  const MutableBufferSequence& buffers, boost::system::error_code& ec)
222  {
223  return service_impl_.read_some(impl, buffers, ec);
224  }
225 
227  template <typename MutableBufferSequence, typename ReadHandler>
228  BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
229  void (boost::system::error_code, std::size_t))
230  async_read_some(implementation_type& impl,
231  const MutableBufferSequence& buffers,
232  BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
233  {
234  detail::async_result_init<
235  ReadHandler, void (boost::system::error_code, std::size_t)> init(
236  BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
237 
238  service_impl_.async_read_some(impl, buffers, init.handler);
239 
240  return init.result.get();
241  }
242 
243 private:
244  // Destroy all user-defined handler objects owned by the service.
245  void shutdown_service()
246  {
247  service_impl_.shutdown_service();
248  }
249 
250  // The platform-specific implementation.
251  service_impl_type service_impl_;
252 };
253 
254 } // namespace asio
255 } // namespace boost
256 
257 //#include <boost/asio/detail/pop_options.hpp>
258 
259 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
260  // || defined(GENERATING_DOCUMENTATION)
261 
262 #endif // BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
boost::system::error_code open(implementation_type &impl, const std::string &device, boost::system::error_code &ec)
Open a serial port.
Definition: mockup_serial_port_service.hpp:125
native_type native(implementation_type &impl)
(Deprecated: Use native_handle().) Get the native handle implementation.
Definition: mockup_serial_port_service.hpp:152
native_handle_type native_handle(implementation_type &impl)
Get the native handle implementation.
Definition: mockup_serial_port_service.hpp:158
bool is_open(const implementation_type &impl) const
Determine whether the handle is open.
Definition: mockup_serial_port_service.hpp:139
mockup_serial_port_service : A virtual serial port allowing to write cross platform unit tests of ser...
Definition: mockup_serial_port_service.hpp:70
service_impl_type::native_handle_type native_type
(Deprecated: Use native_handle_type.) The native handle type.
Definition: mockup_serial_port_service.hpp:83
void move_construct(implementation_type &impl, implementation_type &other_impl)
Move-construct a new serial port implementation.
Definition: mockup_serial_port_service.hpp:103
mockup_serial_port_service(boost::asio::io_service &io_service)
Construct a new serial port service for the specified io_service.
Definition: mockup_serial_port_service.hpp:89
service_impl_type::implementation_type implementation_type
The type of a serial port implementation.
Definition: mockup_serial_port_service.hpp:80
void construct(implementation_type &impl)
Construct a new serial port implementation.
Definition: mockup_serial_port_service.hpp:96
boost::system::error_code cancel(implementation_type &impl, boost::system::error_code &ec)
Cancel all asynchronous operations associated with the handle.
Definition: mockup_serial_port_service.hpp:164
std::size_t read_some(implementation_type &impl, const MutableBufferSequence &buffers, boost::system::error_code &ec)
Read some data from the stream.
Definition: mockup_serial_port_service.hpp:220
service_impl_type::native_handle_type native_handle_type
The native handle type.
Definition: mockup_serial_port_service.hpp:86
boost::system::error_code get_option(const implementation_type &impl, GettableSerialPortOption &option, boost::system::error_code &ec) const
Get a serial port option.
Definition: mockup_serial_port_service.hpp:180
boost::system::error_code set_option(implementation_type &impl, const SettableSerialPortOption &option, boost::system::error_code &ec)
Set a serial port option.
Definition: mockup_serial_port_service.hpp:172
BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void(boost::system::error_code, std::size_t)) async_write_some(implementation_type &impl
Start an asynchronous write.
boost::system::error_code assign(implementation_type &impl, const native_handle_type &handle, boost::system::error_code &ec)
Assign an existing native handle to a serial port.
Definition: mockup_serial_port_service.hpp:132
boost::system::error_code send_break(implementation_type &impl, boost::system::error_code &ec)
Send a break sequence to the serial port.
Definition: mockup_serial_port_service.hpp:187
boost::system::error_code close(implementation_type &impl, boost::system::error_code &ec)
Close a serial port implementation.
Definition: mockup_serial_port_service.hpp:145
std::size_t write_some(implementation_type &impl, const ConstBufferSequence &buffers, boost::system::error_code &ec)
Write the given data to the stream.
Definition: mockup_serial_port_service.hpp:195
void destroy(implementation_type &impl)
Destroy a serial port implementation.
Definition: mockup_serial_port_service.hpp:119