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