composed_work.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // detail/composed_work.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_DETAIL_COMPOSED_WORK_HPP
  11. #define BOOST_ASIO_DETAIL_COMPOSED_WORK_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/execution/outstanding_work.hpp>
  19. #include <boost/asio/executor_work_guard.hpp>
  20. #include <boost/asio/is_executor.hpp>
  21. #include <boost/asio/system_executor.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. template <typename Executor, typename = void>
  27. class composed_work_guard
  28. {
  29. public:
  30. typedef decay_t<
  31. prefer_result_t<Executor, execution::outstanding_work_t::tracked_t>
  32. > executor_type;
  33. composed_work_guard(const Executor& ex)
  34. : executor_(boost::asio::prefer(ex, execution::outstanding_work.tracked))
  35. {
  36. }
  37. void reset()
  38. {
  39. }
  40. executor_type get_executor() const noexcept
  41. {
  42. return executor_;
  43. }
  44. private:
  45. executor_type executor_;
  46. };
  47. template <>
  48. struct composed_work_guard<system_executor>
  49. {
  50. public:
  51. typedef system_executor executor_type;
  52. composed_work_guard(const system_executor&)
  53. {
  54. }
  55. void reset()
  56. {
  57. }
  58. executor_type get_executor() const noexcept
  59. {
  60. return system_executor();
  61. }
  62. };
  63. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  64. template <typename Executor>
  65. struct composed_work_guard<Executor,
  66. enable_if_t<
  67. !execution::is_executor<Executor>::value
  68. >
  69. > : executor_work_guard<Executor>
  70. {
  71. composed_work_guard(const Executor& ex)
  72. : executor_work_guard<Executor>(ex)
  73. {
  74. }
  75. };
  76. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  77. template <typename>
  78. struct composed_io_executors;
  79. template <>
  80. struct composed_io_executors<void()>
  81. {
  82. composed_io_executors() noexcept
  83. : head_(system_executor())
  84. {
  85. }
  86. typedef system_executor head_type;
  87. system_executor head_;
  88. };
  89. inline composed_io_executors<void()> make_composed_io_executors()
  90. {
  91. return composed_io_executors<void()>();
  92. }
  93. template <typename Head>
  94. struct composed_io_executors<void(Head)>
  95. {
  96. explicit composed_io_executors(const Head& ex) noexcept
  97. : head_(ex)
  98. {
  99. }
  100. typedef Head head_type;
  101. Head head_;
  102. };
  103. template <typename Head>
  104. inline composed_io_executors<void(Head)>
  105. make_composed_io_executors(const Head& head)
  106. {
  107. return composed_io_executors<void(Head)>(head);
  108. }
  109. template <typename Head, typename... Tail>
  110. struct composed_io_executors<void(Head, Tail...)>
  111. {
  112. explicit composed_io_executors(const Head& head,
  113. const Tail&... tail) noexcept
  114. : head_(head),
  115. tail_(tail...)
  116. {
  117. }
  118. void reset()
  119. {
  120. head_.reset();
  121. tail_.reset();
  122. }
  123. typedef Head head_type;
  124. Head head_;
  125. composed_io_executors<void(Tail...)> tail_;
  126. };
  127. template <typename Head, typename... Tail>
  128. inline composed_io_executors<void(Head, Tail...)>
  129. make_composed_io_executors(const Head& head, const Tail&... tail)
  130. {
  131. return composed_io_executors<void(Head, Tail...)>(head, tail...);
  132. }
  133. template <typename>
  134. struct composed_work;
  135. template <>
  136. struct composed_work<void()>
  137. {
  138. typedef composed_io_executors<void()> executors_type;
  139. composed_work(const executors_type&) noexcept
  140. : head_(system_executor())
  141. {
  142. }
  143. void reset()
  144. {
  145. head_.reset();
  146. }
  147. typedef system_executor head_type;
  148. composed_work_guard<system_executor> head_;
  149. };
  150. template <typename Head>
  151. struct composed_work<void(Head)>
  152. {
  153. typedef composed_io_executors<void(Head)> executors_type;
  154. explicit composed_work(const executors_type& ex) noexcept
  155. : head_(ex.head_)
  156. {
  157. }
  158. void reset()
  159. {
  160. head_.reset();
  161. }
  162. typedef Head head_type;
  163. composed_work_guard<Head> head_;
  164. };
  165. template <typename Head, typename... Tail>
  166. struct composed_work<void(Head, Tail...)>
  167. {
  168. typedef composed_io_executors<void(Head, Tail...)> executors_type;
  169. explicit composed_work(const executors_type& ex) noexcept
  170. : head_(ex.head_),
  171. tail_(ex.tail_)
  172. {
  173. }
  174. void reset()
  175. {
  176. head_.reset();
  177. tail_.reset();
  178. }
  179. typedef Head head_type;
  180. composed_work_guard<Head> head_;
  181. composed_work<void(Tail...)> tail_;
  182. };
  183. template <typename IoObject>
  184. inline typename IoObject::executor_type
  185. get_composed_io_executor(IoObject& io_object,
  186. enable_if_t<
  187. !is_executor<IoObject>::value
  188. >* = 0,
  189. enable_if_t<
  190. !execution::is_executor<IoObject>::value
  191. >* = 0)
  192. {
  193. return io_object.get_executor();
  194. }
  195. template <typename Executor>
  196. inline const Executor& get_composed_io_executor(const Executor& ex,
  197. enable_if_t<
  198. is_executor<Executor>::value
  199. || execution::is_executor<Executor>::value
  200. >* = 0)
  201. {
  202. return ex;
  203. }
  204. } // namespace detail
  205. } // namespace asio
  206. } // namespace boost
  207. #include <boost/asio/detail/pop_options.hpp>
  208. #endif // BOOST_ASIO_DETAIL_COMPOSED_WORK_HPP