bind_cancellation_slot.hpp 16 KB

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