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