async_base.hpp 28 KB

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