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.

420 lines
12KB

  1. //
  2. // impl/compose.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_IMPL_COMPOSE_HPP
  11. #define ASIO_IMPL_COMPOSE_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/detail/handler_alloc_helpers.hpp"
  17. #include "asio/detail/handler_cont_helpers.hpp"
  18. #include "asio/detail/handler_invoke_helpers.hpp"
  19. #include "asio/detail/type_traits.hpp"
  20. #include "asio/detail/variadic_templates.hpp"
  21. #include "asio/executor_work_guard.hpp"
  22. #include "asio/is_executor.hpp"
  23. #include "asio/system_executor.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace detail
  27. {
  28. template <typename>
  29. struct composed_work;
  30. template <>
  31. struct composed_work<void()>
  32. {
  33. composed_work() ASIO_NOEXCEPT
  34. : head_(system_executor())
  35. {
  36. }
  37. void reset()
  38. {
  39. head_.reset();
  40. }
  41. typedef system_executor head_type;
  42. executor_work_guard<system_executor> head_;
  43. };
  44. inline composed_work<void()> make_composed_work()
  45. {
  46. return composed_work<void()>();
  47. }
  48. template <typename Head>
  49. struct composed_work<void(Head)>
  50. {
  51. explicit composed_work(const Head& ex) ASIO_NOEXCEPT
  52. : head_(ex)
  53. {
  54. }
  55. void reset()
  56. {
  57. head_.reset();
  58. }
  59. typedef Head head_type;
  60. executor_work_guard<Head> head_;
  61. };
  62. template <typename Head>
  63. inline composed_work<void(Head)> make_composed_work(const Head& head)
  64. {
  65. return composed_work<void(Head)>(head);
  66. }
  67. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  68. template <typename Head, typename... Tail>
  69. struct composed_work<void(Head, Tail...)>
  70. {
  71. explicit composed_work(const Head& head,
  72. const Tail&... tail) ASIO_NOEXCEPT
  73. : head_(head),
  74. tail_(tail...)
  75. {
  76. }
  77. void reset()
  78. {
  79. head_.reset();
  80. tail_.reset();
  81. }
  82. typedef Head head_type;
  83. executor_work_guard<Head> head_;
  84. composed_work<void(Tail...)> tail_;
  85. };
  86. template <typename Head, typename... Tail>
  87. inline composed_work<void(Head, Tail...)>
  88. make_composed_work(const Head& head, const Tail&... tail)
  89. {
  90. return composed_work<void(Head, Tail...)>(head, tail...);
  91. }
  92. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  93. #define ASIO_PRIVATE_COMPOSED_WORK_DEF(n) \
  94. template <typename Head, ASIO_VARIADIC_TPARAMS(n)> \
  95. struct composed_work<void(Head, ASIO_VARIADIC_TARGS(n))> \
  96. { \
  97. explicit composed_work(const Head& head, \
  98. ASIO_VARIADIC_CONSTREF_PARAMS(n)) ASIO_NOEXCEPT \
  99. : head_(head), \
  100. tail_(ASIO_VARIADIC_BYVAL_ARGS(n)) \
  101. { \
  102. } \
  103. \
  104. void reset() \
  105. { \
  106. head_.reset(); \
  107. tail_.reset(); \
  108. } \
  109. \
  110. typedef Head head_type; \
  111. executor_work_guard<Head> head_; \
  112. composed_work<void(ASIO_VARIADIC_TARGS(n))> tail_; \
  113. }; \
  114. \
  115. template <typename Head, ASIO_VARIADIC_TPARAMS(n)> \
  116. inline composed_work<void(Head, ASIO_VARIADIC_TARGS(n))> \
  117. make_composed_work(const Head& head, ASIO_VARIADIC_CONSTREF_PARAMS(n)) \
  118. { \
  119. return composed_work< \
  120. void(Head, ASIO_VARIADIC_TARGS(n))>( \
  121. head, ASIO_VARIADIC_BYVAL_ARGS(n)); \
  122. } \
  123. /**/
  124. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_WORK_DEF)
  125. #undef ASIO_PRIVATE_COMPOSED_WORK_DEF
  126. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  127. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  128. template <typename Impl, typename Work, typename Handler, typename Signature>
  129. class composed_op;
  130. template <typename Impl, typename Work, typename Handler,
  131. typename R, typename... Args>
  132. class composed_op<Impl, Work, Handler, R(Args...)>
  133. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  134. template <typename Impl, typename Work, typename Handler, typename Signature>
  135. class composed_op
  136. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  137. {
  138. public:
  139. composed_op(ASIO_MOVE_ARG(Impl) impl,
  140. ASIO_MOVE_ARG(Work) work,
  141. ASIO_MOVE_ARG(Handler) handler)
  142. : impl_(ASIO_MOVE_CAST(Impl)(impl)),
  143. work_(ASIO_MOVE_CAST(Work)(work)),
  144. handler_(ASIO_MOVE_CAST(Handler)(handler)),
  145. invocations_(0)
  146. {
  147. }
  148. #if defined(ASIO_HAS_MOVE)
  149. composed_op(composed_op&& other)
  150. : impl_(ASIO_MOVE_CAST(Impl)(other.impl_)),
  151. work_(ASIO_MOVE_CAST(Work)(other.work_)),
  152. handler_(ASIO_MOVE_CAST(Handler)(other.handler_)),
  153. invocations_(other.invocations_)
  154. {
  155. }
  156. #endif // defined(ASIO_HAS_MOVE)
  157. typedef typename associated_executor<Handler,
  158. typename Work::head_type>::type executor_type;
  159. executor_type get_executor() const ASIO_NOEXCEPT
  160. {
  161. return (get_associated_executor)(handler_, work_.head_.get_executor());
  162. }
  163. typedef typename associated_allocator<Handler,
  164. std::allocator<void> >::type allocator_type;
  165. allocator_type get_allocator() const ASIO_NOEXCEPT
  166. {
  167. return (get_associated_allocator)(handler_, std::allocator<void>());
  168. }
  169. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  170. template<typename... T>
  171. void operator()(ASIO_MOVE_ARG(T)... t)
  172. {
  173. if (invocations_ < ~unsigned(0))
  174. ++invocations_;
  175. impl_(*this, ASIO_MOVE_CAST(T)(t)...);
  176. }
  177. void complete(Args... args)
  178. {
  179. this->work_.reset();
  180. this->handler_(ASIO_MOVE_CAST(Args)(args)...);
  181. }
  182. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  183. void operator()()
  184. {
  185. if (invocations_ < ~unsigned(0))
  186. ++invocations_;
  187. impl_(*this);
  188. }
  189. void complete()
  190. {
  191. this->work_.reset();
  192. this->handler_();
  193. }
  194. #define ASIO_PRIVATE_COMPOSED_OP_DEF(n) \
  195. template<ASIO_VARIADIC_TPARAMS(n)> \
  196. void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \
  197. { \
  198. if (invocations_ < ~unsigned(0)) \
  199. ++invocations_; \
  200. impl_(*this, ASIO_VARIADIC_MOVE_ARGS(n)); \
  201. } \
  202. \
  203. template<ASIO_VARIADIC_TPARAMS(n)> \
  204. void complete(ASIO_VARIADIC_MOVE_PARAMS(n)) \
  205. { \
  206. this->work_.reset(); \
  207. this->handler_(ASIO_VARIADIC_MOVE_ARGS(n)); \
  208. } \
  209. /**/
  210. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_OP_DEF)
  211. #undef ASIO_PRIVATE_COMPOSED_OP_DEF
  212. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  213. //private:
  214. Impl impl_;
  215. Work work_;
  216. Handler handler_;
  217. unsigned invocations_;
  218. };
  219. template <typename Impl, typename Work, typename Handler, typename Signature>
  220. inline void* asio_handler_allocate(std::size_t size,
  221. composed_op<Impl, Work, Handler, Signature>* this_handler)
  222. {
  223. return asio_handler_alloc_helpers::allocate(
  224. size, this_handler->handler_);
  225. }
  226. template <typename Impl, typename Work, typename Handler, typename Signature>
  227. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  228. composed_op<Impl, Work, Handler, Signature>* this_handler)
  229. {
  230. asio_handler_alloc_helpers::deallocate(
  231. pointer, size, this_handler->handler_);
  232. }
  233. template <typename Impl, typename Work, typename Handler, typename Signature>
  234. inline bool asio_handler_is_continuation(
  235. composed_op<Impl, Work, Handler, Signature>* this_handler)
  236. {
  237. return this_handler->invocations_ > 1 ? true
  238. : asio_handler_cont_helpers::is_continuation(
  239. this_handler->handler_);
  240. }
  241. template <typename Function, typename Impl,
  242. typename Work, typename Handler, typename Signature>
  243. inline void asio_handler_invoke(Function& function,
  244. composed_op<Impl, Work, Handler, Signature>* this_handler)
  245. {
  246. asio_handler_invoke_helpers::invoke(
  247. function, this_handler->handler_);
  248. }
  249. template <typename Function, typename Impl,
  250. typename Work, typename Handler, typename Signature>
  251. inline void asio_handler_invoke(const Function& function,
  252. composed_op<Impl, Work, Handler, Signature>* this_handler)
  253. {
  254. asio_handler_invoke_helpers::invoke(
  255. function, this_handler->handler_);
  256. }
  257. template <typename Signature>
  258. struct initiate_composed_op
  259. {
  260. template <typename Handler, typename Impl, typename Work>
  261. void operator()(ASIO_MOVE_ARG(Handler) handler,
  262. ASIO_MOVE_ARG(Impl) impl,
  263. ASIO_MOVE_ARG(Work) work) const
  264. {
  265. composed_op<typename decay<Impl>::type, typename decay<Work>::type,
  266. typename decay<Handler>::type, Signature>(
  267. ASIO_MOVE_CAST(Impl)(impl), ASIO_MOVE_CAST(Work)(work),
  268. ASIO_MOVE_CAST(Handler)(handler))();
  269. }
  270. };
  271. template <typename IoObject>
  272. inline typename IoObject::executor_type
  273. get_composed_io_executor(IoObject& io_object)
  274. {
  275. return io_object.get_executor();
  276. }
  277. template <typename Executor>
  278. inline const Executor& get_composed_io_executor(const Executor& ex,
  279. typename enable_if<is_executor<Executor>::value>::type* = 0)
  280. {
  281. return ex;
  282. }
  283. } // namespace detail
  284. #if !defined(GENERATING_DOCUMENTATION)
  285. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  286. template <typename CompletionToken, typename Signature,
  287. typename Implementation, typename... IoObjectsOrExecutors>
  288. ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature)
  289. async_compose(ASIO_MOVE_ARG(Implementation) implementation,
  290. ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token,
  291. ASIO_MOVE_ARG(IoObjectsOrExecutors)... io_objects_or_executors)
  292. {
  293. return async_initiate<CompletionToken, Signature>(
  294. detail::initiate_composed_op<Signature>(), token,
  295. ASIO_MOVE_CAST(Implementation)(implementation),
  296. detail::make_composed_work(
  297. detail::get_composed_io_executor(
  298. ASIO_MOVE_CAST(IoObjectsOrExecutors)(
  299. io_objects_or_executors))...));
  300. }
  301. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  302. template <typename CompletionToken, typename Signature, typename Implementation>
  303. ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature)
  304. async_compose(ASIO_MOVE_ARG(Implementation) implementation,
  305. ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token)
  306. {
  307. return async_initiate<CompletionToken, Signature>(
  308. detail::initiate_composed_op<Signature>(), token,
  309. ASIO_MOVE_CAST(Implementation)(implementation),
  310. detail::make_composed_work());
  311. }
  312. # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n) \
  313. ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_##n
  314. # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1 \
  315. detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1))
  316. # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2 \
  317. detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
  318. detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2))
  319. # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3 \
  320. detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
  321. detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
  322. detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3))
  323. # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4 \
  324. detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
  325. detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
  326. detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)), \
  327. detail::get_composed_io_executor(ASIO_MOVE_CAST(T4)(x4))
  328. # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5 \
  329. detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
  330. detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
  331. detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)), \
  332. detail::get_composed_io_executor(ASIO_MOVE_CAST(T4)(x4)), \
  333. detail::get_composed_io_executor(ASIO_MOVE_CAST(T5)(x5))
  334. #define ASIO_PRIVATE_ASYNC_COMPOSE_DEF(n) \
  335. template <typename CompletionToken, typename Signature, \
  336. typename Implementation, ASIO_VARIADIC_TPARAMS(n)> \
  337. ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature) \
  338. async_compose(ASIO_MOVE_ARG(Implementation) implementation, \
  339. ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token, \
  340. ASIO_VARIADIC_MOVE_PARAMS(n)) \
  341. { \
  342. return async_initiate<CompletionToken, Signature>( \
  343. detail::initiate_composed_op<Signature>(), token, \
  344. ASIO_MOVE_CAST(Implementation)(implementation), \
  345. detail::make_composed_work( \
  346. ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n))); \
  347. } \
  348. /**/
  349. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_COMPOSE_DEF)
  350. #undef ASIO_PRIVATE_ASYNC_COMPOSE_DEF
  351. #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR
  352. #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1
  353. #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2
  354. #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3
  355. #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4
  356. #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5
  357. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  358. #endif // !defined(GENERATING_DOCUMENTATION)
  359. } // namespace asio
  360. #include "asio/detail/pop_options.hpp"
  361. #endif // ASIO_IMPL_COMPOSE_HPP