bind_cancellation_slot.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. //
  2. // bind_cancellation_slot.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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_BIND_CANCELLATION_SLOT_HPP
  11. #define BOOST_ASIO_BIND_CANCELLATION_SLOT_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 <boost/asio/associated_cancellation_slot.hpp>
  17. #include <boost/asio/associated_executor.hpp>
  18. #include <boost/asio/associator.hpp>
  19. #include <boost/asio/async_result.hpp>
  20. #include <boost/asio/detail/initiation_base.hpp>
  21. #include <boost/asio/detail/type_traits.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. // Helper to automatically define nested typedef result_type.
  27. template <typename T, typename = void>
  28. struct cancellation_slot_binder_result_type
  29. {
  30. protected:
  31. typedef void result_type_or_void;
  32. };
  33. template <typename T>
  34. struct cancellation_slot_binder_result_type<T, void_t<typename T::result_type>>
  35. {
  36. typedef typename T::result_type result_type;
  37. protected:
  38. typedef result_type result_type_or_void;
  39. };
  40. template <typename R>
  41. struct cancellation_slot_binder_result_type<R(*)()>
  42. {
  43. typedef R result_type;
  44. protected:
  45. typedef result_type result_type_or_void;
  46. };
  47. template <typename R>
  48. struct cancellation_slot_binder_result_type<R(&)()>
  49. {
  50. typedef R result_type;
  51. protected:
  52. typedef result_type result_type_or_void;
  53. };
  54. template <typename R, typename A1>
  55. struct cancellation_slot_binder_result_type<R(*)(A1)>
  56. {
  57. typedef R result_type;
  58. protected:
  59. typedef result_type result_type_or_void;
  60. };
  61. template <typename R, typename A1>
  62. struct cancellation_slot_binder_result_type<R(&)(A1)>
  63. {
  64. typedef R result_type;
  65. protected:
  66. typedef result_type result_type_or_void;
  67. };
  68. template <typename R, typename A1, typename A2>
  69. struct cancellation_slot_binder_result_type<R(*)(A1, A2)>
  70. {
  71. typedef R result_type;
  72. protected:
  73. typedef result_type result_type_or_void;
  74. };
  75. template <typename R, typename A1, typename A2>
  76. struct cancellation_slot_binder_result_type<R(&)(A1, A2)>
  77. {
  78. typedef R result_type;
  79. protected:
  80. typedef result_type result_type_or_void;
  81. };
  82. // Helper to automatically define nested typedef argument_type.
  83. template <typename T, typename = void>
  84. struct cancellation_slot_binder_argument_type {};
  85. template <typename T>
  86. struct cancellation_slot_binder_argument_type<T,
  87. void_t<typename T::argument_type>>
  88. {
  89. typedef typename T::argument_type argument_type;
  90. };
  91. template <typename R, typename A1>
  92. struct cancellation_slot_binder_argument_type<R(*)(A1)>
  93. {
  94. typedef A1 argument_type;
  95. };
  96. template <typename R, typename A1>
  97. struct cancellation_slot_binder_argument_type<R(&)(A1)>
  98. {
  99. typedef A1 argument_type;
  100. };
  101. // Helper to automatically define nested typedefs first_argument_type and
  102. // second_argument_type.
  103. template <typename T, typename = void>
  104. struct cancellation_slot_binder_argument_types {};
  105. template <typename T>
  106. struct cancellation_slot_binder_argument_types<T,
  107. void_t<typename T::first_argument_type>>
  108. {
  109. typedef typename T::first_argument_type first_argument_type;
  110. typedef typename T::second_argument_type second_argument_type;
  111. };
  112. template <typename R, typename A1, typename A2>
  113. struct cancellation_slot_binder_argument_type<R(*)(A1, A2)>
  114. {
  115. typedef A1 first_argument_type;
  116. typedef A2 second_argument_type;
  117. };
  118. template <typename R, typename A1, typename A2>
  119. struct cancellation_slot_binder_argument_type<R(&)(A1, A2)>
  120. {
  121. typedef A1 first_argument_type;
  122. typedef A2 second_argument_type;
  123. };
  124. } // namespace detail
  125. /// A call wrapper type to bind a cancellation slot of type @c CancellationSlot
  126. /// to an object of type @c T.
  127. template <typename T, typename CancellationSlot>
  128. class cancellation_slot_binder
  129. #if !defined(GENERATING_DOCUMENTATION)
  130. : public detail::cancellation_slot_binder_result_type<T>,
  131. public detail::cancellation_slot_binder_argument_type<T>,
  132. public detail::cancellation_slot_binder_argument_types<T>
  133. #endif // !defined(GENERATING_DOCUMENTATION)
  134. {
  135. public:
  136. /// The type of the target object.
  137. typedef T target_type;
  138. /// The type of the associated cancellation slot.
  139. typedef CancellationSlot cancellation_slot_type;
  140. #if defined(GENERATING_DOCUMENTATION)
  141. /// The return type if a function.
  142. /**
  143. * The type of @c result_type is based on the type @c T of the wrapper's
  144. * target object:
  145. *
  146. * @li if @c T is a pointer to function type, @c result_type is a synonym for
  147. * the return type of @c T;
  148. *
  149. * @li if @c T is a class type with a member type @c result_type, then @c
  150. * result_type is a synonym for @c T::result_type;
  151. *
  152. * @li otherwise @c result_type is not defined.
  153. */
  154. typedef see_below result_type;
  155. /// The type of the function's argument.
  156. /**
  157. * The type of @c argument_type is based on the type @c T of the wrapper's
  158. * target object:
  159. *
  160. * @li if @c T is a pointer to a function type accepting a single argument,
  161. * @c argument_type is a synonym for the return type of @c T;
  162. *
  163. * @li if @c T is a class type with a member type @c argument_type, then @c
  164. * argument_type is a synonym for @c T::argument_type;
  165. *
  166. * @li otherwise @c argument_type is not defined.
  167. */
  168. typedef see_below argument_type;
  169. /// The type of the function's first argument.
  170. /**
  171. * The type of @c first_argument_type is based on the type @c T of the
  172. * wrapper's target object:
  173. *
  174. * @li if @c T is a pointer to a function type accepting two arguments, @c
  175. * first_argument_type is a synonym for the return type of @c T;
  176. *
  177. * @li if @c T is a class type with a member type @c first_argument_type,
  178. * then @c first_argument_type is a synonym for @c T::first_argument_type;
  179. *
  180. * @li otherwise @c first_argument_type is not defined.
  181. */
  182. typedef see_below first_argument_type;
  183. /// The type of the function's second argument.
  184. /**
  185. * The type of @c second_argument_type is based on the type @c T of the
  186. * wrapper's target object:
  187. *
  188. * @li if @c T is a pointer to a function type accepting two arguments, @c
  189. * second_argument_type is a synonym for the return type of @c T;
  190. *
  191. * @li if @c T is a class type with a member type @c first_argument_type,
  192. * then @c second_argument_type is a synonym for @c T::second_argument_type;
  193. *
  194. * @li otherwise @c second_argument_type is not defined.
  195. */
  196. typedef see_below second_argument_type;
  197. #endif // defined(GENERATING_DOCUMENTATION)
  198. /// Construct a cancellation slot wrapper for the specified object.
  199. /**
  200. * This constructor is only valid if the type @c T is constructible from type
  201. * @c U.
  202. */
  203. template <typename U>
  204. cancellation_slot_binder(const cancellation_slot_type& s, U&& u)
  205. : slot_(s),
  206. target_(static_cast<U&&>(u))
  207. {
  208. }
  209. /// Copy constructor.
  210. cancellation_slot_binder(const cancellation_slot_binder& other)
  211. : slot_(other.get_cancellation_slot()),
  212. target_(other.get())
  213. {
  214. }
  215. /// Construct a copy, but specify a different cancellation slot.
  216. cancellation_slot_binder(const cancellation_slot_type& s,
  217. const cancellation_slot_binder& other)
  218. : slot_(s),
  219. target_(other.get())
  220. {
  221. }
  222. /// Construct a copy of a different cancellation slot wrapper type.
  223. /**
  224. * This constructor is only valid if the @c CancellationSlot type is
  225. * constructible from type @c OtherCancellationSlot, and the type @c T is
  226. * constructible from type @c U.
  227. */
  228. template <typename U, typename OtherCancellationSlot>
  229. cancellation_slot_binder(
  230. const cancellation_slot_binder<U, OtherCancellationSlot>& other,
  231. constraint_t<is_constructible<CancellationSlot,
  232. OtherCancellationSlot>::value> = 0,
  233. constraint_t<is_constructible<T, U>::value> = 0)
  234. : slot_(other.get_cancellation_slot()),
  235. target_(other.get())
  236. {
  237. }
  238. /// Construct a copy of a different cancellation slot wrapper type, but
  239. /// specify a different cancellation slot.
  240. /**
  241. * This constructor is only valid if the type @c T is constructible from type
  242. * @c U.
  243. */
  244. template <typename U, typename OtherCancellationSlot>
  245. cancellation_slot_binder(const cancellation_slot_type& s,
  246. const cancellation_slot_binder<U, OtherCancellationSlot>& other,
  247. constraint_t<is_constructible<T, U>::value> = 0)
  248. : slot_(s),
  249. target_(other.get())
  250. {
  251. }
  252. /// Move constructor.
  253. cancellation_slot_binder(cancellation_slot_binder&& other)
  254. : slot_(static_cast<cancellation_slot_type&&>(
  255. other.get_cancellation_slot())),
  256. target_(static_cast<T&&>(other.get()))
  257. {
  258. }
  259. /// Move construct the target object, but specify a different cancellation
  260. /// slot.
  261. cancellation_slot_binder(const cancellation_slot_type& s,
  262. cancellation_slot_binder&& other)
  263. : slot_(s),
  264. target_(static_cast<T&&>(other.get()))
  265. {
  266. }
  267. /// Move construct from a different cancellation slot wrapper type.
  268. template <typename U, typename OtherCancellationSlot>
  269. cancellation_slot_binder(
  270. cancellation_slot_binder<U, OtherCancellationSlot>&& other,
  271. constraint_t<is_constructible<CancellationSlot,
  272. OtherCancellationSlot>::value> = 0,
  273. constraint_t<is_constructible<T, U>::value> = 0)
  274. : slot_(static_cast<OtherCancellationSlot&&>(
  275. other.get_cancellation_slot())),
  276. target_(static_cast<U&&>(other.get()))
  277. {
  278. }
  279. /// Move construct from a different cancellation slot wrapper type, but
  280. /// specify a different cancellation slot.
  281. template <typename U, typename OtherCancellationSlot>
  282. cancellation_slot_binder(const cancellation_slot_type& s,
  283. cancellation_slot_binder<U, OtherCancellationSlot>&& other,
  284. constraint_t<is_constructible<T, U>::value> = 0)
  285. : slot_(s),
  286. target_(static_cast<U&&>(other.get()))
  287. {
  288. }
  289. /// Destructor.
  290. ~cancellation_slot_binder()
  291. {
  292. }
  293. /// Obtain a reference to the target object.
  294. target_type& get() noexcept
  295. {
  296. return target_;
  297. }
  298. /// Obtain a reference to the target object.
  299. const target_type& get() const noexcept
  300. {
  301. return target_;
  302. }
  303. /// Obtain the associated cancellation slot.
  304. cancellation_slot_type get_cancellation_slot() const noexcept
  305. {
  306. return slot_;
  307. }
  308. /// Forwarding function call operator.
  309. template <typename... Args>
  310. result_of_t<T(Args...)> operator()(Args&&... args) &
  311. {
  312. return target_(static_cast<Args&&>(args)...);
  313. }
  314. /// Forwarding function call operator.
  315. template <typename... Args>
  316. result_of_t<T(Args...)> operator()(Args&&... args) &&
  317. {
  318. return static_cast<T&&>(target_)(static_cast<Args&&>(args)...);
  319. }
  320. /// Forwarding function call operator.
  321. template <typename... Args>
  322. result_of_t<T(Args...)> operator()(Args&&... args) const&
  323. {
  324. return target_(static_cast<Args&&>(args)...);
  325. }
  326. private:
  327. CancellationSlot slot_;
  328. T target_;
  329. };
  330. /// A function object type that adapts a @ref completion_token to specify that
  331. /// the completion handler should have the supplied cancellation slot as its
  332. /// associated cancellation slot.
  333. /**
  334. * May also be used directly as a completion token, in which case it adapts the
  335. * asynchronous operation's default completion token (or boost::asio::deferred
  336. * if no default is available).
  337. */
  338. template <typename CancellationSlot>
  339. struct partial_cancellation_slot_binder
  340. {
  341. /// Constructor that specifies associated cancellation slot.
  342. explicit partial_cancellation_slot_binder(const CancellationSlot& ex)
  343. : cancellation_slot_(ex)
  344. {
  345. }
  346. /// Adapt a @ref completion_token to specify that the completion handler
  347. /// should have the cancellation slot as its associated cancellation slot.
  348. template <typename CompletionToken>
  349. BOOST_ASIO_NODISCARD inline
  350. constexpr cancellation_slot_binder<decay_t<CompletionToken>, CancellationSlot>
  351. operator()(CompletionToken&& completion_token) const
  352. {
  353. return cancellation_slot_binder<decay_t<CompletionToken>, CancellationSlot>(
  354. static_cast<CompletionToken&&>(completion_token), cancellation_slot_);
  355. }
  356. //private:
  357. CancellationSlot cancellation_slot_;
  358. };
  359. /// Create a partial completion token that associates a cancellation slot.
  360. template <typename CancellationSlot>
  361. BOOST_ASIO_NODISCARD inline partial_cancellation_slot_binder<CancellationSlot>
  362. bind_cancellation_slot(const CancellationSlot& ex)
  363. {
  364. return partial_cancellation_slot_binder<CancellationSlot>(ex);
  365. }
  366. /// Associate an object of type @c T with a cancellation slot of type
  367. /// @c CancellationSlot.
  368. template <typename CancellationSlot, typename T>
  369. BOOST_ASIO_NODISCARD inline
  370. cancellation_slot_binder<decay_t<T>, CancellationSlot>
  371. bind_cancellation_slot(const CancellationSlot& s, T&& t)
  372. {
  373. return cancellation_slot_binder<decay_t<T>, CancellationSlot>(
  374. s, static_cast<T&&>(t));
  375. }
  376. #if !defined(GENERATING_DOCUMENTATION)
  377. namespace detail {
  378. template <typename TargetAsyncResult,
  379. typename CancellationSlot, typename = void>
  380. class cancellation_slot_binder_completion_handler_async_result
  381. {
  382. public:
  383. template <typename T>
  384. explicit cancellation_slot_binder_completion_handler_async_result(T&)
  385. {
  386. }
  387. };
  388. template <typename TargetAsyncResult, typename CancellationSlot>
  389. class cancellation_slot_binder_completion_handler_async_result<
  390. TargetAsyncResult, CancellationSlot,
  391. void_t<typename TargetAsyncResult::completion_handler_type>>
  392. {
  393. private:
  394. TargetAsyncResult target_;
  395. public:
  396. typedef cancellation_slot_binder<
  397. typename TargetAsyncResult::completion_handler_type, CancellationSlot>
  398. completion_handler_type;
  399. explicit cancellation_slot_binder_completion_handler_async_result(
  400. typename TargetAsyncResult::completion_handler_type& handler)
  401. : target_(handler)
  402. {
  403. }
  404. auto get() -> decltype(target_.get())
  405. {
  406. return target_.get();
  407. }
  408. };
  409. template <typename TargetAsyncResult, typename = void>
  410. struct cancellation_slot_binder_async_result_return_type
  411. {
  412. };
  413. template <typename TargetAsyncResult>
  414. struct cancellation_slot_binder_async_result_return_type<
  415. TargetAsyncResult, void_t<typename TargetAsyncResult::return_type>>
  416. {
  417. typedef typename TargetAsyncResult::return_type return_type;
  418. };
  419. } // namespace detail
  420. template <typename T, typename CancellationSlot, typename Signature>
  421. class async_result<cancellation_slot_binder<T, CancellationSlot>, Signature> :
  422. public detail::cancellation_slot_binder_completion_handler_async_result<
  423. async_result<T, Signature>, CancellationSlot>,
  424. public detail::cancellation_slot_binder_async_result_return_type<
  425. async_result<T, Signature>>
  426. {
  427. public:
  428. explicit async_result(cancellation_slot_binder<T, CancellationSlot>& b)
  429. : detail::cancellation_slot_binder_completion_handler_async_result<
  430. async_result<T, Signature>, CancellationSlot>(b.get())
  431. {
  432. }
  433. template <typename Initiation>
  434. struct init_wrapper : detail::initiation_base<Initiation>
  435. {
  436. using detail::initiation_base<Initiation>::initiation_base;
  437. template <typename Handler, typename... Args>
  438. void operator()(Handler&& handler,
  439. const CancellationSlot& slot, Args&&... args) &&
  440. {
  441. static_cast<Initiation&&>(*this)(
  442. cancellation_slot_binder<decay_t<Handler>, CancellationSlot>(
  443. slot, static_cast<Handler&&>(handler)),
  444. static_cast<Args&&>(args)...);
  445. }
  446. template <typename Handler, typename... Args>
  447. void operator()(Handler&& handler,
  448. const CancellationSlot& slot, Args&&... args) const &
  449. {
  450. static_cast<const Initiation&>(*this)(
  451. cancellation_slot_binder<decay_t<Handler>, CancellationSlot>(
  452. slot, static_cast<Handler&&>(handler)),
  453. static_cast<Args&&>(args)...);
  454. }
  455. };
  456. template <typename Initiation, typename RawCompletionToken, typename... Args>
  457. static auto initiate(Initiation&& initiation,
  458. RawCompletionToken&& token, Args&&... args)
  459. -> decltype(
  460. async_initiate<
  461. conditional_t<
  462. is_const<remove_reference_t<RawCompletionToken>>::value, const T, T>,
  463. Signature>(
  464. declval<init_wrapper<decay_t<Initiation>>>(),
  465. token.get(), token.get_cancellation_slot(),
  466. static_cast<Args&&>(args)...))
  467. {
  468. return async_initiate<
  469. conditional_t<
  470. is_const<remove_reference_t<RawCompletionToken>>::value, const T, T>,
  471. Signature>(
  472. init_wrapper<decay_t<Initiation>>(
  473. static_cast<Initiation&&>(initiation)),
  474. token.get(), token.get_cancellation_slot(),
  475. static_cast<Args&&>(args)...);
  476. }
  477. private:
  478. async_result(const async_result&) = delete;
  479. async_result& operator=(const async_result&) = delete;
  480. async_result<T, Signature> target_;
  481. };
  482. template <typename CancellationSlot, typename... Signatures>
  483. struct async_result<partial_cancellation_slot_binder<CancellationSlot>,
  484. Signatures...>
  485. {
  486. template <typename Initiation, typename RawCompletionToken, typename... Args>
  487. static auto initiate(Initiation&& initiation,
  488. RawCompletionToken&& token, Args&&... args)
  489. -> decltype(
  490. async_initiate<Signatures...>(
  491. static_cast<Initiation&&>(initiation),
  492. cancellation_slot_binder<
  493. default_completion_token_t<associated_executor_t<Initiation>>,
  494. CancellationSlot>(token.cancellation_slot_,
  495. default_completion_token_t<associated_executor_t<Initiation>>{}),
  496. static_cast<Args&&>(args)...))
  497. {
  498. return async_initiate<Signatures...>(
  499. static_cast<Initiation&&>(initiation),
  500. cancellation_slot_binder<
  501. default_completion_token_t<associated_executor_t<Initiation>>,
  502. CancellationSlot>(token.cancellation_slot_,
  503. default_completion_token_t<associated_executor_t<Initiation>>{}),
  504. static_cast<Args&&>(args)...);
  505. }
  506. };
  507. template <template <typename, typename> class Associator,
  508. typename T, typename CancellationSlot, typename DefaultCandidate>
  509. struct associator<Associator,
  510. cancellation_slot_binder<T, CancellationSlot>,
  511. DefaultCandidate>
  512. : Associator<T, DefaultCandidate>
  513. {
  514. static typename Associator<T, DefaultCandidate>::type get(
  515. const cancellation_slot_binder<T, CancellationSlot>& b) noexcept
  516. {
  517. return Associator<T, DefaultCandidate>::get(b.get());
  518. }
  519. static auto get(const cancellation_slot_binder<T, CancellationSlot>& b,
  520. const DefaultCandidate& c) noexcept
  521. -> decltype(Associator<T, DefaultCandidate>::get(b.get(), c))
  522. {
  523. return Associator<T, DefaultCandidate>::get(b.get(), c);
  524. }
  525. };
  526. template <typename T, typename CancellationSlot, typename CancellationSlot1>
  527. struct associated_cancellation_slot<
  528. cancellation_slot_binder<T, CancellationSlot>,
  529. CancellationSlot1>
  530. {
  531. typedef CancellationSlot type;
  532. static auto get(const cancellation_slot_binder<T, CancellationSlot>& b,
  533. const CancellationSlot1& = CancellationSlot1()) noexcept
  534. -> decltype(b.get_cancellation_slot())
  535. {
  536. return b.get_cancellation_slot();
  537. }
  538. };
  539. #endif // !defined(GENERATING_DOCUMENTATION)
  540. } // namespace asio
  541. } // namespace boost
  542. #include <boost/asio/detail/pop_options.hpp>
  543. #endif // BOOST_ASIO_BIND_CANCELLATION_SLOT_HPP