bind_allocator.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. //
  2. // bind_allocator.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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) &&
  311. {
  312. return static_cast<T&&>(target_)(static_cast<Args&&>(args)...);
  313. }
  314. /// Forwarding function call operator.
  315. template <typename... Args>
  316. result_of_t<T(Args...)> operator()(Args&&... args) const&
  317. {
  318. return target_(static_cast<Args&&>(args)...);
  319. }
  320. private:
  321. Allocator allocator_;
  322. T target_;
  323. };
  324. /// A function object type that adapts a @ref completion_token to specify that
  325. /// the completion handler should have the supplied allocator as its associated
  326. /// allocator.
  327. /**
  328. * May also be used directly as a completion token, in which case it adapts the
  329. * asynchronous operation's default completion token (or boost::asio::deferred
  330. * if no default is available).
  331. */
  332. template <typename Allocator>
  333. struct partial_allocator_binder
  334. {
  335. /// Constructor that specifies associated allocator.
  336. explicit partial_allocator_binder(const Allocator& ex)
  337. : allocator_(ex)
  338. {
  339. }
  340. /// Adapt a @ref completion_token to specify that the completion handler
  341. /// should have the allocator as its associated allocator.
  342. template <typename CompletionToken>
  343. BOOST_ASIO_NODISCARD inline
  344. constexpr allocator_binder<decay_t<CompletionToken>, Allocator>
  345. operator()(CompletionToken&& completion_token) const
  346. {
  347. return allocator_binder<decay_t<CompletionToken>, Allocator>(
  348. allocator_, static_cast<CompletionToken&&>(completion_token));
  349. }
  350. //private:
  351. Allocator allocator_;
  352. };
  353. /// Create a partial completion token that associates an allocator.
  354. template <typename Allocator>
  355. BOOST_ASIO_NODISCARD inline partial_allocator_binder<Allocator>
  356. bind_allocator(const Allocator& ex)
  357. {
  358. return partial_allocator_binder<Allocator>(ex);
  359. }
  360. /// Associate an object of type @c T with an allocator of type
  361. /// @c Allocator.
  362. template <typename Allocator, typename T>
  363. BOOST_ASIO_NODISCARD inline allocator_binder<decay_t<T>, Allocator>
  364. bind_allocator(const Allocator& s, T&& t)
  365. {
  366. return allocator_binder<decay_t<T>, Allocator>(s, static_cast<T&&>(t));
  367. }
  368. #if !defined(GENERATING_DOCUMENTATION)
  369. namespace detail {
  370. template <typename TargetAsyncResult, typename Allocator, typename = void>
  371. class allocator_binder_completion_handler_async_result
  372. {
  373. public:
  374. template <typename T>
  375. explicit allocator_binder_completion_handler_async_result(T&)
  376. {
  377. }
  378. };
  379. template <typename TargetAsyncResult, typename Allocator>
  380. class allocator_binder_completion_handler_async_result<
  381. TargetAsyncResult, Allocator,
  382. void_t<typename TargetAsyncResult::completion_handler_type>>
  383. {
  384. private:
  385. TargetAsyncResult target_;
  386. public:
  387. typedef allocator_binder<
  388. typename TargetAsyncResult::completion_handler_type, Allocator>
  389. completion_handler_type;
  390. explicit allocator_binder_completion_handler_async_result(
  391. typename TargetAsyncResult::completion_handler_type& handler)
  392. : target_(handler)
  393. {
  394. }
  395. auto get() -> decltype(target_.get())
  396. {
  397. return target_.get();
  398. }
  399. };
  400. template <typename TargetAsyncResult, typename = void>
  401. struct allocator_binder_async_result_return_type
  402. {
  403. };
  404. template <typename TargetAsyncResult>
  405. struct allocator_binder_async_result_return_type<
  406. TargetAsyncResult, void_type<typename TargetAsyncResult::return_type>>
  407. {
  408. typedef typename TargetAsyncResult::return_type return_type;
  409. };
  410. } // namespace detail
  411. template <typename T, typename Allocator, typename Signature>
  412. class async_result<allocator_binder<T, Allocator>, Signature> :
  413. public detail::allocator_binder_completion_handler_async_result<
  414. async_result<T, Signature>, Allocator>,
  415. public detail::allocator_binder_async_result_return_type<
  416. async_result<T, Signature>>
  417. {
  418. public:
  419. explicit async_result(allocator_binder<T, Allocator>& b)
  420. : detail::allocator_binder_completion_handler_async_result<
  421. async_result<T, Signature>, Allocator>(b.get())
  422. {
  423. }
  424. template <typename Initiation>
  425. struct init_wrapper : detail::initiation_base<Initiation>
  426. {
  427. using detail::initiation_base<Initiation>::initiation_base;
  428. template <typename Handler, typename... Args>
  429. void operator()(Handler&& handler, const Allocator& a, Args&&... args) &&
  430. {
  431. static_cast<Initiation&&>(*this)(
  432. allocator_binder<decay_t<Handler>, Allocator>(
  433. a, static_cast<Handler&&>(handler)),
  434. static_cast<Args&&>(args)...);
  435. }
  436. template <typename Handler, typename... Args>
  437. void operator()(Handler&& handler,
  438. const Allocator& a, Args&&... args) const &
  439. {
  440. static_cast<const Initiation&>(*this)(
  441. allocator_binder<decay_t<Handler>, Allocator>(
  442. a, static_cast<Handler&&>(handler)),
  443. static_cast<Args&&>(args)...);
  444. }
  445. };
  446. template <typename Initiation, typename RawCompletionToken, typename... Args>
  447. static auto initiate(Initiation&& initiation,
  448. RawCompletionToken&& token, Args&&... args)
  449. -> decltype(
  450. async_initiate<
  451. conditional_t<
  452. is_const<remove_reference_t<RawCompletionToken>>::value, const T, T>,
  453. Signature>(
  454. declval<init_wrapper<decay_t<Initiation>>>(),
  455. token.get(), token.get_allocator(), static_cast<Args&&>(args)...))
  456. {
  457. return async_initiate<
  458. conditional_t<
  459. is_const<remove_reference_t<RawCompletionToken>>::value, const T, T>,
  460. Signature>(
  461. init_wrapper<decay_t<Initiation>>(
  462. static_cast<Initiation&&>(initiation)),
  463. token.get(), token.get_allocator(), static_cast<Args&&>(args)...);
  464. }
  465. private:
  466. async_result(const async_result&) = delete;
  467. async_result& operator=(const async_result&) = delete;
  468. async_result<T, Signature> target_;
  469. };
  470. template <typename Allocator, typename... Signatures>
  471. struct async_result<partial_allocator_binder<Allocator>, Signatures...>
  472. {
  473. template <typename Initiation, typename RawCompletionToken, typename... Args>
  474. static auto initiate(Initiation&& initiation,
  475. RawCompletionToken&& token, Args&&... args)
  476. -> decltype(
  477. async_initiate<Signatures...>(
  478. static_cast<Initiation&&>(initiation),
  479. allocator_binder<
  480. default_completion_token_t<associated_executor_t<Initiation>>,
  481. Allocator>(token.allocator_,
  482. default_completion_token_t<associated_executor_t<Initiation>>{}),
  483. static_cast<Args&&>(args)...))
  484. {
  485. return async_initiate<Signatures...>(
  486. static_cast<Initiation&&>(initiation),
  487. allocator_binder<
  488. default_completion_token_t<associated_executor_t<Initiation>>,
  489. Allocator>(token.allocator_,
  490. default_completion_token_t<associated_executor_t<Initiation>>{}),
  491. static_cast<Args&&>(args)...);
  492. }
  493. };
  494. template <template <typename, typename> class Associator,
  495. typename T, typename Allocator, typename DefaultCandidate>
  496. struct associator<Associator, allocator_binder<T, Allocator>, DefaultCandidate>
  497. : Associator<T, DefaultCandidate>
  498. {
  499. static typename Associator<T, DefaultCandidate>::type get(
  500. const allocator_binder<T, Allocator>& b) noexcept
  501. {
  502. return Associator<T, DefaultCandidate>::get(b.get());
  503. }
  504. static auto get(const allocator_binder<T, Allocator>& b,
  505. const DefaultCandidate& c) noexcept
  506. -> decltype(Associator<T, DefaultCandidate>::get(b.get(), c))
  507. {
  508. return Associator<T, DefaultCandidate>::get(b.get(), c);
  509. }
  510. };
  511. template <typename T, typename Allocator, typename Allocator1>
  512. struct associated_allocator<allocator_binder<T, Allocator>, Allocator1>
  513. {
  514. typedef Allocator type;
  515. static auto get(const allocator_binder<T, Allocator>& b,
  516. const Allocator1& = Allocator1()) noexcept
  517. -> decltype(b.get_allocator())
  518. {
  519. return b.get_allocator();
  520. }
  521. };
  522. #endif // !defined(GENERATING_DOCUMENTATION)
  523. } // namespace asio
  524. } // namespace boost
  525. #include <boost/asio/detail/pop_options.hpp>
  526. #endif // BOOST_ASIO_BIND_ALLOCATOR_HPP