Realistic 3D camera system
3D camera system components
basic_serial_port.hpp
Go to the documentation of this file.
1 //
2 // basic_serial_port.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_BASIC_SERIAL_PORT_HPP
13 #define ASIO_BASIC_SERIAL_PORT_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_SERIAL_PORT) \
22  || defined(GENERATING_DOCUMENTATION)
23 
24 #include <string>
25 #include "asio/basic_io_object.hpp"
28 #include "asio/error.hpp"
31 
33 
34 namespace asio {
35 
37 
45 template <typename SerialPortService = serial_port_service>
46 class basic_serial_port
47  : public basic_io_object<SerialPortService>,
48  public serial_port_base
49 {
50 public:
53  typedef typename SerialPortService::native_handle_type native_type;
54 
56  typedef typename SerialPortService::native_handle_type native_handle_type;
57 
59  typedef basic_serial_port<SerialPortService> lowest_layer_type;
60 
62 
68  explicit basic_serial_port(asio::io_service& io_service)
69  : basic_io_object<SerialPortService>(io_service)
70  {
71  }
72 
74 
84  explicit basic_serial_port(asio::io_service& io_service,
85  const char* device)
86  : basic_io_object<SerialPortService>(io_service)
87  {
89  this->get_service().open(this->get_implementation(), device, ec);
90  asio::detail::throw_error(ec, "open");
91  }
92 
94 
104  explicit basic_serial_port(asio::io_service& io_service,
105  const std::string& device)
106  : basic_io_object<SerialPortService>(io_service)
107  {
108  asio::error_code ec;
109  this->get_service().open(this->get_implementation(), device, ec);
110  asio::detail::throw_error(ec, "open");
111  }
112 
114 
125  basic_serial_port(asio::io_service& io_service,
126  const native_handle_type& native_serial_port)
127  : basic_io_object<SerialPortService>(io_service)
128  {
129  asio::error_code ec;
130  this->get_service().assign(this->get_implementation(),
131  native_serial_port, ec);
132  asio::detail::throw_error(ec, "assign");
133  }
134 
135 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
136 
146  basic_serial_port(basic_serial_port&& other)
147  : basic_io_object<SerialPortService>(
148  ASIO_MOVE_CAST(basic_serial_port)(other))
149  {
150  }
151 
153 
162  basic_serial_port& operator=(basic_serial_port&& other)
163  {
164  basic_io_object<SerialPortService>::operator=(
165  ASIO_MOVE_CAST(basic_serial_port)(other));
166  return *this;
167  }
168 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
169 
171 
179  lowest_layer_type& lowest_layer()
180  {
181  return *this;
182  }
183 
185 
193  const lowest_layer_type& lowest_layer() const
194  {
195  return *this;
196  }
197 
199 
206  void open(const std::string& device)
207  {
208  asio::error_code ec;
209  this->get_service().open(this->get_implementation(), device, ec);
210  asio::detail::throw_error(ec, "open");
211  }
212 
214 
222  asio::error_code open(const std::string& device,
223  asio::error_code& ec)
224  {
225  return this->get_service().open(this->get_implementation(), device, ec);
226  }
227 
229  /*
230  * This function opens the serial port to hold an existing native serial port.
231  *
232  * @param native_serial_port A native serial port.
233  *
234  * @throws asio::system_error Thrown on failure.
235  */
236  void assign(const native_handle_type& native_serial_port)
237  {
238  asio::error_code ec;
239  this->get_service().assign(this->get_implementation(),
240  native_serial_port, ec);
241  asio::detail::throw_error(ec, "assign");
242  }
243 
245  /*
246  * This function opens the serial port to hold an existing native serial port.
247  *
248  * @param native_serial_port A native serial port.
249  *
250  * @param ec Set to indicate what error occurred, if any.
251  */
252  asio::error_code assign(const native_handle_type& native_serial_port,
253  asio::error_code& ec)
254  {
255  return this->get_service().assign(this->get_implementation(),
256  native_serial_port, ec);
257  }
258 
260  bool is_open() const
261  {
262  return this->get_service().is_open(this->get_implementation());
263  }
264 
266 
273  void close()
274  {
275  asio::error_code ec;
276  this->get_service().close(this->get_implementation(), ec);
277  asio::detail::throw_error(ec, "close");
278  }
279 
281 
289  {
290  return this->get_service().close(this->get_implementation(), ec);
291  }
292 
295 
300  native_type native()
301  {
302  return this->get_service().native_handle(this->get_implementation());
303  }
304 
306 
311  native_handle_type native_handle()
312  {
313  return this->get_service().native_handle(this->get_implementation());
314  }
315 
317 
324  void cancel()
325  {
326  asio::error_code ec;
327  this->get_service().cancel(this->get_implementation(), ec);
328  asio::detail::throw_error(ec, "cancel");
329  }
330 
332 
340  {
341  return this->get_service().cancel(this->get_implementation(), ec);
342  }
343 
345 
351  void send_break()
352  {
353  asio::error_code ec;
354  this->get_service().send_break(this->get_implementation(), ec);
355  asio::detail::throw_error(ec, "send_break");
356  }
357 
359 
365  asio::error_code send_break(asio::error_code& ec)
366  {
367  return this->get_service().send_break(this->get_implementation(), ec);
368  }
369 
371 
385  template <typename SettableSerialPortOption>
386  void set_option(const SettableSerialPortOption& option)
387  {
388  asio::error_code ec;
389  this->get_service().set_option(this->get_implementation(), option, ec);
390  asio::detail::throw_error(ec, "set_option");
391  }
392 
394 
408  template <typename SettableSerialPortOption>
409  asio::error_code set_option(const SettableSerialPortOption& option,
410  asio::error_code& ec)
411  {
412  return this->get_service().set_option(
413  this->get_implementation(), option, ec);
414  }
415 
417 
432  template <typename GettableSerialPortOption>
433  void get_option(GettableSerialPortOption& option)
434  {
435  asio::error_code ec;
436  this->get_service().get_option(this->get_implementation(), option, ec);
437  asio::detail::throw_error(ec, "get_option");
438  }
439 
441 
456  template <typename GettableSerialPortOption>
457  asio::error_code get_option(GettableSerialPortOption& option,
458  asio::error_code& ec)
459  {
460  return this->get_service().get_option(
461  this->get_implementation(), option, ec);
462  }
463 
465 
491  template <typename ConstBufferSequence>
492  std::size_t write_some(const ConstBufferSequence& buffers)
493  {
494  asio::error_code ec;
495  std::size_t s = this->get_service().write_some(
496  this->get_implementation(), buffers, ec);
497  asio::detail::throw_error(ec, "write_some");
498  return s;
499  }
500 
502 
517  template <typename ConstBufferSequence>
518  std::size_t write_some(const ConstBufferSequence& buffers,
519  asio::error_code& ec)
520  {
521  return this->get_service().write_some(
522  this->get_implementation(), buffers, ec);
523  }
524 
526 
560  template <typename ConstBufferSequence, typename WriteHandler>
561  ASIO_INITFN_RESULT_TYPE(WriteHandler,
562  void (asio::error_code, std::size_t))
563  async_write_some(const ConstBufferSequence& buffers,
564  ASIO_MOVE_ARG(WriteHandler) handler)
565  {
566  // If you get an error on the following line it means that your handler does
567  // not meet the documented type requirements for a WriteHandler.
568  ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
569 
570  return this->get_service().async_write_some(this->get_implementation(),
571  buffers, ASIO_MOVE_CAST(WriteHandler)(handler));
572  }
573 
575 
602  template <typename MutableBufferSequence>
603  std::size_t read_some(const MutableBufferSequence& buffers)
604  {
605  asio::error_code ec;
606  std::size_t s = this->get_service().read_some(
607  this->get_implementation(), buffers, ec);
608  asio::detail::throw_error(ec, "read_some");
609  return s;
610  }
611 
613 
629  template <typename MutableBufferSequence>
630  std::size_t read_some(const MutableBufferSequence& buffers,
631  asio::error_code& ec)
632  {
633  return this->get_service().read_some(
634  this->get_implementation(), buffers, ec);
635  }
636 
638 
673  template <typename MutableBufferSequence, typename ReadHandler>
674  ASIO_INITFN_RESULT_TYPE(ReadHandler,
675  void (asio::error_code, std::size_t))
676  async_read_some(const MutableBufferSequence& buffers,
677  ASIO_MOVE_ARG(ReadHandler) handler)
678  {
679  // If you get an error on the following line it means that your handler does
680  // not meet the documented type requirements for a ReadHandler.
681  ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
682 
683  return this->get_service().async_read_some(this->get_implementation(),
684  buffers, ASIO_MOVE_CAST(ReadHandler)(handler));
685  }
686 };
687 
688 } // namespace asio
689 
690 #include "asio/detail/pop_options.hpp"
691 
692 #endif // defined(ASIO_HAS_SERIAL_PORT)
693  // || defined(GENERATING_DOCUMENTATION)
694 
695 #endif // ASIO_BASIC_SERIAL_PORT_HPP
void throw_error(const asio::error_code &err)
Definition: throw_error.hpp:31
Provides core I/O functionality.
Definition: io_service.hpp:184
SocketService & s
Definition: connect.hpp:521
STL namespace.
ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler, void(asio::error_code, Iterator)) async_connect(basic_socket< Protocol
const MutableBufferSequence & buffers
Definition: read.hpp:521
#define ASIO_READ_HANDLER_CHECK(handler_type, handler)
asio::basic_streambuf< Allocator > CompletionCondition ASIO_MOVE_ARG(ReadHandler) handler)
Definition: read.hpp:704
Class to represent an error code value.
Definition: error_code.hpp:80
ASIO_DECL int close(int d, state_type &state, asio::error_code &ec)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
#define ASIO_WRITE_HANDLER_CHECK(handler_type, handler)
ASIO_DECL int open(const char *path, int flags, asio::error_code &ec)