async_base.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BHO_BEAST_CORE_ASYNC_BASE_HPP
  10. #define BHO_BEAST_CORE_ASYNC_BASE_HPP
  11. #include <asio2/bho/beast/core/detail/config.hpp>
  12. #include <asio2/bho/beast/core/bind_handler.hpp>
  13. #include <asio2/bho/beast/core/detail/allocator.hpp>
  14. #include <asio2/bho/beast/core/detail/async_base.hpp>
  15. #include <asio2/bho/beast/core/detail/filtering_cancellation_slot.hpp>
  16. #include <asio2/bho/beast/core/detail/work_guard.hpp>
  17. #include <asio/associated_allocator.hpp>
  18. #include <asio/associated_executor.hpp>
  19. #include <asio/associated_immediate_executor.hpp>
  20. #include <asio/bind_executor.hpp>
  21. #include <asio/handler_continuation_hook.hpp>
  22. #include <asio/dispatch.hpp>
  23. #include <asio/post.hpp>
  24. #include <asio2/bho/core/exchange.hpp>
  25. #include <asio2/bho/core/empty_value.hpp>
  26. #include <utility>
  27. namespace bho {
  28. namespace beast {
  29. /** Base class to assist writing composed operations.
  30. A function object submitted to intermediate initiating functions during
  31. a composed operation may derive from this type to inherit all of the
  32. boilerplate to forward the executor, allocator, and legacy customization
  33. points associated with the completion handler invoked at the end of the
  34. composed operation.
  35. The composed operation must be typical; that is, associated with one
  36. executor of an I/O object, and invoking a caller-provided completion
  37. handler when the operation is finished. Classes derived from
  38. @ref async_base will acquire these properties:
  39. @li Ownership of the final completion handler provided upon construction.
  40. @li If the final handler has an associated allocator, this allocator will
  41. be propagated to the composed operation subclass. Otherwise, the
  42. associated allocator will be the type specified in the allocator
  43. template parameter, or the default of `std::allocator<void>` if the
  44. parameter is omitted.
  45. @li If the final handler has an associated executor, then it will be used
  46. as the executor associated with the composed operation. Otherwise,
  47. the specified `Executor1` will be the type of executor associated
  48. with the composed operation.
  49. @li An instance of `net::executor_work_guard` for the instance of `Executor1`
  50. shall be maintained until either the final handler is invoked, or the
  51. operation base is destroyed, whichever comes first.
  52. @li Calls to the legacy customization point `asio_handler_is_continuation`
  53. which use argument-dependent lookup, will be forwarded to the
  54. legacy customization points associated with the handler.
  55. @par Example
  56. The following code demonstrates how @ref async_base may be be used to
  57. assist authoring an asynchronous initiating function, by providing all of
  58. the boilerplate to manage the final completion handler in a way that
  59. maintains the allocator and executor associations:
  60. @code
  61. // Asynchronously read into a buffer until the buffer is full, or an error occurs
  62. template<class AsyncReadStream, class ReadHandler>
  63. typename net::async_result<ReadHandler, void(error_code, std::size_t)>::return_type
  64. async_read(AsyncReadStream& stream, net::mutable_buffer buffer, ReadHandler&& handler)
  65. {
  66. using handler_type = ASIO_HANDLER_TYPE(ReadHandler, void(error_code, std::size_t));
  67. using base_type = async_base<handler_type, typename AsyncReadStream::executor_type>;
  68. struct op : base_type
  69. {
  70. AsyncReadStream& stream_;
  71. net::mutable_buffer buffer_;
  72. std::size_t total_bytes_transferred_;
  73. op(
  74. AsyncReadStream& stream,
  75. net::mutable_buffer buffer,
  76. handler_type& handler)
  77. : base_type(std::move(handler), stream.get_executor())
  78. , stream_(stream)
  79. , buffer_(buffer)
  80. , total_bytes_transferred_(0)
  81. {
  82. (*this)({}, 0, false); // start the operation
  83. }
  84. void operator()(error_code ec, std::size_t bytes_transferred, bool is_continuation = true)
  85. {
  86. // Adjust the count of bytes and advance our buffer
  87. total_bytes_transferred_ += bytes_transferred;
  88. buffer_ = buffer_ + bytes_transferred;
  89. // Keep reading until buffer is full or an error occurs
  90. if(! ec && buffer_.size() > 0)
  91. return stream_.async_read_some(buffer_, std::move(*this));
  92. // Call the completion handler with the result. If `is_continuation` is
  93. // false, which happens on the first time through this function, then
  94. // `net::post` will be used to call the completion handler, otherwise
  95. // the completion handler will be invoked directly.
  96. this->complete(is_continuation, ec, total_bytes_transferred_);
  97. }
  98. };
  99. net::async_completion<ReadHandler, void(error_code, std::size_t)> init{handler};
  100. op(stream, buffer, init.completion_handler);
  101. return init.result.get();
  102. }
  103. @endcode
  104. Data members of composed operations implemented as completion handlers
  105. do not have stable addresses, as the composed operation object is move
  106. constructed upon each call to an initiating function. For most operations
  107. this is not a problem. For complex operations requiring stable temporary
  108. storage, the class @ref stable_async_base is provided which offers
  109. additional functionality:
  110. @li The free function @ref allocate_stable may be used to allocate
  111. one or more temporary objects associated with the composed operation.
  112. @li Memory for stable temporary objects is allocated using the allocator
  113. associated with the composed operation.
  114. @li Stable temporary objects are automatically destroyed, and the memory
  115. freed using the associated allocator, either before the final completion
  116. handler is invoked (a Networking requirement) or when the composed operation
  117. is destroyed, whichever occurs first.
  118. @par Temporary Storage Example
  119. The following example demonstrates how a composed operation may store a
  120. temporary object.
  121. @code
  122. @endcode
  123. @tparam Handler The type of the completion handler to store.
  124. This type must meet the requirements of <em>CompletionHandler</em>.
  125. @tparam Executor1 The type of the executor used when the handler has no
  126. associated executor. An instance of this type must be provided upon
  127. construction. The implementation will maintain an executor work guard
  128. and a copy of this instance.
  129. @tparam Allocator The allocator type to use if the handler does not
  130. have an associated allocator. If this parameter is omitted, then
  131. `std::allocator<void>` will be used. If the specified allocator is
  132. not default constructible, an instance of the type must be provided
  133. upon construction.
  134. @see stable_async_base
  135. */
  136. template<
  137. class Handler,
  138. class Executor1,
  139. class Allocator = std::allocator<void>
  140. >
  141. class async_base
  142. #if ! BHO_BEAST_DOXYGEN
  143. : private bho::empty_value<Allocator>
  144. #endif
  145. {
  146. static_assert(
  147. net::is_executor<Executor1>::value || net::execution::is_executor<Executor1>::value,
  148. "Executor type requirements not met");
  149. Handler h_;
  150. detail::select_work_guard_t<Executor1> wg1_;
  151. net::cancellation_type act_{net::cancellation_type::terminal};
  152. public:
  153. /** The type of executor associated with this object.
  154. If a class derived from @ref bho::beast::async_base is a completion
  155. handler, then the associated executor of the derived class will
  156. be this type.
  157. */
  158. using executor_type =
  159. #if BHO_BEAST_DOXYGEN
  160. __implementation_defined__;
  161. #else
  162. typename
  163. net::associated_executor<
  164. Handler,
  165. typename detail::select_work_guard_t<Executor1>::executor_type
  166. >::type;
  167. #endif
  168. /** The type of the immediate executor associated with this object.
  169. If a class derived from @ref bho::beast::async_base is a completion
  170. handler, then the associated immediage executor of the derived class will
  171. be this type.
  172. */
  173. using immediate_executor_type =
  174. #if BHO_BEAST_DOXYGEN
  175. __implementation_defined__;
  176. #else
  177. typename
  178. net::associated_immediate_executor<
  179. Handler,
  180. typename detail::select_work_guard_t<Executor1>::executor_type
  181. >::type;
  182. #endif
  183. private:
  184. virtual
  185. void
  186. before_invoke_hook()
  187. {
  188. }
  189. public:
  190. /** Constructor
  191. @param handler The final completion handler.
  192. The type of this object must meet the requirements of <em>CompletionHandler</em>.
  193. The implementation takes ownership of the handler by performing a decay-copy.
  194. @param ex1 The executor associated with the implied I/O object
  195. target of the operation. The implementation shall maintain an
  196. executor work guard for the lifetime of the operation, or until
  197. the final completion handler is invoked, whichever is shorter.
  198. @param alloc The allocator to be associated with objects
  199. derived from this class. If `Allocator` is default-constructible,
  200. this parameter is optional and may be omitted.
  201. */
  202. #if BHO_BEAST_DOXYGEN
  203. template<class Handler_>
  204. async_base(
  205. Handler&& handler,
  206. Executor1 const& ex1,
  207. Allocator const& alloc = Allocator());
  208. #else
  209. template<
  210. class Handler_,
  211. class = typename std::enable_if<
  212. ! std::is_same<typename
  213. std::decay<Handler_>::type,
  214. async_base
  215. >::value>::type
  216. >
  217. async_base(
  218. Handler_&& handler,
  219. Executor1 const& ex1)
  220. : h_(std::forward<Handler_>(handler))
  221. , wg1_(detail::make_work_guard(ex1))
  222. {
  223. }
  224. template<class Handler_>
  225. async_base(
  226. Handler_&& handler,
  227. Executor1 const& ex1,
  228. Allocator const& alloc)
  229. : bho::empty_value<Allocator>(
  230. bho::empty_init_t{}, alloc)
  231. , h_(std::forward<Handler_>(handler))
  232. , wg1_(ex1)
  233. {
  234. }
  235. #endif
  236. /// Move Constructor
  237. async_base(async_base&& other) = default;
  238. virtual ~async_base() = default;
  239. async_base(async_base const&) = delete;
  240. async_base& operator=(async_base const&) = delete;
  241. /** The type of allocator associated with this object.
  242. If a class derived from @ref bho::beast::async_base is a completion
  243. handler, then the associated allocator of the derived class will
  244. be this type.
  245. */
  246. using allocator_type =
  247. net::associated_allocator_t<Handler, Allocator>;
  248. /** Returns the allocator associated with this object.
  249. If a class derived from @ref bho::beast::async_base is a completion
  250. handler, then the object returned from this function will be used
  251. as the associated allocator of the derived class.
  252. */
  253. allocator_type
  254. get_allocator() const noexcept
  255. {
  256. return net::get_associated_allocator(h_,
  257. bho::empty_value<Allocator>::get());
  258. }
  259. /** Returns the executor associated with this object.
  260. If a class derived from @ref bho::beast::async_base is a completion
  261. handler, then the object returned from this function will be used
  262. as the associated executor of the derived class.
  263. */
  264. executor_type
  265. get_executor() const noexcept
  266. {
  267. return net::get_associated_executor(
  268. h_, wg1_.get_executor());
  269. }
  270. /** Returns the immediate executor associated with this handler.
  271. If the handler has none it returns asios default immediate
  272. executor based on the executor of the object.
  273. If a class derived from @ref bho::beast::async_base is a completion
  274. handler, then the object returned from this function will be used
  275. as the associated immediate executor of the derived class.
  276. */
  277. immediate_executor_type
  278. get_immediate_executor() const noexcept
  279. {
  280. return net::get_associated_immediate_executor(
  281. h_, wg1_.get_executor());
  282. }
  283. /** The type of cancellation_slot associated with this object.
  284. If a class derived from @ref async_base is a completion
  285. handler, then the associated cancellation_slot of the
  286. derived class will be this type.
  287. The default type is a filtering cancellation slot,
  288. that only allows terminal cancellation.
  289. */
  290. using cancellation_slot_type =
  291. beast::detail::filtering_cancellation_slot<net::associated_cancellation_slot_t<Handler>>;
  292. /** Returns the cancellation_slot associated with this object.
  293. If a class derived from @ref async_base is a completion
  294. handler, then the object returned from this function will be used
  295. as the associated cancellation_slot of the derived class.
  296. */
  297. cancellation_slot_type
  298. get_cancellation_slot() const noexcept
  299. {
  300. return cancellation_slot_type(act_, net::get_associated_cancellation_slot(h_,
  301. net::cancellation_slot()));
  302. }
  303. /// Set the allowed cancellation types, default is `terminal`.
  304. void set_allowed_cancellation(
  305. net::cancellation_type allowed_cancellation_types = net::cancellation_type::terminal)
  306. {
  307. act_ = allowed_cancellation_types;
  308. }
  309. /// Returns the handler associated with this object
  310. Handler const&
  311. handler() const noexcept
  312. {
  313. return h_;
  314. }
  315. /** Returns ownership of the handler associated with this object
  316. This function is used to transfer ownership of the handler to
  317. the caller, by move-construction. After the move, the only
  318. valid operations on the base object are move construction and
  319. destruction.
  320. */
  321. Handler
  322. release_handler()
  323. {
  324. return std::move(h_);
  325. }
  326. /** Invoke the final completion handler, maybe using post.
  327. This invokes the final completion handler with the specified
  328. arguments forwarded. It is undefined to call either of
  329. @ref bho::beast::async_base::complete or
  330. @ref bho::beast::async_base::complete_now more than once.
  331. Any temporary objects allocated with @ref bho::beast::allocate_stable will
  332. be automatically destroyed before the final completion handler
  333. is invoked.
  334. @param is_continuation If this value is `false`, then the
  335. handler will be submitted to the to the immediate executor using
  336. `net::dispatch`. If the handler has no immediate executor,
  337. this will submit to the executor via `net::post`.
  338. Otherwise the handler will be invoked as if by calling
  339. @ref bho::beast::async_base::complete_now.
  340. @param args A list of optional parameters to invoke the handler
  341. with. The completion handler must be invocable with the parameter
  342. list, or else a compilation error will result.
  343. */
  344. template<class... Args>
  345. void
  346. complete(bool is_continuation, Args&&... args)
  347. {
  348. this->before_invoke_hook();
  349. if(! is_continuation)
  350. {
  351. auto const ex = this->get_immediate_executor();
  352. net::dispatch(
  353. ex,
  354. beast::bind_front_handler(
  355. std::move(h_),
  356. std::forward<Args>(args)...));
  357. wg1_.reset();
  358. }
  359. else
  360. {
  361. wg1_.reset();
  362. h_(std::forward<Args>(args)...);
  363. }
  364. }
  365. /** Invoke the final completion handler.
  366. This invokes the final completion handler with the specified
  367. arguments forwarded. It is undefined to call either of
  368. @ref bho::beast::async_base::complete or @ref bho::beast::async_base::complete_now more than once.
  369. Any temporary objects allocated with @ref bho::beast::allocate_stable will
  370. be automatically destroyed before the final completion handler
  371. is invoked.
  372. @param args A list of optional parameters to invoke the handler
  373. with. The completion handler must be invocable with the parameter
  374. list, or else a compilation error will result.
  375. */
  376. template<class... Args>
  377. void
  378. complete_now(Args&&... args)
  379. {
  380. this->before_invoke_hook();
  381. wg1_.reset();
  382. h_(std::forward<Args>(args)...);
  383. }
  384. #if ! BHO_BEAST_DOXYGEN
  385. Handler*
  386. get_legacy_handler_pointer() noexcept
  387. {
  388. return std::addressof(h_);
  389. }
  390. #endif
  391. };
  392. //------------------------------------------------------------------------------
  393. /** Base class to provide completion handler boilerplate for composed operations.
  394. A function object submitted to intermediate initiating functions during
  395. a composed operation may derive from this type to inherit all of the
  396. boilerplate to forward the executor, allocator, and legacy customization
  397. points associated with the completion handler invoked at the end of the
  398. composed operation.
  399. The composed operation must be typical; that is, associated with one
  400. executor of an I/O object, and invoking a caller-provided completion
  401. handler when the operation is finished. Classes derived from
  402. @ref async_base will acquire these properties:
  403. @li Ownership of the final completion handler provided upon construction.
  404. @li If the final handler has an associated allocator, this allocator will
  405. be propagated to the composed operation subclass. Otherwise, the
  406. associated allocator will be the type specified in the allocator
  407. template parameter, or the default of `std::allocator<void>` if the
  408. parameter is omitted.
  409. @li If the final handler has an associated executor, then it will be used
  410. as the executor associated with the composed operation. Otherwise,
  411. the specified `Executor1` will be the type of executor associated
  412. with the composed operation.
  413. @li An instance of `net::executor_work_guard` for the instance of `Executor1`
  414. shall be maintained until either the final handler is invoked, or the
  415. operation base is destroyed, whichever comes first.
  416. Data members of composed operations implemented as completion handlers
  417. do not have stable addresses, as the composed operation object is move
  418. constructed upon each call to an initiating function. For most operations
  419. this is not a problem. For complex operations requiring stable temporary
  420. storage, the class @ref stable_async_base is provided which offers
  421. additional functionality:
  422. @li The free function @ref beast::allocate_stable may be used to allocate
  423. one or more temporary objects associated with the composed operation.
  424. @li Memory for stable temporary objects is allocated using the allocator
  425. associated with the composed operation.
  426. @li Stable temporary objects are automatically destroyed, and the memory
  427. freed using the associated allocator, either before the final completion
  428. handler is invoked (a Networking requirement) or when the composed operation
  429. is destroyed, whichever occurs first.
  430. @par Example
  431. The following code demonstrates how @ref stable_async_base may be be used to
  432. assist authoring an asynchronous initiating function, by providing all of
  433. the boilerplate to manage the final completion handler in a way that maintains
  434. the allocator and executor associations. Furthermore, the operation shown
  435. allocates temporary memory using @ref beast::allocate_stable for the timer and
  436. message, whose addresses must not change between intermediate operations:
  437. @code
  438. // Asynchronously send a message multiple times, once per second
  439. template <class AsyncWriteStream, class T, class WriteHandler>
  440. auto async_write_messages(
  441. AsyncWriteStream& stream,
  442. T const& message,
  443. std::size_t repeat_count,
  444. WriteHandler&& handler) ->
  445. typename net::async_result<
  446. typename std::decay<WriteHandler>::type,
  447. void(error_code)>::return_type
  448. {
  449. using handler_type = typename net::async_completion<WriteHandler, void(error_code)>::completion_handler_type;
  450. using base_type = stable_async_base<handler_type, typename AsyncWriteStream::executor_type>;
  451. struct op : base_type, asio::coroutine
  452. {
  453. // This object must have a stable address
  454. struct temporary_data
  455. {
  456. // Although std::string is in theory movable, most implementations
  457. // use a "small buffer optimization" which means that we might
  458. // be submitting a buffer to the write operation and then
  459. // moving the string, invalidating the buffer. To prevent
  460. // undefined behavior we store the string object itself at
  461. // a stable location.
  462. std::string const message;
  463. net::steady_timer timer;
  464. temporary_data(std::string message_, net::io_context& ctx)
  465. : message(std::move(message_))
  466. , timer(ctx)
  467. {
  468. }
  469. };
  470. AsyncWriteStream& stream_;
  471. std::size_t repeats_;
  472. temporary_data& data_;
  473. op(AsyncWriteStream& stream, std::size_t repeats, std::string message, handler_type& handler)
  474. : base_type(std::move(handler), stream.get_executor())
  475. , stream_(stream)
  476. , repeats_(repeats)
  477. , data_(allocate_stable<temporary_data>(*this, std::move(message), stream.get_executor().context()))
  478. {
  479. (*this)(); // start the operation
  480. }
  481. // Including this file provides the keywords for macro-based coroutines
  482. #include <asio/yield.hpp>
  483. void operator()(error_code ec = {}, std::size_t = 0)
  484. {
  485. reenter(*this)
  486. {
  487. // If repeats starts at 0 then we must complete immediately. But
  488. // we can't call the final handler from inside the initiating
  489. // function, so we post our intermediate handler first. We use
  490. // net::async_write with an empty buffer instead of calling
  491. // net::post to avoid an extra function template instantiation, to
  492. // keep compile times lower and make the resulting executable smaller.
  493. yield net::async_write(stream_, net::const_buffer{}, std::move(*this));
  494. while(! ec && repeats_-- > 0)
  495. {
  496. // Send the string. We construct a `const_buffer` here to guarantee
  497. // that we do not create an additional function template instantation
  498. // of net::async_write, since we already instantiated it above for
  499. // net::const_buffer.
  500. yield net::async_write(stream_,
  501. net::const_buffer(net::buffer(data_.message)), std::move(*this));
  502. if(ec)
  503. break;
  504. // Set the timer and wait
  505. data_.timer.expires_after(std::chrono::seconds(1));
  506. yield data_.timer.async_wait(std::move(*this));
  507. }
  508. }
  509. // The base class destroys the temporary data automatically,
  510. // before invoking the final completion handler
  511. this->complete_now(ec);
  512. }
  513. // Including this file undefines the macros for the coroutines
  514. #include <asio/unyield.hpp>
  515. };
  516. net::async_completion<WriteHandler, void(error_code)> completion(handler);
  517. std::ostringstream os;
  518. os << message;
  519. op(stream, repeat_count, os.str(), completion.completion_handler);
  520. return completion.result.get();
  521. }
  522. @endcode
  523. @tparam Handler The type of the completion handler to store.
  524. This type must meet the requirements of <em>CompletionHandler</em>.
  525. @tparam Executor1 The type of the executor used when the handler has no
  526. associated executor. An instance of this type must be provided upon
  527. construction. The implementation will maintain an executor work guard
  528. and a copy of this instance.
  529. @tparam Allocator The allocator type to use if the handler does not
  530. have an associated allocator. If this parameter is omitted, then
  531. `std::allocator<void>` will be used. If the specified allocator is
  532. not default constructible, an instance of the type must be provided
  533. upon construction.
  534. @see allocate_stable, async_base
  535. */
  536. template<
  537. class Handler,
  538. class Executor1,
  539. class Allocator = std::allocator<void>
  540. >
  541. class stable_async_base
  542. : public async_base<
  543. Handler, Executor1, Allocator>
  544. {
  545. detail::stable_base* list_ = nullptr;
  546. void
  547. before_invoke_hook() override
  548. {
  549. detail::stable_base::destroy_list(list_);
  550. }
  551. public:
  552. /** Constructor
  553. @param handler The final completion handler.
  554. The type of this object must meet the requirements of <em>CompletionHandler</em>.
  555. The implementation takes ownership of the handler by performing a decay-copy.
  556. @param ex1 The executor associated with the implied I/O object
  557. target of the operation. The implementation shall maintain an
  558. executor work guard for the lifetime of the operation, or until
  559. the final completion handler is invoked, whichever is shorter.
  560. @param alloc The allocator to be associated with objects
  561. derived from this class. If `Allocator` is default-constructible,
  562. this parameter is optional and may be omitted.
  563. */
  564. #if BHO_BEAST_DOXYGEN
  565. template<class Handler>
  566. stable_async_base(
  567. Handler&& handler,
  568. Executor1 const& ex1,
  569. Allocator const& alloc = Allocator());
  570. #else
  571. template<
  572. class Handler_,
  573. class = typename std::enable_if<
  574. ! std::is_same<typename
  575. std::decay<Handler_>::type,
  576. stable_async_base
  577. >::value>::type
  578. >
  579. stable_async_base(
  580. Handler_&& handler,
  581. Executor1 const& ex1)
  582. : async_base<
  583. Handler, Executor1, Allocator>(
  584. std::forward<Handler_>(handler), ex1)
  585. {
  586. }
  587. template<class Handler_>
  588. stable_async_base(
  589. Handler_&& handler,
  590. Executor1 const& ex1,
  591. Allocator const& alloc)
  592. : async_base<
  593. Handler, Executor1, Allocator>(
  594. std::forward<Handler_>(handler), ex1, alloc)
  595. {
  596. }
  597. #endif
  598. /// Move Constructor
  599. stable_async_base(stable_async_base&& other)
  600. : async_base<Handler, Executor1, Allocator>(
  601. std::move(other))
  602. , list_(std::exchange(other.list_, nullptr))
  603. {
  604. }
  605. /** Destructor
  606. If the completion handler was not invoked, then any
  607. state objects allocated with @ref allocate_stable will
  608. be destroyed here.
  609. */
  610. ~stable_async_base()
  611. {
  612. detail::stable_base::destroy_list(list_);
  613. }
  614. /** Allocate a temporary object to hold operation state.
  615. The object will be destroyed just before the completion
  616. handler is invoked, or when the operation base is destroyed.
  617. */
  618. template<
  619. class State,
  620. class Handler_,
  621. class Executor1_,
  622. class Allocator_,
  623. class... Args>
  624. friend
  625. State&
  626. allocate_stable(
  627. stable_async_base<
  628. Handler_, Executor1_, Allocator_>& base,
  629. Args&&... args);
  630. };
  631. /** Allocate a temporary object to hold stable asynchronous operation state.
  632. The object will be destroyed just before the completion
  633. handler is invoked, or when the base is destroyed.
  634. @tparam State The type of object to allocate.
  635. @param base The helper to allocate from.
  636. @param args An optional list of parameters to forward to the
  637. constructor of the object being allocated.
  638. @see stable_async_base
  639. */
  640. template<
  641. class State,
  642. class Handler,
  643. class Executor1,
  644. class Allocator,
  645. class... Args>
  646. State&
  647. allocate_stable(
  648. stable_async_base<
  649. Handler, Executor1, Allocator>& base,
  650. Args&&... args);
  651. } // beast
  652. } // bho
  653. #include <asio2/bho/beast/core/impl/async_base.hpp>
  654. #endif