associated_executor.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // associated_executor.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. #ifndef BOOST_ASIO_ASSOCIATED_EXECUTOR_HPP
  11. #define BOOST_ASIO_ASSOCIATED_EXECUTOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/type_traits.hpp>
  17. #include <boost/asio/execution/executor.hpp>
  18. #include <boost/asio/is_executor.hpp>
  19. #include <boost/asio/system_executor.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. template <typename T, typename E, typename = void>
  25. struct associated_executor_impl
  26. {
  27. typedef void asio_associated_executor_is_unspecialised;
  28. typedef E type;
  29. static type get(const T&, const E& e = E()) BOOST_ASIO_NOEXCEPT
  30. {
  31. return e;
  32. }
  33. };
  34. template <typename T, typename E>
  35. struct associated_executor_impl<T, E,
  36. typename void_type<typename T::executor_type>::type>
  37. {
  38. typedef typename T::executor_type type;
  39. static type get(const T& t, const E& = E()) BOOST_ASIO_NOEXCEPT
  40. {
  41. return t.get_executor();
  42. }
  43. };
  44. } // namespace detail
  45. /// Traits type used to obtain the executor associated with an object.
  46. /**
  47. * A program may specialise this traits type if the @c T template parameter in
  48. * the specialisation is a user-defined type. The template parameter @c
  49. * Executor shall be a type meeting the Executor requirements.
  50. *
  51. * Specialisations shall meet the following requirements, where @c t is a const
  52. * reference to an object of type @c T, and @c e is an object of type @c
  53. * Executor.
  54. *
  55. * @li Provide a nested typedef @c type that identifies a type meeting the
  56. * Executor requirements.
  57. *
  58. * @li Provide a noexcept static member function named @c get, callable as @c
  59. * get(t) and with return type @c type.
  60. *
  61. * @li Provide a noexcept static member function named @c get, callable as @c
  62. * get(t,e) and with return type @c type.
  63. */
  64. template <typename T, typename Executor = system_executor>
  65. struct associated_executor
  66. #if !defined(GENERATING_DOCUMENTATION)
  67. : detail::associated_executor_impl<T, Executor>
  68. #endif // !defined(GENERATING_DOCUMENTATION)
  69. {
  70. #if defined(GENERATING_DOCUMENTATION)
  71. /// If @c T has a nested type @c executor_type, <tt>T::executor_type</tt>.
  72. /// Otherwise @c Executor.
  73. typedef see_below type;
  74. /// If @c T has a nested type @c executor_type, returns
  75. /// <tt>t.get_executor()</tt>. Otherwise returns @c ex.
  76. static type get(const T& t,
  77. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT;
  78. #endif // defined(GENERATING_DOCUMENTATION)
  79. };
  80. /// Helper function to obtain an object's associated executor.
  81. /**
  82. * @returns <tt>associated_executor<T>::get(t)</tt>
  83. */
  84. template <typename T>
  85. inline typename associated_executor<T>::type
  86. get_associated_executor(const T& t) BOOST_ASIO_NOEXCEPT
  87. {
  88. return associated_executor<T>::get(t);
  89. }
  90. /// Helper function to obtain an object's associated executor.
  91. /**
  92. * @returns <tt>associated_executor<T, Executor>::get(t, ex)</tt>
  93. */
  94. template <typename T, typename Executor>
  95. inline typename associated_executor<T, Executor>::type
  96. get_associated_executor(const T& t, const Executor& ex,
  97. typename constraint<
  98. is_executor<Executor>::value || execution::is_executor<Executor>::value
  99. >::type = 0) BOOST_ASIO_NOEXCEPT
  100. {
  101. return associated_executor<T, Executor>::get(t, ex);
  102. }
  103. /// Helper function to obtain an object's associated executor.
  104. /**
  105. * @returns <tt>associated_executor<T, typename
  106. * ExecutionContext::executor_type>::get(t, ctx.get_executor())</tt>
  107. */
  108. template <typename T, typename ExecutionContext>
  109. inline typename associated_executor<T,
  110. typename ExecutionContext::executor_type>::type
  111. get_associated_executor(const T& t, ExecutionContext& ctx,
  112. typename constraint<is_convertible<ExecutionContext&,
  113. execution_context&>::value>::type = 0) BOOST_ASIO_NOEXCEPT
  114. {
  115. return associated_executor<T,
  116. typename ExecutionContext::executor_type>::get(t, ctx.get_executor());
  117. }
  118. #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  119. template <typename T, typename Executor = system_executor>
  120. using associated_executor_t = typename associated_executor<T, Executor>::type;
  121. #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  122. namespace detail {
  123. template <typename T, typename E, typename = void>
  124. struct associated_executor_forwarding_base
  125. {
  126. };
  127. template <typename T, typename E>
  128. struct associated_executor_forwarding_base<T, E,
  129. typename enable_if<
  130. is_same<
  131. typename associated_executor<T,
  132. E>::asio_associated_executor_is_unspecialised,
  133. void
  134. >::value
  135. >::type>
  136. {
  137. typedef void asio_associated_executor_is_unspecialised;
  138. };
  139. } // namespace detail
  140. } // namespace asio
  141. } // namespace boost
  142. #include <boost/asio/detail/pop_options.hpp>
  143. #endif // BOOST_ASIO_ASSOCIATED_EXECUTOR_HPP