bind_cancellation_slot.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. //
  2. // bind_cancellation_slot.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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) const
  317. {
  318. return target_(static_cast<Args&&>(args)...);
  319. }
  320. private:
  321. CancellationSlot slot_;
  322. T target_;
  323. };
  324. /// A function object type that adapts a @ref completion_token to specify that
  325. /// the completion handler should have the supplied cancellation slot as its
  326. /// associated cancellation slot.
  327. /**
  328. * May also be used directly as a completion token, in which case it adapts the
  329. * asynchronous operation's default completion token (or boost::asio::deferred
  330. * if no default is available).
  331. */
  332. template <typename CancellationSlot>
  333. struct partial_cancellation_slot_binder
  334. {
  335. /// Constructor that specifies associated cancellation slot.
  336. explicit partial_cancellation_slot_binder(const CancellationSlot& ex)
  337. : cancellation_slot_(ex)
  338. {
  339. }
  340. /// Adapt a @ref completion_token to specify that the completion handler
  341. /// should have the cancellation slot as its associated cancellation slot.
  342. template <typename CompletionToken>
  343. BOOST_ASIO_NODISCARD inline
  344. constexpr cancellation_slot_binder<decay_t<CompletionToken>, CancellationSlot>
  345. operator()(CompletionToken&& completion_token) const
  346. {
  347. return cancellation_slot_binder<decay_t<CompletionToken>, CancellationSlot>(
  348. static_cast<CompletionToken&&>(completion_token), cancellation_slot_);
  349. }
  350. //private:
  351. CancellationSlot cancellation_slot_;
  352. };
  353. /// Create a partial completion token that associates a cancellation slot.
  354. template <typename CancellationSlot>
  355. BOOST_ASIO_NODISCARD inline partial_cancellation_slot_binder<CancellationSlot>
  356. bind_cancellation_slot(const CancellationSlot& ex)
  357. {
  358. return partial_cancellation_slot_binder<CancellationSlot>(ex);
  359. }
  360. /// Associate an object of type @c T with a cancellation slot of type
  361. /// @c CancellationSlot.
  362. template <typename CancellationSlot, typename T>
  363. BOOST_ASIO_NODISCARD inline
  364. cancellation_slot_binder<decay_t<T>, CancellationSlot>
  365. bind_cancellation_slot(const CancellationSlot& s, T&& t)
  366. {
  367. return cancellation_slot_binder<decay_t<T>, CancellationSlot>(
  368. s, static_cast<T&&>(t));
  369. }
  370. #if !defined(GENERATING_DOCUMENTATION)
  371. namespace detail {
  372. template <typename TargetAsyncResult,
  373. typename CancellationSlot, typename = void>
  374. class cancellation_slot_binder_completion_handler_async_result
  375. {
  376. public:
  377. template <typename T>
  378. explicit cancellation_slot_binder_completion_handler_async_result(T&)
  379. {
  380. }
  381. };
  382. template <typename TargetAsyncResult, typename CancellationSlot>
  383. class cancellation_slot_binder_completion_handler_async_result<
  384. TargetAsyncResult, CancellationSlot,
  385. void_t<typename TargetAsyncResult::completion_handler_type>>
  386. {
  387. private:
  388. TargetAsyncResult target_;
  389. public:
  390. typedef cancellation_slot_binder<
  391. typename TargetAsyncResult::completion_handler_type, CancellationSlot>
  392. completion_handler_type;
  393. explicit cancellation_slot_binder_completion_handler_async_result(
  394. typename TargetAsyncResult::completion_handler_type& handler)
  395. : target_(handler)
  396. {
  397. }
  398. auto get() -> decltype(target_.get())
  399. {
  400. return target_.get();
  401. }
  402. };
  403. template <typename TargetAsyncResult, typename = void>
  404. struct cancellation_slot_binder_async_result_return_type
  405. {
  406. };
  407. template <typename TargetAsyncResult>
  408. struct cancellation_slot_binder_async_result_return_type<
  409. TargetAsyncResult, void_t<typename TargetAsyncResult::return_type>>
  410. {
  411. typedef typename TargetAsyncResult::return_type return_type;
  412. };
  413. } // namespace detail
  414. template <typename T, typename CancellationSlot, typename Signature>
  415. class async_result<cancellation_slot_binder<T, CancellationSlot>, Signature> :
  416. public detail::cancellation_slot_binder_completion_handler_async_result<
  417. async_result<T, Signature>, CancellationSlot>,
  418. public detail::cancellation_slot_binder_async_result_return_type<
  419. async_result<T, Signature>>
  420. {
  421. public:
  422. explicit async_result(cancellation_slot_binder<T, CancellationSlot>& b)
  423. : detail::cancellation_slot_binder_completion_handler_async_result<
  424. async_result<T, Signature>, CancellationSlot>(b.get())
  425. {
  426. }
  427. template <typename Initiation>
  428. struct init_wrapper : detail::initiation_base<Initiation>
  429. {
  430. using detail::initiation_base<Initiation>::initiation_base;
  431. template <typename Handler, typename... Args>
  432. void operator()(Handler&& handler,
  433. const CancellationSlot& slot, Args&&... args) &&
  434. {
  435. static_cast<Initiation&&>(*this)(
  436. cancellation_slot_binder<decay_t<Handler>, CancellationSlot>(
  437. slot, static_cast<Handler&&>(handler)),
  438. static_cast<Args&&>(args)...);
  439. }
  440. template <typename Handler, typename... Args>
  441. void operator()(Handler&& handler,
  442. const CancellationSlot& slot, Args&&... args) const &
  443. {
  444. static_cast<const Initiation&>(*this)(
  445. cancellation_slot_binder<decay_t<Handler>, CancellationSlot>(
  446. slot, static_cast<Handler&&>(handler)),
  447. static_cast<Args&&>(args)...);
  448. }
  449. };
  450. template <typename Initiation, typename RawCompletionToken, typename... Args>
  451. static auto initiate(Initiation&& initiation,
  452. RawCompletionToken&& token, Args&&... args)
  453. -> decltype(
  454. async_initiate<
  455. conditional_t<
  456. is_const<remove_reference_t<RawCompletionToken>>::value, const T, T>,
  457. Signature>(
  458. declval<init_wrapper<decay_t<Initiation>>>(),
  459. token.get(), token.get_cancellation_slot(),
  460. static_cast<Args&&>(args)...))
  461. {
  462. return async_initiate<
  463. conditional_t<
  464. is_const<remove_reference_t<RawCompletionToken>>::value, const T, T>,
  465. Signature>(
  466. init_wrapper<decay_t<Initiation>>(
  467. static_cast<Initiation&&>(initiation)),
  468. token.get(), token.get_cancellation_slot(),
  469. static_cast<Args&&>(args)...);
  470. }
  471. private:
  472. async_result(const async_result&) = delete;
  473. async_result& operator=(const async_result&) = delete;
  474. async_result<T, Signature> target_;
  475. };
  476. template <typename CancellationSlot, typename... Signatures>
  477. struct async_result<partial_cancellation_slot_binder<CancellationSlot>,
  478. Signatures...>
  479. {
  480. template <typename Initiation, typename RawCompletionToken, typename... Args>
  481. static auto initiate(Initiation&& initiation,
  482. RawCompletionToken&& token, Args&&... args)
  483. -> decltype(
  484. async_initiate<Signatures...>(
  485. static_cast<Initiation&&>(initiation),
  486. cancellation_slot_binder<
  487. default_completion_token_t<associated_executor_t<Initiation>>,
  488. CancellationSlot>(token.cancellation_slot_,
  489. default_completion_token_t<associated_executor_t<Initiation>>{}),
  490. static_cast<Args&&>(args)...))
  491. {
  492. return async_initiate<Signatures...>(
  493. static_cast<Initiation&&>(initiation),
  494. cancellation_slot_binder<
  495. default_completion_token_t<associated_executor_t<Initiation>>,
  496. CancellationSlot>(token.cancellation_slot_,
  497. default_completion_token_t<associated_executor_t<Initiation>>{}),
  498. static_cast<Args&&>(args)...);
  499. }
  500. };
  501. template <template <typename, typename> class Associator,
  502. typename T, typename CancellationSlot, typename DefaultCandidate>
  503. struct associator<Associator,
  504. cancellation_slot_binder<T, CancellationSlot>,
  505. DefaultCandidate>
  506. : Associator<T, DefaultCandidate>
  507. {
  508. static typename Associator<T, DefaultCandidate>::type get(
  509. const cancellation_slot_binder<T, CancellationSlot>& b) noexcept
  510. {
  511. return Associator<T, DefaultCandidate>::get(b.get());
  512. }
  513. static auto get(const cancellation_slot_binder<T, CancellationSlot>& b,
  514. const DefaultCandidate& c) noexcept
  515. -> decltype(Associator<T, DefaultCandidate>::get(b.get(), c))
  516. {
  517. return Associator<T, DefaultCandidate>::get(b.get(), c);
  518. }
  519. };
  520. template <typename T, typename CancellationSlot, typename CancellationSlot1>
  521. struct associated_cancellation_slot<
  522. cancellation_slot_binder<T, CancellationSlot>,
  523. CancellationSlot1>
  524. {
  525. typedef CancellationSlot type;
  526. static auto get(const cancellation_slot_binder<T, CancellationSlot>& b,
  527. const CancellationSlot1& = CancellationSlot1()) noexcept
  528. -> decltype(b.get_cancellation_slot())
  529. {
  530. return b.get_cancellation_slot();
  531. }
  532. };
  533. #endif // !defined(GENERATING_DOCUMENTATION)
  534. } // namespace asio
  535. } // namespace boost
  536. #include <boost/asio/detail/pop_options.hpp>
  537. #endif // BOOST_ASIO_BIND_CANCELLATION_SLOT_HPP