basic_descriptor.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. //
  2. // posix/basic_descriptor.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_POSIX_BASIC_DESCRIPTOR_HPP
  11. #define BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_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. #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <boost/asio/async_result.hpp>
  19. #include <boost/asio/detail/handler_type_requirements.hpp>
  20. #include <boost/asio/detail/io_object_impl.hpp>
  21. #include <boost/asio/detail/non_const_lvalue.hpp>
  22. #include <boost/asio/detail/reactive_descriptor_service.hpp>
  23. #include <boost/asio/detail/throw_error.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/execution_context.hpp>
  26. #include <boost/asio/executor.hpp>
  27. #include <boost/asio/posix/descriptor_base.hpp>
  28. #if defined(BOOST_ASIO_HAS_MOVE)
  29. # include <utility>
  30. #endif // defined(BOOST_ASIO_HAS_MOVE)
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace posix {
  35. /// Provides POSIX descriptor functionality.
  36. /**
  37. * The posix::basic_descriptor class template provides the ability to wrap a
  38. * POSIX descriptor.
  39. *
  40. * @par Thread Safety
  41. * @e Distinct @e objects: Safe.@n
  42. * @e Shared @e objects: Unsafe.
  43. */
  44. template <typename Executor = executor>
  45. class basic_descriptor
  46. : public descriptor_base
  47. {
  48. public:
  49. /// The type of the executor associated with the object.
  50. typedef Executor executor_type;
  51. /// The native representation of a descriptor.
  52. #if defined(GENERATING_DOCUMENTATION)
  53. typedef implementation_defined native_handle_type;
  54. #else
  55. typedef detail::reactive_descriptor_service::native_handle_type
  56. native_handle_type;
  57. #endif
  58. /// A descriptor is always the lowest layer.
  59. typedef basic_descriptor lowest_layer_type;
  60. /// Construct a descriptor without opening it.
  61. /**
  62. * This constructor creates a descriptor without opening it.
  63. *
  64. * @param ex The I/O executor that the descriptor will use, by default, to
  65. * dispatch handlers for any asynchronous operations performed on the
  66. * descriptor.
  67. */
  68. explicit basic_descriptor(const executor_type& ex)
  69. : impl_(ex)
  70. {
  71. }
  72. /// Construct a descriptor without opening it.
  73. /**
  74. * This constructor creates a descriptor without opening it.
  75. *
  76. * @param context An execution context which provides the I/O executor that
  77. * the descriptor will use, by default, to dispatch handlers for any
  78. * asynchronous operations performed on the descriptor.
  79. */
  80. template <typename ExecutionContext>
  81. explicit basic_descriptor(ExecutionContext& context,
  82. typename enable_if<
  83. is_convertible<ExecutionContext&, execution_context&>::value
  84. >::type* = 0)
  85. : impl_(context)
  86. {
  87. }
  88. /// Construct a descriptor on an existing native descriptor.
  89. /**
  90. * This constructor creates a descriptor object to hold an existing native
  91. * descriptor.
  92. *
  93. * @param ex The I/O executor that the descriptor will use, by default, to
  94. * dispatch handlers for any asynchronous operations performed on the
  95. * descriptor.
  96. *
  97. * @param native_descriptor A native descriptor.
  98. *
  99. * @throws boost::system::system_error Thrown on failure.
  100. */
  101. basic_descriptor(const executor_type& ex,
  102. const native_handle_type& native_descriptor)
  103. : impl_(ex)
  104. {
  105. boost::system::error_code ec;
  106. impl_.get_service().assign(impl_.get_implementation(),
  107. native_descriptor, ec);
  108. boost::asio::detail::throw_error(ec, "assign");
  109. }
  110. /// Construct a descriptor on an existing native descriptor.
  111. /**
  112. * This constructor creates a descriptor object to hold an existing native
  113. * descriptor.
  114. *
  115. * @param context An execution context which provides the I/O executor that
  116. * the descriptor will use, by default, to dispatch handlers for any
  117. * asynchronous operations performed on the descriptor.
  118. *
  119. * @param native_descriptor A native descriptor.
  120. *
  121. * @throws boost::system::system_error Thrown on failure.
  122. */
  123. template <typename ExecutionContext>
  124. basic_descriptor(ExecutionContext& context,
  125. const native_handle_type& native_descriptor,
  126. typename enable_if<
  127. is_convertible<ExecutionContext&, execution_context&>::value
  128. >::type* = 0)
  129. : impl_(context)
  130. {
  131. boost::system::error_code ec;
  132. impl_.get_service().assign(impl_.get_implementation(),
  133. native_descriptor, ec);
  134. boost::asio::detail::throw_error(ec, "assign");
  135. }
  136. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  137. /// Move-construct a descriptor from another.
  138. /**
  139. * This constructor moves a descriptor from one object to another.
  140. *
  141. * @param other The other descriptor object from which the move will
  142. * occur.
  143. *
  144. * @note Following the move, the moved-from object is in the same state as if
  145. * constructed using the @c basic_descriptor(const executor_type&)
  146. * constructor.
  147. */
  148. basic_descriptor(basic_descriptor&& other)
  149. : impl_(std::move(other.impl_))
  150. {
  151. }
  152. /// Move-assign a descriptor from another.
  153. /**
  154. * This assignment operator moves a descriptor from one object to another.
  155. *
  156. * @param other The other descriptor object from which the move will
  157. * occur.
  158. *
  159. * @note Following the move, the moved-from object is in the same state as if
  160. * constructed using the @c basic_descriptor(const executor_type&)
  161. * constructor.
  162. */
  163. basic_descriptor& operator=(basic_descriptor&& other)
  164. {
  165. impl_ = std::move(other.impl_);
  166. return *this;
  167. }
  168. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  169. /// Get the executor associated with the object.
  170. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  171. {
  172. return impl_.get_executor();
  173. }
  174. /// Get a reference to the lowest layer.
  175. /**
  176. * This function returns a reference to the lowest layer in a stack of
  177. * layers. Since a descriptor cannot contain any further layers, it
  178. * simply returns a reference to itself.
  179. *
  180. * @return A reference to the lowest layer in the stack of layers. Ownership
  181. * is not transferred to the caller.
  182. */
  183. lowest_layer_type& lowest_layer()
  184. {
  185. return *this;
  186. }
  187. /// Get a const reference to the lowest layer.
  188. /**
  189. * This function returns a const reference to the lowest layer in a stack of
  190. * layers. Since a descriptor cannot contain any further layers, it
  191. * simply returns a reference to itself.
  192. *
  193. * @return A const reference to the lowest layer in the stack of layers.
  194. * Ownership is not transferred to the caller.
  195. */
  196. const lowest_layer_type& lowest_layer() const
  197. {
  198. return *this;
  199. }
  200. /// Assign an existing native descriptor to the descriptor.
  201. /*
  202. * This function opens the descriptor to hold an existing native descriptor.
  203. *
  204. * @param native_descriptor A native descriptor.
  205. *
  206. * @throws boost::system::system_error Thrown on failure.
  207. */
  208. void assign(const native_handle_type& native_descriptor)
  209. {
  210. boost::system::error_code ec;
  211. impl_.get_service().assign(impl_.get_implementation(),
  212. native_descriptor, ec);
  213. boost::asio::detail::throw_error(ec, "assign");
  214. }
  215. /// Assign an existing native descriptor to the descriptor.
  216. /*
  217. * This function opens the descriptor to hold an existing native descriptor.
  218. *
  219. * @param native_descriptor A native descriptor.
  220. *
  221. * @param ec Set to indicate what error occurred, if any.
  222. */
  223. BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& native_descriptor,
  224. boost::system::error_code& ec)
  225. {
  226. impl_.get_service().assign(
  227. impl_.get_implementation(), native_descriptor, ec);
  228. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  229. }
  230. /// Determine whether the descriptor is open.
  231. bool is_open() const
  232. {
  233. return impl_.get_service().is_open(impl_.get_implementation());
  234. }
  235. /// Close the descriptor.
  236. /**
  237. * This function is used to close the descriptor. Any asynchronous read or
  238. * write operations will be cancelled immediately, and will complete with the
  239. * boost::asio::error::operation_aborted error.
  240. *
  241. * @throws boost::system::system_error Thrown on failure. Note that, even if
  242. * the function indicates an error, the underlying descriptor is closed.
  243. */
  244. void close()
  245. {
  246. boost::system::error_code ec;
  247. impl_.get_service().close(impl_.get_implementation(), ec);
  248. boost::asio::detail::throw_error(ec, "close");
  249. }
  250. /// Close the descriptor.
  251. /**
  252. * This function is used to close the descriptor. Any asynchronous read or
  253. * write operations will be cancelled immediately, and will complete with the
  254. * boost::asio::error::operation_aborted error.
  255. *
  256. * @param ec Set to indicate what error occurred, if any. Note that, even if
  257. * the function indicates an error, the underlying descriptor is closed.
  258. */
  259. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  260. {
  261. impl_.get_service().close(impl_.get_implementation(), ec);
  262. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  263. }
  264. /// Get the native descriptor representation.
  265. /**
  266. * This function may be used to obtain the underlying representation of the
  267. * descriptor. This is intended to allow access to native descriptor
  268. * functionality that is not otherwise provided.
  269. */
  270. native_handle_type native_handle()
  271. {
  272. return impl_.get_service().native_handle(impl_.get_implementation());
  273. }
  274. /// Release ownership of the native descriptor implementation.
  275. /**
  276. * This function may be used to obtain the underlying representation of the
  277. * descriptor. After calling this function, @c is_open() returns false. The
  278. * caller is responsible for closing the descriptor.
  279. *
  280. * All outstanding asynchronous read or write operations will finish
  281. * immediately, and the handlers for cancelled operations will be passed the
  282. * boost::asio::error::operation_aborted error.
  283. */
  284. native_handle_type release()
  285. {
  286. return impl_.get_service().release(impl_.get_implementation());
  287. }
  288. /// Cancel all asynchronous operations associated with the descriptor.
  289. /**
  290. * This function causes all outstanding asynchronous read or write operations
  291. * to finish immediately, and the handlers for cancelled operations will be
  292. * passed the boost::asio::error::operation_aborted error.
  293. *
  294. * @throws boost::system::system_error Thrown on failure.
  295. */
  296. void cancel()
  297. {
  298. boost::system::error_code ec;
  299. impl_.get_service().cancel(impl_.get_implementation(), ec);
  300. boost::asio::detail::throw_error(ec, "cancel");
  301. }
  302. /// Cancel all asynchronous operations associated with the descriptor.
  303. /**
  304. * This function causes all outstanding asynchronous read or write operations
  305. * to finish immediately, and the handlers for cancelled operations will be
  306. * passed the boost::asio::error::operation_aborted error.
  307. *
  308. * @param ec Set to indicate what error occurred, if any.
  309. */
  310. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  311. {
  312. impl_.get_service().cancel(impl_.get_implementation(), ec);
  313. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  314. }
  315. /// Perform an IO control command on the descriptor.
  316. /**
  317. * This function is used to execute an IO control command on the descriptor.
  318. *
  319. * @param command The IO control command to be performed on the descriptor.
  320. *
  321. * @throws boost::system::system_error Thrown on failure.
  322. *
  323. * @sa IoControlCommand @n
  324. * boost::asio::posix::descriptor_base::bytes_readable @n
  325. * boost::asio::posix::descriptor_base::non_blocking_io
  326. *
  327. * @par Example
  328. * Getting the number of bytes ready to read:
  329. * @code
  330. * boost::asio::posix::stream_descriptor descriptor(my_context);
  331. * ...
  332. * boost::asio::posix::stream_descriptor::bytes_readable command;
  333. * descriptor.io_control(command);
  334. * std::size_t bytes_readable = command.get();
  335. * @endcode
  336. */
  337. template <typename IoControlCommand>
  338. void io_control(IoControlCommand& command)
  339. {
  340. boost::system::error_code ec;
  341. impl_.get_service().io_control(impl_.get_implementation(), command, ec);
  342. boost::asio::detail::throw_error(ec, "io_control");
  343. }
  344. /// Perform an IO control command on the descriptor.
  345. /**
  346. * This function is used to execute an IO control command on the descriptor.
  347. *
  348. * @param command The IO control command to be performed on the descriptor.
  349. *
  350. * @param ec Set to indicate what error occurred, if any.
  351. *
  352. * @sa IoControlCommand @n
  353. * boost::asio::posix::descriptor_base::bytes_readable @n
  354. * boost::asio::posix::descriptor_base::non_blocking_io
  355. *
  356. * @par Example
  357. * Getting the number of bytes ready to read:
  358. * @code
  359. * boost::asio::posix::stream_descriptor descriptor(my_context);
  360. * ...
  361. * boost::asio::posix::stream_descriptor::bytes_readable command;
  362. * boost::system::error_code ec;
  363. * descriptor.io_control(command, ec);
  364. * if (ec)
  365. * {
  366. * // An error occurred.
  367. * }
  368. * std::size_t bytes_readable = command.get();
  369. * @endcode
  370. */
  371. template <typename IoControlCommand>
  372. BOOST_ASIO_SYNC_OP_VOID io_control(IoControlCommand& command,
  373. boost::system::error_code& ec)
  374. {
  375. impl_.get_service().io_control(impl_.get_implementation(), command, ec);
  376. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  377. }
  378. /// Gets the non-blocking mode of the descriptor.
  379. /**
  380. * @returns @c true if the descriptor's synchronous operations will fail with
  381. * boost::asio::error::would_block if they are unable to perform the requested
  382. * operation immediately. If @c false, synchronous operations will block
  383. * until complete.
  384. *
  385. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  386. * operations. Asynchronous operations will never fail with the error
  387. * boost::asio::error::would_block.
  388. */
  389. bool non_blocking() const
  390. {
  391. return impl_.get_service().non_blocking(impl_.get_implementation());
  392. }
  393. /// Sets the non-blocking mode of the descriptor.
  394. /**
  395. * @param mode If @c true, the descriptor's synchronous operations will fail
  396. * with boost::asio::error::would_block if they are unable to perform the
  397. * requested operation immediately. If @c false, synchronous operations will
  398. * block until complete.
  399. *
  400. * @throws boost::system::system_error Thrown on failure.
  401. *
  402. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  403. * operations. Asynchronous operations will never fail with the error
  404. * boost::asio::error::would_block.
  405. */
  406. void non_blocking(bool mode)
  407. {
  408. boost::system::error_code ec;
  409. impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
  410. boost::asio::detail::throw_error(ec, "non_blocking");
  411. }
  412. /// Sets the non-blocking mode of the descriptor.
  413. /**
  414. * @param mode If @c true, the descriptor's synchronous operations will fail
  415. * with boost::asio::error::would_block if they are unable to perform the
  416. * requested operation immediately. If @c false, synchronous operations will
  417. * block until complete.
  418. *
  419. * @param ec Set to indicate what error occurred, if any.
  420. *
  421. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  422. * operations. Asynchronous operations will never fail with the error
  423. * boost::asio::error::would_block.
  424. */
  425. BOOST_ASIO_SYNC_OP_VOID non_blocking(
  426. bool mode, boost::system::error_code& ec)
  427. {
  428. impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
  429. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  430. }
  431. /// Gets the non-blocking mode of the native descriptor implementation.
  432. /**
  433. * This function is used to retrieve the non-blocking mode of the underlying
  434. * native descriptor. This mode has no effect on the behaviour of the
  435. * descriptor object's synchronous operations.
  436. *
  437. * @returns @c true if the underlying descriptor is in non-blocking mode and
  438. * direct system calls may fail with boost::asio::error::would_block (or the
  439. * equivalent system error).
  440. *
  441. * @note The current non-blocking mode is cached by the descriptor object.
  442. * Consequently, the return value may be incorrect if the non-blocking mode
  443. * was set directly on the native descriptor.
  444. */
  445. bool native_non_blocking() const
  446. {
  447. return impl_.get_service().native_non_blocking(
  448. impl_.get_implementation());
  449. }
  450. /// Sets the non-blocking mode of the native descriptor implementation.
  451. /**
  452. * This function is used to modify the non-blocking mode of the underlying
  453. * native descriptor. It has no effect on the behaviour of the descriptor
  454. * object's synchronous operations.
  455. *
  456. * @param mode If @c true, the underlying descriptor is put into non-blocking
  457. * mode and direct system calls may fail with boost::asio::error::would_block
  458. * (or the equivalent system error).
  459. *
  460. * @throws boost::system::system_error Thrown on failure. If the @c mode is
  461. * @c false, but the current value of @c non_blocking() is @c true, this
  462. * function fails with boost::asio::error::invalid_argument, as the
  463. * combination does not make sense.
  464. */
  465. void native_non_blocking(bool mode)
  466. {
  467. boost::system::error_code ec;
  468. impl_.get_service().native_non_blocking(
  469. impl_.get_implementation(), mode, ec);
  470. boost::asio::detail::throw_error(ec, "native_non_blocking");
  471. }
  472. /// Sets the non-blocking mode of the native descriptor implementation.
  473. /**
  474. * This function is used to modify the non-blocking mode of the underlying
  475. * native descriptor. It has no effect on the behaviour of the descriptor
  476. * object's synchronous operations.
  477. *
  478. * @param mode If @c true, the underlying descriptor is put into non-blocking
  479. * mode and direct system calls may fail with boost::asio::error::would_block
  480. * (or the equivalent system error).
  481. *
  482. * @param ec Set to indicate what error occurred, if any. If the @c mode is
  483. * @c false, but the current value of @c non_blocking() is @c true, this
  484. * function fails with boost::asio::error::invalid_argument, as the
  485. * combination does not make sense.
  486. */
  487. BOOST_ASIO_SYNC_OP_VOID native_non_blocking(
  488. bool mode, boost::system::error_code& ec)
  489. {
  490. impl_.get_service().native_non_blocking(
  491. impl_.get_implementation(), mode, ec);
  492. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  493. }
  494. /// Wait for the descriptor to become ready to read, ready to write, or to
  495. /// have pending error conditions.
  496. /**
  497. * This function is used to perform a blocking wait for a descriptor to enter
  498. * a ready to read, write or error condition state.
  499. *
  500. * @param w Specifies the desired descriptor state.
  501. *
  502. * @par Example
  503. * Waiting for a descriptor to become readable.
  504. * @code
  505. * boost::asio::posix::stream_descriptor descriptor(my_context);
  506. * ...
  507. * descriptor.wait(boost::asio::posix::stream_descriptor::wait_read);
  508. * @endcode
  509. */
  510. void wait(wait_type w)
  511. {
  512. boost::system::error_code ec;
  513. impl_.get_service().wait(impl_.get_implementation(), w, ec);
  514. boost::asio::detail::throw_error(ec, "wait");
  515. }
  516. /// Wait for the descriptor to become ready to read, ready to write, or to
  517. /// have pending error conditions.
  518. /**
  519. * This function is used to perform a blocking wait for a descriptor to enter
  520. * a ready to read, write or error condition state.
  521. *
  522. * @param w Specifies the desired descriptor state.
  523. *
  524. * @param ec Set to indicate what error occurred, if any.
  525. *
  526. * @par Example
  527. * Waiting for a descriptor to become readable.
  528. * @code
  529. * boost::asio::posix::stream_descriptor descriptor(my_context);
  530. * ...
  531. * boost::system::error_code ec;
  532. * descriptor.wait(boost::asio::posix::stream_descriptor::wait_read, ec);
  533. * @endcode
  534. */
  535. BOOST_ASIO_SYNC_OP_VOID wait(wait_type w, boost::system::error_code& ec)
  536. {
  537. impl_.get_service().wait(impl_.get_implementation(), w, ec);
  538. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  539. }
  540. /// Asynchronously wait for the descriptor to become ready to read, ready to
  541. /// write, or to have pending error conditions.
  542. /**
  543. * This function is used to perform an asynchronous wait for a descriptor to
  544. * enter a ready to read, write or error condition state.
  545. *
  546. * @param w Specifies the desired descriptor state.
  547. *
  548. * @param handler The handler to be called when the wait operation completes.
  549. * Copies will be made of the handler as required. The function signature of
  550. * the handler must be:
  551. * @code void handler(
  552. * const boost::system::error_code& error // Result of operation
  553. * ); @endcode
  554. * Regardless of whether the asynchronous operation completes immediately or
  555. * not, the handler will not be invoked from within this function. On
  556. * immediate completion, invocation of the handler will be performed in a
  557. * manner equivalent to using boost::asio::post().
  558. *
  559. * @par Example
  560. * @code
  561. * void wait_handler(const boost::system::error_code& error)
  562. * {
  563. * if (!error)
  564. * {
  565. * // Wait succeeded.
  566. * }
  567. * }
  568. *
  569. * ...
  570. *
  571. * boost::asio::posix::stream_descriptor descriptor(my_context);
  572. * ...
  573. * descriptor.async_wait(
  574. * boost::asio::posix::stream_descriptor::wait_read,
  575. * wait_handler);
  576. * @endcode
  577. */
  578. template <typename WaitHandler>
  579. BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
  580. void (boost::system::error_code))
  581. async_wait(wait_type w, BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
  582. {
  583. return async_initiate<WaitHandler, void (boost::system::error_code)>(
  584. initiate_async_wait(), handler, this, w);
  585. }
  586. protected:
  587. /// Protected destructor to prevent deletion through this type.
  588. /**
  589. * This function destroys the descriptor, cancelling any outstanding
  590. * asynchronous wait operations associated with the descriptor as if by
  591. * calling @c cancel.
  592. */
  593. ~basic_descriptor()
  594. {
  595. }
  596. detail::io_object_impl<detail::reactive_descriptor_service, Executor> impl_;
  597. private:
  598. // Disallow copying and assignment.
  599. basic_descriptor(const basic_descriptor&) BOOST_ASIO_DELETED;
  600. basic_descriptor& operator=(const basic_descriptor&) BOOST_ASIO_DELETED;
  601. struct initiate_async_wait
  602. {
  603. template <typename WaitHandler>
  604. void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler,
  605. basic_descriptor* self, wait_type w) const
  606. {
  607. // If you get an error on the following line it means that your handler
  608. // does not meet the documented type requirements for a WaitHandler.
  609. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  610. detail::non_const_lvalue<WaitHandler> handler2(handler);
  611. self->impl_.get_service().async_wait(
  612. self->impl_.get_implementation(), w, handler2.value,
  613. self->impl_.get_implementation_executor());
  614. }
  615. };
  616. };
  617. } // namespace posix
  618. } // namespace asio
  619. } // namespace boost
  620. #include <boost/asio/detail/pop_options.hpp>
  621. #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  622. // || defined(GENERATING_DOCUMENTATION)
  623. #endif // BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_HPP