blob: 2eb87c3b7876d300dd9f9018e1fc42cbda7000b9 [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
55 | ptrdiff_t difference_type;
56 typedef Alloc::size_type | size_t size_type;
57 typedef Alloc::propagate_on_container_copy_assignment
58 | false_type propagate_on_container_copy_assignment;
59 typedef Alloc::propagate_on_container_move_assignment
60 | false_type propagate_on_container_move_assignment;
61 typedef Alloc::propagate_on_container_swap
62 | false_type propagate_on_container_swap;
63
64 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
65 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
66
67 static pointer allocate(allocator_type& a, size_type n);
68 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
69
70 static void deallocate(allocator_type& a, pointer p, size_type n);
71
72 template <class T, class... Args>
73 static void construct(allocator_type& a, T* p, Args&&... args);
74
75 template <class T>
76 static void destroy(allocator_type& a, T* p);
77
78 static size_type max_size(const allocator_type& a);
79
80 static allocator_type
81 select_on_container_copy_construction(const allocator_type& a);
82};
83
84template <>
85class allocator<void>
86{
87public:
88 typedef void* pointer;
89 typedef const void* const_pointer;
90 typedef void value_type;
91
92 template <class _Up> struct rebind {typedef allocator<_Up> other;};
93};
94
95template <class T>
96class allocator
97{
98public:
99 typedef size_t size_type;
100 typedef ptrdiff_t difference_type;
101 typedef T* pointer;
102 typedef const T* const_pointer;
103 typedef typename add_lvalue_reference<T>::type reference;
104 typedef typename add_lvalue_reference<const T>::type const_reference;
105 typedef T value_type;
106
107 template <class U> struct rebind {typedef allocator<U> other;};
108
109 allocator() throw();
110 allocator(const allocator&) throw();
111 template <class U> allocator(const allocator<U>&) throw();
112 ~allocator() throw();
113 pointer address(reference x) const;
114 const_pointer address(const_reference x) const;
115 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
116 void deallocate(pointer p, size_type n);
117 size_type max_size() const throw();
118 void construct(pointer p, const T& val);
119 void destroy(pointer p);
120};
121
122template <class T, class U>
123bool operator==(const allocator<T>&, const allocator<U>&) throw();
124
125template <class T, class U>
126bool operator!=(const allocator<T>&, const allocator<U>&) throw();
127
128template <class OutputIterator, class T>
129class raw_storage_iterator
130 : public iterator<output_iterator_tag,
131 T, // purposefully not C++03
132 ptrdiff_t, // purposefully not C++03
133 T*, // purposefully not C++03
134 raw_storage_iterator&> // purposefully not C++03
135{
136public:
137 explicit raw_storage_iterator(OutputIterator x);
138 raw_storage_iterator& operator*();
139 raw_storage_iterator& operator=(const T& element);
140 raw_storage_iterator& operator++();
141 raw_storage_iterator operator++(int);
142};
143
144template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n);
145template <class T> void return_temporary_buffer(T* p);
146
147template <class InputIterator, class ForwardIterator>
148ForwardIterator
149uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
150
151template <class ForwardIterator, class T>
152void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
153
154template <class ForwardIterator, class Size, class T>
155void uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
156
157template <class Y> struct auto_ptr_ref {};
158
159template<class X>
160class auto_ptr
161{
162public:
163 typedef X element_type;
164
165 explicit auto_ptr(X* p =0) throw();
166 auto_ptr(auto_ptr&) throw();
167 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
168 auto_ptr& operator=(auto_ptr&) throw();
169 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
170 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
171 ~auto_ptr() throw();
172
173 typename add_lvalue_reference<X>::type operator*() const throw();
174 X* operator->() const throw();
175 X* get() const throw();
176 X* release() throw();
177 void reset(X* p =0) throw();
178
179 auto_ptr(auto_ptr_ref<X>) throw();
180 template<class Y> operator auto_ptr_ref<Y>() throw();
181 template<class Y> operator auto_ptr<Y>() throw();
182};
183
Howard Hinnante92c3d72010-08-19 18:39:17 +0000184template <class T>
185struct default_delete
186{
187 constexpr default_delete();
188 template <class U> default_delete(const default_delete<U>&);
189
190 void operator()(T*) const;
191};
192
193template <class T>
194struct default_delete<T[]>
195{
196 constexpr default_delete();
197 void operator()(T*) const;
198 template <class U> void operator()(U*) const = delete;
199};
200
201template <class T, class D = default_delete<T>> class unique_ptr;
202
203template <class T, class D = default_delete<T>>
204class unique_ptr
205{
206public:
207 typedef see below pointer;
208 typedef T element_type;
209 typedef D deleter_type;
210
211 // constructors
212 constexpr unique_ptr();
213 explicit unique_ptr(pointer p);
214 unique_ptr(pointer p, implementation-defined d1);
215 unique_ptr(pointer p, implementation-defined d2);
216 unique_ptr(unique_ptr&& u);
217 unique_ptr(nullptr_t) : unique_ptr() { }
218 template <class U, class E>
219 unique_ptr(unique_ptr<U, E>&& u);
220 template <class U>
Howard Hinnante92c3d72010-08-19 18:39:17 +0000221 unique_ptr(auto_ptr<U>&& u);
222
223 // destructor
224 ~unique_ptr();
225
226 // assignment
227 unique_ptr& operator=(unique_ptr&& u);
228 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u);
229 unique_ptr& operator=(nullptr_t);
230
231 // observers
232 typename add_lvalue_reference<T>::type operator*() const;
233 pointer operator->() const;
234 pointer get() const;
235 deleter_type& get_deleter();
236 const deleter_type& get_deleter() const;
237 explicit operator bool() const;
238
239 // modifiers
240 pointer release();
241 void reset(pointer p = pointer());
242 void swap(unique_ptr& u);
243};
244
245template <class T, class D>
246class unique_ptr<T[], D>
247{
248public:
249 typedef implementation-defined pointer;
250 typedef T element_type;
251 typedef D deleter_type;
252
253 // constructors
254 constexpr unique_ptr();
255 explicit unique_ptr(pointer p);
256 unique_ptr(pointer p, implementation-defined d);
257 unique_ptr(pointer p, implementation-defined d);
258 unique_ptr(unique_ptr&& u);
259 unique_ptr(nullptr_t) : unique_ptr() { }
260
261 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000262 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000263
264 // assignment
265 unique_ptr& operator=(unique_ptr&& u);
266 unique_ptr& operator=(nullptr_t);
267
268 // observers
269 T& operator[](size_t i) const;
270 pointer get() const;
271 deleter_type& get_deleter();
272 const deleter_type& get_deleter() const;
273 explicit operator bool() const;
274
275 // modifiers
276 pointer release();
277 void reset(pointer p = pointer());
278 void reset(nullptr_t);
279 template <class U> void reset(U) = delete;
280 void swap(unique_ptr& u);
281};
282
283template <class T, class D>
284 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y);
285
286template <class T1, class D1, class T2, class D2>
287 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
288template <class T1, class D1, class T2, class D2>
289 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
290template <class T1, class D1, class T2, class D2>
291 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
292template <class T1, class D1, class T2, class D2>
293 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
294template <class T1, class D1, class T2, class D2>
295 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
296template <class T1, class D1, class T2, class D2>
297 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
298
299class bad_weak_ptr
300 : public std::exception
301{
302 bad_weak_ptr();
303};
304
305template<class T>
306class shared_ptr
307{
308public:
309 typedef T element_type;
310
311 // constructors:
312 constexpr shared_ptr();
313 template<class Y> explicit shared_ptr(Y* p);
314 template<class Y, class D> shared_ptr(Y* p, D d);
315 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
316 template <class D> shared_ptr(nullptr_t p, D d);
317 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
318 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p);
319 shared_ptr(const shared_ptr& r);
320 template<class Y> shared_ptr(const shared_ptr<Y>& r);
321 shared_ptr(shared_ptr&& r);
322 template<class Y> shared_ptr(shared_ptr<Y>&& r);
323 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
324 template<class Y> shared_ptr(auto_ptr<Y>&& r);
325 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
326 shared_ptr(nullptr_t) : shared_ptr() { }
327
328 // destructor:
329 ~shared_ptr();
330
331 // assignment:
332 shared_ptr& operator=(const shared_ptr& r);
333 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r);
334 shared_ptr& operator=(shared_ptr&& r);
335 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
336 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
337 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
338
339 // modifiers:
340 void swap(shared_ptr& r);
341 void reset();
342 template<class Y> void reset(Y* p);
343 template<class Y, class D> void reset(Y* p, D d);
344 template<class Y, class D, class A> void reset(Y* p, D d, A a);
345
346 // observers: T* get() const;
347 T& operator*() const;
348 T* operator->() const;
349 long use_count() const;
350 bool unique() const;
351 explicit operator bool() const;
352 template<class U> bool owner_before(shared_ptr<U> const& b) const;
353 template<class U> bool owner_before(weak_ptr<U> const& b) const;
354};
355
356// shared_ptr comparisons:
357template<class T, class U>
358 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b);
359template<class T, class U>
360 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b);
361template<class T, class U>
362 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b);
363template<class T, class U>
364 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b);
365template<class T, class U>
366 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b);
367template<class T, class U>
368 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b);
369
370// shared_ptr specialized algorithms:
371template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b);
372
373// shared_ptr casts:
374template<class T, class U>
375 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r);
376template<class T, class U>
377 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r);
378template<class T, class U>
379 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r);
380
381// shared_ptr I/O:
382template<class E, class T, class Y>
383 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
384
385// shared_ptr get_deleter:
386template<class D, class T> D* get_deleter(shared_ptr<T> const& p);
387
388template<class T, class... Args>
389 shared_ptr<T> make_shared(Args&&... args);
390template<class T, class A, class... Args>
391 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
392
393template<class T>
394class weak_ptr
395{
396public:
397 typedef T element_type;
398
399 // constructors
400 constexpr weak_ptr();
401 template<class Y> weak_ptr(shared_ptr<Y> const& r);
402 weak_ptr(weak_ptr const& r);
403 template<class Y> weak_ptr(weak_ptr<Y> const& r);
404
405 // destructor
406 ~weak_ptr();
407
408 // assignment
409 weak_ptr& operator=(weak_ptr const& r);
410 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r);
411 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r);
412
413 // modifiers
414 void swap(weak_ptr& r);
415 void reset();
416
417 // observers
418 long use_count() const;
419 bool expired() const;
420 shared_ptr<T> lock() const;
421 template<class U> bool owner_before(shared_ptr<U> const& b);
422 template<class U> bool owner_before(weak_ptr<U> const& b);
423};
424
425// weak_ptr specialized algorithms:
426template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b);
427
428// class owner_less:
429template<class T> struct owner_less;
430
431template<class T>
432struct owner_less<shared_ptr<T>>
433 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
434{
435 typedef bool result_type;
436 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
437 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
438 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
439};
440
441template<class T>
442struct owner_less<weak_ptr<T>>
443 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
444{
445 typedef bool result_type;
446 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
447 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
448 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
449};
450
451template<class T>
452class enable_shared_from_this
453{
454protected:
455 constexpr enable_shared_from_this();
456 enable_shared_from_this(enable_shared_from_this const&);
457 enable_shared_from_this& operator=(enable_shared_from_this const&);
458 ~enable_shared_from_this();
459public:
460 shared_ptr<T> shared_from_this();
461 shared_ptr<T const> shared_from_this() const;
462};
463
464template<class T>
465 bool atomic_is_lock_free(const shared_ptr<T>* p);
466template<class T>
467 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
468template<class T>
469 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
470template<class T>
471 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
472template<class T>
473 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
474template<class T>
475 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
476template<class T>
477 shared_ptr<T>
478 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
479template<class T>
480 bool
481 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
482template<class T>
483 bool
484 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
485template<class T>
486 bool
487 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
488 shared_ptr<T> w, memory_order success,
489 memory_order failure);
490template<class T>
491 bool
492 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
493 shared_ptr<T> w, memory_order success,
494 memory_order failure);
495// Hash support
496template <class T> struct hash;
497template <class T, class D> struct hash<unique_ptr<T, D> >;
498template <class T> struct hash<shared_ptr<T> >;
499
500// Pointer safety
501enum class pointer_safety { relaxed, preferred, strict };
502void declare_reachable(void *p);
503template <class T> T *undeclare_reachable(T *p);
504void declare_no_pointers(char *p, size_t n);
505void undeclare_no_pointers(char *p, size_t n);
506pointer_safety get_pointer_safety();
507
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
509
510} // std
511
512*/
513
514#include <__config>
515#include <type_traits>
516#include <typeinfo>
517#include <cstddef>
518#include <cstdint>
519#include <new>
520#include <utility>
521#include <limits>
522#include <iterator>
523#include <__functional_base>
524#if defined(_LIBCPP_NO_EXCEPTIONS)
525 #include <cassert>
526#endif
527
528#pragma GCC system_header
529
530_LIBCPP_BEGIN_NAMESPACE_STD
531
532// allocator_arg_t
533
Howard Hinnant82894812010-09-22 16:48:34 +0000534struct _LIBCPP_VISIBLE allocator_arg_t { };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000535
536extern const allocator_arg_t allocator_arg;
537
538// addressof
539
540template <class _Tp>
541inline _LIBCPP_INLINE_VISIBILITY
542_Tp*
543addressof(_Tp& __x)
544{
545 return (_Tp*)&(char&)__x;
546}
547
548template <class _Tp> class allocator;
549
550template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000551class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552{
553public:
554 typedef void* pointer;
555 typedef const void* const_pointer;
556 typedef void value_type;
557
558 template <class _Up> struct rebind {typedef allocator<_Up> other;};
559};
560
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561// pointer_traits
562
563template <class _Tp>
564struct __has_element_type
565{
566private:
567 struct __two {char _; char __;};
568 template <class _Up> static __two __test(...);
569 template <class _Up> static char __test(typename _Up::element_type* = 0);
570public:
571 static const bool value = sizeof(__test<_Tp>(0)) == 1;
572};
573
574template <class _Ptr, bool = __has_element_type<_Ptr>::value>
575struct __pointer_traits_element_type;
576
577template <class _Ptr>
578struct __pointer_traits_element_type<_Ptr, true>
579{
580 typedef typename _Ptr::element_type type;
581};
582
583#ifndef _LIBCPP_HAS_NO_VARIADICS
584
585template <template <class, class...> class _Sp, class _Tp, class ..._Args>
586struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
587{
588 typedef typename _Sp<_Tp, _Args...>::element_type type;
589};
590
591template <template <class, class...> class _Sp, class _Tp, class ..._Args>
592struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
593{
594 typedef _Tp type;
595};
596
Howard Hinnant324bb032010-08-22 00:02:43 +0000597#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598
599template <template <class> class _Sp, class _Tp>
600struct __pointer_traits_element_type<_Sp<_Tp>, true>
601{
602 typedef typename _Sp<_Tp>::element_type type;
603};
604
605template <template <class> class _Sp, class _Tp>
606struct __pointer_traits_element_type<_Sp<_Tp>, false>
607{
608 typedef _Tp type;
609};
610
611template <template <class, class> class _Sp, class _Tp, class _A0>
612struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
613{
614 typedef typename _Sp<_Tp, _A0>::element_type type;
615};
616
617template <template <class, class> class _Sp, class _Tp, class _A0>
618struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
619{
620 typedef _Tp type;
621};
622
623template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
624struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
625{
626 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
627};
628
629template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
630struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
631{
632 typedef _Tp type;
633};
634
635template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
636 class _A1, class _A2>
637struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
638{
639 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
640};
641
642template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
643 class _A1, class _A2>
644struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
645{
646 typedef _Tp type;
647};
648
Howard Hinnant324bb032010-08-22 00:02:43 +0000649#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650
651template <class _Tp>
652struct __has_difference_type
653{
654private:
655 struct __two {char _; char __;};
656 template <class _Up> static __two __test(...);
657 template <class _Up> static char __test(typename _Up::difference_type* = 0);
658public:
659 static const bool value = sizeof(__test<_Tp>(0)) == 1;
660};
661
662template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
663struct __pointer_traits_difference_type
664{
665 typedef ptrdiff_t type;
666};
667
668template <class _Ptr>
669struct __pointer_traits_difference_type<_Ptr, true>
670{
671 typedef typename _Ptr::difference_type type;
672};
673
674template <class _Tp, class _Up>
675struct __has_rebind
676{
677private:
678 struct __two {char _; char __;};
679 template <class _Xp> static __two __test(...);
680 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
681public:
682 static const bool value = sizeof(__test<_Tp>(0)) == 1;
683};
684
685template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
686struct __pointer_traits_rebind
687{
688#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
689 typedef typename _Tp::template rebind<_Up> type;
690#else
691 typedef typename _Tp::template rebind<_Up>::other type;
692#endif
693};
694
695#ifndef _LIBCPP_HAS_NO_VARIADICS
696
697template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
698struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
699{
700#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
701 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
702#else
703 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
704#endif
705};
706
707template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
708struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
709{
710 typedef _Sp<_Up, _Args...> type;
711};
712
Howard Hinnant324bb032010-08-22 00:02:43 +0000713#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714
715template <template <class> class _Sp, class _Tp, class _Up>
716struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
717{
718#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
719 typedef typename _Sp<_Tp>::template rebind<_Up> type;
720#else
721 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
722#endif
723};
724
725template <template <class> class _Sp, class _Tp, class _Up>
726struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
727{
728 typedef _Sp<_Up> type;
729};
730
731template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
732struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
733{
734#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
735 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
736#else
737 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
738#endif
739};
740
741template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
742struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
743{
744 typedef _Sp<_Up, _A0> type;
745};
746
747template <template <class, class, class> class _Sp, class _Tp, class _A0,
748 class _A1, class _Up>
749struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
750{
751#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
752 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
753#else
754 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
755#endif
756};
757
758template <template <class, class, class> class _Sp, class _Tp, class _A0,
759 class _A1, class _Up>
760struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
761{
762 typedef _Sp<_Up, _A0, _A1> type;
763};
764
765template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
766 class _A1, class _A2, class _Up>
767struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
768{
769#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
770 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
771#else
772 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
773#endif
774};
775
776template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
777 class _A1, class _A2, class _Up>
778struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
779{
780 typedef _Sp<_Up, _A0, _A1, _A2> type;
781};
782
Howard Hinnant324bb032010-08-22 00:02:43 +0000783#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784
785template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000786struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787{
788 typedef _Ptr pointer;
789 typedef typename __pointer_traits_element_type<pointer>::type element_type;
790 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
791
792#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
793 template <class _Up> using rebind = __pointer_traits_rebind<pointer, _Up>::type;
794#else
795 template <class _Up> struct rebind
796 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000797#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798
799private:
800 struct __nat {};
801public:
Howard Hinnant82894812010-09-22 16:48:34 +0000802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000803 static pointer pointer_to(typename conditional<is_void<element_type>::value,
804 __nat, element_type>::type& __r)
805 {return pointer::pointer_to(__r);}
806};
807
808template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000809struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810{
811 typedef _Tp* pointer;
812 typedef _Tp element_type;
813 typedef ptrdiff_t difference_type;
814
815#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
816 template <class _Up> using rebind = _Up*;
817#else
818 template <class _Up> struct rebind {typedef _Up* other;};
819#endif
820
821private:
822 struct __nat {};
823public:
Howard Hinnant82894812010-09-22 16:48:34 +0000824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825 static pointer pointer_to(typename conditional<is_void<element_type>::value,
826 __nat, element_type>::type& __r)
827 {return _STD::addressof(__r);}
828};
829
830// allocator_traits
831
832namespace __has_pointer_type_imp
833{
834 template <class _Up> static __two test(...);
835 template <class _Up> static char test(typename _Up::pointer* = 0);
836}
837
838template <class _Tp>
839struct __has_pointer_type
840 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
841{
842};
843
844namespace __pointer_type_imp
845{
846
847template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
848struct __pointer_type
849{
850 typedef typename _Dp::pointer type;
851};
852
853template <class _Tp, class _Dp>
854struct __pointer_type<_Tp, _Dp, false>
855{
856 typedef _Tp* type;
857};
858
859}
860
861template <class _Tp, class _Dp>
862struct __pointer_type
863{
864 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
865};
866
867template <class _Tp>
868struct __has_const_pointer
869{
870private:
871 struct __two {char _; char __;};
872 template <class _Up> static __two __test(...);
873 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
874public:
875 static const bool value = sizeof(__test<_Tp>(0)) == 1;
876};
877
878template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
879struct __const_pointer
880{
881 typedef typename _Alloc::const_pointer type;
882};
883
884template <class _Tp, class _Ptr, class _Alloc>
885struct __const_pointer<_Tp, _Ptr, _Alloc, false>
886{
887#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
888 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
889#else
890 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
891#endif
892};
893
894template <class _Tp>
895struct __has_void_pointer
896{
897private:
898 struct __two {char _; char __;};
899 template <class _Up> static __two __test(...);
900 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
901public:
902 static const bool value = sizeof(__test<_Tp>(0)) == 1;
903};
904
905template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
906struct __void_pointer
907{
908 typedef typename _Alloc::void_pointer type;
909};
910
911template <class _Ptr, class _Alloc>
912struct __void_pointer<_Ptr, _Alloc, false>
913{
914#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
915 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
916#else
917 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
918#endif
919};
920
921template <class _Tp>
922struct __has_const_void_pointer
923{
924private:
925 struct __two {char _; char __;};
926 template <class _Up> static __two __test(...);
927 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
928public:
929 static const bool value = sizeof(__test<_Tp>(0)) == 1;
930};
931
932template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
933struct __const_void_pointer
934{
935 typedef typename _Alloc::const_void_pointer type;
936};
937
938template <class _Ptr, class _Alloc>
939struct __const_void_pointer<_Ptr, _Alloc, false>
940{
941#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
942 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
943#else
944 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
945#endif
946};
947
948template <class _T>
949inline _LIBCPP_INLINE_VISIBILITY
950_T*
951__to_raw_pointer(_T* __p)
952{
953 return __p;
954}
955
956template <class _Pointer>
957inline _LIBCPP_INLINE_VISIBILITY
958typename pointer_traits<_Pointer>::element_type*
959__to_raw_pointer(_Pointer __p)
960{
961 return _STD::__to_raw_pointer(__p.operator->());
962}
963
964template <class _Tp>
965struct __has_size_type
966{
967private:
968 struct __two {char _; char __;};
969 template <class _Up> static __two __test(...);
970 template <class _Up> static char __test(typename _Up::size_type* = 0);
971public:
972 static const bool value = sizeof(__test<_Tp>(0)) == 1;
973};
974
975template <class _Alloc, bool = __has_size_type<_Alloc>::value>
976struct __size_type
977{
978 typedef size_t type;
979};
980
981template <class _Alloc>
982struct __size_type<_Alloc, true>
983{
984 typedef typename _Alloc::size_type type;
985};
986
987template <class _Tp>
988struct __has_propagate_on_container_copy_assignment
989{
990private:
991 struct __two {char _; char __;};
992 template <class _Up> static __two __test(...);
993 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
994public:
995 static const bool value = sizeof(__test<_Tp>(0)) == 1;
996};
997
998template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
999struct __propagate_on_container_copy_assignment
1000{
1001 typedef false_type type;
1002};
1003
1004template <class _Alloc>
1005struct __propagate_on_container_copy_assignment<_Alloc, true>
1006{
1007 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1008};
1009
1010template <class _Tp>
1011struct __has_propagate_on_container_move_assignment
1012{
1013private:
1014 struct __two {char _; char __;};
1015 template <class _Up> static __two __test(...);
1016 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1017public:
1018 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1019};
1020
1021template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1022struct __propagate_on_container_move_assignment
1023{
1024 typedef false_type type;
1025};
1026
1027template <class _Alloc>
1028struct __propagate_on_container_move_assignment<_Alloc, true>
1029{
1030 typedef typename _Alloc::propagate_on_container_move_assignment type;
1031};
1032
1033template <class _Tp>
1034struct __has_propagate_on_container_swap
1035{
1036private:
1037 struct __two {char _; char __;};
1038 template <class _Up> static __two __test(...);
1039 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1040public:
1041 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1042};
1043
1044template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1045struct __propagate_on_container_swap
1046{
1047 typedef false_type type;
1048};
1049
1050template <class _Alloc>
1051struct __propagate_on_container_swap<_Alloc, true>
1052{
1053 typedef typename _Alloc::propagate_on_container_swap type;
1054};
1055
1056template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1057struct __has_rebind_other
1058{
1059private:
1060 struct __two {char _; char __;};
1061 template <class _Xp> static __two __test(...);
1062 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1063public:
1064 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1065};
1066
1067template <class _Tp, class _Up>
1068struct __has_rebind_other<_Tp, _Up, false>
1069{
1070 static const bool value = false;
1071};
1072
1073template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1074struct __allocator_traits_rebind
1075{
1076 typedef typename _Tp::template rebind<_Up>::other type;
1077};
1078
1079#ifndef _LIBCPP_HAS_NO_VARIADICS
1080
1081template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1082struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1083{
1084 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1085};
1086
1087template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1088struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1089{
1090 typedef _Alloc<_Up, _Args...> type;
1091};
1092
Howard Hinnant324bb032010-08-22 00:02:43 +00001093#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094
1095template <template <class> class _Alloc, class _Tp, class _Up>
1096struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1097{
1098 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1099};
1100
1101template <template <class> class _Alloc, class _Tp, class _Up>
1102struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1103{
1104 typedef _Alloc<_Up> type;
1105};
1106
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1108struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1109{
1110 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1111};
1112
1113template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1114struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1115{
1116 typedef _Alloc<_Up, _A0> type;
1117};
1118
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1120 class _A1, class _Up>
1121struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1122{
1123 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1124};
1125
1126template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1127 class _A1, class _Up>
1128struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1129{
1130 typedef _Alloc<_Up, _A0, _A1> type;
1131};
1132
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001133template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1134 class _A1, class _A2, class _Up>
1135struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1136{
1137 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1138};
1139
1140template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1141 class _A1, class _A2, class _Up>
1142struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1143{
1144 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1145};
1146
Howard Hinnant324bb032010-08-22 00:02:43 +00001147#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148
1149#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1150
1151template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1152auto
1153__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1154 -> decltype(__a.allocate(__sz, __p), true_type());
1155
1156template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1157auto
1158__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1159 -> false_type;
1160
1161template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1162struct __has_allocate_hint
1163 : integral_constant<bool,
1164 is_same<
1165 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1166 declval<_SizeType>(),
1167 declval<_ConstVoidPtr>())),
1168 true_type>::value>
1169{
1170};
1171
Howard Hinnant324bb032010-08-22 00:02:43 +00001172#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173
1174template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1175struct __has_allocate_hint
1176 : true_type
1177{
1178};
1179
Howard Hinnant324bb032010-08-22 00:02:43 +00001180#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181
1182#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1183
1184template <class _Alloc, class _Tp, class ..._Args>
1185decltype(_STD::declval<_Alloc>().construct(_STD::declval<_Tp*>(),
1186 _STD::declval<_Args>()...),
1187 true_type())
1188__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1189
1190template <class _Alloc, class _Pointer, class ..._Args>
1191false_type
1192__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1193
1194template <class _Alloc, class _Pointer, class ..._Args>
1195struct __has_construct
1196 : integral_constant<bool,
1197 is_same<
1198 decltype(__has_construct_test(declval<_Alloc>(),
1199 declval<_Pointer>(),
1200 declval<_Args>()...)),
1201 true_type>::value>
1202{
1203};
1204
1205template <class _Alloc, class _Pointer>
1206auto
1207__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1208 -> decltype(__a.destroy(__p), true_type());
1209
1210template <class _Alloc, class _Pointer>
1211auto
1212__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1213 -> false_type;
1214
1215template <class _Alloc, class _Pointer>
1216struct __has_destroy
1217 : integral_constant<bool,
1218 is_same<
1219 decltype(__has_destroy_test(declval<_Alloc>(),
1220 declval<_Pointer>())),
1221 true_type>::value>
1222{
1223};
1224
1225template <class _Alloc>
1226auto
1227__has_max_size_test(_Alloc&& __a)
1228 -> decltype(__a.max_size(), true_type());
1229
1230template <class _Alloc>
1231auto
1232__has_max_size_test(const volatile _Alloc& __a)
1233 -> false_type;
1234
1235template <class _Alloc>
1236struct __has_max_size
1237 : integral_constant<bool,
1238 is_same<
1239 decltype(__has_max_size_test(declval<_Alloc&>())),
1240 true_type>::value>
1241{
1242};
1243
1244template <class _Alloc>
1245auto
1246__has_select_on_container_copy_construction_test(_Alloc&& __a)
1247 -> decltype(__a.select_on_container_copy_construction(), true_type());
1248
1249template <class _Alloc>
1250auto
1251__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1252 -> false_type;
1253
1254template <class _Alloc>
1255struct __has_select_on_container_copy_construction
1256 : integral_constant<bool,
1257 is_same<
1258 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1259 true_type>::value>
1260{
1261};
1262
Howard Hinnant324bb032010-08-22 00:02:43 +00001263#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264
1265#ifndef _LIBCPP_HAS_NO_VARIADICS
1266
1267template <class _Alloc, class _Pointer, class ..._Args>
1268struct __has_construct
1269 : false_type
1270{
1271};
1272
Howard Hinnant324bb032010-08-22 00:02:43 +00001273#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274
1275template <class _Alloc, class _Pointer>
1276struct __has_destroy
1277 : false_type
1278{
1279};
1280
1281template <class _Alloc>
1282struct __has_max_size
1283 : true_type
1284{
1285};
1286
1287template <class _Alloc>
1288struct __has_select_on_container_copy_construction
1289 : false_type
1290{
1291};
1292
Howard Hinnant324bb032010-08-22 00:02:43 +00001293#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294
1295template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001296struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297{
1298 typedef _Alloc allocator_type;
1299 typedef typename allocator_type::value_type value_type;
1300
1301 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1302 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1303 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1304 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1305
1306 typedef typename __pointer_traits_difference_type<allocator_type>::type difference_type;
1307 typedef typename __size_type<allocator_type>::type size_type;
1308
1309 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1310 propagate_on_container_copy_assignment;
1311 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1312 propagate_on_container_move_assignment;
1313 typedef typename __propagate_on_container_swap<allocator_type>::type
1314 propagate_on_container_swap;
1315
1316#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1317 template <class _Tp> using rebind_alloc =
1318 __allocator_traits_rebind<allocator_type, _Tp>::type;
1319 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001320#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 template <class _Tp> struct rebind_alloc
1322 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1323 template <class _Tp> struct rebind_traits
1324 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001325#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326
Howard Hinnant82894812010-09-22 16:48:34 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001328 static pointer allocate(allocator_type& __a, size_type __n)
1329 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1332 {return allocate(__a, __n, __hint,
1333 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1334
Howard Hinnant82894812010-09-22 16:48:34 +00001335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336 static void deallocate(allocator_type& __a, pointer __p, size_type __n)
1337 {__a.deallocate(__p, __n);}
1338
1339#ifndef _LIBCPP_HAS_NO_VARIADICS
1340 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1343 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
1344 __a, __p, _STD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001345#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 static void construct(allocator_type& __a, _Tp* __p)
1349 {
1350 ::new ((void*)__p) _Tp();
1351 }
1352 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1355 {
1356 ::new ((void*)__p) _Tp(__a0);
1357 }
1358 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1361 const _A1& __a1)
1362 {
1363 ::new ((void*)__p) _Tp(__a0, __a1);
1364 }
1365 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1368 const _A1& __a1, const _A2& __a2)
1369 {
1370 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1371 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001372#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001373
1374 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376 static void destroy(allocator_type& __a, _Tp* __p)
1377 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1378
Howard Hinnant82894812010-09-22 16:48:34 +00001379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380 static size_type max_size(const allocator_type& __a)
1381 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1382
Howard Hinnant82894812010-09-22 16:48:34 +00001383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001384 static allocator_type
1385 select_on_container_copy_construction(const allocator_type& __a)
1386 {return select_on_container_copy_construction(
1387 __has_select_on_container_copy_construction<const allocator_type>(),
1388 __a);}
1389
1390private:
1391
Howard Hinnant82894812010-09-22 16:48:34 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 static pointer allocate(allocator_type& __a, size_type __n,
1394 const_void_pointer __hint, true_type)
1395 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 static pointer allocate(allocator_type& __a, size_type __n,
1398 const_void_pointer __hint, false_type)
1399 {return __a.allocate(__n);}
1400
1401#ifndef _LIBCPP_HAS_NO_VARIADICS
1402 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1405 {__a.construct(__p, _STD::forward<_Args>(__args)...);}
1406 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1409 {
1410 ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
1411 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001412#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413
1414 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1417 {__a.destroy(__p);}
1418 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420 static void __destroy(false_type, allocator_type&, _Tp* __p)
1421 {
1422 __p->~_Tp();
1423 }
1424
Howard Hinnant82894812010-09-22 16:48:34 +00001425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426 static size_type __max_size(true_type, const allocator_type& __a)
1427 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429 static size_type __max_size(false_type, const allocator_type&)
1430 {return numeric_limits<size_type>::max();}
1431
Howard Hinnant82894812010-09-22 16:48:34 +00001432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433 static allocator_type
1434 select_on_container_copy_construction(true_type, const allocator_type& __a)
1435 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437 static allocator_type
1438 select_on_container_copy_construction(false_type, const allocator_type& __a)
1439 {return __a;}
1440};
1441
1442// uses_allocator
1443
1444template <class _Tp>
1445struct __has_allocator_type
1446{
1447private:
1448 struct __two {char _; char __;};
1449 template <class _Up> static __two __test(...);
1450 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1451public:
1452 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1453};
1454
1455template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1456struct __uses_allocator
1457 : public integral_constant<bool,
1458 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1459{
1460};
1461
1462template <class _Tp, class _Alloc>
1463struct __uses_allocator<_Tp, _Alloc, false>
1464 : public false_type
1465{
1466};
1467
1468template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001469struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470 : public __uses_allocator<_Tp, _Alloc>
1471{
1472};
1473
Howard Hinnant726a76f2010-11-16 21:10:23 +00001474#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475
1476// uses-allocator construction
1477
1478template <class _Tp, class _Alloc, class ..._Args>
1479struct __uses_alloc_ctor_imp
1480{
1481 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1482 static const bool __ic =
1483 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1484 static const int value = __ua ? 2 - __ic : 0;
1485};
1486
1487template <class _Tp, class _Alloc, class ..._Args>
1488struct __uses_alloc_ctor
1489 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1490 {};
1491
Howard Hinnant726a76f2010-11-16 21:10:23 +00001492#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493
1494// allocator
1495
1496template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001497class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498{
1499public:
1500 typedef size_t size_type;
1501 typedef ptrdiff_t difference_type;
1502 typedef _Tp* pointer;
1503 typedef const _Tp* const_pointer;
1504 typedef _Tp& reference;
1505 typedef const _Tp& const_reference;
1506 typedef _Tp value_type;
1507
1508 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1509
1510 _LIBCPP_INLINE_VISIBILITY allocator() throw() {}
1511 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) throw() {}
1512 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const {return addressof(__x);}
1513 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const {return addressof(__x);}
1514 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1515 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1516 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) {::operator delete((void*)__p);}
1517 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001518#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 template <class _Up, class... _Args>
1520 _LIBCPP_INLINE_VISIBILITY
1521 void
1522 construct(_Up* __p, _Args&&... __args)
1523 {
1524 ::new((void*)__p) _Up(_STD::forward<_Args>(__args)...);
1525 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001526#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527 _LIBCPP_INLINE_VISIBILITY
1528 void
1529 construct(pointer __p)
1530 {
1531 ::new((void*)__p) _Tp();
1532 }
1533 template <class _A0>
1534 _LIBCPP_INLINE_VISIBILITY
1535 typename enable_if
1536 <
1537 !is_convertible<_A0, __rv<_A0> >::value,
1538 void
1539 >::type
1540 construct(pointer __p, _A0& __a0)
1541 {
1542 ::new((void*)__p) _Tp(__a0);
1543 }
1544 template <class _A0>
1545 _LIBCPP_INLINE_VISIBILITY
1546 typename enable_if
1547 <
1548 !is_convertible<_A0, __rv<_A0> >::value,
1549 void
1550 >::type
1551 construct(pointer __p, const _A0& __a0)
1552 {
1553 ::new((void*)__p) _Tp(__a0);
1554 }
1555 template <class _A0>
1556 _LIBCPP_INLINE_VISIBILITY
1557 typename enable_if
1558 <
1559 is_convertible<_A0, __rv<_A0> >::value,
1560 void
1561 >::type
1562 construct(pointer __p, _A0 __a0)
1563 {
1564 ::new((void*)__p) _Tp(_STD::move(__a0));
1565 }
1566 template <class _A0, class _A1>
1567 _LIBCPP_INLINE_VISIBILITY
1568 void
1569 construct(pointer __p, _A0& __a0, _A1& __a1)
1570 {
1571 ::new((void*)__p) _Tp(__a0, __a1);
1572 }
1573 template <class _A0, class _A1>
1574 _LIBCPP_INLINE_VISIBILITY
1575 void
1576 construct(pointer __p, const _A0& __a0, _A1& __a1)
1577 {
1578 ::new((void*)__p) _Tp(__a0, __a1);
1579 }
1580 template <class _A0, class _A1>
1581 _LIBCPP_INLINE_VISIBILITY
1582 void
1583 construct(pointer __p, _A0& __a0, const _A1& __a1)
1584 {
1585 ::new((void*)__p) _Tp(__a0, __a1);
1586 }
1587 template <class _A0, class _A1>
1588 _LIBCPP_INLINE_VISIBILITY
1589 void
1590 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1591 {
1592 ::new((void*)__p) _Tp(__a0, __a1);
1593 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001594#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1596};
1597
1598template <class _Tp, class _Up>
1599inline _LIBCPP_INLINE_VISIBILITY
1600bool operator==(const allocator<_Tp>&, const allocator<_Up>&) throw() {return true;}
1601
1602template <class _Tp, class _Up>
1603inline _LIBCPP_INLINE_VISIBILITY
1604bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) throw() {return false;}
1605
1606template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001607class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608 : public iterator<output_iterator_tag,
1609 _Tp, // purposefully not C++03
1610 ptrdiff_t, // purposefully not C++03
1611 _Tp*, // purposefully not C++03
1612 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1613{
1614private:
1615 _OutputIterator __x_;
1616public:
1617 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1618 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1619 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1620 {::new(&*__x_) _Tp(__element); return *this;}
1621 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1622 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1623 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1624};
1625
1626template <class _Tp>
1627pair<_Tp*, ptrdiff_t>
1628get_temporary_buffer(ptrdiff_t __n)
1629{
1630 pair<_Tp*, ptrdiff_t> __r(0, 0);
1631 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1632 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1633 / sizeof(_Tp);
1634 if (__n > __m)
1635 __n = __m;
1636 while (__n > 0)
1637 {
1638 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1639 if (__r.first)
1640 {
1641 __r.second = __n;
1642 break;
1643 }
1644 __n /= 2;
1645 }
1646 return __r;
1647}
1648
1649template <class _Tp>
1650inline _LIBCPP_INLINE_VISIBILITY
1651void return_temporary_buffer(_Tp* __p) {::operator delete(__p);}
1652
1653template <class _Tp>
1654struct auto_ptr_ref
1655{
1656 _Tp* __ptr_;
1657};
1658
1659template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001660class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661{
1662private:
1663 _Tp* __ptr_;
1664public:
1665 typedef _Tp element_type;
1666
1667 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1668 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1669 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1670 : __ptr_(__p.release()) {}
1671 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1672 {reset(__p.release()); return *this;}
1673 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1674 {reset(__p.release()); return *this;}
1675 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1676 {reset(__p.__ptr_); return *this;}
1677 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1678
1679 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1680 {return *__ptr_;}
1681 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1682 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1683 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1684 {
1685 _Tp* __t = __ptr_;
1686 __ptr_ = 0;
1687 return __t;
1688 }
1689 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1690 {
1691 if (__ptr_ != __p)
1692 delete __ptr_;
1693 __ptr_ = __p;
1694 }
1695
1696 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1697 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1698 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1699 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1700 {return auto_ptr<_Up>(release());}
1701};
1702
1703template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001704class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705{
1706public:
1707 typedef void element_type;
1708};
1709
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1711 typename remove_cv<_T2>::type>::value,
1712 bool = is_empty<_T1>::value,
1713 bool = is_empty<_T2>::value>
1714struct __libcpp_compressed_pair_switch;
1715
1716template <class _T1, class _T2, bool IsSame>
1717struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1718
1719template <class _T1, class _T2, bool IsSame>
1720struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1721
1722template <class _T1, class _T2, bool IsSame>
1723struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1724
1725template <class _T1, class _T2>
1726struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1727
1728template <class _T1, class _T2>
1729struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1730
1731template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1732class __libcpp_compressed_pair_imp;
1733
1734template <class _T1, class _T2>
1735class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1736{
1737private:
1738 _T1 __first_;
1739 _T2 __second_;
1740public:
1741 typedef _T1 _T1_param;
1742 typedef _T2 _T2_param;
1743
1744 typedef typename remove_reference<_T1>::type& _T1_reference;
1745 typedef typename remove_reference<_T2>::type& _T2_reference;
1746
1747 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1748 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1749
1750 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1751 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1752 : __first_(_STD::forward<_T1_param>(__t1)) {}
1753 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1754 : __second_(_STD::forward<_T2_param>(__t2)) {}
1755 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1756 : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1757
Howard Hinnant73d21a42010-09-04 23:28:19 +00001758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1760 : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001761#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762
1763 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1764 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1765
1766 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1767 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1768
1769 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1770 {
1771 using _STD::swap;
1772 swap(__first_, __x.__first_);
1773 swap(__second_, __x.__second_);
1774 }
1775};
1776
1777template <class _T1, class _T2>
1778class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1779 : private _T1
1780{
1781private:
1782 _T2 __second_;
1783public:
1784 typedef _T1 _T1_param;
1785 typedef _T2 _T2_param;
1786
1787 typedef _T1& _T1_reference;
1788 typedef typename remove_reference<_T2>::type& _T2_reference;
1789
1790 typedef const _T1& _T1_const_reference;
1791 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1792
1793 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1794 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1795 : _T1(_STD::forward<_T1_param>(__t1)) {}
1796 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1797 : __second_(_STD::forward<_T2_param>(__t2)) {}
1798 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1799 : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1800
Howard Hinnant73d21a42010-09-04 23:28:19 +00001801#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1803 : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001804#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805
1806 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1807 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1808
1809 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1810 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1811
1812 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1813 {
1814 using _STD::swap;
1815 swap(__second_, __x.__second_);
1816 }
1817};
1818
1819template <class _T1, class _T2>
1820class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1821 : private _T2
1822{
1823private:
1824 _T1 __first_;
1825public:
1826 typedef _T1 _T1_param;
1827 typedef _T2 _T2_param;
1828
1829 typedef typename remove_reference<_T1>::type& _T1_reference;
1830 typedef _T2& _T2_reference;
1831
1832 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1833 typedef const _T2& _T2_const_reference;
1834
1835 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1836 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1837 : __first_(_STD::forward<_T1_param>(__t1)) {}
1838 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1839 : _T2(_STD::forward<_T2_param>(__t2)) {}
1840 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1841 : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1842
Howard Hinnant73d21a42010-09-04 23:28:19 +00001843#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1845 : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001846#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847
1848 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1849 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1850
1851 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1852 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1853
1854 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1855 {
1856 using _STD::swap;
1857 swap(__first_, __x.__first_);
1858 }
1859};
1860
1861template <class _T1, class _T2>
1862class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1863 : private _T1,
1864 private _T2
1865{
1866public:
1867 typedef _T1 _T1_param;
1868 typedef _T2 _T2_param;
1869
1870 typedef _T1& _T1_reference;
1871 typedef _T2& _T2_reference;
1872
1873 typedef const _T1& _T1_const_reference;
1874 typedef const _T2& _T2_const_reference;
1875
1876 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1877 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1878 : _T1(_STD::forward<_T1_param>(__t1)) {}
1879 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1880 : _T2(_STD::forward<_T2_param>(__t2)) {}
1881 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1882 : _T1(_STD::forward<_T1_param>(__t1)), _T2(_STD::forward<_T2_param>(__t2)) {}
1883
Howard Hinnant73d21a42010-09-04 23:28:19 +00001884#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1886 : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001887#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888
1889 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1890 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1891
1892 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1893 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1894
1895 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1896 {
1897 }
1898};
1899
1900template <class _T1, class _T2>
1901class __compressed_pair
1902 : private __libcpp_compressed_pair_imp<_T1, _T2>
1903{
1904 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
1905public:
1906 typedef typename base::_T1_param _T1_param;
1907 typedef typename base::_T2_param _T2_param;
1908
1909 typedef typename base::_T1_reference _T1_reference;
1910 typedef typename base::_T2_reference _T2_reference;
1911
1912 typedef typename base::_T1_const_reference _T1_const_reference;
1913 typedef typename base::_T2_const_reference _T2_const_reference;
1914
1915 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
1916 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
1917 : base(_STD::forward<_T1_param>(__t1)) {}
1918 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
1919 : base(_STD::forward<_T2_param>(__t2)) {}
1920 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
1921 : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
1922
Howard Hinnant73d21a42010-09-04 23:28:19 +00001923#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924 __compressed_pair(__compressed_pair&& __p)
1925 : base(_STD::move(__p)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001926#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927
1928 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return base::first();}
1929 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return base::first();}
1930
1931 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return base::second();}
1932 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return base::second();}
1933
1934 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x) {base::swap(__x);}
1935};
1936
1937template <class _T1, class _T2>
1938inline _LIBCPP_INLINE_VISIBILITY
1939void
1940swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1941 {__x.swap(__y);}
1942
1943template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001944struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945{
1946 _LIBCPP_INLINE_VISIBILITY default_delete() {}
1947 template <class _Up>
1948 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
1949 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) {}
1950 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1951 {
1952 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1953 delete __ptr;
1954 }
1955};
1956
1957template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001958struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959{
1960 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1961 {
1962 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1963 delete [] __ptr;
1964 }
1965private:
1966 template <class _Up> void operator() (_Up*) const;
1967};
1968
1969template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00001970class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001971{
1972public:
1973 typedef _Tp element_type;
1974 typedef _Dp deleter_type;
1975 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1976private:
1977 __compressed_pair<pointer, deleter_type> __ptr_;
1978
Howard Hinnant73d21a42010-09-04 23:28:19 +00001979#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980 unique_ptr(const unique_ptr&);
1981 unique_ptr& operator=(const unique_ptr&);
1982 template <class _Up, class _Ep>
1983 unique_ptr(const unique_ptr<_Up, _Ep>&);
1984 template <class _Up, class _Ep>
1985 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001986#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987 unique_ptr(unique_ptr&);
1988 template <class _Up, class _Ep>
1989 unique_ptr(unique_ptr<_Up, _Ep>&);
1990 unique_ptr& operator=(unique_ptr&);
1991 template <class _Up, class _Ep>
1992 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001993#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994
1995 struct __nat {int __for_bool_;};
1996
1997 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
1998 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
1999public:
2000 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2001 : __ptr_(pointer())
2002 {
2003 static_assert(!is_pointer<deleter_type>::value,
2004 "unique_ptr constructed with null function pointer deleter");
2005 }
2006 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2007 : __ptr_(pointer())
2008 {
2009 static_assert(!is_pointer<deleter_type>::value,
2010 "unique_ptr constructed with null function pointer deleter");
2011 }
2012 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2013 : __ptr_(_STD::move(__p))
2014 {
2015 static_assert(!is_pointer<deleter_type>::value,
2016 "unique_ptr constructed with null function pointer deleter");
2017 }
2018
Howard Hinnant73d21a42010-09-04 23:28:19 +00002019#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2021 is_reference<deleter_type>::value,
2022 deleter_type,
2023 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2024 : __ptr_(__p, __d) {}
2025
2026 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
2027 : __ptr_(__p, _STD::move(__d))
2028 {
2029 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2030 }
2031 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2032 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2033 template <class _Up, class _Ep>
2034 _LIBCPP_INLINE_VISIBILITY
2035 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2036 typename enable_if
2037 <
2038 !is_array<_Up>::value &&
2039 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2040 is_convertible<_Ep, deleter_type>::value &&
2041 (
2042 !is_reference<deleter_type>::value ||
2043 is_same<deleter_type, _Ep>::value
2044 ),
2045 __nat
2046 >::type = __nat())
2047 : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2048
2049 template <class _Up>
2050 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2051 typename enable_if<
2052 is_convertible<_Up*, _Tp*>::value &&
2053 is_same<_Dp, default_delete<_Tp> >::value,
2054 __nat
2055 >::type = __nat())
2056 : __ptr_(__p.release())
2057 {
2058 }
2059
2060 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2061 {
2062 reset(__u.release());
2063 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2064 return *this;
2065 }
2066
2067 template <class _Up, class _Ep>
2068 _LIBCPP_INLINE_VISIBILITY
2069 typename enable_if
2070 <
2071 !is_array<_Up>::value,
2072 unique_ptr&
2073 >::type
2074 operator=(unique_ptr<_Up, _Ep>&& __u)
2075 {
2076 reset(__u.release());
2077 __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2078 return *this;
2079 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002080#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081
2082 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2083 {
2084 return __rv<unique_ptr>(*this);
2085 }
2086
2087 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2088 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2089
2090 template <class _Up, class _Ep>
2091 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2092 {
2093 reset(__u.release());
2094 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2095 return *this;
2096 }
2097
2098 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2099 : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2100
2101 template <class _Up>
2102 _LIBCPP_INLINE_VISIBILITY
2103 typename enable_if<
2104 is_convertible<_Up*, _Tp*>::value &&
2105 is_same<_Dp, default_delete<_Tp> >::value,
2106 unique_ptr&
2107 >::type
2108 operator=(auto_ptr<_Up> __p)
2109 {reset(__p.release()); return *this;}
2110
Howard Hinnant73d21a42010-09-04 23:28:19 +00002111#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2113
2114 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2115 {
2116 reset();
2117 return *this;
2118 }
2119
2120 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2121 {return *__ptr_.first();}
2122 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_.first();}
2123 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2124 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2125 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2126 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2127
2128 _LIBCPP_INLINE_VISIBILITY pointer release()
2129 {
2130 pointer __t = __ptr_.first();
2131 __ptr_.first() = pointer();
2132 return __t;
2133 }
2134
2135 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2136 {
2137 pointer __tmp = __ptr_.first();
2138 __ptr_.first() = __p;
2139 if (__tmp)
2140 __ptr_.second()(__tmp);
2141 }
2142
2143 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2144};
2145
2146template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002147class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148{
2149public:
2150 typedef _Tp element_type;
2151 typedef _Dp deleter_type;
2152 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2153private:
2154 __compressed_pair<pointer, deleter_type> __ptr_;
2155
Howard Hinnant73d21a42010-09-04 23:28:19 +00002156#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157 unique_ptr(const unique_ptr&);
2158 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002159#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160 unique_ptr(unique_ptr&);
2161 template <class _Up>
2162 unique_ptr(unique_ptr<_Up>&);
2163 unique_ptr& operator=(unique_ptr&);
2164 template <class _Up>
2165 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002166#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167
2168 struct __nat {int __for_bool_;};
2169
2170 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2171 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2172public:
2173 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2174 : __ptr_(pointer())
2175 {
2176 static_assert(!is_pointer<deleter_type>::value,
2177 "unique_ptr constructed with null function pointer deleter");
2178 }
2179 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2180 : __ptr_(pointer())
2181 {
2182 static_assert(!is_pointer<deleter_type>::value,
2183 "unique_ptr constructed with null function pointer deleter");
2184 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002185#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186 template <class _P,
2187 class = typename enable_if<is_same<_P, pointer>::value>::type
2188 >
2189 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p)
2190 : __ptr_(__p)
2191 {
2192 static_assert(!is_pointer<deleter_type>::value,
2193 "unique_ptr constructed with null function pointer deleter");
2194 }
2195
2196 template <class _P,
2197 class = typename enable_if<is_same<_P, pointer>::value>::type
2198 >
2199 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2200 is_reference<deleter_type>::value,
2201 deleter_type,
2202 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2203 : __ptr_(__p, __d) {}
2204
2205 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2206 is_reference<deleter_type>::value,
2207 deleter_type,
2208 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2209 : __ptr_(pointer(), __d) {}
2210
2211 template <class _P,
2212 class = typename enable_if<is_same<_P, pointer>::value ||
2213 is_same<_P, nullptr_t>::value>::type
2214 >
2215 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
2216 : __ptr_(__p, _STD::move(__d))
2217 {
2218 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2219 }
2220
2221 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
2222 : __ptr_(pointer(), _STD::move(__d))
2223 {
2224 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2225 }
2226
2227 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2228 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2229
2230 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2231 {
2232 reset(__u.release());
2233 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2234 return *this;
2235 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002236#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237
2238 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2239 : __ptr_(__p)
2240 {
2241 static_assert(!is_pointer<deleter_type>::value,
2242 "unique_ptr constructed with null function pointer deleter");
2243 }
2244
2245 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2246 : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2247
2248 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2249 : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2250
2251 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2252 {
2253 return __rv<unique_ptr>(*this);
2254 }
2255
2256 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2257 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2258
2259 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2260 {
2261 reset(__u->release());
2262 __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2263 return *this;
2264 }
2265
Howard Hinnant73d21a42010-09-04 23:28:19 +00002266#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002267 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2268
2269 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2270 {
2271 reset();
2272 return *this;
2273 }
2274
2275 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2276 {return __ptr_.first()[__i];}
2277 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2278 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2279 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2280 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2281
2282 _LIBCPP_INLINE_VISIBILITY pointer release()
2283 {
2284 pointer __t = __ptr_.first();
2285 __ptr_.first() = pointer();
2286 return __t;
2287 }
2288
Howard Hinnant73d21a42010-09-04 23:28:19 +00002289#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290 template <class _P,
2291 class = typename enable_if<is_same<_P, pointer>::value>::type
2292 >
2293 _LIBCPP_INLINE_VISIBILITY void reset(_P __p)
2294 {
2295 pointer __tmp = __ptr_.first();
2296 __ptr_.first() = __p;
2297 if (__tmp)
2298 __ptr_.second()(__tmp);
2299 }
2300 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t)
2301 {
2302 pointer __tmp = __ptr_.first();
2303 __ptr_.first() = nullptr;
2304 if (__tmp)
2305 __ptr_.second()(__tmp);
2306 }
2307 _LIBCPP_INLINE_VISIBILITY void reset()
2308 {
2309 pointer __tmp = __ptr_.first();
2310 __ptr_.first() = nullptr;
2311 if (__tmp)
2312 __ptr_.second()(__tmp);
2313 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002314#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2316 {
2317 pointer __tmp = __ptr_.first();
2318 __ptr_.first() = __p;
2319 if (__tmp)
2320 __ptr_.second()(__tmp);
2321 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002322#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323
2324 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2325private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002326
Howard Hinnant73d21a42010-09-04 23:28:19 +00002327#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 template <class _Up>
2329 explicit unique_ptr(_Up);
2330 template <class _Up>
2331 unique_ptr(_Up __u,
2332 typename conditional<
2333 is_reference<deleter_type>::value,
2334 deleter_type,
2335 typename add_lvalue_reference<const deleter_type>::type>::type,
2336 typename enable_if
2337 <
2338 is_convertible<_Up, pointer>::value,
2339 __nat
2340 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002341#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002342};
2343
2344template <class _Tp, class _Dp>
2345inline _LIBCPP_INLINE_VISIBILITY
2346void
2347swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) {__x.swap(__y);}
2348
2349template <class _T1, class _D1, class _T2, class _D2>
2350inline _LIBCPP_INLINE_VISIBILITY
2351bool
2352operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2353
2354template <class _T1, class _D1, class _T2, class _D2>
2355inline _LIBCPP_INLINE_VISIBILITY
2356bool
2357operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2358
2359template <class _T1, class _D1, class _T2, class _D2>
2360inline _LIBCPP_INLINE_VISIBILITY
2361bool
2362operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
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 __y < __x;}
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 !(__y < __x);}
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 < __y);}
2378
Howard Hinnant21aefc32010-06-03 16:42:57 +00002379template <class> struct hash;
2380
2381template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002382struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002383 : public unary_function<_Tp*, size_t>
2384{
Howard Hinnant82894812010-09-22 16:48:34 +00002385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00002386 size_t operator()(_Tp* __v) const
2387 {
2388 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2389 return *__p;
2390 }
2391};
2392
2393template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002394struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002395{
2396 typedef unique_ptr<_Tp, _Dp> argument_type;
2397 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00002399 result_type operator()(const argument_type& __ptr) const
2400 {
2401 typedef typename argument_type::pointer pointer;
2402 return hash<pointer>()(__ptr.get());
2403 }
2404};
2405
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406struct __destruct_n
2407{
2408private:
2409 size_t size;
2410
2411 template <class _Tp>
2412 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type)
2413 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2414
2415 template <class _Tp>
2416 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type)
2417 {}
2418
2419 _LIBCPP_INLINE_VISIBILITY void __incr(false_type)
2420 {++size;}
2421 _LIBCPP_INLINE_VISIBILITY void __incr(true_type)
2422 {}
2423
2424 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type)
2425 {size = __s;}
2426 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type)
2427 {}
2428public:
2429 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) : size(__s) {}
2430
2431 template <class _Tp>
2432 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*)
2433 {__incr(integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2434
2435 template <class _Tp>
2436 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*)
2437 {__set(__s, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2438
2439 template <class _Tp>
2440 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p)
2441 {__process(__p, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2442};
2443
2444template <class _Alloc>
2445class __allocator_destructor
2446{
2447 typedef allocator_traits<_Alloc> __alloc_traits;
2448public:
2449 typedef typename __alloc_traits::pointer pointer;
2450 typedef typename __alloc_traits::size_type size_type;
2451private:
2452 _Alloc& __alloc_;
2453 size_type __s_;
2454public:
2455 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2456 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458 void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2459};
2460
2461template <class _InputIterator, class _ForwardIterator>
2462_ForwardIterator
2463uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2464{
2465 __destruct_n __d(0);
2466 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2467 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2468 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2469 ::new(&*__r) value_type(*__f);
2470 __h.release();
2471 return __r;
2472}
2473
2474template <class _InputIterator, class _Size, class _ForwardIterator>
2475_ForwardIterator
2476uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2477{
2478 __destruct_n __d(0);
2479 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2480 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2481 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2482 ::new(&*__r) value_type(*__f);
2483 __h.release();
2484 return __r;
2485}
2486
2487template <class _ForwardIterator, class _Tp>
2488void
2489uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2490{
2491 __destruct_n __d(0);
2492 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2493 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2494 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2495 ::new(&*__f) value_type(__x);
2496 __h.release();
2497}
2498
2499template <class _ForwardIterator, class _Size, class _Tp>
2500void
2501uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2502{
2503 __destruct_n __d(0);
2504 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2505 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2506 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2507 ::new(&*__f) value_type(__x);
2508 __h.release();
2509}
2510
Howard Hinnant82894812010-09-22 16:48:34 +00002511class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512 : public std::exception
2513{
2514public:
2515 virtual ~bad_weak_ptr() throw();
2516 virtual const char* what() const throw();
2517};
2518
2519template<class _Tp> class weak_ptr;
2520
2521class __shared_count
2522{
2523 __shared_count(const __shared_count&);
2524 __shared_count& operator=(const __shared_count&);
2525
2526protected:
2527 long __shared_owners_;
2528 virtual ~__shared_count();
2529private:
2530 virtual void __on_zero_shared() = 0;
2531
2532public:
Howard Hinnant82894812010-09-22 16:48:34 +00002533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534 explicit __shared_count(long __refs = 0)
2535 : __shared_owners_(__refs) {}
2536
2537 void __add_shared();
Howard Hinnant28dbbe02010-11-16 21:33:17 +00002538 bool __release_shared();
Howard Hinnant82894812010-09-22 16:48:34 +00002539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540 long use_count() const {return __shared_owners_ + 1;}
2541};
2542
2543class __shared_weak_count
2544 : private __shared_count
2545{
2546 long __shared_weak_owners_;
2547
2548public:
Howard Hinnant82894812010-09-22 16:48:34 +00002549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550 explicit __shared_weak_count(long __refs = 0)
2551 : __shared_count(__refs),
2552 __shared_weak_owners_(__refs) {}
2553protected:
2554 virtual ~__shared_weak_count();
2555
2556public:
2557 void __add_shared();
2558 void __add_weak();
2559 void __release_shared();
2560 void __release_weak();
Howard Hinnant82894812010-09-22 16:48:34 +00002561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562 long use_count() const {return __shared_count::use_count();}
2563 __shared_weak_count* lock();
2564
Howard Hinnantd4444702010-08-11 17:04:31 +00002565#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002567#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568private:
2569 virtual void __on_zero_shared_weak() = 0;
2570};
2571
2572template <class _Tp, class _Dp, class _Alloc>
2573class __shared_ptr_pointer
2574 : public __shared_weak_count
2575{
2576 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2577public:
Howard Hinnant82894812010-09-22 16:48:34 +00002578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2580 : __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2581
Howard Hinnantd4444702010-08-11 17:04:31 +00002582#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002583 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002584#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585
2586private:
2587 virtual void __on_zero_shared();
2588 virtual void __on_zero_shared_weak();
2589};
2590
Howard Hinnantd4444702010-08-11 17:04:31 +00002591#ifndef _LIBCPP_NO_RTTI
2592
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002593template <class _Tp, class _Dp, class _Alloc>
2594const void*
2595__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
2596{
2597 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2598}
2599
Howard Hinnant324bb032010-08-22 00:02:43 +00002600#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002601
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602template <class _Tp, class _Dp, class _Alloc>
2603void
2604__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
2605{
2606 __data_.first().second()(__data_.first().first());
2607 __data_.first().second().~_Dp();
2608}
2609
2610template <class _Tp, class _Dp, class _Alloc>
2611void
2612__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak()
2613{
2614 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2615 __data_.second().~_Alloc();
2616 __a.deallocate(this, 1);
2617}
2618
2619template <class _Tp, class _Alloc>
2620class __shared_ptr_emplace
2621 : public __shared_weak_count
2622{
2623 __compressed_pair<_Alloc, _Tp> __data_;
2624public:
2625#ifndef _LIBCPP_HAS_NO_VARIADICS
2626
Howard Hinnant82894812010-09-22 16:48:34 +00002627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002628 __shared_ptr_emplace(_Alloc __a)
2629 : __data_(_STD::move(__a)) {}
2630
2631 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2634 : __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2635
2636#else // _LIBCPP_HAS_NO_VARIADICS
2637
Howard Hinnant82894812010-09-22 16:48:34 +00002638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 __shared_ptr_emplace(_Alloc __a)
2640 : __data_(__a) {}
2641
2642 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2645 : __data_(__a, _Tp(__a0)) {}
2646
2647 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2650 : __data_(__a, _Tp(__a0, __a1)) {}
2651
2652 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002654 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2655 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2656
2657#endif // _LIBCPP_HAS_NO_VARIADICS
2658
2659private:
2660 virtual void __on_zero_shared();
2661 virtual void __on_zero_shared_weak();
2662public:
Howard Hinnant82894812010-09-22 16:48:34 +00002663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664 _Tp* get() {return &__data_.second();}
2665};
2666
2667template <class _Tp, class _Alloc>
2668void
2669__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared()
2670{
2671 __data_.second().~_Tp();
2672}
2673
2674template <class _Tp, class _Alloc>
2675void
2676__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak()
2677{
2678 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2679 __data_.first().~_Alloc();
2680 __a.deallocate(this, 1);
2681}
2682
2683template<class _Tp> class enable_shared_from_this;
2684
2685template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002686class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687{
Howard Hinnant324bb032010-08-22 00:02:43 +00002688public:
2689 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690private:
2691 element_type* __ptr_;
2692 __shared_weak_count* __cntrl_;
2693
2694 struct __nat {int __for_bool_;};
2695public:
2696 shared_ptr();
2697 shared_ptr(nullptr_t);
2698 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00002699 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2700 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002701 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2702 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant324bb032010-08-22 00:02:43 +00002703 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002704 shared_ptr(const shared_ptr& __r);
2705 template<class _Yp>
2706 shared_ptr(const shared_ptr<_Yp>& __r,
2707 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002708#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709 shared_ptr(shared_ptr&& __r);
2710 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002711 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002712#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002714 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002715#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002716 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2717#else
2718 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002720#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002721private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002722 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002723public:
2724 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2725 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2726 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2727 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002728#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002729 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2730 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2731 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2732 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002733#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734
2735 ~shared_ptr();
2736
Howard Hinnant324bb032010-08-22 00:02:43 +00002737 shared_ptr& operator=(const shared_ptr& __r);
2738 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002739#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002740 shared_ptr& operator=(shared_ptr&& __r);
2741 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2742 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002743#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002744 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002745#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002746#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002748 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749public:
Howard Hinnant324bb032010-08-22 00:02:43 +00002750 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002751#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002752 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753#endif
2754
2755 void swap(shared_ptr& __r);
2756 void reset();
2757 template<class _Yp> void reset(_Yp* __p);
2758 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2759 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2760
Howard Hinnant82894812010-09-22 16:48:34 +00002761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 element_type* get() const {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764 typename add_lvalue_reference<element_type>::type operator*() const {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766 element_type* operator->() const {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770 bool unique() const {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00002771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772 bool empty() const {return __cntrl_ == 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002774 /*explicit*/ operator bool() const {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002775 template <class _U>
2776 _LIBCPP_INLINE_VISIBILITY
2777 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002779 template <class _U>
2780 _LIBCPP_INLINE_VISIBILITY
2781 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782 {return __cntrl_ < __p.__cntrl_;}
2783
Howard Hinnantd4444702010-08-11 17:04:31 +00002784#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 _Dp* __get_deleter() const
2788 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002789#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790
2791#ifndef _LIBCPP_HAS_NO_VARIADICS
2792
2793 template<class ..._Args>
2794 static
2795 shared_ptr<_Tp>
2796 make_shared(_Args&& ...__args);
2797
2798 template<class _Alloc, class ..._Args>
2799 static
2800 shared_ptr<_Tp>
2801 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2802
2803#else // _LIBCPP_HAS_NO_VARIADICS
2804
2805 static shared_ptr<_Tp> make_shared();
2806
2807 template<class _A0>
2808 static shared_ptr<_Tp> make_shared(_A0&);
2809
2810 template<class _A0, class _A1>
2811 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2812
2813 template<class _A0, class _A1, class _A2>
2814 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2815
2816 template<class _Alloc>
2817 static shared_ptr<_Tp>
2818 allocate_shared(const _Alloc& __a);
2819
2820 template<class _Alloc, class _A0>
2821 static shared_ptr<_Tp>
2822 allocate_shared(const _Alloc& __a, _A0& __a0);
2823
2824 template<class _Alloc, class _A0, class _A1>
2825 static shared_ptr<_Tp>
2826 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2827
2828 template<class _Alloc, class _A0, class _A1, class _A2>
2829 static shared_ptr<_Tp>
2830 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2831
2832#endif // _LIBCPP_HAS_NO_VARIADICS
2833
2834private:
2835
2836 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00002837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838 void
2839 __enable_weak_this(const enable_shared_from_this<_Yp>* __e)
2840 {
2841 if (__e)
2842 __e->__weak_this_ = *this;
2843 }
2844
Howard Hinnant82894812010-09-22 16:48:34 +00002845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846 void __enable_weak_this(const void*) {}
2847
Howard Hinnant82894812010-09-22 16:48:34 +00002848 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
2849 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850};
2851
2852template<class _Tp>
2853inline _LIBCPP_INLINE_VISIBILITY
2854shared_ptr<_Tp>::shared_ptr()
2855 : __ptr_(0),
2856 __cntrl_(0)
2857{
2858}
2859
2860template<class _Tp>
2861inline _LIBCPP_INLINE_VISIBILITY
2862shared_ptr<_Tp>::shared_ptr(nullptr_t)
2863 : __ptr_(0),
2864 __cntrl_(0)
2865{
2866}
2867
2868template<class _Tp>
2869template<class _Yp>
2870shared_ptr<_Tp>::shared_ptr(_Yp* __p)
2871 : __ptr_(__p)
2872{
2873 unique_ptr<_Yp> __hold(__p);
2874 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2875 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
2876 __hold.release();
2877 __enable_weak_this(__p);
2878}
2879
2880template<class _Tp>
2881template<class _Yp, class _Dp>
2882shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
2883 : __ptr_(__p)
2884{
2885#ifndef _LIBCPP_NO_EXCEPTIONS
2886 try
2887 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002888#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002889 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
2890 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
2891 __enable_weak_this(__p);
2892#ifndef _LIBCPP_NO_EXCEPTIONS
2893 }
2894 catch (...)
2895 {
2896 __d(__p);
2897 throw;
2898 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002899#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002900}
2901
2902template<class _Tp>
2903template<class _Dp>
2904shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
2905 : __ptr_(0)
2906{
2907#ifndef _LIBCPP_NO_EXCEPTIONS
2908 try
2909 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002910#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002911 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
2912 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
2913#ifndef _LIBCPP_NO_EXCEPTIONS
2914 }
2915 catch (...)
2916 {
2917 __d(__p);
2918 throw;
2919 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002920#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002921}
2922
2923template<class _Tp>
2924template<class _Yp, class _Dp, class _Alloc>
2925shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
2926 : __ptr_(__p)
2927{
2928#ifndef _LIBCPP_NO_EXCEPTIONS
2929 try
2930 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002931#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002932 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
2933 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2934 typedef __allocator_destructor<_A2> _D2;
2935 _A2 __a2(__a);
2936 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2937 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2938 __cntrl_ = __hold2.release();
2939 __enable_weak_this(__p);
2940#ifndef _LIBCPP_NO_EXCEPTIONS
2941 }
2942 catch (...)
2943 {
2944 __d(__p);
2945 throw;
2946 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002947#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948}
2949
2950template<class _Tp>
2951template<class _Dp, class _Alloc>
2952shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
2953 : __ptr_(0)
2954{
2955#ifndef _LIBCPP_NO_EXCEPTIONS
2956 try
2957 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002958#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
2960 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2961 typedef __allocator_destructor<_A2> _D2;
2962 _A2 __a2(__a);
2963 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2964 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2965 __cntrl_ = __hold2.release();
2966#ifndef _LIBCPP_NO_EXCEPTIONS
2967 }
2968 catch (...)
2969 {
2970 __d(__p);
2971 throw;
2972 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002973#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974}
2975
2976template<class _Tp>
2977template<class _Yp>
2978inline _LIBCPP_INLINE_VISIBILITY
2979shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p)
2980 : __ptr_(__p),
2981 __cntrl_(__r.__cntrl_)
2982{
2983 if (__cntrl_)
2984 __cntrl_->__add_shared();
2985}
2986
2987template<class _Tp>
2988inline _LIBCPP_INLINE_VISIBILITY
2989shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r)
2990 : __ptr_(__r.__ptr_),
2991 __cntrl_(__r.__cntrl_)
2992{
2993 if (__cntrl_)
2994 __cntrl_->__add_shared();
2995}
2996
2997template<class _Tp>
2998template<class _Yp>
2999inline _LIBCPP_INLINE_VISIBILITY
3000shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3001 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3002 : __ptr_(__r.__ptr_),
3003 __cntrl_(__r.__cntrl_)
3004{
3005 if (__cntrl_)
3006 __cntrl_->__add_shared();
3007}
3008
Howard Hinnant73d21a42010-09-04 23:28:19 +00003009#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003010
3011template<class _Tp>
3012inline _LIBCPP_INLINE_VISIBILITY
3013shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r)
3014 : __ptr_(__r.__ptr_),
3015 __cntrl_(__r.__cntrl_)
3016{
3017 __r.__ptr_ = 0;
3018 __r.__cntrl_ = 0;
3019}
3020
3021template<class _Tp>
3022template<class _Yp>
3023inline _LIBCPP_INLINE_VISIBILITY
3024shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3025 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3026 : __ptr_(__r.__ptr_),
3027 __cntrl_(__r.__cntrl_)
3028{
3029 __r.__ptr_ = 0;
3030 __r.__cntrl_ = 0;
3031}
3032
Howard Hinnant73d21a42010-09-04 23:28:19 +00003033#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003034
3035template<class _Tp>
3036template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003037#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3039#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003040shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041#endif
3042 : __ptr_(__r.get())
3043{
3044 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3045 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3046 __enable_weak_this(__r.get());
3047 __r.release();
3048}
3049
3050template<class _Tp>
3051template <class _Yp, class _Dp>
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(unique_ptr<_Yp, _Dp>&& __r,
3054#else
3055shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3056#endif
3057 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3058 : __ptr_(__r.get())
3059{
3060 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3061 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3062 __enable_weak_this(__r.get());
3063 __r.release();
3064}
3065
3066template<class _Tp>
3067template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003068#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003069shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3070#else
3071shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3072#endif
3073 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3074 : __ptr_(__r.get())
3075{
3076 typedef __shared_ptr_pointer<_Yp*,
3077 reference_wrapper<typename remove_reference<_Dp>::type>,
3078 allocator<_Yp> > _CntrlBlk;
3079 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3080 __enable_weak_this(__r.get());
3081 __r.release();
3082}
3083
3084#ifndef _LIBCPP_HAS_NO_VARIADICS
3085
3086template<class _Tp>
3087template<class ..._Args>
3088shared_ptr<_Tp>
3089shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3090{
3091 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3092 typedef allocator<_CntrlBlk> _A2;
3093 typedef __allocator_destructor<_A2> _D2;
3094 _A2 __a2;
3095 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3096 ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3097 shared_ptr<_Tp> __r;
3098 __r.__ptr_ = __hold2.get()->get();
3099 __r.__cntrl_ = __hold2.release();
3100 __r.__enable_weak_this(__r.__ptr_);
3101 return __r;
3102}
3103
3104template<class _Tp>
3105template<class _Alloc, class ..._Args>
3106shared_ptr<_Tp>
3107shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3108{
3109 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3110 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3111 typedef __allocator_destructor<_A2> _D2;
3112 _A2 __a2(__a);
3113 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3114 ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3115 shared_ptr<_Tp> __r;
3116 __r.__ptr_ = __hold2.get()->get();
3117 __r.__cntrl_ = __hold2.release();
3118 __r.__enable_weak_this(__r.__ptr_);
3119 return __r;
3120}
3121
3122#else // _LIBCPP_HAS_NO_VARIADICS
3123
3124template<class _Tp>
3125shared_ptr<_Tp>
3126shared_ptr<_Tp>::make_shared()
3127{
3128 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3129 typedef allocator<_CntrlBlk> _Alloc2;
3130 typedef __allocator_destructor<_Alloc2> _D2;
3131 _Alloc2 __alloc2;
3132 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3133 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3134 shared_ptr<_Tp> __r;
3135 __r.__ptr_ = __hold2.get()->get();
3136 __r.__cntrl_ = __hold2.release();
3137 __r.__enable_weak_this(__r.__ptr_);
3138 return __r;
3139}
3140
3141template<class _Tp>
3142template<class _A0>
3143shared_ptr<_Tp>
3144shared_ptr<_Tp>::make_shared(_A0& __a0)
3145{
3146 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3147 typedef allocator<_CntrlBlk> _Alloc2;
3148 typedef __allocator_destructor<_Alloc2> _D2;
3149 _Alloc2 __alloc2;
3150 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3151 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3152 shared_ptr<_Tp> __r;
3153 __r.__ptr_ = __hold2.get()->get();
3154 __r.__cntrl_ = __hold2.release();
3155 __r.__enable_weak_this(__r.__ptr_);
3156 return __r;
3157}
3158
3159template<class _Tp>
3160template<class _A0, class _A1>
3161shared_ptr<_Tp>
3162shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3163{
3164 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3165 typedef allocator<_CntrlBlk> _Alloc2;
3166 typedef __allocator_destructor<_Alloc2> _D2;
3167 _Alloc2 __alloc2;
3168 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3169 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3170 shared_ptr<_Tp> __r;
3171 __r.__ptr_ = __hold2.get()->get();
3172 __r.__cntrl_ = __hold2.release();
3173 __r.__enable_weak_this(__r.__ptr_);
3174 return __r;
3175}
3176
3177template<class _Tp>
3178template<class _A0, class _A1, class _A2>
3179shared_ptr<_Tp>
3180shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3181{
3182 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3183 typedef allocator<_CntrlBlk> _Alloc2;
3184 typedef __allocator_destructor<_Alloc2> _D2;
3185 _Alloc2 __alloc2;
3186 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3187 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3188 shared_ptr<_Tp> __r;
3189 __r.__ptr_ = __hold2.get()->get();
3190 __r.__cntrl_ = __hold2.release();
3191 __r.__enable_weak_this(__r.__ptr_);
3192 return __r;
3193}
3194
3195template<class _Tp>
3196template<class _Alloc>
3197shared_ptr<_Tp>
3198shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3199{
3200 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3201 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3202 typedef __allocator_destructor<_Alloc2> _D2;
3203 _Alloc2 __alloc2(__a);
3204 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3205 ::new(__hold2.get()) _CntrlBlk(__a);
3206 shared_ptr<_Tp> __r;
3207 __r.__ptr_ = __hold2.get()->get();
3208 __r.__cntrl_ = __hold2.release();
3209 __r.__enable_weak_this(__r.__ptr_);
3210 return __r;
3211}
3212
3213template<class _Tp>
3214template<class _Alloc, class _A0>
3215shared_ptr<_Tp>
3216shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3217{
3218 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3219 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3220 typedef __allocator_destructor<_Alloc2> _D2;
3221 _Alloc2 __alloc2(__a);
3222 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3223 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3224 shared_ptr<_Tp> __r;
3225 __r.__ptr_ = __hold2.get()->get();
3226 __r.__cntrl_ = __hold2.release();
3227 __r.__enable_weak_this(__r.__ptr_);
3228 return __r;
3229}
3230
3231template<class _Tp>
3232template<class _Alloc, class _A0, class _A1>
3233shared_ptr<_Tp>
3234shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3235{
3236 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3237 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3238 typedef __allocator_destructor<_Alloc2> _D2;
3239 _Alloc2 __alloc2(__a);
3240 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3241 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3242 shared_ptr<_Tp> __r;
3243 __r.__ptr_ = __hold2.get()->get();
3244 __r.__cntrl_ = __hold2.release();
3245 __r.__enable_weak_this(__r.__ptr_);
3246 return __r;
3247}
3248
3249template<class _Tp>
3250template<class _Alloc, class _A0, class _A1, class _A2>
3251shared_ptr<_Tp>
3252shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3253{
3254 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3255 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3256 typedef __allocator_destructor<_Alloc2> _D2;
3257 _Alloc2 __alloc2(__a);
3258 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3259 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3260 shared_ptr<_Tp> __r;
3261 __r.__ptr_ = __hold2.get()->get();
3262 __r.__cntrl_ = __hold2.release();
3263 __r.__enable_weak_this(__r.__ptr_);
3264 return __r;
3265}
3266
3267#endif // _LIBCPP_HAS_NO_VARIADICS
3268
3269template<class _Tp>
3270shared_ptr<_Tp>::~shared_ptr()
3271{
3272 if (__cntrl_)
3273 __cntrl_->__release_shared();
3274}
3275
3276template<class _Tp>
3277inline _LIBCPP_INLINE_VISIBILITY
3278shared_ptr<_Tp>&
3279shared_ptr<_Tp>::operator=(const shared_ptr& __r)
3280{
3281 shared_ptr(__r).swap(*this);
3282 return *this;
3283}
3284
3285template<class _Tp>
3286template<class _Yp>
3287inline _LIBCPP_INLINE_VISIBILITY
3288shared_ptr<_Tp>&
3289shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r)
3290{
3291 shared_ptr(__r).swap(*this);
3292 return *this;
3293}
3294
Howard Hinnant73d21a42010-09-04 23:28:19 +00003295#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003296
3297template<class _Tp>
3298inline _LIBCPP_INLINE_VISIBILITY
3299shared_ptr<_Tp>&
3300shared_ptr<_Tp>::operator=(shared_ptr&& __r)
3301{
3302 shared_ptr(_STD::move(__r)).swap(*this);
3303 return *this;
3304}
3305
3306template<class _Tp>
3307template<class _Yp>
3308inline _LIBCPP_INLINE_VISIBILITY
3309shared_ptr<_Tp>&
3310shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3311{
3312 shared_ptr(_STD::move(__r)).swap(*this);
3313 return *this;
3314}
3315
3316template<class _Tp>
3317template<class _Yp>
3318inline _LIBCPP_INLINE_VISIBILITY
3319shared_ptr<_Tp>&
3320shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3321{
3322 shared_ptr(__r).swap(*this);
3323 return *this;
3324}
3325
3326template<class _Tp>
3327template <class _Yp, class _Dp>
3328inline _LIBCPP_INLINE_VISIBILITY
3329shared_ptr<_Tp>&
3330shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3331{
3332 shared_ptr(_STD::move(__r)).swap(*this);
3333 return *this;
3334}
3335
Howard Hinnant73d21a42010-09-04 23:28:19 +00003336#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003337
3338template<class _Tp>
3339template<class _Yp>
3340inline _LIBCPP_INLINE_VISIBILITY
3341shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003342shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003343{
3344 shared_ptr(__r).swap(*this);
3345 return *this;
3346}
3347
3348template<class _Tp>
3349template <class _Yp, class _Dp>
3350inline _LIBCPP_INLINE_VISIBILITY
3351shared_ptr<_Tp>&
3352shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3353{
3354 shared_ptr(_STD::move(__r)).swap(*this);
3355 return *this;
3356}
3357
Howard Hinnant73d21a42010-09-04 23:28:19 +00003358#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003359
3360template<class _Tp>
3361inline _LIBCPP_INLINE_VISIBILITY
3362void
3363shared_ptr<_Tp>::swap(shared_ptr& __r)
3364{
3365 _STD::swap(__ptr_, __r.__ptr_);
3366 _STD::swap(__cntrl_, __r.__cntrl_);
3367}
3368
3369template<class _Tp>
3370inline _LIBCPP_INLINE_VISIBILITY
3371void
3372shared_ptr<_Tp>::reset()
3373{
3374 shared_ptr().swap(*this);
3375}
3376
3377template<class _Tp>
3378template<class _Yp>
3379inline _LIBCPP_INLINE_VISIBILITY
3380void
3381shared_ptr<_Tp>::reset(_Yp* __p)
3382{
3383 shared_ptr(__p).swap(*this);
3384}
3385
3386template<class _Tp>
3387template<class _Yp, class _Dp>
3388inline _LIBCPP_INLINE_VISIBILITY
3389void
3390shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3391{
3392 shared_ptr(__p, __d).swap(*this);
3393}
3394
3395template<class _Tp>
3396template<class _Yp, class _Dp, class _Alloc>
3397inline _LIBCPP_INLINE_VISIBILITY
3398void
3399shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3400{
3401 shared_ptr(__p, __d, __a).swap(*this);
3402}
3403
3404#ifndef _LIBCPP_HAS_NO_VARIADICS
3405
Howard Hinnant324bb032010-08-22 00:02:43 +00003406template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003407inline _LIBCPP_INLINE_VISIBILITY
3408shared_ptr<_Tp>
3409make_shared(_Args&& ...__args)
3410{
3411 return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3412}
3413
Howard Hinnant324bb032010-08-22 00:02:43 +00003414template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415inline _LIBCPP_INLINE_VISIBILITY
3416shared_ptr<_Tp>
3417allocate_shared(const _Alloc& __a, _Args&& ...__args)
3418{
3419 return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3420}
3421
3422#else // _LIBCPP_HAS_NO_VARIADICS
3423
3424template<class _Tp>
3425inline _LIBCPP_INLINE_VISIBILITY
3426shared_ptr<_Tp>
3427make_shared()
3428{
3429 return shared_ptr<_Tp>::make_shared();
3430}
3431
3432template<class _Tp, class _A0>
3433inline _LIBCPP_INLINE_VISIBILITY
3434shared_ptr<_Tp>
3435make_shared(_A0& __a0)
3436{
3437 return shared_ptr<_Tp>::make_shared(__a0);
3438}
3439
3440template<class _Tp, class _A0, class _A1>
3441inline _LIBCPP_INLINE_VISIBILITY
3442shared_ptr<_Tp>
3443make_shared(_A0& __a0, _A1& __a1)
3444{
3445 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3446}
3447
Howard Hinnant324bb032010-08-22 00:02:43 +00003448template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003449inline _LIBCPP_INLINE_VISIBILITY
3450shared_ptr<_Tp>
3451make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3452{
3453 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3454}
3455
3456template<class _Tp, class _Alloc>
3457inline _LIBCPP_INLINE_VISIBILITY
3458shared_ptr<_Tp>
3459allocate_shared(const _Alloc& __a)
3460{
3461 return shared_ptr<_Tp>::allocate_shared(__a);
3462}
3463
3464template<class _Tp, class _Alloc, class _A0>
3465inline _LIBCPP_INLINE_VISIBILITY
3466shared_ptr<_Tp>
3467allocate_shared(const _Alloc& __a, _A0& __a0)
3468{
3469 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3470}
3471
3472template<class _Tp, class _Alloc, class _A0, class _A1>
3473inline _LIBCPP_INLINE_VISIBILITY
3474shared_ptr<_Tp>
3475allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3476{
3477 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3478}
3479
3480template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3481inline _LIBCPP_INLINE_VISIBILITY
3482shared_ptr<_Tp>
3483allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3484{
3485 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3486}
3487
3488#endif // _LIBCPP_HAS_NO_VARIADICS
3489
3490template<class _Tp, class _Up>
3491inline _LIBCPP_INLINE_VISIBILITY
3492bool
3493operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3494{
3495 return __x.get() == __y.get();
3496}
3497
3498template<class _Tp, class _Up>
3499inline _LIBCPP_INLINE_VISIBILITY
3500bool
3501operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3502{
3503 return !(__x == __y);
3504}
3505
3506template<class _Tp, class _Up>
3507inline _LIBCPP_INLINE_VISIBILITY
3508bool
3509operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3510{
3511 return __x.get() < __y.get();
3512}
3513
3514template<class _Tp>
3515inline _LIBCPP_INLINE_VISIBILITY
3516void
3517swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y)
3518{
3519 __x.swap(__y);
3520}
3521
3522template<class _Tp, class _Up>
3523inline _LIBCPP_INLINE_VISIBILITY
3524shared_ptr<_Tp>
3525static_pointer_cast(const shared_ptr<_Up>& __r)
3526{
3527 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3528}
3529
3530template<class _Tp, class _Up>
3531inline _LIBCPP_INLINE_VISIBILITY
3532shared_ptr<_Tp>
3533dynamic_pointer_cast(const shared_ptr<_Up>& __r)
3534{
3535 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3536 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3537}
3538
3539template<class _Tp, class _Up>
3540shared_ptr<_Tp>
3541const_pointer_cast(const shared_ptr<_Up>& __r)
3542{
3543 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3544}
3545
Howard Hinnantd4444702010-08-11 17:04:31 +00003546#ifndef _LIBCPP_NO_RTTI
3547
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003548template<class _Dp, class _Tp>
3549inline _LIBCPP_INLINE_VISIBILITY
3550_Dp*
3551get_deleter(const shared_ptr<_Tp>& __p)
3552{
3553 return __p.template __get_deleter<_Dp>();
3554}
3555
Howard Hinnant324bb032010-08-22 00:02:43 +00003556#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003557
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003559class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003560{
Howard Hinnant324bb032010-08-22 00:02:43 +00003561public:
3562 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563private:
3564 element_type* __ptr_;
3565 __shared_weak_count* __cntrl_;
3566
Howard Hinnant324bb032010-08-22 00:02:43 +00003567public:
3568 weak_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003569 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
3570 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003571 weak_ptr(weak_ptr const& __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003573 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3574
3575 ~weak_ptr();
3576
3577 weak_ptr& operator=(weak_ptr const& __r);
3578 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r);
3579 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r);
3580
3581 void swap(weak_ptr& __r);
3582 void reset();
3583
Howard Hinnant82894812010-09-22 16:48:34 +00003584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003585 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587 bool expired() const {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
Howard Hinnant324bb032010-08-22 00:02:43 +00003588 shared_ptr<_Tp> lock() const;
Howard Hinnant82894812010-09-22 16:48:34 +00003589 template<class _Up>
3590 _LIBCPP_INLINE_VISIBILITY
3591 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003592 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003593 template<class _Up>
3594 _LIBCPP_INLINE_VISIBILITY
3595 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003596 {return __cntrl_ < __r.__cntrl_;}
3597
Howard Hinnant82894812010-09-22 16:48:34 +00003598 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3599 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600};
3601
3602template<class _Tp>
3603inline _LIBCPP_INLINE_VISIBILITY
3604weak_ptr<_Tp>::weak_ptr()
3605 : __ptr_(0),
3606 __cntrl_(0)
3607{
3608}
3609
3610template<class _Tp>
3611inline _LIBCPP_INLINE_VISIBILITY
3612weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r)
3613 : __ptr_(__r.__ptr_),
3614 __cntrl_(__r.__cntrl_)
3615{
3616 if (__cntrl_)
3617 __cntrl_->__add_weak();
3618}
3619
3620template<class _Tp>
3621template<class _Yp>
3622inline _LIBCPP_INLINE_VISIBILITY
3623weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3624 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3625 : __ptr_(__r.__ptr_),
3626 __cntrl_(__r.__cntrl_)
3627{
3628 if (__cntrl_)
3629 __cntrl_->__add_weak();
3630}
3631
3632template<class _Tp>
3633template<class _Yp>
3634inline _LIBCPP_INLINE_VISIBILITY
3635weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3636 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3637 : __ptr_(__r.__ptr_),
3638 __cntrl_(__r.__cntrl_)
3639{
3640 if (__cntrl_)
3641 __cntrl_->__add_weak();
3642}
3643
3644template<class _Tp>
3645weak_ptr<_Tp>::~weak_ptr()
3646{
3647 if (__cntrl_)
3648 __cntrl_->__release_weak();
3649}
3650
3651template<class _Tp>
3652inline _LIBCPP_INLINE_VISIBILITY
3653weak_ptr<_Tp>&
3654weak_ptr<_Tp>::operator=(weak_ptr const& __r)
3655{
3656 weak_ptr(__r).swap(*this);
3657 return *this;
3658}
3659
3660template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003661template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003662inline _LIBCPP_INLINE_VISIBILITY
3663weak_ptr<_Tp>&
3664weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r)
3665{
3666 weak_ptr(__r).swap(*this);
3667 return *this;
3668}
3669
3670template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003671template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003672inline _LIBCPP_INLINE_VISIBILITY
3673weak_ptr<_Tp>&
3674weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r)
3675{
3676 weak_ptr(__r).swap(*this);
3677 return *this;
3678}
3679
3680template<class _Tp>
3681inline _LIBCPP_INLINE_VISIBILITY
3682void
3683weak_ptr<_Tp>::swap(weak_ptr& __r)
3684{
3685 _STD::swap(__ptr_, __r.__ptr_);
3686 _STD::swap(__cntrl_, __r.__cntrl_);
3687}
3688
3689template<class _Tp>
3690inline _LIBCPP_INLINE_VISIBILITY
3691void
3692swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y)
3693{
3694 __x.swap(__y);
3695}
3696
3697template<class _Tp>
3698inline _LIBCPP_INLINE_VISIBILITY
3699void
3700weak_ptr<_Tp>::reset()
3701{
3702 weak_ptr().swap(*this);
3703}
3704
3705template<class _Tp>
3706template<class _Yp>
3707shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3708 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3709 : __ptr_(__r.__ptr_),
3710 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3711{
3712 if (__cntrl_ == 0)
3713#ifndef _LIBCPP_NO_EXCEPTIONS
3714 throw bad_weak_ptr();
3715#else
3716 assert(!"bad_weak_ptr");
3717#endif
3718}
3719
3720template<class _Tp>
3721shared_ptr<_Tp>
3722weak_ptr<_Tp>::lock() const
3723{
3724 shared_ptr<_Tp> __r;
3725 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3726 if (__r.__cntrl_)
3727 __r.__ptr_ = __ptr_;
3728 return __r;
3729}
3730
Howard Hinnant324bb032010-08-22 00:02:43 +00003731template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732
3733template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003734struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003735 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00003736{
3737 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003739 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3740 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003742 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3743 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003745 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3746 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003747};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003748
3749template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003750struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3752{
Howard Hinnant324bb032010-08-22 00:02:43 +00003753 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3756 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3759 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003761 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3762 {return __x.owner_before(__y);}
3763};
3764
3765template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003766class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767{
3768 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00003769protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003771 enable_shared_from_this() {}
Howard Hinnant82894812010-09-22 16:48:34 +00003772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003773 enable_shared_from_this(enable_shared_from_this const&) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775 enable_shared_from_this& operator=(enable_shared_from_this const&) {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00003776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003778public:
Howard Hinnant82894812010-09-22 16:48:34 +00003779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003780 shared_ptr<_Tp> shared_from_this() {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00003781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003782 shared_ptr<_Tp const> shared_from_this() const {return shared_ptr<const _Tp>(__weak_this_);}
3783
3784 template <class _Up> friend class shared_ptr;
3785};
3786
Howard Hinnant21aefc32010-06-03 16:42:57 +00003787template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003788struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003789{
3790 typedef shared_ptr<_Tp> argument_type;
3791 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00003793 result_type operator()(const argument_type& __ptr) const
3794 {
3795 return hash<_Tp*>()(__ptr.get());
3796 }
3797};
3798
Howard Hinnant324bb032010-08-22 00:02:43 +00003799//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00003800struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003801{
3802 enum _
3803 {
3804 relaxed,
3805 preferred,
3806 strict
3807 };
3808
3809 _ __v_;
3810
Howard Hinnant82894812010-09-22 16:48:34 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003812 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003814 operator int() const {return __v_;}
3815};
3816
3817void declare_reachable(void* __p);
3818void declare_no_pointers(char* __p, size_t __n);
3819void undeclare_no_pointers(char* __p, size_t __n);
3820pointer_safety get_pointer_safety();
3821void* __undeclare_reachable(void*);
3822
3823template <class _Tp>
3824inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00003825_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003826undeclare_reachable(_Tp* __p)
3827{
3828 return static_cast<_Tp*>(__undeclare_reachable(__p));
3829}
3830
3831void* align(size_t, size_t, void*&, size_t&);
3832
3833_LIBCPP_END_NAMESPACE_STD
3834
3835#endif // _LIBCPP_MEMORY