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.

114 lines
3.1KB

  1. //
  2. // detail/handler_work.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_HANDLER_WORK_HPP
  11. #define ASIO_DETAIL_HANDLER_WORK_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/associated_executor.hpp"
  17. #include "asio/detail/handler_invoke_helpers.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. namespace detail {
  21. // A helper class template to allow completion handlers to be dispatched
  22. // through either the new executors framework or the old invocaton hook. The
  23. // primary template uses the new executors framework.
  24. template <typename Handler,
  25. typename IoExecutor = system_executor, typename HandlerExecutor
  26. = typename associated_executor<Handler, IoExecutor>::type>
  27. class handler_work
  28. {
  29. public:
  30. explicit handler_work(Handler& handler) ASIO_NOEXCEPT
  31. : io_executor_(),
  32. executor_(asio::get_associated_executor(handler, io_executor_))
  33. {
  34. }
  35. handler_work(Handler& handler, const IoExecutor& io_ex) ASIO_NOEXCEPT
  36. : io_executor_(io_ex),
  37. executor_(asio::get_associated_executor(handler, io_executor_))
  38. {
  39. }
  40. static void start(Handler& handler) ASIO_NOEXCEPT
  41. {
  42. HandlerExecutor ex(asio::get_associated_executor(handler));
  43. ex.on_work_started();
  44. }
  45. static void start(Handler& handler,
  46. const IoExecutor& io_ex) ASIO_NOEXCEPT
  47. {
  48. HandlerExecutor ex(asio::get_associated_executor(handler, io_ex));
  49. ex.on_work_started();
  50. io_ex.on_work_started();
  51. }
  52. ~handler_work()
  53. {
  54. io_executor_.on_work_finished();
  55. executor_.on_work_finished();
  56. }
  57. template <typename Function>
  58. void complete(Function& function, Handler& handler)
  59. {
  60. executor_.dispatch(ASIO_MOVE_CAST(Function)(function),
  61. asio::get_associated_allocator(handler));
  62. }
  63. private:
  64. // Disallow copying and assignment.
  65. handler_work(const handler_work&);
  66. handler_work& operator=(const handler_work&);
  67. IoExecutor io_executor_;
  68. HandlerExecutor executor_;
  69. };
  70. // This specialisation dispatches a handler through the old invocation hook.
  71. // The specialisation is not strictly required for correctness, as the
  72. // system_executor will dispatch through the hook anyway. However, by doing
  73. // this we avoid an extra copy of the handler.
  74. template <typename Handler>
  75. class handler_work<Handler, system_executor, system_executor>
  76. {
  77. public:
  78. explicit handler_work(Handler&) ASIO_NOEXCEPT {}
  79. static void start(Handler&) ASIO_NOEXCEPT {}
  80. ~handler_work() {}
  81. template <typename Function>
  82. void complete(Function& function, Handler& handler)
  83. {
  84. asio_handler_invoke_helpers::invoke(function, handler);
  85. }
  86. private:
  87. // Disallow copying and assignment.
  88. handler_work(const handler_work&);
  89. handler_work& operator=(const handler_work&);
  90. };
  91. } // namespace detail
  92. } // namespace asio
  93. #include "asio/detail/pop_options.hpp"
  94. #endif // ASIO_DETAIL_HANDLER_WORK_HPP