29 #ifndef _SCOPED_ALLOCATOR
30 #define _SCOPED_ALLOCATOR 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 template<
template<
typename>
class _Pred,
typename... _Allocs>
49 template<
template<
typename>
class _Pred,
typename _Alloc,
typename... _Allocs>
50 struct __any_of<_Pred, _Alloc, _Allocs...>
51 : __or_<_Pred<_Alloc>, __any_of<_Pred, _Allocs...>>
54 template<
template<
typename>
class _Pred,
typename _Alloc>
55 struct __any_of<_Pred, _Alloc>
64 template<
typename _Alloc>
65 struct __propagate_on_copy
66 : allocator_traits<_Alloc>::propagate_on_container_copy_assignment
68 template<
typename _Alloc>
69 struct __propagate_on_move
70 : allocator_traits<_Alloc>::propagate_on_container_move_assignment
72 template<
typename _Alloc>
73 struct __propagate_on_swap
74 : allocator_traits<_Alloc>::propagate_on_container_swap
78 template<
typename _Alloc>
80 __do_outermost(_Alloc& __a, _Alloc*) -> decltype(__a.outer_allocator())
81 {
return __a.outer_allocator(); }
83 template<
typename _Alloc>
85 __do_outermost(_Alloc& __a, ...)
88 template<
typename _Alloc>
90 __outermost(_Alloc& __a) -> decltype(__do_outermost(__a, &__a))
91 {
return __do_outermost(__a, &__a); }
93 template<
typename _OuterAlloc,
typename... _InnerAllocs>
97 struct __inner_type_impl;
99 template<
typename _Outer>
100 struct __inner_type_impl<_Outer>
104 __inner_type_impl() =
default;
105 __inner_type_impl(
const __inner_type_impl&) =
default;
106 __inner_type_impl(__inner_type_impl&&) =
default;
108 template<
typename _Alloc>
109 __inner_type_impl(
const __inner_type_impl<_Alloc>& __other)
112 template<
typename _Alloc>
113 __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
117 _M_get(__type* __p) noexcept {
return *__p; }
120 _M_get(
const __type* __p)
const noexcept {
return *__p; }
123 _M_tie() const noexcept {
return tuple<>(); }
126 operator==(
const __inner_type_impl&) const noexcept
130 template<
typename _Outer,
typename _InnerHead,
typename... _InnerTail>
131 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
133 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
135 __inner_type_impl() =
default;
136 __inner_type_impl(
const __inner_type_impl&) =
default;
137 __inner_type_impl(__inner_type_impl&&) =
default;
139 template<
typename... _Allocs>
140 __inner_type_impl(
const __inner_type_impl<_Allocs...>& __other)
141 : _M_inner(__other._M_inner) { }
143 template<
typename... _Allocs>
144 __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
145 : _M_inner(std::move(__other._M_inner)) { }
147 template<
typename... _Args>
149 __inner_type_impl(_Args&&... __args)
150 : _M_inner(std::
forward<_Args>(__args)...) { }
153 _M_get(
void*) noexcept {
return _M_inner; }
156 _M_get(
const void*) const noexcept {
return _M_inner; }
158 tuple<
const _InnerHead&,
const _InnerTail&...>
159 _M_tie() const noexcept
160 {
return _M_inner._M_tie(); }
163 operator==(
const __inner_type_impl& __other)
const noexcept
164 {
return _M_inner == __other._M_inner; }
167 template<
typename...>
friend class __inner_type_impl;
168 template<
typename,
typename...>
friend class scoped_allocator_adaptor;
174 template<
typename _OuterAlloc,
typename... _InnerAllocs>
175 class scoped_allocator_adaptor
178 typedef allocator_traits<_OuterAlloc> __traits;
180 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
181 __inner_type _M_inner;
183 template<
typename _Outer,
typename... _Inner>
184 friend class scoped_allocator_adaptor;
186 template<
typename...>
187 friend class __inner_type_impl;
189 tuple<
const _OuterAlloc&,
const _InnerAllocs&...>
190 _M_tie() const noexcept
191 {
return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
194 template<
typename _Tp,
typename... _Args>
196 _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
198 auto& __outer = __outermost(*
this);
199 typedef typename std::decay<decltype(__outer)>::type __outer_type;
200 typedef allocator_traits<__outer_type> __o_traits;
201 __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...);
204 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
205 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
207 template<
typename _Tp,
typename... _Args>
209 _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
211 auto& __outer = __outermost(*
this);
212 typedef typename std::decay<decltype(__outer)>::type __outer_type;
213 typedef allocator_traits<__outer_type> __o_traits;
214 __o_traits::construct(__outer, __p, allocator_arg, inner_allocator(),
215 std::forward<_Args>(__args)...);
218 template<
typename _Tp,
typename... _Args>
220 _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
222 auto& __outer = __outermost(*
this);
223 typedef typename std::decay<decltype(__outer)>::type __outer_type;
224 typedef allocator_traits<__outer_type> __o_traits;
225 __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...,
229 template<
typename _Alloc>
231 _S_select_on_copy(
const _Alloc& __a)
233 typedef allocator_traits<_Alloc> __a_traits;
234 return __a_traits::select_on_container_copy_construction(__a);
237 template<std::size_t... _Indices>
238 scoped_allocator_adaptor(tuple<
const _OuterAlloc&,
239 const _InnerAllocs&...> __refs,
240 _Index_tuple<_Indices...>)
241 : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
242 _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
246 typedef _OuterAlloc outer_allocator_type;
247 typedef typename __inner_type::__type inner_allocator_type;
257 typedef typename conditional<
258 __any_of<__propagate_on_copy, _OuterAlloc, _InnerAllocs...>::value,
260 typedef typename conditional<
261 __any_of<__propagate_on_move, _OuterAlloc, _InnerAllocs...>::value,
263 typedef typename conditional<
264 __any_of<__propagate_on_swap, _OuterAlloc, _InnerAllocs...>::value,
270 typedef scoped_allocator_adaptor<
271 typename __traits::template rebind_alloc<_Tp>,
272 _InnerAllocs...> other;
275 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
277 template<
typename _Outer2>
278 scoped_allocator_adaptor(_Outer2&& __outer,
279 const _InnerAllocs&... __inner)
280 : _OuterAlloc(std::
forward<_Outer2>(__outer)),
284 scoped_allocator_adaptor(
const scoped_allocator_adaptor& __other)
285 : _OuterAlloc(__other.outer_allocator()),
286 _M_inner(__other._M_inner)
289 scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
290 : _OuterAlloc(std::move(__other.outer_allocator())),
291 _M_inner(std::move(__other._M_inner))
294 template<
typename _Outer2>
295 scoped_allocator_adaptor(
296 const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
297 : _OuterAlloc(__other.outer_allocator()),
298 _M_inner(__other._M_inner)
301 template<
typename _Outer2>
302 scoped_allocator_adaptor(
303 scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
304 : _OuterAlloc(std::move(__other.outer_allocator())),
305 _M_inner(std::move(__other._M_inner))
308 inner_allocator_type& inner_allocator() noexcept
309 {
return _M_inner._M_get(
this); }
311 const inner_allocator_type& inner_allocator() const noexcept
312 {
return _M_inner._M_get(
this); }
314 outer_allocator_type& outer_allocator() noexcept
315 {
return static_cast<_OuterAlloc&
>(*this); }
317 const outer_allocator_type& outer_allocator() const noexcept
318 {
return static_cast<const _OuterAlloc&
>(*this); }
320 pointer allocate(size_type __n)
323 pointer allocate(size_type __n, const_void_pointer __hint)
326 void deallocate(pointer __p, size_type __n)
329 size_type max_size()
const
332 template<
typename _Tp,
typename... _Args>
333 void construct(_Tp* __p, _Args&&... __args)
335 auto& __inner = inner_allocator();
337 = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
338 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
343 template<
typename _Tp>
344 void destroy(_Tp* __p)
346 auto& __outer = __outermost(*
this);
347 typedef typename std::decay<decltype(__outer)>::type __outer_type;
351 scoped_allocator_adaptor
352 select_on_container_copy_construction()
const
354 typedef typename _Build_index_tuple<
sizeof...(_InnerAllocs)>::__type
356 return scoped_allocator_adaptor(_M_tie(), _Indices());
359 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
361 operator==(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
362 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
365 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
367 operator==(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
368 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
370 return __a.outer_allocator() == __b.outer_allocator()
371 && __a._M_inner == __b._M_inner;
374 template <
typename _OutA1,
typename _OutA2,
typename... _InA>
376 operator!=(
const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
377 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
378 {
return !(__a == __b); }
382 _GLIBCXX_END_NAMESPACE_VERSION
385 #endif // __GXX_EXPERIMENTAL_CXX0X__
387 #endif // _SCOPED_ALLOCATOR
static void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
__size_type size_type
The allocator's size type.
__void_pointer void_pointer
The allocator's void pointer type.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
static pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
__difference_type difference_type
The allocator's difference type.
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
static size_type max_size(const _Alloc &__a)
The maximum supported allocation size.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
__const_pointer const_pointer
The allocator's const pointer type.
__pointer pointer
The allocator's pointer type.
__const_void_pointer const_void_pointer
The allocator's const void pointer type.
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
_Alloc::value_type value_type
The allocated type.