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.

292 lines
7.9KB

  1. //
  2. // detail/wrapped_handler.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_DETAIL_WRAPPED_HANDLER_HPP
  11. #define ASIO_DETAIL_WRAPPED_HANDLER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/bind_handler.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/push_options.hpp"
  20. namespace asio {
  21. namespace detail {
  22. struct is_continuation_delegated
  23. {
  24. template <typename Dispatcher, typename Handler>
  25. bool operator()(Dispatcher&, Handler& handler) const
  26. {
  27. return asio_handler_cont_helpers::is_continuation(handler);
  28. }
  29. };
  30. struct is_continuation_if_running
  31. {
  32. template <typename Dispatcher, typename Handler>
  33. bool operator()(Dispatcher& dispatcher, Handler&) const
  34. {
  35. return dispatcher.running_in_this_thread();
  36. }
  37. };
  38. template <typename Dispatcher, typename Handler,
  39. typename IsContinuation = is_continuation_delegated>
  40. class wrapped_handler
  41. {
  42. public:
  43. typedef void result_type;
  44. wrapped_handler(Dispatcher dispatcher, Handler& handler)
  45. : dispatcher_(dispatcher),
  46. handler_(ASIO_MOVE_CAST(Handler)(handler))
  47. {
  48. }
  49. #if defined(ASIO_HAS_MOVE)
  50. wrapped_handler(const wrapped_handler& other)
  51. : dispatcher_(other.dispatcher_),
  52. handler_(other.handler_)
  53. {
  54. }
  55. wrapped_handler(wrapped_handler&& other)
  56. : dispatcher_(other.dispatcher_),
  57. handler_(ASIO_MOVE_CAST(Handler)(other.handler_))
  58. {
  59. }
  60. #endif // defined(ASIO_HAS_MOVE)
  61. void operator()()
  62. {
  63. dispatcher_.dispatch(ASIO_MOVE_CAST(Handler)(handler_));
  64. }
  65. void operator()() const
  66. {
  67. dispatcher_.dispatch(handler_);
  68. }
  69. template <typename Arg1>
  70. void operator()(const Arg1& arg1)
  71. {
  72. dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
  73. }
  74. template <typename Arg1>
  75. void operator()(const Arg1& arg1) const
  76. {
  77. dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
  78. }
  79. template <typename Arg1, typename Arg2>
  80. void operator()(const Arg1& arg1, const Arg2& arg2)
  81. {
  82. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
  83. }
  84. template <typename Arg1, typename Arg2>
  85. void operator()(const Arg1& arg1, const Arg2& arg2) const
  86. {
  87. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
  88. }
  89. template <typename Arg1, typename Arg2, typename Arg3>
  90. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
  91. {
  92. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
  93. }
  94. template <typename Arg1, typename Arg2, typename Arg3>
  95. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const
  96. {
  97. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
  98. }
  99. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  100. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  101. const Arg4& arg4)
  102. {
  103. dispatcher_.dispatch(
  104. detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
  105. }
  106. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  107. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  108. const Arg4& arg4) const
  109. {
  110. dispatcher_.dispatch(
  111. detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
  112. }
  113. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
  114. typename Arg5>
  115. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  116. const Arg4& arg4, const Arg5& arg5)
  117. {
  118. dispatcher_.dispatch(
  119. detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
  120. }
  121. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
  122. typename Arg5>
  123. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  124. const Arg4& arg4, const Arg5& arg5) const
  125. {
  126. dispatcher_.dispatch(
  127. detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
  128. }
  129. //private:
  130. Dispatcher dispatcher_;
  131. Handler handler_;
  132. };
  133. template <typename Handler, typename Context>
  134. class rewrapped_handler
  135. {
  136. public:
  137. explicit rewrapped_handler(Handler& handler, const Context& context)
  138. : context_(context),
  139. handler_(ASIO_MOVE_CAST(Handler)(handler))
  140. {
  141. }
  142. explicit rewrapped_handler(const Handler& handler, const Context& context)
  143. : context_(context),
  144. handler_(handler)
  145. {
  146. }
  147. #if defined(ASIO_HAS_MOVE)
  148. rewrapped_handler(const rewrapped_handler& other)
  149. : context_(other.context_),
  150. handler_(other.handler_)
  151. {
  152. }
  153. rewrapped_handler(rewrapped_handler&& other)
  154. : context_(ASIO_MOVE_CAST(Context)(other.context_)),
  155. handler_(ASIO_MOVE_CAST(Handler)(other.handler_))
  156. {
  157. }
  158. #endif // defined(ASIO_HAS_MOVE)
  159. void operator()()
  160. {
  161. handler_();
  162. }
  163. void operator()() const
  164. {
  165. handler_();
  166. }
  167. //private:
  168. Context context_;
  169. Handler handler_;
  170. };
  171. template <typename Dispatcher, typename Handler, typename IsContinuation>
  172. inline void* asio_handler_allocate(std::size_t size,
  173. wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
  174. {
  175. return asio_handler_alloc_helpers::allocate(
  176. size, this_handler->handler_);
  177. }
  178. template <typename Dispatcher, typename Handler, typename IsContinuation>
  179. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  180. wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
  181. {
  182. asio_handler_alloc_helpers::deallocate(
  183. pointer, size, this_handler->handler_);
  184. }
  185. template <typename Dispatcher, typename Handler, typename IsContinuation>
  186. inline bool asio_handler_is_continuation(
  187. wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
  188. {
  189. return IsContinuation()(this_handler->dispatcher_, this_handler->handler_);
  190. }
  191. template <typename Function, typename Dispatcher,
  192. typename Handler, typename IsContinuation>
  193. inline void asio_handler_invoke(Function& function,
  194. wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
  195. {
  196. this_handler->dispatcher_.dispatch(
  197. rewrapped_handler<Function, Handler>(
  198. function, this_handler->handler_));
  199. }
  200. template <typename Function, typename Dispatcher,
  201. typename Handler, typename IsContinuation>
  202. inline void asio_handler_invoke(const Function& function,
  203. wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
  204. {
  205. this_handler->dispatcher_.dispatch(
  206. rewrapped_handler<Function, Handler>(
  207. function, this_handler->handler_));
  208. }
  209. template <typename Handler, typename Context>
  210. inline void* asio_handler_allocate(std::size_t size,
  211. rewrapped_handler<Handler, Context>* this_handler)
  212. {
  213. return asio_handler_alloc_helpers::allocate(
  214. size, this_handler->context_);
  215. }
  216. template <typename Handler, typename Context>
  217. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  218. rewrapped_handler<Handler, Context>* this_handler)
  219. {
  220. asio_handler_alloc_helpers::deallocate(
  221. pointer, size, this_handler->context_);
  222. }
  223. template <typename Dispatcher, typename Context>
  224. inline bool asio_handler_is_continuation(
  225. rewrapped_handler<Dispatcher, Context>* this_handler)
  226. {
  227. return asio_handler_cont_helpers::is_continuation(
  228. this_handler->context_);
  229. }
  230. template <typename Function, typename Handler, typename Context>
  231. inline void asio_handler_invoke(Function& function,
  232. rewrapped_handler<Handler, Context>* this_handler)
  233. {
  234. asio_handler_invoke_helpers::invoke(
  235. function, this_handler->context_);
  236. }
  237. template <typename Function, typename Handler, typename Context>
  238. inline void asio_handler_invoke(const Function& function,
  239. rewrapped_handler<Handler, Context>* this_handler)
  240. {
  241. asio_handler_invoke_helpers::invoke(
  242. function, this_handler->context_);
  243. }
  244. } // namespace detail
  245. } // namespace asio
  246. #include "asio/detail/pop_options.hpp"
  247. #endif // ASIO_DETAIL_WRAPPED_HANDLER_HPP