1 // <scoped_allocator> -*- C++ -*-
3 // Copyright (C) 2011, 2012 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;
97 struct __inner_type_impl;
99 template<typename _Outer>
100 struct __inner_type_impl<_Outer>
102 typedef scoped_allocator_adaptor<_Outer> __type;
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;
173 /// Primary class template.
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;
249 typedef typename __traits::value_type value_type;
250 typedef typename __traits::size_type size_type;
251 typedef typename __traits::difference_type difference_type;
252 typedef typename __traits::pointer pointer;
253 typedef typename __traits::const_pointer const_pointer;
254 typedef typename __traits::void_pointer void_pointer;
255 typedef typename __traits::const_void_pointer const_void_pointer;
257 typedef typename conditional<
258 __any_of<__propagate_on_copy, _OuterAlloc, _InnerAllocs...>::value,
259 true_type, false_type>::type propagate_on_container_copy_assignment;
260 typedef typename conditional<
261 __any_of<__propagate_on_move, _OuterAlloc, _InnerAllocs...>::value,
262 true_type, false_type>::type propagate_on_container_move_assignment;
263 typedef typename conditional<
264 __any_of<__propagate_on_swap, _OuterAlloc, _InnerAllocs...>::value,
265 true_type, false_type>::type propagate_on_container_swap;
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)
321 { return __traits::allocate(outer_allocator(), __n); }
323 pointer allocate(size_type __n, const_void_pointer __hint)
324 { return __traits::allocate(outer_allocator(), __n, __hint); }
326 void deallocate(pointer __p, size_type __n)
327 { return __traits::deallocate(outer_allocator(), __p, __n); }
329 size_type max_size() const
330 { return __traits::max_size(outer_allocator()); }
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)...);
341 // TODO: construct pairs
343 template<typename _Tp>
344 void destroy(_Tp* __p)
346 auto& __outer = __outermost(*this);
347 typedef typename std::decay<decltype(__outer)>::type __outer_type;
348 allocator_traits<__outer_type>::destroy(__outer, __p);
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