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