bind_executor.hpp 16 KB

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