get_io_context.hpp 2.2 KB

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