Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
6.7KB

  1. //
  2. // serial_port_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_SERIAL_PORT_SERVICE_HPP
  11. #define ASIO_SERIAL_PORT_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_SERIAL_PORT) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <cstddef>
  19. #include <string>
  20. #include "asio/async_result.hpp"
  21. #include "asio/detail/reactive_serial_port_service.hpp"
  22. #include "asio/detail/win_iocp_serial_port_service.hpp"
  23. #include "asio/error.hpp"
  24. #include "asio/io_context.hpp"
  25. #include "asio/serial_port_base.hpp"
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. /// Default service implementation for a serial port.
  29. class serial_port_service
  30. #if defined(GENERATING_DOCUMENTATION)
  31. : public asio::io_context::service
  32. #else
  33. : public asio::detail::service_base<serial_port_service>
  34. #endif
  35. {
  36. public:
  37. #if defined(GENERATING_DOCUMENTATION)
  38. /// The unique service identifier.
  39. static asio::io_context::id id;
  40. #endif
  41. private:
  42. // The type of the platform-specific implementation.
  43. #if defined(ASIO_HAS_IOCP)
  44. typedef detail::win_iocp_serial_port_service service_impl_type;
  45. #else
  46. typedef detail::reactive_serial_port_service service_impl_type;
  47. #endif
  48. public:
  49. /// The type of a serial port implementation.
  50. #if defined(GENERATING_DOCUMENTATION)
  51. typedef implementation_defined implementation_type;
  52. #else
  53. typedef service_impl_type::implementation_type implementation_type;
  54. #endif
  55. /// The native handle type.
  56. #if defined(GENERATING_DOCUMENTATION)
  57. typedef implementation_defined native_handle_type;
  58. #else
  59. typedef service_impl_type::native_handle_type native_handle_type;
  60. #endif
  61. /// Construct a new serial port service for the specified io_context.
  62. explicit serial_port_service(asio::io_context& io_context)
  63. : asio::detail::service_base<serial_port_service>(io_context),
  64. service_impl_(io_context)
  65. {
  66. }
  67. /// Construct a new serial port implementation.
  68. void construct(implementation_type& impl)
  69. {
  70. service_impl_.construct(impl);
  71. }
  72. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  73. /// Move-construct a new serial port implementation.
  74. void move_construct(implementation_type& impl,
  75. implementation_type& other_impl)
  76. {
  77. service_impl_.move_construct(impl, other_impl);
  78. }
  79. /// Move-assign from another serial port implementation.
  80. void move_assign(implementation_type& impl,
  81. serial_port_service& other_service,
  82. implementation_type& other_impl)
  83. {
  84. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  85. }
  86. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  87. /// Destroy a serial port implementation.
  88. void destroy(implementation_type& impl)
  89. {
  90. service_impl_.destroy(impl);
  91. }
  92. /// Open a serial port.
  93. asio::error_code open(implementation_type& impl,
  94. const std::string& device, asio::error_code& ec)
  95. {
  96. return service_impl_.open(impl, device, ec);
  97. }
  98. /// Assign an existing native handle to a serial port.
  99. asio::error_code assign(implementation_type& impl,
  100. const native_handle_type& handle, asio::error_code& ec)
  101. {
  102. return service_impl_.assign(impl, handle, ec);
  103. }
  104. /// Determine whether the handle is open.
  105. bool is_open(const implementation_type& impl) const
  106. {
  107. return service_impl_.is_open(impl);
  108. }
  109. /// Close a serial port implementation.
  110. asio::error_code close(implementation_type& impl,
  111. asio::error_code& ec)
  112. {
  113. return service_impl_.close(impl, ec);
  114. }
  115. /// Get the native handle implementation.
  116. native_handle_type native_handle(implementation_type& impl)
  117. {
  118. return service_impl_.native_handle(impl);
  119. }
  120. /// Cancel all asynchronous operations associated with the handle.
  121. asio::error_code cancel(implementation_type& impl,
  122. asio::error_code& ec)
  123. {
  124. return service_impl_.cancel(impl, ec);
  125. }
  126. /// Set a serial port option.
  127. template <typename SettableSerialPortOption>
  128. asio::error_code set_option(implementation_type& impl,
  129. const SettableSerialPortOption& option, asio::error_code& ec)
  130. {
  131. return service_impl_.set_option(impl, option, ec);
  132. }
  133. /// Get a serial port option.
  134. template <typename GettableSerialPortOption>
  135. asio::error_code get_option(const implementation_type& impl,
  136. GettableSerialPortOption& option, asio::error_code& ec) const
  137. {
  138. return service_impl_.get_option(impl, option, ec);
  139. }
  140. /// Send a break sequence to the serial port.
  141. asio::error_code send_break(implementation_type& impl,
  142. asio::error_code& ec)
  143. {
  144. return service_impl_.send_break(impl, ec);
  145. }
  146. /// Write the given data to the stream.
  147. template <typename ConstBufferSequence>
  148. std::size_t write_some(implementation_type& impl,
  149. const ConstBufferSequence& buffers, asio::error_code& ec)
  150. {
  151. return service_impl_.write_some(impl, buffers, ec);
  152. }
  153. /// Start an asynchronous write.
  154. template <typename ConstBufferSequence, typename WriteHandler>
  155. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  156. void (asio::error_code, std::size_t))
  157. async_write_some(implementation_type& impl,
  158. const ConstBufferSequence& buffers,
  159. ASIO_MOVE_ARG(WriteHandler) handler)
  160. {
  161. async_completion<WriteHandler,
  162. void (asio::error_code, std::size_t)> init(handler);
  163. service_impl_.async_write_some(impl, buffers, init.handler);
  164. return init.result.get();
  165. }
  166. /// Read some data from the stream.
  167. template <typename MutableBufferSequence>
  168. std::size_t read_some(implementation_type& impl,
  169. const MutableBufferSequence& buffers, asio::error_code& ec)
  170. {
  171. return service_impl_.read_some(impl, buffers, ec);
  172. }
  173. /// Start an asynchronous read.
  174. template <typename MutableBufferSequence, typename ReadHandler>
  175. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  176. void (asio::error_code, std::size_t))
  177. async_read_some(implementation_type& impl,
  178. const MutableBufferSequence& buffers,
  179. ASIO_MOVE_ARG(ReadHandler) handler)
  180. {
  181. async_completion<ReadHandler,
  182. void (asio::error_code, std::size_t)> init(handler);
  183. service_impl_.async_read_some(impl, buffers, init.handler);
  184. return init.result.get();
  185. }
  186. private:
  187. // Destroy all user-defined handler objects owned by the service.
  188. void shutdown()
  189. {
  190. service_impl_.shutdown();
  191. }
  192. // The platform-specific implementation.
  193. service_impl_type service_impl_;
  194. };
  195. } // namespace asio
  196. #include "asio/detail/pop_options.hpp"
  197. #endif // defined(ASIO_HAS_SERIAL_PORT)
  198. // || defined(GENERATING_DOCUMENTATION)
  199. #endif // ASIO_SERIAL_PORT_SERVICE_HPP