libstdc++
scoped_allocator
Go to the documentation of this file.
1// <scoped_allocator> -*- C++ -*-
2
3// Copyright (C) 2011-2016 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/** @file include/scoped_allocator
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _SCOPED_ALLOCATOR
30#define _SCOPED_ALLOCATOR 1
31
32#pragma GCC system_header
33
34#if __cplusplus < 201103L
35# include <bits/c++0x_warning.h>
36#else
37
38#include <utility>
39#include <tuple>
40#include <bits/alloc_traits.h>
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 /**
47 * @addtogroup allocators
48 * @{
49 */
50
51 template<typename _Alloc>
52 using __outer_allocator_t
53 = decltype(std::declval<_Alloc>().outer_allocator());
54
55 template<typename _Alloc, typename = void>
56 struct __outermost_type
57 {
58 using type = _Alloc;
59 static type& _S_outermost(_Alloc& __a) { return __a; }
60 };
61
62 template<typename _Alloc>
63 struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>>
64 : __outermost_type<
65 typename remove_reference<__outer_allocator_t<_Alloc>>::type
66 >
67 {
68 using __base = __outermost_type<
69 typename remove_reference<__outer_allocator_t<_Alloc>>::type
70 >;
71
72 static typename __base::type&
73 _S_outermost(_Alloc& __a)
74 { return __base::_S_outermost(__a.outer_allocator()); }
75 };
76
77 template<typename _Alloc>
78 inline typename __outermost_type<_Alloc>::type&
79 __outermost(_Alloc& __a)
80 { return __outermost_type<_Alloc>::_S_outermost(__a); }
81
82 template<typename _OuterAlloc, typename... _InnerAllocs>
83 class scoped_allocator_adaptor;
84
85 template<typename...>
86 struct __inner_type_impl;
87
88 template<typename _Outer>
89 struct __inner_type_impl<_Outer>
90 {
91 typedef scoped_allocator_adaptor<_Outer> __type;
92
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;
98
99 template<typename _Alloc>
100 __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
101 { }
102
103 template<typename _Alloc>
104 __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
105 { }
106
107 __type&
108 _M_get(__type* __p) noexcept { return *__p; }
109
110 const __type&
111 _M_get(const __type* __p) const noexcept { return *__p; }
112
113 tuple<>
114 _M_tie() const noexcept { return tuple<>(); }
115
116 bool
117 operator==(const __inner_type_impl&) const noexcept
118 { return true; }
119 };
120
121 template<typename _Outer, typename _InnerHead, typename... _InnerTail>
122 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
123 {
124 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
125
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;
131
132 template<typename... _Allocs>
133 __inner_type_impl(const __inner_type_impl<_Allocs...>& __other)
134 : _M_inner(__other._M_inner) { }
135
136 template<typename... _Allocs>
137 __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
138 : _M_inner(std::move(__other._M_inner)) { }
139
140 template<typename... _Args>
141 explicit
142 __inner_type_impl(_Args&&... __args)
143 : _M_inner(std::forward<_Args>(__args)...) { }
144
145 __type&
146 _M_get(void*) noexcept { return _M_inner; }
147
148 const __type&
149 _M_get(const void*) const noexcept { return _M_inner; }
150
151 tuple<const _InnerHead&, const _InnerTail&...>
152 _M_tie() const noexcept
153 { return _M_inner._M_tie(); }
154
155 bool
156 operator==(const __inner_type_impl& __other) const noexcept
157 { return _M_inner == __other._M_inner; }
158
159 private:
160 template<typename...> friend class __inner_type_impl;
161 template<typename, typename...> friend class scoped_allocator_adaptor;
162
163 __type _M_inner;
164 };
165
166 /// Primary class template.
167 template<typename _OuterAlloc, typename... _InnerAllocs>
168 class scoped_allocator_adaptor
169 : public _OuterAlloc
170 {
171 typedef allocator_traits<_OuterAlloc> __traits;
172
173 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
174 __inner_type _M_inner;
175
176 template<typename _Outer, typename... _Inner>
177 friend class scoped_allocator_adaptor;
178
179 template<typename...>
180 friend class __inner_type_impl;
181
182 tuple<const _OuterAlloc&, const _InnerAllocs&...>
183 _M_tie() const noexcept
184 { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
185
186 template<typename _Alloc>
187 using __outermost_alloc_traits
188 = allocator_traits<typename __outermost_type<_Alloc>::type>;
189
190 template<typename _Tp, typename... _Args>
191 void
192 _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
193 {
194 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
195 _O_traits::construct(__outermost(*this), __p,
196 std::forward<_Args>(__args)...);
197 }
198
199 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
200 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
201
202 template<typename _Tp, typename... _Args>
203 void
204 _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
205 {
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)...);
210 }
211
212 template<typename _Tp, typename... _Args>
213 void
214 _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
215 {
216 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
217 _O_traits::construct(__outermost(*this), __p,
218 std::forward<_Args>(__args)...,
219 inner_allocator());
220 }
221
222 template<typename _Alloc>
223 static _Alloc
224 _S_select_on_copy(const _Alloc& __a)
225 {
226 typedef allocator_traits<_Alloc> __a_traits;
227 return __a_traits::select_on_container_copy_construction(__a);
228 }
229
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))...)
236 { }
237
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
242 >::type;
243
244 public:
245 typedef _OuterAlloc outer_allocator_type;
246 typedef typename __inner_type::__type inner_allocator_type;
247
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;
255
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;
261
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;
267
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;
273
274 typedef typename __and_<
275 typename __traits::is_always_equal,
276 typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
277 is_always_equal;
278
279 template <class _Tp>
280 struct rebind
281 {
282 typedef scoped_allocator_adaptor<
283 typename __traits::template rebind_alloc<_Tp>,
284 _InnerAllocs...> other;
285 };
286
287 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
288
289 template<typename _Outer2, typename = _Constructible<_Outer2>>
290 scoped_allocator_adaptor(_Outer2&& __outer,
291 const _InnerAllocs&... __inner)
292 : _OuterAlloc(std::forward<_Outer2>(__outer)),
293 _M_inner(__inner...)
294 { }
295
296 scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
297 : _OuterAlloc(__other.outer_allocator()),
298 _M_inner(__other._M_inner)
299 { }
300
301 scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
302 : _OuterAlloc(std::move(__other.outer_allocator())),
303 _M_inner(std::move(__other._M_inner))
304 { }
305
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)
311 { }
312
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))
318 { }
319
320 scoped_allocator_adaptor&
321 operator=(const scoped_allocator_adaptor&) = default;
322
323 scoped_allocator_adaptor&
324 operator=(scoped_allocator_adaptor&&) = default;
325
326 inner_allocator_type& inner_allocator() noexcept
327 { return _M_inner._M_get(this); }
328
329 const inner_allocator_type& inner_allocator() const noexcept
330 { return _M_inner._M_get(this); }
331
332 outer_allocator_type& outer_allocator() noexcept
333 { return static_cast<_OuterAlloc&>(*this); }
334
335 const outer_allocator_type& outer_allocator() const noexcept
336 { return static_cast<const _OuterAlloc&>(*this); }
337
338 pointer allocate(size_type __n)
339 { return __traits::allocate(outer_allocator(), __n); }
340
341 pointer allocate(size_type __n, const_void_pointer __hint)
342 { return __traits::allocate(outer_allocator(), __n, __hint); }
343
344 void deallocate(pointer __p, size_type __n)
345 { return __traits::deallocate(outer_allocator(), __p, __n); }
346
347 size_type max_size() const
348 { return __traits::max_size(outer_allocator()); }
349
350 template<typename _Tp, typename... _Args>
351 void construct(_Tp* __p, _Args&&... __args)
352 {
353 auto& __inner = inner_allocator();
354 auto __use_tag
355 = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
356 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
357 }
358
359 template<typename _T1, typename _T2, typename... _Args1,
360 typename... _Args2>
361 void
362 construct(pair<_T1, _T2>* __p, piecewise_construct_t,
363 tuple<_Args1...> __x, tuple<_Args2...> __y)
364 {
365 // _GLIBCXX_RESOLVE_LIB_DEFECTS
366 // 2203. wrong argument types for piecewise construction
367 auto& __inner = inner_allocator();
368 auto __x_use_tag
369 = __use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
370 auto __y_use_tag
371 = __use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
372 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
373 _O_traits::construct(__outermost(*this), __p, piecewise_construct,
374 _M_construct_p(__x_use_tag, __x),
375 _M_construct_p(__y_use_tag, __y));
376 }
377
378 template<typename _T1, typename _T2>
379 void
380 construct(pair<_T1, _T2>* __p)
381 { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
382
383 template<typename _T1, typename _T2, typename _Up, typename _Vp>
384 void
385 construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
386 {
387 construct(__p, piecewise_construct,
388 std::forward_as_tuple(std::forward<_Up>(__u)),
389 std::forward_as_tuple(std::forward<_Vp>(__v)));
390 }
391
392 template<typename _T1, typename _T2, typename _Up, typename _Vp>
393 void
394 construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x)
395 {
396 construct(__p, piecewise_construct,
397 std::forward_as_tuple(__x.first),
398 std::forward_as_tuple(__x.second));
399 }
400
401 template<typename _T1, typename _T2, typename _Up, typename _Vp>
402 void
403 construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
404 {
405 construct(__p, piecewise_construct,
406 std::forward_as_tuple(std::forward<_Up>(__x.first)),
407 std::forward_as_tuple(std::forward<_Vp>(__x.second)));
408 }
409
410 template<typename _Tp>
411 void destroy(_Tp* __p)
412 {
413 typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
414 _O_traits::destroy(__outermost(*this), __p);
415 }
416
417 scoped_allocator_adaptor
418 select_on_container_copy_construction() const
419 {
420 typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
421 _Indices;
422 return scoped_allocator_adaptor(_M_tie(), _Indices());
423 }
424
425 template <typename _OutA1, typename _OutA2, typename... _InA>
426 friend bool
427 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
428 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
429
430 private:
431 template<typename _Tuple>
432 _Tuple&&
433 _M_construct_p(__uses_alloc0, _Tuple& __t)
434 { return std::move(__t); }
435
436 template<typename... _Args>
437 std::tuple<allocator_arg_t, inner_allocator_type&, _Args...>
438 _M_construct_p(__uses_alloc1_, std::tuple<_Args...>& __t)
439 {
440 typedef std::tuple<allocator_arg_t, inner_allocator_type&> _Tuple;
441 return std::tuple_cat(_Tuple(allocator_arg, inner_allocator()),
442 std::move(__t));
443 }
444
445 template<typename... _Args>
446 std::tuple<_Args..., inner_allocator_type&>
447 _M_construct_p(__uses_alloc2_, std::tuple<_Args...>& __t)
448 {
449 typedef std::tuple<inner_allocator_type&> _Tuple;
450 return std::tuple_cat(std::move(__t), _Tuple(inner_allocator()));
451 }
452 };
453
454 template <typename _OutA1, typename _OutA2, typename... _InA>
455 inline bool
456 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
457 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
458 {
459 return __a.outer_allocator() == __b.outer_allocator()
460 && __a._M_inner == __b._M_inner;
461 }
462
463 template <typename _OutA1, typename _OutA2, typename... _InA>
464 inline bool
465 operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
466 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
467 { return !(__a == __b); }
468
469 /// @}
470
471_GLIBCXX_END_NAMESPACE_VERSION
472} // namespace
473
474#endif // C++11
475
476#endif // _SCOPED_ALLOCATOR