Chris@16
|
1 //
|
Chris@16
|
2 // handler_invoke_hook.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_HANDLER_INVOKE_HOOK_HPP
|
Chris@16
|
12 #define BOOST_ASIO_HANDLER_INVOKE_HOOK_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost {
|
Chris@16
|
23 namespace asio {
|
Chris@16
|
24
|
Chris@16
|
25 /** @defgroup asio_handler_invoke boost::asio::asio_handler_invoke
|
Chris@16
|
26 *
|
Chris@16
|
27 * @brief Default invoke function for handlers.
|
Chris@16
|
28 *
|
Chris@16
|
29 * Completion handlers for asynchronous operations are invoked by the
|
Chris@16
|
30 * io_service associated with the corresponding object (e.g. a socket or
|
Chris@16
|
31 * deadline_timer). Certain guarantees are made on when the handler may be
|
Chris@16
|
32 * invoked, in particular that a handler can only be invoked from a thread that
|
Chris@16
|
33 * is currently calling @c run() on the corresponding io_service object.
|
Chris@16
|
34 * Handlers may subsequently be invoked through other objects (such as
|
Chris@16
|
35 * io_service::strand objects) that provide additional guarantees.
|
Chris@16
|
36 *
|
Chris@16
|
37 * When asynchronous operations are composed from other asynchronous
|
Chris@16
|
38 * operations, all intermediate handlers should be invoked using the same
|
Chris@16
|
39 * method as the final handler. This is required to ensure that user-defined
|
Chris@16
|
40 * objects are not accessed in a way that may violate the guarantees. This
|
Chris@16
|
41 * hooking function ensures that the invoked method used for the final handler
|
Chris@16
|
42 * is accessible at each intermediate step.
|
Chris@16
|
43 *
|
Chris@16
|
44 * Implement asio_handler_invoke for your own handlers to specify a custom
|
Chris@16
|
45 * invocation strategy.
|
Chris@16
|
46 *
|
Chris@16
|
47 * This default implementation invokes the function object like so:
|
Chris@16
|
48 * @code function(); @endcode
|
Chris@16
|
49 * If necessary, the default implementation makes a copy of the function object
|
Chris@16
|
50 * so that the non-const operator() can be used.
|
Chris@16
|
51 *
|
Chris@16
|
52 * @par Example
|
Chris@16
|
53 * @code
|
Chris@16
|
54 * class my_handler;
|
Chris@16
|
55 *
|
Chris@16
|
56 * template <typename Function>
|
Chris@16
|
57 * void asio_handler_invoke(Function function, my_handler* context)
|
Chris@16
|
58 * {
|
Chris@16
|
59 * context->strand_.dispatch(function);
|
Chris@16
|
60 * }
|
Chris@16
|
61 * @endcode
|
Chris@16
|
62 */
|
Chris@16
|
63 /*@{*/
|
Chris@16
|
64
|
Chris@16
|
65 /// Default handler invocation hook used for non-const function objects.
|
Chris@16
|
66 template <typename Function>
|
Chris@16
|
67 inline void asio_handler_invoke(Function& function, ...)
|
Chris@16
|
68 {
|
Chris@16
|
69 function();
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 /// Default handler invocation hook used for const function objects.
|
Chris@16
|
73 template <typename Function>
|
Chris@16
|
74 inline void asio_handler_invoke(const Function& function, ...)
|
Chris@16
|
75 {
|
Chris@16
|
76 Function tmp(function);
|
Chris@16
|
77 tmp();
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 /*@}*/
|
Chris@16
|
81
|
Chris@16
|
82 } // namespace asio
|
Chris@16
|
83 } // namespace boost
|
Chris@16
|
84
|
Chris@16
|
85 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
86
|
Chris@16
|
87 #endif // BOOST_ASIO_HANDLER_INVOKE_HOOK_HPP
|