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