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.

executor_function.hpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // detail/executor_function.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_EXECUTOR_FUNCTION_HPP
  11. #define ASIO_DETAIL_EXECUTOR_FUNCTION_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/push_options.hpp"
  18. namespace asio {
  19. namespace detail {
  20. class executor_function_base
  21. {
  22. public:
  23. void complete()
  24. {
  25. func_(this, true);
  26. }
  27. void destroy()
  28. {
  29. func_(this, false);
  30. }
  31. protected:
  32. typedef void (*func_type)(executor_function_base*, bool);
  33. executor_function_base(func_type func)
  34. : func_(func)
  35. {
  36. }
  37. // Prevents deletion through this type.
  38. ~executor_function_base()
  39. {
  40. }
  41. private:
  42. func_type func_;
  43. };
  44. template <typename Function, typename Alloc>
  45. class executor_function : public executor_function_base
  46. {
  47. public:
  48. ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR(
  49. thread_info_base::executor_function_tag, executor_function);
  50. template <typename F>
  51. executor_function(ASIO_MOVE_ARG(F) f, const Alloc& allocator)
  52. : executor_function_base(&executor_function::do_complete),
  53. function_(ASIO_MOVE_CAST(F)(f)),
  54. allocator_(allocator)
  55. {
  56. }
  57. static void do_complete(executor_function_base* base, bool call)
  58. {
  59. // Take ownership of the function object.
  60. executor_function* o(static_cast<executor_function*>(base));
  61. Alloc allocator(o->allocator_);
  62. ptr p = { detail::addressof(allocator), o, o };
  63. // Make a copy of the function so that the memory can be deallocated before
  64. // the upcall is made. Even if we're not about to make an upcall, a
  65. // sub-object of the function may be the true owner of the memory
  66. // associated with the function. Consequently, a local copy of the function
  67. // is required to ensure that any owning sub-object remains valid until
  68. // after we have deallocated the memory here.
  69. Function function(ASIO_MOVE_CAST(Function)(o->function_));
  70. p.reset();
  71. // Make the upcall if required.
  72. if (call)
  73. {
  74. function();
  75. }
  76. }
  77. private:
  78. Function function_;
  79. Alloc allocator_;
  80. };
  81. } // namespace detail
  82. } // namespace asio
  83. #include "asio/detail/pop_options.hpp"
  84. #endif // ASIO_DETAIL_EXECUTOR_FUNCTION_HPP