bind_immediate_executor.hpp 18 KB

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