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.

275 lines
7.0KB

  1. //
  2. // basic_io_object.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_BASIC_IO_OBJECT_HPP
  11. #define ASIO_BASIC_IO_OBJECT_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. #include "asio/io_context.hpp"
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. #if defined(ASIO_HAS_MOVE)
  20. namespace detail
  21. {
  22. // Type trait used to determine whether a service supports move.
  23. template <typename IoObjectService>
  24. class service_has_move
  25. {
  26. private:
  27. typedef IoObjectService service_type;
  28. typedef typename service_type::implementation_type implementation_type;
  29. template <typename T, typename U>
  30. static auto eval(T* t, U* u) -> decltype(t->move_construct(*u, *u), char());
  31. static char (&eval(...))[2];
  32. public:
  33. static const bool value =
  34. sizeof(service_has_move::eval(
  35. static_cast<service_type*>(0),
  36. static_cast<implementation_type*>(0))) == 1;
  37. };
  38. }
  39. #endif // defined(ASIO_HAS_MOVE)
  40. /// Base class for all I/O objects.
  41. /**
  42. * @note All I/O objects are non-copyable. However, when using C++0x, certain
  43. * I/O objects do support move construction and move assignment.
  44. */
  45. #if !defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  46. template <typename IoObjectService>
  47. #else
  48. template <typename IoObjectService,
  49. bool Movable = detail::service_has_move<IoObjectService>::value>
  50. #endif
  51. class basic_io_object
  52. {
  53. public:
  54. /// The type of the service that will be used to provide I/O operations.
  55. typedef IoObjectService service_type;
  56. /// The underlying implementation type of I/O object.
  57. typedef typename service_type::implementation_type implementation_type;
  58. #if !defined(ASIO_NO_DEPRECATED)
  59. /// (Deprecated: Use get_executor().) Get the io_context associated with the
  60. /// object.
  61. /**
  62. * This function may be used to obtain the io_context object that the I/O
  63. * object uses to dispatch handlers for asynchronous operations.
  64. *
  65. * @return A reference to the io_context object that the I/O object will use
  66. * to dispatch handlers. Ownership is not transferred to the caller.
  67. */
  68. asio::io_context& get_io_context()
  69. {
  70. return service_.get_io_context();
  71. }
  72. /// (Deprecated: Use get_executor().) Get the io_context associated with the
  73. /// object.
  74. /**
  75. * This function may be used to obtain the io_context object that the I/O
  76. * object uses to dispatch handlers for asynchronous operations.
  77. *
  78. * @return A reference to the io_context object that the I/O object will use
  79. * to dispatch handlers. Ownership is not transferred to the caller.
  80. */
  81. asio::io_context& get_io_service()
  82. {
  83. return service_.get_io_context();
  84. }
  85. #endif // !defined(ASIO_NO_DEPRECATED)
  86. /// The type of the executor associated with the object.
  87. typedef asio::io_context::executor_type executor_type;
  88. /// Get the executor associated with the object.
  89. executor_type get_executor() ASIO_NOEXCEPT
  90. {
  91. return service_.get_io_context().get_executor();
  92. }
  93. protected:
  94. /// Construct a basic_io_object.
  95. /**
  96. * Performs:
  97. * @code get_service().construct(get_implementation()); @endcode
  98. */
  99. explicit basic_io_object(asio::io_context& io_context)
  100. : service_(asio::use_service<IoObjectService>(io_context))
  101. {
  102. service_.construct(implementation_);
  103. }
  104. #if defined(GENERATING_DOCUMENTATION)
  105. /// Move-construct a basic_io_object.
  106. /**
  107. * Performs:
  108. * @code get_service().move_construct(
  109. * get_implementation(), other.get_implementation()); @endcode
  110. *
  111. * @note Available only for services that support movability,
  112. */
  113. basic_io_object(basic_io_object&& other);
  114. /// Move-assign a basic_io_object.
  115. /**
  116. * Performs:
  117. * @code get_service().move_assign(get_implementation(),
  118. * other.get_service(), other.get_implementation()); @endcode
  119. *
  120. * @note Available only for services that support movability,
  121. */
  122. basic_io_object& operator=(basic_io_object&& other);
  123. #endif // defined(GENERATING_DOCUMENTATION)
  124. /// Protected destructor to prevent deletion through this type.
  125. /**
  126. * Performs:
  127. * @code get_service().destroy(get_implementation()); @endcode
  128. */
  129. ~basic_io_object()
  130. {
  131. service_.destroy(implementation_);
  132. }
  133. /// Get the service associated with the I/O object.
  134. service_type& get_service()
  135. {
  136. return service_;
  137. }
  138. /// Get the service associated with the I/O object.
  139. const service_type& get_service() const
  140. {
  141. return service_;
  142. }
  143. /// Get the underlying implementation of the I/O object.
  144. implementation_type& get_implementation()
  145. {
  146. return implementation_;
  147. }
  148. /// Get the underlying implementation of the I/O object.
  149. const implementation_type& get_implementation() const
  150. {
  151. return implementation_;
  152. }
  153. private:
  154. basic_io_object(const basic_io_object&);
  155. basic_io_object& operator=(const basic_io_object&);
  156. // The service associated with the I/O object.
  157. service_type& service_;
  158. /// The underlying implementation of the I/O object.
  159. implementation_type implementation_;
  160. };
  161. #if defined(ASIO_HAS_MOVE)
  162. // Specialisation for movable objects.
  163. template <typename IoObjectService>
  164. class basic_io_object<IoObjectService, true>
  165. {
  166. public:
  167. typedef IoObjectService service_type;
  168. typedef typename service_type::implementation_type implementation_type;
  169. #if !defined(ASIO_NO_DEPRECATED)
  170. asio::io_context& get_io_context()
  171. {
  172. return service_->get_io_context();
  173. }
  174. asio::io_context& get_io_service()
  175. {
  176. return service_->get_io_context();
  177. }
  178. #endif // !defined(ASIO_NO_DEPRECATED)
  179. typedef asio::io_context::executor_type executor_type;
  180. executor_type get_executor() ASIO_NOEXCEPT
  181. {
  182. return service_->get_io_context().get_executor();
  183. }
  184. protected:
  185. explicit basic_io_object(asio::io_context& io_context)
  186. : service_(&asio::use_service<IoObjectService>(io_context))
  187. {
  188. service_->construct(implementation_);
  189. }
  190. basic_io_object(basic_io_object&& other)
  191. : service_(&other.get_service())
  192. {
  193. service_->move_construct(implementation_, other.implementation_);
  194. }
  195. ~basic_io_object()
  196. {
  197. service_->destroy(implementation_);
  198. }
  199. basic_io_object& operator=(basic_io_object&& other)
  200. {
  201. service_->move_assign(implementation_,
  202. *other.service_, other.implementation_);
  203. service_ = other.service_;
  204. return *this;
  205. }
  206. service_type& get_service()
  207. {
  208. return *service_;
  209. }
  210. const service_type& get_service() const
  211. {
  212. return *service_;
  213. }
  214. implementation_type& get_implementation()
  215. {
  216. return implementation_;
  217. }
  218. const implementation_type& get_implementation() const
  219. {
  220. return implementation_;
  221. }
  222. private:
  223. basic_io_object(const basic_io_object&);
  224. void operator=(const basic_io_object&);
  225. IoObjectService* service_;
  226. implementation_type implementation_;
  227. };
  228. #endif // defined(ASIO_HAS_MOVE)
  229. } // namespace asio
  230. #include "asio/detail/pop_options.hpp"
  231. #endif // ASIO_BASIC_IO_OBJECT_HPP