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