blob: 5aa2fadd2294dbd7b34497cc46c194153200b256 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
31
32 template <class U> using rebind = <details>;
33
34 static pointer pointer_to(<details>);
35};
36
37template <class Alloc>
38struct allocator_traits
39{
40 typedef Alloc allocator_type;
41 typedef typename allocator_type::value_type
42 value_type;
43
44 typedef Alloc::pointer | value_type* pointer;
45 typedef Alloc::const_pointer
46 | pointer_traits<pointer>::rebind<const value_type>
47 const_pointer;
48 typedef Alloc::void_pointer
49 | pointer_traits<pointer>::rebind<void>
50 void_pointer;
51 typedef Alloc::const_void_pointer
52 | pointer_traits<pointer>::rebind<const void>
53 const_void_pointer;
54 typedef Alloc::difference_type
55 | ptrdiff_t difference_type;
56 typedef Alloc::size_type | size_t size_type;
57 typedef Alloc::propagate_on_container_copy_assignment
58 | false_type propagate_on_container_copy_assignment;
59 typedef Alloc::propagate_on_container_move_assignment
60 | false_type propagate_on_container_move_assignment;
61 typedef Alloc::propagate_on_container_swap
62 | false_type propagate_on_container_swap;
63
64 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
65 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
66
67 static pointer allocate(allocator_type& a, size_type n);
68 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
69
70 static void deallocate(allocator_type& a, pointer p, size_type n);
71
72 template <class T, class... Args>
73 static void construct(allocator_type& a, T* p, Args&&... args);
74
75 template <class T>
76 static void destroy(allocator_type& a, T* p);
77
78 static size_type max_size(const allocator_type& a);
79
80 static allocator_type
81 select_on_container_copy_construction(const allocator_type& a);
82};
83
84template <>
85class allocator<void>
86{
87public:
88 typedef void* pointer;
89 typedef const void* const_pointer;
90 typedef void value_type;
91
92 template <class _Up> struct rebind {typedef allocator<_Up> other;};
93};
94
95template <class T>
96class allocator
97{
98public:
99 typedef size_t size_type;
100 typedef ptrdiff_t difference_type;
101 typedef T* pointer;
102 typedef const T* const_pointer;
103 typedef typename add_lvalue_reference<T>::type reference;
104 typedef typename add_lvalue_reference<const T>::type const_reference;
105 typedef T value_type;
106
107 template <class U> struct rebind {typedef allocator<U> other;};
108
109 allocator() throw();
110 allocator(const allocator&) throw();
111 template <class U> allocator(const allocator<U>&) throw();
112 ~allocator() throw();
113 pointer address(reference x) const;
114 const_pointer address(const_reference x) const;
115 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
116 void deallocate(pointer p, size_type n);
117 size_type max_size() const throw();
118 void construct(pointer p, const T& val);
119 void destroy(pointer p);
120};
121
122template <class T, class U>
123bool operator==(const allocator<T>&, const allocator<U>&) throw();
124
125template <class T, class U>
126bool operator!=(const allocator<T>&, const allocator<U>&) throw();
127
128template <class OutputIterator, class T>
129class raw_storage_iterator
130 : public iterator<output_iterator_tag,
131 T, // purposefully not C++03
132 ptrdiff_t, // purposefully not C++03
133 T*, // purposefully not C++03
134 raw_storage_iterator&> // purposefully not C++03
135{
136public:
137 explicit raw_storage_iterator(OutputIterator x);
138 raw_storage_iterator& operator*();
139 raw_storage_iterator& operator=(const T& element);
140 raw_storage_iterator& operator++();
141 raw_storage_iterator operator++(int);
142};
143
144template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n);
145template <class T> void return_temporary_buffer(T* p);
146
147template <class InputIterator, class ForwardIterator>
148ForwardIterator
149uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
150
151template <class ForwardIterator, class T>
152void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
153
154template <class ForwardIterator, class Size, class T>
155void uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
156
157template <class Y> struct auto_ptr_ref {};
158
159template<class X>
160class auto_ptr
161{
162public:
163 typedef X element_type;
164
165 explicit auto_ptr(X* p =0) throw();
166 auto_ptr(auto_ptr&) throw();
167 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
168 auto_ptr& operator=(auto_ptr&) throw();
169 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
170 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
171 ~auto_ptr() throw();
172
173 typename add_lvalue_reference<X>::type operator*() const throw();
174 X* operator->() const throw();
175 X* get() const throw();
176 X* release() throw();
177 void reset(X* p =0) throw();
178
179 auto_ptr(auto_ptr_ref<X>) throw();
180 template<class Y> operator auto_ptr_ref<Y>() throw();
181 template<class Y> operator auto_ptr<Y>() throw();
182};
183
184void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
185
186} // std
187
188*/
189
190#include <__config>
191#include <type_traits>
192#include <typeinfo>
193#include <cstddef>
194#include <cstdint>
195#include <new>
196#include <utility>
197#include <limits>
198#include <iterator>
199#include <__functional_base>
200#if defined(_LIBCPP_NO_EXCEPTIONS)
201 #include <cassert>
202#endif
203
204#pragma GCC system_header
205
206_LIBCPP_BEGIN_NAMESPACE_STD
207
208// allocator_arg_t
209
210struct allocator_arg_t { };
211
212extern const allocator_arg_t allocator_arg;
213
214// addressof
215
216template <class _Tp>
217inline _LIBCPP_INLINE_VISIBILITY
218_Tp*
219addressof(_Tp& __x)
220{
221 return (_Tp*)&(char&)__x;
222}
223
224template <class _Tp> class allocator;
225
226template <>
227class allocator<void>
228{
229public:
230 typedef void* pointer;
231 typedef const void* const_pointer;
232 typedef void value_type;
233
234 template <class _Up> struct rebind {typedef allocator<_Up> other;};
235};
236
237
238// pointer_traits
239
240template <class _Tp>
241struct __has_element_type
242{
243private:
244 struct __two {char _; char __;};
245 template <class _Up> static __two __test(...);
246 template <class _Up> static char __test(typename _Up::element_type* = 0);
247public:
248 static const bool value = sizeof(__test<_Tp>(0)) == 1;
249};
250
251template <class _Ptr, bool = __has_element_type<_Ptr>::value>
252struct __pointer_traits_element_type;
253
254template <class _Ptr>
255struct __pointer_traits_element_type<_Ptr, true>
256{
257 typedef typename _Ptr::element_type type;
258};
259
260#ifndef _LIBCPP_HAS_NO_VARIADICS
261
262template <template <class, class...> class _Sp, class _Tp, class ..._Args>
263struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
264{
265 typedef typename _Sp<_Tp, _Args...>::element_type type;
266};
267
268template <template <class, class...> class _Sp, class _Tp, class ..._Args>
269struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
270{
271 typedef _Tp type;
272};
273
274#else
275
276template <template <class> class _Sp, class _Tp>
277struct __pointer_traits_element_type<_Sp<_Tp>, true>
278{
279 typedef typename _Sp<_Tp>::element_type type;
280};
281
282template <template <class> class _Sp, class _Tp>
283struct __pointer_traits_element_type<_Sp<_Tp>, false>
284{
285 typedef _Tp type;
286};
287
288template <template <class, class> class _Sp, class _Tp, class _A0>
289struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
290{
291 typedef typename _Sp<_Tp, _A0>::element_type type;
292};
293
294template <template <class, class> class _Sp, class _Tp, class _A0>
295struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
296{
297 typedef _Tp type;
298};
299
300template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
301struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
302{
303 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
304};
305
306template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
307struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
308{
309 typedef _Tp type;
310};
311
312template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
313 class _A1, class _A2>
314struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
315{
316 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
317};
318
319template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
320 class _A1, class _A2>
321struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
322{
323 typedef _Tp type;
324};
325
326#endif
327
328template <class _Tp>
329struct __has_difference_type
330{
331private:
332 struct __two {char _; char __;};
333 template <class _Up> static __two __test(...);
334 template <class _Up> static char __test(typename _Up::difference_type* = 0);
335public:
336 static const bool value = sizeof(__test<_Tp>(0)) == 1;
337};
338
339template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
340struct __pointer_traits_difference_type
341{
342 typedef ptrdiff_t type;
343};
344
345template <class _Ptr>
346struct __pointer_traits_difference_type<_Ptr, true>
347{
348 typedef typename _Ptr::difference_type type;
349};
350
351template <class _Tp, class _Up>
352struct __has_rebind
353{
354private:
355 struct __two {char _; char __;};
356 template <class _Xp> static __two __test(...);
357 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
358public:
359 static const bool value = sizeof(__test<_Tp>(0)) == 1;
360};
361
362template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
363struct __pointer_traits_rebind
364{
365#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
366 typedef typename _Tp::template rebind<_Up> type;
367#else
368 typedef typename _Tp::template rebind<_Up>::other type;
369#endif
370};
371
372#ifndef _LIBCPP_HAS_NO_VARIADICS
373
374template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
375struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
376{
377#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
378 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
379#else
380 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
381#endif
382};
383
384template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
385struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
386{
387 typedef _Sp<_Up, _Args...> type;
388};
389
390#else
391
392template <template <class> class _Sp, class _Tp, class _Up>
393struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
394{
395#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
396 typedef typename _Sp<_Tp>::template rebind<_Up> type;
397#else
398 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
399#endif
400};
401
402template <template <class> class _Sp, class _Tp, class _Up>
403struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
404{
405 typedef _Sp<_Up> type;
406};
407
408template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
409struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
410{
411#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
412 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
413#else
414 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
415#endif
416};
417
418template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
419struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
420{
421 typedef _Sp<_Up, _A0> type;
422};
423
424template <template <class, class, class> class _Sp, class _Tp, class _A0,
425 class _A1, class _Up>
426struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
427{
428#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
429 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
430#else
431 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
432#endif
433};
434
435template <template <class, class, class> class _Sp, class _Tp, class _A0,
436 class _A1, class _Up>
437struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
438{
439 typedef _Sp<_Up, _A0, _A1> type;
440};
441
442template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
443 class _A1, class _A2, class _Up>
444struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
445{
446#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
447 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
448#else
449 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
450#endif
451};
452
453template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
454 class _A1, class _A2, class _Up>
455struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
456{
457 typedef _Sp<_Up, _A0, _A1, _A2> type;
458};
459
460#endif
461
462template <class _Ptr>
463struct pointer_traits
464{
465 typedef _Ptr pointer;
466 typedef typename __pointer_traits_element_type<pointer>::type element_type;
467 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
468
469#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
470 template <class _Up> using rebind = __pointer_traits_rebind<pointer, _Up>::type;
471#else
472 template <class _Up> struct rebind
473 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
474#endif
475
476private:
477 struct __nat {};
478public:
479 static pointer pointer_to(typename conditional<is_void<element_type>::value,
480 __nat, element_type>::type& __r)
481 {return pointer::pointer_to(__r);}
482};
483
484template <class _Tp>
485struct pointer_traits<_Tp*>
486{
487 typedef _Tp* pointer;
488 typedef _Tp element_type;
489 typedef ptrdiff_t difference_type;
490
491#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
492 template <class _Up> using rebind = _Up*;
493#else
494 template <class _Up> struct rebind {typedef _Up* other;};
495#endif
496
497private:
498 struct __nat {};
499public:
500 static pointer pointer_to(typename conditional<is_void<element_type>::value,
501 __nat, element_type>::type& __r)
502 {return _STD::addressof(__r);}
503};
504
505// allocator_traits
506
507namespace __has_pointer_type_imp
508{
509 template <class _Up> static __two test(...);
510 template <class _Up> static char test(typename _Up::pointer* = 0);
511}
512
513template <class _Tp>
514struct __has_pointer_type
515 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
516{
517};
518
519namespace __pointer_type_imp
520{
521
522template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
523struct __pointer_type
524{
525 typedef typename _Dp::pointer type;
526};
527
528template <class _Tp, class _Dp>
529struct __pointer_type<_Tp, _Dp, false>
530{
531 typedef _Tp* type;
532};
533
534}
535
536template <class _Tp, class _Dp>
537struct __pointer_type
538{
539 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
540};
541
542template <class _Tp>
543struct __has_const_pointer
544{
545private:
546 struct __two {char _; char __;};
547 template <class _Up> static __two __test(...);
548 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
549public:
550 static const bool value = sizeof(__test<_Tp>(0)) == 1;
551};
552
553template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
554struct __const_pointer
555{
556 typedef typename _Alloc::const_pointer type;
557};
558
559template <class _Tp, class _Ptr, class _Alloc>
560struct __const_pointer<_Tp, _Ptr, _Alloc, false>
561{
562#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
563 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
564#else
565 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
566#endif
567};
568
569template <class _Tp>
570struct __has_void_pointer
571{
572private:
573 struct __two {char _; char __;};
574 template <class _Up> static __two __test(...);
575 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
576public:
577 static const bool value = sizeof(__test<_Tp>(0)) == 1;
578};
579
580template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
581struct __void_pointer
582{
583 typedef typename _Alloc::void_pointer type;
584};
585
586template <class _Ptr, class _Alloc>
587struct __void_pointer<_Ptr, _Alloc, false>
588{
589#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
590 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
591#else
592 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
593#endif
594};
595
596template <class _Tp>
597struct __has_const_void_pointer
598{
599private:
600 struct __two {char _; char __;};
601 template <class _Up> static __two __test(...);
602 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
603public:
604 static const bool value = sizeof(__test<_Tp>(0)) == 1;
605};
606
607template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
608struct __const_void_pointer
609{
610 typedef typename _Alloc::const_void_pointer type;
611};
612
613template <class _Ptr, class _Alloc>
614struct __const_void_pointer<_Ptr, _Alloc, false>
615{
616#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
617 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
618#else
619 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
620#endif
621};
622
623template <class _T>
624inline _LIBCPP_INLINE_VISIBILITY
625_T*
626__to_raw_pointer(_T* __p)
627{
628 return __p;
629}
630
631template <class _Pointer>
632inline _LIBCPP_INLINE_VISIBILITY
633typename pointer_traits<_Pointer>::element_type*
634__to_raw_pointer(_Pointer __p)
635{
636 return _STD::__to_raw_pointer(__p.operator->());
637}
638
639template <class _Tp>
640struct __has_size_type
641{
642private:
643 struct __two {char _; char __;};
644 template <class _Up> static __two __test(...);
645 template <class _Up> static char __test(typename _Up::size_type* = 0);
646public:
647 static const bool value = sizeof(__test<_Tp>(0)) == 1;
648};
649
650template <class _Alloc, bool = __has_size_type<_Alloc>::value>
651struct __size_type
652{
653 typedef size_t type;
654};
655
656template <class _Alloc>
657struct __size_type<_Alloc, true>
658{
659 typedef typename _Alloc::size_type type;
660};
661
662template <class _Tp>
663struct __has_propagate_on_container_copy_assignment
664{
665private:
666 struct __two {char _; char __;};
667 template <class _Up> static __two __test(...);
668 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
669public:
670 static const bool value = sizeof(__test<_Tp>(0)) == 1;
671};
672
673template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
674struct __propagate_on_container_copy_assignment
675{
676 typedef false_type type;
677};
678
679template <class _Alloc>
680struct __propagate_on_container_copy_assignment<_Alloc, true>
681{
682 typedef typename _Alloc::propagate_on_container_copy_assignment type;
683};
684
685template <class _Tp>
686struct __has_propagate_on_container_move_assignment
687{
688private:
689 struct __two {char _; char __;};
690 template <class _Up> static __two __test(...);
691 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
692public:
693 static const bool value = sizeof(__test<_Tp>(0)) == 1;
694};
695
696template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
697struct __propagate_on_container_move_assignment
698{
699 typedef false_type type;
700};
701
702template <class _Alloc>
703struct __propagate_on_container_move_assignment<_Alloc, true>
704{
705 typedef typename _Alloc::propagate_on_container_move_assignment type;
706};
707
708template <class _Tp>
709struct __has_propagate_on_container_swap
710{
711private:
712 struct __two {char _; char __;};
713 template <class _Up> static __two __test(...);
714 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
715public:
716 static const bool value = sizeof(__test<_Tp>(0)) == 1;
717};
718
719template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
720struct __propagate_on_container_swap
721{
722 typedef false_type type;
723};
724
725template <class _Alloc>
726struct __propagate_on_container_swap<_Alloc, true>
727{
728 typedef typename _Alloc::propagate_on_container_swap type;
729};
730
731template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
732struct __has_rebind_other
733{
734private:
735 struct __two {char _; char __;};
736 template <class _Xp> static __two __test(...);
737 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
738public:
739 static const bool value = sizeof(__test<_Tp>(0)) == 1;
740};
741
742template <class _Tp, class _Up>
743struct __has_rebind_other<_Tp, _Up, false>
744{
745 static const bool value = false;
746};
747
748template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
749struct __allocator_traits_rebind
750{
751 typedef typename _Tp::template rebind<_Up>::other type;
752};
753
754#ifndef _LIBCPP_HAS_NO_VARIADICS
755
756template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
757struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
758{
759 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
760};
761
762template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
763struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
764{
765 typedef _Alloc<_Up, _Args...> type;
766};
767
768#else
769
770template <template <class> class _Alloc, class _Tp, class _Up>
771struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
772{
773 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
774};
775
776template <template <class> class _Alloc, class _Tp, class _Up>
777struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
778{
779 typedef _Alloc<_Up> type;
780};
781
782
783template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
784struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
785{
786 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
787};
788
789template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
790struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
791{
792 typedef _Alloc<_Up, _A0> type;
793};
794
795
796template <template <class, class, class> class _Alloc, class _Tp, class _A0,
797 class _A1, class _Up>
798struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
799{
800 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
801};
802
803template <template <class, class, class> class _Alloc, class _Tp, class _A0,
804 class _A1, class _Up>
805struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
806{
807 typedef _Alloc<_Up, _A0, _A1> type;
808};
809
810
811template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
812 class _A1, class _A2, class _Up>
813struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
814{
815 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
816};
817
818template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
819 class _A1, class _A2, class _Up>
820struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
821{
822 typedef _Alloc<_Up, _A0, _A1, _A2> type;
823};
824
825#endif
826
827#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
828
829template <class _Alloc, class _SizeType, class _ConstVoidPtr>
830auto
831__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
832 -> decltype(__a.allocate(__sz, __p), true_type());
833
834template <class _Alloc, class _SizeType, class _ConstVoidPtr>
835auto
836__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
837 -> false_type;
838
839template <class _Alloc, class _SizeType, class _ConstVoidPtr>
840struct __has_allocate_hint
841 : integral_constant<bool,
842 is_same<
843 decltype(__has_allocate_hint_test(declval<_Alloc>(),
844 declval<_SizeType>(),
845 declval<_ConstVoidPtr>())),
846 true_type>::value>
847{
848};
849
850#else
851
852template <class _Alloc, class _SizeType, class _ConstVoidPtr>
853struct __has_allocate_hint
854 : true_type
855{
856};
857
858#endif
859
860#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
861
862template <class _Alloc, class _Tp, class ..._Args>
863decltype(_STD::declval<_Alloc>().construct(_STD::declval<_Tp*>(),
864 _STD::declval<_Args>()...),
865 true_type())
866__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
867
868template <class _Alloc, class _Pointer, class ..._Args>
869false_type
870__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
871
872template <class _Alloc, class _Pointer, class ..._Args>
873struct __has_construct
874 : integral_constant<bool,
875 is_same<
876 decltype(__has_construct_test(declval<_Alloc>(),
877 declval<_Pointer>(),
878 declval<_Args>()...)),
879 true_type>::value>
880{
881};
882
883template <class _Alloc, class _Pointer>
884auto
885__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
886 -> decltype(__a.destroy(__p), true_type());
887
888template <class _Alloc, class _Pointer>
889auto
890__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
891 -> false_type;
892
893template <class _Alloc, class _Pointer>
894struct __has_destroy
895 : integral_constant<bool,
896 is_same<
897 decltype(__has_destroy_test(declval<_Alloc>(),
898 declval<_Pointer>())),
899 true_type>::value>
900{
901};
902
903template <class _Alloc>
904auto
905__has_max_size_test(_Alloc&& __a)
906 -> decltype(__a.max_size(), true_type());
907
908template <class _Alloc>
909auto
910__has_max_size_test(const volatile _Alloc& __a)
911 -> false_type;
912
913template <class _Alloc>
914struct __has_max_size
915 : integral_constant<bool,
916 is_same<
917 decltype(__has_max_size_test(declval<_Alloc&>())),
918 true_type>::value>
919{
920};
921
922template <class _Alloc>
923auto
924__has_select_on_container_copy_construction_test(_Alloc&& __a)
925 -> decltype(__a.select_on_container_copy_construction(), true_type());
926
927template <class _Alloc>
928auto
929__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
930 -> false_type;
931
932template <class _Alloc>
933struct __has_select_on_container_copy_construction
934 : integral_constant<bool,
935 is_same<
936 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
937 true_type>::value>
938{
939};
940
941#else
942
943
944#ifndef _LIBCPP_HAS_NO_VARIADICS
945
946template <class _Alloc, class _Pointer, class ..._Args>
947struct __has_construct
948 : false_type
949{
950};
951
952#endif
953
954template <class _Alloc, class _Pointer>
955struct __has_destroy
956 : false_type
957{
958};
959
960template <class _Alloc>
961struct __has_max_size
962 : true_type
963{
964};
965
966template <class _Alloc>
967struct __has_select_on_container_copy_construction
968 : false_type
969{
970};
971
972#endif
973
974template <class _Alloc>
975struct allocator_traits
976{
977 typedef _Alloc allocator_type;
978 typedef typename allocator_type::value_type value_type;
979
980 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
981 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
982 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
983 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
984
985 typedef typename __pointer_traits_difference_type<allocator_type>::type difference_type;
986 typedef typename __size_type<allocator_type>::type size_type;
987
988 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
989 propagate_on_container_copy_assignment;
990 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
991 propagate_on_container_move_assignment;
992 typedef typename __propagate_on_container_swap<allocator_type>::type
993 propagate_on_container_swap;
994
995#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
996 template <class _Tp> using rebind_alloc =
997 __allocator_traits_rebind<allocator_type, _Tp>::type;
998 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
999#else
1000 template <class _Tp> struct rebind_alloc
1001 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1002 template <class _Tp> struct rebind_traits
1003 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
1004#endif
1005
1006 static pointer allocate(allocator_type& __a, size_type __n)
1007 {return __a.allocate(__n);}
1008 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1009 {return allocate(__a, __n, __hint,
1010 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1011
1012 static void deallocate(allocator_type& __a, pointer __p, size_type __n)
1013 {__a.deallocate(__p, __n);}
1014
1015#ifndef _LIBCPP_HAS_NO_VARIADICS
1016 template <class _Tp, class... _Args>
1017 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1018 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
1019 __a, __p, _STD::forward<_Args>(__args)...);}
1020#else
1021 template <class _Tp>
1022 static void construct(allocator_type& __a, _Tp* __p)
1023 {
1024 ::new ((void*)__p) _Tp();
1025 }
1026 template <class _Tp, class _A0>
1027 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1028 {
1029 ::new ((void*)__p) _Tp(__a0);
1030 }
1031 template <class _Tp, class _A0, class _A1>
1032 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1033 const _A1& __a1)
1034 {
1035 ::new ((void*)__p) _Tp(__a0, __a1);
1036 }
1037 template <class _Tp, class _A0, class _A1, class _A2>
1038 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1039 const _A1& __a1, const _A2& __a2)
1040 {
1041 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1042 }
1043#endif
1044
1045 template <class _Tp>
1046 static void destroy(allocator_type& __a, _Tp* __p)
1047 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1048
1049 static size_type max_size(const allocator_type& __a)
1050 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1051
1052 static allocator_type
1053 select_on_container_copy_construction(const allocator_type& __a)
1054 {return select_on_container_copy_construction(
1055 __has_select_on_container_copy_construction<const allocator_type>(),
1056 __a);}
1057
1058private:
1059
1060 static pointer allocate(allocator_type& __a, size_type __n,
1061 const_void_pointer __hint, true_type)
1062 {return __a.allocate(__n, __hint);}
1063 static pointer allocate(allocator_type& __a, size_type __n,
1064 const_void_pointer __hint, false_type)
1065 {return __a.allocate(__n);}
1066
1067#ifndef _LIBCPP_HAS_NO_VARIADICS
1068 template <class _Tp, class... _Args>
1069 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1070 {__a.construct(__p, _STD::forward<_Args>(__args)...);}
1071 template <class _Tp, class... _Args>
1072 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1073 {
1074 ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
1075 }
1076#endif
1077
1078 template <class _Tp>
1079 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1080 {__a.destroy(__p);}
1081 template <class _Tp>
1082 static void __destroy(false_type, allocator_type&, _Tp* __p)
1083 {
1084 __p->~_Tp();
1085 }
1086
1087 static size_type __max_size(true_type, const allocator_type& __a)
1088 {return __a.max_size();}
1089 static size_type __max_size(false_type, const allocator_type&)
1090 {return numeric_limits<size_type>::max();}
1091
1092 static allocator_type
1093 select_on_container_copy_construction(true_type, const allocator_type& __a)
1094 {return __a.select_on_container_copy_construction();}
1095 static allocator_type
1096 select_on_container_copy_construction(false_type, const allocator_type& __a)
1097 {return __a;}
1098};
1099
1100// uses_allocator
1101
1102template <class _Tp>
1103struct __has_allocator_type
1104{
1105private:
1106 struct __two {char _; char __;};
1107 template <class _Up> static __two __test(...);
1108 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1109public:
1110 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1111};
1112
1113template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1114struct __uses_allocator
1115 : public integral_constant<bool,
1116 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1117{
1118};
1119
1120template <class _Tp, class _Alloc>
1121struct __uses_allocator<_Tp, _Alloc, false>
1122 : public false_type
1123{
1124};
1125
1126template <class _Tp, class _Alloc>
1127struct uses_allocator
1128 : public __uses_allocator<_Tp, _Alloc>
1129{
1130};
1131
Howard Hinnant60a0a8e2010-08-10 20:48:29 +00001132#if defined(_LIBCPP_MOVE) && !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001133
1134// uses-allocator construction
1135
1136template <class _Tp, class _Alloc, class ..._Args>
1137struct __uses_alloc_ctor_imp
1138{
1139 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1140 static const bool __ic =
1141 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1142 static const int value = __ua ? 2 - __ic : 0;
1143};
1144
1145template <class _Tp, class _Alloc, class ..._Args>
1146struct __uses_alloc_ctor
1147 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1148 {};
1149
1150// scoped_allocator_adaptor
1151
1152template <class ..._Allocs>
1153class scoped_allocator_adaptor;
1154
1155template <class ..._Allocs> struct __get_poc_copy_assignment;
1156
1157template <class _A0>
1158struct __get_poc_copy_assignment<_A0>
1159{
1160 static const bool value = allocator_traits<_A0>::
1161 propagate_on_container_copy_assignment::value;
1162};
1163
1164template <class _A0, class ..._Allocs>
1165struct __get_poc_copy_assignment<_A0, _Allocs...>
1166{
1167 static const bool value =
1168 allocator_traits<_A0>::propagate_on_container_copy_assignment::value ||
1169 __get_poc_copy_assignment<_Allocs...>::value;
1170};
1171
1172template <class ..._Allocs> struct __get_poc_move_assignment;
1173
1174template <class _A0>
1175struct __get_poc_move_assignment<_A0>
1176{
1177 static const bool value = allocator_traits<_A0>::
1178 propagate_on_container_move_assignment::value;
1179};
1180
1181template <class _A0, class ..._Allocs>
1182struct __get_poc_move_assignment<_A0, _Allocs...>
1183{
1184 static const bool value =
1185 allocator_traits<_A0>::propagate_on_container_move_assignment::value ||
1186 __get_poc_move_assignment<_Allocs...>::value;
1187};
1188
1189template <class ..._Allocs> struct __get_poc_swap;
1190
1191template <class _A0>
1192struct __get_poc_swap<_A0>
1193{
1194 static const bool value = allocator_traits<_A0>::
1195 propagate_on_container_swap::value;
1196};
1197
1198template <class _A0, class ..._Allocs>
1199struct __get_poc_swap<_A0, _Allocs...>
1200{
1201 static const bool value =
1202 allocator_traits<_A0>::propagate_on_container_swap::value ||
1203 __get_poc_swap<_Allocs...>::value;
1204};
1205
1206template <class ..._Allocs>
1207class __scoped_allocator_storage;
1208
1209template <class _OuterAlloc, class... _InnerAllocs>
1210class __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...>
1211 : public _OuterAlloc
1212{
1213 typedef _OuterAlloc outer_allocator_type;
1214protected:
1215 typedef scoped_allocator_adaptor<_InnerAllocs...> inner_allocator_type;
1216
1217private:
1218 inner_allocator_type __inner_;
1219
1220protected:
1221
1222 __scoped_allocator_storage() {}
1223
1224 template <class _OuterA2,
1225 class = typename enable_if<
1226 is_constructible<outer_allocator_type, _OuterA2>::value
1227 >::type>
1228 __scoped_allocator_storage(_OuterA2&& __outerAlloc,
1229 const _InnerAllocs& ...__innerAllocs)
1230 : outer_allocator_type(_STD::forward<_OuterA2>(__outerAlloc)),
1231 __inner_(__innerAllocs...) {}
1232
1233 template <class _OuterA2,
1234 class = typename enable_if<
1235 is_constructible<outer_allocator_type, const _OuterA2&>::value
1236 >::type>
1237 __scoped_allocator_storage(
1238 const __scoped_allocator_storage<_OuterA2, _InnerAllocs...>& __other)
1239 : outer_allocator_type(__other.outer_allocator()),
1240 __inner_(__other.inner_allocator()) {}
1241
1242 template <class _OuterA2,
1243 class = typename enable_if<
1244 is_constructible<outer_allocator_type, _OuterA2>::value
1245 >::type>
1246 __scoped_allocator_storage(
1247 __scoped_allocator_storage<_OuterA2, _InnerAllocs...>&& __other)
1248 : outer_allocator_type(_STD::move(__other.outer_allocator())),
1249 __inner_(_STD::move(__other.inner_allocator())) {}
1250
1251 template <class _OuterA2,
1252 class = typename enable_if<
1253 is_constructible<outer_allocator_type, _OuterA2>::value
1254 >::type>
1255 __scoped_allocator_storage(_OuterA2&& __o,
1256 const inner_allocator_type& __i)
1257 : outer_allocator_type(_STD::forward<_OuterA2>(__o)),
1258 __inner_(__i)
1259 {
1260 }
1261
1262 inner_allocator_type& inner_allocator() {return __inner_;}
1263 const inner_allocator_type& inner_allocator() const {return __inner_;}
1264
1265 outer_allocator_type& outer_allocator()
1266 {return static_cast<outer_allocator_type&>(*this);}
1267 const outer_allocator_type& outer_allocator() const
1268 {return static_cast<const outer_allocator_type&>(*this);}
1269
1270 scoped_allocator_adaptor<outer_allocator_type, _InnerAllocs...>
1271 select_on_container_copy_construction() const
1272 {
1273 return scoped_allocator_adaptor<outer_allocator_type, _InnerAllocs...>
1274 (
1275 allocator_traits<outer_allocator_type>::
1276 select_on_container_copy_construction(outer_allocator()),
1277 allocator_traits<inner_allocator_type>::
1278 select_on_container_copy_construction(inner_allocator())
1279 );
1280 }
1281
1282 template <class...> friend class __scoped_allocator_storage;
1283};
1284
1285template <class _OuterAlloc>
1286class __scoped_allocator_storage<_OuterAlloc>
1287 : public _OuterAlloc
1288{
1289 typedef _OuterAlloc outer_allocator_type;
1290protected:
1291 typedef scoped_allocator_adaptor<_OuterAlloc> inner_allocator_type;
1292
1293 __scoped_allocator_storage() {}
1294
1295 template <class _OuterA2,
1296 class = typename enable_if<
1297 is_constructible<outer_allocator_type, _OuterA2>::value
1298 >::type>
1299 __scoped_allocator_storage(_OuterA2&& __outerAlloc)
1300 : outer_allocator_type(_STD::forward<_OuterA2>(__outerAlloc)) {}
1301
1302 template <class _OuterA2,
1303 class = typename enable_if<
1304 is_constructible<outer_allocator_type, const _OuterA2&>::value
1305 >::type>
1306 __scoped_allocator_storage(
1307 const __scoped_allocator_storage<_OuterA2>& __other)
1308 : outer_allocator_type(__other.outer_allocator()) {}
1309
1310 template <class _OuterA2,
1311 class = typename enable_if<
1312 is_constructible<outer_allocator_type, _OuterA2>::value
1313 >::type>
1314 __scoped_allocator_storage(
1315 __scoped_allocator_storage<_OuterA2>&& __other)
1316 : outer_allocator_type(_STD::move(__other.outer_allocator())) {}
1317
1318 inner_allocator_type& inner_allocator()
1319 {return static_cast<inner_allocator_type&>(*this);}
1320 const inner_allocator_type& inner_allocator() const
1321 {return static_cast<const inner_allocator_type&>(*this);}
1322
1323 outer_allocator_type& outer_allocator()
1324 {return static_cast<outer_allocator_type&>(*this);}
1325 const outer_allocator_type& outer_allocator() const
1326 {return static_cast<const outer_allocator_type&>(*this);}
1327
1328 scoped_allocator_adaptor<outer_allocator_type>
1329 select_on_container_copy_construction() const
1330 {return scoped_allocator_adaptor<outer_allocator_type>(
1331 allocator_traits<outer_allocator_type>::
1332 select_on_container_copy_construction(outer_allocator())
1333 );}
1334
1335 __scoped_allocator_storage(const outer_allocator_type& __o,
1336 const inner_allocator_type& __i);
1337
1338 template <class...> friend class __scoped_allocator_storage;
1339};
1340
1341// __outermost
1342
1343template <class _Alloc>
1344decltype(declval<_Alloc>().outer_allocator(), true_type())
1345__has_outer_allocator_test(_Alloc&& __a);
1346
1347template <class _Alloc>
1348false_type
1349__has_outer_allocator_test(const volatile _Alloc& __a);
1350
1351template <class _Alloc>
1352struct __has_outer_allocator
1353 : public common_type
1354 <
1355 decltype(__has_outer_allocator_test(declval<_Alloc&>()))
1356 >::type
1357{
1358};
1359
1360template <class _Alloc, bool = __has_outer_allocator<_Alloc>::value>
1361struct __outermost
1362{
1363 typedef _Alloc type;
1364 type& operator()(type& __a) const {return __a;}
1365};
1366
1367template <class _Alloc>
1368struct __outermost<_Alloc, true>
1369{
1370 typedef typename remove_reference
1371 <
1372 decltype(_STD::declval<_Alloc>().outer_allocator())
1373 >::type _OuterAlloc;
1374 typedef typename __outermost<_OuterAlloc>::type type;
1375 type& operator()(_Alloc& __a) const
1376 {return __outermost<_OuterAlloc>()(__a.outer_allocator());}
1377};
1378
1379template <class _OuterAlloc, class... _InnerAllocs>
1380class scoped_allocator_adaptor<_OuterAlloc, _InnerAllocs...>
1381 : public __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...>
1382{
1383 typedef __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...> base;
1384 typedef allocator_traits<_OuterAlloc> _OuterTraits;
1385public:
1386 typedef _OuterAlloc outer_allocator_type;
1387 typedef typename base::inner_allocator_type inner_allocator_type;
1388 typedef typename _OuterTraits::size_type size_type;
1389 typedef typename _OuterTraits::difference_type difference_type;
1390 typedef typename _OuterTraits::pointer pointer;
1391 typedef typename _OuterTraits::const_pointer const_pointer;
1392 typedef typename _OuterTraits::void_pointer void_pointer;
1393 typedef typename _OuterTraits::const_void_pointer const_void_pointer;
1394
1395 typedef integral_constant
1396 <
1397 bool,
1398 __get_poc_copy_assignment<outer_allocator_type,
1399 _InnerAllocs...>::value
1400 > propagate_on_container_copy_assignment;
1401 typedef integral_constant
1402 <
1403 bool,
1404 __get_poc_move_assignment<outer_allocator_type,
1405 _InnerAllocs...>::value
1406 > propagate_on_container_move_assignment;
1407 typedef integral_constant
1408 <
1409 bool,
1410 __get_poc_swap<outer_allocator_type, _InnerAllocs...>::value
1411 > propagate_on_container_swap;
1412
1413 template <class _Tp>
1414 struct rebind
1415 {
1416 typedef scoped_allocator_adaptor
1417 <
1418 typename _OuterTraits::template rebind_alloc<_Tp>, _InnerAllocs...
1419 > other;
1420 };
1421
1422 scoped_allocator_adaptor() {}
1423 template <class _OuterA2,
1424 class = typename enable_if<
1425 is_constructible<outer_allocator_type, _OuterA2>::value
1426 >::type>
1427 scoped_allocator_adaptor(_OuterA2&& __outerAlloc,
1428 const _InnerAllocs& ...__innerAllocs)
1429 : base(_STD::forward<_OuterA2>(__outerAlloc), __innerAllocs...) {}
1430 // scoped_allocator_adaptor(const scoped_allocator_adaptor& __other) = default;
1431 template <class _OuterA2,
1432 class = typename enable_if<
1433 is_constructible<outer_allocator_type, const _OuterA2&>::value
1434 >::type>
1435 scoped_allocator_adaptor(
1436 const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __other)
1437 : base(__other) {}
1438 template <class _OuterA2,
1439 class = typename enable_if<
1440 is_constructible<outer_allocator_type, _OuterA2>::value
1441 >::type>
1442 scoped_allocator_adaptor(
1443 scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>&& __other)
1444 : base(_STD::move(__other)) {}
1445
1446 // ~scoped_allocator_adaptor() = default;
1447
1448 inner_allocator_type& inner_allocator()
1449 {return base::inner_allocator();}
1450 const inner_allocator_type& inner_allocator() const
1451 {return base::inner_allocator();}
1452
1453 outer_allocator_type& outer_allocator()
1454 {return base::outer_allocator();}
1455 const outer_allocator_type& outer_allocator() const
1456 {return base::outer_allocator();}
1457
1458 pointer allocate(size_type __n)
1459 {return allocator_traits<outer_allocator_type>::
1460 allocate(outer_allocator(), __n);}
1461 pointer allocate(size_type __n, const_void_pointer __hint)
1462 {return allocator_traits<outer_allocator_type>::
1463 allocate(outer_allocator(), __n, __hint);}
1464
1465 void deallocate(pointer __p, size_type __n)
1466 {allocator_traits<outer_allocator_type>::
1467 deallocate(outer_allocator(), __p, __n);}
1468
1469 size_type max_size() const
1470 {allocator_traits<outer_allocator_type>::max_size(outer_allocator());}
1471
1472 template <class _Tp, class... _Args>
1473 void construct(_Tp* __p, _Args&& ...__args)
1474 {__construct(__uses_alloc_ctor<_Tp, inner_allocator_type, _Args...>(),
1475 __p, _STD::forward<_Args>(__args)...);}
1476 template <class _Tp>
1477 void destroy(_Tp* __p)
1478 {
1479 typedef __outermost<outer_allocator_type> _OM;
1480 allocator_traits<typename _OM::type>::
1481 destroy(_OM()(outer_allocator()), __p);
1482 }
1483
1484 scoped_allocator_adaptor select_on_container_copy_construction() const
1485 {return base::select_on_container_copy_construction();}
1486
1487private:
1488
1489 template <class _OuterA2,
1490 class = typename enable_if<
1491 is_constructible<outer_allocator_type, _OuterA2>::value
1492 >::type>
1493 scoped_allocator_adaptor(_OuterA2&& __o,
1494 const inner_allocator_type& __i)
1495 : base(_STD::forward<_OuterA2>(__o), __i) {}
1496
1497 template <class _Tp, class... _Args>
1498 void __construct(integral_constant<int, 0>, _Tp* __p, _Args&& ...__args)
1499 {
1500 typedef __outermost<outer_allocator_type> _OM;
1501 allocator_traits<typename _OM::type>::construct
1502 (
1503 _OM()(outer_allocator()),
1504 __p,
1505 _STD::forward<_Args>(__args)...
1506 );
1507 }
1508
1509 template <class _Tp, class... _Args>
1510 void __construct(integral_constant<int, 1>, _Tp* __p, _Args&& ...__args)
1511 {
1512 typedef __outermost<outer_allocator_type> _OM;
1513 allocator_traits<typename _OM::type>::construct
1514 (
1515 _OM()(outer_allocator()),
1516 __p,
1517 allocator_arg,
1518 inner_allocator(),
1519 _STD::forward<_Args>(__args)...
1520 );
1521 }
1522
1523 template <class _Tp, class... _Args>
1524 void __construct(integral_constant<int, 2>, _Tp* __p, _Args&& ...__args)
1525 {
1526 typedef __outermost<outer_allocator_type> _OM;
1527 allocator_traits<typename _OM::type>::construct
1528 (
1529 _OM()(outer_allocator()),
1530 __p,
1531 _STD::forward<_Args>(__args)...,
1532 inner_allocator()
1533 );
1534 }
1535
1536 template <class...> friend class __scoped_allocator_storage;
1537};
1538
1539template <class _OuterA1, class _OuterA2>
1540inline _LIBCPP_INLINE_VISIBILITY
1541bool
1542operator==(const scoped_allocator_adaptor<_OuterA1>& __a,
1543 const scoped_allocator_adaptor<_OuterA2>& __b)
1544{
1545 return __a.outer_allocator() == __b.outer_allocator();
1546}
1547
1548template <class _OuterA1, class _OuterA2, class... _InnerAllocs>
1549inline _LIBCPP_INLINE_VISIBILITY
1550bool
1551operator==(const scoped_allocator_adaptor<_OuterA1, _InnerAllocs...>& __a,
1552 const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __b)
1553{
1554 return __a.outer_allocator() == __b.outer_allocator() &&
1555 __a.inner_allocator() == __b.inner_allocator();
1556}
1557
1558template <class _OuterA1, class _OuterA2, class... _InnerAllocs>
1559inline _LIBCPP_INLINE_VISIBILITY
1560bool
1561operator!=(const scoped_allocator_adaptor<_OuterA1, _InnerAllocs...>& __a,
1562 const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __b)
1563{
1564 return !(__a == __b);
1565}
1566
1567#endif
1568
1569// allocator
1570
1571template <class _Tp>
1572class allocator
1573{
1574public:
1575 typedef size_t size_type;
1576 typedef ptrdiff_t difference_type;
1577 typedef _Tp* pointer;
1578 typedef const _Tp* const_pointer;
1579 typedef _Tp& reference;
1580 typedef const _Tp& const_reference;
1581 typedef _Tp value_type;
1582
1583 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1584
1585 _LIBCPP_INLINE_VISIBILITY allocator() throw() {}
1586 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) throw() {}
1587 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const {return addressof(__x);}
1588 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const {return addressof(__x);}
1589 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1590 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1591 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) {::operator delete((void*)__p);}
1592 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
1593#ifdef _LIBCPP_MOVE
1594 template <class _Up, class... _Args>
1595 _LIBCPP_INLINE_VISIBILITY
1596 void
1597 construct(_Up* __p, _Args&&... __args)
1598 {
1599 ::new((void*)__p) _Up(_STD::forward<_Args>(__args)...);
1600 }
1601#else
1602 _LIBCPP_INLINE_VISIBILITY
1603 void
1604 construct(pointer __p)
1605 {
1606 ::new((void*)__p) _Tp();
1607 }
1608 template <class _A0>
1609 _LIBCPP_INLINE_VISIBILITY
1610 typename enable_if
1611 <
1612 !is_convertible<_A0, __rv<_A0> >::value,
1613 void
1614 >::type
1615 construct(pointer __p, _A0& __a0)
1616 {
1617 ::new((void*)__p) _Tp(__a0);
1618 }
1619 template <class _A0>
1620 _LIBCPP_INLINE_VISIBILITY
1621 typename enable_if
1622 <
1623 !is_convertible<_A0, __rv<_A0> >::value,
1624 void
1625 >::type
1626 construct(pointer __p, const _A0& __a0)
1627 {
1628 ::new((void*)__p) _Tp(__a0);
1629 }
1630 template <class _A0>
1631 _LIBCPP_INLINE_VISIBILITY
1632 typename enable_if
1633 <
1634 is_convertible<_A0, __rv<_A0> >::value,
1635 void
1636 >::type
1637 construct(pointer __p, _A0 __a0)
1638 {
1639 ::new((void*)__p) _Tp(_STD::move(__a0));
1640 }
1641 template <class _A0, class _A1>
1642 _LIBCPP_INLINE_VISIBILITY
1643 void
1644 construct(pointer __p, _A0& __a0, _A1& __a1)
1645 {
1646 ::new((void*)__p) _Tp(__a0, __a1);
1647 }
1648 template <class _A0, class _A1>
1649 _LIBCPP_INLINE_VISIBILITY
1650 void
1651 construct(pointer __p, const _A0& __a0, _A1& __a1)
1652 {
1653 ::new((void*)__p) _Tp(__a0, __a1);
1654 }
1655 template <class _A0, class _A1>
1656 _LIBCPP_INLINE_VISIBILITY
1657 void
1658 construct(pointer __p, _A0& __a0, const _A1& __a1)
1659 {
1660 ::new((void*)__p) _Tp(__a0, __a1);
1661 }
1662 template <class _A0, class _A1>
1663 _LIBCPP_INLINE_VISIBILITY
1664 void
1665 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1666 {
1667 ::new((void*)__p) _Tp(__a0, __a1);
1668 }
1669#endif
1670 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1671};
1672
1673template <class _Tp, class _Up>
1674inline _LIBCPP_INLINE_VISIBILITY
1675bool operator==(const allocator<_Tp>&, const allocator<_Up>&) throw() {return true;}
1676
1677template <class _Tp, class _Up>
1678inline _LIBCPP_INLINE_VISIBILITY
1679bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) throw() {return false;}
1680
1681template <class _OutputIterator, class _Tp>
1682class raw_storage_iterator
1683 : public iterator<output_iterator_tag,
1684 _Tp, // purposefully not C++03
1685 ptrdiff_t, // purposefully not C++03
1686 _Tp*, // purposefully not C++03
1687 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1688{
1689private:
1690 _OutputIterator __x_;
1691public:
1692 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1693 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1694 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1695 {::new(&*__x_) _Tp(__element); return *this;}
1696 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1697 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1698 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1699};
1700
1701template <class _Tp>
1702pair<_Tp*, ptrdiff_t>
1703get_temporary_buffer(ptrdiff_t __n)
1704{
1705 pair<_Tp*, ptrdiff_t> __r(0, 0);
1706 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1707 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1708 / sizeof(_Tp);
1709 if (__n > __m)
1710 __n = __m;
1711 while (__n > 0)
1712 {
1713 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1714 if (__r.first)
1715 {
1716 __r.second = __n;
1717 break;
1718 }
1719 __n /= 2;
1720 }
1721 return __r;
1722}
1723
1724template <class _Tp>
1725inline _LIBCPP_INLINE_VISIBILITY
1726void return_temporary_buffer(_Tp* __p) {::operator delete(__p);}
1727
1728template <class _Tp>
1729struct auto_ptr_ref
1730{
1731 _Tp* __ptr_;
1732};
1733
1734template<class _Tp>
1735class auto_ptr
1736{
1737private:
1738 _Tp* __ptr_;
1739public:
1740 typedef _Tp element_type;
1741
1742 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1743 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1744 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1745 : __ptr_(__p.release()) {}
1746 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1747 {reset(__p.release()); return *this;}
1748 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1749 {reset(__p.release()); return *this;}
1750 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1751 {reset(__p.__ptr_); return *this;}
1752 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1753
1754 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1755 {return *__ptr_;}
1756 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1757 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1758 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1759 {
1760 _Tp* __t = __ptr_;
1761 __ptr_ = 0;
1762 return __t;
1763 }
1764 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1765 {
1766 if (__ptr_ != __p)
1767 delete __ptr_;
1768 __ptr_ = __p;
1769 }
1770
1771 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1772 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1773 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1774 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1775 {return auto_ptr<_Up>(release());}
1776};
1777
1778template <>
1779class auto_ptr<void>
1780{
1781public:
1782 typedef void element_type;
1783};
1784
1785
1786template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1787 typename remove_cv<_T2>::type>::value,
1788 bool = is_empty<_T1>::value,
1789 bool = is_empty<_T2>::value>
1790struct __libcpp_compressed_pair_switch;
1791
1792template <class _T1, class _T2, bool IsSame>
1793struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1794
1795template <class _T1, class _T2, bool IsSame>
1796struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1797
1798template <class _T1, class _T2, bool IsSame>
1799struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1800
1801template <class _T1, class _T2>
1802struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1803
1804template <class _T1, class _T2>
1805struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1806
1807template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1808class __libcpp_compressed_pair_imp;
1809
1810template <class _T1, class _T2>
1811class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1812{
1813private:
1814 _T1 __first_;
1815 _T2 __second_;
1816public:
1817 typedef _T1 _T1_param;
1818 typedef _T2 _T2_param;
1819
1820 typedef typename remove_reference<_T1>::type& _T1_reference;
1821 typedef typename remove_reference<_T2>::type& _T2_reference;
1822
1823 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1824 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1825
1826 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1827 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1828 : __first_(_STD::forward<_T1_param>(__t1)) {}
1829 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1830 : __second_(_STD::forward<_T2_param>(__t2)) {}
1831 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1832 : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1833
1834#ifdef _LIBCPP_MOVE
1835 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1836 : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
1837#endif
1838
1839 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1840 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1841
1842 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1843 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1844
1845 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1846 {
1847 using _STD::swap;
1848 swap(__first_, __x.__first_);
1849 swap(__second_, __x.__second_);
1850 }
1851};
1852
1853template <class _T1, class _T2>
1854class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1855 : private _T1
1856{
1857private:
1858 _T2 __second_;
1859public:
1860 typedef _T1 _T1_param;
1861 typedef _T2 _T2_param;
1862
1863 typedef _T1& _T1_reference;
1864 typedef typename remove_reference<_T2>::type& _T2_reference;
1865
1866 typedef const _T1& _T1_const_reference;
1867 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1868
1869 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1870 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1871 : _T1(_STD::forward<_T1_param>(__t1)) {}
1872 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1873 : __second_(_STD::forward<_T2_param>(__t2)) {}
1874 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1875 : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1876
1877#ifdef _LIBCPP_MOVE
1878 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1879 : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
1880#endif
1881
1882 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1883 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1884
1885 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1886 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1887
1888 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1889 {
1890 using _STD::swap;
1891 swap(__second_, __x.__second_);
1892 }
1893};
1894
1895template <class _T1, class _T2>
1896class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1897 : private _T2
1898{
1899private:
1900 _T1 __first_;
1901public:
1902 typedef _T1 _T1_param;
1903 typedef _T2 _T2_param;
1904
1905 typedef typename remove_reference<_T1>::type& _T1_reference;
1906 typedef _T2& _T2_reference;
1907
1908 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1909 typedef const _T2& _T2_const_reference;
1910
1911 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1912 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1913 : __first_(_STD::forward<_T1_param>(__t1)) {}
1914 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1915 : _T2(_STD::forward<_T2_param>(__t2)) {}
1916 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1917 : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1918
1919#ifdef _LIBCPP_MOVE
1920 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1921 : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
1922#endif
1923
1924 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1925 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1926
1927 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1928 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1929
1930 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1931 {
1932 using _STD::swap;
1933 swap(__first_, __x.__first_);
1934 }
1935};
1936
1937template <class _T1, class _T2>
1938class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1939 : private _T1,
1940 private _T2
1941{
1942public:
1943 typedef _T1 _T1_param;
1944 typedef _T2 _T2_param;
1945
1946 typedef _T1& _T1_reference;
1947 typedef _T2& _T2_reference;
1948
1949 typedef const _T1& _T1_const_reference;
1950 typedef const _T2& _T2_const_reference;
1951
1952 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1953 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1954 : _T1(_STD::forward<_T1_param>(__t1)) {}
1955 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1956 : _T2(_STD::forward<_T2_param>(__t2)) {}
1957 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1958 : _T1(_STD::forward<_T1_param>(__t1)), _T2(_STD::forward<_T2_param>(__t2)) {}
1959
1960#ifdef _LIBCPP_MOVE
1961 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1962 : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
1963#endif
1964
1965 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1966 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1967
1968 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1969 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1970
1971 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1972 {
1973 }
1974};
1975
1976template <class _T1, class _T2>
1977class __compressed_pair
1978 : private __libcpp_compressed_pair_imp<_T1, _T2>
1979{
1980 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
1981public:
1982 typedef typename base::_T1_param _T1_param;
1983 typedef typename base::_T2_param _T2_param;
1984
1985 typedef typename base::_T1_reference _T1_reference;
1986 typedef typename base::_T2_reference _T2_reference;
1987
1988 typedef typename base::_T1_const_reference _T1_const_reference;
1989 typedef typename base::_T2_const_reference _T2_const_reference;
1990
1991 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
1992 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
1993 : base(_STD::forward<_T1_param>(__t1)) {}
1994 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
1995 : base(_STD::forward<_T2_param>(__t2)) {}
1996 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
1997 : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
1998
1999#ifdef _LIBCPP_MOVE
2000 __compressed_pair(__compressed_pair&& __p)
2001 : base(_STD::move(__p)) {}
2002#endif
2003
2004 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return base::first();}
2005 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return base::first();}
2006
2007 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return base::second();}
2008 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return base::second();}
2009
2010 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x) {base::swap(__x);}
2011};
2012
2013template <class _T1, class _T2>
2014inline _LIBCPP_INLINE_VISIBILITY
2015void
2016swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2017 {__x.swap(__y);}
2018
2019template <class _Tp>
2020struct default_delete
2021{
2022 _LIBCPP_INLINE_VISIBILITY default_delete() {}
2023 template <class _Up>
2024 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
2025 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) {}
2026 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
2027 {
2028 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2029 delete __ptr;
2030 }
2031};
2032
2033template <class _Tp>
2034struct default_delete<_Tp[]>
2035{
2036 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
2037 {
2038 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2039 delete [] __ptr;
2040 }
2041private:
2042 template <class _Up> void operator() (_Up*) const;
2043};
2044
2045template <class _Tp, class _Dp = default_delete<_Tp> >
2046class unique_ptr
2047{
2048public:
2049 typedef _Tp element_type;
2050 typedef _Dp deleter_type;
2051 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2052private:
2053 __compressed_pair<pointer, deleter_type> __ptr_;
2054
2055#ifdef _LIBCPP_MOVE
2056 unique_ptr(const unique_ptr&);
2057 unique_ptr& operator=(const unique_ptr&);
2058 template <class _Up, class _Ep>
2059 unique_ptr(const unique_ptr<_Up, _Ep>&);
2060 template <class _Up, class _Ep>
2061 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
2062#else
2063 unique_ptr(unique_ptr&);
2064 template <class _Up, class _Ep>
2065 unique_ptr(unique_ptr<_Up, _Ep>&);
2066 unique_ptr& operator=(unique_ptr&);
2067 template <class _Up, class _Ep>
2068 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2069#endif
2070
2071 struct __nat {int __for_bool_;};
2072
2073 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2074 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2075public:
2076 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2077 : __ptr_(pointer())
2078 {
2079 static_assert(!is_pointer<deleter_type>::value,
2080 "unique_ptr constructed with null function pointer deleter");
2081 }
2082 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2083 : __ptr_(pointer())
2084 {
2085 static_assert(!is_pointer<deleter_type>::value,
2086 "unique_ptr constructed with null function pointer deleter");
2087 }
2088 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2089 : __ptr_(_STD::move(__p))
2090 {
2091 static_assert(!is_pointer<deleter_type>::value,
2092 "unique_ptr constructed with null function pointer deleter");
2093 }
2094
2095 template <class _Up>
2096 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(auto_ptr<_Up>& __p,
2097 typename enable_if<
2098 is_convertible<_Up*, _Tp*>::value &&
2099 is_same<_Dp, default_delete<_Tp> >::value,
2100 __nat
2101 >::type = __nat())
2102 : __ptr_(__p.release())
2103 {
2104 }
2105
2106#ifdef _LIBCPP_MOVE
2107 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2108 is_reference<deleter_type>::value,
2109 deleter_type,
2110 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2111 : __ptr_(__p, __d) {}
2112
2113 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
2114 : __ptr_(__p, _STD::move(__d))
2115 {
2116 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2117 }
2118 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2119 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2120 template <class _Up, class _Ep>
2121 _LIBCPP_INLINE_VISIBILITY
2122 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2123 typename enable_if
2124 <
2125 !is_array<_Up>::value &&
2126 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2127 is_convertible<_Ep, deleter_type>::value &&
2128 (
2129 !is_reference<deleter_type>::value ||
2130 is_same<deleter_type, _Ep>::value
2131 ),
2132 __nat
2133 >::type = __nat())
2134 : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2135
2136 template <class _Up>
2137 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2138 typename enable_if<
2139 is_convertible<_Up*, _Tp*>::value &&
2140 is_same<_Dp, default_delete<_Tp> >::value,
2141 __nat
2142 >::type = __nat())
2143 : __ptr_(__p.release())
2144 {
2145 }
2146
2147 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2148 {
2149 reset(__u.release());
2150 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2151 return *this;
2152 }
2153
2154 template <class _Up, class _Ep>
2155 _LIBCPP_INLINE_VISIBILITY
2156 typename enable_if
2157 <
2158 !is_array<_Up>::value,
2159 unique_ptr&
2160 >::type
2161 operator=(unique_ptr<_Up, _Ep>&& __u)
2162 {
2163 reset(__u.release());
2164 __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2165 return *this;
2166 }
2167#else
2168
2169 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2170 {
2171 return __rv<unique_ptr>(*this);
2172 }
2173
2174 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2175 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2176
2177 template <class _Up, class _Ep>
2178 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2179 {
2180 reset(__u.release());
2181 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2182 return *this;
2183 }
2184
2185 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2186 : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2187
2188 template <class _Up>
2189 _LIBCPP_INLINE_VISIBILITY
2190 typename enable_if<
2191 is_convertible<_Up*, _Tp*>::value &&
2192 is_same<_Dp, default_delete<_Tp> >::value,
2193 unique_ptr&
2194 >::type
2195 operator=(auto_ptr<_Up> __p)
2196 {reset(__p.release()); return *this;}
2197
2198#endif
2199 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2200
2201 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2202 {
2203 reset();
2204 return *this;
2205 }
2206
2207 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2208 {return *__ptr_.first();}
2209 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_.first();}
2210 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2211 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2212 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2213 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2214
2215 _LIBCPP_INLINE_VISIBILITY pointer release()
2216 {
2217 pointer __t = __ptr_.first();
2218 __ptr_.first() = pointer();
2219 return __t;
2220 }
2221
2222 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2223 {
2224 pointer __tmp = __ptr_.first();
2225 __ptr_.first() = __p;
2226 if (__tmp)
2227 __ptr_.second()(__tmp);
2228 }
2229
2230 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2231};
2232
2233template <class _Tp, class _Dp>
2234class unique_ptr<_Tp[], _Dp>
2235{
2236public:
2237 typedef _Tp element_type;
2238 typedef _Dp deleter_type;
2239 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2240private:
2241 __compressed_pair<pointer, deleter_type> __ptr_;
2242
2243#ifdef _LIBCPP_MOVE
2244 unique_ptr(const unique_ptr&);
2245 unique_ptr& operator=(const unique_ptr&);
2246#else
2247 unique_ptr(unique_ptr&);
2248 template <class _Up>
2249 unique_ptr(unique_ptr<_Up>&);
2250 unique_ptr& operator=(unique_ptr&);
2251 template <class _Up>
2252 unique_ptr& operator=(unique_ptr<_Up>&);
2253#endif
2254
2255 struct __nat {int __for_bool_;};
2256
2257 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2258 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2259public:
2260 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2261 : __ptr_(pointer())
2262 {
2263 static_assert(!is_pointer<deleter_type>::value,
2264 "unique_ptr constructed with null function pointer deleter");
2265 }
2266 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2267 : __ptr_(pointer())
2268 {
2269 static_assert(!is_pointer<deleter_type>::value,
2270 "unique_ptr constructed with null function pointer deleter");
2271 }
2272#ifdef _LIBCPP_MOVE
2273 template <class _P,
2274 class = typename enable_if<is_same<_P, pointer>::value>::type
2275 >
2276 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p)
2277 : __ptr_(__p)
2278 {
2279 static_assert(!is_pointer<deleter_type>::value,
2280 "unique_ptr constructed with null function pointer deleter");
2281 }
2282
2283 template <class _P,
2284 class = typename enable_if<is_same<_P, pointer>::value>::type
2285 >
2286 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2287 is_reference<deleter_type>::value,
2288 deleter_type,
2289 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2290 : __ptr_(__p, __d) {}
2291
2292 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2293 is_reference<deleter_type>::value,
2294 deleter_type,
2295 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2296 : __ptr_(pointer(), __d) {}
2297
2298 template <class _P,
2299 class = typename enable_if<is_same<_P, pointer>::value ||
2300 is_same<_P, nullptr_t>::value>::type
2301 >
2302 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
2303 : __ptr_(__p, _STD::move(__d))
2304 {
2305 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2306 }
2307
2308 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
2309 : __ptr_(pointer(), _STD::move(__d))
2310 {
2311 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2312 }
2313
2314 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2315 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2316
2317 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2318 {
2319 reset(__u.release());
2320 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2321 return *this;
2322 }
2323#else
2324
2325 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2326 : __ptr_(__p)
2327 {
2328 static_assert(!is_pointer<deleter_type>::value,
2329 "unique_ptr constructed with null function pointer deleter");
2330 }
2331
2332 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2333 : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2334
2335 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2336 : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2337
2338 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2339 {
2340 return __rv<unique_ptr>(*this);
2341 }
2342
2343 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2344 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2345
2346 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2347 {
2348 reset(__u->release());
2349 __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2350 return *this;
2351 }
2352
2353#endif
2354 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2355
2356 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2357 {
2358 reset();
2359 return *this;
2360 }
2361
2362 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2363 {return __ptr_.first()[__i];}
2364 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2365 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2366 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2367 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2368
2369 _LIBCPP_INLINE_VISIBILITY pointer release()
2370 {
2371 pointer __t = __ptr_.first();
2372 __ptr_.first() = pointer();
2373 return __t;
2374 }
2375
2376#ifdef _LIBCPP_MOVE
2377 template <class _P,
2378 class = typename enable_if<is_same<_P, pointer>::value>::type
2379 >
2380 _LIBCPP_INLINE_VISIBILITY void reset(_P __p)
2381 {
2382 pointer __tmp = __ptr_.first();
2383 __ptr_.first() = __p;
2384 if (__tmp)
2385 __ptr_.second()(__tmp);
2386 }
2387 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t)
2388 {
2389 pointer __tmp = __ptr_.first();
2390 __ptr_.first() = nullptr;
2391 if (__tmp)
2392 __ptr_.second()(__tmp);
2393 }
2394 _LIBCPP_INLINE_VISIBILITY void reset()
2395 {
2396 pointer __tmp = __ptr_.first();
2397 __ptr_.first() = nullptr;
2398 if (__tmp)
2399 __ptr_.second()(__tmp);
2400 }
2401#else
2402 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2403 {
2404 pointer __tmp = __ptr_.first();
2405 __ptr_.first() = __p;
2406 if (__tmp)
2407 __ptr_.second()(__tmp);
2408 }
2409#endif
2410
2411 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2412private:
2413
2414#ifndef _LIBCPP_MOVE
2415 template <class _Up>
2416 explicit unique_ptr(_Up);
2417 template <class _Up>
2418 unique_ptr(_Up __u,
2419 typename conditional<
2420 is_reference<deleter_type>::value,
2421 deleter_type,
2422 typename add_lvalue_reference<const deleter_type>::type>::type,
2423 typename enable_if
2424 <
2425 is_convertible<_Up, pointer>::value,
2426 __nat
2427 >::type = __nat());
2428#endif
2429};
2430
2431template <class _Tp, class _Dp>
2432inline _LIBCPP_INLINE_VISIBILITY
2433void
2434swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) {__x.swap(__y);}
2435
2436template <class _T1, class _D1, class _T2, class _D2>
2437inline _LIBCPP_INLINE_VISIBILITY
2438bool
2439operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2440
2441template <class _T1, class _D1, class _T2, class _D2>
2442inline _LIBCPP_INLINE_VISIBILITY
2443bool
2444operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2445
2446template <class _T1, class _D1, class _T2, class _D2>
2447inline _LIBCPP_INLINE_VISIBILITY
2448bool
2449operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2450
2451template <class _T1, class _D1, class _T2, class _D2>
2452inline _LIBCPP_INLINE_VISIBILITY
2453bool
2454operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2455
2456template <class _T1, class _D1, class _T2, class _D2>
2457inline _LIBCPP_INLINE_VISIBILITY
2458bool
2459operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2460
2461template <class _T1, class _D1, class _T2, class _D2>
2462inline _LIBCPP_INLINE_VISIBILITY
2463bool
2464operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2465
Howard Hinnant21aefc32010-06-03 16:42:57 +00002466template <class> struct hash;
2467
2468template<class _Tp>
2469struct hash<_Tp*>
2470 : public unary_function<_Tp*, size_t>
2471{
2472 size_t operator()(_Tp* __v) const
2473 {
2474 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2475 return *__p;
2476 }
2477};
2478
2479template <class _Tp, class _Dp>
2480struct hash<unique_ptr<_Tp, _Dp> >
2481{
2482 typedef unique_ptr<_Tp, _Dp> argument_type;
2483 typedef size_t result_type;
2484 result_type operator()(const argument_type& __ptr) const
2485 {
2486 typedef typename argument_type::pointer pointer;
2487 return hash<pointer>()(__ptr.get());
2488 }
2489};
2490
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491struct __destruct_n
2492{
2493private:
2494 size_t size;
2495
2496 template <class _Tp>
2497 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type)
2498 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2499
2500 template <class _Tp>
2501 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type)
2502 {}
2503
2504 _LIBCPP_INLINE_VISIBILITY void __incr(false_type)
2505 {++size;}
2506 _LIBCPP_INLINE_VISIBILITY void __incr(true_type)
2507 {}
2508
2509 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type)
2510 {size = __s;}
2511 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type)
2512 {}
2513public:
2514 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) : size(__s) {}
2515
2516 template <class _Tp>
2517 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*)
2518 {__incr(integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2519
2520 template <class _Tp>
2521 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*)
2522 {__set(__s, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2523
2524 template <class _Tp>
2525 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p)
2526 {__process(__p, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2527};
2528
2529template <class _Alloc>
2530class __allocator_destructor
2531{
2532 typedef allocator_traits<_Alloc> __alloc_traits;
2533public:
2534 typedef typename __alloc_traits::pointer pointer;
2535 typedef typename __alloc_traits::size_type size_type;
2536private:
2537 _Alloc& __alloc_;
2538 size_type __s_;
2539public:
2540 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2541 : __alloc_(__a), __s_(__s) {}
2542 void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2543};
2544
2545template <class _InputIterator, class _ForwardIterator>
2546_ForwardIterator
2547uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2548{
2549 __destruct_n __d(0);
2550 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2551 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2552 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2553 ::new(&*__r) value_type(*__f);
2554 __h.release();
2555 return __r;
2556}
2557
2558template <class _InputIterator, class _Size, class _ForwardIterator>
2559_ForwardIterator
2560uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2561{
2562 __destruct_n __d(0);
2563 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2564 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2565 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2566 ::new(&*__r) value_type(*__f);
2567 __h.release();
2568 return __r;
2569}
2570
2571template <class _ForwardIterator, class _Tp>
2572void
2573uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2574{
2575 __destruct_n __d(0);
2576 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2577 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2578 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2579 ::new(&*__f) value_type(__x);
2580 __h.release();
2581}
2582
2583template <class _ForwardIterator, class _Size, class _Tp>
2584void
2585uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2586{
2587 __destruct_n __d(0);
2588 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2589 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2590 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2591 ::new(&*__f) value_type(__x);
2592 __h.release();
2593}
2594
2595class bad_weak_ptr
2596 : public std::exception
2597{
2598public:
2599 virtual ~bad_weak_ptr() throw();
2600 virtual const char* what() const throw();
2601};
2602
2603template<class _Tp> class weak_ptr;
2604
2605class __shared_count
2606{
2607 __shared_count(const __shared_count&);
2608 __shared_count& operator=(const __shared_count&);
2609
2610protected:
2611 long __shared_owners_;
2612 virtual ~__shared_count();
2613private:
2614 virtual void __on_zero_shared() = 0;
2615
2616public:
2617 explicit __shared_count(long __refs = 0)
2618 : __shared_owners_(__refs) {}
2619
2620 void __add_shared();
2621 void __release_shared();
2622 long use_count() const {return __shared_owners_ + 1;}
2623};
2624
2625class __shared_weak_count
2626 : private __shared_count
2627{
2628 long __shared_weak_owners_;
2629
2630public:
2631 explicit __shared_weak_count(long __refs = 0)
2632 : __shared_count(__refs),
2633 __shared_weak_owners_(__refs) {}
2634protected:
2635 virtual ~__shared_weak_count();
2636
2637public:
2638 void __add_shared();
2639 void __add_weak();
2640 void __release_shared();
2641 void __release_weak();
2642 long use_count() const {return __shared_count::use_count();}
2643 __shared_weak_count* lock();
2644
Howard Hinnantd4444702010-08-11 17:04:31 +00002645#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002646 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002647#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648private:
2649 virtual void __on_zero_shared_weak() = 0;
2650};
2651
2652template <class _Tp, class _Dp, class _Alloc>
2653class __shared_ptr_pointer
2654 : public __shared_weak_count
2655{
2656 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2657public:
2658 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2659 : __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2660
Howard Hinnantd4444702010-08-11 17:04:31 +00002661#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002663#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664
2665private:
2666 virtual void __on_zero_shared();
2667 virtual void __on_zero_shared_weak();
2668};
2669
Howard Hinnantd4444702010-08-11 17:04:31 +00002670#ifndef _LIBCPP_NO_RTTI
2671
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002672template <class _Tp, class _Dp, class _Alloc>
2673const void*
2674__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
2675{
2676 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2677}
2678
Howard Hinnantd4444702010-08-11 17:04:31 +00002679#endif
2680
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681template <class _Tp, class _Dp, class _Alloc>
2682void
2683__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
2684{
2685 __data_.first().second()(__data_.first().first());
2686 __data_.first().second().~_Dp();
2687}
2688
2689template <class _Tp, class _Dp, class _Alloc>
2690void
2691__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak()
2692{
2693 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2694 __data_.second().~_Alloc();
2695 __a.deallocate(this, 1);
2696}
2697
2698template <class _Tp, class _Alloc>
2699class __shared_ptr_emplace
2700 : public __shared_weak_count
2701{
2702 __compressed_pair<_Alloc, _Tp> __data_;
2703public:
2704#ifndef _LIBCPP_HAS_NO_VARIADICS
2705
2706 __shared_ptr_emplace(_Alloc __a)
2707 : __data_(_STD::move(__a)) {}
2708
2709 template <class ..._Args>
2710 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2711 : __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2712
2713#else // _LIBCPP_HAS_NO_VARIADICS
2714
2715 __shared_ptr_emplace(_Alloc __a)
2716 : __data_(__a) {}
2717
2718 template <class _A0>
2719 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2720 : __data_(__a, _Tp(__a0)) {}
2721
2722 template <class _A0, class _A1>
2723 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2724 : __data_(__a, _Tp(__a0, __a1)) {}
2725
2726 template <class _A0, class _A1, class _A2>
2727 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2728 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2729
2730#endif // _LIBCPP_HAS_NO_VARIADICS
2731
2732private:
2733 virtual void __on_zero_shared();
2734 virtual void __on_zero_shared_weak();
2735public:
2736 _Tp* get() {return &__data_.second();}
2737};
2738
2739template <class _Tp, class _Alloc>
2740void
2741__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared()
2742{
2743 __data_.second().~_Tp();
2744}
2745
2746template <class _Tp, class _Alloc>
2747void
2748__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak()
2749{
2750 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2751 __data_.first().~_Alloc();
2752 __a.deallocate(this, 1);
2753}
2754
2755template<class _Tp> class enable_shared_from_this;
2756
2757template<class _Tp>
2758class shared_ptr
2759{
2760public:
2761 typedef _Tp element_type;
2762private:
2763 element_type* __ptr_;
2764 __shared_weak_count* __cntrl_;
2765
2766 struct __nat {int __for_bool_;};
2767public:
2768 shared_ptr();
2769 shared_ptr(nullptr_t);
2770 template<class _Yp> explicit shared_ptr(_Yp* __p);
2771 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2772 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
2773 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2774 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
2775 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p);
2776 shared_ptr(const shared_ptr& __r);
2777 template<class _Yp>
2778 shared_ptr(const shared_ptr<_Yp>& __r,
2779 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
2780#ifdef _LIBCPP_MOVE
2781 shared_ptr(shared_ptr&& __r);
2782 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
2783 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
2784#endif
2785 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
2786 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
2787#ifdef _LIBCPP_MOVE
2788 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2789#else
2790 template<class _Yp> shared_ptr(auto_ptr<_Yp>& __r);
2791#endif
2792#ifdef _LIBCPP_MOVE
2793private:
2794 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
2795public:
2796 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2797 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2798 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2799 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2800#else
2801 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2802 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2803 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2804 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2805#endif
2806
2807 ~shared_ptr();
2808
2809 shared_ptr& operator=(const shared_ptr& __r);
2810 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r);
2811#ifdef _LIBCPP_MOVE
2812 shared_ptr& operator=(shared_ptr&& __r);
2813 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2814 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
2815#else
2816 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>& __r);
2817#endif
2818#ifdef _LIBCPP_MOVE
2819private:
2820 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
2821public:
2822 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
2823#else
2824 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
2825#endif
2826
2827 void swap(shared_ptr& __r);
2828 void reset();
2829 template<class _Yp> void reset(_Yp* __p);
2830 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2831 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2832
2833 element_type* get() const {return __ptr_;}
2834 typename add_lvalue_reference<element_type>::type operator*() const {return *__ptr_;}
2835 element_type* operator->() const {return __ptr_;}
2836 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
2837 bool unique() const {return use_count() == 1;}
2838 bool empty() const {return __cntrl_ == 0;}
2839 /*explicit*/ operator bool() const {return get() != 0;}
2840 template <class _U> bool owner_before(shared_ptr<_U> const& __p) const
2841 {return __cntrl_ < __p.__cntrl_;}
2842 template <class _U> bool owner_before(weak_ptr<_U> const& __p) const
2843 {return __cntrl_ < __p.__cntrl_;}
2844
Howard Hinnantd4444702010-08-11 17:04:31 +00002845#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846 template <class _Dp>
2847 _Dp* __get_deleter() const
2848 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnantd4444702010-08-11 17:04:31 +00002849#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850
2851#ifndef _LIBCPP_HAS_NO_VARIADICS
2852
2853 template<class ..._Args>
2854 static
2855 shared_ptr<_Tp>
2856 make_shared(_Args&& ...__args);
2857
2858 template<class _Alloc, class ..._Args>
2859 static
2860 shared_ptr<_Tp>
2861 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2862
2863#else // _LIBCPP_HAS_NO_VARIADICS
2864
2865 static shared_ptr<_Tp> make_shared();
2866
2867 template<class _A0>
2868 static shared_ptr<_Tp> make_shared(_A0&);
2869
2870 template<class _A0, class _A1>
2871 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2872
2873 template<class _A0, class _A1, class _A2>
2874 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2875
2876 template<class _Alloc>
2877 static shared_ptr<_Tp>
2878 allocate_shared(const _Alloc& __a);
2879
2880 template<class _Alloc, class _A0>
2881 static shared_ptr<_Tp>
2882 allocate_shared(const _Alloc& __a, _A0& __a0);
2883
2884 template<class _Alloc, class _A0, class _A1>
2885 static shared_ptr<_Tp>
2886 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2887
2888 template<class _Alloc, class _A0, class _A1, class _A2>
2889 static shared_ptr<_Tp>
2890 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2891
2892#endif // _LIBCPP_HAS_NO_VARIADICS
2893
2894private:
2895
2896 template <class _Yp>
2897 void
2898 __enable_weak_this(const enable_shared_from_this<_Yp>* __e)
2899 {
2900 if (__e)
2901 __e->__weak_this_ = *this;
2902 }
2903
2904 void __enable_weak_this(const void*) {}
2905
2906 template <class _Up> friend class shared_ptr;
2907 template <class _Up> friend class weak_ptr;
2908};
2909
2910template<class _Tp>
2911inline _LIBCPP_INLINE_VISIBILITY
2912shared_ptr<_Tp>::shared_ptr()
2913 : __ptr_(0),
2914 __cntrl_(0)
2915{
2916}
2917
2918template<class _Tp>
2919inline _LIBCPP_INLINE_VISIBILITY
2920shared_ptr<_Tp>::shared_ptr(nullptr_t)
2921 : __ptr_(0),
2922 __cntrl_(0)
2923{
2924}
2925
2926template<class _Tp>
2927template<class _Yp>
2928shared_ptr<_Tp>::shared_ptr(_Yp* __p)
2929 : __ptr_(__p)
2930{
2931 unique_ptr<_Yp> __hold(__p);
2932 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2933 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
2934 __hold.release();
2935 __enable_weak_this(__p);
2936}
2937
2938template<class _Tp>
2939template<class _Yp, class _Dp>
2940shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
2941 : __ptr_(__p)
2942{
2943#ifndef _LIBCPP_NO_EXCEPTIONS
2944 try
2945 {
2946#endif
2947 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
2948 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
2949 __enable_weak_this(__p);
2950#ifndef _LIBCPP_NO_EXCEPTIONS
2951 }
2952 catch (...)
2953 {
2954 __d(__p);
2955 throw;
2956 }
2957#endif
2958}
2959
2960template<class _Tp>
2961template<class _Dp>
2962shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
2963 : __ptr_(0)
2964{
2965#ifndef _LIBCPP_NO_EXCEPTIONS
2966 try
2967 {
2968#endif
2969 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
2970 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
2971#ifndef _LIBCPP_NO_EXCEPTIONS
2972 }
2973 catch (...)
2974 {
2975 __d(__p);
2976 throw;
2977 }
2978#endif
2979}
2980
2981template<class _Tp>
2982template<class _Yp, class _Dp, class _Alloc>
2983shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
2984 : __ptr_(__p)
2985{
2986#ifndef _LIBCPP_NO_EXCEPTIONS
2987 try
2988 {
2989#endif
2990 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
2991 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2992 typedef __allocator_destructor<_A2> _D2;
2993 _A2 __a2(__a);
2994 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2995 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2996 __cntrl_ = __hold2.release();
2997 __enable_weak_this(__p);
2998#ifndef _LIBCPP_NO_EXCEPTIONS
2999 }
3000 catch (...)
3001 {
3002 __d(__p);
3003 throw;
3004 }
3005#endif
3006}
3007
3008template<class _Tp>
3009template<class _Dp, class _Alloc>
3010shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3011 : __ptr_(0)
3012{
3013#ifndef _LIBCPP_NO_EXCEPTIONS
3014 try
3015 {
3016#endif
3017 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3018 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3019 typedef __allocator_destructor<_A2> _D2;
3020 _A2 __a2(__a);
3021 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3022 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3023 __cntrl_ = __hold2.release();
3024#ifndef _LIBCPP_NO_EXCEPTIONS
3025 }
3026 catch (...)
3027 {
3028 __d(__p);
3029 throw;
3030 }
3031#endif
3032}
3033
3034template<class _Tp>
3035template<class _Yp>
3036inline _LIBCPP_INLINE_VISIBILITY
3037shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p)
3038 : __ptr_(__p),
3039 __cntrl_(__r.__cntrl_)
3040{
3041 if (__cntrl_)
3042 __cntrl_->__add_shared();
3043}
3044
3045template<class _Tp>
3046inline _LIBCPP_INLINE_VISIBILITY
3047shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r)
3048 : __ptr_(__r.__ptr_),
3049 __cntrl_(__r.__cntrl_)
3050{
3051 if (__cntrl_)
3052 __cntrl_->__add_shared();
3053}
3054
3055template<class _Tp>
3056template<class _Yp>
3057inline _LIBCPP_INLINE_VISIBILITY
3058shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3059 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3060 : __ptr_(__r.__ptr_),
3061 __cntrl_(__r.__cntrl_)
3062{
3063 if (__cntrl_)
3064 __cntrl_->__add_shared();
3065}
3066
3067#ifdef _LIBCPP_MOVE
3068
3069template<class _Tp>
3070inline _LIBCPP_INLINE_VISIBILITY
3071shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r)
3072 : __ptr_(__r.__ptr_),
3073 __cntrl_(__r.__cntrl_)
3074{
3075 __r.__ptr_ = 0;
3076 __r.__cntrl_ = 0;
3077}
3078
3079template<class _Tp>
3080template<class _Yp>
3081inline _LIBCPP_INLINE_VISIBILITY
3082shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3083 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3084 : __ptr_(__r.__ptr_),
3085 __cntrl_(__r.__cntrl_)
3086{
3087 __r.__ptr_ = 0;
3088 __r.__cntrl_ = 0;
3089}
3090
3091#endif
3092
3093template<class _Tp>
3094template<class _Yp>
3095#ifdef _LIBCPP_MOVE
3096shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3097#else
3098shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>& __r)
3099#endif
3100 : __ptr_(__r.get())
3101{
3102 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3103 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3104 __enable_weak_this(__r.get());
3105 __r.release();
3106}
3107
3108template<class _Tp>
3109template <class _Yp, class _Dp>
3110#ifdef _LIBCPP_MOVE
3111shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3112#else
3113shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3114#endif
3115 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3116 : __ptr_(__r.get())
3117{
3118 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3119 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3120 __enable_weak_this(__r.get());
3121 __r.release();
3122}
3123
3124template<class _Tp>
3125template <class _Yp, class _Dp>
3126#ifdef _LIBCPP_MOVE
3127shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3128#else
3129shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3130#endif
3131 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3132 : __ptr_(__r.get())
3133{
3134 typedef __shared_ptr_pointer<_Yp*,
3135 reference_wrapper<typename remove_reference<_Dp>::type>,
3136 allocator<_Yp> > _CntrlBlk;
3137 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3138 __enable_weak_this(__r.get());
3139 __r.release();
3140}
3141
3142#ifndef _LIBCPP_HAS_NO_VARIADICS
3143
3144template<class _Tp>
3145template<class ..._Args>
3146shared_ptr<_Tp>
3147shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3148{
3149 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3150 typedef allocator<_CntrlBlk> _A2;
3151 typedef __allocator_destructor<_A2> _D2;
3152 _A2 __a2;
3153 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3154 ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3155 shared_ptr<_Tp> __r;
3156 __r.__ptr_ = __hold2.get()->get();
3157 __r.__cntrl_ = __hold2.release();
3158 __r.__enable_weak_this(__r.__ptr_);
3159 return __r;
3160}
3161
3162template<class _Tp>
3163template<class _Alloc, class ..._Args>
3164shared_ptr<_Tp>
3165shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3166{
3167 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3168 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3169 typedef __allocator_destructor<_A2> _D2;
3170 _A2 __a2(__a);
3171 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3172 ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3173 shared_ptr<_Tp> __r;
3174 __r.__ptr_ = __hold2.get()->get();
3175 __r.__cntrl_ = __hold2.release();
3176 __r.__enable_weak_this(__r.__ptr_);
3177 return __r;
3178}
3179
3180#else // _LIBCPP_HAS_NO_VARIADICS
3181
3182template<class _Tp>
3183shared_ptr<_Tp>
3184shared_ptr<_Tp>::make_shared()
3185{
3186 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3187 typedef allocator<_CntrlBlk> _Alloc2;
3188 typedef __allocator_destructor<_Alloc2> _D2;
3189 _Alloc2 __alloc2;
3190 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3191 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3192 shared_ptr<_Tp> __r;
3193 __r.__ptr_ = __hold2.get()->get();
3194 __r.__cntrl_ = __hold2.release();
3195 __r.__enable_weak_this(__r.__ptr_);
3196 return __r;
3197}
3198
3199template<class _Tp>
3200template<class _A0>
3201shared_ptr<_Tp>
3202shared_ptr<_Tp>::make_shared(_A0& __a0)
3203{
3204 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3205 typedef allocator<_CntrlBlk> _Alloc2;
3206 typedef __allocator_destructor<_Alloc2> _D2;
3207 _Alloc2 __alloc2;
3208 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3209 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3210 shared_ptr<_Tp> __r;
3211 __r.__ptr_ = __hold2.get()->get();
3212 __r.__cntrl_ = __hold2.release();
3213 __r.__enable_weak_this(__r.__ptr_);
3214 return __r;
3215}
3216
3217template<class _Tp>
3218template<class _A0, class _A1>
3219shared_ptr<_Tp>
3220shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3221{
3222 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3223 typedef allocator<_CntrlBlk> _Alloc2;
3224 typedef __allocator_destructor<_Alloc2> _D2;
3225 _Alloc2 __alloc2;
3226 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3227 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3228 shared_ptr<_Tp> __r;
3229 __r.__ptr_ = __hold2.get()->get();
3230 __r.__cntrl_ = __hold2.release();
3231 __r.__enable_weak_this(__r.__ptr_);
3232 return __r;
3233}
3234
3235template<class _Tp>
3236template<class _A0, class _A1, class _A2>
3237shared_ptr<_Tp>
3238shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3239{
3240 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3241 typedef allocator<_CntrlBlk> _Alloc2;
3242 typedef __allocator_destructor<_Alloc2> _D2;
3243 _Alloc2 __alloc2;
3244 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3245 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3246 shared_ptr<_Tp> __r;
3247 __r.__ptr_ = __hold2.get()->get();
3248 __r.__cntrl_ = __hold2.release();
3249 __r.__enable_weak_this(__r.__ptr_);
3250 return __r;
3251}
3252
3253template<class _Tp>
3254template<class _Alloc>
3255shared_ptr<_Tp>
3256shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3257{
3258 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3259 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3260 typedef __allocator_destructor<_Alloc2> _D2;
3261 _Alloc2 __alloc2(__a);
3262 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3263 ::new(__hold2.get()) _CntrlBlk(__a);
3264 shared_ptr<_Tp> __r;
3265 __r.__ptr_ = __hold2.get()->get();
3266 __r.__cntrl_ = __hold2.release();
3267 __r.__enable_weak_this(__r.__ptr_);
3268 return __r;
3269}
3270
3271template<class _Tp>
3272template<class _Alloc, class _A0>
3273shared_ptr<_Tp>
3274shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3275{
3276 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3277 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3278 typedef __allocator_destructor<_Alloc2> _D2;
3279 _Alloc2 __alloc2(__a);
3280 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3281 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3282 shared_ptr<_Tp> __r;
3283 __r.__ptr_ = __hold2.get()->get();
3284 __r.__cntrl_ = __hold2.release();
3285 __r.__enable_weak_this(__r.__ptr_);
3286 return __r;
3287}
3288
3289template<class _Tp>
3290template<class _Alloc, class _A0, class _A1>
3291shared_ptr<_Tp>
3292shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3293{
3294 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3295 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3296 typedef __allocator_destructor<_Alloc2> _D2;
3297 _Alloc2 __alloc2(__a);
3298 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3299 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3300 shared_ptr<_Tp> __r;
3301 __r.__ptr_ = __hold2.get()->get();
3302 __r.__cntrl_ = __hold2.release();
3303 __r.__enable_weak_this(__r.__ptr_);
3304 return __r;
3305}
3306
3307template<class _Tp>
3308template<class _Alloc, class _A0, class _A1, class _A2>
3309shared_ptr<_Tp>
3310shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3311{
3312 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3313 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3314 typedef __allocator_destructor<_Alloc2> _D2;
3315 _Alloc2 __alloc2(__a);
3316 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3317 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3318 shared_ptr<_Tp> __r;
3319 __r.__ptr_ = __hold2.get()->get();
3320 __r.__cntrl_ = __hold2.release();
3321 __r.__enable_weak_this(__r.__ptr_);
3322 return __r;
3323}
3324
3325#endif // _LIBCPP_HAS_NO_VARIADICS
3326
3327template<class _Tp>
3328shared_ptr<_Tp>::~shared_ptr()
3329{
3330 if (__cntrl_)
3331 __cntrl_->__release_shared();
3332}
3333
3334template<class _Tp>
3335inline _LIBCPP_INLINE_VISIBILITY
3336shared_ptr<_Tp>&
3337shared_ptr<_Tp>::operator=(const shared_ptr& __r)
3338{
3339 shared_ptr(__r).swap(*this);
3340 return *this;
3341}
3342
3343template<class _Tp>
3344template<class _Yp>
3345inline _LIBCPP_INLINE_VISIBILITY
3346shared_ptr<_Tp>&
3347shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r)
3348{
3349 shared_ptr(__r).swap(*this);
3350 return *this;
3351}
3352
3353#ifdef _LIBCPP_MOVE
3354
3355template<class _Tp>
3356inline _LIBCPP_INLINE_VISIBILITY
3357shared_ptr<_Tp>&
3358shared_ptr<_Tp>::operator=(shared_ptr&& __r)
3359{
3360 shared_ptr(_STD::move(__r)).swap(*this);
3361 return *this;
3362}
3363
3364template<class _Tp>
3365template<class _Yp>
3366inline _LIBCPP_INLINE_VISIBILITY
3367shared_ptr<_Tp>&
3368shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3369{
3370 shared_ptr(_STD::move(__r)).swap(*this);
3371 return *this;
3372}
3373
3374template<class _Tp>
3375template<class _Yp>
3376inline _LIBCPP_INLINE_VISIBILITY
3377shared_ptr<_Tp>&
3378shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3379{
3380 shared_ptr(__r).swap(*this);
3381 return *this;
3382}
3383
3384template<class _Tp>
3385template <class _Yp, class _Dp>
3386inline _LIBCPP_INLINE_VISIBILITY
3387shared_ptr<_Tp>&
3388shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3389{
3390 shared_ptr(_STD::move(__r)).swap(*this);
3391 return *this;
3392}
3393
3394#else
3395
3396template<class _Tp>
3397template<class _Yp>
3398inline _LIBCPP_INLINE_VISIBILITY
3399shared_ptr<_Tp>&
3400shared_ptr<_Tp>::operator=(auto_ptr<_Yp>& __r)
3401{
3402 shared_ptr(__r).swap(*this);
3403 return *this;
3404}
3405
3406template<class _Tp>
3407template <class _Yp, class _Dp>
3408inline _LIBCPP_INLINE_VISIBILITY
3409shared_ptr<_Tp>&
3410shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3411{
3412 shared_ptr(_STD::move(__r)).swap(*this);
3413 return *this;
3414}
3415
3416#endif
3417
3418template<class _Tp>
3419inline _LIBCPP_INLINE_VISIBILITY
3420void
3421shared_ptr<_Tp>::swap(shared_ptr& __r)
3422{
3423 _STD::swap(__ptr_, __r.__ptr_);
3424 _STD::swap(__cntrl_, __r.__cntrl_);
3425}
3426
3427template<class _Tp>
3428inline _LIBCPP_INLINE_VISIBILITY
3429void
3430shared_ptr<_Tp>::reset()
3431{
3432 shared_ptr().swap(*this);
3433}
3434
3435template<class _Tp>
3436template<class _Yp>
3437inline _LIBCPP_INLINE_VISIBILITY
3438void
3439shared_ptr<_Tp>::reset(_Yp* __p)
3440{
3441 shared_ptr(__p).swap(*this);
3442}
3443
3444template<class _Tp>
3445template<class _Yp, class _Dp>
3446inline _LIBCPP_INLINE_VISIBILITY
3447void
3448shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3449{
3450 shared_ptr(__p, __d).swap(*this);
3451}
3452
3453template<class _Tp>
3454template<class _Yp, class _Dp, class _Alloc>
3455inline _LIBCPP_INLINE_VISIBILITY
3456void
3457shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3458{
3459 shared_ptr(__p, __d, __a).swap(*this);
3460}
3461
3462#ifndef _LIBCPP_HAS_NO_VARIADICS
3463
3464template<class _Tp, class ..._Args>
3465inline _LIBCPP_INLINE_VISIBILITY
3466shared_ptr<_Tp>
3467make_shared(_Args&& ...__args)
3468{
3469 return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3470}
3471
3472template<class _Tp, class _Alloc, class ..._Args>
3473inline _LIBCPP_INLINE_VISIBILITY
3474shared_ptr<_Tp>
3475allocate_shared(const _Alloc& __a, _Args&& ...__args)
3476{
3477 return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3478}
3479
3480#else // _LIBCPP_HAS_NO_VARIADICS
3481
3482template<class _Tp>
3483inline _LIBCPP_INLINE_VISIBILITY
3484shared_ptr<_Tp>
3485make_shared()
3486{
3487 return shared_ptr<_Tp>::make_shared();
3488}
3489
3490template<class _Tp, class _A0>
3491inline _LIBCPP_INLINE_VISIBILITY
3492shared_ptr<_Tp>
3493make_shared(_A0& __a0)
3494{
3495 return shared_ptr<_Tp>::make_shared(__a0);
3496}
3497
3498template<class _Tp, class _A0, class _A1>
3499inline _LIBCPP_INLINE_VISIBILITY
3500shared_ptr<_Tp>
3501make_shared(_A0& __a0, _A1& __a1)
3502{
3503 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3504}
3505
3506template<class _Tp, class _A0, class _A1, class _A2>
3507inline _LIBCPP_INLINE_VISIBILITY
3508shared_ptr<_Tp>
3509make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3510{
3511 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3512}
3513
3514template<class _Tp, class _Alloc>
3515inline _LIBCPP_INLINE_VISIBILITY
3516shared_ptr<_Tp>
3517allocate_shared(const _Alloc& __a)
3518{
3519 return shared_ptr<_Tp>::allocate_shared(__a);
3520}
3521
3522template<class _Tp, class _Alloc, class _A0>
3523inline _LIBCPP_INLINE_VISIBILITY
3524shared_ptr<_Tp>
3525allocate_shared(const _Alloc& __a, _A0& __a0)
3526{
3527 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3528}
3529
3530template<class _Tp, class _Alloc, class _A0, class _A1>
3531inline _LIBCPP_INLINE_VISIBILITY
3532shared_ptr<_Tp>
3533allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3534{
3535 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3536}
3537
3538template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3539inline _LIBCPP_INLINE_VISIBILITY
3540shared_ptr<_Tp>
3541allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3542{
3543 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3544}
3545
3546#endif // _LIBCPP_HAS_NO_VARIADICS
3547
3548template<class _Tp, class _Up>
3549inline _LIBCPP_INLINE_VISIBILITY
3550bool
3551operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3552{
3553 return __x.get() == __y.get();
3554}
3555
3556template<class _Tp, class _Up>
3557inline _LIBCPP_INLINE_VISIBILITY
3558bool
3559operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3560{
3561 return !(__x == __y);
3562}
3563
3564template<class _Tp, class _Up>
3565inline _LIBCPP_INLINE_VISIBILITY
3566bool
3567operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3568{
3569 return __x.get() < __y.get();
3570}
3571
3572template<class _Tp>
3573inline _LIBCPP_INLINE_VISIBILITY
3574void
3575swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y)
3576{
3577 __x.swap(__y);
3578}
3579
3580template<class _Tp, class _Up>
3581inline _LIBCPP_INLINE_VISIBILITY
3582shared_ptr<_Tp>
3583static_pointer_cast(const shared_ptr<_Up>& __r)
3584{
3585 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3586}
3587
3588template<class _Tp, class _Up>
3589inline _LIBCPP_INLINE_VISIBILITY
3590shared_ptr<_Tp>
3591dynamic_pointer_cast(const shared_ptr<_Up>& __r)
3592{
3593 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3594 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3595}
3596
3597template<class _Tp, class _Up>
3598shared_ptr<_Tp>
3599const_pointer_cast(const shared_ptr<_Up>& __r)
3600{
3601 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3602}
3603
Howard Hinnantd4444702010-08-11 17:04:31 +00003604#ifndef _LIBCPP_NO_RTTI
3605
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003606template<class _Dp, class _Tp>
3607inline _LIBCPP_INLINE_VISIBILITY
3608_Dp*
3609get_deleter(const shared_ptr<_Tp>& __p)
3610{
3611 return __p.template __get_deleter<_Dp>();
3612}
3613
Howard Hinnantd4444702010-08-11 17:04:31 +00003614#endif
3615
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003616template<class _Tp>
3617class weak_ptr
3618{
3619public:
3620 typedef _Tp element_type;
3621private:
3622 element_type* __ptr_;
3623 __shared_weak_count* __cntrl_;
3624
3625public:
3626 weak_ptr();
3627 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
3628 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3629 weak_ptr(weak_ptr const& __r);
3630 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
3631 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3632
3633 ~weak_ptr();
3634
3635 weak_ptr& operator=(weak_ptr const& __r);
3636 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r);
3637 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r);
3638
3639 void swap(weak_ptr& __r);
3640 void reset();
3641
3642 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
3643 bool expired() const {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
3644 shared_ptr<_Tp> lock() const;
3645 template<class _Up> bool owner_before(const shared_ptr<_Up>& __r) const
3646 {return __cntrl_ < __r.__cntrl_;}
3647 template<class _Up> bool owner_before(const weak_ptr<_Up>& __r) const
3648 {return __cntrl_ < __r.__cntrl_;}
3649
3650 template <class _Up> friend class weak_ptr;
3651 template <class _Up> friend class shared_ptr;
3652};
3653
3654template<class _Tp>
3655inline _LIBCPP_INLINE_VISIBILITY
3656weak_ptr<_Tp>::weak_ptr()
3657 : __ptr_(0),
3658 __cntrl_(0)
3659{
3660}
3661
3662template<class _Tp>
3663inline _LIBCPP_INLINE_VISIBILITY
3664weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r)
3665 : __ptr_(__r.__ptr_),
3666 __cntrl_(__r.__cntrl_)
3667{
3668 if (__cntrl_)
3669 __cntrl_->__add_weak();
3670}
3671
3672template<class _Tp>
3673template<class _Yp>
3674inline _LIBCPP_INLINE_VISIBILITY
3675weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3676 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3677 : __ptr_(__r.__ptr_),
3678 __cntrl_(__r.__cntrl_)
3679{
3680 if (__cntrl_)
3681 __cntrl_->__add_weak();
3682}
3683
3684template<class _Tp>
3685template<class _Yp>
3686inline _LIBCPP_INLINE_VISIBILITY
3687weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3688 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3689 : __ptr_(__r.__ptr_),
3690 __cntrl_(__r.__cntrl_)
3691{
3692 if (__cntrl_)
3693 __cntrl_->__add_weak();
3694}
3695
3696template<class _Tp>
3697weak_ptr<_Tp>::~weak_ptr()
3698{
3699 if (__cntrl_)
3700 __cntrl_->__release_weak();
3701}
3702
3703template<class _Tp>
3704inline _LIBCPP_INLINE_VISIBILITY
3705weak_ptr<_Tp>&
3706weak_ptr<_Tp>::operator=(weak_ptr const& __r)
3707{
3708 weak_ptr(__r).swap(*this);
3709 return *this;
3710}
3711
3712template<class _Tp>
3713template<class _Yp>
3714inline _LIBCPP_INLINE_VISIBILITY
3715weak_ptr<_Tp>&
3716weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r)
3717{
3718 weak_ptr(__r).swap(*this);
3719 return *this;
3720}
3721
3722template<class _Tp>
3723template<class _Yp>
3724inline _LIBCPP_INLINE_VISIBILITY
3725weak_ptr<_Tp>&
3726weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r)
3727{
3728 weak_ptr(__r).swap(*this);
3729 return *this;
3730}
3731
3732template<class _Tp>
3733inline _LIBCPP_INLINE_VISIBILITY
3734void
3735weak_ptr<_Tp>::swap(weak_ptr& __r)
3736{
3737 _STD::swap(__ptr_, __r.__ptr_);
3738 _STD::swap(__cntrl_, __r.__cntrl_);
3739}
3740
3741template<class _Tp>
3742inline _LIBCPP_INLINE_VISIBILITY
3743void
3744swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y)
3745{
3746 __x.swap(__y);
3747}
3748
3749template<class _Tp>
3750inline _LIBCPP_INLINE_VISIBILITY
3751void
3752weak_ptr<_Tp>::reset()
3753{
3754 weak_ptr().swap(*this);
3755}
3756
3757template<class _Tp>
3758template<class _Yp>
3759shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3760 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3761 : __ptr_(__r.__ptr_),
3762 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3763{
3764 if (__cntrl_ == 0)
3765#ifndef _LIBCPP_NO_EXCEPTIONS
3766 throw bad_weak_ptr();
3767#else
3768 assert(!"bad_weak_ptr");
3769#endif
3770}
3771
3772template<class _Tp>
3773shared_ptr<_Tp>
3774weak_ptr<_Tp>::lock() const
3775{
3776 shared_ptr<_Tp> __r;
3777 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3778 if (__r.__cntrl_)
3779 __r.__ptr_ = __ptr_;
3780 return __r;
3781}
3782
3783template <class _Tp> struct owner_less;
3784
3785template <class _Tp>
3786struct owner_less<shared_ptr<_Tp> >
3787 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
3788{
3789 typedef bool result_type;
3790 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3791 {return __x.owner_before(__y);}
3792 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3793 {return __x.owner_before(__y);}
3794 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3795 {return __x.owner_before(__y);}
3796};
3797
3798template <class _Tp>
3799struct owner_less<weak_ptr<_Tp> >
3800 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3801{
3802 typedef bool result_type;
3803 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3804 {return __x.owner_before(__y);}
3805 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3806 {return __x.owner_before(__y);}
3807 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3808 {return __x.owner_before(__y);}
3809};
3810
3811template<class _Tp>
3812class enable_shared_from_this
3813{
3814 mutable weak_ptr<_Tp> __weak_this_;
3815protected:
3816 enable_shared_from_this() {}
3817 enable_shared_from_this(enable_shared_from_this const&) {}
3818 enable_shared_from_this& operator=(enable_shared_from_this const&) {return *this;}
3819 ~enable_shared_from_this() {}
3820public:
3821 shared_ptr<_Tp> shared_from_this() {return shared_ptr<_Tp>(__weak_this_);}
3822 shared_ptr<_Tp const> shared_from_this() const {return shared_ptr<const _Tp>(__weak_this_);}
3823
3824 template <class _Up> friend class shared_ptr;
3825};
3826
Howard Hinnant21aefc32010-06-03 16:42:57 +00003827template <class _Tp>
3828struct hash<shared_ptr<_Tp> >
3829{
3830 typedef shared_ptr<_Tp> argument_type;
3831 typedef size_t result_type;
3832 result_type operator()(const argument_type& __ptr) const
3833 {
3834 return hash<_Tp*>()(__ptr.get());
3835 }
3836};
3837
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838//enum class
3839struct pointer_safety
3840{
3841 enum _
3842 {
3843 relaxed,
3844 preferred,
3845 strict
3846 };
3847
3848 _ __v_;
3849
3850 pointer_safety(_ __v) : __v_(__v) {}
3851 operator int() const {return __v_;}
3852};
3853
3854void declare_reachable(void* __p);
3855void declare_no_pointers(char* __p, size_t __n);
3856void undeclare_no_pointers(char* __p, size_t __n);
3857pointer_safety get_pointer_safety();
3858void* __undeclare_reachable(void*);
3859
3860template <class _Tp>
3861inline _LIBCPP_INLINE_VISIBILITY
3862_Tp*
3863undeclare_reachable(_Tp* __p)
3864{
3865 return static_cast<_Tp*>(__undeclare_reachable(__p));
3866}
3867
3868void* align(size_t, size_t, void*&, size_t&);
3869
3870_LIBCPP_END_NAMESPACE_STD
3871
3872#endif // _LIBCPP_MEMORY