PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/detail/completion_handler.hpp

http://hadesmem.googlecode.com/
C++ Header | 81 lines | 49 code | 15 blank | 17 comment | 2 complexity | c2c7f39e733a6fdab36704dbde2b8723 MD5 | raw file
 1//
 2// detail/completion_handler.hpp
 3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 4//
 5// Copyright (c) 2003-2011 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
11#ifndef BOOST_ASIO_DETAIL_COMPLETION_HANDLER_HPP
12#define BOOST_ASIO_DETAIL_COMPLETION_HANDLER_HPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/config.hpp>
19#include <boost/asio/detail/fenced_block.hpp>
20#include <boost/asio/detail/handler_alloc_helpers.hpp>
21#include <boost/asio/detail/handler_invoke_helpers.hpp>
22#include <boost/asio/detail/operation.hpp>
23
24#include <boost/asio/detail/push_options.hpp>
25
26namespace boost {
27namespace asio {
28namespace detail {
29
30template <typename Handler>
31class completion_handler : public operation
32{
33public:
34  BOOST_ASIO_DEFINE_HANDLER_PTR(completion_handler);
35
36  completion_handler(Handler& h)
37    : operation(&completion_handler::do_complete),
38      handler_(BOOST_ASIO_MOVE_CAST(Handler)(h))
39  {
40  }
41
42  static void do_complete(io_service_impl* owner, operation* base,
43      boost::system::error_code /*ec*/, std::size_t /*bytes_transferred*/)
44  {
45    // Take ownership of the handler object.
46    completion_handler* h(static_cast<completion_handler*>(base));
47    ptr p = { boost::addressof(h->handler_), h, h };
48
49    BOOST_ASIO_HANDLER_COMPLETION((h));
50
51    // Make a copy of the handler so that the memory can be deallocated before
52    // the upcall is made. Even if we're not about to make an upcall, a
53    // sub-object of the handler may be the true owner of the memory associated
54    // with the handler. Consequently, a local copy of the handler is required
55    // to ensure that any owning sub-object remains valid until after we have
56    // deallocated the memory here.
57    Handler handler(BOOST_ASIO_MOVE_CAST(Handler)(h->handler_));
58    p.h = boost::addressof(handler);
59    p.reset();
60
61    // Make the upcall if required.
62    if (owner)
63    {
64      boost::asio::detail::fenced_block b;
65      BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
66      boost_asio_handler_invoke_helpers::invoke(handler, handler);
67      BOOST_ASIO_HANDLER_INVOCATION_END;
68    }
69  }
70
71private:
72  Handler handler_;
73};
74
75} // namespace detail
76} // namespace asio
77} // namespace boost
78
79#include <boost/asio/detail/pop_options.hpp>
80
81#endif // BOOST_ASIO_DETAIL_COMPLETION_HANDLER_HPP