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>
53 __do_outermost(_Alloc& __a,
int) -> decltype(__a.outer_allocator())
54 {
return __a.outer_allocator(); }
56 template<
typename _Alloc>
58 __do_outermost(_Alloc& __a, ...)
62 template<
typename _Alloc>
64 __outermost(_Alloc& __a)
65 -> decltype(__do_outermost(__a, 0))
66 {
return __do_outermost(__a, 0); }
68 template<
typename _OuterAlloc,
typename... _InnerAllocs>
72 struct __inner_type_impl;
74 template<
typename _Outer>
75 struct __inner_type_impl<_Outer>
79 __inner_type_impl() =
default;
80 __inner_type_impl(
const __inner_type_impl&) =
default;
81 __inner_type_impl(__inner_type_impl&&) =
default;
82 __inner_type_impl& operator=(
const __inner_type_impl&) =
default;
83 __inner_type_impl& operator=(__inner_type_impl&&) =
default;
85 template<
typename _Alloc>
86 __inner_type_impl(
const __inner_type_impl<_Alloc>& __other)
89 template<
typename _Alloc>
90 __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
94 _M_get(__type* __p) noexcept {
return *__p; }
97 _M_get(
const __type* __p)
const noexcept {
return *__p; }
100 _M_tie() const noexcept {
return tuple<>(); }
103 operator==(
const __inner_type_impl&) const noexcept
107 template<
typename _Outer,
typename _InnerHead,
typename... _InnerTail>
108 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
110 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
112 __inner_type_impl() =
default;
113 __inner_type_impl(
const __inner_type_impl&) =
default;
114 __inner_type_impl(__inner_type_impl&&) =
default;
115 __inner_type_impl& operator=(
const __inner_type_impl&) =
default;
116 __inner_type_impl& operator=(__inner_type_impl&&) =
default;
118 template<
typename... _Allocs>
119 __inner_type_impl(
const __inner_type_impl<_Allocs...>& __other)
120 : _M_inner(__other._M_inner) { }
122 template<
typename... _Allocs>
123 __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
124 : _M_inner(std::move(__other._M_inner)) { }
126 template<
typename... _Args>
128 __inner_type_impl(_Args&&... __args)
129 : _M_inner(std::
forward<_Args>(__args)...) { }
132 _M_get(
void*) noexcept {
return _M_inner; }
135 _M_get(
const void*) const noexcept {
return _M_inner; }
137 tuple<
const _InnerHead&,
const _InnerTail&...>
138 _M_tie() const noexcept
139 {
return _M_inner._M_tie(); }
142 operator==(
const __inner_type_impl& __other)
const noexcept
143 {
return _M_inner == __other._M_inner; }
146 template<
typename...>
friend class __inner_type_impl;
147 template<
typename,
typename...>
friend class scoped_allocator_adaptor;
153 template<
typename _OuterAlloc,
typename... _InnerAllocs>
154 class scoped_allocator_adaptor
157 typedef allocator_traits<_OuterAlloc> __traits;
159 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
160 __inner_type _M_inner;
162 template<
typename _Outer,
typename... _Inner>
163 friend class scoped_allocator_adaptor;
165 template<
typename...>
166 friend class __inner_type_impl;
168 tuple<
const _OuterAlloc&,
const _InnerAllocs&...>
169 _M_tie() const noexcept
170 {
return std::tuple_cat(
std::tie(outer_allocator()), _M_inner._M_tie()); }
172 template<
typename _Alloc>
173 using __outermost_type =
typename
174 std::decay<decltype(__outermost(std::declval<_Alloc&>()))>::type;
176 template<
typename _Alloc>
177 using __outermost_alloc_traits
178 = allocator_traits<__outermost_type<_Alloc>>;
180 template<
typename _Tp,
typename... _Args>
182 _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
184 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
185 _O_traits::construct(__outermost(*
this), __p,
186 std::forward<_Args>(__args)...);
189 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
190 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
192 template<
typename _Tp,
typename... _Args>
194 _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
196 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
197 _O_traits::construct(__outermost(*
this), __p,
198 allocator_arg, inner_allocator(),
199 std::forward<_Args>(__args)...);
202 template<
typename _Tp,
typename... _Args>
204 _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
206 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
207 _O_traits::construct(__outermost(*
this), __p,
208 std::forward<_Args>(__args)...,
212 template<
typename _Alloc>
214 _S_select_on_copy(
const _Alloc& __a)
216 typedef allocator_traits<_Alloc> __a_traits;
217 return __a_traits::select_on_container_copy_construction(__a);
220 template<std::size_t... _Indices>
221 scoped_allocator_adaptor(tuple<
const _OuterAlloc&,
222 const _InnerAllocs&...> __refs,
223 _Index_tuple<_Indices...>)
224 : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
225 _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
229 typedef _OuterAlloc outer_allocator_type;
230 typedef typename __inner_type::__type inner_allocator_type;
240 typedef typename __or_<
242 typename allocator_traits<_InnerAllocs>::
243 propagate_on_container_copy_assignment...>::type
244 propagate_on_container_copy_assignment;
246 typedef typename __or_<
248 typename allocator_traits<_InnerAllocs>::
249 propagate_on_container_move_assignment...>::type
250 propagate_on_container_move_assignment;
252 typedef typename __or_<
254 typename allocator_traits<_InnerAllocs>::
255 propagate_on_container_swap...>::type
256 propagate_on_container_swap;
258 typedef typename __and_<
260 typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
266 typedef scoped_allocator_adaptor<
267 typename __traits::template rebind_alloc<_Tp>,
268 _InnerAllocs...> other;
271 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
273 template<
typename _Outer2>
274 scoped_allocator_adaptor(_Outer2&& __outer,
275 const _InnerAllocs&... __inner)
276 : _OuterAlloc(std::
forward<_Outer2>(__outer)),
280 scoped_allocator_adaptor(
const scoped_allocator_adaptor& __other)
281 : _OuterAlloc(__other.outer_allocator()),
282 _M_inner(__other._M_inner)
285 scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
286 : _OuterAlloc(std::move(__other.outer_allocator())),
287 _M_inner(std::move(__other._M_inner))
290 template<
typename _Outer2>
291 scoped_allocator_adaptor(
292 const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
293 : _OuterAlloc(__other.outer_allocator()),
294 _M_inner(__other._M_inner)
297 template<
typename _Outer2>
298 scoped_allocator_adaptor(
299 scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
300 : _OuterAlloc(std::move(__other.outer_allocator())),
301 _M_inner(std::move(__other._M_inner))
304 scoped_allocator_adaptor&
305 operator=(
const scoped_allocator_adaptor&) =
default;
307 scoped_allocator_adaptor&
308 operator=(scoped_allocator_adaptor&&) =
default;
310 inner_allocator_type& inner_allocator() noexcept
311 {
return _M_inner._M_get(
this); }
313 const inner_allocator_type& inner_allocator() const noexcept
314 {
return _M_inner._M_get(
this); }
316 outer_allocator_type& outer_allocator() noexcept
317 {
return static_cast<_OuterAlloc&
>(*this); }
319 const outer_allocator_type& outer_allocator() const noexcept
320 {
return static_cast<const _OuterAlloc&
>(*this); }
322 pointer allocate(size_type __n)
323 {
return __traits::allocate(outer_allocator(), __n); }
325 pointer allocate(size_type __n, const_void_pointer __hint)
326 {
return __traits::allocate(outer_allocator(), __n, __hint); }
328 void deallocate(pointer __p, size_type __n)
331 size_type max_size()
const
334 template<
typename _Tp,
typename... _Args>
335 void construct(_Tp* __p, _Args&&... __args)
337 auto& __inner = inner_allocator();
339 = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
340 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
343 template<
typename _T1,
typename _T2,
typename... _Args1,
346 construct(pair<_T1, _T2>* __p, piecewise_construct_t,
347 tuple<_Args1...> __x, tuple<_Args2...> __y)
351 auto& __inner = inner_allocator();
353 = __use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
355 = __use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
356 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
358 _M_construct_p(__x_use_tag, __x),
359 _M_construct_p(__y_use_tag, __y));
362 template<
typename _T1,
typename _T2>
364 construct(pair<_T1, _T2>* __p)
367 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
369 construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
372 std::forward_as_tuple(std::forward<_Up>(__u)),
373 std::forward_as_tuple(std::forward<_Vp>(__v)));
376 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
378 construct(pair<_T1, _T2>* __p,
const pair<_Up, _Vp>& __x)
381 std::forward_as_tuple(__x.first),
382 std::forward_as_tuple(__x.second));
385 template<
typename _T1,
typename _T2,
typename _Up,
typename _Vp>
387 construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
390 std::forward_as_tuple(std::forward<_Up>(__x.first)),
391 std::forward_as_tuple(std::forward<_Vp>(__x.second)));
394 template<
typename _Tp>
395 void destroy(_Tp* __p)
397 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
398 _O_traits::destroy(__outermost(*
this), __p);
401 scoped_allocator_adaptor
402 select_on_container_copy_construction()
const
404 typedef typename _Build_index_tuple<
sizeof...(_InnerAllocs)>::__type
406 return scoped_allocator_adaptor(_M_tie(), _Indices());
409 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
411 operator==(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
412 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
415 template<
typename _Tuple>
417 _M_construct_p(__uses_alloc0, _Tuple& __t)
418 {
return std::move(__t); }
420 template<
typename... _Args>
421 std::tuple<allocator_arg_t, inner_allocator_type&, _Args...>
425 return std::tuple_cat(_Tuple(allocator_arg, inner_allocator()),
429 template<
typename... _Args>
434 return std::tuple_cat(std::move(__t), _Tuple(inner_allocator()));
438 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
440 operator==(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
441 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
443 return __a.outer_allocator() == __b.outer_allocator()
444 && __a._M_inner == __b._M_inner;
447 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
449 operator!=(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
450 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
451 {
return !(__a == __b); }
455 _GLIBCXX_END_NAMESPACE_VERSION
460 #endif // _SCOPED_ALLOCATOR
_Alloc::value_type value_type
The allocated type.
__detected_or_t< typename make_unsigned< difference_type >::type, __size_type, _Alloc > size_type
The allocator's size type.
__detected_or_t< value_type *, __pointer, _Alloc > pointer
The allocator's pointer type.
constexpr piecewise_construct_t piecewise_construct
piecewise_construct
constexpr tuple< _Elements &...> tie(_Elements &...__args) noexcept
tie
static void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
__detected_or_t< __ptr_rebind< pointer, void >, __v_pointer, _Alloc > void_pointer
The allocator's void pointer type.
__detected_or_t< false_type, __pocs, _Alloc > propagate_on_container_swap
How the allocator is propagated on swap.
__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.
__detected_or_t< typename pointer_traits< pointer >::difference_type, __diff_type, _Alloc > difference_type
The allocator's difference type.
__detected_or_t< false_type, __pocma, _Alloc > propagate_on_container_move_assignment
How the allocator is propagated on move assignment.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Primary class template, tuple.
__detected_or_t< __ptr_rebind< pointer, const void >, __cv_pointer, _Alloc > const_void_pointer
The allocator's const void pointer type.
__detected_or_t< __ptr_rebind< pointer, const value_type >, __c_pointer, _Alloc > const_pointer
The allocator's const pointer type.
static size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.