use_future.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. //
  2. // impl/use_future.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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/system/error_code.hpp>
  20. #include <boost/asio/packaged_task.hpp>
  21. #include <boost/system/system_error.hpp>
  22. #include <boost/asio/system_executor.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace detail {
  27. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  28. template <typename T, typename F, typename... Args>
  29. inline void promise_invoke_and_set(std::promise<T>& p,
  30. F& f, BOOST_ASIO_MOVE_ARG(Args)... args)
  31. {
  32. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  33. try
  34. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  35. {
  36. p.set_value(f(BOOST_ASIO_MOVE_CAST(Args)(args)...));
  37. }
  38. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  39. catch (...)
  40. {
  41. p.set_exception(std::current_exception());
  42. }
  43. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  44. }
  45. template <typename F, typename... Args>
  46. inline void promise_invoke_and_set(std::promise<void>& p,
  47. F& f, BOOST_ASIO_MOVE_ARG(Args)... args)
  48. {
  49. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  50. try
  51. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  52. {
  53. f(BOOST_ASIO_MOVE_CAST(Args)(args)...);
  54. p.set_value();
  55. }
  56. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  57. catch (...)
  58. {
  59. p.set_exception(std::current_exception());
  60. }
  61. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  62. }
  63. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  64. template <typename T, typename F>
  65. inline void promise_invoke_and_set(std::promise<T>& p, F& f)
  66. {
  67. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  68. try
  69. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  70. {
  71. p.set_value(f());
  72. }
  73. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  74. catch (...)
  75. {
  76. p.set_exception(std::current_exception());
  77. }
  78. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  79. }
  80. template <typename F, typename Args>
  81. inline void promise_invoke_and_set(std::promise<void>& p, F& f)
  82. {
  83. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  84. try
  85. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  86. {
  87. f();
  88. p.set_value();
  89. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  90. }
  91. catch (...)
  92. {
  93. p.set_exception(std::current_exception());
  94. }
  95. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  96. }
  97. #if defined(BOOST_ASIO_NO_EXCEPTIONS)
  98. #define BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \
  99. template <typename T, typename F, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  100. inline void promise_invoke_and_set(std::promise<T>& p, \
  101. F& f, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  102. { \
  103. p.set_value(f(BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
  104. } \
  105. \
  106. template <typename F, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  107. inline void promise_invoke_and_set(std::promise<void>& p, \
  108. F& f, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  109. { \
  110. f(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
  111. p.set_value(); \
  112. } \
  113. /**/
  114. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF)
  115. #undef BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF
  116. #else // defined(BOOST_ASIO_NO_EXCEPTIONS)
  117. #define BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \
  118. template <typename T, typename F, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  119. inline void promise_invoke_and_set(std::promise<T>& p, \
  120. F& f, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  121. { \
  122. try \
  123. { \
  124. p.set_value(f(BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
  125. } \
  126. catch (...) \
  127. { \
  128. p.set_exception(std::current_exception()); \
  129. } \
  130. } \
  131. \
  132. template <typename F, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  133. inline void promise_invoke_and_set(std::promise<void>& p, \
  134. F& f, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  135. { \
  136. try \
  137. { \
  138. f(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
  139. p.set_value(); \
  140. } \
  141. catch (...) \
  142. { \
  143. p.set_exception(std::current_exception()); \
  144. } \
  145. } \
  146. /**/
  147. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF)
  148. #undef BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF
  149. #endif // defined(BOOST_ASIO_NO_EXCEPTIONS)
  150. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  151. // A function object adapter to invoke a nullary function object and capture
  152. // any exception thrown into a promise.
  153. template <typename T, typename F>
  154. class promise_invoker
  155. {
  156. public:
  157. promise_invoker(const shared_ptr<std::promise<T> >& p,
  158. BOOST_ASIO_MOVE_ARG(F) f)
  159. : p_(p), f_(BOOST_ASIO_MOVE_CAST(F)(f))
  160. {
  161. }
  162. void operator()()
  163. {
  164. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  165. try
  166. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  167. {
  168. f_();
  169. }
  170. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  171. catch (...)
  172. {
  173. p_->set_exception(std::current_exception());
  174. }
  175. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  176. }
  177. private:
  178. shared_ptr<std::promise<T> > p_;
  179. typename decay<F>::type f_;
  180. };
  181. // An executor that adapts the system_executor to capture any exeption thrown
  182. // by a submitted function object and save it into a promise.
  183. template <typename T>
  184. class promise_executor
  185. {
  186. public:
  187. explicit promise_executor(const shared_ptr<std::promise<T> >& p)
  188. : p_(p)
  189. {
  190. }
  191. execution_context& context() const BOOST_ASIO_NOEXCEPT
  192. {
  193. return system_executor().context();
  194. }
  195. void on_work_started() const BOOST_ASIO_NOEXCEPT {}
  196. void on_work_finished() const BOOST_ASIO_NOEXCEPT {}
  197. template <typename F, typename A>
  198. void dispatch(BOOST_ASIO_MOVE_ARG(F) f, const A&) const
  199. {
  200. promise_invoker<T, F>(p_, BOOST_ASIO_MOVE_CAST(F)(f))();
  201. }
  202. template <typename F, typename A>
  203. void post(BOOST_ASIO_MOVE_ARG(F) f, const A& a) const
  204. {
  205. system_executor().post(
  206. promise_invoker<T, F>(p_, BOOST_ASIO_MOVE_CAST(F)(f)), a);
  207. }
  208. template <typename F, typename A>
  209. void defer(BOOST_ASIO_MOVE_ARG(F) f, const A& a) const
  210. {
  211. system_executor().defer(
  212. promise_invoker<T, F>(p_, BOOST_ASIO_MOVE_CAST(F)(f)), a);
  213. }
  214. friend bool operator==(const promise_executor& a,
  215. const promise_executor& b) BOOST_ASIO_NOEXCEPT
  216. {
  217. return a.p_ == b.p_;
  218. }
  219. friend bool operator!=(const promise_executor& a,
  220. const promise_executor& b) BOOST_ASIO_NOEXCEPT
  221. {
  222. return a.p_ != b.p_;
  223. }
  224. private:
  225. shared_ptr<std::promise<T> > p_;
  226. };
  227. // The base class for all completion handlers that create promises.
  228. template <typename T>
  229. class promise_creator
  230. {
  231. public:
  232. typedef promise_executor<T> executor_type;
  233. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  234. {
  235. return executor_type(p_);
  236. }
  237. typedef std::future<T> future_type;
  238. future_type get_future()
  239. {
  240. return p_->get_future();
  241. }
  242. protected:
  243. template <typename Allocator>
  244. void create_promise(const Allocator& a)
  245. {
  246. BOOST_ASIO_REBIND_ALLOC(Allocator, char) b(a);
  247. p_ = std::allocate_shared<std::promise<T>>(b, std::allocator_arg, b);
  248. }
  249. shared_ptr<std::promise<T> > p_;
  250. };
  251. // For completion signature void().
  252. class promise_handler_0
  253. : public promise_creator<void>
  254. {
  255. public:
  256. void operator()()
  257. {
  258. this->p_->set_value();
  259. }
  260. };
  261. // For completion signature void(error_code).
  262. class promise_handler_ec_0
  263. : public promise_creator<void>
  264. {
  265. public:
  266. void operator()(const boost::system::error_code& ec)
  267. {
  268. if (ec)
  269. {
  270. this->p_->set_exception(
  271. std::make_exception_ptr(
  272. boost::system::system_error(ec)));
  273. }
  274. else
  275. {
  276. this->p_->set_value();
  277. }
  278. }
  279. };
  280. // For completion signature void(exception_ptr).
  281. class promise_handler_ex_0
  282. : public promise_creator<void>
  283. {
  284. public:
  285. void operator()(const std::exception_ptr& ex)
  286. {
  287. if (ex)
  288. {
  289. this->p_->set_exception(ex);
  290. }
  291. else
  292. {
  293. this->p_->set_value();
  294. }
  295. }
  296. };
  297. // For completion signature void(T).
  298. template <typename T>
  299. class promise_handler_1
  300. : public promise_creator<T>
  301. {
  302. public:
  303. template <typename Arg>
  304. void operator()(BOOST_ASIO_MOVE_ARG(Arg) arg)
  305. {
  306. this->p_->set_value(BOOST_ASIO_MOVE_CAST(Arg)(arg));
  307. }
  308. };
  309. // For completion signature void(error_code, T).
  310. template <typename T>
  311. class promise_handler_ec_1
  312. : public promise_creator<T>
  313. {
  314. public:
  315. template <typename Arg>
  316. void operator()(const boost::system::error_code& ec,
  317. BOOST_ASIO_MOVE_ARG(Arg) arg)
  318. {
  319. if (ec)
  320. {
  321. this->p_->set_exception(
  322. std::make_exception_ptr(
  323. boost::system::system_error(ec)));
  324. }
  325. else
  326. this->p_->set_value(BOOST_ASIO_MOVE_CAST(Arg)(arg));
  327. }
  328. };
  329. // For completion signature void(exception_ptr, T).
  330. template <typename T>
  331. class promise_handler_ex_1
  332. : public promise_creator<T>
  333. {
  334. public:
  335. template <typename Arg>
  336. void operator()(const std::exception_ptr& ex,
  337. BOOST_ASIO_MOVE_ARG(Arg) arg)
  338. {
  339. if (ex)
  340. this->p_->set_exception(ex);
  341. else
  342. this->p_->set_value(BOOST_ASIO_MOVE_CAST(Arg)(arg));
  343. }
  344. };
  345. // For completion signature void(T1, ..., Tn);
  346. template <typename T>
  347. class promise_handler_n
  348. : public promise_creator<T>
  349. {
  350. public:
  351. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  352. template <typename... Args>
  353. void operator()(BOOST_ASIO_MOVE_ARG(Args)... args)
  354. {
  355. this->p_->set_value(
  356. std::forward_as_tuple(
  357. BOOST_ASIO_MOVE_CAST(Args)(args)...));
  358. }
  359. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  360. #define BOOST_ASIO_PRIVATE_CALL_OP_DEF(n) \
  361. template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  362. void operator()(BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  363. {\
  364. this->p_->set_value( \
  365. std::forward_as_tuple( \
  366. BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
  367. } \
  368. /**/
  369. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CALL_OP_DEF)
  370. #undef BOOST_ASIO_PRIVATE_CALL_OP_DEF
  371. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  372. };
  373. // For completion signature void(error_code, T1, ..., Tn);
  374. template <typename T>
  375. class promise_handler_ec_n
  376. : public promise_creator<T>
  377. {
  378. public:
  379. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  380. template <typename... Args>
  381. void operator()(const boost::system::error_code& ec,
  382. BOOST_ASIO_MOVE_ARG(Args)... args)
  383. {
  384. if (ec)
  385. {
  386. this->p_->set_exception(
  387. std::make_exception_ptr(
  388. boost::system::system_error(ec)));
  389. }
  390. else
  391. {
  392. this->p_->set_value(
  393. std::forward_as_tuple(
  394. BOOST_ASIO_MOVE_CAST(Args)(args)...));
  395. }
  396. }
  397. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  398. #define BOOST_ASIO_PRIVATE_CALL_OP_DEF(n) \
  399. template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  400. void operator()(const boost::system::error_code& ec, \
  401. BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  402. {\
  403. if (ec) \
  404. { \
  405. this->p_->set_exception( \
  406. std::make_exception_ptr( \
  407. boost::system::system_error(ec))); \
  408. } \
  409. else \
  410. { \
  411. this->p_->set_value( \
  412. std::forward_as_tuple( \
  413. BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
  414. } \
  415. } \
  416. /**/
  417. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CALL_OP_DEF)
  418. #undef BOOST_ASIO_PRIVATE_CALL_OP_DEF
  419. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  420. };
  421. // For completion signature void(exception_ptr, T1, ..., Tn);
  422. template <typename T>
  423. class promise_handler_ex_n
  424. : public promise_creator<T>
  425. {
  426. public:
  427. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  428. template <typename... Args>
  429. void operator()(const std::exception_ptr& ex,
  430. BOOST_ASIO_MOVE_ARG(Args)... args)
  431. {
  432. if (ex)
  433. this->p_->set_exception(ex);
  434. else
  435. {
  436. this->p_->set_value(
  437. std::forward_as_tuple(
  438. BOOST_ASIO_MOVE_CAST(Args)(args)...));
  439. }
  440. }
  441. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  442. #define BOOST_ASIO_PRIVATE_CALL_OP_DEF(n) \
  443. template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  444. void operator()(const std::exception_ptr& ex, \
  445. BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  446. {\
  447. if (ex) \
  448. this->p_->set_exception(ex); \
  449. else \
  450. { \
  451. this->p_->set_value( \
  452. std::forward_as_tuple( \
  453. BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
  454. } \
  455. } \
  456. /**/
  457. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CALL_OP_DEF)
  458. #undef BOOST_ASIO_PRIVATE_CALL_OP_DEF
  459. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  460. };
  461. // Helper template to choose the appropriate concrete promise handler
  462. // implementation based on the supplied completion signature.
  463. template <typename> class promise_handler_selector;
  464. template <>
  465. class promise_handler_selector<void()>
  466. : public promise_handler_0 {};
  467. template <>
  468. class promise_handler_selector<void(boost::system::error_code)>
  469. : public promise_handler_ec_0 {};
  470. template <>
  471. class promise_handler_selector<void(std::exception_ptr)>
  472. : public promise_handler_ex_0 {};
  473. template <typename Arg>
  474. class promise_handler_selector<void(Arg)>
  475. : public promise_handler_1<Arg> {};
  476. template <typename Arg>
  477. class promise_handler_selector<void(boost::system::error_code, Arg)>
  478. : public promise_handler_ec_1<Arg> {};
  479. template <typename Arg>
  480. class promise_handler_selector<void(std::exception_ptr, Arg)>
  481. : public promise_handler_ex_1<Arg> {};
  482. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  483. template <typename... Arg>
  484. class promise_handler_selector<void(Arg...)>
  485. : public promise_handler_n<std::tuple<Arg...> > {};
  486. template <typename... Arg>
  487. class promise_handler_selector<void(boost::system::error_code, Arg...)>
  488. : public promise_handler_ec_n<std::tuple<Arg...> > {};
  489. template <typename... Arg>
  490. class promise_handler_selector<void(std::exception_ptr, Arg...)>
  491. : public promise_handler_ex_n<std::tuple<Arg...> > {};
  492. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  493. #define BOOST_ASIO_PRIVATE_PROMISE_SELECTOR_DEF(n) \
  494. template <typename Arg, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  495. class promise_handler_selector< \
  496. void(Arg, BOOST_ASIO_VARIADIC_TARGS(n))> \
  497. : public promise_handler_n< \
  498. std::tuple<Arg, BOOST_ASIO_VARIADIC_TARGS(n)> > {}; \
  499. \
  500. template <typename Arg, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  501. class promise_handler_selector< \
  502. void(boost::system::error_code, Arg, BOOST_ASIO_VARIADIC_TARGS(n))> \
  503. : public promise_handler_ec_n< \
  504. std::tuple<Arg, BOOST_ASIO_VARIADIC_TARGS(n)> > {}; \
  505. \
  506. template <typename Arg, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  507. class promise_handler_selector< \
  508. void(std::exception_ptr, Arg, BOOST_ASIO_VARIADIC_TARGS(n))> \
  509. : public promise_handler_ex_n< \
  510. std::tuple<Arg, BOOST_ASIO_VARIADIC_TARGS(n)> > {}; \
  511. /**/
  512. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_PROMISE_SELECTOR_DEF)
  513. #undef BOOST_ASIO_PRIVATE_PROMISE_SELECTOR_DEF
  514. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  515. // Completion handlers produced from the use_future completion token, when not
  516. // using use_future::operator().
  517. template <typename Signature, typename Allocator>
  518. class promise_handler
  519. : public promise_handler_selector<Signature>
  520. {
  521. public:
  522. typedef Allocator allocator_type;
  523. typedef void result_type;
  524. promise_handler(use_future_t<Allocator> u)
  525. : allocator_(u.get_allocator())
  526. {
  527. this->create_promise(allocator_);
  528. }
  529. allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
  530. {
  531. return allocator_;
  532. }
  533. private:
  534. Allocator allocator_;
  535. };
  536. template <typename Function, typename Signature, typename Allocator>
  537. inline void asio_handler_invoke(Function& f,
  538. promise_handler<Signature, Allocator>* h)
  539. {
  540. typename promise_handler<Signature, Allocator>::executor_type
  541. ex(h->get_executor());
  542. ex.dispatch(BOOST_ASIO_MOVE_CAST(Function)(f), std::allocator<void>());
  543. }
  544. template <typename Function, typename Signature, typename Allocator>
  545. inline void asio_handler_invoke(const Function& f,
  546. promise_handler<Signature, Allocator>* h)
  547. {
  548. typename promise_handler<Signature, Allocator>::executor_type
  549. ex(h->get_executor());
  550. ex.dispatch(f, std::allocator<void>());
  551. }
  552. // Helper base class for async_result specialisation.
  553. template <typename Signature, typename Allocator>
  554. class promise_async_result
  555. {
  556. public:
  557. typedef promise_handler<Signature, Allocator> completion_handler_type;
  558. typedef typename completion_handler_type::future_type return_type;
  559. explicit promise_async_result(completion_handler_type& h)
  560. : future_(h.get_future())
  561. {
  562. }
  563. return_type get()
  564. {
  565. return BOOST_ASIO_MOVE_CAST(return_type)(future_);
  566. }
  567. private:
  568. return_type future_;
  569. };
  570. // Return value from use_future::operator().
  571. template <typename Function, typename Allocator>
  572. class packaged_token
  573. {
  574. public:
  575. packaged_token(Function f, const Allocator& a)
  576. : function_(BOOST_ASIO_MOVE_CAST(Function)(f)),
  577. allocator_(a)
  578. {
  579. }
  580. //private:
  581. Function function_;
  582. Allocator allocator_;
  583. };
  584. // Completion handlers produced from the use_future completion token, when
  585. // using use_future::operator().
  586. template <typename Function, typename Allocator, typename Result>
  587. class packaged_handler
  588. : public promise_creator<Result>
  589. {
  590. public:
  591. typedef Allocator allocator_type;
  592. typedef void result_type;
  593. packaged_handler(packaged_token<Function, Allocator> t)
  594. : function_(BOOST_ASIO_MOVE_CAST(Function)(t.function_)),
  595. allocator_(t.allocator_)
  596. {
  597. this->create_promise(allocator_);
  598. }
  599. allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
  600. {
  601. return allocator_;
  602. }
  603. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  604. template <typename... Args>
  605. void operator()(BOOST_ASIO_MOVE_ARG(Args)... args)
  606. {
  607. (promise_invoke_and_set)(*this->p_,
  608. function_, BOOST_ASIO_MOVE_CAST(Args)(args)...);
  609. }
  610. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  611. void operator()()
  612. {
  613. (promise_invoke_and_set)(*this->p_, function_);
  614. }
  615. #define BOOST_ASIO_PRIVATE_CALL_OP_DEF(n) \
  616. template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  617. void operator()(BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  618. {\
  619. (promise_invoke_and_set)(*this->p_, \
  620. function_, BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
  621. } \
  622. /**/
  623. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CALL_OP_DEF)
  624. #undef BOOST_ASIO_PRIVATE_CALL_OP_DEF
  625. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  626. private:
  627. Function function_;
  628. Allocator allocator_;
  629. };
  630. template <typename Function,
  631. typename Function1, typename Allocator, typename Result>
  632. inline void asio_handler_invoke(Function& f,
  633. packaged_handler<Function1, Allocator, Result>* h)
  634. {
  635. typename packaged_handler<Function1, Allocator, Result>::executor_type
  636. ex(h->get_executor());
  637. ex.dispatch(BOOST_ASIO_MOVE_CAST(Function)(f), std::allocator<void>());
  638. }
  639. template <typename Function,
  640. typename Function1, typename Allocator, typename Result>
  641. inline void asio_handler_invoke(const Function& f,
  642. packaged_handler<Function1, Allocator, Result>* h)
  643. {
  644. typename packaged_handler<Function1, Allocator, Result>::executor_type
  645. ex(h->get_executor());
  646. ex.dispatch(f, std::allocator<void>());
  647. }
  648. // Helper base class for async_result specialisation.
  649. template <typename Function, typename Allocator, typename Result>
  650. class packaged_async_result
  651. {
  652. public:
  653. typedef packaged_handler<Function, Allocator, Result> completion_handler_type;
  654. typedef typename completion_handler_type::future_type return_type;
  655. explicit packaged_async_result(completion_handler_type& h)
  656. : future_(h.get_future())
  657. {
  658. }
  659. return_type get()
  660. {
  661. return BOOST_ASIO_MOVE_CAST(return_type)(future_);
  662. }
  663. private:
  664. return_type future_;
  665. };
  666. } // namespace detail
  667. template <typename Allocator> template <typename Function>
  668. inline detail::packaged_token<typename decay<Function>::type, Allocator>
  669. use_future_t<Allocator>::operator()(BOOST_ASIO_MOVE_ARG(Function) f) const
  670. {
  671. return detail::packaged_token<typename decay<Function>::type, Allocator>(
  672. BOOST_ASIO_MOVE_CAST(Function)(f), allocator_);
  673. }
  674. #if !defined(GENERATING_DOCUMENTATION)
  675. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  676. template <typename Allocator, typename Result, typename... Args>
  677. class async_result<use_future_t<Allocator>, Result(Args...)>
  678. : public detail::promise_async_result<
  679. void(typename decay<Args>::type...), Allocator>
  680. {
  681. public:
  682. explicit async_result(
  683. typename detail::promise_async_result<void(typename decay<Args>::type...),
  684. Allocator>::completion_handler_type& h)
  685. : detail::promise_async_result<
  686. void(typename decay<Args>::type...), Allocator>(h)
  687. {
  688. }
  689. };
  690. template <typename Function, typename Allocator,
  691. typename Result, typename... Args>
  692. class async_result<detail::packaged_token<Function, Allocator>, Result(Args...)>
  693. : public detail::packaged_async_result<Function, Allocator,
  694. typename result_of<Function(Args...)>::type>
  695. {
  696. public:
  697. explicit async_result(
  698. typename detail::packaged_async_result<Function, Allocator,
  699. typename result_of<Function(Args...)>::type>::completion_handler_type& h)
  700. : detail::packaged_async_result<Function, Allocator,
  701. typename result_of<Function(Args...)>::type>(h)
  702. {
  703. }
  704. };
  705. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  706. template <typename Allocator, typename Result>
  707. class async_result<use_future_t<Allocator>, Result()>
  708. : public detail::promise_async_result<void(), Allocator>
  709. {
  710. public:
  711. explicit async_result(
  712. typename detail::promise_async_result<
  713. void(), Allocator>::completion_handler_type& h)
  714. : detail::promise_async_result<void(), Allocator>(h)
  715. {
  716. }
  717. };
  718. template <typename Function, typename Allocator, typename Result>
  719. class async_result<detail::packaged_token<Function, Allocator>, Result()>
  720. : public detail::packaged_async_result<Function, Allocator,
  721. typename result_of<Function()>::type>
  722. {
  723. public:
  724. explicit async_result(
  725. typename detail::packaged_async_result<Function, Allocator,
  726. typename result_of<Function()>::type>::completion_handler_type& h)
  727. : detail::packaged_async_result<Function, Allocator,
  728. typename result_of<Function()>::type>(h)
  729. {
  730. }
  731. };
  732. #define BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \
  733. template <typename Allocator, \
  734. typename Result, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  735. class async_result<use_future_t<Allocator>, \
  736. Result(BOOST_ASIO_VARIADIC_TARGS(n))> \
  737. : public detail::promise_async_result< \
  738. void(BOOST_ASIO_VARIADIC_DECAY(n)), Allocator> \
  739. { \
  740. public: \
  741. explicit async_result( \
  742. typename detail::promise_async_result< \
  743. void(BOOST_ASIO_VARIADIC_DECAY(n)), \
  744. Allocator>::completion_handler_type& h) \
  745. : detail::promise_async_result< \
  746. void(BOOST_ASIO_VARIADIC_DECAY(n)), Allocator>(h) \
  747. { \
  748. } \
  749. }; \
  750. \
  751. template <typename Function, typename Allocator, \
  752. typename Result, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  753. class async_result<detail::packaged_token<Function, Allocator>, \
  754. Result(BOOST_ASIO_VARIADIC_TARGS(n))> \
  755. : public detail::packaged_async_result<Function, Allocator, \
  756. typename result_of<Function(BOOST_ASIO_VARIADIC_TARGS(n))>::type> \
  757. { \
  758. public: \
  759. explicit async_result( \
  760. typename detail::packaged_async_result<Function, Allocator, \
  761. typename result_of<Function(BOOST_ASIO_VARIADIC_TARGS(n))>::type \
  762. >::completion_handler_type& h) \
  763. : detail::packaged_async_result<Function, Allocator, \
  764. typename result_of<Function(BOOST_ASIO_VARIADIC_TARGS(n))>::type>(h) \
  765. { \
  766. } \
  767. }; \
  768. /**/
  769. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF)
  770. #undef BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF
  771. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  772. #endif // !defined(GENERATING_DOCUMENTATION)
  773. } // namespace asio
  774. } // namespace boost
  775. #include <boost/asio/detail/pop_options.hpp>
  776. #endif // BOOST_ASIO_IMPL_USE_FUTURE_HPP