1 // <scoped_allocator> -*- C++ -*-
3 // Copyright (C) 2011-2017 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>
52 using __outer_allocator_t
53 = decltype(std::declval<_Alloc>().outer_allocator());
55 template<typename _Alloc, typename = void>
56 struct __outermost_type
59 static type& _S_outermost(_Alloc& __a) { return __a; }
62 template<typename _Alloc>
63 struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>>
65 typename remove_reference<__outer_allocator_t<_Alloc>>::type
68 using __base = __outermost_type<
69 typename remove_reference<__outer_allocator_t<_Alloc>>::type
72 static typename __base::type&
73 _S_outermost(_Alloc& __a)
74 { return __base::_S_outermost(__a.outer_allocator()); }
77 template<typename _Alloc>
78 inline typename __outermost_type<_Alloc>::type&
79 __outermost(_Alloc& __a)
80 { return __outermost_type<_Alloc>::_S_outermost(__a); }
82 template<typename _OuterAlloc, typename... _InnerAllocs>
83 class scoped_allocator_adaptor;
86 struct __inner_type_impl;
88 template<typename _Outer>
89 struct __inner_type_impl<_Outer>
91 typedef scoped_allocator_adaptor<_Outer> __type;
93 __inner_type_impl() = default;
94 __inner_type_impl(const __inner_type_impl&) = default;
95 __inner_type_impl(__inner_type_impl&&) = default;
96 __inner_type_impl& operator=(const __inner_type_impl&) = default;
97 __inner_type_impl& operator=(__inner_type_impl&&) = default;
99 template<typename _Alloc>
100 __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
103 template<typename _Alloc>
104 __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
108 _M_get(__type* __p) noexcept { return *__p; }
111 _M_get(const __type* __p) const noexcept { return *__p; }
114 _M_tie() const noexcept { return tuple<>(); }
117 operator==(const __inner_type_impl&) const noexcept
121 template<typename _Outer, typename _InnerHead, typename... _InnerTail>
122 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
124 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
126 __inner_type_impl() = default;
127 __inner_type_impl(const __inner_type_impl&) = default;
128 __inner_type_impl(__inner_type_impl&&) = default;
129 __inner_type_impl& operator=(const __inner_type_impl&) = default;
130 __inner_type_impl& operator=(__inner_type_impl&&) = default;
132 template<typename... _Allocs>
133 __inner_type_impl(const __inner_type_impl<_Allocs...>& __other)
134 : _M_inner(__other._M_inner) { }
136 template<typename... _Allocs>
137 __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
138 : _M_inner(std::move(__other._M_inner)) { }
140 template<typename... _Args>
142 __inner_type_impl(_Args&&... __args)
143 : _M_inner(std::forward<_Args>(__args)...) { }
146 _M_get(void*) noexcept { return _M_inner; }
149 _M_get(const void*) const noexcept { return _M_inner; }
151 tuple<const _InnerHead&, const _InnerTail&...>
152 _M_tie() const noexcept
153 { return _M_inner._M_tie(); }
156 operator==(const __inner_type_impl& __other) const noexcept
157 { return _M_inner == __other._M_inner; }
160 template<typename...> friend class __inner_type_impl;
161 template<typename, typename...> friend class scoped_allocator_adaptor;
166 /// Primary class template.
167 template<typename _OuterAlloc, typename... _InnerAllocs>
168 class scoped_allocator_adaptor
171 typedef allocator_traits<_OuterAlloc> __traits;
173 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
174 __inner_type _M_inner;
176 template<typename _Outer, typename... _Inner>
177 friend class scoped_allocator_adaptor;
179 template<typename...>
180 friend class __inner_type_impl;
182 tuple<const _OuterAlloc&, const _InnerAllocs&...>
183 _M_tie() const noexcept
184 { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
186 template<typename _Alloc>
187 using __outermost_alloc_traits
188 = allocator_traits<typename __outermost_type<_Alloc>::type>;
190 template<typename _Tp, typename... _Args>
192 _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
194 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
195 _O_traits::construct(__outermost(*this), __p,
196 std::forward<_Args>(__args)...);
199 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
200 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
202 template<typename _Tp, typename... _Args>
204 _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
206 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
207 _O_traits::construct(__outermost(*this), __p,
208 allocator_arg, inner_allocator(),
209 std::forward<_Args>(__args)...);
212 template<typename _Tp, typename... _Args>
214 _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
216 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
217 _O_traits::construct(__outermost(*this), __p,
218 std::forward<_Args>(__args)...,
222 template<typename _Alloc>
224 _S_select_on_copy(const _Alloc& __a)
226 typedef allocator_traits<_Alloc> __a_traits;
227 return __a_traits::select_on_container_copy_construction(__a);
230 template<std::size_t... _Indices>
231 scoped_allocator_adaptor(tuple<const _OuterAlloc&,
232 const _InnerAllocs&...> __refs,
233 _Index_tuple<_Indices...>)
234 : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
235 _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
238 // Used to constrain constructors to disallow invalid conversions.
239 template<typename _Alloc>
240 using _Constructible = typename enable_if<
241 is_constructible<_OuterAlloc, _Alloc>::value
245 typedef _OuterAlloc outer_allocator_type;
246 typedef typename __inner_type::__type inner_allocator_type;
248 typedef typename __traits::value_type value_type;
249 typedef typename __traits::size_type size_type;
250 typedef typename __traits::difference_type difference_type;
251 typedef typename __traits::pointer pointer;
252 typedef typename __traits::const_pointer const_pointer;
253 typedef typename __traits::void_pointer void_pointer;
254 typedef typename __traits::const_void_pointer const_void_pointer;
256 typedef typename __or_<
257 typename __traits::propagate_on_container_copy_assignment,
258 typename allocator_traits<_InnerAllocs>::
259 propagate_on_container_copy_assignment...>::type
260 propagate_on_container_copy_assignment;
262 typedef typename __or_<
263 typename __traits::propagate_on_container_move_assignment,
264 typename allocator_traits<_InnerAllocs>::
265 propagate_on_container_move_assignment...>::type
266 propagate_on_container_move_assignment;
268 typedef typename __or_<
269 typename __traits::propagate_on_container_swap,
270 typename allocator_traits<_InnerAllocs>::
271 propagate_on_container_swap...>::type
272 propagate_on_container_swap;
274 typedef typename __and_<
275 typename __traits::is_always_equal,
276 typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
282 typedef scoped_allocator_adaptor<
283 typename __traits::template rebind_alloc<_Tp>,
284 _InnerAllocs...> other;
287 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
289 template<typename _Outer2, typename = _Constructible<_Outer2>>
290 scoped_allocator_adaptor(_Outer2&& __outer,
291 const _InnerAllocs&... __inner)
292 : _OuterAlloc(std::forward<_Outer2>(__outer)),
296 scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
297 : _OuterAlloc(__other.outer_allocator()),
298 _M_inner(__other._M_inner)
301 scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
302 : _OuterAlloc(std::move(__other.outer_allocator())),
303 _M_inner(std::move(__other._M_inner))
306 template<typename _Outer2, typename = _Constructible<const _Outer2&>>
307 scoped_allocator_adaptor(
308 const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
309 : _OuterAlloc(__other.outer_allocator()),
310 _M_inner(__other._M_inner)
313 template<typename _Outer2, typename = _Constructible<_Outer2>>
314 scoped_allocator_adaptor(
315 scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
316 : _OuterAlloc(std::move(__other.outer_allocator())),
317 _M_inner(std::move(__other._M_inner))
320 scoped_allocator_adaptor&
321 operator=(const scoped_allocator_adaptor&) = default;
323 scoped_allocator_adaptor&
324 operator=(scoped_allocator_adaptor&&) = default;
326 inner_allocator_type& inner_allocator() noexcept
327 { return _M_inner._M_get(this); }
329 const inner_allocator_type& inner_allocator() const noexcept
330 { return _M_inner._M_get(this); }
332 outer_allocator_type& outer_allocator() noexcept
333 { return static_cast<_OuterAlloc&>(*this); }
335 const outer_allocator_type& outer_allocator() const noexcept
336 { return static_cast<const _OuterAlloc&>(*this); }
338 pointer allocate(size_type __n)
339 { return __traits::allocate(outer_allocator(), __n); }
341 pointer allocate(size_type __n, const_void_pointer __hint)
342 { return __traits::allocate(outer_allocator(), __n, __hint); }
344 void deallocate(pointer __p, size_type __n)
345 { return __traits::deallocate(outer_allocator(), __p, __n); }
347 size_type max_size() const
348 { return __traits::max_size(outer_allocator()); }
350 template<typename _Tp, typename... _Args>
351 void construct(_Tp* __p, _Args&&... __args)
353 auto& __inner = inner_allocator();
355 = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
356 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
359 template<typename _T1, typename _T2, typename... _Args1,
362 construct(pair<_T1, _T2>* __p, piecewise_construct_t,
363 tuple<_Args1...> __x, tuple<_Args2...> __y)
365 // _GLIBCXX_RESOLVE_LIB_DEFECTS
366 // 2203. wrong argument types for piecewise construction
367 auto& __inner = inner_allocator();
369 = __use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
371 = __use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
372 typename _Build_index_tuple<sizeof...(_Args1)>::__type __x_indices;
373 typename _Build_index_tuple<sizeof...(_Args2)>::__type __y_indices;
374 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
375 _O_traits::construct(__outermost(*this), __p, piecewise_construct,
376 _M_construct_p(__x_use_tag, __x_indices, __x),
377 _M_construct_p(__y_use_tag, __y_indices, __y));
380 template<typename _T1, typename _T2>
382 construct(pair<_T1, _T2>* __p)
383 { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
385 template<typename _T1, typename _T2, typename _Up, typename _Vp>
387 construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
389 construct(__p, piecewise_construct,
390 std::forward_as_tuple(std::forward<_Up>(__u)),
391 std::forward_as_tuple(std::forward<_Vp>(__v)));
394 template<typename _T1, typename _T2, typename _Up, typename _Vp>
396 construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x)
398 construct(__p, piecewise_construct,
399 std::forward_as_tuple(__x.first),
400 std::forward_as_tuple(__x.second));
403 template<typename _T1, typename _T2, typename _Up, typename _Vp>
405 construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
407 construct(__p, piecewise_construct,
408 std::forward_as_tuple(std::forward<_Up>(__x.first)),
409 std::forward_as_tuple(std::forward<_Vp>(__x.second)));
412 template<typename _Tp>
413 void destroy(_Tp* __p)
415 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
416 _O_traits::destroy(__outermost(*this), __p);
419 scoped_allocator_adaptor
420 select_on_container_copy_construction() const
422 typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
424 return scoped_allocator_adaptor(_M_tie(), _Indices());
427 template <typename _OutA1, typename _OutA2, typename... _InA>
429 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
430 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
433 template<typename _Ind, typename... _Args>
435 _M_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
436 { return std::move(__t); }
438 template<size_t... _Ind, typename... _Args>
439 tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
440 _M_construct_p(__uses_alloc1_, _Index_tuple<_Ind...>,
441 tuple<_Args...>& __t)
443 return { allocator_arg, inner_allocator(),
444 std::get<_Ind>(std::move(__t))...
448 template<size_t... _Ind, typename... _Args>
449 tuple<_Args&&..., inner_allocator_type&>
450 _M_construct_p(__uses_alloc2_, _Index_tuple<_Ind...>,
451 tuple<_Args...>& __t)
453 return { std::get<_Ind>(std::move(__t))..., inner_allocator() };
457 template <typename _OutA1, typename _OutA2, typename... _InA>
459 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
460 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
462 return __a.outer_allocator() == __b.outer_allocator()
463 && __a._M_inner == __b._M_inner;
466 template <typename _OutA1, typename _OutA2, typename... _InA>
468 operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
469 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
470 { return !(__a == __b); }
474 _GLIBCXX_END_NAMESPACE_VERSION
479 #endif // _SCOPED_ALLOCATOR