1 // <scoped_allocator> -*- C++ -*-
3 // Copyright (C) 2011 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/scoped_allocator
26 * This is a Standard C++ Library header.
29 #ifndef _SCOPED_ALLOCATOR
30 #define _SCOPED_ALLOCATOR 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
40 #include <bits/alloc_traits.h>
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>
60 * @addtogroup allocators
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>
94 class scoped_allocator_adaptor;
96 template<typename...> struct __inner_type_impl;
98 template<typename _Outer>
99 struct __inner_type_impl<_Outer>
101 typedef scoped_allocator_adaptor<_Outer> __type;
103 __inner_type_impl() = default;
104 __inner_type_impl(const __inner_type_impl&) = default;
105 __inner_type_impl(__inner_type_impl&&) = default;
107 template<typename _Alloc>
108 __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
111 template<typename _Alloc>
112 __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
115 __type& _M_get(__type* __p) noexcept { return *__p; }
116 const __type& _M_get(const __type* __p) const noexcept { return *__p; }
118 tuple<> _M_tie() const noexcept { return tuple<>(); }
120 bool operator==(const __inner_type_impl&) const noexcept
124 template<typename _Outer, typename _InnerHead, typename... _InnerTail>
125 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
127 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
129 __inner_type_impl() = default;
130 __inner_type_impl(const __inner_type_impl&) = default;
131 __inner_type_impl(__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)...) { }
146 __type& _M_get(void*) noexcept { return _M_inner; }
147 const __type& _M_get(const void*) const noexcept { return _M_inner; }
149 tuple<const _InnerHead&, const _InnerTail&...> _M_tie() const noexcept
150 { return _M_inner._M_tie(); }
152 bool operator==(const __inner_type_impl& __other) const noexcept
153 { return _M_inner == __other._M_inner; }
156 template<typename...> friend class __inner_type_impl;
157 template<typename, typename...> friend class scoped_allocator_adaptor;
162 template<typename _OuterAlloc, typename... _InnerAllocs>
163 class scoped_allocator_adaptor
166 typedef allocator_traits<_OuterAlloc> __traits;
168 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
169 __inner_type _M_inner;
171 template<typename _Outer, typename... _Inner>
172 friend class scoped_allocator_adaptor;
174 template<typename...>
175 friend class __inner_type_impl;
177 tuple<const _OuterAlloc&, const _InnerAllocs&...>
178 _M_tie() const noexcept
179 { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
182 template<typename _Tp, typename... _Args>
183 void _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
185 auto& __outer = __outermost(*this);
186 typedef typename std::decay<decltype(__outer)>::type __outer_type;
187 typedef allocator_traits<__outer_type> __o_traits;
188 __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...);
191 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
192 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
194 template<typename _Tp, typename... _Args>
195 void _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
197 auto& __outer = __outermost(*this);
198 typedef typename std::decay<decltype(__outer)>::type __outer_type;
199 typedef allocator_traits<__outer_type> __o_traits;
200 __o_traits::construct(__outer, __p, allocator_arg, inner_allocator(),
201 std::forward<_Args>(__args)...);
204 template<typename _Tp, typename... _Args>
205 void _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
207 auto& __outer = __outermost(*this);
208 typedef typename std::decay<decltype(__outer)>::type __outer_type;
209 typedef allocator_traits<__outer_type> __o_traits;
210 __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...,
214 template<typename _Alloc>
216 _S_select_on_copy(const _Alloc& __a)
218 typedef allocator_traits<_Alloc> __a_traits;
219 return __a_traits::select_on_container_copy_construction(__a);
222 template<int... _Indices>
223 scoped_allocator_adaptor(tuple<const _OuterAlloc&,
224 const _InnerAllocs&...> __refs,
225 _Index_tuple<_Indices...>)
226 : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
227 _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
231 typedef _OuterAlloc outer_allocator_type;
232 typedef typename __inner_type::__type inner_allocator_type;
234 typedef typename __traits::value_type value_type;
235 typedef typename __traits::size_type size_type;
236 typedef typename __traits::difference_type difference_type;
237 typedef typename __traits::pointer pointer;
238 typedef typename __traits::const_pointer const_pointer;
239 typedef typename __traits::void_pointer void_pointer;
240 typedef typename __traits::const_void_pointer const_void_pointer;
242 typedef typename conditional<
243 __any_of<__propagate_on_copy, _OuterAlloc, _InnerAllocs...>::value,
244 true_type, false_type>::type propagate_on_container_copy_assignment;
245 typedef typename conditional<
246 __any_of<__propagate_on_move, _OuterAlloc, _InnerAllocs...>::value,
247 true_type, false_type>::type propagate_on_container_move_assignment;
248 typedef typename conditional<
249 __any_of<__propagate_on_swap, _OuterAlloc, _InnerAllocs...>::value,
250 true_type, false_type>::type propagate_on_container_swap;
255 // TODO: use rebind_alloc<Tp> instead of __rebind_alloc<Tp>::__type
256 typedef scoped_allocator_adaptor<
257 typename __traits::template __rebind_alloc<_Tp>::__type,
258 _InnerAllocs...> other;
261 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
263 template<typename _Outer2>
264 scoped_allocator_adaptor(_Outer2&& __outer,
265 const _InnerAllocs&... __inner)
266 : _OuterAlloc(std::forward<_Outer2>(__outer)),
270 scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
271 : _OuterAlloc(__other.outer_allocator()),
272 _M_inner(__other._M_inner)
275 scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
276 : _OuterAlloc(std::move(__other.outer_allocator())),
277 _M_inner(std::move(__other._M_inner))
280 template<typename _Outer2>
281 scoped_allocator_adaptor(
282 const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
283 : _OuterAlloc(__other.outer_allocator()),
284 _M_inner(__other._M_inner)
287 template<typename _Outer2>
288 scoped_allocator_adaptor(
289 scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
290 : _OuterAlloc(std::move(__other.outer_allocator())),
291 _M_inner(std::move(__other._M_inner))
294 inner_allocator_type& inner_allocator() noexcept
295 { return _M_inner._M_get(this); }
297 const inner_allocator_type& inner_allocator() const noexcept
298 { return _M_inner._M_get(this); }
300 outer_allocator_type& outer_allocator() noexcept
301 { return static_cast<_OuterAlloc&>(*this); }
303 const outer_allocator_type& outer_allocator() const noexcept
304 { return static_cast<const _OuterAlloc&>(*this); }
306 pointer allocate(size_type __n)
307 { return __traits::allocate(outer_allocator(), __n); }
309 pointer allocate(size_type __n, const_void_pointer __hint)
310 { return __traits::allocate(outer_allocator(), __n, __hint); }
312 void deallocate(pointer __p, size_type __n)
313 { return __traits::deallocate(outer_allocator(), __p, __n); }
315 size_type max_size() const
316 { return __traits::max_size(outer_allocator()); }
318 template<typename _Tp, typename... _Args>
319 void construct(_Tp* __p, _Args&&... __args)
321 auto& __inner = inner_allocator();
323 = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
324 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
327 // TODO: construct pairs
329 template<typename _Tp>
330 void destroy(_Tp* __p)
332 auto& __outer = __outermost(*this);
333 typedef typename std::decay<decltype(__outer)>::type __outer_type;
334 allocator_traits<__outer_type>::destroy(__outer, __p);
337 scoped_allocator_adaptor
338 select_on_container_copy_construction() const
340 typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
342 return scoped_allocator_adaptor(_M_tie(), _Indices());
345 template <typename _OutA1, typename _OutA2, typename... _InA>
347 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
348 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
351 template <typename _OutA1, typename _OutA2, typename... _InA>
353 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
354 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
356 return __a.outer_allocator() == __b.outer_allocator()
357 && __a._M_inner == __b._M_inner;
360 template <typename _OutA1, typename _OutA2, typename... _InA>
362 operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
363 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
364 { return !(__a == __b); }
368 _GLIBCXX_END_NAMESPACE_VERSION
371 #endif // __GXX_EXPERIMENTAL_CXX0X__
373 #endif // _SCOPED_ALLOCATOR