| 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_GET_IO_CONTEXT_HPP |
| 11 | #define BOOST_BEAST_DETAIL_GET_IO_CONTEXT_HPP |
| 12 | |
| 13 | #include <boost/beast/core/stream_traits.hpp> |
| 14 | #ifdef BOOST_ASIO_NO_TS_EXECUTORS |
| 15 | #include <boost/asio/execution.hpp> |
| 16 | #endif |
| 17 | #include <boost/asio/executor.hpp> |
| 18 | #include <boost/asio/io_context.hpp> |
| 19 | #include <boost/asio/strand.hpp> |
| 20 | #include <memory> |
| 21 | #include <type_traits> |
| 22 | |
| 23 | namespace boost { |
| 24 | namespace beast { |
| 25 | namespace detail { |
| 26 | |
| 27 | //------------------------------------------------------------------------------ |
| 28 | |
| 29 | inline |
| 30 | net::io_context* |
| 31 | get_io_context(net::io_context& ioc) |
| 32 | { |
| 33 | return std::addressof(r&: ioc); |
| 34 | } |
| 35 | |
| 36 | inline |
| 37 | net::io_context* |
| 38 | get_io_context(net::io_context::executor_type const& ex) |
| 39 | { |
| 40 | return std::addressof(r&: net::query(ex, net::execution::context)); |
| 41 | } |
| 42 | |
| 43 | inline |
| 44 | net::io_context* |
| 45 | get_io_context(net::strand< |
| 46 | net::io_context::executor_type> const& ex) |
| 47 | { |
| 48 | return get_io_context(ex: ex.get_inner_executor()); |
| 49 | } |
| 50 | |
| 51 | template<class Executor> |
| 52 | net::io_context* |
| 53 | get_io_context(net::strand<Executor> const& ex) |
| 54 | { |
| 55 | return get_io_context(ex.get_inner_executor()); |
| 56 | } |
| 57 | |
| 58 | template< |
| 59 | class T, |
| 60 | class = typename std::enable_if< |
| 61 | std::is_same<T, net::executor>::value || std::is_same<T, net::any_io_executor>::value>::type> |
| 62 | net::io_context* |
| 63 | get_io_context(T const& ex) |
| 64 | { |
| 65 | auto p = ex.template target<typename |
| 66 | net::io_context::executor_type>(); |
| 67 | if(! p) |
| 68 | return nullptr; |
| 69 | return get_io_context(*p); |
| 70 | } |
| 71 | |
| 72 | inline |
| 73 | net::io_context* |
| 74 | get_io_context(...) |
| 75 | { |
| 76 | return nullptr; |
| 77 | } |
| 78 | |
| 79 | //------------------------------------------------------------------------------ |
| 80 | |
| 81 | template<class T> |
| 82 | net::io_context* |
| 83 | get_io_context_impl(T& t, std::true_type) |
| 84 | { |
| 85 | return get_io_context( |
| 86 | t.get_executor()); |
| 87 | } |
| 88 | |
| 89 | template<class T> |
| 90 | net::io_context* |
| 91 | get_io_context_impl(T const&, std::false_type) |
| 92 | { |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
| 96 | // Returns the io_context*, or nullptr, for any object. |
| 97 | template<class T> |
| 98 | net::io_context* |
| 99 | get_io_context(T& t) |
| 100 | { |
| 101 | return get_io_context_impl(t, |
| 102 | has_get_executor<T>{}); |
| 103 | } |
| 104 | |
| 105 | } // detail |
| 106 | } // beast |
| 107 | } // boost |
| 108 | |
| 109 | #endif |
| 110 | |