1 // <scoped_allocator> -*- C++ -*-
3 // Copyright (C) 2011-2016 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 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
40 #include <bits/alloc_traits.h>
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 * @addtogroup allocators
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, ...)
61 // TODO: make recursive (see note in 20.12.4/1)
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>
69 class scoped_allocator_adaptor;
72 struct __inner_type_impl;
74 template<typename _Outer>
75 struct __inner_type_impl<_Outer>
77 typedef scoped_allocator_adaptor<_Outer> __type;
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;
152 /// Primary class template.
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;
232 typedef typename __traits::value_type value_type;
233 typedef typename __traits::size_type size_type;
234 typedef typename __traits::difference_type difference_type;
235 typedef typename __traits::pointer pointer;
236 typedef typename __traits::const_pointer const_pointer;
237 typedef typename __traits::void_pointer void_pointer;
238 typedef typename __traits::const_void_pointer const_void_pointer;
240 typedef typename __or_<
241 typename __traits::propagate_on_container_copy_assignment,
242 typename allocator_traits<_InnerAllocs>::
243 propagate_on_container_copy_assignment...>::type
244 propagate_on_container_copy_assignment;
246 typedef typename __or_<
247 typename __traits::propagate_on_container_move_assignment,
248 typename allocator_traits<_InnerAllocs>::
249 propagate_on_container_move_assignment...>::type
250 propagate_on_container_move_assignment;
252 typedef typename __or_<
253 typename __traits::propagate_on_container_swap,
254 typename allocator_traits<_InnerAllocs>::
255 propagate_on_container_swap...>::type
256 propagate_on_container_swap;
258 typedef typename __and_<
259 typename __traits::is_always_equal,
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)
329 { return __traits::deallocate(outer_allocator(), __p, __n); }
331 size_type max_size() const
332 { return __traits::max_size(outer_allocator()); }
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)
349 // _GLIBCXX_RESOLVE_LIB_DEFECTS
350 // 2203. wrong argument types for piecewise construction
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;
357 _O_traits::construct(__outermost(*this), __p, piecewise_construct,
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)
365 { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
367 template<typename _T1, typename _T2, typename _Up, typename _Vp>
369 construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
371 construct(__p, piecewise_construct,
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)
380 construct(__p, piecewise_construct,
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)
389 construct(__p, piecewise_construct,
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...>
422 _M_construct_p(__uses_alloc1_, std::tuple<_Args...>& __t)
424 typedef std::tuple<allocator_arg_t, inner_allocator_type&> _Tuple;
425 return std::tuple_cat(_Tuple(allocator_arg, inner_allocator()),
429 template<typename... _Args>
430 std::tuple<_Args..., inner_allocator_type&>
431 _M_construct_p(__uses_alloc2_, std::tuple<_Args...>& __t)
433 typedef std::tuple<inner_allocator_type&> _Tuple;
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