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