libstdc++
scoped_allocator
Go to the documentation of this file.
1 // <scoped_allocator> -*- C++ -*-
2 
3 // Copyright (C) 2011-2019 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 
42 namespace 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  // _GLIBCXX_RESOLVE_LIB_DEFECTS
245  // 2975. Missing case for pair construction in scoped [...] allocators
246  template<typename _Tp>
247  struct __not_pair { using type = void; };
248 
249  template<typename _Tp, typename _Up>
250  struct __not_pair<pair<_Tp, _Up>> { };
251 
252  public:
253  typedef _OuterAlloc outer_allocator_type;
254  typedef typename __inner_type::__type inner_allocator_type;
255 
256  typedef typename __traits::value_type value_type;
257  typedef typename __traits::size_type size_type;
258  typedef typename __traits::difference_type difference_type;
259  typedef typename __traits::pointer pointer;
260  typedef typename __traits::const_pointer const_pointer;
261  typedef typename __traits::void_pointer void_pointer;
262  typedef typename __traits::const_void_pointer const_void_pointer;
263 
264  typedef typename __or_<
265  typename __traits::propagate_on_container_copy_assignment,
266  typename allocator_traits<_InnerAllocs>::
267  propagate_on_container_copy_assignment...>::type
268  propagate_on_container_copy_assignment;
269 
270  typedef typename __or_<
271  typename __traits::propagate_on_container_move_assignment,
272  typename allocator_traits<_InnerAllocs>::
273  propagate_on_container_move_assignment...>::type
274  propagate_on_container_move_assignment;
275 
276  typedef typename __or_<
277  typename __traits::propagate_on_container_swap,
278  typename allocator_traits<_InnerAllocs>::
279  propagate_on_container_swap...>::type
280  propagate_on_container_swap;
281 
282  typedef typename __and_<
283  typename __traits::is_always_equal,
284  typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
285  is_always_equal;
286 
287  template <class _Tp>
288  struct rebind
289  {
290  typedef scoped_allocator_adaptor<
291  typename __traits::template rebind_alloc<_Tp>,
292  _InnerAllocs...> other;
293  };
294 
295  scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
296 
297  template<typename _Outer2, typename = _Constructible<_Outer2>>
298  scoped_allocator_adaptor(_Outer2&& __outer,
299  const _InnerAllocs&... __inner)
300  : _OuterAlloc(std::forward<_Outer2>(__outer)),
301  _M_inner(__inner...)
302  { }
303 
304  scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
305  : _OuterAlloc(__other.outer_allocator()),
306  _M_inner(__other._M_inner)
307  { }
308 
309  scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
310  : _OuterAlloc(std::move(__other.outer_allocator())),
311  _M_inner(std::move(__other._M_inner))
312  { }
313 
314  template<typename _Outer2, typename = _Constructible<const _Outer2&>>
315  scoped_allocator_adaptor(
316  const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
317  : _OuterAlloc(__other.outer_allocator()),
318  _M_inner(__other._M_inner)
319  { }
320 
321  template<typename _Outer2, typename = _Constructible<_Outer2>>
322  scoped_allocator_adaptor(
323  scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
324  : _OuterAlloc(std::move(__other.outer_allocator())),
325  _M_inner(std::move(__other._M_inner))
326  { }
327 
328  scoped_allocator_adaptor&
329  operator=(const scoped_allocator_adaptor&) = default;
330 
331  scoped_allocator_adaptor&
332  operator=(scoped_allocator_adaptor&&) = default;
333 
334  inner_allocator_type& inner_allocator() noexcept
335  { return _M_inner._M_get(this); }
336 
337  const inner_allocator_type& inner_allocator() const noexcept
338  { return _M_inner._M_get(this); }
339 
340  outer_allocator_type& outer_allocator() noexcept
341  { return static_cast<_OuterAlloc&>(*this); }
342 
343  const outer_allocator_type& outer_allocator() const noexcept
344  { return static_cast<const _OuterAlloc&>(*this); }
345 
346  _GLIBCXX_NODISCARD pointer allocate(size_type __n)
347  { return __traits::allocate(outer_allocator(), __n); }
348 
349  _GLIBCXX_NODISCARD pointer allocate(size_type __n, const_void_pointer __hint)
350  { return __traits::allocate(outer_allocator(), __n, __hint); }
351 
352  void deallocate(pointer __p, size_type __n)
353  { return __traits::deallocate(outer_allocator(), __p, __n); }
354 
355  size_type max_size() const
356  { return __traits::max_size(outer_allocator()); }
357 
358  template<typename _Tp, typename... _Args>
359  typename __not_pair<_Tp>::type
360  construct(_Tp* __p, _Args&&... __args)
361  {
362  auto& __inner = inner_allocator();
363  auto __use_tag
364  = std::__use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
365  _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
366  }
367 
368  template<typename _T1, typename _T2, typename... _Args1,
369  typename... _Args2>
370  void
371  construct(pair<_T1, _T2>* __p, piecewise_construct_t,
372  tuple<_Args1...> __x, tuple<_Args2...> __y)
373  {
374  // _GLIBCXX_RESOLVE_LIB_DEFECTS
375  // 2203. wrong argument types for piecewise construction
376  auto& __inner = inner_allocator();
377  auto __x_use_tag
378  = std::__use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
379  auto __y_use_tag
380  = std::__use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
381  typename _Build_index_tuple<sizeof...(_Args1)>::__type __x_indices;
382  typename _Build_index_tuple<sizeof...(_Args2)>::__type __y_indices;
383  typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
384  _O_traits::construct(__outermost(*this), __p, piecewise_construct,
385  _M_construct_p(__x_use_tag, __x_indices, __x),
386  _M_construct_p(__y_use_tag, __y_indices, __y));
387  }
388 
389  template<typename _T1, typename _T2>
390  void
391  construct(pair<_T1, _T2>* __p)
392  { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
393 
394  template<typename _T1, typename _T2, typename _Up, typename _Vp>
395  void
396  construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
397  {
398  construct(__p, piecewise_construct,
399  std::forward_as_tuple(std::forward<_Up>(__u)),
400  std::forward_as_tuple(std::forward<_Vp>(__v)));
401  }
402 
403  template<typename _T1, typename _T2, typename _Up, typename _Vp>
404  void
405  construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x)
406  {
407  construct(__p, piecewise_construct,
408  std::forward_as_tuple(__x.first),
409  std::forward_as_tuple(__x.second));
410  }
411 
412  template<typename _T1, typename _T2, typename _Up, typename _Vp>
413  void
414  construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
415  {
416  construct(__p, piecewise_construct,
417  std::forward_as_tuple(std::forward<_Up>(__x.first)),
418  std::forward_as_tuple(std::forward<_Vp>(__x.second)));
419  }
420 
421  template<typename _Tp>
422  void destroy(_Tp* __p)
423  {
424  typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
425  _O_traits::destroy(__outermost(*this), __p);
426  }
427 
428  scoped_allocator_adaptor
429  select_on_container_copy_construction() const
430  {
431  typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
432  _Indices;
433  return scoped_allocator_adaptor(_M_tie(), _Indices());
434  }
435 
436  template <typename _OutA1, typename _OutA2, typename... _InA>
437  friend bool
438  operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
439  const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
440 
441  private:
442  template<typename _Ind, typename... _Args>
443  tuple<_Args&&...>
444  _M_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
445  { return std::move(__t); }
446 
447  template<size_t... _Ind, typename... _Args>
448  tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
449  _M_construct_p(__uses_alloc1_, _Index_tuple<_Ind...>,
450  tuple<_Args...>& __t)
451  {
452  return { allocator_arg, inner_allocator(),
453  std::get<_Ind>(std::move(__t))...
454  };
455  }
456 
457  template<size_t... _Ind, typename... _Args>
458  tuple<_Args&&..., inner_allocator_type&>
459  _M_construct_p(__uses_alloc2_, _Index_tuple<_Ind...>,
460  tuple<_Args...>& __t)
461  {
462  return { std::get<_Ind>(std::move(__t))..., inner_allocator() };
463  }
464  };
465 
466  template <typename _OutA1, typename _OutA2, typename... _InA>
467  inline bool
468  operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
469  const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
470  {
471  return __a.outer_allocator() == __b.outer_allocator()
472  && __a._M_inner == __b._M_inner;
473  }
474 
475  template <typename _OutA1, typename _OutA2, typename... _InA>
476  inline bool
477  operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
478  const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
479  { return !(__a == __b); }
480 
481  /// @}
482 
483 _GLIBCXX_END_NAMESPACE_VERSION
484 } // namespace
485 
486 #endif // C++11
487 
488 #endif // _SCOPED_ALLOCATOR