blob: e7fb9d12fabdcd52c78815773ffadd95f584008c [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
Howard Hinnant1694d232011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
46 static pointer pointer_to(<details>) noexcept;
47};
48
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant47761072010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
78
79 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
80 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
81
82 static pointer allocate(allocator_type& a, size_type n);
83 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
84
Howard Hinnant1694d232011-05-28 14:41:13 +000085 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87 template <class T, class... Args>
88 static void construct(allocator_type& a, T* p, Args&&... args);
89
90 template <class T>
91 static void destroy(allocator_type& a, T* p);
92
93 static size_type max_size(const allocator_type& a);
94
95 static allocator_type
96 select_on_container_copy_construction(const allocator_type& a);
97};
98
99template <>
100class allocator<void>
101{
102public:
103 typedef void* pointer;
104 typedef const void* const_pointer;
105 typedef void value_type;
106
107 template <class _Up> struct rebind {typedef allocator<_Up> other;};
108};
109
110template <class T>
111class allocator
112{
113public:
114 typedef size_t size_type;
115 typedef ptrdiff_t difference_type;
116 typedef T* pointer;
117 typedef const T* const_pointer;
118 typedef typename add_lvalue_reference<T>::type reference;
119 typedef typename add_lvalue_reference<const T>::type const_reference;
120 typedef T value_type;
121
122 template <class U> struct rebind {typedef allocator<U> other;};
123
Howard Hinnant1694d232011-05-28 14:41:13 +0000124 allocator() noexcept;
125 allocator(const allocator&) noexcept;
126 template <class U> allocator(const allocator<U>&) noexcept;
127 ~allocator();
128 pointer address(reference x) const noexcept;
129 const_pointer address(const_reference x) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000130 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant1694d232011-05-28 14:41:13 +0000131 void deallocate(pointer p, size_type n) noexcept;
132 size_type max_size() const noexcept;
133 template<class U, class... Args>
134 void construct(U* p, Args&&... args);
135 template <class U>
136 void destroy(U* p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137};
138
139template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000140bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
142template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000143bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144
145template <class OutputIterator, class T>
146class raw_storage_iterator
147 : public iterator<output_iterator_tag,
148 T, // purposefully not C++03
149 ptrdiff_t, // purposefully not C++03
150 T*, // purposefully not C++03
151 raw_storage_iterator&> // purposefully not C++03
152{
153public:
154 explicit raw_storage_iterator(OutputIterator x);
155 raw_storage_iterator& operator*();
156 raw_storage_iterator& operator=(const T& element);
157 raw_storage_iterator& operator++();
158 raw_storage_iterator operator++(int);
159};
160
Howard Hinnant1694d232011-05-28 14:41:13 +0000161template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
162template <class T> void return_temporary_buffer(T* p) noexcept;
163
164template <class T> T* addressof(T& r) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165
166template <class InputIterator, class ForwardIterator>
167ForwardIterator
168uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
169
Howard Hinnant1694d232011-05-28 14:41:13 +0000170template <class InputIterator, class Size, class ForwardIterator>
171ForwardIterator
172uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
173
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174template <class ForwardIterator, class T>
175void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
176
177template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +0000178ForwardIterator
179uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180
181template <class Y> struct auto_ptr_ref {};
182
183template<class X>
184class auto_ptr
185{
186public:
187 typedef X element_type;
188
189 explicit auto_ptr(X* p =0) throw();
190 auto_ptr(auto_ptr&) throw();
191 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
192 auto_ptr& operator=(auto_ptr&) throw();
193 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
194 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
195 ~auto_ptr() throw();
196
197 typename add_lvalue_reference<X>::type operator*() const throw();
198 X* operator->() const throw();
199 X* get() const throw();
200 X* release() throw();
201 void reset(X* p =0) throw();
202
203 auto_ptr(auto_ptr_ref<X>) throw();
204 template<class Y> operator auto_ptr_ref<Y>() throw();
205 template<class Y> operator auto_ptr<Y>() throw();
206};
207
Howard Hinnante92c3d72010-08-19 18:39:17 +0000208template <class T>
209struct default_delete
210{
Howard Hinnant1694d232011-05-28 14:41:13 +0000211 constexpr default_delete() noexcept = default;
212 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000213
Howard Hinnant1694d232011-05-28 14:41:13 +0000214 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000215};
216
217template <class T>
218struct default_delete<T[]>
219{
Howard Hinnant1694d232011-05-28 14:41:13 +0000220 constexpr default_delete() noexcept = default;
221 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000222 template <class U> void operator()(U*) const = delete;
223};
224
Howard Hinnante92c3d72010-08-19 18:39:17 +0000225template <class T, class D = default_delete<T>>
226class unique_ptr
227{
228public:
229 typedef see below pointer;
230 typedef T element_type;
231 typedef D deleter_type;
232
233 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000234 constexpr unique_ptr() noexcept;
235 explicit unique_ptr(pointer p) noexcept;
236 unique_ptr(pointer p, see below d1) noexcept;
237 unique_ptr(pointer p, see below d2) noexcept;
238 unique_ptr(unique_ptr&& u) noexcept;
239 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000240 template <class U, class E>
Howard Hinnant1694d232011-05-28 14:41:13 +0000241 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000242 template <class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000243 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000244
245 // destructor
246 ~unique_ptr();
247
248 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000249 unique_ptr& operator=(unique_ptr&& u) noexcept;
250 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
251 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000252
253 // observers
254 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000255 pointer operator->() const noexcept;
256 pointer get() const noexcept;
257 deleter_type& get_deleter() noexcept;
258 const deleter_type& get_deleter() const noexcept;
259 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000260
261 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000262 pointer release() noexcept;
263 void reset(pointer p = pointer()) noexcept;
264 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000265};
266
267template <class T, class D>
268class unique_ptr<T[], D>
269{
270public:
271 typedef implementation-defined pointer;
272 typedef T element_type;
273 typedef D deleter_type;
274
275 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000276 constexpr unique_ptr() noexcept;
277 explicit unique_ptr(pointer p) noexcept;
278 unique_ptr(pointer p, see below d) noexcept;
279 unique_ptr(pointer p, see below d) noexcept;
280 unique_ptr(unique_ptr&& u) noexcept;
281 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000282
283 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000284 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000285
286 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000287 unique_ptr& operator=(unique_ptr&& u) noexcept;
288 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000289
290 // observers
291 T& operator[](size_t i) const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000292 pointer get() const noexcept;
293 deleter_type& get_deleter() noexcept;
294 const deleter_type& get_deleter() const noexcept;
295 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000296
297 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000298 pointer release() noexcept;
299 void reset(pointer p = pointer()) noexcept;
300 void reset(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000301 template <class U> void reset(U) = delete;
Howard Hinnant1694d232011-05-28 14:41:13 +0000302 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000303};
304
305template <class T, class D>
Howard Hinnant1694d232011-05-28 14:41:13 +0000306 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000307
308template <class T1, class D1, class T2, class D2>
309 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
310template <class T1, class D1, class T2, class D2>
311 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
312template <class T1, class D1, class T2, class D2>
313 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
314template <class T1, class D1, class T2, class D2>
315 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
316template <class T1, class D1, class T2, class D2>
317 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
318template <class T1, class D1, class T2, class D2>
319 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
320
Howard Hinnant1694d232011-05-28 14:41:13 +0000321template <class T, class D>
322 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
323template <class T, class D>
324 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
325template <class T, class D>
326 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
327template <class T, class D>
328 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
329
330template <class T, class D>
331 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
332template <class T, class D>
333 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
334template <class T, class D>
335 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
336template <class T, class D>
337 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
338template <class T, class D>
339 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
340template <class T, class D>
341 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
342template <class T, class D>
343 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
344template <class T, class D>
345 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
346
Howard Hinnante92c3d72010-08-19 18:39:17 +0000347class bad_weak_ptr
348 : public std::exception
349{
Howard Hinnant1694d232011-05-28 14:41:13 +0000350 bad_weak_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000351};
352
353template<class T>
354class shared_ptr
355{
356public:
357 typedef T element_type;
358
359 // constructors:
Howard Hinnant1694d232011-05-28 14:41:13 +0000360 constexpr shared_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000361 template<class Y> explicit shared_ptr(Y* p);
362 template<class Y, class D> shared_ptr(Y* p, D d);
363 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
364 template <class D> shared_ptr(nullptr_t p, D d);
365 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant1694d232011-05-28 14:41:13 +0000366 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
367 shared_ptr(const shared_ptr& r) noexcept;
368 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
369 shared_ptr(shared_ptr&& r) noexcept;
370 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000371 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
372 template<class Y> shared_ptr(auto_ptr<Y>&& r);
373 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
374 shared_ptr(nullptr_t) : shared_ptr() { }
375
376 // destructor:
377 ~shared_ptr();
378
379 // assignment:
Howard Hinnant1694d232011-05-28 14:41:13 +0000380 shared_ptr& operator=(const shared_ptr& r) noexcept;
381 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
382 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000383 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
384 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
385 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
386
387 // modifiers:
Howard Hinnant1694d232011-05-28 14:41:13 +0000388 void swap(shared_ptr& r) noexcept;
389 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000390 template<class Y> void reset(Y* p);
391 template<class Y, class D> void reset(Y* p, D d);
392 template<class Y, class D, class A> void reset(Y* p, D d, A a);
393
Howard Hinnant1694d232011-05-28 14:41:13 +0000394 // observers:
395 T* get() const noexcept;
396 T& operator*() const noexcept;
397 T* operator->() const noexcept;
398 long use_count() const noexcept;
399 bool unique() const noexcept;
400 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000401 template<class U> bool owner_before(shared_ptr<U> const& b) const;
402 template<class U> bool owner_before(weak_ptr<U> const& b) const;
403};
404
405// shared_ptr comparisons:
406template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000407 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000408template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000409 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000410template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000411 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000412template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000413 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000414template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000415 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000416template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000417 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
418
419template <class T>
420 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
421template <class T>
422 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
423template <class T>
424 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
425template <class T>
426 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
427template <class T>
428 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
429template <class T>
430bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
431template <class T>
432 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
433template <class T>
434 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
435template <class T>
436 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
437template <class T>
438 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
439template <class T>
440 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
441template <class T>
442 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000443
444// shared_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000445template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000446
447// shared_ptr casts:
448template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000449 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000451 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000453 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000454
455// shared_ptr I/O:
456template<class E, class T, class Y>
457 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
458
459// shared_ptr get_deleter:
Howard Hinnant1694d232011-05-28 14:41:13 +0000460template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000461
462template<class T, class... Args>
463 shared_ptr<T> make_shared(Args&&... args);
464template<class T, class A, class... Args>
465 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
466
467template<class T>
468class weak_ptr
469{
470public:
471 typedef T element_type;
472
473 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000474 constexpr weak_ptr() noexcept;
475 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
476 weak_ptr(weak_ptr const& r) noexcept;
477 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000478
479 // destructor
480 ~weak_ptr();
481
482 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000483 weak_ptr& operator=(weak_ptr const& r) noexcept;
484 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
485 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000486
487 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000488 void swap(weak_ptr& r) noexcept;
489 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000490
491 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000492 long use_count() const noexcept;
493 bool expired() const noexcept;
494 shared_ptr<T> lock() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000495 template<class U> bool owner_before(shared_ptr<U> const& b);
496 template<class U> bool owner_before(weak_ptr<U> const& b);
497};
498
499// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000500template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000501
502// class owner_less:
503template<class T> struct owner_less;
504
505template<class T>
506struct owner_less<shared_ptr<T>>
507 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
508{
509 typedef bool result_type;
510 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
511 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
512 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
513};
514
515template<class T>
516struct owner_less<weak_ptr<T>>
517 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
518{
519 typedef bool result_type;
520 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
521 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
522 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
523};
524
525template<class T>
526class enable_shared_from_this
527{
528protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000529 constexpr enable_shared_from_this() noexcept;
530 enable_shared_from_this(enable_shared_from_this const&) noexcept;
531 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000532 ~enable_shared_from_this();
533public:
534 shared_ptr<T> shared_from_this();
535 shared_ptr<T const> shared_from_this() const;
536};
537
538template<class T>
539 bool atomic_is_lock_free(const shared_ptr<T>* p);
540template<class T>
541 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
542template<class T>
543 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
544template<class T>
545 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
546template<class T>
547 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
548template<class T>
549 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
550template<class T>
551 shared_ptr<T>
552 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
553template<class T>
554 bool
555 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
556template<class T>
557 bool
558 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
559template<class T>
560 bool
561 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
562 shared_ptr<T> w, memory_order success,
563 memory_order failure);
564template<class T>
565 bool
566 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
567 shared_ptr<T> w, memory_order success,
568 memory_order failure);
569// Hash support
570template <class T> struct hash;
571template <class T, class D> struct hash<unique_ptr<T, D> >;
572template <class T> struct hash<shared_ptr<T> >;
573
574// Pointer safety
575enum class pointer_safety { relaxed, preferred, strict };
576void declare_reachable(void *p);
577template <class T> T *undeclare_reachable(T *p);
578void declare_no_pointers(char *p, size_t n);
579void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000580pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000581
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
583
584} // std
585
586*/
587
588#include <__config>
589#include <type_traits>
590#include <typeinfo>
591#include <cstddef>
592#include <cstdint>
593#include <new>
594#include <utility>
595#include <limits>
596#include <iterator>
597#include <__functional_base>
Howard Hinnant464aa5c2011-07-18 15:51:59 +0000598#include <iosfwd>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599#if defined(_LIBCPP_NO_EXCEPTIONS)
600 #include <cassert>
601#endif
602
Howard Hinnant66c6f972011-11-29 16:45:27 +0000603#include <__undef_min_max>
604
Howard Hinnant08e17472011-10-17 20:05:10 +0000605#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000607#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608
609_LIBCPP_BEGIN_NAMESPACE_STD
610
611// allocator_arg_t
612
Howard Hinnant82894812010-09-22 16:48:34 +0000613struct _LIBCPP_VISIBLE allocator_arg_t { };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614
615extern const allocator_arg_t allocator_arg;
616
617// addressof
618
619template <class _Tp>
620inline _LIBCPP_INLINE_VISIBILITY
621_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000622addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623{
624 return (_Tp*)&(char&)__x;
625}
626
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000627#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
628// Objective-C++ Automatic Reference Counting uses qualified pointers
629// that require special addressof() signatures. When
630// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
631// itself is providing these definitions. Otherwise, we provide them.
632template <class _Tp>
633inline _LIBCPP_INLINE_VISIBILITY
634__strong _Tp*
635addressof(__strong _Tp& __x) _NOEXCEPT
636{
637 return &__x;
638}
639
640#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
641template <class _Tp>
642inline _LIBCPP_INLINE_VISIBILITY
643__weak _Tp*
644addressof(__weak _Tp& __x) _NOEXCEPT
645{
646 return &__x;
647}
648#endif
649
650template <class _Tp>
651inline _LIBCPP_INLINE_VISIBILITY
652__autoreleasing _Tp*
653addressof(__autoreleasing _Tp& __x) _NOEXCEPT
654{
655 return &__x;
656}
657
658template <class _Tp>
659inline _LIBCPP_INLINE_VISIBILITY
660__unsafe_unretained _Tp*
661addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
662{
663 return &__x;
664}
665#endif
666
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667template <class _Tp> class allocator;
668
669template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000670class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671{
672public:
673 typedef void* pointer;
674 typedef const void* const_pointer;
675 typedef void value_type;
676
677 template <class _Up> struct rebind {typedef allocator<_Up> other;};
678};
679
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680// pointer_traits
681
682template <class _Tp>
683struct __has_element_type
684{
685private:
686 struct __two {char _; char __;};
687 template <class _Up> static __two __test(...);
688 template <class _Up> static char __test(typename _Up::element_type* = 0);
689public:
690 static const bool value = sizeof(__test<_Tp>(0)) == 1;
691};
692
693template <class _Ptr, bool = __has_element_type<_Ptr>::value>
694struct __pointer_traits_element_type;
695
696template <class _Ptr>
697struct __pointer_traits_element_type<_Ptr, true>
698{
699 typedef typename _Ptr::element_type type;
700};
701
702#ifndef _LIBCPP_HAS_NO_VARIADICS
703
704template <template <class, class...> class _Sp, class _Tp, class ..._Args>
705struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
706{
707 typedef typename _Sp<_Tp, _Args...>::element_type type;
708};
709
710template <template <class, class...> class _Sp, class _Tp, class ..._Args>
711struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
712{
713 typedef _Tp type;
714};
715
Howard Hinnant324bb032010-08-22 00:02:43 +0000716#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717
718template <template <class> class _Sp, class _Tp>
719struct __pointer_traits_element_type<_Sp<_Tp>, true>
720{
721 typedef typename _Sp<_Tp>::element_type type;
722};
723
724template <template <class> class _Sp, class _Tp>
725struct __pointer_traits_element_type<_Sp<_Tp>, false>
726{
727 typedef _Tp type;
728};
729
730template <template <class, class> class _Sp, class _Tp, class _A0>
731struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
732{
733 typedef typename _Sp<_Tp, _A0>::element_type type;
734};
735
736template <template <class, class> class _Sp, class _Tp, class _A0>
737struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
738{
739 typedef _Tp type;
740};
741
742template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
743struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
744{
745 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
746};
747
748template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
749struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
750{
751 typedef _Tp type;
752};
753
754template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
755 class _A1, class _A2>
756struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
757{
758 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
759};
760
761template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
762 class _A1, class _A2>
763struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
764{
765 typedef _Tp type;
766};
767
Howard Hinnant324bb032010-08-22 00:02:43 +0000768#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000769
770template <class _Tp>
771struct __has_difference_type
772{
773private:
774 struct __two {char _; char __;};
775 template <class _Up> static __two __test(...);
776 template <class _Up> static char __test(typename _Up::difference_type* = 0);
777public:
778 static const bool value = sizeof(__test<_Tp>(0)) == 1;
779};
780
781template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
782struct __pointer_traits_difference_type
783{
784 typedef ptrdiff_t type;
785};
786
787template <class _Ptr>
788struct __pointer_traits_difference_type<_Ptr, true>
789{
790 typedef typename _Ptr::difference_type type;
791};
792
793template <class _Tp, class _Up>
794struct __has_rebind
795{
796private:
797 struct __two {char _; char __;};
798 template <class _Xp> static __two __test(...);
799 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
800public:
801 static const bool value = sizeof(__test<_Tp>(0)) == 1;
802};
803
804template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
805struct __pointer_traits_rebind
806{
807#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
808 typedef typename _Tp::template rebind<_Up> type;
809#else
810 typedef typename _Tp::template rebind<_Up>::other type;
811#endif
812};
813
814#ifndef _LIBCPP_HAS_NO_VARIADICS
815
816template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
817struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
818{
819#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
820 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
821#else
822 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
823#endif
824};
825
826template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
827struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
828{
829 typedef _Sp<_Up, _Args...> type;
830};
831
Howard Hinnant324bb032010-08-22 00:02:43 +0000832#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833
834template <template <class> class _Sp, class _Tp, class _Up>
835struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
836{
837#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
838 typedef typename _Sp<_Tp>::template rebind<_Up> type;
839#else
840 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
841#endif
842};
843
844template <template <class> class _Sp, class _Tp, class _Up>
845struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
846{
847 typedef _Sp<_Up> type;
848};
849
850template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
851struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
852{
853#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
854 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
855#else
856 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
857#endif
858};
859
860template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
861struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
862{
863 typedef _Sp<_Up, _A0> type;
864};
865
866template <template <class, class, class> class _Sp, class _Tp, class _A0,
867 class _A1, class _Up>
868struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
869{
870#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
871 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
872#else
873 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
874#endif
875};
876
877template <template <class, class, class> class _Sp, class _Tp, class _A0,
878 class _A1, class _Up>
879struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
880{
881 typedef _Sp<_Up, _A0, _A1> type;
882};
883
884template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
885 class _A1, class _A2, class _Up>
886struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
887{
888#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
889 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
890#else
891 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
892#endif
893};
894
895template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
896 class _A1, class _A2, class _Up>
897struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
898{
899 typedef _Sp<_Up, _A0, _A1, _A2> type;
900};
901
Howard Hinnant324bb032010-08-22 00:02:43 +0000902#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903
904template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000905struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906{
907 typedef _Ptr pointer;
908 typedef typename __pointer_traits_element_type<pointer>::type element_type;
909 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
910
911#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000912 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913#else
914 template <class _Up> struct rebind
915 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000916#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917
918private:
919 struct __nat {};
920public:
Howard Hinnant82894812010-09-22 16:48:34 +0000921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922 static pointer pointer_to(typename conditional<is_void<element_type>::value,
923 __nat, element_type>::type& __r)
924 {return pointer::pointer_to(__r);}
925};
926
927template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000928struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929{
930 typedef _Tp* pointer;
931 typedef _Tp element_type;
932 typedef ptrdiff_t difference_type;
933
934#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
935 template <class _Up> using rebind = _Up*;
936#else
937 template <class _Up> struct rebind {typedef _Up* other;};
938#endif
939
940private:
941 struct __nat {};
942public:
Howard Hinnant82894812010-09-22 16:48:34 +0000943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000945 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000946 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947};
948
949// allocator_traits
950
951namespace __has_pointer_type_imp
952{
953 template <class _Up> static __two test(...);
954 template <class _Up> static char test(typename _Up::pointer* = 0);
955}
956
957template <class _Tp>
958struct __has_pointer_type
959 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
960{
961};
962
963namespace __pointer_type_imp
964{
965
966template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
967struct __pointer_type
968{
969 typedef typename _Dp::pointer type;
970};
971
972template <class _Tp, class _Dp>
973struct __pointer_type<_Tp, _Dp, false>
974{
975 typedef _Tp* type;
976};
977
Howard Hinnant47761072010-11-18 01:40:00 +0000978} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979
980template <class _Tp, class _Dp>
981struct __pointer_type
982{
983 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
984};
985
986template <class _Tp>
987struct __has_const_pointer
988{
989private:
990 struct __two {char _; char __;};
991 template <class _Up> static __two __test(...);
992 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
993public:
994 static const bool value = sizeof(__test<_Tp>(0)) == 1;
995};
996
997template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
998struct __const_pointer
999{
1000 typedef typename _Alloc::const_pointer type;
1001};
1002
1003template <class _Tp, class _Ptr, class _Alloc>
1004struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1005{
1006#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1007 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1008#else
1009 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1010#endif
1011};
1012
1013template <class _Tp>
1014struct __has_void_pointer
1015{
1016private:
1017 struct __two {char _; char __;};
1018 template <class _Up> static __two __test(...);
1019 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1020public:
1021 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1022};
1023
1024template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1025struct __void_pointer
1026{
1027 typedef typename _Alloc::void_pointer type;
1028};
1029
1030template <class _Ptr, class _Alloc>
1031struct __void_pointer<_Ptr, _Alloc, false>
1032{
1033#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1034 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1035#else
1036 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1037#endif
1038};
1039
1040template <class _Tp>
1041struct __has_const_void_pointer
1042{
1043private:
1044 struct __two {char _; char __;};
1045 template <class _Up> static __two __test(...);
1046 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1047public:
1048 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1049};
1050
1051template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1052struct __const_void_pointer
1053{
1054 typedef typename _Alloc::const_void_pointer type;
1055};
1056
1057template <class _Ptr, class _Alloc>
1058struct __const_void_pointer<_Ptr, _Alloc, false>
1059{
1060#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1061 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1062#else
1063 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1064#endif
1065};
1066
Howard Hinnant99968442011-11-29 18:15:50 +00001067template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001069_Tp*
1070__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071{
1072 return __p;
1073}
1074
1075template <class _Pointer>
1076inline _LIBCPP_INLINE_VISIBILITY
1077typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001078__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001080 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081}
1082
1083template <class _Tp>
1084struct __has_size_type
1085{
1086private:
1087 struct __two {char _; char __;};
1088 template <class _Up> static __two __test(...);
1089 template <class _Up> static char __test(typename _Up::size_type* = 0);
1090public:
1091 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1092};
1093
Howard Hinnant47761072010-11-18 01:40:00 +00001094template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095struct __size_type
1096{
Howard Hinnant47761072010-11-18 01:40:00 +00001097 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001098};
1099
Howard Hinnant47761072010-11-18 01:40:00 +00001100template <class _Alloc, class _DiffType>
1101struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102{
1103 typedef typename _Alloc::size_type type;
1104};
1105
1106template <class _Tp>
1107struct __has_propagate_on_container_copy_assignment
1108{
1109private:
1110 struct __two {char _; char __;};
1111 template <class _Up> static __two __test(...);
1112 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1113public:
1114 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1115};
1116
1117template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1118struct __propagate_on_container_copy_assignment
1119{
1120 typedef false_type type;
1121};
1122
1123template <class _Alloc>
1124struct __propagate_on_container_copy_assignment<_Alloc, true>
1125{
1126 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1127};
1128
1129template <class _Tp>
1130struct __has_propagate_on_container_move_assignment
1131{
1132private:
1133 struct __two {char _; char __;};
1134 template <class _Up> static __two __test(...);
1135 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1136public:
1137 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1138};
1139
1140template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1141struct __propagate_on_container_move_assignment
1142{
1143 typedef false_type type;
1144};
1145
1146template <class _Alloc>
1147struct __propagate_on_container_move_assignment<_Alloc, true>
1148{
1149 typedef typename _Alloc::propagate_on_container_move_assignment type;
1150};
1151
1152template <class _Tp>
1153struct __has_propagate_on_container_swap
1154{
1155private:
1156 struct __two {char _; char __;};
1157 template <class _Up> static __two __test(...);
1158 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1159public:
1160 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1161};
1162
1163template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1164struct __propagate_on_container_swap
1165{
1166 typedef false_type type;
1167};
1168
1169template <class _Alloc>
1170struct __propagate_on_container_swap<_Alloc, true>
1171{
1172 typedef typename _Alloc::propagate_on_container_swap type;
1173};
1174
1175template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1176struct __has_rebind_other
1177{
1178private:
1179 struct __two {char _; char __;};
1180 template <class _Xp> static __two __test(...);
1181 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1182public:
1183 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1184};
1185
1186template <class _Tp, class _Up>
1187struct __has_rebind_other<_Tp, _Up, false>
1188{
1189 static const bool value = false;
1190};
1191
1192template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1193struct __allocator_traits_rebind
1194{
1195 typedef typename _Tp::template rebind<_Up>::other type;
1196};
1197
1198#ifndef _LIBCPP_HAS_NO_VARIADICS
1199
1200template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1201struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1202{
1203 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1204};
1205
1206template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1207struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1208{
1209 typedef _Alloc<_Up, _Args...> type;
1210};
1211
Howard Hinnant324bb032010-08-22 00:02:43 +00001212#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213
1214template <template <class> class _Alloc, class _Tp, class _Up>
1215struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1216{
1217 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1218};
1219
1220template <template <class> class _Alloc, class _Tp, class _Up>
1221struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1222{
1223 typedef _Alloc<_Up> type;
1224};
1225
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1227struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1228{
1229 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1230};
1231
1232template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1233struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1234{
1235 typedef _Alloc<_Up, _A0> type;
1236};
1237
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001238template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1239 class _A1, class _Up>
1240struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1241{
1242 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1243};
1244
1245template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1246 class _A1, class _Up>
1247struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1248{
1249 typedef _Alloc<_Up, _A0, _A1> type;
1250};
1251
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1253 class _A1, class _A2, class _Up>
1254struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1255{
1256 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1257};
1258
1259template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1260 class _A1, class _A2, class _Up>
1261struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1262{
1263 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1264};
1265
Howard Hinnant324bb032010-08-22 00:02:43 +00001266#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267
1268#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1269
1270template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1271auto
1272__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1273 -> decltype(__a.allocate(__sz, __p), true_type());
1274
1275template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1276auto
1277__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1278 -> false_type;
1279
1280template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1281struct __has_allocate_hint
1282 : integral_constant<bool,
1283 is_same<
1284 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1285 declval<_SizeType>(),
1286 declval<_ConstVoidPtr>())),
1287 true_type>::value>
1288{
1289};
1290
Howard Hinnant324bb032010-08-22 00:02:43 +00001291#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292
1293template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1294struct __has_allocate_hint
1295 : true_type
1296{
1297};
1298
Howard Hinnant324bb032010-08-22 00:02:43 +00001299#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300
Howard Hinnant23369ee2011-07-29 21:35:53 +00001301#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302
1303template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001304decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1305 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 true_type())
1307__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1308
1309template <class _Alloc, class _Pointer, class ..._Args>
1310false_type
1311__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1312
1313template <class _Alloc, class _Pointer, class ..._Args>
1314struct __has_construct
1315 : integral_constant<bool,
1316 is_same<
1317 decltype(__has_construct_test(declval<_Alloc>(),
1318 declval<_Pointer>(),
1319 declval<_Args>()...)),
1320 true_type>::value>
1321{
1322};
1323
1324template <class _Alloc, class _Pointer>
1325auto
1326__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1327 -> decltype(__a.destroy(__p), true_type());
1328
1329template <class _Alloc, class _Pointer>
1330auto
1331__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1332 -> false_type;
1333
1334template <class _Alloc, class _Pointer>
1335struct __has_destroy
1336 : integral_constant<bool,
1337 is_same<
1338 decltype(__has_destroy_test(declval<_Alloc>(),
1339 declval<_Pointer>())),
1340 true_type>::value>
1341{
1342};
1343
1344template <class _Alloc>
1345auto
1346__has_max_size_test(_Alloc&& __a)
1347 -> decltype(__a.max_size(), true_type());
1348
1349template <class _Alloc>
1350auto
1351__has_max_size_test(const volatile _Alloc& __a)
1352 -> false_type;
1353
1354template <class _Alloc>
1355struct __has_max_size
1356 : integral_constant<bool,
1357 is_same<
1358 decltype(__has_max_size_test(declval<_Alloc&>())),
1359 true_type>::value>
1360{
1361};
1362
1363template <class _Alloc>
1364auto
1365__has_select_on_container_copy_construction_test(_Alloc&& __a)
1366 -> decltype(__a.select_on_container_copy_construction(), true_type());
1367
1368template <class _Alloc>
1369auto
1370__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1371 -> false_type;
1372
1373template <class _Alloc>
1374struct __has_select_on_container_copy_construction
1375 : integral_constant<bool,
1376 is_same<
1377 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1378 true_type>::value>
1379{
1380};
1381
Howard Hinnant324bb032010-08-22 00:02:43 +00001382#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383
1384#ifndef _LIBCPP_HAS_NO_VARIADICS
1385
1386template <class _Alloc, class _Pointer, class ..._Args>
1387struct __has_construct
1388 : false_type
1389{
1390};
1391
Howard Hinnant324bb032010-08-22 00:02:43 +00001392#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393
1394template <class _Alloc, class _Pointer>
1395struct __has_destroy
1396 : false_type
1397{
1398};
1399
1400template <class _Alloc>
1401struct __has_max_size
1402 : true_type
1403{
1404};
1405
1406template <class _Alloc>
1407struct __has_select_on_container_copy_construction
1408 : false_type
1409{
1410};
1411
Howard Hinnant324bb032010-08-22 00:02:43 +00001412#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413
Howard Hinnant47761072010-11-18 01:40:00 +00001414template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1415struct __alloc_traits_difference_type
1416{
1417 typedef typename pointer_traits<_Ptr>::difference_type type;
1418};
1419
1420template <class _Alloc, class _Ptr>
1421struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1422{
1423 typedef typename _Alloc::difference_type type;
1424};
1425
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001427struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428{
1429 typedef _Alloc allocator_type;
1430 typedef typename allocator_type::value_type value_type;
1431
1432 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1433 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1434 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1435 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1436
Howard Hinnant47761072010-11-18 01:40:00 +00001437 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1438 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439
1440 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1441 propagate_on_container_copy_assignment;
1442 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1443 propagate_on_container_move_assignment;
1444 typedef typename __propagate_on_container_swap<allocator_type>::type
1445 propagate_on_container_swap;
1446
1447#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1448 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001449 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001451#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452 template <class _Tp> struct rebind_alloc
1453 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1454 template <class _Tp> struct rebind_traits
1455 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001456#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457
Howard Hinnant82894812010-09-22 16:48:34 +00001458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459 static pointer allocate(allocator_type& __a, size_type __n)
1460 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1463 {return allocate(__a, __n, __hint,
1464 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1465
Howard Hinnant82894812010-09-22 16:48:34 +00001466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001467 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 {__a.deallocate(__p, __n);}
1469
1470#ifndef _LIBCPP_HAS_NO_VARIADICS
1471 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1474 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001475 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001476#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479 static void construct(allocator_type& __a, _Tp* __p)
1480 {
1481 ::new ((void*)__p) _Tp();
1482 }
1483 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1486 {
1487 ::new ((void*)__p) _Tp(__a0);
1488 }
1489 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1492 const _A1& __a1)
1493 {
1494 ::new ((void*)__p) _Tp(__a0, __a1);
1495 }
1496 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1499 const _A1& __a1, const _A2& __a2)
1500 {
1501 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1502 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001503#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504
1505 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507 static void destroy(allocator_type& __a, _Tp* __p)
1508 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1509
Howard Hinnant82894812010-09-22 16:48:34 +00001510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511 static size_type max_size(const allocator_type& __a)
1512 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1513
Howard Hinnant82894812010-09-22 16:48:34 +00001514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 static allocator_type
1516 select_on_container_copy_construction(const allocator_type& __a)
1517 {return select_on_container_copy_construction(
1518 __has_select_on_container_copy_construction<const allocator_type>(),
1519 __a);}
1520
1521private:
1522
Howard Hinnant82894812010-09-22 16:48:34 +00001523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524 static pointer allocate(allocator_type& __a, size_type __n,
1525 const_void_pointer __hint, true_type)
1526 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001529 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 {return __a.allocate(__n);}
1531
1532#ifndef _LIBCPP_HAS_NO_VARIADICS
1533 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001536 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1540 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001541 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001543#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544
1545 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1548 {__a.destroy(__p);}
1549 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551 static void __destroy(false_type, allocator_type&, _Tp* __p)
1552 {
1553 __p->~_Tp();
1554 }
1555
Howard Hinnant82894812010-09-22 16:48:34 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557 static size_type __max_size(true_type, const allocator_type& __a)
1558 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 static size_type __max_size(false_type, const allocator_type&)
1561 {return numeric_limits<size_type>::max();}
1562
Howard Hinnant82894812010-09-22 16:48:34 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 static allocator_type
1565 select_on_container_copy_construction(true_type, const allocator_type& __a)
1566 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568 static allocator_type
1569 select_on_container_copy_construction(false_type, const allocator_type& __a)
1570 {return __a;}
1571};
1572
1573// uses_allocator
1574
1575template <class _Tp>
1576struct __has_allocator_type
1577{
1578private:
1579 struct __two {char _; char __;};
1580 template <class _Up> static __two __test(...);
1581 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1582public:
1583 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1584};
1585
1586template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1587struct __uses_allocator
1588 : public integral_constant<bool,
1589 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1590{
1591};
1592
1593template <class _Tp, class _Alloc>
1594struct __uses_allocator<_Tp, _Alloc, false>
1595 : public false_type
1596{
1597};
1598
1599template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001600struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601 : public __uses_allocator<_Tp, _Alloc>
1602{
1603};
1604
Howard Hinnantac38bae2011-01-11 20:02:45 +00001605#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606
1607// uses-allocator construction
1608
1609template <class _Tp, class _Alloc, class ..._Args>
1610struct __uses_alloc_ctor_imp
1611{
1612 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1613 static const bool __ic =
1614 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1615 static const int value = __ua ? 2 - __ic : 0;
1616};
1617
1618template <class _Tp, class _Alloc, class ..._Args>
1619struct __uses_alloc_ctor
1620 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1621 {};
1622
Howard Hinnantac38bae2011-01-11 20:02:45 +00001623#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624
1625// allocator
1626
1627template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001628class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629{
1630public:
1631 typedef size_t size_type;
1632 typedef ptrdiff_t difference_type;
1633 typedef _Tp* pointer;
1634 typedef const _Tp* const_pointer;
1635 typedef _Tp& reference;
1636 typedef const _Tp& const_reference;
1637 typedef _Tp value_type;
1638
Howard Hinnant18884f42011-06-02 21:38:57 +00001639 typedef true_type propagate_on_container_move_assignment;
1640
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1642
Howard Hinnant1694d232011-05-28 14:41:13 +00001643 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1644 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1645 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001646 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001647 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001648 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1650 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001651 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1652 {::operator delete((void*)__p);}
1653 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1654 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001655#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656 template <class _Up, class... _Args>
1657 _LIBCPP_INLINE_VISIBILITY
1658 void
1659 construct(_Up* __p, _Args&&... __args)
1660 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001661 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001663#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664 _LIBCPP_INLINE_VISIBILITY
1665 void
1666 construct(pointer __p)
1667 {
1668 ::new((void*)__p) _Tp();
1669 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001670# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671 template <class _A0>
1672 _LIBCPP_INLINE_VISIBILITY
1673 typename enable_if
1674 <
1675 !is_convertible<_A0, __rv<_A0> >::value,
1676 void
1677 >::type
1678 construct(pointer __p, _A0& __a0)
1679 {
1680 ::new((void*)__p) _Tp(__a0);
1681 }
1682 template <class _A0>
1683 _LIBCPP_INLINE_VISIBILITY
1684 typename enable_if
1685 <
1686 !is_convertible<_A0, __rv<_A0> >::value,
1687 void
1688 >::type
1689 construct(pointer __p, const _A0& __a0)
1690 {
1691 ::new((void*)__p) _Tp(__a0);
1692 }
1693 template <class _A0>
1694 _LIBCPP_INLINE_VISIBILITY
1695 typename enable_if
1696 <
1697 is_convertible<_A0, __rv<_A0> >::value,
1698 void
1699 >::type
1700 construct(pointer __p, _A0 __a0)
1701 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001702 ::new((void*)__p) _Tp(_VSTD::move(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001704# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705 template <class _A0, class _A1>
1706 _LIBCPP_INLINE_VISIBILITY
1707 void
1708 construct(pointer __p, _A0& __a0, _A1& __a1)
1709 {
1710 ::new((void*)__p) _Tp(__a0, __a1);
1711 }
1712 template <class _A0, class _A1>
1713 _LIBCPP_INLINE_VISIBILITY
1714 void
1715 construct(pointer __p, const _A0& __a0, _A1& __a1)
1716 {
1717 ::new((void*)__p) _Tp(__a0, __a1);
1718 }
1719 template <class _A0, class _A1>
1720 _LIBCPP_INLINE_VISIBILITY
1721 void
1722 construct(pointer __p, _A0& __a0, const _A1& __a1)
1723 {
1724 ::new((void*)__p) _Tp(__a0, __a1);
1725 }
1726 template <class _A0, class _A1>
1727 _LIBCPP_INLINE_VISIBILITY
1728 void
1729 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1730 {
1731 ::new((void*)__p) _Tp(__a0, __a1);
1732 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001733#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1735};
1736
1737template <class _Tp, class _Up>
1738inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001739bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740
1741template <class _Tp, class _Up>
1742inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001743bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744
1745template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001746class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747 : public iterator<output_iterator_tag,
1748 _Tp, // purposefully not C++03
1749 ptrdiff_t, // purposefully not C++03
1750 _Tp*, // purposefully not C++03
1751 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1752{
1753private:
1754 _OutputIterator __x_;
1755public:
1756 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1757 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1758 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1759 {::new(&*__x_) _Tp(__element); return *this;}
1760 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1761 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1762 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1763};
1764
1765template <class _Tp>
1766pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001767get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001768{
1769 pair<_Tp*, ptrdiff_t> __r(0, 0);
1770 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1771 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1772 / sizeof(_Tp);
1773 if (__n > __m)
1774 __n = __m;
1775 while (__n > 0)
1776 {
1777 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1778 if (__r.first)
1779 {
1780 __r.second = __n;
1781 break;
1782 }
1783 __n /= 2;
1784 }
1785 return __r;
1786}
1787
1788template <class _Tp>
1789inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001790void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791
1792template <class _Tp>
1793struct auto_ptr_ref
1794{
1795 _Tp* __ptr_;
1796};
1797
1798template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001799class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800{
1801private:
1802 _Tp* __ptr_;
1803public:
1804 typedef _Tp element_type;
1805
1806 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1807 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1808 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1809 : __ptr_(__p.release()) {}
1810 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1811 {reset(__p.release()); return *this;}
1812 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1813 {reset(__p.release()); return *this;}
1814 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1815 {reset(__p.__ptr_); return *this;}
1816 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1817
1818 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1819 {return *__ptr_;}
1820 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1821 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1822 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1823 {
1824 _Tp* __t = __ptr_;
1825 __ptr_ = 0;
1826 return __t;
1827 }
1828 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1829 {
1830 if (__ptr_ != __p)
1831 delete __ptr_;
1832 __ptr_ = __p;
1833 }
1834
1835 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1836 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1837 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1838 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1839 {return auto_ptr<_Up>(release());}
1840};
1841
1842template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001843class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844{
1845public:
1846 typedef void element_type;
1847};
1848
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1850 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001851 bool = is_empty<_T1>::value
1852#if __has_feature(is_final)
1853 && !__is_final(_T1)
1854#endif
1855 ,
1856 bool = is_empty<_T2>::value
1857#if __has_feature(is_final)
1858 && !__is_final(_T2)
1859#endif
1860 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861struct __libcpp_compressed_pair_switch;
1862
1863template <class _T1, class _T2, bool IsSame>
1864struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1865
1866template <class _T1, class _T2, bool IsSame>
1867struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1868
1869template <class _T1, class _T2, bool IsSame>
1870struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1871
1872template <class _T1, class _T2>
1873struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1874
1875template <class _T1, class _T2>
1876struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1877
1878template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1879class __libcpp_compressed_pair_imp;
1880
1881template <class _T1, class _T2>
1882class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1883{
1884private:
1885 _T1 __first_;
1886 _T2 __second_;
1887public:
1888 typedef _T1 _T1_param;
1889 typedef _T2 _T2_param;
1890
1891 typedef typename remove_reference<_T1>::type& _T1_reference;
1892 typedef typename remove_reference<_T2>::type& _T2_reference;
1893
1894 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1895 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1896
1897 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1898 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001899 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001901 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001903 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904
Howard Hinnant61aa6012011-07-01 19:24:36 +00001905#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1906
1907 _LIBCPP_INLINE_VISIBILITY
1908 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1909 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1910 is_nothrow_copy_constructible<_T2>::value)
1911 : __first_(__p.first()),
1912 __second_(__p.second()) {}
1913
1914 _LIBCPP_INLINE_VISIBILITY
1915 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1916 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1917 is_nothrow_copy_assignable<_T2>::value)
1918 {
1919 __first_ = __p.first();
1920 __second_ = __p.second();
1921 return *this;
1922 }
1923
Howard Hinnant73d21a42010-09-04 23:28:19 +00001924#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001925
1926 _LIBCPP_INLINE_VISIBILITY
1927 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001928 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1929 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00001930 : __first_(_VSTD::forward<_T1>(__p.first())),
1931 __second_(_VSTD::forward<_T2>(__p.second())) {}
1932
1933 _LIBCPP_INLINE_VISIBILITY
1934 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
1935 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
1936 is_nothrow_move_assignable<_T2>::value)
1937 {
1938 __first_ = _VSTD::forward<_T1>(__p.first());
1939 __second_ = _VSTD::forward<_T2>(__p.second());
1940 return *this;
1941 }
1942
Howard Hinnant73d21a42010-09-04 23:28:19 +00001943#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944
Howard Hinnant61aa6012011-07-01 19:24:36 +00001945#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1946
Howard Hinnant1694d232011-05-28 14:41:13 +00001947 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1948 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949
Howard Hinnant1694d232011-05-28 14:41:13 +00001950 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1951 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952
1953 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001954 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1955 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001957 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001958 swap(__first_, __x.__first_);
1959 swap(__second_, __x.__second_);
1960 }
1961};
1962
1963template <class _T1, class _T2>
1964class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1965 : private _T1
1966{
1967private:
1968 _T2 __second_;
1969public:
1970 typedef _T1 _T1_param;
1971 typedef _T2 _T2_param;
1972
1973 typedef _T1& _T1_reference;
1974 typedef typename remove_reference<_T2>::type& _T2_reference;
1975
1976 typedef const _T1& _T1_const_reference;
1977 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1978
1979 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1980 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001981 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001983 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001985 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986
Howard Hinnant61aa6012011-07-01 19:24:36 +00001987#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1988
1989 _LIBCPP_INLINE_VISIBILITY
1990 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1991 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1992 is_nothrow_copy_constructible<_T2>::value)
1993 : _T1(__p.first()), __second_(__p.second()) {}
1994
1995 _LIBCPP_INLINE_VISIBILITY
1996 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1997 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1998 is_nothrow_copy_assignable<_T2>::value)
1999 {
2000 _T1::operator=(__p.first());
2001 __second_ = __p.second();
2002 return *this;
2003 }
2004
Howard Hinnant73d21a42010-09-04 23:28:19 +00002005#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002006
2007 _LIBCPP_INLINE_VISIBILITY
2008 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002009 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2010 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002011 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002012
2013 _LIBCPP_INLINE_VISIBILITY
2014 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2015 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2016 is_nothrow_move_assignable<_T2>::value)
2017 {
2018 _T1::operator=(_VSTD::move(__p.first()));
2019 __second_ = _VSTD::forward<_T2>(__p.second());
2020 return *this;
2021 }
2022
Howard Hinnant73d21a42010-09-04 23:28:19 +00002023#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024
Howard Hinnant61aa6012011-07-01 19:24:36 +00002025#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2026
Howard Hinnant1694d232011-05-28 14:41:13 +00002027 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2028 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029
Howard Hinnant1694d232011-05-28 14:41:13 +00002030 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2031 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032
2033 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002034 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2035 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002037 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038 swap(__second_, __x.__second_);
2039 }
2040};
2041
2042template <class _T1, class _T2>
2043class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2044 : private _T2
2045{
2046private:
2047 _T1 __first_;
2048public:
2049 typedef _T1 _T1_param;
2050 typedef _T2 _T2_param;
2051
2052 typedef typename remove_reference<_T1>::type& _T1_reference;
2053 typedef _T2& _T2_reference;
2054
2055 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2056 typedef const _T2& _T2_const_reference;
2057
2058 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2059 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002060 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002062 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002064 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2065 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002066 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067
Howard Hinnant61aa6012011-07-01 19:24:36 +00002068#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2069
2070 _LIBCPP_INLINE_VISIBILITY
2071 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2072 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2073 is_nothrow_copy_constructible<_T2>::value)
2074 : _T2(__p.second()), __first_(__p.first()) {}
2075
2076 _LIBCPP_INLINE_VISIBILITY
2077 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2078 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2079 is_nothrow_copy_assignable<_T2>::value)
2080 {
2081 _T2::operator=(__p.second());
2082 __first_ = __p.first();
2083 return *this;
2084 }
2085
Howard Hinnant73d21a42010-09-04 23:28:19 +00002086#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002087
2088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002090 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2091 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002092 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002093
2094 _LIBCPP_INLINE_VISIBILITY
2095 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2096 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2097 is_nothrow_move_assignable<_T2>::value)
2098 {
2099 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2100 __first_ = _VSTD::move(__p.first());
2101 return *this;
2102 }
2103
Howard Hinnant73d21a42010-09-04 23:28:19 +00002104#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105
Howard Hinnant61aa6012011-07-01 19:24:36 +00002106#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2107
Howard Hinnant1694d232011-05-28 14:41:13 +00002108 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2109 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110
Howard Hinnant1694d232011-05-28 14:41:13 +00002111 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2112 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113
2114 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002115 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2116 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002118 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 swap(__first_, __x.__first_);
2120 }
2121};
2122
2123template <class _T1, class _T2>
2124class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2125 : private _T1,
2126 private _T2
2127{
2128public:
2129 typedef _T1 _T1_param;
2130 typedef _T2 _T2_param;
2131
2132 typedef _T1& _T1_reference;
2133 typedef _T2& _T2_reference;
2134
2135 typedef const _T1& _T1_const_reference;
2136 typedef const _T2& _T2_const_reference;
2137
2138 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2139 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002140 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002142 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002143 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002144 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002145
Howard Hinnant61aa6012011-07-01 19:24:36 +00002146#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2147
2148 _LIBCPP_INLINE_VISIBILITY
2149 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2150 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2151 is_nothrow_copy_constructible<_T2>::value)
2152 : _T1(__p.first()), _T2(__p.second()) {}
2153
2154 _LIBCPP_INLINE_VISIBILITY
2155 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2156 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2157 is_nothrow_copy_assignable<_T2>::value)
2158 {
2159 _T1::operator=(__p.first());
2160 _T2::operator=(__p.second());
2161 return *this;
2162 }
2163
Howard Hinnant73d21a42010-09-04 23:28:19 +00002164#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002165
2166 _LIBCPP_INLINE_VISIBILITY
2167 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002168 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2169 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002170 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002171
2172 _LIBCPP_INLINE_VISIBILITY
2173 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2174 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2175 is_nothrow_move_assignable<_T2>::value)
2176 {
2177 _T1::operator=(_VSTD::move(__p.first()));
2178 _T2::operator=(_VSTD::move(__p.second()));
2179 return *this;
2180 }
2181
Howard Hinnant73d21a42010-09-04 23:28:19 +00002182#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183
Howard Hinnant61aa6012011-07-01 19:24:36 +00002184#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2185
Howard Hinnant1694d232011-05-28 14:41:13 +00002186 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2187 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188
Howard Hinnant1694d232011-05-28 14:41:13 +00002189 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2190 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191
Howard Hinnantec3773c2011-12-01 20:21:04 +00002192 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002193 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2194 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002195 {
2196 }
2197};
2198
2199template <class _T1, class _T2>
2200class __compressed_pair
2201 : private __libcpp_compressed_pair_imp<_T1, _T2>
2202{
2203 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2204public:
2205 typedef typename base::_T1_param _T1_param;
2206 typedef typename base::_T2_param _T2_param;
2207
2208 typedef typename base::_T1_reference _T1_reference;
2209 typedef typename base::_T2_reference _T2_reference;
2210
2211 typedef typename base::_T1_const_reference _T1_const_reference;
2212 typedef typename base::_T2_const_reference _T2_const_reference;
2213
2214 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
2215 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002216 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002217 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002218 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002220 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221
Howard Hinnant61aa6012011-07-01 19:24:36 +00002222#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2223
2224 _LIBCPP_INLINE_VISIBILITY
2225 __compressed_pair(const __compressed_pair& __p)
2226 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2227 is_nothrow_copy_constructible<_T2>::value)
2228 : base(__p) {}
2229
2230 _LIBCPP_INLINE_VISIBILITY
2231 __compressed_pair& operator=(const __compressed_pair& __p)
2232 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2233 is_nothrow_copy_assignable<_T2>::value)
2234 {
2235 base::operator=(__p);
2236 return *this;
2237 }
2238
Howard Hinnant73d21a42010-09-04 23:28:19 +00002239#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002242 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2243 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002244 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002245
2246 _LIBCPP_INLINE_VISIBILITY
2247 __compressed_pair& operator=(__compressed_pair&& __p)
2248 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2249 is_nothrow_move_assignable<_T2>::value)
2250 {
2251 base::operator=(_VSTD::move(__p));
2252 return *this;
2253 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002254#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255
Howard Hinnant61aa6012011-07-01 19:24:36 +00002256#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2257
Howard Hinnant1694d232011-05-28 14:41:13 +00002258 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2259 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260
Howard Hinnant1694d232011-05-28 14:41:13 +00002261 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2262 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263
Howard Hinnant1694d232011-05-28 14:41:13 +00002264 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2265 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2266 __is_nothrow_swappable<_T1>::value)
2267 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268};
2269
2270template <class _T1, class _T2>
2271inline _LIBCPP_INLINE_VISIBILITY
2272void
2273swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002274 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2275 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 {__x.swap(__y);}
2277
2278template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002279struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280{
Howard Hinnant1694d232011-05-28 14:41:13 +00002281 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282 template <class _Up>
2283 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002284 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2285 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286 {
2287 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2288 delete __ptr;
2289 }
2290};
2291
2292template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002293struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294{
Howard Hinnant8e843502011-12-18 21:19:44 +00002295private:
2296 template <class _P1,
2297 bool = is_same<typename remove_cv<typename pointer_traits<_P1>::element_type>::type,
2298 typename remove_cv<_Tp>::type>::value>
2299 struct __same_or_less_cv_qualified_imp
2300 : is_convertible<_P1, _Tp*> {};
2301 template <class _P1>
2302 struct __same_or_less_cv_qualified_imp<_P1, false>
2303 : false_type {};
2304
2305 template <class _P1, bool = is_scalar<_P1>::value && !is_pointer<_P1>::value>
2306 struct __same_or_less_cv_qualified
2307 : __same_or_less_cv_qualified_imp<_P1> {};
2308
2309 template <class _P1>
2310 struct __same_or_less_cv_qualified<_P1, true>
2311 : false_type {};
2312
2313public:
2314 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
2315 template <class _Up>
2316 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
2317 typename enable_if<__same_or_less_cv_qualified<_Up*>::value>::type* = 0) _NOEXCEPT {}
2318 template <class _Up>
2319 _LIBCPP_INLINE_VISIBILITY
2320 void operator() (_Up* __ptr,
2321 typename enable_if<__same_or_less_cv_qualified<_Up*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322 {
2323 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2324 delete [] __ptr;
2325 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326};
2327
2328template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002329class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330{
2331public:
2332 typedef _Tp element_type;
2333 typedef _Dp deleter_type;
2334 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2335private:
2336 __compressed_pair<pointer, deleter_type> __ptr_;
2337
Howard Hinnant73d21a42010-09-04 23:28:19 +00002338#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002339 unique_ptr(const unique_ptr&);
2340 unique_ptr& operator=(const unique_ptr&);
2341 template <class _Up, class _Ep>
2342 unique_ptr(const unique_ptr<_Up, _Ep>&);
2343 template <class _Up, class _Ep>
2344 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002345#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346 unique_ptr(unique_ptr&);
2347 template <class _Up, class _Ep>
2348 unique_ptr(unique_ptr<_Up, _Ep>&);
2349 unique_ptr& operator=(unique_ptr&);
2350 template <class _Up, class _Ep>
2351 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002352#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002353
2354 struct __nat {int __for_bool_;};
2355
2356 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2357 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2358public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002359 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360 : __ptr_(pointer())
2361 {
2362 static_assert(!is_pointer<deleter_type>::value,
2363 "unique_ptr constructed with null function pointer deleter");
2364 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002365 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366 : __ptr_(pointer())
2367 {
2368 static_assert(!is_pointer<deleter_type>::value,
2369 "unique_ptr constructed with null function pointer deleter");
2370 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002371 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002372 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373 {
2374 static_assert(!is_pointer<deleter_type>::value,
2375 "unique_ptr constructed with null function pointer deleter");
2376 }
2377
Howard Hinnant73d21a42010-09-04 23:28:19 +00002378#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002379 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2380 is_reference<deleter_type>::value,
2381 deleter_type,
2382 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002383 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002384 : __ptr_(__p, __d) {}
2385
2386 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002387 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002388 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002389 {
2390 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2391 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002392 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002393 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394 template <class _Up, class _Ep>
2395 _LIBCPP_INLINE_VISIBILITY
2396 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2397 typename enable_if
2398 <
2399 !is_array<_Up>::value &&
2400 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2401 is_convertible<_Ep, deleter_type>::value &&
2402 (
2403 !is_reference<deleter_type>::value ||
2404 is_same<deleter_type, _Ep>::value
2405 ),
2406 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002407 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002408 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409
2410 template <class _Up>
2411 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2412 typename enable_if<
2413 is_convertible<_Up*, _Tp*>::value &&
2414 is_same<_Dp, default_delete<_Tp> >::value,
2415 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002416 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002417 : __ptr_(__p.release())
2418 {
2419 }
2420
Howard Hinnant1694d232011-05-28 14:41:13 +00002421 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422 {
2423 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002424 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 return *this;
2426 }
2427
2428 template <class _Up, class _Ep>
2429 _LIBCPP_INLINE_VISIBILITY
2430 typename enable_if
2431 <
2432 !is_array<_Up>::value,
2433 unique_ptr&
2434 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002435 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436 {
2437 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002438 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 return *this;
2440 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002441#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002442
2443 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2444 {
2445 return __rv<unique_ptr>(*this);
2446 }
2447
2448 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002449 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002450
2451 template <class _Up, class _Ep>
2452 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2453 {
2454 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002455 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456 return *this;
2457 }
2458
2459 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002460 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002461
2462 template <class _Up>
2463 _LIBCPP_INLINE_VISIBILITY
2464 typename enable_if<
2465 is_convertible<_Up*, _Tp*>::value &&
2466 is_same<_Dp, default_delete<_Tp> >::value,
2467 unique_ptr&
2468 >::type
2469 operator=(auto_ptr<_Up> __p)
2470 {reset(__p.release()); return *this;}
2471
Howard Hinnant73d21a42010-09-04 23:28:19 +00002472#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2474
Howard Hinnant1694d232011-05-28 14:41:13 +00002475 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002476 {
2477 reset();
2478 return *this;
2479 }
2480
2481 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2482 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002483 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2484 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2485 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2486 {return __ptr_.second();}
2487 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2488 {return __ptr_.second();}
2489 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2490 _NOEXCEPT
2491 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002492
Howard Hinnant1694d232011-05-28 14:41:13 +00002493 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494 {
2495 pointer __t = __ptr_.first();
2496 __ptr_.first() = pointer();
2497 return __t;
2498 }
2499
Howard Hinnant1694d232011-05-28 14:41:13 +00002500 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501 {
2502 pointer __tmp = __ptr_.first();
2503 __ptr_.first() = __p;
2504 if (__tmp)
2505 __ptr_.second()(__tmp);
2506 }
2507
Howard Hinnant1694d232011-05-28 14:41:13 +00002508 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2509 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510};
2511
2512template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002513class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514{
2515public:
2516 typedef _Tp element_type;
2517 typedef _Dp deleter_type;
2518 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2519private:
2520 __compressed_pair<pointer, deleter_type> __ptr_;
2521
Howard Hinnant73d21a42010-09-04 23:28:19 +00002522#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523 unique_ptr(const unique_ptr&);
2524 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant0a631192011-12-16 15:37:23 +00002525
Howard Hinnant8e843502011-12-18 21:19:44 +00002526 template <class _P1,
2527 bool = is_same<typename remove_cv<typename pointer_traits<_P1>::element_type>::type,
2528 typename remove_cv<element_type>::type>::value>
2529 struct __same_or_less_cv_qualified_imp
2530 : is_convertible<_P1, pointer> {};
2531 template <class _P1>
2532 struct __same_or_less_cv_qualified_imp<_P1, false>
2533 : false_type {};
2534
2535 template <class _P1, bool = is_scalar<_P1>::value && !is_pointer<_P1>::value>
2536 struct __same_or_less_cv_qualified
2537 : __same_or_less_cv_qualified_imp<_P1> {};
2538
2539 template <class _P1>
2540 struct __same_or_less_cv_qualified<_P1, true>
2541 : false_type {};
Howard Hinnant0a631192011-12-16 15:37:23 +00002542
Howard Hinnant73d21a42010-09-04 23:28:19 +00002543#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544 unique_ptr(unique_ptr&);
2545 template <class _Up>
2546 unique_ptr(unique_ptr<_Up>&);
2547 unique_ptr& operator=(unique_ptr&);
2548 template <class _Up>
2549 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002550#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551
2552 struct __nat {int __for_bool_;};
2553
2554 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2555 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2556public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002557 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558 : __ptr_(pointer())
2559 {
2560 static_assert(!is_pointer<deleter_type>::value,
2561 "unique_ptr constructed with null function pointer deleter");
2562 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002563 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 : __ptr_(pointer())
2565 {
2566 static_assert(!is_pointer<deleter_type>::value,
2567 "unique_ptr constructed with null function pointer deleter");
2568 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002569#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002570 template <class _Pp,
Howard Hinnant0a631192011-12-16 15:37:23 +00002571 class = typename enable_if<__same_or_less_cv_qualified<_Pp>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002572 >
Howard Hinnant99968442011-11-29 18:15:50 +00002573 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 : __ptr_(__p)
2575 {
2576 static_assert(!is_pointer<deleter_type>::value,
2577 "unique_ptr constructed with null function pointer deleter");
2578 }
2579
Howard Hinnant99968442011-11-29 18:15:50 +00002580 template <class _Pp,
Howard Hinnant0a631192011-12-16 15:37:23 +00002581 class = typename enable_if<__same_or_less_cv_qualified<_Pp>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002582 >
Howard Hinnant99968442011-11-29 18:15:50 +00002583 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584 is_reference<deleter_type>::value,
2585 deleter_type,
2586 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002587 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588 : __ptr_(__p, __d) {}
2589
2590 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2591 is_reference<deleter_type>::value,
2592 deleter_type,
2593 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002594 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002595 : __ptr_(pointer(), __d) {}
2596
Howard Hinnant99968442011-11-29 18:15:50 +00002597 template <class _Pp,
Howard Hinnant0a631192011-12-16 15:37:23 +00002598 class = typename enable_if<__same_or_less_cv_qualified<_Pp>::value ||
Howard Hinnant99968442011-11-29 18:15:50 +00002599 is_same<_Pp, nullptr_t>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600 >
Howard Hinnant99968442011-11-29 18:15:50 +00002601 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002602 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002603 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604 {
2605 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2606 }
2607
2608 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002609 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002610 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002611 {
2612 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2613 }
2614
Howard Hinnant1694d232011-05-28 14:41:13 +00002615 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002616 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617
Howard Hinnant1694d232011-05-28 14:41:13 +00002618 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619 {
2620 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002621 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 return *this;
2623 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002624
2625 template <class _Up, class _Ep>
2626 _LIBCPP_INLINE_VISIBILITY
2627 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2628 typename enable_if
2629 <
2630 is_array<_Up>::value &&
2631 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer>::value
2632 && is_convertible<_Ep, deleter_type>::value &&
2633 (
2634 !is_reference<deleter_type>::value ||
2635 is_same<deleter_type, _Ep>::value
2636 ),
2637 __nat
2638 >::type = __nat()
2639 ) _NOEXCEPT
2640 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2641
2642
2643 template <class _Up, class _Ep>
2644 _LIBCPP_INLINE_VISIBILITY
2645 typename enable_if
2646 <
2647 is_array<_Up>::value &&
2648 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer>::value &&
2649 is_assignable<deleter_type, _Ep>::value,
2650 unique_ptr&
2651 >::type
2652 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2653 {
2654 reset(__u.release());
2655 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2656 return *this;
2657 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002658#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659
2660 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2661 : __ptr_(__p)
2662 {
2663 static_assert(!is_pointer<deleter_type>::value,
2664 "unique_ptr constructed with null function pointer deleter");
2665 }
2666
2667 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002668 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669
2670 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002671 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002672
2673 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2674 {
2675 return __rv<unique_ptr>(*this);
2676 }
2677
2678 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002679 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680
2681 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2682 {
2683 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002684 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002685 return *this;
2686 }
2687
Howard Hinnant73d21a42010-09-04 23:28:19 +00002688#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002689 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2690
Howard Hinnant1694d232011-05-28 14:41:13 +00002691 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692 {
2693 reset();
2694 return *this;
2695 }
2696
2697 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2698 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002699 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2700 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2701 {return __ptr_.second();}
2702 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2703 {return __ptr_.second();}
2704 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2705 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706
Howard Hinnant1694d232011-05-28 14:41:13 +00002707 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002708 {
2709 pointer __t = __ptr_.first();
2710 __ptr_.first() = pointer();
2711 return __t;
2712 }
2713
Howard Hinnant73d21a42010-09-04 23:28:19 +00002714#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002715 template <class _Pp,
Howard Hinnant0a631192011-12-16 15:37:23 +00002716 class = typename enable_if<__same_or_less_cv_qualified<_Pp>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717 >
Howard Hinnant99968442011-11-29 18:15:50 +00002718 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719 {
2720 pointer __tmp = __ptr_.first();
2721 __ptr_.first() = __p;
2722 if (__tmp)
2723 __ptr_.second()(__tmp);
2724 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002725 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002726 {
2727 pointer __tmp = __ptr_.first();
2728 __ptr_.first() = nullptr;
2729 if (__tmp)
2730 __ptr_.second()(__tmp);
2731 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002732 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733 {
2734 pointer __tmp = __ptr_.first();
2735 __ptr_.first() = nullptr;
2736 if (__tmp)
2737 __ptr_.second()(__tmp);
2738 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002739#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2741 {
2742 pointer __tmp = __ptr_.first();
2743 __ptr_.first() = __p;
2744 if (__tmp)
2745 __ptr_.second()(__tmp);
2746 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002747#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748
2749 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2750private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002751
Howard Hinnant73d21a42010-09-04 23:28:19 +00002752#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 template <class _Up>
2754 explicit unique_ptr(_Up);
2755 template <class _Up>
2756 unique_ptr(_Up __u,
2757 typename conditional<
2758 is_reference<deleter_type>::value,
2759 deleter_type,
2760 typename add_lvalue_reference<const deleter_type>::type>::type,
2761 typename enable_if
2762 <
2763 is_convertible<_Up, pointer>::value,
2764 __nat
2765 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002766#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767};
2768
2769template <class _Tp, class _Dp>
2770inline _LIBCPP_INLINE_VISIBILITY
2771void
Howard Hinnant1694d232011-05-28 14:41:13 +00002772swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773
2774template <class _T1, class _D1, class _T2, class _D2>
2775inline _LIBCPP_INLINE_VISIBILITY
2776bool
2777operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2778
2779template <class _T1, class _D1, class _T2, class _D2>
2780inline _LIBCPP_INLINE_VISIBILITY
2781bool
2782operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2783
2784template <class _T1, class _D1, class _T2, class _D2>
2785inline _LIBCPP_INLINE_VISIBILITY
2786bool
2787operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2788
2789template <class _T1, class _D1, class _T2, class _D2>
2790inline _LIBCPP_INLINE_VISIBILITY
2791bool
2792operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2793
2794template <class _T1, class _D1, class _T2, class _D2>
2795inline _LIBCPP_INLINE_VISIBILITY
2796bool
2797operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2798
2799template <class _T1, class _D1, class _T2, class _D2>
2800inline _LIBCPP_INLINE_VISIBILITY
2801bool
2802operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2803
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00002804template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00002805
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002806// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
2807// is 64 bits. This is because cityhash64 uses 64bit x 64bit
2808// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00002809template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002810struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00002811
2812template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002813struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00002814{
2815 _Size operator()(const void* __key, _Size __len);
2816};
2817
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002818// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00002819template <class _Size>
2820_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002821__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00002822{
2823 const _Size __m = 0x5bd1e995;
2824 const _Size __r = 24;
2825 _Size __h = __len;
2826 const unsigned char* __data = static_cast<const unsigned char*>(__key);
2827 for (; __len >= 4; __data += 4, __len -= 4)
2828 {
2829 _Size __k = *(const _Size*)__data;
2830 __k *= __m;
2831 __k ^= __k >> __r;
2832 __k *= __m;
2833 __h *= __m;
2834 __h ^= __k;
2835 }
2836 switch (__len)
2837 {
2838 case 3:
2839 __h ^= __data[2] << 16;
2840 case 2:
2841 __h ^= __data[1] << 8;
2842 case 1:
2843 __h ^= __data[0];
2844 __h *= __m;
2845 }
2846 __h ^= __h >> 13;
2847 __h *= __m;
2848 __h ^= __h >> 15;
2849 return __h;
2850}
2851
2852template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002853struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00002854{
2855 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002856
2857 private:
2858 // Some primes between 2^63 and 2^64.
2859 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
2860 static const _Size __k1 = 0xb492b66fbe98f273ULL;
2861 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
2862 static const _Size __k3 = 0xc949d7c7509e6557ULL;
2863
2864 static _Size __rotate(_Size __val, int __shift) {
2865 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
2866 }
2867
2868 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
2869 return (__val >> __shift) | (__val << (64 - __shift));
2870 }
2871
2872 static _Size __shift_mix(_Size __val) {
2873 return __val ^ (__val >> 47);
2874 }
2875
2876 static _Size __hash_len_16(_Size __u, _Size __v) {
2877 const _Size __mul = 0x9ddfea08eb382d69ULL;
2878 _Size __a = (__u ^ __v) * __mul;
2879 __a ^= (__a >> 47);
2880 _Size __b = (__v ^ __a) * __mul;
2881 __b ^= (__b >> 47);
2882 __b *= __mul;
2883 return __b;
2884 }
2885
2886 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
2887 if (__len > 8) {
2888 const _Size __a = *(const _Size*)__s;
2889 const _Size __b = *(const _Size*)(__s + __len - 8);
2890 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
2891 }
2892 if (__len >= 4) {
2893 const uint32_t __a = *(const uint32_t*)(__s);
2894 const uint32_t __b = *(const uint32_t*)(__s + __len - 4);
2895 return __hash_len_16(__len + (__a << 3), __b);
2896 }
2897 if (__len > 0) {
2898 const unsigned char __a = __s[0];
2899 const unsigned char __b = __s[__len >> 1];
2900 const unsigned char __c = __s[__len - 1];
2901 const uint32_t __y = static_cast<uint32_t>(__a) +
2902 (static_cast<uint32_t>(__b) << 8);
2903 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
2904 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
2905 }
2906 return __k2;
2907 }
2908
2909 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
2910 const _Size __a = *(const _Size*)(__s) * __k1;
2911 const _Size __b = *(const _Size*)(__s + 8);
2912 const _Size __c = *(const _Size*)(__s + __len - 8) * __k2;
2913 const _Size __d = *(const _Size*)(__s + __len - 16) * __k0;
2914 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
2915 __a + __rotate(__b ^ __k3, 20) - __c + __len);
2916 }
2917
2918 // Return a 16-byte hash for 48 bytes. Quick and dirty.
2919 // Callers do best to use "random-looking" values for a and b.
2920 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
2921 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
2922 __a += __w;
2923 __b = __rotate(__b + __a + __z, 21);
2924 const _Size __c = __a;
2925 __a += __x;
2926 __a += __y;
2927 __b += __rotate(__a, 44);
2928 return pair<_Size, _Size>(__a + __z, __b + __c);
2929 }
2930
2931 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
2932 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
2933 const char* __s, _Size __a, _Size __b) {
2934 return __weak_hash_len_32_with_seeds(*(const _Size*)(__s),
2935 *(const _Size*)(__s + 8),
2936 *(const _Size*)(__s + 16),
2937 *(const _Size*)(__s + 24),
2938 __a,
2939 __b);
2940 }
2941
2942 // Return an 8-byte hash for 33 to 64 bytes.
2943 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
2944 _Size __z = *(const _Size*)(__s + 24);
2945 _Size __a = *(const _Size*)(__s) +
2946 (__len + *(const _Size*)(__s + __len - 16)) * __k0;
2947 _Size __b = __rotate(__a + __z, 52);
2948 _Size __c = __rotate(__a, 37);
2949 __a += *(const _Size*)(__s + 8);
2950 __c += __rotate(__a, 7);
2951 __a += *(const _Size*)(__s + 16);
2952 _Size __vf = __a + __z;
2953 _Size __vs = __b + __rotate(__a, 31) + __c;
2954 __a = *(const _Size*)(__s + 16) + *(const _Size*)(__s + __len - 32);
2955 __z += *(const _Size*)(__s + __len - 8);
2956 __b = __rotate(__a + __z, 52);
2957 __c = __rotate(__a, 37);
2958 __a += *(const _Size*)(__s + __len - 24);
2959 __c += __rotate(__a, 7);
2960 __a += *(const _Size*)(__s + __len - 16);
2961 _Size __wf = __a + __z;
2962 _Size __ws = __b + __rotate(__a, 31) + __c;
2963 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
2964 return __shift_mix(__r * __k0 + __vs) * __k2;
2965 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00002966};
2967
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002968// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00002969template <class _Size>
2970_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002971__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00002972{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002973 const char* __s = static_cast<const char*>(__key);
2974 if (__len <= 32) {
2975 if (__len <= 16) {
2976 return __hash_len_0_to_16(__s, __len);
2977 } else {
2978 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00002979 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002980 } else if (__len <= 64) {
2981 return __hash_len_33_to_64(__s, __len);
2982 }
2983
2984 // For strings over 64 bytes we hash the end first, and then as we
2985 // loop we keep 56 bytes of state: v, w, x, y, and z.
2986 _Size __x = *(const _Size*)(__s + __len - 40);
2987 _Size __y = *(const _Size*)(__s + __len - 16) +
2988 *(const _Size*)(__s + __len - 56);
2989 _Size __z = __hash_len_16(*(const _Size*)(__s + __len - 48) + __len,
2990 *(const _Size*)(__s + __len - 24));
2991 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
2992 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
2993 __x = __x * __k1 + *(const _Size*)(__s);
2994
2995 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
2996 __len = (__len - 1) & ~static_cast<_Size>(63);
2997 do {
2998 __x = __rotate(__x + __y + __v.first + *(const _Size*)(__s + 8), 37) * __k1;
2999 __y = __rotate(__y + __v.second + *(const _Size*)(__s + 48), 42) * __k1;
3000 __x ^= __w.second;
3001 __y += __v.first + *(const _Size*)(__s + 40);
3002 __z = __rotate(__z + __w.first, 33) * __k1;
3003 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3004 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
3005 __y + *(const _Size*)(__s + 16));
3006 std::swap(__z, __x);
3007 __s += 64;
3008 __len -= 64;
3009 } while (__len != 0);
3010 return __hash_len_16(
3011 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3012 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003013}
3014
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003015template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3016struct __scalar_hash;
3017
3018template <class _Tp>
3019struct __scalar_hash<_Tp, 0>
3020 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003021{
Howard Hinnant82894812010-09-22 16:48:34 +00003022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003023 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003024 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003025 union
3026 {
3027 _Tp __t;
3028 size_t __a;
3029 } __u;
3030 __u.__a = 0;
3031 __u.__t = __v;
3032 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003033 }
3034};
3035
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003036template <class _Tp>
3037struct __scalar_hash<_Tp, 1>
3038 : public unary_function<_Tp, size_t>
3039{
3040 _LIBCPP_INLINE_VISIBILITY
3041 size_t operator()(_Tp __v) const _NOEXCEPT
3042 {
3043 union
3044 {
3045 _Tp __t;
3046 size_t __a;
3047 } __u;
3048 __u.__t = __v;
3049 return __u.__a;
3050 }
3051};
3052
3053template <class _Tp>
3054struct __scalar_hash<_Tp, 2>
3055 : public unary_function<_Tp, size_t>
3056{
3057 _LIBCPP_INLINE_VISIBILITY
3058 size_t operator()(_Tp __v) const _NOEXCEPT
3059 {
3060 union
3061 {
3062 _Tp __t;
3063 struct
3064 {
3065 size_t __a;
3066 size_t __b;
3067 };
3068 } __u;
3069 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003070 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003071 }
3072};
3073
3074template <class _Tp>
3075struct __scalar_hash<_Tp, 3>
3076 : public unary_function<_Tp, size_t>
3077{
3078 _LIBCPP_INLINE_VISIBILITY
3079 size_t operator()(_Tp __v) const _NOEXCEPT
3080 {
3081 union
3082 {
3083 _Tp __t;
3084 struct
3085 {
3086 size_t __a;
3087 size_t __b;
3088 size_t __c;
3089 };
3090 } __u;
3091 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003092 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003093 }
3094};
3095
3096template <class _Tp>
3097struct __scalar_hash<_Tp, 4>
3098 : public unary_function<_Tp, size_t>
3099{
3100 _LIBCPP_INLINE_VISIBILITY
3101 size_t operator()(_Tp __v) const _NOEXCEPT
3102 {
3103 union
3104 {
3105 _Tp __t;
3106 struct
3107 {
3108 size_t __a;
3109 size_t __b;
3110 size_t __c;
3111 size_t __d;
3112 };
3113 } __u;
3114 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003115 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003116 }
3117};
3118
3119template<class _Tp>
3120struct _LIBCPP_VISIBLE hash<_Tp*>
3121 : public __scalar_hash<_Tp*>
3122{
3123};
3124
Howard Hinnant21aefc32010-06-03 16:42:57 +00003125template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003126struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003127{
3128 typedef unique_ptr<_Tp, _Dp> argument_type;
3129 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003131 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003132 {
3133 typedef typename argument_type::pointer pointer;
3134 return hash<pointer>()(__ptr.get());
3135 }
3136};
3137
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003138struct __destruct_n
3139{
3140private:
3141 size_t size;
3142
3143 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003144 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003145 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3146
3147 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003148 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149 {}
3150
Howard Hinnant1694d232011-05-28 14:41:13 +00003151 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003152 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003153 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003154 {}
3155
Howard Hinnant1694d232011-05-28 14:41:13 +00003156 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003157 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003158 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003159 {}
3160public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003161 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3162 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003163
3164 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003165 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003166 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003167
3168 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003169 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003170 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003171
3172 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003173 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003174 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003175};
3176
3177template <class _Alloc>
3178class __allocator_destructor
3179{
3180 typedef allocator_traits<_Alloc> __alloc_traits;
3181public:
3182 typedef typename __alloc_traits::pointer pointer;
3183 typedef typename __alloc_traits::size_type size_type;
3184private:
3185 _Alloc& __alloc_;
3186 size_type __s_;
3187public:
3188 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003189 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003190 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003192 void operator()(pointer __p) _NOEXCEPT
3193 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194};
3195
3196template <class _InputIterator, class _ForwardIterator>
3197_ForwardIterator
3198uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3199{
3200 __destruct_n __d(0);
3201 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3202 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
3203 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
3204 ::new(&*__r) value_type(*__f);
3205 __h.release();
3206 return __r;
3207}
3208
3209template <class _InputIterator, class _Size, class _ForwardIterator>
3210_ForwardIterator
3211uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3212{
3213 __destruct_n __d(0);
3214 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3215 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
3216 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
3217 ::new(&*__r) value_type(*__f);
3218 __h.release();
3219 return __r;
3220}
3221
3222template <class _ForwardIterator, class _Tp>
3223void
3224uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3225{
3226 __destruct_n __d(0);
3227 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3228 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
3229 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
3230 ::new(&*__f) value_type(__x);
3231 __h.release();
3232}
3233
3234template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003235_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003236uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3237{
3238 __destruct_n __d(0);
3239 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3240 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
3241 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
3242 ::new(&*__f) value_type(__x);
3243 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003244 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003245}
3246
Howard Hinnant82894812010-09-22 16:48:34 +00003247class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003248 : public std::exception
3249{
3250public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003251 virtual ~bad_weak_ptr() _NOEXCEPT;
3252 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003253};
3254
3255template<class _Tp> class weak_ptr;
3256
3257class __shared_count
3258{
3259 __shared_count(const __shared_count&);
3260 __shared_count& operator=(const __shared_count&);
3261
3262protected:
3263 long __shared_owners_;
3264 virtual ~__shared_count();
3265private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003266 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003267
3268public:
Howard Hinnant82894812010-09-22 16:48:34 +00003269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003270 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271 : __shared_owners_(__refs) {}
3272
Howard Hinnant1694d232011-05-28 14:41:13 +00003273 void __add_shared() _NOEXCEPT;
3274 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003276 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003277};
3278
3279class __shared_weak_count
3280 : private __shared_count
3281{
3282 long __shared_weak_owners_;
3283
3284public:
Howard Hinnant82894812010-09-22 16:48:34 +00003285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003286 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003287 : __shared_count(__refs),
3288 __shared_weak_owners_(__refs) {}
3289protected:
3290 virtual ~__shared_weak_count();
3291
3292public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003293 void __add_shared() _NOEXCEPT;
3294 void __add_weak() _NOEXCEPT;
3295 void __release_shared() _NOEXCEPT;
3296 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003298 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3299 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300
Howard Hinnant1694d232011-05-28 14:41:13 +00003301 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003302private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003303 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003304};
3305
3306template <class _Tp, class _Dp, class _Alloc>
3307class __shared_ptr_pointer
3308 : public __shared_weak_count
3309{
3310 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3311public:
Howard Hinnant82894812010-09-22 16:48:34 +00003312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003313 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003314 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003315
Howard Hinnantd4444702010-08-11 17:04:31 +00003316#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003317 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003318#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003319
3320private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003321 virtual void __on_zero_shared() _NOEXCEPT;
3322 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003323};
3324
Howard Hinnantd4444702010-08-11 17:04:31 +00003325#ifndef _LIBCPP_NO_RTTI
3326
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003327template <class _Tp, class _Dp, class _Alloc>
3328const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003329__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003330{
3331 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3332}
3333
Howard Hinnant324bb032010-08-22 00:02:43 +00003334#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003335
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003336template <class _Tp, class _Dp, class _Alloc>
3337void
Howard Hinnant1694d232011-05-28 14:41:13 +00003338__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003339{
3340 __data_.first().second()(__data_.first().first());
3341 __data_.first().second().~_Dp();
3342}
3343
3344template <class _Tp, class _Dp, class _Alloc>
3345void
Howard Hinnant1694d232011-05-28 14:41:13 +00003346__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003347{
3348 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3349 __data_.second().~_Alloc();
3350 __a.deallocate(this, 1);
3351}
3352
3353template <class _Tp, class _Alloc>
3354class __shared_ptr_emplace
3355 : public __shared_weak_count
3356{
3357 __compressed_pair<_Alloc, _Tp> __data_;
3358public:
3359#ifndef _LIBCPP_HAS_NO_VARIADICS
3360
Howard Hinnant82894812010-09-22 16:48:34 +00003361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003362 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003363 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003364
3365 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003367 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003368 : __data_(_VSTD::move(__a), _Tp(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003369
3370#else // _LIBCPP_HAS_NO_VARIADICS
3371
Howard Hinnant82894812010-09-22 16:48:34 +00003372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003373 __shared_ptr_emplace(_Alloc __a)
3374 : __data_(__a) {}
3375
3376 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003378 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3379 : __data_(__a, _Tp(__a0)) {}
3380
3381 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003383 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3384 : __data_(__a, _Tp(__a0, __a1)) {}
3385
3386 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003388 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3389 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3390
3391#endif // _LIBCPP_HAS_NO_VARIADICS
3392
3393private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003394 virtual void __on_zero_shared() _NOEXCEPT;
3395 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396public:
Howard Hinnant82894812010-09-22 16:48:34 +00003397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003398 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003399};
3400
3401template <class _Tp, class _Alloc>
3402void
Howard Hinnant1694d232011-05-28 14:41:13 +00003403__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003404{
3405 __data_.second().~_Tp();
3406}
3407
3408template <class _Tp, class _Alloc>
3409void
Howard Hinnant1694d232011-05-28 14:41:13 +00003410__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003411{
3412 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3413 __data_.first().~_Alloc();
3414 __a.deallocate(this, 1);
3415}
3416
3417template<class _Tp> class enable_shared_from_this;
3418
3419template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003420class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003421{
Howard Hinnant324bb032010-08-22 00:02:43 +00003422public:
3423 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424private:
3425 element_type* __ptr_;
3426 __shared_weak_count* __cntrl_;
3427
3428 struct __nat {int __for_bool_;};
3429public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003430 shared_ptr() _NOEXCEPT;
3431 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003432 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00003433 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
3434 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003435 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3436 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003437 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3438 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003439 template<class _Yp>
3440 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003441 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3442 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003443#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003444 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003445 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003446 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3447 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003448#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003449 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003450 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003451#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003452 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
3453#else
3454 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003455#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003456#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003457private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003458 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003459public:
3460 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3461 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3462 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3463 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003464#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003465 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3466 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3467 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3468 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003469#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003470
3471 ~shared_ptr();
3472
Howard Hinnant1694d232011-05-28 14:41:13 +00003473 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
3474 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003475#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003476 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003477 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
3478 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003479#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003480 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003481#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003482#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003483private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003484 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003485public:
Howard Hinnant324bb032010-08-22 00:02:43 +00003486 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003487#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003488 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003489#endif
3490
Howard Hinnant1694d232011-05-28 14:41:13 +00003491 void swap(shared_ptr& __r) _NOEXCEPT;
3492 void reset() _NOEXCEPT;
Howard Hinnantec3773c2011-12-01 20:21:04 +00003493 template<class _Yp> void reset(_Yp* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003494 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
3495 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
3496
Howard Hinnant82894812010-09-22 16:48:34 +00003497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003498 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003500 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3501 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003503 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003505 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003507 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003509 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003510 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003512 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003514 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003516 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003517 {return __cntrl_ < __p.__cntrl_;}
3518
Howard Hinnantd4444702010-08-11 17:04:31 +00003519#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003520 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003522 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003523 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003524#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525
3526#ifndef _LIBCPP_HAS_NO_VARIADICS
3527
3528 template<class ..._Args>
3529 static
3530 shared_ptr<_Tp>
3531 make_shared(_Args&& ...__args);
3532
3533 template<class _Alloc, class ..._Args>
3534 static
3535 shared_ptr<_Tp>
3536 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3537
3538#else // _LIBCPP_HAS_NO_VARIADICS
3539
3540 static shared_ptr<_Tp> make_shared();
3541
3542 template<class _A0>
3543 static shared_ptr<_Tp> make_shared(_A0&);
3544
3545 template<class _A0, class _A1>
3546 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3547
3548 template<class _A0, class _A1, class _A2>
3549 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3550
3551 template<class _Alloc>
3552 static shared_ptr<_Tp>
3553 allocate_shared(const _Alloc& __a);
3554
3555 template<class _Alloc, class _A0>
3556 static shared_ptr<_Tp>
3557 allocate_shared(const _Alloc& __a, _A0& __a0);
3558
3559 template<class _Alloc, class _A0, class _A1>
3560 static shared_ptr<_Tp>
3561 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3562
3563 template<class _Alloc, class _A0, class _A1, class _A2>
3564 static shared_ptr<_Tp>
3565 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3566
3567#endif // _LIBCPP_HAS_NO_VARIADICS
3568
3569private:
3570
3571 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003573 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003574 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003575 {
3576 if (__e)
3577 __e->__weak_this_ = *this;
3578 }
3579
Howard Hinnant82894812010-09-22 16:48:34 +00003580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003581 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582
Howard Hinnant82894812010-09-22 16:48:34 +00003583 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3584 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003585};
3586
3587template<class _Tp>
3588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003589shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003590 : __ptr_(0),
3591 __cntrl_(0)
3592{
3593}
3594
3595template<class _Tp>
3596inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003597shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003598 : __ptr_(0),
3599 __cntrl_(0)
3600{
3601}
3602
3603template<class _Tp>
3604template<class _Yp>
3605shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3606 : __ptr_(__p)
3607{
3608 unique_ptr<_Yp> __hold(__p);
3609 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3610 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3611 __hold.release();
3612 __enable_weak_this(__p);
3613}
3614
3615template<class _Tp>
3616template<class _Yp, class _Dp>
3617shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3618 : __ptr_(__p)
3619{
3620#ifndef _LIBCPP_NO_EXCEPTIONS
3621 try
3622 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003623#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3625 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3626 __enable_weak_this(__p);
3627#ifndef _LIBCPP_NO_EXCEPTIONS
3628 }
3629 catch (...)
3630 {
3631 __d(__p);
3632 throw;
3633 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003634#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003635}
3636
3637template<class _Tp>
3638template<class _Dp>
3639shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3640 : __ptr_(0)
3641{
3642#ifndef _LIBCPP_NO_EXCEPTIONS
3643 try
3644 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003645#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3647 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3648#ifndef _LIBCPP_NO_EXCEPTIONS
3649 }
3650 catch (...)
3651 {
3652 __d(__p);
3653 throw;
3654 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003655#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656}
3657
3658template<class _Tp>
3659template<class _Yp, class _Dp, class _Alloc>
3660shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3661 : __ptr_(__p)
3662{
3663#ifndef _LIBCPP_NO_EXCEPTIONS
3664 try
3665 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003666#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003667 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3668 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3669 typedef __allocator_destructor<_A2> _D2;
3670 _A2 __a2(__a);
3671 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3672 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3673 __cntrl_ = __hold2.release();
3674 __enable_weak_this(__p);
3675#ifndef _LIBCPP_NO_EXCEPTIONS
3676 }
3677 catch (...)
3678 {
3679 __d(__p);
3680 throw;
3681 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003682#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003683}
3684
3685template<class _Tp>
3686template<class _Dp, class _Alloc>
3687shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3688 : __ptr_(0)
3689{
3690#ifndef _LIBCPP_NO_EXCEPTIONS
3691 try
3692 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003693#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3695 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3696 typedef __allocator_destructor<_A2> _D2;
3697 _A2 __a2(__a);
3698 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3699 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3700 __cntrl_ = __hold2.release();
3701#ifndef _LIBCPP_NO_EXCEPTIONS
3702 }
3703 catch (...)
3704 {
3705 __d(__p);
3706 throw;
3707 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003708#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709}
3710
3711template<class _Tp>
3712template<class _Yp>
3713inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003714shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003715 : __ptr_(__p),
3716 __cntrl_(__r.__cntrl_)
3717{
3718 if (__cntrl_)
3719 __cntrl_->__add_shared();
3720}
3721
3722template<class _Tp>
3723inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003724shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003725 : __ptr_(__r.__ptr_),
3726 __cntrl_(__r.__cntrl_)
3727{
3728 if (__cntrl_)
3729 __cntrl_->__add_shared();
3730}
3731
3732template<class _Tp>
3733template<class _Yp>
3734inline _LIBCPP_INLINE_VISIBILITY
3735shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3736 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003737 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003738 : __ptr_(__r.__ptr_),
3739 __cntrl_(__r.__cntrl_)
3740{
3741 if (__cntrl_)
3742 __cntrl_->__add_shared();
3743}
3744
Howard Hinnant73d21a42010-09-04 23:28:19 +00003745#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746
3747template<class _Tp>
3748inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003749shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750 : __ptr_(__r.__ptr_),
3751 __cntrl_(__r.__cntrl_)
3752{
3753 __r.__ptr_ = 0;
3754 __r.__cntrl_ = 0;
3755}
3756
3757template<class _Tp>
3758template<class _Yp>
3759inline _LIBCPP_INLINE_VISIBILITY
3760shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3761 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003762 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763 : __ptr_(__r.__ptr_),
3764 __cntrl_(__r.__cntrl_)
3765{
3766 __r.__ptr_ = 0;
3767 __r.__cntrl_ = 0;
3768}
3769
Howard Hinnant73d21a42010-09-04 23:28:19 +00003770#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003771
3772template<class _Tp>
3773template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003774#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3776#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003777shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003778#endif
3779 : __ptr_(__r.get())
3780{
3781 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3782 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3783 __enable_weak_this(__r.get());
3784 __r.release();
3785}
3786
3787template<class _Tp>
3788template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003789#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3791#else
3792shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3793#endif
3794 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3795 : __ptr_(__r.get())
3796{
3797 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3798 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3799 __enable_weak_this(__r.get());
3800 __r.release();
3801}
3802
3803template<class _Tp>
3804template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003805#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003806shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3807#else
3808shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3809#endif
3810 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3811 : __ptr_(__r.get())
3812{
3813 typedef __shared_ptr_pointer<_Yp*,
3814 reference_wrapper<typename remove_reference<_Dp>::type>,
3815 allocator<_Yp> > _CntrlBlk;
3816 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3817 __enable_weak_this(__r.get());
3818 __r.release();
3819}
3820
3821#ifndef _LIBCPP_HAS_NO_VARIADICS
3822
3823template<class _Tp>
3824template<class ..._Args>
3825shared_ptr<_Tp>
3826shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3827{
3828 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3829 typedef allocator<_CntrlBlk> _A2;
3830 typedef __allocator_destructor<_A2> _D2;
3831 _A2 __a2;
3832 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003833 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003834 shared_ptr<_Tp> __r;
3835 __r.__ptr_ = __hold2.get()->get();
3836 __r.__cntrl_ = __hold2.release();
3837 __r.__enable_weak_this(__r.__ptr_);
3838 return __r;
3839}
3840
3841template<class _Tp>
3842template<class _Alloc, class ..._Args>
3843shared_ptr<_Tp>
3844shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3845{
3846 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3847 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3848 typedef __allocator_destructor<_A2> _D2;
3849 _A2 __a2(__a);
3850 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003851 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003852 shared_ptr<_Tp> __r;
3853 __r.__ptr_ = __hold2.get()->get();
3854 __r.__cntrl_ = __hold2.release();
3855 __r.__enable_weak_this(__r.__ptr_);
3856 return __r;
3857}
3858
3859#else // _LIBCPP_HAS_NO_VARIADICS
3860
3861template<class _Tp>
3862shared_ptr<_Tp>
3863shared_ptr<_Tp>::make_shared()
3864{
3865 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3866 typedef allocator<_CntrlBlk> _Alloc2;
3867 typedef __allocator_destructor<_Alloc2> _D2;
3868 _Alloc2 __alloc2;
3869 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3870 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3871 shared_ptr<_Tp> __r;
3872 __r.__ptr_ = __hold2.get()->get();
3873 __r.__cntrl_ = __hold2.release();
3874 __r.__enable_weak_this(__r.__ptr_);
3875 return __r;
3876}
3877
3878template<class _Tp>
3879template<class _A0>
3880shared_ptr<_Tp>
3881shared_ptr<_Tp>::make_shared(_A0& __a0)
3882{
3883 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3884 typedef allocator<_CntrlBlk> _Alloc2;
3885 typedef __allocator_destructor<_Alloc2> _D2;
3886 _Alloc2 __alloc2;
3887 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3888 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3889 shared_ptr<_Tp> __r;
3890 __r.__ptr_ = __hold2.get()->get();
3891 __r.__cntrl_ = __hold2.release();
3892 __r.__enable_weak_this(__r.__ptr_);
3893 return __r;
3894}
3895
3896template<class _Tp>
3897template<class _A0, class _A1>
3898shared_ptr<_Tp>
3899shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3900{
3901 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3902 typedef allocator<_CntrlBlk> _Alloc2;
3903 typedef __allocator_destructor<_Alloc2> _D2;
3904 _Alloc2 __alloc2;
3905 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3906 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3907 shared_ptr<_Tp> __r;
3908 __r.__ptr_ = __hold2.get()->get();
3909 __r.__cntrl_ = __hold2.release();
3910 __r.__enable_weak_this(__r.__ptr_);
3911 return __r;
3912}
3913
3914template<class _Tp>
3915template<class _A0, class _A1, class _A2>
3916shared_ptr<_Tp>
3917shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3918{
3919 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3920 typedef allocator<_CntrlBlk> _Alloc2;
3921 typedef __allocator_destructor<_Alloc2> _D2;
3922 _Alloc2 __alloc2;
3923 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3924 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3925 shared_ptr<_Tp> __r;
3926 __r.__ptr_ = __hold2.get()->get();
3927 __r.__cntrl_ = __hold2.release();
3928 __r.__enable_weak_this(__r.__ptr_);
3929 return __r;
3930}
3931
3932template<class _Tp>
3933template<class _Alloc>
3934shared_ptr<_Tp>
3935shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3936{
3937 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3938 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3939 typedef __allocator_destructor<_Alloc2> _D2;
3940 _Alloc2 __alloc2(__a);
3941 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3942 ::new(__hold2.get()) _CntrlBlk(__a);
3943 shared_ptr<_Tp> __r;
3944 __r.__ptr_ = __hold2.get()->get();
3945 __r.__cntrl_ = __hold2.release();
3946 __r.__enable_weak_this(__r.__ptr_);
3947 return __r;
3948}
3949
3950template<class _Tp>
3951template<class _Alloc, class _A0>
3952shared_ptr<_Tp>
3953shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3954{
3955 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3956 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3957 typedef __allocator_destructor<_Alloc2> _D2;
3958 _Alloc2 __alloc2(__a);
3959 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3960 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3961 shared_ptr<_Tp> __r;
3962 __r.__ptr_ = __hold2.get()->get();
3963 __r.__cntrl_ = __hold2.release();
3964 __r.__enable_weak_this(__r.__ptr_);
3965 return __r;
3966}
3967
3968template<class _Tp>
3969template<class _Alloc, class _A0, class _A1>
3970shared_ptr<_Tp>
3971shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3972{
3973 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3974 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3975 typedef __allocator_destructor<_Alloc2> _D2;
3976 _Alloc2 __alloc2(__a);
3977 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3978 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3979 shared_ptr<_Tp> __r;
3980 __r.__ptr_ = __hold2.get()->get();
3981 __r.__cntrl_ = __hold2.release();
3982 __r.__enable_weak_this(__r.__ptr_);
3983 return __r;
3984}
3985
3986template<class _Tp>
3987template<class _Alloc, class _A0, class _A1, class _A2>
3988shared_ptr<_Tp>
3989shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3990{
3991 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3992 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3993 typedef __allocator_destructor<_Alloc2> _D2;
3994 _Alloc2 __alloc2(__a);
3995 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3996 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3997 shared_ptr<_Tp> __r;
3998 __r.__ptr_ = __hold2.get()->get();
3999 __r.__cntrl_ = __hold2.release();
4000 __r.__enable_weak_this(__r.__ptr_);
4001 return __r;
4002}
4003
4004#endif // _LIBCPP_HAS_NO_VARIADICS
4005
4006template<class _Tp>
4007shared_ptr<_Tp>::~shared_ptr()
4008{
4009 if (__cntrl_)
4010 __cntrl_->__release_shared();
4011}
4012
4013template<class _Tp>
4014inline _LIBCPP_INLINE_VISIBILITY
4015shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004016shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004017{
4018 shared_ptr(__r).swap(*this);
4019 return *this;
4020}
4021
4022template<class _Tp>
4023template<class _Yp>
4024inline _LIBCPP_INLINE_VISIBILITY
4025shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004026shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004027{
4028 shared_ptr(__r).swap(*this);
4029 return *this;
4030}
4031
Howard Hinnant73d21a42010-09-04 23:28:19 +00004032#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004033
4034template<class _Tp>
4035inline _LIBCPP_INLINE_VISIBILITY
4036shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004037shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004038{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004039 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004040 return *this;
4041}
4042
4043template<class _Tp>
4044template<class _Yp>
4045inline _LIBCPP_INLINE_VISIBILITY
4046shared_ptr<_Tp>&
4047shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4048{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004049 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004050 return *this;
4051}
4052
4053template<class _Tp>
4054template<class _Yp>
4055inline _LIBCPP_INLINE_VISIBILITY
4056shared_ptr<_Tp>&
4057shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4058{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004059 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004060 return *this;
4061}
4062
4063template<class _Tp>
4064template <class _Yp, class _Dp>
4065inline _LIBCPP_INLINE_VISIBILITY
4066shared_ptr<_Tp>&
4067shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4068{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004069 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004070 return *this;
4071}
4072
Howard Hinnant73d21a42010-09-04 23:28:19 +00004073#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004074
4075template<class _Tp>
4076template<class _Yp>
4077inline _LIBCPP_INLINE_VISIBILITY
4078shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00004079shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004080{
4081 shared_ptr(__r).swap(*this);
4082 return *this;
4083}
4084
4085template<class _Tp>
4086template <class _Yp, class _Dp>
4087inline _LIBCPP_INLINE_VISIBILITY
4088shared_ptr<_Tp>&
4089shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4090{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004091 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004092 return *this;
4093}
4094
Howard Hinnant73d21a42010-09-04 23:28:19 +00004095#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004096
4097template<class _Tp>
4098inline _LIBCPP_INLINE_VISIBILITY
4099void
Howard Hinnant1694d232011-05-28 14:41:13 +00004100shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004101{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004102 _VSTD::swap(__ptr_, __r.__ptr_);
4103 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004104}
4105
4106template<class _Tp>
4107inline _LIBCPP_INLINE_VISIBILITY
4108void
Howard Hinnant1694d232011-05-28 14:41:13 +00004109shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004110{
4111 shared_ptr().swap(*this);
4112}
4113
4114template<class _Tp>
4115template<class _Yp>
4116inline _LIBCPP_INLINE_VISIBILITY
4117void
4118shared_ptr<_Tp>::reset(_Yp* __p)
4119{
4120 shared_ptr(__p).swap(*this);
4121}
4122
4123template<class _Tp>
4124template<class _Yp, class _Dp>
4125inline _LIBCPP_INLINE_VISIBILITY
4126void
4127shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4128{
4129 shared_ptr(__p, __d).swap(*this);
4130}
4131
4132template<class _Tp>
4133template<class _Yp, class _Dp, class _Alloc>
4134inline _LIBCPP_INLINE_VISIBILITY
4135void
4136shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4137{
4138 shared_ptr(__p, __d, __a).swap(*this);
4139}
4140
4141#ifndef _LIBCPP_HAS_NO_VARIADICS
4142
Howard Hinnant324bb032010-08-22 00:02:43 +00004143template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004144inline _LIBCPP_INLINE_VISIBILITY
4145shared_ptr<_Tp>
4146make_shared(_Args&& ...__args)
4147{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004148 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004149}
4150
Howard Hinnant324bb032010-08-22 00:02:43 +00004151template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004152inline _LIBCPP_INLINE_VISIBILITY
4153shared_ptr<_Tp>
4154allocate_shared(const _Alloc& __a, _Args&& ...__args)
4155{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004156 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004157}
4158
4159#else // _LIBCPP_HAS_NO_VARIADICS
4160
4161template<class _Tp>
4162inline _LIBCPP_INLINE_VISIBILITY
4163shared_ptr<_Tp>
4164make_shared()
4165{
4166 return shared_ptr<_Tp>::make_shared();
4167}
4168
4169template<class _Tp, class _A0>
4170inline _LIBCPP_INLINE_VISIBILITY
4171shared_ptr<_Tp>
4172make_shared(_A0& __a0)
4173{
4174 return shared_ptr<_Tp>::make_shared(__a0);
4175}
4176
4177template<class _Tp, class _A0, class _A1>
4178inline _LIBCPP_INLINE_VISIBILITY
4179shared_ptr<_Tp>
4180make_shared(_A0& __a0, _A1& __a1)
4181{
4182 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4183}
4184
Howard Hinnant324bb032010-08-22 00:02:43 +00004185template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004186inline _LIBCPP_INLINE_VISIBILITY
4187shared_ptr<_Tp>
4188make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4189{
4190 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4191}
4192
4193template<class _Tp, class _Alloc>
4194inline _LIBCPP_INLINE_VISIBILITY
4195shared_ptr<_Tp>
4196allocate_shared(const _Alloc& __a)
4197{
4198 return shared_ptr<_Tp>::allocate_shared(__a);
4199}
4200
4201template<class _Tp, class _Alloc, class _A0>
4202inline _LIBCPP_INLINE_VISIBILITY
4203shared_ptr<_Tp>
4204allocate_shared(const _Alloc& __a, _A0& __a0)
4205{
4206 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4207}
4208
4209template<class _Tp, class _Alloc, class _A0, class _A1>
4210inline _LIBCPP_INLINE_VISIBILITY
4211shared_ptr<_Tp>
4212allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4213{
4214 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4215}
4216
4217template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4218inline _LIBCPP_INLINE_VISIBILITY
4219shared_ptr<_Tp>
4220allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4221{
4222 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4223}
4224
4225#endif // _LIBCPP_HAS_NO_VARIADICS
4226
4227template<class _Tp, class _Up>
4228inline _LIBCPP_INLINE_VISIBILITY
4229bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004230operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004231{
4232 return __x.get() == __y.get();
4233}
4234
4235template<class _Tp, class _Up>
4236inline _LIBCPP_INLINE_VISIBILITY
4237bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004238operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004239{
4240 return !(__x == __y);
4241}
4242
4243template<class _Tp, class _Up>
4244inline _LIBCPP_INLINE_VISIBILITY
4245bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004246operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004247{
4248 return __x.get() < __y.get();
4249}
4250
4251template<class _Tp>
4252inline _LIBCPP_INLINE_VISIBILITY
4253void
Howard Hinnant1694d232011-05-28 14:41:13 +00004254swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004255{
4256 __x.swap(__y);
4257}
4258
4259template<class _Tp, class _Up>
4260inline _LIBCPP_INLINE_VISIBILITY
4261shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004262static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004263{
4264 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4265}
4266
4267template<class _Tp, class _Up>
4268inline _LIBCPP_INLINE_VISIBILITY
4269shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004270dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004271{
4272 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4273 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4274}
4275
4276template<class _Tp, class _Up>
4277shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004278const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004279{
4280 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
4281}
4282
Howard Hinnantd4444702010-08-11 17:04:31 +00004283#ifndef _LIBCPP_NO_RTTI
4284
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004285template<class _Dp, class _Tp>
4286inline _LIBCPP_INLINE_VISIBILITY
4287_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004288get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004289{
4290 return __p.template __get_deleter<_Dp>();
4291}
4292
Howard Hinnant324bb032010-08-22 00:02:43 +00004293#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004294
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004295template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004296class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004297{
Howard Hinnant324bb032010-08-22 00:02:43 +00004298public:
4299 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004300private:
4301 element_type* __ptr_;
4302 __shared_weak_count* __cntrl_;
4303
Howard Hinnant324bb032010-08-22 00:02:43 +00004304public:
Howard Hinnant1694d232011-05-28 14:41:13 +00004305 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004306 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004307 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4308 _NOEXCEPT;
4309 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004310 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004311 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4312 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004313
4314 ~weak_ptr();
4315
Howard Hinnant1694d232011-05-28 14:41:13 +00004316 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
4317 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4318 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004319
Howard Hinnant1694d232011-05-28 14:41:13 +00004320 void swap(weak_ptr& __r) _NOEXCEPT;
4321 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004322
Howard Hinnant82894812010-09-22 16:48:34 +00004323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004324 long use_count() const _NOEXCEPT
4325 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004327 bool expired() const _NOEXCEPT
4328 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4329 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004330 template<class _Up>
4331 _LIBCPP_INLINE_VISIBILITY
4332 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004333 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004334 template<class _Up>
4335 _LIBCPP_INLINE_VISIBILITY
4336 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004337 {return __cntrl_ < __r.__cntrl_;}
4338
Howard Hinnant82894812010-09-22 16:48:34 +00004339 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
4340 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004341};
4342
4343template<class _Tp>
4344inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004345weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004346 : __ptr_(0),
4347 __cntrl_(0)
4348{
4349}
4350
4351template<class _Tp>
4352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004353weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004354 : __ptr_(__r.__ptr_),
4355 __cntrl_(__r.__cntrl_)
4356{
4357 if (__cntrl_)
4358 __cntrl_->__add_weak();
4359}
4360
4361template<class _Tp>
4362template<class _Yp>
4363inline _LIBCPP_INLINE_VISIBILITY
4364weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004365 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004366 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004367 : __ptr_(__r.__ptr_),
4368 __cntrl_(__r.__cntrl_)
4369{
4370 if (__cntrl_)
4371 __cntrl_->__add_weak();
4372}
4373
4374template<class _Tp>
4375template<class _Yp>
4376inline _LIBCPP_INLINE_VISIBILITY
4377weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004378 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004379 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004380 : __ptr_(__r.__ptr_),
4381 __cntrl_(__r.__cntrl_)
4382{
4383 if (__cntrl_)
4384 __cntrl_->__add_weak();
4385}
4386
4387template<class _Tp>
4388weak_ptr<_Tp>::~weak_ptr()
4389{
4390 if (__cntrl_)
4391 __cntrl_->__release_weak();
4392}
4393
4394template<class _Tp>
4395inline _LIBCPP_INLINE_VISIBILITY
4396weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004397weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004398{
4399 weak_ptr(__r).swap(*this);
4400 return *this;
4401}
4402
4403template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004404template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004405inline _LIBCPP_INLINE_VISIBILITY
4406weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004407weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004408{
4409 weak_ptr(__r).swap(*this);
4410 return *this;
4411}
4412
4413template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004414template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004415inline _LIBCPP_INLINE_VISIBILITY
4416weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004417weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004418{
4419 weak_ptr(__r).swap(*this);
4420 return *this;
4421}
4422
4423template<class _Tp>
4424inline _LIBCPP_INLINE_VISIBILITY
4425void
Howard Hinnant1694d232011-05-28 14:41:13 +00004426weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004427{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004428 _VSTD::swap(__ptr_, __r.__ptr_);
4429 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004430}
4431
4432template<class _Tp>
4433inline _LIBCPP_INLINE_VISIBILITY
4434void
Howard Hinnant1694d232011-05-28 14:41:13 +00004435swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004436{
4437 __x.swap(__y);
4438}
4439
4440template<class _Tp>
4441inline _LIBCPP_INLINE_VISIBILITY
4442void
Howard Hinnant1694d232011-05-28 14:41:13 +00004443weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004444{
4445 weak_ptr().swap(*this);
4446}
4447
4448template<class _Tp>
4449template<class _Yp>
4450shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4451 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4452 : __ptr_(__r.__ptr_),
4453 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4454{
4455 if (__cntrl_ == 0)
4456#ifndef _LIBCPP_NO_EXCEPTIONS
4457 throw bad_weak_ptr();
4458#else
4459 assert(!"bad_weak_ptr");
4460#endif
4461}
4462
4463template<class _Tp>
4464shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004465weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004466{
4467 shared_ptr<_Tp> __r;
4468 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4469 if (__r.__cntrl_)
4470 __r.__ptr_ = __ptr_;
4471 return __r;
4472}
4473
Howard Hinnant324bb032010-08-22 00:02:43 +00004474template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004475
4476template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004477struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004478 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00004479{
4480 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004482 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4483 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004485 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4486 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004488 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4489 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004490};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004491
4492template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004493struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004494 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4495{
Howard Hinnant324bb032010-08-22 00:02:43 +00004496 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004498 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4499 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004501 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4502 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004504 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4505 {return __x.owner_before(__y);}
4506};
4507
4508template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004509class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004510{
4511 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00004512protected:
Howard Hinnant82894812010-09-22 16:48:34 +00004513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004514 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004516 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004518 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4519 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00004520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004521 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00004522public:
Howard Hinnant82894812010-09-22 16:48:34 +00004523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004524 shared_ptr<_Tp> shared_from_this()
4525 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00004526 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004527 shared_ptr<_Tp const> shared_from_this() const
4528 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004529
4530 template <class _Up> friend class shared_ptr;
4531};
4532
Howard Hinnant21aefc32010-06-03 16:42:57 +00004533template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004534struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00004535{
4536 typedef shared_ptr<_Tp> argument_type;
4537 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004539 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00004540 {
4541 return hash<_Tp*>()(__ptr.get());
4542 }
4543};
4544
Howard Hinnant99968442011-11-29 18:15:50 +00004545template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004546inline _LIBCPP_INLINE_VISIBILITY
4547basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00004548operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004549
Howard Hinnant324bb032010-08-22 00:02:43 +00004550//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00004551struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004552{
4553 enum _
4554 {
4555 relaxed,
4556 preferred,
4557 strict
4558 };
4559
4560 _ __v_;
4561
Howard Hinnant82894812010-09-22 16:48:34 +00004562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004563 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00004564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004565 operator int() const {return __v_;}
4566};
4567
4568void declare_reachable(void* __p);
4569void declare_no_pointers(char* __p, size_t __n);
4570void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00004571pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004572void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004573
4574template <class _Tp>
4575inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004576_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004577undeclare_reachable(_Tp* __p)
4578{
4579 return static_cast<_Tp*>(__undeclare_reachable(__p));
4580}
4581
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004582void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004583
4584_LIBCPP_END_NAMESPACE_STD
4585
4586#endif // _LIBCPP_MEMORY