bind_immediate_executor.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. //
  2. // bind_immediate_executor.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_IMMEDIATE_EXECUTOR_HPP
  11. #define BOOST_ASIO_BIND_IMMEDIATE_EXECUTOR_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_executor.hpp>
  17. #include <boost/asio/associated_immediate_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 immediate_executor_binder_result_type
  29. {
  30. protected:
  31. typedef void result_type_or_void;
  32. };
  33. template <typename T>
  34. struct immediate_executor_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 immediate_executor_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 immediate_executor_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 immediate_executor_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 immediate_executor_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 immediate_executor_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 immediate_executor_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 immediate_executor_binder_argument_type {};
  85. template <typename T>
  86. struct immediate_executor_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 immediate_executor_binder_argument_type<R(*)(A1)>
  93. {
  94. typedef A1 argument_type;
  95. };
  96. template <typename R, typename A1>
  97. struct immediate_executor_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 immediate_executor_binder_argument_types {};
  105. template <typename T>
  106. struct immediate_executor_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 immediate_executor_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 immediate_executor_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 immediate executor of type @c Executor
  126. /// to an object of type @c T.
  127. template <typename T, typename Executor>
  128. class immediate_executor_binder
  129. #if !defined(GENERATING_DOCUMENTATION)
  130. : public detail::immediate_executor_binder_result_type<T>,
  131. public detail::immediate_executor_binder_argument_type<T>,
  132. public detail::immediate_executor_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 immediate executor.
  139. typedef Executor immediate_executor_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 immediate executor 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. immediate_executor_binder(const immediate_executor_type& e,
  205. U&& u)
  206. : executor_(e),
  207. target_(static_cast<U&&>(u))
  208. {
  209. }
  210. /// Copy constructor.
  211. immediate_executor_binder(const immediate_executor_binder& other)
  212. : executor_(other.get_immediate_executor()),
  213. target_(other.get())
  214. {
  215. }
  216. /// Construct a copy, but specify a different immediate executor.
  217. immediate_executor_binder(const immediate_executor_type& e,
  218. const immediate_executor_binder& other)
  219. : executor_(e),
  220. target_(other.get())
  221. {
  222. }
  223. /// Construct a copy of a different immediate executor wrapper type.
  224. /**
  225. * This constructor is only valid if the @c Executor type is
  226. * constructible from type @c OtherExecutor, and the type @c T is
  227. * constructible from type @c U.
  228. */
  229. template <typename U, typename OtherExecutor>
  230. immediate_executor_binder(
  231. const immediate_executor_binder<U, OtherExecutor>& other,
  232. constraint_t<is_constructible<Executor, OtherExecutor>::value> = 0,
  233. constraint_t<is_constructible<T, U>::value> = 0)
  234. : executor_(other.get_immediate_executor()),
  235. target_(other.get())
  236. {
  237. }
  238. /// Construct a copy of a different immediate executor wrapper type, but
  239. /// specify a different immediate executor.
  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 OtherExecutor>
  245. immediate_executor_binder(const immediate_executor_type& e,
  246. const immediate_executor_binder<U, OtherExecutor>& other,
  247. constraint_t<is_constructible<T, U>::value> = 0)
  248. : executor_(e),
  249. target_(other.get())
  250. {
  251. }
  252. /// Move constructor.
  253. immediate_executor_binder(immediate_executor_binder&& other)
  254. : executor_(static_cast<immediate_executor_type&&>(
  255. other.get_immediate_executor())),
  256. target_(static_cast<T&&>(other.get()))
  257. {
  258. }
  259. /// Move construct the target object, but specify a different immediate
  260. /// executor.
  261. immediate_executor_binder(const immediate_executor_type& e,
  262. immediate_executor_binder&& other)
  263. : executor_(e),
  264. target_(static_cast<T&&>(other.get()))
  265. {
  266. }
  267. /// Move construct from a different immediate executor wrapper type.
  268. template <typename U, typename OtherExecutor>
  269. immediate_executor_binder(
  270. immediate_executor_binder<U, OtherExecutor>&& other,
  271. constraint_t<is_constructible<Executor, OtherExecutor>::value> = 0,
  272. constraint_t<is_constructible<T, U>::value> = 0)
  273. : executor_(static_cast<OtherExecutor&&>(
  274. other.get_immediate_executor())),
  275. target_(static_cast<U&&>(other.get()))
  276. {
  277. }
  278. /// Move construct from a different immediate executor wrapper type, but
  279. /// specify a different immediate executor.
  280. template <typename U, typename OtherExecutor>
  281. immediate_executor_binder(const immediate_executor_type& e,
  282. immediate_executor_binder<U, OtherExecutor>&& other,
  283. constraint_t<is_constructible<T, U>::value> = 0)
  284. : executor_(e),
  285. target_(static_cast<U&&>(other.get()))
  286. {
  287. }
  288. /// Destructor.
  289. ~immediate_executor_binder()
  290. {
  291. }
  292. /// Obtain a reference to the target object.
  293. target_type& get() noexcept
  294. {
  295. return target_;
  296. }
  297. /// Obtain a reference to the target object.
  298. const target_type& get() const noexcept
  299. {
  300. return target_;
  301. }
  302. /// Obtain the associated immediate executor.
  303. immediate_executor_type get_immediate_executor() const noexcept
  304. {
  305. return executor_;
  306. }
  307. /// Forwarding function call operator.
  308. template <typename... Args>
  309. result_of_t<T(Args...)> operator()(Args&&... args)
  310. {
  311. return target_(static_cast<Args&&>(args)...);
  312. }
  313. /// Forwarding function call operator.
  314. template <typename... Args>
  315. result_of_t<T(Args...)> operator()(Args&&... args) const
  316. {
  317. return target_(static_cast<Args&&>(args)...);
  318. }
  319. private:
  320. Executor executor_;
  321. T target_;
  322. };
  323. /// A function object type that adapts a @ref completion_token to specify that
  324. /// the completion handler should have the supplied executor as its associated
  325. /// immediate executor.
  326. /**
  327. * May also be used directly as a completion token, in which case it adapts the
  328. * asynchronous operation's default completion token (or boost::asio::deferred
  329. * if no default is available).
  330. */
  331. template <typename Executor>
  332. struct partial_immediate_executor_binder
  333. {
  334. /// Constructor that specifies associated executor.
  335. explicit partial_immediate_executor_binder(const Executor& ex)
  336. : executor_(ex)
  337. {
  338. }
  339. /// Adapt a @ref completion_token to specify that the completion handler
  340. /// should have the executor as its associated immediate executor.
  341. template <typename CompletionToken>
  342. BOOST_ASIO_NODISCARD inline
  343. constexpr immediate_executor_binder<decay_t<CompletionToken>, Executor>
  344. operator()(CompletionToken&& completion_token) const
  345. {
  346. return immediate_executor_binder<decay_t<CompletionToken>, Executor>(
  347. static_cast<CompletionToken&&>(completion_token), executor_);
  348. }
  349. //private:
  350. Executor executor_;
  351. };
  352. /// Create a partial completion token that associates an executor.
  353. template <typename Executor>
  354. BOOST_ASIO_NODISCARD inline partial_immediate_executor_binder<Executor>
  355. bind_immediate_executor(const Executor& ex)
  356. {
  357. return partial_immediate_executor_binder<Executor>(ex);
  358. }
  359. /// Associate an object of type @c T with a immediate executor of type
  360. /// @c Executor.
  361. template <typename Executor, typename T>
  362. BOOST_ASIO_NODISCARD inline immediate_executor_binder<decay_t<T>, Executor>
  363. bind_immediate_executor(const Executor& e, T&& t)
  364. {
  365. return immediate_executor_binder<
  366. decay_t<T>, Executor>(
  367. e, static_cast<T&&>(t));
  368. }
  369. #if !defined(GENERATING_DOCUMENTATION)
  370. namespace detail {
  371. template <typename TargetAsyncResult, typename Executor, typename = void>
  372. class immediate_executor_binder_completion_handler_async_result
  373. {
  374. public:
  375. template <typename T>
  376. explicit immediate_executor_binder_completion_handler_async_result(T&)
  377. {
  378. }
  379. };
  380. template <typename TargetAsyncResult, typename Executor>
  381. class immediate_executor_binder_completion_handler_async_result<
  382. TargetAsyncResult, Executor,
  383. void_t<
  384. typename TargetAsyncResult::completion_handler_type
  385. >>
  386. {
  387. private:
  388. TargetAsyncResult target_;
  389. public:
  390. typedef immediate_executor_binder<
  391. typename TargetAsyncResult::completion_handler_type, Executor>
  392. completion_handler_type;
  393. explicit immediate_executor_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 immediate_executor_binder_async_result_return_type
  405. {
  406. };
  407. template <typename TargetAsyncResult>
  408. struct immediate_executor_binder_async_result_return_type<
  409. TargetAsyncResult,
  410. void_t<
  411. typename TargetAsyncResult::return_type
  412. >>
  413. {
  414. typedef typename TargetAsyncResult::return_type return_type;
  415. };
  416. } // namespace detail
  417. template <typename T, typename Executor, typename Signature>
  418. class async_result<immediate_executor_binder<T, Executor>, Signature> :
  419. public detail::immediate_executor_binder_completion_handler_async_result<
  420. async_result<T, Signature>, Executor>,
  421. public detail::immediate_executor_binder_async_result_return_type<
  422. async_result<T, Signature>>
  423. {
  424. public:
  425. explicit async_result(immediate_executor_binder<T, Executor>& b)
  426. : detail::immediate_executor_binder_completion_handler_async_result<
  427. async_result<T, Signature>, Executor>(b.get())
  428. {
  429. }
  430. template <typename Initiation>
  431. struct init_wrapper : detail::initiation_base<Initiation>
  432. {
  433. using detail::initiation_base<Initiation>::initiation_base;
  434. template <typename Handler, typename... Args>
  435. void operator()(Handler&& handler, const Executor& e, Args&&... args) &&
  436. {
  437. static_cast<Initiation&&>(*this)(
  438. immediate_executor_binder<
  439. decay_t<Handler>, Executor>(
  440. e, static_cast<Handler&&>(handler)),
  441. static_cast<Args&&>(args)...);
  442. }
  443. template <typename Handler, typename... Args>
  444. void operator()(Handler&& handler,
  445. const Executor& e, Args&&... args) const &
  446. {
  447. static_cast<const Initiation&>(*this)(
  448. immediate_executor_binder<
  449. decay_t<Handler>, Executor>(
  450. e, static_cast<Handler&&>(handler)),
  451. static_cast<Args&&>(args)...);
  452. }
  453. };
  454. template <typename Initiation, typename RawCompletionToken, typename... Args>
  455. static auto initiate(Initiation&& initiation,
  456. RawCompletionToken&& token, Args&&... args)
  457. -> decltype(
  458. async_initiate<
  459. conditional_t<
  460. is_const<remove_reference_t<RawCompletionToken>>::value, const T, T>,
  461. Signature>(
  462. declval<init_wrapper<decay_t<Initiation>>>(),
  463. token.get(), token.get_immediate_executor(),
  464. static_cast<Args&&>(args)...))
  465. {
  466. return async_initiate<
  467. conditional_t<
  468. is_const<remove_reference_t<RawCompletionToken>>::value, const T, T>,
  469. Signature>(
  470. init_wrapper<decay_t<Initiation>>(
  471. static_cast<Initiation&&>(initiation)),
  472. token.get(), token.get_immediate_executor(),
  473. static_cast<Args&&>(args)...);
  474. }
  475. private:
  476. async_result(const async_result&) = delete;
  477. async_result& operator=(const async_result&) = delete;
  478. async_result<T, Signature> target_;
  479. };
  480. template <typename Executor, typename... Signatures>
  481. struct async_result<partial_immediate_executor_binder<Executor>, Signatures...>
  482. {
  483. template <typename Initiation, typename RawCompletionToken, typename... Args>
  484. static auto initiate(Initiation&& initiation,
  485. RawCompletionToken&& token, Args&&... args)
  486. -> decltype(
  487. async_initiate<Signatures...>(
  488. static_cast<Initiation&&>(initiation),
  489. immediate_executor_binder<
  490. default_completion_token_t<associated_executor_t<Initiation>>,
  491. Executor>(token.executor_,
  492. default_completion_token_t<associated_executor_t<Initiation>>{}),
  493. static_cast<Args&&>(args)...))
  494. {
  495. return async_initiate<Signatures...>(
  496. static_cast<Initiation&&>(initiation),
  497. immediate_executor_binder<
  498. default_completion_token_t<associated_executor_t<Initiation>>,
  499. Executor>(token.executor_,
  500. default_completion_token_t<associated_executor_t<Initiation>>{}),
  501. static_cast<Args&&>(args)...);
  502. }
  503. };
  504. template <template <typename, typename> class Associator,
  505. typename T, typename Executor, typename DefaultCandidate>
  506. struct associator<Associator,
  507. immediate_executor_binder<T, Executor>,
  508. DefaultCandidate>
  509. : Associator<T, DefaultCandidate>
  510. {
  511. static typename Associator<T, DefaultCandidate>::type get(
  512. const immediate_executor_binder<T, Executor>& b) noexcept
  513. {
  514. return Associator<T, DefaultCandidate>::get(b.get());
  515. }
  516. static auto get(const immediate_executor_binder<T, Executor>& b,
  517. const DefaultCandidate& c) noexcept
  518. -> decltype(Associator<T, DefaultCandidate>::get(b.get(), c))
  519. {
  520. return Associator<T, DefaultCandidate>::get(b.get(), c);
  521. }
  522. };
  523. template <typename T, typename Executor, typename Executor1>
  524. struct associated_immediate_executor<
  525. immediate_executor_binder<T, Executor>,
  526. Executor1>
  527. {
  528. typedef Executor type;
  529. static auto get(const immediate_executor_binder<T, Executor>& b,
  530. const Executor1& = Executor1()) noexcept
  531. -> decltype(b.get_immediate_executor())
  532. {
  533. return b.get_immediate_executor();
  534. }
  535. };
  536. #endif // !defined(GENERATING_DOCUMENTATION)
  537. } // namespace asio
  538. } // namespace boost
  539. #include <boost/asio/detail/pop_options.hpp>
  540. #endif // BOOST_ASIO_BIND_IMMEDIATE_EXECUTOR_HPP