bind_immediate_executor.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. //
  2. // bind_immediate_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_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. : executor_(other.get_immediate_executor()),
  231. target_(other.get())
  232. {
  233. }
  234. /// Construct a copy of a different immediate executor wrapper type, but
  235. /// specify a different immediate executor.
  236. /**
  237. * This constructor is only valid if the type @c T is constructible from type
  238. * @c U.
  239. */
  240. template <typename U, typename OtherExecutor>
  241. immediate_executor_binder(const immediate_executor_type& e,
  242. const immediate_executor_binder<U, OtherExecutor>& other)
  243. : executor_(e),
  244. target_(other.get())
  245. {
  246. }
  247. /// Move constructor.
  248. immediate_executor_binder(immediate_executor_binder&& other)
  249. : executor_(static_cast<immediate_executor_type&&>(
  250. other.get_immediate_executor())),
  251. target_(static_cast<T&&>(other.get()))
  252. {
  253. }
  254. /// Move construct the target object, but specify a different immediate
  255. /// executor.
  256. immediate_executor_binder(const immediate_executor_type& e,
  257. immediate_executor_binder&& other)
  258. : executor_(e),
  259. target_(static_cast<T&&>(other.get()))
  260. {
  261. }
  262. /// Move construct from a different immediate executor wrapper type.
  263. template <typename U, typename OtherExecutor>
  264. immediate_executor_binder(
  265. immediate_executor_binder<U, OtherExecutor>&& other)
  266. : executor_(static_cast<OtherExecutor&&>(
  267. other.get_immediate_executor())),
  268. target_(static_cast<U&&>(other.get()))
  269. {
  270. }
  271. /// Move construct from a different immediate executor wrapper type, but
  272. /// specify a different immediate executor.
  273. template <typename U, typename OtherExecutor>
  274. immediate_executor_binder(const immediate_executor_type& e,
  275. immediate_executor_binder<U, OtherExecutor>&& other)
  276. : executor_(e),
  277. target_(static_cast<U&&>(other.get()))
  278. {
  279. }
  280. /// Destructor.
  281. ~immediate_executor_binder()
  282. {
  283. }
  284. /// Obtain a reference to the target object.
  285. target_type& get() noexcept
  286. {
  287. return target_;
  288. }
  289. /// Obtain a reference to the target object.
  290. const target_type& get() const noexcept
  291. {
  292. return target_;
  293. }
  294. /// Obtain the associated immediate executor.
  295. immediate_executor_type get_immediate_executor() const noexcept
  296. {
  297. return executor_;
  298. }
  299. /// Forwarding function call operator.
  300. template <typename... Args>
  301. result_of_t<T(Args...)> operator()(Args&&... args)
  302. {
  303. return target_(static_cast<Args&&>(args)...);
  304. }
  305. /// Forwarding function call operator.
  306. template <typename... Args>
  307. result_of_t<T(Args...)> operator()(Args&&... args) const
  308. {
  309. return target_(static_cast<Args&&>(args)...);
  310. }
  311. private:
  312. Executor executor_;
  313. T target_;
  314. };
  315. /// Associate an object of type @c T with a immediate executor of type
  316. /// @c Executor.
  317. template <typename Executor, typename T>
  318. BOOST_ASIO_NODISCARD inline immediate_executor_binder<decay_t<T>, Executor>
  319. bind_immediate_executor(const Executor& e, T&& t)
  320. {
  321. return immediate_executor_binder<
  322. decay_t<T>, Executor>(
  323. e, static_cast<T&&>(t));
  324. }
  325. #if !defined(GENERATING_DOCUMENTATION)
  326. namespace detail {
  327. template <typename TargetAsyncResult, typename Executor, typename = void>
  328. class immediate_executor_binder_completion_handler_async_result
  329. {
  330. public:
  331. template <typename T>
  332. explicit immediate_executor_binder_completion_handler_async_result(T&)
  333. {
  334. }
  335. };
  336. template <typename TargetAsyncResult, typename Executor>
  337. class immediate_executor_binder_completion_handler_async_result<
  338. TargetAsyncResult, Executor,
  339. void_t<
  340. typename TargetAsyncResult::completion_handler_type
  341. >>
  342. {
  343. public:
  344. typedef immediate_executor_binder<
  345. typename TargetAsyncResult::completion_handler_type, Executor>
  346. completion_handler_type;
  347. explicit immediate_executor_binder_completion_handler_async_result(
  348. typename TargetAsyncResult::completion_handler_type& handler)
  349. : target_(handler)
  350. {
  351. }
  352. typename TargetAsyncResult::return_type get()
  353. {
  354. return target_.get();
  355. }
  356. private:
  357. TargetAsyncResult target_;
  358. };
  359. template <typename TargetAsyncResult, typename = void>
  360. struct immediate_executor_binder_async_result_return_type
  361. {
  362. };
  363. template <typename TargetAsyncResult>
  364. struct immediate_executor_binder_async_result_return_type<
  365. TargetAsyncResult,
  366. void_t<
  367. typename TargetAsyncResult::return_type
  368. >>
  369. {
  370. typedef typename TargetAsyncResult::return_type return_type;
  371. };
  372. } // namespace detail
  373. template <typename T, typename Executor, typename Signature>
  374. class async_result<immediate_executor_binder<T, Executor>, Signature> :
  375. public detail::immediate_executor_binder_completion_handler_async_result<
  376. async_result<T, Signature>, Executor>,
  377. public detail::immediate_executor_binder_async_result_return_type<
  378. async_result<T, Signature>>
  379. {
  380. public:
  381. explicit async_result(immediate_executor_binder<T, Executor>& b)
  382. : detail::immediate_executor_binder_completion_handler_async_result<
  383. async_result<T, Signature>, Executor>(b.get())
  384. {
  385. }
  386. template <typename Initiation>
  387. struct init_wrapper
  388. {
  389. template <typename Init>
  390. init_wrapper(const Executor& e, Init&& init)
  391. : executor_(e),
  392. initiation_(static_cast<Init&&>(init))
  393. {
  394. }
  395. template <typename Handler, typename... Args>
  396. void operator()(Handler&& handler, Args&&... args)
  397. {
  398. static_cast<Initiation&&>(initiation_)(
  399. immediate_executor_binder<
  400. decay_t<Handler>, Executor>(
  401. executor_, static_cast<Handler&&>(handler)),
  402. static_cast<Args&&>(args)...);
  403. }
  404. template <typename Handler, typename... Args>
  405. void operator()(Handler&& handler, Args&&... args) const
  406. {
  407. initiation_(
  408. immediate_executor_binder<
  409. decay_t<Handler>, Executor>(
  410. executor_, static_cast<Handler&&>(handler)),
  411. static_cast<Args&&>(args)...);
  412. }
  413. Executor executor_;
  414. Initiation initiation_;
  415. };
  416. template <typename Initiation, typename RawCompletionToken, typename... Args>
  417. static auto initiate(Initiation&& initiation,
  418. RawCompletionToken&& token, Args&&... args)
  419. -> decltype(
  420. async_initiate<T, Signature>(
  421. declval<init_wrapper<decay_t<Initiation>>>(),
  422. token.get(), static_cast<Args&&>(args)...))
  423. {
  424. return async_initiate<T, Signature>(
  425. init_wrapper<decay_t<Initiation>>(
  426. token.get_immediate_executor(),
  427. static_cast<Initiation&&>(initiation)),
  428. token.get(), static_cast<Args&&>(args)...);
  429. }
  430. private:
  431. async_result(const async_result&) = delete;
  432. async_result& operator=(const async_result&) = delete;
  433. async_result<T, Signature> target_;
  434. };
  435. template <template <typename, typename> class Associator,
  436. typename T, typename Executor, typename DefaultCandidate>
  437. struct associator<Associator,
  438. immediate_executor_binder<T, Executor>,
  439. DefaultCandidate>
  440. : Associator<T, DefaultCandidate>
  441. {
  442. static typename Associator<T, DefaultCandidate>::type get(
  443. const immediate_executor_binder<T, Executor>& b) noexcept
  444. {
  445. return Associator<T, DefaultCandidate>::get(b.get());
  446. }
  447. static auto get(const immediate_executor_binder<T, Executor>& b,
  448. const DefaultCandidate& c) noexcept
  449. -> decltype(Associator<T, DefaultCandidate>::get(b.get(), c))
  450. {
  451. return Associator<T, DefaultCandidate>::get(b.get(), c);
  452. }
  453. };
  454. template <typename T, typename Executor, typename Executor1>
  455. struct associated_immediate_executor<
  456. immediate_executor_binder<T, Executor>,
  457. Executor1>
  458. {
  459. typedef Executor type;
  460. static auto get(const immediate_executor_binder<T, Executor>& b,
  461. const Executor1& = Executor1()) noexcept
  462. -> decltype(b.get_immediate_executor())
  463. {
  464. return b.get_immediate_executor();
  465. }
  466. };
  467. #endif // !defined(GENERATING_DOCUMENTATION)
  468. } // namespace asio
  469. } // namespace boost
  470. #include <boost/asio/detail/pop_options.hpp>
  471. #endif // BOOST_ASIO_BIND_IMMEDIATE_EXECUTOR_HPP