bind_cancellation_slot.hpp 15 KB

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