use_future.hpp 17 KB

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