bind_executor.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 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. : base_type(other.get_executor(), other.get())
  262. {
  263. }
  264. /// Construct a copy of a different executor wrapper type, but specify a
  265. /// different executor.
  266. /**
  267. * This constructor is only valid if the type @c T is constructible from type
  268. * @c U.
  269. */
  270. template <typename U, typename OtherExecutor>
  271. executor_binder(executor_arg_t, const executor_type& e,
  272. const executor_binder<U, OtherExecutor>& other)
  273. : base_type(e, other.get())
  274. {
  275. }
  276. /// Move constructor.
  277. executor_binder(executor_binder&& other)
  278. : base_type(static_cast<executor_type&&>(other.get_executor()),
  279. static_cast<T&&>(other.get()))
  280. {
  281. }
  282. /// Move construct the target object, but specify a different executor.
  283. executor_binder(executor_arg_t, const executor_type& e,
  284. executor_binder&& other)
  285. : base_type(e, static_cast<T&&>(other.get()))
  286. {
  287. }
  288. /// Move construct from a different executor wrapper type.
  289. template <typename U, typename OtherExecutor>
  290. executor_binder(executor_binder<U, OtherExecutor>&& other)
  291. : base_type(static_cast<OtherExecutor&&>(other.get_executor()),
  292. static_cast<U&&>(other.get()))
  293. {
  294. }
  295. /// Move construct from a different executor wrapper type, but specify a
  296. /// different executor.
  297. template <typename U, typename OtherExecutor>
  298. executor_binder(executor_arg_t, const executor_type& e,
  299. executor_binder<U, OtherExecutor>&& other)
  300. : base_type(e, static_cast<U&&>(other.get()))
  301. {
  302. }
  303. /// Destructor.
  304. ~executor_binder()
  305. {
  306. }
  307. /// Obtain a reference to the target object.
  308. target_type& get() noexcept
  309. {
  310. return this->target_;
  311. }
  312. /// Obtain a reference to the target object.
  313. const target_type& get() const noexcept
  314. {
  315. return this->target_;
  316. }
  317. /// Obtain the associated executor.
  318. executor_type get_executor() const noexcept
  319. {
  320. return this->executor_;
  321. }
  322. /// Forwarding function call operator.
  323. template <typename... Args>
  324. result_of_t<T(Args...)> operator()(Args&&... args)
  325. {
  326. return this->target_(static_cast<Args&&>(args)...);
  327. }
  328. /// Forwarding function call operator.
  329. template <typename... Args>
  330. result_of_t<T(Args...)> operator()(Args&&... args) const
  331. {
  332. return this->target_(static_cast<Args&&>(args)...);
  333. }
  334. private:
  335. typedef detail::executor_binder_base<T, Executor,
  336. uses_executor<T, Executor>::value> base_type;
  337. };
  338. /// Associate an object of type @c T with an executor of type @c Executor.
  339. template <typename Executor, typename T>
  340. BOOST_ASIO_NODISCARD inline executor_binder<decay_t<T>, Executor>
  341. bind_executor(const Executor& ex, T&& t,
  342. constraint_t<
  343. is_executor<Executor>::value || execution::is_executor<Executor>::value
  344. > = 0)
  345. {
  346. return executor_binder<decay_t<T>, Executor>(
  347. executor_arg_t(), ex, static_cast<T&&>(t));
  348. }
  349. /// Associate an object of type @c T with an execution context's executor.
  350. template <typename ExecutionContext, typename T>
  351. BOOST_ASIO_NODISCARD inline executor_binder<decay_t<T>,
  352. typename ExecutionContext::executor_type>
  353. bind_executor(ExecutionContext& ctx, T&& t,
  354. constraint_t<
  355. is_convertible<ExecutionContext&, execution_context&>::value
  356. > = 0)
  357. {
  358. return executor_binder<decay_t<T>, typename ExecutionContext::executor_type>(
  359. executor_arg_t(), ctx.get_executor(), static_cast<T&&>(t));
  360. }
  361. #if !defined(GENERATING_DOCUMENTATION)
  362. template <typename T, typename Executor>
  363. struct uses_executor<executor_binder<T, Executor>, Executor>
  364. : true_type {};
  365. namespace detail {
  366. template <typename TargetAsyncResult, typename Executor, typename = void>
  367. class executor_binder_completion_handler_async_result
  368. {
  369. public:
  370. template <typename T>
  371. explicit executor_binder_completion_handler_async_result(T&)
  372. {
  373. }
  374. };
  375. template <typename TargetAsyncResult, typename Executor>
  376. class executor_binder_completion_handler_async_result<
  377. TargetAsyncResult, Executor,
  378. void_t<typename TargetAsyncResult::completion_handler_type >>
  379. {
  380. public:
  381. typedef executor_binder<
  382. typename TargetAsyncResult::completion_handler_type, Executor>
  383. completion_handler_type;
  384. explicit executor_binder_completion_handler_async_result(
  385. typename TargetAsyncResult::completion_handler_type& handler)
  386. : target_(handler)
  387. {
  388. }
  389. typename TargetAsyncResult::return_type get()
  390. {
  391. return target_.get();
  392. }
  393. private:
  394. TargetAsyncResult target_;
  395. };
  396. template <typename TargetAsyncResult, typename = void>
  397. struct executor_binder_async_result_return_type
  398. {
  399. };
  400. template <typename TargetAsyncResult>
  401. struct executor_binder_async_result_return_type<TargetAsyncResult,
  402. void_t<typename TargetAsyncResult::return_type>>
  403. {
  404. typedef typename TargetAsyncResult::return_type return_type;
  405. };
  406. } // namespace detail
  407. template <typename T, typename Executor, typename Signature>
  408. class async_result<executor_binder<T, Executor>, Signature> :
  409. public detail::executor_binder_completion_handler_async_result<
  410. async_result<T, Signature>, Executor>,
  411. public detail::executor_binder_async_result_return_type<
  412. async_result<T, Signature>>
  413. {
  414. public:
  415. explicit async_result(executor_binder<T, Executor>& b)
  416. : detail::executor_binder_completion_handler_async_result<
  417. async_result<T, Signature>, Executor>(b.get())
  418. {
  419. }
  420. template <typename Initiation>
  421. struct init_wrapper
  422. {
  423. template <typename Init>
  424. init_wrapper(const Executor& ex, Init&& init)
  425. : ex_(ex),
  426. initiation_(static_cast<Init&&>(init))
  427. {
  428. }
  429. template <typename Handler, typename... Args>
  430. void operator()(Handler&& handler, Args&&... args)
  431. {
  432. static_cast<Initiation&&>(initiation_)(
  433. executor_binder<decay_t<Handler>, Executor>(
  434. executor_arg_t(), ex_, static_cast<Handler&&>(handler)),
  435. static_cast<Args&&>(args)...);
  436. }
  437. template <typename Handler, typename... Args>
  438. void operator()(Handler&& handler, Args&&... args) const
  439. {
  440. initiation_(
  441. executor_binder<decay_t<Handler>, Executor>(
  442. executor_arg_t(), ex_, static_cast<Handler&&>(handler)),
  443. static_cast<Args&&>(args)...);
  444. }
  445. Executor ex_;
  446. Initiation initiation_;
  447. };
  448. template <typename Initiation, typename RawCompletionToken, typename... Args>
  449. static auto initiate(Initiation&& initiation,
  450. RawCompletionToken&& token, Args&&... args)
  451. -> decltype(
  452. async_initiate<T, Signature>(
  453. declval<init_wrapper<decay_t<Initiation>>>(),
  454. token.get(), static_cast<Args&&>(args)...))
  455. {
  456. return async_initiate<T, Signature>(
  457. init_wrapper<decay_t<Initiation>>(
  458. token.get_executor(), static_cast<Initiation&&>(initiation)),
  459. token.get(), static_cast<Args&&>(args)...);
  460. }
  461. private:
  462. async_result(const async_result&) = delete;
  463. async_result& operator=(const async_result&) = delete;
  464. };
  465. template <template <typename, typename> class Associator,
  466. typename T, typename Executor, typename DefaultCandidate>
  467. struct associator<Associator, executor_binder<T, Executor>, DefaultCandidate>
  468. : Associator<T, DefaultCandidate>
  469. {
  470. static typename Associator<T, DefaultCandidate>::type get(
  471. const executor_binder<T, Executor>& b) noexcept
  472. {
  473. return Associator<T, DefaultCandidate>::get(b.get());
  474. }
  475. static auto get(const executor_binder<T, Executor>& b,
  476. const DefaultCandidate& c) noexcept
  477. -> decltype(Associator<T, DefaultCandidate>::get(b.get(), c))
  478. {
  479. return Associator<T, DefaultCandidate>::get(b.get(), c);
  480. }
  481. };
  482. template <typename T, typename Executor, typename Executor1>
  483. struct associated_executor<executor_binder<T, Executor>, Executor1>
  484. {
  485. typedef Executor type;
  486. static auto get(const executor_binder<T, Executor>& b,
  487. const Executor1& = Executor1()) noexcept
  488. -> decltype(b.get_executor())
  489. {
  490. return b.get_executor();
  491. }
  492. };
  493. #endif // !defined(GENERATING_DOCUMENTATION)
  494. } // namespace asio
  495. } // namespace boost
  496. #include <boost/asio/detail/pop_options.hpp>
  497. #endif // BOOST_ASIO_BIND_EXECUTOR_HPP