composed_work.hpp 4.9 KB

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