1 //
2 // detail/handler_invoke_helpers.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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_HANDLER_INVOKE_HELPERS_HPP
12 #define BOOST_ASIO_DETAIL_HANDLER_INVOKE_HELPERS_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/memory.hpp>
20 #include <boost/asio/handler_invoke_hook.hpp>
21 
22 #include <boost/asio/detail/push_options.hpp>
23 
24 // Calls to asio_handler_invoke must be made from a namespace that does not
25 // contain overloads of this function. The boost_asio_handler_invoke_helpers
26 // namespace is defined here for that purpose.
27 namespace boost_asio_handler_invoke_helpers {
28 
29 #if defined(BOOST_ASIO_NO_DEPRECATED)
30 template <typename Function, typename Context>
error_if_hook_is_defined(Function & function,Context & context)31 inline void error_if_hook_is_defined(Function& function, Context& context)
32 {
33   using boost::asio::asio_handler_invoke;
34   // If you get an error here it is because some of your handlers still
35   // overload asio_handler_invoke, but this hook is no longer used.
36   (void)static_cast<boost::asio::asio_handler_invoke_is_no_longer_used>(
37     asio_handler_invoke(function, boost::asio::detail::addressof(context)));
38 }
39 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
40 
41 template <typename Function, typename Context>
invoke(Function & function,Context & context)42 inline void invoke(Function& function, Context& context)
43 {
44 #if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS)
45   Function tmp(function);
46   tmp();
47 #elif defined(BOOST_ASIO_NO_DEPRECATED)
48   // The asio_handler_invoke hook is no longer used to invoke the function.
49   (void)&error_if_hook_is_defined<Function, Context>;
50   (void)context;
51   function();
52 #else
53   using boost::asio::asio_handler_invoke;
54   asio_handler_invoke(function, boost::asio::detail::addressof(context));
55 #endif
56 }
57 
58 template <typename Function, typename Context>
invoke(const Function & function,Context & context)59 inline void invoke(const Function& function, Context& context)
60 {
61 #if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS)
62   Function tmp(function);
63   tmp();
64 #elif defined(BOOST_ASIO_NO_DEPRECATED)
65   // The asio_handler_invoke hook is no longer used to invoke the function.
66   (void)&error_if_hook_is_defined<const Function, Context>;
67   (void)context;
68   Function tmp(function);
69   tmp();
70 #else
71   using boost::asio::asio_handler_invoke;
72   asio_handler_invoke(function, boost::asio::detail::addressof(context));
73 #endif
74 }
75 
76 } // namespace boost_asio_handler_invoke_helpers
77 
78 #include <boost/asio/detail/pop_options.hpp>
79 
80 #endif // BOOST_ASIO_DETAIL_HANDLER_INVOKE_HELPERS_HPP
81