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
23namespace boost {
24namespace beast {
25namespace detail {
26
27//------------------------------------------------------------------------------
28
29inline
30net::io_context*
31get_io_context(net::io_context& ioc)
32{
33 return std::addressof(r&: ioc);
34}
35
36inline
37net::io_context*
38get_io_context(net::io_context::executor_type const& ex)
39{
40 return std::addressof(r&: net::query(ex, net::execution::context));
41}
42
43inline
44net::io_context*
45get_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
51template<class Executor>
52net::io_context*
53get_io_context(net::strand<Executor> const& ex)
54{
55 return get_io_context(ex.get_inner_executor());
56}
57
58template<
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>
62net::io_context*
63get_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
72inline
73net::io_context*
74get_io_context(...)
75{
76 return nullptr;
77}
78
79//------------------------------------------------------------------------------
80
81template<class T>
82net::io_context*
83get_io_context_impl(T& t, std::true_type)
84{
85 return get_io_context(
86 t.get_executor());
87}
88
89template<class T>
90net::io_context*
91get_io_context_impl(T const&, std::false_type)
92{
93 return nullptr;
94}
95
96// Returns the io_context*, or nullptr, for any object.
97template<class T>
98net::io_context*
99get_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

source code of boost/libs/beast/include/boost/beast/core/detail/get_io_context.hpp