blob: 5b2b41dcfe5c89a5768bb18d6ae29bf93b4dab9c [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
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 Hinnant324bb032010-08-22 00:02:43 +0000534struct 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 <>
551class allocator<void>
552{
553public:
554 typedef void* pointer;
555 typedef const void* const_pointer;
556 typedef void value_type;
557
558 template <class _Up> struct rebind {typedef allocator<_Up> other;};
559};
560
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>
786struct pointer_traits
787{
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:
802 static pointer pointer_to(typename conditional<is_void<element_type>::value,
803 __nat, element_type>::type& __r)
804 {return pointer::pointer_to(__r);}
805};
806
807template <class _Tp>
808struct pointer_traits<_Tp*>
809{
810 typedef _Tp* pointer;
811 typedef _Tp element_type;
812 typedef ptrdiff_t difference_type;
813
814#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
815 template <class _Up> using rebind = _Up*;
816#else
817 template <class _Up> struct rebind {typedef _Up* other;};
818#endif
819
820private:
821 struct __nat {};
822public:
823 static pointer pointer_to(typename conditional<is_void<element_type>::value,
824 __nat, element_type>::type& __r)
825 {return _STD::addressof(__r);}
826};
827
828// allocator_traits
829
830namespace __has_pointer_type_imp
831{
832 template <class _Up> static __two test(...);
833 template <class _Up> static char test(typename _Up::pointer* = 0);
834}
835
836template <class _Tp>
837struct __has_pointer_type
838 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
839{
840};
841
842namespace __pointer_type_imp
843{
844
845template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
846struct __pointer_type
847{
848 typedef typename _Dp::pointer type;
849};
850
851template <class _Tp, class _Dp>
852struct __pointer_type<_Tp, _Dp, false>
853{
854 typedef _Tp* type;
855};
856
857}
858
859template <class _Tp, class _Dp>
860struct __pointer_type
861{
862 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
863};
864
865template <class _Tp>
866struct __has_const_pointer
867{
868private:
869 struct __two {char _; char __;};
870 template <class _Up> static __two __test(...);
871 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
872public:
873 static const bool value = sizeof(__test<_Tp>(0)) == 1;
874};
875
876template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
877struct __const_pointer
878{
879 typedef typename _Alloc::const_pointer type;
880};
881
882template <class _Tp, class _Ptr, class _Alloc>
883struct __const_pointer<_Tp, _Ptr, _Alloc, false>
884{
885#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
886 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
887#else
888 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
889#endif
890};
891
892template <class _Tp>
893struct __has_void_pointer
894{
895private:
896 struct __two {char _; char __;};
897 template <class _Up> static __two __test(...);
898 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
899public:
900 static const bool value = sizeof(__test<_Tp>(0)) == 1;
901};
902
903template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
904struct __void_pointer
905{
906 typedef typename _Alloc::void_pointer type;
907};
908
909template <class _Ptr, class _Alloc>
910struct __void_pointer<_Ptr, _Alloc, false>
911{
912#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
913 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
914#else
915 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
916#endif
917};
918
919template <class _Tp>
920struct __has_const_void_pointer
921{
922private:
923 struct __two {char _; char __;};
924 template <class _Up> static __two __test(...);
925 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
926public:
927 static const bool value = sizeof(__test<_Tp>(0)) == 1;
928};
929
930template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
931struct __const_void_pointer
932{
933 typedef typename _Alloc::const_void_pointer type;
934};
935
936template <class _Ptr, class _Alloc>
937struct __const_void_pointer<_Ptr, _Alloc, false>
938{
939#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
940 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
941#else
942 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
943#endif
944};
945
946template <class _T>
947inline _LIBCPP_INLINE_VISIBILITY
948_T*
949__to_raw_pointer(_T* __p)
950{
951 return __p;
952}
953
954template <class _Pointer>
955inline _LIBCPP_INLINE_VISIBILITY
956typename pointer_traits<_Pointer>::element_type*
957__to_raw_pointer(_Pointer __p)
958{
959 return _STD::__to_raw_pointer(__p.operator->());
960}
961
962template <class _Tp>
963struct __has_size_type
964{
965private:
966 struct __two {char _; char __;};
967 template <class _Up> static __two __test(...);
968 template <class _Up> static char __test(typename _Up::size_type* = 0);
969public:
970 static const bool value = sizeof(__test<_Tp>(0)) == 1;
971};
972
973template <class _Alloc, bool = __has_size_type<_Alloc>::value>
974struct __size_type
975{
976 typedef size_t type;
977};
978
979template <class _Alloc>
980struct __size_type<_Alloc, true>
981{
982 typedef typename _Alloc::size_type type;
983};
984
985template <class _Tp>
986struct __has_propagate_on_container_copy_assignment
987{
988private:
989 struct __two {char _; char __;};
990 template <class _Up> static __two __test(...);
991 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
992public:
993 static const bool value = sizeof(__test<_Tp>(0)) == 1;
994};
995
996template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
997struct __propagate_on_container_copy_assignment
998{
999 typedef false_type type;
1000};
1001
1002template <class _Alloc>
1003struct __propagate_on_container_copy_assignment<_Alloc, true>
1004{
1005 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1006};
1007
1008template <class _Tp>
1009struct __has_propagate_on_container_move_assignment
1010{
1011private:
1012 struct __two {char _; char __;};
1013 template <class _Up> static __two __test(...);
1014 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1015public:
1016 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1017};
1018
1019template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1020struct __propagate_on_container_move_assignment
1021{
1022 typedef false_type type;
1023};
1024
1025template <class _Alloc>
1026struct __propagate_on_container_move_assignment<_Alloc, true>
1027{
1028 typedef typename _Alloc::propagate_on_container_move_assignment type;
1029};
1030
1031template <class _Tp>
1032struct __has_propagate_on_container_swap
1033{
1034private:
1035 struct __two {char _; char __;};
1036 template <class _Up> static __two __test(...);
1037 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1038public:
1039 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1040};
1041
1042template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1043struct __propagate_on_container_swap
1044{
1045 typedef false_type type;
1046};
1047
1048template <class _Alloc>
1049struct __propagate_on_container_swap<_Alloc, true>
1050{
1051 typedef typename _Alloc::propagate_on_container_swap type;
1052};
1053
1054template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1055struct __has_rebind_other
1056{
1057private:
1058 struct __two {char _; char __;};
1059 template <class _Xp> static __two __test(...);
1060 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1061public:
1062 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1063};
1064
1065template <class _Tp, class _Up>
1066struct __has_rebind_other<_Tp, _Up, false>
1067{
1068 static const bool value = false;
1069};
1070
1071template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1072struct __allocator_traits_rebind
1073{
1074 typedef typename _Tp::template rebind<_Up>::other type;
1075};
1076
1077#ifndef _LIBCPP_HAS_NO_VARIADICS
1078
1079template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1080struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1081{
1082 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1083};
1084
1085template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1086struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1087{
1088 typedef _Alloc<_Up, _Args...> type;
1089};
1090
Howard Hinnant324bb032010-08-22 00:02:43 +00001091#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092
1093template <template <class> class _Alloc, class _Tp, class _Up>
1094struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1095{
1096 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1097};
1098
1099template <template <class> class _Alloc, class _Tp, class _Up>
1100struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1101{
1102 typedef _Alloc<_Up> type;
1103};
1104
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001105template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1106struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1107{
1108 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1109};
1110
1111template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1112struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1113{
1114 typedef _Alloc<_Up, _A0> type;
1115};
1116
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1118 class _A1, class _Up>
1119struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1120{
1121 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1122};
1123
1124template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1125 class _A1, class _Up>
1126struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1127{
1128 typedef _Alloc<_Up, _A0, _A1> type;
1129};
1130
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1132 class _A1, class _A2, class _Up>
1133struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1134{
1135 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1136};
1137
1138template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1139 class _A1, class _A2, class _Up>
1140struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1141{
1142 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1143};
1144
Howard Hinnant324bb032010-08-22 00:02:43 +00001145#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146
1147#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1148
1149template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1150auto
1151__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1152 -> decltype(__a.allocate(__sz, __p), true_type());
1153
1154template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1155auto
1156__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1157 -> false_type;
1158
1159template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1160struct __has_allocate_hint
1161 : integral_constant<bool,
1162 is_same<
1163 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1164 declval<_SizeType>(),
1165 declval<_ConstVoidPtr>())),
1166 true_type>::value>
1167{
1168};
1169
Howard Hinnant324bb032010-08-22 00:02:43 +00001170#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171
1172template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1173struct __has_allocate_hint
1174 : true_type
1175{
1176};
1177
Howard Hinnant324bb032010-08-22 00:02:43 +00001178#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001179
1180#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1181
1182template <class _Alloc, class _Tp, class ..._Args>
1183decltype(_STD::declval<_Alloc>().construct(_STD::declval<_Tp*>(),
1184 _STD::declval<_Args>()...),
1185 true_type())
1186__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1187
1188template <class _Alloc, class _Pointer, class ..._Args>
1189false_type
1190__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1191
1192template <class _Alloc, class _Pointer, class ..._Args>
1193struct __has_construct
1194 : integral_constant<bool,
1195 is_same<
1196 decltype(__has_construct_test(declval<_Alloc>(),
1197 declval<_Pointer>(),
1198 declval<_Args>()...)),
1199 true_type>::value>
1200{
1201};
1202
1203template <class _Alloc, class _Pointer>
1204auto
1205__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1206 -> decltype(__a.destroy(__p), true_type());
1207
1208template <class _Alloc, class _Pointer>
1209auto
1210__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1211 -> false_type;
1212
1213template <class _Alloc, class _Pointer>
1214struct __has_destroy
1215 : integral_constant<bool,
1216 is_same<
1217 decltype(__has_destroy_test(declval<_Alloc>(),
1218 declval<_Pointer>())),
1219 true_type>::value>
1220{
1221};
1222
1223template <class _Alloc>
1224auto
1225__has_max_size_test(_Alloc&& __a)
1226 -> decltype(__a.max_size(), true_type());
1227
1228template <class _Alloc>
1229auto
1230__has_max_size_test(const volatile _Alloc& __a)
1231 -> false_type;
1232
1233template <class _Alloc>
1234struct __has_max_size
1235 : integral_constant<bool,
1236 is_same<
1237 decltype(__has_max_size_test(declval<_Alloc&>())),
1238 true_type>::value>
1239{
1240};
1241
1242template <class _Alloc>
1243auto
1244__has_select_on_container_copy_construction_test(_Alloc&& __a)
1245 -> decltype(__a.select_on_container_copy_construction(), true_type());
1246
1247template <class _Alloc>
1248auto
1249__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1250 -> false_type;
1251
1252template <class _Alloc>
1253struct __has_select_on_container_copy_construction
1254 : integral_constant<bool,
1255 is_same<
1256 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1257 true_type>::value>
1258{
1259};
1260
Howard Hinnant324bb032010-08-22 00:02:43 +00001261#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262
1263#ifndef _LIBCPP_HAS_NO_VARIADICS
1264
1265template <class _Alloc, class _Pointer, class ..._Args>
1266struct __has_construct
1267 : false_type
1268{
1269};
1270
Howard Hinnant324bb032010-08-22 00:02:43 +00001271#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272
1273template <class _Alloc, class _Pointer>
1274struct __has_destroy
1275 : false_type
1276{
1277};
1278
1279template <class _Alloc>
1280struct __has_max_size
1281 : true_type
1282{
1283};
1284
1285template <class _Alloc>
1286struct __has_select_on_container_copy_construction
1287 : false_type
1288{
1289};
1290
Howard Hinnant324bb032010-08-22 00:02:43 +00001291#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292
1293template <class _Alloc>
1294struct allocator_traits
1295{
1296 typedef _Alloc allocator_type;
1297 typedef typename allocator_type::value_type value_type;
1298
1299 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1300 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1301 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1302 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1303
1304 typedef typename __pointer_traits_difference_type<allocator_type>::type difference_type;
1305 typedef typename __size_type<allocator_type>::type size_type;
1306
1307 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1308 propagate_on_container_copy_assignment;
1309 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1310 propagate_on_container_move_assignment;
1311 typedef typename __propagate_on_container_swap<allocator_type>::type
1312 propagate_on_container_swap;
1313
1314#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1315 template <class _Tp> using rebind_alloc =
1316 __allocator_traits_rebind<allocator_type, _Tp>::type;
1317 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001318#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319 template <class _Tp> struct rebind_alloc
1320 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1321 template <class _Tp> struct rebind_traits
1322 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001323#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324
1325 static pointer allocate(allocator_type& __a, size_type __n)
1326 {return __a.allocate(__n);}
1327 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1328 {return allocate(__a, __n, __hint,
1329 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1330
1331 static void deallocate(allocator_type& __a, pointer __p, size_type __n)
1332 {__a.deallocate(__p, __n);}
1333
1334#ifndef _LIBCPP_HAS_NO_VARIADICS
1335 template <class _Tp, class... _Args>
1336 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1337 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
1338 __a, __p, _STD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001339#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340 template <class _Tp>
1341 static void construct(allocator_type& __a, _Tp* __p)
1342 {
1343 ::new ((void*)__p) _Tp();
1344 }
1345 template <class _Tp, class _A0>
1346 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1347 {
1348 ::new ((void*)__p) _Tp(__a0);
1349 }
1350 template <class _Tp, class _A0, class _A1>
1351 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1352 const _A1& __a1)
1353 {
1354 ::new ((void*)__p) _Tp(__a0, __a1);
1355 }
1356 template <class _Tp, class _A0, class _A1, class _A2>
1357 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1358 const _A1& __a1, const _A2& __a2)
1359 {
1360 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1361 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001362#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363
1364 template <class _Tp>
1365 static void destroy(allocator_type& __a, _Tp* __p)
1366 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1367
1368 static size_type max_size(const allocator_type& __a)
1369 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1370
1371 static allocator_type
1372 select_on_container_copy_construction(const allocator_type& __a)
1373 {return select_on_container_copy_construction(
1374 __has_select_on_container_copy_construction<const allocator_type>(),
1375 __a);}
1376
1377private:
1378
1379 static pointer allocate(allocator_type& __a, size_type __n,
1380 const_void_pointer __hint, true_type)
1381 {return __a.allocate(__n, __hint);}
1382 static pointer allocate(allocator_type& __a, size_type __n,
1383 const_void_pointer __hint, false_type)
1384 {return __a.allocate(__n);}
1385
1386#ifndef _LIBCPP_HAS_NO_VARIADICS
1387 template <class _Tp, class... _Args>
1388 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1389 {__a.construct(__p, _STD::forward<_Args>(__args)...);}
1390 template <class _Tp, class... _Args>
1391 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1392 {
1393 ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
1394 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001395#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396
1397 template <class _Tp>
1398 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1399 {__a.destroy(__p);}
1400 template <class _Tp>
1401 static void __destroy(false_type, allocator_type&, _Tp* __p)
1402 {
1403 __p->~_Tp();
1404 }
1405
1406 static size_type __max_size(true_type, const allocator_type& __a)
1407 {return __a.max_size();}
1408 static size_type __max_size(false_type, const allocator_type&)
1409 {return numeric_limits<size_type>::max();}
1410
1411 static allocator_type
1412 select_on_container_copy_construction(true_type, const allocator_type& __a)
1413 {return __a.select_on_container_copy_construction();}
1414 static allocator_type
1415 select_on_container_copy_construction(false_type, const allocator_type& __a)
1416 {return __a;}
1417};
1418
1419// uses_allocator
1420
1421template <class _Tp>
1422struct __has_allocator_type
1423{
1424private:
1425 struct __two {char _; char __;};
1426 template <class _Up> static __two __test(...);
1427 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1428public:
1429 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1430};
1431
1432template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1433struct __uses_allocator
1434 : public integral_constant<bool,
1435 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1436{
1437};
1438
1439template <class _Tp, class _Alloc>
1440struct __uses_allocator<_Tp, _Alloc, false>
1441 : public false_type
1442{
1443};
1444
1445template <class _Tp, class _Alloc>
1446struct uses_allocator
1447 : public __uses_allocator<_Tp, _Alloc>
1448{
1449};
1450
Howard Hinnant73d21a42010-09-04 23:28:19 +00001451#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452
1453// uses-allocator construction
1454
1455template <class _Tp, class _Alloc, class ..._Args>
1456struct __uses_alloc_ctor_imp
1457{
1458 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1459 static const bool __ic =
1460 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1461 static const int value = __ua ? 2 - __ic : 0;
1462};
1463
1464template <class _Tp, class _Alloc, class ..._Args>
1465struct __uses_alloc_ctor
1466 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1467 {};
1468
Howard Hinnant73d21a42010-09-04 23:28:19 +00001469#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470
1471// allocator
1472
1473template <class _Tp>
1474class allocator
1475{
1476public:
1477 typedef size_t size_type;
1478 typedef ptrdiff_t difference_type;
1479 typedef _Tp* pointer;
1480 typedef const _Tp* const_pointer;
1481 typedef _Tp& reference;
1482 typedef const _Tp& const_reference;
1483 typedef _Tp value_type;
1484
1485 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1486
1487 _LIBCPP_INLINE_VISIBILITY allocator() throw() {}
1488 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) throw() {}
1489 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const {return addressof(__x);}
1490 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const {return addressof(__x);}
1491 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1492 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1493 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) {::operator delete((void*)__p);}
1494 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001495#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 template <class _Up, class... _Args>
1497 _LIBCPP_INLINE_VISIBILITY
1498 void
1499 construct(_Up* __p, _Args&&... __args)
1500 {
1501 ::new((void*)__p) _Up(_STD::forward<_Args>(__args)...);
1502 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001503#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 _LIBCPP_INLINE_VISIBILITY
1505 void
1506 construct(pointer __p)
1507 {
1508 ::new((void*)__p) _Tp();
1509 }
1510 template <class _A0>
1511 _LIBCPP_INLINE_VISIBILITY
1512 typename enable_if
1513 <
1514 !is_convertible<_A0, __rv<_A0> >::value,
1515 void
1516 >::type
1517 construct(pointer __p, _A0& __a0)
1518 {
1519 ::new((void*)__p) _Tp(__a0);
1520 }
1521 template <class _A0>
1522 _LIBCPP_INLINE_VISIBILITY
1523 typename enable_if
1524 <
1525 !is_convertible<_A0, __rv<_A0> >::value,
1526 void
1527 >::type
1528 construct(pointer __p, const _A0& __a0)
1529 {
1530 ::new((void*)__p) _Tp(__a0);
1531 }
1532 template <class _A0>
1533 _LIBCPP_INLINE_VISIBILITY
1534 typename enable_if
1535 <
1536 is_convertible<_A0, __rv<_A0> >::value,
1537 void
1538 >::type
1539 construct(pointer __p, _A0 __a0)
1540 {
1541 ::new((void*)__p) _Tp(_STD::move(__a0));
1542 }
1543 template <class _A0, class _A1>
1544 _LIBCPP_INLINE_VISIBILITY
1545 void
1546 construct(pointer __p, _A0& __a0, _A1& __a1)
1547 {
1548 ::new((void*)__p) _Tp(__a0, __a1);
1549 }
1550 template <class _A0, class _A1>
1551 _LIBCPP_INLINE_VISIBILITY
1552 void
1553 construct(pointer __p, const _A0& __a0, _A1& __a1)
1554 {
1555 ::new((void*)__p) _Tp(__a0, __a1);
1556 }
1557 template <class _A0, class _A1>
1558 _LIBCPP_INLINE_VISIBILITY
1559 void
1560 construct(pointer __p, _A0& __a0, const _A1& __a1)
1561 {
1562 ::new((void*)__p) _Tp(__a0, __a1);
1563 }
1564 template <class _A0, class _A1>
1565 _LIBCPP_INLINE_VISIBILITY
1566 void
1567 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1568 {
1569 ::new((void*)__p) _Tp(__a0, __a1);
1570 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001571#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1573};
1574
1575template <class _Tp, class _Up>
1576inline _LIBCPP_INLINE_VISIBILITY
1577bool operator==(const allocator<_Tp>&, const allocator<_Up>&) throw() {return true;}
1578
1579template <class _Tp, class _Up>
1580inline _LIBCPP_INLINE_VISIBILITY
1581bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) throw() {return false;}
1582
1583template <class _OutputIterator, class _Tp>
1584class raw_storage_iterator
1585 : public iterator<output_iterator_tag,
1586 _Tp, // purposefully not C++03
1587 ptrdiff_t, // purposefully not C++03
1588 _Tp*, // purposefully not C++03
1589 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1590{
1591private:
1592 _OutputIterator __x_;
1593public:
1594 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1595 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1596 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1597 {::new(&*__x_) _Tp(__element); return *this;}
1598 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1599 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1600 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1601};
1602
1603template <class _Tp>
1604pair<_Tp*, ptrdiff_t>
1605get_temporary_buffer(ptrdiff_t __n)
1606{
1607 pair<_Tp*, ptrdiff_t> __r(0, 0);
1608 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1609 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1610 / sizeof(_Tp);
1611 if (__n > __m)
1612 __n = __m;
1613 while (__n > 0)
1614 {
1615 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1616 if (__r.first)
1617 {
1618 __r.second = __n;
1619 break;
1620 }
1621 __n /= 2;
1622 }
1623 return __r;
1624}
1625
1626template <class _Tp>
1627inline _LIBCPP_INLINE_VISIBILITY
1628void return_temporary_buffer(_Tp* __p) {::operator delete(__p);}
1629
1630template <class _Tp>
1631struct auto_ptr_ref
1632{
1633 _Tp* __ptr_;
1634};
1635
1636template<class _Tp>
1637class auto_ptr
1638{
1639private:
1640 _Tp* __ptr_;
1641public:
1642 typedef _Tp element_type;
1643
1644 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1645 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1646 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1647 : __ptr_(__p.release()) {}
1648 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1649 {reset(__p.release()); return *this;}
1650 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1651 {reset(__p.release()); return *this;}
1652 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1653 {reset(__p.__ptr_); return *this;}
1654 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1655
1656 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1657 {return *__ptr_;}
1658 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1659 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1660 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1661 {
1662 _Tp* __t = __ptr_;
1663 __ptr_ = 0;
1664 return __t;
1665 }
1666 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1667 {
1668 if (__ptr_ != __p)
1669 delete __ptr_;
1670 __ptr_ = __p;
1671 }
1672
1673 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1674 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1675 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1676 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1677 {return auto_ptr<_Up>(release());}
1678};
1679
1680template <>
1681class auto_ptr<void>
1682{
1683public:
1684 typedef void element_type;
1685};
1686
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1688 typename remove_cv<_T2>::type>::value,
1689 bool = is_empty<_T1>::value,
1690 bool = is_empty<_T2>::value>
1691struct __libcpp_compressed_pair_switch;
1692
1693template <class _T1, class _T2, bool IsSame>
1694struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1695
1696template <class _T1, class _T2, bool IsSame>
1697struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1698
1699template <class _T1, class _T2, bool IsSame>
1700struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1701
1702template <class _T1, class _T2>
1703struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1704
1705template <class _T1, class _T2>
1706struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1707
1708template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1709class __libcpp_compressed_pair_imp;
1710
1711template <class _T1, class _T2>
1712class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1713{
1714private:
1715 _T1 __first_;
1716 _T2 __second_;
1717public:
1718 typedef _T1 _T1_param;
1719 typedef _T2 _T2_param;
1720
1721 typedef typename remove_reference<_T1>::type& _T1_reference;
1722 typedef typename remove_reference<_T2>::type& _T2_reference;
1723
1724 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1725 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1726
1727 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1728 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1729 : __first_(_STD::forward<_T1_param>(__t1)) {}
1730 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1731 : __second_(_STD::forward<_T2_param>(__t2)) {}
1732 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1733 : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1734
Howard Hinnant73d21a42010-09-04 23:28:19 +00001735#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1737 : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001738#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739
1740 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1741 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1742
1743 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1744 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1745
1746 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1747 {
1748 using _STD::swap;
1749 swap(__first_, __x.__first_);
1750 swap(__second_, __x.__second_);
1751 }
1752};
1753
1754template <class _T1, class _T2>
1755class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1756 : private _T1
1757{
1758private:
1759 _T2 __second_;
1760public:
1761 typedef _T1 _T1_param;
1762 typedef _T2 _T2_param;
1763
1764 typedef _T1& _T1_reference;
1765 typedef typename remove_reference<_T2>::type& _T2_reference;
1766
1767 typedef const _T1& _T1_const_reference;
1768 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1769
1770 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1771 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1772 : _T1(_STD::forward<_T1_param>(__t1)) {}
1773 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1774 : __second_(_STD::forward<_T2_param>(__t2)) {}
1775 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1776 : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1777
Howard Hinnant73d21a42010-09-04 23:28:19 +00001778#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1780 : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001781#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782
1783 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1784 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1785
1786 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1787 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1788
1789 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1790 {
1791 using _STD::swap;
1792 swap(__second_, __x.__second_);
1793 }
1794};
1795
1796template <class _T1, class _T2>
1797class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1798 : private _T2
1799{
1800private:
1801 _T1 __first_;
1802public:
1803 typedef _T1 _T1_param;
1804 typedef _T2 _T2_param;
1805
1806 typedef typename remove_reference<_T1>::type& _T1_reference;
1807 typedef _T2& _T2_reference;
1808
1809 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1810 typedef const _T2& _T2_const_reference;
1811
1812 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1813 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1814 : __first_(_STD::forward<_T1_param>(__t1)) {}
1815 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1816 : _T2(_STD::forward<_T2_param>(__t2)) {}
1817 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1818 : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1819
Howard Hinnant73d21a42010-09-04 23:28:19 +00001820#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1822 : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001823#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824
1825 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1826 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1827
1828 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1829 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1830
1831 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1832 {
1833 using _STD::swap;
1834 swap(__first_, __x.__first_);
1835 }
1836};
1837
1838template <class _T1, class _T2>
1839class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1840 : private _T1,
1841 private _T2
1842{
1843public:
1844 typedef _T1 _T1_param;
1845 typedef _T2 _T2_param;
1846
1847 typedef _T1& _T1_reference;
1848 typedef _T2& _T2_reference;
1849
1850 typedef const _T1& _T1_const_reference;
1851 typedef const _T2& _T2_const_reference;
1852
1853 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1854 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1855 : _T1(_STD::forward<_T1_param>(__t1)) {}
1856 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1857 : _T2(_STD::forward<_T2_param>(__t2)) {}
1858 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1859 : _T1(_STD::forward<_T1_param>(__t1)), _T2(_STD::forward<_T2_param>(__t2)) {}
1860
Howard Hinnant73d21a42010-09-04 23:28:19 +00001861#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1863 : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001864#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865
1866 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1867 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1868
1869 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1870 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1871
1872 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1873 {
1874 }
1875};
1876
1877template <class _T1, class _T2>
1878class __compressed_pair
1879 : private __libcpp_compressed_pair_imp<_T1, _T2>
1880{
1881 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
1882public:
1883 typedef typename base::_T1_param _T1_param;
1884 typedef typename base::_T2_param _T2_param;
1885
1886 typedef typename base::_T1_reference _T1_reference;
1887 typedef typename base::_T2_reference _T2_reference;
1888
1889 typedef typename base::_T1_const_reference _T1_const_reference;
1890 typedef typename base::_T2_const_reference _T2_const_reference;
1891
1892 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
1893 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
1894 : base(_STD::forward<_T1_param>(__t1)) {}
1895 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
1896 : base(_STD::forward<_T2_param>(__t2)) {}
1897 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
1898 : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
1899
Howard Hinnant73d21a42010-09-04 23:28:19 +00001900#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901 __compressed_pair(__compressed_pair&& __p)
1902 : base(_STD::move(__p)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001903#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904
1905 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return base::first();}
1906 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return base::first();}
1907
1908 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return base::second();}
1909 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return base::second();}
1910
1911 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x) {base::swap(__x);}
1912};
1913
1914template <class _T1, class _T2>
1915inline _LIBCPP_INLINE_VISIBILITY
1916void
1917swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1918 {__x.swap(__y);}
1919
1920template <class _Tp>
1921struct default_delete
1922{
1923 _LIBCPP_INLINE_VISIBILITY default_delete() {}
1924 template <class _Up>
1925 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
1926 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) {}
1927 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1928 {
1929 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1930 delete __ptr;
1931 }
1932};
1933
1934template <class _Tp>
1935struct default_delete<_Tp[]>
1936{
1937 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1938 {
1939 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1940 delete [] __ptr;
1941 }
1942private:
1943 template <class _Up> void operator() (_Up*) const;
1944};
1945
1946template <class _Tp, class _Dp = default_delete<_Tp> >
1947class unique_ptr
1948{
1949public:
1950 typedef _Tp element_type;
1951 typedef _Dp deleter_type;
1952 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1953private:
1954 __compressed_pair<pointer, deleter_type> __ptr_;
1955
Howard Hinnant73d21a42010-09-04 23:28:19 +00001956#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 unique_ptr(const unique_ptr&);
1958 unique_ptr& operator=(const unique_ptr&);
1959 template <class _Up, class _Ep>
1960 unique_ptr(const unique_ptr<_Up, _Ep>&);
1961 template <class _Up, class _Ep>
1962 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001963#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964 unique_ptr(unique_ptr&);
1965 template <class _Up, class _Ep>
1966 unique_ptr(unique_ptr<_Up, _Ep>&);
1967 unique_ptr& operator=(unique_ptr&);
1968 template <class _Up, class _Ep>
1969 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001970#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001971
1972 struct __nat {int __for_bool_;};
1973
1974 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
1975 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
1976public:
1977 _LIBCPP_INLINE_VISIBILITY unique_ptr()
1978 : __ptr_(pointer())
1979 {
1980 static_assert(!is_pointer<deleter_type>::value,
1981 "unique_ptr constructed with null function pointer deleter");
1982 }
1983 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
1984 : __ptr_(pointer())
1985 {
1986 static_assert(!is_pointer<deleter_type>::value,
1987 "unique_ptr constructed with null function pointer deleter");
1988 }
1989 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
1990 : __ptr_(_STD::move(__p))
1991 {
1992 static_assert(!is_pointer<deleter_type>::value,
1993 "unique_ptr constructed with null function pointer deleter");
1994 }
1995
Howard Hinnant73d21a42010-09-04 23:28:19 +00001996#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
1998 is_reference<deleter_type>::value,
1999 deleter_type,
2000 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2001 : __ptr_(__p, __d) {}
2002
2003 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
2004 : __ptr_(__p, _STD::move(__d))
2005 {
2006 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2007 }
2008 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2009 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2010 template <class _Up, class _Ep>
2011 _LIBCPP_INLINE_VISIBILITY
2012 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2013 typename enable_if
2014 <
2015 !is_array<_Up>::value &&
2016 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2017 is_convertible<_Ep, deleter_type>::value &&
2018 (
2019 !is_reference<deleter_type>::value ||
2020 is_same<deleter_type, _Ep>::value
2021 ),
2022 __nat
2023 >::type = __nat())
2024 : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2025
2026 template <class _Up>
2027 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2028 typename enable_if<
2029 is_convertible<_Up*, _Tp*>::value &&
2030 is_same<_Dp, default_delete<_Tp> >::value,
2031 __nat
2032 >::type = __nat())
2033 : __ptr_(__p.release())
2034 {
2035 }
2036
2037 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2038 {
2039 reset(__u.release());
2040 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2041 return *this;
2042 }
2043
2044 template <class _Up, class _Ep>
2045 _LIBCPP_INLINE_VISIBILITY
2046 typename enable_if
2047 <
2048 !is_array<_Up>::value,
2049 unique_ptr&
2050 >::type
2051 operator=(unique_ptr<_Up, _Ep>&& __u)
2052 {
2053 reset(__u.release());
2054 __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2055 return *this;
2056 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002057#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058
2059 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2060 {
2061 return __rv<unique_ptr>(*this);
2062 }
2063
2064 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2065 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2066
2067 template <class _Up, class _Ep>
2068 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2069 {
2070 reset(__u.release());
2071 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2072 return *this;
2073 }
2074
2075 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2076 : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2077
2078 template <class _Up>
2079 _LIBCPP_INLINE_VISIBILITY
2080 typename enable_if<
2081 is_convertible<_Up*, _Tp*>::value &&
2082 is_same<_Dp, default_delete<_Tp> >::value,
2083 unique_ptr&
2084 >::type
2085 operator=(auto_ptr<_Up> __p)
2086 {reset(__p.release()); return *this;}
2087
Howard Hinnant73d21a42010-09-04 23:28:19 +00002088#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2090
2091 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2092 {
2093 reset();
2094 return *this;
2095 }
2096
2097 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2098 {return *__ptr_.first();}
2099 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_.first();}
2100 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2101 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2102 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2103 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2104
2105 _LIBCPP_INLINE_VISIBILITY pointer release()
2106 {
2107 pointer __t = __ptr_.first();
2108 __ptr_.first() = pointer();
2109 return __t;
2110 }
2111
2112 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2113 {
2114 pointer __tmp = __ptr_.first();
2115 __ptr_.first() = __p;
2116 if (__tmp)
2117 __ptr_.second()(__tmp);
2118 }
2119
2120 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2121};
2122
2123template <class _Tp, class _Dp>
2124class unique_ptr<_Tp[], _Dp>
2125{
2126public:
2127 typedef _Tp element_type;
2128 typedef _Dp deleter_type;
2129 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2130private:
2131 __compressed_pair<pointer, deleter_type> __ptr_;
2132
Howard Hinnant73d21a42010-09-04 23:28:19 +00002133#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134 unique_ptr(const unique_ptr&);
2135 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002136#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137 unique_ptr(unique_ptr&);
2138 template <class _Up>
2139 unique_ptr(unique_ptr<_Up>&);
2140 unique_ptr& operator=(unique_ptr&);
2141 template <class _Up>
2142 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002143#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144
2145 struct __nat {int __for_bool_;};
2146
2147 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2148 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2149public:
2150 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2151 : __ptr_(pointer())
2152 {
2153 static_assert(!is_pointer<deleter_type>::value,
2154 "unique_ptr constructed with null function pointer deleter");
2155 }
2156 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2157 : __ptr_(pointer())
2158 {
2159 static_assert(!is_pointer<deleter_type>::value,
2160 "unique_ptr constructed with null function pointer deleter");
2161 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002162#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163 template <class _P,
2164 class = typename enable_if<is_same<_P, pointer>::value>::type
2165 >
2166 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p)
2167 : __ptr_(__p)
2168 {
2169 static_assert(!is_pointer<deleter_type>::value,
2170 "unique_ptr constructed with null function pointer deleter");
2171 }
2172
2173 template <class _P,
2174 class = typename enable_if<is_same<_P, pointer>::value>::type
2175 >
2176 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2177 is_reference<deleter_type>::value,
2178 deleter_type,
2179 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2180 : __ptr_(__p, __d) {}
2181
2182 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2183 is_reference<deleter_type>::value,
2184 deleter_type,
2185 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2186 : __ptr_(pointer(), __d) {}
2187
2188 template <class _P,
2189 class = typename enable_if<is_same<_P, pointer>::value ||
2190 is_same<_P, nullptr_t>::value>::type
2191 >
2192 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
2193 : __ptr_(__p, _STD::move(__d))
2194 {
2195 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2196 }
2197
2198 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
2199 : __ptr_(pointer(), _STD::move(__d))
2200 {
2201 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2202 }
2203
2204 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2205 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2206
2207 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2208 {
2209 reset(__u.release());
2210 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2211 return *this;
2212 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002213#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214
2215 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2216 : __ptr_(__p)
2217 {
2218 static_assert(!is_pointer<deleter_type>::value,
2219 "unique_ptr constructed with null function pointer deleter");
2220 }
2221
2222 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2223 : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2224
2225 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2226 : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2227
2228 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2229 {
2230 return __rv<unique_ptr>(*this);
2231 }
2232
2233 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2234 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2235
2236 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2237 {
2238 reset(__u->release());
2239 __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2240 return *this;
2241 }
2242
Howard Hinnant73d21a42010-09-04 23:28:19 +00002243#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2245
2246 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2247 {
2248 reset();
2249 return *this;
2250 }
2251
2252 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2253 {return __ptr_.first()[__i];}
2254 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2255 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2256 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2257 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2258
2259 _LIBCPP_INLINE_VISIBILITY pointer release()
2260 {
2261 pointer __t = __ptr_.first();
2262 __ptr_.first() = pointer();
2263 return __t;
2264 }
2265
Howard Hinnant73d21a42010-09-04 23:28:19 +00002266#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002267 template <class _P,
2268 class = typename enable_if<is_same<_P, pointer>::value>::type
2269 >
2270 _LIBCPP_INLINE_VISIBILITY void reset(_P __p)
2271 {
2272 pointer __tmp = __ptr_.first();
2273 __ptr_.first() = __p;
2274 if (__tmp)
2275 __ptr_.second()(__tmp);
2276 }
2277 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t)
2278 {
2279 pointer __tmp = __ptr_.first();
2280 __ptr_.first() = nullptr;
2281 if (__tmp)
2282 __ptr_.second()(__tmp);
2283 }
2284 _LIBCPP_INLINE_VISIBILITY void reset()
2285 {
2286 pointer __tmp = __ptr_.first();
2287 __ptr_.first() = nullptr;
2288 if (__tmp)
2289 __ptr_.second()(__tmp);
2290 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002291#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2293 {
2294 pointer __tmp = __ptr_.first();
2295 __ptr_.first() = __p;
2296 if (__tmp)
2297 __ptr_.second()(__tmp);
2298 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002299#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300
2301 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2302private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002303
Howard Hinnant73d21a42010-09-04 23:28:19 +00002304#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 template <class _Up>
2306 explicit unique_ptr(_Up);
2307 template <class _Up>
2308 unique_ptr(_Up __u,
2309 typename conditional<
2310 is_reference<deleter_type>::value,
2311 deleter_type,
2312 typename add_lvalue_reference<const deleter_type>::type>::type,
2313 typename enable_if
2314 <
2315 is_convertible<_Up, pointer>::value,
2316 __nat
2317 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002318#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002319};
2320
2321template <class _Tp, class _Dp>
2322inline _LIBCPP_INLINE_VISIBILITY
2323void
2324swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) {__x.swap(__y);}
2325
2326template <class _T1, class _D1, class _T2, class _D2>
2327inline _LIBCPP_INLINE_VISIBILITY
2328bool
2329operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2330
2331template <class _T1, class _D1, class _T2, class _D2>
2332inline _LIBCPP_INLINE_VISIBILITY
2333bool
2334operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2335
2336template <class _T1, class _D1, class _T2, class _D2>
2337inline _LIBCPP_INLINE_VISIBILITY
2338bool
2339operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2340
2341template <class _T1, class _D1, class _T2, class _D2>
2342inline _LIBCPP_INLINE_VISIBILITY
2343bool
2344operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2345
2346template <class _T1, class _D1, class _T2, class _D2>
2347inline _LIBCPP_INLINE_VISIBILITY
2348bool
2349operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2350
2351template <class _T1, class _D1, class _T2, class _D2>
2352inline _LIBCPP_INLINE_VISIBILITY
2353bool
2354operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2355
Howard Hinnant21aefc32010-06-03 16:42:57 +00002356template <class> struct hash;
2357
2358template<class _Tp>
2359struct hash<_Tp*>
2360 : public unary_function<_Tp*, size_t>
2361{
2362 size_t operator()(_Tp* __v) const
2363 {
2364 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2365 return *__p;
2366 }
2367};
2368
2369template <class _Tp, class _Dp>
2370struct hash<unique_ptr<_Tp, _Dp> >
2371{
2372 typedef unique_ptr<_Tp, _Dp> argument_type;
2373 typedef size_t result_type;
2374 result_type operator()(const argument_type& __ptr) const
2375 {
2376 typedef typename argument_type::pointer pointer;
2377 return hash<pointer>()(__ptr.get());
2378 }
2379};
2380
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381struct __destruct_n
2382{
2383private:
2384 size_t size;
2385
2386 template <class _Tp>
2387 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type)
2388 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2389
2390 template <class _Tp>
2391 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type)
2392 {}
2393
2394 _LIBCPP_INLINE_VISIBILITY void __incr(false_type)
2395 {++size;}
2396 _LIBCPP_INLINE_VISIBILITY void __incr(true_type)
2397 {}
2398
2399 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type)
2400 {size = __s;}
2401 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type)
2402 {}
2403public:
2404 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) : size(__s) {}
2405
2406 template <class _Tp>
2407 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*)
2408 {__incr(integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2409
2410 template <class _Tp>
2411 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*)
2412 {__set(__s, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2413
2414 template <class _Tp>
2415 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p)
2416 {__process(__p, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2417};
2418
2419template <class _Alloc>
2420class __allocator_destructor
2421{
2422 typedef allocator_traits<_Alloc> __alloc_traits;
2423public:
2424 typedef typename __alloc_traits::pointer pointer;
2425 typedef typename __alloc_traits::size_type size_type;
2426private:
2427 _Alloc& __alloc_;
2428 size_type __s_;
2429public:
2430 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2431 : __alloc_(__a), __s_(__s) {}
2432 void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2433};
2434
2435template <class _InputIterator, class _ForwardIterator>
2436_ForwardIterator
2437uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2438{
2439 __destruct_n __d(0);
2440 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2441 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2442 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2443 ::new(&*__r) value_type(*__f);
2444 __h.release();
2445 return __r;
2446}
2447
2448template <class _InputIterator, class _Size, class _ForwardIterator>
2449_ForwardIterator
2450uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2451{
2452 __destruct_n __d(0);
2453 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2454 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2455 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2456 ::new(&*__r) value_type(*__f);
2457 __h.release();
2458 return __r;
2459}
2460
2461template <class _ForwardIterator, class _Tp>
2462void
2463uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2464{
2465 __destruct_n __d(0);
2466 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2467 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2468 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2469 ::new(&*__f) value_type(__x);
2470 __h.release();
2471}
2472
2473template <class _ForwardIterator, class _Size, class _Tp>
2474void
2475uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2476{
2477 __destruct_n __d(0);
2478 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2479 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2480 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2481 ::new(&*__f) value_type(__x);
2482 __h.release();
2483}
2484
2485class bad_weak_ptr
2486 : public std::exception
2487{
2488public:
2489 virtual ~bad_weak_ptr() throw();
2490 virtual const char* what() const throw();
2491};
2492
2493template<class _Tp> class weak_ptr;
2494
2495class __shared_count
2496{
2497 __shared_count(const __shared_count&);
2498 __shared_count& operator=(const __shared_count&);
2499
2500protected:
2501 long __shared_owners_;
2502 virtual ~__shared_count();
2503private:
2504 virtual void __on_zero_shared() = 0;
2505
2506public:
2507 explicit __shared_count(long __refs = 0)
2508 : __shared_owners_(__refs) {}
2509
2510 void __add_shared();
2511 void __release_shared();
2512 long use_count() const {return __shared_owners_ + 1;}
2513};
2514
2515class __shared_weak_count
2516 : private __shared_count
2517{
2518 long __shared_weak_owners_;
2519
2520public:
2521 explicit __shared_weak_count(long __refs = 0)
2522 : __shared_count(__refs),
2523 __shared_weak_owners_(__refs) {}
2524protected:
2525 virtual ~__shared_weak_count();
2526
2527public:
2528 void __add_shared();
2529 void __add_weak();
2530 void __release_shared();
2531 void __release_weak();
2532 long use_count() const {return __shared_count::use_count();}
2533 __shared_weak_count* lock();
2534
Howard Hinnantd4444702010-08-11 17:04:31 +00002535#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002537#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538private:
2539 virtual void __on_zero_shared_weak() = 0;
2540};
2541
2542template <class _Tp, class _Dp, class _Alloc>
2543class __shared_ptr_pointer
2544 : public __shared_weak_count
2545{
2546 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2547public:
2548 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2549 : __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2550
Howard Hinnantd4444702010-08-11 17:04:31 +00002551#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002553#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554
2555private:
2556 virtual void __on_zero_shared();
2557 virtual void __on_zero_shared_weak();
2558};
2559
Howard Hinnantd4444702010-08-11 17:04:31 +00002560#ifndef _LIBCPP_NO_RTTI
2561
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562template <class _Tp, class _Dp, class _Alloc>
2563const void*
2564__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
2565{
2566 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2567}
2568
Howard Hinnant324bb032010-08-22 00:02:43 +00002569#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002570
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002571template <class _Tp, class _Dp, class _Alloc>
2572void
2573__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
2574{
2575 __data_.first().second()(__data_.first().first());
2576 __data_.first().second().~_Dp();
2577}
2578
2579template <class _Tp, class _Dp, class _Alloc>
2580void
2581__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak()
2582{
2583 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2584 __data_.second().~_Alloc();
2585 __a.deallocate(this, 1);
2586}
2587
2588template <class _Tp, class _Alloc>
2589class __shared_ptr_emplace
2590 : public __shared_weak_count
2591{
2592 __compressed_pair<_Alloc, _Tp> __data_;
2593public:
2594#ifndef _LIBCPP_HAS_NO_VARIADICS
2595
2596 __shared_ptr_emplace(_Alloc __a)
2597 : __data_(_STD::move(__a)) {}
2598
2599 template <class ..._Args>
2600 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2601 : __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2602
2603#else // _LIBCPP_HAS_NO_VARIADICS
2604
2605 __shared_ptr_emplace(_Alloc __a)
2606 : __data_(__a) {}
2607
2608 template <class _A0>
2609 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2610 : __data_(__a, _Tp(__a0)) {}
2611
2612 template <class _A0, class _A1>
2613 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2614 : __data_(__a, _Tp(__a0, __a1)) {}
2615
2616 template <class _A0, class _A1, class _A2>
2617 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2618 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2619
2620#endif // _LIBCPP_HAS_NO_VARIADICS
2621
2622private:
2623 virtual void __on_zero_shared();
2624 virtual void __on_zero_shared_weak();
2625public:
2626 _Tp* get() {return &__data_.second();}
2627};
2628
2629template <class _Tp, class _Alloc>
2630void
2631__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared()
2632{
2633 __data_.second().~_Tp();
2634}
2635
2636template <class _Tp, class _Alloc>
2637void
2638__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak()
2639{
2640 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2641 __data_.first().~_Alloc();
2642 __a.deallocate(this, 1);
2643}
2644
2645template<class _Tp> class enable_shared_from_this;
2646
2647template<class _Tp>
2648class shared_ptr
2649{
Howard Hinnant324bb032010-08-22 00:02:43 +00002650public:
2651 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652private:
2653 element_type* __ptr_;
2654 __shared_weak_count* __cntrl_;
2655
2656 struct __nat {int __for_bool_;};
2657public:
2658 shared_ptr();
2659 shared_ptr(nullptr_t);
2660 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00002661 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2662 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2664 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant324bb032010-08-22 00:02:43 +00002665 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 shared_ptr(const shared_ptr& __r);
2667 template<class _Yp>
2668 shared_ptr(const shared_ptr<_Yp>& __r,
2669 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002670#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 shared_ptr(shared_ptr&& __r);
2672 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002673 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002674#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002676 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002678 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2679#else
2680 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002682#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002684 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002685public:
2686 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2687 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2688 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2689 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002690#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2692 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2693 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2694 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002695#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002696
2697 ~shared_ptr();
2698
Howard Hinnant324bb032010-08-22 00:02:43 +00002699 shared_ptr& operator=(const shared_ptr& __r);
2700 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002701#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002702 shared_ptr& operator=(shared_ptr&& __r);
2703 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2704 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002705#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002706 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002708#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002710 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711public:
Howard Hinnant324bb032010-08-22 00:02:43 +00002712 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002713#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002714 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715#endif
2716
2717 void swap(shared_ptr& __r);
2718 void reset();
2719 template<class _Yp> void reset(_Yp* __p);
2720 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2721 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2722
2723 element_type* get() const {return __ptr_;}
2724 typename add_lvalue_reference<element_type>::type operator*() const {return *__ptr_;}
2725 element_type* operator->() const {return __ptr_;}
2726 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
2727 bool unique() const {return use_count() == 1;}
2728 bool empty() const {return __cntrl_ == 0;}
2729 /*explicit*/ operator bool() const {return get() != 0;}
2730 template <class _U> bool owner_before(shared_ptr<_U> const& __p) const
2731 {return __cntrl_ < __p.__cntrl_;}
2732 template <class _U> bool owner_before(weak_ptr<_U> const& __p) const
2733 {return __cntrl_ < __p.__cntrl_;}
2734
Howard Hinnantd4444702010-08-11 17:04:31 +00002735#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736 template <class _Dp>
2737 _Dp* __get_deleter() const
2738 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002739#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740
2741#ifndef _LIBCPP_HAS_NO_VARIADICS
2742
2743 template<class ..._Args>
2744 static
2745 shared_ptr<_Tp>
2746 make_shared(_Args&& ...__args);
2747
2748 template<class _Alloc, class ..._Args>
2749 static
2750 shared_ptr<_Tp>
2751 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2752
2753#else // _LIBCPP_HAS_NO_VARIADICS
2754
2755 static shared_ptr<_Tp> make_shared();
2756
2757 template<class _A0>
2758 static shared_ptr<_Tp> make_shared(_A0&);
2759
2760 template<class _A0, class _A1>
2761 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2762
2763 template<class _A0, class _A1, class _A2>
2764 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2765
2766 template<class _Alloc>
2767 static shared_ptr<_Tp>
2768 allocate_shared(const _Alloc& __a);
2769
2770 template<class _Alloc, class _A0>
2771 static shared_ptr<_Tp>
2772 allocate_shared(const _Alloc& __a, _A0& __a0);
2773
2774 template<class _Alloc, class _A0, class _A1>
2775 static shared_ptr<_Tp>
2776 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2777
2778 template<class _Alloc, class _A0, class _A1, class _A2>
2779 static shared_ptr<_Tp>
2780 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2781
2782#endif // _LIBCPP_HAS_NO_VARIADICS
2783
2784private:
2785
2786 template <class _Yp>
2787 void
2788 __enable_weak_this(const enable_shared_from_this<_Yp>* __e)
2789 {
2790 if (__e)
2791 __e->__weak_this_ = *this;
2792 }
2793
2794 void __enable_weak_this(const void*) {}
2795
2796 template <class _Up> friend class shared_ptr;
2797 template <class _Up> friend class weak_ptr;
2798};
2799
2800template<class _Tp>
2801inline _LIBCPP_INLINE_VISIBILITY
2802shared_ptr<_Tp>::shared_ptr()
2803 : __ptr_(0),
2804 __cntrl_(0)
2805{
2806}
2807
2808template<class _Tp>
2809inline _LIBCPP_INLINE_VISIBILITY
2810shared_ptr<_Tp>::shared_ptr(nullptr_t)
2811 : __ptr_(0),
2812 __cntrl_(0)
2813{
2814}
2815
2816template<class _Tp>
2817template<class _Yp>
2818shared_ptr<_Tp>::shared_ptr(_Yp* __p)
2819 : __ptr_(__p)
2820{
2821 unique_ptr<_Yp> __hold(__p);
2822 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2823 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
2824 __hold.release();
2825 __enable_weak_this(__p);
2826}
2827
2828template<class _Tp>
2829template<class _Yp, class _Dp>
2830shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
2831 : __ptr_(__p)
2832{
2833#ifndef _LIBCPP_NO_EXCEPTIONS
2834 try
2835 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002836#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
2838 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
2839 __enable_weak_this(__p);
2840#ifndef _LIBCPP_NO_EXCEPTIONS
2841 }
2842 catch (...)
2843 {
2844 __d(__p);
2845 throw;
2846 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002847#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848}
2849
2850template<class _Tp>
2851template<class _Dp>
2852shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
2853 : __ptr_(0)
2854{
2855#ifndef _LIBCPP_NO_EXCEPTIONS
2856 try
2857 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002858#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002859 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
2860 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
2861#ifndef _LIBCPP_NO_EXCEPTIONS
2862 }
2863 catch (...)
2864 {
2865 __d(__p);
2866 throw;
2867 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002868#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869}
2870
2871template<class _Tp>
2872template<class _Yp, class _Dp, class _Alloc>
2873shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
2874 : __ptr_(__p)
2875{
2876#ifndef _LIBCPP_NO_EXCEPTIONS
2877 try
2878 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002879#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
2881 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2882 typedef __allocator_destructor<_A2> _D2;
2883 _A2 __a2(__a);
2884 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2885 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2886 __cntrl_ = __hold2.release();
2887 __enable_weak_this(__p);
2888#ifndef _LIBCPP_NO_EXCEPTIONS
2889 }
2890 catch (...)
2891 {
2892 __d(__p);
2893 throw;
2894 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002895#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002896}
2897
2898template<class _Tp>
2899template<class _Dp, class _Alloc>
2900shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
2901 : __ptr_(0)
2902{
2903#ifndef _LIBCPP_NO_EXCEPTIONS
2904 try
2905 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002906#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002907 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
2908 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2909 typedef __allocator_destructor<_A2> _D2;
2910 _A2 __a2(__a);
2911 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2912 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2913 __cntrl_ = __hold2.release();
2914#ifndef _LIBCPP_NO_EXCEPTIONS
2915 }
2916 catch (...)
2917 {
2918 __d(__p);
2919 throw;
2920 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002921#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922}
2923
2924template<class _Tp>
2925template<class _Yp>
2926inline _LIBCPP_INLINE_VISIBILITY
2927shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p)
2928 : __ptr_(__p),
2929 __cntrl_(__r.__cntrl_)
2930{
2931 if (__cntrl_)
2932 __cntrl_->__add_shared();
2933}
2934
2935template<class _Tp>
2936inline _LIBCPP_INLINE_VISIBILITY
2937shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r)
2938 : __ptr_(__r.__ptr_),
2939 __cntrl_(__r.__cntrl_)
2940{
2941 if (__cntrl_)
2942 __cntrl_->__add_shared();
2943}
2944
2945template<class _Tp>
2946template<class _Yp>
2947inline _LIBCPP_INLINE_VISIBILITY
2948shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
2949 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
2950 : __ptr_(__r.__ptr_),
2951 __cntrl_(__r.__cntrl_)
2952{
2953 if (__cntrl_)
2954 __cntrl_->__add_shared();
2955}
2956
Howard Hinnant73d21a42010-09-04 23:28:19 +00002957#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002958
2959template<class _Tp>
2960inline _LIBCPP_INLINE_VISIBILITY
2961shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r)
2962 : __ptr_(__r.__ptr_),
2963 __cntrl_(__r.__cntrl_)
2964{
2965 __r.__ptr_ = 0;
2966 __r.__cntrl_ = 0;
2967}
2968
2969template<class _Tp>
2970template<class _Yp>
2971inline _LIBCPP_INLINE_VISIBILITY
2972shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
2973 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
2974 : __ptr_(__r.__ptr_),
2975 __cntrl_(__r.__cntrl_)
2976{
2977 __r.__ptr_ = 0;
2978 __r.__cntrl_ = 0;
2979}
2980
Howard Hinnant73d21a42010-09-04 23:28:19 +00002981#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002982
2983template<class _Tp>
2984template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00002985#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
2987#else
Howard Hinnant92172b82010-08-21 21:14:53 +00002988shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002989#endif
2990 : __ptr_(__r.get())
2991{
2992 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2993 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
2994 __enable_weak_this(__r.get());
2995 __r.release();
2996}
2997
2998template<class _Tp>
2999template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003000#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003001shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3002#else
3003shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3004#endif
3005 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3006 : __ptr_(__r.get())
3007{
3008 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3009 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3010 __enable_weak_this(__r.get());
3011 __r.release();
3012}
3013
3014template<class _Tp>
3015template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003017shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3018#else
3019shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3020#endif
3021 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3022 : __ptr_(__r.get())
3023{
3024 typedef __shared_ptr_pointer<_Yp*,
3025 reference_wrapper<typename remove_reference<_Dp>::type>,
3026 allocator<_Yp> > _CntrlBlk;
3027 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3028 __enable_weak_this(__r.get());
3029 __r.release();
3030}
3031
3032#ifndef _LIBCPP_HAS_NO_VARIADICS
3033
3034template<class _Tp>
3035template<class ..._Args>
3036shared_ptr<_Tp>
3037shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3038{
3039 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3040 typedef allocator<_CntrlBlk> _A2;
3041 typedef __allocator_destructor<_A2> _D2;
3042 _A2 __a2;
3043 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3044 ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3045 shared_ptr<_Tp> __r;
3046 __r.__ptr_ = __hold2.get()->get();
3047 __r.__cntrl_ = __hold2.release();
3048 __r.__enable_weak_this(__r.__ptr_);
3049 return __r;
3050}
3051
3052template<class _Tp>
3053template<class _Alloc, class ..._Args>
3054shared_ptr<_Tp>
3055shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3056{
3057 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3058 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3059 typedef __allocator_destructor<_A2> _D2;
3060 _A2 __a2(__a);
3061 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3062 ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3063 shared_ptr<_Tp> __r;
3064 __r.__ptr_ = __hold2.get()->get();
3065 __r.__cntrl_ = __hold2.release();
3066 __r.__enable_weak_this(__r.__ptr_);
3067 return __r;
3068}
3069
3070#else // _LIBCPP_HAS_NO_VARIADICS
3071
3072template<class _Tp>
3073shared_ptr<_Tp>
3074shared_ptr<_Tp>::make_shared()
3075{
3076 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3077 typedef allocator<_CntrlBlk> _Alloc2;
3078 typedef __allocator_destructor<_Alloc2> _D2;
3079 _Alloc2 __alloc2;
3080 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3081 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3082 shared_ptr<_Tp> __r;
3083 __r.__ptr_ = __hold2.get()->get();
3084 __r.__cntrl_ = __hold2.release();
3085 __r.__enable_weak_this(__r.__ptr_);
3086 return __r;
3087}
3088
3089template<class _Tp>
3090template<class _A0>
3091shared_ptr<_Tp>
3092shared_ptr<_Tp>::make_shared(_A0& __a0)
3093{
3094 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3095 typedef allocator<_CntrlBlk> _Alloc2;
3096 typedef __allocator_destructor<_Alloc2> _D2;
3097 _Alloc2 __alloc2;
3098 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3099 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3100 shared_ptr<_Tp> __r;
3101 __r.__ptr_ = __hold2.get()->get();
3102 __r.__cntrl_ = __hold2.release();
3103 __r.__enable_weak_this(__r.__ptr_);
3104 return __r;
3105}
3106
3107template<class _Tp>
3108template<class _A0, class _A1>
3109shared_ptr<_Tp>
3110shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3111{
3112 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3113 typedef allocator<_CntrlBlk> _Alloc2;
3114 typedef __allocator_destructor<_Alloc2> _D2;
3115 _Alloc2 __alloc2;
3116 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3117 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3118 shared_ptr<_Tp> __r;
3119 __r.__ptr_ = __hold2.get()->get();
3120 __r.__cntrl_ = __hold2.release();
3121 __r.__enable_weak_this(__r.__ptr_);
3122 return __r;
3123}
3124
3125template<class _Tp>
3126template<class _A0, class _A1, class _A2>
3127shared_ptr<_Tp>
3128shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3129{
3130 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3131 typedef allocator<_CntrlBlk> _Alloc2;
3132 typedef __allocator_destructor<_Alloc2> _D2;
3133 _Alloc2 __alloc2;
3134 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3135 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3136 shared_ptr<_Tp> __r;
3137 __r.__ptr_ = __hold2.get()->get();
3138 __r.__cntrl_ = __hold2.release();
3139 __r.__enable_weak_this(__r.__ptr_);
3140 return __r;
3141}
3142
3143template<class _Tp>
3144template<class _Alloc>
3145shared_ptr<_Tp>
3146shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3147{
3148 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3149 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3150 typedef __allocator_destructor<_Alloc2> _D2;
3151 _Alloc2 __alloc2(__a);
3152 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3153 ::new(__hold2.get()) _CntrlBlk(__a);
3154 shared_ptr<_Tp> __r;
3155 __r.__ptr_ = __hold2.get()->get();
3156 __r.__cntrl_ = __hold2.release();
3157 __r.__enable_weak_this(__r.__ptr_);
3158 return __r;
3159}
3160
3161template<class _Tp>
3162template<class _Alloc, class _A0>
3163shared_ptr<_Tp>
3164shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3165{
3166 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3167 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3168 typedef __allocator_destructor<_Alloc2> _D2;
3169 _Alloc2 __alloc2(__a);
3170 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3171 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3172 shared_ptr<_Tp> __r;
3173 __r.__ptr_ = __hold2.get()->get();
3174 __r.__cntrl_ = __hold2.release();
3175 __r.__enable_weak_this(__r.__ptr_);
3176 return __r;
3177}
3178
3179template<class _Tp>
3180template<class _Alloc, class _A0, class _A1>
3181shared_ptr<_Tp>
3182shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3183{
3184 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3185 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3186 typedef __allocator_destructor<_Alloc2> _D2;
3187 _Alloc2 __alloc2(__a);
3188 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3189 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3190 shared_ptr<_Tp> __r;
3191 __r.__ptr_ = __hold2.get()->get();
3192 __r.__cntrl_ = __hold2.release();
3193 __r.__enable_weak_this(__r.__ptr_);
3194 return __r;
3195}
3196
3197template<class _Tp>
3198template<class _Alloc, class _A0, class _A1, class _A2>
3199shared_ptr<_Tp>
3200shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3201{
3202 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3203 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3204 typedef __allocator_destructor<_Alloc2> _D2;
3205 _Alloc2 __alloc2(__a);
3206 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3207 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3208 shared_ptr<_Tp> __r;
3209 __r.__ptr_ = __hold2.get()->get();
3210 __r.__cntrl_ = __hold2.release();
3211 __r.__enable_weak_this(__r.__ptr_);
3212 return __r;
3213}
3214
3215#endif // _LIBCPP_HAS_NO_VARIADICS
3216
3217template<class _Tp>
3218shared_ptr<_Tp>::~shared_ptr()
3219{
3220 if (__cntrl_)
3221 __cntrl_->__release_shared();
3222}
3223
3224template<class _Tp>
3225inline _LIBCPP_INLINE_VISIBILITY
3226shared_ptr<_Tp>&
3227shared_ptr<_Tp>::operator=(const shared_ptr& __r)
3228{
3229 shared_ptr(__r).swap(*this);
3230 return *this;
3231}
3232
3233template<class _Tp>
3234template<class _Yp>
3235inline _LIBCPP_INLINE_VISIBILITY
3236shared_ptr<_Tp>&
3237shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r)
3238{
3239 shared_ptr(__r).swap(*this);
3240 return *this;
3241}
3242
Howard Hinnant73d21a42010-09-04 23:28:19 +00003243#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003244
3245template<class _Tp>
3246inline _LIBCPP_INLINE_VISIBILITY
3247shared_ptr<_Tp>&
3248shared_ptr<_Tp>::operator=(shared_ptr&& __r)
3249{
3250 shared_ptr(_STD::move(__r)).swap(*this);
3251 return *this;
3252}
3253
3254template<class _Tp>
3255template<class _Yp>
3256inline _LIBCPP_INLINE_VISIBILITY
3257shared_ptr<_Tp>&
3258shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3259{
3260 shared_ptr(_STD::move(__r)).swap(*this);
3261 return *this;
3262}
3263
3264template<class _Tp>
3265template<class _Yp>
3266inline _LIBCPP_INLINE_VISIBILITY
3267shared_ptr<_Tp>&
3268shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3269{
3270 shared_ptr(__r).swap(*this);
3271 return *this;
3272}
3273
3274template<class _Tp>
3275template <class _Yp, class _Dp>
3276inline _LIBCPP_INLINE_VISIBILITY
3277shared_ptr<_Tp>&
3278shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3279{
3280 shared_ptr(_STD::move(__r)).swap(*this);
3281 return *this;
3282}
3283
Howard Hinnant73d21a42010-09-04 23:28:19 +00003284#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003285
3286template<class _Tp>
3287template<class _Yp>
3288inline _LIBCPP_INLINE_VISIBILITY
3289shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003290shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003291{
3292 shared_ptr(__r).swap(*this);
3293 return *this;
3294}
3295
3296template<class _Tp>
3297template <class _Yp, class _Dp>
3298inline _LIBCPP_INLINE_VISIBILITY
3299shared_ptr<_Tp>&
3300shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3301{
3302 shared_ptr(_STD::move(__r)).swap(*this);
3303 return *this;
3304}
3305
Howard Hinnant73d21a42010-09-04 23:28:19 +00003306#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003307
3308template<class _Tp>
3309inline _LIBCPP_INLINE_VISIBILITY
3310void
3311shared_ptr<_Tp>::swap(shared_ptr& __r)
3312{
3313 _STD::swap(__ptr_, __r.__ptr_);
3314 _STD::swap(__cntrl_, __r.__cntrl_);
3315}
3316
3317template<class _Tp>
3318inline _LIBCPP_INLINE_VISIBILITY
3319void
3320shared_ptr<_Tp>::reset()
3321{
3322 shared_ptr().swap(*this);
3323}
3324
3325template<class _Tp>
3326template<class _Yp>
3327inline _LIBCPP_INLINE_VISIBILITY
3328void
3329shared_ptr<_Tp>::reset(_Yp* __p)
3330{
3331 shared_ptr(__p).swap(*this);
3332}
3333
3334template<class _Tp>
3335template<class _Yp, class _Dp>
3336inline _LIBCPP_INLINE_VISIBILITY
3337void
3338shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3339{
3340 shared_ptr(__p, __d).swap(*this);
3341}
3342
3343template<class _Tp>
3344template<class _Yp, class _Dp, class _Alloc>
3345inline _LIBCPP_INLINE_VISIBILITY
3346void
3347shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3348{
3349 shared_ptr(__p, __d, __a).swap(*this);
3350}
3351
3352#ifndef _LIBCPP_HAS_NO_VARIADICS
3353
Howard Hinnant324bb032010-08-22 00:02:43 +00003354template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003355inline _LIBCPP_INLINE_VISIBILITY
3356shared_ptr<_Tp>
3357make_shared(_Args&& ...__args)
3358{
3359 return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3360}
3361
Howard Hinnant324bb032010-08-22 00:02:43 +00003362template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003363inline _LIBCPP_INLINE_VISIBILITY
3364shared_ptr<_Tp>
3365allocate_shared(const _Alloc& __a, _Args&& ...__args)
3366{
3367 return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3368}
3369
3370#else // _LIBCPP_HAS_NO_VARIADICS
3371
3372template<class _Tp>
3373inline _LIBCPP_INLINE_VISIBILITY
3374shared_ptr<_Tp>
3375make_shared()
3376{
3377 return shared_ptr<_Tp>::make_shared();
3378}
3379
3380template<class _Tp, class _A0>
3381inline _LIBCPP_INLINE_VISIBILITY
3382shared_ptr<_Tp>
3383make_shared(_A0& __a0)
3384{
3385 return shared_ptr<_Tp>::make_shared(__a0);
3386}
3387
3388template<class _Tp, class _A0, class _A1>
3389inline _LIBCPP_INLINE_VISIBILITY
3390shared_ptr<_Tp>
3391make_shared(_A0& __a0, _A1& __a1)
3392{
3393 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3394}
3395
Howard Hinnant324bb032010-08-22 00:02:43 +00003396template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003397inline _LIBCPP_INLINE_VISIBILITY
3398shared_ptr<_Tp>
3399make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3400{
3401 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3402}
3403
3404template<class _Tp, class _Alloc>
3405inline _LIBCPP_INLINE_VISIBILITY
3406shared_ptr<_Tp>
3407allocate_shared(const _Alloc& __a)
3408{
3409 return shared_ptr<_Tp>::allocate_shared(__a);
3410}
3411
3412template<class _Tp, class _Alloc, class _A0>
3413inline _LIBCPP_INLINE_VISIBILITY
3414shared_ptr<_Tp>
3415allocate_shared(const _Alloc& __a, _A0& __a0)
3416{
3417 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3418}
3419
3420template<class _Tp, class _Alloc, class _A0, class _A1>
3421inline _LIBCPP_INLINE_VISIBILITY
3422shared_ptr<_Tp>
3423allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3424{
3425 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3426}
3427
3428template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3429inline _LIBCPP_INLINE_VISIBILITY
3430shared_ptr<_Tp>
3431allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3432{
3433 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3434}
3435
3436#endif // _LIBCPP_HAS_NO_VARIADICS
3437
3438template<class _Tp, class _Up>
3439inline _LIBCPP_INLINE_VISIBILITY
3440bool
3441operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3442{
3443 return __x.get() == __y.get();
3444}
3445
3446template<class _Tp, class _Up>
3447inline _LIBCPP_INLINE_VISIBILITY
3448bool
3449operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3450{
3451 return !(__x == __y);
3452}
3453
3454template<class _Tp, class _Up>
3455inline _LIBCPP_INLINE_VISIBILITY
3456bool
3457operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3458{
3459 return __x.get() < __y.get();
3460}
3461
3462template<class _Tp>
3463inline _LIBCPP_INLINE_VISIBILITY
3464void
3465swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y)
3466{
3467 __x.swap(__y);
3468}
3469
3470template<class _Tp, class _Up>
3471inline _LIBCPP_INLINE_VISIBILITY
3472shared_ptr<_Tp>
3473static_pointer_cast(const shared_ptr<_Up>& __r)
3474{
3475 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3476}
3477
3478template<class _Tp, class _Up>
3479inline _LIBCPP_INLINE_VISIBILITY
3480shared_ptr<_Tp>
3481dynamic_pointer_cast(const shared_ptr<_Up>& __r)
3482{
3483 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3484 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3485}
3486
3487template<class _Tp, class _Up>
3488shared_ptr<_Tp>
3489const_pointer_cast(const shared_ptr<_Up>& __r)
3490{
3491 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3492}
3493
Howard Hinnantd4444702010-08-11 17:04:31 +00003494#ifndef _LIBCPP_NO_RTTI
3495
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003496template<class _Dp, class _Tp>
3497inline _LIBCPP_INLINE_VISIBILITY
3498_Dp*
3499get_deleter(const shared_ptr<_Tp>& __p)
3500{
3501 return __p.template __get_deleter<_Dp>();
3502}
3503
Howard Hinnant324bb032010-08-22 00:02:43 +00003504#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003505
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003506template<class _Tp>
3507class weak_ptr
3508{
Howard Hinnant324bb032010-08-22 00:02:43 +00003509public:
3510 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003511private:
3512 element_type* __ptr_;
3513 __shared_weak_count* __cntrl_;
3514
Howard Hinnant324bb032010-08-22 00:02:43 +00003515public:
3516 weak_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003517 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
3518 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003519 weak_ptr(weak_ptr const& __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003520 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003521 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3522
3523 ~weak_ptr();
3524
3525 weak_ptr& operator=(weak_ptr const& __r);
3526 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r);
3527 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r);
3528
3529 void swap(weak_ptr& __r);
3530 void reset();
3531
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
3533 bool expired() const {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
Howard Hinnant324bb032010-08-22 00:02:43 +00003534 shared_ptr<_Tp> lock() const;
3535 template<class _Up> bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 {return __cntrl_ < __r.__cntrl_;}
3537 template<class _Up> bool owner_before(const weak_ptr<_Up>& __r) const
3538 {return __cntrl_ < __r.__cntrl_;}
3539
3540 template <class _Up> friend class weak_ptr;
3541 template <class _Up> friend class shared_ptr;
3542};
3543
3544template<class _Tp>
3545inline _LIBCPP_INLINE_VISIBILITY
3546weak_ptr<_Tp>::weak_ptr()
3547 : __ptr_(0),
3548 __cntrl_(0)
3549{
3550}
3551
3552template<class _Tp>
3553inline _LIBCPP_INLINE_VISIBILITY
3554weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r)
3555 : __ptr_(__r.__ptr_),
3556 __cntrl_(__r.__cntrl_)
3557{
3558 if (__cntrl_)
3559 __cntrl_->__add_weak();
3560}
3561
3562template<class _Tp>
3563template<class _Yp>
3564inline _LIBCPP_INLINE_VISIBILITY
3565weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3566 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3567 : __ptr_(__r.__ptr_),
3568 __cntrl_(__r.__cntrl_)
3569{
3570 if (__cntrl_)
3571 __cntrl_->__add_weak();
3572}
3573
3574template<class _Tp>
3575template<class _Yp>
3576inline _LIBCPP_INLINE_VISIBILITY
3577weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3578 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3579 : __ptr_(__r.__ptr_),
3580 __cntrl_(__r.__cntrl_)
3581{
3582 if (__cntrl_)
3583 __cntrl_->__add_weak();
3584}
3585
3586template<class _Tp>
3587weak_ptr<_Tp>::~weak_ptr()
3588{
3589 if (__cntrl_)
3590 __cntrl_->__release_weak();
3591}
3592
3593template<class _Tp>
3594inline _LIBCPP_INLINE_VISIBILITY
3595weak_ptr<_Tp>&
3596weak_ptr<_Tp>::operator=(weak_ptr const& __r)
3597{
3598 weak_ptr(__r).swap(*this);
3599 return *this;
3600}
3601
3602template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003603template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604inline _LIBCPP_INLINE_VISIBILITY
3605weak_ptr<_Tp>&
3606weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r)
3607{
3608 weak_ptr(__r).swap(*this);
3609 return *this;
3610}
3611
3612template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003613template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003614inline _LIBCPP_INLINE_VISIBILITY
3615weak_ptr<_Tp>&
3616weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r)
3617{
3618 weak_ptr(__r).swap(*this);
3619 return *this;
3620}
3621
3622template<class _Tp>
3623inline _LIBCPP_INLINE_VISIBILITY
3624void
3625weak_ptr<_Tp>::swap(weak_ptr& __r)
3626{
3627 _STD::swap(__ptr_, __r.__ptr_);
3628 _STD::swap(__cntrl_, __r.__cntrl_);
3629}
3630
3631template<class _Tp>
3632inline _LIBCPP_INLINE_VISIBILITY
3633void
3634swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y)
3635{
3636 __x.swap(__y);
3637}
3638
3639template<class _Tp>
3640inline _LIBCPP_INLINE_VISIBILITY
3641void
3642weak_ptr<_Tp>::reset()
3643{
3644 weak_ptr().swap(*this);
3645}
3646
3647template<class _Tp>
3648template<class _Yp>
3649shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3650 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3651 : __ptr_(__r.__ptr_),
3652 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3653{
3654 if (__cntrl_ == 0)
3655#ifndef _LIBCPP_NO_EXCEPTIONS
3656 throw bad_weak_ptr();
3657#else
3658 assert(!"bad_weak_ptr");
3659#endif
3660}
3661
3662template<class _Tp>
3663shared_ptr<_Tp>
3664weak_ptr<_Tp>::lock() const
3665{
3666 shared_ptr<_Tp> __r;
3667 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3668 if (__r.__cntrl_)
3669 __r.__ptr_ = __ptr_;
3670 return __r;
3671}
3672
Howard Hinnant324bb032010-08-22 00:02:43 +00003673template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674
3675template <class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003676struct owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00003678{
3679 typedef bool result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003680 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3681 {return __x.owner_before(__y);}
3682 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3683 {return __x.owner_before(__y);}
3684 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3685 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003686};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687
3688template <class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003689struct owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3691{
Howard Hinnant324bb032010-08-22 00:02:43 +00003692 typedef bool result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003693 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3694 {return __x.owner_before(__y);}
3695 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3696 {return __x.owner_before(__y);}
3697 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3698 {return __x.owner_before(__y);}
3699};
3700
3701template<class _Tp>
3702class enable_shared_from_this
3703{
3704 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00003705protected:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003706 enable_shared_from_this() {}
3707 enable_shared_from_this(enable_shared_from_this const&) {}
3708 enable_shared_from_this& operator=(enable_shared_from_this const&) {return *this;}
3709 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003710public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711 shared_ptr<_Tp> shared_from_this() {return shared_ptr<_Tp>(__weak_this_);}
3712 shared_ptr<_Tp const> shared_from_this() const {return shared_ptr<const _Tp>(__weak_this_);}
3713
3714 template <class _Up> friend class shared_ptr;
3715};
3716
Howard Hinnant21aefc32010-06-03 16:42:57 +00003717template <class _Tp>
3718struct hash<shared_ptr<_Tp> >
3719{
3720 typedef shared_ptr<_Tp> argument_type;
3721 typedef size_t result_type;
3722 result_type operator()(const argument_type& __ptr) const
3723 {
3724 return hash<_Tp*>()(__ptr.get());
3725 }
3726};
3727
Howard Hinnant324bb032010-08-22 00:02:43 +00003728//enum class
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003729struct pointer_safety
3730{
3731 enum _
3732 {
3733 relaxed,
3734 preferred,
3735 strict
3736 };
3737
3738 _ __v_;
3739
3740 pointer_safety(_ __v) : __v_(__v) {}
3741 operator int() const {return __v_;}
3742};
3743
3744void declare_reachable(void* __p);
3745void declare_no_pointers(char* __p, size_t __n);
3746void undeclare_no_pointers(char* __p, size_t __n);
3747pointer_safety get_pointer_safety();
3748void* __undeclare_reachable(void*);
3749
3750template <class _Tp>
3751inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00003752_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003753undeclare_reachable(_Tp* __p)
3754{
3755 return static_cast<_Tp*>(__undeclare_reachable(__p));
3756}
3757
3758void* align(size_t, size_t, void*&, size_t&);
3759
3760_LIBCPP_END_NAMESPACE_STD
3761
3762#endif // _LIBCPP_MEMORY