29 #ifndef _SCOPED_ALLOCATOR
30 #define _SCOPED_ALLOCATOR 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 template<
typename _Alloc>
52 using __outer_allocator_t
53 = decltype(std::declval<_Alloc>().outer_allocator());
55 template<
typename _Alloc,
typename =
void>
56 struct __outermost_type
59 static type& _S_outermost(_Alloc& __a) {
return __a; }
62 template<
typename _Alloc>
63 struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>>
65 typename remove_reference<__outer_allocator_t<_Alloc>>::type
68 using __base = __outermost_type<
69 typename remove_reference<__outer_allocator_t<_Alloc>>::type
72 static typename __base::type&
73 _S_outermost(_Alloc& __a)
74 {
return __base::_S_outermost(__a.outer_allocator()); }
77 template<
typename _Alloc>
78 inline typename __outermost_type<_Alloc>::type&
79 __outermost(_Alloc& __a)
80 {
return __outermost_type<_Alloc>::_S_outermost(__a); }
82 template<
typename _OuterAlloc,
typename... _InnerAllocs>
86 struct __inner_type_impl;
88 template<
typename _Outer>
89 struct __inner_type_impl<_Outer>
93 __inner_type_impl() =
default;
94 __inner_type_impl(
const __inner_type_impl&) =
default;
95 __inner_type_impl(__inner_type_impl&&) =
default;
96 __inner_type_impl& operator=(
const __inner_type_impl&) =
default;
97 __inner_type_impl& operator=(__inner_type_impl&&) =
default;
99 template<
typename _Alloc>
100 __inner_type_impl(
const __inner_type_impl<_Alloc>& __other)
103 template<
typename _Alloc>
104 __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
108 _M_get(__type* __p)
noexcept {
return *__p; }
111 _M_get(
const __type* __p)
const noexcept {
return *__p; }
114 _M_tie() const
noexcept {
return tuple<>(); }
117 operator==(
const __inner_type_impl&) const
noexcept
121 template<
typename _Outer,
typename _InnerHead,
typename... _InnerTail>
122 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
124 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
126 __inner_type_impl() =
default;
127 __inner_type_impl(
const __inner_type_impl&) =
default;
128 __inner_type_impl(__inner_type_impl&&) =
default;
129 __inner_type_impl& operator=(
const __inner_type_impl&) =
default;
130 __inner_type_impl& operator=(__inner_type_impl&&) =
default;
132 template<
typename... _Allocs>
133 __inner_type_impl(
const __inner_type_impl<_Allocs...>& __other)
134 : _M_inner(__other._M_inner) { }
136 template<
typename... _Allocs>
137 __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
138 : _M_inner(std::move(__other._M_inner)) { }
140 template<
typename... _Args>
142 __inner_type_impl(_Args&&... __args)
143 : _M_inner(std::
forward<_Args>(__args)...) { }
146 _M_get(
void*)
noexcept {
return _M_inner; }
149 _M_get(
const void*) const
noexcept {
return _M_inner; }
151 tuple<
const _InnerHead&,
const _InnerTail&...>
153 {
return _M_inner._M_tie(); }
156 operator==(
const __inner_type_impl& __other)
const noexcept
157 {
return _M_inner == __other._M_inner; }
160 template<
typename...>
friend class __inner_type_impl;
161 template<
typename,
typename...>
friend class scoped_allocator_adaptor;
167 template<
typename _OuterAlloc,
typename... _InnerAllocs>
168 class scoped_allocator_adaptor
171 typedef allocator_traits<_OuterAlloc> __traits;
173 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
174 __inner_type _M_inner;
176 template<
typename _Outer,
typename... _Inner>
177 friend class scoped_allocator_adaptor;
179 template<
typename...>
180 friend class __inner_type_impl;
182 tuple<
const _OuterAlloc&,
const _InnerAllocs&...>
184 {
return std::tuple_cat(
std::tie(outer_allocator()), _M_inner._M_tie()); }
186 template<
typename _Alloc>
187 using __outermost_alloc_traits
188 = allocator_traits<typename __outermost_type<_Alloc>::type>;
190 template<
typename _Tp,
typename... _Args>
192 _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
194 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
195 _O_traits::construct(__outermost(*
this), __p,
196 std::forward<_Args>(__args)...);
199 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
200 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
202 template<
typename _Tp,
typename... _Args>
204 _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
206 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
207 _O_traits::construct(__outermost(*
this), __p,
208 allocator_arg, inner_allocator(),
209 std::forward<_Args>(__args)...);
212 template<
typename _Tp,
typename... _Args>
214 _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
216 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
217 _O_traits::construct(__outermost(*
this), __p,
218 std::forward<_Args>(__args)...,
222 template<
typename _Alloc>
224 _S_select_on_copy(
const _Alloc& __a)
226 typedef allocator_traits<_Alloc> __a_traits;
227 return __a_traits::select_on_container_copy_construction(__a);
230 template<std::size_t... _Indices>
231 scoped_allocator_adaptor(tuple<
const _OuterAlloc&,
232 const _InnerAllocs&...> __refs,
233 _Index_tuple<_Indices...>)
234 : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
235 _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
239 template<
typename _Alloc>
240 using _Constructible =
typename enable_if<
241 is_constructible<_OuterAlloc, _Alloc>::value
245 typedef _OuterAlloc outer_allocator_type;
246 typedef typename __inner_type::__type inner_allocator_type;
256 typedef typename __or_<
258 typename allocator_traits<_InnerAllocs>::
259 propagate_on_container_copy_assignment...>::type
260 propagate_on_container_copy_assignment;
262 typedef typename __or_<
264 typename allocator_traits<_InnerAllocs>::
265 propagate_on_container_move_assignment...>::type
266 propagate_on_container_move_assignment;
268 typedef typename __or_<
270 typename allocator_traits<_InnerAllocs>::
271 propagate_on_container_swap...>::type
272 propagate_on_container_swap;
274 typedef typename __and_<
276 typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
282 typedef scoped_allocator_adaptor<
283 typename __traits::template rebind_alloc<_Tp>,
284 _InnerAllocs...> other;
287 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
289 template<
typename _Outer2,
typename = _Constructible<_Outer2>>
290 scoped_allocator_adaptor(_Outer2&& __outer,
291 const _InnerAllocs&... __inner)
292 : _OuterAlloc(std::
forward<_Outer2>(__outer)),
296 scoped_allocator_adaptor(
const scoped_allocator_adaptor& __other)
297 : _OuterAlloc(__other.outer_allocator()),
298 _M_inner(__other._M_inner)
301 scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
302 : _OuterAlloc(std::move(__other.outer_allocator())),
303 _M_inner(std::move(__other._M_inner))
306 template<
typename _Outer2,
typename = _Constructible<const _Outer2&>>
307 scoped_allocator_adaptor(
308 const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
309 : _OuterAlloc(__other.outer_allocator()),
310 _M_inner(__other._M_inner)
313 template<
typename _Outer2,
typename = _Constructible<_Outer2>>
314 scoped_allocator_adaptor(
315 scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
316 : _OuterAlloc(std::move(__other.outer_allocator())),
317 _M_inner(std::move(__other._M_inner))
320 scoped_allocator_adaptor&
321 operator=(
const scoped_allocator_adaptor&) =
default;
323 scoped_allocator_adaptor&
324 operator=(scoped_allocator_adaptor&&) =
default;
326 inner_allocator_type& inner_allocator()
noexcept
327 {
return _M_inner._M_get(
this); }
329 const inner_allocator_type& inner_allocator() const
noexcept
330 {
return _M_inner._M_get(
this); }
332 outer_allocator_type& outer_allocator()
noexcept
333 {
return static_cast<_OuterAlloc&
>(*this); }
335 const outer_allocator_type& outer_allocator() const
noexcept
336 {
return static_cast<const _OuterAlloc&
>(*this); }
338 pointer allocate(size_type __n)
339 {
return __traits::allocate(outer_allocator(), __n); }
341 pointer allocate(size_type __n, const_void_pointer __hint)
342 {
return __traits::allocate(outer_allocator(), __n, __hint); }
344 void deallocate(pointer __p, size_type __n)
347 size_type max_size()
const
350 template<
typename _Tp,
typename... _Args>
351 void construct(_Tp* __p, _Args&&... __args)
353 auto& __inner = inner_allocator();
355 = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
356 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
359 template<
typename _T1,
typename _T2,
typename... _Args1,
362 construct(pair<_T1, _T2>* __p, piecewise_construct_t,
363 tuple<_Args1...> __x, tuple<_Args2...> __y)
367 auto& __inner = inner_allocator();
369 = __use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
371 = __use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
372 typename _Build_index_tuple<
sizeof...(_Args1)>::__type __x_indices;
373 typename _Build_index_tuple<
sizeof...(_Args2)>::__type __y_indices;
374 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
376 _M_construct_p(__x_use_tag, __x_indices, __x),
377 _M_construct_p(__y_use_tag, __y_indices, __y));
380 template<
typename _T1,
typename _T2>
382 construct(pair<_T1, _T2>* __p)
385 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
387 construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
390 std::forward_as_tuple(std::forward<_Up>(__u)),
391 std::forward_as_tuple(std::forward<_Vp>(__v)));
394 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
396 construct(pair<_T1, _T2>* __p,
const pair<_Up, _Vp>& __x)
399 std::forward_as_tuple(__x.first),
400 std::forward_as_tuple(__x.second));
403 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
405 construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
408 std::forward_as_tuple(std::forward<_Up>(__x.first)),
409 std::forward_as_tuple(std::forward<_Vp>(__x.second)));
412 template<
typename _Tp>
413 void destroy(_Tp* __p)
415 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
416 _O_traits::destroy(__outermost(*
this), __p);
419 scoped_allocator_adaptor
420 select_on_container_copy_construction()
const
422 typedef typename _Build_index_tuple<
sizeof...(_InnerAllocs)>::__type
424 return scoped_allocator_adaptor(_M_tie(), _Indices());
427 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
429 operator==(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
430 const scoped_allocator_adaptor<_OutA2, _InA...>& __b)
noexcept;
433 template<
typename _Ind,
typename... _Args>
435 _M_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
436 {
return std::move(__t); }
438 template<
size_t... _Ind,
typename... _Args>
439 tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
440 _M_construct_p(__uses_alloc1_, _Index_tuple<_Ind...>,
441 tuple<_Args...>& __t)
443 return { allocator_arg, inner_allocator(),
444 std::get<_Ind>(std::move(__t))...
448 template<
size_t... _Ind,
typename... _Args>
449 tuple<_Args&&..., inner_allocator_type&>
450 _M_construct_p(__uses_alloc2_, _Index_tuple<_Ind...>,
451 tuple<_Args...>& __t)
453 return { std::get<_Ind>(std::move(__t))..., inner_allocator() };
457 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
459 operator==(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
460 const scoped_allocator_adaptor<_OutA2, _InA...>& __b)
noexcept
462 return __a.outer_allocator() == __b.outer_allocator()
463 && __a._M_inner == __b._M_inner;
466 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
468 operator!=(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
469 const scoped_allocator_adaptor<_OutA2, _InA...>& __b)
noexcept
470 {
return !(__a == __b); }
474 _GLIBCXX_END_NAMESPACE_VERSION
479 #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 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.
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
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.
_GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct
piecewise_construct
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
__detected_or_t< false_type, __pocca, _Alloc > propagate_on_container_copy_assignment
How the allocator is propagated on copy assignment.
__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.