| 1 | // |
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | |
| 10 | #ifndef BOOST_BEAST_DETAIL_BIND_CONTINUATION_HPP |
| 11 | #define BOOST_BEAST_DETAIL_BIND_CONTINUATION_HPP |
| 12 | |
| 13 | #include <boost/beast/core/detail/config.hpp> |
| 14 | #include <boost/beast/core/detail/remap_post_to_defer.hpp> |
| 15 | #include <boost/asio/bind_executor.hpp> |
| 16 | #include <boost/core/empty_value.hpp> |
| 17 | #include <type_traits> |
| 18 | #include <utility> |
| 19 | |
| 20 | namespace boost { |
| 21 | namespace beast { |
| 22 | namespace detail { |
| 23 | |
| 24 | #if 0 |
| 25 | /** Mark a completion handler as a continuation. |
| 26 | |
| 27 | This function wraps a completion handler to associate it with an |
| 28 | executor whose `post` operation is remapped to the `defer` operation. |
| 29 | It is used by composed asynchronous operation implementations to |
| 30 | indicate that a completion handler submitted to an initiating |
| 31 | function represents a continuation of the current asynchronous |
| 32 | flow of control. |
| 33 | |
| 34 | @param handler The handler to wrap. |
| 35 | The implementation takes ownership of the handler by performing a decay-copy. |
| 36 | |
| 37 | @see |
| 38 | |
| 39 | @li <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4242.html">[N4242] Executors and Asynchronous Operations, Revision 1</a> |
| 40 | */ |
| 41 | template<class CompletionHandler> |
| 42 | #if BOOST_BEAST_DOXYGEN |
| 43 | __implementation_defined__ |
| 44 | #else |
| 45 | net::executor_binder< |
| 46 | typename std::decay<CompletionHandler>::type, |
| 47 | detail::remap_post_to_defer< |
| 48 | net::associated_executor_t<CompletionHandler>>> |
| 49 | #endif |
| 50 | bind_continuation(CompletionHandler&& handler) |
| 51 | { |
| 52 | return net::bind_executor( |
| 53 | detail::remap_post_to_defer< |
| 54 | net::associated_executor_t<CompletionHandler>>( |
| 55 | net::get_associated_executor(handler)), |
| 56 | std::forward<CompletionHandler>(handler)); |
| 57 | } |
| 58 | |
| 59 | /** Mark a completion handler as a continuation. |
| 60 | |
| 61 | This function wraps a completion handler to associate it with an |
| 62 | executor whose `post` operation is remapped to the `defer` operation. |
| 63 | It is used by composed asynchronous operation implementations to |
| 64 | indicate that a completion handler submitted to an initiating |
| 65 | function represents a continuation of the current asynchronous |
| 66 | flow of control. |
| 67 | |
| 68 | @param ex The executor to use |
| 69 | |
| 70 | @param handler The handler to wrap |
| 71 | The implementation takes ownership of the handler by performing a decay-copy. |
| 72 | |
| 73 | @see |
| 74 | |
| 75 | @li <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4242.html">[N4242] Executors and Asynchronous Operations, Revision 1</a> |
| 76 | */ |
| 77 | template<class Executor, class CompletionHandler> |
| 78 | #if BOOST_BEAST_DOXYGEN |
| 79 | __implementation_defined__ |
| 80 | #else |
| 81 | net::executor_binder<typename |
| 82 | std::decay<CompletionHandler>::type, |
| 83 | detail::remap_post_to_defer<Executor>> |
| 84 | #endif |
| 85 | bind_continuation( |
| 86 | Executor const& ex, CompletionHandler&& handler) |
| 87 | { |
| 88 | return net::bind_executor( |
| 89 | detail::remap_post_to_defer<Executor>(ex), |
| 90 | std::forward<CompletionHandler>(handler)); |
| 91 | } |
| 92 | #else |
| 93 | // VFALCO I turned these off at the last minute because they cause |
| 94 | // the completion handler to be moved before the initiating |
| 95 | // function is invoked rather than after, which is a foot-gun. |
| 96 | // |
| 97 | // REMINDER: Uncomment the tests when this is put back |
| 98 | template<class F> |
| 99 | F&& |
| 100 | bind_continuation(F&& f) |
| 101 | { |
| 102 | return std::forward<F>(f); |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | } // detail |
| 107 | } // beast |
| 108 | } // boost |
| 109 | |
| 110 | #endif |
| 111 | |