2 // detail/executor_function.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_EXECUTOR_FUNCTION_HPP
12 #define BOOST_ASIO_DETAIL_EXECUTOR_FUNCTION_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/handler_alloc_helpers.hpp>
20 #include <boost/asio/detail/handler_invoke_helpers.hpp>
21 #include <boost/asio/detail/memory.hpp>
23 #include <boost/asio/detail/push_options.hpp>
29 #if defined(BOOST_ASIO_HAS_MOVE)
31 // Lightweight, move-only function object wrapper.
32 class executor_function
35 template <typename F, typename Alloc>
36 explicit executor_function(F f, const Alloc& a)
38 // Allocate and construct an object to wrap the function.
39 typedef impl<F, Alloc> impl_type;
40 typename impl_type::ptr p = {
41 detail::addressof(a), impl_type::ptr::allocate(a), 0 };
42 impl_ = new (p.v) impl_type(BOOST_ASIO_MOVE_CAST(F)(f), a);
46 executor_function(executor_function&& other) BOOST_ASIO_NOEXCEPT
55 impl_->complete_(impl_, false);
64 i->complete_(i, true);
69 // Base class for polymorphic function implementations.
72 void (*complete_)(impl_base*, bool);
75 // Polymorphic function implementation.
76 template <typename Function, typename Alloc>
77 struct impl : impl_base
79 BOOST_ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR(
80 thread_info_base::executor_function_tag, impl);
83 impl(BOOST_ASIO_MOVE_ARG(F) f, const Alloc& a)
84 : function_(BOOST_ASIO_MOVE_CAST(F)(f)),
87 complete_ = &executor_function::complete<Function, Alloc>;
94 // Helper to complete function invocation.
95 template <typename Function, typename Alloc>
96 static void complete(impl_base* base, bool call)
98 // Take ownership of the function object.
99 impl<Function, Alloc>* i(static_cast<impl<Function, Alloc>*>(base));
100 Alloc allocator(i->allocator_);
101 typename impl<Function, Alloc>::ptr p = {
102 detail::addressof(allocator), i, i };
104 // Make a copy of the function so that the memory can be deallocated before
105 // the upcall is made. Even if we're not about to make an upcall, a
106 // sub-object of the function may be the true owner of the memory
107 // associated with the function. Consequently, a local copy of the function
108 // is required to ensure that any owning sub-object remains valid until
109 // after we have deallocated the memory here.
110 Function function(BOOST_ASIO_MOVE_CAST(Function)(i->function_));
113 // Make the upcall if required.
116 boost_asio_handler_invoke_helpers::invoke(function, function);
123 #else // defined(BOOST_ASIO_HAS_MOVE)
125 // Not so lightweight, copyable function object wrapper.
126 class executor_function
129 template <typename F, typename Alloc>
130 explicit executor_function(const F& f, const Alloc&)
131 : impl_(new impl<typename decay<F>::type>(f))
137 impl_->complete_(impl_.get());
141 // Base class for polymorphic function implementations.
144 void (*complete_)(impl_base*);
147 // Polymorphic function implementation.
148 template <typename F>
149 struct impl : impl_base
154 complete_ = &executor_function::complete<F>;
160 // Helper to complete function invocation.
161 template <typename F>
162 static void complete(impl_base* i)
164 static_cast<impl<F>*>(i)->function_();
167 shared_ptr<impl_base> impl_;
170 #endif // defined(BOOST_ASIO_HAS_MOVE)
172 // Lightweight, non-owning, copyable function object wrapper.
173 class executor_function_view
176 template <typename F>
177 explicit executor_function_view(F& f) BOOST_ASIO_NOEXCEPT
178 : complete_(&executor_function_view::complete<F>),
185 complete_(function_);
189 // Helper to complete function invocation.
190 template <typename F>
191 static void complete(void* f)
193 (*static_cast<F*>(f))();
196 void (*complete_)(void*);
200 } // namespace detail
204 #include <boost/asio/detail/pop_options.hpp>
206 #endif // BOOST_ASIO_DETAIL_EXECUTOR_FUNCTION_HPP