Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:37

0001 //
0002 // detail/completion_handler.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_COMPLETION_HANDLER_HPP
0012 #define BOOST_ASIO_DETAIL_COMPLETION_HANDLER_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/fenced_block.hpp>
0020 #include <boost/asio/detail/handler_alloc_helpers.hpp>
0021 #include <boost/asio/detail/handler_work.hpp>
0022 #include <boost/asio/detail/memory.hpp>
0023 #include <boost/asio/detail/operation.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace detail {
0030 
0031 template <typename Handler, typename IoExecutor>
0032 class completion_handler : public operation
0033 {
0034 public:
0035   BOOST_ASIO_DEFINE_HANDLER_PTR(completion_handler);
0036 
0037   completion_handler(Handler& h, const IoExecutor& io_ex)
0038     : operation(&completion_handler::do_complete),
0039       handler_(static_cast<Handler&&>(h)),
0040       work_(handler_, io_ex)
0041   {
0042   }
0043 
0044   static void do_complete(void* owner, operation* base,
0045       const boost::system::error_code& /*ec*/,
0046       std::size_t /*bytes_transferred*/)
0047   {
0048     // Take ownership of the handler object.
0049     completion_handler* h(static_cast<completion_handler*>(base));
0050     ptr p = { boost::asio::detail::addressof(h->handler_), h, h };
0051 
0052     BOOST_ASIO_HANDLER_COMPLETION((*h));
0053 
0054     // Take ownership of the operation's outstanding work.
0055     handler_work<Handler, IoExecutor> w(
0056         static_cast<handler_work<Handler, IoExecutor>&&>(
0057           h->work_));
0058 
0059     // Make a copy of the handler so that the memory can be deallocated before
0060     // the upcall is made. Even if we're not about to make an upcall, a
0061     // sub-object of the handler may be the true owner of the memory associated
0062     // with the handler. Consequently, a local copy of the handler is required
0063     // to ensure that any owning sub-object remains valid until after we have
0064     // deallocated the memory here.
0065     Handler handler(static_cast<Handler&&>(h->handler_));
0066     p.h = boost::asio::detail::addressof(handler);
0067     p.reset();
0068 
0069     // Make the upcall if required.
0070     if (owner)
0071     {
0072       fenced_block b(fenced_block::half);
0073       BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
0074       w.complete(handler, handler);
0075       BOOST_ASIO_HANDLER_INVOCATION_END;
0076     }
0077   }
0078 
0079 private:
0080   Handler handler_;
0081   handler_work<Handler, IoExecutor> work_;
0082 };
0083 
0084 } // namespace detail
0085 } // namespace asio
0086 } // namespace boost
0087 
0088 #include <boost/asio/detail/pop_options.hpp>
0089 
0090 #endif // BOOST_ASIO_DETAIL_COMPLETION_HANDLER_HPP