libstdc++
scoped_allocator
Go to the documentation of this file.
1// <scoped_allocator> -*- C++ -*-
2
3// Copyright (C) 2011-2020 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 * @ingroup allocators
28 */
29
30#ifndef _SCOPED_ALLOCATOR
31#define _SCOPED_ALLOCATOR 1
32
33#pragma GCC system_header
34
35#if __cplusplus < 201103L
36# include <bits/c++0x_warning.h>
37#else
38
39#include <memory>
40#include <utility>
41#include <tuple>
42#include <bits/alloc_traits.h>
43
44namespace std _GLIBCXX_VISIBILITY(default)
45{
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47
48 /**
49 * @addtogroup allocators
50 * @{
51 */
52
53 /// @cond undocumented
54
55 template<typename _Alloc>
56 using __outer_allocator_t
57 = decltype(std::declval<_Alloc>().outer_allocator());
58
59 template<typename _Alloc, typename = void>
60 struct __outermost_type
61 {
62 using type = _Alloc;
63 static type& _S_outermost(_Alloc& __a) { return __a; }
64 };
65
66 template<typename _Alloc>
67 struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>>
68 : __outermost_type<
69 typename remove_reference<__outer_allocator_t<_Alloc>>::type
70 >
71 {
72 using __base = __outermost_type<
73 typename remove_reference<__outer_allocator_t<_Alloc>>::type
74 >;
75
76 static typename __base::type&
77 _S_outermost(_Alloc& __a)
78 { return __base::_S_outermost(__a.outer_allocator()); }
79 };
80
81 /// Implementation of the OUTERMOST pseudofunction
82 template<typename _Alloc>
83 inline typename __outermost_type<_Alloc>::type&
84 __outermost(_Alloc& __a)
85 { return __outermost_type<_Alloc>::_S_outermost(__a); }
86
87 template<typename _OuterAlloc, typename... _InnerAllocs>
88 class scoped_allocator_adaptor;
89
90 template<typename...>
91 struct __inner_type_impl;
92
93 template<typename _Outer>
94 struct __inner_type_impl<_Outer>
95 {
96 typedef scoped_allocator_adaptor<_Outer> __type;
97
98 __inner_type_impl() = default;
99 __inner_type_impl(const __inner_type_impl&) = default;
100 __inner_type_impl(__inner_type_impl&&) = default;
101 __inner_type_impl& operator=(const __inner_type_impl&) = default;
102 __inner_type_impl& operator=(__inner_type_impl&&) = default;
103
104 template<typename _Alloc>
105 __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
106 { }
107
108 template<typename _Alloc>
109 __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
110 { }
111
112 __type&
113 _M_get(__type* __p) noexcept { return *__p; }
114
115 const __type&
116 _M_get(const __type* __p) const noexcept { return *__p; }
117
118 tuple<>
119 _M_tie() const noexcept { return tuple<>(); }
120
121 bool
122 operator==(const __inner_type_impl&) const noexcept
123 { return true; }
124 };
125
126 template<typename _Outer, typename _InnerHead, typename... _InnerTail>
127 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
128 {
129 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
130
131 __inner_type_impl() = default;
132 __inner_type_impl(const __inner_type_impl&) = default;
133 __inner_type_impl(__inner_type_impl&&) = default;
134 __inner_type_impl& operator=(const __inner_type_impl&) = default;
135 __inner_type_impl& operator=(__inner_type_impl&&) = default;
136
137 template<typename... _Allocs>
138 __inner_type_impl(const __inner_type_impl<_Allocs...>& __other)
139 : _M_inner(__other._M_inner) { }
140
141 template<typename... _Allocs>
142 __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
143 : _M_inner(std::move(__other._M_inner)) { }
144
145 template<typename... _Args>
146 explicit
147 __inner_type_impl(_Args&&... __args)
148 : _M_inner(std::forward<_Args>(__args)...) { }
149
150 __type&
151 _M_get(void*) noexcept { return _M_inner; }
152
153 const __type&
154 _M_get(const void*) const noexcept { return _M_inner; }
155
156 tuple<const _InnerHead&, const _InnerTail&...>
157 _M_tie() const noexcept
158 { return _M_inner._M_tie(); }
159
160 bool
161 operator==(const __inner_type_impl& __other) const noexcept
162 { return _M_inner == __other._M_inner; }
163
164 private:
165 template<typename...> friend class __inner_type_impl;
166 template<typename, typename...> friend class scoped_allocator_adaptor;
167
168 __type _M_inner;
169 };
170
171 /// @endcond
172
173 /// An adaptor to recursively pass an allocator to the objects it constructs
174 template<typename _OuterAlloc, typename... _InnerAllocs>
176 : public _OuterAlloc
177 {
179
180 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
181 __inner_type _M_inner;
182
183 template<typename _Outer, typename... _Inner>
184 friend class scoped_allocator_adaptor;
185
186 template<typename...>
187 friend class __inner_type_impl;
188
189 tuple<const _OuterAlloc&, const _InnerAllocs&...>
190 _M_tie() const noexcept
191 { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
192
193 template<typename _Alloc>
196
197#if __cplusplus <= 201703
198 template<typename _Tp, typename... _Args>
199 void
200 _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
201 {
203 _O_traits::construct(__outermost(*this), __p,
204 std::forward<_Args>(__args)...);
205 }
206
207 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
208 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
209
210 template<typename _Tp, typename... _Args>
211 void
212 _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
213 {
215 _O_traits::construct(__outermost(*this), __p,
216 allocator_arg, inner_allocator(),
217 std::forward<_Args>(__args)...);
218 }
219
220 template<typename _Tp, typename... _Args>
221 void
222 _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
223 {
225 _O_traits::construct(__outermost(*this), __p,
226 std::forward<_Args>(__args)...,
227 inner_allocator());
228 }
229#endif // C++17
230
231 template<typename _Alloc>
232 static _Alloc
233 _S_select_on_copy(const _Alloc& __a)
234 {
235 typedef allocator_traits<_Alloc> __a_traits;
236 return __a_traits::select_on_container_copy_construction(__a);
237 }
238
239 template<std::size_t... _Indices>
240 scoped_allocator_adaptor(tuple<const _OuterAlloc&,
241 const _InnerAllocs&...> __refs,
242 _Index_tuple<_Indices...>)
243 : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
244 _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
245 { }
246
247 // Used to constrain constructors to disallow invalid conversions.
248 template<typename _Alloc>
249 using _Constructible = typename enable_if<
251 >::type;
252
253 // _GLIBCXX_RESOLVE_LIB_DEFECTS
254 // 2975. Missing case for pair construction in scoped [...] allocators
255 template<typename _Tp>
256 struct __not_pair { using type = void; };
257
258 template<typename _Tp, typename _Up>
259 struct __not_pair<pair<_Tp, _Up>> { };
260
261 public:
262 typedef _OuterAlloc outer_allocator_type;
263 typedef typename __inner_type::__type inner_allocator_type;
264
265 typedef typename __traits::value_type value_type;
266 typedef typename __traits::size_type size_type;
267 typedef typename __traits::difference_type difference_type;
268 typedef typename __traits::pointer pointer;
269 typedef typename __traits::const_pointer const_pointer;
270 typedef typename __traits::void_pointer void_pointer;
271 typedef typename __traits::const_void_pointer const_void_pointer;
272
273 typedef typename __or_<
276 propagate_on_container_copy_assignment...>::type
277 propagate_on_container_copy_assignment;
278
279 typedef typename __or_<
282 propagate_on_container_move_assignment...>::type
283 propagate_on_container_move_assignment;
284
285 typedef typename __or_<
288 propagate_on_container_swap...>::type
289 propagate_on_container_swap;
290
291 typedef typename __and_<
294 is_always_equal;
295
296 template <class _Tp>
297 struct rebind
298 {
300 typename __traits::template rebind_alloc<_Tp>,
301 _InnerAllocs...> other;
302 };
303
304 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
305
306 template<typename _Outer2, typename = _Constructible<_Outer2>>
307 scoped_allocator_adaptor(_Outer2&& __outer,
308 const _InnerAllocs&... __inner)
309 : _OuterAlloc(std::forward<_Outer2>(__outer)),
310 _M_inner(__inner...)
311 { }
312
314 : _OuterAlloc(__other.outer_allocator()),
315 _M_inner(__other._M_inner)
316 { }
317
319 : _OuterAlloc(std::move(__other.outer_allocator())),
320 _M_inner(std::move(__other._M_inner))
321 { }
322
323 template<typename _Outer2, typename = _Constructible<const _Outer2&>>
326 : _OuterAlloc(__other.outer_allocator()),
327 _M_inner(__other._M_inner)
328 { }
329
330 template<typename _Outer2, typename = _Constructible<_Outer2>>
333 : _OuterAlloc(std::move(__other.outer_allocator())),
334 _M_inner(std::move(__other._M_inner))
335 { }
336
338 operator=(const scoped_allocator_adaptor&) = default;
339
341 operator=(scoped_allocator_adaptor&&) = default;
342
343 inner_allocator_type& inner_allocator() noexcept
344 { return _M_inner._M_get(this); }
345
346 const inner_allocator_type& inner_allocator() const noexcept
347 { return _M_inner._M_get(this); }
348
349 outer_allocator_type& outer_allocator() noexcept
350 { return static_cast<_OuterAlloc&>(*this); }
351
352 const outer_allocator_type& outer_allocator() const noexcept
353 { return static_cast<const _OuterAlloc&>(*this); }
354
355 _GLIBCXX_NODISCARD pointer allocate(size_type __n)
356 { return __traits::allocate(outer_allocator(), __n); }
357
358 _GLIBCXX_NODISCARD pointer allocate(size_type __n, const_void_pointer __hint)
359 { return __traits::allocate(outer_allocator(), __n, __hint); }
360
361 void deallocate(pointer __p, size_type __n)
362 { return __traits::deallocate(outer_allocator(), __p, __n); }
363
364 size_type max_size() const
365 { return __traits::max_size(outer_allocator()); }
366
367#if __cplusplus <= 201703
368 template<typename _Tp, typename... _Args>
369 typename __not_pair<_Tp>::type
370 construct(_Tp* __p, _Args&&... __args)
371 {
372 auto& __inner = inner_allocator();
373 auto __use_tag
374 = std::__use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
375 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
376 }
377
378 template<typename _T1, typename _T2, typename... _Args1,
379 typename... _Args2>
380 void
383 {
384 // _GLIBCXX_RESOLVE_LIB_DEFECTS
385 // 2203. wrong argument types for piecewise construction
386 auto& __inner = inner_allocator();
387 auto __x_use_tag
388 = std::__use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
389 auto __y_use_tag
390 = std::__use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
391 typename _Build_index_tuple<sizeof...(_Args1)>::__type __x_indices;
392 typename _Build_index_tuple<sizeof...(_Args2)>::__type __y_indices;
394 _O_traits::construct(__outermost(*this), __p, piecewise_construct,
395 _M_construct_p(__x_use_tag, __x_indices, __x),
396 _M_construct_p(__y_use_tag, __y_indices, __y));
397 }
398
399 template<typename _T1, typename _T2>
400 void
401 construct(pair<_T1, _T2>* __p)
402 { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
403
404 template<typename _T1, typename _T2, typename _Up, typename _Vp>
405 void
406 construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
407 {
408 construct(__p, piecewise_construct,
409 std::forward_as_tuple(std::forward<_Up>(__u)),
410 std::forward_as_tuple(std::forward<_Vp>(__v)));
411 }
412
413 template<typename _T1, typename _T2, typename _Up, typename _Vp>
414 void
415 construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x)
416 {
417 construct(__p, piecewise_construct,
420 }
421
422 template<typename _T1, typename _T2, typename _Up, typename _Vp>
423 void
424 construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
425 {
426 construct(__p, piecewise_construct,
427 std::forward_as_tuple(std::forward<_Up>(__x.first)),
428 std::forward_as_tuple(std::forward<_Vp>(__x.second)));
429 }
430#else // C++2a
431 template<typename _Tp, typename... _Args>
432 __attribute__((__nonnull__))
433 void
434 construct(_Tp* __p, _Args&&... __args)
435 {
437 std::apply([__p, this](auto&&... __newargs) {
438 _O_traits::construct(__outermost(*this), __p,
439 std::forward<decltype(__newargs)>(__newargs)...);
440 },
441 uses_allocator_construction_args<_Tp>(inner_allocator(),
442 std::forward<_Args>(__args)...));
443 }
444#endif // C++2a
445
446 template<typename _Tp>
447 void destroy(_Tp* __p)
448 {
450 _O_traits::destroy(__outermost(*this), __p);
451 }
452
454 select_on_container_copy_construction() const
455 {
456 typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
457 _Indices;
458 return scoped_allocator_adaptor(_M_tie(), _Indices());
459 }
460
461 template <typename _OutA1, typename _OutA2, typename... _InA>
462 friend bool
463 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
465
466 private:
467#if __cplusplus <= 201703L
468 template<typename _Ind, typename... _Args>
469 tuple<_Args&&...>
470 _M_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
471 { return std::move(__t); }
472
473 template<size_t... _Ind, typename... _Args>
474 tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
475 _M_construct_p(__uses_alloc1_, _Index_tuple<_Ind...>,
476 tuple<_Args...>& __t)
477 {
478 return { allocator_arg, inner_allocator(),
479 std::get<_Ind>(std::move(__t))...
480 };
481 }
482
483 template<size_t... _Ind, typename... _Args>
484 tuple<_Args&&..., inner_allocator_type&>
485 _M_construct_p(__uses_alloc2_, _Index_tuple<_Ind...>,
486 tuple<_Args...>& __t)
487 {
488 return { std::get<_Ind>(std::move(__t))..., inner_allocator() };
489 }
490#endif // C++17
491 };
492
493 /// @related std::scoped_allocator_adaptor
494 template <typename _OutA1, typename _OutA2, typename... _InA>
495 inline bool
496 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
498 {
499 return __a.outer_allocator() == __b.outer_allocator()
500 && __a._M_inner == __b._M_inner;
501 }
502
503 /// @related std::scoped_allocator_adaptor
504 template <typename _OutA1, typename _OutA2, typename... _InA>
505 inline bool
506 operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
507 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
508 { return !(__a == __b); }
509
510 /// @}
511
512_GLIBCXX_END_NAMESPACE_VERSION
513} // namespace
514
515#endif // C++11
516
517#endif // _SCOPED_ALLOCATOR
_T1 first
The first member.
Definition: stl_pair.h:216
constexpr auto tuple_cat(_Tpls &&... __tpls) -> typename __tuple_cat_result< _Tpls... >::__type
tuple_cat
Definition: tuple:1600
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
std::forward_as_tuple
Definition: tuple:1485
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
_T2 second
The second member.
Definition: stl_pair.h:217
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition: stl_pair.h:82
constexpr tuple< _Elements &... > tie(_Elements &... __args) noexcept
tie
Definition: tuple:1614
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:76
ISO C++ entities toplevel namespace is std.
constexpr _Iterator __base(_Iterator __it)
An adaptor to recursively pass an allocator to the objects it constructs.
Primary class template, tuple.
Definition: tuple:516
is_constructible
Definition: type_traits:908
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:2170
Uniform interface to all allocator types.
__detected_or_t< false_type, __pocma, _Alloc > propagate_on_container_move_assignment
How the allocator is propagated on move assignment.
typename _Ptr< __v_pointer, void >::type void_pointer
The allocator's void pointer type.
__detected_or_t< value_type *, __pointer, _Alloc > pointer
The allocator's pointer type.
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
typename _Size< _Alloc, difference_type >::type size_type
The allocator's size type.
typename _Ptr< __cv_pointer, const void >::type const_void_pointer
The allocator's const void pointer type.
typename _Diff< _Alloc, pointer >::type difference_type
The allocator's difference type.
typename _Ptr< __c_pointer, const value_type >::type const_pointer
The allocator's const pointer type.
_Alloc::value_type value_type
The allocated type.
static constexpr void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
static constexpr size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.
__detected_or_t< false_type, __pocca, _Alloc > propagate_on_container_copy_assignment
How the allocator is propagated on copy assignment.
__detected_or_t< false_type, __pocs, _Alloc > propagate_on_container_swap
How the allocator is propagated on swap.
__detected_or_t< typename is_empty< _Alloc >::type, __equal, _Alloc > is_always_equal
Whether all instances of the allocator type compare equal.
Tag type for piecewise construction of std::pair objects.
Definition: stl_pair.h:79
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:212
[allocator.tag]