bind_allocator.hpp 17 KB

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