bind_executor.hpp 16 KB

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