29 #ifndef _SCOPED_ALLOCATOR
30 #define _SCOPED_ALLOCATOR 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 template<
typename _Alloc>
53 using __outer_allocator_t
54 = decltype(std::declval<_Alloc>().outer_allocator());
56 template<
typename _Alloc,
typename =
void>
57 struct __outermost_type
60 static type& _S_outermost(_Alloc& __a) {
return __a; }
63 template<
typename _Alloc>
64 struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>>
66 typename remove_reference<__outer_allocator_t<_Alloc>>::type
69 using __base = __outermost_type<
70 typename remove_reference<__outer_allocator_t<_Alloc>>::type
73 static typename __base::type&
74 _S_outermost(_Alloc& __a)
75 {
return __base::_S_outermost(__a.outer_allocator()); }
78 template<
typename _Alloc>
79 inline typename __outermost_type<_Alloc>::type&
80 __outermost(_Alloc& __a)
81 {
return __outermost_type<_Alloc>::_S_outermost(__a); }
83 template<
typename _OuterAlloc,
typename... _InnerAllocs>
87 struct __inner_type_impl;
89 template<
typename _Outer>
90 struct __inner_type_impl<_Outer>
94 __inner_type_impl() =
default;
95 __inner_type_impl(
const __inner_type_impl&) =
default;
96 __inner_type_impl(__inner_type_impl&&) =
default;
97 __inner_type_impl& operator=(
const __inner_type_impl&) =
default;
98 __inner_type_impl& operator=(__inner_type_impl&&) =
default;
100 template<
typename _Alloc>
101 __inner_type_impl(
const __inner_type_impl<_Alloc>& __other)
104 template<
typename _Alloc>
105 __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
109 _M_get(__type* __p)
noexcept {
return *__p; }
112 _M_get(
const __type* __p)
const noexcept {
return *__p; }
115 _M_tie() const
noexcept {
return tuple<>(); }
118 operator==(
const __inner_type_impl&) const
noexcept
122 template<
typename _Outer,
typename _InnerHead,
typename... _InnerTail>
123 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
125 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
127 __inner_type_impl() =
default;
128 __inner_type_impl(
const __inner_type_impl&) =
default;
129 __inner_type_impl(__inner_type_impl&&) =
default;
130 __inner_type_impl& operator=(
const __inner_type_impl&) =
default;
131 __inner_type_impl& operator=(__inner_type_impl&&) =
default;
133 template<
typename... _Allocs>
134 __inner_type_impl(
const __inner_type_impl<_Allocs...>& __other)
135 : _M_inner(__other._M_inner) { }
137 template<
typename... _Allocs>
138 __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
139 : _M_inner(std::move(__other._M_inner)) { }
141 template<
typename... _Args>
143 __inner_type_impl(_Args&&... __args)
144 : _M_inner(std::
forward<_Args>(__args)...) { }
147 _M_get(
void*)
noexcept {
return _M_inner; }
150 _M_get(
const void*) const
noexcept {
return _M_inner; }
152 tuple<
const _InnerHead&,
const _InnerTail&...>
154 {
return _M_inner._M_tie(); }
157 operator==(
const __inner_type_impl& __other)
const noexcept
158 {
return _M_inner == __other._M_inner; }
161 template<
typename...>
friend class __inner_type_impl;
162 template<
typename,
typename...>
friend class scoped_allocator_adaptor;
168 template<
typename _OuterAlloc,
typename... _InnerAllocs>
169 class scoped_allocator_adaptor
172 typedef allocator_traits<_OuterAlloc> __traits;
174 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
175 __inner_type _M_inner;
177 template<
typename _Outer,
typename... _Inner>
178 friend class scoped_allocator_adaptor;
180 template<
typename...>
181 friend class __inner_type_impl;
183 tuple<
const _OuterAlloc&,
const _InnerAllocs&...>
185 {
return std::tuple_cat(
std::tie(outer_allocator()), _M_inner._M_tie()); }
187 template<
typename _Alloc>
188 using __outermost_alloc_traits
189 = allocator_traits<typename __outermost_type<_Alloc>::type>;
191 #if __cplusplus <= 201703
192 template<
typename _Tp,
typename... _Args>
194 _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
196 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
197 _O_traits::construct(__outermost(*
this), __p,
198 std::forward<_Args>(__args)...);
201 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
202 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
204 template<
typename _Tp,
typename... _Args>
206 _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
208 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
209 _O_traits::construct(__outermost(*
this), __p,
210 allocator_arg, inner_allocator(),
211 std::forward<_Args>(__args)...);
214 template<
typename _Tp,
typename... _Args>
216 _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
218 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
219 _O_traits::construct(__outermost(*
this), __p,
220 std::forward<_Args>(__args)...,
225 template<
typename _Alloc>
227 _S_select_on_copy(
const _Alloc& __a)
229 typedef allocator_traits<_Alloc> __a_traits;
230 return __a_traits::select_on_container_copy_construction(__a);
233 template<std::size_t... _Indices>
234 scoped_allocator_adaptor(tuple<
const _OuterAlloc&,
235 const _InnerAllocs&...> __refs,
236 _Index_tuple<_Indices...>)
237 : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
238 _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
242 template<
typename _Alloc>
243 using _Constructible =
typename enable_if<
244 is_constructible<_OuterAlloc, _Alloc>::value
249 template<
typename _Tp>
250 struct __not_pair {
using type = void; };
252 template<
typename _Tp,
typename _Up>
253 struct __not_pair<pair<_Tp, _Up>> { };
256 typedef _OuterAlloc outer_allocator_type;
257 typedef typename __inner_type::__type inner_allocator_type;
267 typedef typename __or_<
269 typename allocator_traits<_InnerAllocs>::
270 propagate_on_container_copy_assignment...>::type
271 propagate_on_container_copy_assignment;
273 typedef typename __or_<
275 typename allocator_traits<_InnerAllocs>::
276 propagate_on_container_move_assignment...>::type
277 propagate_on_container_move_assignment;
279 typedef typename __or_<
281 typename allocator_traits<_InnerAllocs>::
282 propagate_on_container_swap...>::type
283 propagate_on_container_swap;
285 typedef typename __and_<
287 typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
293 typedef scoped_allocator_adaptor<
294 typename __traits::template rebind_alloc<_Tp>,
295 _InnerAllocs...> other;
298 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
300 template<
typename _Outer2,
typename = _Constructible<_Outer2>>
301 scoped_allocator_adaptor(_Outer2&& __outer,
302 const _InnerAllocs&... __inner)
303 : _OuterAlloc(std::
forward<_Outer2>(__outer)),
307 scoped_allocator_adaptor(
const scoped_allocator_adaptor& __other)
308 : _OuterAlloc(__other.outer_allocator()),
309 _M_inner(__other._M_inner)
312 scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
313 : _OuterAlloc(std::move(__other.outer_allocator())),
314 _M_inner(std::move(__other._M_inner))
317 template<
typename _Outer2,
typename = _Constructible<const _Outer2&>>
318 scoped_allocator_adaptor(
319 const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
320 : _OuterAlloc(__other.outer_allocator()),
321 _M_inner(__other._M_inner)
324 template<
typename _Outer2,
typename = _Constructible<_Outer2>>
325 scoped_allocator_adaptor(
326 scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
327 : _OuterAlloc(std::move(__other.outer_allocator())),
328 _M_inner(std::move(__other._M_inner))
331 scoped_allocator_adaptor&
332 operator=(
const scoped_allocator_adaptor&) =
default;
334 scoped_allocator_adaptor&
335 operator=(scoped_allocator_adaptor&&) =
default;
337 inner_allocator_type& inner_allocator()
noexcept
338 {
return _M_inner._M_get(
this); }
340 const inner_allocator_type& inner_allocator() const
noexcept
341 {
return _M_inner._M_get(
this); }
343 outer_allocator_type& outer_allocator()
noexcept
344 {
return static_cast<_OuterAlloc&
>(*this); }
346 const outer_allocator_type& outer_allocator() const
noexcept
347 {
return static_cast<const _OuterAlloc&
>(*this); }
349 _GLIBCXX_NODISCARD pointer allocate(size_type __n)
350 {
return __traits::allocate(outer_allocator(), __n); }
352 _GLIBCXX_NODISCARD pointer allocate(size_type __n, const_void_pointer __hint)
353 {
return __traits::allocate(outer_allocator(), __n, __hint); }
355 void deallocate(pointer __p, size_type __n)
358 size_type max_size()
const
361 #if __cplusplus <= 201703
362 template<
typename _Tp,
typename... _Args>
363 typename __not_pair<_Tp>::type
364 construct(_Tp* __p, _Args&&... __args)
366 auto& __inner = inner_allocator();
368 = std::__use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
369 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
372 template<
typename _T1,
typename _T2,
typename... _Args1,
375 construct(pair<_T1, _T2>* __p, piecewise_construct_t,
376 tuple<_Args1...> __x, tuple<_Args2...> __y)
380 auto& __inner = inner_allocator();
382 = std::__use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
384 = std::__use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
385 typename _Build_index_tuple<
sizeof...(_Args1)>::__type __x_indices;
386 typename _Build_index_tuple<
sizeof...(_Args2)>::__type __y_indices;
387 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
389 _M_construct_p(__x_use_tag, __x_indices, __x),
390 _M_construct_p(__y_use_tag, __y_indices, __y));
393 template<
typename _T1,
typename _T2>
395 construct(pair<_T1, _T2>* __p)
398 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
400 construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
407 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
409 construct(pair<_T1, _T2>* __p,
const pair<_Up, _Vp>& __x)
416 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
418 construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
425 template<
typename _Tp,
typename... _Args>
426 __attribute__((__nonnull__))
428 construct(_Tp* __p, _Args&&... __args)
430 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
431 std::apply([__p,
this](
auto&&... __newargs) {
432 _O_traits::construct(__outermost(*
this), __p,
435 uses_allocator_construction_args<_Tp>(inner_allocator(),
436 std::forward<_Args>(__args)...));
440 template<
typename _Tp>
441 void destroy(_Tp* __p)
443 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
444 _O_traits::destroy(__outermost(*
this), __p);
447 scoped_allocator_adaptor
448 select_on_container_copy_construction()
const
450 typedef typename _Build_index_tuple<
sizeof...(_InnerAllocs)>::__type
452 return scoped_allocator_adaptor(_M_tie(), _Indices());
455 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
457 operator==(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
458 const scoped_allocator_adaptor<_OutA2, _InA...>& __b)
noexcept;
461 #if __cplusplus <= 201703L
462 template<
typename _Ind,
typename... _Args>
464 _M_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
465 {
return std::move(__t); }
467 template<
size_t... _Ind,
typename... _Args>
468 tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
469 _M_construct_p(__uses_alloc1_, _Index_tuple<_Ind...>,
470 tuple<_Args...>& __t)
472 return { allocator_arg, inner_allocator(),
473 std::get<_Ind>(std::move(__t))...
477 template<
size_t... _Ind,
typename... _Args>
478 tuple<_Args&&..., inner_allocator_type&>
479 _M_construct_p(__uses_alloc2_, _Index_tuple<_Ind...>,
480 tuple<_Args...>& __t)
482 return { std::get<_Ind>(std::move(__t))..., inner_allocator() };
487 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
489 operator==(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
490 const scoped_allocator_adaptor<_OutA2, _InA...>& __b)
noexcept
492 return __a.outer_allocator() == __b.outer_allocator()
493 && __a._M_inner == __b._M_inner;
496 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
498 operator!=(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
499 const scoped_allocator_adaptor<_OutA2, _InA...>& __b)
noexcept
500 {
return !(__a == __b); }
504 _GLIBCXX_END_NAMESPACE_VERSION
509 #endif // _SCOPED_ALLOCATOR
static void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
__detected_or_t< false_type, __pocs, _Alloc > propagate_on_container_swap
How the allocator is propagated on swap.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
constexpr tuple< _Elements &...> tie(_Elements &...__args) noexcept
tie
__detected_or_t< false_type, __pocma, _Alloc > propagate_on_container_move_assignment
How the allocator is propagated on move assignment.
typename _Ptr< __cv_pointer, const void >::type const_void_pointer
The allocator's const void pointer type.
typename _Size< _Alloc, difference_type >::type size_type
The allocator's size type.
static size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.
typename _Diff< _Alloc, pointer >::type difference_type
The allocator's difference type.
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
_Iterator __base(_Iterator __it)
_GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct
piecewise_construct
__detected_or_t< false_type, __pocca, _Alloc > propagate_on_container_copy_assignment
How the allocator is propagated on copy assignment.
constexpr tuple< _Elements &&...> forward_as_tuple(_Elements &&...__args) noexcept
std::forward_as_tuple
__detected_or_t< typename is_empty< _Alloc >::type, __equal, _Alloc > is_always_equal
Whether all instances of the allocator type compare equal.
typename _Ptr< __v_pointer, void >::type void_pointer
The allocator's void pointer type.
typename _Ptr< __c_pointer, const value_type >::type const_pointer
The allocator's const pointer type.
__detected_or_t< value_type *, __pointer, _Alloc > pointer
The allocator's pointer type.
_Alloc::value_type value_type
The allocated type.