1 // Allocator traits -*- C++ -*-
3 // Copyright (C) 2011, 2012 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
30 #ifndef _ALLOC_TRAITS_H
31 #define _ALLOC_TRAITS_H 1
33 #ifdef __GXX_EXPERIMENTAL_CXX0X__
35 #include <bits/ptr_traits.h>
36 #include <ext/numeric_traits.h>
38 namespace std _GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 template<typename _Alloc, typename _Tp>
43 class __alloctr_rebind_helper
45 template<typename _Alloc2, typename _Tp2>
47 _S_chk(typename _Alloc2::template rebind<_Tp2>::other*)
50 template<typename, typename>
56 static const bool __value = _S_chk<_Alloc, _Tp>(nullptr);
59 template<typename _Alloc, typename _Tp>
60 const bool __alloctr_rebind_helper<_Alloc, _Tp>::__value;
62 template<typename _Alloc, typename _Tp,
63 bool = __alloctr_rebind_helper<_Alloc, _Tp>::__value>
64 struct __alloctr_rebind;
66 template<typename _Alloc, typename _Tp>
67 struct __alloctr_rebind<_Alloc, _Tp, true>
69 typedef typename _Alloc::template rebind<_Tp>::other __type;
72 template<template<typename, typename...> class _Alloc, typename _Tp,
73 typename _Up, typename... _Args>
74 struct __alloctr_rebind<_Alloc<_Up, _Args...>, _Tp, false>
76 typedef _Alloc<_Tp, _Args...> __type;
80 * @brief Uniform interface to all allocator types.
83 template<typename _Alloc>
84 struct allocator_traits
86 /// The allocator type
87 typedef _Alloc allocator_type;
88 /// The allocated type
89 typedef typename _Alloc::value_type value_type;
91 #define _GLIBCXX_ALLOC_TR_NESTED_TYPE(_NTYPE, _ALT) \
93 template<typename _Tp> \
94 static typename _Tp::_NTYPE _S_##_NTYPE##_helper(_Tp*); \
95 static _ALT _S_##_NTYPE##_helper(...); \
96 typedef decltype(_S_##_NTYPE##_helper((_Alloc*)0)) __##_NTYPE; \
99 _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer, value_type*)
102 * @brief The allocator's pointer type.
104 * @c Alloc::pointer if that type exists, otherwise @c value_type*
106 typedef __pointer pointer;
108 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_pointer,
109 typename pointer_traits<pointer>::template rebind<const value_type>)
112 * @brief The allocator's const pointer type.
114 * @c Alloc::const_pointer if that type exists, otherwise
115 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
117 typedef __const_pointer const_pointer;
119 _GLIBCXX_ALLOC_TR_NESTED_TYPE(void_pointer,
120 typename pointer_traits<pointer>::template rebind<void>)
123 * @brief The allocator's void pointer type.
125 * @c Alloc::void_pointer if that type exists, otherwise
126 * <tt> pointer_traits<pointer>::rebind<void> </tt>
128 typedef __void_pointer void_pointer;
130 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer,
131 typename pointer_traits<pointer>::template rebind<const void>)
134 * @brief The allocator's const void pointer type.
136 * @c Alloc::const_void_pointer if that type exists, otherwise
137 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
139 typedef __const_void_pointer const_void_pointer;
141 _GLIBCXX_ALLOC_TR_NESTED_TYPE(difference_type,
142 typename pointer_traits<pointer>::difference_type)
145 * @brief The allocator's difference type
147 * @c Alloc::difference_type if that type exists, otherwise
148 * <tt> pointer_traits<pointer>::difference_type </tt>
150 typedef __difference_type difference_type;
152 _GLIBCXX_ALLOC_TR_NESTED_TYPE(size_type,
153 typename make_unsigned<difference_type>::type)
156 * @brief The allocator's size type
158 * @c Alloc::size_type if that type exists, otherwise
159 * <tt> make_unsigned<difference_type>::type </tt>
161 typedef __size_type size_type;
163 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_copy_assignment,
167 * @brief How the allocator is propagated on copy assignment
169 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
170 * otherwise @c false_type
172 typedef __propagate_on_container_copy_assignment
173 propagate_on_container_copy_assignment;
175 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_move_assignment,
179 * @brief How the allocator is propagated on move assignment
181 * @c Alloc::propagate_on_container_move_assignment if that type exists,
182 * otherwise @c false_type
184 typedef __propagate_on_container_move_assignment
185 propagate_on_container_move_assignment;
187 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap,
191 * @brief How the allocator is propagated on swap
193 * @c Alloc::propagate_on_container_swap if that type exists,
194 * otherwise @c false_type
196 typedef __propagate_on_container_swap propagate_on_container_swap;
198 #undef _GLIBCXX_ALLOC_TR_NESTED_TYPE
200 template<typename _Tp>
201 using rebind_alloc = typename __alloctr_rebind<_Alloc, _Tp>::__type;
202 template<typename _Tp>
203 using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
206 template<typename _Alloc2>
207 struct __allocate_helper
209 template<typename _Alloc3,
210 typename = decltype(std::declval<_Alloc3*>()->allocate(
211 std::declval<size_type>(),
212 std::declval<const_void_pointer>()))>
213 static true_type __test(int);
216 static false_type __test(...);
218 typedef decltype(__test<_Alloc>(0)) type;
219 static const bool value = type::value;
222 template<typename _Alloc2>
224 enable_if<__allocate_helper<_Alloc2>::value, pointer>::type
225 _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer __hint)
226 { return __a.allocate(__n, __hint); }
228 template<typename _Alloc2>
230 enable_if<!__allocate_helper<_Alloc2>::value, pointer>::type
231 _S_allocate(_Alloc2& __a, size_type __n, ...)
232 { return __a.allocate(__n); }
234 template<typename _Tp, typename... _Args>
235 struct __construct_helper
237 template<typename _Alloc2,
238 typename = decltype(std::declval<_Alloc2*>()->construct(
239 std::declval<_Tp*>(), std::declval<_Args>()...))>
240 static true_type __test(int);
243 static false_type __test(...);
245 typedef decltype(__test<_Alloc>(0)) type;
246 static const bool value = type::value;
249 template<typename _Tp, typename... _Args>
251 enable_if<__construct_helper<_Tp, _Args...>::value, void>::type
252 _S_construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
253 { __a.construct(__p, std::forward<_Args>(__args)...); }
255 template<typename _Tp, typename... _Args>
257 enable_if<!__construct_helper<_Tp, _Args...>::value, void>::type
258 _S_construct(_Alloc&, _Tp* __p, _Args&&... __args)
259 { ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); }
261 template<typename _Tp>
262 struct __destroy_helper
264 template<typename _Alloc2,
265 typename = decltype(std::declval<_Alloc2*>()->destroy(
266 std::declval<_Tp*>()))>
267 static true_type __test(int);
270 static false_type __test(...);
272 typedef decltype(__test<_Alloc>(0)) type;
273 static const bool value = type::value;
276 template<typename _Tp>
277 static typename enable_if<__destroy_helper<_Tp>::value, void>::type
278 _S_destroy(_Alloc& __a, _Tp* __p)
279 { __a.destroy(__p); }
281 template<typename _Tp>
282 static typename enable_if<!__destroy_helper<_Tp>::value, void>::type
283 _S_destroy(_Alloc&, _Tp* __p)
286 template<typename _Alloc2>
287 struct __maxsize_helper
289 template<typename _Alloc3,
290 typename = decltype(std::declval<_Alloc3*>()->max_size())>
291 static true_type __test(int);
294 static false_type __test(...);
296 typedef decltype(__test<_Alloc2>(0)) type;
297 static const bool value = type::value;
300 template<typename _Alloc2>
302 enable_if<__maxsize_helper<_Alloc2>::value, size_type>::type
303 _S_max_size(_Alloc2& __a)
304 { return __a.max_size(); }
306 template<typename _Alloc2>
308 enable_if<!__maxsize_helper<_Alloc2>::value, size_type>::type
309 _S_max_size(_Alloc2&)
310 { return __gnu_cxx::__numeric_traits<size_type>::__max; }
312 template<typename _Alloc2>
313 struct __select_helper
315 template<typename _Alloc3, typename
316 = decltype(std::declval<_Alloc3*>()
317 ->select_on_container_copy_construction())>
318 static true_type __test(int);
321 static false_type __test(...);
323 typedef decltype(__test<_Alloc2>(0)) type;
324 static const bool value = type::value;
326 template<typename _Alloc2>
328 enable_if<__select_helper<_Alloc2>::value, _Alloc2>::type
329 _S_select(_Alloc2& __a)
330 { return __a.select_on_container_copy_construction(); }
332 template<typename _Alloc2>
334 enable_if<!__select_helper<_Alloc2>::value, _Alloc2>::type
335 _S_select(_Alloc2& __a)
341 * @brief Allocate memory.
342 * @param __a An allocator.
343 * @param __n The number of objects to allocate space for.
345 * Calls @c a.allocate(n)
348 allocate(_Alloc& __a, size_type __n)
349 { return __a.allocate(__n); }
352 * @brief Allocate memory.
353 * @param __a An allocator.
354 * @param __n The number of objects to allocate space for.
355 * @param __hint Aid to locality.
356 * @return Memory of suitable size and alignment for @a n objects
357 * of type @c value_type
359 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
360 * well-formed, otherwise returns @c a.allocate(n)
363 allocate(_Alloc& __a, size_type __n, const_void_pointer __hint)
364 { return _S_allocate(__a, __n, __hint); }
367 * @brief Deallocate memory.
368 * @param __a An allocator.
369 * @param __p Pointer to the memory to deallocate.
370 * @param __n The number of objects space was allocated for.
372 * Calls <tt> a.deallocate(p, n) </tt>
374 static void deallocate(_Alloc& __a, pointer __p, size_type __n)
375 { __a.deallocate(__p, __n); }
378 * @brief Construct an object of type @a _Tp
379 * @param __a An allocator.
380 * @param __p Pointer to memory of suitable size and alignment for Tp
381 * @param __args Constructor arguments.
383 * Calls <tt> __a.construct(__p, std::forward<Args>(__args)...) </tt>
384 * if that expression is well-formed, otherwise uses placement-new
385 * to construct an object of type @a _Tp at location @a __p from the
386 * arguments @a __args...
388 template<typename _Tp, typename... _Args>
389 static void construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
390 { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
393 * @brief Destroy an object of type @a _Tp
394 * @param __a An allocator.
395 * @param __p Pointer to the object to destroy
397 * Calls @c __a.destroy(__p) if that expression is well-formed,
398 * otherwise calls @c __p->~_Tp()
401 static void destroy(_Alloc& __a, _Tp* __p)
402 { _S_destroy(__a, __p); }
405 * @brief The maximum supported allocation size
406 * @param __a An allocator.
407 * @return @c __a.max_size() or @c numeric_limits<size_type>::max()
409 * Returns @c __a.max_size() if that expression is well-formed,
410 * otherwise returns @c numeric_limits<size_type>::max()
412 static size_type max_size(const _Alloc& __a)
413 { return _S_max_size(__a); }
416 * @brief Obtain an allocator to use when copying a container.
417 * @param __rhs An allocator.
418 * @return @c __rhs.select_on_container_copy_construction() or @a __rhs
420 * Returns @c __rhs.select_on_container_copy_construction() if that
421 * expression is well-formed, otherwise returns @a __rhs
424 select_on_container_copy_construction(const _Alloc& __rhs)
425 { return _S_select(__rhs); }
428 template<typename _Alloc>
429 template<typename _Alloc2>
430 const bool allocator_traits<_Alloc>::__allocate_helper<_Alloc2>::value;
432 template<typename _Alloc>
433 template<typename _Tp, typename... _Args>
435 allocator_traits<_Alloc>::__construct_helper<_Tp, _Args...>::value;
437 template<typename _Alloc>
438 template<typename _Tp>
439 const bool allocator_traits<_Alloc>::__destroy_helper<_Tp>::value;
441 template<typename _Alloc>
442 template<typename _Alloc2>
443 const bool allocator_traits<_Alloc>::__maxsize_helper<_Alloc2>::value;
445 template<typename _Alloc>
446 template<typename _Alloc2>
447 const bool allocator_traits<_Alloc>::__select_helper<_Alloc2>::value;
449 template<typename _Alloc>
451 __do_alloc_on_copy(_Alloc& __one, const _Alloc& __two, true_type)
454 template<typename _Alloc>
456 __do_alloc_on_copy(_Alloc&, const _Alloc&, false_type)
459 template<typename _Alloc>
460 inline void __alloc_on_copy(_Alloc& __one, const _Alloc& __two)
462 typedef allocator_traits<_Alloc> __traits;
463 typedef typename __traits::propagate_on_container_copy_assignment __pocca;
464 __do_alloc_on_copy(__one, __two, __pocca());
467 template<typename _Alloc>
468 inline _Alloc __alloc_on_copy(const _Alloc& __a)
470 typedef allocator_traits<_Alloc> __traits;
471 return __traits::select_on_container_copy_construction(__a);
474 template<typename _Alloc>
475 inline void __do_alloc_on_move(_Alloc& __one, _Alloc& __two, true_type)
476 { __one = std::move(__two); }
478 template<typename _Alloc>
479 inline void __do_alloc_on_move(_Alloc&, _Alloc&, false_type)
482 template<typename _Alloc>
483 inline void __alloc_on_move(_Alloc& __one, _Alloc& __two)
485 typedef allocator_traits<_Alloc> __traits;
486 typedef typename __traits::propagate_on_container_move_assignment __pocma;
487 __do_alloc_on_move(__one, __two, __pocma());
490 template<typename _Alloc>
491 inline void __do_alloc_on_swap(_Alloc& __one, _Alloc& __two, true_type)
497 template<typename _Alloc>
498 inline void __do_alloc_on_swap(_Alloc&, _Alloc&, false_type)
501 template<typename _Alloc>
502 inline void __alloc_on_swap(_Alloc& __one, _Alloc& __two)
504 typedef allocator_traits<_Alloc> __traits;
505 typedef typename __traits::propagate_on_container_swap __pocs;
506 __do_alloc_on_swap(__one, __two, __pocs());
509 _GLIBCXX_END_NAMESPACE_VERSION