bind_cancellation_slot.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. //
  2. // bind_cancellation_slot.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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. : slot_(other.get_cancellation_slot()),
  230. target_(other.get())
  231. {
  232. }
  233. /// Construct a copy of a different cancellation slot wrapper type, but
  234. /// specify a different cancellation slot.
  235. /**
  236. * This constructor is only valid if the type @c T is constructible from type
  237. * @c U.
  238. */
  239. template <typename U, typename OtherCancellationSlot>
  240. cancellation_slot_binder(const cancellation_slot_type& s,
  241. const cancellation_slot_binder<U, OtherCancellationSlot>& other)
  242. : slot_(s),
  243. target_(other.get())
  244. {
  245. }
  246. /// Move constructor.
  247. cancellation_slot_binder(cancellation_slot_binder&& other)
  248. : slot_(static_cast<cancellation_slot_type&&>(
  249. other.get_cancellation_slot())),
  250. target_(static_cast<T&&>(other.get()))
  251. {
  252. }
  253. /// Move construct the target object, but specify a different cancellation
  254. /// slot.
  255. cancellation_slot_binder(const cancellation_slot_type& s,
  256. cancellation_slot_binder&& other)
  257. : slot_(s),
  258. target_(static_cast<T&&>(other.get()))
  259. {
  260. }
  261. /// Move construct from a different cancellation slot wrapper type.
  262. template <typename U, typename OtherCancellationSlot>
  263. cancellation_slot_binder(
  264. cancellation_slot_binder<U, OtherCancellationSlot>&& other)
  265. : slot_(static_cast<OtherCancellationSlot&&>(
  266. other.get_cancellation_slot())),
  267. target_(static_cast<U&&>(other.get()))
  268. {
  269. }
  270. /// Move construct from a different cancellation slot wrapper type, but
  271. /// specify a different cancellation slot.
  272. template <typename U, typename OtherCancellationSlot>
  273. cancellation_slot_binder(const cancellation_slot_type& s,
  274. cancellation_slot_binder<U, OtherCancellationSlot>&& other)
  275. : slot_(s),
  276. target_(static_cast<U&&>(other.get()))
  277. {
  278. }
  279. /// Destructor.
  280. ~cancellation_slot_binder()
  281. {
  282. }
  283. /// Obtain a reference to the target object.
  284. target_type& get() noexcept
  285. {
  286. return target_;
  287. }
  288. /// Obtain a reference to the target object.
  289. const target_type& get() const noexcept
  290. {
  291. return target_;
  292. }
  293. /// Obtain the associated cancellation slot.
  294. cancellation_slot_type get_cancellation_slot() const noexcept
  295. {
  296. return slot_;
  297. }
  298. /// Forwarding function call operator.
  299. template <typename... Args>
  300. result_of_t<T(Args...)> operator()(Args&&... args)
  301. {
  302. return target_(static_cast<Args&&>(args)...);
  303. }
  304. /// Forwarding function call operator.
  305. template <typename... Args>
  306. result_of_t<T(Args...)> operator()(Args&&... args) const
  307. {
  308. return target_(static_cast<Args&&>(args)...);
  309. }
  310. private:
  311. CancellationSlot slot_;
  312. T target_;
  313. };
  314. /// Associate an object of type @c T with a cancellation slot of type
  315. /// @c CancellationSlot.
  316. template <typename CancellationSlot, typename T>
  317. BOOST_ASIO_NODISCARD inline
  318. cancellation_slot_binder<decay_t<T>, CancellationSlot>
  319. bind_cancellation_slot(const CancellationSlot& s, T&& t)
  320. {
  321. return cancellation_slot_binder<decay_t<T>, CancellationSlot>(
  322. s, static_cast<T&&>(t));
  323. }
  324. #if !defined(GENERATING_DOCUMENTATION)
  325. namespace detail {
  326. template <typename TargetAsyncResult,
  327. typename CancellationSlot, typename = void>
  328. class cancellation_slot_binder_completion_handler_async_result
  329. {
  330. public:
  331. template <typename T>
  332. explicit cancellation_slot_binder_completion_handler_async_result(T&)
  333. {
  334. }
  335. };
  336. template <typename TargetAsyncResult, typename CancellationSlot>
  337. class cancellation_slot_binder_completion_handler_async_result<
  338. TargetAsyncResult, CancellationSlot,
  339. void_t<typename TargetAsyncResult::completion_handler_type>>
  340. {
  341. public:
  342. typedef cancellation_slot_binder<
  343. typename TargetAsyncResult::completion_handler_type, CancellationSlot>
  344. completion_handler_type;
  345. explicit cancellation_slot_binder_completion_handler_async_result(
  346. typename TargetAsyncResult::completion_handler_type& handler)
  347. : target_(handler)
  348. {
  349. }
  350. typename TargetAsyncResult::return_type get()
  351. {
  352. return target_.get();
  353. }
  354. private:
  355. TargetAsyncResult target_;
  356. };
  357. template <typename TargetAsyncResult, typename = void>
  358. struct cancellation_slot_binder_async_result_return_type
  359. {
  360. };
  361. template <typename TargetAsyncResult>
  362. struct cancellation_slot_binder_async_result_return_type<
  363. TargetAsyncResult, void_t<typename TargetAsyncResult::return_type>>
  364. {
  365. typedef typename TargetAsyncResult::return_type return_type;
  366. };
  367. } // namespace detail
  368. template <typename T, typename CancellationSlot, typename Signature>
  369. class async_result<cancellation_slot_binder<T, CancellationSlot>, Signature> :
  370. public detail::cancellation_slot_binder_completion_handler_async_result<
  371. async_result<T, Signature>, CancellationSlot>,
  372. public detail::cancellation_slot_binder_async_result_return_type<
  373. async_result<T, Signature>>
  374. {
  375. public:
  376. explicit async_result(cancellation_slot_binder<T, CancellationSlot>& b)
  377. : detail::cancellation_slot_binder_completion_handler_async_result<
  378. async_result<T, Signature>, CancellationSlot>(b.get())
  379. {
  380. }
  381. template <typename Initiation>
  382. struct init_wrapper
  383. {
  384. template <typename Init>
  385. init_wrapper(const CancellationSlot& slot, Init&& init)
  386. : slot_(slot),
  387. initiation_(static_cast<Init&&>(init))
  388. {
  389. }
  390. template <typename Handler, typename... Args>
  391. void operator()(Handler&& handler, Args&&... args)
  392. {
  393. static_cast<Initiation&&>(initiation_)(
  394. cancellation_slot_binder<decay_t<Handler>, CancellationSlot>(
  395. slot_, static_cast<Handler&&>(handler)),
  396. static_cast<Args&&>(args)...);
  397. }
  398. template <typename Handler, typename... Args>
  399. void operator()(Handler&& handler, Args&&... args) const
  400. {
  401. initiation_(
  402. cancellation_slot_binder<decay_t<Handler>, CancellationSlot>(
  403. slot_, static_cast<Handler&&>(handler)),
  404. static_cast<Args&&>(args)...);
  405. }
  406. CancellationSlot slot_;
  407. Initiation initiation_;
  408. };
  409. template <typename Initiation, typename RawCompletionToken, typename... Args>
  410. static auto initiate(Initiation&& initiation,
  411. RawCompletionToken&& token, Args&&... args)
  412. -> decltype(
  413. async_initiate<T, Signature>(
  414. declval<init_wrapper<decay_t<Initiation>>>(),
  415. token.get(), static_cast<Args&&>(args)...))
  416. {
  417. return async_initiate<T, Signature>(
  418. init_wrapper<decay_t<Initiation>>(
  419. token.get_cancellation_slot(),
  420. static_cast<Initiation&&>(initiation)),
  421. token.get(), static_cast<Args&&>(args)...);
  422. }
  423. private:
  424. async_result(const async_result&) = delete;
  425. async_result& operator=(const async_result&) = delete;
  426. async_result<T, Signature> target_;
  427. };
  428. template <template <typename, typename> class Associator,
  429. typename T, typename CancellationSlot, typename DefaultCandidate>
  430. struct associator<Associator,
  431. cancellation_slot_binder<T, CancellationSlot>,
  432. DefaultCandidate>
  433. : Associator<T, DefaultCandidate>
  434. {
  435. static typename Associator<T, DefaultCandidate>::type get(
  436. const cancellation_slot_binder<T, CancellationSlot>& b) noexcept
  437. {
  438. return Associator<T, DefaultCandidate>::get(b.get());
  439. }
  440. static auto get(const cancellation_slot_binder<T, CancellationSlot>& b,
  441. const DefaultCandidate& c) noexcept
  442. -> decltype(Associator<T, DefaultCandidate>::get(b.get(), c))
  443. {
  444. return Associator<T, DefaultCandidate>::get(b.get(), c);
  445. }
  446. };
  447. template <typename T, typename CancellationSlot, typename CancellationSlot1>
  448. struct associated_cancellation_slot<
  449. cancellation_slot_binder<T, CancellationSlot>,
  450. CancellationSlot1>
  451. {
  452. typedef CancellationSlot type;
  453. static auto get(const cancellation_slot_binder<T, CancellationSlot>& b,
  454. const CancellationSlot1& = CancellationSlot1()) noexcept
  455. -> decltype(b.get_cancellation_slot())
  456. {
  457. return b.get_cancellation_slot();
  458. }
  459. };
  460. #endif // !defined(GENERATING_DOCUMENTATION)
  461. } // namespace asio
  462. } // namespace boost
  463. #include <boost/asio/detail/pop_options.hpp>
  464. #endif // BOOST_ASIO_BIND_CANCELLATION_SLOT_HPP