bind_immediate_executor.hpp 16 KB

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