blob: d9dda72aac760ff0ae6b34b0807e391fb5dfd457 [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
Howard Hinnant6b41c602011-05-11 20:21:19 +0000797 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798#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 =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001334 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 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 Hinnantac38bae2011-01-11 20:02:45 +00001490#ifndef _LIBCPP_HAS_NO_VARIADICS
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 Hinnantac38bae2011-01-11 20:02:45 +00001508#endif // _LIBCPP_HAS_NO_VARIADICS
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() {}
Howard Hinnant2529d022011-02-02 17:36:20 +00001528 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const {return _STD::addressof(__x);}
1529 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const {return _STD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 _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 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001549# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550 template <class _A0>
1551 _LIBCPP_INLINE_VISIBILITY
1552 typename enable_if
1553 <
1554 !is_convertible<_A0, __rv<_A0> >::value,
1555 void
1556 >::type
1557 construct(pointer __p, _A0& __a0)
1558 {
1559 ::new((void*)__p) _Tp(__a0);
1560 }
1561 template <class _A0>
1562 _LIBCPP_INLINE_VISIBILITY
1563 typename enable_if
1564 <
1565 !is_convertible<_A0, __rv<_A0> >::value,
1566 void
1567 >::type
1568 construct(pointer __p, const _A0& __a0)
1569 {
1570 ::new((void*)__p) _Tp(__a0);
1571 }
1572 template <class _A0>
1573 _LIBCPP_INLINE_VISIBILITY
1574 typename enable_if
1575 <
1576 is_convertible<_A0, __rv<_A0> >::value,
1577 void
1578 >::type
1579 construct(pointer __p, _A0 __a0)
1580 {
1581 ::new((void*)__p) _Tp(_STD::move(__a0));
1582 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001583# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584 template <class _A0, class _A1>
1585 _LIBCPP_INLINE_VISIBILITY
1586 void
1587 construct(pointer __p, _A0& __a0, _A1& __a1)
1588 {
1589 ::new((void*)__p) _Tp(__a0, __a1);
1590 }
1591 template <class _A0, class _A1>
1592 _LIBCPP_INLINE_VISIBILITY
1593 void
1594 construct(pointer __p, const _A0& __a0, _A1& __a1)
1595 {
1596 ::new((void*)__p) _Tp(__a0, __a1);
1597 }
1598 template <class _A0, class _A1>
1599 _LIBCPP_INLINE_VISIBILITY
1600 void
1601 construct(pointer __p, _A0& __a0, const _A1& __a1)
1602 {
1603 ::new((void*)__p) _Tp(__a0, __a1);
1604 }
1605 template <class _A0, class _A1>
1606 _LIBCPP_INLINE_VISIBILITY
1607 void
1608 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1609 {
1610 ::new((void*)__p) _Tp(__a0, __a1);
1611 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001612#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1614};
1615
1616template <class _Tp, class _Up>
1617inline _LIBCPP_INLINE_VISIBILITY
1618bool operator==(const allocator<_Tp>&, const allocator<_Up>&) throw() {return true;}
1619
1620template <class _Tp, class _Up>
1621inline _LIBCPP_INLINE_VISIBILITY
1622bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) throw() {return false;}
1623
1624template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001625class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626 : public iterator<output_iterator_tag,
1627 _Tp, // purposefully not C++03
1628 ptrdiff_t, // purposefully not C++03
1629 _Tp*, // purposefully not C++03
1630 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1631{
1632private:
1633 _OutputIterator __x_;
1634public:
1635 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1636 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1637 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1638 {::new(&*__x_) _Tp(__element); return *this;}
1639 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1640 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1641 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1642};
1643
1644template <class _Tp>
1645pair<_Tp*, ptrdiff_t>
1646get_temporary_buffer(ptrdiff_t __n)
1647{
1648 pair<_Tp*, ptrdiff_t> __r(0, 0);
1649 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1650 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1651 / sizeof(_Tp);
1652 if (__n > __m)
1653 __n = __m;
1654 while (__n > 0)
1655 {
1656 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1657 if (__r.first)
1658 {
1659 __r.second = __n;
1660 break;
1661 }
1662 __n /= 2;
1663 }
1664 return __r;
1665}
1666
1667template <class _Tp>
1668inline _LIBCPP_INLINE_VISIBILITY
1669void return_temporary_buffer(_Tp* __p) {::operator delete(__p);}
1670
1671template <class _Tp>
1672struct auto_ptr_ref
1673{
1674 _Tp* __ptr_;
1675};
1676
1677template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001678class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679{
1680private:
1681 _Tp* __ptr_;
1682public:
1683 typedef _Tp element_type;
1684
1685 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1686 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1687 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1688 : __ptr_(__p.release()) {}
1689 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1690 {reset(__p.release()); return *this;}
1691 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1692 {reset(__p.release()); return *this;}
1693 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1694 {reset(__p.__ptr_); return *this;}
1695 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1696
1697 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1698 {return *__ptr_;}
1699 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1700 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1701 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1702 {
1703 _Tp* __t = __ptr_;
1704 __ptr_ = 0;
1705 return __t;
1706 }
1707 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1708 {
1709 if (__ptr_ != __p)
1710 delete __ptr_;
1711 __ptr_ = __p;
1712 }
1713
1714 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1715 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1716 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1717 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1718 {return auto_ptr<_Up>(release());}
1719};
1720
1721template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001722class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723{
1724public:
1725 typedef void element_type;
1726};
1727
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1729 typename remove_cv<_T2>::type>::value,
1730 bool = is_empty<_T1>::value,
1731 bool = is_empty<_T2>::value>
1732struct __libcpp_compressed_pair_switch;
1733
1734template <class _T1, class _T2, bool IsSame>
1735struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1736
1737template <class _T1, class _T2, bool IsSame>
1738struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1739
1740template <class _T1, class _T2, bool IsSame>
1741struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1742
1743template <class _T1, class _T2>
1744struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1745
1746template <class _T1, class _T2>
1747struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1748
1749template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1750class __libcpp_compressed_pair_imp;
1751
1752template <class _T1, class _T2>
1753class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1754{
1755private:
1756 _T1 __first_;
1757 _T2 __second_;
1758public:
1759 typedef _T1 _T1_param;
1760 typedef _T2 _T2_param;
1761
1762 typedef typename remove_reference<_T1>::type& _T1_reference;
1763 typedef typename remove_reference<_T2>::type& _T2_reference;
1764
1765 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1766 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1767
1768 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1769 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1770 : __first_(_STD::forward<_T1_param>(__t1)) {}
1771 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1772 : __second_(_STD::forward<_T2_param>(__t2)) {}
1773 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1774 : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1775
Howard Hinnant73d21a42010-09-04 23:28:19 +00001776#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1778 : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001779#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780
1781 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1782 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1783
1784 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1785 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1786
1787 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1788 {
1789 using _STD::swap;
1790 swap(__first_, __x.__first_);
1791 swap(__second_, __x.__second_);
1792 }
1793};
1794
1795template <class _T1, class _T2>
1796class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1797 : private _T1
1798{
1799private:
1800 _T2 __second_;
1801public:
1802 typedef _T1 _T1_param;
1803 typedef _T2 _T2_param;
1804
1805 typedef _T1& _T1_reference;
1806 typedef typename remove_reference<_T2>::type& _T2_reference;
1807
1808 typedef const _T1& _T1_const_reference;
1809 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1810
1811 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1812 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1813 : _T1(_STD::forward<_T1_param>(__t1)) {}
1814 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1815 : __second_(_STD::forward<_T2_param>(__t2)) {}
1816 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1817 : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1818
Howard Hinnant73d21a42010-09-04 23:28:19 +00001819#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1821 : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001822#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823
1824 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1825 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1826
1827 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return __second_;}
1828 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1829
1830 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1831 {
1832 using _STD::swap;
1833 swap(__second_, __x.__second_);
1834 }
1835};
1836
1837template <class _T1, class _T2>
1838class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1839 : private _T2
1840{
1841private:
1842 _T1 __first_;
1843public:
1844 typedef _T1 _T1_param;
1845 typedef _T2 _T2_param;
1846
1847 typedef typename remove_reference<_T1>::type& _T1_reference;
1848 typedef _T2& _T2_reference;
1849
1850 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1851 typedef const _T2& _T2_const_reference;
1852
1853 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1854 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1855 : __first_(_STD::forward<_T1_param>(__t1)) {}
1856 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1857 : _T2(_STD::forward<_T2_param>(__t2)) {}
1858 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1859 : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1860
Howard Hinnant73d21a42010-09-04 23:28:19 +00001861#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1863 : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001864#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865
1866 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return __first_;}
1867 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1868
1869 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1870 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1871
1872 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1873 {
1874 using _STD::swap;
1875 swap(__first_, __x.__first_);
1876 }
1877};
1878
1879template <class _T1, class _T2>
1880class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1881 : private _T1,
1882 private _T2
1883{
1884public:
1885 typedef _T1 _T1_param;
1886 typedef _T2 _T2_param;
1887
1888 typedef _T1& _T1_reference;
1889 typedef _T2& _T2_reference;
1890
1891 typedef const _T1& _T1_const_reference;
1892 typedef const _T2& _T2_const_reference;
1893
1894 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1895 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1896 : _T1(_STD::forward<_T1_param>(__t1)) {}
1897 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1898 : _T2(_STD::forward<_T2_param>(__t2)) {}
1899 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1900 : _T1(_STD::forward<_T1_param>(__t1)), _T2(_STD::forward<_T2_param>(__t2)) {}
1901
Howard Hinnant73d21a42010-09-04 23:28:19 +00001902#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1904 : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001905#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906
1907 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return *this;}
1908 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1909
1910 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return *this;}
1911 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1912
1913 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1914 {
1915 }
1916};
1917
1918template <class _T1, class _T2>
1919class __compressed_pair
1920 : private __libcpp_compressed_pair_imp<_T1, _T2>
1921{
1922 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
1923public:
1924 typedef typename base::_T1_param _T1_param;
1925 typedef typename base::_T2_param _T2_param;
1926
1927 typedef typename base::_T1_reference _T1_reference;
1928 typedef typename base::_T2_reference _T2_reference;
1929
1930 typedef typename base::_T1_const_reference _T1_const_reference;
1931 typedef typename base::_T2_const_reference _T2_const_reference;
1932
1933 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
1934 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
1935 : base(_STD::forward<_T1_param>(__t1)) {}
1936 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
1937 : base(_STD::forward<_T2_param>(__t2)) {}
1938 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
1939 : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
1940
Howard Hinnant73d21a42010-09-04 23:28:19 +00001941#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942 __compressed_pair(__compressed_pair&& __p)
1943 : base(_STD::move(__p)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001944#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945
1946 _LIBCPP_INLINE_VISIBILITY _T1_reference first() {return base::first();}
1947 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return base::first();}
1948
1949 _LIBCPP_INLINE_VISIBILITY _T2_reference second() {return base::second();}
1950 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return base::second();}
1951
1952 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x) {base::swap(__x);}
1953};
1954
1955template <class _T1, class _T2>
1956inline _LIBCPP_INLINE_VISIBILITY
1957void
1958swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1959 {__x.swap(__y);}
1960
1961template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001962struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963{
1964 _LIBCPP_INLINE_VISIBILITY default_delete() {}
1965 template <class _Up>
1966 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
1967 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) {}
1968 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1969 {
1970 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1971 delete __ptr;
1972 }
1973};
1974
1975template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001976struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001977{
1978 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1979 {
1980 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1981 delete [] __ptr;
1982 }
1983private:
1984 template <class _Up> void operator() (_Up*) const;
1985};
1986
1987template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00001988class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989{
1990public:
1991 typedef _Tp element_type;
1992 typedef _Dp deleter_type;
1993 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1994private:
1995 __compressed_pair<pointer, deleter_type> __ptr_;
1996
Howard Hinnant73d21a42010-09-04 23:28:19 +00001997#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001998 unique_ptr(const unique_ptr&);
1999 unique_ptr& operator=(const unique_ptr&);
2000 template <class _Up, class _Ep>
2001 unique_ptr(const unique_ptr<_Up, _Ep>&);
2002 template <class _Up, class _Ep>
2003 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002004#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005 unique_ptr(unique_ptr&);
2006 template <class _Up, class _Ep>
2007 unique_ptr(unique_ptr<_Up, _Ep>&);
2008 unique_ptr& operator=(unique_ptr&);
2009 template <class _Up, class _Ep>
2010 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002011#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002012
2013 struct __nat {int __for_bool_;};
2014
2015 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2016 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2017public:
2018 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2019 : __ptr_(pointer())
2020 {
2021 static_assert(!is_pointer<deleter_type>::value,
2022 "unique_ptr constructed with null function pointer deleter");
2023 }
2024 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2025 : __ptr_(pointer())
2026 {
2027 static_assert(!is_pointer<deleter_type>::value,
2028 "unique_ptr constructed with null function pointer deleter");
2029 }
2030 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2031 : __ptr_(_STD::move(__p))
2032 {
2033 static_assert(!is_pointer<deleter_type>::value,
2034 "unique_ptr constructed with null function pointer deleter");
2035 }
2036
Howard Hinnant73d21a42010-09-04 23:28:19 +00002037#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2039 is_reference<deleter_type>::value,
2040 deleter_type,
2041 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2042 : __ptr_(__p, __d) {}
2043
2044 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
2045 : __ptr_(__p, _STD::move(__d))
2046 {
2047 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2048 }
2049 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2050 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2051 template <class _Up, class _Ep>
2052 _LIBCPP_INLINE_VISIBILITY
2053 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2054 typename enable_if
2055 <
2056 !is_array<_Up>::value &&
2057 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2058 is_convertible<_Ep, deleter_type>::value &&
2059 (
2060 !is_reference<deleter_type>::value ||
2061 is_same<deleter_type, _Ep>::value
2062 ),
2063 __nat
2064 >::type = __nat())
2065 : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2066
2067 template <class _Up>
2068 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2069 typename enable_if<
2070 is_convertible<_Up*, _Tp*>::value &&
2071 is_same<_Dp, default_delete<_Tp> >::value,
2072 __nat
2073 >::type = __nat())
2074 : __ptr_(__p.release())
2075 {
2076 }
2077
2078 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2079 {
2080 reset(__u.release());
2081 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2082 return *this;
2083 }
2084
2085 template <class _Up, class _Ep>
2086 _LIBCPP_INLINE_VISIBILITY
2087 typename enable_if
2088 <
2089 !is_array<_Up>::value,
2090 unique_ptr&
2091 >::type
2092 operator=(unique_ptr<_Up, _Ep>&& __u)
2093 {
2094 reset(__u.release());
2095 __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2096 return *this;
2097 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002098#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099
2100 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2101 {
2102 return __rv<unique_ptr>(*this);
2103 }
2104
2105 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2106 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2107
2108 template <class _Up, class _Ep>
2109 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2110 {
2111 reset(__u.release());
2112 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2113 return *this;
2114 }
2115
2116 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2117 : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2118
2119 template <class _Up>
2120 _LIBCPP_INLINE_VISIBILITY
2121 typename enable_if<
2122 is_convertible<_Up*, _Tp*>::value &&
2123 is_same<_Dp, default_delete<_Tp> >::value,
2124 unique_ptr&
2125 >::type
2126 operator=(auto_ptr<_Up> __p)
2127 {reset(__p.release()); return *this;}
2128
Howard Hinnant73d21a42010-09-04 23:28:19 +00002129#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2131
2132 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2133 {
2134 reset();
2135 return *this;
2136 }
2137
2138 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2139 {return *__ptr_.first();}
2140 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_.first();}
2141 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2142 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2143 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2144 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2145
2146 _LIBCPP_INLINE_VISIBILITY pointer release()
2147 {
2148 pointer __t = __ptr_.first();
2149 __ptr_.first() = pointer();
2150 return __t;
2151 }
2152
2153 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2154 {
2155 pointer __tmp = __ptr_.first();
2156 __ptr_.first() = __p;
2157 if (__tmp)
2158 __ptr_.second()(__tmp);
2159 }
2160
2161 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2162};
2163
2164template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002165class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166{
2167public:
2168 typedef _Tp element_type;
2169 typedef _Dp deleter_type;
2170 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2171private:
2172 __compressed_pair<pointer, deleter_type> __ptr_;
2173
Howard Hinnant73d21a42010-09-04 23:28:19 +00002174#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 unique_ptr(const unique_ptr&);
2176 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002177#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 unique_ptr(unique_ptr&);
2179 template <class _Up>
2180 unique_ptr(unique_ptr<_Up>&);
2181 unique_ptr& operator=(unique_ptr&);
2182 template <class _Up>
2183 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002184#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002185
2186 struct __nat {int __for_bool_;};
2187
2188 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2189 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2190public:
2191 _LIBCPP_INLINE_VISIBILITY unique_ptr()
2192 : __ptr_(pointer())
2193 {
2194 static_assert(!is_pointer<deleter_type>::value,
2195 "unique_ptr constructed with null function pointer deleter");
2196 }
2197 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2198 : __ptr_(pointer())
2199 {
2200 static_assert(!is_pointer<deleter_type>::value,
2201 "unique_ptr constructed with null function pointer deleter");
2202 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002203#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002204 template <class _P,
2205 class = typename enable_if<is_same<_P, pointer>::value>::type
2206 >
2207 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p)
2208 : __ptr_(__p)
2209 {
2210 static_assert(!is_pointer<deleter_type>::value,
2211 "unique_ptr constructed with null function pointer deleter");
2212 }
2213
2214 template <class _P,
2215 class = typename enable_if<is_same<_P, pointer>::value>::type
2216 >
2217 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2218 is_reference<deleter_type>::value,
2219 deleter_type,
2220 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2221 : __ptr_(__p, __d) {}
2222
2223 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2224 is_reference<deleter_type>::value,
2225 deleter_type,
2226 typename add_lvalue_reference<const deleter_type>::type>::type __d)
2227 : __ptr_(pointer(), __d) {}
2228
2229 template <class _P,
2230 class = typename enable_if<is_same<_P, pointer>::value ||
2231 is_same<_P, nullptr_t>::value>::type
2232 >
2233 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
2234 : __ptr_(__p, _STD::move(__d))
2235 {
2236 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2237 }
2238
2239 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
2240 : __ptr_(pointer(), _STD::move(__d))
2241 {
2242 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2243 }
2244
2245 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2246 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2247
2248 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2249 {
2250 reset(__u.release());
2251 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2252 return *this;
2253 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002254#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255
2256 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2257 : __ptr_(__p)
2258 {
2259 static_assert(!is_pointer<deleter_type>::value,
2260 "unique_ptr constructed with null function pointer deleter");
2261 }
2262
2263 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2264 : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2265
2266 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2267 : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2268
2269 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2270 {
2271 return __rv<unique_ptr>(*this);
2272 }
2273
2274 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2275 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2276
2277 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2278 {
2279 reset(__u->release());
2280 __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2281 return *this;
2282 }
2283
Howard Hinnant73d21a42010-09-04 23:28:19 +00002284#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2286
2287 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2288 {
2289 reset();
2290 return *this;
2291 }
2292
2293 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2294 {return __ptr_.first()[__i];}
2295 _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2296 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() {return __ptr_.second();}
2297 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2298 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2299
2300 _LIBCPP_INLINE_VISIBILITY pointer release()
2301 {
2302 pointer __t = __ptr_.first();
2303 __ptr_.first() = pointer();
2304 return __t;
2305 }
2306
Howard Hinnant73d21a42010-09-04 23:28:19 +00002307#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308 template <class _P,
2309 class = typename enable_if<is_same<_P, pointer>::value>::type
2310 >
2311 _LIBCPP_INLINE_VISIBILITY void reset(_P __p)
2312 {
2313 pointer __tmp = __ptr_.first();
2314 __ptr_.first() = __p;
2315 if (__tmp)
2316 __ptr_.second()(__tmp);
2317 }
2318 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t)
2319 {
2320 pointer __tmp = __ptr_.first();
2321 __ptr_.first() = nullptr;
2322 if (__tmp)
2323 __ptr_.second()(__tmp);
2324 }
2325 _LIBCPP_INLINE_VISIBILITY void reset()
2326 {
2327 pointer __tmp = __ptr_.first();
2328 __ptr_.first() = nullptr;
2329 if (__tmp)
2330 __ptr_.second()(__tmp);
2331 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002332#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2334 {
2335 pointer __tmp = __ptr_.first();
2336 __ptr_.first() = __p;
2337 if (__tmp)
2338 __ptr_.second()(__tmp);
2339 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002340#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341
2342 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2343private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002344
Howard Hinnant73d21a42010-09-04 23:28:19 +00002345#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346 template <class _Up>
2347 explicit unique_ptr(_Up);
2348 template <class _Up>
2349 unique_ptr(_Up __u,
2350 typename conditional<
2351 is_reference<deleter_type>::value,
2352 deleter_type,
2353 typename add_lvalue_reference<const deleter_type>::type>::type,
2354 typename enable_if
2355 <
2356 is_convertible<_Up, pointer>::value,
2357 __nat
2358 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002359#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360};
2361
2362template <class _Tp, class _Dp>
2363inline _LIBCPP_INLINE_VISIBILITY
2364void
2365swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) {__x.swap(__y);}
2366
2367template <class _T1, class _D1, class _T2, class _D2>
2368inline _LIBCPP_INLINE_VISIBILITY
2369bool
2370operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2371
2372template <class _T1, class _D1, class _T2, class _D2>
2373inline _LIBCPP_INLINE_VISIBILITY
2374bool
2375operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2376
2377template <class _T1, class _D1, class _T2, class _D2>
2378inline _LIBCPP_INLINE_VISIBILITY
2379bool
2380operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2381
2382template <class _T1, class _D1, class _T2, class _D2>
2383inline _LIBCPP_INLINE_VISIBILITY
2384bool
2385operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2386
2387template <class _T1, class _D1, class _T2, class _D2>
2388inline _LIBCPP_INLINE_VISIBILITY
2389bool
2390operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2391
2392template <class _T1, class _D1, class _T2, class _D2>
2393inline _LIBCPP_INLINE_VISIBILITY
2394bool
2395operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2396
Howard Hinnant21aefc32010-06-03 16:42:57 +00002397template <class> struct hash;
2398
2399template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002400struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002401 : public unary_function<_Tp*, size_t>
2402{
Howard Hinnant82894812010-09-22 16:48:34 +00002403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00002404 size_t operator()(_Tp* __v) const
2405 {
2406 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2407 return *__p;
2408 }
2409};
2410
2411template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002412struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002413{
2414 typedef unique_ptr<_Tp, _Dp> argument_type;
2415 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00002417 result_type operator()(const argument_type& __ptr) const
2418 {
2419 typedef typename argument_type::pointer pointer;
2420 return hash<pointer>()(__ptr.get());
2421 }
2422};
2423
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424struct __destruct_n
2425{
2426private:
2427 size_t size;
2428
2429 template <class _Tp>
2430 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type)
2431 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2432
2433 template <class _Tp>
2434 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type)
2435 {}
2436
2437 _LIBCPP_INLINE_VISIBILITY void __incr(false_type)
2438 {++size;}
2439 _LIBCPP_INLINE_VISIBILITY void __incr(true_type)
2440 {}
2441
2442 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type)
2443 {size = __s;}
2444 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type)
2445 {}
2446public:
2447 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) : size(__s) {}
2448
2449 template <class _Tp>
2450 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*)
Howard Hinnant1468b662010-11-19 22:17:28 +00002451 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452
2453 template <class _Tp>
2454 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*)
Howard Hinnant1468b662010-11-19 22:17:28 +00002455 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456
2457 template <class _Tp>
2458 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p)
Howard Hinnant1468b662010-11-19 22:17:28 +00002459 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460};
2461
2462template <class _Alloc>
2463class __allocator_destructor
2464{
2465 typedef allocator_traits<_Alloc> __alloc_traits;
2466public:
2467 typedef typename __alloc_traits::pointer pointer;
2468 typedef typename __alloc_traits::size_type size_type;
2469private:
2470 _Alloc& __alloc_;
2471 size_type __s_;
2472public:
2473 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2474 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002476 void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2477};
2478
2479template <class _InputIterator, class _ForwardIterator>
2480_ForwardIterator
2481uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2482{
2483 __destruct_n __d(0);
2484 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2485 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2486 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2487 ::new(&*__r) value_type(*__f);
2488 __h.release();
2489 return __r;
2490}
2491
2492template <class _InputIterator, class _Size, class _ForwardIterator>
2493_ForwardIterator
2494uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2495{
2496 __destruct_n __d(0);
2497 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2498 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2499 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2500 ::new(&*__r) value_type(*__f);
2501 __h.release();
2502 return __r;
2503}
2504
2505template <class _ForwardIterator, class _Tp>
2506void
2507uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2508{
2509 __destruct_n __d(0);
2510 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2511 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2512 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2513 ::new(&*__f) value_type(__x);
2514 __h.release();
2515}
2516
2517template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002518_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2520{
2521 __destruct_n __d(0);
2522 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2523 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2524 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2525 ::new(&*__f) value_type(__x);
2526 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002527 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528}
2529
Howard Hinnant82894812010-09-22 16:48:34 +00002530class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 : public std::exception
2532{
2533public:
2534 virtual ~bad_weak_ptr() throw();
2535 virtual const char* what() const throw();
2536};
2537
2538template<class _Tp> class weak_ptr;
2539
2540class __shared_count
2541{
2542 __shared_count(const __shared_count&);
2543 __shared_count& operator=(const __shared_count&);
2544
2545protected:
2546 long __shared_owners_;
2547 virtual ~__shared_count();
2548private:
2549 virtual void __on_zero_shared() = 0;
2550
2551public:
Howard Hinnant82894812010-09-22 16:48:34 +00002552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553 explicit __shared_count(long __refs = 0)
2554 : __shared_owners_(__refs) {}
2555
2556 void __add_shared();
Howard Hinnant28dbbe02010-11-16 21:33:17 +00002557 bool __release_shared();
Howard Hinnant82894812010-09-22 16:48:34 +00002558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559 long use_count() const {return __shared_owners_ + 1;}
2560};
2561
2562class __shared_weak_count
2563 : private __shared_count
2564{
2565 long __shared_weak_owners_;
2566
2567public:
Howard Hinnant82894812010-09-22 16:48:34 +00002568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 explicit __shared_weak_count(long __refs = 0)
2570 : __shared_count(__refs),
2571 __shared_weak_owners_(__refs) {}
2572protected:
2573 virtual ~__shared_weak_count();
2574
2575public:
2576 void __add_shared();
2577 void __add_weak();
2578 void __release_shared();
2579 void __release_weak();
Howard Hinnant82894812010-09-22 16:48:34 +00002580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581 long use_count() const {return __shared_count::use_count();}
2582 __shared_weak_count* lock();
2583
Howard Hinnantd4444702010-08-11 17:04:31 +00002584#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002586#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002587private:
2588 virtual void __on_zero_shared_weak() = 0;
2589};
2590
2591template <class _Tp, class _Dp, class _Alloc>
2592class __shared_ptr_pointer
2593 : public __shared_weak_count
2594{
2595 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2596public:
Howard Hinnant82894812010-09-22 16:48:34 +00002597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002598 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2599 : __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2600
Howard Hinnantd4444702010-08-11 17:04:31 +00002601#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 virtual const void* __get_deleter(const type_info&) const;
Howard Hinnantd4444702010-08-11 17:04:31 +00002603#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604
2605private:
2606 virtual void __on_zero_shared();
2607 virtual void __on_zero_shared_weak();
2608};
2609
Howard Hinnantd4444702010-08-11 17:04:31 +00002610#ifndef _LIBCPP_NO_RTTI
2611
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002612template <class _Tp, class _Dp, class _Alloc>
2613const void*
2614__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
2615{
2616 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2617}
2618
Howard Hinnant324bb032010-08-22 00:02:43 +00002619#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002620
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621template <class _Tp, class _Dp, class _Alloc>
2622void
2623__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
2624{
2625 __data_.first().second()(__data_.first().first());
2626 __data_.first().second().~_Dp();
2627}
2628
2629template <class _Tp, class _Dp, class _Alloc>
2630void
2631__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak()
2632{
2633 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2634 __data_.second().~_Alloc();
2635 __a.deallocate(this, 1);
2636}
2637
2638template <class _Tp, class _Alloc>
2639class __shared_ptr_emplace
2640 : public __shared_weak_count
2641{
2642 __compressed_pair<_Alloc, _Tp> __data_;
2643public:
2644#ifndef _LIBCPP_HAS_NO_VARIADICS
2645
Howard Hinnant82894812010-09-22 16:48:34 +00002646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002647 __shared_ptr_emplace(_Alloc __a)
2648 : __data_(_STD::move(__a)) {}
2649
2650 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2653 : __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2654
2655#else // _LIBCPP_HAS_NO_VARIADICS
2656
Howard Hinnant82894812010-09-22 16:48:34 +00002657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658 __shared_ptr_emplace(_Alloc __a)
2659 : __data_(__a) {}
2660
2661 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2664 : __data_(__a, _Tp(__a0)) {}
2665
2666 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002668 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2669 : __data_(__a, _Tp(__a0, __a1)) {}
2670
2671 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2674 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2675
2676#endif // _LIBCPP_HAS_NO_VARIADICS
2677
2678private:
2679 virtual void __on_zero_shared();
2680 virtual void __on_zero_shared_weak();
2681public:
Howard Hinnant82894812010-09-22 16:48:34 +00002682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 _Tp* get() {return &__data_.second();}
2684};
2685
2686template <class _Tp, class _Alloc>
2687void
2688__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared()
2689{
2690 __data_.second().~_Tp();
2691}
2692
2693template <class _Tp, class _Alloc>
2694void
2695__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak()
2696{
2697 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2698 __data_.first().~_Alloc();
2699 __a.deallocate(this, 1);
2700}
2701
2702template<class _Tp> class enable_shared_from_this;
2703
2704template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002705class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706{
Howard Hinnant324bb032010-08-22 00:02:43 +00002707public:
2708 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709private:
2710 element_type* __ptr_;
2711 __shared_weak_count* __cntrl_;
2712
2713 struct __nat {int __for_bool_;};
2714public:
2715 shared_ptr();
2716 shared_ptr(nullptr_t);
2717 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00002718 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2719 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2721 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant324bb032010-08-22 00:02:43 +00002722 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002723 shared_ptr(const shared_ptr& __r);
2724 template<class _Yp>
2725 shared_ptr(const shared_ptr<_Yp>& __r,
2726 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002727#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 shared_ptr(shared_ptr&& __r);
2729 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002730 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002731#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002732 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002733 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002734#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002735 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2736#else
2737 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002739#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002741 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742public:
2743 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2744 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2745 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2746 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002747#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2749 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2750 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2751 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002752#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753
2754 ~shared_ptr();
2755
Howard Hinnant324bb032010-08-22 00:02:43 +00002756 shared_ptr& operator=(const shared_ptr& __r);
2757 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002759 shared_ptr& operator=(shared_ptr&& __r);
2760 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2761 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002762#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002763 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002765#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002767 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768public:
Howard Hinnant324bb032010-08-22 00:02:43 +00002769 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002770#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002771 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772#endif
2773
2774 void swap(shared_ptr& __r);
2775 void reset();
2776 template<class _Yp> void reset(_Yp* __p);
2777 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2778 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2779
Howard Hinnant82894812010-09-22 16:48:34 +00002780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781 element_type* get() const {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 typename add_lvalue_reference<element_type>::type operator*() const {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785 element_type* operator->() const {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 bool unique() const {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00002790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 bool empty() const {return __cntrl_ == 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793 /*explicit*/ operator bool() const {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002794 template <class _U>
2795 _LIBCPP_INLINE_VISIBILITY
2796 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002798 template <class _U>
2799 _LIBCPP_INLINE_VISIBILITY
2800 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801 {return __cntrl_ < __p.__cntrl_;}
2802
Howard Hinnantd4444702010-08-11 17:04:31 +00002803#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002804 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806 _Dp* __get_deleter() const
2807 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002808#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002809
2810#ifndef _LIBCPP_HAS_NO_VARIADICS
2811
2812 template<class ..._Args>
2813 static
2814 shared_ptr<_Tp>
2815 make_shared(_Args&& ...__args);
2816
2817 template<class _Alloc, class ..._Args>
2818 static
2819 shared_ptr<_Tp>
2820 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2821
2822#else // _LIBCPP_HAS_NO_VARIADICS
2823
2824 static shared_ptr<_Tp> make_shared();
2825
2826 template<class _A0>
2827 static shared_ptr<_Tp> make_shared(_A0&);
2828
2829 template<class _A0, class _A1>
2830 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2831
2832 template<class _A0, class _A1, class _A2>
2833 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2834
2835 template<class _Alloc>
2836 static shared_ptr<_Tp>
2837 allocate_shared(const _Alloc& __a);
2838
2839 template<class _Alloc, class _A0>
2840 static shared_ptr<_Tp>
2841 allocate_shared(const _Alloc& __a, _A0& __a0);
2842
2843 template<class _Alloc, class _A0, class _A1>
2844 static shared_ptr<_Tp>
2845 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2846
2847 template<class _Alloc, class _A0, class _A1, class _A2>
2848 static shared_ptr<_Tp>
2849 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2850
2851#endif // _LIBCPP_HAS_NO_VARIADICS
2852
2853private:
2854
2855 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00002856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857 void
2858 __enable_weak_this(const enable_shared_from_this<_Yp>* __e)
2859 {
2860 if (__e)
2861 __e->__weak_this_ = *this;
2862 }
2863
Howard Hinnant82894812010-09-22 16:48:34 +00002864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865 void __enable_weak_this(const void*) {}
2866
Howard Hinnant82894812010-09-22 16:48:34 +00002867 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
2868 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869};
2870
2871template<class _Tp>
2872inline _LIBCPP_INLINE_VISIBILITY
2873shared_ptr<_Tp>::shared_ptr()
2874 : __ptr_(0),
2875 __cntrl_(0)
2876{
2877}
2878
2879template<class _Tp>
2880inline _LIBCPP_INLINE_VISIBILITY
2881shared_ptr<_Tp>::shared_ptr(nullptr_t)
2882 : __ptr_(0),
2883 __cntrl_(0)
2884{
2885}
2886
2887template<class _Tp>
2888template<class _Yp>
2889shared_ptr<_Tp>::shared_ptr(_Yp* __p)
2890 : __ptr_(__p)
2891{
2892 unique_ptr<_Yp> __hold(__p);
2893 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2894 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
2895 __hold.release();
2896 __enable_weak_this(__p);
2897}
2898
2899template<class _Tp>
2900template<class _Yp, class _Dp>
2901shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
2902 : __ptr_(__p)
2903{
2904#ifndef _LIBCPP_NO_EXCEPTIONS
2905 try
2906 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002907#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002908 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
2909 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
2910 __enable_weak_this(__p);
2911#ifndef _LIBCPP_NO_EXCEPTIONS
2912 }
2913 catch (...)
2914 {
2915 __d(__p);
2916 throw;
2917 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002918#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919}
2920
2921template<class _Tp>
2922template<class _Dp>
2923shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
2924 : __ptr_(0)
2925{
2926#ifndef _LIBCPP_NO_EXCEPTIONS
2927 try
2928 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002929#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002930 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
2931 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
2932#ifndef _LIBCPP_NO_EXCEPTIONS
2933 }
2934 catch (...)
2935 {
2936 __d(__p);
2937 throw;
2938 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002939#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002940}
2941
2942template<class _Tp>
2943template<class _Yp, class _Dp, class _Alloc>
2944shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
2945 : __ptr_(__p)
2946{
2947#ifndef _LIBCPP_NO_EXCEPTIONS
2948 try
2949 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002950#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002951 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
2952 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2953 typedef __allocator_destructor<_A2> _D2;
2954 _A2 __a2(__a);
2955 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2956 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2957 __cntrl_ = __hold2.release();
2958 __enable_weak_this(__p);
2959#ifndef _LIBCPP_NO_EXCEPTIONS
2960 }
2961 catch (...)
2962 {
2963 __d(__p);
2964 throw;
2965 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002966#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002967}
2968
2969template<class _Tp>
2970template<class _Dp, class _Alloc>
2971shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
2972 : __ptr_(0)
2973{
2974#ifndef _LIBCPP_NO_EXCEPTIONS
2975 try
2976 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002977#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
2979 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2980 typedef __allocator_destructor<_A2> _D2;
2981 _A2 __a2(__a);
2982 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2983 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2984 __cntrl_ = __hold2.release();
2985#ifndef _LIBCPP_NO_EXCEPTIONS
2986 }
2987 catch (...)
2988 {
2989 __d(__p);
2990 throw;
2991 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002992#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002993}
2994
2995template<class _Tp>
2996template<class _Yp>
2997inline _LIBCPP_INLINE_VISIBILITY
2998shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p)
2999 : __ptr_(__p),
3000 __cntrl_(__r.__cntrl_)
3001{
3002 if (__cntrl_)
3003 __cntrl_->__add_shared();
3004}
3005
3006template<class _Tp>
3007inline _LIBCPP_INLINE_VISIBILITY
3008shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r)
3009 : __ptr_(__r.__ptr_),
3010 __cntrl_(__r.__cntrl_)
3011{
3012 if (__cntrl_)
3013 __cntrl_->__add_shared();
3014}
3015
3016template<class _Tp>
3017template<class _Yp>
3018inline _LIBCPP_INLINE_VISIBILITY
3019shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3020 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3021 : __ptr_(__r.__ptr_),
3022 __cntrl_(__r.__cntrl_)
3023{
3024 if (__cntrl_)
3025 __cntrl_->__add_shared();
3026}
3027
Howard Hinnant73d21a42010-09-04 23:28:19 +00003028#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003029
3030template<class _Tp>
3031inline _LIBCPP_INLINE_VISIBILITY
3032shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r)
3033 : __ptr_(__r.__ptr_),
3034 __cntrl_(__r.__cntrl_)
3035{
3036 __r.__ptr_ = 0;
3037 __r.__cntrl_ = 0;
3038}
3039
3040template<class _Tp>
3041template<class _Yp>
3042inline _LIBCPP_INLINE_VISIBILITY
3043shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3044 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3045 : __ptr_(__r.__ptr_),
3046 __cntrl_(__r.__cntrl_)
3047{
3048 __r.__ptr_ = 0;
3049 __r.__cntrl_ = 0;
3050}
3051
Howard Hinnant73d21a42010-09-04 23:28:19 +00003052#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003053
3054template<class _Tp>
3055template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003056#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003057shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3058#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003059shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003060#endif
3061 : __ptr_(__r.get())
3062{
3063 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3064 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3065 __enable_weak_this(__r.get());
3066 __r.release();
3067}
3068
3069template<class _Tp>
3070template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003071#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003072shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3073#else
3074shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3075#endif
3076 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3077 : __ptr_(__r.get())
3078{
3079 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3080 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3081 __enable_weak_this(__r.get());
3082 __r.release();
3083}
3084
3085template<class _Tp>
3086template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003087#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3089#else
3090shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3091#endif
3092 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3093 : __ptr_(__r.get())
3094{
3095 typedef __shared_ptr_pointer<_Yp*,
3096 reference_wrapper<typename remove_reference<_Dp>::type>,
3097 allocator<_Yp> > _CntrlBlk;
3098 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3099 __enable_weak_this(__r.get());
3100 __r.release();
3101}
3102
3103#ifndef _LIBCPP_HAS_NO_VARIADICS
3104
3105template<class _Tp>
3106template<class ..._Args>
3107shared_ptr<_Tp>
3108shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3109{
3110 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3111 typedef allocator<_CntrlBlk> _A2;
3112 typedef __allocator_destructor<_A2> _D2;
3113 _A2 __a2;
3114 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3115 ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3116 shared_ptr<_Tp> __r;
3117 __r.__ptr_ = __hold2.get()->get();
3118 __r.__cntrl_ = __hold2.release();
3119 __r.__enable_weak_this(__r.__ptr_);
3120 return __r;
3121}
3122
3123template<class _Tp>
3124template<class _Alloc, class ..._Args>
3125shared_ptr<_Tp>
3126shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3127{
3128 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3129 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3130 typedef __allocator_destructor<_A2> _D2;
3131 _A2 __a2(__a);
3132 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3133 ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3134 shared_ptr<_Tp> __r;
3135 __r.__ptr_ = __hold2.get()->get();
3136 __r.__cntrl_ = __hold2.release();
3137 __r.__enable_weak_this(__r.__ptr_);
3138 return __r;
3139}
3140
3141#else // _LIBCPP_HAS_NO_VARIADICS
3142
3143template<class _Tp>
3144shared_ptr<_Tp>
3145shared_ptr<_Tp>::make_shared()
3146{
3147 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3148 typedef allocator<_CntrlBlk> _Alloc2;
3149 typedef __allocator_destructor<_Alloc2> _D2;
3150 _Alloc2 __alloc2;
3151 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3152 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3153 shared_ptr<_Tp> __r;
3154 __r.__ptr_ = __hold2.get()->get();
3155 __r.__cntrl_ = __hold2.release();
3156 __r.__enable_weak_this(__r.__ptr_);
3157 return __r;
3158}
3159
3160template<class _Tp>
3161template<class _A0>
3162shared_ptr<_Tp>
3163shared_ptr<_Tp>::make_shared(_A0& __a0)
3164{
3165 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3166 typedef allocator<_CntrlBlk> _Alloc2;
3167 typedef __allocator_destructor<_Alloc2> _D2;
3168 _Alloc2 __alloc2;
3169 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3170 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3171 shared_ptr<_Tp> __r;
3172 __r.__ptr_ = __hold2.get()->get();
3173 __r.__cntrl_ = __hold2.release();
3174 __r.__enable_weak_this(__r.__ptr_);
3175 return __r;
3176}
3177
3178template<class _Tp>
3179template<class _A0, class _A1>
3180shared_ptr<_Tp>
3181shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3182{
3183 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3184 typedef allocator<_CntrlBlk> _Alloc2;
3185 typedef __allocator_destructor<_Alloc2> _D2;
3186 _Alloc2 __alloc2;
3187 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3188 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3189 shared_ptr<_Tp> __r;
3190 __r.__ptr_ = __hold2.get()->get();
3191 __r.__cntrl_ = __hold2.release();
3192 __r.__enable_weak_this(__r.__ptr_);
3193 return __r;
3194}
3195
3196template<class _Tp>
3197template<class _A0, class _A1, class _A2>
3198shared_ptr<_Tp>
3199shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3200{
3201 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3202 typedef allocator<_CntrlBlk> _Alloc2;
3203 typedef __allocator_destructor<_Alloc2> _D2;
3204 _Alloc2 __alloc2;
3205 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3206 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3207 shared_ptr<_Tp> __r;
3208 __r.__ptr_ = __hold2.get()->get();
3209 __r.__cntrl_ = __hold2.release();
3210 __r.__enable_weak_this(__r.__ptr_);
3211 return __r;
3212}
3213
3214template<class _Tp>
3215template<class _Alloc>
3216shared_ptr<_Tp>
3217shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3218{
3219 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3220 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3221 typedef __allocator_destructor<_Alloc2> _D2;
3222 _Alloc2 __alloc2(__a);
3223 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3224 ::new(__hold2.get()) _CntrlBlk(__a);
3225 shared_ptr<_Tp> __r;
3226 __r.__ptr_ = __hold2.get()->get();
3227 __r.__cntrl_ = __hold2.release();
3228 __r.__enable_weak_this(__r.__ptr_);
3229 return __r;
3230}
3231
3232template<class _Tp>
3233template<class _Alloc, class _A0>
3234shared_ptr<_Tp>
3235shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3236{
3237 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3238 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3239 typedef __allocator_destructor<_Alloc2> _D2;
3240 _Alloc2 __alloc2(__a);
3241 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3242 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3243 shared_ptr<_Tp> __r;
3244 __r.__ptr_ = __hold2.get()->get();
3245 __r.__cntrl_ = __hold2.release();
3246 __r.__enable_weak_this(__r.__ptr_);
3247 return __r;
3248}
3249
3250template<class _Tp>
3251template<class _Alloc, class _A0, class _A1>
3252shared_ptr<_Tp>
3253shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3254{
3255 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3256 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3257 typedef __allocator_destructor<_Alloc2> _D2;
3258 _Alloc2 __alloc2(__a);
3259 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3260 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3261 shared_ptr<_Tp> __r;
3262 __r.__ptr_ = __hold2.get()->get();
3263 __r.__cntrl_ = __hold2.release();
3264 __r.__enable_weak_this(__r.__ptr_);
3265 return __r;
3266}
3267
3268template<class _Tp>
3269template<class _Alloc, class _A0, class _A1, class _A2>
3270shared_ptr<_Tp>
3271shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3272{
3273 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3274 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3275 typedef __allocator_destructor<_Alloc2> _D2;
3276 _Alloc2 __alloc2(__a);
3277 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3278 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3279 shared_ptr<_Tp> __r;
3280 __r.__ptr_ = __hold2.get()->get();
3281 __r.__cntrl_ = __hold2.release();
3282 __r.__enable_weak_this(__r.__ptr_);
3283 return __r;
3284}
3285
3286#endif // _LIBCPP_HAS_NO_VARIADICS
3287
3288template<class _Tp>
3289shared_ptr<_Tp>::~shared_ptr()
3290{
3291 if (__cntrl_)
3292 __cntrl_->__release_shared();
3293}
3294
3295template<class _Tp>
3296inline _LIBCPP_INLINE_VISIBILITY
3297shared_ptr<_Tp>&
3298shared_ptr<_Tp>::operator=(const shared_ptr& __r)
3299{
3300 shared_ptr(__r).swap(*this);
3301 return *this;
3302}
3303
3304template<class _Tp>
3305template<class _Yp>
3306inline _LIBCPP_INLINE_VISIBILITY
3307shared_ptr<_Tp>&
3308shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r)
3309{
3310 shared_ptr(__r).swap(*this);
3311 return *this;
3312}
3313
Howard Hinnant73d21a42010-09-04 23:28:19 +00003314#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003315
3316template<class _Tp>
3317inline _LIBCPP_INLINE_VISIBILITY
3318shared_ptr<_Tp>&
3319shared_ptr<_Tp>::operator=(shared_ptr&& __r)
3320{
3321 shared_ptr(_STD::move(__r)).swap(*this);
3322 return *this;
3323}
3324
3325template<class _Tp>
3326template<class _Yp>
3327inline _LIBCPP_INLINE_VISIBILITY
3328shared_ptr<_Tp>&
3329shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3330{
3331 shared_ptr(_STD::move(__r)).swap(*this);
3332 return *this;
3333}
3334
3335template<class _Tp>
3336template<class _Yp>
3337inline _LIBCPP_INLINE_VISIBILITY
3338shared_ptr<_Tp>&
3339shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3340{
Howard Hinnant6b41c602011-05-11 20:21:19 +00003341 shared_ptr(_STD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003342 return *this;
3343}
3344
3345template<class _Tp>
3346template <class _Yp, class _Dp>
3347inline _LIBCPP_INLINE_VISIBILITY
3348shared_ptr<_Tp>&
3349shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3350{
3351 shared_ptr(_STD::move(__r)).swap(*this);
3352 return *this;
3353}
3354
Howard Hinnant73d21a42010-09-04 23:28:19 +00003355#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003356
3357template<class _Tp>
3358template<class _Yp>
3359inline _LIBCPP_INLINE_VISIBILITY
3360shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003361shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003362{
3363 shared_ptr(__r).swap(*this);
3364 return *this;
3365}
3366
3367template<class _Tp>
3368template <class _Yp, class _Dp>
3369inline _LIBCPP_INLINE_VISIBILITY
3370shared_ptr<_Tp>&
3371shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3372{
3373 shared_ptr(_STD::move(__r)).swap(*this);
3374 return *this;
3375}
3376
Howard Hinnant73d21a42010-09-04 23:28:19 +00003377#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003378
3379template<class _Tp>
3380inline _LIBCPP_INLINE_VISIBILITY
3381void
3382shared_ptr<_Tp>::swap(shared_ptr& __r)
3383{
3384 _STD::swap(__ptr_, __r.__ptr_);
3385 _STD::swap(__cntrl_, __r.__cntrl_);
3386}
3387
3388template<class _Tp>
3389inline _LIBCPP_INLINE_VISIBILITY
3390void
3391shared_ptr<_Tp>::reset()
3392{
3393 shared_ptr().swap(*this);
3394}
3395
3396template<class _Tp>
3397template<class _Yp>
3398inline _LIBCPP_INLINE_VISIBILITY
3399void
3400shared_ptr<_Tp>::reset(_Yp* __p)
3401{
3402 shared_ptr(__p).swap(*this);
3403}
3404
3405template<class _Tp>
3406template<class _Yp, class _Dp>
3407inline _LIBCPP_INLINE_VISIBILITY
3408void
3409shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3410{
3411 shared_ptr(__p, __d).swap(*this);
3412}
3413
3414template<class _Tp>
3415template<class _Yp, class _Dp, class _Alloc>
3416inline _LIBCPP_INLINE_VISIBILITY
3417void
3418shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3419{
3420 shared_ptr(__p, __d, __a).swap(*this);
3421}
3422
3423#ifndef _LIBCPP_HAS_NO_VARIADICS
3424
Howard Hinnant324bb032010-08-22 00:02:43 +00003425template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003426inline _LIBCPP_INLINE_VISIBILITY
3427shared_ptr<_Tp>
3428make_shared(_Args&& ...__args)
3429{
3430 return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3431}
3432
Howard Hinnant324bb032010-08-22 00:02:43 +00003433template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003434inline _LIBCPP_INLINE_VISIBILITY
3435shared_ptr<_Tp>
3436allocate_shared(const _Alloc& __a, _Args&& ...__args)
3437{
3438 return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3439}
3440
3441#else // _LIBCPP_HAS_NO_VARIADICS
3442
3443template<class _Tp>
3444inline _LIBCPP_INLINE_VISIBILITY
3445shared_ptr<_Tp>
3446make_shared()
3447{
3448 return shared_ptr<_Tp>::make_shared();
3449}
3450
3451template<class _Tp, class _A0>
3452inline _LIBCPP_INLINE_VISIBILITY
3453shared_ptr<_Tp>
3454make_shared(_A0& __a0)
3455{
3456 return shared_ptr<_Tp>::make_shared(__a0);
3457}
3458
3459template<class _Tp, class _A0, class _A1>
3460inline _LIBCPP_INLINE_VISIBILITY
3461shared_ptr<_Tp>
3462make_shared(_A0& __a0, _A1& __a1)
3463{
3464 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3465}
3466
Howard Hinnant324bb032010-08-22 00:02:43 +00003467template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003468inline _LIBCPP_INLINE_VISIBILITY
3469shared_ptr<_Tp>
3470make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3471{
3472 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3473}
3474
3475template<class _Tp, class _Alloc>
3476inline _LIBCPP_INLINE_VISIBILITY
3477shared_ptr<_Tp>
3478allocate_shared(const _Alloc& __a)
3479{
3480 return shared_ptr<_Tp>::allocate_shared(__a);
3481}
3482
3483template<class _Tp, class _Alloc, class _A0>
3484inline _LIBCPP_INLINE_VISIBILITY
3485shared_ptr<_Tp>
3486allocate_shared(const _Alloc& __a, _A0& __a0)
3487{
3488 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3489}
3490
3491template<class _Tp, class _Alloc, class _A0, class _A1>
3492inline _LIBCPP_INLINE_VISIBILITY
3493shared_ptr<_Tp>
3494allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3495{
3496 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3497}
3498
3499template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3500inline _LIBCPP_INLINE_VISIBILITY
3501shared_ptr<_Tp>
3502allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3503{
3504 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3505}
3506
3507#endif // _LIBCPP_HAS_NO_VARIADICS
3508
3509template<class _Tp, class _Up>
3510inline _LIBCPP_INLINE_VISIBILITY
3511bool
3512operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3513{
3514 return __x.get() == __y.get();
3515}
3516
3517template<class _Tp, class _Up>
3518inline _LIBCPP_INLINE_VISIBILITY
3519bool
3520operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3521{
3522 return !(__x == __y);
3523}
3524
3525template<class _Tp, class _Up>
3526inline _LIBCPP_INLINE_VISIBILITY
3527bool
3528operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3529{
3530 return __x.get() < __y.get();
3531}
3532
3533template<class _Tp>
3534inline _LIBCPP_INLINE_VISIBILITY
3535void
3536swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y)
3537{
3538 __x.swap(__y);
3539}
3540
3541template<class _Tp, class _Up>
3542inline _LIBCPP_INLINE_VISIBILITY
3543shared_ptr<_Tp>
3544static_pointer_cast(const shared_ptr<_Up>& __r)
3545{
3546 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3547}
3548
3549template<class _Tp, class _Up>
3550inline _LIBCPP_INLINE_VISIBILITY
3551shared_ptr<_Tp>
3552dynamic_pointer_cast(const shared_ptr<_Up>& __r)
3553{
3554 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3555 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3556}
3557
3558template<class _Tp, class _Up>
3559shared_ptr<_Tp>
3560const_pointer_cast(const shared_ptr<_Up>& __r)
3561{
3562 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3563}
3564
Howard Hinnantd4444702010-08-11 17:04:31 +00003565#ifndef _LIBCPP_NO_RTTI
3566
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003567template<class _Dp, class _Tp>
3568inline _LIBCPP_INLINE_VISIBILITY
3569_Dp*
3570get_deleter(const shared_ptr<_Tp>& __p)
3571{
3572 return __p.template __get_deleter<_Dp>();
3573}
3574
Howard Hinnant324bb032010-08-22 00:02:43 +00003575#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003576
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003577template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003578class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579{
Howard Hinnant324bb032010-08-22 00:02:43 +00003580public:
3581 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582private:
3583 element_type* __ptr_;
3584 __shared_weak_count* __cntrl_;
3585
Howard Hinnant324bb032010-08-22 00:02:43 +00003586public:
3587 weak_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003588 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
3589 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003590 weak_ptr(weak_ptr const& __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003591 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003592 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3593
3594 ~weak_ptr();
3595
3596 weak_ptr& operator=(weak_ptr const& __r);
3597 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r);
3598 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r);
3599
3600 void swap(weak_ptr& __r);
3601 void reset();
3602
Howard Hinnant82894812010-09-22 16:48:34 +00003603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604 long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003606 bool expired() const {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
Howard Hinnant324bb032010-08-22 00:02:43 +00003607 shared_ptr<_Tp> lock() const;
Howard Hinnant82894812010-09-22 16:48:34 +00003608 template<class _Up>
3609 _LIBCPP_INLINE_VISIBILITY
3610 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003611 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003612 template<class _Up>
3613 _LIBCPP_INLINE_VISIBILITY
3614 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615 {return __cntrl_ < __r.__cntrl_;}
3616
Howard Hinnant82894812010-09-22 16:48:34 +00003617 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3618 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619};
3620
3621template<class _Tp>
3622inline _LIBCPP_INLINE_VISIBILITY
3623weak_ptr<_Tp>::weak_ptr()
3624 : __ptr_(0),
3625 __cntrl_(0)
3626{
3627}
3628
3629template<class _Tp>
3630inline _LIBCPP_INLINE_VISIBILITY
3631weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r)
3632 : __ptr_(__r.__ptr_),
3633 __cntrl_(__r.__cntrl_)
3634{
3635 if (__cntrl_)
3636 __cntrl_->__add_weak();
3637}
3638
3639template<class _Tp>
3640template<class _Yp>
3641inline _LIBCPP_INLINE_VISIBILITY
3642weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3643 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3644 : __ptr_(__r.__ptr_),
3645 __cntrl_(__r.__cntrl_)
3646{
3647 if (__cntrl_)
3648 __cntrl_->__add_weak();
3649}
3650
3651template<class _Tp>
3652template<class _Yp>
3653inline _LIBCPP_INLINE_VISIBILITY
3654weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3655 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3656 : __ptr_(__r.__ptr_),
3657 __cntrl_(__r.__cntrl_)
3658{
3659 if (__cntrl_)
3660 __cntrl_->__add_weak();
3661}
3662
3663template<class _Tp>
3664weak_ptr<_Tp>::~weak_ptr()
3665{
3666 if (__cntrl_)
3667 __cntrl_->__release_weak();
3668}
3669
3670template<class _Tp>
3671inline _LIBCPP_INLINE_VISIBILITY
3672weak_ptr<_Tp>&
3673weak_ptr<_Tp>::operator=(weak_ptr const& __r)
3674{
3675 weak_ptr(__r).swap(*this);
3676 return *this;
3677}
3678
3679template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003680template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003681inline _LIBCPP_INLINE_VISIBILITY
3682weak_ptr<_Tp>&
3683weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r)
3684{
3685 weak_ptr(__r).swap(*this);
3686 return *this;
3687}
3688
3689template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003690template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003691inline _LIBCPP_INLINE_VISIBILITY
3692weak_ptr<_Tp>&
3693weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r)
3694{
3695 weak_ptr(__r).swap(*this);
3696 return *this;
3697}
3698
3699template<class _Tp>
3700inline _LIBCPP_INLINE_VISIBILITY
3701void
3702weak_ptr<_Tp>::swap(weak_ptr& __r)
3703{
3704 _STD::swap(__ptr_, __r.__ptr_);
3705 _STD::swap(__cntrl_, __r.__cntrl_);
3706}
3707
3708template<class _Tp>
3709inline _LIBCPP_INLINE_VISIBILITY
3710void
3711swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y)
3712{
3713 __x.swap(__y);
3714}
3715
3716template<class _Tp>
3717inline _LIBCPP_INLINE_VISIBILITY
3718void
3719weak_ptr<_Tp>::reset()
3720{
3721 weak_ptr().swap(*this);
3722}
3723
3724template<class _Tp>
3725template<class _Yp>
3726shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3727 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3728 : __ptr_(__r.__ptr_),
3729 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3730{
3731 if (__cntrl_ == 0)
3732#ifndef _LIBCPP_NO_EXCEPTIONS
3733 throw bad_weak_ptr();
3734#else
3735 assert(!"bad_weak_ptr");
3736#endif
3737}
3738
3739template<class _Tp>
3740shared_ptr<_Tp>
3741weak_ptr<_Tp>::lock() const
3742{
3743 shared_ptr<_Tp> __r;
3744 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3745 if (__r.__cntrl_)
3746 __r.__ptr_ = __ptr_;
3747 return __r;
3748}
3749
Howard Hinnant324bb032010-08-22 00:02:43 +00003750template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751
3752template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003753struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00003755{
3756 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3759 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003761 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3762 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003764 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3765 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003766};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767
3768template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003769struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003770 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3771{
Howard Hinnant324bb032010-08-22 00:02:43 +00003772 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3775 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3778 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003780 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3781 {return __x.owner_before(__y);}
3782};
3783
3784template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003785class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003786{
3787 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00003788protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790 enable_shared_from_this() {}
Howard Hinnant82894812010-09-22 16:48:34 +00003791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792 enable_shared_from_this(enable_shared_from_this const&) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794 enable_shared_from_this& operator=(enable_shared_from_this const&) {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00003795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003797public:
Howard Hinnant82894812010-09-22 16:48:34 +00003798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003799 shared_ptr<_Tp> shared_from_this() {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00003800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003801 shared_ptr<_Tp const> shared_from_this() const {return shared_ptr<const _Tp>(__weak_this_);}
3802
3803 template <class _Up> friend class shared_ptr;
3804};
3805
Howard Hinnant21aefc32010-06-03 16:42:57 +00003806template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003807struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003808{
3809 typedef shared_ptr<_Tp> argument_type;
3810 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21aefc32010-06-03 16:42:57 +00003812 result_type operator()(const argument_type& __ptr) const
3813 {
3814 return hash<_Tp*>()(__ptr.get());
3815 }
3816};
3817
Howard Hinnant324bb032010-08-22 00:02:43 +00003818//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00003819struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003820{
3821 enum _
3822 {
3823 relaxed,
3824 preferred,
3825 strict
3826 };
3827
3828 _ __v_;
3829
Howard Hinnant82894812010-09-22 16:48:34 +00003830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003831 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003833 operator int() const {return __v_;}
3834};
3835
3836void declare_reachable(void* __p);
3837void declare_no_pointers(char* __p, size_t __n);
3838void undeclare_no_pointers(char* __p, size_t __n);
3839pointer_safety get_pointer_safety();
3840void* __undeclare_reachable(void*);
3841
3842template <class _Tp>
3843inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00003844_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003845undeclare_reachable(_Tp* __p)
3846{
3847 return static_cast<_Tp*>(__undeclare_reachable(__p));
3848}
3849
3850void* align(size_t, size_t, void*&, size_t&);
3851
3852_LIBCPP_END_NAMESPACE_STD
3853
3854#endif // _LIBCPP_MEMORY