1 // Allocator traits -*- C++ -*-
3 // Copyright (C) 2011 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 #ifndef _ALLOC_TRAITS_H
26 #define _ALLOC_TRAITS_H 1
28 #ifdef __GXX_EXPERIMENTAL_CXX0X__
30 #include <bits/ptr_traits.h>
31 #include <ext/numeric_traits.h>
33 namespace std _GLIBCXX_VISIBILITY(default)
35 _GLIBCXX_BEGIN_NAMESPACE_VERSION
37 template<typename _Alloc, typename _Tp>
38 class __alloctr_rebind_helper
40 template<typename _Alloc2, typename _Tp2>
42 _S_chk(typename _Alloc2::template rebind<_Tp2>::other*)
45 template<typename, typename>
51 static const bool __value = _S_chk<_Alloc, _Tp>(nullptr);
54 template<typename _Alloc, typename _Tp,
55 bool = __alloctr_rebind_helper<_Alloc, _Tp>::__value>
56 struct __alloctr_rebind;
58 template<typename _Alloc, typename _Tp>
59 struct __alloctr_rebind<_Alloc, _Tp, true>
61 typedef typename _Alloc::template rebind<_Tp>::other __type;
64 template<template<typename, typename...> class _Alloc, typename _Tp,
65 typename _Up, typename... _Args>
66 struct __alloctr_rebind<_Alloc<_Up, _Args...>, _Tp, false>
68 typedef _Alloc<_Tp, _Args...> __type;
72 * @brief Uniform interface to all allocator types.
75 template<typename _Alloc>
76 struct allocator_traits
78 /// The allocator type
79 typedef _Alloc allocator_type;
80 /// The allocated type
81 typedef typename _Alloc::value_type value_type;
83 #define _GLIBCXX_ALLOC_TR_NESTED_TYPE(_NTYPE, _ALT) \
85 template<typename _Tp> \
86 static typename _Tp::_NTYPE _S_##_NTYPE##_helper(_Tp*); \
87 static _ALT _S_##_NTYPE##_helper(...); \
88 typedef decltype(_S_##_NTYPE##_helper((_Alloc*)0)) __##_NTYPE; \
91 _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer, value_type*)
94 * @brief The allocator's pointer type.
96 * @c Alloc::pointer if that type exists, otherwise @c value_type*
98 typedef __pointer pointer;
100 // TODO: Use pointer_traits::rebind alias template.
102 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_pointer,
103 typename pointer_traits<pointer>::template __rebind<const value_type>::__type)
106 * @brief The allocator's const pointer type.
108 * @c Alloc::const_pointer if that type exists, otherwise
109 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
111 typedef __const_pointer const_pointer;
113 _GLIBCXX_ALLOC_TR_NESTED_TYPE(void_pointer,
114 typename pointer_traits<pointer>::template __rebind<void>::__type)
117 * @brief The allocator's void pointer type.
119 * @c Alloc::void_pointer if that type exists, otherwise
120 * <tt> pointer_traits<pointer>::rebind<void> </tt>
122 typedef __void_pointer void_pointer;
124 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer,
125 typename pointer_traits<pointer>::template __rebind<const void>::__type)
128 * @brief The allocator's const void pointer type.
130 * @c Alloc::const_void_pointer if that type exists, otherwise
131 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
133 typedef __const_void_pointer const_void_pointer;
135 _GLIBCXX_ALLOC_TR_NESTED_TYPE(difference_type,
136 typename pointer_traits<pointer>::difference_type)
139 * @brief The allocator's difference type
141 * @c Alloc::difference_type if that type exists, otherwise
142 * <tt> pointer_traits<pointer>::difference_type </tt>
144 typedef __difference_type difference_type;
146 _GLIBCXX_ALLOC_TR_NESTED_TYPE(size_type,
147 typename make_unsigned<difference_type>::type)
150 * @brief The allocator's size type
152 * @c Alloc::size_type if that type exists, otherwise
153 * <tt> make_unsigned<difference_type>::type </tt>
155 typedef __size_type size_type;
157 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_copy_assignment,
161 * @brief How the allocator is propagated on copy assignment
163 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
164 * otherwise @c false_type
166 typedef __propagate_on_container_copy_assignment
167 propagate_on_container_copy_assignment;
169 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_move_assignment,
173 * @brief How the allocator is propagated on move assignment
175 * @c Alloc::propagate_on_container_move_assignment if that type exists,
176 * otherwise @c false_type
178 typedef __propagate_on_container_move_assignment
179 propagate_on_container_move_assignment;
181 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap,
185 * @brief How the allocator is propagated on swap
187 * @c Alloc::propagate_on_container_swap if that type exists,
188 * otherwise @c false_type
190 typedef __propagate_on_container_swap propagate_on_container_swap;
192 #undef _GLIBCXX_ALLOC_TR_NESTED_TYPE
194 /* TODO: use template alias
195 template<typename _Tp>
196 using rebind_alloc = __alloctr_rebind<_Alloc, _Tp>::__type;
197 template<typename _Tp>
198 using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
200 template<typename _Tp>
201 struct __rebind_alloc
203 typedef typename __alloctr_rebind<_Alloc, _Tp>::__type __type;
206 template<typename _Tp>
207 struct __rebind_traits
209 typedef allocator_traits<typename __rebind_alloc<_Tp>::__type> __type;
213 template<typename _Alloc2>
214 struct __allocate_helper
216 template<typename _Alloc3,
217 typename = decltype(std::declval<_Alloc3*>()->allocate(
218 std::declval<size_type>(),
219 std::declval<const_void_pointer>()))>
220 static true_type __test(int);
223 static false_type __test(...);
225 typedef decltype(__test<_Alloc>(0)) type;
226 static const bool value = type::value;
229 template<typename _Alloc2>
231 enable_if<__allocate_helper<_Alloc2>::value, pointer>::type
232 _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer __hint)
233 { return __a.allocate(__n, __hint); }
235 template<typename _Alloc2>
237 enable_if<!__allocate_helper<_Alloc2>::value, pointer>::type
238 _S_allocate(_Alloc2& __a, size_type __n, ...)
239 { return __a.allocate(__n); }
241 template<typename _Tp, typename... _Args>
242 struct __construct_helper
244 template<typename _Alloc2,
245 typename = decltype(std::declval<_Alloc2*>()->construct(
246 std::declval<_Tp*>(), std::declval<_Args>()...))>
247 static true_type __test(int);
250 static false_type __test(...);
252 typedef decltype(__test<_Alloc>(0)) type;
253 static const bool value = type::value;
256 template<typename _Tp, typename... _Args>
258 enable_if<__construct_helper<_Tp, _Args...>::value, void>::type
259 _S_construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
260 { __a.construct(__p, std::forward<_Args>(__args)...); }
262 template<typename _Tp, typename... _Args>
264 enable_if<!__construct_helper<_Tp, _Args...>::value, void>::type
265 _S_construct(_Alloc&, _Tp* __p, _Args&&... __args)
266 { ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); }
268 template<typename _Tp>
269 struct __destroy_helper
271 template<typename _Alloc2,
272 typename = decltype(std::declval<_Alloc2*>()->destroy(
273 std::declval<_Tp*>()))>
274 static true_type __test(int);
277 static false_type __test(...);
279 typedef decltype(__test<_Alloc>(0)) type;
280 static const bool value = type::value;
283 template<typename _Tp>
284 static typename enable_if<__destroy_helper<_Tp>::value, void>::type
285 _S_destroy(_Alloc& __a, _Tp* __p)
286 { __a.destroy(__p); }
288 template<typename _Tp>
289 static typename enable_if<!__destroy_helper<_Tp>::value, void>::type
290 _S_destroy(_Alloc&, _Tp* __p)
293 template<typename _Alloc2>
294 struct __maxsize_helper
296 template<typename _Alloc3,
297 typename = decltype(std::declval<_Alloc3*>()->max_size())>
298 static true_type __test(int);
301 static false_type __test(...);
303 typedef decltype(__test<_Alloc2>(0)) type;
304 static const bool value = type::value;
307 template<typename _Alloc2>
309 enable_if<__maxsize_helper<_Alloc2>::value, size_type>::type
310 _S_max_size(_Alloc2& __a)
311 { return __a.max_size(); }
313 template<typename _Alloc2>
315 enable_if<!__maxsize_helper<_Alloc2>::value, size_type>::type
316 _S_max_size(_Alloc2&)
317 { return __gnu_cxx::__numeric_traits<size_type>::__max; }
319 template<typename _Alloc2>
320 struct __select_helper
322 template<typename _Alloc3, typename
323 = decltype(std::declval<_Alloc3*>()
324 ->select_on_container_copy_construction())>
325 static true_type __test(int);
328 static false_type __test(...);
330 typedef decltype(__test<_Alloc2>(0)) type;
331 static const bool value = type::value;
333 template<typename _Alloc2>
335 enable_if<__select_helper<_Alloc2>::value, _Alloc2>::type
336 _S_select(_Alloc2& __a)
337 { return __a.select_on_container_copy_construction(); }
339 template<typename _Alloc2>
341 enable_if<!__select_helper<_Alloc2>::value, _Alloc2>::type
342 _S_select(_Alloc2& __a)
348 * @brief Allocate memory.
349 * @param a An allocator.
350 * @param n The number of objects to allocate space for.
352 * Calls @c a.allocate(n)
355 allocate(_Alloc& __a, size_type __n)
356 { return __a.allocate(__n); }
359 * @brief Allocate memory.
360 * @param a An allocator.
361 * @param n The number of objects to allocate space for.
362 * @param hint Aid to locality.
363 * @return Memory of suitable size and alignment for @a n objects
364 * of type @c value_type
366 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
367 * well-formed, otherwise returns @c a.allocate(n)
370 allocate(_Alloc& __a, size_type __n, const_void_pointer __hint)
371 { return _S_allocate(__a, __n, __hint); }
374 * @brief Deallocate memory.
375 * @param a An allocator.
376 * @param p Pointer to the memory to deallocate.
377 * @param n The number of objects space was allocated for.
379 * Calls <tt> a.deallocate(p, n) </tt>
381 static void deallocate(_Alloc& __a, pointer __p, size_type __n)
382 { __a.deallocate(__p, __n); }
385 * @brief Construct an object of type @a Tp
386 * @param a An allocator.
387 * @param p Pointer to memory of suitable size and alignment for Tp
388 * @param args Constructor arguments.
390 * Calls <tt> a.construct(p, std::forward<Args>(args)...) </tt>
391 * if that expression is well-formed, otherwise uses placement-new
392 * to construct an object of type @a Tp at location @a p from the
393 * arguments @a args...
395 template<typename _Tp, typename... _Args>
396 static void construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
397 { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
400 * @brief Destroy an object of type @a Tp
401 * @param a An allocator.
402 * @param p Pointer to the object to destroy
404 * Calls @c a.destroy(p) if that expression is well-formed,
405 * otherwise calls @c p->~Tp()
408 static void destroy(_Alloc& __a, _Tp* __p)
409 { _S_destroy(__a, __p); }
412 * @brief The maximum supported allocation size
413 * @param a An allocator.
414 * @return @c a.max_size() or @c numeric_limits<size_type>::max()
416 * Returns @c a.max_size() if that expression is well-formed,
417 * otherwise returns @c numeric_limits<size_type>::max()
419 static size_type max_size(const _Alloc& __a)
420 { return _S_max_size(__a); }
423 * @brief Obtain an allocator to use when copying a container.
424 * @param rhs An allocator.
425 * @return @c rhs.select_on_container_copy_construction() or @a rhs
427 * Returns @c rhs.select_on_container_copy_construction() if that
428 * expression is well-formed, otherwise returns @a rhs
431 select_on_container_copy_construction(const _Alloc& __rhs)
432 { return _S_select(__rhs); }
435 template<typename _Alloc>
437 __do_alloc_on_copy(_Alloc& __one, const _Alloc& __two, true_type)
440 template<typename _Alloc>
442 __do_alloc_on_copy(_Alloc&, const _Alloc&, false_type)
445 template<typename _Alloc>
446 inline void __alloc_on_copy(_Alloc& __one, const _Alloc& __two)
448 typedef allocator_traits<_Alloc> __traits;
449 typedef typename __traits::propagate_on_container_copy_assignment __pocca;
450 __do_alloc_on_copy(__one, __two, __pocca());
453 template<typename _Alloc>
454 inline _Alloc __alloc_on_copy(const _Alloc& __a)
456 typedef allocator_traits<_Alloc> __traits;
457 return __traits::select_on_container_copy_construction(__a);
460 template<typename _Alloc>
461 inline void __do_alloc_on_move(_Alloc& __one, _Alloc& __two, true_type)
462 { __one = std::move(__two); }
464 template<typename _Alloc>
465 inline void __do_alloc_on_move(_Alloc&, _Alloc&, false_type)
468 template<typename _Alloc>
469 inline void __alloc_on_move(_Alloc& __one, _Alloc& __two)
471 typedef allocator_traits<_Alloc> __traits;
472 typedef typename __traits::propagate_on_container_move_assignment __pocma;
473 __do_alloc_on_move(__one, __two, __pocma());
476 template<typename _Alloc>
477 inline void __do_alloc_on_swap(_Alloc& __one, _Alloc& __two, true_type)
483 template<typename _Alloc>
484 inline void __do_alloc_on_swap(_Alloc&, _Alloc&, false_type)
487 template<typename _Alloc>
488 inline void __alloc_on_swap(_Alloc& __one, _Alloc& __two)
490 typedef allocator_traits<_Alloc> __traits;
491 typedef typename __traits::propagate_on_container_swap __pocs;
492 __do_alloc_on_swap(__one, __two, __pocs());
495 _GLIBCXX_END_NAMESPACE_VERSION