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.

194 lines
5.4KB

  1. //
  2. // io_object_impl.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_DETAIL_IO_OBJECT_IMPL_HPP
  11. #define ASIO_DETAIL_IO_OBJECT_IMPL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <new>
  16. #include "asio/detail/config.hpp"
  17. #include "asio/detail/io_object_executor.hpp"
  18. #include "asio/detail/type_traits.hpp"
  19. #include "asio/io_context.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. class executor;
  23. namespace detail {
  24. inline bool is_native_io_executor(const io_context::executor_type&)
  25. {
  26. return true;
  27. }
  28. template <typename Executor>
  29. inline bool is_native_io_executor(const Executor&,
  30. typename enable_if<!is_same<Executor, executor>::value>::type* = 0)
  31. {
  32. return false;
  33. }
  34. template <typename Executor>
  35. inline bool is_native_io_executor(const Executor& ex,
  36. typename enable_if<is_same<Executor, executor>::value>::type* = 0)
  37. {
  38. #if !defined (ASIO_NO_TYPEID)
  39. return ex.target_type() == typeid(io_context::executor_type);
  40. #else // !defined (ASIO_NO_TYPEID)
  41. return false;
  42. #endif // !defined (ASIO_NO_TYPEID)
  43. }
  44. template <typename IoObjectService,
  45. typename Executor = io_context::executor_type>
  46. class io_object_impl
  47. {
  48. public:
  49. // The type of the service that will be used to provide I/O operations.
  50. typedef IoObjectService service_type;
  51. // The underlying implementation type of I/O object.
  52. typedef typename service_type::implementation_type implementation_type;
  53. // The type of the executor associated with the object.
  54. typedef Executor executor_type;
  55. // The type of executor to be used when implementing asynchronous operations.
  56. typedef io_object_executor<Executor> implementation_executor_type;
  57. // Construct an I/O object using an executor.
  58. explicit io_object_impl(const executor_type& ex)
  59. : service_(&asio::use_service<IoObjectService>(ex.context())),
  60. implementation_executor_(ex, (is_native_io_executor)(ex))
  61. {
  62. service_->construct(implementation_);
  63. }
  64. // Construct an I/O object using an execution context.
  65. template <typename ExecutionContext>
  66. explicit io_object_impl(ExecutionContext& context,
  67. typename enable_if<is_convertible<
  68. ExecutionContext&, execution_context&>::value>::type* = 0)
  69. : service_(&asio::use_service<IoObjectService>(context)),
  70. implementation_executor_(context.get_executor(),
  71. is_same<ExecutionContext, io_context>::value)
  72. {
  73. service_->construct(implementation_);
  74. }
  75. #if defined(ASIO_HAS_MOVE)
  76. // Move-construct an I/O object.
  77. io_object_impl(io_object_impl&& other)
  78. : service_(&other.get_service()),
  79. implementation_executor_(other.get_implementation_executor())
  80. {
  81. service_->move_construct(implementation_, other.implementation_);
  82. }
  83. // Perform a converting move-construction of an I/O object.
  84. template <typename IoObjectService1, typename Executor1>
  85. io_object_impl(io_object_impl<IoObjectService1, Executor1>&& other)
  86. : service_(&asio::use_service<IoObjectService>(
  87. other.get_implementation_executor().context())),
  88. implementation_executor_(other.get_implementation_executor())
  89. {
  90. service_->converting_move_construct(implementation_,
  91. other.get_service(), other.get_implementation());
  92. }
  93. #endif // defined(ASIO_HAS_MOVE)
  94. // Destructor.
  95. ~io_object_impl()
  96. {
  97. service_->destroy(implementation_);
  98. }
  99. #if defined(ASIO_HAS_MOVE)
  100. // Move-assign an I/O object.
  101. io_object_impl& operator=(io_object_impl&& other)
  102. {
  103. if (this != &other)
  104. {
  105. service_->move_assign(implementation_,
  106. *other.service_, other.implementation_);
  107. implementation_executor_.~implementation_executor_type();
  108. new (&implementation_executor_) implementation_executor_type(
  109. std::move(other.implementation_executor_));
  110. service_ = other.service_;
  111. }
  112. return *this;
  113. }
  114. #endif // defined(ASIO_HAS_MOVE)
  115. // Get the executor associated with the object.
  116. executor_type get_executor() ASIO_NOEXCEPT
  117. {
  118. return implementation_executor_.inner_executor();
  119. }
  120. // Get the executor to be used when implementing asynchronous operations.
  121. const implementation_executor_type& get_implementation_executor()
  122. ASIO_NOEXCEPT
  123. {
  124. return implementation_executor_;
  125. }
  126. // Get the service associated with the I/O object.
  127. service_type& get_service()
  128. {
  129. return *service_;
  130. }
  131. // Get the service associated with the I/O object.
  132. const service_type& get_service() const
  133. {
  134. return *service_;
  135. }
  136. // Get the underlying implementation of the I/O object.
  137. implementation_type& get_implementation()
  138. {
  139. return implementation_;
  140. }
  141. // Get the underlying implementation of the I/O object.
  142. const implementation_type& get_implementation() const
  143. {
  144. return implementation_;
  145. }
  146. private:
  147. // Disallow copying and copy assignment.
  148. io_object_impl(const io_object_impl&);
  149. io_object_impl& operator=(const io_object_impl&);
  150. // The service associated with the I/O object.
  151. service_type* service_;
  152. // The underlying implementation of the I/O object.
  153. implementation_type implementation_;
  154. // The associated executor.
  155. implementation_executor_type implementation_executor_;
  156. };
  157. } // namespace detail
  158. } // namespace asio
  159. #include "asio/detail/pop_options.hpp"
  160. #endif // ASIO_DETAIL_IO_OBJECT_IMPL_HPP