blob: c20e1a0c61554e708440195b8841d9ba82c054d5 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant324bb032010-08-22 00:02:43 +000031
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant324bb032010-08-22 00:02:43 +000033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
37template <class Alloc>
38struct allocator_traits
39{
40 typedef Alloc allocator_type;
41 typedef typename allocator_type::value_type
42 value_type;
43
44 typedef Alloc::pointer | value_type* pointer;
45 typedef Alloc::const_pointer
46 | pointer_traits<pointer>::rebind<const value_type>
47 const_pointer;
48 typedef Alloc::void_pointer
49 | pointer_traits<pointer>::rebind<void>
50 void_pointer;
51 typedef Alloc::const_void_pointer
52 | pointer_traits<pointer>::rebind<const void>
53 const_void_pointer;
54 typedef Alloc::difference_type
Howard Hinnant47761072010-11-18 01:40:00 +000055 | pointer_traits<pointer>::difference_type
56 difference_type;
57 typedef Alloc::size_type
58 | make_unsigned<difference_type>::type
59 size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 typedef Alloc::propagate_on_container_copy_assignment
61 | false_type propagate_on_container_copy_assignment;
62 typedef Alloc::propagate_on_container_move_assignment
63 | false_type propagate_on_container_move_assignment;
64 typedef Alloc::propagate_on_container_swap
65 | false_type propagate_on_container_swap;
66
67 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
68 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
69
70 static pointer allocate(allocator_type& a, size_type n);
71 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
72
73 static void deallocate(allocator_type& a, pointer p, size_type n);
74
75 template <class T, class... Args>
76 static void construct(allocator_type& a, T* p, Args&&... args);
77
78 template <class T>
79 static void destroy(allocator_type& a, T* p);
80
81 static size_type max_size(const allocator_type& a);
82
83 static allocator_type
84 select_on_container_copy_construction(const allocator_type& a);
85};
86
87template <>
88class allocator<void>
89{
90public:
91 typedef void* pointer;
92 typedef const void* const_pointer;
93 typedef void value_type;
94
95 template <class _Up> struct rebind {typedef allocator<_Up> other;};
96};
97
98template <class T>
99class allocator
100{
101public:
102 typedef size_t size_type;
103 typedef ptrdiff_t difference_type;
104 typedef T* pointer;
105 typedef const T* const_pointer;
106 typedef typename add_lvalue_reference<T>::type reference;
107 typedef typename add_lvalue_reference<const T>::type const_reference;
108 typedef T value_type;
109
110 template <class U> struct rebind {typedef allocator<U> other;};
111
112 allocator() throw();
113 allocator(const allocator&) throw();
114 template <class U> allocator(const allocator<U>&) throw();
115 ~allocator() throw();
116 pointer address(reference x) const;
117 const_pointer address(const_reference x) const;
118 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
119 void deallocate(pointer p, size_type n);
120 size_type max_size() const throw();
121 void construct(pointer p, const T& val);
122 void destroy(pointer p);
123};
124
125template <class T, class U>
126bool operator==(const allocator<T>&, const allocator<U>&) throw();
127
128template <class T, class U>
129bool operator!=(const allocator<T>&, const allocator<U>&) throw();
130
131template <class OutputIterator, class T>
132class raw_storage_iterator
133 : public iterator<output_iterator_tag,
134 T, // purposefully not C++03
135 ptrdiff_t, // purposefully not C++03
136 T*, // purposefully not C++03
137 raw_storage_iterator&> // purposefully not C++03
138{
139public:
140 explicit raw_storage_iterator(OutputIterator x);
141 raw_storage_iterator& operator*();
142 raw_storage_iterator& operator=(const T& element);
143 raw_storage_iterator& operator++();
144 raw_storage_iterator operator++(int);
145};
146
147template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n);
148template <class T> void return_temporary_buffer(T* p);
149
150template <class InputIterator, class ForwardIterator>
151ForwardIterator
152uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
153
154template <class ForwardIterator, class T>
155void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
156
157template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +0000158ForwardIterator
159uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160
161template <class Y> struct auto_ptr_ref {};
162
163template<class X>
164class auto_ptr
165{
166public:
167 typedef X element_type;
168
169 explicit auto_ptr(X* p =0) throw();
170 auto_ptr(auto_ptr&) throw();
171 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
172 auto_ptr& operator=(auto_ptr&) throw();
173 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
174 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
175 ~auto_ptr() throw();
176
177 typename add_lvalue_reference<X>::type operator*() const throw();
178 X* operator->() const throw();
179 X* get() const throw();
180 X* release() throw();
181 void reset(X* p =0) throw();
182
183 auto_ptr(auto_ptr_ref<X>) throw();
184 template<class Y> operator auto_ptr_ref<Y>() throw();
185 template<class Y> operator auto_ptr<Y>() throw();
186};
187
Howard Hinnante92c3d72010-08-19 18:39:17 +0000188template <class T>
189struct default_delete
190{
191 constexpr default_delete();
192 template <class U> default_delete(const default_delete<U>&);
193
194 void operator()(T*) const;
195};
196
197template <class T>
198struct default_delete<T[]>
199{
200 constexpr default_delete();
201 void operator()(T*) const;
202 template <class U> void operator()(U*) const = delete;
203};
204
205template <class T, class D = default_delete<T>> class unique_ptr;
206
207template <class T, class D = default_delete<T>>
208class unique_ptr
209{
210public:
211 typedef see below pointer;
212 typedef T element_type;
213 typedef D deleter_type;
214
215 // constructors
216 constexpr unique_ptr();
217 explicit unique_ptr(pointer p);
218 unique_ptr(pointer p, implementation-defined d1);
219 unique_ptr(pointer p, implementation-defined d2);
220 unique_ptr(unique_ptr&& u);
221 unique_ptr(nullptr_t) : unique_ptr() { }
222 template <class U, class E>
223 unique_ptr(unique_ptr<U, E>&& u);
224 template <class U>
Howard Hinnante92c3d72010-08-19 18:39:17 +0000225 unique_ptr(auto_ptr<U>&& u);
226
227 // destructor
228 ~unique_ptr();
229
230 // assignment
231 unique_ptr& operator=(unique_ptr&& u);
232 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u);
233 unique_ptr& operator=(nullptr_t);
234
235 // observers
236 typename add_lvalue_reference<T>::type operator*() const;
237 pointer operator->() const;
238 pointer get() const;
239 deleter_type& get_deleter();
240 const deleter_type& get_deleter() const;
241 explicit operator bool() const;
242
243 // modifiers
244 pointer release();
245 void reset(pointer p = pointer());
246 void swap(unique_ptr& u);
247};
248
249template <class T, class D>
250class unique_ptr<T[], D>
251{
252public:
253 typedef implementation-defined pointer;
254 typedef T element_type;
255 typedef D deleter_type;
256
257 // constructors
258 constexpr unique_ptr();
259 explicit unique_ptr(pointer p);
260 unique_ptr(pointer p, implementation-defined d);
261 unique_ptr(pointer p, implementation-defined d);
262 unique_ptr(unique_ptr&& u);
263 unique_ptr(nullptr_t) : unique_ptr() { }
264
265 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000266 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000267
268 // assignment
269 unique_ptr& operator=(unique_ptr&& u);
270 unique_ptr& operator=(nullptr_t);
271
272 // observers
273 T& operator[](size_t i) const;
274 pointer get() const;
275 deleter_type& get_deleter();
276 const deleter_type& get_deleter() const;
277 explicit operator bool() const;
278
279 // modifiers
280 pointer release();
281 void reset(pointer p = pointer());
282 void reset(nullptr_t);
283 template <class U> void reset(U) = delete;
284 void swap(unique_ptr& u);
285};
286
287template <class T, class D>
288 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y);
289
290template <class T1, class D1, class T2, class D2>
291 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
292template <class T1, class D1, class T2, class D2>
293 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
294template <class T1, class D1, class T2, class D2>
295 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
296template <class T1, class D1, class T2, class D2>
297 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
298template <class T1, class D1, class T2, class D2>
299 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
300template <class T1, class D1, class T2, class D2>
301 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
302
303class bad_weak_ptr
304 : public std::exception
305{
306 bad_weak_ptr();
307};
308
309template<class T>
310class shared_ptr
311{
312public:
313 typedef T element_type;
314
315 // constructors:
316 constexpr shared_ptr();
317 template<class Y> explicit shared_ptr(Y* p);
318 template<class Y, class D> shared_ptr(Y* p, D d);
319 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
320 template <class D> shared_ptr(nullptr_t p, D d);
321 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
322 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p);
323 shared_ptr(const shared_ptr& r);
324 template<class Y> shared_ptr(const shared_ptr<Y>& r);
325 shared_ptr(shared_ptr&& r);
326 template<class Y> shared_ptr(shared_ptr<Y>&& r);
327 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
328 template<class Y> shared_ptr(auto_ptr<Y>&& r);
329 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
330 shared_ptr(nullptr_t) : shared_ptr() { }
331
332 // destructor:
333 ~shared_ptr();
334
335 // assignment:
336 shared_ptr& operator=(const shared_ptr& r);
337 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r);
338 shared_ptr& operator=(shared_ptr&& r);
339 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
340 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
341 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
342
343 // modifiers:
344 void swap(shared_ptr& r);
345 void reset();
346 template<class Y> void reset(Y* p);
347 template<class Y, class D> void reset(Y* p, D d);
348 template<class Y, class D, class A> void reset(Y* p, D d, A a);
349
350 // observers: T* get() const;
351 T& operator*() const;
352 T* operator->() const;
353 long use_count() const;
354 bool unique() const;
355 explicit operator bool() const;
356 template<class U> bool owner_before(shared_ptr<U> const& b) const;
357 template<class U> bool owner_before(weak_ptr<U> const& b) const;
358};
359
360// shared_ptr comparisons:
361template<class T, class U>
362 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b);
363template<class T, class U>
364 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b);
365template<class T, class U>
366 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b);
367template<class T, class U>
368 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b);
369template<class T, class U>
370 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b);
371template<class T, class U>
372 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b);
373
374// shared_ptr specialized algorithms:
375template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b);
376
377// shared_ptr casts:
378template<class T, class U>
379 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r);
380template<class T, class U>
381 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r);
382template<class T, class U>
383 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r);
384
385// shared_ptr I/O:
386template<class E, class T, class Y>
387 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
388
389// shared_ptr get_deleter:
390template<class D, class T> D* get_deleter(shared_ptr<T> const& p);
391
392template<class T, class... Args>
393 shared_ptr<T> make_shared(Args&&... args);
394template<class T, class A, class... Args>
395 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
396
397template<class T>
398class weak_ptr
399{
400public:
401 typedef T element_type;
402
403 // constructors
404 constexpr weak_ptr();
405 template<class Y> weak_ptr(shared_ptr<Y> const& r);
406 weak_ptr(weak_ptr const& r);
407 template<class Y> weak_ptr(weak_ptr<Y> const& r);
408
409 // destructor
410 ~weak_ptr();
411
412 // assignment
413 weak_ptr& operator=(weak_ptr const& r);
414 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r);
415 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r);
416
417 // modifiers
418 void swap(weak_ptr& r);
419 void reset();
420
421 // observers
422 long use_count() const;
423 bool expired() const;
424 shared_ptr<T> lock() const;
425 template<class U> bool owner_before(shared_ptr<U> const& b);
426 template<class U> bool owner_before(weak_ptr<U> const& b);
427};
428
429// weak_ptr specialized algorithms:
430template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b);
431
432// class owner_less:
433template<class T> struct owner_less;
434
435template<class T>
436struct owner_less<shared_ptr<T>>
437 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
438{
439 typedef bool result_type;
440 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
441 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
442 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
443};
444
445template<class T>
446struct owner_less<weak_ptr<T>>
447 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
448{
449 typedef bool result_type;
450 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
451 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
452 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
453};
454
455template<class T>
456class enable_shared_from_this
457{
458protected:
459 constexpr enable_shared_from_this();
460 enable_shared_from_this(enable_shared_from_this const&);
461 enable_shared_from_this& operator=(enable_shared_from_this const&);
462 ~enable_shared_from_this();
463public:
464 shared_ptr<T> shared_from_this();
465 shared_ptr<T const> shared_from_this() const;
466};
467
468template<class T>
469 bool atomic_is_lock_free(const shared_ptr<T>* p);
470template<class T>
471 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
472template<class T>
473 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
474template<class T>
475 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
476template<class T>
477 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
478template<class T>
479 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
480template<class T>
481 shared_ptr<T>
482 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
483template<class T>
484 bool
485 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
486template<class T>
487 bool
488 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
489template<class T>
490 bool
491 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
492 shared_ptr<T> w, memory_order success,
493 memory_order failure);
494template<class T>
495 bool
496 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
497 shared_ptr<T> w, memory_order success,
498 memory_order failure);
499// Hash support
500template <class T> struct hash;
501template <class T, class D> struct hash<unique_ptr<T, D> >;
502template <class T> struct hash<shared_ptr<T> >;
503
504// Pointer safety
505enum class pointer_safety { relaxed, preferred, strict };
506void declare_reachable(void *p);
507template <class T> T *undeclare_reachable(T *p);
508void declare_no_pointers(char *p, size_t n);
509void undeclare_no_pointers(char *p, size_t n);
510pointer_safety get_pointer_safety();
511
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000512void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
513
514} // std
515
516*/
517
518#include <__config>
519#include <type_traits>
520#include <typeinfo>
521#include <cstddef>
522#include <cstdint>
523#include <new>
524#include <utility>
525#include <limits>
526#include <iterator>
527#include <__functional_base>
528#if defined(_LIBCPP_NO_EXCEPTIONS)
529 #include <cassert>
530#endif
531
532#pragma GCC system_header
533
534_LIBCPP_BEGIN_NAMESPACE_STD
535
536// allocator_arg_t
537
Howard Hinnant82894812010-09-22 16:48:34 +0000538struct _LIBCPP_VISIBLE allocator_arg_t { };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539
540extern const allocator_arg_t allocator_arg;
541
542// addressof
543
544template <class _Tp>
545inline _LIBCPP_INLINE_VISIBILITY
546_Tp*
547addressof(_Tp& __x)
548{
549 return (_Tp*)&(char&)__x;
550}
551
552template <class _Tp> class allocator;
553
554template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000555class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556{
557public:
558 typedef void* pointer;
559 typedef const void* const_pointer;
560 typedef void value_type;
561
562 template <class _Up> struct rebind {typedef allocator<_Up> other;};
563};
564
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000565// pointer_traits
566
567template <class _Tp>
568struct __has_element_type
569{
570private:
571 struct __two {char _; char __;};
572 template <class _Up> static __two __test(...);
573 template <class _Up> static char __test(typename _Up::element_type* = 0);
574public:
575 static const bool value = sizeof(__test<_Tp>(0)) == 1;
576};
577
578template <class _Ptr, bool = __has_element_type<_Ptr>::value>
579struct __pointer_traits_element_type;
580
581template <class _Ptr>
582struct __pointer_traits_element_type<_Ptr, true>
583{
584 typedef typename _Ptr::element_type type;
585};
586
587#ifndef _LIBCPP_HAS_NO_VARIADICS
588
589template <template <class, class...> class _Sp, class _Tp, class ..._Args>
590struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
591{
592 typedef typename _Sp<_Tp, _Args...>::element_type type;
593};
594
595template <template <class, class...> class _Sp, class _Tp, class ..._Args>
596struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
597{
598 typedef _Tp type;
599};
600
Howard Hinnant324bb032010-08-22 00:02:43 +0000601#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602
603template <template <class> class _Sp, class _Tp>
604struct __pointer_traits_element_type<_Sp<_Tp>, true>
605{
606 typedef typename _Sp<_Tp>::element_type type;
607};
608
609template <template <class> class _Sp, class _Tp>
610struct __pointer_traits_element_type<_Sp<_Tp>, false>
611{
612 typedef _Tp type;
613};
614
615template <template <class, class> class _Sp, class _Tp, class _A0>
616struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
617{
618 typedef typename _Sp<_Tp, _A0>::element_type type;
619};
620
621template <template <class, class> class _Sp, class _Tp, class _A0>
622struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
623{
624 typedef _Tp type;
625};
626
627template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
628struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
629{
630 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
631};
632
633template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
634struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
635{
636 typedef _Tp type;
637};
638
639template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
640 class _A1, class _A2>
641struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
642{
643 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
644};
645
646template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
647 class _A1, class _A2>
648struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
649{
650 typedef _Tp type;
651};
652
Howard Hinnant324bb032010-08-22 00:02:43 +0000653#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654
655template <class _Tp>
656struct __has_difference_type
657{
658private:
659 struct __two {char _; char __;};
660 template <class _Up> static __two __test(...);
661 template <class _Up> static char __test(typename _Up::difference_type* = 0);
662public:
663 static const bool value = sizeof(__test<_Tp>(0)) == 1;
664};
665
666template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
667struct __pointer_traits_difference_type
668{
669 typedef ptrdiff_t type;
670};
671
672template <class _Ptr>
673struct __pointer_traits_difference_type<_Ptr, true>
674{
675 typedef typename _Ptr::difference_type type;
676};
677
678template <class _Tp, class _Up>
679struct __has_rebind
680{
681private:
682 struct __two {char _; char __;};
683 template <class _Xp> static __two __test(...);
684 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
685public:
686 static const bool value = sizeof(__test<_Tp>(0)) == 1;
687};
688
689template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
690struct __pointer_traits_rebind
691{
692#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
693 typedef typename _Tp::template rebind<_Up> type;
694#else
695 typedef typename _Tp::template rebind<_Up>::other type;
696#endif
697};
698
699#ifndef _LIBCPP_HAS_NO_VARIADICS
700
701template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
702struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
703{
704#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
705 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
706#else
707 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
708#endif
709};
710
711template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
712struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
713{
714 typedef _Sp<_Up, _Args...> type;
715};
716
Howard Hinnant324bb032010-08-22 00:02:43 +0000717#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718
719template <template <class> class _Sp, class _Tp, class _Up>
720struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
721{
722#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
723 typedef typename _Sp<_Tp>::template rebind<_Up> type;
724#else
725 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
726#endif
727};
728
729template <template <class> class _Sp, class _Tp, class _Up>
730struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
731{
732 typedef _Sp<_Up> type;
733};
734
735template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
736struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
737{
738#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
739 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
740#else
741 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
742#endif
743};
744
745template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
746struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
747{
748 typedef _Sp<_Up, _A0> type;
749};
750
751template <template <class, class, class> class _Sp, class _Tp, class _A0,
752 class _A1, class _Up>
753struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
754{
755#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
756 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
757#else
758 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
759#endif
760};
761
762template <template <class, class, class> class _Sp, class _Tp, class _A0,
763 class _A1, class _Up>
764struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
765{
766 typedef _Sp<_Up, _A0, _A1> type;
767};
768
769template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
770 class _A1, class _A2, class _Up>
771struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
772{
773#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
774 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
775#else
776 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
777#endif
778};
779
780template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
781 class _A1, class _A2, class _Up>
782struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
783{
784 typedef _Sp<_Up, _A0, _A1, _A2> type;
785};
786
Howard Hinnant324bb032010-08-22 00:02:43 +0000787#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788
789template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000790struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000791{
792 typedef _Ptr pointer;
793 typedef typename __pointer_traits_element_type<pointer>::type element_type;
794 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
795
796#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
797 template <class _Up> using rebind = __pointer_traits_rebind<pointer, _Up>::type;
798#else
799 template <class _Up> struct rebind
800 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000801#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802
803private:
804 struct __nat {};
805public:
Howard Hinnant82894812010-09-22 16:48:34 +0000806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807 static pointer pointer_to(typename conditional<is_void<element_type>::value,
808 __nat, element_type>::type& __r)
809 {return pointer::pointer_to(__r);}
810};
811
812template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000813struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814{
815 typedef _Tp* pointer;
816 typedef _Tp element_type;
817 typedef ptrdiff_t difference_type;
818
819#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
820 template <class _Up> using rebind = _Up*;
821#else
822 template <class _Up> struct rebind {typedef _Up* other;};
823#endif
824
825private:
826 struct __nat {};
827public:
Howard Hinnant82894812010-09-22 16:48:34 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000829 static pointer pointer_to(typename conditional<is_void<element_type>::value,
830 __nat, element_type>::type& __r)
831 {return _STD::addressof(__r);}
832};
833
834// allocator_traits
835
836namespace __has_pointer_type_imp
837{
838 template <class _Up> static __two test(...);
839 template <class _Up> static char test(typename _Up::pointer* = 0);
840}
841
842template <class _Tp>
843struct __has_pointer_type
844 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
845{
846};
847
848namespace __pointer_type_imp
849{
850
851template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
852struct __pointer_type
853{
854 typedef typename _Dp::pointer type;
855};
856
857template <class _Tp, class _Dp>
858struct __pointer_type<_Tp, _Dp, false>
859{
860 typedef _Tp* type;
861};
862
Howard Hinnant47761072010-11-18 01:40:00 +0000863} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864
865template <class _Tp, class _Dp>
866struct __pointer_type
867{
868 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
869};
870
871template <class _Tp>
872struct __has_const_pointer
873{
874private:
875 struct __two {char _; char __;};
876 template <class _Up> static __two __test(...);
877 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
878public:
879 static const bool value = sizeof(__test<_Tp>(0)) == 1;
880};
881
882template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
883struct __const_pointer
884{
885 typedef typename _Alloc::const_pointer type;
886};
887
888template <class _Tp, class _Ptr, class _Alloc>
889struct __const_pointer<_Tp, _Ptr, _Alloc, false>
890{
891#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
892 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
893#else
894 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
895#endif
896};
897
898template <class _Tp>
899struct __has_void_pointer
900{
901private:
902 struct __two {char _; char __;};
903 template <class _Up> static __two __test(...);
904 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
905public:
906 static const bool value = sizeof(__test<_Tp>(0)) == 1;
907};
908
909template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
910struct __void_pointer
911{
912 typedef typename _Alloc::void_pointer type;
913};
914
915template <class _Ptr, class _Alloc>
916struct __void_pointer<_Ptr, _Alloc, false>
917{
918#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
919 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
920#else
921 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
922#endif
923};
924
925template <class _Tp>
926struct __has_const_void_pointer
927{
928private:
929 struct __two {char _; char __;};
930 template <class _Up> static __two __test(...);
931 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
932public:
933 static const bool value = sizeof(__test<_Tp>(0)) == 1;
934};
935
936template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
937struct __const_void_pointer
938{
939 typedef typename _Alloc::const_void_pointer type;
940};
941
942template <class _Ptr, class _Alloc>
943struct __const_void_pointer<_Ptr, _Alloc, false>
944{
945#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
946 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
947#else
948 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
949#endif
950};
951
952template <class _T>
953inline _LIBCPP_INLINE_VISIBILITY
954_T*
955__to_raw_pointer(_T* __p)
956{
957 return __p;
958}
959
960template <class _Pointer>
961inline _LIBCPP_INLINE_VISIBILITY
962typename pointer_traits<_Pointer>::element_type*
963__to_raw_pointer(_Pointer __p)
964{
965 return _STD::__to_raw_pointer(__p.operator->());
966}
967
968template <class _Tp>
969struct __has_size_type
970{
971private:
972 struct __two {char _; char __;};
973 template <class _Up> static __two __test(...);
974 template <class _Up> static char __test(typename _Up::size_type* = 0);
975public:
976 static const bool value = sizeof(__test<_Tp>(0)) == 1;
977};
978
Howard Hinnant47761072010-11-18 01:40:00 +0000979template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000980struct __size_type
981{
Howard Hinnant47761072010-11-18 01:40:00 +0000982 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983};
984
Howard Hinnant47761072010-11-18 01:40:00 +0000985template <class _Alloc, class _DiffType>
986struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987{
988 typedef typename _Alloc::size_type type;
989};
990
991template <class _Tp>
992struct __has_propagate_on_container_copy_assignment
993{
994private:
995 struct __two {char _; char __;};
996 template <class _Up> static __two __test(...);
997 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
998public:
999 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1000};
1001
1002template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1003struct __propagate_on_container_copy_assignment
1004{
1005 typedef false_type type;
1006};
1007
1008template <class _Alloc>
1009struct __propagate_on_container_copy_assignment<_Alloc, true>
1010{
1011 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1012};
1013
1014template <class _Tp>
1015struct __has_propagate_on_container_move_assignment
1016{
1017private:
1018 struct __two {char _; char __;};
1019 template <class _Up> static __two __test(...);
1020 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1021public:
1022 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1023};
1024
1025template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1026struct __propagate_on_container_move_assignment
1027{
1028 typedef false_type type;
1029};
1030
1031template <class _Alloc>
1032struct __propagate_on_container_move_assignment<_Alloc, true>
1033{
1034 typedef typename _Alloc::propagate_on_container_move_assignment type;
1035};
1036
1037template <class _Tp>
1038struct __has_propagate_on_container_swap
1039{
1040private:
1041 struct __two {char _; char __;};
1042 template <class _Up> static __two __test(...);
1043 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1044public:
1045 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1046};
1047
1048template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1049struct __propagate_on_container_swap
1050{
1051 typedef false_type type;
1052};
1053
1054template <class _Alloc>
1055struct __propagate_on_container_swap<_Alloc, true>
1056{
1057 typedef typename _Alloc::propagate_on_container_swap type;
1058};
1059
1060template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1061struct __has_rebind_other
1062{
1063private:
1064 struct __two {char _; char __;};
1065 template <class _Xp> static __two __test(...);
1066 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1067public:
1068 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1069};
1070
1071template <class _Tp, class _Up>
1072struct __has_rebind_other<_Tp, _Up, false>
1073{
1074 static const bool value = false;
1075};
1076
1077template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1078struct __allocator_traits_rebind
1079{
1080 typedef typename _Tp::template rebind<_Up>::other type;
1081};
1082
1083#ifndef _LIBCPP_HAS_NO_VARIADICS
1084
1085template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1086struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1087{
1088 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1089};
1090
1091template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1092struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1093{
1094 typedef _Alloc<_Up, _Args...> type;
1095};
1096
Howard Hinnant324bb032010-08-22 00:02:43 +00001097#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001098
1099template <template <class> class _Alloc, class _Tp, class _Up>
1100struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1101{
1102 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1103};
1104
1105template <template <class> class _Alloc, class _Tp, class _Up>
1106struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1107{
1108 typedef _Alloc<_Up> type;
1109};
1110
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1112struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1113{
1114 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1115};
1116
1117template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1118struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1119{
1120 typedef _Alloc<_Up, _A0> type;
1121};
1122
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1124 class _A1, class _Up>
1125struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1126{
1127 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1128};
1129
1130template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1131 class _A1, class _Up>
1132struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1133{
1134 typedef _Alloc<_Up, _A0, _A1> type;
1135};
1136
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001137template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1138 class _A1, class _A2, class _Up>
1139struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1140{
1141 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1142};
1143
1144template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1145 class _A1, class _A2, class _Up>
1146struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1147{
1148 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1149};
1150
Howard Hinnant324bb032010-08-22 00:02:43 +00001151#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152
1153#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1154
1155template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1156auto
1157__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1158 -> decltype(__a.allocate(__sz, __p), true_type());
1159
1160template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1161auto
1162__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1163 -> false_type;
1164
1165template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1166struct __has_allocate_hint
1167 : integral_constant<bool,
1168 is_same<
1169 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1170 declval<_SizeType>(),
1171 declval<_ConstVoidPtr>())),
1172 true_type>::value>
1173{
1174};
1175
Howard Hinnant324bb032010-08-22 00:02:43 +00001176#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177
1178template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1179struct __has_allocate_hint
1180 : true_type
1181{
1182};
1183
Howard Hinnant324bb032010-08-22 00:02:43 +00001184#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001185
1186#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1187
1188template <class _Alloc, class _Tp, class ..._Args>
1189decltype(_STD::declval<_Alloc>().construct(_STD::declval<_Tp*>(),
1190 _STD::declval<_Args>()...),
1191 true_type())
1192__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1193
1194template <class _Alloc, class _Pointer, class ..._Args>
1195false_type
1196__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1197
1198template <class _Alloc, class _Pointer, class ..._Args>
1199struct __has_construct
1200 : integral_constant<bool,
1201 is_same<
1202 decltype(__has_construct_test(declval<_Alloc>(),
1203 declval<_Pointer>(),
1204 declval<_Args>()...)),
1205 true_type>::value>
1206{
1207};
1208
1209template <class _Alloc, class _Pointer>
1210auto
1211__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1212 -> decltype(__a.destroy(__p), true_type());
1213
1214template <class _Alloc, class _Pointer>
1215auto
1216__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1217 -> false_type;
1218
1219template <class _Alloc, class _Pointer>
1220struct __has_destroy
1221 : integral_constant<bool,
1222 is_same<
1223 decltype(__has_destroy_test(declval<_Alloc>(),
1224 declval<_Pointer>())),
1225 true_type>::value>
1226{
1227};
1228
1229template <class _Alloc>
1230auto
1231__has_max_size_test(_Alloc&& __a)
1232 -> decltype(__a.max_size(), true_type());
1233
1234template <class _Alloc>
1235auto
1236__has_max_size_test(const volatile _Alloc& __a)
1237 -> false_type;
1238
1239template <class _Alloc>
1240struct __has_max_size
1241 : integral_constant<bool,
1242 is_same<
1243 decltype(__has_max_size_test(declval<_Alloc&>())),
1244 true_type>::value>
1245{
1246};
1247
1248template <class _Alloc>
1249auto
1250__has_select_on_container_copy_construction_test(_Alloc&& __a)
1251 -> decltype(__a.select_on_container_copy_construction(), true_type());
1252
1253template <class _Alloc>
1254auto
1255__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1256 -> false_type;
1257
1258template <class _Alloc>
1259struct __has_select_on_container_copy_construction
1260 : integral_constant<bool,
1261 is_same<
1262 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1263 true_type>::value>
1264{
1265};
1266
Howard Hinnant324bb032010-08-22 00:02:43 +00001267#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268
1269#ifndef _LIBCPP_HAS_NO_VARIADICS
1270
1271template <class _Alloc, class _Pointer, class ..._Args>
1272struct __has_construct
1273 : false_type
1274{
1275};
1276
Howard Hinnant324bb032010-08-22 00:02:43 +00001277#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278
1279template <class _Alloc, class _Pointer>
1280struct __has_destroy
1281 : false_type
1282{
1283};
1284
1285template <class _Alloc>
1286struct __has_max_size
1287 : true_type
1288{
1289};
1290
1291template <class _Alloc>
1292struct __has_select_on_container_copy_construction
1293 : false_type
1294{
1295};
1296
Howard Hinnant324bb032010-08-22 00:02:43 +00001297#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298
Howard Hinnant47761072010-11-18 01:40:00 +00001299template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1300struct __alloc_traits_difference_type
1301{
1302 typedef typename pointer_traits<_Ptr>::difference_type type;
1303};
1304
1305template <class _Alloc, class _Ptr>
1306struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1307{
1308 typedef typename _Alloc::difference_type type;
1309};
1310
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001312struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313{
1314 typedef _Alloc allocator_type;
1315 typedef typename allocator_type::value_type value_type;
1316
1317 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1318 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1319 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1320 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1321
Howard Hinnant47761072010-11-18 01:40:00 +00001322 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1323 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324
1325 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1326 propagate_on_container_copy_assignment;
1327 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1328 propagate_on_container_move_assignment;
1329 typedef typename __propagate_on_container_swap<allocator_type>::type
1330 propagate_on_container_swap;
1331
1332#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1333 template <class _Tp> using rebind_alloc =
1334 __allocator_traits_rebind<allocator_type, _Tp>::type;
1335 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001336#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 template <class _Tp> struct rebind_alloc
1338 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1339 template <class _Tp> struct rebind_traits
1340 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001341#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342
Howard Hinnant82894812010-09-22 16:48:34 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 static pointer allocate(allocator_type& __a, size_type __n)
1345 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001347 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1348 {return allocate(__a, __n, __hint,
1349 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1350
Howard Hinnant82894812010-09-22 16:48:34 +00001351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352 static void deallocate(allocator_type& __a, pointer __p, size_type __n)
1353 {__a.deallocate(__p, __n);}
1354
1355#ifndef _LIBCPP_HAS_NO_VARIADICS
1356 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1359 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
1360 __a, __p, _STD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001361#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364 static void construct(allocator_type& __a, _Tp* __p)
1365 {
1366 ::new ((void*)__p) _Tp();
1367 }
1368 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1371 {
1372 ::new ((void*)__p) _Tp(__a0);
1373 }
1374 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1377 const _A1& __a1)
1378 {
1379 ::new ((void*)__p) _Tp(__a0, __a1);
1380 }
1381 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1384 const _A1& __a1, const _A2& __a2)
1385 {
1386 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1387 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001388#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389
1390 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392 static void destroy(allocator_type& __a, _Tp* __p)
1393 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1394
Howard Hinnant82894812010-09-22 16:48:34 +00001395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396 static size_type max_size(const allocator_type& __a)
1397 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1398
Howard Hinnant82894812010-09-22 16:48:34 +00001399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001400 static allocator_type
1401 select_on_container_copy_construction(const allocator_type& __a)
1402 {return select_on_container_copy_construction(
1403 __has_select_on_container_copy_construction<const allocator_type>(),
1404 __a);}
1405
1406private:
1407
Howard Hinnant82894812010-09-22 16:48:34 +00001408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409 static pointer allocate(allocator_type& __a, size_type __n,
1410 const_void_pointer __hint, true_type)
1411 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413 static pointer allocate(allocator_type& __a, size_type __n,
1414 const_void_pointer __hint, false_type)
1415 {return __a.allocate(__n);}
1416
1417#ifndef _LIBCPP_HAS_NO_VARIADICS
1418 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1421 {__a.construct(__p, _STD::forward<_Args>(__args)...);}
1422 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1425 {
1426 ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
1427 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001428#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429
1430 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1433 {__a.destroy(__p);}
1434 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436 static void __destroy(false_type, allocator_type&, _Tp* __p)
1437 {
1438 __p->~_Tp();
1439 }
1440
Howard Hinnant82894812010-09-22 16:48:34 +00001441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442 static size_type __max_size(true_type, const allocator_type& __a)
1443 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445 static size_type __max_size(false_type, const allocator_type&)
1446 {return numeric_limits<size_type>::max();}
1447
Howard Hinnant82894812010-09-22 16:48:34 +00001448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449 static allocator_type
1450 select_on_container_copy_construction(true_type, const allocator_type& __a)
1451 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453 static allocator_type
1454 select_on_container_copy_construction(false_type, const allocator_type& __a)
1455 {return __a;}
1456};
1457
1458// uses_allocator
1459
1460template <class _Tp>
1461struct __has_allocator_type
1462{
1463private:
1464 struct __two {char _; char __;};
1465 template <class _Up> static __two __test(...);
1466 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1467public:
1468 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1469};
1470
1471template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1472struct __uses_allocator
1473 : public integral_constant<bool,
1474 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1475{
1476};
1477
1478template <class _Tp, class _Alloc>
1479struct __uses_allocator<_Tp, _Alloc, false>
1480 : public false_type
1481{
1482};
1483
1484template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001485struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 : public __uses_allocator<_Tp, _Alloc>
1487{
1488};
1489
Howard Hinnant726a76f2010-11-16 21:10:23 +00001490#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491
1492// uses-allocator construction
1493
1494template <class _Tp, class _Alloc, class ..._Args>
1495struct __uses_alloc_ctor_imp
1496{
1497 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1498 static const bool __ic =
1499 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1500 static const int value = __ua ? 2 - __ic : 0;
1501};
1502
1503template <class _Tp, class _Alloc, class ..._Args>
1504struct __uses_alloc_ctor
1505 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1506 {};
1507
Howard Hinnant726a76f2010-11-16 21:10:23 +00001508#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509
1510// allocator
1511
1512template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001513class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514{
1515public:
1516 typedef size_t size_type;
1517 typedef ptrdiff_t difference_type;
1518 typedef _Tp* pointer;
1519 typedef const _Tp* const_pointer;
1520 typedef _Tp& reference;
1521 typedef const _Tp& const_reference;
1522 typedef _Tp value_type;
1523
1524 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1525
1526 _LIBCPP_INLINE_VISIBILITY allocator() throw() {}
1527 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) throw() {}
1528 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const {return addressof(__x);}
1529 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const {return addressof(__x);}
1530 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1531 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1532 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) {::operator delete((void*)__p);}
1533 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001534#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 template <class _Up, class... _Args>
1536 _LIBCPP_INLINE_VISIBILITY
1537 void
1538 construct(_Up* __p, _Args&&... __args)
1539 {
1540 ::new((void*)__p) _Up(_STD::forward<_Args>(__args)...);
1541 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001542#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 _LIBCPP_INLINE_VISIBILITY
1544 void
1545 construct(pointer __p)
1546 {
1547 ::new((void*)__p) _Tp();
1548 }
1549 template <class _A0>
1550 _LIBCPP_INLINE_VISIBILITY
1551 typename enable_if
1552 <
1553 !is_convertible<_A0, __rv<_A0> >::value,
1554 void
1555 >::type
1556 construct(pointer __p, _A0& __a0)
1557 {
1558 ::new((void*)__p) _Tp(__a0);
1559 }
1560 template <class _A0>
1561 _LIBCPP_INLINE_VISIBILITY
1562 typename enable_if
1563 <
1564 !is_convertible<_A0, __rv<_A0> >::value,
1565 void
1566 >::type
1567 construct(pointer __p, const _A0& __a0)
1568 {
1569 ::new((void*)__p) _Tp(__a0);
1570 }
1571 template <class _A0>
1572 _LIBCPP_INLINE_VISIBILITY
1573 typename enable_if
1574 <
1575 is_convertible<_A0, __rv<_A0> >::value,
1576 void
1577 >::type
1578 construct(pointer __p, _A0 __a0)
1579 {
1580 ::new((void*)__p) _Tp(_STD::move(__a0));
1581 }
1582 template <class _A0, class _A1>
1583 _LIBCPP_INLINE_VISIBILITY
1584 void
1585 construct(pointer __p, _A0& __a0, _A1& __a1)
1586 {
1587 ::new((void*)__p) _Tp(__a0, __a1);
1588 }
1589 template <class _A0, class _A1>
1590 _LIBCPP_INLINE_VISIBILITY
1591 void
1592 construct(pointer __p, const _A0& __a0, _A1& __a1)
1593 {
1594 ::new((void*)__p) _Tp(__a0, __a1);
1595 }
1596 template <class _A0, class _A1>
1597 _LIBCPP_INLINE_VISIBILITY
1598 void
1599 construct(pointer __p, _A0& __a0, const _A1& __a1)
1600 {
1601 ::new((void*)__p) _Tp(__a0, __a1);
1602 }
1603 template <class _A0, class _A1>
1604 _LIBCPP_INLINE_VISIBILITY
1605 void
1606 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1607 {
1608 ::new((void*)__p) _Tp(__a0, __a1);
1609 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001610#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1612};
1613
1614template <class _Tp, class _Up>
1615inline _LIBCPP_INLINE_VISIBILITY
1616bool operator==(const allocator<_Tp>&, const allocator<_Up>&) throw() {return true;}
1617
1618template <class _Tp, class _Up>
1619inline _LIBCPP_INLINE_VISIBILITY
1620bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) throw() {return false;}
1621
1622template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001623class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624 : public iterator<output_iterator_tag,
1625 _Tp, // purposefully not C++03
1626 ptrdiff_t, // purposefully not C++03
1627 _Tp*, // purposefully not C++03
1628 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1629{
1630private:
1631 _OutputIterator __x_;
1632public:
1633 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1634 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1635 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1636 {::new(&*__x_) _Tp(__element); return *this;}
1637 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1638 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1639 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1640};
1641
1642template <class _Tp>
1643pair<_Tp*, ptrdiff_t>
1644get_temporary_buffer(ptrdiff_t __n)
1645{
1646 pair<_Tp*, ptrdiff_t> __r(0, 0);
1647 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1648 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1649 / sizeof(_Tp);
1650 if (__n > __m)
1651 __n = __m;
1652 while (__n > 0)
1653 {
1654 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1655 if (__r.first)
1656 {
1657 __r.second = __n;
1658 break;
1659 }
1660 __n /= 2;
1661 }
1662 return __r;
1663}
1664
1665template <class _Tp>
1666inline _LIBCPP_INLINE_VISIBILITY
1667void return_temporary_buffer(_Tp* __p) {::operator delete(__p);}
1668
1669template <class _Tp>
1670struct auto_ptr_ref
1671{
1672 _Tp* __ptr_;
1673};
1674
1675template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001676class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677{
1678private:
1679 _Tp* __ptr_;
1680public:
1681 typedef _Tp element_type;
1682
1683 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1684 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1685 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1686 : __ptr_(__p.release()) {}
1687 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1688 {reset(__p.release()); return *this;}
1689 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1690 {reset(__p.release()); return *this;}
1691 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1692 {reset(__p.__ptr_); return *this;}
1693 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1694
1695 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1696 {return *__ptr_;}
1697 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1698 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1699 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1700 {
1701 _Tp* __t = __ptr_;
1702 __ptr_ = 0;
1703 return __t;
1704 }
1705 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1706 {
1707 if (__ptr_ != __p)
1708 delete __ptr_;
1709 __ptr_ = __p;
1710 }
1711
1712 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1713 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1714 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1715 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1716 {return auto_ptr<_Up>(release());}
1717};
1718
1719template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001720class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721{
1722public:
1723 typedef void element_type;
1724};
1725
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1727 typename remove_cv<_T2>::type>::value,
1728 bool = is_empty<_T1>::value,
1729 bool = is_empty<_T2>::value>
1730struct __libcpp_compressed_pair_switch;
1731
1732template <class _T1, class _T2, bool IsSame>
1733struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1734
1735template <class _T1, class _T2, bool IsSame>
1736struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1737
1738template <class _T1, class _T2, bool IsSame>
1739struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1740
1741template <class _T1, class _T2>
1742struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1743
1744template <class _T1, class _T2>
1745struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1746
1747template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1748class __libcpp_compressed_pair_imp;
1749
1750template <class _T1, class _T2>
1751class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1752{
1753private:
1754 _T1 __first_;
1755 _T2 __second_;
1756public:
1757 typedef _T1 _T1_param;
1758 typedef _T2 _T2_param;
1759
1760 typedef typename remove_reference<_T1>::type& _T1_reference;
1761 typedef typename remove_reference<_T2>::type& _T2_reference;
1762
1763 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1764 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1765
1766 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1767 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1768 : __first_(_STD::forward<_T1_param>(__t1)) {}
1769 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1770 : __second_(_STD::forward<_T2_param>(__t2)) {}
1771 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1772 : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1773
Howard Hinnant73d21a42010-09-04 23:28:19 +00001774#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1776 : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001777#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778
1779 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1780 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1781
1782 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1783 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1784
1785 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1786 {
1787 using _STD::swap;
1788 swap(__first_, __x.__first_);
1789 swap(__second_, __x.__second_);
1790 }
1791};
1792
1793template <class _T1, class _T2>
1794class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1795 : private _T1
1796{
1797private:
1798 _T2 __second_;
1799public:
1800 typedef _T1 _T1_param;
1801 typedef _T2 _T2_param;
1802
1803 typedef _T1& _T1_reference;
1804 typedef typename remove_reference<_T2>::type& _T2_reference;
1805
1806 typedef const _T1& _T1_const_reference;
1807 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1808
1809 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1810 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1811 : _T1(_STD::forward<_T1_param>(__t1)) {}
1812 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1813 : __second_(_STD::forward<_T2_param>(__t2)) {}
1814 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1815 : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1816
Howard Hinnant73d21a42010-09-04 23:28:19 +00001817#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1819 : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001820#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821
1822 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1823 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1824
1825 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1826 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1827
1828 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1829 {
1830 using _STD::swap;
1831 swap(__second_, __x.__second_);
1832 }
1833};
1834
1835template <class _T1, class _T2>
1836class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1837 : private _T2
1838{
1839private:
1840 _T1 __first_;
1841public:
1842 typedef _T1 _T1_param;
1843 typedef _T2 _T2_param;
1844
1845 typedef typename remove_reference<_T1>::type& _T1_reference;
1846 typedef _T2& _T2_reference;
1847
1848 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1849 typedef const _T2& _T2_const_reference;
1850
1851 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1852 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1853 : __first_(_STD::forward<_T1_param>(__t1)) {}
1854 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1855 : _T2(_STD::forward<_T2_param>(__t2)) {}
1856 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1857 : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1858
Howard Hinnant73d21a42010-09-04 23:28:19 +00001859#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1861 : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001862#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863
1864 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1865 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1866
1867 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1868 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1869
1870 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1871 {
1872 using _STD::swap;
1873 swap(__first_, __x.__first_);
1874 }
1875};
1876
1877template <class _T1, class _T2>
1878class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1879 : private _T1,
1880 private _T2
1881{
1882public:
1883 typedef _T1 _T1_param;
1884 typedef _T2 _T2_param;
1885
1886 typedef _T1& _T1_reference;
1887 typedef _T2& _T2_reference;
1888
1889 typedef const _T1& _T1_const_reference;
1890 typedef const _T2& _T2_const_reference;
1891
1892 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1893 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1894 : _T1(_STD::forward<_T1_param>(__t1)) {}
1895 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1896 : _T2(_STD::forward<_T2_param>(__t2)) {}
1897 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1898 : _T1(_STD::forward<_T1_param>(__t1)), _T2(_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 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1902 : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
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 *this;}
1906 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1907
1908 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1909 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1910
1911 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1912 {
1913 }
1914};
1915
1916template <class _T1, class _T2>
1917class __compressed_pair
1918 : private __libcpp_compressed_pair_imp<_T1, _T2>
1919{
1920 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
1921public:
1922 typedef typename base::_T1_param _T1_param;
1923 typedef typename base::_T2_param _T2_param;
1924
1925 typedef typename base::_T1_reference _T1_reference;
1926 typedef typename base::_T2_reference _T2_reference;
1927
1928 typedef typename base::_T1_const_reference _T1_const_reference;
1929 typedef typename base::_T2_const_reference _T2_const_reference;
1930
1931 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
1932 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
1933 : base(_STD::forward<_T1_param>(__t1)) {}
1934 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
1935 : base(_STD::forward<_T2_param>(__t2)) {}
1936 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
1937 : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
1938
Howard Hinnant73d21a42010-09-04 23:28:19 +00001939#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940 __compressed_pair(__compressed_pair&& __p)
1941 : base(_STD::move(__p)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001942#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943
1944 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return base::first();}
1945 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return base::first();}
1946
1947 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return base::second();}
1948 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return base::second();}
1949
1950 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x) {base::swap(__x);}
1951};
1952
1953template <class _T1, class _T2>
1954inline _LIBCPP_INLINE_VISIBILITY
1955void
1956swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1957 {__x.swap(__y);}
1958
1959template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001960struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961{
1962 _LIBCPP_INLINE_VISIBILITY default_delete() {}
1963 template <class _Up>
1964 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
1965 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) {}
1966 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1967 {
1968 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1969 delete __ptr;
1970 }
1971};
1972
1973template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001974struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975{
1976 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1977 {
1978 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1979 delete [] __ptr;
1980 }
1981private:
1982 template <class _Up> void operator() (_Up*) const;
1983};
1984
1985template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00001986class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987{
1988public:
1989 typedef _Tp element_type;
1990 typedef _Dp deleter_type;
1991 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1992private:
1993 __compressed_pair<pointer, deleter_type> __ptr_;
1994
Howard Hinnant73d21a42010-09-04 23:28:19 +00001995#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996 unique_ptr(const unique_ptr&);
1997 unique_ptr& operator=(const unique_ptr&);
1998 template <class _Up, class _Ep>
1999 unique_ptr(const unique_ptr<_Up, _Ep>&);
2000 template <class _Up, class _Ep>
2001 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002002#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002003 unique_ptr(unique_ptr&);
2004 template <class _Up, class _Ep>
2005 unique_ptr(unique_ptr<_Up, _Ep>&);
2006 unique_ptr& operator=(unique_ptr&);
2007 template <class _Up, class _Ep>
2008 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002009#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002010
2011 struct __nat {int __for_bool_;};
2012
2013 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2014 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2015public:
2016 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2017 : __ptr_(pointer())
2018 {
2019 static_assert(!is_pointer<deleter_type>::value,
2020 "unique_ptr constructed with null function pointer deleter");
2021 }
2022 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2023 : __ptr_(pointer())
2024 {
2025 static_assert(!is_pointer<deleter_type>::value,
2026 "unique_ptr constructed with null function pointer deleter");
2027 }
2028 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2029 : __ptr_(_STD::move(__p))
2030 {
2031 static_assert(!is_pointer<deleter_type>::value,
2032 "unique_ptr constructed with null function pointer deleter");
2033 }
2034
Howard Hinnant73d21a42010-09-04 23:28:19 +00002035#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2037 is_reference<deleter_type>::value,
2038 deleter_type,
2039 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2040 : __ptr_(__p, __d) {}
2041
2042 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
2043 : __ptr_(__p, _STD::move(__d))
2044 {
2045 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2046 }
2047 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2048 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2049 template <class _Up, class _Ep>
2050 _LIBCPP_INLINE_VISIBILITY
2051 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2052 typename enable_if
2053 <
2054 !is_array<_Up>::value &&
2055 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2056 is_convertible<_Ep, deleter_type>::value &&
2057 (
2058 !is_reference<deleter_type>::value ||
2059 is_same<deleter_type, _Ep>::value
2060 ),
2061 __nat
2062 >::type = __nat())
2063 : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2064
2065 template <class _Up>
2066 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2067 typename enable_if<
2068 is_convertible<_Up*, _Tp*>::value &&
2069 is_same<_Dp, default_delete<_Tp> >::value,
2070 __nat
2071 >::type = __nat())
2072 : __ptr_(__p.release())
2073 {
2074 }
2075
2076 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2077 {
2078 reset(__u.release());
2079 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2080 return *this;
2081 }
2082
2083 template <class _Up, class _Ep>
2084 _LIBCPP_INLINE_VISIBILITY
2085 typename enable_if
2086 <
2087 !is_array<_Up>::value,
2088 unique_ptr&
2089 >::type
2090 operator=(unique_ptr<_Up, _Ep>&& __u)
2091 {
2092 reset(__u.release());
2093 __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2094 return *this;
2095 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002096#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097
2098 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2099 {
2100 return __rv<unique_ptr>(*this);
2101 }
2102
2103 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2104 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2105
2106 template <class _Up, class _Ep>
2107 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2108 {
2109 reset(__u.release());
2110 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2111 return *this;
2112 }
2113
2114 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2115 : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2116
2117 template <class _Up>
2118 _LIBCPP_INLINE_VISIBILITY
2119 typename enable_if<
2120 is_convertible<_Up*, _Tp*>::value &&
2121 is_same<_Dp, default_delete<_Tp> >::value,
2122 unique_ptr&
2123 >::type
2124 operator=(auto_ptr<_Up> __p)
2125 {reset(__p.release()); return *this;}
2126
Howard Hinnant73d21a42010-09-04 23:28:19 +00002127#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2129
2130 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2131 {
2132 reset();
2133 return *this;
2134 }
2135
2136 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2137 {return *__ptr_.first();}
2138 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_.first();}
2139 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2140 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2141 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2142 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2143
2144 _LIBCPP_INLINE_VISIBILITY pointer release()
2145 {
2146 pointer __t = __ptr_.first();
2147 __ptr_.first() = pointer();
2148 return __t;
2149 }
2150
2151 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2152 {
2153 pointer __tmp = __ptr_.first();
2154 __ptr_.first() = __p;
2155 if (__tmp)
2156 __ptr_.second()(__tmp);
2157 }
2158
2159 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2160};
2161
2162template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002163class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164{
2165public:
2166 typedef _Tp element_type;
2167 typedef _Dp deleter_type;
2168 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2169private:
2170 __compressed_pair<pointer, deleter_type> __ptr_;
2171
Howard Hinnant73d21a42010-09-04 23:28:19 +00002172#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 unique_ptr(const unique_ptr&);
2174 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002175#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176 unique_ptr(unique_ptr&);
2177 template <class _Up>
2178 unique_ptr(unique_ptr<_Up>&);
2179 unique_ptr& operator=(unique_ptr&);
2180 template <class _Up>
2181 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002182#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183
2184 struct __nat {int __for_bool_;};
2185
2186 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2187 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2188public:
2189 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2190 : __ptr_(pointer())
2191 {
2192 static_assert(!is_pointer<deleter_type>::value,
2193 "unique_ptr constructed with null function pointer deleter");
2194 }
2195 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2196 : __ptr_(pointer())
2197 {
2198 static_assert(!is_pointer<deleter_type>::value,
2199 "unique_ptr constructed with null function pointer deleter");
2200 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002201#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202 template <class _P,
2203 class = typename enable_if<is_same<_P, pointer>::value>::type
2204 >
2205 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p)
2206 : __ptr_(__p)
2207 {
2208 static_assert(!is_pointer<deleter_type>::value,
2209 "unique_ptr constructed with null function pointer deleter");
2210 }
2211
2212 template <class _P,
2213 class = typename enable_if<is_same<_P, pointer>::value>::type
2214 >
2215 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2216 is_reference<deleter_type>::value,
2217 deleter_type,
2218 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2219 : __ptr_(__p, __d) {}
2220
2221 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2222 is_reference<deleter_type>::value,
2223 deleter_type,
2224 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2225 : __ptr_(pointer(), __d) {}
2226
2227 template <class _P,
2228 class = typename enable_if<is_same<_P, pointer>::value ||
2229 is_same<_P, nullptr_t>::value>::type
2230 >
2231 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
2232 : __ptr_(__p, _STD::move(__d))
2233 {
2234 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2235 }
2236
2237 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
2238 : __ptr_(pointer(), _STD::move(__d))
2239 {
2240 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2241 }
2242
2243 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2244 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2245
2246 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2247 {
2248 reset(__u.release());
2249 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2250 return *this;
2251 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002252#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253
2254 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2255 : __ptr_(__p)
2256 {
2257 static_assert(!is_pointer<deleter_type>::value,
2258 "unique_ptr constructed with null function pointer deleter");
2259 }
2260
2261 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2262 : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2263
2264 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2265 : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2266
2267 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2268 {
2269 return __rv<unique_ptr>(*this);
2270 }
2271
2272 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2273 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2274
2275 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2276 {
2277 reset(__u->release());
2278 __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2279 return *this;
2280 }
2281
Howard Hinnant73d21a42010-09-04 23:28:19 +00002282#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002283 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2284
2285 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2286 {
2287 reset();
2288 return *this;
2289 }
2290
2291 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2292 {return __ptr_.first()[__i];}
2293 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2294 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2295 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2296 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2297
2298 _LIBCPP_INLINE_VISIBILITY pointer release()
2299 {
2300 pointer __t = __ptr_.first();
2301 __ptr_.first() = pointer();
2302 return __t;
2303 }
2304
Howard Hinnant73d21a42010-09-04 23:28:19 +00002305#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306 template <class _P,
2307 class = typename enable_if<is_same<_P, pointer>::value>::type
2308 >
2309 _LIBCPP_INLINE_VISIBILITY void reset(_P __p)
2310 {
2311 pointer __tmp = __ptr_.first();
2312 __ptr_.first() = __p;
2313 if (__tmp)
2314 __ptr_.second()(__tmp);
2315 }
2316 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t)
2317 {
2318 pointer __tmp = __ptr_.first();
2319 __ptr_.first() = nullptr;
2320 if (__tmp)
2321 __ptr_.second()(__tmp);
2322 }
2323 _LIBCPP_INLINE_VISIBILITY void reset()
2324 {
2325 pointer __tmp = __ptr_.first();
2326 __ptr_.first() = nullptr;
2327 if (__tmp)
2328 __ptr_.second()(__tmp);
2329 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002330#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2332 {
2333 pointer __tmp = __ptr_.first();
2334 __ptr_.first() = __p;
2335 if (__tmp)
2336 __ptr_.second()(__tmp);
2337 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002338#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002339
2340 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2341private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002342
Howard Hinnant73d21a42010-09-04 23:28:19 +00002343#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 template <class _Up>
2345 explicit unique_ptr(_Up);
2346 template <class _Up>
2347 unique_ptr(_Up __u,
2348 typename conditional<
2349 is_reference<deleter_type>::value,
2350 deleter_type,
2351 typename add_lvalue_reference<const deleter_type>::type>::type,
2352 typename enable_if
2353 <
2354 is_convertible<_Up, pointer>::value,
2355 __nat
2356 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002357#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358};
2359
2360template <class _Tp, class _Dp>
2361inline _LIBCPP_INLINE_VISIBILITY
2362void
2363swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) {__x.swap(__y);}
2364
2365template <class _T1, class _D1, class _T2, class _D2>
2366inline _LIBCPP_INLINE_VISIBILITY
2367bool
2368operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2369
2370template <class _T1, class _D1, class _T2, class _D2>
2371inline _LIBCPP_INLINE_VISIBILITY
2372bool
2373operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2374
2375template <class _T1, class _D1, class _T2, class _D2>
2376inline _LIBCPP_INLINE_VISIBILITY
2377bool
2378operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2379
2380template <class _T1, class _D1, class _T2, class _D2>
2381inline _LIBCPP_INLINE_VISIBILITY
2382bool
2383operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2384
2385template <class _T1, class _D1, class _T2, class _D2>
2386inline _LIBCPP_INLINE_VISIBILITY
2387bool
2388operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2389
2390template <class _T1, class _D1, class _T2, class _D2>
2391inline _LIBCPP_INLINE_VISIBILITY
2392bool
2393operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2394
Howard Hinnant21aefc32010-06-03 16:42:57 +00002395template <class> struct hash;
2396
2397template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002398struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002399 : public unary_function<_Tp*, size_t>
2400{
Howard Hinnant82894812010-09-22 16:48:34 +00002401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00002402 size_t operator()(_Tp* __v) const
2403 {
2404 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2405 return *__p;
2406 }
2407};
2408
2409template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002410struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002411{
2412 typedef unique_ptr<_Tp, _Dp> argument_type;
2413 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00002415 result_type operator()(const argument_type& __ptr) const
2416 {
2417 typedef typename argument_type::pointer pointer;
2418 return hash<pointer>()(__ptr.get());
2419 }
2420};
2421
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422struct __destruct_n
2423{
2424private:
2425 size_t size;
2426
2427 template <class _Tp>
2428 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type)
2429 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2430
2431 template <class _Tp>
2432 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type)
2433 {}
2434
2435 _LIBCPP_INLINE_VISIBILITY void __incr(false_type)
2436 {++size;}
2437 _LIBCPP_INLINE_VISIBILITY void __incr(true_type)
2438 {}
2439
2440 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type)
2441 {size = __s;}
2442 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type)
2443 {}
2444public:
2445 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) : size(__s) {}
2446
2447 template <class _Tp>
2448 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*)
Howard Hinnant1468b662010-11-19 22:17:28 +00002449 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002450
2451 template <class _Tp>
2452 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*)
Howard Hinnant1468b662010-11-19 22:17:28 +00002453 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454
2455 template <class _Tp>
2456 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p)
Howard Hinnant1468b662010-11-19 22:17:28 +00002457 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458};
2459
2460template <class _Alloc>
2461class __allocator_destructor
2462{
2463 typedef allocator_traits<_Alloc> __alloc_traits;
2464public:
2465 typedef typename __alloc_traits::pointer pointer;
2466 typedef typename __alloc_traits::size_type size_type;
2467private:
2468 _Alloc& __alloc_;
2469 size_type __s_;
2470public:
2471 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2472 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474 void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2475};
2476
2477template <class _InputIterator, class _ForwardIterator>
2478_ForwardIterator
2479uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2480{
2481 __destruct_n __d(0);
2482 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2483 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2484 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2485 ::new(&*__r) value_type(*__f);
2486 __h.release();
2487 return __r;
2488}
2489
2490template <class _InputIterator, class _Size, class _ForwardIterator>
2491_ForwardIterator
2492uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2493{
2494 __destruct_n __d(0);
2495 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2496 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2497 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2498 ::new(&*__r) value_type(*__f);
2499 __h.release();
2500 return __r;
2501}
2502
2503template <class _ForwardIterator, class _Tp>
2504void
2505uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2506{
2507 __destruct_n __d(0);
2508 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2509 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2510 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2511 ::new(&*__f) value_type(__x);
2512 __h.release();
2513}
2514
2515template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002516_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002517uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2518{
2519 __destruct_n __d(0);
2520 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2521 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2522 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2523 ::new(&*__f) value_type(__x);
2524 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002525 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526}
2527
Howard Hinnant82894812010-09-22 16:48:34 +00002528class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529 : public std::exception
2530{
2531public:
2532 virtual ~bad_weak_ptr() throw();
2533 virtual const char* what() const throw();
2534};
2535
2536template<class _Tp> class weak_ptr;
2537
2538class __shared_count
2539{
2540 __shared_count(const __shared_count&);
2541 __shared_count& operator=(const __shared_count&);
2542
2543protected:
2544 long __shared_owners_;
2545 virtual ~__shared_count();
2546private:
2547 virtual void __on_zero_shared() = 0;
2548
2549public:
Howard Hinnant82894812010-09-22 16:48:34 +00002550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 explicit __shared_count(long __refs = 0)
2552 : __shared_owners_(__refs) {}
2553
2554 void __add_shared();
Howard Hinnant28dbbe02010-11-16 21:33:17 +00002555 bool __release_shared();
Howard Hinnant82894812010-09-22 16:48:34 +00002556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557 long use_count() const {return __shared_owners_ + 1;}
2558};
2559
2560class __shared_weak_count
2561 : private __shared_count
2562{
2563 long __shared_weak_owners_;
2564
2565public:
Howard Hinnant82894812010-09-22 16:48:34 +00002566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567 explicit __shared_weak_count(long __refs = 0)
2568 : __shared_count(__refs),
2569 __shared_weak_owners_(__refs) {}
2570protected:
2571 virtual ~__shared_weak_count();
2572
2573public:
2574 void __add_shared();
2575 void __add_weak();
2576 void __release_shared();
2577 void __release_weak();
Howard Hinnant82894812010-09-22 16:48:34 +00002578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 long use_count() const {return __shared_count::use_count();}
2580 __shared_weak_count* lock();
2581
Howard Hinnantd4444702010-08-11 17:04:31 +00002582#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002583 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002584#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585private:
2586 virtual void __on_zero_shared_weak() = 0;
2587};
2588
2589template <class _Tp, class _Dp, class _Alloc>
2590class __shared_ptr_pointer
2591 : public __shared_weak_count
2592{
2593 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2594public:
Howard Hinnant82894812010-09-22 16:48:34 +00002595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002596 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2597 : __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2598
Howard Hinnantd4444702010-08-11 17:04:31 +00002599#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002601#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602
2603private:
2604 virtual void __on_zero_shared();
2605 virtual void __on_zero_shared_weak();
2606};
2607
Howard Hinnantd4444702010-08-11 17:04:31 +00002608#ifndef _LIBCPP_NO_RTTI
2609
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610template <class _Tp, class _Dp, class _Alloc>
2611const void*
2612__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
2613{
2614 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2615}
2616
Howard Hinnant324bb032010-08-22 00:02:43 +00002617#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002618
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619template <class _Tp, class _Dp, class _Alloc>
2620void
2621__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
2622{
2623 __data_.first().second()(__data_.first().first());
2624 __data_.first().second().~_Dp();
2625}
2626
2627template <class _Tp, class _Dp, class _Alloc>
2628void
2629__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak()
2630{
2631 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2632 __data_.second().~_Alloc();
2633 __a.deallocate(this, 1);
2634}
2635
2636template <class _Tp, class _Alloc>
2637class __shared_ptr_emplace
2638 : public __shared_weak_count
2639{
2640 __compressed_pair<_Alloc, _Tp> __data_;
2641public:
2642#ifndef _LIBCPP_HAS_NO_VARIADICS
2643
Howard Hinnant82894812010-09-22 16:48:34 +00002644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645 __shared_ptr_emplace(_Alloc __a)
2646 : __data_(_STD::move(__a)) {}
2647
2648 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002650 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2651 : __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2652
2653#else // _LIBCPP_HAS_NO_VARIADICS
2654
Howard Hinnant82894812010-09-22 16:48:34 +00002655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 __shared_ptr_emplace(_Alloc __a)
2657 : __data_(__a) {}
2658
2659 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2662 : __data_(__a, _Tp(__a0)) {}
2663
2664 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2667 : __data_(__a, _Tp(__a0, __a1)) {}
2668
2669 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2672 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2673
2674#endif // _LIBCPP_HAS_NO_VARIADICS
2675
2676private:
2677 virtual void __on_zero_shared();
2678 virtual void __on_zero_shared_weak();
2679public:
Howard Hinnant82894812010-09-22 16:48:34 +00002680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 _Tp* get() {return &__data_.second();}
2682};
2683
2684template <class _Tp, class _Alloc>
2685void
2686__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared()
2687{
2688 __data_.second().~_Tp();
2689}
2690
2691template <class _Tp, class _Alloc>
2692void
2693__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak()
2694{
2695 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2696 __data_.first().~_Alloc();
2697 __a.deallocate(this, 1);
2698}
2699
2700template<class _Tp> class enable_shared_from_this;
2701
2702template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002703class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002704{
Howard Hinnant324bb032010-08-22 00:02:43 +00002705public:
2706 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707private:
2708 element_type* __ptr_;
2709 __shared_weak_count* __cntrl_;
2710
2711 struct __nat {int __for_bool_;};
2712public:
2713 shared_ptr();
2714 shared_ptr(nullptr_t);
2715 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00002716 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2717 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002718 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2719 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant324bb032010-08-22 00:02:43 +00002720 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002721 shared_ptr(const shared_ptr& __r);
2722 template<class _Yp>
2723 shared_ptr(const shared_ptr<_Yp>& __r,
2724 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002725#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002726 shared_ptr(shared_ptr&& __r);
2727 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002728 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002729#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002730 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002731 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002732#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002733 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2734#else
2735 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002737#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002739 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740public:
2741 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2742 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2743 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2744 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002745#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2747 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2748 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2749 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002750#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751
2752 ~shared_ptr();
2753
Howard Hinnant324bb032010-08-22 00:02:43 +00002754 shared_ptr& operator=(const shared_ptr& __r);
2755 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002756#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002757 shared_ptr& operator=(shared_ptr&& __r);
2758 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2759 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002760#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002761 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002763#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002765 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766public:
Howard Hinnant324bb032010-08-22 00:02:43 +00002767 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002768#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002769 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770#endif
2771
2772 void swap(shared_ptr& __r);
2773 void reset();
2774 template<class _Yp> void reset(_Yp* __p);
2775 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2776 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2777
Howard Hinnant82894812010-09-22 16:48:34 +00002778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779 element_type* get() const {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781 typename add_lvalue_reference<element_type>::type operator*() const {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 element_type* operator->() const {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 bool unique() const {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00002788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 bool empty() const {return __cntrl_ == 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 /*explicit*/ operator bool() const {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002792 template <class _U>
2793 _LIBCPP_INLINE_VISIBILITY
2794 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002796 template <class _U>
2797 _LIBCPP_INLINE_VISIBILITY
2798 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799 {return __cntrl_ < __p.__cntrl_;}
2800
Howard Hinnantd4444702010-08-11 17:04:31 +00002801#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002802 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002804 _Dp* __get_deleter() const
2805 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002806#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807
2808#ifndef _LIBCPP_HAS_NO_VARIADICS
2809
2810 template<class ..._Args>
2811 static
2812 shared_ptr<_Tp>
2813 make_shared(_Args&& ...__args);
2814
2815 template<class _Alloc, class ..._Args>
2816 static
2817 shared_ptr<_Tp>
2818 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2819
2820#else // _LIBCPP_HAS_NO_VARIADICS
2821
2822 static shared_ptr<_Tp> make_shared();
2823
2824 template<class _A0>
2825 static shared_ptr<_Tp> make_shared(_A0&);
2826
2827 template<class _A0, class _A1>
2828 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2829
2830 template<class _A0, class _A1, class _A2>
2831 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2832
2833 template<class _Alloc>
2834 static shared_ptr<_Tp>
2835 allocate_shared(const _Alloc& __a);
2836
2837 template<class _Alloc, class _A0>
2838 static shared_ptr<_Tp>
2839 allocate_shared(const _Alloc& __a, _A0& __a0);
2840
2841 template<class _Alloc, class _A0, class _A1>
2842 static shared_ptr<_Tp>
2843 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2844
2845 template<class _Alloc, class _A0, class _A1, class _A2>
2846 static shared_ptr<_Tp>
2847 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2848
2849#endif // _LIBCPP_HAS_NO_VARIADICS
2850
2851private:
2852
2853 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00002854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002855 void
2856 __enable_weak_this(const enable_shared_from_this<_Yp>* __e)
2857 {
2858 if (__e)
2859 __e->__weak_this_ = *this;
2860 }
2861
Howard Hinnant82894812010-09-22 16:48:34 +00002862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002863 void __enable_weak_this(const void*) {}
2864
Howard Hinnant82894812010-09-22 16:48:34 +00002865 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
2866 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867};
2868
2869template<class _Tp>
2870inline _LIBCPP_INLINE_VISIBILITY
2871shared_ptr<_Tp>::shared_ptr()
2872 : __ptr_(0),
2873 __cntrl_(0)
2874{
2875}
2876
2877template<class _Tp>
2878inline _LIBCPP_INLINE_VISIBILITY
2879shared_ptr<_Tp>::shared_ptr(nullptr_t)
2880 : __ptr_(0),
2881 __cntrl_(0)
2882{
2883}
2884
2885template<class _Tp>
2886template<class _Yp>
2887shared_ptr<_Tp>::shared_ptr(_Yp* __p)
2888 : __ptr_(__p)
2889{
2890 unique_ptr<_Yp> __hold(__p);
2891 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2892 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
2893 __hold.release();
2894 __enable_weak_this(__p);
2895}
2896
2897template<class _Tp>
2898template<class _Yp, class _Dp>
2899shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
2900 : __ptr_(__p)
2901{
2902#ifndef _LIBCPP_NO_EXCEPTIONS
2903 try
2904 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002905#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
2907 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
2908 __enable_weak_this(__p);
2909#ifndef _LIBCPP_NO_EXCEPTIONS
2910 }
2911 catch (...)
2912 {
2913 __d(__p);
2914 throw;
2915 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002916#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917}
2918
2919template<class _Tp>
2920template<class _Dp>
2921shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
2922 : __ptr_(0)
2923{
2924#ifndef _LIBCPP_NO_EXCEPTIONS
2925 try
2926 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002927#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002928 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
2929 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
2930#ifndef _LIBCPP_NO_EXCEPTIONS
2931 }
2932 catch (...)
2933 {
2934 __d(__p);
2935 throw;
2936 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002937#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002938}
2939
2940template<class _Tp>
2941template<class _Yp, class _Dp, class _Alloc>
2942shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
2943 : __ptr_(__p)
2944{
2945#ifndef _LIBCPP_NO_EXCEPTIONS
2946 try
2947 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002948#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002949 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
2950 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2951 typedef __allocator_destructor<_A2> _D2;
2952 _A2 __a2(__a);
2953 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2954 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2955 __cntrl_ = __hold2.release();
2956 __enable_weak_this(__p);
2957#ifndef _LIBCPP_NO_EXCEPTIONS
2958 }
2959 catch (...)
2960 {
2961 __d(__p);
2962 throw;
2963 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002964#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002965}
2966
2967template<class _Tp>
2968template<class _Dp, class _Alloc>
2969shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
2970 : __ptr_(0)
2971{
2972#ifndef _LIBCPP_NO_EXCEPTIONS
2973 try
2974 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002975#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002976 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
2977 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2978 typedef __allocator_destructor<_A2> _D2;
2979 _A2 __a2(__a);
2980 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2981 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2982 __cntrl_ = __hold2.release();
2983#ifndef _LIBCPP_NO_EXCEPTIONS
2984 }
2985 catch (...)
2986 {
2987 __d(__p);
2988 throw;
2989 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002990#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002991}
2992
2993template<class _Tp>
2994template<class _Yp>
2995inline _LIBCPP_INLINE_VISIBILITY
2996shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p)
2997 : __ptr_(__p),
2998 __cntrl_(__r.__cntrl_)
2999{
3000 if (__cntrl_)
3001 __cntrl_->__add_shared();
3002}
3003
3004template<class _Tp>
3005inline _LIBCPP_INLINE_VISIBILITY
3006shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r)
3007 : __ptr_(__r.__ptr_),
3008 __cntrl_(__r.__cntrl_)
3009{
3010 if (__cntrl_)
3011 __cntrl_->__add_shared();
3012}
3013
3014template<class _Tp>
3015template<class _Yp>
3016inline _LIBCPP_INLINE_VISIBILITY
3017shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3018 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3019 : __ptr_(__r.__ptr_),
3020 __cntrl_(__r.__cntrl_)
3021{
3022 if (__cntrl_)
3023 __cntrl_->__add_shared();
3024}
3025
Howard Hinnant73d21a42010-09-04 23:28:19 +00003026#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003027
3028template<class _Tp>
3029inline _LIBCPP_INLINE_VISIBILITY
3030shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r)
3031 : __ptr_(__r.__ptr_),
3032 __cntrl_(__r.__cntrl_)
3033{
3034 __r.__ptr_ = 0;
3035 __r.__cntrl_ = 0;
3036}
3037
3038template<class _Tp>
3039template<class _Yp>
3040inline _LIBCPP_INLINE_VISIBILITY
3041shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3042 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3043 : __ptr_(__r.__ptr_),
3044 __cntrl_(__r.__cntrl_)
3045{
3046 __r.__ptr_ = 0;
3047 __r.__cntrl_ = 0;
3048}
3049
Howard Hinnant73d21a42010-09-04 23:28:19 +00003050#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051
3052template<class _Tp>
3053template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003054#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003055shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3056#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003057shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003058#endif
3059 : __ptr_(__r.get())
3060{
3061 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3062 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3063 __enable_weak_this(__r.get());
3064 __r.release();
3065}
3066
3067template<class _Tp>
3068template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003069#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003070shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3071#else
3072shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3073#endif
3074 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3075 : __ptr_(__r.get())
3076{
3077 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3078 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3079 __enable_weak_this(__r.get());
3080 __r.release();
3081}
3082
3083template<class _Tp>
3084template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003085#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3087#else
3088shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3089#endif
3090 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3091 : __ptr_(__r.get())
3092{
3093 typedef __shared_ptr_pointer<_Yp*,
3094 reference_wrapper<typename remove_reference<_Dp>::type>,
3095 allocator<_Yp> > _CntrlBlk;
3096 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3097 __enable_weak_this(__r.get());
3098 __r.release();
3099}
3100
3101#ifndef _LIBCPP_HAS_NO_VARIADICS
3102
3103template<class _Tp>
3104template<class ..._Args>
3105shared_ptr<_Tp>
3106shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3107{
3108 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3109 typedef allocator<_CntrlBlk> _A2;
3110 typedef __allocator_destructor<_A2> _D2;
3111 _A2 __a2;
3112 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3113 ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3114 shared_ptr<_Tp> __r;
3115 __r.__ptr_ = __hold2.get()->get();
3116 __r.__cntrl_ = __hold2.release();
3117 __r.__enable_weak_this(__r.__ptr_);
3118 return __r;
3119}
3120
3121template<class _Tp>
3122template<class _Alloc, class ..._Args>
3123shared_ptr<_Tp>
3124shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3125{
3126 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3127 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3128 typedef __allocator_destructor<_A2> _D2;
3129 _A2 __a2(__a);
3130 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3131 ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3132 shared_ptr<_Tp> __r;
3133 __r.__ptr_ = __hold2.get()->get();
3134 __r.__cntrl_ = __hold2.release();
3135 __r.__enable_weak_this(__r.__ptr_);
3136 return __r;
3137}
3138
3139#else // _LIBCPP_HAS_NO_VARIADICS
3140
3141template<class _Tp>
3142shared_ptr<_Tp>
3143shared_ptr<_Tp>::make_shared()
3144{
3145 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3146 typedef allocator<_CntrlBlk> _Alloc2;
3147 typedef __allocator_destructor<_Alloc2> _D2;
3148 _Alloc2 __alloc2;
3149 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3150 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3151 shared_ptr<_Tp> __r;
3152 __r.__ptr_ = __hold2.get()->get();
3153 __r.__cntrl_ = __hold2.release();
3154 __r.__enable_weak_this(__r.__ptr_);
3155 return __r;
3156}
3157
3158template<class _Tp>
3159template<class _A0>
3160shared_ptr<_Tp>
3161shared_ptr<_Tp>::make_shared(_A0& __a0)
3162{
3163 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3164 typedef allocator<_CntrlBlk> _Alloc2;
3165 typedef __allocator_destructor<_Alloc2> _D2;
3166 _Alloc2 __alloc2;
3167 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3168 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3169 shared_ptr<_Tp> __r;
3170 __r.__ptr_ = __hold2.get()->get();
3171 __r.__cntrl_ = __hold2.release();
3172 __r.__enable_weak_this(__r.__ptr_);
3173 return __r;
3174}
3175
3176template<class _Tp>
3177template<class _A0, class _A1>
3178shared_ptr<_Tp>
3179shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3180{
3181 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3182 typedef allocator<_CntrlBlk> _Alloc2;
3183 typedef __allocator_destructor<_Alloc2> _D2;
3184 _Alloc2 __alloc2;
3185 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3186 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3187 shared_ptr<_Tp> __r;
3188 __r.__ptr_ = __hold2.get()->get();
3189 __r.__cntrl_ = __hold2.release();
3190 __r.__enable_weak_this(__r.__ptr_);
3191 return __r;
3192}
3193
3194template<class _Tp>
3195template<class _A0, class _A1, class _A2>
3196shared_ptr<_Tp>
3197shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3198{
3199 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3200 typedef allocator<_CntrlBlk> _Alloc2;
3201 typedef __allocator_destructor<_Alloc2> _D2;
3202 _Alloc2 __alloc2;
3203 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3204 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3205 shared_ptr<_Tp> __r;
3206 __r.__ptr_ = __hold2.get()->get();
3207 __r.__cntrl_ = __hold2.release();
3208 __r.__enable_weak_this(__r.__ptr_);
3209 return __r;
3210}
3211
3212template<class _Tp>
3213template<class _Alloc>
3214shared_ptr<_Tp>
3215shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3216{
3217 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3218 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3219 typedef __allocator_destructor<_Alloc2> _D2;
3220 _Alloc2 __alloc2(__a);
3221 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3222 ::new(__hold2.get()) _CntrlBlk(__a);
3223 shared_ptr<_Tp> __r;
3224 __r.__ptr_ = __hold2.get()->get();
3225 __r.__cntrl_ = __hold2.release();
3226 __r.__enable_weak_this(__r.__ptr_);
3227 return __r;
3228}
3229
3230template<class _Tp>
3231template<class _Alloc, class _A0>
3232shared_ptr<_Tp>
3233shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3234{
3235 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3236 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3237 typedef __allocator_destructor<_Alloc2> _D2;
3238 _Alloc2 __alloc2(__a);
3239 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3240 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3241 shared_ptr<_Tp> __r;
3242 __r.__ptr_ = __hold2.get()->get();
3243 __r.__cntrl_ = __hold2.release();
3244 __r.__enable_weak_this(__r.__ptr_);
3245 return __r;
3246}
3247
3248template<class _Tp>
3249template<class _Alloc, class _A0, class _A1>
3250shared_ptr<_Tp>
3251shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3252{
3253 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3254 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3255 typedef __allocator_destructor<_Alloc2> _D2;
3256 _Alloc2 __alloc2(__a);
3257 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3258 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3259 shared_ptr<_Tp> __r;
3260 __r.__ptr_ = __hold2.get()->get();
3261 __r.__cntrl_ = __hold2.release();
3262 __r.__enable_weak_this(__r.__ptr_);
3263 return __r;
3264}
3265
3266template<class _Tp>
3267template<class _Alloc, class _A0, class _A1, class _A2>
3268shared_ptr<_Tp>
3269shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3270{
3271 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3272 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3273 typedef __allocator_destructor<_Alloc2> _D2;
3274 _Alloc2 __alloc2(__a);
3275 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3276 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3277 shared_ptr<_Tp> __r;
3278 __r.__ptr_ = __hold2.get()->get();
3279 __r.__cntrl_ = __hold2.release();
3280 __r.__enable_weak_this(__r.__ptr_);
3281 return __r;
3282}
3283
3284#endif // _LIBCPP_HAS_NO_VARIADICS
3285
3286template<class _Tp>
3287shared_ptr<_Tp>::~shared_ptr()
3288{
3289 if (__cntrl_)
3290 __cntrl_->__release_shared();
3291}
3292
3293template<class _Tp>
3294inline _LIBCPP_INLINE_VISIBILITY
3295shared_ptr<_Tp>&
3296shared_ptr<_Tp>::operator=(const shared_ptr& __r)
3297{
3298 shared_ptr(__r).swap(*this);
3299 return *this;
3300}
3301
3302template<class _Tp>
3303template<class _Yp>
3304inline _LIBCPP_INLINE_VISIBILITY
3305shared_ptr<_Tp>&
3306shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r)
3307{
3308 shared_ptr(__r).swap(*this);
3309 return *this;
3310}
3311
Howard Hinnant73d21a42010-09-04 23:28:19 +00003312#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003313
3314template<class _Tp>
3315inline _LIBCPP_INLINE_VISIBILITY
3316shared_ptr<_Tp>&
3317shared_ptr<_Tp>::operator=(shared_ptr&& __r)
3318{
3319 shared_ptr(_STD::move(__r)).swap(*this);
3320 return *this;
3321}
3322
3323template<class _Tp>
3324template<class _Yp>
3325inline _LIBCPP_INLINE_VISIBILITY
3326shared_ptr<_Tp>&
3327shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3328{
3329 shared_ptr(_STD::move(__r)).swap(*this);
3330 return *this;
3331}
3332
3333template<class _Tp>
3334template<class _Yp>
3335inline _LIBCPP_INLINE_VISIBILITY
3336shared_ptr<_Tp>&
3337shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3338{
3339 shared_ptr(__r).swap(*this);
3340 return *this;
3341}
3342
3343template<class _Tp>
3344template <class _Yp, class _Dp>
3345inline _LIBCPP_INLINE_VISIBILITY
3346shared_ptr<_Tp>&
3347shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3348{
3349 shared_ptr(_STD::move(__r)).swap(*this);
3350 return *this;
3351}
3352
Howard Hinnant73d21a42010-09-04 23:28:19 +00003353#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354
3355template<class _Tp>
3356template<class _Yp>
3357inline _LIBCPP_INLINE_VISIBILITY
3358shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003359shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003360{
3361 shared_ptr(__r).swap(*this);
3362 return *this;
3363}
3364
3365template<class _Tp>
3366template <class _Yp, class _Dp>
3367inline _LIBCPP_INLINE_VISIBILITY
3368shared_ptr<_Tp>&
3369shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3370{
3371 shared_ptr(_STD::move(__r)).swap(*this);
3372 return *this;
3373}
3374
Howard Hinnant73d21a42010-09-04 23:28:19 +00003375#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003376
3377template<class _Tp>
3378inline _LIBCPP_INLINE_VISIBILITY
3379void
3380shared_ptr<_Tp>::swap(shared_ptr& __r)
3381{
3382 _STD::swap(__ptr_, __r.__ptr_);
3383 _STD::swap(__cntrl_, __r.__cntrl_);
3384}
3385
3386template<class _Tp>
3387inline _LIBCPP_INLINE_VISIBILITY
3388void
3389shared_ptr<_Tp>::reset()
3390{
3391 shared_ptr().swap(*this);
3392}
3393
3394template<class _Tp>
3395template<class _Yp>
3396inline _LIBCPP_INLINE_VISIBILITY
3397void
3398shared_ptr<_Tp>::reset(_Yp* __p)
3399{
3400 shared_ptr(__p).swap(*this);
3401}
3402
3403template<class _Tp>
3404template<class _Yp, class _Dp>
3405inline _LIBCPP_INLINE_VISIBILITY
3406void
3407shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3408{
3409 shared_ptr(__p, __d).swap(*this);
3410}
3411
3412template<class _Tp>
3413template<class _Yp, class _Dp, class _Alloc>
3414inline _LIBCPP_INLINE_VISIBILITY
3415void
3416shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3417{
3418 shared_ptr(__p, __d, __a).swap(*this);
3419}
3420
3421#ifndef _LIBCPP_HAS_NO_VARIADICS
3422
Howard Hinnant324bb032010-08-22 00:02:43 +00003423template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424inline _LIBCPP_INLINE_VISIBILITY
3425shared_ptr<_Tp>
3426make_shared(_Args&& ...__args)
3427{
3428 return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3429}
3430
Howard Hinnant324bb032010-08-22 00:02:43 +00003431template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003432inline _LIBCPP_INLINE_VISIBILITY
3433shared_ptr<_Tp>
3434allocate_shared(const _Alloc& __a, _Args&& ...__args)
3435{
3436 return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3437}
3438
3439#else // _LIBCPP_HAS_NO_VARIADICS
3440
3441template<class _Tp>
3442inline _LIBCPP_INLINE_VISIBILITY
3443shared_ptr<_Tp>
3444make_shared()
3445{
3446 return shared_ptr<_Tp>::make_shared();
3447}
3448
3449template<class _Tp, class _A0>
3450inline _LIBCPP_INLINE_VISIBILITY
3451shared_ptr<_Tp>
3452make_shared(_A0& __a0)
3453{
3454 return shared_ptr<_Tp>::make_shared(__a0);
3455}
3456
3457template<class _Tp, class _A0, class _A1>
3458inline _LIBCPP_INLINE_VISIBILITY
3459shared_ptr<_Tp>
3460make_shared(_A0& __a0, _A1& __a1)
3461{
3462 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3463}
3464
Howard Hinnant324bb032010-08-22 00:02:43 +00003465template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003466inline _LIBCPP_INLINE_VISIBILITY
3467shared_ptr<_Tp>
3468make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3469{
3470 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3471}
3472
3473template<class _Tp, class _Alloc>
3474inline _LIBCPP_INLINE_VISIBILITY
3475shared_ptr<_Tp>
3476allocate_shared(const _Alloc& __a)
3477{
3478 return shared_ptr<_Tp>::allocate_shared(__a);
3479}
3480
3481template<class _Tp, class _Alloc, class _A0>
3482inline _LIBCPP_INLINE_VISIBILITY
3483shared_ptr<_Tp>
3484allocate_shared(const _Alloc& __a, _A0& __a0)
3485{
3486 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3487}
3488
3489template<class _Tp, class _Alloc, class _A0, class _A1>
3490inline _LIBCPP_INLINE_VISIBILITY
3491shared_ptr<_Tp>
3492allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3493{
3494 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3495}
3496
3497template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3498inline _LIBCPP_INLINE_VISIBILITY
3499shared_ptr<_Tp>
3500allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3501{
3502 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3503}
3504
3505#endif // _LIBCPP_HAS_NO_VARIADICS
3506
3507template<class _Tp, class _Up>
3508inline _LIBCPP_INLINE_VISIBILITY
3509bool
3510operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3511{
3512 return __x.get() == __y.get();
3513}
3514
3515template<class _Tp, class _Up>
3516inline _LIBCPP_INLINE_VISIBILITY
3517bool
3518operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3519{
3520 return !(__x == __y);
3521}
3522
3523template<class _Tp, class _Up>
3524inline _LIBCPP_INLINE_VISIBILITY
3525bool
3526operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3527{
3528 return __x.get() < __y.get();
3529}
3530
3531template<class _Tp>
3532inline _LIBCPP_INLINE_VISIBILITY
3533void
3534swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y)
3535{
3536 __x.swap(__y);
3537}
3538
3539template<class _Tp, class _Up>
3540inline _LIBCPP_INLINE_VISIBILITY
3541shared_ptr<_Tp>
3542static_pointer_cast(const shared_ptr<_Up>& __r)
3543{
3544 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3545}
3546
3547template<class _Tp, class _Up>
3548inline _LIBCPP_INLINE_VISIBILITY
3549shared_ptr<_Tp>
3550dynamic_pointer_cast(const shared_ptr<_Up>& __r)
3551{
3552 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3553 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3554}
3555
3556template<class _Tp, class _Up>
3557shared_ptr<_Tp>
3558const_pointer_cast(const shared_ptr<_Up>& __r)
3559{
3560 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3561}
3562
Howard Hinnantd4444702010-08-11 17:04:31 +00003563#ifndef _LIBCPP_NO_RTTI
3564
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003565template<class _Dp, class _Tp>
3566inline _LIBCPP_INLINE_VISIBILITY
3567_Dp*
3568get_deleter(const shared_ptr<_Tp>& __p)
3569{
3570 return __p.template __get_deleter<_Dp>();
3571}
3572
Howard Hinnant324bb032010-08-22 00:02:43 +00003573#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003574
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003575template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003576class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003577{
Howard Hinnant324bb032010-08-22 00:02:43 +00003578public:
3579 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003580private:
3581 element_type* __ptr_;
3582 __shared_weak_count* __cntrl_;
3583
Howard Hinnant324bb032010-08-22 00:02:43 +00003584public:
3585 weak_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
3587 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003588 weak_ptr(weak_ptr const& __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003590 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3591
3592 ~weak_ptr();
3593
3594 weak_ptr& operator=(weak_ptr const& __r);
3595 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r);
3596 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r);
3597
3598 void swap(weak_ptr& __r);
3599 void reset();
3600
Howard Hinnant82894812010-09-22 16:48:34 +00003601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003602 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604 bool expired() const {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
Howard Hinnant324bb032010-08-22 00:02:43 +00003605 shared_ptr<_Tp> lock() const;
Howard Hinnant82894812010-09-22 16:48:34 +00003606 template<class _Up>
3607 _LIBCPP_INLINE_VISIBILITY
3608 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003609 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003610 template<class _Up>
3611 _LIBCPP_INLINE_VISIBILITY
3612 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003613 {return __cntrl_ < __r.__cntrl_;}
3614
Howard Hinnant82894812010-09-22 16:48:34 +00003615 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3616 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003617};
3618
3619template<class _Tp>
3620inline _LIBCPP_INLINE_VISIBILITY
3621weak_ptr<_Tp>::weak_ptr()
3622 : __ptr_(0),
3623 __cntrl_(0)
3624{
3625}
3626
3627template<class _Tp>
3628inline _LIBCPP_INLINE_VISIBILITY
3629weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r)
3630 : __ptr_(__r.__ptr_),
3631 __cntrl_(__r.__cntrl_)
3632{
3633 if (__cntrl_)
3634 __cntrl_->__add_weak();
3635}
3636
3637template<class _Tp>
3638template<class _Yp>
3639inline _LIBCPP_INLINE_VISIBILITY
3640weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3641 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3642 : __ptr_(__r.__ptr_),
3643 __cntrl_(__r.__cntrl_)
3644{
3645 if (__cntrl_)
3646 __cntrl_->__add_weak();
3647}
3648
3649template<class _Tp>
3650template<class _Yp>
3651inline _LIBCPP_INLINE_VISIBILITY
3652weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3653 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3654 : __ptr_(__r.__ptr_),
3655 __cntrl_(__r.__cntrl_)
3656{
3657 if (__cntrl_)
3658 __cntrl_->__add_weak();
3659}
3660
3661template<class _Tp>
3662weak_ptr<_Tp>::~weak_ptr()
3663{
3664 if (__cntrl_)
3665 __cntrl_->__release_weak();
3666}
3667
3668template<class _Tp>
3669inline _LIBCPP_INLINE_VISIBILITY
3670weak_ptr<_Tp>&
3671weak_ptr<_Tp>::operator=(weak_ptr const& __r)
3672{
3673 weak_ptr(__r).swap(*this);
3674 return *this;
3675}
3676
3677template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003678template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003679inline _LIBCPP_INLINE_VISIBILITY
3680weak_ptr<_Tp>&
3681weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r)
3682{
3683 weak_ptr(__r).swap(*this);
3684 return *this;
3685}
3686
3687template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003688template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003689inline _LIBCPP_INLINE_VISIBILITY
3690weak_ptr<_Tp>&
3691weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r)
3692{
3693 weak_ptr(__r).swap(*this);
3694 return *this;
3695}
3696
3697template<class _Tp>
3698inline _LIBCPP_INLINE_VISIBILITY
3699void
3700weak_ptr<_Tp>::swap(weak_ptr& __r)
3701{
3702 _STD::swap(__ptr_, __r.__ptr_);
3703 _STD::swap(__cntrl_, __r.__cntrl_);
3704}
3705
3706template<class _Tp>
3707inline _LIBCPP_INLINE_VISIBILITY
3708void
3709swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y)
3710{
3711 __x.swap(__y);
3712}
3713
3714template<class _Tp>
3715inline _LIBCPP_INLINE_VISIBILITY
3716void
3717weak_ptr<_Tp>::reset()
3718{
3719 weak_ptr().swap(*this);
3720}
3721
3722template<class _Tp>
3723template<class _Yp>
3724shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3725 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3726 : __ptr_(__r.__ptr_),
3727 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3728{
3729 if (__cntrl_ == 0)
3730#ifndef _LIBCPP_NO_EXCEPTIONS
3731 throw bad_weak_ptr();
3732#else
3733 assert(!"bad_weak_ptr");
3734#endif
3735}
3736
3737template<class _Tp>
3738shared_ptr<_Tp>
3739weak_ptr<_Tp>::lock() const
3740{
3741 shared_ptr<_Tp> __r;
3742 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3743 if (__r.__cntrl_)
3744 __r.__ptr_ = __ptr_;
3745 return __r;
3746}
3747
Howard Hinnant324bb032010-08-22 00:02:43 +00003748template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749
3750template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003751struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003752 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00003753{
3754 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003756 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3757 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003759 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3760 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003762 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3763 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003764};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003765
3766template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003767struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003768 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3769{
Howard Hinnant324bb032010-08-22 00:02:43 +00003770 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3773 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3776 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003778 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3779 {return __x.owner_before(__y);}
3780};
3781
3782template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003783class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003784{
3785 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00003786protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003788 enable_shared_from_this() {}
Howard Hinnant82894812010-09-22 16:48:34 +00003789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790 enable_shared_from_this(enable_shared_from_this const&) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792 enable_shared_from_this& operator=(enable_shared_from_this const&) {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003795public:
Howard Hinnant82894812010-09-22 16:48:34 +00003796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797 shared_ptr<_Tp> shared_from_this() {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00003798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003799 shared_ptr<_Tp const> shared_from_this() const {return shared_ptr<const _Tp>(__weak_this_);}
3800
3801 template <class _Up> friend class shared_ptr;
3802};
3803
Howard Hinnant21aefc32010-06-03 16:42:57 +00003804template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003805struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003806{
3807 typedef shared_ptr<_Tp> argument_type;
3808 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00003810 result_type operator()(const argument_type& __ptr) const
3811 {
3812 return hash<_Tp*>()(__ptr.get());
3813 }
3814};
3815
Howard Hinnant324bb032010-08-22 00:02:43 +00003816//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00003817struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003818{
3819 enum _
3820 {
3821 relaxed,
3822 preferred,
3823 strict
3824 };
3825
3826 _ __v_;
3827
Howard Hinnant82894812010-09-22 16:48:34 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003829 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003831 operator int() const {return __v_;}
3832};
3833
3834void declare_reachable(void* __p);
3835void declare_no_pointers(char* __p, size_t __n);
3836void undeclare_no_pointers(char* __p, size_t __n);
3837pointer_safety get_pointer_safety();
3838void* __undeclare_reachable(void*);
3839
3840template <class _Tp>
3841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00003842_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003843undeclare_reachable(_Tp* __p)
3844{
3845 return static_cast<_Tp*>(__undeclare_reachable(__p));
3846}
3847
3848void* align(size_t, size_t, void*&, size_t&);
3849
3850_LIBCPP_END_NAMESPACE_STD
3851
3852#endif // _LIBCPP_MEMORY