any_io_executor.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // any_io_executor.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_ANY_IO_EXECUTOR_HPP
  11. #define BOOST_ASIO_ANY_IO_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. #if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  17. # include <boost/asio/executor.hpp>
  18. #else // defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  19. # include <boost/asio/execution.hpp>
  20. # include <boost/asio/execution_context.hpp>
  21. #endif // defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. #if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  26. typedef executor any_io_executor;
  27. #else // defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  28. /// Polymorphic executor type for use with I/O objects.
  29. /**
  30. * The @c any_io_executor type is a polymorphic executor that supports the set
  31. * of properties required by I/O objects. It is defined as the
  32. * execution::any_executor class template parameterised as follows:
  33. * @code execution::any_executor<
  34. * execution::context_as_t<execution_context&>,
  35. * execution::blocking_t::never_t,
  36. * execution::prefer_only<execution::blocking_t::possibly_t>,
  37. * execution::prefer_only<execution::outstanding_work_t::tracked_t>,
  38. * execution::prefer_only<execution::outstanding_work_t::untracked_t>,
  39. * execution::prefer_only<execution::relationship_t::fork_t>,
  40. * execution::prefer_only<execution::relationship_t::continuation_t>
  41. * > @endcode
  42. */
  43. class any_io_executor :
  44. #if defined(GENERATING_DOCUMENTATION)
  45. public execution::any_executor<...>
  46. #else // defined(GENERATING_DOCUMENTATION)
  47. public execution::any_executor<
  48. execution::context_as_t<execution_context&>,
  49. execution::blocking_t::never_t,
  50. execution::prefer_only<execution::blocking_t::possibly_t>,
  51. execution::prefer_only<execution::outstanding_work_t::tracked_t>,
  52. execution::prefer_only<execution::outstanding_work_t::untracked_t>,
  53. execution::prefer_only<execution::relationship_t::fork_t>,
  54. execution::prefer_only<execution::relationship_t::continuation_t>
  55. >
  56. #endif // defined(GENERATING_DOCUMENTATION)
  57. {
  58. public:
  59. #if !defined(GENERATING_DOCUMENTATION)
  60. typedef execution::any_executor<
  61. execution::context_as_t<execution_context&>,
  62. execution::blocking_t::never_t,
  63. execution::prefer_only<execution::blocking_t::possibly_t>,
  64. execution::prefer_only<execution::outstanding_work_t::tracked_t>,
  65. execution::prefer_only<execution::outstanding_work_t::untracked_t>,
  66. execution::prefer_only<execution::relationship_t::fork_t>,
  67. execution::prefer_only<execution::relationship_t::continuation_t>
  68. > base_type;
  69. typedef void supportable_properties_type(
  70. execution::context_as_t<execution_context&>,
  71. execution::blocking_t::never_t,
  72. execution::prefer_only<execution::blocking_t::possibly_t>,
  73. execution::prefer_only<execution::outstanding_work_t::tracked_t>,
  74. execution::prefer_only<execution::outstanding_work_t::untracked_t>,
  75. execution::prefer_only<execution::relationship_t::fork_t>,
  76. execution::prefer_only<execution::relationship_t::continuation_t>
  77. );
  78. #endif // !defined(GENERATING_DOCUMENTATION)
  79. /// Default constructor.
  80. BOOST_ASIO_DECL any_io_executor() noexcept;
  81. /// Construct in an empty state. Equivalent effects to default constructor.
  82. BOOST_ASIO_DECL any_io_executor(nullptr_t) noexcept;
  83. /// Copy constructor.
  84. BOOST_ASIO_DECL any_io_executor(const any_io_executor& e) noexcept;
  85. /// Move constructor.
  86. BOOST_ASIO_DECL any_io_executor(any_io_executor&& e) noexcept;
  87. /// Construct to point to the same target as another any_executor.
  88. #if defined(GENERATING_DOCUMENTATION)
  89. template <class... OtherSupportableProperties>
  90. any_io_executor(execution::any_executor<OtherSupportableProperties...> e);
  91. #else // defined(GENERATING_DOCUMENTATION)
  92. template <typename OtherAnyExecutor>
  93. any_io_executor(OtherAnyExecutor e,
  94. constraint_t<
  95. conditional_t<
  96. !is_same<OtherAnyExecutor, any_io_executor>::value
  97. && is_base_of<execution::detail::any_executor_base,
  98. OtherAnyExecutor>::value,
  99. typename execution::detail::supportable_properties<
  100. 0, supportable_properties_type>::template
  101. is_valid_target<OtherAnyExecutor>,
  102. false_type
  103. >::value
  104. > = 0)
  105. : base_type(static_cast<OtherAnyExecutor&&>(e))
  106. {
  107. }
  108. #endif // defined(GENERATING_DOCUMENTATION)
  109. /// Construct to point to the same target as another any_executor.
  110. #if defined(GENERATING_DOCUMENTATION)
  111. template <class... OtherSupportableProperties>
  112. any_io_executor(std::nothrow_t,
  113. execution::any_executor<OtherSupportableProperties...> e);
  114. #else // defined(GENERATING_DOCUMENTATION)
  115. template <typename OtherAnyExecutor>
  116. any_io_executor(std::nothrow_t, OtherAnyExecutor e,
  117. constraint_t<
  118. conditional_t<
  119. !is_same<OtherAnyExecutor, any_io_executor>::value
  120. && is_base_of<execution::detail::any_executor_base,
  121. OtherAnyExecutor>::value,
  122. typename execution::detail::supportable_properties<
  123. 0, supportable_properties_type>::template
  124. is_valid_target<OtherAnyExecutor>,
  125. false_type
  126. >::value
  127. > = 0) noexcept
  128. : base_type(std::nothrow, static_cast<OtherAnyExecutor&&>(e))
  129. {
  130. }
  131. #endif // defined(GENERATING_DOCUMENTATION)
  132. /// Construct to point to the same target as another any_executor.
  133. BOOST_ASIO_DECL any_io_executor(std::nothrow_t,
  134. const any_io_executor& e) noexcept;
  135. /// Construct to point to the same target as another any_executor.
  136. BOOST_ASIO_DECL any_io_executor(std::nothrow_t, any_io_executor&& e) noexcept;
  137. /// Construct a polymorphic wrapper for the specified executor.
  138. #if defined(GENERATING_DOCUMENTATION)
  139. template <BOOST_ASIO_EXECUTION_EXECUTOR Executor>
  140. any_io_executor(Executor e);
  141. #else // defined(GENERATING_DOCUMENTATION)
  142. template <BOOST_ASIO_EXECUTION_EXECUTOR Executor>
  143. any_io_executor(Executor e,
  144. constraint_t<
  145. conditional_t<
  146. !is_same<Executor, any_io_executor>::value
  147. && !is_base_of<execution::detail::any_executor_base,
  148. Executor>::value,
  149. execution::detail::is_valid_target_executor<
  150. Executor, supportable_properties_type>,
  151. false_type
  152. >::value
  153. > = 0)
  154. : base_type(static_cast<Executor&&>(e))
  155. {
  156. }
  157. #endif // defined(GENERATING_DOCUMENTATION)
  158. /// Construct a polymorphic wrapper for the specified executor.
  159. #if defined(GENERATING_DOCUMENTATION)
  160. template <BOOST_ASIO_EXECUTION_EXECUTOR Executor>
  161. any_io_executor(std::nothrow_t, Executor e);
  162. #else // defined(GENERATING_DOCUMENTATION)
  163. template <BOOST_ASIO_EXECUTION_EXECUTOR Executor>
  164. any_io_executor(std::nothrow_t, Executor e,
  165. constraint_t<
  166. conditional_t<
  167. !is_same<Executor, any_io_executor>::value
  168. && !is_base_of<execution::detail::any_executor_base,
  169. Executor>::value,
  170. execution::detail::is_valid_target_executor<
  171. Executor, supportable_properties_type>,
  172. false_type
  173. >::value
  174. > = 0) noexcept
  175. : base_type(std::nothrow, static_cast<Executor&&>(e))
  176. {
  177. }
  178. #endif // defined(GENERATING_DOCUMENTATION)
  179. /// Assignment operator.
  180. BOOST_ASIO_DECL any_io_executor& operator=(
  181. const any_io_executor& e) noexcept;
  182. /// Move assignment operator.
  183. BOOST_ASIO_DECL any_io_executor& operator=(any_io_executor&& e) noexcept;
  184. /// Assignment operator that sets the polymorphic wrapper to the empty state.
  185. BOOST_ASIO_DECL any_io_executor& operator=(nullptr_t);
  186. /// Destructor.
  187. BOOST_ASIO_DECL ~any_io_executor();
  188. /// Swap targets with another polymorphic wrapper.
  189. BOOST_ASIO_DECL void swap(any_io_executor& other) noexcept;
  190. /// Obtain a polymorphic wrapper with the specified property.
  191. /**
  192. * Do not call this function directly. It is intended for use with the
  193. * boost::asio::require and boost::asio::prefer customisation points.
  194. *
  195. * For example:
  196. * @code any_io_executor ex = ...;
  197. * auto ex2 = boost::asio::require(ex, execution::blocking.possibly); @endcode
  198. */
  199. template <typename Property>
  200. any_io_executor require(const Property& p,
  201. constraint_t<
  202. traits::require_member<const base_type&, const Property&>::is_valid
  203. > = 0) const
  204. {
  205. return static_cast<const base_type&>(*this).require(p);
  206. }
  207. /// Obtain a polymorphic wrapper with the specified property.
  208. /**
  209. * Do not call this function directly. It is intended for use with the
  210. * boost::asio::prefer customisation point.
  211. *
  212. * For example:
  213. * @code any_io_executor ex = ...;
  214. * auto ex2 = boost::asio::prefer(ex, execution::blocking.possibly); @endcode
  215. */
  216. template <typename Property>
  217. any_io_executor prefer(const Property& p,
  218. constraint_t<
  219. traits::prefer_member<const base_type&, const Property&>::is_valid
  220. > = 0) const
  221. {
  222. return static_cast<const base_type&>(*this).prefer(p);
  223. }
  224. };
  225. #if !defined(GENERATING_DOCUMENTATION)
  226. template <>
  227. BOOST_ASIO_DECL any_io_executor any_io_executor::require(
  228. const execution::blocking_t::never_t&, int) const;
  229. template <>
  230. BOOST_ASIO_DECL any_io_executor any_io_executor::prefer(
  231. const execution::blocking_t::possibly_t&, int) const;
  232. template <>
  233. BOOST_ASIO_DECL any_io_executor any_io_executor::prefer(
  234. const execution::outstanding_work_t::tracked_t&, int) const;
  235. template <>
  236. BOOST_ASIO_DECL any_io_executor any_io_executor::prefer(
  237. const execution::outstanding_work_t::untracked_t&, int) const;
  238. template <>
  239. BOOST_ASIO_DECL any_io_executor any_io_executor::prefer(
  240. const execution::relationship_t::fork_t&, int) const;
  241. template <>
  242. BOOST_ASIO_DECL any_io_executor any_io_executor::prefer(
  243. const execution::relationship_t::continuation_t&, int) const;
  244. namespace traits {
  245. #if !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  246. template <>
  247. struct equality_comparable<any_io_executor>
  248. {
  249. static const bool is_valid = true;
  250. static const bool is_noexcept = true;
  251. };
  252. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  253. #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  254. template <typename F>
  255. struct execute_member<any_io_executor, F>
  256. {
  257. static const bool is_valid = true;
  258. static const bool is_noexcept = false;
  259. typedef void result_type;
  260. };
  261. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  262. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  263. template <typename Prop>
  264. struct query_member<any_io_executor, Prop> :
  265. query_member<any_io_executor::base_type, Prop>
  266. {
  267. };
  268. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  269. #if !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  270. template <typename Prop>
  271. struct require_member<any_io_executor, Prop> :
  272. require_member<any_io_executor::base_type, Prop>
  273. {
  274. typedef any_io_executor result_type;
  275. };
  276. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  277. #if !defined(BOOST_ASIO_HAS_DEDUCED_PREFER_MEMBER_TRAIT)
  278. template <typename Prop>
  279. struct prefer_member<any_io_executor, Prop> :
  280. prefer_member<any_io_executor::base_type, Prop>
  281. {
  282. typedef any_io_executor result_type;
  283. };
  284. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_PREFER_MEMBER_TRAIT)
  285. } // namespace traits
  286. #endif // !defined(GENERATING_DOCUMENTATION)
  287. #endif // defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  288. } // namespace asio
  289. } // namespace boost
  290. #include <boost/asio/detail/pop_options.hpp>
  291. #if defined(BOOST_ASIO_HEADER_ONLY) \
  292. && !defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  293. # include <boost/asio/impl/any_io_executor.ipp>
  294. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  295. // && !defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  296. #endif // BOOST_ASIO_ANY_IO_EXECUTOR_HPP