executor_function.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // detail/executor_function.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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 BOOST_ASIO_DETAIL_EXECUTOR_FUNCTION_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  17. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  18. #include <boost/asio/detail/memory.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. #if defined(BOOST_ASIO_HAS_MOVE)
  24. // Lightweight, move-only function object wrapper.
  25. class executor_function
  26. {
  27. public:
  28. template <typename F, typename Alloc>
  29. explicit executor_function(F f, const Alloc& a)
  30. {
  31. // Allocate and construct an object to wrap the function.
  32. typedef impl<F, Alloc> impl_type;
  33. typename impl_type::ptr p = {
  34. detail::addressof(a), impl_type::ptr::allocate(a), 0 };
  35. impl_ = new (p.v) impl_type(BOOST_ASIO_MOVE_CAST(F)(f), a);
  36. p.v = 0;
  37. }
  38. executor_function(executor_function&& other) BOOST_ASIO_NOEXCEPT
  39. : impl_(other.impl_)
  40. {
  41. other.impl_ = 0;
  42. }
  43. ~executor_function()
  44. {
  45. if (impl_)
  46. impl_->complete_(impl_, false);
  47. }
  48. void operator()()
  49. {
  50. if (impl_)
  51. {
  52. impl_base* i = impl_;
  53. impl_ = 0;
  54. i->complete_(i, true);
  55. }
  56. }
  57. private:
  58. // Base class for polymorphic function implementations.
  59. struct impl_base
  60. {
  61. void (*complete_)(impl_base*, bool);
  62. };
  63. // Polymorphic function implementation.
  64. template <typename Function, typename Alloc>
  65. struct impl : impl_base
  66. {
  67. BOOST_ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR(
  68. thread_info_base::executor_function_tag, impl);
  69. template <typename F>
  70. impl(BOOST_ASIO_MOVE_ARG(F) f, const Alloc& a)
  71. : function_(BOOST_ASIO_MOVE_CAST(F)(f)),
  72. allocator_(a)
  73. {
  74. complete_ = &executor_function::complete<Function, Alloc>;
  75. }
  76. Function function_;
  77. Alloc allocator_;
  78. };
  79. // Helper to complete function invocation.
  80. template <typename Function, typename Alloc>
  81. static void complete(impl_base* base, bool call)
  82. {
  83. // Take ownership of the function object.
  84. impl<Function, Alloc>* i(static_cast<impl<Function, Alloc>*>(base));
  85. Alloc allocator(i->allocator_);
  86. typename impl<Function, Alloc>::ptr p = {
  87. detail::addressof(allocator), i, i };
  88. // Make a copy of the function so that the memory can be deallocated before
  89. // the upcall is made. Even if we're not about to make an upcall, a
  90. // sub-object of the function may be the true owner of the memory
  91. // associated with the function. Consequently, a local copy of the function
  92. // is required to ensure that any owning sub-object remains valid until
  93. // after we have deallocated the memory here.
  94. Function function(BOOST_ASIO_MOVE_CAST(Function)(i->function_));
  95. p.reset();
  96. // Make the upcall if required.
  97. if (call)
  98. {
  99. boost_asio_handler_invoke_helpers::invoke(function, function);
  100. }
  101. }
  102. impl_base* impl_;
  103. };
  104. #else // defined(BOOST_ASIO_HAS_MOVE)
  105. // Not so lightweight, copyable function object wrapper.
  106. class executor_function
  107. {
  108. public:
  109. template <typename F, typename Alloc>
  110. explicit executor_function(const F& f, const Alloc&)
  111. : impl_(new impl<typename decay<F>::type>(f))
  112. {
  113. }
  114. void operator()()
  115. {
  116. impl_->complete_(impl_.get());
  117. }
  118. private:
  119. // Base class for polymorphic function implementations.
  120. struct impl_base
  121. {
  122. void (*complete_)(impl_base*);
  123. };
  124. // Polymorphic function implementation.
  125. template <typename F>
  126. struct impl : impl_base
  127. {
  128. impl(const F& f)
  129. : function_(f)
  130. {
  131. complete_ = &executor_function::complete<F>;
  132. }
  133. F function_;
  134. };
  135. // Helper to complete function invocation.
  136. template <typename F>
  137. static void complete(impl_base* i)
  138. {
  139. static_cast<impl<F>*>(i)->function_();
  140. }
  141. shared_ptr<impl_base> impl_;
  142. };
  143. #endif // defined(BOOST_ASIO_HAS_MOVE)
  144. // Lightweight, non-owning, copyable function object wrapper.
  145. class executor_function_view
  146. {
  147. public:
  148. template <typename F>
  149. explicit executor_function_view(F& f) BOOST_ASIO_NOEXCEPT
  150. : complete_(&executor_function_view::complete<F>),
  151. function_(&f)
  152. {
  153. }
  154. void operator()()
  155. {
  156. complete_(function_);
  157. }
  158. private:
  159. // Helper to complete function invocation.
  160. template <typename F>
  161. static void complete(void* f)
  162. {
  163. (*static_cast<F*>(f))();
  164. }
  165. void (*complete_)(void*);
  166. void* function_;
  167. };
  168. } // namespace detail
  169. } // namespace asio
  170. } // namespace boost
  171. #include <boost/asio/detail/pop_options.hpp>
  172. #endif // BOOST_ASIO_DETAIL_EXECUTOR_FUNCTION_HPP