serial_port_base.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // serial_port_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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. #ifndef ASIO_SERIAL_PORT_BASE_HPP
  12. #define ASIO_SERIAL_PORT_BASE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_HAS_SERIAL_PORT) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  20. # include <termios.h>
  21. #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  22. #include <boost/detail/workaround.hpp>
  23. #include "asio/detail/socket_types.hpp"
  24. #include "asio/error_code.hpp"
  25. #if defined(GENERATING_DOCUMENTATION)
  26. # define ASIO_OPTION_STORAGE implementation_defined
  27. #elif defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  28. # define ASIO_OPTION_STORAGE DCB
  29. #else
  30. # define ASIO_OPTION_STORAGE termios
  31. #endif
  32. #include "asio/detail/push_options.hpp"
  33. namespace asio {
  34. /// The serial_port_base class is used as a base for the basic_serial_port class
  35. /// template so that we have a common place to define the serial port options.
  36. class serial_port_base
  37. {
  38. public:
  39. /// Serial port option to permit changing the baud rate.
  40. /**
  41. * Implements changing the baud rate for a given serial port.
  42. */
  43. class baud_rate
  44. {
  45. public:
  46. explicit baud_rate(unsigned int rate = 0);
  47. unsigned int value() const;
  48. ASIO_DECL asio::error_code store(
  49. ASIO_OPTION_STORAGE& storage,
  50. asio::error_code& ec) const;
  51. ASIO_DECL asio::error_code load(
  52. const ASIO_OPTION_STORAGE& storage,
  53. asio::error_code& ec);
  54. private:
  55. unsigned int value_;
  56. };
  57. /// Serial port option to permit changing the flow control.
  58. /**
  59. * Implements changing the flow control for a given serial port.
  60. */
  61. class flow_control
  62. {
  63. public:
  64. enum type { none, software, hardware };
  65. ASIO_DECL explicit flow_control(type t = none);
  66. type value() const;
  67. ASIO_DECL asio::error_code store(
  68. ASIO_OPTION_STORAGE& storage,
  69. asio::error_code& ec) const;
  70. ASIO_DECL asio::error_code load(
  71. const ASIO_OPTION_STORAGE& storage,
  72. asio::error_code& ec);
  73. private:
  74. type value_;
  75. };
  76. /// Serial port option to permit changing the parity.
  77. /**
  78. * Implements changing the parity for a given serial port.
  79. */
  80. class parity
  81. {
  82. public:
  83. enum type { none, odd, even };
  84. ASIO_DECL explicit parity(type t = none);
  85. type value() const;
  86. ASIO_DECL asio::error_code store(
  87. ASIO_OPTION_STORAGE& storage,
  88. asio::error_code& ec) const;
  89. ASIO_DECL asio::error_code load(
  90. const ASIO_OPTION_STORAGE& storage,
  91. asio::error_code& ec);
  92. private:
  93. type value_;
  94. };
  95. /// Serial port option to permit changing the number of stop bits.
  96. /**
  97. * Implements changing the number of stop bits for a given serial port.
  98. */
  99. class stop_bits
  100. {
  101. public:
  102. enum type { one, onepointfive, two };
  103. ASIO_DECL explicit stop_bits(type t = one);
  104. type value() const;
  105. ASIO_DECL asio::error_code store(
  106. ASIO_OPTION_STORAGE& storage,
  107. asio::error_code& ec) const;
  108. ASIO_DECL asio::error_code load(
  109. const ASIO_OPTION_STORAGE& storage,
  110. asio::error_code& ec);
  111. private:
  112. type value_;
  113. };
  114. /// Serial port option to permit changing the character size.
  115. /**
  116. * Implements changing the character size for a given serial port.
  117. */
  118. class character_size
  119. {
  120. public:
  121. ASIO_DECL explicit character_size(unsigned int t = 8);
  122. unsigned int value() const;
  123. ASIO_DECL asio::error_code store(
  124. ASIO_OPTION_STORAGE& storage,
  125. asio::error_code& ec) const;
  126. ASIO_DECL asio::error_code load(
  127. const ASIO_OPTION_STORAGE& storage,
  128. asio::error_code& ec);
  129. private:
  130. unsigned int value_;
  131. };
  132. protected:
  133. /// Protected destructor to prevent deletion through this type.
  134. ~serial_port_base()
  135. {
  136. }
  137. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  138. private:
  139. // Workaround to enable the empty base optimisation with Borland C++.
  140. char dummy_;
  141. #endif
  142. };
  143. } // namespace asio
  144. #include "asio/detail/pop_options.hpp"
  145. #undef ASIO_OPTION_STORAGE
  146. #include "asio/impl/serial_port_base.hpp"
  147. #if defined(ASIO_HEADER_ONLY)
  148. # include "asio/impl/serial_port_base.ipp"
  149. #endif // defined(ASIO_HEADER_ONLY)
  150. #endif // defined(ASIO_HAS_SERIAL_PORT)
  151. // || defined(GENERATING_DOCUMENTATION)
  152. #endif // ASIO_SERIAL_PORT_BASE_HPP