scoped_allocator 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // <scoped_allocator> -*- C++ -*-
  2. // Copyright (C) 2011-2018 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. */
  23. #ifndef _SCOPED_ALLOCATOR
  24. #define _SCOPED_ALLOCATOR 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <utility>
  30. #include <tuple>
  31. #include <bits/alloc_traits.h>
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @addtogroup allocators
  37. * @{
  38. */
  39. template<typename _Alloc>
  40. using __outer_allocator_t
  41. = decltype(std::declval<_Alloc>().outer_allocator());
  42. template<typename _Alloc, typename = void>
  43. struct __outermost_type
  44. {
  45. using type = _Alloc;
  46. static type& _S_outermost(_Alloc& __a) { return __a; }
  47. };
  48. template<typename _Alloc>
  49. struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>>
  50. : __outermost_type<
  51. typename remove_reference<__outer_allocator_t<_Alloc>>::type
  52. >
  53. {
  54. using __base = __outermost_type<
  55. typename remove_reference<__outer_allocator_t<_Alloc>>::type
  56. >;
  57. static typename __base::type&
  58. _S_outermost(_Alloc& __a)
  59. { return __base::_S_outermost(__a.outer_allocator()); }
  60. };
  61. template<typename _Alloc>
  62. inline typename __outermost_type<_Alloc>::type&
  63. __outermost(_Alloc& __a)
  64. { return __outermost_type<_Alloc>::_S_outermost(__a); }
  65. template<typename _OuterAlloc, typename... _InnerAllocs>
  66. class scoped_allocator_adaptor;
  67. template<typename...>
  68. struct __inner_type_impl;
  69. template<typename _Outer>
  70. struct __inner_type_impl<_Outer>
  71. {
  72. typedef scoped_allocator_adaptor<_Outer> __type;
  73. __inner_type_impl() = default;
  74. __inner_type_impl(const __inner_type_impl&) = default;
  75. __inner_type_impl(__inner_type_impl&&) = default;
  76. __inner_type_impl& operator=(const __inner_type_impl&) = default;
  77. __inner_type_impl& operator=(__inner_type_impl&&) = default;
  78. template<typename _Alloc>
  79. __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
  80. { }
  81. template<typename _Alloc>
  82. __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
  83. { }
  84. __type&
  85. _M_get(__type* __p) noexcept { return *__p; }
  86. const __type&
  87. _M_get(const __type* __p) const noexcept { return *__p; }
  88. tuple<>
  89. _M_tie() const noexcept { return tuple<>(); }
  90. bool
  91. operator==(const __inner_type_impl&) const noexcept
  92. { return true; }
  93. };
  94. template<typename _Outer, typename _InnerHead, typename... _InnerTail>
  95. struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
  96. {
  97. typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
  98. __inner_type_impl() = default;
  99. __inner_type_impl(const __inner_type_impl&) = default;
  100. __inner_type_impl(__inner_type_impl&&) = default;
  101. __inner_type_impl& operator=(const __inner_type_impl&) = default;
  102. __inner_type_impl& operator=(__inner_type_impl&&) = default;
  103. template<typename... _Allocs>
  104. __inner_type_impl(const __inner_type_impl<_Allocs...>& __other)
  105. : _M_inner(__other._M_inner) { }
  106. template<typename... _Allocs>
  107. __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
  108. : _M_inner(std::move(__other._M_inner)) { }
  109. template<typename... _Args>
  110. explicit
  111. __inner_type_impl(_Args&&... __args)
  112. : _M_inner(std::forward<_Args>(__args)...) { }
  113. __type&
  114. _M_get(void*) noexcept { return _M_inner; }
  115. const __type&
  116. _M_get(const void*) const noexcept { return _M_inner; }
  117. tuple<const _InnerHead&, const _InnerTail&...>
  118. _M_tie() const noexcept
  119. { return _M_inner._M_tie(); }
  120. bool
  121. operator==(const __inner_type_impl& __other) const noexcept
  122. { return _M_inner == __other._M_inner; }
  123. private:
  124. template<typename...> friend class __inner_type_impl;
  125. template<typename, typename...> friend class scoped_allocator_adaptor;
  126. __type _M_inner;
  127. };
  128. /// Primary class template.
  129. template<typename _OuterAlloc, typename... _InnerAllocs>
  130. class scoped_allocator_adaptor
  131. : public _OuterAlloc
  132. {
  133. typedef allocator_traits<_OuterAlloc> __traits;
  134. typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
  135. __inner_type _M_inner;
  136. template<typename _Outer, typename... _Inner>
  137. friend class scoped_allocator_adaptor;
  138. template<typename...>
  139. friend class __inner_type_impl;
  140. tuple<const _OuterAlloc&, const _InnerAllocs&...>
  141. _M_tie() const noexcept
  142. { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
  143. template<typename _Alloc>
  144. using __outermost_alloc_traits
  145. = allocator_traits<typename __outermost_type<_Alloc>::type>;
  146. template<typename _Tp, typename... _Args>
  147. void
  148. _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
  149. {
  150. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  151. _O_traits::construct(__outermost(*this), __p,
  152. std::forward<_Args>(__args)...);
  153. }
  154. typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
  155. typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
  156. template<typename _Tp, typename... _Args>
  157. void
  158. _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
  159. {
  160. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  161. _O_traits::construct(__outermost(*this), __p,
  162. allocator_arg, inner_allocator(),
  163. std::forward<_Args>(__args)...);
  164. }
  165. template<typename _Tp, typename... _Args>
  166. void
  167. _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
  168. {
  169. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  170. _O_traits::construct(__outermost(*this), __p,
  171. std::forward<_Args>(__args)...,
  172. inner_allocator());
  173. }
  174. template<typename _Alloc>
  175. static _Alloc
  176. _S_select_on_copy(const _Alloc& __a)
  177. {
  178. typedef allocator_traits<_Alloc> __a_traits;
  179. return __a_traits::select_on_container_copy_construction(__a);
  180. }
  181. template<std::size_t... _Indices>
  182. scoped_allocator_adaptor(tuple<const _OuterAlloc&,
  183. const _InnerAllocs&...> __refs,
  184. _Index_tuple<_Indices...>)
  185. : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
  186. _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
  187. { }
  188. // Used to constrain constructors to disallow invalid conversions.
  189. template<typename _Alloc>
  190. using _Constructible = typename enable_if<
  191. is_constructible<_OuterAlloc, _Alloc>::value
  192. >::type;
  193. public:
  194. typedef _OuterAlloc outer_allocator_type;
  195. typedef typename __inner_type::__type inner_allocator_type;
  196. typedef typename __traits::value_type value_type;
  197. typedef typename __traits::size_type size_type;
  198. typedef typename __traits::difference_type difference_type;
  199. typedef typename __traits::pointer pointer;
  200. typedef typename __traits::const_pointer const_pointer;
  201. typedef typename __traits::void_pointer void_pointer;
  202. typedef typename __traits::const_void_pointer const_void_pointer;
  203. typedef typename __or_<
  204. typename __traits::propagate_on_container_copy_assignment,
  205. typename allocator_traits<_InnerAllocs>::
  206. propagate_on_container_copy_assignment...>::type
  207. propagate_on_container_copy_assignment;
  208. typedef typename __or_<
  209. typename __traits::propagate_on_container_move_assignment,
  210. typename allocator_traits<_InnerAllocs>::
  211. propagate_on_container_move_assignment...>::type
  212. propagate_on_container_move_assignment;
  213. typedef typename __or_<
  214. typename __traits::propagate_on_container_swap,
  215. typename allocator_traits<_InnerAllocs>::
  216. propagate_on_container_swap...>::type
  217. propagate_on_container_swap;
  218. typedef typename __and_<
  219. typename __traits::is_always_equal,
  220. typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
  221. is_always_equal;
  222. template <class _Tp>
  223. struct rebind
  224. {
  225. typedef scoped_allocator_adaptor<
  226. typename __traits::template rebind_alloc<_Tp>,
  227. _InnerAllocs...> other;
  228. };
  229. scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
  230. template<typename _Outer2, typename = _Constructible<_Outer2>>
  231. scoped_allocator_adaptor(_Outer2&& __outer,
  232. const _InnerAllocs&... __inner)
  233. : _OuterAlloc(std::forward<_Outer2>(__outer)),
  234. _M_inner(__inner...)
  235. { }
  236. scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
  237. : _OuterAlloc(__other.outer_allocator()),
  238. _M_inner(__other._M_inner)
  239. { }
  240. scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
  241. : _OuterAlloc(std::move(__other.outer_allocator())),
  242. _M_inner(std::move(__other._M_inner))
  243. { }
  244. template<typename _Outer2, typename = _Constructible<const _Outer2&>>
  245. scoped_allocator_adaptor(
  246. const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
  247. : _OuterAlloc(__other.outer_allocator()),
  248. _M_inner(__other._M_inner)
  249. { }
  250. template<typename _Outer2, typename = _Constructible<_Outer2>>
  251. scoped_allocator_adaptor(
  252. scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
  253. : _OuterAlloc(std::move(__other.outer_allocator())),
  254. _M_inner(std::move(__other._M_inner))
  255. { }
  256. scoped_allocator_adaptor&
  257. operator=(const scoped_allocator_adaptor&) = default;
  258. scoped_allocator_adaptor&
  259. operator=(scoped_allocator_adaptor&&) = default;
  260. inner_allocator_type& inner_allocator() noexcept
  261. { return _M_inner._M_get(this); }
  262. const inner_allocator_type& inner_allocator() const noexcept
  263. { return _M_inner._M_get(this); }
  264. outer_allocator_type& outer_allocator() noexcept
  265. { return static_cast<_OuterAlloc&>(*this); }
  266. const outer_allocator_type& outer_allocator() const noexcept
  267. { return static_cast<const _OuterAlloc&>(*this); }
  268. pointer allocate(size_type __n)
  269. { return __traits::allocate(outer_allocator(), __n); }
  270. pointer allocate(size_type __n, const_void_pointer __hint)
  271. { return __traits::allocate(outer_allocator(), __n, __hint); }
  272. void deallocate(pointer __p, size_type __n)
  273. { return __traits::deallocate(outer_allocator(), __p, __n); }
  274. size_type max_size() const
  275. { return __traits::max_size(outer_allocator()); }
  276. template<typename _Tp, typename... _Args>
  277. void construct(_Tp* __p, _Args&&... __args)
  278. {
  279. auto& __inner = inner_allocator();
  280. auto __use_tag
  281. = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
  282. _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
  283. }
  284. template<typename _T1, typename _T2, typename... _Args1,
  285. typename... _Args2>
  286. void
  287. construct(pair<_T1, _T2>* __p, piecewise_construct_t,
  288. tuple<_Args1...> __x, tuple<_Args2...> __y)
  289. {
  290. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  291. // 2203. wrong argument types for piecewise construction
  292. auto& __inner = inner_allocator();
  293. auto __x_use_tag
  294. = __use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
  295. auto __y_use_tag
  296. = __use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
  297. typename _Build_index_tuple<sizeof...(_Args1)>::__type __x_indices;
  298. typename _Build_index_tuple<sizeof...(_Args2)>::__type __y_indices;
  299. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  300. _O_traits::construct(__outermost(*this), __p, piecewise_construct,
  301. _M_construct_p(__x_use_tag, __x_indices, __x),
  302. _M_construct_p(__y_use_tag, __y_indices, __y));
  303. }
  304. template<typename _T1, typename _T2>
  305. void
  306. construct(pair<_T1, _T2>* __p)
  307. { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
  308. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  309. void
  310. construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
  311. {
  312. construct(__p, piecewise_construct,
  313. std::forward_as_tuple(std::forward<_Up>(__u)),
  314. std::forward_as_tuple(std::forward<_Vp>(__v)));
  315. }
  316. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  317. void
  318. construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x)
  319. {
  320. construct(__p, piecewise_construct,
  321. std::forward_as_tuple(__x.first),
  322. std::forward_as_tuple(__x.second));
  323. }
  324. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  325. void
  326. construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
  327. {
  328. construct(__p, piecewise_construct,
  329. std::forward_as_tuple(std::forward<_Up>(__x.first)),
  330. std::forward_as_tuple(std::forward<_Vp>(__x.second)));
  331. }
  332. template<typename _Tp>
  333. void destroy(_Tp* __p)
  334. {
  335. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  336. _O_traits::destroy(__outermost(*this), __p);
  337. }
  338. scoped_allocator_adaptor
  339. select_on_container_copy_construction() const
  340. {
  341. typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
  342. _Indices;
  343. return scoped_allocator_adaptor(_M_tie(), _Indices());
  344. }
  345. template <typename _OutA1, typename _OutA2, typename... _InA>
  346. friend bool
  347. operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  348. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
  349. private:
  350. template<typename _Ind, typename... _Args>
  351. tuple<_Args&&...>
  352. _M_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
  353. { return std::move(__t); }
  354. template<size_t... _Ind, typename... _Args>
  355. tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
  356. _M_construct_p(__uses_alloc1_, _Index_tuple<_Ind...>,
  357. tuple<_Args...>& __t)
  358. {
  359. return { allocator_arg, inner_allocator(),
  360. std::get<_Ind>(std::move(__t))...
  361. };
  362. }
  363. template<size_t... _Ind, typename... _Args>
  364. tuple<_Args&&..., inner_allocator_type&>
  365. _M_construct_p(__uses_alloc2_, _Index_tuple<_Ind...>,
  366. tuple<_Args...>& __t)
  367. {
  368. return { std::get<_Ind>(std::move(__t))..., inner_allocator() };
  369. }
  370. };
  371. template <typename _OutA1, typename _OutA2, typename... _InA>
  372. inline bool
  373. operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  374. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
  375. {
  376. return __a.outer_allocator() == __b.outer_allocator()
  377. && __a._M_inner == __b._M_inner;
  378. }
  379. template <typename _OutA1, typename _OutA2, typename... _InA>
  380. inline bool
  381. operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  382. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
  383. { return !(__a == __b); }
  384. /// @}
  385. _GLIBCXX_END_NAMESPACE_VERSION
  386. } // namespace
  387. #endif // C++11
  388. #endif // _SCOPED_ALLOCATOR