awaitable_operators.hpp 15 KB

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