blob: 43f8dbadadcdcaf9adc8745246354bd3d98f6aa8 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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 Hinnant3b6579a2010-08-22 00:02:43 +000031
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000033
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant719bda32011-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 Hinnantc51e1022010-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 Hinnant8e4e6002010-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 Hinnantc51e1022010-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 Hinnant719bda32011-05-28 14:41:13 +000085 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-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
Marshall Clow4f834b52013-08-27 20:22:15 +000093 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000094
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 Hinnant719bda32011-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 Hinnantc51e1022010-05-11 19:42:16 +0000130 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-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 Hinnantc51e1022010-05-11 19:42:16 +0000137};
138
139template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000140bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141
142template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000143bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-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 Hinnant719bda32011-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 Hinnantc51e1022010-05-11 19:42:16 +0000165
166template <class InputIterator, class ForwardIterator>
167ForwardIterator
168uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
169
Howard Hinnant719bda32011-05-28 14:41:13 +0000170template <class InputIterator, class Size, class ForwardIterator>
171ForwardIterator
172uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
173
Howard Hinnantc51e1022010-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 Hinnant3c811092010-11-18 16:13:03 +0000178ForwardIterator
179uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-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 Hinnantd26c01d2010-08-19 18:39:17 +0000208template <class T>
209struct default_delete
210{
Howard Hinnant719bda32011-05-28 14:41:13 +0000211 constexpr default_delete() noexcept = default;
212 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000213
Howard Hinnant719bda32011-05-28 14:41:13 +0000214 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000215};
216
217template <class T>
218struct default_delete<T[]>
219{
Howard Hinnant719bda32011-05-28 14:41:13 +0000220 constexpr default_delete() noexcept = default;
221 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000222 template <class U> void operator()(U*) const = delete;
223};
224
Howard Hinnantd26c01d2010-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 Hinnant719bda32011-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 Hinnantd26c01d2010-08-19 18:39:17 +0000240 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000241 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000242 template <class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000243 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000244
245 // destructor
246 ~unique_ptr();
247
248 // assignment
Howard Hinnant719bda32011-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 Hinnantd26c01d2010-08-19 18:39:17 +0000252
253 // observers
254 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-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 Hinnantd26c01d2010-08-19 18:39:17 +0000260
261 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000262 pointer release() noexcept;
263 void reset(pointer p = pointer()) noexcept;
264 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-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 Hinnant719bda32011-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 Hinnantd26c01d2010-08-19 18:39:17 +0000282
283 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000284 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000287 unique_ptr& operator=(unique_ptr&& u) noexcept;
288 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000289
290 // observers
291 T& operator[](size_t i) const;
Howard Hinnant719bda32011-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 Hinnantd26c01d2010-08-19 18:39:17 +0000296
297 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000298 pointer release() noexcept;
299 void reset(pointer p = pointer()) noexcept;
300 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000301 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000302 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000303};
304
305template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-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 Hinnant719bda32011-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 Hinnantd26c01d2010-08-19 18:39:17 +0000347class bad_weak_ptr
348 : public std::exception
349{
Howard Hinnant719bda32011-05-28 14:41:13 +0000350 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000351};
352
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000353template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
354template<class T> unique_ptr<T> make_unique(size_t n); // C++14
355template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
356
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000357template<class T>
358class shared_ptr
359{
360public:
361 typedef T element_type;
362
363 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000364 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000365 template<class Y> explicit shared_ptr(Y* p);
366 template<class Y, class D> shared_ptr(Y* p, D d);
367 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
368 template <class D> shared_ptr(nullptr_t p, D d);
369 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000370 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
371 shared_ptr(const shared_ptr& r) noexcept;
372 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
373 shared_ptr(shared_ptr&& r) noexcept;
374 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000375 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
376 template<class Y> shared_ptr(auto_ptr<Y>&& r);
377 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
378 shared_ptr(nullptr_t) : shared_ptr() { }
379
380 // destructor:
381 ~shared_ptr();
382
383 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000384 shared_ptr& operator=(const shared_ptr& r) noexcept;
385 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
386 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
388 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
389 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
390
391 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000392 void swap(shared_ptr& r) noexcept;
393 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000394 template<class Y> void reset(Y* p);
395 template<class Y, class D> void reset(Y* p, D d);
396 template<class Y, class D, class A> void reset(Y* p, D d, A a);
397
Howard Hinnant719bda32011-05-28 14:41:13 +0000398 // observers:
399 T* get() const noexcept;
400 T& operator*() const noexcept;
401 T* operator->() const noexcept;
402 long use_count() const noexcept;
403 bool unique() const noexcept;
404 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000405 template<class U> bool owner_before(shared_ptr<U> const& b) const;
406 template<class U> bool owner_before(weak_ptr<U> const& b) const;
407};
408
409// shared_ptr comparisons:
410template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000411 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000412template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000413 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000414template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000416template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000417 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000418template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000419 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000421 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
422
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>
430 bool 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>
434bool 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;
443template <class T>
444 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
445template <class T>
446 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000447
448// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000449template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000450
451// shared_ptr casts:
452template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000453 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000454template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000455 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000456template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000457 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458
459// shared_ptr I/O:
460template<class E, class T, class Y>
461 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
462
463// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000464template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000465
466template<class T, class... Args>
467 shared_ptr<T> make_shared(Args&&... args);
468template<class T, class A, class... Args>
469 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
470
471template<class T>
472class weak_ptr
473{
474public:
475 typedef T element_type;
476
477 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000478 constexpr weak_ptr() noexcept;
479 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
480 weak_ptr(weak_ptr const& r) noexcept;
481 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000482 weak_ptr(weak_ptr&& r) noexcept; // C++14
483 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000484
485 // destructor
486 ~weak_ptr();
487
488 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000489 weak_ptr& operator=(weak_ptr const& r) noexcept;
490 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
491 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000492 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
493 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000494
495 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000496 void swap(weak_ptr& r) noexcept;
497 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000498
499 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000500 long use_count() const noexcept;
501 bool expired() const noexcept;
502 shared_ptr<T> lock() const noexcept;
Marshall Clow495e4a62013-09-03 14:37:50 +0000503 template<class U> bool owner_before(shared_ptr<U> const& b) const;
504 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000505};
506
507// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000508template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000509
510// class owner_less:
511template<class T> struct owner_less;
512
513template<class T>
514struct owner_less<shared_ptr<T>>
515 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
516{
517 typedef bool result_type;
518 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
519 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
520 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
521};
522
523template<class T>
524struct owner_less<weak_ptr<T>>
525 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
526{
527 typedef bool result_type;
528 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
529 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
530 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
531};
532
533template<class T>
534class enable_shared_from_this
535{
536protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000537 constexpr enable_shared_from_this() noexcept;
538 enable_shared_from_this(enable_shared_from_this const&) noexcept;
539 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000540 ~enable_shared_from_this();
541public:
542 shared_ptr<T> shared_from_this();
543 shared_ptr<T const> shared_from_this() const;
544};
545
546template<class T>
547 bool atomic_is_lock_free(const shared_ptr<T>* p);
548template<class T>
549 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
550template<class T>
551 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
552template<class T>
553 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
554template<class T>
555 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
556template<class T>
557 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
558template<class T>
559 shared_ptr<T>
560 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
561template<class T>
562 bool
563 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
564template<class T>
565 bool
566 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
567template<class T>
568 bool
569 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
570 shared_ptr<T> w, memory_order success,
571 memory_order failure);
572template<class T>
573 bool
574 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
575 shared_ptr<T> w, memory_order success,
576 memory_order failure);
577// Hash support
578template <class T> struct hash;
579template <class T, class D> struct hash<unique_ptr<T, D> >;
580template <class T> struct hash<shared_ptr<T> >;
581
582// Pointer safety
583enum class pointer_safety { relaxed, preferred, strict };
584void declare_reachable(void *p);
585template <class T> T *undeclare_reachable(T *p);
586void declare_no_pointers(char *p, size_t n);
587void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000588pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000589
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
591
592} // std
593
594*/
595
596#include <__config>
597#include <type_traits>
598#include <typeinfo>
599#include <cstddef>
600#include <cstdint>
601#include <new>
602#include <utility>
603#include <limits>
604#include <iterator>
605#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000606#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000607#include <tuple>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000608#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609#if defined(_LIBCPP_NO_EXCEPTIONS)
610 #include <cassert>
611#endif
612
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000613#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000614# include <atomic>
615#endif
616
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000617#include <__undef_min_max>
Saleem Abdulrasooldcf27a62015-02-13 22:15:32 +0000618#include <__undef___deallocate>
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000619
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000620#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000622#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623
624_LIBCPP_BEGIN_NAMESPACE_STD
625
Howard Hinnant8bea6da2013-08-08 18:38:55 +0000626// addressof moved to <__functional_base>
Douglas Gregor13034442011-06-22 22:17:44 +0000627
Howard Hinnantc51e1022010-05-11 19:42:16 +0000628template <class _Tp> class allocator;
629
630template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000631class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632{
633public:
634 typedef void* pointer;
635 typedef const void* const_pointer;
636 typedef void value_type;
637
638 template <class _Up> struct rebind {typedef allocator<_Up> other;};
639};
640
Howard Hinnant9f771052012-01-19 23:15:22 +0000641template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000642class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000643{
644public:
645 typedef const void* pointer;
646 typedef const void* const_pointer;
647 typedef const void value_type;
648
649 template <class _Up> struct rebind {typedef allocator<_Up> other;};
650};
651
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652// pointer_traits
653
654template <class _Tp>
655struct __has_element_type
656{
657private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000658 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659 template <class _Up> static __two __test(...);
660 template <class _Up> static char __test(typename _Up::element_type* = 0);
661public:
662 static const bool value = sizeof(__test<_Tp>(0)) == 1;
663};
664
665template <class _Ptr, bool = __has_element_type<_Ptr>::value>
666struct __pointer_traits_element_type;
667
668template <class _Ptr>
669struct __pointer_traits_element_type<_Ptr, true>
670{
671 typedef typename _Ptr::element_type type;
672};
673
674#ifndef _LIBCPP_HAS_NO_VARIADICS
675
676template <template <class, class...> class _Sp, class _Tp, class ..._Args>
677struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
678{
679 typedef typename _Sp<_Tp, _Args...>::element_type type;
680};
681
682template <template <class, class...> class _Sp, class _Tp, class ..._Args>
683struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
684{
685 typedef _Tp type;
686};
687
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000688#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689
690template <template <class> class _Sp, class _Tp>
691struct __pointer_traits_element_type<_Sp<_Tp>, true>
692{
693 typedef typename _Sp<_Tp>::element_type type;
694};
695
696template <template <class> class _Sp, class _Tp>
697struct __pointer_traits_element_type<_Sp<_Tp>, false>
698{
699 typedef _Tp type;
700};
701
702template <template <class, class> class _Sp, class _Tp, class _A0>
703struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
704{
705 typedef typename _Sp<_Tp, _A0>::element_type type;
706};
707
708template <template <class, class> class _Sp, class _Tp, class _A0>
709struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
710{
711 typedef _Tp type;
712};
713
714template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
715struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
716{
717 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
718};
719
720template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
721struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
722{
723 typedef _Tp type;
724};
725
726template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
727 class _A1, class _A2>
728struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
729{
730 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
731};
732
733template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
734 class _A1, class _A2>
735struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
736{
737 typedef _Tp type;
738};
739
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000740#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
742template <class _Tp>
743struct __has_difference_type
744{
745private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000746 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 template <class _Up> static __two __test(...);
748 template <class _Up> static char __test(typename _Up::difference_type* = 0);
749public:
750 static const bool value = sizeof(__test<_Tp>(0)) == 1;
751};
752
753template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
754struct __pointer_traits_difference_type
755{
756 typedef ptrdiff_t type;
757};
758
759template <class _Ptr>
760struct __pointer_traits_difference_type<_Ptr, true>
761{
762 typedef typename _Ptr::difference_type type;
763};
764
765template <class _Tp, class _Up>
766struct __has_rebind
767{
768private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000769 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 template <class _Xp> static __two __test(...);
771 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
772public:
773 static const bool value = sizeof(__test<_Tp>(0)) == 1;
774};
775
776template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
777struct __pointer_traits_rebind
778{
779#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
780 typedef typename _Tp::template rebind<_Up> type;
781#else
782 typedef typename _Tp::template rebind<_Up>::other type;
783#endif
784};
785
786#ifndef _LIBCPP_HAS_NO_VARIADICS
787
788template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
789struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
790{
791#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
792 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
793#else
794 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
795#endif
796};
797
798template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
799struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
800{
801 typedef _Sp<_Up, _Args...> type;
802};
803
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000804#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805
806template <template <class> class _Sp, class _Tp, class _Up>
807struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
808{
809#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
810 typedef typename _Sp<_Tp>::template rebind<_Up> type;
811#else
812 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
813#endif
814};
815
816template <template <class> class _Sp, class _Tp, class _Up>
817struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
818{
819 typedef _Sp<_Up> type;
820};
821
822template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
823struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
824{
825#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
826 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
827#else
828 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
829#endif
830};
831
832template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
833struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
834{
835 typedef _Sp<_Up, _A0> type;
836};
837
838template <template <class, class, class> class _Sp, class _Tp, class _A0,
839 class _A1, class _Up>
840struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
841{
842#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
843 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
844#else
845 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
846#endif
847};
848
849template <template <class, class, class> class _Sp, class _Tp, class _A0,
850 class _A1, class _Up>
851struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
852{
853 typedef _Sp<_Up, _A0, _A1> type;
854};
855
856template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
857 class _A1, class _A2, class _Up>
858struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
859{
860#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
861 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
862#else
863 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
864#endif
865};
866
867template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
868 class _A1, class _A2, class _Up>
869struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
870{
871 typedef _Sp<_Up, _A0, _A1, _A2> type;
872};
873
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000874#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875
876template <class _Ptr>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000877struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878{
879 typedef _Ptr pointer;
880 typedef typename __pointer_traits_element_type<pointer>::type element_type;
881 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
882
883#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantf09283d2011-05-11 20:21:19 +0000884 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885#else
886 template <class _Up> struct rebind
887 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000888#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889
890private:
891 struct __nat {};
892public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894 static pointer pointer_to(typename conditional<is_void<element_type>::value,
895 __nat, element_type>::type& __r)
896 {return pointer::pointer_to(__r);}
897};
898
899template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000900struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901{
902 typedef _Tp* pointer;
903 typedef _Tp element_type;
904 typedef ptrdiff_t difference_type;
905
906#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
907 template <class _Up> using rebind = _Up*;
908#else
909 template <class _Up> struct rebind {typedef _Up* other;};
910#endif
911
912private:
913 struct __nat {};
914public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000917 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000918 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919};
920
921// allocator_traits
922
923namespace __has_pointer_type_imp
924{
Howard Hinnant6e260172013-09-21 01:45:05 +0000925 template <class _Up> static __two __test(...);
926 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927}
928
929template <class _Tp>
930struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +0000931 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932{
933};
934
935namespace __pointer_type_imp
936{
937
938template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
939struct __pointer_type
940{
941 typedef typename _Dp::pointer type;
942};
943
944template <class _Tp, class _Dp>
945struct __pointer_type<_Tp, _Dp, false>
946{
947 typedef _Tp* type;
948};
949
Howard Hinnant8e4e6002010-11-18 01:40:00 +0000950} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951
952template <class _Tp, class _Dp>
953struct __pointer_type
954{
955 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
956};
957
958template <class _Tp>
959struct __has_const_pointer
960{
961private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000962 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963 template <class _Up> static __two __test(...);
964 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
965public:
966 static const bool value = sizeof(__test<_Tp>(0)) == 1;
967};
968
969template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
970struct __const_pointer
971{
972 typedef typename _Alloc::const_pointer type;
973};
974
975template <class _Tp, class _Ptr, class _Alloc>
976struct __const_pointer<_Tp, _Ptr, _Alloc, false>
977{
978#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
979 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
980#else
981 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
982#endif
983};
984
985template <class _Tp>
986struct __has_void_pointer
987{
988private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000989 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990 template <class _Up> static __two __test(...);
991 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
992public:
993 static const bool value = sizeof(__test<_Tp>(0)) == 1;
994};
995
996template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
997struct __void_pointer
998{
999 typedef typename _Alloc::void_pointer type;
1000};
1001
1002template <class _Ptr, class _Alloc>
1003struct __void_pointer<_Ptr, _Alloc, false>
1004{
1005#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1006 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1007#else
1008 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1009#endif
1010};
1011
1012template <class _Tp>
1013struct __has_const_void_pointer
1014{
1015private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001016 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 template <class _Up> static __two __test(...);
1018 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1019public:
1020 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1021};
1022
1023template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1024struct __const_void_pointer
1025{
1026 typedef typename _Alloc::const_void_pointer type;
1027};
1028
1029template <class _Ptr, class _Alloc>
1030struct __const_void_pointer<_Ptr, _Alloc, false>
1031{
1032#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1033 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1034#else
1035 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1036#endif
1037};
1038
Howard Hinnantc834c512011-11-29 18:15:50 +00001039template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001041_Tp*
1042__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043{
1044 return __p;
1045}
1046
1047template <class _Pointer>
1048inline _LIBCPP_INLINE_VISIBILITY
1049typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001050__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001052 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053}
1054
1055template <class _Tp>
1056struct __has_size_type
1057{
1058private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001059 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060 template <class _Up> static __two __test(...);
1061 template <class _Up> static char __test(typename _Up::size_type* = 0);
1062public:
1063 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1064};
1065
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001066template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067struct __size_type
1068{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001069 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070};
1071
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001072template <class _Alloc, class _DiffType>
1073struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074{
1075 typedef typename _Alloc::size_type type;
1076};
1077
1078template <class _Tp>
1079struct __has_propagate_on_container_copy_assignment
1080{
1081private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001082 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 template <class _Up> static __two __test(...);
1084 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1085public:
1086 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1087};
1088
1089template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1090struct __propagate_on_container_copy_assignment
1091{
1092 typedef false_type type;
1093};
1094
1095template <class _Alloc>
1096struct __propagate_on_container_copy_assignment<_Alloc, true>
1097{
1098 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1099};
1100
1101template <class _Tp>
1102struct __has_propagate_on_container_move_assignment
1103{
1104private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001105 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 template <class _Up> static __two __test(...);
1107 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1108public:
1109 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1110};
1111
1112template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1113struct __propagate_on_container_move_assignment
1114{
1115 typedef false_type type;
1116};
1117
1118template <class _Alloc>
1119struct __propagate_on_container_move_assignment<_Alloc, true>
1120{
1121 typedef typename _Alloc::propagate_on_container_move_assignment type;
1122};
1123
1124template <class _Tp>
1125struct __has_propagate_on_container_swap
1126{
1127private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001128 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 template <class _Up> static __two __test(...);
1130 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1131public:
1132 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1133};
1134
1135template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1136struct __propagate_on_container_swap
1137{
1138 typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_swap<_Alloc, true>
1143{
1144 typedef typename _Alloc::propagate_on_container_swap type;
1145};
1146
1147template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1148struct __has_rebind_other
1149{
1150private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001151 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 template <class _Xp> static __two __test(...);
1153 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1154public:
1155 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1156};
1157
1158template <class _Tp, class _Up>
1159struct __has_rebind_other<_Tp, _Up, false>
1160{
1161 static const bool value = false;
1162};
1163
1164template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1165struct __allocator_traits_rebind
1166{
1167 typedef typename _Tp::template rebind<_Up>::other type;
1168};
1169
1170#ifndef _LIBCPP_HAS_NO_VARIADICS
1171
1172template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1173struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1174{
1175 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1176};
1177
1178template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1179struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1180{
1181 typedef _Alloc<_Up, _Args...> type;
1182};
1183
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001184#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185
1186template <template <class> class _Alloc, class _Tp, class _Up>
1187struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1188{
1189 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1190};
1191
1192template <template <class> class _Alloc, class _Tp, class _Up>
1193struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1194{
1195 typedef _Alloc<_Up> type;
1196};
1197
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1199struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1200{
1201 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1202};
1203
1204template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1205struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1206{
1207 typedef _Alloc<_Up, _A0> type;
1208};
1209
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1211 class _A1, class _Up>
1212struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1213{
1214 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1215};
1216
1217template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1218 class _A1, class _Up>
1219struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1220{
1221 typedef _Alloc<_Up, _A0, _A1> type;
1222};
1223
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1225 class _A1, class _A2, class _Up>
1226struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1227{
1228 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1229};
1230
1231template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1232 class _A1, class _A2, class _Up>
1233struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1234{
1235 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1236};
1237
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001238#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239
1240#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1241
1242template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1243auto
1244__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1245 -> decltype(__a.allocate(__sz, __p), true_type());
1246
1247template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1248auto
1249__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1250 -> false_type;
1251
1252template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1253struct __has_allocate_hint
1254 : integral_constant<bool,
1255 is_same<
1256 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1257 declval<_SizeType>(),
1258 declval<_ConstVoidPtr>())),
1259 true_type>::value>
1260{
1261};
1262
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001263#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264
1265template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1266struct __has_allocate_hint
1267 : true_type
1268{
1269};
1270
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001271#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272
Howard Hinnant986832c2011-07-29 21:35:53 +00001273#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274
1275template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001276decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1277 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 true_type())
1279__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1280
1281template <class _Alloc, class _Pointer, class ..._Args>
1282false_type
1283__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1284
1285template <class _Alloc, class _Pointer, class ..._Args>
1286struct __has_construct
1287 : integral_constant<bool,
1288 is_same<
1289 decltype(__has_construct_test(declval<_Alloc>(),
1290 declval<_Pointer>(),
1291 declval<_Args>()...)),
1292 true_type>::value>
1293{
1294};
1295
1296template <class _Alloc, class _Pointer>
1297auto
1298__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1299 -> decltype(__a.destroy(__p), true_type());
1300
1301template <class _Alloc, class _Pointer>
1302auto
1303__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1304 -> false_type;
1305
1306template <class _Alloc, class _Pointer>
1307struct __has_destroy
1308 : integral_constant<bool,
1309 is_same<
1310 decltype(__has_destroy_test(declval<_Alloc>(),
1311 declval<_Pointer>())),
1312 true_type>::value>
1313{
1314};
1315
1316template <class _Alloc>
1317auto
1318__has_max_size_test(_Alloc&& __a)
1319 -> decltype(__a.max_size(), true_type());
1320
1321template <class _Alloc>
1322auto
1323__has_max_size_test(const volatile _Alloc& __a)
1324 -> false_type;
1325
1326template <class _Alloc>
1327struct __has_max_size
1328 : integral_constant<bool,
1329 is_same<
1330 decltype(__has_max_size_test(declval<_Alloc&>())),
1331 true_type>::value>
1332{
1333};
1334
1335template <class _Alloc>
1336auto
1337__has_select_on_container_copy_construction_test(_Alloc&& __a)
1338 -> decltype(__a.select_on_container_copy_construction(), true_type());
1339
1340template <class _Alloc>
1341auto
1342__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1343 -> false_type;
1344
1345template <class _Alloc>
1346struct __has_select_on_container_copy_construction
1347 : integral_constant<bool,
1348 is_same<
1349 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1350 true_type>::value>
1351{
1352};
1353
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001354#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355
1356#ifndef _LIBCPP_HAS_NO_VARIADICS
1357
1358template <class _Alloc, class _Pointer, class ..._Args>
1359struct __has_construct
1360 : false_type
1361{
1362};
1363
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001364#else // _LIBCPP_HAS_NO_VARIADICS
1365
1366template <class _Alloc, class _Pointer, class _Args>
1367struct __has_construct
1368 : false_type
1369{
1370};
1371
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001372#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
1374template <class _Alloc, class _Pointer>
1375struct __has_destroy
1376 : false_type
1377{
1378};
1379
1380template <class _Alloc>
1381struct __has_max_size
1382 : true_type
1383{
1384};
1385
1386template <class _Alloc>
1387struct __has_select_on_container_copy_construction
1388 : false_type
1389{
1390};
1391
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001392#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001394template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1395struct __alloc_traits_difference_type
1396{
1397 typedef typename pointer_traits<_Ptr>::difference_type type;
1398};
1399
1400template <class _Alloc, class _Ptr>
1401struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1402{
1403 typedef typename _Alloc::difference_type type;
1404};
1405
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406template <class _Alloc>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001407struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408{
1409 typedef _Alloc allocator_type;
1410 typedef typename allocator_type::value_type value_type;
1411
1412 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1413 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1414 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1415 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1416
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001417 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1418 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419
1420 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1421 propagate_on_container_copy_assignment;
1422 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1423 propagate_on_container_move_assignment;
1424 typedef typename __propagate_on_container_swap<allocator_type>::type
1425 propagate_on_container_swap;
1426
1427#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1428 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001429 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001431#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 template <class _Tp> struct rebind_alloc
1433 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1434 template <class _Tp> struct rebind_traits
1435 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001436#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437
Howard Hinnant756c69b2010-09-22 16:48:34 +00001438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439 static pointer allocate(allocator_type& __a, size_type __n)
1440 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1443 {return allocate(__a, __n, __hint,
1444 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1445
Howard Hinnant756c69b2010-09-22 16:48:34 +00001446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001447 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 {__a.deallocate(__p, __n);}
1449
1450#ifndef _LIBCPP_HAS_NO_VARIADICS
1451 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001454 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001455 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001456#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 static void construct(allocator_type& __a, _Tp* __p)
1460 {
1461 ::new ((void*)__p) _Tp();
1462 }
1463 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1466 {
1467 ::new ((void*)__p) _Tp(__a0);
1468 }
1469 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1472 const _A1& __a1)
1473 {
1474 ::new ((void*)__p) _Tp(__a0, __a1);
1475 }
1476 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1479 const _A1& __a1, const _A2& __a2)
1480 {
1481 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1482 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001483#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484
1485 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487 static void destroy(allocator_type& __a, _Tp* __p)
1488 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1489
Howard Hinnant756c69b2010-09-22 16:48:34 +00001490 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001491 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1493
Howard Hinnant756c69b2010-09-22 16:48:34 +00001494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495 static allocator_type
1496 select_on_container_copy_construction(const allocator_type& __a)
1497 {return select_on_container_copy_construction(
1498 __has_select_on_container_copy_construction<const allocator_type>(),
1499 __a);}
1500
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001501 template <class _Ptr>
1502 _LIBCPP_INLINE_VISIBILITY
1503 static
1504 void
1505 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1506 {
1507 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1508 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1509 }
1510
1511 template <class _Tp>
1512 _LIBCPP_INLINE_VISIBILITY
1513 static
1514 typename enable_if
1515 <
1516 (is_same<allocator_type, allocator<_Tp> >::value
1517 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1518 is_trivially_move_constructible<_Tp>::value,
1519 void
1520 >::type
1521 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1522 {
1523 ptrdiff_t _Np = __end1 - __begin1;
1524 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1525 __begin2 += _Np;
1526 }
1527
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001528 template <class _Iter, class _Ptr>
1529 _LIBCPP_INLINE_VISIBILITY
1530 static
1531 void
1532 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1533 {
1534 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1535 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1536 }
1537
1538 template <class _Tp>
1539 _LIBCPP_INLINE_VISIBILITY
1540 static
1541 typename enable_if
1542 <
1543 (is_same<allocator_type, allocator<_Tp> >::value
1544 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1545 is_trivially_move_constructible<_Tp>::value,
1546 void
1547 >::type
1548 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1549 {
1550 typedef typename remove_const<_Tp>::type _Vp;
1551 ptrdiff_t _Np = __end1 - __begin1;
1552 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
1553 __begin2 += _Np;
1554 }
1555
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001556 template <class _Ptr>
1557 _LIBCPP_INLINE_VISIBILITY
1558 static
1559 void
1560 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1561 {
1562 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001563 {
1564 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1565 --__end2;
1566 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001567 }
1568
1569 template <class _Tp>
1570 _LIBCPP_INLINE_VISIBILITY
1571 static
1572 typename enable_if
1573 <
1574 (is_same<allocator_type, allocator<_Tp> >::value
1575 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1576 is_trivially_move_constructible<_Tp>::value,
1577 void
1578 >::type
1579 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1580 {
1581 ptrdiff_t _Np = __end1 - __begin1;
1582 __end2 -= _Np;
1583 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1584 }
1585
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586private:
1587
Howard Hinnant756c69b2010-09-22 16:48:34 +00001588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 static pointer allocate(allocator_type& __a, size_type __n,
1590 const_void_pointer __hint, true_type)
1591 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001594 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 {return __a.allocate(__n);}
1596
1597#ifndef _LIBCPP_HAS_NO_VARIADICS
1598 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001601 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1605 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001606 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001608#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609
1610 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1613 {__a.destroy(__p);}
1614 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 static void __destroy(false_type, allocator_type&, _Tp* __p)
1617 {
1618 __p->~_Tp();
1619 }
1620
Howard Hinnant756c69b2010-09-22 16:48:34 +00001621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622 static size_type __max_size(true_type, const allocator_type& __a)
1623 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 static size_type __max_size(false_type, const allocator_type&)
1626 {return numeric_limits<size_type>::max();}
1627
Howard Hinnant756c69b2010-09-22 16:48:34 +00001628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 static allocator_type
1630 select_on_container_copy_construction(true_type, const allocator_type& __a)
1631 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 static allocator_type
1634 select_on_container_copy_construction(false_type, const allocator_type& __a)
1635 {return __a;}
1636};
1637
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638// allocator
1639
1640template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001641class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642{
1643public:
1644 typedef size_t size_type;
1645 typedef ptrdiff_t difference_type;
1646 typedef _Tp* pointer;
1647 typedef const _Tp* const_pointer;
1648 typedef _Tp& reference;
1649 typedef const _Tp& const_reference;
1650 typedef _Tp value_type;
1651
Howard Hinnant4931e092011-06-02 21:38:57 +00001652 typedef true_type propagate_on_container_move_assignment;
1653
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1655
Howard Hinnant719bda32011-05-28 14:41:13 +00001656 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1657 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1658 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001659 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001660 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001661 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Richard Smith1f1c1472014-06-04 19:54:15 +00001663 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001664 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001665 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001666 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1667 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001668#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669 template <class _Up, class... _Args>
1670 _LIBCPP_INLINE_VISIBILITY
1671 void
1672 construct(_Up* __p, _Args&&... __args)
1673 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001674 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001676#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677 _LIBCPP_INLINE_VISIBILITY
1678 void
1679 construct(pointer __p)
1680 {
1681 ::new((void*)__p) _Tp();
1682 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001683# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001684
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685 template <class _A0>
1686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001687 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 construct(pointer __p, _A0& __a0)
1689 {
1690 ::new((void*)__p) _Tp(__a0);
1691 }
1692 template <class _A0>
1693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001694 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695 construct(pointer __p, const _A0& __a0)
1696 {
1697 ::new((void*)__p) _Tp(__a0);
1698 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001699# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 template <class _A0, class _A1>
1701 _LIBCPP_INLINE_VISIBILITY
1702 void
1703 construct(pointer __p, _A0& __a0, _A1& __a1)
1704 {
1705 ::new((void*)__p) _Tp(__a0, __a1);
1706 }
1707 template <class _A0, class _A1>
1708 _LIBCPP_INLINE_VISIBILITY
1709 void
1710 construct(pointer __p, const _A0& __a0, _A1& __a1)
1711 {
1712 ::new((void*)__p) _Tp(__a0, __a1);
1713 }
1714 template <class _A0, class _A1>
1715 _LIBCPP_INLINE_VISIBILITY
1716 void
1717 construct(pointer __p, _A0& __a0, const _A1& __a1)
1718 {
1719 ::new((void*)__p) _Tp(__a0, __a1);
1720 }
1721 template <class _A0, class _A1>
1722 _LIBCPP_INLINE_VISIBILITY
1723 void
1724 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1725 {
1726 ::new((void*)__p) _Tp(__a0, __a1);
1727 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001728#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1730};
1731
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001732template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001733class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001734{
1735public:
1736 typedef size_t size_type;
1737 typedef ptrdiff_t difference_type;
1738 typedef const _Tp* pointer;
1739 typedef const _Tp* const_pointer;
1740 typedef const _Tp& reference;
1741 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001742 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001743
1744 typedef true_type propagate_on_container_move_assignment;
1745
1746 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1747
1748 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1749 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1750 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1751 {return _VSTD::addressof(__x);}
1752 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Richard Smith1f1c1472014-06-04 19:54:15 +00001753 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001754 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001755 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001756 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1757 {return size_type(~0) / sizeof(_Tp);}
1758#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1759 template <class _Up, class... _Args>
1760 _LIBCPP_INLINE_VISIBILITY
1761 void
1762 construct(_Up* __p, _Args&&... __args)
1763 {
1764 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1765 }
1766#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1767 _LIBCPP_INLINE_VISIBILITY
1768 void
1769 construct(pointer __p)
1770 {
1771 ::new((void*)__p) _Tp();
1772 }
1773# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001774
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001775 template <class _A0>
1776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001777 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001778 construct(pointer __p, _A0& __a0)
1779 {
1780 ::new((void*)__p) _Tp(__a0);
1781 }
1782 template <class _A0>
1783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001784 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001785 construct(pointer __p, const _A0& __a0)
1786 {
1787 ::new((void*)__p) _Tp(__a0);
1788 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001789# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1790 template <class _A0, class _A1>
1791 _LIBCPP_INLINE_VISIBILITY
1792 void
1793 construct(pointer __p, _A0& __a0, _A1& __a1)
1794 {
1795 ::new((void*)__p) _Tp(__a0, __a1);
1796 }
1797 template <class _A0, class _A1>
1798 _LIBCPP_INLINE_VISIBILITY
1799 void
1800 construct(pointer __p, const _A0& __a0, _A1& __a1)
1801 {
1802 ::new((void*)__p) _Tp(__a0, __a1);
1803 }
1804 template <class _A0, class _A1>
1805 _LIBCPP_INLINE_VISIBILITY
1806 void
1807 construct(pointer __p, _A0& __a0, const _A1& __a1)
1808 {
1809 ::new((void*)__p) _Tp(__a0, __a1);
1810 }
1811 template <class _A0, class _A1>
1812 _LIBCPP_INLINE_VISIBILITY
1813 void
1814 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1815 {
1816 ::new((void*)__p) _Tp(__a0, __a1);
1817 }
1818#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1819 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1820};
1821
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822template <class _Tp, class _Up>
1823inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001824bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825
1826template <class _Tp, class _Up>
1827inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001828bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829
1830template <class _OutputIterator, class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001831class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 : public iterator<output_iterator_tag,
1833 _Tp, // purposefully not C++03
1834 ptrdiff_t, // purposefully not C++03
1835 _Tp*, // purposefully not C++03
1836 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1837{
1838private:
1839 _OutputIterator __x_;
1840public:
1841 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1842 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1843 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1844 {::new(&*__x_) _Tp(__element); return *this;}
1845 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1846 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1847 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1848};
1849
1850template <class _Tp>
1851pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001852get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853{
1854 pair<_Tp*, ptrdiff_t> __r(0, 0);
1855 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1856 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1857 / sizeof(_Tp);
1858 if (__n > __m)
1859 __n = __m;
1860 while (__n > 0)
1861 {
1862 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1863 if (__r.first)
1864 {
1865 __r.second = __n;
1866 break;
1867 }
1868 __n /= 2;
1869 }
1870 return __r;
1871}
1872
1873template <class _Tp>
1874inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001875void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876
1877template <class _Tp>
1878struct auto_ptr_ref
1879{
1880 _Tp* __ptr_;
1881};
1882
1883template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001884class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885{
1886private:
1887 _Tp* __ptr_;
1888public:
1889 typedef _Tp element_type;
1890
1891 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1892 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1893 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1894 : __ptr_(__p.release()) {}
1895 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1896 {reset(__p.release()); return *this;}
1897 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1898 {reset(__p.release()); return *this;}
1899 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1900 {reset(__p.__ptr_); return *this;}
1901 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1902
1903 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1904 {return *__ptr_;}
1905 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1906 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1907 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1908 {
1909 _Tp* __t = __ptr_;
1910 __ptr_ = 0;
1911 return __t;
1912 }
1913 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1914 {
1915 if (__ptr_ != __p)
1916 delete __ptr_;
1917 __ptr_ = __p;
1918 }
1919
1920 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1921 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1922 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1923 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1924 {return auto_ptr<_Up>(release());}
1925};
1926
1927template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001928class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929{
1930public:
1931 typedef void element_type;
1932};
1933
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1935 typename remove_cv<_T2>::type>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00001936 bool = is_empty<_T1>::value
1937#if __has_feature(is_final)
1938 && !__is_final(_T1)
1939#endif
1940 ,
1941 bool = is_empty<_T2>::value
1942#if __has_feature(is_final)
1943 && !__is_final(_T2)
1944#endif
1945 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946struct __libcpp_compressed_pair_switch;
1947
1948template <class _T1, class _T2, bool IsSame>
1949struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1950
1951template <class _T1, class _T2, bool IsSame>
1952struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1953
1954template <class _T1, class _T2, bool IsSame>
1955struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1956
1957template <class _T1, class _T2>
1958struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1959
1960template <class _T1, class _T2>
1961struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1962
1963template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1964class __libcpp_compressed_pair_imp;
1965
1966template <class _T1, class _T2>
1967class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1968{
1969private:
1970 _T1 __first_;
1971 _T2 __second_;
1972public:
1973 typedef _T1 _T1_param;
1974 typedef _T2 _T2_param;
1975
1976 typedef typename remove_reference<_T1>::type& _T1_reference;
1977 typedef typename remove_reference<_T2>::type& _T2_reference;
1978
1979 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1980 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1981
1982 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001983 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001984 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001985 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001986 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001988 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989
Howard Hinnant59f73012013-11-13 00:39:22 +00001990#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00001991
1992 _LIBCPP_INLINE_VISIBILITY
1993 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1994 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1995 is_nothrow_copy_constructible<_T2>::value)
1996 : __first_(__p.first()),
1997 __second_(__p.second()) {}
1998
1999 _LIBCPP_INLINE_VISIBILITY
2000 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2001 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2002 is_nothrow_copy_assignable<_T2>::value)
2003 {
2004 __first_ = __p.first();
2005 __second_ = __p.second();
2006 return *this;
2007 }
2008
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002009 _LIBCPP_INLINE_VISIBILITY
2010 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002011 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2012 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002013 : __first_(_VSTD::forward<_T1>(__p.first())),
2014 __second_(_VSTD::forward<_T2>(__p.second())) {}
2015
2016 _LIBCPP_INLINE_VISIBILITY
2017 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2018 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2019 is_nothrow_move_assignable<_T2>::value)
2020 {
2021 __first_ = _VSTD::forward<_T1>(__p.first());
2022 __second_ = _VSTD::forward<_T2>(__p.second());
2023 return *this;
2024 }
2025
Howard Hinnant59f73012013-11-13 00:39:22 +00002026#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002027
Howard Hinnant83b1c052011-12-19 17:58:44 +00002028#ifndef _LIBCPP_HAS_NO_VARIADICS
2029
2030 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2031 _LIBCPP_INLINE_VISIBILITY
2032 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2033 tuple<_Args1...> __first_args,
2034 tuple<_Args2...> __second_args,
2035 __tuple_indices<_I1...>,
2036 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002037 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2038 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002039 {}
2040
2041#endif // _LIBCPP_HAS_NO_VARIADICS
2042
Howard Hinnant719bda32011-05-28 14:41:13 +00002043 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2044 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045
Howard Hinnant719bda32011-05-28 14:41:13 +00002046 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2047 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048
2049 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002050 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002051 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002053 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054 swap(__first_, __x.__first_);
2055 swap(__second_, __x.__second_);
2056 }
2057};
2058
2059template <class _T1, class _T2>
2060class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2061 : private _T1
2062{
2063private:
2064 _T2 __second_;
2065public:
2066 typedef _T1 _T1_param;
2067 typedef _T2 _T2_param;
2068
2069 typedef _T1& _T1_reference;
2070 typedef typename remove_reference<_T2>::type& _T2_reference;
2071
2072 typedef const _T1& _T1_const_reference;
2073 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2074
2075 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002076 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002077 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002078 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002079 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002081 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082
Howard Hinnant59f73012013-11-13 00:39:22 +00002083#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002084
2085 _LIBCPP_INLINE_VISIBILITY
2086 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2087 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2088 is_nothrow_copy_constructible<_T2>::value)
2089 : _T1(__p.first()), __second_(__p.second()) {}
2090
2091 _LIBCPP_INLINE_VISIBILITY
2092 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2093 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2094 is_nothrow_copy_assignable<_T2>::value)
2095 {
2096 _T1::operator=(__p.first());
2097 __second_ = __p.second();
2098 return *this;
2099 }
2100
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002101 _LIBCPP_INLINE_VISIBILITY
2102 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002103 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2104 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002105 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002106
2107 _LIBCPP_INLINE_VISIBILITY
2108 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2109 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2110 is_nothrow_move_assignable<_T2>::value)
2111 {
2112 _T1::operator=(_VSTD::move(__p.first()));
2113 __second_ = _VSTD::forward<_T2>(__p.second());
2114 return *this;
2115 }
2116
Howard Hinnant59f73012013-11-13 00:39:22 +00002117#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002118
Howard Hinnant83b1c052011-12-19 17:58:44 +00002119#ifndef _LIBCPP_HAS_NO_VARIADICS
2120
2121 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2122 _LIBCPP_INLINE_VISIBILITY
2123 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2124 tuple<_Args1...> __first_args,
2125 tuple<_Args2...> __second_args,
2126 __tuple_indices<_I1...>,
2127 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002128 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2129 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002130 {}
2131
2132#endif // _LIBCPP_HAS_NO_VARIADICS
2133
Howard Hinnant719bda32011-05-28 14:41:13 +00002134 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2135 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136
Howard Hinnant719bda32011-05-28 14:41:13 +00002137 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2138 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139
2140 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002141 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002142 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002144 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 swap(__second_, __x.__second_);
2146 }
2147};
2148
2149template <class _T1, class _T2>
2150class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2151 : private _T2
2152{
2153private:
2154 _T1 __first_;
2155public:
2156 typedef _T1 _T1_param;
2157 typedef _T2 _T2_param;
2158
2159 typedef typename remove_reference<_T1>::type& _T1_reference;
2160 typedef _T2& _T2_reference;
2161
2162 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2163 typedef const _T2& _T2_const_reference;
2164
2165 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2166 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002167 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002169 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002171 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2172 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002173 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174
Howard Hinnant59f73012013-11-13 00:39:22 +00002175#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002176
2177 _LIBCPP_INLINE_VISIBILITY
2178 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2179 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2180 is_nothrow_copy_constructible<_T2>::value)
2181 : _T2(__p.second()), __first_(__p.first()) {}
2182
2183 _LIBCPP_INLINE_VISIBILITY
2184 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2185 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2186 is_nothrow_copy_assignable<_T2>::value)
2187 {
2188 _T2::operator=(__p.second());
2189 __first_ = __p.first();
2190 return *this;
2191 }
2192
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002195 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2196 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002197 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002198
2199 _LIBCPP_INLINE_VISIBILITY
2200 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2201 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2202 is_nothrow_move_assignable<_T2>::value)
2203 {
2204 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2205 __first_ = _VSTD::move(__p.first());
2206 return *this;
2207 }
2208
Howard Hinnant59f73012013-11-13 00:39:22 +00002209#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002210
Howard Hinnant83b1c052011-12-19 17:58:44 +00002211#ifndef _LIBCPP_HAS_NO_VARIADICS
2212
2213 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2214 _LIBCPP_INLINE_VISIBILITY
2215 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2216 tuple<_Args1...> __first_args,
2217 tuple<_Args2...> __second_args,
2218 __tuple_indices<_I1...>,
2219 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002220 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2221 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002222
2223 {}
2224
2225#endif // _LIBCPP_HAS_NO_VARIADICS
2226
Howard Hinnant719bda32011-05-28 14:41:13 +00002227 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2228 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229
Howard Hinnant719bda32011-05-28 14:41:13 +00002230 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2231 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232
2233 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002234 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002235 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002237 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 swap(__first_, __x.__first_);
2239 }
2240};
2241
2242template <class _T1, class _T2>
2243class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2244 : private _T1,
2245 private _T2
2246{
2247public:
2248 typedef _T1 _T1_param;
2249 typedef _T2 _T2_param;
2250
2251 typedef _T1& _T1_reference;
2252 typedef _T2& _T2_reference;
2253
2254 typedef const _T1& _T1_const_reference;
2255 typedef const _T2& _T2_const_reference;
2256
2257 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2258 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002259 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002261 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002263 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264
Howard Hinnant59f73012013-11-13 00:39:22 +00002265#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002266
2267 _LIBCPP_INLINE_VISIBILITY
2268 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2269 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2270 is_nothrow_copy_constructible<_T2>::value)
2271 : _T1(__p.first()), _T2(__p.second()) {}
2272
2273 _LIBCPP_INLINE_VISIBILITY
2274 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2275 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2276 is_nothrow_copy_assignable<_T2>::value)
2277 {
2278 _T1::operator=(__p.first());
2279 _T2::operator=(__p.second());
2280 return *this;
2281 }
2282
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002283 _LIBCPP_INLINE_VISIBILITY
2284 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002285 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2286 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002287 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002288
2289 _LIBCPP_INLINE_VISIBILITY
2290 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2291 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2292 is_nothrow_move_assignable<_T2>::value)
2293 {
2294 _T1::operator=(_VSTD::move(__p.first()));
2295 _T2::operator=(_VSTD::move(__p.second()));
2296 return *this;
2297 }
2298
Howard Hinnant59f73012013-11-13 00:39:22 +00002299#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002300
Howard Hinnant83b1c052011-12-19 17:58:44 +00002301#ifndef _LIBCPP_HAS_NO_VARIADICS
2302
2303 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2304 _LIBCPP_INLINE_VISIBILITY
2305 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2306 tuple<_Args1...> __first_args,
2307 tuple<_Args2...> __second_args,
2308 __tuple_indices<_I1...>,
2309 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002310 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2311 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002312 {}
2313
2314#endif // _LIBCPP_HAS_NO_VARIADICS
2315
Howard Hinnant719bda32011-05-28 14:41:13 +00002316 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2317 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318
Howard Hinnant719bda32011-05-28 14:41:13 +00002319 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2320 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321
Howard Hinnant28b24882011-12-01 20:21:04 +00002322 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002323 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002324 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325 {
2326 }
2327};
2328
2329template <class _T1, class _T2>
2330class __compressed_pair
2331 : private __libcpp_compressed_pair_imp<_T1, _T2>
2332{
2333 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2334public:
2335 typedef typename base::_T1_param _T1_param;
2336 typedef typename base::_T2_param _T2_param;
2337
2338 typedef typename base::_T1_reference _T1_reference;
2339 typedef typename base::_T2_reference _T2_reference;
2340
2341 typedef typename base::_T1_const_reference _T1_const_reference;
2342 typedef typename base::_T2_const_reference _T2_const_reference;
2343
2344 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002345 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002346 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002347 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002348 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002350 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351
Howard Hinnant59f73012013-11-13 00:39:22 +00002352#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002353
2354 _LIBCPP_INLINE_VISIBILITY
2355 __compressed_pair(const __compressed_pair& __p)
2356 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2357 is_nothrow_copy_constructible<_T2>::value)
2358 : base(__p) {}
2359
2360 _LIBCPP_INLINE_VISIBILITY
2361 __compressed_pair& operator=(const __compressed_pair& __p)
2362 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2363 is_nothrow_copy_assignable<_T2>::value)
2364 {
2365 base::operator=(__p);
2366 return *this;
2367 }
2368
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002371 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2372 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002373 : base(_VSTD::move(__p)) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002374
2375 _LIBCPP_INLINE_VISIBILITY
2376 __compressed_pair& operator=(__compressed_pair&& __p)
2377 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2378 is_nothrow_move_assignable<_T2>::value)
2379 {
2380 base::operator=(_VSTD::move(__p));
2381 return *this;
2382 }
Howard Hinnant83b1c052011-12-19 17:58:44 +00002383
Howard Hinnant59f73012013-11-13 00:39:22 +00002384#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002385
Howard Hinnant83b1c052011-12-19 17:58:44 +00002386#ifndef _LIBCPP_HAS_NO_VARIADICS
2387
2388 template <class... _Args1, class... _Args2>
2389 _LIBCPP_INLINE_VISIBILITY
2390 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2391 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002392 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002393 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2394 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2395 {}
2396
2397#endif // _LIBCPP_HAS_NO_VARIADICS
2398
Howard Hinnant719bda32011-05-28 14:41:13 +00002399 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2400 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401
Howard Hinnant719bda32011-05-28 14:41:13 +00002402 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2403 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404
Howard Hinnant719bda32011-05-28 14:41:13 +00002405 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2406 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002407 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002408 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409};
2410
2411template <class _T1, class _T2>
2412inline _LIBCPP_INLINE_VISIBILITY
2413void
2414swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002415 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002416 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417 {__x.swap(__y);}
2418
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002419// __same_or_less_cv_qualified
2420
2421template <class _Ptr1, class _Ptr2,
2422 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2423 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2424 >::value
2425 >
2426struct __same_or_less_cv_qualified_imp
2427 : is_convertible<_Ptr1, _Ptr2> {};
2428
2429template <class _Ptr1, class _Ptr2>
2430struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2431 : false_type {};
2432
Marshall Clowdf296162014-04-26 05:19:48 +00002433template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2434 is_same<_Ptr1, _Ptr2>::value ||
2435 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002436struct __same_or_less_cv_qualified
2437 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2438
2439template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002440struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002441 : false_type {};
2442
2443// default_delete
2444
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002446struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447{
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002448#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2449 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2450#else
2451 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2452#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453 template <class _Up>
2454 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002455 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2456 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457 {
2458 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002459 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460 delete __ptr;
2461 }
2462};
2463
2464template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002465struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002467public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002468#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2469 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2470#else
2471 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2472#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002473 template <class _Up>
2474 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002475 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002476 template <class _Up>
2477 _LIBCPP_INLINE_VISIBILITY
2478 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002479 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 {
2481 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002482 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 delete [] __ptr;
2484 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485};
2486
2487template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002488class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489{
2490public:
2491 typedef _Tp element_type;
2492 typedef _Dp deleter_type;
2493 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2494private:
2495 __compressed_pair<pointer, deleter_type> __ptr_;
2496
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002497#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 unique_ptr(unique_ptr&);
2499 template <class _Up, class _Ep>
2500 unique_ptr(unique_ptr<_Up, _Ep>&);
2501 unique_ptr& operator=(unique_ptr&);
2502 template <class _Up, class _Ep>
2503 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002504#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505
2506 struct __nat {int __for_bool_;};
2507
2508 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2509 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2510public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002511 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512 : __ptr_(pointer())
2513 {
2514 static_assert(!is_pointer<deleter_type>::value,
2515 "unique_ptr constructed with null function pointer deleter");
2516 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002517 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518 : __ptr_(pointer())
2519 {
2520 static_assert(!is_pointer<deleter_type>::value,
2521 "unique_ptr constructed with null function pointer deleter");
2522 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002523 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002524 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525 {
2526 static_assert(!is_pointer<deleter_type>::value,
2527 "unique_ptr constructed with null function pointer deleter");
2528 }
2529
Howard Hinnant74279a52010-09-04 23:28:19 +00002530#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2532 is_reference<deleter_type>::value,
2533 deleter_type,
2534 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002535 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536 : __ptr_(__p, __d) {}
2537
2538 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002539 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002540 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541 {
2542 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2543 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002544 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002545 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546 template <class _Up, class _Ep>
2547 _LIBCPP_INLINE_VISIBILITY
2548 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2549 typename enable_if
2550 <
2551 !is_array<_Up>::value &&
2552 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2553 is_convertible<_Ep, deleter_type>::value &&
2554 (
2555 !is_reference<deleter_type>::value ||
2556 is_same<deleter_type, _Ep>::value
2557 ),
2558 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002559 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002560 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561
2562 template <class _Up>
2563 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2564 typename enable_if<
2565 is_convertible<_Up*, _Tp*>::value &&
2566 is_same<_Dp, default_delete<_Tp> >::value,
2567 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002568 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 : __ptr_(__p.release())
2570 {
2571 }
2572
Howard Hinnant719bda32011-05-28 14:41:13 +00002573 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574 {
2575 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002576 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577 return *this;
2578 }
2579
2580 template <class _Up, class _Ep>
2581 _LIBCPP_INLINE_VISIBILITY
2582 typename enable_if
2583 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002584 !is_array<_Up>::value &&
2585 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2586 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587 unique_ptr&
2588 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002589 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 {
2591 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002592 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 return *this;
2594 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002595#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596
2597 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2598 {
2599 return __rv<unique_ptr>(*this);
2600 }
2601
2602 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002603 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604
2605 template <class _Up, class _Ep>
2606 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2607 {
2608 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002609 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610 return *this;
2611 }
2612
2613 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002614 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615
2616 template <class _Up>
2617 _LIBCPP_INLINE_VISIBILITY
2618 typename enable_if<
2619 is_convertible<_Up*, _Tp*>::value &&
2620 is_same<_Dp, default_delete<_Tp> >::value,
2621 unique_ptr&
2622 >::type
2623 operator=(auto_ptr<_Up> __p)
2624 {reset(__p.release()); return *this;}
2625
Howard Hinnant74279a52010-09-04 23:28:19 +00002626#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2628
Howard Hinnant719bda32011-05-28 14:41:13 +00002629 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630 {
2631 reset();
2632 return *this;
2633 }
2634
2635 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2636 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002637 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2638 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2639 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2640 {return __ptr_.second();}
2641 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2642 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002643 _LIBCPP_INLINE_VISIBILITY
2644 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2645 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646
Howard Hinnant719bda32011-05-28 14:41:13 +00002647 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648 {
2649 pointer __t = __ptr_.first();
2650 __ptr_.first() = pointer();
2651 return __t;
2652 }
2653
Howard Hinnant719bda32011-05-28 14:41:13 +00002654 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 {
2656 pointer __tmp = __ptr_.first();
2657 __ptr_.first() = __p;
2658 if (__tmp)
2659 __ptr_.second()(__tmp);
2660 }
2661
Howard Hinnant719bda32011-05-28 14:41:13 +00002662 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2663 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664};
2665
2666template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002667class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668{
2669public:
2670 typedef _Tp element_type;
2671 typedef _Dp deleter_type;
2672 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2673private:
2674 __compressed_pair<pointer, deleter_type> __ptr_;
2675
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002676#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 unique_ptr(unique_ptr&);
2678 template <class _Up>
2679 unique_ptr(unique_ptr<_Up>&);
2680 unique_ptr& operator=(unique_ptr&);
2681 template <class _Up>
2682 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002683#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684
2685 struct __nat {int __for_bool_;};
2686
2687 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2688 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2689public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002690 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 : __ptr_(pointer())
2692 {
2693 static_assert(!is_pointer<deleter_type>::value,
2694 "unique_ptr constructed with null function pointer deleter");
2695 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002696 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697 : __ptr_(pointer())
2698 {
2699 static_assert(!is_pointer<deleter_type>::value,
2700 "unique_ptr constructed with null function pointer deleter");
2701 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002702#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002703 template <class _Pp>
2704 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2705 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 : __ptr_(__p)
2707 {
2708 static_assert(!is_pointer<deleter_type>::value,
2709 "unique_ptr constructed with null function pointer deleter");
2710 }
2711
Logan Chiend435f8b2014-01-31 09:30:46 +00002712 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002713 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714 is_reference<deleter_type>::value,
2715 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002716 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2717 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002718 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 : __ptr_(__p, __d) {}
2720
2721 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2722 is_reference<deleter_type>::value,
2723 deleter_type,
2724 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002725 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726 : __ptr_(pointer(), __d) {}
2727
Logan Chiend435f8b2014-01-31 09:30:46 +00002728 template <class _Pp>
2729 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2730 typename remove_reference<deleter_type>::type&& __d,
2731 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002732 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002733 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 {
2735 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2736 }
2737
2738 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002739 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002740 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 {
2742 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2743 }
2744
Howard Hinnant719bda32011-05-28 14:41:13 +00002745 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002746 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747
Howard Hinnant719bda32011-05-28 14:41:13 +00002748 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749 {
2750 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002751 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 return *this;
2753 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002754
2755 template <class _Up, class _Ep>
2756 _LIBCPP_INLINE_VISIBILITY
2757 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2758 typename enable_if
2759 <
2760 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002761 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002762 && is_convertible<_Ep, deleter_type>::value &&
2763 (
2764 !is_reference<deleter_type>::value ||
2765 is_same<deleter_type, _Ep>::value
2766 ),
2767 __nat
2768 >::type = __nat()
2769 ) _NOEXCEPT
2770 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2771
2772
2773 template <class _Up, class _Ep>
2774 _LIBCPP_INLINE_VISIBILITY
2775 typename enable_if
2776 <
2777 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002778 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2779 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002780 unique_ptr&
2781 >::type
2782 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2783 {
2784 reset(__u.release());
2785 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2786 return *this;
2787 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002788#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789
2790 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2791 : __ptr_(__p)
2792 {
2793 static_assert(!is_pointer<deleter_type>::value,
2794 "unique_ptr constructed with null function pointer deleter");
2795 }
2796
2797 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002798 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799
2800 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002801 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802
2803 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2804 {
2805 return __rv<unique_ptr>(*this);
2806 }
2807
2808 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002809 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810
2811 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2812 {
2813 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002814 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815 return *this;
2816 }
2817
Howard Hinnant74279a52010-09-04 23:28:19 +00002818#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2820
Howard Hinnant719bda32011-05-28 14:41:13 +00002821 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 {
2823 reset();
2824 return *this;
2825 }
2826
2827 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2828 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002829 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2830 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2831 {return __ptr_.second();}
2832 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2833 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002834 _LIBCPP_INLINE_VISIBILITY
2835 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2836 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837
Howard Hinnant719bda32011-05-28 14:41:13 +00002838 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839 {
2840 pointer __t = __ptr_.first();
2841 __ptr_.first() = pointer();
2842 return __t;
2843 }
2844
Howard Hinnant74279a52010-09-04 23:28:19 +00002845#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002846 template <class _Pp>
2847 _LIBCPP_INLINE_VISIBILITY
2848 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2849 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 {
2851 pointer __tmp = __ptr_.first();
2852 __ptr_.first() = __p;
2853 if (__tmp)
2854 __ptr_.second()(__tmp);
2855 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002856 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 {
2858 pointer __tmp = __ptr_.first();
2859 __ptr_.first() = nullptr;
2860 if (__tmp)
2861 __ptr_.second()(__tmp);
2862 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002863 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 {
2865 pointer __tmp = __ptr_.first();
2866 __ptr_.first() = nullptr;
2867 if (__tmp)
2868 __ptr_.second()(__tmp);
2869 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002870#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2872 {
2873 pointer __tmp = __ptr_.first();
2874 __ptr_.first() = __p;
2875 if (__tmp)
2876 __ptr_.second()(__tmp);
2877 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002878#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879
2880 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2881private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002882
Howard Hinnant74279a52010-09-04 23:28:19 +00002883#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 template <class _Up>
2885 explicit unique_ptr(_Up);
2886 template <class _Up>
2887 unique_ptr(_Up __u,
2888 typename conditional<
2889 is_reference<deleter_type>::value,
2890 deleter_type,
2891 typename add_lvalue_reference<const deleter_type>::type>::type,
2892 typename enable_if
2893 <
2894 is_convertible<_Up, pointer>::value,
2895 __nat
2896 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00002897#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898};
2899
2900template <class _Tp, class _Dp>
2901inline _LIBCPP_INLINE_VISIBILITY
2902void
Howard Hinnant719bda32011-05-28 14:41:13 +00002903swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904
2905template <class _T1, class _D1, class _T2, class _D2>
2906inline _LIBCPP_INLINE_VISIBILITY
2907bool
2908operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2909
2910template <class _T1, class _D1, class _T2, class _D2>
2911inline _LIBCPP_INLINE_VISIBILITY
2912bool
2913operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2914
2915template <class _T1, class _D1, class _T2, class _D2>
2916inline _LIBCPP_INLINE_VISIBILITY
2917bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002918operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2919{
2920 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2921 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002922 typedef typename common_type<_P1, _P2>::type _Vp;
2923 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002924}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925
2926template <class _T1, class _D1, class _T2, class _D2>
2927inline _LIBCPP_INLINE_VISIBILITY
2928bool
2929operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2930
2931template <class _T1, class _D1, class _T2, class _D2>
2932inline _LIBCPP_INLINE_VISIBILITY
2933bool
2934operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2935
2936template <class _T1, class _D1, class _T2, class _D2>
2937inline _LIBCPP_INLINE_VISIBILITY
2938bool
2939operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2940
Howard Hinnantb17caf92012-02-21 21:02:58 +00002941template <class _T1, class _D1>
2942inline _LIBCPP_INLINE_VISIBILITY
2943bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002944operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002945{
2946 return !__x;
2947}
2948
2949template <class _T1, class _D1>
2950inline _LIBCPP_INLINE_VISIBILITY
2951bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002952operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002953{
2954 return !__x;
2955}
2956
2957template <class _T1, class _D1>
2958inline _LIBCPP_INLINE_VISIBILITY
2959bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002960operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002961{
2962 return static_cast<bool>(__x);
2963}
2964
2965template <class _T1, class _D1>
2966inline _LIBCPP_INLINE_VISIBILITY
2967bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002968operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002969{
2970 return static_cast<bool>(__x);
2971}
2972
2973template <class _T1, class _D1>
2974inline _LIBCPP_INLINE_VISIBILITY
2975bool
2976operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2977{
2978 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2979 return less<_P1>()(__x.get(), nullptr);
2980}
2981
2982template <class _T1, class _D1>
2983inline _LIBCPP_INLINE_VISIBILITY
2984bool
2985operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2986{
2987 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2988 return less<_P1>()(nullptr, __x.get());
2989}
2990
2991template <class _T1, class _D1>
2992inline _LIBCPP_INLINE_VISIBILITY
2993bool
2994operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2995{
2996 return nullptr < __x;
2997}
2998
2999template <class _T1, class _D1>
3000inline _LIBCPP_INLINE_VISIBILITY
3001bool
3002operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3003{
3004 return __x < nullptr;
3005}
3006
3007template <class _T1, class _D1>
3008inline _LIBCPP_INLINE_VISIBILITY
3009bool
3010operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3011{
3012 return !(nullptr < __x);
3013}
3014
3015template <class _T1, class _D1>
3016inline _LIBCPP_INLINE_VISIBILITY
3017bool
3018operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3019{
3020 return !(__x < nullptr);
3021}
3022
3023template <class _T1, class _D1>
3024inline _LIBCPP_INLINE_VISIBILITY
3025bool
3026operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3027{
3028 return !(__x < nullptr);
3029}
3030
3031template <class _T1, class _D1>
3032inline _LIBCPP_INLINE_VISIBILITY
3033bool
3034operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3035{
3036 return !(nullptr < __x);
3037}
3038
Howard Hinnant9e028f12012-05-01 15:37:54 +00003039#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3040
3041template <class _Tp, class _Dp>
3042inline _LIBCPP_INLINE_VISIBILITY
3043unique_ptr<_Tp, _Dp>
3044move(unique_ptr<_Tp, _Dp>& __t)
3045{
3046 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3047}
3048
3049#endif
3050
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003051#if _LIBCPP_STD_VER > 11
3052
3053template<class _Tp>
3054struct __unique_if
3055{
3056 typedef unique_ptr<_Tp> __unique_single;
3057};
3058
3059template<class _Tp>
3060struct __unique_if<_Tp[]>
3061{
3062 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3063};
3064
3065template<class _Tp, size_t _Np>
3066struct __unique_if<_Tp[_Np]>
3067{
3068 typedef void __unique_array_known_bound;
3069};
3070
3071template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003072inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003073typename __unique_if<_Tp>::__unique_single
3074make_unique(_Args&&... __args)
3075{
3076 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3077}
3078
3079template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003080inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003081typename __unique_if<_Tp>::__unique_array_unknown_bound
3082make_unique(size_t __n)
3083{
3084 typedef typename remove_extent<_Tp>::type _Up;
3085 return unique_ptr<_Tp>(new _Up[__n]());
3086}
3087
3088template<class _Tp, class... _Args>
3089 typename __unique_if<_Tp>::__unique_array_known_bound
3090 make_unique(_Args&&...) = delete;
3091
3092#endif // _LIBCPP_STD_VER > 11
3093
Howard Hinnant944510a2011-06-14 19:58:17 +00003094template <class _Tp> struct hash;
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003095
Howard Hinnant6a855442013-07-03 17:39:28 +00003096template <class _Size>
3097inline _LIBCPP_INLINE_VISIBILITY
3098_Size
3099__loadword(const void* __p)
3100{
3101 _Size __r;
3102 std::memcpy(&__r, __p, sizeof(__r));
3103 return __r;
3104}
3105
Howard Hinnantf1973402011-12-10 20:28:56 +00003106// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3107// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3108// multiplication, which can be very slow on 32-bit systems.
Howard Hinnantad71c232011-12-05 00:08:45 +00003109template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantf1973402011-12-10 20:28:56 +00003110struct __murmur2_or_cityhash;
Howard Hinnantad71c232011-12-05 00:08:45 +00003111
3112template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003113struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnantad71c232011-12-05 00:08:45 +00003114{
3115 _Size operator()(const void* __key, _Size __len);
3116};
3117
Howard Hinnantf1973402011-12-10 20:28:56 +00003118// murmur2
Howard Hinnantad71c232011-12-05 00:08:45 +00003119template <class _Size>
3120_Size
Howard Hinnantf1973402011-12-10 20:28:56 +00003121__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnantad71c232011-12-05 00:08:45 +00003122{
3123 const _Size __m = 0x5bd1e995;
3124 const _Size __r = 24;
3125 _Size __h = __len;
3126 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3127 for (; __len >= 4; __data += 4, __len -= 4)
3128 {
Howard Hinnant6a855442013-07-03 17:39:28 +00003129 _Size __k = __loadword<_Size>(__data);
Howard Hinnantad71c232011-12-05 00:08:45 +00003130 __k *= __m;
3131 __k ^= __k >> __r;
3132 __k *= __m;
3133 __h *= __m;
3134 __h ^= __k;
3135 }
3136 switch (__len)
3137 {
3138 case 3:
3139 __h ^= __data[2] << 16;
3140 case 2:
3141 __h ^= __data[1] << 8;
3142 case 1:
3143 __h ^= __data[0];
3144 __h *= __m;
3145 }
3146 __h ^= __h >> 13;
3147 __h *= __m;
3148 __h ^= __h >> 15;
3149 return __h;
3150}
3151
3152template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003153struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnantad71c232011-12-05 00:08:45 +00003154{
3155 _Size operator()(const void* __key, _Size __len);
Howard Hinnantf1973402011-12-10 20:28:56 +00003156
3157 private:
3158 // Some primes between 2^63 and 2^64.
3159 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3160 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3161 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3162 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3163
3164 static _Size __rotate(_Size __val, int __shift) {
3165 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3166 }
3167
3168 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3169 return (__val >> __shift) | (__val << (64 - __shift));
3170 }
3171
3172 static _Size __shift_mix(_Size __val) {
3173 return __val ^ (__val >> 47);
3174 }
3175
3176 static _Size __hash_len_16(_Size __u, _Size __v) {
3177 const _Size __mul = 0x9ddfea08eb382d69ULL;
3178 _Size __a = (__u ^ __v) * __mul;
3179 __a ^= (__a >> 47);
3180 _Size __b = (__v ^ __a) * __mul;
3181 __b ^= (__b >> 47);
3182 __b *= __mul;
3183 return __b;
3184 }
3185
3186 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3187 if (__len > 8) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003188 const _Size __a = __loadword<_Size>(__s);
3189 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003190 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3191 }
3192 if (__len >= 4) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003193 const uint32_t __a = __loadword<uint32_t>(__s);
3194 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantf1973402011-12-10 20:28:56 +00003195 return __hash_len_16(__len + (__a << 3), __b);
3196 }
3197 if (__len > 0) {
3198 const unsigned char __a = __s[0];
3199 const unsigned char __b = __s[__len >> 1];
3200 const unsigned char __c = __s[__len - 1];
3201 const uint32_t __y = static_cast<uint32_t>(__a) +
3202 (static_cast<uint32_t>(__b) << 8);
3203 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3204 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3205 }
3206 return __k2;
3207 }
3208
3209 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003210 const _Size __a = __loadword<_Size>(__s) * __k1;
3211 const _Size __b = __loadword<_Size>(__s + 8);
3212 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3213 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003214 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3215 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3216 }
3217
3218 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3219 // Callers do best to use "random-looking" values for a and b.
3220 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3221 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3222 __a += __w;
3223 __b = __rotate(__b + __a + __z, 21);
3224 const _Size __c = __a;
3225 __a += __x;
3226 __a += __y;
3227 __b += __rotate(__a, 44);
3228 return pair<_Size, _Size>(__a + __z, __b + __c);
3229 }
3230
3231 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3232 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3233 const char* __s, _Size __a, _Size __b) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003234 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3235 __loadword<_Size>(__s + 8),
3236 __loadword<_Size>(__s + 16),
3237 __loadword<_Size>(__s + 24),
Howard Hinnantf1973402011-12-10 20:28:56 +00003238 __a,
3239 __b);
3240 }
3241
3242 // Return an 8-byte hash for 33 to 64 bytes.
3243 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003244 _Size __z = __loadword<_Size>(__s + 24);
3245 _Size __a = __loadword<_Size>(__s) +
3246 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003247 _Size __b = __rotate(__a + __z, 52);
3248 _Size __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003249 __a += __loadword<_Size>(__s + 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003250 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003251 __a += __loadword<_Size>(__s + 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003252 _Size __vf = __a + __z;
3253 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant6a855442013-07-03 17:39:28 +00003254 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3255 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003256 __b = __rotate(__a + __z, 52);
3257 __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003258 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantf1973402011-12-10 20:28:56 +00003259 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003260 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003261 _Size __wf = __a + __z;
3262 _Size __ws = __b + __rotate(__a, 31) + __c;
3263 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3264 return __shift_mix(__r * __k0 + __vs) * __k2;
3265 }
Howard Hinnantad71c232011-12-05 00:08:45 +00003266};
3267
Howard Hinnantf1973402011-12-10 20:28:56 +00003268// cityhash64
Howard Hinnantad71c232011-12-05 00:08:45 +00003269template <class _Size>
3270_Size
Howard Hinnantf1973402011-12-10 20:28:56 +00003271__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnantad71c232011-12-05 00:08:45 +00003272{
Howard Hinnantf1973402011-12-10 20:28:56 +00003273 const char* __s = static_cast<const char*>(__key);
3274 if (__len <= 32) {
3275 if (__len <= 16) {
3276 return __hash_len_0_to_16(__s, __len);
3277 } else {
3278 return __hash_len_17_to_32(__s, __len);
Howard Hinnantad71c232011-12-05 00:08:45 +00003279 }
Howard Hinnantf1973402011-12-10 20:28:56 +00003280 } else if (__len <= 64) {
3281 return __hash_len_33_to_64(__s, __len);
3282 }
3283
3284 // For strings over 64 bytes we hash the end first, and then as we
3285 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant6a855442013-07-03 17:39:28 +00003286 _Size __x = __loadword<_Size>(__s + __len - 40);
3287 _Size __y = __loadword<_Size>(__s + __len - 16) +
3288 __loadword<_Size>(__s + __len - 56);
3289 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3290 __loadword<_Size>(__s + __len - 24));
Howard Hinnantf1973402011-12-10 20:28:56 +00003291 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3292 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant6a855442013-07-03 17:39:28 +00003293 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantf1973402011-12-10 20:28:56 +00003294
3295 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3296 __len = (__len - 1) & ~static_cast<_Size>(63);
3297 do {
Howard Hinnant6a855442013-07-03 17:39:28 +00003298 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3299 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantf1973402011-12-10 20:28:56 +00003300 __x ^= __w.second;
Howard Hinnant6a855442013-07-03 17:39:28 +00003301 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantf1973402011-12-10 20:28:56 +00003302 __z = __rotate(__z + __w.first, 33) * __k1;
3303 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3304 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant6a855442013-07-03 17:39:28 +00003305 __y + __loadword<_Size>(__s + 16));
Howard Hinnantf1973402011-12-10 20:28:56 +00003306 std::swap(__z, __x);
3307 __s += 64;
3308 __len -= 64;
3309 } while (__len != 0);
3310 return __hash_len_16(
3311 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3312 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnantad71c232011-12-05 00:08:45 +00003313}
3314
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003315template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3316struct __scalar_hash;
3317
3318template <class _Tp>
3319struct __scalar_hash<_Tp, 0>
3320 : public unary_function<_Tp, size_t>
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003321{
Howard Hinnant756c69b2010-09-22 16:48:34 +00003322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003323 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003324 {
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003325 union
3326 {
3327 _Tp __t;
3328 size_t __a;
3329 } __u;
3330 __u.__a = 0;
3331 __u.__t = __v;
3332 return __u.__a;
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003333 }
3334};
3335
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003336template <class _Tp>
3337struct __scalar_hash<_Tp, 1>
3338 : public unary_function<_Tp, size_t>
3339{
3340 _LIBCPP_INLINE_VISIBILITY
3341 size_t operator()(_Tp __v) const _NOEXCEPT
3342 {
3343 union
3344 {
3345 _Tp __t;
3346 size_t __a;
3347 } __u;
3348 __u.__t = __v;
3349 return __u.__a;
3350 }
3351};
3352
3353template <class _Tp>
3354struct __scalar_hash<_Tp, 2>
3355 : public unary_function<_Tp, size_t>
3356{
3357 _LIBCPP_INLINE_VISIBILITY
3358 size_t operator()(_Tp __v) const _NOEXCEPT
3359 {
3360 union
3361 {
3362 _Tp __t;
3363 struct
3364 {
3365 size_t __a;
3366 size_t __b;
3367 };
3368 } __u;
3369 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003370 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003371 }
3372};
3373
3374template <class _Tp>
3375struct __scalar_hash<_Tp, 3>
3376 : public unary_function<_Tp, size_t>
3377{
3378 _LIBCPP_INLINE_VISIBILITY
3379 size_t operator()(_Tp __v) const _NOEXCEPT
3380 {
3381 union
3382 {
3383 _Tp __t;
3384 struct
3385 {
3386 size_t __a;
3387 size_t __b;
3388 size_t __c;
3389 };
3390 } __u;
3391 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003392 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003393 }
3394};
3395
3396template <class _Tp>
3397struct __scalar_hash<_Tp, 4>
3398 : public unary_function<_Tp, size_t>
3399{
3400 _LIBCPP_INLINE_VISIBILITY
3401 size_t operator()(_Tp __v) const _NOEXCEPT
3402 {
3403 union
3404 {
3405 _Tp __t;
3406 struct
3407 {
3408 size_t __a;
3409 size_t __b;
3410 size_t __c;
3411 size_t __d;
3412 };
3413 } __u;
3414 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003415 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003416 }
3417};
3418
3419template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003420struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant9fa30202012-07-30 01:40:57 +00003421 : public unary_function<_Tp*, size_t>
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003422{
Howard Hinnant9fa30202012-07-30 01:40:57 +00003423 _LIBCPP_INLINE_VISIBILITY
3424 size_t operator()(_Tp* __v) const _NOEXCEPT
3425 {
3426 union
3427 {
3428 _Tp* __t;
3429 size_t __a;
3430 } __u;
3431 __u.__t = __v;
3432 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3433 }
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003434};
3435
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003436template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003437struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003438{
3439 typedef unique_ptr<_Tp, _Dp> argument_type;
3440 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003442 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003443 {
3444 typedef typename argument_type::pointer pointer;
3445 return hash<pointer>()(__ptr.get());
3446 }
3447};
3448
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449struct __destruct_n
3450{
3451private:
3452 size_t size;
3453
3454 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003455 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3457
3458 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003459 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460 {}
3461
Howard Hinnant719bda32011-05-28 14:41:13 +00003462 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003464 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465 {}
3466
Howard Hinnant719bda32011-05-28 14:41:13 +00003467 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003469 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003470 {}
3471public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003472 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3473 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474
3475 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003476 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003477 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003478
3479 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003480 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003481 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482
3483 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003484 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003485 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486};
3487
3488template <class _Alloc>
3489class __allocator_destructor
3490{
3491 typedef allocator_traits<_Alloc> __alloc_traits;
3492public:
3493 typedef typename __alloc_traits::pointer pointer;
3494 typedef typename __alloc_traits::size_type size_type;
3495private:
3496 _Alloc& __alloc_;
3497 size_type __s_;
3498public:
3499 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003500 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003503 void operator()(pointer __p) _NOEXCEPT
3504 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505};
3506
3507template <class _InputIterator, class _ForwardIterator>
3508_ForwardIterator
3509uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3510{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003512#ifndef _LIBCPP_NO_EXCEPTIONS
3513 _ForwardIterator __s = __r;
3514 try
3515 {
3516#endif
3517 for (; __f != __l; ++__f, ++__r)
3518 ::new(&*__r) value_type(*__f);
3519#ifndef _LIBCPP_NO_EXCEPTIONS
3520 }
3521 catch (...)
3522 {
3523 for (; __s != __r; ++__s)
3524 __s->~value_type();
3525 throw;
3526 }
3527#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528 return __r;
3529}
3530
3531template <class _InputIterator, class _Size, class _ForwardIterator>
3532_ForwardIterator
3533uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3534{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003536#ifndef _LIBCPP_NO_EXCEPTIONS
3537 _ForwardIterator __s = __r;
3538 try
3539 {
3540#endif
3541 for (; __n > 0; ++__f, ++__r, --__n)
3542 ::new(&*__r) value_type(*__f);
3543#ifndef _LIBCPP_NO_EXCEPTIONS
3544 }
3545 catch (...)
3546 {
3547 for (; __s != __r; ++__s)
3548 __s->~value_type();
3549 throw;
3550 }
3551#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552 return __r;
3553}
3554
3555template <class _ForwardIterator, class _Tp>
3556void
3557uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3558{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003560#ifndef _LIBCPP_NO_EXCEPTIONS
3561 _ForwardIterator __s = __f;
3562 try
3563 {
3564#endif
3565 for (; __f != __l; ++__f)
3566 ::new(&*__f) value_type(__x);
3567#ifndef _LIBCPP_NO_EXCEPTIONS
3568 }
3569 catch (...)
3570 {
3571 for (; __s != __f; ++__s)
3572 __s->~value_type();
3573 throw;
3574 }
3575#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576}
3577
3578template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003579_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3581{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003583#ifndef _LIBCPP_NO_EXCEPTIONS
3584 _ForwardIterator __s = __f;
3585 try
3586 {
3587#endif
3588 for (; __n > 0; ++__f, --__n)
3589 ::new(&*__f) value_type(__x);
3590#ifndef _LIBCPP_NO_EXCEPTIONS
3591 }
3592 catch (...)
3593 {
3594 for (; __s != __f; ++__s)
3595 __s->~value_type();
3596 throw;
3597 }
3598#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003599 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600}
3601
Howard Hinnant756c69b2010-09-22 16:48:34 +00003602class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603 : public std::exception
3604{
3605public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003606 virtual ~bad_weak_ptr() _NOEXCEPT;
3607 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608};
3609
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003610template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003612class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613{
3614 __shared_count(const __shared_count&);
3615 __shared_count& operator=(const __shared_count&);
3616
3617protected:
3618 long __shared_owners_;
3619 virtual ~__shared_count();
3620private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003621 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622
3623public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003625 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626 : __shared_owners_(__refs) {}
3627
Howard Hinnant719bda32011-05-28 14:41:13 +00003628 void __add_shared() _NOEXCEPT;
3629 bool __release_shared() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003631 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632};
3633
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003634class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635 : private __shared_count
3636{
3637 long __shared_weak_owners_;
3638
3639public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003641 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642 : __shared_count(__refs),
3643 __shared_weak_owners_(__refs) {}
3644protected:
3645 virtual ~__shared_weak_count();
3646
3647public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003648 void __add_shared() _NOEXCEPT;
3649 void __add_weak() _NOEXCEPT;
3650 void __release_shared() _NOEXCEPT;
3651 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003653 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3654 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655
Howard Hinnant807d6332013-02-25 15:50:36 +00003656 // Define the function out only if we build static libc++ without RTTI.
3657 // Otherwise we may break clients who need to compile their projects with
3658 // -fno-rtti and yet link against a libc++.dylib compiled
3659 // without -fno-rtti.
3660#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003661 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003662#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003664 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665};
3666
3667template <class _Tp, class _Dp, class _Alloc>
3668class __shared_ptr_pointer
3669 : public __shared_weak_count
3670{
3671 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3672public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003675 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003676
Howard Hinnant72f73582010-08-11 17:04:31 +00003677#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003678 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003679#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680
3681private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003682 virtual void __on_zero_shared() _NOEXCEPT;
3683 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684};
3685
Howard Hinnant72f73582010-08-11 17:04:31 +00003686#ifndef _LIBCPP_NO_RTTI
3687
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688template <class _Tp, class _Dp, class _Alloc>
3689const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003690__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003692 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693}
3694
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003695#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003696
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697template <class _Tp, class _Dp, class _Alloc>
3698void
Howard Hinnant719bda32011-05-28 14:41:13 +00003699__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700{
3701 __data_.first().second()(__data_.first().first());
3702 __data_.first().second().~_Dp();
3703}
3704
3705template <class _Tp, class _Dp, class _Alloc>
3706void
Howard Hinnant719bda32011-05-28 14:41:13 +00003707__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003709 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3710 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003711 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3712
Eric Fiselierf8898c82015-02-05 23:01:40 +00003713 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003715 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716}
3717
3718template <class _Tp, class _Alloc>
3719class __shared_ptr_emplace
3720 : public __shared_weak_count
3721{
3722 __compressed_pair<_Alloc, _Tp> __data_;
3723public:
3724#ifndef _LIBCPP_HAS_NO_VARIADICS
3725
Howard Hinnant756c69b2010-09-22 16:48:34 +00003726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003728 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729
3730 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003733 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3734 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735
3736#else // _LIBCPP_HAS_NO_VARIADICS
3737
Howard Hinnant756c69b2010-09-22 16:48:34 +00003738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739 __shared_ptr_emplace(_Alloc __a)
3740 : __data_(__a) {}
3741
3742 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3745 : __data_(__a, _Tp(__a0)) {}
3746
3747 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3750 : __data_(__a, _Tp(__a0, __a1)) {}
3751
3752 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3755 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3756
3757#endif // _LIBCPP_HAS_NO_VARIADICS
3758
3759private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003760 virtual void __on_zero_shared() _NOEXCEPT;
3761 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003764 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765};
3766
3767template <class _Tp, class _Alloc>
3768void
Howard Hinnant719bda32011-05-28 14:41:13 +00003769__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770{
3771 __data_.second().~_Tp();
3772}
3773
3774template <class _Tp, class _Alloc>
3775void
Howard Hinnant719bda32011-05-28 14:41:13 +00003776__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003778 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3779 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003780 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003781 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003782 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003783 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784}
3785
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003786template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787
3788template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003789class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003791public:
3792 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793private:
3794 element_type* __ptr_;
3795 __shared_weak_count* __cntrl_;
3796
3797 struct __nat {int __for_bool_;};
3798public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003799 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3800 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003801 template<class _Yp>
3802 explicit shared_ptr(_Yp* __p,
3803 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3804 template<class _Yp, class _Dp>
3805 shared_ptr(_Yp* __p, _Dp __d,
3806 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3807 template<class _Yp, class _Dp, class _Alloc>
3808 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3809 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3811 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant719bda32011-05-28 14:41:13 +00003812 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3813 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814 template<class _Yp>
3815 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003816 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3817 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003818#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant719bda32011-05-28 14:41:13 +00003819 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003821 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3822 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003823#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003825 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003826#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003827 template<class _Yp>
3828 shared_ptr(auto_ptr<_Yp>&& __r,
3829 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003830#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003831 template<class _Yp>
3832 shared_ptr(auto_ptr<_Yp> __r,
3833 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003835#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003836 template <class _Yp, class _Dp>
3837 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3838 typename enable_if
3839 <
3840 !is_lvalue_reference<_Dp>::value &&
3841 !is_array<_Yp>::value &&
3842 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3843 __nat
3844 >::type = __nat());
3845 template <class _Yp, class _Dp>
3846 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3847 typename enable_if
3848 <
3849 is_lvalue_reference<_Dp>::value &&
3850 !is_array<_Yp>::value &&
3851 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3852 __nat
3853 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003854#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003855 template <class _Yp, class _Dp>
3856 shared_ptr(unique_ptr<_Yp, _Dp>,
3857 typename enable_if
3858 <
3859 !is_lvalue_reference<_Dp>::value &&
3860 !is_array<_Yp>::value &&
3861 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3862 __nat
3863 >::type = __nat());
3864 template <class _Yp, class _Dp>
3865 shared_ptr(unique_ptr<_Yp, _Dp>,
3866 typename enable_if
3867 <
3868 is_lvalue_reference<_Dp>::value &&
3869 !is_array<_Yp>::value &&
3870 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3871 __nat
3872 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003873#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874
3875 ~shared_ptr();
3876
Howard Hinnant719bda32011-05-28 14:41:13 +00003877 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003878 template<class _Yp>
3879 typename enable_if
3880 <
3881 is_convertible<_Yp*, element_type*>::value,
3882 shared_ptr&
3883 >::type
3884 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003885#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant719bda32011-05-28 14:41:13 +00003886 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003887 template<class _Yp>
3888 typename enable_if
3889 <
3890 is_convertible<_Yp*, element_type*>::value,
3891 shared_ptr<_Tp>&
3892 >::type
3893 operator=(shared_ptr<_Yp>&& __r);
3894 template<class _Yp>
3895 typename enable_if
3896 <
3897 !is_array<_Yp>::value &&
3898 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003899 shared_ptr
3900 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003901 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003902#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003903 template<class _Yp>
3904 typename enable_if
3905 <
3906 !is_array<_Yp>::value &&
3907 is_convertible<_Yp*, element_type*>::value,
3908 shared_ptr&
3909 >::type
3910 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003912 template <class _Yp, class _Dp>
3913 typename enable_if
3914 <
3915 !is_array<_Yp>::value &&
3916 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3917 shared_ptr&
3918 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003919#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003920 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003921#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003922 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003923#endif
3924
Howard Hinnant719bda32011-05-28 14:41:13 +00003925 void swap(shared_ptr& __r) _NOEXCEPT;
3926 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003927 template<class _Yp>
3928 typename enable_if
3929 <
3930 is_convertible<_Yp*, element_type*>::value,
3931 void
3932 >::type
3933 reset(_Yp* __p);
3934 template<class _Yp, class _Dp>
3935 typename enable_if
3936 <
3937 is_convertible<_Yp*, element_type*>::value,
3938 void
3939 >::type
3940 reset(_Yp* __p, _Dp __d);
3941 template<class _Yp, class _Dp, class _Alloc>
3942 typename enable_if
3943 <
3944 is_convertible<_Yp*, element_type*>::value,
3945 void
3946 >::type
3947 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948
Howard Hinnant756c69b2010-09-22 16:48:34 +00003949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003950 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003952 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3953 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003955 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003957 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003959 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003961 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003962 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003964 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003966 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003968 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003970 _LIBCPP_INLINE_VISIBILITY
3971 bool
3972 __owner_equivalent(const shared_ptr& __p) const
3973 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974
Howard Hinnant72f73582010-08-11 17:04:31 +00003975#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003978 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003980#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981
3982#ifndef _LIBCPP_HAS_NO_VARIADICS
3983
3984 template<class ..._Args>
3985 static
3986 shared_ptr<_Tp>
3987 make_shared(_Args&& ...__args);
3988
3989 template<class _Alloc, class ..._Args>
3990 static
3991 shared_ptr<_Tp>
3992 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3993
3994#else // _LIBCPP_HAS_NO_VARIADICS
3995
3996 static shared_ptr<_Tp> make_shared();
3997
3998 template<class _A0>
3999 static shared_ptr<_Tp> make_shared(_A0&);
4000
4001 template<class _A0, class _A1>
4002 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4003
4004 template<class _A0, class _A1, class _A2>
4005 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4006
4007 template<class _Alloc>
4008 static shared_ptr<_Tp>
4009 allocate_shared(const _Alloc& __a);
4010
4011 template<class _Alloc, class _A0>
4012 static shared_ptr<_Tp>
4013 allocate_shared(const _Alloc& __a, _A0& __a0);
4014
4015 template<class _Alloc, class _A0, class _A1>
4016 static shared_ptr<_Tp>
4017 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4018
4019 template<class _Alloc, class _A0, class _A1, class _A2>
4020 static shared_ptr<_Tp>
4021 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4022
4023#endif // _LIBCPP_HAS_NO_VARIADICS
4024
4025private:
4026
4027 template <class _Yp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004028 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029 void
Howard Hinnant719bda32011-05-28 14:41:13 +00004030 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031 {
4032 if (__e)
4033 __e->__weak_this_ = *this;
4034 }
4035
Howard Hinnant756c69b2010-09-22 16:48:34 +00004036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004037 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004039 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4040 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041};
4042
4043template<class _Tp>
4044inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004045_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004046shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047 : __ptr_(0),
4048 __cntrl_(0)
4049{
4050}
4051
4052template<class _Tp>
4053inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004054_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004055shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056 : __ptr_(0),
4057 __cntrl_(0)
4058{
4059}
4060
4061template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004062template<class _Yp>
4063shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4064 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065 : __ptr_(__p)
4066{
4067 unique_ptr<_Yp> __hold(__p);
4068 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4069 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4070 __hold.release();
4071 __enable_weak_this(__p);
4072}
4073
4074template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004075template<class _Yp, class _Dp>
4076shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4077 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078 : __ptr_(__p)
4079{
4080#ifndef _LIBCPP_NO_EXCEPTIONS
4081 try
4082 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004083#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4085 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4086 __enable_weak_this(__p);
4087#ifndef _LIBCPP_NO_EXCEPTIONS
4088 }
4089 catch (...)
4090 {
4091 __d(__p);
4092 throw;
4093 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004094#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095}
4096
4097template<class _Tp>
4098template<class _Dp>
4099shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4100 : __ptr_(0)
4101{
4102#ifndef _LIBCPP_NO_EXCEPTIONS
4103 try
4104 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004105#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4107 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4108#ifndef _LIBCPP_NO_EXCEPTIONS
4109 }
4110 catch (...)
4111 {
4112 __d(__p);
4113 throw;
4114 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004115#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116}
4117
4118template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004119template<class _Yp, class _Dp, class _Alloc>
4120shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4121 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122 : __ptr_(__p)
4123{
4124#ifndef _LIBCPP_NO_EXCEPTIONS
4125 try
4126 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004127#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004129 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130 typedef __allocator_destructor<_A2> _D2;
4131 _A2 __a2(__a);
4132 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004133 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4134 _CntrlBlk(__p, __d, __a);
4135 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136 __enable_weak_this(__p);
4137#ifndef _LIBCPP_NO_EXCEPTIONS
4138 }
4139 catch (...)
4140 {
4141 __d(__p);
4142 throw;
4143 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004144#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145}
4146
4147template<class _Tp>
4148template<class _Dp, class _Alloc>
4149shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4150 : __ptr_(0)
4151{
4152#ifndef _LIBCPP_NO_EXCEPTIONS
4153 try
4154 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004155#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004157 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158 typedef __allocator_destructor<_A2> _D2;
4159 _A2 __a2(__a);
4160 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004161 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4162 _CntrlBlk(__p, __d, __a);
4163 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004164#ifndef _LIBCPP_NO_EXCEPTIONS
4165 }
4166 catch (...)
4167 {
4168 __d(__p);
4169 throw;
4170 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004171#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172}
4173
4174template<class _Tp>
4175template<class _Yp>
4176inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004177shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178 : __ptr_(__p),
4179 __cntrl_(__r.__cntrl_)
4180{
4181 if (__cntrl_)
4182 __cntrl_->__add_shared();
4183}
4184
4185template<class _Tp>
4186inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004187shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188 : __ptr_(__r.__ptr_),
4189 __cntrl_(__r.__cntrl_)
4190{
4191 if (__cntrl_)
4192 __cntrl_->__add_shared();
4193}
4194
4195template<class _Tp>
4196template<class _Yp>
4197inline _LIBCPP_INLINE_VISIBILITY
4198shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4199 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004200 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201 : __ptr_(__r.__ptr_),
4202 __cntrl_(__r.__cntrl_)
4203{
4204 if (__cntrl_)
4205 __cntrl_->__add_shared();
4206}
4207
Howard Hinnant74279a52010-09-04 23:28:19 +00004208#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209
4210template<class _Tp>
4211inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004212shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213 : __ptr_(__r.__ptr_),
4214 __cntrl_(__r.__cntrl_)
4215{
4216 __r.__ptr_ = 0;
4217 __r.__cntrl_ = 0;
4218}
4219
4220template<class _Tp>
4221template<class _Yp>
4222inline _LIBCPP_INLINE_VISIBILITY
4223shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4224 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004225 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226 : __ptr_(__r.__ptr_),
4227 __cntrl_(__r.__cntrl_)
4228{
4229 __r.__ptr_ = 0;
4230 __r.__cntrl_ = 0;
4231}
4232
Howard Hinnant74279a52010-09-04 23:28:19 +00004233#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234
4235template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004236template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004237#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004238shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004240shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004242 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243 : __ptr_(__r.get())
4244{
4245 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4246 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4247 __enable_weak_this(__r.get());
4248 __r.release();
4249}
4250
4251template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004252template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004253#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4255#else
4256shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4257#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004258 typename enable_if
4259 <
4260 !is_lvalue_reference<_Dp>::value &&
4261 !is_array<_Yp>::value &&
4262 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4263 __nat
4264 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265 : __ptr_(__r.get())
4266{
4267 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4268 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4269 __enable_weak_this(__r.get());
4270 __r.release();
4271}
4272
4273template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004274template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004275#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4277#else
4278shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4279#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004280 typename enable_if
4281 <
4282 is_lvalue_reference<_Dp>::value &&
4283 !is_array<_Yp>::value &&
4284 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4285 __nat
4286 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287 : __ptr_(__r.get())
4288{
4289 typedef __shared_ptr_pointer<_Yp*,
4290 reference_wrapper<typename remove_reference<_Dp>::type>,
4291 allocator<_Yp> > _CntrlBlk;
4292 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4293 __enable_weak_this(__r.get());
4294 __r.release();
4295}
4296
4297#ifndef _LIBCPP_HAS_NO_VARIADICS
4298
4299template<class _Tp>
4300template<class ..._Args>
4301shared_ptr<_Tp>
4302shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4303{
4304 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4305 typedef allocator<_CntrlBlk> _A2;
4306 typedef __allocator_destructor<_A2> _D2;
4307 _A2 __a2;
4308 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004309 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310 shared_ptr<_Tp> __r;
4311 __r.__ptr_ = __hold2.get()->get();
4312 __r.__cntrl_ = __hold2.release();
4313 __r.__enable_weak_this(__r.__ptr_);
4314 return __r;
4315}
4316
4317template<class _Tp>
4318template<class _Alloc, class ..._Args>
4319shared_ptr<_Tp>
4320shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4321{
4322 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004323 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324 typedef __allocator_destructor<_A2> _D2;
4325 _A2 __a2(__a);
4326 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004327 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4328 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329 shared_ptr<_Tp> __r;
4330 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004331 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332 __r.__enable_weak_this(__r.__ptr_);
4333 return __r;
4334}
4335
4336#else // _LIBCPP_HAS_NO_VARIADICS
4337
4338template<class _Tp>
4339shared_ptr<_Tp>
4340shared_ptr<_Tp>::make_shared()
4341{
4342 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4343 typedef allocator<_CntrlBlk> _Alloc2;
4344 typedef __allocator_destructor<_Alloc2> _D2;
4345 _Alloc2 __alloc2;
4346 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4347 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4348 shared_ptr<_Tp> __r;
4349 __r.__ptr_ = __hold2.get()->get();
4350 __r.__cntrl_ = __hold2.release();
4351 __r.__enable_weak_this(__r.__ptr_);
4352 return __r;
4353}
4354
4355template<class _Tp>
4356template<class _A0>
4357shared_ptr<_Tp>
4358shared_ptr<_Tp>::make_shared(_A0& __a0)
4359{
4360 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4361 typedef allocator<_CntrlBlk> _Alloc2;
4362 typedef __allocator_destructor<_Alloc2> _D2;
4363 _Alloc2 __alloc2;
4364 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4365 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4366 shared_ptr<_Tp> __r;
4367 __r.__ptr_ = __hold2.get()->get();
4368 __r.__cntrl_ = __hold2.release();
4369 __r.__enable_weak_this(__r.__ptr_);
4370 return __r;
4371}
4372
4373template<class _Tp>
4374template<class _A0, class _A1>
4375shared_ptr<_Tp>
4376shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4377{
4378 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4379 typedef allocator<_CntrlBlk> _Alloc2;
4380 typedef __allocator_destructor<_Alloc2> _D2;
4381 _Alloc2 __alloc2;
4382 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4383 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4384 shared_ptr<_Tp> __r;
4385 __r.__ptr_ = __hold2.get()->get();
4386 __r.__cntrl_ = __hold2.release();
4387 __r.__enable_weak_this(__r.__ptr_);
4388 return __r;
4389}
4390
4391template<class _Tp>
4392template<class _A0, class _A1, class _A2>
4393shared_ptr<_Tp>
4394shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4395{
4396 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4397 typedef allocator<_CntrlBlk> _Alloc2;
4398 typedef __allocator_destructor<_Alloc2> _D2;
4399 _Alloc2 __alloc2;
4400 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4401 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4402 shared_ptr<_Tp> __r;
4403 __r.__ptr_ = __hold2.get()->get();
4404 __r.__cntrl_ = __hold2.release();
4405 __r.__enable_weak_this(__r.__ptr_);
4406 return __r;
4407}
4408
4409template<class _Tp>
4410template<class _Alloc>
4411shared_ptr<_Tp>
4412shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4413{
4414 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004415 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416 typedef __allocator_destructor<_Alloc2> _D2;
4417 _Alloc2 __alloc2(__a);
4418 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004419 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4420 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421 shared_ptr<_Tp> __r;
4422 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004423 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424 __r.__enable_weak_this(__r.__ptr_);
4425 return __r;
4426}
4427
4428template<class _Tp>
4429template<class _Alloc, class _A0>
4430shared_ptr<_Tp>
4431shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4432{
4433 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004434 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435 typedef __allocator_destructor<_Alloc2> _D2;
4436 _Alloc2 __alloc2(__a);
4437 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004438 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4439 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440 shared_ptr<_Tp> __r;
4441 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004442 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443 __r.__enable_weak_this(__r.__ptr_);
4444 return __r;
4445}
4446
4447template<class _Tp>
4448template<class _Alloc, class _A0, class _A1>
4449shared_ptr<_Tp>
4450shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4451{
4452 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004453 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004454 typedef __allocator_destructor<_Alloc2> _D2;
4455 _Alloc2 __alloc2(__a);
4456 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004457 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4458 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459 shared_ptr<_Tp> __r;
4460 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004461 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462 __r.__enable_weak_this(__r.__ptr_);
4463 return __r;
4464}
4465
4466template<class _Tp>
4467template<class _Alloc, class _A0, class _A1, class _A2>
4468shared_ptr<_Tp>
4469shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4470{
4471 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004472 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473 typedef __allocator_destructor<_Alloc2> _D2;
4474 _Alloc2 __alloc2(__a);
4475 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004476 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4477 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004478 shared_ptr<_Tp> __r;
4479 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004480 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481 __r.__enable_weak_this(__r.__ptr_);
4482 return __r;
4483}
4484
4485#endif // _LIBCPP_HAS_NO_VARIADICS
4486
4487template<class _Tp>
4488shared_ptr<_Tp>::~shared_ptr()
4489{
4490 if (__cntrl_)
4491 __cntrl_->__release_shared();
4492}
4493
4494template<class _Tp>
4495inline _LIBCPP_INLINE_VISIBILITY
4496shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004497shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498{
4499 shared_ptr(__r).swap(*this);
4500 return *this;
4501}
4502
4503template<class _Tp>
4504template<class _Yp>
4505inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004506typename enable_if
4507<
4508 is_convertible<_Yp*, _Tp*>::value,
4509 shared_ptr<_Tp>&
4510>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004511shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004512{
4513 shared_ptr(__r).swap(*this);
4514 return *this;
4515}
4516
Howard Hinnant74279a52010-09-04 23:28:19 +00004517#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004518
4519template<class _Tp>
4520inline _LIBCPP_INLINE_VISIBILITY
4521shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004522shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004524 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004525 return *this;
4526}
4527
4528template<class _Tp>
4529template<class _Yp>
4530inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004531typename enable_if
4532<
4533 is_convertible<_Yp*, _Tp*>::value,
4534 shared_ptr<_Tp>&
4535>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4537{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004538 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539 return *this;
4540}
4541
4542template<class _Tp>
4543template<class _Yp>
4544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004545typename enable_if
4546<
4547 !is_array<_Yp>::value &&
4548 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004549 shared_ptr<_Tp>
4550>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004551shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4552{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004553 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554 return *this;
4555}
4556
4557template<class _Tp>
4558template <class _Yp, class _Dp>
4559inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004560typename enable_if
4561<
4562 !is_array<_Yp>::value &&
4563 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4564 shared_ptr<_Tp>&
4565>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4567{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004568 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004569 return *this;
4570}
4571
Howard Hinnant74279a52010-09-04 23:28:19 +00004572#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573
4574template<class _Tp>
4575template<class _Yp>
4576inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004577typename enable_if
4578<
4579 !is_array<_Yp>::value &&
4580 is_convertible<_Yp*, _Tp*>::value,
4581 shared_ptr<_Tp>&
4582>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004583shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004584{
4585 shared_ptr(__r).swap(*this);
4586 return *this;
4587}
4588
4589template<class _Tp>
4590template <class _Yp, class _Dp>
4591inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004592typename enable_if
4593<
4594 !is_array<_Yp>::value &&
4595 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4596 shared_ptr<_Tp>&
4597>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004598shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4599{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004600 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004601 return *this;
4602}
4603
Howard Hinnant74279a52010-09-04 23:28:19 +00004604#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004605
4606template<class _Tp>
4607inline _LIBCPP_INLINE_VISIBILITY
4608void
Howard Hinnant719bda32011-05-28 14:41:13 +00004609shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004610{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004611 _VSTD::swap(__ptr_, __r.__ptr_);
4612 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004613}
4614
4615template<class _Tp>
4616inline _LIBCPP_INLINE_VISIBILITY
4617void
Howard Hinnant719bda32011-05-28 14:41:13 +00004618shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004619{
4620 shared_ptr().swap(*this);
4621}
4622
4623template<class _Tp>
4624template<class _Yp>
4625inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004626typename enable_if
4627<
4628 is_convertible<_Yp*, _Tp*>::value,
4629 void
4630>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004631shared_ptr<_Tp>::reset(_Yp* __p)
4632{
4633 shared_ptr(__p).swap(*this);
4634}
4635
4636template<class _Tp>
4637template<class _Yp, class _Dp>
4638inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004639typename enable_if
4640<
4641 is_convertible<_Yp*, _Tp*>::value,
4642 void
4643>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4645{
4646 shared_ptr(__p, __d).swap(*this);
4647}
4648
4649template<class _Tp>
4650template<class _Yp, class _Dp, class _Alloc>
4651inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004652typename enable_if
4653<
4654 is_convertible<_Yp*, _Tp*>::value,
4655 void
4656>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004657shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4658{
4659 shared_ptr(__p, __d, __a).swap(*this);
4660}
4661
4662#ifndef _LIBCPP_HAS_NO_VARIADICS
4663
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004664template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004665inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004666typename enable_if
4667<
4668 !is_array<_Tp>::value,
4669 shared_ptr<_Tp>
4670>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004671make_shared(_Args&& ...__args)
4672{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004673 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004674}
4675
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004676template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004677inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004678typename enable_if
4679<
4680 !is_array<_Tp>::value,
4681 shared_ptr<_Tp>
4682>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004683allocate_shared(const _Alloc& __a, _Args&& ...__args)
4684{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004685 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004686}
4687
4688#else // _LIBCPP_HAS_NO_VARIADICS
4689
4690template<class _Tp>
4691inline _LIBCPP_INLINE_VISIBILITY
4692shared_ptr<_Tp>
4693make_shared()
4694{
4695 return shared_ptr<_Tp>::make_shared();
4696}
4697
4698template<class _Tp, class _A0>
4699inline _LIBCPP_INLINE_VISIBILITY
4700shared_ptr<_Tp>
4701make_shared(_A0& __a0)
4702{
4703 return shared_ptr<_Tp>::make_shared(__a0);
4704}
4705
4706template<class _Tp, class _A0, class _A1>
4707inline _LIBCPP_INLINE_VISIBILITY
4708shared_ptr<_Tp>
4709make_shared(_A0& __a0, _A1& __a1)
4710{
4711 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4712}
4713
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004714template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004715inline _LIBCPP_INLINE_VISIBILITY
4716shared_ptr<_Tp>
4717make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4718{
4719 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4720}
4721
4722template<class _Tp, class _Alloc>
4723inline _LIBCPP_INLINE_VISIBILITY
4724shared_ptr<_Tp>
4725allocate_shared(const _Alloc& __a)
4726{
4727 return shared_ptr<_Tp>::allocate_shared(__a);
4728}
4729
4730template<class _Tp, class _Alloc, class _A0>
4731inline _LIBCPP_INLINE_VISIBILITY
4732shared_ptr<_Tp>
4733allocate_shared(const _Alloc& __a, _A0& __a0)
4734{
4735 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4736}
4737
4738template<class _Tp, class _Alloc, class _A0, class _A1>
4739inline _LIBCPP_INLINE_VISIBILITY
4740shared_ptr<_Tp>
4741allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4742{
4743 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4744}
4745
4746template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4747inline _LIBCPP_INLINE_VISIBILITY
4748shared_ptr<_Tp>
4749allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4750{
4751 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4752}
4753
4754#endif // _LIBCPP_HAS_NO_VARIADICS
4755
4756template<class _Tp, class _Up>
4757inline _LIBCPP_INLINE_VISIBILITY
4758bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004759operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004760{
4761 return __x.get() == __y.get();
4762}
4763
4764template<class _Tp, class _Up>
4765inline _LIBCPP_INLINE_VISIBILITY
4766bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004767operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004768{
4769 return !(__x == __y);
4770}
4771
4772template<class _Tp, class _Up>
4773inline _LIBCPP_INLINE_VISIBILITY
4774bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004775operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004776{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004777 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4778 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004779}
4780
4781template<class _Tp, class _Up>
4782inline _LIBCPP_INLINE_VISIBILITY
4783bool
4784operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4785{
4786 return __y < __x;
4787}
4788
4789template<class _Tp, class _Up>
4790inline _LIBCPP_INLINE_VISIBILITY
4791bool
4792operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4793{
4794 return !(__y < __x);
4795}
4796
4797template<class _Tp, class _Up>
4798inline _LIBCPP_INLINE_VISIBILITY
4799bool
4800operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4801{
4802 return !(__x < __y);
4803}
4804
4805template<class _Tp>
4806inline _LIBCPP_INLINE_VISIBILITY
4807bool
4808operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4809{
4810 return !__x;
4811}
4812
4813template<class _Tp>
4814inline _LIBCPP_INLINE_VISIBILITY
4815bool
4816operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4817{
4818 return !__x;
4819}
4820
4821template<class _Tp>
4822inline _LIBCPP_INLINE_VISIBILITY
4823bool
4824operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4825{
4826 return static_cast<bool>(__x);
4827}
4828
4829template<class _Tp>
4830inline _LIBCPP_INLINE_VISIBILITY
4831bool
4832operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4833{
4834 return static_cast<bool>(__x);
4835}
4836
4837template<class _Tp>
4838inline _LIBCPP_INLINE_VISIBILITY
4839bool
4840operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4841{
4842 return less<_Tp*>()(__x.get(), nullptr);
4843}
4844
4845template<class _Tp>
4846inline _LIBCPP_INLINE_VISIBILITY
4847bool
4848operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4849{
4850 return less<_Tp*>()(nullptr, __x.get());
4851}
4852
4853template<class _Tp>
4854inline _LIBCPP_INLINE_VISIBILITY
4855bool
4856operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4857{
4858 return nullptr < __x;
4859}
4860
4861template<class _Tp>
4862inline _LIBCPP_INLINE_VISIBILITY
4863bool
4864operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4865{
4866 return __x < nullptr;
4867}
4868
4869template<class _Tp>
4870inline _LIBCPP_INLINE_VISIBILITY
4871bool
4872operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4873{
4874 return !(nullptr < __x);
4875}
4876
4877template<class _Tp>
4878inline _LIBCPP_INLINE_VISIBILITY
4879bool
4880operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4881{
4882 return !(__x < nullptr);
4883}
4884
4885template<class _Tp>
4886inline _LIBCPP_INLINE_VISIBILITY
4887bool
4888operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4889{
4890 return !(__x < nullptr);
4891}
4892
4893template<class _Tp>
4894inline _LIBCPP_INLINE_VISIBILITY
4895bool
4896operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4897{
4898 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004899}
4900
4901template<class _Tp>
4902inline _LIBCPP_INLINE_VISIBILITY
4903void
Howard Hinnant719bda32011-05-28 14:41:13 +00004904swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004905{
4906 __x.swap(__y);
4907}
4908
4909template<class _Tp, class _Up>
4910inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004911typename enable_if
4912<
4913 !is_array<_Tp>::value && !is_array<_Up>::value,
4914 shared_ptr<_Tp>
4915>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004916static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004917{
4918 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4919}
4920
4921template<class _Tp, class _Up>
4922inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004923typename enable_if
4924<
4925 !is_array<_Tp>::value && !is_array<_Up>::value,
4926 shared_ptr<_Tp>
4927>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004928dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004929{
4930 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4931 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4932}
4933
4934template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004935typename enable_if
4936<
4937 is_array<_Tp>::value == is_array<_Up>::value,
4938 shared_ptr<_Tp>
4939>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004940const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004941{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004942 typedef typename remove_extent<_Tp>::type _RTp;
4943 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004944}
4945
Howard Hinnant72f73582010-08-11 17:04:31 +00004946#ifndef _LIBCPP_NO_RTTI
4947
Howard Hinnantc51e1022010-05-11 19:42:16 +00004948template<class _Dp, class _Tp>
4949inline _LIBCPP_INLINE_VISIBILITY
4950_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004951get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004952{
4953 return __p.template __get_deleter<_Dp>();
4954}
4955
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004956#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004957
Howard Hinnantc51e1022010-05-11 19:42:16 +00004958template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004959class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004960{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004961public:
4962 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963private:
4964 element_type* __ptr_;
4965 __shared_weak_count* __cntrl_;
4966
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004967public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004968 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004969 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004970 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4971 _NOEXCEPT;
4972 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004973 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004974 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4975 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004976
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004977#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4978 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4979 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
4980 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4981 _NOEXCEPT;
4982#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004983 ~weak_ptr();
4984
Howard Hinnant719bda32011-05-28 14:41:13 +00004985 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004986 template<class _Yp>
4987 typename enable_if
4988 <
4989 is_convertible<_Yp*, element_type*>::value,
4990 weak_ptr&
4991 >::type
4992 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4993
4994#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4995
4996 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4997 template<class _Yp>
4998 typename enable_if
4999 <
5000 is_convertible<_Yp*, element_type*>::value,
5001 weak_ptr&
5002 >::type
5003 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5004
5005#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5006
5007 template<class _Yp>
5008 typename enable_if
5009 <
5010 is_convertible<_Yp*, element_type*>::value,
5011 weak_ptr&
5012 >::type
5013 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005014
Howard Hinnant719bda32011-05-28 14:41:13 +00005015 void swap(weak_ptr& __r) _NOEXCEPT;
5016 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005017
Howard Hinnant756c69b2010-09-22 16:48:34 +00005018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005019 long use_count() const _NOEXCEPT
5020 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005022 bool expired() const _NOEXCEPT
5023 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5024 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005025 template<class _Up>
5026 _LIBCPP_INLINE_VISIBILITY
5027 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005028 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005029 template<class _Up>
5030 _LIBCPP_INLINE_VISIBILITY
5031 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005032 {return __cntrl_ < __r.__cntrl_;}
5033
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005034 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5035 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005036};
5037
5038template<class _Tp>
5039inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005040_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005041weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005042 : __ptr_(0),
5043 __cntrl_(0)
5044{
5045}
5046
5047template<class _Tp>
5048inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005049weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005050 : __ptr_(__r.__ptr_),
5051 __cntrl_(__r.__cntrl_)
5052{
5053 if (__cntrl_)
5054 __cntrl_->__add_weak();
5055}
5056
5057template<class _Tp>
5058template<class _Yp>
5059inline _LIBCPP_INLINE_VISIBILITY
5060weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005061 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005062 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005063 : __ptr_(__r.__ptr_),
5064 __cntrl_(__r.__cntrl_)
5065{
5066 if (__cntrl_)
5067 __cntrl_->__add_weak();
5068}
5069
5070template<class _Tp>
5071template<class _Yp>
5072inline _LIBCPP_INLINE_VISIBILITY
5073weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005074 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005075 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005076 : __ptr_(__r.__ptr_),
5077 __cntrl_(__r.__cntrl_)
5078{
5079 if (__cntrl_)
5080 __cntrl_->__add_weak();
5081}
5082
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005083#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5084
5085template<class _Tp>
5086inline _LIBCPP_INLINE_VISIBILITY
5087weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5088 : __ptr_(__r.__ptr_),
5089 __cntrl_(__r.__cntrl_)
5090{
5091 __r.__ptr_ = 0;
5092 __r.__cntrl_ = 0;
5093}
5094
5095template<class _Tp>
5096template<class _Yp>
5097inline _LIBCPP_INLINE_VISIBILITY
5098weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5099 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5100 _NOEXCEPT
5101 : __ptr_(__r.__ptr_),
5102 __cntrl_(__r.__cntrl_)
5103{
5104 __r.__ptr_ = 0;
5105 __r.__cntrl_ = 0;
5106}
5107
5108#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5109
Howard Hinnantc51e1022010-05-11 19:42:16 +00005110template<class _Tp>
5111weak_ptr<_Tp>::~weak_ptr()
5112{
5113 if (__cntrl_)
5114 __cntrl_->__release_weak();
5115}
5116
5117template<class _Tp>
5118inline _LIBCPP_INLINE_VISIBILITY
5119weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005120weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005121{
5122 weak_ptr(__r).swap(*this);
5123 return *this;
5124}
5125
5126template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005127template<class _Yp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00005128inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005129typename enable_if
5130<
5131 is_convertible<_Yp*, _Tp*>::value,
5132 weak_ptr<_Tp>&
5133>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005134weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005135{
5136 weak_ptr(__r).swap(*this);
5137 return *this;
5138}
5139
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005140#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5141
5142template<class _Tp>
5143inline _LIBCPP_INLINE_VISIBILITY
5144weak_ptr<_Tp>&
5145weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5146{
5147 weak_ptr(_VSTD::move(__r)).swap(*this);
5148 return *this;
5149}
5150
Howard Hinnantc51e1022010-05-11 19:42:16 +00005151template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005152template<class _Yp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00005153inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005154typename enable_if
5155<
5156 is_convertible<_Yp*, _Tp*>::value,
5157 weak_ptr<_Tp>&
5158>::type
5159weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5160{
5161 weak_ptr(_VSTD::move(__r)).swap(*this);
5162 return *this;
5163}
5164
5165#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5166
5167template<class _Tp>
5168template<class _Yp>
5169inline _LIBCPP_INLINE_VISIBILITY
5170typename enable_if
5171<
5172 is_convertible<_Yp*, _Tp*>::value,
5173 weak_ptr<_Tp>&
5174>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005175weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005176{
5177 weak_ptr(__r).swap(*this);
5178 return *this;
5179}
5180
5181template<class _Tp>
5182inline _LIBCPP_INLINE_VISIBILITY
5183void
Howard Hinnant719bda32011-05-28 14:41:13 +00005184weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005185{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005186 _VSTD::swap(__ptr_, __r.__ptr_);
5187 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005188}
5189
5190template<class _Tp>
5191inline _LIBCPP_INLINE_VISIBILITY
5192void
Howard Hinnant719bda32011-05-28 14:41:13 +00005193swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005194{
5195 __x.swap(__y);
5196}
5197
5198template<class _Tp>
5199inline _LIBCPP_INLINE_VISIBILITY
5200void
Howard Hinnant719bda32011-05-28 14:41:13 +00005201weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005202{
5203 weak_ptr().swap(*this);
5204}
5205
5206template<class _Tp>
5207template<class _Yp>
5208shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5209 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5210 : __ptr_(__r.__ptr_),
5211 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5212{
5213 if (__cntrl_ == 0)
5214#ifndef _LIBCPP_NO_EXCEPTIONS
5215 throw bad_weak_ptr();
5216#else
5217 assert(!"bad_weak_ptr");
5218#endif
5219}
5220
5221template<class _Tp>
5222shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005223weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005224{
5225 shared_ptr<_Tp> __r;
5226 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5227 if (__r.__cntrl_)
5228 __r.__ptr_ = __ptr_;
5229 return __r;
5230}
5231
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005232template <class _Tp> struct owner_less;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005233
5234template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005235struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005236 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005237{
5238 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005240 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5241 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005243 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5244 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005246 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5247 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005248};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005249
5250template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005251struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005252 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5253{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005254 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005256 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5257 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005259 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5260 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005262 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5263 {return __x.owner_before(__y);}
5264};
5265
5266template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005267class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005268{
5269 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005270protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005271 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005272 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005274 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005276 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5277 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005279 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005280public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005282 shared_ptr<_Tp> shared_from_this()
5283 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005285 shared_ptr<_Tp const> shared_from_this() const
5286 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005287
5288 template <class _Up> friend class shared_ptr;
5289};
5290
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005291template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005292struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005293{
5294 typedef shared_ptr<_Tp> argument_type;
5295 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005297 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005298 {
5299 return hash<_Tp*>()(__ptr.get());
5300 }
5301};
5302
Howard Hinnantc834c512011-11-29 18:15:50 +00005303template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005304inline _LIBCPP_INLINE_VISIBILITY
5305basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005306operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005307
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +00005308#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005309
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005310class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005311{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005312 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005313public:
5314 void lock() _NOEXCEPT;
5315 void unlock() _NOEXCEPT;
5316
5317private:
5318 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5319 __sp_mut(const __sp_mut&);
5320 __sp_mut& operator=(const __sp_mut&);
5321
Howard Hinnant8331b762013-03-06 23:30:19 +00005322 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005323};
5324
Howard Hinnant8331b762013-03-06 23:30:19 +00005325_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005326
5327template <class _Tp>
5328inline _LIBCPP_INLINE_VISIBILITY
5329bool
5330atomic_is_lock_free(const shared_ptr<_Tp>*)
5331{
5332 return false;
5333}
5334
5335template <class _Tp>
5336shared_ptr<_Tp>
5337atomic_load(const shared_ptr<_Tp>* __p)
5338{
5339 __sp_mut& __m = __get_sp_mut(__p);
5340 __m.lock();
5341 shared_ptr<_Tp> __q = *__p;
5342 __m.unlock();
5343 return __q;
5344}
5345
5346template <class _Tp>
5347inline _LIBCPP_INLINE_VISIBILITY
5348shared_ptr<_Tp>
5349atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5350{
5351 return atomic_load(__p);
5352}
5353
5354template <class _Tp>
5355void
5356atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5357{
5358 __sp_mut& __m = __get_sp_mut(__p);
5359 __m.lock();
5360 __p->swap(__r);
5361 __m.unlock();
5362}
5363
5364template <class _Tp>
5365inline _LIBCPP_INLINE_VISIBILITY
5366void
5367atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5368{
5369 atomic_store(__p, __r);
5370}
5371
5372template <class _Tp>
5373shared_ptr<_Tp>
5374atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5375{
5376 __sp_mut& __m = __get_sp_mut(__p);
5377 __m.lock();
5378 __p->swap(__r);
5379 __m.unlock();
5380 return __r;
5381}
5382
5383template <class _Tp>
5384inline _LIBCPP_INLINE_VISIBILITY
5385shared_ptr<_Tp>
5386atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5387{
5388 return atomic_exchange(__p, __r);
5389}
5390
5391template <class _Tp>
5392bool
5393atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5394{
5395 __sp_mut& __m = __get_sp_mut(__p);
5396 __m.lock();
5397 if (__p->__owner_equivalent(*__v))
5398 {
5399 *__p = __w;
5400 __m.unlock();
5401 return true;
5402 }
5403 *__v = *__p;
5404 __m.unlock();
5405 return false;
5406}
5407
5408template <class _Tp>
5409inline _LIBCPP_INLINE_VISIBILITY
5410bool
5411atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5412{
5413 return atomic_compare_exchange_strong(__p, __v, __w);
5414}
5415
5416template <class _Tp>
5417inline _LIBCPP_INLINE_VISIBILITY
5418bool
5419atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5420 shared_ptr<_Tp> __w, memory_order, memory_order)
5421{
5422 return atomic_compare_exchange_strong(__p, __v, __w);
5423}
5424
5425template <class _Tp>
5426inline _LIBCPP_INLINE_VISIBILITY
5427bool
5428atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5429 shared_ptr<_Tp> __w, memory_order, memory_order)
5430{
5431 return atomic_compare_exchange_weak(__p, __v, __w);
5432}
5433
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +00005434#endif // __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005435
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005436//enum class
Howard Hinnant8331b762013-03-06 23:30:19 +00005437struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005438{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005439 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005440 {
5441 relaxed,
5442 preferred,
5443 strict
5444 };
5445
Howard Hinnant49e145e2012-10-30 19:06:59 +00005446 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005447
Howard Hinnant756c69b2010-09-22 16:48:34 +00005448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005449 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005451 operator int() const {return __v_;}
5452};
5453
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005454_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5455_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5456_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5457_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5458_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005459
5460template <class _Tp>
5461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005462_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005463undeclare_reachable(_Tp* __p)
5464{
5465 return static_cast<_Tp*>(__undeclare_reachable(__p));
5466}
5467
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005468_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005469
5470_LIBCPP_END_NAMESPACE_STD
5471
5472#endif // _LIBCPP_MEMORY