libstdc++
bits/alloc_traits.h
Go to the documentation of this file.
1 // Allocator traits -*- C++ -*-
2 
3 // Copyright (C) 2011 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 bits/alloc_traits.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{memory}
28  */
29 
30 #ifndef _ALLOC_TRAITS_H
31 #define _ALLOC_TRAITS_H 1
32 
33 #ifdef __GXX_EXPERIMENTAL_CXX0X__
34 
35 #include <bits/ptr_traits.h>
36 #include <ext/numeric_traits.h>
37 
38 namespace std _GLIBCXX_VISIBILITY(default)
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 
42  template<typename _Alloc, typename _Tp>
43  class __alloctr_rebind_helper
44  {
45  template<typename _Alloc2, typename _Tp2>
46  static constexpr bool
47  _S_chk(typename _Alloc2::template rebind<_Tp2>::other*)
48  { return true; }
49 
50  template<typename, typename>
51  static constexpr bool
52  _S_chk(...)
53  { return false; }
54 
55  public:
56  static const bool __value = _S_chk<_Alloc, _Tp>(nullptr);
57  };
58 
59  template<typename _Alloc, typename _Tp,
60  bool = __alloctr_rebind_helper<_Alloc, _Tp>::__value>
61  struct __alloctr_rebind;
62 
63  template<typename _Alloc, typename _Tp>
64  struct __alloctr_rebind<_Alloc, _Tp, true>
65  {
66  typedef typename _Alloc::template rebind<_Tp>::other __type;
67  };
68 
69  template<template<typename, typename...> class _Alloc, typename _Tp,
70  typename _Up, typename... _Args>
71  struct __alloctr_rebind<_Alloc<_Up, _Args...>, _Tp, false>
72  {
73  typedef _Alloc<_Tp, _Args...> __type;
74  };
75 
76  /**
77  * @brief Uniform interface to all allocator types.
78  * @ingroup allocators
79  */
80  template<typename _Alloc>
82  {
83  /// The allocator type
84  typedef _Alloc allocator_type;
85  /// The allocated type
86  typedef typename _Alloc::value_type value_type;
87 
88 #define _GLIBCXX_ALLOC_TR_NESTED_TYPE(_NTYPE, _ALT) \
89  private: \
90  template<typename _Tp> \
91  static typename _Tp::_NTYPE _S_##_NTYPE##_helper(_Tp*); \
92  static _ALT _S_##_NTYPE##_helper(...); \
93  typedef decltype(_S_##_NTYPE##_helper((_Alloc*)0)) __##_NTYPE; \
94  public:
95 
96 _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer, value_type*)
97 
98  /**
99  * @brief The allocator's pointer type.
100  *
101  * @c Alloc::pointer if that type exists, otherwise @c value_type*
102  */
103  typedef __pointer pointer;
104 
105 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_pointer,
106  typename pointer_traits<pointer>::template rebind<const value_type>)
107 
108  /**
109  * @brief The allocator's const pointer type.
110  *
111  * @c Alloc::const_pointer if that type exists, otherwise
112  * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
113  */
114  typedef __const_pointer const_pointer;
115 
116 _GLIBCXX_ALLOC_TR_NESTED_TYPE(void_pointer,
117  typename pointer_traits<pointer>::template rebind<void>)
118 
119  /**
120  * @brief The allocator's void pointer type.
121  *
122  * @c Alloc::void_pointer if that type exists, otherwise
123  * <tt> pointer_traits<pointer>::rebind<void> </tt>
124  */
125  typedef __void_pointer void_pointer;
126 
127 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer,
128  typename pointer_traits<pointer>::template rebind<const void>)
129 
130  /**
131  * @brief The allocator's const void pointer type.
132  *
133  * @c Alloc::const_void_pointer if that type exists, otherwise
134  * <tt> pointer_traits<pointer>::rebind<const void> </tt>
135  */
136  typedef __const_void_pointer const_void_pointer;
137 
138 _GLIBCXX_ALLOC_TR_NESTED_TYPE(difference_type,
139  typename pointer_traits<pointer>::difference_type)
140 
141  /**
142  * @brief The allocator's difference type
143  *
144  * @c Alloc::difference_type if that type exists, otherwise
145  * <tt> pointer_traits<pointer>::difference_type </tt>
146  */
147  typedef __difference_type difference_type;
148 
149 _GLIBCXX_ALLOC_TR_NESTED_TYPE(size_type,
150  typename make_unsigned<difference_type>::type)
151 
152  /**
153  * @brief The allocator's size type
154  *
155  * @c Alloc::size_type if that type exists, otherwise
156  * <tt> make_unsigned<difference_type>::type </tt>
157  */
158  typedef __size_type size_type;
159 
160 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_copy_assignment,
161  false_type)
162 
163  /**
164  * @brief How the allocator is propagated on copy assignment
165  *
166  * @c Alloc::propagate_on_container_copy_assignment if that type exists,
167  * otherwise @c false_type
168  */
169  typedef __propagate_on_container_copy_assignment
170  propagate_on_container_copy_assignment;
171 
172 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_move_assignment,
173  false_type)
174 
175  /**
176  * @brief How the allocator is propagated on move assignment
177  *
178  * @c Alloc::propagate_on_container_move_assignment if that type exists,
179  * otherwise @c false_type
180  */
181  typedef __propagate_on_container_move_assignment
182  propagate_on_container_move_assignment;
183 
184 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap,
185  false_type)
186 
187  /**
188  * @brief How the allocator is propagated on swap
189  *
190  * @c Alloc::propagate_on_container_swap if that type exists,
191  * otherwise @c false_type
192  */
193  typedef __propagate_on_container_swap propagate_on_container_swap;
194 
195 #undef _GLIBCXX_ALLOC_TR_NESTED_TYPE
196 
197  template<typename _Tp>
198  using rebind_alloc = typename __alloctr_rebind<_Alloc, _Tp>::__type;
199  template<typename _Tp>
200  using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
201 
202  private:
203  template<typename _Alloc2>
204  struct __allocate_helper
205  {
206  template<typename _Alloc3,
207  typename = decltype(std::declval<_Alloc3*>()->allocate(
208  std::declval<size_type>(),
209  std::declval<const_void_pointer>()))>
210  static true_type __test(int);
211 
212  template<typename>
213  static false_type __test(...);
214 
215  typedef decltype(__test<_Alloc>(0)) type;
216  static const bool value = type::value;
217  };
218 
219  template<typename _Alloc2>
220  static typename
222  _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer __hint)
223  { return __a.allocate(__n, __hint); }
224 
225  template<typename _Alloc2>
226  static typename
227  enable_if<!__allocate_helper<_Alloc2>::value, pointer>::type
228  _S_allocate(_Alloc2& __a, size_type __n, ...)
229  { return __a.allocate(__n); }
230 
231  template<typename _Tp, typename... _Args>
232  struct __construct_helper
233  {
234  template<typename _Alloc2,
235  typename = decltype(std::declval<_Alloc2*>()->construct(
236  std::declval<_Tp*>(), std::declval<_Args>()...))>
237  static true_type __test(int);
238 
239  template<typename>
240  static false_type __test(...);
241 
242  typedef decltype(__test<_Alloc>(0)) type;
243  static const bool value = type::value;
244  };
245 
246  template<typename _Tp, typename... _Args>
247  static typename
248  enable_if<__construct_helper<_Tp, _Args...>::value, void>::type
249  _S_construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
250  { __a.construct(__p, std::forward<_Args>(__args)...); }
251 
252  template<typename _Tp, typename... _Args>
253  static typename
254  enable_if<!__construct_helper<_Tp, _Args...>::value, void>::type
255  _S_construct(_Alloc&, _Tp* __p, _Args&&... __args)
256  { ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); }
257 
258  template<typename _Tp>
259  struct __destroy_helper
260  {
261  template<typename _Alloc2,
262  typename = decltype(std::declval<_Alloc2*>()->destroy(
263  std::declval<_Tp*>()))>
264  static true_type __test(int);
265 
266  template<typename>
267  static false_type __test(...);
268 
269  typedef decltype(__test<_Alloc>(0)) type;
270  static const bool value = type::value;
271  };
272 
273  template<typename _Tp>
274  static typename enable_if<__destroy_helper<_Tp>::value, void>::type
275  _S_destroy(_Alloc& __a, _Tp* __p)
276  { __a.destroy(__p); }
277 
278  template<typename _Tp>
279  static typename enable_if<!__destroy_helper<_Tp>::value, void>::type
280  _S_destroy(_Alloc&, _Tp* __p)
281  { __p->~_Tp(); }
282 
283  template<typename _Alloc2>
284  struct __maxsize_helper
285  {
286  template<typename _Alloc3,
287  typename = decltype(std::declval<_Alloc3*>()->max_size())>
288  static true_type __test(int);
289 
290  template<typename>
291  static false_type __test(...);
292 
293  typedef decltype(__test<_Alloc2>(0)) type;
294  static const bool value = type::value;
295  };
296 
297  template<typename _Alloc2>
298  static typename
299  enable_if<__maxsize_helper<_Alloc2>::value, size_type>::type
300  _S_max_size(_Alloc2& __a)
301  { return __a.max_size(); }
302 
303  template<typename _Alloc2>
304  static typename
305  enable_if<!__maxsize_helper<_Alloc2>::value, size_type>::type
306  _S_max_size(_Alloc2&)
307  { return __gnu_cxx::__numeric_traits<size_type>::__max; }
308 
309  template<typename _Alloc2>
310  struct __select_helper
311  {
312  template<typename _Alloc3, typename
313  = decltype(std::declval<_Alloc3*>()
315  static true_type __test(int);
316 
317  template<typename>
318  static false_type __test(...);
319 
320  typedef decltype(__test<_Alloc2>(0)) type;
321  static const bool value = type::value;
322  };
323  template<typename _Alloc2>
324  static typename
325  enable_if<__select_helper<_Alloc2>::value, _Alloc2>::type
326  _S_select(_Alloc2& __a)
327  { return __a.select_on_container_copy_construction(); }
328 
329  template<typename _Alloc2>
330  static typename
331  enable_if<!__select_helper<_Alloc2>::value, _Alloc2>::type
332  _S_select(_Alloc2& __a)
333  { return __a; }
334 
335  public:
336 
337  /**
338  * @brief Allocate memory.
339  * @param __a An allocator.
340  * @param __n The number of objects to allocate space for.
341  *
342  * Calls @c a.allocate(n)
343  */
344  static pointer
345  allocate(_Alloc& __a, size_type __n)
346  { return __a.allocate(__n); }
347 
348  /**
349  * @brief Allocate memory.
350  * @param __a An allocator.
351  * @param __n The number of objects to allocate space for.
352  * @param __hint Aid to locality.
353  * @return Memory of suitable size and alignment for @a n objects
354  * of type @c value_type
355  *
356  * Returns <tt> a.allocate(n, hint) </tt> if that expression is
357  * well-formed, otherwise returns @c a.allocate(n)
358  */
359  static pointer
360  allocate(_Alloc& __a, size_type __n, const_void_pointer __hint)
361  { return _S_allocate(__a, __n, __hint); }
362 
363  /**
364  * @brief Deallocate memory.
365  * @param __a An allocator.
366  * @param __p Pointer to the memory to deallocate.
367  * @param __n The number of objects space was allocated for.
368  *
369  * Calls <tt> a.deallocate(p, n) </tt>
370  */
371  static void deallocate(_Alloc& __a, pointer __p, size_type __n)
372  { __a.deallocate(__p, __n); }
373 
374  /**
375  * @brief Construct an object of type @a _Tp
376  * @param __a An allocator.
377  * @param __p Pointer to memory of suitable size and alignment for Tp
378  * @param __args Constructor arguments.
379  *
380  * Calls <tt> __a.construct(__p, std::forward<Args>(__args)...) </tt>
381  * if that expression is well-formed, otherwise uses placement-new
382  * to construct an object of type @a _Tp at location @a __p from the
383  * arguments @a __args...
384  */
385  template<typename _Tp, typename... _Args>
386  static void construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
387  { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
388 
389  /**
390  * @brief Destroy an object of type @a _Tp
391  * @param __a An allocator.
392  * @param __p Pointer to the object to destroy
393  *
394  * Calls @c __a.destroy(__p) if that expression is well-formed,
395  * otherwise calls @c __p->~_Tp()
396  */
397  template <class _Tp>
398  static void destroy(_Alloc& __a, _Tp* __p)
399  { _S_destroy(__a, __p); }
400 
401  /**
402  * @brief The maximum supported allocation size
403  * @param __a An allocator.
404  * @return @c __a.max_size() or @c numeric_limits<size_type>::max()
405  *
406  * Returns @c __a.max_size() if that expression is well-formed,
407  * otherwise returns @c numeric_limits<size_type>::max()
408  */
409  static size_type max_size(const _Alloc& __a)
410  { return _S_max_size(__a); }
411 
412  /**
413  * @brief Obtain an allocator to use when copying a container.
414  * @param __rhs An allocator.
415  * @return @c __rhs.select_on_container_copy_construction() or @a __rhs
416  *
417  * Returns @c __rhs.select_on_container_copy_construction() if that
418  * expression is well-formed, otherwise returns @a __rhs
419  */
420  static _Alloc
422  { return _S_select(__rhs); }
423  };
424 
425  template<typename _Alloc>
426  inline void
427  __do_alloc_on_copy(_Alloc& __one, const _Alloc& __two, true_type)
428  { __one = __two; }
429 
430  template<typename _Alloc>
431  inline void
432  __do_alloc_on_copy(_Alloc&, const _Alloc&, false_type)
433  { }
434 
435  template<typename _Alloc>
436  inline void __alloc_on_copy(_Alloc& __one, const _Alloc& __two)
437  {
438  typedef allocator_traits<_Alloc> __traits;
439  typedef typename __traits::propagate_on_container_copy_assignment __pocca;
440  __do_alloc_on_copy(__one, __two, __pocca());
441  }
442 
443  template<typename _Alloc>
444  inline _Alloc __alloc_on_copy(const _Alloc& __a)
445  {
446  typedef allocator_traits<_Alloc> __traits;
448  }
449 
450  template<typename _Alloc>
451  inline void __do_alloc_on_move(_Alloc& __one, _Alloc& __two, true_type)
452  { __one = std::move(__two); }
453 
454  template<typename _Alloc>
455  inline void __do_alloc_on_move(_Alloc&, _Alloc&, false_type)
456  { }
457 
458  template<typename _Alloc>
459  inline void __alloc_on_move(_Alloc& __one, _Alloc& __two)
460  {
461  typedef allocator_traits<_Alloc> __traits;
462  typedef typename __traits::propagate_on_container_move_assignment __pocma;
463  __do_alloc_on_move(__one, __two, __pocma());
464  }
465 
466  template<typename _Alloc>
467  inline void __do_alloc_on_swap(_Alloc& __one, _Alloc& __two, true_type)
468  {
469  using std::swap;
470  swap(__one, __two);
471  }
472 
473  template<typename _Alloc>
474  inline void __do_alloc_on_swap(_Alloc&, _Alloc&, false_type)
475  { }
476 
477  template<typename _Alloc>
478  inline void __alloc_on_swap(_Alloc& __one, _Alloc& __two)
479  {
480  typedef allocator_traits<_Alloc> __traits;
481  typedef typename __traits::propagate_on_container_swap __pocs;
482  __do_alloc_on_swap(__one, __two, __pocs());
483  }
484 
485 _GLIBCXX_END_NAMESPACE_VERSION
486 } // namespace std
487 
488 #endif
489 #endif