blob: 50a1f00a5e5f696c03ae44ddbf490f8d9a86754c [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;
Marshall Clow0b587562015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
81 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
82 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
83
84 static pointer allocate(allocator_type& a, size_type n);
85 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
86
Howard Hinnant719bda32011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 template <class T, class... Args>
90 static void construct(allocator_type& a, T* p, Args&&... args);
91
92 template <class T>
93 static void destroy(allocator_type& a, T* p);
94
Marshall Clow4f834b52013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 static allocator_type
98 select_on_container_copy_construction(const allocator_type& a);
99};
100
101template <>
102class allocator<void>
103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef T* pointer;
119 typedef const T* const_pointer;
120 typedef typename add_lvalue_reference<T>::type reference;
121 typedef typename add_lvalue_reference<const T>::type const_reference;
122 typedef T value_type;
123
124 template <class U> struct rebind {typedef allocator<U> other;};
125
Howard Hinnant719bda32011-05-28 14:41:13 +0000126 allocator() noexcept;
127 allocator(const allocator&) noexcept;
128 template <class U> allocator(const allocator<U>&) noexcept;
129 ~allocator();
130 pointer address(reference x) const noexcept;
131 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 void deallocate(pointer p, size_type n) noexcept;
134 size_type max_size() const noexcept;
135 template<class U, class... Args>
136 void construct(U* p, Args&&... args);
137 template <class U>
138 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class OutputIterator, class T>
148class raw_storage_iterator
149 : public iterator<output_iterator_tag,
150 T, // purposefully not C++03
151 ptrdiff_t, // purposefully not C++03
152 T*, // purposefully not C++03
153 raw_storage_iterator&> // purposefully not C++03
154{
155public:
156 explicit raw_storage_iterator(OutputIterator x);
157 raw_storage_iterator& operator*();
158 raw_storage_iterator& operator=(const T& element);
159 raw_storage_iterator& operator++();
160 raw_storage_iterator operator++(int);
161};
162
Howard Hinnant719bda32011-05-28 14:41:13 +0000163template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
164template <class T> void return_temporary_buffer(T* p) noexcept;
165
166template <class T> T* addressof(T& r) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167
168template <class InputIterator, class ForwardIterator>
169ForwardIterator
170uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
171
Howard Hinnant719bda32011-05-28 14:41:13 +0000172template <class InputIterator, class Size, class ForwardIterator>
173ForwardIterator
174uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
175
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176template <class ForwardIterator, class T>
177void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
178
179template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000180ForwardIterator
181uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182
183template <class Y> struct auto_ptr_ref {};
184
185template<class X>
186class auto_ptr
187{
188public:
189 typedef X element_type;
190
191 explicit auto_ptr(X* p =0) throw();
192 auto_ptr(auto_ptr&) throw();
193 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
194 auto_ptr& operator=(auto_ptr&) throw();
195 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
196 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
197 ~auto_ptr() throw();
198
199 typename add_lvalue_reference<X>::type operator*() const throw();
200 X* operator->() const throw();
201 X* get() const throw();
202 X* release() throw();
203 void reset(X* p =0) throw();
204
205 auto_ptr(auto_ptr_ref<X>) throw();
206 template<class Y> operator auto_ptr_ref<Y>() throw();
207 template<class Y> operator auto_ptr<Y>() throw();
208};
209
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000210template <class T>
211struct default_delete
212{
Howard Hinnant719bda32011-05-28 14:41:13 +0000213 constexpr default_delete() noexcept = default;
214 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000215
Howard Hinnant719bda32011-05-28 14:41:13 +0000216 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000217};
218
219template <class T>
220struct default_delete<T[]>
221{
Howard Hinnant719bda32011-05-28 14:41:13 +0000222 constexpr default_delete() noexcept = default;
223 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000224 template <class U> void operator()(U*) const = delete;
225};
226
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000227template <class T, class D = default_delete<T>>
228class unique_ptr
229{
230public:
231 typedef see below pointer;
232 typedef T element_type;
233 typedef D deleter_type;
234
235 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000236 constexpr unique_ptr() noexcept;
237 explicit unique_ptr(pointer p) noexcept;
238 unique_ptr(pointer p, see below d1) noexcept;
239 unique_ptr(pointer p, see below d2) noexcept;
240 unique_ptr(unique_ptr&& u) noexcept;
241 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000242 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000243 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000244 template <class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000245 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000246
247 // destructor
248 ~unique_ptr();
249
250 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000251 unique_ptr& operator=(unique_ptr&& u) noexcept;
252 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
253 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000254
255 // observers
256 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000257 pointer operator->() const noexcept;
258 pointer get() const noexcept;
259 deleter_type& get_deleter() noexcept;
260 const deleter_type& get_deleter() const noexcept;
261 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000262
263 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 pointer release() noexcept;
265 void reset(pointer p = pointer()) noexcept;
266 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000267};
268
269template <class T, class D>
270class unique_ptr<T[], D>
271{
272public:
273 typedef implementation-defined pointer;
274 typedef T element_type;
275 typedef D deleter_type;
276
277 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000278 constexpr unique_ptr() noexcept;
279 explicit unique_ptr(pointer p) noexcept;
280 unique_ptr(pointer p, see below d) noexcept;
281 unique_ptr(pointer p, see below d) noexcept;
282 unique_ptr(unique_ptr&& u) noexcept;
283 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000284
285 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000286 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000287
288 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000289 unique_ptr& operator=(unique_ptr&& u) noexcept;
290 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000291
292 // observers
293 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000294 pointer get() const noexcept;
295 deleter_type& get_deleter() noexcept;
296 const deleter_type& get_deleter() const noexcept;
297 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000298
299 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000300 pointer release() noexcept;
301 void reset(pointer p = pointer()) noexcept;
302 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000303 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000304 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000305};
306
307template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000308 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000309
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);
320template <class T1, class D1, class T2, class D2>
321 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
322
Howard Hinnant719bda32011-05-28 14:41:13 +0000323template <class T, class D>
324 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
325template <class T, class D>
326 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
327template <class T, class D>
328 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
329template <class T, class D>
330 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
331
332template <class T, class D>
333 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
334template <class T, class D>
335 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
336template <class T, class D>
337 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
338template <class T, class D>
339 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
340template <class T, class D>
341 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
342template <class T, class D>
343 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
344template <class T, class D>
345 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
346template <class T, class D>
347 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
348
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000349class bad_weak_ptr
350 : public std::exception
351{
Howard Hinnant719bda32011-05-28 14:41:13 +0000352 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000353};
354
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000355template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
356template<class T> unique_ptr<T> make_unique(size_t n); // C++14
357template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
358
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000359template<class T>
360class shared_ptr
361{
362public:
363 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000364 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000365
366 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000367 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000368 template<class Y> explicit shared_ptr(Y* p);
369 template<class Y, class D> shared_ptr(Y* p, D d);
370 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
371 template <class D> shared_ptr(nullptr_t p, D d);
372 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000373 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
374 shared_ptr(const shared_ptr& r) noexcept;
375 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
376 shared_ptr(shared_ptr&& r) noexcept;
377 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000378 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
379 template<class Y> shared_ptr(auto_ptr<Y>&& r);
380 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
381 shared_ptr(nullptr_t) : shared_ptr() { }
382
383 // destructor:
384 ~shared_ptr();
385
386 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000387 shared_ptr& operator=(const shared_ptr& r) noexcept;
388 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
389 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000390 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
391 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
392 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
393
394 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000395 void swap(shared_ptr& r) noexcept;
396 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000397 template<class Y> void reset(Y* p);
398 template<class Y, class D> void reset(Y* p, D d);
399 template<class Y, class D, class A> void reset(Y* p, D d, A a);
400
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 // observers:
402 T* get() const noexcept;
403 T& operator*() const noexcept;
404 T* operator->() const noexcept;
405 long use_count() const noexcept;
406 bool unique() const noexcept;
407 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000408 template<class U> bool owner_before(shared_ptr<U> const& b) const;
409 template<class U> bool owner_before(weak_ptr<U> const& b) const;
410};
411
412// shared_ptr comparisons:
413template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000414 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000415template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000416 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000417template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000418 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000419template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000420 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000421template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000422 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000423template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000424 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
425
426template <class T>
427 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
428template <class T>
429 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
430template <class T>
431 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
432template <class T>
433 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
434template <class T>
435 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
436template <class T>
437bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
438template <class T>
439 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
440template <class T>
441 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
442template <class T>
443 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
444template <class T>
445 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
446template <class T>
447 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
448template <class T>
449 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000450
451// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000452template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000453
454// shared_ptr casts:
455template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000456 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000457template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000458 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000459template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000460 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000461
462// shared_ptr I/O:
463template<class E, class T, class Y>
464 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
465
466// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000467template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000468
469template<class T, class... Args>
470 shared_ptr<T> make_shared(Args&&... args);
471template<class T, class A, class... Args>
472 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
473
474template<class T>
475class weak_ptr
476{
477public:
478 typedef T element_type;
479
480 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000481 constexpr weak_ptr() noexcept;
482 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
483 weak_ptr(weak_ptr const& r) noexcept;
484 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000485 weak_ptr(weak_ptr&& r) noexcept; // C++14
486 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487
488 // destructor
489 ~weak_ptr();
490
491 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000492 weak_ptr& operator=(weak_ptr const& r) noexcept;
493 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
494 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000495 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
496 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000497
498 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000499 void swap(weak_ptr& r) noexcept;
500 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000501
502 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000503 long use_count() const noexcept;
504 bool expired() const noexcept;
505 shared_ptr<T> lock() const noexcept;
Marshall Clow495e4a62013-09-03 14:37:50 +0000506 template<class U> bool owner_before(shared_ptr<U> const& b) const;
507 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000508};
509
510// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000511template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000512
513// class owner_less:
514template<class T> struct owner_less;
515
516template<class T>
517struct owner_less<shared_ptr<T>>
518 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
519{
520 typedef bool result_type;
521 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
522 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
523 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
524};
525
526template<class T>
527struct owner_less<weak_ptr<T>>
528 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
529{
530 typedef bool result_type;
531 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
532 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
533 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
534};
535
536template<class T>
537class enable_shared_from_this
538{
539protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000540 constexpr enable_shared_from_this() noexcept;
541 enable_shared_from_this(enable_shared_from_this const&) noexcept;
542 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000543 ~enable_shared_from_this();
544public:
545 shared_ptr<T> shared_from_this();
546 shared_ptr<T const> shared_from_this() const;
547};
548
549template<class T>
550 bool atomic_is_lock_free(const shared_ptr<T>* p);
551template<class T>
552 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
553template<class T>
554 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
555template<class T>
556 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
557template<class T>
558 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
559template<class T>
560 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
561template<class T>
562 shared_ptr<T>
563 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
564template<class T>
565 bool
566 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
567template<class T>
568 bool
569 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
570template<class T>
571 bool
572 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
573 shared_ptr<T> w, memory_order success,
574 memory_order failure);
575template<class T>
576 bool
577 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
578 shared_ptr<T> w, memory_order success,
579 memory_order failure);
580// Hash support
581template <class T> struct hash;
582template <class T, class D> struct hash<unique_ptr<T, D> >;
583template <class T> struct hash<shared_ptr<T> >;
584
585// Pointer safety
586enum class pointer_safety { relaxed, preferred, strict };
587void declare_reachable(void *p);
588template <class T> T *undeclare_reachable(T *p);
589void declare_no_pointers(char *p, size_t n);
590void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000591pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000592
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
594
595} // std
596
597*/
598
599#include <__config>
600#include <type_traits>
601#include <typeinfo>
602#include <cstddef>
603#include <cstdint>
604#include <new>
605#include <utility>
606#include <limits>
607#include <iterator>
608#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000609#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000610#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000611#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000612#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613#if defined(_LIBCPP_NO_EXCEPTIONS)
614 #include <cassert>
615#endif
616
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000617#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000618# include <atomic>
619#endif
620
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000621#include <__undef_min_max>
Saleem Abdulrasooldcf27a62015-02-13 22:15:32 +0000622#include <__undef___deallocate>
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000623
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000624#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000626#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627
628_LIBCPP_BEGIN_NAMESPACE_STD
629
Eric Fiselier89659d12015-07-07 00:27:16 +0000630template <class _ValueType>
631inline _LIBCPP_ALWAYS_INLINE
632_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
633#if !defined(_LIBCPP_HAS_NO_THREADS) && \
634 defined(__ATOMIC_RELAXED) && \
635 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
636 return __atomic_load_n(__value, __ATOMIC_RELAXED);
637#else
638 return *__value;
639#endif
640}
641
Howard Hinnant8bea6da2013-08-08 18:38:55 +0000642// addressof moved to <__functional_base>
Douglas Gregor13034442011-06-22 22:17:44 +0000643
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644template <class _Tp> class allocator;
645
646template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000647class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648{
649public:
650 typedef void* pointer;
651 typedef const void* const_pointer;
652 typedef void value_type;
653
654 template <class _Up> struct rebind {typedef allocator<_Up> other;};
655};
656
Howard Hinnant9f771052012-01-19 23:15:22 +0000657template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000658class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000659{
660public:
661 typedef const void* pointer;
662 typedef const void* const_pointer;
663 typedef const void value_type;
664
665 template <class _Up> struct rebind {typedef allocator<_Up> other;};
666};
667
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668// pointer_traits
669
670template <class _Tp>
671struct __has_element_type
672{
673private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000674 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 template <class _Up> static __two __test(...);
676 template <class _Up> static char __test(typename _Up::element_type* = 0);
677public:
678 static const bool value = sizeof(__test<_Tp>(0)) == 1;
679};
680
681template <class _Ptr, bool = __has_element_type<_Ptr>::value>
682struct __pointer_traits_element_type;
683
684template <class _Ptr>
685struct __pointer_traits_element_type<_Ptr, true>
686{
687 typedef typename _Ptr::element_type type;
688};
689
690#ifndef _LIBCPP_HAS_NO_VARIADICS
691
692template <template <class, class...> class _Sp, class _Tp, class ..._Args>
693struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
694{
695 typedef typename _Sp<_Tp, _Args...>::element_type type;
696};
697
698template <template <class, class...> class _Sp, class _Tp, class ..._Args>
699struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
700{
701 typedef _Tp type;
702};
703
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000704#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705
706template <template <class> class _Sp, class _Tp>
707struct __pointer_traits_element_type<_Sp<_Tp>, true>
708{
709 typedef typename _Sp<_Tp>::element_type type;
710};
711
712template <template <class> class _Sp, class _Tp>
713struct __pointer_traits_element_type<_Sp<_Tp>, false>
714{
715 typedef _Tp type;
716};
717
718template <template <class, class> class _Sp, class _Tp, class _A0>
719struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
720{
721 typedef typename _Sp<_Tp, _A0>::element_type type;
722};
723
724template <template <class, class> class _Sp, class _Tp, class _A0>
725struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
726{
727 typedef _Tp type;
728};
729
730template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
731struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
732{
733 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
734};
735
736template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
737struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
738{
739 typedef _Tp type;
740};
741
742template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
743 class _A1, class _A2>
744struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
745{
746 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
747};
748
749template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
750 class _A1, class _A2>
751struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
752{
753 typedef _Tp type;
754};
755
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000756#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757
758template <class _Tp>
759struct __has_difference_type
760{
761private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000762 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763 template <class _Up> static __two __test(...);
764 template <class _Up> static char __test(typename _Up::difference_type* = 0);
765public:
766 static const bool value = sizeof(__test<_Tp>(0)) == 1;
767};
768
769template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
770struct __pointer_traits_difference_type
771{
772 typedef ptrdiff_t type;
773};
774
775template <class _Ptr>
776struct __pointer_traits_difference_type<_Ptr, true>
777{
778 typedef typename _Ptr::difference_type type;
779};
780
781template <class _Tp, class _Up>
782struct __has_rebind
783{
784private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000785 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 template <class _Xp> static __two __test(...);
787 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
788public:
789 static const bool value = sizeof(__test<_Tp>(0)) == 1;
790};
791
792template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
793struct __pointer_traits_rebind
794{
795#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
796 typedef typename _Tp::template rebind<_Up> type;
797#else
798 typedef typename _Tp::template rebind<_Up>::other type;
799#endif
800};
801
802#ifndef _LIBCPP_HAS_NO_VARIADICS
803
804template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
805struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
806{
807#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
808 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
809#else
810 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
811#endif
812};
813
814template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
815struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
816{
817 typedef _Sp<_Up, _Args...> type;
818};
819
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000820#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821
822template <template <class> class _Sp, class _Tp, class _Up>
823struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
824{
825#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
826 typedef typename _Sp<_Tp>::template rebind<_Up> type;
827#else
828 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
829#endif
830};
831
832template <template <class> class _Sp, class _Tp, class _Up>
833struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
834{
835 typedef _Sp<_Up> type;
836};
837
838template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
839struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
840{
841#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
842 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
843#else
844 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
845#endif
846};
847
848template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
849struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
850{
851 typedef _Sp<_Up, _A0> type;
852};
853
854template <template <class, class, class> class _Sp, class _Tp, class _A0,
855 class _A1, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
857{
858#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
859 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
860#else
861 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
862#endif
863};
864
865template <template <class, class, class> class _Sp, class _Tp, class _A0,
866 class _A1, class _Up>
867struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
868{
869 typedef _Sp<_Up, _A0, _A1> type;
870};
871
872template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
873 class _A1, class _A2, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
875{
876#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
877 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
878#else
879 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
880#endif
881};
882
883template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
884 class _A1, class _A2, class _Up>
885struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
886{
887 typedef _Sp<_Up, _A0, _A1, _A2> type;
888};
889
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000890#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891
892template <class _Ptr>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000893struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894{
895 typedef _Ptr pointer;
896 typedef typename __pointer_traits_element_type<pointer>::type element_type;
897 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
898
899#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantf09283d2011-05-11 20:21:19 +0000900 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901#else
902 template <class _Up> struct rebind
903 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000904#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905
906private:
907 struct __nat {};
908public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 static pointer pointer_to(typename conditional<is_void<element_type>::value,
911 __nat, element_type>::type& __r)
912 {return pointer::pointer_to(__r);}
913};
914
915template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000916struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917{
918 typedef _Tp* pointer;
919 typedef _Tp element_type;
920 typedef ptrdiff_t difference_type;
921
922#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
923 template <class _Up> using rebind = _Up*;
924#else
925 template <class _Up> struct rebind {typedef _Up* other;};
926#endif
927
928private:
929 struct __nat {};
930public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000933 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000934 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935};
936
Eric Fiselierae3ab842015-08-23 02:56:05 +0000937template <class _From, class _To>
938struct __rebind_pointer {
939#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
940 typedef typename pointer_traits<_From>::template rebind<_To> type;
941#else
942 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
943#endif
944};
945
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946// allocator_traits
947
948namespace __has_pointer_type_imp
949{
Howard Hinnant6e260172013-09-21 01:45:05 +0000950 template <class _Up> static __two __test(...);
951 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952}
953
954template <class _Tp>
955struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +0000956 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957{
958};
959
960namespace __pointer_type_imp
961{
962
963template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
964struct __pointer_type
965{
966 typedef typename _Dp::pointer type;
967};
968
969template <class _Tp, class _Dp>
970struct __pointer_type<_Tp, _Dp, false>
971{
972 typedef _Tp* type;
973};
974
Howard Hinnant8e4e6002010-11-18 01:40:00 +0000975} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976
977template <class _Tp, class _Dp>
978struct __pointer_type
979{
980 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
981};
982
983template <class _Tp>
984struct __has_const_pointer
985{
986private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000987 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 template <class _Up> static __two __test(...);
989 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
990public:
991 static const bool value = sizeof(__test<_Tp>(0)) == 1;
992};
993
994template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
995struct __const_pointer
996{
997 typedef typename _Alloc::const_pointer type;
998};
999
1000template <class _Tp, class _Ptr, class _Alloc>
1001struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1002{
1003#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1004 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1005#else
1006 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1007#endif
1008};
1009
1010template <class _Tp>
1011struct __has_void_pointer
1012{
1013private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001014 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 template <class _Up> static __two __test(...);
1016 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1017public:
1018 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1019};
1020
1021template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1022struct __void_pointer
1023{
1024 typedef typename _Alloc::void_pointer type;
1025};
1026
1027template <class _Ptr, class _Alloc>
1028struct __void_pointer<_Ptr, _Alloc, false>
1029{
1030#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1031 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1032#else
1033 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1034#endif
1035};
1036
1037template <class _Tp>
1038struct __has_const_void_pointer
1039{
1040private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001041 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 template <class _Up> static __two __test(...);
1043 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1044public:
1045 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1046};
1047
1048template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1049struct __const_void_pointer
1050{
1051 typedef typename _Alloc::const_void_pointer type;
1052};
1053
1054template <class _Ptr, class _Alloc>
1055struct __const_void_pointer<_Ptr, _Alloc, false>
1056{
1057#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1058 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1059#else
1060 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1061#endif
1062};
1063
Howard Hinnantc834c512011-11-29 18:15:50 +00001064template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001066_Tp*
1067__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068{
1069 return __p;
1070}
1071
1072template <class _Pointer>
1073inline _LIBCPP_INLINE_VISIBILITY
1074typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001075__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001077 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078}
1079
1080template <class _Tp>
1081struct __has_size_type
1082{
1083private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001084 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 template <class _Up> static __two __test(...);
1086 template <class _Up> static char __test(typename _Up::size_type* = 0);
1087public:
1088 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1089};
1090
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001091template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092struct __size_type
1093{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001094 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095};
1096
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001097template <class _Alloc, class _DiffType>
1098struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099{
1100 typedef typename _Alloc::size_type type;
1101};
1102
1103template <class _Tp>
1104struct __has_propagate_on_container_copy_assignment
1105{
1106private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001107 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 template <class _Up> static __two __test(...);
1109 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1110public:
1111 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1112};
1113
1114template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1115struct __propagate_on_container_copy_assignment
1116{
1117 typedef false_type type;
1118};
1119
1120template <class _Alloc>
1121struct __propagate_on_container_copy_assignment<_Alloc, true>
1122{
1123 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1124};
1125
1126template <class _Tp>
1127struct __has_propagate_on_container_move_assignment
1128{
1129private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001130 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 template <class _Up> static __two __test(...);
1132 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1133public:
1134 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1135};
1136
1137template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1138struct __propagate_on_container_move_assignment
1139{
1140 typedef false_type type;
1141};
1142
1143template <class _Alloc>
1144struct __propagate_on_container_move_assignment<_Alloc, true>
1145{
1146 typedef typename _Alloc::propagate_on_container_move_assignment type;
1147};
1148
1149template <class _Tp>
1150struct __has_propagate_on_container_swap
1151{
1152private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001153 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 template <class _Up> static __two __test(...);
1155 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1156public:
1157 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1158};
1159
1160template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1161struct __propagate_on_container_swap
1162{
1163 typedef false_type type;
1164};
1165
1166template <class _Alloc>
1167struct __propagate_on_container_swap<_Alloc, true>
1168{
1169 typedef typename _Alloc::propagate_on_container_swap type;
1170};
1171
Marshall Clow0b587562015-06-02 16:34:03 +00001172template <class _Tp>
1173struct __has_is_always_equal
1174{
1175private:
1176 struct __two {char __lx; char __lxx;};
1177 template <class _Up> static __two __test(...);
1178 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1179public:
1180 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1181};
1182
1183template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1184struct __is_always_equal
1185{
1186 typedef typename _VSTD::is_empty<_Alloc>::type type;
1187};
1188
1189template <class _Alloc>
1190struct __is_always_equal<_Alloc, true>
1191{
1192 typedef typename _Alloc::is_always_equal type;
1193};
1194
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1196struct __has_rebind_other
1197{
1198private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001199 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 template <class _Xp> static __two __test(...);
1201 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1202public:
1203 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1204};
1205
1206template <class _Tp, class _Up>
1207struct __has_rebind_other<_Tp, _Up, false>
1208{
1209 static const bool value = false;
1210};
1211
1212template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1213struct __allocator_traits_rebind
1214{
1215 typedef typename _Tp::template rebind<_Up>::other type;
1216};
1217
1218#ifndef _LIBCPP_HAS_NO_VARIADICS
1219
1220template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1221struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1222{
1223 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1224};
1225
1226template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1227struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1228{
1229 typedef _Alloc<_Up, _Args...> type;
1230};
1231
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001232#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233
1234template <template <class> class _Alloc, class _Tp, class _Up>
1235struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1236{
1237 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1238};
1239
1240template <template <class> class _Alloc, class _Tp, class _Up>
1241struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1242{
1243 typedef _Alloc<_Up> type;
1244};
1245
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1247struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1248{
1249 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1250};
1251
1252template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1253struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1254{
1255 typedef _Alloc<_Up, _A0> type;
1256};
1257
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1259 class _A1, class _Up>
1260struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1261{
1262 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1263};
1264
1265template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1266 class _A1, class _Up>
1267struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1268{
1269 typedef _Alloc<_Up, _A0, _A1> type;
1270};
1271
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1273 class _A1, class _A2, class _Up>
1274struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1275{
1276 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1277};
1278
1279template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1280 class _A1, class _A2, class _Up>
1281struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1282{
1283 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1284};
1285
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001286#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
1288#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1289
1290template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1291auto
1292__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1293 -> decltype(__a.allocate(__sz, __p), true_type());
1294
1295template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1296auto
1297__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1298 -> false_type;
1299
1300template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1301struct __has_allocate_hint
1302 : integral_constant<bool,
1303 is_same<
1304 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1305 declval<_SizeType>(),
1306 declval<_ConstVoidPtr>())),
1307 true_type>::value>
1308{
1309};
1310
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001311#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312
1313template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1314struct __has_allocate_hint
1315 : true_type
1316{
1317};
1318
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001319#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320
Howard Hinnant986832c2011-07-29 21:35:53 +00001321#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322
1323template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001324decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1325 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326 true_type())
1327__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1328
1329template <class _Alloc, class _Pointer, class ..._Args>
1330false_type
1331__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1332
1333template <class _Alloc, class _Pointer, class ..._Args>
1334struct __has_construct
1335 : integral_constant<bool,
1336 is_same<
1337 decltype(__has_construct_test(declval<_Alloc>(),
1338 declval<_Pointer>(),
1339 declval<_Args>()...)),
1340 true_type>::value>
1341{
1342};
1343
1344template <class _Alloc, class _Pointer>
1345auto
1346__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1347 -> decltype(__a.destroy(__p), true_type());
1348
1349template <class _Alloc, class _Pointer>
1350auto
1351__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1352 -> false_type;
1353
1354template <class _Alloc, class _Pointer>
1355struct __has_destroy
1356 : integral_constant<bool,
1357 is_same<
1358 decltype(__has_destroy_test(declval<_Alloc>(),
1359 declval<_Pointer>())),
1360 true_type>::value>
1361{
1362};
1363
1364template <class _Alloc>
1365auto
1366__has_max_size_test(_Alloc&& __a)
1367 -> decltype(__a.max_size(), true_type());
1368
1369template <class _Alloc>
1370auto
1371__has_max_size_test(const volatile _Alloc& __a)
1372 -> false_type;
1373
1374template <class _Alloc>
1375struct __has_max_size
1376 : integral_constant<bool,
1377 is_same<
1378 decltype(__has_max_size_test(declval<_Alloc&>())),
1379 true_type>::value>
1380{
1381};
1382
1383template <class _Alloc>
1384auto
1385__has_select_on_container_copy_construction_test(_Alloc&& __a)
1386 -> decltype(__a.select_on_container_copy_construction(), true_type());
1387
1388template <class _Alloc>
1389auto
1390__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1391 -> false_type;
1392
1393template <class _Alloc>
1394struct __has_select_on_container_copy_construction
1395 : integral_constant<bool,
1396 is_same<
1397 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1398 true_type>::value>
1399{
1400};
1401
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001402#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403
1404#ifndef _LIBCPP_HAS_NO_VARIADICS
1405
1406template <class _Alloc, class _Pointer, class ..._Args>
1407struct __has_construct
1408 : false_type
1409{
1410};
1411
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001412#else // _LIBCPP_HAS_NO_VARIADICS
1413
1414template <class _Alloc, class _Pointer, class _Args>
1415struct __has_construct
1416 : false_type
1417{
1418};
1419
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001420#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421
1422template <class _Alloc, class _Pointer>
1423struct __has_destroy
1424 : false_type
1425{
1426};
1427
1428template <class _Alloc>
1429struct __has_max_size
1430 : true_type
1431{
1432};
1433
1434template <class _Alloc>
1435struct __has_select_on_container_copy_construction
1436 : false_type
1437{
1438};
1439
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001440#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001442template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1443struct __alloc_traits_difference_type
1444{
1445 typedef typename pointer_traits<_Ptr>::difference_type type;
1446};
1447
1448template <class _Alloc, class _Ptr>
1449struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1450{
1451 typedef typename _Alloc::difference_type type;
1452};
1453
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454template <class _Alloc>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001455struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456{
1457 typedef _Alloc allocator_type;
1458 typedef typename allocator_type::value_type value_type;
1459
1460 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1461 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1462 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1463 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1464
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001465 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1466 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467
1468 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1469 propagate_on_container_copy_assignment;
1470 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1471 propagate_on_container_move_assignment;
1472 typedef typename __propagate_on_container_swap<allocator_type>::type
1473 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001474 typedef typename __is_always_equal<allocator_type>::type
1475 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476
1477#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1478 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001479 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001481#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482 template <class _Tp> struct rebind_alloc
1483 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1484 template <class _Tp> struct rebind_traits
1485 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001486#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487
Howard Hinnant756c69b2010-09-22 16:48:34 +00001488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489 static pointer allocate(allocator_type& __a, size_type __n)
1490 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1493 {return allocate(__a, __n, __hint,
1494 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1495
Howard Hinnant756c69b2010-09-22 16:48:34 +00001496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001497 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498 {__a.deallocate(__p, __n);}
1499
1500#ifndef _LIBCPP_HAS_NO_VARIADICS
1501 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001504 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001505 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001506#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509 static void construct(allocator_type& __a, _Tp* __p)
1510 {
1511 ::new ((void*)__p) _Tp();
1512 }
1513 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1516 {
1517 ::new ((void*)__p) _Tp(__a0);
1518 }
1519 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1522 const _A1& __a1)
1523 {
1524 ::new ((void*)__p) _Tp(__a0, __a1);
1525 }
1526 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1529 const _A1& __a1, const _A2& __a2)
1530 {
1531 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1532 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001533#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534
1535 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537 static void destroy(allocator_type& __a, _Tp* __p)
1538 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1539
Howard Hinnant756c69b2010-09-22 16:48:34 +00001540 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001541 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1543
Howard Hinnant756c69b2010-09-22 16:48:34 +00001544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 static allocator_type
1546 select_on_container_copy_construction(const allocator_type& __a)
1547 {return select_on_container_copy_construction(
1548 __has_select_on_container_copy_construction<const allocator_type>(),
1549 __a);}
1550
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001551 template <class _Ptr>
1552 _LIBCPP_INLINE_VISIBILITY
1553 static
1554 void
1555 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1556 {
1557 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1558 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1559 }
1560
1561 template <class _Tp>
1562 _LIBCPP_INLINE_VISIBILITY
1563 static
1564 typename enable_if
1565 <
1566 (is_same<allocator_type, allocator<_Tp> >::value
1567 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1568 is_trivially_move_constructible<_Tp>::value,
1569 void
1570 >::type
1571 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1572 {
1573 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001574 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001575 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001576 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001577 __begin2 += _Np;
1578 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001579 }
1580
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001581 template <class _Iter, class _Ptr>
1582 _LIBCPP_INLINE_VISIBILITY
1583 static
1584 void
1585 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1586 {
1587 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1588 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1589 }
1590
1591 template <class _Tp>
1592 _LIBCPP_INLINE_VISIBILITY
1593 static
1594 typename enable_if
1595 <
1596 (is_same<allocator_type, allocator<_Tp> >::value
1597 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1598 is_trivially_move_constructible<_Tp>::value,
1599 void
1600 >::type
1601 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1602 {
1603 typedef typename remove_const<_Tp>::type _Vp;
1604 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001605 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001606 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001607 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001608 __begin2 += _Np;
1609 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001610 }
1611
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001612 template <class _Ptr>
1613 _LIBCPP_INLINE_VISIBILITY
1614 static
1615 void
1616 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1617 {
1618 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001619 {
1620 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1621 --__end2;
1622 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001623 }
1624
1625 template <class _Tp>
1626 _LIBCPP_INLINE_VISIBILITY
1627 static
1628 typename enable_if
1629 <
1630 (is_same<allocator_type, allocator<_Tp> >::value
1631 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1632 is_trivially_move_constructible<_Tp>::value,
1633 void
1634 >::type
1635 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1636 {
1637 ptrdiff_t _Np = __end1 - __begin1;
1638 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001639 if (_Np > 0)
1640 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001641 }
1642
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643private:
1644
Howard Hinnant756c69b2010-09-22 16:48:34 +00001645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 static pointer allocate(allocator_type& __a, size_type __n,
1647 const_void_pointer __hint, true_type)
1648 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001651 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652 {return __a.allocate(__n);}
1653
1654#ifndef _LIBCPP_HAS_NO_VARIADICS
1655 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001658 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1662 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001663 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001665#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666
1667 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1670 {__a.destroy(__p);}
1671 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673 static void __destroy(false_type, allocator_type&, _Tp* __p)
1674 {
1675 __p->~_Tp();
1676 }
1677
Howard Hinnant756c69b2010-09-22 16:48:34 +00001678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679 static size_type __max_size(true_type, const allocator_type& __a)
1680 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001683 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684
Howard Hinnant756c69b2010-09-22 16:48:34 +00001685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 static allocator_type
1687 select_on_container_copy_construction(true_type, const allocator_type& __a)
1688 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690 static allocator_type
1691 select_on_container_copy_construction(false_type, const allocator_type& __a)
1692 {return __a;}
1693};
1694
Marshall Clow940e01c2015-04-07 05:21:38 +00001695template <class _Traits, class _Tp>
1696struct __rebind_alloc_helper
1697{
1698#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Marshall Clow35cde742015-05-10 13:59:45 +00001699 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001700#else
1701 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1702#endif
1703};
1704
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705// allocator
1706
1707template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001708class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709{
1710public:
1711 typedef size_t size_type;
1712 typedef ptrdiff_t difference_type;
1713 typedef _Tp* pointer;
1714 typedef const _Tp* const_pointer;
1715 typedef _Tp& reference;
1716 typedef const _Tp& const_reference;
1717 typedef _Tp value_type;
1718
Howard Hinnant4931e092011-06-02 21:38:57 +00001719 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001720 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001721
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1723
Howard Hinnant719bda32011-05-28 14:41:13 +00001724 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1725 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1726 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001727 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001728 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001729 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001731 {
1732 if (__n > max_size())
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001733 __libcpp_throw(length_error("allocator<T>::allocate(size_t n)"
1734 " 'n' exceeds maximum supported size"));
Marshall Clow813ffb32016-03-03 12:04:39 +00001735 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1736 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001737 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001738 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001739 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1740 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001741#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742 template <class _Up, class... _Args>
1743 _LIBCPP_INLINE_VISIBILITY
1744 void
1745 construct(_Up* __p, _Args&&... __args)
1746 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001747 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001749#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750 _LIBCPP_INLINE_VISIBILITY
1751 void
1752 construct(pointer __p)
1753 {
1754 ::new((void*)__p) _Tp();
1755 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001756# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001757
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 template <class _A0>
1759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001760 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 construct(pointer __p, _A0& __a0)
1762 {
1763 ::new((void*)__p) _Tp(__a0);
1764 }
1765 template <class _A0>
1766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001767 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768 construct(pointer __p, const _A0& __a0)
1769 {
1770 ::new((void*)__p) _Tp(__a0);
1771 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001772# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 template <class _A0, class _A1>
1774 _LIBCPP_INLINE_VISIBILITY
1775 void
1776 construct(pointer __p, _A0& __a0, _A1& __a1)
1777 {
1778 ::new((void*)__p) _Tp(__a0, __a1);
1779 }
1780 template <class _A0, class _A1>
1781 _LIBCPP_INLINE_VISIBILITY
1782 void
1783 construct(pointer __p, const _A0& __a0, _A1& __a1)
1784 {
1785 ::new((void*)__p) _Tp(__a0, __a1);
1786 }
1787 template <class _A0, class _A1>
1788 _LIBCPP_INLINE_VISIBILITY
1789 void
1790 construct(pointer __p, _A0& __a0, const _A1& __a1)
1791 {
1792 ::new((void*)__p) _Tp(__a0, __a1);
1793 }
1794 template <class _A0, class _A1>
1795 _LIBCPP_INLINE_VISIBILITY
1796 void
1797 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1798 {
1799 ::new((void*)__p) _Tp(__a0, __a1);
1800 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001801#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1803};
1804
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001805template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001806class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001807{
1808public:
1809 typedef size_t size_type;
1810 typedef ptrdiff_t difference_type;
1811 typedef const _Tp* pointer;
1812 typedef const _Tp* const_pointer;
1813 typedef const _Tp& reference;
1814 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001815 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001816
1817 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001818 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001819
1820 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1821
1822 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1823 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1824 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1825 {return _VSTD::addressof(__x);}
1826 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001827 {
1828 if (__n > max_size())
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001829 __libcpp_throw(length_error("allocator<const T>::allocate(size_t n)"
1830 " 'n' exceeds maximum supported size"));
Marshall Clow813ffb32016-03-03 12:04:39 +00001831 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001832 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001833 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001834 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001835 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1836 {return size_type(~0) / sizeof(_Tp);}
1837#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1838 template <class _Up, class... _Args>
1839 _LIBCPP_INLINE_VISIBILITY
1840 void
1841 construct(_Up* __p, _Args&&... __args)
1842 {
1843 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1844 }
1845#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1846 _LIBCPP_INLINE_VISIBILITY
1847 void
1848 construct(pointer __p)
1849 {
1850 ::new((void*)__p) _Tp();
1851 }
1852# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001853
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001854 template <class _A0>
1855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001856 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001857 construct(pointer __p, _A0& __a0)
1858 {
1859 ::new((void*)__p) _Tp(__a0);
1860 }
1861 template <class _A0>
1862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001863 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001864 construct(pointer __p, const _A0& __a0)
1865 {
1866 ::new((void*)__p) _Tp(__a0);
1867 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001868# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1869 template <class _A0, class _A1>
1870 _LIBCPP_INLINE_VISIBILITY
1871 void
1872 construct(pointer __p, _A0& __a0, _A1& __a1)
1873 {
1874 ::new((void*)__p) _Tp(__a0, __a1);
1875 }
1876 template <class _A0, class _A1>
1877 _LIBCPP_INLINE_VISIBILITY
1878 void
1879 construct(pointer __p, const _A0& __a0, _A1& __a1)
1880 {
1881 ::new((void*)__p) _Tp(__a0, __a1);
1882 }
1883 template <class _A0, class _A1>
1884 _LIBCPP_INLINE_VISIBILITY
1885 void
1886 construct(pointer __p, _A0& __a0, const _A1& __a1)
1887 {
1888 ::new((void*)__p) _Tp(__a0, __a1);
1889 }
1890 template <class _A0, class _A1>
1891 _LIBCPP_INLINE_VISIBILITY
1892 void
1893 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1894 {
1895 ::new((void*)__p) _Tp(__a0, __a1);
1896 }
1897#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1898 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1899};
1900
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901template <class _Tp, class _Up>
1902inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001903bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904
1905template <class _Tp, class _Up>
1906inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001907bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908
1909template <class _OutputIterator, class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001910class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911 : public iterator<output_iterator_tag,
1912 _Tp, // purposefully not C++03
1913 ptrdiff_t, // purposefully not C++03
1914 _Tp*, // purposefully not C++03
1915 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1916{
1917private:
1918 _OutputIterator __x_;
1919public:
1920 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1921 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1922 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1923 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001924#if _LIBCPP_STD_VER >= 14
1925 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1926 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1927#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1929 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1930 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001931#if _LIBCPP_STD_VER >= 14
1932 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1933#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934};
1935
1936template <class _Tp>
1937pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001938get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939{
1940 pair<_Tp*, ptrdiff_t> __r(0, 0);
1941 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1942 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1943 / sizeof(_Tp);
1944 if (__n > __m)
1945 __n = __m;
1946 while (__n > 0)
1947 {
1948 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1949 if (__r.first)
1950 {
1951 __r.second = __n;
1952 break;
1953 }
1954 __n /= 2;
1955 }
1956 return __r;
1957}
1958
1959template <class _Tp>
1960inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001961void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962
1963template <class _Tp>
1964struct auto_ptr_ref
1965{
1966 _Tp* __ptr_;
1967};
1968
1969template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001970class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971{
1972private:
1973 _Tp* __ptr_;
1974public:
1975 typedef _Tp element_type;
1976
1977 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1978 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1979 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1980 : __ptr_(__p.release()) {}
1981 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1982 {reset(__p.release()); return *this;}
1983 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1984 {reset(__p.release()); return *this;}
1985 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1986 {reset(__p.__ptr_); return *this;}
1987 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1988
1989 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1990 {return *__ptr_;}
1991 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1992 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1993 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1994 {
1995 _Tp* __t = __ptr_;
1996 __ptr_ = 0;
1997 return __t;
1998 }
1999 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2000 {
2001 if (__ptr_ != __p)
2002 delete __ptr_;
2003 __ptr_ = __p;
2004 }
2005
2006 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2007 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2008 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2009 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2010 {return auto_ptr<_Up>(release());}
2011};
2012
2013template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002014class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015{
2016public:
2017 typedef void element_type;
2018};
2019
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2021 typename remove_cv<_T2>::type>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002022 bool = is_empty<_T1>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002023 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002024 bool = is_empty<_T2>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002025 && !__libcpp_is_final<_T2>::value
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002026 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027struct __libcpp_compressed_pair_switch;
2028
2029template <class _T1, class _T2, bool IsSame>
2030struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2031
2032template <class _T1, class _T2, bool IsSame>
2033struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2034
2035template <class _T1, class _T2, bool IsSame>
2036struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2037
2038template <class _T1, class _T2>
2039struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2040
2041template <class _T1, class _T2>
2042struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2043
2044template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2045class __libcpp_compressed_pair_imp;
2046
2047template <class _T1, class _T2>
2048class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2049{
2050private:
2051 _T1 __first_;
2052 _T2 __second_;
2053public:
2054 typedef _T1 _T1_param;
2055 typedef _T2 _T2_param;
2056
2057 typedef typename remove_reference<_T1>::type& _T1_reference;
2058 typedef typename remove_reference<_T2>::type& _T2_reference;
2059
2060 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2061 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2062
Marshall Clowac92bfe2015-07-16 03:05:06 +00002063 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002064 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002065 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002066 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002067 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002069 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070
Howard Hinnant59f73012013-11-13 00:39:22 +00002071#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002072
2073 _LIBCPP_INLINE_VISIBILITY
2074 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2075 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2076 is_nothrow_copy_constructible<_T2>::value)
2077 : __first_(__p.first()),
2078 __second_(__p.second()) {}
2079
2080 _LIBCPP_INLINE_VISIBILITY
2081 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2082 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2083 is_nothrow_copy_assignable<_T2>::value)
2084 {
2085 __first_ = __p.first();
2086 __second_ = __p.second();
2087 return *this;
2088 }
2089
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002090 _LIBCPP_INLINE_VISIBILITY
2091 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002092 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2093 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002094 : __first_(_VSTD::forward<_T1>(__p.first())),
2095 __second_(_VSTD::forward<_T2>(__p.second())) {}
2096
2097 _LIBCPP_INLINE_VISIBILITY
2098 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2099 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2100 is_nothrow_move_assignable<_T2>::value)
2101 {
2102 __first_ = _VSTD::forward<_T1>(__p.first());
2103 __second_ = _VSTD::forward<_T2>(__p.second());
2104 return *this;
2105 }
2106
Howard Hinnant59f73012013-11-13 00:39:22 +00002107#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002108
Howard Hinnant83b1c052011-12-19 17:58:44 +00002109#ifndef _LIBCPP_HAS_NO_VARIADICS
2110
2111 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2112 _LIBCPP_INLINE_VISIBILITY
2113 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2114 tuple<_Args1...> __first_args,
2115 tuple<_Args2...> __second_args,
2116 __tuple_indices<_I1...>,
2117 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002118 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2119 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002120 {}
2121
2122#endif // _LIBCPP_HAS_NO_VARIADICS
2123
Howard Hinnant719bda32011-05-28 14:41:13 +00002124 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2125 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126
Howard Hinnant719bda32011-05-28 14:41:13 +00002127 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2128 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129
2130 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002131 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002132 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002134 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135 swap(__first_, __x.__first_);
2136 swap(__second_, __x.__second_);
2137 }
2138};
2139
2140template <class _T1, class _T2>
2141class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2142 : private _T1
2143{
2144private:
2145 _T2 __second_;
2146public:
2147 typedef _T1 _T1_param;
2148 typedef _T2 _T2_param;
2149
2150 typedef _T1& _T1_reference;
2151 typedef typename remove_reference<_T2>::type& _T2_reference;
2152
2153 typedef const _T1& _T1_const_reference;
2154 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2155
Marshall Clowac92bfe2015-07-16 03:05:06 +00002156 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002157 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002158 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002159 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002160 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002162 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163
Howard Hinnant59f73012013-11-13 00:39:22 +00002164#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002165
2166 _LIBCPP_INLINE_VISIBILITY
2167 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2168 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2169 is_nothrow_copy_constructible<_T2>::value)
2170 : _T1(__p.first()), __second_(__p.second()) {}
2171
2172 _LIBCPP_INLINE_VISIBILITY
2173 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2174 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2175 is_nothrow_copy_assignable<_T2>::value)
2176 {
2177 _T1::operator=(__p.first());
2178 __second_ = __p.second();
2179 return *this;
2180 }
2181
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002182 _LIBCPP_INLINE_VISIBILITY
2183 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002184 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2185 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002186 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002187
2188 _LIBCPP_INLINE_VISIBILITY
2189 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2190 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2191 is_nothrow_move_assignable<_T2>::value)
2192 {
2193 _T1::operator=(_VSTD::move(__p.first()));
2194 __second_ = _VSTD::forward<_T2>(__p.second());
2195 return *this;
2196 }
2197
Howard Hinnant59f73012013-11-13 00:39:22 +00002198#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002199
Howard Hinnant83b1c052011-12-19 17:58:44 +00002200#ifndef _LIBCPP_HAS_NO_VARIADICS
2201
2202 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2203 _LIBCPP_INLINE_VISIBILITY
2204 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2205 tuple<_Args1...> __first_args,
2206 tuple<_Args2...> __second_args,
2207 __tuple_indices<_I1...>,
2208 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002209 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2210 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002211 {}
2212
2213#endif // _LIBCPP_HAS_NO_VARIADICS
2214
Howard Hinnant719bda32011-05-28 14:41:13 +00002215 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2216 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217
Howard Hinnant719bda32011-05-28 14:41:13 +00002218 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2219 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
2221 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002222 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002223 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002225 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226 swap(__second_, __x.__second_);
2227 }
2228};
2229
2230template <class _T1, class _T2>
2231class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2232 : private _T2
2233{
2234private:
2235 _T1 __first_;
2236public:
2237 typedef _T1 _T1_param;
2238 typedef _T2 _T2_param;
2239
2240 typedef typename remove_reference<_T1>::type& _T1_reference;
2241 typedef _T2& _T2_reference;
2242
2243 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2244 typedef const _T2& _T2_const_reference;
2245
Marshall Clowac92bfe2015-07-16 03:05:06 +00002246 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002248 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002250 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002252 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2253 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002254 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255
Howard Hinnant59f73012013-11-13 00:39:22 +00002256#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002257
2258 _LIBCPP_INLINE_VISIBILITY
2259 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2260 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2261 is_nothrow_copy_constructible<_T2>::value)
2262 : _T2(__p.second()), __first_(__p.first()) {}
2263
2264 _LIBCPP_INLINE_VISIBILITY
2265 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2266 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2267 is_nothrow_copy_assignable<_T2>::value)
2268 {
2269 _T2::operator=(__p.second());
2270 __first_ = __p.first();
2271 return *this;
2272 }
2273
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002276 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2277 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002278 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002279
2280 _LIBCPP_INLINE_VISIBILITY
2281 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2282 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2283 is_nothrow_move_assignable<_T2>::value)
2284 {
2285 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2286 __first_ = _VSTD::move(__p.first());
2287 return *this;
2288 }
2289
Howard Hinnant59f73012013-11-13 00:39:22 +00002290#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002291
Howard Hinnant83b1c052011-12-19 17:58:44 +00002292#ifndef _LIBCPP_HAS_NO_VARIADICS
2293
2294 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2295 _LIBCPP_INLINE_VISIBILITY
2296 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2297 tuple<_Args1...> __first_args,
2298 tuple<_Args2...> __second_args,
2299 __tuple_indices<_I1...>,
2300 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002301 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2302 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002303
2304 {}
2305
2306#endif // _LIBCPP_HAS_NO_VARIADICS
2307
Howard Hinnant719bda32011-05-28 14:41:13 +00002308 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2309 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310
Howard Hinnant719bda32011-05-28 14:41:13 +00002311 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2312 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313
2314 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002315 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002316 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002318 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319 swap(__first_, __x.__first_);
2320 }
2321};
2322
2323template <class _T1, class _T2>
2324class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2325 : private _T1,
2326 private _T2
2327{
2328public:
2329 typedef _T1 _T1_param;
2330 typedef _T2 _T2_param;
2331
2332 typedef _T1& _T1_reference;
2333 typedef _T2& _T2_reference;
2334
2335 typedef const _T1& _T1_const_reference;
2336 typedef const _T2& _T2_const_reference;
2337
2338 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2339 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002340 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002342 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002344 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345
Howard Hinnant59f73012013-11-13 00:39:22 +00002346#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002347
2348 _LIBCPP_INLINE_VISIBILITY
2349 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2350 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2351 is_nothrow_copy_constructible<_T2>::value)
2352 : _T1(__p.first()), _T2(__p.second()) {}
2353
2354 _LIBCPP_INLINE_VISIBILITY
2355 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2356 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2357 is_nothrow_copy_assignable<_T2>::value)
2358 {
2359 _T1::operator=(__p.first());
2360 _T2::operator=(__p.second());
2361 return *this;
2362 }
2363
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002364 _LIBCPP_INLINE_VISIBILITY
2365 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002366 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2367 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002368 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002369
2370 _LIBCPP_INLINE_VISIBILITY
2371 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2372 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2373 is_nothrow_move_assignable<_T2>::value)
2374 {
2375 _T1::operator=(_VSTD::move(__p.first()));
2376 _T2::operator=(_VSTD::move(__p.second()));
2377 return *this;
2378 }
2379
Howard Hinnant59f73012013-11-13 00:39:22 +00002380#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002381
Howard Hinnant83b1c052011-12-19 17:58:44 +00002382#ifndef _LIBCPP_HAS_NO_VARIADICS
2383
2384 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2385 _LIBCPP_INLINE_VISIBILITY
2386 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2387 tuple<_Args1...> __first_args,
2388 tuple<_Args2...> __second_args,
2389 __tuple_indices<_I1...>,
2390 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002391 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2392 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002393 {}
2394
2395#endif // _LIBCPP_HAS_NO_VARIADICS
2396
Howard Hinnant719bda32011-05-28 14:41:13 +00002397 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2398 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399
Howard Hinnant719bda32011-05-28 14:41:13 +00002400 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2401 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402
Howard Hinnant28b24882011-12-01 20:21:04 +00002403 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002404 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002405 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002406 {
2407 }
2408};
2409
2410template <class _T1, class _T2>
2411class __compressed_pair
2412 : private __libcpp_compressed_pair_imp<_T1, _T2>
2413{
2414 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2415public:
2416 typedef typename base::_T1_param _T1_param;
2417 typedef typename base::_T2_param _T2_param;
2418
2419 typedef typename base::_T1_reference _T1_reference;
2420 typedef typename base::_T2_reference _T2_reference;
2421
2422 typedef typename base::_T1_const_reference _T1_const_reference;
2423 typedef typename base::_T2_const_reference _T2_const_reference;
2424
2425 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002426 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002427 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002428 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002429 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002431 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432
Howard Hinnant59f73012013-11-13 00:39:22 +00002433#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002434
2435 _LIBCPP_INLINE_VISIBILITY
2436 __compressed_pair(const __compressed_pair& __p)
2437 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2438 is_nothrow_copy_constructible<_T2>::value)
2439 : base(__p) {}
2440
2441 _LIBCPP_INLINE_VISIBILITY
2442 __compressed_pair& operator=(const __compressed_pair& __p)
2443 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2444 is_nothrow_copy_assignable<_T2>::value)
2445 {
2446 base::operator=(__p);
2447 return *this;
2448 }
2449
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002452 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2453 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002454 : base(_VSTD::move(__p)) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002455
2456 _LIBCPP_INLINE_VISIBILITY
2457 __compressed_pair& operator=(__compressed_pair&& __p)
2458 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2459 is_nothrow_move_assignable<_T2>::value)
2460 {
2461 base::operator=(_VSTD::move(__p));
2462 return *this;
2463 }
Howard Hinnant83b1c052011-12-19 17:58:44 +00002464
Howard Hinnant59f73012013-11-13 00:39:22 +00002465#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002466
Howard Hinnant83b1c052011-12-19 17:58:44 +00002467#ifndef _LIBCPP_HAS_NO_VARIADICS
2468
2469 template <class... _Args1, class... _Args2>
2470 _LIBCPP_INLINE_VISIBILITY
2471 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2472 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002473 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002474 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2475 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2476 {}
2477
2478#endif // _LIBCPP_HAS_NO_VARIADICS
2479
Howard Hinnant719bda32011-05-28 14:41:13 +00002480 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2481 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482
Howard Hinnant719bda32011-05-28 14:41:13 +00002483 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2484 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485
Howard Hinnant719bda32011-05-28 14:41:13 +00002486 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2487 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002488 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002489 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490};
2491
2492template <class _T1, class _T2>
2493inline _LIBCPP_INLINE_VISIBILITY
2494void
2495swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002496 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002497 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 {__x.swap(__y);}
2499
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002500// __same_or_less_cv_qualified
2501
2502template <class _Ptr1, class _Ptr2,
2503 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2504 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2505 >::value
2506 >
2507struct __same_or_less_cv_qualified_imp
2508 : is_convertible<_Ptr1, _Ptr2> {};
2509
2510template <class _Ptr1, class _Ptr2>
2511struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2512 : false_type {};
2513
Marshall Clowdf296162014-04-26 05:19:48 +00002514template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2515 is_same<_Ptr1, _Ptr2>::value ||
2516 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002517struct __same_or_less_cv_qualified
2518 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2519
2520template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002521struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002522 : false_type {};
2523
2524// default_delete
2525
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002527struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528{
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002529#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2530 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2531#else
2532 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2533#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534 template <class _Up>
2535 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002536 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2537 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538 {
2539 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002540 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541 delete __ptr;
2542 }
2543};
2544
2545template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002546struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002548public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002549#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2550 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2551#else
2552 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2553#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002554 template <class _Up>
2555 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002556 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002557 template <class _Up>
2558 _LIBCPP_INLINE_VISIBILITY
2559 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002560 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561 {
2562 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002563 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564 delete [] __ptr;
2565 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566};
2567
2568template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002569class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570{
2571public:
2572 typedef _Tp element_type;
2573 typedef _Dp deleter_type;
2574 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2575private:
2576 __compressed_pair<pointer, deleter_type> __ptr_;
2577
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002578#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579 unique_ptr(unique_ptr&);
2580 template <class _Up, class _Ep>
2581 unique_ptr(unique_ptr<_Up, _Ep>&);
2582 unique_ptr& operator=(unique_ptr&);
2583 template <class _Up, class _Ep>
2584 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002585#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586
2587 struct __nat {int __for_bool_;};
2588
2589 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2590 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2591public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002592 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 : __ptr_(pointer())
2594 {
2595 static_assert(!is_pointer<deleter_type>::value,
2596 "unique_ptr constructed with null function pointer deleter");
2597 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002598 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599 : __ptr_(pointer())
2600 {
2601 static_assert(!is_pointer<deleter_type>::value,
2602 "unique_ptr constructed with null function pointer deleter");
2603 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002604 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002605 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606 {
2607 static_assert(!is_pointer<deleter_type>::value,
2608 "unique_ptr constructed with null function pointer deleter");
2609 }
2610
Howard Hinnant74279a52010-09-04 23:28:19 +00002611#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2613 is_reference<deleter_type>::value,
2614 deleter_type,
2615 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002616 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617 : __ptr_(__p, __d) {}
2618
2619 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002620 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002621 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 {
2623 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2624 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002625 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002626 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627 template <class _Up, class _Ep>
2628 _LIBCPP_INLINE_VISIBILITY
2629 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2630 typename enable_if
2631 <
2632 !is_array<_Up>::value &&
2633 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2634 is_convertible<_Ep, deleter_type>::value &&
2635 (
2636 !is_reference<deleter_type>::value ||
2637 is_same<deleter_type, _Ep>::value
2638 ),
2639 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002640 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002641 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642
2643 template <class _Up>
2644 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2645 typename enable_if<
2646 is_convertible<_Up*, _Tp*>::value &&
2647 is_same<_Dp, default_delete<_Tp> >::value,
2648 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002649 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 : __ptr_(__p.release())
2651 {
2652 }
2653
Howard Hinnant719bda32011-05-28 14:41:13 +00002654 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 {
2656 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002657 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658 return *this;
2659 }
2660
2661 template <class _Up, class _Ep>
2662 _LIBCPP_INLINE_VISIBILITY
2663 typename enable_if
2664 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002665 !is_array<_Up>::value &&
2666 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2667 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668 unique_ptr&
2669 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002670 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671 {
2672 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002673 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674 return *this;
2675 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002676#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677
2678 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2679 {
2680 return __rv<unique_ptr>(*this);
2681 }
2682
2683 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002684 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685
2686 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002687 _LIBCPP_INLINE_VISIBILITY
2688 typename enable_if<
2689 !is_array<_Up>::value &&
2690 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2691 is_assignable<deleter_type&, _Ep&>::value,
2692 unique_ptr&
2693 >::type
2694 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 {
2696 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002697 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 return *this;
2699 }
2700
2701 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002702 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703
2704 template <class _Up>
2705 _LIBCPP_INLINE_VISIBILITY
2706 typename enable_if<
2707 is_convertible<_Up*, _Tp*>::value &&
2708 is_same<_Dp, default_delete<_Tp> >::value,
2709 unique_ptr&
2710 >::type
2711 operator=(auto_ptr<_Up> __p)
2712 {reset(__p.release()); return *this;}
2713
Howard Hinnant74279a52010-09-04 23:28:19 +00002714#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2716
Howard Hinnant719bda32011-05-28 14:41:13 +00002717 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 {
2719 reset();
2720 return *this;
2721 }
2722
2723 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2724 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002725 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2726 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2727 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2728 {return __ptr_.second();}
2729 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2730 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002731 _LIBCPP_INLINE_VISIBILITY
2732 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2733 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734
Howard Hinnant719bda32011-05-28 14:41:13 +00002735 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736 {
2737 pointer __t = __ptr_.first();
2738 __ptr_.first() = pointer();
2739 return __t;
2740 }
2741
Howard Hinnant719bda32011-05-28 14:41:13 +00002742 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743 {
2744 pointer __tmp = __ptr_.first();
2745 __ptr_.first() = __p;
2746 if (__tmp)
2747 __ptr_.second()(__tmp);
2748 }
2749
Howard Hinnant719bda32011-05-28 14:41:13 +00002750 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2751 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752};
2753
2754template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002755class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756{
2757public:
2758 typedef _Tp element_type;
2759 typedef _Dp deleter_type;
2760 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2761private:
2762 __compressed_pair<pointer, deleter_type> __ptr_;
2763
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002764#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765 unique_ptr(unique_ptr&);
2766 template <class _Up>
2767 unique_ptr(unique_ptr<_Up>&);
2768 unique_ptr& operator=(unique_ptr&);
2769 template <class _Up>
2770 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002771#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772
2773 struct __nat {int __for_bool_;};
2774
2775 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2776 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2777public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002778 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 : __ptr_(pointer())
2780 {
2781 static_assert(!is_pointer<deleter_type>::value,
2782 "unique_ptr constructed with null function pointer deleter");
2783 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002784 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785 : __ptr_(pointer())
2786 {
2787 static_assert(!is_pointer<deleter_type>::value,
2788 "unique_ptr constructed with null function pointer deleter");
2789 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002790#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002791 template <class _Pp>
2792 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2793 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794 : __ptr_(__p)
2795 {
2796 static_assert(!is_pointer<deleter_type>::value,
2797 "unique_ptr constructed with null function pointer deleter");
2798 }
2799
Logan Chiend435f8b2014-01-31 09:30:46 +00002800 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002801 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 is_reference<deleter_type>::value,
2803 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002804 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2805 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002806 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 : __ptr_(__p, __d) {}
2808
2809 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2810 is_reference<deleter_type>::value,
2811 deleter_type,
2812 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002813 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814 : __ptr_(pointer(), __d) {}
2815
Logan Chiend435f8b2014-01-31 09:30:46 +00002816 template <class _Pp>
2817 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2818 typename remove_reference<deleter_type>::type&& __d,
2819 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002820 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002821 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 {
2823 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2824 }
2825
2826 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002827 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002828 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 {
2830 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2831 }
2832
Howard Hinnant719bda32011-05-28 14:41:13 +00002833 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002834 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835
Howard Hinnant719bda32011-05-28 14:41:13 +00002836 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 {
2838 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002839 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 return *this;
2841 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002842
2843 template <class _Up, class _Ep>
2844 _LIBCPP_INLINE_VISIBILITY
2845 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2846 typename enable_if
2847 <
2848 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002849 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002850 && is_convertible<_Ep, deleter_type>::value &&
2851 (
2852 !is_reference<deleter_type>::value ||
2853 is_same<deleter_type, _Ep>::value
2854 ),
2855 __nat
2856 >::type = __nat()
2857 ) _NOEXCEPT
2858 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2859
2860
2861 template <class _Up, class _Ep>
2862 _LIBCPP_INLINE_VISIBILITY
2863 typename enable_if
2864 <
2865 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002866 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2867 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002868 unique_ptr&
2869 >::type
2870 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2871 {
2872 reset(__u.release());
2873 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2874 return *this;
2875 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002876#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877
2878 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2879 : __ptr_(__p)
2880 {
2881 static_assert(!is_pointer<deleter_type>::value,
2882 "unique_ptr constructed with null function pointer deleter");
2883 }
2884
2885 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002886 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887
2888 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002889 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890
2891 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2892 {
2893 return __rv<unique_ptr>(*this);
2894 }
2895
2896 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002897 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898
2899 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2900 {
2901 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002902 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903 return *this;
2904 }
2905
Howard Hinnant74279a52010-09-04 23:28:19 +00002906#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2908
Howard Hinnant719bda32011-05-28 14:41:13 +00002909 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910 {
2911 reset();
2912 return *this;
2913 }
2914
2915 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2916 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002917 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2918 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2919 {return __ptr_.second();}
2920 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2921 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002922 _LIBCPP_INLINE_VISIBILITY
2923 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2924 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925
Howard Hinnant719bda32011-05-28 14:41:13 +00002926 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 {
2928 pointer __t = __ptr_.first();
2929 __ptr_.first() = pointer();
2930 return __t;
2931 }
2932
Logan Chiend435f8b2014-01-31 09:30:46 +00002933 template <class _Pp>
2934 _LIBCPP_INLINE_VISIBILITY
2935 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2936 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937 {
2938 pointer __tmp = __ptr_.first();
2939 __ptr_.first() = __p;
2940 if (__tmp)
2941 __ptr_.second()(__tmp);
2942 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002943 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 {
2945 pointer __tmp = __ptr_.first();
2946 __ptr_.first() = nullptr;
2947 if (__tmp)
2948 __ptr_.second()(__tmp);
2949 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950
2951 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2952private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002953
Howard Hinnant74279a52010-09-04 23:28:19 +00002954#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955 template <class _Up>
2956 explicit unique_ptr(_Up);
2957 template <class _Up>
2958 unique_ptr(_Up __u,
2959 typename conditional<
2960 is_reference<deleter_type>::value,
2961 deleter_type,
2962 typename add_lvalue_reference<const deleter_type>::type>::type,
2963 typename enable_if
2964 <
2965 is_convertible<_Up, pointer>::value,
2966 __nat
2967 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00002968#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969};
2970
2971template <class _Tp, class _Dp>
2972inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002973typename enable_if<
2974 __is_swappable<_Dp>::value,
2975 void
2976>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002977swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978
2979template <class _T1, class _D1, class _T2, class _D2>
2980inline _LIBCPP_INLINE_VISIBILITY
2981bool
2982operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2983
2984template <class _T1, class _D1, class _T2, class _D2>
2985inline _LIBCPP_INLINE_VISIBILITY
2986bool
2987operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2988
2989template <class _T1, class _D1, class _T2, class _D2>
2990inline _LIBCPP_INLINE_VISIBILITY
2991bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002992operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2993{
2994 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2995 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002996 typedef typename common_type<_P1, _P2>::type _Vp;
2997 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002998}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999
3000template <class _T1, class _D1, class _T2, class _D2>
3001inline _LIBCPP_INLINE_VISIBILITY
3002bool
3003operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
3004
3005template <class _T1, class _D1, class _T2, class _D2>
3006inline _LIBCPP_INLINE_VISIBILITY
3007bool
3008operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
3009
3010template <class _T1, class _D1, class _T2, class _D2>
3011inline _LIBCPP_INLINE_VISIBILITY
3012bool
3013operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
3014
Howard Hinnantb17caf92012-02-21 21:02:58 +00003015template <class _T1, class _D1>
3016inline _LIBCPP_INLINE_VISIBILITY
3017bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003018operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003019{
3020 return !__x;
3021}
3022
3023template <class _T1, class _D1>
3024inline _LIBCPP_INLINE_VISIBILITY
3025bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003026operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003027{
3028 return !__x;
3029}
3030
3031template <class _T1, class _D1>
3032inline _LIBCPP_INLINE_VISIBILITY
3033bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003034operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003035{
3036 return static_cast<bool>(__x);
3037}
3038
3039template <class _T1, class _D1>
3040inline _LIBCPP_INLINE_VISIBILITY
3041bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003042operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003043{
3044 return static_cast<bool>(__x);
3045}
3046
3047template <class _T1, class _D1>
3048inline _LIBCPP_INLINE_VISIBILITY
3049bool
3050operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3051{
3052 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3053 return less<_P1>()(__x.get(), nullptr);
3054}
3055
3056template <class _T1, class _D1>
3057inline _LIBCPP_INLINE_VISIBILITY
3058bool
3059operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3060{
3061 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3062 return less<_P1>()(nullptr, __x.get());
3063}
3064
3065template <class _T1, class _D1>
3066inline _LIBCPP_INLINE_VISIBILITY
3067bool
3068operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3069{
3070 return nullptr < __x;
3071}
3072
3073template <class _T1, class _D1>
3074inline _LIBCPP_INLINE_VISIBILITY
3075bool
3076operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3077{
3078 return __x < nullptr;
3079}
3080
3081template <class _T1, class _D1>
3082inline _LIBCPP_INLINE_VISIBILITY
3083bool
3084operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3085{
3086 return !(nullptr < __x);
3087}
3088
3089template <class _T1, class _D1>
3090inline _LIBCPP_INLINE_VISIBILITY
3091bool
3092operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3093{
3094 return !(__x < nullptr);
3095}
3096
3097template <class _T1, class _D1>
3098inline _LIBCPP_INLINE_VISIBILITY
3099bool
3100operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3101{
3102 return !(__x < nullptr);
3103}
3104
3105template <class _T1, class _D1>
3106inline _LIBCPP_INLINE_VISIBILITY
3107bool
3108operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3109{
3110 return !(nullptr < __x);
3111}
3112
Howard Hinnant9e028f12012-05-01 15:37:54 +00003113#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3114
3115template <class _Tp, class _Dp>
3116inline _LIBCPP_INLINE_VISIBILITY
3117unique_ptr<_Tp, _Dp>
3118move(unique_ptr<_Tp, _Dp>& __t)
3119{
3120 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3121}
3122
3123#endif
3124
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003125#if _LIBCPP_STD_VER > 11
3126
3127template<class _Tp>
3128struct __unique_if
3129{
3130 typedef unique_ptr<_Tp> __unique_single;
3131};
3132
3133template<class _Tp>
3134struct __unique_if<_Tp[]>
3135{
3136 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3137};
3138
3139template<class _Tp, size_t _Np>
3140struct __unique_if<_Tp[_Np]>
3141{
3142 typedef void __unique_array_known_bound;
3143};
3144
3145template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003146inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003147typename __unique_if<_Tp>::__unique_single
3148make_unique(_Args&&... __args)
3149{
3150 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3151}
3152
3153template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003154inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003155typename __unique_if<_Tp>::__unique_array_unknown_bound
3156make_unique(size_t __n)
3157{
3158 typedef typename remove_extent<_Tp>::type _Up;
3159 return unique_ptr<_Tp>(new _Up[__n]());
3160}
3161
3162template<class _Tp, class... _Args>
3163 typename __unique_if<_Tp>::__unique_array_known_bound
3164 make_unique(_Args&&...) = delete;
3165
3166#endif // _LIBCPP_STD_VER > 11
3167
Howard Hinnant6a855442013-07-03 17:39:28 +00003168template <class _Size>
3169inline _LIBCPP_INLINE_VISIBILITY
3170_Size
3171__loadword(const void* __p)
3172{
3173 _Size __r;
3174 std::memcpy(&__r, __p, sizeof(__r));
3175 return __r;
3176}
3177
Howard Hinnantf1973402011-12-10 20:28:56 +00003178// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3179// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3180// multiplication, which can be very slow on 32-bit systems.
Howard Hinnantad71c232011-12-05 00:08:45 +00003181template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantf1973402011-12-10 20:28:56 +00003182struct __murmur2_or_cityhash;
Howard Hinnantad71c232011-12-05 00:08:45 +00003183
3184template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003185struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnantad71c232011-12-05 00:08:45 +00003186{
3187 _Size operator()(const void* __key, _Size __len);
3188};
3189
Howard Hinnantf1973402011-12-10 20:28:56 +00003190// murmur2
Howard Hinnantad71c232011-12-05 00:08:45 +00003191template <class _Size>
3192_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003193__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003194{
3195 const _Size __m = 0x5bd1e995;
3196 const _Size __r = 24;
3197 _Size __h = __len;
3198 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3199 for (; __len >= 4; __data += 4, __len -= 4)
3200 {
Howard Hinnant6a855442013-07-03 17:39:28 +00003201 _Size __k = __loadword<_Size>(__data);
Howard Hinnantad71c232011-12-05 00:08:45 +00003202 __k *= __m;
3203 __k ^= __k >> __r;
3204 __k *= __m;
3205 __h *= __m;
3206 __h ^= __k;
3207 }
3208 switch (__len)
3209 {
3210 case 3:
3211 __h ^= __data[2] << 16;
3212 case 2:
3213 __h ^= __data[1] << 8;
3214 case 1:
3215 __h ^= __data[0];
3216 __h *= __m;
3217 }
3218 __h ^= __h >> 13;
3219 __h *= __m;
3220 __h ^= __h >> 15;
3221 return __h;
3222}
3223
3224template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003225struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnantad71c232011-12-05 00:08:45 +00003226{
3227 _Size operator()(const void* __key, _Size __len);
Howard Hinnantf1973402011-12-10 20:28:56 +00003228
3229 private:
3230 // Some primes between 2^63 and 2^64.
3231 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3232 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3233 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3234 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3235
3236 static _Size __rotate(_Size __val, int __shift) {
3237 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3238 }
3239
3240 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3241 return (__val >> __shift) | (__val << (64 - __shift));
3242 }
3243
3244 static _Size __shift_mix(_Size __val) {
3245 return __val ^ (__val >> 47);
3246 }
3247
3248 static _Size __hash_len_16(_Size __u, _Size __v) {
3249 const _Size __mul = 0x9ddfea08eb382d69ULL;
3250 _Size __a = (__u ^ __v) * __mul;
3251 __a ^= (__a >> 47);
3252 _Size __b = (__v ^ __a) * __mul;
3253 __b ^= (__b >> 47);
3254 __b *= __mul;
3255 return __b;
3256 }
3257
3258 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3259 if (__len > 8) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003260 const _Size __a = __loadword<_Size>(__s);
3261 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003262 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3263 }
3264 if (__len >= 4) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003265 const uint32_t __a = __loadword<uint32_t>(__s);
3266 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantf1973402011-12-10 20:28:56 +00003267 return __hash_len_16(__len + (__a << 3), __b);
3268 }
3269 if (__len > 0) {
3270 const unsigned char __a = __s[0];
3271 const unsigned char __b = __s[__len >> 1];
3272 const unsigned char __c = __s[__len - 1];
3273 const uint32_t __y = static_cast<uint32_t>(__a) +
3274 (static_cast<uint32_t>(__b) << 8);
3275 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3276 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3277 }
3278 return __k2;
3279 }
3280
3281 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003282 const _Size __a = __loadword<_Size>(__s) * __k1;
3283 const _Size __b = __loadword<_Size>(__s + 8);
3284 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3285 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003286 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3287 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3288 }
3289
3290 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3291 // Callers do best to use "random-looking" values for a and b.
3292 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3293 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3294 __a += __w;
3295 __b = __rotate(__b + __a + __z, 21);
3296 const _Size __c = __a;
3297 __a += __x;
3298 __a += __y;
3299 __b += __rotate(__a, 44);
3300 return pair<_Size, _Size>(__a + __z, __b + __c);
3301 }
3302
3303 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3304 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3305 const char* __s, _Size __a, _Size __b) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003306 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3307 __loadword<_Size>(__s + 8),
3308 __loadword<_Size>(__s + 16),
3309 __loadword<_Size>(__s + 24),
Howard Hinnantf1973402011-12-10 20:28:56 +00003310 __a,
3311 __b);
3312 }
3313
3314 // Return an 8-byte hash for 33 to 64 bytes.
3315 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003316 _Size __z = __loadword<_Size>(__s + 24);
3317 _Size __a = __loadword<_Size>(__s) +
3318 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003319 _Size __b = __rotate(__a + __z, 52);
3320 _Size __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003321 __a += __loadword<_Size>(__s + 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003322 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003323 __a += __loadword<_Size>(__s + 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003324 _Size __vf = __a + __z;
3325 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant6a855442013-07-03 17:39:28 +00003326 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3327 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003328 __b = __rotate(__a + __z, 52);
3329 __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003330 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantf1973402011-12-10 20:28:56 +00003331 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003332 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003333 _Size __wf = __a + __z;
3334 _Size __ws = __b + __rotate(__a, 31) + __c;
3335 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3336 return __shift_mix(__r * __k0 + __vs) * __k2;
3337 }
Howard Hinnantad71c232011-12-05 00:08:45 +00003338};
3339
Howard Hinnantf1973402011-12-10 20:28:56 +00003340// cityhash64
Howard Hinnantad71c232011-12-05 00:08:45 +00003341template <class _Size>
3342_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003343__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003344{
Howard Hinnantf1973402011-12-10 20:28:56 +00003345 const char* __s = static_cast<const char*>(__key);
3346 if (__len <= 32) {
3347 if (__len <= 16) {
3348 return __hash_len_0_to_16(__s, __len);
3349 } else {
3350 return __hash_len_17_to_32(__s, __len);
Howard Hinnantad71c232011-12-05 00:08:45 +00003351 }
Howard Hinnantf1973402011-12-10 20:28:56 +00003352 } else if (__len <= 64) {
3353 return __hash_len_33_to_64(__s, __len);
3354 }
3355
3356 // For strings over 64 bytes we hash the end first, and then as we
3357 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant6a855442013-07-03 17:39:28 +00003358 _Size __x = __loadword<_Size>(__s + __len - 40);
3359 _Size __y = __loadword<_Size>(__s + __len - 16) +
3360 __loadword<_Size>(__s + __len - 56);
3361 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3362 __loadword<_Size>(__s + __len - 24));
Howard Hinnantf1973402011-12-10 20:28:56 +00003363 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3364 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant6a855442013-07-03 17:39:28 +00003365 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantf1973402011-12-10 20:28:56 +00003366
3367 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3368 __len = (__len - 1) & ~static_cast<_Size>(63);
3369 do {
Howard Hinnant6a855442013-07-03 17:39:28 +00003370 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3371 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantf1973402011-12-10 20:28:56 +00003372 __x ^= __w.second;
Howard Hinnant6a855442013-07-03 17:39:28 +00003373 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantf1973402011-12-10 20:28:56 +00003374 __z = __rotate(__z + __w.first, 33) * __k1;
3375 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3376 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant6a855442013-07-03 17:39:28 +00003377 __y + __loadword<_Size>(__s + 16));
Howard Hinnantf1973402011-12-10 20:28:56 +00003378 std::swap(__z, __x);
3379 __s += 64;
3380 __len -= 64;
3381 } while (__len != 0);
3382 return __hash_len_16(
3383 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3384 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnantad71c232011-12-05 00:08:45 +00003385}
3386
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003387template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3388struct __scalar_hash;
3389
3390template <class _Tp>
3391struct __scalar_hash<_Tp, 0>
3392 : public unary_function<_Tp, size_t>
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003393{
Howard Hinnant756c69b2010-09-22 16:48:34 +00003394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003395 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003396 {
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003397 union
3398 {
3399 _Tp __t;
3400 size_t __a;
3401 } __u;
3402 __u.__a = 0;
3403 __u.__t = __v;
3404 return __u.__a;
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003405 }
3406};
3407
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003408template <class _Tp>
3409struct __scalar_hash<_Tp, 1>
3410 : public unary_function<_Tp, size_t>
3411{
3412 _LIBCPP_INLINE_VISIBILITY
3413 size_t operator()(_Tp __v) const _NOEXCEPT
3414 {
3415 union
3416 {
3417 _Tp __t;
3418 size_t __a;
3419 } __u;
3420 __u.__t = __v;
3421 return __u.__a;
3422 }
3423};
3424
3425template <class _Tp>
3426struct __scalar_hash<_Tp, 2>
3427 : public unary_function<_Tp, size_t>
3428{
3429 _LIBCPP_INLINE_VISIBILITY
3430 size_t operator()(_Tp __v) const _NOEXCEPT
3431 {
3432 union
3433 {
3434 _Tp __t;
3435 struct
3436 {
3437 size_t __a;
3438 size_t __b;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003439 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003440 } __u;
3441 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003442 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003443 }
3444};
3445
3446template <class _Tp>
3447struct __scalar_hash<_Tp, 3>
3448 : public unary_function<_Tp, size_t>
3449{
3450 _LIBCPP_INLINE_VISIBILITY
3451 size_t operator()(_Tp __v) const _NOEXCEPT
3452 {
3453 union
3454 {
3455 _Tp __t;
3456 struct
3457 {
3458 size_t __a;
3459 size_t __b;
3460 size_t __c;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003461 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003462 } __u;
3463 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003464 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003465 }
3466};
3467
3468template <class _Tp>
3469struct __scalar_hash<_Tp, 4>
3470 : public unary_function<_Tp, size_t>
3471{
3472 _LIBCPP_INLINE_VISIBILITY
3473 size_t operator()(_Tp __v) const _NOEXCEPT
3474 {
3475 union
3476 {
3477 _Tp __t;
3478 struct
3479 {
3480 size_t __a;
3481 size_t __b;
3482 size_t __c;
3483 size_t __d;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003484 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003485 } __u;
3486 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003487 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003488 }
3489};
3490
3491template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003492struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant9fa30202012-07-30 01:40:57 +00003493 : public unary_function<_Tp*, size_t>
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003494{
Howard Hinnant9fa30202012-07-30 01:40:57 +00003495 _LIBCPP_INLINE_VISIBILITY
3496 size_t operator()(_Tp* __v) const _NOEXCEPT
3497 {
3498 union
3499 {
3500 _Tp* __t;
3501 size_t __a;
3502 } __u;
3503 __u.__t = __v;
3504 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3505 }
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003506};
3507
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003508template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003509struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003510{
3511 typedef unique_ptr<_Tp, _Dp> argument_type;
3512 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003514 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003515 {
3516 typedef typename argument_type::pointer pointer;
3517 return hash<pointer>()(__ptr.get());
3518 }
3519};
3520
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521struct __destruct_n
3522{
3523private:
3524 size_t size;
3525
3526 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003527 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3529
3530 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003531 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532 {}
3533
Howard Hinnant719bda32011-05-28 14:41:13 +00003534 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003536 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537 {}
3538
Howard Hinnant719bda32011-05-28 14:41:13 +00003539 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003541 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542 {}
3543public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003544 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3545 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546
3547 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003548 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003549 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550
3551 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003552 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003553 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554
3555 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003556 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003557 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558};
3559
3560template <class _Alloc>
3561class __allocator_destructor
3562{
3563 typedef allocator_traits<_Alloc> __alloc_traits;
3564public:
3565 typedef typename __alloc_traits::pointer pointer;
3566 typedef typename __alloc_traits::size_type size_type;
3567private:
3568 _Alloc& __alloc_;
3569 size_type __s_;
3570public:
3571 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003572 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003575 void operator()(pointer __p) _NOEXCEPT
3576 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577};
3578
3579template <class _InputIterator, class _ForwardIterator>
3580_ForwardIterator
3581uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3582{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003584#ifndef _LIBCPP_NO_EXCEPTIONS
3585 _ForwardIterator __s = __r;
3586 try
3587 {
3588#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003589 for (; __f != __l; ++__f, (void) ++__r)
3590 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003591#ifndef _LIBCPP_NO_EXCEPTIONS
3592 }
3593 catch (...)
3594 {
3595 for (; __s != __r; ++__s)
3596 __s->~value_type();
3597 throw;
3598 }
3599#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600 return __r;
3601}
3602
3603template <class _InputIterator, class _Size, class _ForwardIterator>
3604_ForwardIterator
3605uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3606{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003608#ifndef _LIBCPP_NO_EXCEPTIONS
3609 _ForwardIterator __s = __r;
3610 try
3611 {
3612#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003613 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3614 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003615#ifndef _LIBCPP_NO_EXCEPTIONS
3616 }
3617 catch (...)
3618 {
3619 for (; __s != __r; ++__s)
3620 __s->~value_type();
3621 throw;
3622 }
3623#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624 return __r;
3625}
3626
3627template <class _ForwardIterator, class _Tp>
3628void
3629uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3630{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003632#ifndef _LIBCPP_NO_EXCEPTIONS
3633 _ForwardIterator __s = __f;
3634 try
3635 {
3636#endif
3637 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003638 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003639#ifndef _LIBCPP_NO_EXCEPTIONS
3640 }
3641 catch (...)
3642 {
3643 for (; __s != __f; ++__s)
3644 __s->~value_type();
3645 throw;
3646 }
3647#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648}
3649
3650template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003651_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3653{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003655#ifndef _LIBCPP_NO_EXCEPTIONS
3656 _ForwardIterator __s = __f;
3657 try
3658 {
3659#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003660 for (; __n > 0; ++__f, (void) --__n)
3661 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003662#ifndef _LIBCPP_NO_EXCEPTIONS
3663 }
3664 catch (...)
3665 {
3666 for (; __s != __f; ++__s)
3667 __s->~value_type();
3668 throw;
3669 }
3670#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003671 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672}
3673
Howard Hinnant756c69b2010-09-22 16:48:34 +00003674class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675 : public std::exception
3676{
3677public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003678 virtual ~bad_weak_ptr() _NOEXCEPT;
3679 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680};
3681
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003682template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003684class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685{
3686 __shared_count(const __shared_count&);
3687 __shared_count& operator=(const __shared_count&);
3688
3689protected:
3690 long __shared_owners_;
3691 virtual ~__shared_count();
3692private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003693 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694
3695public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003697 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698 : __shared_owners_(__refs) {}
3699
Howard Hinnant719bda32011-05-28 14:41:13 +00003700 void __add_shared() _NOEXCEPT;
3701 bool __release_shared() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003702 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003703 long use_count() const _NOEXCEPT {
3704 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3705 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706};
3707
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003708class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003709 : private __shared_count
3710{
3711 long __shared_weak_owners_;
3712
3713public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003715 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716 : __shared_count(__refs),
3717 __shared_weak_owners_(__refs) {}
3718protected:
3719 virtual ~__shared_weak_count();
3720
3721public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003722 void __add_shared() _NOEXCEPT;
3723 void __add_weak() _NOEXCEPT;
3724 void __release_shared() _NOEXCEPT;
3725 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003727 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3728 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729
Howard Hinnant807d6332013-02-25 15:50:36 +00003730 // Define the function out only if we build static libc++ without RTTI.
3731 // Otherwise we may break clients who need to compile their projects with
3732 // -fno-rtti and yet link against a libc++.dylib compiled
3733 // without -fno-rtti.
3734#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003735 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003736#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003738 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739};
3740
3741template <class _Tp, class _Dp, class _Alloc>
3742class __shared_ptr_pointer
3743 : public __shared_weak_count
3744{
3745 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3746public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003749 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750
Howard Hinnant72f73582010-08-11 17:04:31 +00003751#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003752 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003753#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754
3755private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003756 virtual void __on_zero_shared() _NOEXCEPT;
3757 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758};
3759
Howard Hinnant72f73582010-08-11 17:04:31 +00003760#ifndef _LIBCPP_NO_RTTI
3761
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762template <class _Tp, class _Dp, class _Alloc>
3763const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003764__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003766 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767}
3768
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003769#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003770
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771template <class _Tp, class _Dp, class _Alloc>
3772void
Howard Hinnant719bda32011-05-28 14:41:13 +00003773__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774{
3775 __data_.first().second()(__data_.first().first());
3776 __data_.first().second().~_Dp();
3777}
3778
3779template <class _Tp, class _Dp, class _Alloc>
3780void
Howard Hinnant719bda32011-05-28 14:41:13 +00003781__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003782{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003783 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3784 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003785 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3786
Eric Fiselierf8898c82015-02-05 23:01:40 +00003787 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003789 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790}
3791
3792template <class _Tp, class _Alloc>
3793class __shared_ptr_emplace
3794 : public __shared_weak_count
3795{
3796 __compressed_pair<_Alloc, _Tp> __data_;
3797public:
3798#ifndef _LIBCPP_HAS_NO_VARIADICS
3799
Howard Hinnant756c69b2010-09-22 16:48:34 +00003800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003802 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803
3804 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003807 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3808 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809
3810#else // _LIBCPP_HAS_NO_VARIADICS
3811
Howard Hinnant756c69b2010-09-22 16:48:34 +00003812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813 __shared_ptr_emplace(_Alloc __a)
3814 : __data_(__a) {}
3815
3816 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3819 : __data_(__a, _Tp(__a0)) {}
3820
3821 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3824 : __data_(__a, _Tp(__a0, __a1)) {}
3825
3826 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3829 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3830
3831#endif // _LIBCPP_HAS_NO_VARIADICS
3832
3833private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003834 virtual void __on_zero_shared() _NOEXCEPT;
3835 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003838 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839};
3840
3841template <class _Tp, class _Alloc>
3842void
Howard Hinnant719bda32011-05-28 14:41:13 +00003843__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844{
3845 __data_.second().~_Tp();
3846}
3847
3848template <class _Tp, class _Alloc>
3849void
Howard Hinnant719bda32011-05-28 14:41:13 +00003850__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003852 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3853 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003854 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003855 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003857 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858}
3859
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003860template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861
3862template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003863class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003865public:
3866 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003867#if _LIBCPP_STD_VER > 14
3868 typedef weak_ptr<_Tp> weak_type;
3869#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870private:
3871 element_type* __ptr_;
3872 __shared_weak_count* __cntrl_;
3873
3874 struct __nat {int __for_bool_;};
3875public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003877 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003879 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003880 template<class _Yp>
3881 explicit shared_ptr(_Yp* __p,
3882 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3883 template<class _Yp, class _Dp>
3884 shared_ptr(_Yp* __p, _Dp __d,
3885 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3886 template<class _Yp, class _Dp, class _Alloc>
3887 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3888 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3890 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003891 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003893 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003896 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003897 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3898 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003899#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003901 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003902 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003903 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3904 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003905#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003906 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003907 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003908#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003909 template<class _Yp>
3910 shared_ptr(auto_ptr<_Yp>&& __r,
3911 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003912#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003913 template<class _Yp>
3914 shared_ptr(auto_ptr<_Yp> __r,
3915 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003917#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003918 template <class _Yp, class _Dp>
3919 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3920 typename enable_if
3921 <
3922 !is_lvalue_reference<_Dp>::value &&
3923 !is_array<_Yp>::value &&
3924 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3925 __nat
3926 >::type = __nat());
3927 template <class _Yp, class _Dp>
3928 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3929 typename enable_if
3930 <
3931 is_lvalue_reference<_Dp>::value &&
3932 !is_array<_Yp>::value &&
3933 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3934 __nat
3935 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003936#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003937 template <class _Yp, class _Dp>
3938 shared_ptr(unique_ptr<_Yp, _Dp>,
3939 typename enable_if
3940 <
3941 !is_lvalue_reference<_Dp>::value &&
3942 !is_array<_Yp>::value &&
3943 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3944 __nat
3945 >::type = __nat());
3946 template <class _Yp, class _Dp>
3947 shared_ptr(unique_ptr<_Yp, _Dp>,
3948 typename enable_if
3949 <
3950 is_lvalue_reference<_Dp>::value &&
3951 !is_array<_Yp>::value &&
3952 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3953 __nat
3954 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003955#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956
3957 ~shared_ptr();
3958
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003960 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003961 template<class _Yp>
3962 typename enable_if
3963 <
3964 is_convertible<_Yp*, element_type*>::value,
3965 shared_ptr&
3966 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003968 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003969#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003971 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003972 template<class _Yp>
3973 typename enable_if
3974 <
3975 is_convertible<_Yp*, element_type*>::value,
3976 shared_ptr<_Tp>&
3977 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003979 operator=(shared_ptr<_Yp>&& __r);
3980 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003982 typename enable_if
3983 <
3984 !is_array<_Yp>::value &&
3985 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003986 shared_ptr
3987 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003988 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003989#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003990 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003992 typename enable_if
3993 <
3994 !is_array<_Yp>::value &&
3995 is_convertible<_Yp*, element_type*>::value,
3996 shared_ptr&
3997 >::type
3998 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004000 template <class _Yp, class _Dp>
4001 typename enable_if
4002 <
4003 !is_array<_Yp>::value &&
4004 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4005 shared_ptr&
4006 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00004007#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004009 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004010#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00004011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004012 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013#endif
4014
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004016 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004018 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004019 template<class _Yp>
4020 typename enable_if
4021 <
4022 is_convertible<_Yp*, element_type*>::value,
4023 void
4024 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004026 reset(_Yp* __p);
4027 template<class _Yp, class _Dp>
4028 typename enable_if
4029 <
4030 is_convertible<_Yp*, element_type*>::value,
4031 void
4032 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004034 reset(_Yp* __p, _Dp __d);
4035 template<class _Yp, class _Dp, class _Alloc>
4036 typename enable_if
4037 <
4038 is_convertible<_Yp*, element_type*>::value,
4039 void
4040 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004042 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043
Howard Hinnant756c69b2010-09-22 16:48:34 +00004044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004045 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004047 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4048 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004050 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004052 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004054 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00004056 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004057 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004059 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004061 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004063 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00004065 _LIBCPP_INLINE_VISIBILITY
4066 bool
4067 __owner_equivalent(const shared_ptr& __p) const
4068 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069
Howard Hinnant72f73582010-08-11 17:04:31 +00004070#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004073 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004075#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076
4077#ifndef _LIBCPP_HAS_NO_VARIADICS
4078
4079 template<class ..._Args>
4080 static
4081 shared_ptr<_Tp>
4082 make_shared(_Args&& ...__args);
4083
4084 template<class _Alloc, class ..._Args>
4085 static
4086 shared_ptr<_Tp>
4087 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4088
4089#else // _LIBCPP_HAS_NO_VARIADICS
4090
4091 static shared_ptr<_Tp> make_shared();
4092
4093 template<class _A0>
4094 static shared_ptr<_Tp> make_shared(_A0&);
4095
4096 template<class _A0, class _A1>
4097 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4098
4099 template<class _A0, class _A1, class _A2>
4100 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4101
4102 template<class _Alloc>
4103 static shared_ptr<_Tp>
4104 allocate_shared(const _Alloc& __a);
4105
4106 template<class _Alloc, class _A0>
4107 static shared_ptr<_Tp>
4108 allocate_shared(const _Alloc& __a, _A0& __a0);
4109
4110 template<class _Alloc, class _A0, class _A1>
4111 static shared_ptr<_Tp>
4112 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4113
4114 template<class _Alloc, class _A0, class _A1, class _A2>
4115 static shared_ptr<_Tp>
4116 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4117
4118#endif // _LIBCPP_HAS_NO_VARIADICS
4119
4120private:
4121
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004122 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004125 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4126 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004128 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004129 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004130 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004131 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4132 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004133 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134 }
4135
Howard Hinnant756c69b2010-09-22 16:48:34 +00004136 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004137 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004139 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4140 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141};
4142
4143template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004144inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004145_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004146shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147 : __ptr_(0),
4148 __cntrl_(0)
4149{
4150}
4151
4152template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004153inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004154_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004155shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156 : __ptr_(0),
4157 __cntrl_(0)
4158{
4159}
4160
4161template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004162template<class _Yp>
4163shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4164 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165 : __ptr_(__p)
4166{
4167 unique_ptr<_Yp> __hold(__p);
4168 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4169 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4170 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004171 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172}
4173
4174template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004175template<class _Yp, class _Dp>
4176shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4177 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178 : __ptr_(__p)
4179{
4180#ifndef _LIBCPP_NO_EXCEPTIONS
4181 try
4182 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004183#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4185 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004186 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187#ifndef _LIBCPP_NO_EXCEPTIONS
4188 }
4189 catch (...)
4190 {
4191 __d(__p);
4192 throw;
4193 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004194#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195}
4196
4197template<class _Tp>
4198template<class _Dp>
4199shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4200 : __ptr_(0)
4201{
4202#ifndef _LIBCPP_NO_EXCEPTIONS
4203 try
4204 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004205#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4207 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4208#ifndef _LIBCPP_NO_EXCEPTIONS
4209 }
4210 catch (...)
4211 {
4212 __d(__p);
4213 throw;
4214 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004215#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004216}
4217
4218template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004219template<class _Yp, class _Dp, class _Alloc>
4220shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4221 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222 : __ptr_(__p)
4223{
4224#ifndef _LIBCPP_NO_EXCEPTIONS
4225 try
4226 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004227#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004228 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004229 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230 typedef __allocator_destructor<_A2> _D2;
4231 _A2 __a2(__a);
4232 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004233 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4234 _CntrlBlk(__p, __d, __a);
4235 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004236 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237#ifndef _LIBCPP_NO_EXCEPTIONS
4238 }
4239 catch (...)
4240 {
4241 __d(__p);
4242 throw;
4243 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004244#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245}
4246
4247template<class _Tp>
4248template<class _Dp, class _Alloc>
4249shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4250 : __ptr_(0)
4251{
4252#ifndef _LIBCPP_NO_EXCEPTIONS
4253 try
4254 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004255#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004257 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258 typedef __allocator_destructor<_A2> _D2;
4259 _A2 __a2(__a);
4260 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004261 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4262 _CntrlBlk(__p, __d, __a);
4263 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264#ifndef _LIBCPP_NO_EXCEPTIONS
4265 }
4266 catch (...)
4267 {
4268 __d(__p);
4269 throw;
4270 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004271#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272}
4273
4274template<class _Tp>
4275template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004276inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004277shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 : __ptr_(__p),
4279 __cntrl_(__r.__cntrl_)
4280{
4281 if (__cntrl_)
4282 __cntrl_->__add_shared();
4283}
4284
4285template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004286inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004287shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288 : __ptr_(__r.__ptr_),
4289 __cntrl_(__r.__cntrl_)
4290{
4291 if (__cntrl_)
4292 __cntrl_->__add_shared();
4293}
4294
4295template<class _Tp>
4296template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004297inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004298shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4299 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004300 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301 : __ptr_(__r.__ptr_),
4302 __cntrl_(__r.__cntrl_)
4303{
4304 if (__cntrl_)
4305 __cntrl_->__add_shared();
4306}
4307
Howard Hinnant74279a52010-09-04 23:28:19 +00004308#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309
4310template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004311inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004312shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313 : __ptr_(__r.__ptr_),
4314 __cntrl_(__r.__cntrl_)
4315{
4316 __r.__ptr_ = 0;
4317 __r.__cntrl_ = 0;
4318}
4319
4320template<class _Tp>
4321template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004322inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4324 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004325 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326 : __ptr_(__r.__ptr_),
4327 __cntrl_(__r.__cntrl_)
4328{
4329 __r.__ptr_ = 0;
4330 __r.__cntrl_ = 0;
4331}
4332
Howard Hinnant74279a52010-09-04 23:28:19 +00004333#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334
4335template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004336template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004337#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004338shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004340shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004342 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343 : __ptr_(__r.get())
4344{
4345 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4346 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004347 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348 __r.release();
4349}
4350
4351template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004352template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004353#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4355#else
4356shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4357#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004358 typename enable_if
4359 <
4360 !is_lvalue_reference<_Dp>::value &&
4361 !is_array<_Yp>::value &&
4362 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4363 __nat
4364 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365 : __ptr_(__r.get())
4366{
Marshall Clow35cde742015-05-10 13:59:45 +00004367#if _LIBCPP_STD_VER > 11
4368 if (__ptr_ == nullptr)
4369 __cntrl_ = nullptr;
4370 else
4371#endif
4372 {
4373 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4374 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004375 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004376 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377 __r.release();
4378}
4379
4380template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004381template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004382#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004383shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4384#else
4385shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4386#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004387 typename enable_if
4388 <
4389 is_lvalue_reference<_Dp>::value &&
4390 !is_array<_Yp>::value &&
4391 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4392 __nat
4393 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394 : __ptr_(__r.get())
4395{
Marshall Clow35cde742015-05-10 13:59:45 +00004396#if _LIBCPP_STD_VER > 11
4397 if (__ptr_ == nullptr)
4398 __cntrl_ = nullptr;
4399 else
4400#endif
4401 {
4402 typedef __shared_ptr_pointer<_Yp*,
4403 reference_wrapper<typename remove_reference<_Dp>::type>,
4404 allocator<_Yp> > _CntrlBlk;
4405 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004406 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004407 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004408 __r.release();
4409}
4410
4411#ifndef _LIBCPP_HAS_NO_VARIADICS
4412
4413template<class _Tp>
4414template<class ..._Args>
4415shared_ptr<_Tp>
4416shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4417{
4418 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4419 typedef allocator<_CntrlBlk> _A2;
4420 typedef __allocator_destructor<_A2> _D2;
4421 _A2 __a2;
4422 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004423 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424 shared_ptr<_Tp> __r;
4425 __r.__ptr_ = __hold2.get()->get();
4426 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004427 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428 return __r;
4429}
4430
4431template<class _Tp>
4432template<class _Alloc, class ..._Args>
4433shared_ptr<_Tp>
4434shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4435{
4436 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004437 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438 typedef __allocator_destructor<_A2> _D2;
4439 _A2 __a2(__a);
4440 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004441 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4442 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443 shared_ptr<_Tp> __r;
4444 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004445 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004446 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004447 return __r;
4448}
4449
4450#else // _LIBCPP_HAS_NO_VARIADICS
4451
4452template<class _Tp>
4453shared_ptr<_Tp>
4454shared_ptr<_Tp>::make_shared()
4455{
4456 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4457 typedef allocator<_CntrlBlk> _Alloc2;
4458 typedef __allocator_destructor<_Alloc2> _D2;
4459 _Alloc2 __alloc2;
4460 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4461 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4462 shared_ptr<_Tp> __r;
4463 __r.__ptr_ = __hold2.get()->get();
4464 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004465 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466 return __r;
4467}
4468
4469template<class _Tp>
4470template<class _A0>
4471shared_ptr<_Tp>
4472shared_ptr<_Tp>::make_shared(_A0& __a0)
4473{
4474 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4475 typedef allocator<_CntrlBlk> _Alloc2;
4476 typedef __allocator_destructor<_Alloc2> _D2;
4477 _Alloc2 __alloc2;
4478 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4479 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4480 shared_ptr<_Tp> __r;
4481 __r.__ptr_ = __hold2.get()->get();
4482 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004483 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484 return __r;
4485}
4486
4487template<class _Tp>
4488template<class _A0, class _A1>
4489shared_ptr<_Tp>
4490shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4491{
4492 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4493 typedef allocator<_CntrlBlk> _Alloc2;
4494 typedef __allocator_destructor<_Alloc2> _D2;
4495 _Alloc2 __alloc2;
4496 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4497 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4498 shared_ptr<_Tp> __r;
4499 __r.__ptr_ = __hold2.get()->get();
4500 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004501 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004502 return __r;
4503}
4504
4505template<class _Tp>
4506template<class _A0, class _A1, class _A2>
4507shared_ptr<_Tp>
4508shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4509{
4510 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4511 typedef allocator<_CntrlBlk> _Alloc2;
4512 typedef __allocator_destructor<_Alloc2> _D2;
4513 _Alloc2 __alloc2;
4514 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4515 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4516 shared_ptr<_Tp> __r;
4517 __r.__ptr_ = __hold2.get()->get();
4518 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004519 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004520 return __r;
4521}
4522
4523template<class _Tp>
4524template<class _Alloc>
4525shared_ptr<_Tp>
4526shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4527{
4528 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004529 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530 typedef __allocator_destructor<_Alloc2> _D2;
4531 _Alloc2 __alloc2(__a);
4532 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004533 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4534 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004535 shared_ptr<_Tp> __r;
4536 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004537 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004538 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539 return __r;
4540}
4541
4542template<class _Tp>
4543template<class _Alloc, class _A0>
4544shared_ptr<_Tp>
4545shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4546{
4547 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004548 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004549 typedef __allocator_destructor<_Alloc2> _D2;
4550 _Alloc2 __alloc2(__a);
4551 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004552 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4553 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554 shared_ptr<_Tp> __r;
4555 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004556 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004557 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004558 return __r;
4559}
4560
4561template<class _Tp>
4562template<class _Alloc, class _A0, class _A1>
4563shared_ptr<_Tp>
4564shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4565{
4566 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004567 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568 typedef __allocator_destructor<_Alloc2> _D2;
4569 _Alloc2 __alloc2(__a);
4570 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004571 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4572 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573 shared_ptr<_Tp> __r;
4574 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004575 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004576 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577 return __r;
4578}
4579
4580template<class _Tp>
4581template<class _Alloc, class _A0, class _A1, class _A2>
4582shared_ptr<_Tp>
4583shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4584{
4585 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004586 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004587 typedef __allocator_destructor<_Alloc2> _D2;
4588 _Alloc2 __alloc2(__a);
4589 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004590 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4591 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592 shared_ptr<_Tp> __r;
4593 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004594 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004595 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004596 return __r;
4597}
4598
4599#endif // _LIBCPP_HAS_NO_VARIADICS
4600
4601template<class _Tp>
4602shared_ptr<_Tp>::~shared_ptr()
4603{
4604 if (__cntrl_)
4605 __cntrl_->__release_shared();
4606}
4607
4608template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004609inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004610shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004611shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004612{
4613 shared_ptr(__r).swap(*this);
4614 return *this;
4615}
4616
4617template<class _Tp>
4618template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004619inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004620typename enable_if
4621<
4622 is_convertible<_Yp*, _Tp*>::value,
4623 shared_ptr<_Tp>&
4624>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004625shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004626{
4627 shared_ptr(__r).swap(*this);
4628 return *this;
4629}
4630
Howard Hinnant74279a52010-09-04 23:28:19 +00004631#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632
4633template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004634inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004636shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004637{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004638 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004639 return *this;
4640}
4641
4642template<class _Tp>
4643template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004644inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004645typename enable_if
4646<
4647 is_convertible<_Yp*, _Tp*>::value,
4648 shared_ptr<_Tp>&
4649>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004650shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4651{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004652 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004653 return *this;
4654}
4655
4656template<class _Tp>
4657template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004658inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004659typename enable_if
4660<
4661 !is_array<_Yp>::value &&
4662 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004663 shared_ptr<_Tp>
4664>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004665shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4666{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004667 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004668 return *this;
4669}
4670
4671template<class _Tp>
4672template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004673inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004674typename enable_if
4675<
4676 !is_array<_Yp>::value &&
4677 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4678 shared_ptr<_Tp>&
4679>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004680shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4681{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004682 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004683 return *this;
4684}
4685
Howard Hinnant74279a52010-09-04 23:28:19 +00004686#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004687
4688template<class _Tp>
4689template<class _Yp>
4690inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004691typename enable_if
4692<
4693 !is_array<_Yp>::value &&
4694 is_convertible<_Yp*, _Tp*>::value,
4695 shared_ptr<_Tp>&
4696>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004697shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004698{
4699 shared_ptr(__r).swap(*this);
4700 return *this;
4701}
4702
4703template<class _Tp>
4704template <class _Yp, class _Dp>
4705inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004706typename enable_if
4707<
4708 !is_array<_Yp>::value &&
4709 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4710 shared_ptr<_Tp>&
4711>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004712shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4713{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004714 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004715 return *this;
4716}
4717
Howard Hinnant74279a52010-09-04 23:28:19 +00004718#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004719
4720template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004721inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004722void
Howard Hinnant719bda32011-05-28 14:41:13 +00004723shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004724{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004725 _VSTD::swap(__ptr_, __r.__ptr_);
4726 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004727}
4728
4729template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004730inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004731void
Howard Hinnant719bda32011-05-28 14:41:13 +00004732shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004733{
4734 shared_ptr().swap(*this);
4735}
4736
4737template<class _Tp>
4738template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004739inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004740typename enable_if
4741<
4742 is_convertible<_Yp*, _Tp*>::value,
4743 void
4744>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004745shared_ptr<_Tp>::reset(_Yp* __p)
4746{
4747 shared_ptr(__p).swap(*this);
4748}
4749
4750template<class _Tp>
4751template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004752inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004753typename enable_if
4754<
4755 is_convertible<_Yp*, _Tp*>::value,
4756 void
4757>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004758shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4759{
4760 shared_ptr(__p, __d).swap(*this);
4761}
4762
4763template<class _Tp>
4764template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004765inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004766typename enable_if
4767<
4768 is_convertible<_Yp*, _Tp*>::value,
4769 void
4770>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004771shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4772{
4773 shared_ptr(__p, __d, __a).swap(*this);
4774}
4775
4776#ifndef _LIBCPP_HAS_NO_VARIADICS
4777
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004778template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004779inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004780typename enable_if
4781<
4782 !is_array<_Tp>::value,
4783 shared_ptr<_Tp>
4784>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004785make_shared(_Args&& ...__args)
4786{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004787 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004788}
4789
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004790template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004791inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004792typename enable_if
4793<
4794 !is_array<_Tp>::value,
4795 shared_ptr<_Tp>
4796>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004797allocate_shared(const _Alloc& __a, _Args&& ...__args)
4798{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004799 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004800}
4801
4802#else // _LIBCPP_HAS_NO_VARIADICS
4803
4804template<class _Tp>
4805inline _LIBCPP_INLINE_VISIBILITY
4806shared_ptr<_Tp>
4807make_shared()
4808{
4809 return shared_ptr<_Tp>::make_shared();
4810}
4811
4812template<class _Tp, class _A0>
4813inline _LIBCPP_INLINE_VISIBILITY
4814shared_ptr<_Tp>
4815make_shared(_A0& __a0)
4816{
4817 return shared_ptr<_Tp>::make_shared(__a0);
4818}
4819
4820template<class _Tp, class _A0, class _A1>
4821inline _LIBCPP_INLINE_VISIBILITY
4822shared_ptr<_Tp>
4823make_shared(_A0& __a0, _A1& __a1)
4824{
4825 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4826}
4827
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004828template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004829inline _LIBCPP_INLINE_VISIBILITY
4830shared_ptr<_Tp>
4831make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4832{
4833 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4834}
4835
4836template<class _Tp, class _Alloc>
4837inline _LIBCPP_INLINE_VISIBILITY
4838shared_ptr<_Tp>
4839allocate_shared(const _Alloc& __a)
4840{
4841 return shared_ptr<_Tp>::allocate_shared(__a);
4842}
4843
4844template<class _Tp, class _Alloc, class _A0>
4845inline _LIBCPP_INLINE_VISIBILITY
4846shared_ptr<_Tp>
4847allocate_shared(const _Alloc& __a, _A0& __a0)
4848{
4849 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4850}
4851
4852template<class _Tp, class _Alloc, class _A0, class _A1>
4853inline _LIBCPP_INLINE_VISIBILITY
4854shared_ptr<_Tp>
4855allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4856{
4857 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4858}
4859
4860template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4861inline _LIBCPP_INLINE_VISIBILITY
4862shared_ptr<_Tp>
4863allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4864{
4865 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4866}
4867
4868#endif // _LIBCPP_HAS_NO_VARIADICS
4869
4870template<class _Tp, class _Up>
4871inline _LIBCPP_INLINE_VISIBILITY
4872bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004873operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004874{
4875 return __x.get() == __y.get();
4876}
4877
4878template<class _Tp, class _Up>
4879inline _LIBCPP_INLINE_VISIBILITY
4880bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004881operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004882{
4883 return !(__x == __y);
4884}
4885
4886template<class _Tp, class _Up>
4887inline _LIBCPP_INLINE_VISIBILITY
4888bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004889operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004890{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004891 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4892 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004893}
4894
4895template<class _Tp, class _Up>
4896inline _LIBCPP_INLINE_VISIBILITY
4897bool
4898operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4899{
4900 return __y < __x;
4901}
4902
4903template<class _Tp, class _Up>
4904inline _LIBCPP_INLINE_VISIBILITY
4905bool
4906operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4907{
4908 return !(__y < __x);
4909}
4910
4911template<class _Tp, class _Up>
4912inline _LIBCPP_INLINE_VISIBILITY
4913bool
4914operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4915{
4916 return !(__x < __y);
4917}
4918
4919template<class _Tp>
4920inline _LIBCPP_INLINE_VISIBILITY
4921bool
4922operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4923{
4924 return !__x;
4925}
4926
4927template<class _Tp>
4928inline _LIBCPP_INLINE_VISIBILITY
4929bool
4930operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4931{
4932 return !__x;
4933}
4934
4935template<class _Tp>
4936inline _LIBCPP_INLINE_VISIBILITY
4937bool
4938operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4939{
4940 return static_cast<bool>(__x);
4941}
4942
4943template<class _Tp>
4944inline _LIBCPP_INLINE_VISIBILITY
4945bool
4946operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4947{
4948 return static_cast<bool>(__x);
4949}
4950
4951template<class _Tp>
4952inline _LIBCPP_INLINE_VISIBILITY
4953bool
4954operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4955{
4956 return less<_Tp*>()(__x.get(), nullptr);
4957}
4958
4959template<class _Tp>
4960inline _LIBCPP_INLINE_VISIBILITY
4961bool
4962operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4963{
4964 return less<_Tp*>()(nullptr, __x.get());
4965}
4966
4967template<class _Tp>
4968inline _LIBCPP_INLINE_VISIBILITY
4969bool
4970operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4971{
4972 return nullptr < __x;
4973}
4974
4975template<class _Tp>
4976inline _LIBCPP_INLINE_VISIBILITY
4977bool
4978operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4979{
4980 return __x < nullptr;
4981}
4982
4983template<class _Tp>
4984inline _LIBCPP_INLINE_VISIBILITY
4985bool
4986operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4987{
4988 return !(nullptr < __x);
4989}
4990
4991template<class _Tp>
4992inline _LIBCPP_INLINE_VISIBILITY
4993bool
4994operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4995{
4996 return !(__x < nullptr);
4997}
4998
4999template<class _Tp>
5000inline _LIBCPP_INLINE_VISIBILITY
5001bool
5002operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5003{
5004 return !(__x < nullptr);
5005}
5006
5007template<class _Tp>
5008inline _LIBCPP_INLINE_VISIBILITY
5009bool
5010operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5011{
5012 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005013}
5014
5015template<class _Tp>
5016inline _LIBCPP_INLINE_VISIBILITY
5017void
Howard Hinnant719bda32011-05-28 14:41:13 +00005018swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005019{
5020 __x.swap(__y);
5021}
5022
5023template<class _Tp, class _Up>
5024inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005025typename enable_if
5026<
5027 !is_array<_Tp>::value && !is_array<_Up>::value,
5028 shared_ptr<_Tp>
5029>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005030static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005031{
5032 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5033}
5034
5035template<class _Tp, class _Up>
5036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005037typename enable_if
5038<
5039 !is_array<_Tp>::value && !is_array<_Up>::value,
5040 shared_ptr<_Tp>
5041>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005042dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005043{
5044 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5045 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5046}
5047
5048template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005049typename enable_if
5050<
5051 is_array<_Tp>::value == is_array<_Up>::value,
5052 shared_ptr<_Tp>
5053>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005054const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005055{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005056 typedef typename remove_extent<_Tp>::type _RTp;
5057 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00005058}
5059
Howard Hinnant72f73582010-08-11 17:04:31 +00005060#ifndef _LIBCPP_NO_RTTI
5061
Howard Hinnantc51e1022010-05-11 19:42:16 +00005062template<class _Dp, class _Tp>
5063inline _LIBCPP_INLINE_VISIBILITY
5064_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00005065get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005066{
5067 return __p.template __get_deleter<_Dp>();
5068}
5069
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005070#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00005071
Howard Hinnantc51e1022010-05-11 19:42:16 +00005072template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005073class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00005074{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005075public:
5076 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005077private:
5078 element_type* __ptr_;
5079 __shared_weak_count* __cntrl_;
5080
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005081public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005083 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005084 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005085 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5086 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005088 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005089 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005090 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5091 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005092
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005093#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005095 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005096 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005097 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5098 _NOEXCEPT;
5099#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005100 ~weak_ptr();
5101
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005103 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005104 template<class _Yp>
5105 typename enable_if
5106 <
5107 is_convertible<_Yp*, element_type*>::value,
5108 weak_ptr&
5109 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005111 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5112
5113#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5114
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005116 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5117 template<class _Yp>
5118 typename enable_if
5119 <
5120 is_convertible<_Yp*, element_type*>::value,
5121 weak_ptr&
5122 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005124 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5125
5126#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5127
5128 template<class _Yp>
5129 typename enable_if
5130 <
5131 is_convertible<_Yp*, element_type*>::value,
5132 weak_ptr&
5133 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005135 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005136
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005138 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005140 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005141
Howard Hinnant756c69b2010-09-22 16:48:34 +00005142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005143 long use_count() const _NOEXCEPT
5144 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005146 bool expired() const _NOEXCEPT
5147 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5148 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005149 template<class _Up>
5150 _LIBCPP_INLINE_VISIBILITY
5151 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005152 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005153 template<class _Up>
5154 _LIBCPP_INLINE_VISIBILITY
5155 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005156 {return __cntrl_ < __r.__cntrl_;}
5157
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005158 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5159 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005160};
5161
5162template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005163inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005164_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005165weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005166 : __ptr_(0),
5167 __cntrl_(0)
5168{
5169}
5170
5171template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005172inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005173weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005174 : __ptr_(__r.__ptr_),
5175 __cntrl_(__r.__cntrl_)
5176{
5177 if (__cntrl_)
5178 __cntrl_->__add_weak();
5179}
5180
5181template<class _Tp>
5182template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005183inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005184weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005185 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005186 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005187 : __ptr_(__r.__ptr_),
5188 __cntrl_(__r.__cntrl_)
5189{
5190 if (__cntrl_)
5191 __cntrl_->__add_weak();
5192}
5193
5194template<class _Tp>
5195template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005196inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005197weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005198 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005199 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005200 : __ptr_(__r.__ptr_),
5201 __cntrl_(__r.__cntrl_)
5202{
5203 if (__cntrl_)
5204 __cntrl_->__add_weak();
5205}
5206
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005207#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5208
5209template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005210inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005211weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5212 : __ptr_(__r.__ptr_),
5213 __cntrl_(__r.__cntrl_)
5214{
5215 __r.__ptr_ = 0;
5216 __r.__cntrl_ = 0;
5217}
5218
5219template<class _Tp>
5220template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005221inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005222weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5223 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5224 _NOEXCEPT
5225 : __ptr_(__r.__ptr_),
5226 __cntrl_(__r.__cntrl_)
5227{
5228 __r.__ptr_ = 0;
5229 __r.__cntrl_ = 0;
5230}
5231
5232#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5233
Howard Hinnantc51e1022010-05-11 19:42:16 +00005234template<class _Tp>
5235weak_ptr<_Tp>::~weak_ptr()
5236{
5237 if (__cntrl_)
5238 __cntrl_->__release_weak();
5239}
5240
5241template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005242inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005243weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005244weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005245{
5246 weak_ptr(__r).swap(*this);
5247 return *this;
5248}
5249
5250template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005251template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005252inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005253typename enable_if
5254<
5255 is_convertible<_Yp*, _Tp*>::value,
5256 weak_ptr<_Tp>&
5257>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005258weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005259{
5260 weak_ptr(__r).swap(*this);
5261 return *this;
5262}
5263
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005264#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5265
5266template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005267inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005268weak_ptr<_Tp>&
5269weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5270{
5271 weak_ptr(_VSTD::move(__r)).swap(*this);
5272 return *this;
5273}
5274
Howard Hinnantc51e1022010-05-11 19:42:16 +00005275template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005276template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005277inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005278typename enable_if
5279<
5280 is_convertible<_Yp*, _Tp*>::value,
5281 weak_ptr<_Tp>&
5282>::type
5283weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5284{
5285 weak_ptr(_VSTD::move(__r)).swap(*this);
5286 return *this;
5287}
5288
5289#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5290
5291template<class _Tp>
5292template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005293inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005294typename enable_if
5295<
5296 is_convertible<_Yp*, _Tp*>::value,
5297 weak_ptr<_Tp>&
5298>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005299weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005300{
5301 weak_ptr(__r).swap(*this);
5302 return *this;
5303}
5304
5305template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005306inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005307void
Howard Hinnant719bda32011-05-28 14:41:13 +00005308weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005309{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005310 _VSTD::swap(__ptr_, __r.__ptr_);
5311 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005312}
5313
5314template<class _Tp>
5315inline _LIBCPP_INLINE_VISIBILITY
5316void
Howard Hinnant719bda32011-05-28 14:41:13 +00005317swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005318{
5319 __x.swap(__y);
5320}
5321
5322template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005323inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005324void
Howard Hinnant719bda32011-05-28 14:41:13 +00005325weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005326{
5327 weak_ptr().swap(*this);
5328}
5329
5330template<class _Tp>
5331template<class _Yp>
5332shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5333 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5334 : __ptr_(__r.__ptr_),
5335 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5336{
5337 if (__cntrl_ == 0)
5338#ifndef _LIBCPP_NO_EXCEPTIONS
5339 throw bad_weak_ptr();
5340#else
5341 assert(!"bad_weak_ptr");
5342#endif
5343}
5344
5345template<class _Tp>
5346shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005347weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005348{
5349 shared_ptr<_Tp> __r;
5350 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5351 if (__r.__cntrl_)
5352 __r.__ptr_ = __ptr_;
5353 return __r;
5354}
5355
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005356#if _LIBCPP_STD_VER > 14
5357template <class _Tp = void> struct owner_less;
5358#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005359template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005360#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005361
5362template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005363struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005364 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005365{
5366 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005368 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5369 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005371 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5372 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005374 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5375 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005376};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005377
5378template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005379struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005380 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5381{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005382 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005384 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5385 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005387 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5388 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005390 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5391 {return __x.owner_before(__y);}
5392};
5393
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005394#if _LIBCPP_STD_VER > 14
5395template <>
5396struct _LIBCPP_TYPE_VIS_ONLY owner_less<void>
5397{
5398 template <class _Tp, class _Up>
5399 _LIBCPP_INLINE_VISIBILITY
5400 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5401 {return __x.owner_before(__y);}
5402 template <class _Tp, class _Up>
5403 _LIBCPP_INLINE_VISIBILITY
5404 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5405 {return __x.owner_before(__y);}
5406 template <class _Tp, class _Up>
5407 _LIBCPP_INLINE_VISIBILITY
5408 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5409 {return __x.owner_before(__y);}
5410 template <class _Tp, class _Up>
5411 _LIBCPP_INLINE_VISIBILITY
5412 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5413 {return __x.owner_before(__y);}
5414 typedef void is_transparent;
5415};
5416#endif
5417
Howard Hinnantc51e1022010-05-11 19:42:16 +00005418template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005419class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005420{
5421 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005422protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005423 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005424 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005426 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005428 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5429 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005431 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005432public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005434 shared_ptr<_Tp> shared_from_this()
5435 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005437 shared_ptr<_Tp const> shared_from_this() const
5438 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005439
Eric Fiselier84006862016-06-02 00:15:35 +00005440#if _LIBCPP_STD_VER > 14
5441 _LIBCPP_INLINE_VISIBILITY
5442 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5443 { return __weak_this_; }
5444
5445 _LIBCPP_INLINE_VISIBILITY
5446 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5447 { return __weak_this_; }
5448#endif // _LIBCPP_STD_VER > 14
5449
Howard Hinnantc51e1022010-05-11 19:42:16 +00005450 template <class _Up> friend class shared_ptr;
5451};
5452
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005453template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005454struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005455{
5456 typedef shared_ptr<_Tp> argument_type;
5457 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005459 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005460 {
5461 return hash<_Tp*>()(__ptr.get());
5462 }
5463};
5464
Howard Hinnantc834c512011-11-29 18:15:50 +00005465template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005466inline _LIBCPP_INLINE_VISIBILITY
5467basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005468operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005469
Eric Fiselier9b492672016-06-18 02:12:53 +00005470
5471#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005472
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005473class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005474{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005475 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005476public:
5477 void lock() _NOEXCEPT;
5478 void unlock() _NOEXCEPT;
5479
5480private:
5481 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5482 __sp_mut(const __sp_mut&);
5483 __sp_mut& operator=(const __sp_mut&);
5484
Howard Hinnant8331b762013-03-06 23:30:19 +00005485 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005486};
5487
Howard Hinnant8331b762013-03-06 23:30:19 +00005488_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005489
5490template <class _Tp>
5491inline _LIBCPP_INLINE_VISIBILITY
5492bool
5493atomic_is_lock_free(const shared_ptr<_Tp>*)
5494{
5495 return false;
5496}
5497
5498template <class _Tp>
5499shared_ptr<_Tp>
5500atomic_load(const shared_ptr<_Tp>* __p)
5501{
5502 __sp_mut& __m = __get_sp_mut(__p);
5503 __m.lock();
5504 shared_ptr<_Tp> __q = *__p;
5505 __m.unlock();
5506 return __q;
5507}
5508
5509template <class _Tp>
5510inline _LIBCPP_INLINE_VISIBILITY
5511shared_ptr<_Tp>
5512atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5513{
5514 return atomic_load(__p);
5515}
5516
5517template <class _Tp>
5518void
5519atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5520{
5521 __sp_mut& __m = __get_sp_mut(__p);
5522 __m.lock();
5523 __p->swap(__r);
5524 __m.unlock();
5525}
5526
5527template <class _Tp>
5528inline _LIBCPP_INLINE_VISIBILITY
5529void
5530atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5531{
5532 atomic_store(__p, __r);
5533}
5534
5535template <class _Tp>
5536shared_ptr<_Tp>
5537atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5538{
5539 __sp_mut& __m = __get_sp_mut(__p);
5540 __m.lock();
5541 __p->swap(__r);
5542 __m.unlock();
5543 return __r;
5544}
5545
5546template <class _Tp>
5547inline _LIBCPP_INLINE_VISIBILITY
5548shared_ptr<_Tp>
5549atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5550{
5551 return atomic_exchange(__p, __r);
5552}
5553
5554template <class _Tp>
5555bool
5556atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5557{
Marshall Clow4201ee82016-05-18 17:50:13 +00005558 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005559 __sp_mut& __m = __get_sp_mut(__p);
5560 __m.lock();
5561 if (__p->__owner_equivalent(*__v))
5562 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005563 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005564 *__p = __w;
5565 __m.unlock();
5566 return true;
5567 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005568 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005569 *__v = *__p;
5570 __m.unlock();
5571 return false;
5572}
5573
5574template <class _Tp>
5575inline _LIBCPP_INLINE_VISIBILITY
5576bool
5577atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5578{
5579 return atomic_compare_exchange_strong(__p, __v, __w);
5580}
5581
5582template <class _Tp>
5583inline _LIBCPP_INLINE_VISIBILITY
5584bool
5585atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5586 shared_ptr<_Tp> __w, memory_order, memory_order)
5587{
5588 return atomic_compare_exchange_strong(__p, __v, __w);
5589}
5590
5591template <class _Tp>
5592inline _LIBCPP_INLINE_VISIBILITY
5593bool
5594atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5595 shared_ptr<_Tp> __w, memory_order, memory_order)
5596{
5597 return atomic_compare_exchange_weak(__p, __v, __w);
5598}
5599
Eric Fiselier9b492672016-06-18 02:12:53 +00005600#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005601
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005602//enum class
Howard Hinnant8331b762013-03-06 23:30:19 +00005603struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005604{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005605 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005606 {
5607 relaxed,
5608 preferred,
5609 strict
5610 };
5611
Howard Hinnant49e145e2012-10-30 19:06:59 +00005612 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005613
Howard Hinnant756c69b2010-09-22 16:48:34 +00005614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005615 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005617 operator int() const {return __v_;}
5618};
5619
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005620_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5621_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5622_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5623_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5624_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005625
5626template <class _Tp>
5627inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005628_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005629undeclare_reachable(_Tp* __p)
5630{
5631 return static_cast<_Tp*>(__undeclare_reachable(__p));
5632}
5633
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005634_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005635
Marshall Clow8982dcd2015-07-13 20:04:56 +00005636// --- Helper for container swap --
5637template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005638inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005639void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5640#if _LIBCPP_STD_VER >= 14
5641 _NOEXCEPT
5642#else
5643 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5644#endif
5645{
5646 __swap_allocator(__a1, __a2,
5647 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5648}
5649
5650template <typename _Alloc>
5651_LIBCPP_INLINE_VISIBILITY
5652void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5653#if _LIBCPP_STD_VER >= 14
5654 _NOEXCEPT
5655#else
5656 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5657#endif
5658{
5659 using _VSTD::swap;
5660 swap(__a1, __a2);
5661}
5662
5663template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005664inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005665void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5666
Marshall Clowff91de82015-08-18 19:51:37 +00005667template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005668struct __noexcept_move_assign_container : public integral_constant<bool,
5669 _Traits::propagate_on_container_move_assignment::value
5670#if _LIBCPP_STD_VER > 14
5671 || _Traits::is_always_equal::value
5672#else
5673 && is_nothrow_move_assignable<_Alloc>::value
5674#endif
5675 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005676
Howard Hinnantc51e1022010-05-11 19:42:16 +00005677_LIBCPP_END_NAMESPACE_STD
5678
5679#endif // _LIBCPP_MEMORY