connect.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. //
  2. // execution/connect.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 BOOST_ASIO_EXECUTION_CONNECT_HPP
  11. #define BOOST_ASIO_EXECUTION_CONNECT_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_NO_DEPRECATED)
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/execution/detail/as_invocable.hpp>
  19. #include <boost/asio/execution/detail/as_operation.hpp>
  20. #include <boost/asio/execution/detail/as_receiver.hpp>
  21. #include <boost/asio/execution/executor.hpp>
  22. #include <boost/asio/execution/operation_state.hpp>
  23. #include <boost/asio/execution/receiver.hpp>
  24. #include <boost/asio/execution/sender.hpp>
  25. #include <boost/asio/traits/connect_member.hpp>
  26. #include <boost/asio/traits/connect_free.hpp>
  27. #include <boost/asio/detail/push_options.hpp>
  28. #if defined(GENERATING_DOCUMENTATION)
  29. namespace boost {
  30. namespace asio {
  31. namespace execution {
  32. /// A customisation point that connects a sender to a receiver.
  33. /**
  34. * The name <tt>execution::connect</tt> denotes a customisation point object.
  35. * For some subexpressions <tt>s</tt> and <tt>r</tt>, let <tt>S</tt> be a type
  36. * such that <tt>decltype((s))</tt> is <tt>S</tt> and let <tt>R</tt> be a type
  37. * such that <tt>decltype((r))</tt> is <tt>R</tt>. The expression
  38. * <tt>execution::connect(s, r)</tt> is expression-equivalent to:
  39. *
  40. * @li <tt>s.connect(r)</tt>, if that expression is valid, if its type
  41. * satisfies <tt>operation_state</tt>, and if <tt>S</tt> satisfies
  42. * <tt>sender</tt>.
  43. *
  44. * @li Otherwise, <tt>connect(s, r)</tt>, if that expression is valid, if its
  45. * type satisfies <tt>operation_state</tt>, and if <tt>S</tt> satisfies
  46. * <tt>sender</tt>, with overload resolution performed in a context that
  47. * includes the declaration <tt>void connect();</tt> and that does not include
  48. * a declaration of <tt>execution::connect</tt>.
  49. *
  50. * @li Otherwise, <tt>as_operation{s, r}</tt>, if <tt>r</tt> is not an instance
  51. * of <tt>as_receiver<F, S></tt> for some type <tt>F</tt>, and if
  52. * <tt>receiver_of<R> && executor_of<remove_cvref_t<S>,
  53. * as_invocable<remove_cvref_t<R>, S>></tt> is <tt>true</tt>, where
  54. * <tt>as_operation</tt> is an implementation-defined class equivalent to
  55. * @code template <class S, class R>
  56. * struct as_operation
  57. * {
  58. * remove_cvref_t<S> e_;
  59. * remove_cvref_t<R> r_;
  60. * void start() noexcept try {
  61. * execution::execute(std::move(e_),
  62. * as_invocable<remove_cvref_t<R>, S>{r_});
  63. * } catch(...) {
  64. * execution::set_error(std::move(r_), current_exception());
  65. * }
  66. * }; @endcode
  67. * and <tt>as_invocable</tt> is a class template equivalent to the following:
  68. * @code template<class R>
  69. * struct as_invocable
  70. * {
  71. * R* r_;
  72. * explicit as_invocable(R& r) noexcept
  73. * : r_(std::addressof(r)) {}
  74. * as_invocable(as_invocable && other) noexcept
  75. * : r_(std::exchange(other.r_, nullptr)) {}
  76. * ~as_invocable() {
  77. * if(r_)
  78. * execution::set_done(std::move(*r_));
  79. * }
  80. * void operator()() & noexcept try {
  81. * execution::set_value(std::move(*r_));
  82. * r_ = nullptr;
  83. * } catch(...) {
  84. * execution::set_error(std::move(*r_), current_exception());
  85. * r_ = nullptr;
  86. * }
  87. * };
  88. * @endcode
  89. *
  90. * @li Otherwise, <tt>execution::connect(s, r)</tt> is ill-formed.
  91. */
  92. inline constexpr unspecified connect = unspecified;
  93. /// A type trait that determines whether a @c connect expression is
  94. /// well-formed.
  95. /**
  96. * Class template @c can_connect is a trait that is derived from
  97. * @c true_type if the expression <tt>execution::connect(std::declval<S>(),
  98. * std::declval<R>())</tt> is well formed; otherwise @c false_type.
  99. */
  100. template <typename S, typename R>
  101. struct can_connect :
  102. integral_constant<bool, automatically_determined>
  103. {
  104. };
  105. /// A type trait to determine the result of a @c connect expression.
  106. template <typename S, typename R>
  107. struct connect_result
  108. {
  109. /// The type of the connect expression.
  110. /**
  111. * The type of the expression <tt>execution::connect(std::declval<S>(),
  112. * std::declval<R>())</tt>.
  113. */
  114. typedef automatically_determined type;
  115. };
  116. /// A type alis to determine the result of a @c connect expression.
  117. template <typename S, typename R>
  118. using connect_result_t = typename connect_result<S, R>::type;
  119. } // namespace execution
  120. } // namespace asio
  121. } // namespace boost
  122. #else // defined(GENERATING_DOCUMENTATION)
  123. namespace boost_asio_execution_connect_fn {
  124. using boost::asio::conditional;
  125. using boost::asio::declval;
  126. using boost::asio::enable_if;
  127. using boost::asio::execution::detail::as_invocable;
  128. using boost::asio::execution::detail::as_operation;
  129. using boost::asio::execution::detail::is_as_receiver;
  130. using boost::asio::execution::is_executor_of;
  131. using boost::asio::execution::is_operation_state;
  132. using boost::asio::execution::is_receiver;
  133. using boost::asio::execution::is_sender;
  134. using boost::asio::false_type;
  135. using boost::asio::remove_cvref;
  136. using boost::asio::traits::connect_free;
  137. using boost::asio::traits::connect_member;
  138. void connect();
  139. enum overload_type
  140. {
  141. call_member,
  142. call_free,
  143. adapter,
  144. ill_formed
  145. };
  146. template <typename S, typename R, typename = void,
  147. typename = void, typename = void, typename = void>
  148. struct call_traits
  149. {
  150. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = ill_formed);
  151. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
  152. typedef void result_type;
  153. };
  154. template <typename S, typename R>
  155. struct call_traits<S, void(R),
  156. typename enable_if<
  157. connect_member<S, R>::is_valid
  158. >::type,
  159. typename enable_if<
  160. is_operation_state<typename connect_member<S, R>::result_type>::value
  161. >::type,
  162. typename enable_if<
  163. is_sender<typename remove_cvref<S>::type>::value
  164. >::type> :
  165. connect_member<S, R>
  166. {
  167. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = call_member);
  168. };
  169. template <typename S, typename R>
  170. struct call_traits<S, void(R),
  171. typename enable_if<
  172. !connect_member<S, R>::is_valid
  173. >::type,
  174. typename enable_if<
  175. connect_free<S, R>::is_valid
  176. >::type,
  177. typename enable_if<
  178. is_operation_state<typename connect_free<S, R>::result_type>::value
  179. >::type,
  180. typename enable_if<
  181. is_sender<typename remove_cvref<S>::type>::value
  182. >::type> :
  183. connect_free<S, R>
  184. {
  185. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = call_free);
  186. };
  187. template <typename S, typename R>
  188. struct call_traits<S, void(R),
  189. typename enable_if<
  190. !connect_member<S, R>::is_valid
  191. >::type,
  192. typename enable_if<
  193. !connect_free<S, R>::is_valid
  194. >::type,
  195. typename enable_if<
  196. is_receiver<R>::value
  197. >::type,
  198. typename enable_if<
  199. conditional<
  200. !is_as_receiver<
  201. typename remove_cvref<R>::type
  202. >::value,
  203. is_executor_of<
  204. typename remove_cvref<S>::type,
  205. as_invocable<typename remove_cvref<R>::type, S>
  206. >,
  207. false_type
  208. >::type::value
  209. >::type>
  210. {
  211. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = adapter);
  212. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
  213. typedef as_operation<S, R> result_type;
  214. };
  215. struct impl
  216. {
  217. #if defined(BOOST_ASIO_HAS_MOVE)
  218. template <typename S, typename R>
  219. BOOST_ASIO_CONSTEXPR typename enable_if<
  220. call_traits<S, void(R)>::overload == call_member,
  221. typename call_traits<S, void(R)>::result_type
  222. >::type
  223. operator()(S&& s, R&& r) const
  224. BOOST_ASIO_NOEXCEPT_IF((
  225. call_traits<S, void(R)>::is_noexcept))
  226. {
  227. return BOOST_ASIO_MOVE_CAST(S)(s).connect(BOOST_ASIO_MOVE_CAST(R)(r));
  228. }
  229. template <typename S, typename R>
  230. BOOST_ASIO_CONSTEXPR typename enable_if<
  231. call_traits<S, void(R)>::overload == call_free,
  232. typename call_traits<S, void(R)>::result_type
  233. >::type
  234. operator()(S&& s, R&& r) const
  235. BOOST_ASIO_NOEXCEPT_IF((
  236. call_traits<S, void(R)>::is_noexcept))
  237. {
  238. return connect(BOOST_ASIO_MOVE_CAST(S)(s), BOOST_ASIO_MOVE_CAST(R)(r));
  239. }
  240. template <typename S, typename R>
  241. BOOST_ASIO_CONSTEXPR typename enable_if<
  242. call_traits<S, void(R)>::overload == adapter,
  243. typename call_traits<S, void(R)>::result_type
  244. >::type
  245. operator()(S&& s, R&& r) const
  246. BOOST_ASIO_NOEXCEPT_IF((
  247. call_traits<S, void(R)>::is_noexcept))
  248. {
  249. return typename call_traits<S, void(R)>::result_type(
  250. BOOST_ASIO_MOVE_CAST(S)(s), BOOST_ASIO_MOVE_CAST(R)(r));
  251. }
  252. #else // defined(BOOST_ASIO_HAS_MOVE)
  253. template <typename S, typename R>
  254. BOOST_ASIO_CONSTEXPR typename enable_if<
  255. call_traits<S&, void(R&)>::overload == call_member,
  256. typename call_traits<S&, void(R&)>::result_type
  257. >::type
  258. operator()(S& s, R& r) const
  259. BOOST_ASIO_NOEXCEPT_IF((
  260. call_traits<S&, void(R&)>::is_noexcept))
  261. {
  262. return s.connect(r);
  263. }
  264. template <typename S, typename R>
  265. BOOST_ASIO_CONSTEXPR typename enable_if<
  266. call_traits<const S&, void(R&)>::overload == call_member,
  267. typename call_traits<const S&, void(R&)>::result_type
  268. >::type
  269. operator()(const S& s, R& r) const
  270. BOOST_ASIO_NOEXCEPT_IF((
  271. call_traits<const S&, void(R&)>::is_noexcept))
  272. {
  273. return s.connect(r);
  274. }
  275. template <typename S, typename R>
  276. BOOST_ASIO_CONSTEXPR typename enable_if<
  277. call_traits<S&, void(R&)>::overload == call_free,
  278. typename call_traits<S&, void(R&)>::result_type
  279. >::type
  280. operator()(S& s, R& r) const
  281. BOOST_ASIO_NOEXCEPT_IF((
  282. call_traits<S&, void(R&)>::is_noexcept))
  283. {
  284. return connect(s, r);
  285. }
  286. template <typename S, typename R>
  287. BOOST_ASIO_CONSTEXPR typename enable_if<
  288. call_traits<const S&, void(R&)>::overload == call_free,
  289. typename call_traits<const S&, void(R&)>::result_type
  290. >::type
  291. operator()(const S& s, R& r) const
  292. BOOST_ASIO_NOEXCEPT_IF((
  293. call_traits<const S&, void(R&)>::is_noexcept))
  294. {
  295. return connect(s, r);
  296. }
  297. template <typename S, typename R>
  298. BOOST_ASIO_CONSTEXPR typename enable_if<
  299. call_traits<S&, void(R&)>::overload == adapter,
  300. typename call_traits<S&, void(R&)>::result_type
  301. >::type
  302. operator()(S& s, R& r) const
  303. BOOST_ASIO_NOEXCEPT_IF((
  304. call_traits<S&, void(R&)>::is_noexcept))
  305. {
  306. return typename call_traits<S&, void(R&)>::result_type(s, r);
  307. }
  308. template <typename S, typename R>
  309. BOOST_ASIO_CONSTEXPR typename enable_if<
  310. call_traits<const S&, void(R&)>::overload == adapter,
  311. typename call_traits<const S&, void(R&)>::result_type
  312. >::type
  313. operator()(const S& s, R& r) const
  314. BOOST_ASIO_NOEXCEPT_IF((
  315. call_traits<const S&, void(R&)>::is_noexcept))
  316. {
  317. return typename call_traits<const S&, void(R&)>::result_type(s, r);
  318. }
  319. template <typename S, typename R>
  320. BOOST_ASIO_CONSTEXPR typename enable_if<
  321. call_traits<S&, void(const R&)>::overload == call_member,
  322. typename call_traits<S&, void(const R&)>::result_type
  323. >::type
  324. operator()(S& s, const R& r) const
  325. BOOST_ASIO_NOEXCEPT_IF((
  326. call_traits<S&, void(const R&)>::is_noexcept))
  327. {
  328. return s.connect(r);
  329. }
  330. template <typename S, typename R>
  331. BOOST_ASIO_CONSTEXPR typename enable_if<
  332. call_traits<const S&, void(const R&)>::overload == call_member,
  333. typename call_traits<const S&, void(const R&)>::result_type
  334. >::type
  335. operator()(const S& s, const R& r) const
  336. BOOST_ASIO_NOEXCEPT_IF((
  337. call_traits<const S&, void(const R&)>::is_noexcept))
  338. {
  339. return s.connect(r);
  340. }
  341. template <typename S, typename R>
  342. BOOST_ASIO_CONSTEXPR typename enable_if<
  343. call_traits<S&, void(const R&)>::overload == call_free,
  344. typename call_traits<S&, void(const R&)>::result_type
  345. >::type
  346. operator()(S& s, const R& r) const
  347. BOOST_ASIO_NOEXCEPT_IF((
  348. call_traits<S&, void(const R&)>::is_noexcept))
  349. {
  350. return connect(s, r);
  351. }
  352. template <typename S, typename R>
  353. BOOST_ASIO_CONSTEXPR typename enable_if<
  354. call_traits<const S&, void(const R&)>::overload == call_free,
  355. typename call_traits<const S&, void(const R&)>::result_type
  356. >::type
  357. operator()(const S& s, const R& r) const
  358. BOOST_ASIO_NOEXCEPT_IF((
  359. call_traits<const S&, void(const R&)>::is_noexcept))
  360. {
  361. return connect(s, r);
  362. }
  363. template <typename S, typename R>
  364. BOOST_ASIO_CONSTEXPR typename enable_if<
  365. call_traits<S&, void(const R&)>::overload == adapter,
  366. typename call_traits<S&, void(const R&)>::result_type
  367. >::type
  368. operator()(S& s, const R& r) const
  369. BOOST_ASIO_NOEXCEPT_IF((
  370. call_traits<S&, void(const R&)>::is_noexcept))
  371. {
  372. return typename call_traits<S&, void(const R&)>::result_type(s, r);
  373. }
  374. template <typename S, typename R>
  375. BOOST_ASIO_CONSTEXPR typename enable_if<
  376. call_traits<const S&, void(const R&)>::overload == adapter,
  377. typename call_traits<const S&, void(const R&)>::result_type
  378. >::type
  379. operator()(const S& s, const R& r) const
  380. BOOST_ASIO_NOEXCEPT_IF((
  381. call_traits<const S&, void(const R&)>::is_noexcept))
  382. {
  383. return typename call_traits<const S&, void(const R&)>::result_type(s, r);
  384. }
  385. #endif // defined(BOOST_ASIO_HAS_MOVE)
  386. };
  387. template <typename T = impl>
  388. struct static_instance
  389. {
  390. static const T instance;
  391. };
  392. template <typename T>
  393. const T static_instance<T>::instance = {};
  394. } // namespace boost_asio_execution_connect_fn
  395. namespace boost {
  396. namespace asio {
  397. namespace execution {
  398. namespace {
  399. static BOOST_ASIO_CONSTEXPR const boost_asio_execution_connect_fn::impl&
  400. connect = boost_asio_execution_connect_fn::static_instance<>::instance;
  401. } // namespace
  402. template <typename S, typename R>
  403. struct can_connect :
  404. integral_constant<bool,
  405. boost_asio_execution_connect_fn::call_traits<S, void(R)>::overload !=
  406. boost_asio_execution_connect_fn::ill_formed>
  407. {
  408. };
  409. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  410. template <typename S, typename R>
  411. constexpr bool can_connect_v = can_connect<S, R>::value;
  412. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  413. template <typename S, typename R>
  414. struct is_nothrow_connect :
  415. integral_constant<bool,
  416. boost_asio_execution_connect_fn::call_traits<S, void(R)>::is_noexcept>
  417. {
  418. };
  419. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  420. template <typename S, typename R>
  421. constexpr bool is_nothrow_connect_v
  422. = is_nothrow_connect<S, R>::value;
  423. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  424. template <typename S, typename R>
  425. struct connect_result
  426. {
  427. typedef typename boost_asio_execution_connect_fn::call_traits<
  428. S, void(R)>::result_type type;
  429. };
  430. #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  431. template <typename S, typename R>
  432. using connect_result_t = typename connect_result<S, R>::type;
  433. #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  434. } // namespace execution
  435. } // namespace asio
  436. } // namespace boost
  437. #endif // defined(GENERATING_DOCUMENTATION)
  438. #include <boost/asio/detail/pop_options.hpp>
  439. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  440. #endif // BOOST_ASIO_EXECUTION_CONNECT_HPP