blob: 130613e2be4ea1bffe06a0e3ae7de74beb7a304a [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//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
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;
Howard Hinnant324bb032010-08-22 00:02:43 +000031
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant324bb032010-08-22 00:02:43 +000033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 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
Howard Hinnant47761072010-11-18 01:40:00 +000055 | pointer_traits<pointer>::difference_type
56 difference_type;
57 typedef Alloc::size_type
58 | make_unsigned<difference_type>::type
59 size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 typedef Alloc::propagate_on_container_copy_assignment
61 | false_type propagate_on_container_copy_assignment;
62 typedef Alloc::propagate_on_container_move_assignment
63 | false_type propagate_on_container_move_assignment;
64 typedef Alloc::propagate_on_container_swap
65 | false_type propagate_on_container_swap;
66
67 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
68 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
69
70 static pointer allocate(allocator_type& a, size_type n);
71 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
72
73 static void deallocate(allocator_type& a, pointer p, size_type n);
74
75 template <class T, class... Args>
76 static void construct(allocator_type& a, T* p, Args&&... args);
77
78 template <class T>
79 static void destroy(allocator_type& a, T* p);
80
81 static size_type max_size(const allocator_type& a);
82
83 static allocator_type
84 select_on_container_copy_construction(const allocator_type& a);
85};
86
87template <>
88class allocator<void>
89{
90public:
91 typedef void* pointer;
92 typedef const void* const_pointer;
93 typedef void value_type;
94
95 template <class _Up> struct rebind {typedef allocator<_Up> other;};
96};
97
98template <class T>
99class allocator
100{
101public:
102 typedef size_t size_type;
103 typedef ptrdiff_t difference_type;
104 typedef T* pointer;
105 typedef const T* const_pointer;
106 typedef typename add_lvalue_reference<T>::type reference;
107 typedef typename add_lvalue_reference<const T>::type const_reference;
108 typedef T value_type;
109
110 template <class U> struct rebind {typedef allocator<U> other;};
111
112 allocator() throw();
113 allocator(const allocator&) throw();
114 template <class U> allocator(const allocator<U>&) throw();
115 ~allocator() throw();
116 pointer address(reference x) const;
117 const_pointer address(const_reference x) const;
118 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
119 void deallocate(pointer p, size_type n);
120 size_type max_size() const throw();
121 void construct(pointer p, const T& val);
122 void destroy(pointer p);
123};
124
125template <class T, class U>
126bool operator==(const allocator<T>&, const allocator<U>&) throw();
127
128template <class T, class U>
129bool operator!=(const allocator<T>&, const allocator<U>&) throw();
130
131template <class OutputIterator, class T>
132class raw_storage_iterator
133 : public iterator<output_iterator_tag,
134 T, // purposefully not C++03
135 ptrdiff_t, // purposefully not C++03
136 T*, // purposefully not C++03
137 raw_storage_iterator&> // purposefully not C++03
138{
139public:
140 explicit raw_storage_iterator(OutputIterator x);
141 raw_storage_iterator& operator*();
142 raw_storage_iterator& operator=(const T& element);
143 raw_storage_iterator& operator++();
144 raw_storage_iterator operator++(int);
145};
146
147template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n);
148template <class T> void return_temporary_buffer(T* p);
149
150template <class InputIterator, class ForwardIterator>
151ForwardIterator
152uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
153
154template <class ForwardIterator, class T>
155void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
156
157template <class ForwardIterator, class Size, class T>
158void uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
159
160template <class Y> struct auto_ptr_ref {};
161
162template<class X>
163class auto_ptr
164{
165public:
166 typedef X element_type;
167
168 explicit auto_ptr(X* p =0) throw();
169 auto_ptr(auto_ptr&) throw();
170 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
171 auto_ptr& operator=(auto_ptr&) throw();
172 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
173 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
174 ~auto_ptr() throw();
175
176 typename add_lvalue_reference<X>::type operator*() const throw();
177 X* operator->() const throw();
178 X* get() const throw();
179 X* release() throw();
180 void reset(X* p =0) throw();
181
182 auto_ptr(auto_ptr_ref<X>) throw();
183 template<class Y> operator auto_ptr_ref<Y>() throw();
184 template<class Y> operator auto_ptr<Y>() throw();
185};
186
Howard Hinnante92c3d72010-08-19 18:39:17 +0000187template <class T>
188struct default_delete
189{
190 constexpr default_delete();
191 template <class U> default_delete(const default_delete<U>&);
192
193 void operator()(T*) const;
194};
195
196template <class T>
197struct default_delete<T[]>
198{
199 constexpr default_delete();
200 void operator()(T*) const;
201 template <class U> void operator()(U*) const = delete;
202};
203
204template <class T, class D = default_delete<T>> class unique_ptr;
205
206template <class T, class D = default_delete<T>>
207class unique_ptr
208{
209public:
210 typedef see below pointer;
211 typedef T element_type;
212 typedef D deleter_type;
213
214 // constructors
215 constexpr unique_ptr();
216 explicit unique_ptr(pointer p);
217 unique_ptr(pointer p, implementation-defined d1);
218 unique_ptr(pointer p, implementation-defined d2);
219 unique_ptr(unique_ptr&& u);
220 unique_ptr(nullptr_t) : unique_ptr() { }
221 template <class U, class E>
222 unique_ptr(unique_ptr<U, E>&& u);
223 template <class U>
Howard Hinnante92c3d72010-08-19 18:39:17 +0000224 unique_ptr(auto_ptr<U>&& u);
225
226 // destructor
227 ~unique_ptr();
228
229 // assignment
230 unique_ptr& operator=(unique_ptr&& u);
231 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u);
232 unique_ptr& operator=(nullptr_t);
233
234 // observers
235 typename add_lvalue_reference<T>::type operator*() const;
236 pointer operator->() const;
237 pointer get() const;
238 deleter_type& get_deleter();
239 const deleter_type& get_deleter() const;
240 explicit operator bool() const;
241
242 // modifiers
243 pointer release();
244 void reset(pointer p = pointer());
245 void swap(unique_ptr& u);
246};
247
248template <class T, class D>
249class unique_ptr<T[], D>
250{
251public:
252 typedef implementation-defined pointer;
253 typedef T element_type;
254 typedef D deleter_type;
255
256 // constructors
257 constexpr unique_ptr();
258 explicit unique_ptr(pointer p);
259 unique_ptr(pointer p, implementation-defined d);
260 unique_ptr(pointer p, implementation-defined d);
261 unique_ptr(unique_ptr&& u);
262 unique_ptr(nullptr_t) : unique_ptr() { }
263
264 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000265 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000266
267 // assignment
268 unique_ptr& operator=(unique_ptr&& u);
269 unique_ptr& operator=(nullptr_t);
270
271 // observers
272 T& operator[](size_t i) const;
273 pointer get() const;
274 deleter_type& get_deleter();
275 const deleter_type& get_deleter() const;
276 explicit operator bool() const;
277
278 // modifiers
279 pointer release();
280 void reset(pointer p = pointer());
281 void reset(nullptr_t);
282 template <class U> void reset(U) = delete;
283 void swap(unique_ptr& u);
284};
285
286template <class T, class D>
287 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y);
288
289template <class T1, class D1, class T2, class D2>
290 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
291template <class T1, class D1, class T2, class D2>
292 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
293template <class T1, class D1, class T2, class D2>
294 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
295template <class T1, class D1, class T2, class D2>
296 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
297template <class T1, class D1, class T2, class D2>
298 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
299template <class T1, class D1, class T2, class D2>
300 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
301
302class bad_weak_ptr
303 : public std::exception
304{
305 bad_weak_ptr();
306};
307
308template<class T>
309class shared_ptr
310{
311public:
312 typedef T element_type;
313
314 // constructors:
315 constexpr shared_ptr();
316 template<class Y> explicit shared_ptr(Y* p);
317 template<class Y, class D> shared_ptr(Y* p, D d);
318 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
319 template <class D> shared_ptr(nullptr_t p, D d);
320 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
321 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p);
322 shared_ptr(const shared_ptr& r);
323 template<class Y> shared_ptr(const shared_ptr<Y>& r);
324 shared_ptr(shared_ptr&& r);
325 template<class Y> shared_ptr(shared_ptr<Y>&& r);
326 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
327 template<class Y> shared_ptr(auto_ptr<Y>&& r);
328 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
329 shared_ptr(nullptr_t) : shared_ptr() { }
330
331 // destructor:
332 ~shared_ptr();
333
334 // assignment:
335 shared_ptr& operator=(const shared_ptr& r);
336 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r);
337 shared_ptr& operator=(shared_ptr&& r);
338 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
339 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
340 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
341
342 // modifiers:
343 void swap(shared_ptr& r);
344 void reset();
345 template<class Y> void reset(Y* p);
346 template<class Y, class D> void reset(Y* p, D d);
347 template<class Y, class D, class A> void reset(Y* p, D d, A a);
348
349 // observers: T* get() const;
350 T& operator*() const;
351 T* operator->() const;
352 long use_count() const;
353 bool unique() const;
354 explicit operator bool() const;
355 template<class U> bool owner_before(shared_ptr<U> const& b) const;
356 template<class U> bool owner_before(weak_ptr<U> const& b) const;
357};
358
359// shared_ptr comparisons:
360template<class T, class U>
361 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b);
362template<class T, class U>
363 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b);
364template<class T, class U>
365 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b);
366template<class T, class U>
367 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b);
368template<class T, class U>
369 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b);
370template<class T, class U>
371 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b);
372
373// shared_ptr specialized algorithms:
374template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b);
375
376// shared_ptr casts:
377template<class T, class U>
378 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r);
379template<class T, class U>
380 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r);
381template<class T, class U>
382 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r);
383
384// shared_ptr I/O:
385template<class E, class T, class Y>
386 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
387
388// shared_ptr get_deleter:
389template<class D, class T> D* get_deleter(shared_ptr<T> const& p);
390
391template<class T, class... Args>
392 shared_ptr<T> make_shared(Args&&... args);
393template<class T, class A, class... Args>
394 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
395
396template<class T>
397class weak_ptr
398{
399public:
400 typedef T element_type;
401
402 // constructors
403 constexpr weak_ptr();
404 template<class Y> weak_ptr(shared_ptr<Y> const& r);
405 weak_ptr(weak_ptr const& r);
406 template<class Y> weak_ptr(weak_ptr<Y> const& r);
407
408 // destructor
409 ~weak_ptr();
410
411 // assignment
412 weak_ptr& operator=(weak_ptr const& r);
413 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r);
414 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r);
415
416 // modifiers
417 void swap(weak_ptr& r);
418 void reset();
419
420 // observers
421 long use_count() const;
422 bool expired() const;
423 shared_ptr<T> lock() const;
424 template<class U> bool owner_before(shared_ptr<U> const& b);
425 template<class U> bool owner_before(weak_ptr<U> const& b);
426};
427
428// weak_ptr specialized algorithms:
429template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b);
430
431// class owner_less:
432template<class T> struct owner_less;
433
434template<class T>
435struct owner_less<shared_ptr<T>>
436 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
437{
438 typedef bool result_type;
439 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
440 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
441 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
442};
443
444template<class T>
445struct owner_less<weak_ptr<T>>
446 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
447{
448 typedef bool result_type;
449 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
450 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
451 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
452};
453
454template<class T>
455class enable_shared_from_this
456{
457protected:
458 constexpr enable_shared_from_this();
459 enable_shared_from_this(enable_shared_from_this const&);
460 enable_shared_from_this& operator=(enable_shared_from_this const&);
461 ~enable_shared_from_this();
462public:
463 shared_ptr<T> shared_from_this();
464 shared_ptr<T const> shared_from_this() const;
465};
466
467template<class T>
468 bool atomic_is_lock_free(const shared_ptr<T>* p);
469template<class T>
470 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
471template<class T>
472 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
473template<class T>
474 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
475template<class T>
476 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
477template<class T>
478 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
479template<class T>
480 shared_ptr<T>
481 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
482template<class T>
483 bool
484 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
485template<class T>
486 bool
487 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
488template<class T>
489 bool
490 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
491 shared_ptr<T> w, memory_order success,
492 memory_order failure);
493template<class T>
494 bool
495 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
496 shared_ptr<T> w, memory_order success,
497 memory_order failure);
498// Hash support
499template <class T> struct hash;
500template <class T, class D> struct hash<unique_ptr<T, D> >;
501template <class T> struct hash<shared_ptr<T> >;
502
503// Pointer safety
504enum class pointer_safety { relaxed, preferred, strict };
505void declare_reachable(void *p);
506template <class T> T *undeclare_reachable(T *p);
507void declare_no_pointers(char *p, size_t n);
508void undeclare_no_pointers(char *p, size_t n);
509pointer_safety get_pointer_safety();
510
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
512
513} // std
514
515*/
516
517#include <__config>
518#include <type_traits>
519#include <typeinfo>
520#include <cstddef>
521#include <cstdint>
522#include <new>
523#include <utility>
524#include <limits>
525#include <iterator>
526#include <__functional_base>
527#if defined(_LIBCPP_NO_EXCEPTIONS)
528 #include <cassert>
529#endif
530
531#pragma GCC system_header
532
533_LIBCPP_BEGIN_NAMESPACE_STD
534
535// allocator_arg_t
536
Howard Hinnant82894812010-09-22 16:48:34 +0000537struct _LIBCPP_VISIBLE allocator_arg_t { };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000538
539extern const allocator_arg_t allocator_arg;
540
541// addressof
542
543template <class _Tp>
544inline _LIBCPP_INLINE_VISIBILITY
545_Tp*
546addressof(_Tp& __x)
547{
548 return (_Tp*)&(char&)__x;
549}
550
551template <class _Tp> class allocator;
552
553template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000554class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555{
556public:
557 typedef void* pointer;
558 typedef const void* const_pointer;
559 typedef void value_type;
560
561 template <class _Up> struct rebind {typedef allocator<_Up> other;};
562};
563
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564// pointer_traits
565
566template <class _Tp>
567struct __has_element_type
568{
569private:
570 struct __two {char _; char __;};
571 template <class _Up> static __two __test(...);
572 template <class _Up> static char __test(typename _Up::element_type* = 0);
573public:
574 static const bool value = sizeof(__test<_Tp>(0)) == 1;
575};
576
577template <class _Ptr, bool = __has_element_type<_Ptr>::value>
578struct __pointer_traits_element_type;
579
580template <class _Ptr>
581struct __pointer_traits_element_type<_Ptr, true>
582{
583 typedef typename _Ptr::element_type type;
584};
585
586#ifndef _LIBCPP_HAS_NO_VARIADICS
587
588template <template <class, class...> class _Sp, class _Tp, class ..._Args>
589struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
590{
591 typedef typename _Sp<_Tp, _Args...>::element_type type;
592};
593
594template <template <class, class...> class _Sp, class _Tp, class ..._Args>
595struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
596{
597 typedef _Tp type;
598};
599
Howard Hinnant324bb032010-08-22 00:02:43 +0000600#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601
602template <template <class> class _Sp, class _Tp>
603struct __pointer_traits_element_type<_Sp<_Tp>, true>
604{
605 typedef typename _Sp<_Tp>::element_type type;
606};
607
608template <template <class> class _Sp, class _Tp>
609struct __pointer_traits_element_type<_Sp<_Tp>, false>
610{
611 typedef _Tp type;
612};
613
614template <template <class, class> class _Sp, class _Tp, class _A0>
615struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
616{
617 typedef typename _Sp<_Tp, _A0>::element_type type;
618};
619
620template <template <class, class> class _Sp, class _Tp, class _A0>
621struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
622{
623 typedef _Tp type;
624};
625
626template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
627struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
628{
629 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
630};
631
632template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
633struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
634{
635 typedef _Tp type;
636};
637
638template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
639 class _A1, class _A2>
640struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
641{
642 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
643};
644
645template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
646 class _A1, class _A2>
647struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
648{
649 typedef _Tp type;
650};
651
Howard Hinnant324bb032010-08-22 00:02:43 +0000652#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653
654template <class _Tp>
655struct __has_difference_type
656{
657private:
658 struct __two {char _; char __;};
659 template <class _Up> static __two __test(...);
660 template <class _Up> static char __test(typename _Up::difference_type* = 0);
661public:
662 static const bool value = sizeof(__test<_Tp>(0)) == 1;
663};
664
665template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
666struct __pointer_traits_difference_type
667{
668 typedef ptrdiff_t type;
669};
670
671template <class _Ptr>
672struct __pointer_traits_difference_type<_Ptr, true>
673{
674 typedef typename _Ptr::difference_type type;
675};
676
677template <class _Tp, class _Up>
678struct __has_rebind
679{
680private:
681 struct __two {char _; char __;};
682 template <class _Xp> static __two __test(...);
683 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
684public:
685 static const bool value = sizeof(__test<_Tp>(0)) == 1;
686};
687
688template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
689struct __pointer_traits_rebind
690{
691#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
692 typedef typename _Tp::template rebind<_Up> type;
693#else
694 typedef typename _Tp::template rebind<_Up>::other type;
695#endif
696};
697
698#ifndef _LIBCPP_HAS_NO_VARIADICS
699
700template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
701struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
702{
703#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
704 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
705#else
706 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
707#endif
708};
709
710template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
711struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
712{
713 typedef _Sp<_Up, _Args...> type;
714};
715
Howard Hinnant324bb032010-08-22 00:02:43 +0000716#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717
718template <template <class> class _Sp, class _Tp, class _Up>
719struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
720{
721#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
722 typedef typename _Sp<_Tp>::template rebind<_Up> type;
723#else
724 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
725#endif
726};
727
728template <template <class> class _Sp, class _Tp, class _Up>
729struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
730{
731 typedef _Sp<_Up> type;
732};
733
734template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
735struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
736{
737#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
738 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
739#else
740 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
741#endif
742};
743
744template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
745struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
746{
747 typedef _Sp<_Up, _A0> type;
748};
749
750template <template <class, class, class> class _Sp, class _Tp, class _A0,
751 class _A1, class _Up>
752struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
753{
754#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
755 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
756#else
757 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
758#endif
759};
760
761template <template <class, class, class> class _Sp, class _Tp, class _A0,
762 class _A1, class _Up>
763struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
764{
765 typedef _Sp<_Up, _A0, _A1> type;
766};
767
768template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
769 class _A1, class _A2, class _Up>
770struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
771{
772#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
773 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
774#else
775 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
776#endif
777};
778
779template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
780 class _A1, class _A2, class _Up>
781struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
782{
783 typedef _Sp<_Up, _A0, _A1, _A2> type;
784};
785
Howard Hinnant324bb032010-08-22 00:02:43 +0000786#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787
788template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000789struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790{
791 typedef _Ptr pointer;
792 typedef typename __pointer_traits_element_type<pointer>::type element_type;
793 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
794
795#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
796 template <class _Up> using rebind = __pointer_traits_rebind<pointer, _Up>::type;
797#else
798 template <class _Up> struct rebind
799 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000800#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000801
802private:
803 struct __nat {};
804public:
Howard Hinnant82894812010-09-22 16:48:34 +0000805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000806 static pointer pointer_to(typename conditional<is_void<element_type>::value,
807 __nat, element_type>::type& __r)
808 {return pointer::pointer_to(__r);}
809};
810
811template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000812struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000813{
814 typedef _Tp* pointer;
815 typedef _Tp element_type;
816 typedef ptrdiff_t difference_type;
817
818#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
819 template <class _Up> using rebind = _Up*;
820#else
821 template <class _Up> struct rebind {typedef _Up* other;};
822#endif
823
824private:
825 struct __nat {};
826public:
Howard Hinnant82894812010-09-22 16:48:34 +0000827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828 static pointer pointer_to(typename conditional<is_void<element_type>::value,
829 __nat, element_type>::type& __r)
830 {return _STD::addressof(__r);}
831};
832
833// allocator_traits
834
835namespace __has_pointer_type_imp
836{
837 template <class _Up> static __two test(...);
838 template <class _Up> static char test(typename _Up::pointer* = 0);
839}
840
841template <class _Tp>
842struct __has_pointer_type
843 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
844{
845};
846
847namespace __pointer_type_imp
848{
849
850template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
851struct __pointer_type
852{
853 typedef typename _Dp::pointer type;
854};
855
856template <class _Tp, class _Dp>
857struct __pointer_type<_Tp, _Dp, false>
858{
859 typedef _Tp* type;
860};
861
Howard Hinnant47761072010-11-18 01:40:00 +0000862} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000863
864template <class _Tp, class _Dp>
865struct __pointer_type
866{
867 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
868};
869
870template <class _Tp>
871struct __has_const_pointer
872{
873private:
874 struct __two {char _; char __;};
875 template <class _Up> static __two __test(...);
876 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
877public:
878 static const bool value = sizeof(__test<_Tp>(0)) == 1;
879};
880
881template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
882struct __const_pointer
883{
884 typedef typename _Alloc::const_pointer type;
885};
886
887template <class _Tp, class _Ptr, class _Alloc>
888struct __const_pointer<_Tp, _Ptr, _Alloc, false>
889{
890#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
891 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
892#else
893 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
894#endif
895};
896
897template <class _Tp>
898struct __has_void_pointer
899{
900private:
901 struct __two {char _; char __;};
902 template <class _Up> static __two __test(...);
903 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
904public:
905 static const bool value = sizeof(__test<_Tp>(0)) == 1;
906};
907
908template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
909struct __void_pointer
910{
911 typedef typename _Alloc::void_pointer type;
912};
913
914template <class _Ptr, class _Alloc>
915struct __void_pointer<_Ptr, _Alloc, false>
916{
917#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
918 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
919#else
920 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
921#endif
922};
923
924template <class _Tp>
925struct __has_const_void_pointer
926{
927private:
928 struct __two {char _; char __;};
929 template <class _Up> static __two __test(...);
930 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
931public:
932 static const bool value = sizeof(__test<_Tp>(0)) == 1;
933};
934
935template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
936struct __const_void_pointer
937{
938 typedef typename _Alloc::const_void_pointer type;
939};
940
941template <class _Ptr, class _Alloc>
942struct __const_void_pointer<_Ptr, _Alloc, false>
943{
944#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
945 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
946#else
947 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
948#endif
949};
950
951template <class _T>
952inline _LIBCPP_INLINE_VISIBILITY
953_T*
954__to_raw_pointer(_T* __p)
955{
956 return __p;
957}
958
959template <class _Pointer>
960inline _LIBCPP_INLINE_VISIBILITY
961typename pointer_traits<_Pointer>::element_type*
962__to_raw_pointer(_Pointer __p)
963{
964 return _STD::__to_raw_pointer(__p.operator->());
965}
966
967template <class _Tp>
968struct __has_size_type
969{
970private:
971 struct __two {char _; char __;};
972 template <class _Up> static __two __test(...);
973 template <class _Up> static char __test(typename _Up::size_type* = 0);
974public:
975 static const bool value = sizeof(__test<_Tp>(0)) == 1;
976};
977
Howard Hinnant47761072010-11-18 01:40:00 +0000978template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979struct __size_type
980{
Howard Hinnant47761072010-11-18 01:40:00 +0000981 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982};
983
Howard Hinnant47761072010-11-18 01:40:00 +0000984template <class _Alloc, class _DiffType>
985struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986{
987 typedef typename _Alloc::size_type type;
988};
989
990template <class _Tp>
991struct __has_propagate_on_container_copy_assignment
992{
993private:
994 struct __two {char _; char __;};
995 template <class _Up> static __two __test(...);
996 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
997public:
998 static const bool value = sizeof(__test<_Tp>(0)) == 1;
999};
1000
1001template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1002struct __propagate_on_container_copy_assignment
1003{
1004 typedef false_type type;
1005};
1006
1007template <class _Alloc>
1008struct __propagate_on_container_copy_assignment<_Alloc, true>
1009{
1010 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1011};
1012
1013template <class _Tp>
1014struct __has_propagate_on_container_move_assignment
1015{
1016private:
1017 struct __two {char _; char __;};
1018 template <class _Up> static __two __test(...);
1019 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1020public:
1021 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1022};
1023
1024template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1025struct __propagate_on_container_move_assignment
1026{
1027 typedef false_type type;
1028};
1029
1030template <class _Alloc>
1031struct __propagate_on_container_move_assignment<_Alloc, true>
1032{
1033 typedef typename _Alloc::propagate_on_container_move_assignment type;
1034};
1035
1036template <class _Tp>
1037struct __has_propagate_on_container_swap
1038{
1039private:
1040 struct __two {char _; char __;};
1041 template <class _Up> static __two __test(...);
1042 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1043public:
1044 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1045};
1046
1047template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1048struct __propagate_on_container_swap
1049{
1050 typedef false_type type;
1051};
1052
1053template <class _Alloc>
1054struct __propagate_on_container_swap<_Alloc, true>
1055{
1056 typedef typename _Alloc::propagate_on_container_swap type;
1057};
1058
1059template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1060struct __has_rebind_other
1061{
1062private:
1063 struct __two {char _; char __;};
1064 template <class _Xp> static __two __test(...);
1065 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1066public:
1067 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1068};
1069
1070template <class _Tp, class _Up>
1071struct __has_rebind_other<_Tp, _Up, false>
1072{
1073 static const bool value = false;
1074};
1075
1076template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1077struct __allocator_traits_rebind
1078{
1079 typedef typename _Tp::template rebind<_Up>::other type;
1080};
1081
1082#ifndef _LIBCPP_HAS_NO_VARIADICS
1083
1084template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1085struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1086{
1087 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1088};
1089
1090template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1091struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1092{
1093 typedef _Alloc<_Up, _Args...> type;
1094};
1095
Howard Hinnant324bb032010-08-22 00:02:43 +00001096#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097
1098template <template <class> class _Alloc, class _Tp, class _Up>
1099struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1100{
1101 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1102};
1103
1104template <template <class> class _Alloc, class _Tp, class _Up>
1105struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1106{
1107 typedef _Alloc<_Up> type;
1108};
1109
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1111struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1112{
1113 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1114};
1115
1116template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1117struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1118{
1119 typedef _Alloc<_Up, _A0> type;
1120};
1121
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1123 class _A1, class _Up>
1124struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1125{
1126 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1127};
1128
1129template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1130 class _A1, class _Up>
1131struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1132{
1133 typedef _Alloc<_Up, _A0, _A1> type;
1134};
1135
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1137 class _A1, class _A2, class _Up>
1138struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1139{
1140 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1141};
1142
1143template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1144 class _A1, class _A2, class _Up>
1145struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1146{
1147 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1148};
1149
Howard Hinnant324bb032010-08-22 00:02:43 +00001150#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151
1152#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1153
1154template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1155auto
1156__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1157 -> decltype(__a.allocate(__sz, __p), true_type());
1158
1159template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1160auto
1161__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1162 -> false_type;
1163
1164template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1165struct __has_allocate_hint
1166 : integral_constant<bool,
1167 is_same<
1168 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1169 declval<_SizeType>(),
1170 declval<_ConstVoidPtr>())),
1171 true_type>::value>
1172{
1173};
1174
Howard Hinnant324bb032010-08-22 00:02:43 +00001175#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176
1177template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1178struct __has_allocate_hint
1179 : true_type
1180{
1181};
1182
Howard Hinnant324bb032010-08-22 00:02:43 +00001183#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001184
1185#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1186
1187template <class _Alloc, class _Tp, class ..._Args>
1188decltype(_STD::declval<_Alloc>().construct(_STD::declval<_Tp*>(),
1189 _STD::declval<_Args>()...),
1190 true_type())
1191__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1192
1193template <class _Alloc, class _Pointer, class ..._Args>
1194false_type
1195__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1196
1197template <class _Alloc, class _Pointer, class ..._Args>
1198struct __has_construct
1199 : integral_constant<bool,
1200 is_same<
1201 decltype(__has_construct_test(declval<_Alloc>(),
1202 declval<_Pointer>(),
1203 declval<_Args>()...)),
1204 true_type>::value>
1205{
1206};
1207
1208template <class _Alloc, class _Pointer>
1209auto
1210__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1211 -> decltype(__a.destroy(__p), true_type());
1212
1213template <class _Alloc, class _Pointer>
1214auto
1215__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1216 -> false_type;
1217
1218template <class _Alloc, class _Pointer>
1219struct __has_destroy
1220 : integral_constant<bool,
1221 is_same<
1222 decltype(__has_destroy_test(declval<_Alloc>(),
1223 declval<_Pointer>())),
1224 true_type>::value>
1225{
1226};
1227
1228template <class _Alloc>
1229auto
1230__has_max_size_test(_Alloc&& __a)
1231 -> decltype(__a.max_size(), true_type());
1232
1233template <class _Alloc>
1234auto
1235__has_max_size_test(const volatile _Alloc& __a)
1236 -> false_type;
1237
1238template <class _Alloc>
1239struct __has_max_size
1240 : integral_constant<bool,
1241 is_same<
1242 decltype(__has_max_size_test(declval<_Alloc&>())),
1243 true_type>::value>
1244{
1245};
1246
1247template <class _Alloc>
1248auto
1249__has_select_on_container_copy_construction_test(_Alloc&& __a)
1250 -> decltype(__a.select_on_container_copy_construction(), true_type());
1251
1252template <class _Alloc>
1253auto
1254__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1255 -> false_type;
1256
1257template <class _Alloc>
1258struct __has_select_on_container_copy_construction
1259 : integral_constant<bool,
1260 is_same<
1261 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1262 true_type>::value>
1263{
1264};
1265
Howard Hinnant324bb032010-08-22 00:02:43 +00001266#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267
1268#ifndef _LIBCPP_HAS_NO_VARIADICS
1269
1270template <class _Alloc, class _Pointer, class ..._Args>
1271struct __has_construct
1272 : false_type
1273{
1274};
1275
Howard Hinnant324bb032010-08-22 00:02:43 +00001276#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277
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
Howard Hinnant324bb032010-08-22 00:02:43 +00001296#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297
Howard Hinnant47761072010-11-18 01:40:00 +00001298template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1299struct __alloc_traits_difference_type
1300{
1301 typedef typename pointer_traits<_Ptr>::difference_type type;
1302};
1303
1304template <class _Alloc, class _Ptr>
1305struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1306{
1307 typedef typename _Alloc::difference_type type;
1308};
1309
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001311struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312{
1313 typedef _Alloc allocator_type;
1314 typedef typename allocator_type::value_type value_type;
1315
1316 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1317 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1318 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1319 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1320
Howard Hinnant47761072010-11-18 01:40:00 +00001321 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1322 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323
1324 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1325 propagate_on_container_copy_assignment;
1326 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1327 propagate_on_container_move_assignment;
1328 typedef typename __propagate_on_container_swap<allocator_type>::type
1329 propagate_on_container_swap;
1330
1331#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1332 template <class _Tp> using rebind_alloc =
1333 __allocator_traits_rebind<allocator_type, _Tp>::type;
1334 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001335#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336 template <class _Tp> struct rebind_alloc
1337 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1338 template <class _Tp> struct rebind_traits
1339 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001340#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341
Howard Hinnant82894812010-09-22 16:48:34 +00001342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343 static pointer allocate(allocator_type& __a, size_type __n)
1344 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1347 {return allocate(__a, __n, __hint,
1348 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1349
Howard Hinnant82894812010-09-22 16:48:34 +00001350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001351 static void deallocate(allocator_type& __a, pointer __p, size_type __n)
1352 {__a.deallocate(__p, __n);}
1353
1354#ifndef _LIBCPP_HAS_NO_VARIADICS
1355 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1358 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
1359 __a, __p, _STD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001360#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363 static void construct(allocator_type& __a, _Tp* __p)
1364 {
1365 ::new ((void*)__p) _Tp();
1366 }
1367 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1370 {
1371 ::new ((void*)__p) _Tp(__a0);
1372 }
1373 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1376 const _A1& __a1)
1377 {
1378 ::new ((void*)__p) _Tp(__a0, __a1);
1379 }
1380 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001382 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1383 const _A1& __a1, const _A2& __a2)
1384 {
1385 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1386 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001387#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388
1389 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391 static void destroy(allocator_type& __a, _Tp* __p)
1392 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1393
Howard Hinnant82894812010-09-22 16:48:34 +00001394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395 static size_type max_size(const allocator_type& __a)
1396 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1397
Howard Hinnant82894812010-09-22 16:48:34 +00001398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399 static allocator_type
1400 select_on_container_copy_construction(const allocator_type& __a)
1401 {return select_on_container_copy_construction(
1402 __has_select_on_container_copy_construction<const allocator_type>(),
1403 __a);}
1404
1405private:
1406
Howard Hinnant82894812010-09-22 16:48:34 +00001407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408 static pointer allocate(allocator_type& __a, size_type __n,
1409 const_void_pointer __hint, true_type)
1410 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412 static pointer allocate(allocator_type& __a, size_type __n,
1413 const_void_pointer __hint, false_type)
1414 {return __a.allocate(__n);}
1415
1416#ifndef _LIBCPP_HAS_NO_VARIADICS
1417 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1420 {__a.construct(__p, _STD::forward<_Args>(__args)...);}
1421 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1424 {
1425 ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
1426 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001427#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428
1429 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1432 {__a.destroy(__p);}
1433 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435 static void __destroy(false_type, allocator_type&, _Tp* __p)
1436 {
1437 __p->~_Tp();
1438 }
1439
Howard Hinnant82894812010-09-22 16:48:34 +00001440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 static size_type __max_size(true_type, const allocator_type& __a)
1442 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444 static size_type __max_size(false_type, const allocator_type&)
1445 {return numeric_limits<size_type>::max();}
1446
Howard Hinnant82894812010-09-22 16:48:34 +00001447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448 static allocator_type
1449 select_on_container_copy_construction(true_type, const allocator_type& __a)
1450 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452 static allocator_type
1453 select_on_container_copy_construction(false_type, const allocator_type& __a)
1454 {return __a;}
1455};
1456
1457// uses_allocator
1458
1459template <class _Tp>
1460struct __has_allocator_type
1461{
1462private:
1463 struct __two {char _; char __;};
1464 template <class _Up> static __two __test(...);
1465 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1466public:
1467 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1468};
1469
1470template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1471struct __uses_allocator
1472 : public integral_constant<bool,
1473 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1474{
1475};
1476
1477template <class _Tp, class _Alloc>
1478struct __uses_allocator<_Tp, _Alloc, false>
1479 : public false_type
1480{
1481};
1482
1483template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001484struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 : public __uses_allocator<_Tp, _Alloc>
1486{
1487};
1488
Howard Hinnant726a76f2010-11-16 21:10:23 +00001489#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490
1491// uses-allocator construction
1492
1493template <class _Tp, class _Alloc, class ..._Args>
1494struct __uses_alloc_ctor_imp
1495{
1496 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1497 static const bool __ic =
1498 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1499 static const int value = __ua ? 2 - __ic : 0;
1500};
1501
1502template <class _Tp, class _Alloc, class ..._Args>
1503struct __uses_alloc_ctor
1504 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1505 {};
1506
Howard Hinnant726a76f2010-11-16 21:10:23 +00001507#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508
1509// allocator
1510
1511template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001512class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513{
1514public:
1515 typedef size_t size_type;
1516 typedef ptrdiff_t difference_type;
1517 typedef _Tp* pointer;
1518 typedef const _Tp* const_pointer;
1519 typedef _Tp& reference;
1520 typedef const _Tp& const_reference;
1521 typedef _Tp value_type;
1522
1523 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1524
1525 _LIBCPP_INLINE_VISIBILITY allocator() throw() {}
1526 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) throw() {}
1527 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const {return addressof(__x);}
1528 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const {return addressof(__x);}
1529 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1530 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1531 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) {::operator delete((void*)__p);}
1532 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001533#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 template <class _Up, class... _Args>
1535 _LIBCPP_INLINE_VISIBILITY
1536 void
1537 construct(_Up* __p, _Args&&... __args)
1538 {
1539 ::new((void*)__p) _Up(_STD::forward<_Args>(__args)...);
1540 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001541#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 _LIBCPP_INLINE_VISIBILITY
1543 void
1544 construct(pointer __p)
1545 {
1546 ::new((void*)__p) _Tp();
1547 }
1548 template <class _A0>
1549 _LIBCPP_INLINE_VISIBILITY
1550 typename enable_if
1551 <
1552 !is_convertible<_A0, __rv<_A0> >::value,
1553 void
1554 >::type
1555 construct(pointer __p, _A0& __a0)
1556 {
1557 ::new((void*)__p) _Tp(__a0);
1558 }
1559 template <class _A0>
1560 _LIBCPP_INLINE_VISIBILITY
1561 typename enable_if
1562 <
1563 !is_convertible<_A0, __rv<_A0> >::value,
1564 void
1565 >::type
1566 construct(pointer __p, const _A0& __a0)
1567 {
1568 ::new((void*)__p) _Tp(__a0);
1569 }
1570 template <class _A0>
1571 _LIBCPP_INLINE_VISIBILITY
1572 typename enable_if
1573 <
1574 is_convertible<_A0, __rv<_A0> >::value,
1575 void
1576 >::type
1577 construct(pointer __p, _A0 __a0)
1578 {
1579 ::new((void*)__p) _Tp(_STD::move(__a0));
1580 }
1581 template <class _A0, class _A1>
1582 _LIBCPP_INLINE_VISIBILITY
1583 void
1584 construct(pointer __p, _A0& __a0, _A1& __a1)
1585 {
1586 ::new((void*)__p) _Tp(__a0, __a1);
1587 }
1588 template <class _A0, class _A1>
1589 _LIBCPP_INLINE_VISIBILITY
1590 void
1591 construct(pointer __p, const _A0& __a0, _A1& __a1)
1592 {
1593 ::new((void*)__p) _Tp(__a0, __a1);
1594 }
1595 template <class _A0, class _A1>
1596 _LIBCPP_INLINE_VISIBILITY
1597 void
1598 construct(pointer __p, _A0& __a0, const _A1& __a1)
1599 {
1600 ::new((void*)__p) _Tp(__a0, __a1);
1601 }
1602 template <class _A0, class _A1>
1603 _LIBCPP_INLINE_VISIBILITY
1604 void
1605 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1606 {
1607 ::new((void*)__p) _Tp(__a0, __a1);
1608 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001609#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1611};
1612
1613template <class _Tp, class _Up>
1614inline _LIBCPP_INLINE_VISIBILITY
1615bool operator==(const allocator<_Tp>&, const allocator<_Up>&) throw() {return true;}
1616
1617template <class _Tp, class _Up>
1618inline _LIBCPP_INLINE_VISIBILITY
1619bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) throw() {return false;}
1620
1621template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001622class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623 : public iterator<output_iterator_tag,
1624 _Tp, // purposefully not C++03
1625 ptrdiff_t, // purposefully not C++03
1626 _Tp*, // purposefully not C++03
1627 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1628{
1629private:
1630 _OutputIterator __x_;
1631public:
1632 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1633 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1634 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1635 {::new(&*__x_) _Tp(__element); return *this;}
1636 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1637 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1638 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1639};
1640
1641template <class _Tp>
1642pair<_Tp*, ptrdiff_t>
1643get_temporary_buffer(ptrdiff_t __n)
1644{
1645 pair<_Tp*, ptrdiff_t> __r(0, 0);
1646 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1647 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1648 / sizeof(_Tp);
1649 if (__n > __m)
1650 __n = __m;
1651 while (__n > 0)
1652 {
1653 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1654 if (__r.first)
1655 {
1656 __r.second = __n;
1657 break;
1658 }
1659 __n /= 2;
1660 }
1661 return __r;
1662}
1663
1664template <class _Tp>
1665inline _LIBCPP_INLINE_VISIBILITY
1666void return_temporary_buffer(_Tp* __p) {::operator delete(__p);}
1667
1668template <class _Tp>
1669struct auto_ptr_ref
1670{
1671 _Tp* __ptr_;
1672};
1673
1674template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001675class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676{
1677private:
1678 _Tp* __ptr_;
1679public:
1680 typedef _Tp element_type;
1681
1682 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1683 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1684 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1685 : __ptr_(__p.release()) {}
1686 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1687 {reset(__p.release()); return *this;}
1688 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1689 {reset(__p.release()); return *this;}
1690 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1691 {reset(__p.__ptr_); return *this;}
1692 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1693
1694 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1695 {return *__ptr_;}
1696 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1697 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1698 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1699 {
1700 _Tp* __t = __ptr_;
1701 __ptr_ = 0;
1702 return __t;
1703 }
1704 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1705 {
1706 if (__ptr_ != __p)
1707 delete __ptr_;
1708 __ptr_ = __p;
1709 }
1710
1711 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1712 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1713 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1714 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1715 {return auto_ptr<_Up>(release());}
1716};
1717
1718template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001719class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720{
1721public:
1722 typedef void element_type;
1723};
1724
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1726 typename remove_cv<_T2>::type>::value,
1727 bool = is_empty<_T1>::value,
1728 bool = is_empty<_T2>::value>
1729struct __libcpp_compressed_pair_switch;
1730
1731template <class _T1, class _T2, bool IsSame>
1732struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1733
1734template <class _T1, class _T2, bool IsSame>
1735struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1736
1737template <class _T1, class _T2, bool IsSame>
1738struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1739
1740template <class _T1, class _T2>
1741struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1742
1743template <class _T1, class _T2>
1744struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1745
1746template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1747class __libcpp_compressed_pair_imp;
1748
1749template <class _T1, class _T2>
1750class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1751{
1752private:
1753 _T1 __first_;
1754 _T2 __second_;
1755public:
1756 typedef _T1 _T1_param;
1757 typedef _T2 _T2_param;
1758
1759 typedef typename remove_reference<_T1>::type& _T1_reference;
1760 typedef typename remove_reference<_T2>::type& _T2_reference;
1761
1762 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1763 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1764
1765 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1766 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1767 : __first_(_STD::forward<_T1_param>(__t1)) {}
1768 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1769 : __second_(_STD::forward<_T2_param>(__t2)) {}
1770 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1771 : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1772
Howard Hinnant73d21a42010-09-04 23:28:19 +00001773#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1775 : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001776#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777
1778 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1779 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1780
1781 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1782 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1783
1784 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1785 {
1786 using _STD::swap;
1787 swap(__first_, __x.__first_);
1788 swap(__second_, __x.__second_);
1789 }
1790};
1791
1792template <class _T1, class _T2>
1793class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1794 : private _T1
1795{
1796private:
1797 _T2 __second_;
1798public:
1799 typedef _T1 _T1_param;
1800 typedef _T2 _T2_param;
1801
1802 typedef _T1& _T1_reference;
1803 typedef typename remove_reference<_T2>::type& _T2_reference;
1804
1805 typedef const _T1& _T1_const_reference;
1806 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1807
1808 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1809 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1810 : _T1(_STD::forward<_T1_param>(__t1)) {}
1811 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1812 : __second_(_STD::forward<_T2_param>(__t2)) {}
1813 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1814 : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1815
Howard Hinnant73d21a42010-09-04 23:28:19 +00001816#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1818 : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001819#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820
1821 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1822 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1823
1824 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1825 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1826
1827 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1828 {
1829 using _STD::swap;
1830 swap(__second_, __x.__second_);
1831 }
1832};
1833
1834template <class _T1, class _T2>
1835class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1836 : private _T2
1837{
1838private:
1839 _T1 __first_;
1840public:
1841 typedef _T1 _T1_param;
1842 typedef _T2 _T2_param;
1843
1844 typedef typename remove_reference<_T1>::type& _T1_reference;
1845 typedef _T2& _T2_reference;
1846
1847 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1848 typedef const _T2& _T2_const_reference;
1849
1850 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1851 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1852 : __first_(_STD::forward<_T1_param>(__t1)) {}
1853 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1854 : _T2(_STD::forward<_T2_param>(__t2)) {}
1855 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1856 : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1857
Howard Hinnant73d21a42010-09-04 23:28:19 +00001858#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1860 : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001861#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862
1863 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1864 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1865
1866 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1867 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1868
1869 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1870 {
1871 using _STD::swap;
1872 swap(__first_, __x.__first_);
1873 }
1874};
1875
1876template <class _T1, class _T2>
1877class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1878 : private _T1,
1879 private _T2
1880{
1881public:
1882 typedef _T1 _T1_param;
1883 typedef _T2 _T2_param;
1884
1885 typedef _T1& _T1_reference;
1886 typedef _T2& _T2_reference;
1887
1888 typedef const _T1& _T1_const_reference;
1889 typedef const _T2& _T2_const_reference;
1890
1891 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1892 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1893 : _T1(_STD::forward<_T1_param>(__t1)) {}
1894 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1895 : _T2(_STD::forward<_T2_param>(__t2)) {}
1896 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1897 : _T1(_STD::forward<_T1_param>(__t1)), _T2(_STD::forward<_T2_param>(__t2)) {}
1898
Howard Hinnant73d21a42010-09-04 23:28:19 +00001899#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1901 : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001902#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903
1904 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1905 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1906
1907 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1908 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1909
1910 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1911 {
1912 }
1913};
1914
1915template <class _T1, class _T2>
1916class __compressed_pair
1917 : private __libcpp_compressed_pair_imp<_T1, _T2>
1918{
1919 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
1920public:
1921 typedef typename base::_T1_param _T1_param;
1922 typedef typename base::_T2_param _T2_param;
1923
1924 typedef typename base::_T1_reference _T1_reference;
1925 typedef typename base::_T2_reference _T2_reference;
1926
1927 typedef typename base::_T1_const_reference _T1_const_reference;
1928 typedef typename base::_T2_const_reference _T2_const_reference;
1929
1930 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
1931 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
1932 : base(_STD::forward<_T1_param>(__t1)) {}
1933 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
1934 : base(_STD::forward<_T2_param>(__t2)) {}
1935 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
1936 : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
1937
Howard Hinnant73d21a42010-09-04 23:28:19 +00001938#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939 __compressed_pair(__compressed_pair&& __p)
1940 : base(_STD::move(__p)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001941#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942
1943 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return base::first();}
1944 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return base::first();}
1945
1946 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return base::second();}
1947 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return base::second();}
1948
1949 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x) {base::swap(__x);}
1950};
1951
1952template <class _T1, class _T2>
1953inline _LIBCPP_INLINE_VISIBILITY
1954void
1955swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1956 {__x.swap(__y);}
1957
1958template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001959struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960{
1961 _LIBCPP_INLINE_VISIBILITY default_delete() {}
1962 template <class _Up>
1963 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
1964 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) {}
1965 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1966 {
1967 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1968 delete __ptr;
1969 }
1970};
1971
1972template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001973struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974{
1975 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1976 {
1977 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1978 delete [] __ptr;
1979 }
1980private:
1981 template <class _Up> void operator() (_Up*) const;
1982};
1983
1984template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00001985class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986{
1987public:
1988 typedef _Tp element_type;
1989 typedef _Dp deleter_type;
1990 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1991private:
1992 __compressed_pair<pointer, deleter_type> __ptr_;
1993
Howard Hinnant73d21a42010-09-04 23:28:19 +00001994#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995 unique_ptr(const unique_ptr&);
1996 unique_ptr& operator=(const unique_ptr&);
1997 template <class _Up, class _Ep>
1998 unique_ptr(const unique_ptr<_Up, _Ep>&);
1999 template <class _Up, class _Ep>
2000 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002001#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002 unique_ptr(unique_ptr&);
2003 template <class _Up, class _Ep>
2004 unique_ptr(unique_ptr<_Up, _Ep>&);
2005 unique_ptr& operator=(unique_ptr&);
2006 template <class _Up, class _Ep>
2007 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002008#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009
2010 struct __nat {int __for_bool_;};
2011
2012 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2013 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2014public:
2015 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2016 : __ptr_(pointer())
2017 {
2018 static_assert(!is_pointer<deleter_type>::value,
2019 "unique_ptr constructed with null function pointer deleter");
2020 }
2021 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2022 : __ptr_(pointer())
2023 {
2024 static_assert(!is_pointer<deleter_type>::value,
2025 "unique_ptr constructed with null function pointer deleter");
2026 }
2027 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2028 : __ptr_(_STD::move(__p))
2029 {
2030 static_assert(!is_pointer<deleter_type>::value,
2031 "unique_ptr constructed with null function pointer deleter");
2032 }
2033
Howard Hinnant73d21a42010-09-04 23:28:19 +00002034#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2036 is_reference<deleter_type>::value,
2037 deleter_type,
2038 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2039 : __ptr_(__p, __d) {}
2040
2041 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
2042 : __ptr_(__p, _STD::move(__d))
2043 {
2044 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2045 }
2046 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2047 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2048 template <class _Up, class _Ep>
2049 _LIBCPP_INLINE_VISIBILITY
2050 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2051 typename enable_if
2052 <
2053 !is_array<_Up>::value &&
2054 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2055 is_convertible<_Ep, deleter_type>::value &&
2056 (
2057 !is_reference<deleter_type>::value ||
2058 is_same<deleter_type, _Ep>::value
2059 ),
2060 __nat
2061 >::type = __nat())
2062 : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2063
2064 template <class _Up>
2065 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2066 typename enable_if<
2067 is_convertible<_Up*, _Tp*>::value &&
2068 is_same<_Dp, default_delete<_Tp> >::value,
2069 __nat
2070 >::type = __nat())
2071 : __ptr_(__p.release())
2072 {
2073 }
2074
2075 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2076 {
2077 reset(__u.release());
2078 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2079 return *this;
2080 }
2081
2082 template <class _Up, class _Ep>
2083 _LIBCPP_INLINE_VISIBILITY
2084 typename enable_if
2085 <
2086 !is_array<_Up>::value,
2087 unique_ptr&
2088 >::type
2089 operator=(unique_ptr<_Up, _Ep>&& __u)
2090 {
2091 reset(__u.release());
2092 __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2093 return *this;
2094 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002095#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096
2097 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2098 {
2099 return __rv<unique_ptr>(*this);
2100 }
2101
2102 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2103 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2104
2105 template <class _Up, class _Ep>
2106 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2107 {
2108 reset(__u.release());
2109 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2110 return *this;
2111 }
2112
2113 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2114 : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2115
2116 template <class _Up>
2117 _LIBCPP_INLINE_VISIBILITY
2118 typename enable_if<
2119 is_convertible<_Up*, _Tp*>::value &&
2120 is_same<_Dp, default_delete<_Tp> >::value,
2121 unique_ptr&
2122 >::type
2123 operator=(auto_ptr<_Up> __p)
2124 {reset(__p.release()); return *this;}
2125
Howard Hinnant73d21a42010-09-04 23:28:19 +00002126#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2128
2129 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2130 {
2131 reset();
2132 return *this;
2133 }
2134
2135 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2136 {return *__ptr_.first();}
2137 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_.first();}
2138 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2139 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2140 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2141 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2142
2143 _LIBCPP_INLINE_VISIBILITY pointer release()
2144 {
2145 pointer __t = __ptr_.first();
2146 __ptr_.first() = pointer();
2147 return __t;
2148 }
2149
2150 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2151 {
2152 pointer __tmp = __ptr_.first();
2153 __ptr_.first() = __p;
2154 if (__tmp)
2155 __ptr_.second()(__tmp);
2156 }
2157
2158 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2159};
2160
2161template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002162class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163{
2164public:
2165 typedef _Tp element_type;
2166 typedef _Dp deleter_type;
2167 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2168private:
2169 __compressed_pair<pointer, deleter_type> __ptr_;
2170
Howard Hinnant73d21a42010-09-04 23:28:19 +00002171#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172 unique_ptr(const unique_ptr&);
2173 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002174#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 unique_ptr(unique_ptr&);
2176 template <class _Up>
2177 unique_ptr(unique_ptr<_Up>&);
2178 unique_ptr& operator=(unique_ptr&);
2179 template <class _Up>
2180 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002181#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182
2183 struct __nat {int __for_bool_;};
2184
2185 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2186 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2187public:
2188 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2189 : __ptr_(pointer())
2190 {
2191 static_assert(!is_pointer<deleter_type>::value,
2192 "unique_ptr constructed with null function pointer deleter");
2193 }
2194 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2195 : __ptr_(pointer())
2196 {
2197 static_assert(!is_pointer<deleter_type>::value,
2198 "unique_ptr constructed with null function pointer deleter");
2199 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002200#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201 template <class _P,
2202 class = typename enable_if<is_same<_P, pointer>::value>::type
2203 >
2204 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p)
2205 : __ptr_(__p)
2206 {
2207 static_assert(!is_pointer<deleter_type>::value,
2208 "unique_ptr constructed with null function pointer deleter");
2209 }
2210
2211 template <class _P,
2212 class = typename enable_if<is_same<_P, pointer>::value>::type
2213 >
2214 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2215 is_reference<deleter_type>::value,
2216 deleter_type,
2217 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2218 : __ptr_(__p, __d) {}
2219
2220 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2221 is_reference<deleter_type>::value,
2222 deleter_type,
2223 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2224 : __ptr_(pointer(), __d) {}
2225
2226 template <class _P,
2227 class = typename enable_if<is_same<_P, pointer>::value ||
2228 is_same<_P, nullptr_t>::value>::type
2229 >
2230 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
2231 : __ptr_(__p, _STD::move(__d))
2232 {
2233 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2234 }
2235
2236 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
2237 : __ptr_(pointer(), _STD::move(__d))
2238 {
2239 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2240 }
2241
2242 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2243 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2244
2245 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2246 {
2247 reset(__u.release());
2248 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2249 return *this;
2250 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002251#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252
2253 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2254 : __ptr_(__p)
2255 {
2256 static_assert(!is_pointer<deleter_type>::value,
2257 "unique_ptr constructed with null function pointer deleter");
2258 }
2259
2260 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2261 : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2262
2263 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2264 : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2265
2266 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2267 {
2268 return __rv<unique_ptr>(*this);
2269 }
2270
2271 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2272 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2273
2274 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2275 {
2276 reset(__u->release());
2277 __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2278 return *this;
2279 }
2280
Howard Hinnant73d21a42010-09-04 23:28:19 +00002281#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2283
2284 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2285 {
2286 reset();
2287 return *this;
2288 }
2289
2290 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2291 {return __ptr_.first()[__i];}
2292 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2293 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2294 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2295 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2296
2297 _LIBCPP_INLINE_VISIBILITY pointer release()
2298 {
2299 pointer __t = __ptr_.first();
2300 __ptr_.first() = pointer();
2301 return __t;
2302 }
2303
Howard Hinnant73d21a42010-09-04 23:28:19 +00002304#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 template <class _P,
2306 class = typename enable_if<is_same<_P, pointer>::value>::type
2307 >
2308 _LIBCPP_INLINE_VISIBILITY void reset(_P __p)
2309 {
2310 pointer __tmp = __ptr_.first();
2311 __ptr_.first() = __p;
2312 if (__tmp)
2313 __ptr_.second()(__tmp);
2314 }
2315 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t)
2316 {
2317 pointer __tmp = __ptr_.first();
2318 __ptr_.first() = nullptr;
2319 if (__tmp)
2320 __ptr_.second()(__tmp);
2321 }
2322 _LIBCPP_INLINE_VISIBILITY void reset()
2323 {
2324 pointer __tmp = __ptr_.first();
2325 __ptr_.first() = nullptr;
2326 if (__tmp)
2327 __ptr_.second()(__tmp);
2328 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002329#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2331 {
2332 pointer __tmp = __ptr_.first();
2333 __ptr_.first() = __p;
2334 if (__tmp)
2335 __ptr_.second()(__tmp);
2336 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002337#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002338
2339 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2340private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002341
Howard Hinnant73d21a42010-09-04 23:28:19 +00002342#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 template <class _Up>
2344 explicit unique_ptr(_Up);
2345 template <class _Up>
2346 unique_ptr(_Up __u,
2347 typename conditional<
2348 is_reference<deleter_type>::value,
2349 deleter_type,
2350 typename add_lvalue_reference<const deleter_type>::type>::type,
2351 typename enable_if
2352 <
2353 is_convertible<_Up, pointer>::value,
2354 __nat
2355 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002356#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357};
2358
2359template <class _Tp, class _Dp>
2360inline _LIBCPP_INLINE_VISIBILITY
2361void
2362swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) {__x.swap(__y);}
2363
2364template <class _T1, class _D1, class _T2, class _D2>
2365inline _LIBCPP_INLINE_VISIBILITY
2366bool
2367operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2368
2369template <class _T1, class _D1, class _T2, class _D2>
2370inline _LIBCPP_INLINE_VISIBILITY
2371bool
2372operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2373
2374template <class _T1, class _D1, class _T2, class _D2>
2375inline _LIBCPP_INLINE_VISIBILITY
2376bool
2377operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2378
2379template <class _T1, class _D1, class _T2, class _D2>
2380inline _LIBCPP_INLINE_VISIBILITY
2381bool
2382operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2383
2384template <class _T1, class _D1, class _T2, class _D2>
2385inline _LIBCPP_INLINE_VISIBILITY
2386bool
2387operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2388
2389template <class _T1, class _D1, class _T2, class _D2>
2390inline _LIBCPP_INLINE_VISIBILITY
2391bool
2392operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2393
Howard Hinnant21aefc32010-06-03 16:42:57 +00002394template <class> struct hash;
2395
2396template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002397struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002398 : public unary_function<_Tp*, size_t>
2399{
Howard Hinnant82894812010-09-22 16:48:34 +00002400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00002401 size_t operator()(_Tp* __v) const
2402 {
2403 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2404 return *__p;
2405 }
2406};
2407
2408template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002409struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002410{
2411 typedef unique_ptr<_Tp, _Dp> argument_type;
2412 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00002414 result_type operator()(const argument_type& __ptr) const
2415 {
2416 typedef typename argument_type::pointer pointer;
2417 return hash<pointer>()(__ptr.get());
2418 }
2419};
2420
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002421struct __destruct_n
2422{
2423private:
2424 size_t size;
2425
2426 template <class _Tp>
2427 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type)
2428 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2429
2430 template <class _Tp>
2431 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type)
2432 {}
2433
2434 _LIBCPP_INLINE_VISIBILITY void __incr(false_type)
2435 {++size;}
2436 _LIBCPP_INLINE_VISIBILITY void __incr(true_type)
2437 {}
2438
2439 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type)
2440 {size = __s;}
2441 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type)
2442 {}
2443public:
2444 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) : size(__s) {}
2445
2446 template <class _Tp>
2447 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*)
2448 {__incr(integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2449
2450 template <class _Tp>
2451 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*)
2452 {__set(__s, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2453
2454 template <class _Tp>
2455 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p)
2456 {__process(__p, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2457};
2458
2459template <class _Alloc>
2460class __allocator_destructor
2461{
2462 typedef allocator_traits<_Alloc> __alloc_traits;
2463public:
2464 typedef typename __alloc_traits::pointer pointer;
2465 typedef typename __alloc_traits::size_type size_type;
2466private:
2467 _Alloc& __alloc_;
2468 size_type __s_;
2469public:
2470 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2471 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473 void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2474};
2475
2476template <class _InputIterator, class _ForwardIterator>
2477_ForwardIterator
2478uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2479{
2480 __destruct_n __d(0);
2481 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2482 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2483 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2484 ::new(&*__r) value_type(*__f);
2485 __h.release();
2486 return __r;
2487}
2488
2489template <class _InputIterator, class _Size, class _ForwardIterator>
2490_ForwardIterator
2491uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2492{
2493 __destruct_n __d(0);
2494 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2495 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2496 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2497 ::new(&*__r) value_type(*__f);
2498 __h.release();
2499 return __r;
2500}
2501
2502template <class _ForwardIterator, class _Tp>
2503void
2504uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2505{
2506 __destruct_n __d(0);
2507 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2508 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2509 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2510 ::new(&*__f) value_type(__x);
2511 __h.release();
2512}
2513
2514template <class _ForwardIterator, class _Size, class _Tp>
2515void
2516uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2517{
2518 __destruct_n __d(0);
2519 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2520 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2521 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2522 ::new(&*__f) value_type(__x);
2523 __h.release();
2524}
2525
Howard Hinnant82894812010-09-22 16:48:34 +00002526class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527 : public std::exception
2528{
2529public:
2530 virtual ~bad_weak_ptr() throw();
2531 virtual const char* what() const throw();
2532};
2533
2534template<class _Tp> class weak_ptr;
2535
2536class __shared_count
2537{
2538 __shared_count(const __shared_count&);
2539 __shared_count& operator=(const __shared_count&);
2540
2541protected:
2542 long __shared_owners_;
2543 virtual ~__shared_count();
2544private:
2545 virtual void __on_zero_shared() = 0;
2546
2547public:
Howard Hinnant82894812010-09-22 16:48:34 +00002548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549 explicit __shared_count(long __refs = 0)
2550 : __shared_owners_(__refs) {}
2551
2552 void __add_shared();
Howard Hinnant28dbbe02010-11-16 21:33:17 +00002553 bool __release_shared();
Howard Hinnant82894812010-09-22 16:48:34 +00002554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555 long use_count() const {return __shared_owners_ + 1;}
2556};
2557
2558class __shared_weak_count
2559 : private __shared_count
2560{
2561 long __shared_weak_owners_;
2562
2563public:
Howard Hinnant82894812010-09-22 16:48:34 +00002564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 explicit __shared_weak_count(long __refs = 0)
2566 : __shared_count(__refs),
2567 __shared_weak_owners_(__refs) {}
2568protected:
2569 virtual ~__shared_weak_count();
2570
2571public:
2572 void __add_shared();
2573 void __add_weak();
2574 void __release_shared();
2575 void __release_weak();
Howard Hinnant82894812010-09-22 16:48:34 +00002576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 long use_count() const {return __shared_count::use_count();}
2578 __shared_weak_count* lock();
2579
Howard Hinnantd4444702010-08-11 17:04:31 +00002580#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002582#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002583private:
2584 virtual void __on_zero_shared_weak() = 0;
2585};
2586
2587template <class _Tp, class _Dp, class _Alloc>
2588class __shared_ptr_pointer
2589 : public __shared_weak_count
2590{
2591 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2592public:
Howard Hinnant82894812010-09-22 16:48:34 +00002593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2595 : __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2596
Howard Hinnantd4444702010-08-11 17:04:31 +00002597#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002598 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002599#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600
2601private:
2602 virtual void __on_zero_shared();
2603 virtual void __on_zero_shared_weak();
2604};
2605
Howard Hinnantd4444702010-08-11 17:04:31 +00002606#ifndef _LIBCPP_NO_RTTI
2607
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002608template <class _Tp, class _Dp, class _Alloc>
2609const void*
2610__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
2611{
2612 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2613}
2614
Howard Hinnant324bb032010-08-22 00:02:43 +00002615#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002616
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617template <class _Tp, class _Dp, class _Alloc>
2618void
2619__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
2620{
2621 __data_.first().second()(__data_.first().first());
2622 __data_.first().second().~_Dp();
2623}
2624
2625template <class _Tp, class _Dp, class _Alloc>
2626void
2627__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak()
2628{
2629 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2630 __data_.second().~_Alloc();
2631 __a.deallocate(this, 1);
2632}
2633
2634template <class _Tp, class _Alloc>
2635class __shared_ptr_emplace
2636 : public __shared_weak_count
2637{
2638 __compressed_pair<_Alloc, _Tp> __data_;
2639public:
2640#ifndef _LIBCPP_HAS_NO_VARIADICS
2641
Howard Hinnant82894812010-09-22 16:48:34 +00002642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 __shared_ptr_emplace(_Alloc __a)
2644 : __data_(_STD::move(__a)) {}
2645
2646 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2649 : __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2650
2651#else // _LIBCPP_HAS_NO_VARIADICS
2652
Howard Hinnant82894812010-09-22 16:48:34 +00002653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002654 __shared_ptr_emplace(_Alloc __a)
2655 : __data_(__a) {}
2656
2657 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2660 : __data_(__a, _Tp(__a0)) {}
2661
2662 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2665 : __data_(__a, _Tp(__a0, __a1)) {}
2666
2667 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2670 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2671
2672#endif // _LIBCPP_HAS_NO_VARIADICS
2673
2674private:
2675 virtual void __on_zero_shared();
2676 virtual void __on_zero_shared_weak();
2677public:
Howard Hinnant82894812010-09-22 16:48:34 +00002678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679 _Tp* get() {return &__data_.second();}
2680};
2681
2682template <class _Tp, class _Alloc>
2683void
2684__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared()
2685{
2686 __data_.second().~_Tp();
2687}
2688
2689template <class _Tp, class _Alloc>
2690void
2691__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak()
2692{
2693 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2694 __data_.first().~_Alloc();
2695 __a.deallocate(this, 1);
2696}
2697
2698template<class _Tp> class enable_shared_from_this;
2699
2700template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002701class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702{
Howard Hinnant324bb032010-08-22 00:02:43 +00002703public:
2704 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002705private:
2706 element_type* __ptr_;
2707 __shared_weak_count* __cntrl_;
2708
2709 struct __nat {int __for_bool_;};
2710public:
2711 shared_ptr();
2712 shared_ptr(nullptr_t);
2713 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00002714 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2715 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2717 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant324bb032010-08-22 00:02:43 +00002718 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719 shared_ptr(const shared_ptr& __r);
2720 template<class _Yp>
2721 shared_ptr(const shared_ptr<_Yp>& __r,
2722 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002723#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724 shared_ptr(shared_ptr&& __r);
2725 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002726 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002727#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002729 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002730#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002731 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2732#else
2733 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002735#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002737 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738public:
2739 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2740 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2741 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2742 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002743#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2745 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2746 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2747 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002748#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749
2750 ~shared_ptr();
2751
Howard Hinnant324bb032010-08-22 00:02:43 +00002752 shared_ptr& operator=(const shared_ptr& __r);
2753 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002754#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002755 shared_ptr& operator=(shared_ptr&& __r);
2756 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2757 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002758#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002759 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002761#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002763 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764public:
Howard Hinnant324bb032010-08-22 00:02:43 +00002765 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002766#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002767 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768#endif
2769
2770 void swap(shared_ptr& __r);
2771 void reset();
2772 template<class _Yp> void reset(_Yp* __p);
2773 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2774 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2775
Howard Hinnant82894812010-09-22 16:48:34 +00002776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777 element_type* get() const {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779 typename add_lvalue_reference<element_type>::type operator*() const {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781 element_type* operator->() const {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785 bool unique() const {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 bool empty() const {return __cntrl_ == 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 /*explicit*/ operator bool() const {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002790 template <class _U>
2791 _LIBCPP_INLINE_VISIBILITY
2792 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002794 template <class _U>
2795 _LIBCPP_INLINE_VISIBILITY
2796 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797 {return __cntrl_ < __p.__cntrl_;}
2798
Howard Hinnantd4444702010-08-11 17:04:31 +00002799#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002802 _Dp* __get_deleter() const
2803 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002804#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805
2806#ifndef _LIBCPP_HAS_NO_VARIADICS
2807
2808 template<class ..._Args>
2809 static
2810 shared_ptr<_Tp>
2811 make_shared(_Args&& ...__args);
2812
2813 template<class _Alloc, class ..._Args>
2814 static
2815 shared_ptr<_Tp>
2816 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2817
2818#else // _LIBCPP_HAS_NO_VARIADICS
2819
2820 static shared_ptr<_Tp> make_shared();
2821
2822 template<class _A0>
2823 static shared_ptr<_Tp> make_shared(_A0&);
2824
2825 template<class _A0, class _A1>
2826 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2827
2828 template<class _A0, class _A1, class _A2>
2829 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2830
2831 template<class _Alloc>
2832 static shared_ptr<_Tp>
2833 allocate_shared(const _Alloc& __a);
2834
2835 template<class _Alloc, class _A0>
2836 static shared_ptr<_Tp>
2837 allocate_shared(const _Alloc& __a, _A0& __a0);
2838
2839 template<class _Alloc, class _A0, class _A1>
2840 static shared_ptr<_Tp>
2841 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2842
2843 template<class _Alloc, class _A0, class _A1, class _A2>
2844 static shared_ptr<_Tp>
2845 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2846
2847#endif // _LIBCPP_HAS_NO_VARIADICS
2848
2849private:
2850
2851 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00002852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853 void
2854 __enable_weak_this(const enable_shared_from_this<_Yp>* __e)
2855 {
2856 if (__e)
2857 __e->__weak_this_ = *this;
2858 }
2859
Howard Hinnant82894812010-09-22 16:48:34 +00002860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002861 void __enable_weak_this(const void*) {}
2862
Howard Hinnant82894812010-09-22 16:48:34 +00002863 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
2864 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865};
2866
2867template<class _Tp>
2868inline _LIBCPP_INLINE_VISIBILITY
2869shared_ptr<_Tp>::shared_ptr()
2870 : __ptr_(0),
2871 __cntrl_(0)
2872{
2873}
2874
2875template<class _Tp>
2876inline _LIBCPP_INLINE_VISIBILITY
2877shared_ptr<_Tp>::shared_ptr(nullptr_t)
2878 : __ptr_(0),
2879 __cntrl_(0)
2880{
2881}
2882
2883template<class _Tp>
2884template<class _Yp>
2885shared_ptr<_Tp>::shared_ptr(_Yp* __p)
2886 : __ptr_(__p)
2887{
2888 unique_ptr<_Yp> __hold(__p);
2889 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2890 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
2891 __hold.release();
2892 __enable_weak_this(__p);
2893}
2894
2895template<class _Tp>
2896template<class _Yp, class _Dp>
2897shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
2898 : __ptr_(__p)
2899{
2900#ifndef _LIBCPP_NO_EXCEPTIONS
2901 try
2902 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002903#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002904 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
2905 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
2906 __enable_weak_this(__p);
2907#ifndef _LIBCPP_NO_EXCEPTIONS
2908 }
2909 catch (...)
2910 {
2911 __d(__p);
2912 throw;
2913 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002914#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915}
2916
2917template<class _Tp>
2918template<class _Dp>
2919shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
2920 : __ptr_(0)
2921{
2922#ifndef _LIBCPP_NO_EXCEPTIONS
2923 try
2924 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002925#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002926 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
2927 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
2928#ifndef _LIBCPP_NO_EXCEPTIONS
2929 }
2930 catch (...)
2931 {
2932 __d(__p);
2933 throw;
2934 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002935#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936}
2937
2938template<class _Tp>
2939template<class _Yp, class _Dp, class _Alloc>
2940shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
2941 : __ptr_(__p)
2942{
2943#ifndef _LIBCPP_NO_EXCEPTIONS
2944 try
2945 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002946#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002947 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
2948 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2949 typedef __allocator_destructor<_A2> _D2;
2950 _A2 __a2(__a);
2951 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2952 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2953 __cntrl_ = __hold2.release();
2954 __enable_weak_this(__p);
2955#ifndef _LIBCPP_NO_EXCEPTIONS
2956 }
2957 catch (...)
2958 {
2959 __d(__p);
2960 throw;
2961 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002962#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002963}
2964
2965template<class _Tp>
2966template<class _Dp, class _Alloc>
2967shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
2968 : __ptr_(0)
2969{
2970#ifndef _LIBCPP_NO_EXCEPTIONS
2971 try
2972 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002973#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
2975 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2976 typedef __allocator_destructor<_A2> _D2;
2977 _A2 __a2(__a);
2978 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2979 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2980 __cntrl_ = __hold2.release();
2981#ifndef _LIBCPP_NO_EXCEPTIONS
2982 }
2983 catch (...)
2984 {
2985 __d(__p);
2986 throw;
2987 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002988#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002989}
2990
2991template<class _Tp>
2992template<class _Yp>
2993inline _LIBCPP_INLINE_VISIBILITY
2994shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p)
2995 : __ptr_(__p),
2996 __cntrl_(__r.__cntrl_)
2997{
2998 if (__cntrl_)
2999 __cntrl_->__add_shared();
3000}
3001
3002template<class _Tp>
3003inline _LIBCPP_INLINE_VISIBILITY
3004shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r)
3005 : __ptr_(__r.__ptr_),
3006 __cntrl_(__r.__cntrl_)
3007{
3008 if (__cntrl_)
3009 __cntrl_->__add_shared();
3010}
3011
3012template<class _Tp>
3013template<class _Yp>
3014inline _LIBCPP_INLINE_VISIBILITY
3015shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3016 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3017 : __ptr_(__r.__ptr_),
3018 __cntrl_(__r.__cntrl_)
3019{
3020 if (__cntrl_)
3021 __cntrl_->__add_shared();
3022}
3023
Howard Hinnant73d21a42010-09-04 23:28:19 +00003024#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003025
3026template<class _Tp>
3027inline _LIBCPP_INLINE_VISIBILITY
3028shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r)
3029 : __ptr_(__r.__ptr_),
3030 __cntrl_(__r.__cntrl_)
3031{
3032 __r.__ptr_ = 0;
3033 __r.__cntrl_ = 0;
3034}
3035
3036template<class _Tp>
3037template<class _Yp>
3038inline _LIBCPP_INLINE_VISIBILITY
3039shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3040 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3041 : __ptr_(__r.__ptr_),
3042 __cntrl_(__r.__cntrl_)
3043{
3044 __r.__ptr_ = 0;
3045 __r.__cntrl_ = 0;
3046}
3047
Howard Hinnant73d21a42010-09-04 23:28:19 +00003048#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003049
3050template<class _Tp>
3051template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003052#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003053shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3054#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003055shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003056#endif
3057 : __ptr_(__r.get())
3058{
3059 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3060 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3061 __enable_weak_this(__r.get());
3062 __r.release();
3063}
3064
3065template<class _Tp>
3066template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003067#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003068shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3069#else
3070shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3071#endif
3072 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3073 : __ptr_(__r.get())
3074{
3075 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3076 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3077 __enable_weak_this(__r.get());
3078 __r.release();
3079}
3080
3081template<class _Tp>
3082template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003083#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003084shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3085#else
3086shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3087#endif
3088 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3089 : __ptr_(__r.get())
3090{
3091 typedef __shared_ptr_pointer<_Yp*,
3092 reference_wrapper<typename remove_reference<_Dp>::type>,
3093 allocator<_Yp> > _CntrlBlk;
3094 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3095 __enable_weak_this(__r.get());
3096 __r.release();
3097}
3098
3099#ifndef _LIBCPP_HAS_NO_VARIADICS
3100
3101template<class _Tp>
3102template<class ..._Args>
3103shared_ptr<_Tp>
3104shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3105{
3106 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3107 typedef allocator<_CntrlBlk> _A2;
3108 typedef __allocator_destructor<_A2> _D2;
3109 _A2 __a2;
3110 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3111 ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3112 shared_ptr<_Tp> __r;
3113 __r.__ptr_ = __hold2.get()->get();
3114 __r.__cntrl_ = __hold2.release();
3115 __r.__enable_weak_this(__r.__ptr_);
3116 return __r;
3117}
3118
3119template<class _Tp>
3120template<class _Alloc, class ..._Args>
3121shared_ptr<_Tp>
3122shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3123{
3124 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3125 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3126 typedef __allocator_destructor<_A2> _D2;
3127 _A2 __a2(__a);
3128 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3129 ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3130 shared_ptr<_Tp> __r;
3131 __r.__ptr_ = __hold2.get()->get();
3132 __r.__cntrl_ = __hold2.release();
3133 __r.__enable_weak_this(__r.__ptr_);
3134 return __r;
3135}
3136
3137#else // _LIBCPP_HAS_NO_VARIADICS
3138
3139template<class _Tp>
3140shared_ptr<_Tp>
3141shared_ptr<_Tp>::make_shared()
3142{
3143 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3144 typedef allocator<_CntrlBlk> _Alloc2;
3145 typedef __allocator_destructor<_Alloc2> _D2;
3146 _Alloc2 __alloc2;
3147 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3148 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3149 shared_ptr<_Tp> __r;
3150 __r.__ptr_ = __hold2.get()->get();
3151 __r.__cntrl_ = __hold2.release();
3152 __r.__enable_weak_this(__r.__ptr_);
3153 return __r;
3154}
3155
3156template<class _Tp>
3157template<class _A0>
3158shared_ptr<_Tp>
3159shared_ptr<_Tp>::make_shared(_A0& __a0)
3160{
3161 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3162 typedef allocator<_CntrlBlk> _Alloc2;
3163 typedef __allocator_destructor<_Alloc2> _D2;
3164 _Alloc2 __alloc2;
3165 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3166 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3167 shared_ptr<_Tp> __r;
3168 __r.__ptr_ = __hold2.get()->get();
3169 __r.__cntrl_ = __hold2.release();
3170 __r.__enable_weak_this(__r.__ptr_);
3171 return __r;
3172}
3173
3174template<class _Tp>
3175template<class _A0, class _A1>
3176shared_ptr<_Tp>
3177shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3178{
3179 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3180 typedef allocator<_CntrlBlk> _Alloc2;
3181 typedef __allocator_destructor<_Alloc2> _D2;
3182 _Alloc2 __alloc2;
3183 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3184 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3185 shared_ptr<_Tp> __r;
3186 __r.__ptr_ = __hold2.get()->get();
3187 __r.__cntrl_ = __hold2.release();
3188 __r.__enable_weak_this(__r.__ptr_);
3189 return __r;
3190}
3191
3192template<class _Tp>
3193template<class _A0, class _A1, class _A2>
3194shared_ptr<_Tp>
3195shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3196{
3197 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3198 typedef allocator<_CntrlBlk> _Alloc2;
3199 typedef __allocator_destructor<_Alloc2> _D2;
3200 _Alloc2 __alloc2;
3201 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3202 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3203 shared_ptr<_Tp> __r;
3204 __r.__ptr_ = __hold2.get()->get();
3205 __r.__cntrl_ = __hold2.release();
3206 __r.__enable_weak_this(__r.__ptr_);
3207 return __r;
3208}
3209
3210template<class _Tp>
3211template<class _Alloc>
3212shared_ptr<_Tp>
3213shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3214{
3215 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3216 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3217 typedef __allocator_destructor<_Alloc2> _D2;
3218 _Alloc2 __alloc2(__a);
3219 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3220 ::new(__hold2.get()) _CntrlBlk(__a);
3221 shared_ptr<_Tp> __r;
3222 __r.__ptr_ = __hold2.get()->get();
3223 __r.__cntrl_ = __hold2.release();
3224 __r.__enable_weak_this(__r.__ptr_);
3225 return __r;
3226}
3227
3228template<class _Tp>
3229template<class _Alloc, class _A0>
3230shared_ptr<_Tp>
3231shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3232{
3233 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3234 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3235 typedef __allocator_destructor<_Alloc2> _D2;
3236 _Alloc2 __alloc2(__a);
3237 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3238 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3239 shared_ptr<_Tp> __r;
3240 __r.__ptr_ = __hold2.get()->get();
3241 __r.__cntrl_ = __hold2.release();
3242 __r.__enable_weak_this(__r.__ptr_);
3243 return __r;
3244}
3245
3246template<class _Tp>
3247template<class _Alloc, class _A0, class _A1>
3248shared_ptr<_Tp>
3249shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3250{
3251 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3252 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3253 typedef __allocator_destructor<_Alloc2> _D2;
3254 _Alloc2 __alloc2(__a);
3255 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3256 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3257 shared_ptr<_Tp> __r;
3258 __r.__ptr_ = __hold2.get()->get();
3259 __r.__cntrl_ = __hold2.release();
3260 __r.__enable_weak_this(__r.__ptr_);
3261 return __r;
3262}
3263
3264template<class _Tp>
3265template<class _Alloc, class _A0, class _A1, class _A2>
3266shared_ptr<_Tp>
3267shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3268{
3269 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3270 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3271 typedef __allocator_destructor<_Alloc2> _D2;
3272 _Alloc2 __alloc2(__a);
3273 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3274 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3275 shared_ptr<_Tp> __r;
3276 __r.__ptr_ = __hold2.get()->get();
3277 __r.__cntrl_ = __hold2.release();
3278 __r.__enable_weak_this(__r.__ptr_);
3279 return __r;
3280}
3281
3282#endif // _LIBCPP_HAS_NO_VARIADICS
3283
3284template<class _Tp>
3285shared_ptr<_Tp>::~shared_ptr()
3286{
3287 if (__cntrl_)
3288 __cntrl_->__release_shared();
3289}
3290
3291template<class _Tp>
3292inline _LIBCPP_INLINE_VISIBILITY
3293shared_ptr<_Tp>&
3294shared_ptr<_Tp>::operator=(const shared_ptr& __r)
3295{
3296 shared_ptr(__r).swap(*this);
3297 return *this;
3298}
3299
3300template<class _Tp>
3301template<class _Yp>
3302inline _LIBCPP_INLINE_VISIBILITY
3303shared_ptr<_Tp>&
3304shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r)
3305{
3306 shared_ptr(__r).swap(*this);
3307 return *this;
3308}
3309
Howard Hinnant73d21a42010-09-04 23:28:19 +00003310#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003311
3312template<class _Tp>
3313inline _LIBCPP_INLINE_VISIBILITY
3314shared_ptr<_Tp>&
3315shared_ptr<_Tp>::operator=(shared_ptr&& __r)
3316{
3317 shared_ptr(_STD::move(__r)).swap(*this);
3318 return *this;
3319}
3320
3321template<class _Tp>
3322template<class _Yp>
3323inline _LIBCPP_INLINE_VISIBILITY
3324shared_ptr<_Tp>&
3325shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3326{
3327 shared_ptr(_STD::move(__r)).swap(*this);
3328 return *this;
3329}
3330
3331template<class _Tp>
3332template<class _Yp>
3333inline _LIBCPP_INLINE_VISIBILITY
3334shared_ptr<_Tp>&
3335shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3336{
3337 shared_ptr(__r).swap(*this);
3338 return *this;
3339}
3340
3341template<class _Tp>
3342template <class _Yp, class _Dp>
3343inline _LIBCPP_INLINE_VISIBILITY
3344shared_ptr<_Tp>&
3345shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3346{
3347 shared_ptr(_STD::move(__r)).swap(*this);
3348 return *this;
3349}
3350
Howard Hinnant73d21a42010-09-04 23:28:19 +00003351#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003352
3353template<class _Tp>
3354template<class _Yp>
3355inline _LIBCPP_INLINE_VISIBILITY
3356shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003357shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003358{
3359 shared_ptr(__r).swap(*this);
3360 return *this;
3361}
3362
3363template<class _Tp>
3364template <class _Yp, class _Dp>
3365inline _LIBCPP_INLINE_VISIBILITY
3366shared_ptr<_Tp>&
3367shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3368{
3369 shared_ptr(_STD::move(__r)).swap(*this);
3370 return *this;
3371}
3372
Howard Hinnant73d21a42010-09-04 23:28:19 +00003373#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003374
3375template<class _Tp>
3376inline _LIBCPP_INLINE_VISIBILITY
3377void
3378shared_ptr<_Tp>::swap(shared_ptr& __r)
3379{
3380 _STD::swap(__ptr_, __r.__ptr_);
3381 _STD::swap(__cntrl_, __r.__cntrl_);
3382}
3383
3384template<class _Tp>
3385inline _LIBCPP_INLINE_VISIBILITY
3386void
3387shared_ptr<_Tp>::reset()
3388{
3389 shared_ptr().swap(*this);
3390}
3391
3392template<class _Tp>
3393template<class _Yp>
3394inline _LIBCPP_INLINE_VISIBILITY
3395void
3396shared_ptr<_Tp>::reset(_Yp* __p)
3397{
3398 shared_ptr(__p).swap(*this);
3399}
3400
3401template<class _Tp>
3402template<class _Yp, class _Dp>
3403inline _LIBCPP_INLINE_VISIBILITY
3404void
3405shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3406{
3407 shared_ptr(__p, __d).swap(*this);
3408}
3409
3410template<class _Tp>
3411template<class _Yp, class _Dp, class _Alloc>
3412inline _LIBCPP_INLINE_VISIBILITY
3413void
3414shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3415{
3416 shared_ptr(__p, __d, __a).swap(*this);
3417}
3418
3419#ifndef _LIBCPP_HAS_NO_VARIADICS
3420
Howard Hinnant324bb032010-08-22 00:02:43 +00003421template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003422inline _LIBCPP_INLINE_VISIBILITY
3423shared_ptr<_Tp>
3424make_shared(_Args&& ...__args)
3425{
3426 return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3427}
3428
Howard Hinnant324bb032010-08-22 00:02:43 +00003429template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003430inline _LIBCPP_INLINE_VISIBILITY
3431shared_ptr<_Tp>
3432allocate_shared(const _Alloc& __a, _Args&& ...__args)
3433{
3434 return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3435}
3436
3437#else // _LIBCPP_HAS_NO_VARIADICS
3438
3439template<class _Tp>
3440inline _LIBCPP_INLINE_VISIBILITY
3441shared_ptr<_Tp>
3442make_shared()
3443{
3444 return shared_ptr<_Tp>::make_shared();
3445}
3446
3447template<class _Tp, class _A0>
3448inline _LIBCPP_INLINE_VISIBILITY
3449shared_ptr<_Tp>
3450make_shared(_A0& __a0)
3451{
3452 return shared_ptr<_Tp>::make_shared(__a0);
3453}
3454
3455template<class _Tp, class _A0, class _A1>
3456inline _LIBCPP_INLINE_VISIBILITY
3457shared_ptr<_Tp>
3458make_shared(_A0& __a0, _A1& __a1)
3459{
3460 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3461}
3462
Howard Hinnant324bb032010-08-22 00:02:43 +00003463template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003464inline _LIBCPP_INLINE_VISIBILITY
3465shared_ptr<_Tp>
3466make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3467{
3468 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3469}
3470
3471template<class _Tp, class _Alloc>
3472inline _LIBCPP_INLINE_VISIBILITY
3473shared_ptr<_Tp>
3474allocate_shared(const _Alloc& __a)
3475{
3476 return shared_ptr<_Tp>::allocate_shared(__a);
3477}
3478
3479template<class _Tp, class _Alloc, class _A0>
3480inline _LIBCPP_INLINE_VISIBILITY
3481shared_ptr<_Tp>
3482allocate_shared(const _Alloc& __a, _A0& __a0)
3483{
3484 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3485}
3486
3487template<class _Tp, class _Alloc, class _A0, class _A1>
3488inline _LIBCPP_INLINE_VISIBILITY
3489shared_ptr<_Tp>
3490allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3491{
3492 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3493}
3494
3495template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3496inline _LIBCPP_INLINE_VISIBILITY
3497shared_ptr<_Tp>
3498allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3499{
3500 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3501}
3502
3503#endif // _LIBCPP_HAS_NO_VARIADICS
3504
3505template<class _Tp, class _Up>
3506inline _LIBCPP_INLINE_VISIBILITY
3507bool
3508operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3509{
3510 return __x.get() == __y.get();
3511}
3512
3513template<class _Tp, class _Up>
3514inline _LIBCPP_INLINE_VISIBILITY
3515bool
3516operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3517{
3518 return !(__x == __y);
3519}
3520
3521template<class _Tp, class _Up>
3522inline _LIBCPP_INLINE_VISIBILITY
3523bool
3524operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3525{
3526 return __x.get() < __y.get();
3527}
3528
3529template<class _Tp>
3530inline _LIBCPP_INLINE_VISIBILITY
3531void
3532swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y)
3533{
3534 __x.swap(__y);
3535}
3536
3537template<class _Tp, class _Up>
3538inline _LIBCPP_INLINE_VISIBILITY
3539shared_ptr<_Tp>
3540static_pointer_cast(const shared_ptr<_Up>& __r)
3541{
3542 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3543}
3544
3545template<class _Tp, class _Up>
3546inline _LIBCPP_INLINE_VISIBILITY
3547shared_ptr<_Tp>
3548dynamic_pointer_cast(const shared_ptr<_Up>& __r)
3549{
3550 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3551 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3552}
3553
3554template<class _Tp, class _Up>
3555shared_ptr<_Tp>
3556const_pointer_cast(const shared_ptr<_Up>& __r)
3557{
3558 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3559}
3560
Howard Hinnantd4444702010-08-11 17:04:31 +00003561#ifndef _LIBCPP_NO_RTTI
3562
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563template<class _Dp, class _Tp>
3564inline _LIBCPP_INLINE_VISIBILITY
3565_Dp*
3566get_deleter(const shared_ptr<_Tp>& __p)
3567{
3568 return __p.template __get_deleter<_Dp>();
3569}
3570
Howard Hinnant324bb032010-08-22 00:02:43 +00003571#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003572
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003573template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003574class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003575{
Howard Hinnant324bb032010-08-22 00:02:43 +00003576public:
3577 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003578private:
3579 element_type* __ptr_;
3580 __shared_weak_count* __cntrl_;
3581
Howard Hinnant324bb032010-08-22 00:02:43 +00003582public:
3583 weak_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003584 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
3585 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003586 weak_ptr(weak_ptr const& __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003588 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3589
3590 ~weak_ptr();
3591
3592 weak_ptr& operator=(weak_ptr const& __r);
3593 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r);
3594 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r);
3595
3596 void swap(weak_ptr& __r);
3597 void reset();
3598
Howard Hinnant82894812010-09-22 16:48:34 +00003599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003602 bool expired() const {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
Howard Hinnant324bb032010-08-22 00:02:43 +00003603 shared_ptr<_Tp> lock() const;
Howard Hinnant82894812010-09-22 16:48:34 +00003604 template<class _Up>
3605 _LIBCPP_INLINE_VISIBILITY
3606 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003607 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003608 template<class _Up>
3609 _LIBCPP_INLINE_VISIBILITY
3610 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003611 {return __cntrl_ < __r.__cntrl_;}
3612
Howard Hinnant82894812010-09-22 16:48:34 +00003613 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3614 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615};
3616
3617template<class _Tp>
3618inline _LIBCPP_INLINE_VISIBILITY
3619weak_ptr<_Tp>::weak_ptr()
3620 : __ptr_(0),
3621 __cntrl_(0)
3622{
3623}
3624
3625template<class _Tp>
3626inline _LIBCPP_INLINE_VISIBILITY
3627weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r)
3628 : __ptr_(__r.__ptr_),
3629 __cntrl_(__r.__cntrl_)
3630{
3631 if (__cntrl_)
3632 __cntrl_->__add_weak();
3633}
3634
3635template<class _Tp>
3636template<class _Yp>
3637inline _LIBCPP_INLINE_VISIBILITY
3638weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3639 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3640 : __ptr_(__r.__ptr_),
3641 __cntrl_(__r.__cntrl_)
3642{
3643 if (__cntrl_)
3644 __cntrl_->__add_weak();
3645}
3646
3647template<class _Tp>
3648template<class _Yp>
3649inline _LIBCPP_INLINE_VISIBILITY
3650weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3651 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3652 : __ptr_(__r.__ptr_),
3653 __cntrl_(__r.__cntrl_)
3654{
3655 if (__cntrl_)
3656 __cntrl_->__add_weak();
3657}
3658
3659template<class _Tp>
3660weak_ptr<_Tp>::~weak_ptr()
3661{
3662 if (__cntrl_)
3663 __cntrl_->__release_weak();
3664}
3665
3666template<class _Tp>
3667inline _LIBCPP_INLINE_VISIBILITY
3668weak_ptr<_Tp>&
3669weak_ptr<_Tp>::operator=(weak_ptr const& __r)
3670{
3671 weak_ptr(__r).swap(*this);
3672 return *this;
3673}
3674
3675template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003676template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677inline _LIBCPP_INLINE_VISIBILITY
3678weak_ptr<_Tp>&
3679weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r)
3680{
3681 weak_ptr(__r).swap(*this);
3682 return *this;
3683}
3684
3685template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003686template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687inline _LIBCPP_INLINE_VISIBILITY
3688weak_ptr<_Tp>&
3689weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r)
3690{
3691 weak_ptr(__r).swap(*this);
3692 return *this;
3693}
3694
3695template<class _Tp>
3696inline _LIBCPP_INLINE_VISIBILITY
3697void
3698weak_ptr<_Tp>::swap(weak_ptr& __r)
3699{
3700 _STD::swap(__ptr_, __r.__ptr_);
3701 _STD::swap(__cntrl_, __r.__cntrl_);
3702}
3703
3704template<class _Tp>
3705inline _LIBCPP_INLINE_VISIBILITY
3706void
3707swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y)
3708{
3709 __x.swap(__y);
3710}
3711
3712template<class _Tp>
3713inline _LIBCPP_INLINE_VISIBILITY
3714void
3715weak_ptr<_Tp>::reset()
3716{
3717 weak_ptr().swap(*this);
3718}
3719
3720template<class _Tp>
3721template<class _Yp>
3722shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3723 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3724 : __ptr_(__r.__ptr_),
3725 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3726{
3727 if (__cntrl_ == 0)
3728#ifndef _LIBCPP_NO_EXCEPTIONS
3729 throw bad_weak_ptr();
3730#else
3731 assert(!"bad_weak_ptr");
3732#endif
3733}
3734
3735template<class _Tp>
3736shared_ptr<_Tp>
3737weak_ptr<_Tp>::lock() const
3738{
3739 shared_ptr<_Tp> __r;
3740 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3741 if (__r.__cntrl_)
3742 __r.__ptr_ = __ptr_;
3743 return __r;
3744}
3745
Howard Hinnant324bb032010-08-22 00:02:43 +00003746template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747
3748template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003749struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00003751{
3752 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3755 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3758 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003760 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3761 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003762};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763
3764template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003765struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3767{
Howard Hinnant324bb032010-08-22 00:02:43 +00003768 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003770 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3771 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003773 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3774 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3777 {return __x.owner_before(__y);}
3778};
3779
3780template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003781class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003782{
3783 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00003784protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003786 enable_shared_from_this() {}
Howard Hinnant82894812010-09-22 16:48:34 +00003787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003788 enable_shared_from_this(enable_shared_from_this const&) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790 enable_shared_from_this& operator=(enable_shared_from_this const&) {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00003791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003793public:
Howard Hinnant82894812010-09-22 16:48:34 +00003794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003795 shared_ptr<_Tp> shared_from_this() {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00003796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797 shared_ptr<_Tp const> shared_from_this() const {return shared_ptr<const _Tp>(__weak_this_);}
3798
3799 template <class _Up> friend class shared_ptr;
3800};
3801
Howard Hinnant21aefc32010-06-03 16:42:57 +00003802template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003803struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003804{
3805 typedef shared_ptr<_Tp> argument_type;
3806 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00003808 result_type operator()(const argument_type& __ptr) const
3809 {
3810 return hash<_Tp*>()(__ptr.get());
3811 }
3812};
3813
Howard Hinnant324bb032010-08-22 00:02:43 +00003814//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00003815struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003816{
3817 enum _
3818 {
3819 relaxed,
3820 preferred,
3821 strict
3822 };
3823
3824 _ __v_;
3825
Howard Hinnant82894812010-09-22 16:48:34 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003829 operator int() const {return __v_;}
3830};
3831
3832void declare_reachable(void* __p);
3833void declare_no_pointers(char* __p, size_t __n);
3834void undeclare_no_pointers(char* __p, size_t __n);
3835pointer_safety get_pointer_safety();
3836void* __undeclare_reachable(void*);
3837
3838template <class _Tp>
3839inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00003840_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003841undeclare_reachable(_Tp* __p)
3842{
3843 return static_cast<_Tp*>(__undeclare_reachable(__p));
3844}
3845
3846void* align(size_t, size_t, void*&, size_t&);
3847
3848_LIBCPP_END_NAMESPACE_STD
3849
3850#endif // _LIBCPP_MEMORY