use_future.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. //
  2. // impl/use_future.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_IMPL_USE_FUTURE_HPP
  11. #define ASIO_IMPL_USE_FUTURE_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 <tuple>
  17. #include "asio/async_result.hpp"
  18. #include "asio/detail/memory.hpp"
  19. #include "asio/dispatch.hpp"
  20. #include "asio/error_code.hpp"
  21. #include "asio/execution.hpp"
  22. #include "asio/packaged_task.hpp"
  23. #include "asio/system_error.hpp"
  24. #include "asio/system_executor.hpp"
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace detail {
  28. template <typename T, typename F, typename... Args>
  29. inline void promise_invoke_and_set(std::promise<T>& p,
  30. F& f, Args&&... args)
  31. {
  32. #if !defined(ASIO_NO_EXCEPTIONS)
  33. try
  34. #endif // !defined(ASIO_NO_EXCEPTIONS)
  35. {
  36. p.set_value(f(static_cast<Args&&>(args)...));
  37. }
  38. #if !defined(ASIO_NO_EXCEPTIONS)
  39. catch (...)
  40. {
  41. p.set_exception(std::current_exception());
  42. }
  43. #endif // !defined(ASIO_NO_EXCEPTIONS)
  44. }
  45. template <typename F, typename... Args>
  46. inline void promise_invoke_and_set(std::promise<void>& p,
  47. F& f, Args&&... args)
  48. {
  49. #if !defined(ASIO_NO_EXCEPTIONS)
  50. try
  51. #endif // !defined(ASIO_NO_EXCEPTIONS)
  52. {
  53. f(static_cast<Args&&>(args)...);
  54. p.set_value();
  55. }
  56. #if !defined(ASIO_NO_EXCEPTIONS)
  57. catch (...)
  58. {
  59. p.set_exception(std::current_exception());
  60. }
  61. #endif // !defined(ASIO_NO_EXCEPTIONS)
  62. }
  63. // A function object adapter to invoke a nullary function object and capture
  64. // any exception thrown into a promise.
  65. template <typename T, typename F>
  66. class promise_invoker
  67. {
  68. public:
  69. promise_invoker(const shared_ptr<std::promise<T>>& p,
  70. F&& f)
  71. : p_(p), f_(static_cast<F&&>(f))
  72. {
  73. }
  74. void operator()()
  75. {
  76. #if !defined(ASIO_NO_EXCEPTIONS)
  77. try
  78. #endif // !defined(ASIO_NO_EXCEPTIONS)
  79. {
  80. f_();
  81. }
  82. #if !defined(ASIO_NO_EXCEPTIONS)
  83. catch (...)
  84. {
  85. p_->set_exception(std::current_exception());
  86. }
  87. #endif // !defined(ASIO_NO_EXCEPTIONS)
  88. }
  89. private:
  90. shared_ptr<std::promise<T>> p_;
  91. decay_t<F> f_;
  92. };
  93. // An executor that adapts the system_executor to capture any exeption thrown
  94. // by a submitted function object and save it into a promise.
  95. template <typename T, typename Blocking = execution::blocking_t::possibly_t>
  96. class promise_executor
  97. {
  98. public:
  99. explicit promise_executor(const shared_ptr<std::promise<T>>& p)
  100. : p_(p)
  101. {
  102. }
  103. execution_context& query(execution::context_t) const noexcept
  104. {
  105. return asio::query(system_executor(), execution::context);
  106. }
  107. static constexpr Blocking query(execution::blocking_t)
  108. {
  109. return Blocking();
  110. }
  111. promise_executor<T, execution::blocking_t::possibly_t>
  112. require(execution::blocking_t::possibly_t) const
  113. {
  114. return promise_executor<T, execution::blocking_t::possibly_t>(p_);
  115. }
  116. promise_executor<T, execution::blocking_t::never_t>
  117. require(execution::blocking_t::never_t) const
  118. {
  119. return promise_executor<T, execution::blocking_t::never_t>(p_);
  120. }
  121. template <typename F>
  122. void execute(F&& f) const
  123. {
  124. asio::require(system_executor(), Blocking()).execute(
  125. promise_invoker<T, F>(p_, static_cast<F&&>(f)));
  126. }
  127. #if !defined(ASIO_NO_TS_EXECUTORS)
  128. execution_context& context() const noexcept
  129. {
  130. return system_executor().context();
  131. }
  132. void on_work_started() const noexcept {}
  133. void on_work_finished() const noexcept {}
  134. template <typename F, typename A>
  135. void dispatch(F&& f, const A&) const
  136. {
  137. promise_invoker<T, F>(p_, static_cast<F&&>(f))();
  138. }
  139. template <typename F, typename A>
  140. void post(F&& f, const A& a) const
  141. {
  142. system_executor().post(
  143. promise_invoker<T, F>(p_, static_cast<F&&>(f)), a);
  144. }
  145. template <typename F, typename A>
  146. void defer(F&& f, const A& a) const
  147. {
  148. system_executor().defer(
  149. promise_invoker<T, F>(p_, static_cast<F&&>(f)), a);
  150. }
  151. #endif // !defined(ASIO_NO_TS_EXECUTORS)
  152. friend bool operator==(const promise_executor& a,
  153. const promise_executor& b) noexcept
  154. {
  155. return a.p_ == b.p_;
  156. }
  157. friend bool operator!=(const promise_executor& a,
  158. const promise_executor& b) noexcept
  159. {
  160. return a.p_ != b.p_;
  161. }
  162. private:
  163. shared_ptr<std::promise<T>> p_;
  164. };
  165. // The base class for all completion handlers that create promises.
  166. template <typename T>
  167. class promise_creator
  168. {
  169. public:
  170. typedef promise_executor<T> executor_type;
  171. executor_type get_executor() const noexcept
  172. {
  173. return executor_type(p_);
  174. }
  175. typedef std::future<T> future_type;
  176. future_type get_future()
  177. {
  178. return p_->get_future();
  179. }
  180. protected:
  181. template <typename Allocator>
  182. void create_promise(const Allocator& a)
  183. {
  184. ASIO_REBIND_ALLOC(Allocator, char) b(a);
  185. p_ = std::allocate_shared<std::promise<T>>(b, std::allocator_arg, b);
  186. }
  187. shared_ptr<std::promise<T>> p_;
  188. };
  189. // For completion signature void().
  190. class promise_handler_0
  191. : public promise_creator<void>
  192. {
  193. public:
  194. void operator()()
  195. {
  196. this->p_->set_value();
  197. }
  198. };
  199. // For completion signature void(error_code).
  200. class promise_handler_ec_0
  201. : public promise_creator<void>
  202. {
  203. public:
  204. void operator()(const asio::error_code& ec)
  205. {
  206. if (ec)
  207. {
  208. this->p_->set_exception(
  209. std::make_exception_ptr(
  210. asio::system_error(ec)));
  211. }
  212. else
  213. {
  214. this->p_->set_value();
  215. }
  216. }
  217. };
  218. // For completion signature void(exception_ptr).
  219. class promise_handler_ex_0
  220. : public promise_creator<void>
  221. {
  222. public:
  223. void operator()(const std::exception_ptr& ex)
  224. {
  225. if (ex)
  226. {
  227. this->p_->set_exception(ex);
  228. }
  229. else
  230. {
  231. this->p_->set_value();
  232. }
  233. }
  234. };
  235. // For completion signature void(T).
  236. template <typename T>
  237. class promise_handler_1
  238. : public promise_creator<T>
  239. {
  240. public:
  241. template <typename Arg>
  242. void operator()(Arg&& arg)
  243. {
  244. this->p_->set_value(static_cast<Arg&&>(arg));
  245. }
  246. };
  247. // For completion signature void(error_code, T).
  248. template <typename T>
  249. class promise_handler_ec_1
  250. : public promise_creator<T>
  251. {
  252. public:
  253. template <typename Arg>
  254. void operator()(const asio::error_code& ec,
  255. Arg&& arg)
  256. {
  257. if (ec)
  258. {
  259. this->p_->set_exception(
  260. std::make_exception_ptr(
  261. asio::system_error(ec)));
  262. }
  263. else
  264. this->p_->set_value(static_cast<Arg&&>(arg));
  265. }
  266. };
  267. // For completion signature void(exception_ptr, T).
  268. template <typename T>
  269. class promise_handler_ex_1
  270. : public promise_creator<T>
  271. {
  272. public:
  273. template <typename Arg>
  274. void operator()(const std::exception_ptr& ex,
  275. Arg&& arg)
  276. {
  277. if (ex)
  278. this->p_->set_exception(ex);
  279. else
  280. this->p_->set_value(static_cast<Arg&&>(arg));
  281. }
  282. };
  283. // For completion signature void(T1, ..., Tn);
  284. template <typename T>
  285. class promise_handler_n
  286. : public promise_creator<T>
  287. {
  288. public:
  289. template <typename... Args>
  290. void operator()(Args&&... args)
  291. {
  292. this->p_->set_value(
  293. std::forward_as_tuple(
  294. static_cast<Args&&>(args)...));
  295. }
  296. };
  297. // For completion signature void(error_code, T1, ..., Tn);
  298. template <typename T>
  299. class promise_handler_ec_n
  300. : public promise_creator<T>
  301. {
  302. public:
  303. template <typename... Args>
  304. void operator()(const asio::error_code& ec, Args&&... args)
  305. {
  306. if (ec)
  307. {
  308. this->p_->set_exception(
  309. std::make_exception_ptr(
  310. asio::system_error(ec)));
  311. }
  312. else
  313. {
  314. this->p_->set_value(
  315. std::forward_as_tuple(
  316. static_cast<Args&&>(args)...));
  317. }
  318. }
  319. };
  320. // For completion signature void(exception_ptr, T1, ..., Tn);
  321. template <typename T>
  322. class promise_handler_ex_n
  323. : public promise_creator<T>
  324. {
  325. public:
  326. template <typename... Args>
  327. void operator()(const std::exception_ptr& ex,
  328. Args&&... args)
  329. {
  330. if (ex)
  331. this->p_->set_exception(ex);
  332. else
  333. {
  334. this->p_->set_value(
  335. std::forward_as_tuple(
  336. static_cast<Args&&>(args)...));
  337. }
  338. }
  339. };
  340. // Helper template to choose the appropriate concrete promise handler
  341. // implementation based on the supplied completion signature.
  342. template <typename> class promise_handler_selector;
  343. template <>
  344. class promise_handler_selector<void()>
  345. : public promise_handler_0 {};
  346. template <>
  347. class promise_handler_selector<void(asio::error_code)>
  348. : public promise_handler_ec_0 {};
  349. template <>
  350. class promise_handler_selector<void(std::exception_ptr)>
  351. : public promise_handler_ex_0 {};
  352. template <typename Arg>
  353. class promise_handler_selector<void(Arg)>
  354. : public promise_handler_1<Arg> {};
  355. template <typename Arg>
  356. class promise_handler_selector<void(asio::error_code, Arg)>
  357. : public promise_handler_ec_1<Arg> {};
  358. template <typename Arg>
  359. class promise_handler_selector<void(std::exception_ptr, Arg)>
  360. : public promise_handler_ex_1<Arg> {};
  361. template <typename... Arg>
  362. class promise_handler_selector<void(Arg...)>
  363. : public promise_handler_n<std::tuple<Arg...>> {};
  364. template <typename... Arg>
  365. class promise_handler_selector<void(asio::error_code, Arg...)>
  366. : public promise_handler_ec_n<std::tuple<Arg...>> {};
  367. template <typename... Arg>
  368. class promise_handler_selector<void(std::exception_ptr, Arg...)>
  369. : public promise_handler_ex_n<std::tuple<Arg...>> {};
  370. // Completion handlers produced from the use_future completion token, when not
  371. // using use_future::operator().
  372. template <typename Signature, typename Allocator>
  373. class promise_handler
  374. : public promise_handler_selector<Signature>
  375. {
  376. public:
  377. typedef Allocator allocator_type;
  378. typedef void result_type;
  379. promise_handler(use_future_t<Allocator> u)
  380. : allocator_(u.get_allocator())
  381. {
  382. this->create_promise(allocator_);
  383. }
  384. allocator_type get_allocator() const noexcept
  385. {
  386. return allocator_;
  387. }
  388. private:
  389. Allocator allocator_;
  390. };
  391. template <typename Function>
  392. struct promise_function_wrapper
  393. {
  394. explicit promise_function_wrapper(Function& f)
  395. : function_(static_cast<Function&&>(f))
  396. {
  397. }
  398. explicit promise_function_wrapper(const Function& f)
  399. : function_(f)
  400. {
  401. }
  402. void operator()()
  403. {
  404. function_();
  405. }
  406. Function function_;
  407. };
  408. // Helper base class for async_result specialisation.
  409. template <typename Signature, typename Allocator>
  410. class promise_async_result
  411. {
  412. public:
  413. typedef promise_handler<Signature, Allocator> completion_handler_type;
  414. typedef typename completion_handler_type::future_type return_type;
  415. explicit promise_async_result(completion_handler_type& h)
  416. : future_(h.get_future())
  417. {
  418. }
  419. return_type get()
  420. {
  421. return static_cast<return_type&&>(future_);
  422. }
  423. private:
  424. return_type future_;
  425. };
  426. // Return value from use_future::operator().
  427. template <typename Function, typename Allocator>
  428. class packaged_token
  429. {
  430. public:
  431. packaged_token(Function f, const Allocator& a)
  432. : function_(static_cast<Function&&>(f)),
  433. allocator_(a)
  434. {
  435. }
  436. //private:
  437. Function function_;
  438. Allocator allocator_;
  439. };
  440. // Completion handlers produced from the use_future completion token, when
  441. // using use_future::operator().
  442. template <typename Function, typename Allocator, typename Result>
  443. class packaged_handler
  444. : public promise_creator<Result>
  445. {
  446. public:
  447. typedef Allocator allocator_type;
  448. typedef void result_type;
  449. packaged_handler(packaged_token<Function, Allocator> t)
  450. : function_(static_cast<Function&&>(t.function_)),
  451. allocator_(t.allocator_)
  452. {
  453. this->create_promise(allocator_);
  454. }
  455. allocator_type get_allocator() const noexcept
  456. {
  457. return allocator_;
  458. }
  459. template <typename... Args>
  460. void operator()(Args&&... args)
  461. {
  462. (promise_invoke_and_set)(*this->p_,
  463. function_, static_cast<Args&&>(args)...);
  464. }
  465. private:
  466. Function function_;
  467. Allocator allocator_;
  468. };
  469. // Helper base class for async_result specialisation.
  470. template <typename Function, typename Allocator, typename Result>
  471. class packaged_async_result
  472. {
  473. public:
  474. typedef packaged_handler<Function, Allocator, Result> completion_handler_type;
  475. typedef typename completion_handler_type::future_type return_type;
  476. explicit packaged_async_result(completion_handler_type& h)
  477. : future_(h.get_future())
  478. {
  479. }
  480. return_type get()
  481. {
  482. return static_cast<return_type&&>(future_);
  483. }
  484. private:
  485. return_type future_;
  486. };
  487. } // namespace detail
  488. template <typename Allocator> template <typename Function>
  489. inline detail::packaged_token<decay_t<Function>, Allocator>
  490. use_future_t<Allocator>::operator()(Function&& f) const
  491. {
  492. return detail::packaged_token<decay_t<Function>, Allocator>(
  493. static_cast<Function&&>(f), allocator_);
  494. }
  495. #if !defined(GENERATING_DOCUMENTATION)
  496. template <typename Allocator, typename Result, typename... Args>
  497. class async_result<use_future_t<Allocator>, Result(Args...)>
  498. : public detail::promise_async_result<
  499. void(decay_t<Args>...), Allocator>
  500. {
  501. public:
  502. explicit async_result(
  503. typename detail::promise_async_result<void(decay_t<Args>...),
  504. Allocator>::completion_handler_type& h)
  505. : detail::promise_async_result<
  506. void(decay_t<Args>...), Allocator>(h)
  507. {
  508. }
  509. };
  510. template <typename Function, typename Allocator,
  511. typename Result, typename... Args>
  512. class async_result<detail::packaged_token<Function, Allocator>, Result(Args...)>
  513. : public detail::packaged_async_result<Function, Allocator,
  514. result_of_t<Function(Args...)>>
  515. {
  516. public:
  517. explicit async_result(
  518. typename detail::packaged_async_result<Function, Allocator,
  519. result_of_t<Function(Args...)>>::completion_handler_type& h)
  520. : detail::packaged_async_result<Function, Allocator,
  521. result_of_t<Function(Args...)>>(h)
  522. {
  523. }
  524. };
  525. namespace traits {
  526. #if !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  527. template <typename T, typename Blocking>
  528. struct equality_comparable<
  529. asio::detail::promise_executor<T, Blocking>>
  530. {
  531. static constexpr bool is_valid = true;
  532. static constexpr bool is_noexcept = true;
  533. };
  534. #endif // !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  535. #if !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  536. template <typename T, typename Blocking, typename Function>
  537. struct execute_member<
  538. asio::detail::promise_executor<T, Blocking>, Function>
  539. {
  540. static constexpr bool is_valid = true;
  541. static constexpr bool is_noexcept = false;
  542. typedef void result_type;
  543. };
  544. #endif // !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  545. #if !defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  546. template <typename T, typename Blocking, typename Property>
  547. struct query_static_constexpr_member<
  548. asio::detail::promise_executor<T, Blocking>,
  549. Property,
  550. typename asio::enable_if<
  551. asio::is_convertible<
  552. Property,
  553. asio::execution::blocking_t
  554. >::value
  555. >::type
  556. >
  557. {
  558. static constexpr bool is_valid = true;
  559. static constexpr bool is_noexcept = true;
  560. typedef Blocking result_type;
  561. static constexpr result_type value() noexcept
  562. {
  563. return Blocking();
  564. }
  565. };
  566. #endif // !defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  567. #if !defined(ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  568. template <typename T, typename Blocking>
  569. struct query_member<
  570. asio::detail::promise_executor<T, Blocking>,
  571. execution::context_t
  572. >
  573. {
  574. static constexpr bool is_valid = true;
  575. static constexpr bool is_noexcept = true;
  576. typedef asio::system_context& result_type;
  577. };
  578. #endif // !defined(ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  579. #if !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  580. template <typename T, typename Blocking>
  581. struct require_member<
  582. asio::detail::promise_executor<T, Blocking>,
  583. execution::blocking_t::possibly_t
  584. >
  585. {
  586. static constexpr bool is_valid = true;
  587. static constexpr bool is_noexcept = true;
  588. typedef asio::detail::promise_executor<T,
  589. execution::blocking_t::possibly_t> result_type;
  590. };
  591. template <typename T, typename Blocking>
  592. struct require_member<
  593. asio::detail::promise_executor<T, Blocking>,
  594. execution::blocking_t::never_t
  595. >
  596. {
  597. static constexpr bool is_valid = true;
  598. static constexpr bool is_noexcept = true;
  599. typedef asio::detail::promise_executor<T,
  600. execution::blocking_t::never_t> result_type;
  601. };
  602. #endif // !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  603. } // namespace traits
  604. #endif // !defined(GENERATING_DOCUMENTATION)
  605. } // namespace asio
  606. #include "asio/detail/pop_options.hpp"
  607. #endif // ASIO_IMPL_USE_FUTURE_HPP