You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scoped_allocator 17KB

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // <scoped_allocator> -*- C++ -*-
  2. // Copyright (C) 2011-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/scoped_allocator
  21. * This is a Standard C++ Library header.
  22. * @ingroup allocators
  23. */
  24. #ifndef _SCOPED_ALLOCATOR
  25. #define _SCOPED_ALLOCATOR 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <memory>
  31. #include <utility>
  32. #include <tuple>
  33. #include <bits/alloc_traits.h>
  34. namespace std _GLIBCXX_VISIBILITY(default)
  35. {
  36. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  37. /**
  38. * @addtogroup allocators
  39. * @{
  40. */
  41. /// @cond undocumented
  42. template<typename _Alloc>
  43. using __outer_allocator_t
  44. = decltype(std::declval<_Alloc>().outer_allocator());
  45. template<typename _Alloc, typename = void>
  46. struct __outermost_type
  47. {
  48. using type = _Alloc;
  49. static type& _S_outermost(_Alloc& __a) { return __a; }
  50. };
  51. template<typename _Alloc>
  52. struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>>
  53. : __outermost_type<
  54. typename remove_reference<__outer_allocator_t<_Alloc>>::type
  55. >
  56. {
  57. using __base = __outermost_type<
  58. typename remove_reference<__outer_allocator_t<_Alloc>>::type
  59. >;
  60. static typename __base::type&
  61. _S_outermost(_Alloc& __a)
  62. { return __base::_S_outermost(__a.outer_allocator()); }
  63. };
  64. /// Implementation of the OUTERMOST pseudofunction
  65. template<typename _Alloc>
  66. inline typename __outermost_type<_Alloc>::type&
  67. __outermost(_Alloc& __a)
  68. { return __outermost_type<_Alloc>::_S_outermost(__a); }
  69. template<typename _OuterAlloc, typename... _InnerAllocs>
  70. class scoped_allocator_adaptor;
  71. template<typename...>
  72. struct __inner_type_impl;
  73. template<typename _Outer>
  74. struct __inner_type_impl<_Outer>
  75. {
  76. typedef scoped_allocator_adaptor<_Outer> __type;
  77. __inner_type_impl() = default;
  78. __inner_type_impl(const __inner_type_impl&) = default;
  79. __inner_type_impl(__inner_type_impl&&) = default;
  80. __inner_type_impl& operator=(const __inner_type_impl&) = default;
  81. __inner_type_impl& operator=(__inner_type_impl&&) = default;
  82. template<typename _Alloc>
  83. __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
  84. { }
  85. template<typename _Alloc>
  86. __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
  87. { }
  88. __type&
  89. _M_get(__type* __p) noexcept { return *__p; }
  90. const __type&
  91. _M_get(const __type* __p) const noexcept { return *__p; }
  92. tuple<>
  93. _M_tie() const noexcept { return tuple<>(); }
  94. bool
  95. operator==(const __inner_type_impl&) const noexcept
  96. { return true; }
  97. };
  98. template<typename _Outer, typename _InnerHead, typename... _InnerTail>
  99. struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
  100. {
  101. typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
  102. __inner_type_impl() = default;
  103. __inner_type_impl(const __inner_type_impl&) = default;
  104. __inner_type_impl(__inner_type_impl&&) = default;
  105. __inner_type_impl& operator=(const __inner_type_impl&) = default;
  106. __inner_type_impl& operator=(__inner_type_impl&&) = default;
  107. template<typename... _Allocs>
  108. __inner_type_impl(const __inner_type_impl<_Allocs...>& __other)
  109. : _M_inner(__other._M_inner) { }
  110. template<typename... _Allocs>
  111. __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
  112. : _M_inner(std::move(__other._M_inner)) { }
  113. template<typename... _Args>
  114. explicit
  115. __inner_type_impl(_Args&&... __args)
  116. : _M_inner(std::forward<_Args>(__args)...) { }
  117. __type&
  118. _M_get(void*) noexcept { return _M_inner; }
  119. const __type&
  120. _M_get(const void*) const noexcept { return _M_inner; }
  121. tuple<const _InnerHead&, const _InnerTail&...>
  122. _M_tie() const noexcept
  123. { return _M_inner._M_tie(); }
  124. bool
  125. operator==(const __inner_type_impl& __other) const noexcept
  126. { return _M_inner == __other._M_inner; }
  127. private:
  128. template<typename...> friend class __inner_type_impl;
  129. template<typename, typename...> friend class scoped_allocator_adaptor;
  130. __type _M_inner;
  131. };
  132. /// @endcond
  133. /// An adaptor to recursively pass an allocator to the objects it constructs
  134. template<typename _OuterAlloc, typename... _InnerAllocs>
  135. class scoped_allocator_adaptor
  136. : public _OuterAlloc
  137. {
  138. typedef allocator_traits<_OuterAlloc> __traits;
  139. typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
  140. __inner_type _M_inner;
  141. template<typename _Outer, typename... _Inner>
  142. friend class scoped_allocator_adaptor;
  143. template<typename...>
  144. friend class __inner_type_impl;
  145. tuple<const _OuterAlloc&, const _InnerAllocs&...>
  146. _M_tie() const noexcept
  147. { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
  148. template<typename _Alloc>
  149. using __outermost_alloc_traits
  150. = allocator_traits<typename __outermost_type<_Alloc>::type>;
  151. #if __cplusplus <= 201703
  152. template<typename _Tp, typename... _Args>
  153. void
  154. _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
  155. {
  156. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  157. _O_traits::construct(__outermost(*this), __p,
  158. std::forward<_Args>(__args)...);
  159. }
  160. typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
  161. typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
  162. template<typename _Tp, typename... _Args>
  163. void
  164. _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
  165. {
  166. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  167. _O_traits::construct(__outermost(*this), __p,
  168. allocator_arg, inner_allocator(),
  169. std::forward<_Args>(__args)...);
  170. }
  171. template<typename _Tp, typename... _Args>
  172. void
  173. _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
  174. {
  175. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  176. _O_traits::construct(__outermost(*this), __p,
  177. std::forward<_Args>(__args)...,
  178. inner_allocator());
  179. }
  180. #endif // C++17
  181. template<typename _Alloc>
  182. static _Alloc
  183. _S_select_on_copy(const _Alloc& __a)
  184. {
  185. typedef allocator_traits<_Alloc> __a_traits;
  186. return __a_traits::select_on_container_copy_construction(__a);
  187. }
  188. template<std::size_t... _Indices>
  189. scoped_allocator_adaptor(tuple<const _OuterAlloc&,
  190. const _InnerAllocs&...> __refs,
  191. _Index_tuple<_Indices...>)
  192. : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
  193. _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
  194. { }
  195. // Used to constrain constructors to disallow invalid conversions.
  196. template<typename _Alloc>
  197. using _Constructible = typename enable_if<
  198. is_constructible<_OuterAlloc, _Alloc>::value
  199. >::type;
  200. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  201. // 2975. Missing case for pair construction in scoped [...] allocators
  202. template<typename _Tp>
  203. struct __not_pair { using type = void; };
  204. template<typename _Tp, typename _Up>
  205. struct __not_pair<pair<_Tp, _Up>> { };
  206. public:
  207. typedef _OuterAlloc outer_allocator_type;
  208. typedef typename __inner_type::__type inner_allocator_type;
  209. typedef typename __traits::value_type value_type;
  210. typedef typename __traits::size_type size_type;
  211. typedef typename __traits::difference_type difference_type;
  212. typedef typename __traits::pointer pointer;
  213. typedef typename __traits::const_pointer const_pointer;
  214. typedef typename __traits::void_pointer void_pointer;
  215. typedef typename __traits::const_void_pointer const_void_pointer;
  216. typedef typename __or_<
  217. typename __traits::propagate_on_container_copy_assignment,
  218. typename allocator_traits<_InnerAllocs>::
  219. propagate_on_container_copy_assignment...>::type
  220. propagate_on_container_copy_assignment;
  221. typedef typename __or_<
  222. typename __traits::propagate_on_container_move_assignment,
  223. typename allocator_traits<_InnerAllocs>::
  224. propagate_on_container_move_assignment...>::type
  225. propagate_on_container_move_assignment;
  226. typedef typename __or_<
  227. typename __traits::propagate_on_container_swap,
  228. typename allocator_traits<_InnerAllocs>::
  229. propagate_on_container_swap...>::type
  230. propagate_on_container_swap;
  231. typedef typename __and_<
  232. typename __traits::is_always_equal,
  233. typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
  234. is_always_equal;
  235. template <class _Tp>
  236. struct rebind
  237. {
  238. typedef scoped_allocator_adaptor<
  239. typename __traits::template rebind_alloc<_Tp>,
  240. _InnerAllocs...> other;
  241. };
  242. scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
  243. template<typename _Outer2, typename = _Constructible<_Outer2>>
  244. scoped_allocator_adaptor(_Outer2&& __outer,
  245. const _InnerAllocs&... __inner)
  246. : _OuterAlloc(std::forward<_Outer2>(__outer)),
  247. _M_inner(__inner...)
  248. { }
  249. scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
  250. : _OuterAlloc(__other.outer_allocator()),
  251. _M_inner(__other._M_inner)
  252. { }
  253. scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
  254. : _OuterAlloc(std::move(__other.outer_allocator())),
  255. _M_inner(std::move(__other._M_inner))
  256. { }
  257. template<typename _Outer2, typename = _Constructible<const _Outer2&>>
  258. scoped_allocator_adaptor(
  259. const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
  260. : _OuterAlloc(__other.outer_allocator()),
  261. _M_inner(__other._M_inner)
  262. { }
  263. template<typename _Outer2, typename = _Constructible<_Outer2>>
  264. scoped_allocator_adaptor(
  265. scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
  266. : _OuterAlloc(std::move(__other.outer_allocator())),
  267. _M_inner(std::move(__other._M_inner))
  268. { }
  269. scoped_allocator_adaptor&
  270. operator=(const scoped_allocator_adaptor&) = default;
  271. scoped_allocator_adaptor&
  272. operator=(scoped_allocator_adaptor&&) = default;
  273. inner_allocator_type& inner_allocator() noexcept
  274. { return _M_inner._M_get(this); }
  275. const inner_allocator_type& inner_allocator() const noexcept
  276. { return _M_inner._M_get(this); }
  277. outer_allocator_type& outer_allocator() noexcept
  278. { return static_cast<_OuterAlloc&>(*this); }
  279. const outer_allocator_type& outer_allocator() const noexcept
  280. { return static_cast<const _OuterAlloc&>(*this); }
  281. _GLIBCXX_NODISCARD pointer allocate(size_type __n)
  282. { return __traits::allocate(outer_allocator(), __n); }
  283. _GLIBCXX_NODISCARD pointer allocate(size_type __n, const_void_pointer __hint)
  284. { return __traits::allocate(outer_allocator(), __n, __hint); }
  285. void deallocate(pointer __p, size_type __n)
  286. { return __traits::deallocate(outer_allocator(), __p, __n); }
  287. size_type max_size() const
  288. { return __traits::max_size(outer_allocator()); }
  289. #if __cplusplus <= 201703
  290. template<typename _Tp, typename... _Args>
  291. typename __not_pair<_Tp>::type
  292. construct(_Tp* __p, _Args&&... __args)
  293. {
  294. auto& __inner = inner_allocator();
  295. auto __use_tag
  296. = std::__use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
  297. _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
  298. }
  299. template<typename _T1, typename _T2, typename... _Args1,
  300. typename... _Args2>
  301. void
  302. construct(pair<_T1, _T2>* __p, piecewise_construct_t,
  303. tuple<_Args1...> __x, tuple<_Args2...> __y)
  304. {
  305. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  306. // 2203. wrong argument types for piecewise construction
  307. auto& __inner = inner_allocator();
  308. auto __x_use_tag
  309. = std::__use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
  310. auto __y_use_tag
  311. = std::__use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
  312. typename _Build_index_tuple<sizeof...(_Args1)>::__type __x_indices;
  313. typename _Build_index_tuple<sizeof...(_Args2)>::__type __y_indices;
  314. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  315. _O_traits::construct(__outermost(*this), __p, piecewise_construct,
  316. _M_construct_p(__x_use_tag, __x_indices, __x),
  317. _M_construct_p(__y_use_tag, __y_indices, __y));
  318. }
  319. template<typename _T1, typename _T2>
  320. void
  321. construct(pair<_T1, _T2>* __p)
  322. { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
  323. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  324. void
  325. construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
  326. {
  327. construct(__p, piecewise_construct,
  328. std::forward_as_tuple(std::forward<_Up>(__u)),
  329. std::forward_as_tuple(std::forward<_Vp>(__v)));
  330. }
  331. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  332. void
  333. construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x)
  334. {
  335. construct(__p, piecewise_construct,
  336. std::forward_as_tuple(__x.first),
  337. std::forward_as_tuple(__x.second));
  338. }
  339. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  340. void
  341. construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
  342. {
  343. construct(__p, piecewise_construct,
  344. std::forward_as_tuple(std::forward<_Up>(__x.first)),
  345. std::forward_as_tuple(std::forward<_Vp>(__x.second)));
  346. }
  347. #else // C++2a
  348. template<typename _Tp, typename... _Args>
  349. __attribute__((__nonnull__))
  350. void
  351. construct(_Tp* __p, _Args&&... __args)
  352. {
  353. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  354. std::apply([__p, this](auto&&... __newargs) {
  355. _O_traits::construct(__outermost(*this), __p,
  356. std::forward<decltype(__newargs)>(__newargs)...);
  357. },
  358. uses_allocator_construction_args<_Tp>(inner_allocator(),
  359. std::forward<_Args>(__args)...));
  360. }
  361. #endif // C++2a
  362. template<typename _Tp>
  363. void destroy(_Tp* __p)
  364. {
  365. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  366. _O_traits::destroy(__outermost(*this), __p);
  367. }
  368. scoped_allocator_adaptor
  369. select_on_container_copy_construction() const
  370. {
  371. typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
  372. _Indices;
  373. return scoped_allocator_adaptor(_M_tie(), _Indices());
  374. }
  375. template <typename _OutA1, typename _OutA2, typename... _InA>
  376. friend bool
  377. operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  378. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
  379. private:
  380. #if __cplusplus <= 201703L
  381. template<typename _Ind, typename... _Args>
  382. tuple<_Args&&...>
  383. _M_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
  384. { return std::move(__t); }
  385. template<size_t... _Ind, typename... _Args>
  386. tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
  387. _M_construct_p(__uses_alloc1_, _Index_tuple<_Ind...>,
  388. tuple<_Args...>& __t)
  389. {
  390. return { allocator_arg, inner_allocator(),
  391. std::get<_Ind>(std::move(__t))...
  392. };
  393. }
  394. template<size_t... _Ind, typename... _Args>
  395. tuple<_Args&&..., inner_allocator_type&>
  396. _M_construct_p(__uses_alloc2_, _Index_tuple<_Ind...>,
  397. tuple<_Args...>& __t)
  398. {
  399. return { std::get<_Ind>(std::move(__t))..., inner_allocator() };
  400. }
  401. #endif // C++17
  402. };
  403. /// @related std::scoped_allocator_adaptor
  404. template <typename _OutA1, typename _OutA2, typename... _InA>
  405. inline bool
  406. operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  407. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
  408. {
  409. return __a.outer_allocator() == __b.outer_allocator()
  410. && __a._M_inner == __b._M_inner;
  411. }
  412. #if __cpp_impl_three_way_comparison < 201907L
  413. /// @related std::scoped_allocator_adaptor
  414. template <typename _OutA1, typename _OutA2, typename... _InA>
  415. inline bool
  416. operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  417. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
  418. { return !(__a == __b); }
  419. #endif
  420. /// @}
  421. _GLIBCXX_END_NAMESPACE_VERSION
  422. } // namespace
  423. #endif // C++11
  424. #endif // _SCOPED_ALLOCATOR