awaitable_operators.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. //
  2. // experimental/awaitable_operators.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_EXPERIMENTAL_AWAITABLE_OPERATORS_HPP
  11. #define BOOST_ASIO_EXPERIMENTAL_AWAITABLE_OPERATORS_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 <optional>
  17. #include <stdexcept>
  18. #include <tuple>
  19. #include <variant>
  20. #include <boost/asio/awaitable.hpp>
  21. #include <boost/asio/co_spawn.hpp>
  22. #include <boost/asio/detail/type_traits.hpp>
  23. #include <boost/asio/experimental/deferred.hpp>
  24. #include <boost/asio/experimental/parallel_group.hpp>
  25. #include <boost/asio/multiple_exceptions.hpp>
  26. #include <boost/asio/this_coro.hpp>
  27. #include <boost/asio/detail/push_options.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace experimental {
  31. namespace awaitable_operators {
  32. namespace detail {
  33. template <typename T, typename Executor>
  34. awaitable<T, Executor> awaitable_wrap(awaitable<T, Executor> a,
  35. constraint_t<is_constructible<T>::value>* = 0)
  36. {
  37. return a;
  38. }
  39. template <typename T, typename Executor>
  40. awaitable<std::optional<T>, Executor> awaitable_wrap(awaitable<T, Executor> a,
  41. constraint_t<!is_constructible<T>::value>* = 0)
  42. {
  43. co_return std::optional<T>(co_await std::move(a));
  44. }
  45. template <typename T>
  46. T& awaitable_unwrap(conditional_t<true, T, void>& r,
  47. constraint_t<is_constructible<T>::value>* = 0)
  48. {
  49. return r;
  50. }
  51. template <typename T>
  52. T& awaitable_unwrap(std::optional<conditional_t<true, T, void>>& r,
  53. constraint_t<!is_constructible<T>::value>* = 0)
  54. {
  55. return *r;
  56. }
  57. } // namespace detail
  58. /// Wait for both operations to succeed.
  59. /**
  60. * If one operations fails, the other is cancelled as the AND-condition can no
  61. * longer be satisfied.
  62. */
  63. template <typename Executor>
  64. awaitable<void, Executor> operator&&(
  65. awaitable<void, Executor> t, awaitable<void, Executor> u)
  66. {
  67. auto ex = co_await this_coro::executor;
  68. auto [order, ex0, ex1] =
  69. co_await make_parallel_group(
  70. co_spawn(ex, std::move(t), deferred),
  71. co_spawn(ex, std::move(u), deferred)
  72. ).async_wait(
  73. wait_for_one_error(),
  74. deferred
  75. );
  76. if (ex0 && ex1)
  77. throw multiple_exceptions(ex0);
  78. if (ex0)
  79. std::rethrow_exception(ex0);
  80. if (ex1)
  81. std::rethrow_exception(ex1);
  82. co_return;
  83. }
  84. /// Wait for both operations to succeed.
  85. /**
  86. * If one operations fails, the other is cancelled as the AND-condition can no
  87. * longer be satisfied.
  88. */
  89. template <typename U, typename Executor>
  90. awaitable<U, Executor> operator&&(
  91. awaitable<void, Executor> t, awaitable<U, Executor> u)
  92. {
  93. auto ex = co_await this_coro::executor;
  94. auto [order, ex0, ex1, r1] =
  95. co_await make_parallel_group(
  96. co_spawn(ex, std::move(t), deferred),
  97. co_spawn(ex, detail::awaitable_wrap(std::move(u)), deferred)
  98. ).async_wait(
  99. wait_for_one_error(),
  100. deferred
  101. );
  102. if (ex0 && ex1)
  103. throw multiple_exceptions(ex0);
  104. if (ex0)
  105. std::rethrow_exception(ex0);
  106. if (ex1)
  107. std::rethrow_exception(ex1);
  108. co_return std::move(detail::awaitable_unwrap<U>(r1));
  109. }
  110. /// Wait for both operations to succeed.
  111. /**
  112. * If one operations fails, the other is cancelled as the AND-condition can no
  113. * longer be satisfied.
  114. */
  115. template <typename T, typename Executor>
  116. awaitable<T, Executor> operator&&(
  117. awaitable<T, Executor> t, awaitable<void, Executor> u)
  118. {
  119. auto ex = co_await this_coro::executor;
  120. auto [order, ex0, r0, ex1] =
  121. co_await make_parallel_group(
  122. co_spawn(ex, detail::awaitable_wrap(std::move(t)), deferred),
  123. co_spawn(ex, std::move(u), deferred)
  124. ).async_wait(
  125. wait_for_one_error(),
  126. deferred
  127. );
  128. if (ex0 && ex1)
  129. throw multiple_exceptions(ex0);
  130. if (ex0)
  131. std::rethrow_exception(ex0);
  132. if (ex1)
  133. std::rethrow_exception(ex1);
  134. co_return std::move(detail::awaitable_unwrap<T>(r0));
  135. }
  136. /// Wait for both operations to succeed.
  137. /**
  138. * If one operations fails, the other is cancelled as the AND-condition can no
  139. * longer be satisfied.
  140. */
  141. template <typename T, typename U, typename Executor>
  142. awaitable<std::tuple<T, U>, Executor> operator&&(
  143. awaitable<T, Executor> t, awaitable<U, Executor> u)
  144. {
  145. auto ex = co_await this_coro::executor;
  146. auto [order, ex0, r0, ex1, r1] =
  147. co_await make_parallel_group(
  148. co_spawn(ex, detail::awaitable_wrap(std::move(t)), deferred),
  149. co_spawn(ex, detail::awaitable_wrap(std::move(u)), deferred)
  150. ).async_wait(
  151. wait_for_one_error(),
  152. deferred
  153. );
  154. if (ex0 && ex1)
  155. throw multiple_exceptions(ex0);
  156. if (ex0)
  157. std::rethrow_exception(ex0);
  158. if (ex1)
  159. std::rethrow_exception(ex1);
  160. co_return std::make_tuple(
  161. std::move(detail::awaitable_unwrap<T>(r0)),
  162. std::move(detail::awaitable_unwrap<U>(r1)));
  163. }
  164. /// Wait for both operations to succeed.
  165. /**
  166. * If one operations fails, the other is cancelled as the AND-condition can no
  167. * longer be satisfied.
  168. */
  169. template <typename... T, typename Executor>
  170. awaitable<std::tuple<T..., std::monostate>, Executor> operator&&(
  171. awaitable<std::tuple<T...>, Executor> t, awaitable<void, Executor> u)
  172. {
  173. auto ex = co_await this_coro::executor;
  174. auto [order, ex0, r0, ex1, r1] =
  175. co_await make_parallel_group(
  176. co_spawn(ex, detail::awaitable_wrap(std::move(t)), deferred),
  177. co_spawn(ex, std::move(u), deferred)
  178. ).async_wait(
  179. wait_for_one_error(),
  180. deferred
  181. );
  182. if (ex0 && ex1)
  183. throw multiple_exceptions(ex0);
  184. if (ex0)
  185. std::rethrow_exception(ex0);
  186. if (ex1)
  187. std::rethrow_exception(ex1);
  188. co_return std::move(detail::awaitable_unwrap<std::tuple<T...>>(r0));
  189. }
  190. /// Wait for both operations to succeed.
  191. /**
  192. * If one operations fails, the other is cancelled as the AND-condition can no
  193. * longer be satisfied.
  194. */
  195. template <typename... T, typename U, typename Executor>
  196. awaitable<std::tuple<T..., U>, Executor> operator&&(
  197. awaitable<std::tuple<T...>, Executor> t, awaitable<U, Executor> u)
  198. {
  199. auto ex = co_await this_coro::executor;
  200. auto [order, ex0, r0, ex1, r1] =
  201. co_await make_parallel_group(
  202. co_spawn(ex, detail::awaitable_wrap(std::move(t)), deferred),
  203. co_spawn(ex, detail::awaitable_wrap(std::move(u)), deferred)
  204. ).async_wait(
  205. wait_for_one_error(),
  206. deferred
  207. );
  208. if (ex0 && ex1)
  209. throw multiple_exceptions(ex0);
  210. if (ex0)
  211. std::rethrow_exception(ex0);
  212. if (ex1)
  213. std::rethrow_exception(ex1);
  214. co_return std::tuple_cat(
  215. std::move(detail::awaitable_unwrap<std::tuple<T...>>(r0)),
  216. std::make_tuple(std::move(detail::awaitable_unwrap<U>(r1))));
  217. }
  218. /// Wait for one operation to succeed.
  219. /**
  220. * If one operations succeeds, the other is cancelled as the OR-condition is
  221. * already satisfied.
  222. */
  223. template <typename Executor>
  224. awaitable<std::variant<std::monostate, std::monostate>, Executor> operator||(
  225. awaitable<void, Executor> t, awaitable<void, Executor> u)
  226. {
  227. auto ex = co_await this_coro::executor;
  228. auto [order, ex0, ex1] =
  229. co_await make_parallel_group(
  230. co_spawn(ex, std::move(t), deferred),
  231. co_spawn(ex, std::move(u), deferred)
  232. ).async_wait(
  233. wait_for_one_success(),
  234. deferred
  235. );
  236. if (order[0] == 0)
  237. {
  238. if (!ex0)
  239. co_return std::variant<std::monostate, std::monostate>{
  240. std::in_place_index<0>};
  241. if (!ex1)
  242. co_return std::variant<std::monostate, std::monostate>{
  243. std::in_place_index<1>};
  244. throw multiple_exceptions(ex0);
  245. }
  246. else
  247. {
  248. if (!ex1)
  249. co_return std::variant<std::monostate, std::monostate>{
  250. std::in_place_index<1>};
  251. if (!ex0)
  252. co_return std::variant<std::monostate, std::monostate>{
  253. std::in_place_index<0>};
  254. throw multiple_exceptions(ex1);
  255. }
  256. }
  257. /// Wait for one operation to succeed.
  258. /**
  259. * If one operations succeeds, the other is cancelled as the OR-condition is
  260. * already satisfied.
  261. */
  262. template <typename U, typename Executor>
  263. awaitable<std::variant<std::monostate, U>, Executor> operator||(
  264. awaitable<void, Executor> t, awaitable<U, Executor> u)
  265. {
  266. auto ex = co_await this_coro::executor;
  267. auto [order, ex0, ex1, r1] =
  268. co_await make_parallel_group(
  269. co_spawn(ex, std::move(t), deferred),
  270. co_spawn(ex, detail::awaitable_wrap(std::move(u)), deferred)
  271. ).async_wait(
  272. wait_for_one_success(),
  273. deferred
  274. );
  275. if (order[0] == 0)
  276. {
  277. if (!ex0)
  278. co_return std::variant<std::monostate, U>{
  279. std::in_place_index<0>};
  280. if (!ex1)
  281. co_return std::variant<std::monostate, U>{
  282. std::in_place_index<1>,
  283. std::move(detail::awaitable_unwrap<U>(r1))};
  284. throw multiple_exceptions(ex0);
  285. }
  286. else
  287. {
  288. if (!ex1)
  289. co_return std::variant<std::monostate, U>{
  290. std::in_place_index<1>,
  291. std::move(detail::awaitable_unwrap<U>(r1))};
  292. if (!ex0)
  293. co_return std::variant<std::monostate, U>{
  294. std::in_place_index<0>};
  295. throw multiple_exceptions(ex1);
  296. }
  297. }
  298. /// Wait for one operation to succeed.
  299. /**
  300. * If one operations succeeds, the other is cancelled as the OR-condition is
  301. * already satisfied.
  302. */
  303. template <typename T, typename Executor>
  304. awaitable<std::variant<T, std::monostate>, Executor> operator||(
  305. awaitable<T, Executor> t, awaitable<void, Executor> u)
  306. {
  307. auto ex = co_await this_coro::executor;
  308. auto [order, ex0, r0, ex1] =
  309. co_await make_parallel_group(
  310. co_spawn(ex, detail::awaitable_wrap(std::move(t)), deferred),
  311. co_spawn(ex, std::move(u), deferred)
  312. ).async_wait(
  313. wait_for_one_success(),
  314. deferred
  315. );
  316. if (order[0] == 0)
  317. {
  318. if (!ex0)
  319. co_return std::variant<T, std::monostate>{
  320. std::in_place_index<0>,
  321. std::move(detail::awaitable_unwrap<T>(r0))};
  322. if (!ex1)
  323. co_return std::variant<T, std::monostate>{
  324. std::in_place_index<1>};
  325. throw multiple_exceptions(ex0);
  326. }
  327. else
  328. {
  329. if (!ex1)
  330. co_return std::variant<T, std::monostate>{
  331. std::in_place_index<1>};
  332. if (!ex0)
  333. co_return std::variant<T, std::monostate>{
  334. std::in_place_index<0>,
  335. std::move(detail::awaitable_unwrap<T>(r0))};
  336. throw multiple_exceptions(ex1);
  337. }
  338. }
  339. /// Wait for one operation to succeed.
  340. /**
  341. * If one operations succeeds, the other is cancelled as the OR-condition is
  342. * already satisfied.
  343. */
  344. template <typename T, typename U, typename Executor>
  345. awaitable<std::variant<T, U>, Executor> operator||(
  346. awaitable<T, Executor> t, awaitable<U, Executor> u)
  347. {
  348. auto ex = co_await this_coro::executor;
  349. auto [order, ex0, r0, ex1, r1] =
  350. co_await make_parallel_group(
  351. co_spawn(ex, detail::awaitable_wrap(std::move(t)), deferred),
  352. co_spawn(ex, detail::awaitable_wrap(std::move(u)), deferred)
  353. ).async_wait(
  354. wait_for_one_success(),
  355. deferred
  356. );
  357. if (order[0] == 0)
  358. {
  359. if (!ex0)
  360. co_return std::variant<T, U>{
  361. std::in_place_index<0>,
  362. std::move(detail::awaitable_unwrap<T>(r0))};
  363. if (!ex1)
  364. co_return std::variant<T, U>{
  365. std::in_place_index<1>,
  366. std::move(detail::awaitable_unwrap<U>(r1))};
  367. throw multiple_exceptions(ex0);
  368. }
  369. else
  370. {
  371. if (!ex1)
  372. co_return std::variant<T, U>{
  373. std::in_place_index<1>,
  374. std::move(detail::awaitable_unwrap<U>(r1))};
  375. if (!ex0)
  376. co_return std::variant<T, U>{
  377. std::in_place_index<0>,
  378. std::move(detail::awaitable_unwrap<T>(r0))};
  379. throw multiple_exceptions(ex1);
  380. }
  381. }
  382. namespace detail {
  383. template <typename... T>
  384. struct widen_variant
  385. {
  386. template <std::size_t I, typename SourceVariant>
  387. static std::variant<T...> call(SourceVariant& source)
  388. {
  389. if (source.index() == I)
  390. return std::variant<T...>{
  391. std::in_place_index<I>, std::move(std::get<I>(source))};
  392. else if constexpr (I + 1 < std::variant_size_v<SourceVariant>)
  393. return call<I + 1>(source);
  394. else
  395. throw std::logic_error("empty variant");
  396. }
  397. };
  398. } // namespace detail
  399. /// Wait for one operation to succeed.
  400. /**
  401. * If one operations succeeds, the other is cancelled as the OR-condition is
  402. * already satisfied.
  403. */
  404. template <typename... T, typename Executor>
  405. awaitable<std::variant<T..., std::monostate>, Executor> operator||(
  406. awaitable<std::variant<T...>, Executor> t, awaitable<void, Executor> u)
  407. {
  408. auto ex = co_await this_coro::executor;
  409. auto [order, ex0, r0, ex1] =
  410. co_await make_parallel_group(
  411. co_spawn(ex, detail::awaitable_wrap(std::move(t)), deferred),
  412. co_spawn(ex, std::move(u), deferred)
  413. ).async_wait(
  414. wait_for_one_success(),
  415. deferred
  416. );
  417. using widen = detail::widen_variant<T..., std::monostate>;
  418. if (order[0] == 0)
  419. {
  420. if (!ex0)
  421. co_return widen::template call<0>(
  422. detail::awaitable_unwrap<std::variant<T...>>(r0));
  423. if (!ex1)
  424. co_return std::variant<T..., std::monostate>{
  425. std::in_place_index<sizeof...(T)>};
  426. throw multiple_exceptions(ex0);
  427. }
  428. else
  429. {
  430. if (!ex1)
  431. co_return std::variant<T..., std::monostate>{
  432. std::in_place_index<sizeof...(T)>};
  433. if (!ex0)
  434. co_return widen::template call<0>(
  435. detail::awaitable_unwrap<std::variant<T...>>(r0));
  436. throw multiple_exceptions(ex1);
  437. }
  438. }
  439. /// Wait for one operation to succeed.
  440. /**
  441. * If one operations succeeds, the other is cancelled as the OR-condition is
  442. * already satisfied.
  443. */
  444. template <typename... T, typename U, typename Executor>
  445. awaitable<std::variant<T..., U>, Executor> operator||(
  446. awaitable<std::variant<T...>, Executor> t, awaitable<U, Executor> u)
  447. {
  448. auto ex = co_await this_coro::executor;
  449. auto [order, ex0, r0, ex1, r1] =
  450. co_await make_parallel_group(
  451. co_spawn(ex, detail::awaitable_wrap(std::move(t)), deferred),
  452. co_spawn(ex, detail::awaitable_wrap(std::move(u)), deferred)
  453. ).async_wait(
  454. wait_for_one_success(),
  455. deferred
  456. );
  457. using widen = detail::widen_variant<T..., U>;
  458. if (order[0] == 0)
  459. {
  460. if (!ex0)
  461. co_return widen::template call<0>(
  462. detail::awaitable_unwrap<std::variant<T...>>(r0));
  463. if (!ex1)
  464. co_return std::variant<T..., U>{
  465. std::in_place_index<sizeof...(T)>,
  466. std::move(detail::awaitable_unwrap<U>(r1))};
  467. throw multiple_exceptions(ex0);
  468. }
  469. else
  470. {
  471. if (!ex1)
  472. co_return std::variant<T..., U>{
  473. std::in_place_index<sizeof...(T)>,
  474. std::move(detail::awaitable_unwrap<U>(r1))};
  475. if (!ex0)
  476. co_return widen::template call<0>(
  477. detail::awaitable_unwrap<std::variant<T...>>(r0));
  478. throw multiple_exceptions(ex1);
  479. }
  480. }
  481. } // namespace awaitable_operators
  482. } // namespace experimental
  483. } // namespace asio
  484. } // namespace boost
  485. #include <boost/asio/detail/pop_options.hpp>
  486. #endif // BOOST_ASIO_EXPERIMENTAL_AWAITABLE_OPERATORS_HPP