blob: 0ce7d092a2e11aadd57d7d98adf0484e0a06b15a [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MEMORY
11#define _LIBCPP_MEMORY
12
13/*
14 memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000020inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27 typedef Ptr pointer;
28 typedef <details> element_type;
29 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000030
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000032
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 static pointer pointer_to(<details>);
34};
35
Howard Hinnant719bda32011-05-28 14:41:13 +000036template <class T>
37struct pointer_traits<T*>
38{
39 typedef T* pointer;
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
42
43 template <class U> using rebind = U*;
44
Louis Dionne44e1ea82018-11-13 17:04:05 +000045 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000046};
47
Eric Fiselier45c9aac2017-11-22 19:49:21 +000048template <class T> constexpr T* to_address(T* p) noexcept; // C++20
49template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
50
Howard Hinnantc51e1022010-05-11 19:42:16 +000051template <class Alloc>
52struct allocator_traits
53{
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
56 value_type;
57
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
61 const_pointer;
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
64 void_pointer;
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
67 const_void_pointer;
68 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000069 | pointer_traits<pointer>::difference_type
70 difference_type;
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
73 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000080 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Michael Park99f9e912020-03-04 11:27:14 -050083 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
Marshall Clow0e58cae2017-11-26 02:55:38 +000086 static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Howard Hinnant719bda32011-05-28 14:41:13 +000089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
92 static void construct(allocator_type& a, T* p, Args&&... args);
93
94 template <class T>
95 static void destroy(allocator_type& a, T* p);
96
Marshall Clow4f834b52013-08-27 20:22:15 +000097 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 static allocator_type
100 select_on_container_copy_construction(const allocator_type& a);
101};
102
103template <>
Michael Park99f9e912020-03-04 11:27:14 -0500104class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105{
106public:
107 typedef void* pointer;
108 typedef const void* const_pointer;
109 typedef void value_type;
110
111 template <class _Up> struct rebind {typedef allocator<_Up> other;};
112};
113
114template <class T>
115class allocator
116{
117public:
Louis Dionnec0056af2020-08-28 12:31:16 -0400118 typedef size_t size_type;
119 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -0500120 typedef T* pointer; // deprecated in C++17, removed in C++20
121 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<T>::type
123 reference; // deprecated in C++17, removed in C++20
124 typedef typename add_lvalue_reference<const T>::type
125 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Michael Park99f9e912020-03-04 11:27:14 -0500127 typedef T value_type;
128
129 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
130
131 typedef true_type propagate_on_container_move_assignment;
132 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
Marshall Clowbc759762018-03-20 23:02:53 +0000134 constexpr allocator() noexcept; // constexpr in C++20
135 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
136 template <class U>
137 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000138 ~allocator();
Michael Park99f9e912020-03-04 11:27:14 -0500139 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
140 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
141 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
142 T* allocate(size_t n);
143 void deallocate(T* p, size_t n) noexcept;
144 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000147 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500148 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149};
150
151template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000152bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153
154template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000155bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
157template <class OutputIterator, class T>
158class raw_storage_iterator
159 : public iterator<output_iterator_tag,
160 T, // purposefully not C++03
161 ptrdiff_t, // purposefully not C++03
162 T*, // purposefully not C++03
163 raw_storage_iterator&> // purposefully not C++03
164{
165public:
166 explicit raw_storage_iterator(OutputIterator x);
167 raw_storage_iterator& operator*();
168 raw_storage_iterator& operator=(const T& element);
169 raw_storage_iterator& operator++();
170 raw_storage_iterator operator++(int);
171};
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
174template <class T> void return_temporary_buffer(T* p) noexcept;
175
176template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000177template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
179template <class InputIterator, class ForwardIterator>
180ForwardIterator
181uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
182
Howard Hinnant719bda32011-05-28 14:41:13 +0000183template <class InputIterator, class Size, class ForwardIterator>
184ForwardIterator
185uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
186
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187template <class ForwardIterator, class T>
188void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
189
190template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000191ForwardIterator
192uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000194template <class T>
195void destroy_at(T* location);
196
197template <class ForwardIterator>
198 void destroy(ForwardIterator first, ForwardIterator last);
199
200template <class ForwardIterator, class Size>
201 ForwardIterator destroy_n(ForwardIterator first, Size n);
202
203template <class InputIterator, class ForwardIterator>
204 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
205
206template <class InputIterator, class Size, class ForwardIterator>
207 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
208
209template <class ForwardIterator>
210 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
211
212template <class ForwardIterator, class Size>
213 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
214
215template <class ForwardIterator>
216 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
217
218template <class ForwardIterator, class Size>
219 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
220
Louis Dionne481a2662018-09-23 18:35:00 +0000221template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222
223template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000224class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225{
226public:
227 typedef X element_type;
228
229 explicit auto_ptr(X* p =0) throw();
230 auto_ptr(auto_ptr&) throw();
231 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
232 auto_ptr& operator=(auto_ptr&) throw();
233 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
234 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
235 ~auto_ptr() throw();
236
237 typename add_lvalue_reference<X>::type operator*() const throw();
238 X* operator->() const throw();
239 X* get() const throw();
240 X* release() throw();
241 void reset(X* p =0) throw();
242
243 auto_ptr(auto_ptr_ref<X>) throw();
244 template<class Y> operator auto_ptr_ref<Y>() throw();
245 template<class Y> operator auto_ptr<Y>() throw();
246};
247
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248template <class T>
249struct default_delete
250{
Howard Hinnant719bda32011-05-28 14:41:13 +0000251 constexpr default_delete() noexcept = default;
252 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000253
Howard Hinnant719bda32011-05-28 14:41:13 +0000254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255};
256
257template <class T>
258struct default_delete<T[]>
259{
Howard Hinnant719bda32011-05-28 14:41:13 +0000260 constexpr default_delete() noexcept = default;
261 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000262 template <class U> void operator()(U*) const = delete;
263};
264
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000265template <class T, class D = default_delete<T>>
266class unique_ptr
267{
268public:
269 typedef see below pointer;
270 typedef T element_type;
271 typedef D deleter_type;
272
273 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 constexpr unique_ptr() noexcept;
275 explicit unique_ptr(pointer p) noexcept;
276 unique_ptr(pointer p, see below d1) noexcept;
277 unique_ptr(pointer p, see below d2) noexcept;
278 unique_ptr(unique_ptr&& u) noexcept;
279 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000280 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000281 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000283 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000284
285 // destructor
286 ~unique_ptr();
287
288 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000289 unique_ptr& operator=(unique_ptr&& u) noexcept;
290 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
291 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000292
293 // observers
294 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer operator->() const noexcept;
296 pointer get() const noexcept;
297 deleter_type& get_deleter() noexcept;
298 const deleter_type& get_deleter() const noexcept;
299 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000300
301 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000302 pointer release() noexcept;
303 void reset(pointer p = pointer()) noexcept;
304 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000305};
306
307template <class T, class D>
308class unique_ptr<T[], D>
309{
310public:
311 typedef implementation-defined pointer;
312 typedef T element_type;
313 typedef D deleter_type;
314
315 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000316 constexpr unique_ptr() noexcept;
317 explicit unique_ptr(pointer p) noexcept;
318 unique_ptr(pointer p, see below d) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(unique_ptr&& u) noexcept;
321 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000324 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000325
326 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000327 unique_ptr& operator=(unique_ptr&& u) noexcept;
328 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // observers
331 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 pointer get() const noexcept;
333 deleter_type& get_deleter() noexcept;
334 const deleter_type& get_deleter() const noexcept;
335 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336
337 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000338 pointer release() noexcept;
339 void reset(pointer p = pointer()) noexcept;
340 void reset(nullptr_t) noexcept;
Vy Nguyene369bd92020-07-13 12:34:37 -0400341 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000342 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000343};
344
345template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000346 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000347
348template <class T1, class D1, class T2, class D2>
349 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350template <class T1, class D1, class T2, class D2>
351 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
352template <class T1, class D1, class T2, class D2>
353 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
354template <class T1, class D1, class T2, class D2>
355 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
356template <class T1, class D1, class T2, class D2>
357 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
358template <class T1, class D1, class T2, class D2>
359 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
360
Howard Hinnant719bda32011-05-28 14:41:13 +0000361template <class T, class D>
362 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
363template <class T, class D>
364 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
365template <class T, class D>
366 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
367template <class T, class D>
368 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
369
370template <class T, class D>
371 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
372template <class T, class D>
373 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
374template <class T, class D>
375 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
376template <class T, class D>
377 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
378template <class T, class D>
379 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
380template <class T, class D>
381 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
382template <class T, class D>
383 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
384template <class T, class D>
385 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387class bad_weak_ptr
388 : public std::exception
389{
Howard Hinnant719bda32011-05-28 14:41:13 +0000390 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000391};
392
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000393template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
394template<class T> unique_ptr<T> make_unique(size_t n); // C++14
395template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
396
Marshall Clowe52a3242017-11-27 15:51:36 +0000397template<class E, class T, class Y, class D>
398 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
399
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000400template<class T>
401class shared_ptr
402{
403public:
404 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000405 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406
407 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000408 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000409 template<class Y> explicit shared_ptr(Y* p);
410 template<class Y, class D> shared_ptr(Y* p, D d);
411 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
412 template <class D> shared_ptr(nullptr_t p, D d);
413 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000414 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
415 shared_ptr(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
417 shared_ptr(shared_ptr&& r) noexcept;
418 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000419 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000420 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000421 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
422 shared_ptr(nullptr_t) : shared_ptr() { }
423
424 // destructor:
425 ~shared_ptr();
426
427 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000428 shared_ptr& operator=(const shared_ptr& r) noexcept;
429 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
430 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000431 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000432 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000433 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
434
435 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000436 void swap(shared_ptr& r) noexcept;
437 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438 template<class Y> void reset(Y* p);
439 template<class Y, class D> void reset(Y* p, D d);
440 template<class Y, class D, class A> void reset(Y* p, D d, A a);
441
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 // observers:
443 T* get() const noexcept;
444 T& operator*() const noexcept;
445 T* operator->() const noexcept;
446 long use_count() const noexcept;
447 bool unique() const noexcept;
448 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000449 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
450 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451};
452
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400453template<class T>
454shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
455template<class T, class D>
456shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
457
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458// shared_ptr comparisons:
459template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000460 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000461template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000462 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000463template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000464 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000465template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000466 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000467template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000468 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000469template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000470 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
471
472template <class T>
473 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
474template <class T>
475 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
476template <class T>
477 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
478template <class T>
479 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
480template <class T>
481 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
482template <class T>
483bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
484template <class T>
485 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
486template <class T>
487 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
488template <class T>
489 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
490template <class T>
491 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
492template <class T>
493 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
494template <class T>
495 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000498template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000499
500// shared_ptr casts:
501template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000502 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000503template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000504 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000505template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000506 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000507
508// shared_ptr I/O:
509template<class E, class T, class Y>
510 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
511
512// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000513template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000514
515template<class T, class... Args>
516 shared_ptr<T> make_shared(Args&&... args);
517template<class T, class A, class... Args>
518 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
519
520template<class T>
521class weak_ptr
522{
523public:
524 typedef T element_type;
525
526 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 constexpr weak_ptr() noexcept;
528 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
529 weak_ptr(weak_ptr const& r) noexcept;
530 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000531 weak_ptr(weak_ptr&& r) noexcept; // C++14
532 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000533
534 // destructor
535 ~weak_ptr();
536
537 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000538 weak_ptr& operator=(weak_ptr const& r) noexcept;
539 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
540 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000541 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
542 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000543
544 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000545 void swap(weak_ptr& r) noexcept;
546 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000547
548 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000549 long use_count() const noexcept;
550 bool expired() const noexcept;
551 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000552 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
553 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000554};
555
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400556template<class T>
557weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
558
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000559// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000560template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000561
562// class owner_less:
563template<class T> struct owner_less;
564
565template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000566struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000567 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
568{
569 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000570 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
571 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
572 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000573};
574
575template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000576struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000577 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
578{
579 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000580 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
581 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
583};
584
585template <> // Added in C++14
586struct owner_less<void>
587{
588 template <class _Tp, class _Up>
589 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
590 template <class _Tp, class _Up>
591 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
592 template <class _Tp, class _Up>
593 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
594 template <class _Tp, class _Up>
595 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
596
597 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000598};
599
600template<class T>
601class enable_shared_from_this
602{
603protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000604 constexpr enable_shared_from_this() noexcept;
605 enable_shared_from_this(enable_shared_from_this const&) noexcept;
606 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000607 ~enable_shared_from_this();
608public:
609 shared_ptr<T> shared_from_this();
610 shared_ptr<T const> shared_from_this() const;
611};
612
613template<class T>
614 bool atomic_is_lock_free(const shared_ptr<T>* p);
615template<class T>
616 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
617template<class T>
618 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
619template<class T>
620 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
621template<class T>
622 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
623template<class T>
624 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
625template<class T>
626 shared_ptr<T>
627 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
628template<class T>
629 bool
630 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
631template<class T>
632 bool
633 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
634template<class T>
635 bool
636 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
637 shared_ptr<T> w, memory_order success,
638 memory_order failure);
639template<class T>
640 bool
641 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
642 shared_ptr<T> w, memory_order success,
643 memory_order failure);
644// Hash support
645template <class T> struct hash;
646template <class T, class D> struct hash<unique_ptr<T, D> >;
647template <class T> struct hash<shared_ptr<T> >;
648
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000649template <class T, class Alloc>
650 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
651
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000652// Pointer safety
653enum class pointer_safety { relaxed, preferred, strict };
654void declare_reachable(void *p);
655template <class T> T *undeclare_reachable(T *p);
656void declare_no_pointers(char *p, size_t n);
657void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000658pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000659
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
661
662} // std
663
664*/
665
666#include <__config>
667#include <type_traits>
668#include <typeinfo>
669#include <cstddef>
670#include <cstdint>
671#include <new>
672#include <utility>
673#include <limits>
674#include <iterator>
675#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000676#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000677#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000678#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000679#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000680#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000681# include <atomic>
682#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000683#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000684
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000685#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000687#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688
Eric Fiselierf4433a32017-05-31 22:07:49 +0000689_LIBCPP_PUSH_MACROS
690#include <__undef_macros>
691
692
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693_LIBCPP_BEGIN_NAMESPACE_STD
694
Eric Fiselier89659d12015-07-07 00:27:16 +0000695template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000696inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000697_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
698#if !defined(_LIBCPP_HAS_NO_THREADS) && \
699 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000700 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000701 return __atomic_load_n(__value, __ATOMIC_RELAXED);
702#else
703 return *__value;
704#endif
705}
706
Kuba Breckade9d6792016-09-04 09:55:12 +0000707template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000708inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000709_ValueType __libcpp_acquire_load(_ValueType const* __value) {
710#if !defined(_LIBCPP_HAS_NO_THREADS) && \
711 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000712 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000713 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
714#else
715 return *__value;
716#endif
717}
718
Marshall Clow78dbe462016-11-14 18:22:19 +0000719// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000720
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721template <class _Tp> class allocator;
722
Michael Park99f9e912020-03-04 11:27:14 -0500723#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724template <>
Michael Park99f9e912020-03-04 11:27:14 -0500725class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726{
727public:
728 typedef void* pointer;
729 typedef const void* const_pointer;
730 typedef void value_type;
731
732 template <class _Up> struct rebind {typedef allocator<_Up> other;};
733};
734
Howard Hinnant9f771052012-01-19 23:15:22 +0000735template <>
Michael Park99f9e912020-03-04 11:27:14 -0500736class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000737{
738public:
739 typedef const void* pointer;
740 typedef const void* const_pointer;
741 typedef const void value_type;
742
743 template <class _Up> struct rebind {typedef allocator<_Up> other;};
744};
Michael Park99f9e912020-03-04 11:27:14 -0500745#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000746
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747// pointer_traits
748
Marshall Clow0be70c32017-06-14 21:23:57 +0000749template <class _Tp, class = void>
750struct __has_element_type : false_type {};
751
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000753struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000754 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755
756template <class _Ptr, bool = __has_element_type<_Ptr>::value>
757struct __pointer_traits_element_type;
758
759template <class _Ptr>
760struct __pointer_traits_element_type<_Ptr, true>
761{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000762 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763};
764
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765template <template <class, class...> class _Sp, class _Tp, class ..._Args>
766struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
767{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000768 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769};
770
771template <template <class, class...> class _Sp, class _Tp, class ..._Args>
772struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
773{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000774 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775};
776
Marshall Clow0be70c32017-06-14 21:23:57 +0000777template <class _Tp, class = void>
778struct __has_difference_type : false_type {};
779
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000781struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000782 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783
784template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
785struct __pointer_traits_difference_type
786{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000787 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788};
789
790template <class _Ptr>
791struct __pointer_traits_difference_type<_Ptr, true>
792{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000793 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794};
795
796template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500797struct __has_rebind
798{
799private:
800 struct __two {char __lx; char __lxx;};
801 template <class _Xp> static __two __test(...);
Louis Dionne95f24792020-03-04 14:38:51 -0500802 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Louis Dionnef1bb8492020-03-04 13:54:58 -0500803 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
Louis Dionne95f24792020-03-04 14:38:51 -0500804 _LIBCPP_SUPPRESS_DEPRECATED_POP
Louis Dionnef1bb8492020-03-04 13:54:58 -0500805public:
806 static const bool value = sizeof(__test<_Tp>(0)) == 1;
807};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808
809template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
810struct __pointer_traits_rebind
811{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000812#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000813 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000815 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816#endif
817};
818
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
820struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
821{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000822#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000823 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000825 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826#endif
827};
828
829template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
830struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
831{
832 typedef _Sp<_Up, _Args...> type;
833};
834
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000836struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837{
838 typedef _Ptr pointer;
839 typedef typename __pointer_traits_element_type<pointer>::type element_type;
840 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
841
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000842#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000843 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844#else
845 template <class _Up> struct rebind
846 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000847#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000848
849private:
850 struct __nat {};
851public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853 static pointer pointer_to(typename conditional<is_void<element_type>::value,
854 __nat, element_type>::type& __r)
855 {return pointer::pointer_to(__r);}
856};
857
858template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000859struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860{
861 typedef _Tp* pointer;
862 typedef _Tp element_type;
863 typedef ptrdiff_t difference_type;
864
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000865#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866 template <class _Up> using rebind = _Up*;
867#else
868 template <class _Up> struct rebind {typedef _Up* other;};
869#endif
870
871private:
872 struct __nat {};
873public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000874 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000876 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000877 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878};
879
Eric Fiselierae3ab842015-08-23 02:56:05 +0000880template <class _From, class _To>
881struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000882#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000883 typedef typename pointer_traits<_From>::template rebind<_To> type;
884#else
885 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
886#endif
887};
888
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889// allocator_traits
890
Marshall Clow0be70c32017-06-14 21:23:57 +0000891template <class _Tp, class = void>
892struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893
894template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000895struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000896 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897
898namespace __pointer_type_imp
899{
900
901template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
902struct __pointer_type
903{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000904 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905};
906
907template <class _Tp, class _Dp>
908struct __pointer_type<_Tp, _Dp, false>
909{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000910 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911};
912
Howard Hinnant8e4e6002010-11-18 01:40:00 +0000913} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914
915template <class _Tp, class _Dp>
916struct __pointer_type
917{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000918 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919};
920
Marshall Clow0be70c32017-06-14 21:23:57 +0000921template <class _Tp, class = void>
922struct __has_const_pointer : false_type {};
923
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000925struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000926 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927
928template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
929struct __const_pointer
930{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000931 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932};
933
934template <class _Tp, class _Ptr, class _Alloc>
935struct __const_pointer<_Tp, _Ptr, _Alloc, false>
936{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000937#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000938 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939#else
940 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
941#endif
942};
943
Marshall Clow0be70c32017-06-14 21:23:57 +0000944template <class _Tp, class = void>
945struct __has_void_pointer : false_type {};
946
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000948struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000949 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
951template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
952struct __void_pointer
953{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000954 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955};
956
957template <class _Ptr, class _Alloc>
958struct __void_pointer<_Ptr, _Alloc, false>
959{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000960#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000961 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000963 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964#endif
965};
966
Marshall Clow0be70c32017-06-14 21:23:57 +0000967template <class _Tp, class = void>
968struct __has_const_void_pointer : false_type {};
969
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000971struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000972 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973
974template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
975struct __const_void_pointer
976{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000977 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978};
979
980template <class _Ptr, class _Alloc>
981struct __const_void_pointer<_Ptr, _Alloc, false>
982{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000983#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000984 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000986 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987#endif
988};
989
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500990
991template <bool _UsePointerTraits> struct __to_address_helper;
992
993template <> struct __to_address_helper<true> {
994 template <class _Pointer>
995 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
996
997 template <class _Pointer>
998 _LIBCPP_CONSTEXPR
999 static __return_type<_Pointer>
1000 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1001};
1002
1003template <class _Pointer, bool _Dummy = true>
1004using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1005
1006
Howard Hinnantc834c512011-11-29 18:15:50 +00001007template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001008inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001009_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001010__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001012 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 return __p;
1014}
1015
1016template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001017inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1018typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1019__to_address(const _Pointer& __p) _NOEXCEPT {
1020 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001021}
1022
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001023template <> struct __to_address_helper<false> {
1024 template <class _Pointer>
1025 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001026
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001027 template <class _Pointer>
1028 _LIBCPP_CONSTEXPR
1029 static __return_type<_Pointer>
1030 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1031};
1032
1033
1034#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001035template <class _Tp>
1036inline _LIBCPP_INLINE_VISIBILITY constexpr
1037_Tp*
1038to_address(_Tp* __p) _NOEXCEPT
1039{
1040 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1041 return __p;
1042}
1043
1044template <class _Pointer>
1045inline _LIBCPP_INLINE_VISIBILITY
1046auto
1047to_address(const _Pointer& __p) _NOEXCEPT
1048{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001049 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001050}
1051#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052
Marshall Clow0be70c32017-06-14 21:23:57 +00001053template <class _Tp, class = void>
1054struct __has_size_type : false_type {};
1055
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001057struct __has_size_type<_Tp,
1058 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001060template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061struct __size_type
1062{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001063 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064};
1065
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001066template <class _Alloc, class _DiffType>
1067struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001069 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070};
1071
Marshall Clow0be70c32017-06-14 21:23:57 +00001072template <class _Tp, class = void>
1073struct __has_propagate_on_container_copy_assignment : false_type {};
1074
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001076struct __has_propagate_on_container_copy_assignment<_Tp,
1077 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1078 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079
1080template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1081struct __propagate_on_container_copy_assignment
1082{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001083 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084};
1085
1086template <class _Alloc>
1087struct __propagate_on_container_copy_assignment<_Alloc, true>
1088{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001089 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090};
1091
Marshall Clow0be70c32017-06-14 21:23:57 +00001092template <class _Tp, class = void>
1093struct __has_propagate_on_container_move_assignment : false_type {};
1094
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001096struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001097 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1098 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099
1100template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1101struct __propagate_on_container_move_assignment
1102{
1103 typedef false_type type;
1104};
1105
1106template <class _Alloc>
1107struct __propagate_on_container_move_assignment<_Alloc, true>
1108{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001109 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110};
1111
Marshall Clow0be70c32017-06-14 21:23:57 +00001112template <class _Tp, class = void>
1113struct __has_propagate_on_container_swap : false_type {};
1114
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001116struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001117 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1118 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119
1120template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1121struct __propagate_on_container_swap
1122{
1123 typedef false_type type;
1124};
1125
1126template <class _Alloc>
1127struct __propagate_on_container_swap<_Alloc, true>
1128{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001129 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130};
1131
Marshall Clow0be70c32017-06-14 21:23:57 +00001132template <class _Tp, class = void>
1133struct __has_is_always_equal : false_type {};
1134
Marshall Clow0b587562015-06-02 16:34:03 +00001135template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001136struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001137 typename __void_t<typename _Tp::is_always_equal>::type>
1138 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001139
1140template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1141struct __is_always_equal
1142{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001143 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001144};
1145
1146template <class _Alloc>
1147struct __is_always_equal<_Alloc, true>
1148{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001149 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001150};
1151
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1153struct __has_rebind_other
1154{
1155private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001156 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001158 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001160 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161public:
1162 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1163};
1164
1165template <class _Tp, class _Up>
1166struct __has_rebind_other<_Tp, _Up, false>
1167{
1168 static const bool value = false;
1169};
1170
1171template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1172struct __allocator_traits_rebind
1173{
Michael Park99f9e912020-03-04 11:27:14 -05001174 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001175 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001176 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177};
1178
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1180struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1181{
Michael Park99f9e912020-03-04 11:27:14 -05001182 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001183 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001184 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185};
1186
1187template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1188struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1189{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001190 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191};
1192
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001193#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194
Michael Park99f9e912020-03-04 11:27:14 -05001195_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1197auto
1198__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001199 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001200_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201
1202template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1203auto
1204__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1205 -> false_type;
1206
1207template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1208struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001209 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1210 declval<_SizeType>(),
1211 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212{
1213};
1214
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001215#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216
1217template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1218struct __has_allocate_hint
1219 : true_type
1220{
1221};
1222
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001223#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224
zoecarver75816d42020-05-23 14:32:12 -07001225_LIBCPP_SUPPRESS_DEPRECATED_PUSH
zoecarver20590dd2020-05-23 14:03:04 -07001226template <class _Alloc, class ..._Args,
1227 class = decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Args>()...))>
1228static true_type __test_has_construct(int);
zoecarver75816d42020-05-23 14:32:12 -07001229_LIBCPP_SUPPRESS_DEPRECATED_POP
1230
zoecarver20590dd2020-05-23 14:03:04 -07001231template <class _Alloc, class...>
1232static false_type __test_has_construct(...);
1233
1234template <class _Alloc, class ..._Args>
1235struct __has_construct : decltype(__test_has_construct<_Alloc, _Args...>(0)) {};
1236
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001237#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238
Michael Park99f9e912020-03-04 11:27:14 -05001239_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240template <class _Alloc, class _Pointer>
1241auto
1242__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1243 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001244_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
1246template <class _Alloc, class _Pointer>
1247auto
1248__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1249 -> false_type;
1250
1251template <class _Alloc, class _Pointer>
1252struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001253 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1254 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255{
1256};
1257
Michael Park99f9e912020-03-04 11:27:14 -05001258_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259template <class _Alloc>
1260auto
1261__has_max_size_test(_Alloc&& __a)
1262 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001263_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264
1265template <class _Alloc>
1266auto
1267__has_max_size_test(const volatile _Alloc& __a)
1268 -> false_type;
1269
1270template <class _Alloc>
1271struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001272 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273{
1274};
1275
1276template <class _Alloc>
1277auto
1278__has_select_on_container_copy_construction_test(_Alloc&& __a)
1279 -> decltype(__a.select_on_container_copy_construction(), true_type());
1280
1281template <class _Alloc>
1282auto
1283__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1284 -> false_type;
1285
1286template <class _Alloc>
1287struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001288 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289{
1290};
1291
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001292#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001294template <class _Alloc, class _Pointer, class = void>
1295struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296
1297template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001298struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1299 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1300>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301
1302template <class _Alloc>
1303struct __has_max_size
1304 : true_type
1305{
1306};
1307
1308template <class _Alloc>
1309struct __has_select_on_container_copy_construction
1310 : false_type
1311{
1312};
1313
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001314#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001316template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1317struct __alloc_traits_difference_type
1318{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001319 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001320};
1321
1322template <class _Alloc, class _Ptr>
1323struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1324{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001325 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001326};
1327
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001328template <class _Tp>
1329struct __is_default_allocator : false_type {};
1330
1331template <class _Tp>
1332struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1333
Eric Fiselier909fe962019-09-13 16:09:33 +00001334
1335
1336template <class _Alloc,
1337 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1338 >
1339struct __is_cpp17_move_insertable;
1340template <class _Alloc>
1341struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1342template <class _Alloc>
1343struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1344
1345template <class _Alloc,
1346 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1347 >
1348struct __is_cpp17_copy_insertable;
1349template <class _Alloc>
1350struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1351template <class _Alloc>
1352struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1353 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1354 __is_cpp17_move_insertable<_Alloc>::value>
1355 {};
1356
1357
1358
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001360struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361{
1362 typedef _Alloc allocator_type;
1363 typedef typename allocator_type::value_type value_type;
1364
1365 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1366 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1367 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1368 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1369
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001370 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1371 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372
1373 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1374 propagate_on_container_copy_assignment;
1375 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1376 propagate_on_container_move_assignment;
1377 typedef typename __propagate_on_container_swap<allocator_type>::type
1378 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001379 typedef typename __is_always_equal<allocator_type>::type
1380 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001382#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001384 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001385 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001386#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387 template <class _Tp> struct rebind_alloc
1388 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1389 template <class _Tp> struct rebind_traits
1390 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001391#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392
Marshall Clow0e58cae2017-11-26 02:55:38 +00001393 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 static pointer allocate(allocator_type& __a, size_type __n)
1395 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001396 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001398 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1400
Howard Hinnant756c69b2010-09-22 16:48:34 +00001401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001402 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403 {__a.deallocate(__p, __n);}
1404
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001408 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001409 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410
1411 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413 static void destroy(allocator_type& __a, _Tp* __p)
1414 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1415
Howard Hinnant756c69b2010-09-22 16:48:34 +00001416 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001417 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1419
Howard Hinnant756c69b2010-09-22 16:48:34 +00001420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 static allocator_type
1422 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001423 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424 __has_select_on_container_copy_construction<const allocator_type>(),
1425 __a);}
1426
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001427 template <class _Ptr>
1428 _LIBCPP_INLINE_VISIBILITY
1429 static
1430 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001431 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001432 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001433 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1434 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001435 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001436 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001437#ifdef _LIBCPP_NO_EXCEPTIONS
1438 _VSTD::move(*__begin1)
1439#else
1440 _VSTD::move_if_noexcept(*__begin1)
1441#endif
1442 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001443 }
1444
1445 template <class _Tp>
1446 _LIBCPP_INLINE_VISIBILITY
1447 static
1448 typename enable_if
1449 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001450 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001451 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1452 is_trivially_move_constructible<_Tp>::value,
1453 void
1454 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001455 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001456 {
1457 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001458 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001459 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001460 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001461 __begin2 += _Np;
1462 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001463 }
1464
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001465 template <class _Iter, class _Ptr>
1466 _LIBCPP_INLINE_VISIBILITY
1467 static
1468 void
1469 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1470 {
1471 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001472 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001473 }
1474
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001475 template <class _SourceTp, class _DestTp,
1476 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1477 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001478 _LIBCPP_INLINE_VISIBILITY
1479 static
1480 typename enable_if
1481 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001482 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001483 is_same<_RawSourceTp, _RawDestTp>::value &&
1484 (__is_default_allocator<allocator_type>::value ||
1485 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001486 void
1487 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001488 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001489 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001490 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001491 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001492 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001493 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001494 __begin2 += _Np;
1495 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001496 }
1497
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001498 template <class _Ptr>
1499 _LIBCPP_INLINE_VISIBILITY
1500 static
1501 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001502 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001503 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001504 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1505 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001506 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001507 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001508 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001509#ifdef _LIBCPP_NO_EXCEPTIONS
1510 _VSTD::move(*--__end1)
1511#else
1512 _VSTD::move_if_noexcept(*--__end1)
1513#endif
1514 );
1515 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001516 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001517 }
1518
1519 template <class _Tp>
1520 _LIBCPP_INLINE_VISIBILITY
1521 static
1522 typename enable_if
1523 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001524 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001525 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1526 is_trivially_move_constructible<_Tp>::value,
1527 void
1528 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001529 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001530 {
1531 ptrdiff_t _Np = __end1 - __begin1;
1532 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001533 if (_Np > 0)
1534 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001535 }
1536
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537private:
1538
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001540 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001542 {
1543 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1544 return __a.allocate(__n, __hint);
1545 _LIBCPP_SUPPRESS_DEPRECATED_POP
1546 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001548 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001549 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 {return __a.allocate(__n);}
1551
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001555 {
1556 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1557 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1558 _LIBCPP_SUPPRESS_DEPRECATED_POP
1559 }
1560
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1564 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001565 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567
1568 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001571 {
1572 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1573 __a.destroy(__p);
1574 _LIBCPP_SUPPRESS_DEPRECATED_POP
1575 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 static void __destroy(false_type, allocator_type&, _Tp* __p)
1579 {
1580 __p->~_Tp();
1581 }
1582
Howard Hinnant756c69b2010-09-22 16:48:34 +00001583 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001584 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001585 {
1586 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1587 return __a.max_size();
1588 _LIBCPP_SUPPRESS_DEPRECATED_POP
1589 }
1590
Howard Hinnant756c69b2010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001592 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001593 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001597 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001601 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 {return __a;}
1603};
1604
Marshall Clow940e01c2015-04-07 05:21:38 +00001605template <class _Traits, class _Tp>
1606struct __rebind_alloc_helper
1607{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001608#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001609 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001610#else
1611 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1612#endif
1613};
1614
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615// allocator
1616
1617template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001618class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619{
1620public:
Louis Dionnec0056af2020-08-28 12:31:16 -04001621 typedef size_t size_type;
1622 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -05001623#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -05001624 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1625 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1626 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1627 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1628
1629 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1630#endif
1631
1632 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633
Howard Hinnant4931e092011-06-02 21:38:57 +00001634 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001635 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001636
Marshall Clowbc759762018-03-20 23:02:53 +00001637 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1638 allocator() _NOEXCEPT {}
1639
Louis Dionne481a2662018-09-23 18:35:00 +00001640 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001641 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1642 allocator(const allocator<_Up>&) _NOEXCEPT {}
1643
Michael Park99f9e912020-03-04 11:27:14 -05001644#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1645 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1646 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001647 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001648 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1649 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001650 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001651#endif
1652
1653 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001654 {
Michael Park99f9e912020-03-04 11:27:14 -05001655 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1656 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001657 __throw_length_error("allocator<T>::allocate(size_t n)"
1658 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001659 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001660 }
Michael Park99f9e912020-03-04 11:27:14 -05001661
1662#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1663 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1664 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1665#endif
1666
1667 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001668 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001669
1670#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1671 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001672 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001673
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001675 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676 void
1677 construct(_Up* __p, _Args&&... __args)
1678 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001679 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 }
Michael Park99f9e912020-03-04 11:27:14 -05001681 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1682#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683};
1684
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001685template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001686class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001687{
1688public:
Louis Dionnec0056af2020-08-28 12:31:16 -04001689 typedef size_t size_type;
1690 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -05001691#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -05001692 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1693 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1694 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1695 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1696
1697 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1698#endif
1699
1700 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001701
1702 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001703 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001704
Marshall Clowbc759762018-03-20 23:02:53 +00001705 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1706 allocator() _NOEXCEPT {}
1707
1708 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001709 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001710 allocator(const allocator<_Up>&) _NOEXCEPT {}
1711
Michael Park99f9e912020-03-04 11:27:14 -05001712#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1713 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1714 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001715 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001716#endif
1717
1718 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001719 {
Michael Park99f9e912020-03-04 11:27:14 -05001720 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1721 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001722 __throw_length_error("allocator<const T>::allocate(size_t n)"
1723 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001724 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001725 }
Michael Park99f9e912020-03-04 11:27:14 -05001726
1727#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1728 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1729 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1730#endif
1731
1732 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001733 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001734
1735#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1736 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001737 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001738
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001739 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001740 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001741 void
1742 construct(_Up* __p, _Args&&... __args)
1743 {
1744 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1745 }
Howard Hinnant9e028f12012-05-01 15:37:54 +00001746
Michael Park99f9e912020-03-04 11:27:14 -05001747 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1748#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001749};
1750
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751template <class _Tp, class _Up>
1752inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001753bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754
1755template <class _Tp, class _Up>
1756inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001757bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758
1759template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001760class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 : public iterator<output_iterator_tag,
1762 _Tp, // purposefully not C++03
1763 ptrdiff_t, // purposefully not C++03
1764 _Tp*, // purposefully not C++03
1765 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1766{
1767private:
1768 _OutputIterator __x_;
1769public:
1770 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1771 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1772 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001773 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001774#if _LIBCPP_STD_VER >= 14
1775 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001776 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001777#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1779 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1780 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001781#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001782 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001783#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784};
1785
1786template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001787_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001789get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790{
1791 pair<_Tp*, ptrdiff_t> __r(0, 0);
1792 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1793 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1794 / sizeof(_Tp);
1795 if (__n > __m)
1796 __n = __m;
1797 while (__n > 0)
1798 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001799#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001800 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001801 {
1802 std::align_val_t __al =
1803 std::align_val_t(std::alignment_of<_Tp>::value);
1804 __r.first = static_cast<_Tp*>(::operator new(
1805 __n * sizeof(_Tp), __al, nothrow));
1806 } else {
1807 __r.first = static_cast<_Tp*>(::operator new(
1808 __n * sizeof(_Tp), nothrow));
1809 }
1810#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001811 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001812 {
1813 // Since aligned operator new is unavailable, return an empty
1814 // buffer rather than one with invalid alignment.
1815 return __r;
1816 }
1817
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001819#endif
1820
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 if (__r.first)
1822 {
1823 __r.second = __n;
1824 break;
1825 }
1826 __n /= 2;
1827 }
1828 return __r;
1829}
1830
1831template <class _Tp>
1832inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001833void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1834{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001835 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001836}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837
Marshall Clowb22274f2017-01-24 22:22:33 +00001838#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001840struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841{
1842 _Tp* __ptr_;
1843};
1844
1845template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001846class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847{
1848private:
1849 _Tp* __ptr_;
1850public:
1851 typedef _Tp element_type;
1852
Dimitry Andric47269ce2020-03-13 19:36:26 +01001853 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1854 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1855 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001857 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001859 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001861 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001863 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864
Dimitry Andric47269ce2020-03-13 19:36:26 +01001865 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001867 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1868 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1869 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 {
1871 _Tp* __t = __ptr_;
1872 __ptr_ = 0;
1873 return __t;
1874 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001875 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 {
1877 if (__ptr_ != __p)
1878 delete __ptr_;
1879 __ptr_ = __p;
1880 }
1881
Dimitry Andric47269ce2020-03-13 19:36:26 +01001882 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1883 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001885 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 {return auto_ptr<_Up>(release());}
1887};
1888
1889template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001890class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891{
1892public:
1893 typedef void element_type;
1894};
Marshall Clowb22274f2017-01-24 22:22:33 +00001895#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001897// Tag used to default initialize one or both of the pair's elements.
1898struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001899struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001900
Eric Fiselier9d355982017-04-12 23:45:53 +00001901template <class _Tp, int _Idx,
1902 bool _CanBeEmptyBase =
1903 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1904struct __compressed_pair_elem {
1905 typedef _Tp _ParamT;
1906 typedef _Tp& reference;
1907 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001909 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001910 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001911 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1912 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913
Eric Fiselier9d355982017-04-12 23:45:53 +00001914 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001915 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1916 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001917 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001918 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001919 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001920 : __value_(_VSTD::forward<_Up>(__u))
1921 {
1922 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001924
1925#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001926 template <class... _Args, size_t... _Indexes>
1927 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1928 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1929 __tuple_indices<_Indexes...>)
1930 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001931#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001933
Alex Lorenz76132112017-11-09 17:54:49 +00001934 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1935 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001936 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001939 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940};
1941
Eric Fiselier9d355982017-04-12 23:45:53 +00001942template <class _Tp, int _Idx>
1943struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1944 typedef _Tp _ParamT;
1945 typedef _Tp& reference;
1946 typedef const _Tp& const_reference;
1947 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001949 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1950 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1951 __compressed_pair_elem(__default_init_tag) {}
1952 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1953 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954
Eric Fiselier9d355982017-04-12 23:45:53 +00001955 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001956 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1957 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001958 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001959 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001960 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001961 : __value_type(_VSTD::forward<_Up>(__u))
1962 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001964#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001965 template <class... _Args, size_t... _Indexes>
1966 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1967 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1968 __tuple_indices<_Indexes...>)
1969 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001970#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971
Alex Lorenz76132112017-11-09 17:54:49 +00001972 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1973 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001974 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975};
1976
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00001978class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1979 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001980 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1981 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00001982
1983 // NOTE: This static assert should never fire because __compressed_pair
1984 // is *almost never* used in a scenario where it's possible for T1 == T2.
1985 // (The exception is std::function where it is possible that the function
1986 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00001987 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00001988 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
1989 "The current implementation is NOT ABI-compatible with the previous "
1990 "implementation for this configuration");
1991
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001993 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00001994 class = typename enable_if<
1995 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1996 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1997 >::type
1998 >
Eric Fiselier9d355982017-04-12 23:45:53 +00001999 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002000 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001
Eric Fiselier9d355982017-04-12 23:45:53 +00002002 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002003 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002004 __compressed_pair(_U1&& __t1, _U2&& __t2)
2005 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002007#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002008 template <class... _Args1, class... _Args2>
2009 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2010 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2011 tuple<_Args2...> __second_args)
2012 : _Base1(__pc, _VSTD::move(__first_args),
2013 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2014 _Base2(__pc, _VSTD::move(__second_args),
2015 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002016#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017
Eric Fiselier9d355982017-04-12 23:45:53 +00002018 _LIBCPP_INLINE_VISIBILITY
2019 typename _Base1::reference first() _NOEXCEPT {
2020 return static_cast<_Base1&>(*this).__get();
2021 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022
Eric Fiselier9d355982017-04-12 23:45:53 +00002023 _LIBCPP_INLINE_VISIBILITY
2024 typename _Base1::const_reference first() const _NOEXCEPT {
2025 return static_cast<_Base1 const&>(*this).__get();
2026 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027
Eric Fiselier9d355982017-04-12 23:45:53 +00002028 _LIBCPP_INLINE_VISIBILITY
2029 typename _Base2::reference second() _NOEXCEPT {
2030 return static_cast<_Base2&>(*this).__get();
2031 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032
Eric Fiselier9d355982017-04-12 23:45:53 +00002033 _LIBCPP_INLINE_VISIBILITY
2034 typename _Base2::const_reference second() const _NOEXCEPT {
2035 return static_cast<_Base2 const&>(*this).__get();
2036 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037
Eric Fiselier9d355982017-04-12 23:45:53 +00002038 _LIBCPP_INLINE_VISIBILITY
2039 void swap(__compressed_pair& __x)
2040 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2041 __is_nothrow_swappable<_T2>::value)
2042 {
2043 using std::swap;
2044 swap(first(), __x.first());
2045 swap(second(), __x.second());
2046 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047};
2048
2049template <class _T1, class _T2>
2050inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002051void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2052 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2053 __is_nothrow_swappable<_T2>::value) {
2054 __x.swap(__y);
2055}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002057// default_delete
2058
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002060struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002061 static_assert(!is_function<_Tp>::value,
2062 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002063#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002064 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002065#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002066 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002067#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002068 template <class _Up>
2069 _LIBCPP_INLINE_VISIBILITY
2070 default_delete(const default_delete<_Up>&,
2071 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2072 0) _NOEXCEPT {}
2073
2074 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2075 static_assert(sizeof(_Tp) > 0,
2076 "default_delete can not delete incomplete type");
2077 static_assert(!is_void<_Tp>::value,
2078 "default_delete can not delete incomplete type");
2079 delete __ptr;
2080 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081};
2082
2083template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002084struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2085private:
2086 template <class _Up>
2087 struct _EnableIfConvertible
2088 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2089
Howard Hinnant4500ca52011-12-18 21:19:44 +00002090public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002091#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002092 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002093#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002094 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002095#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002096
2097 template <class _Up>
2098 _LIBCPP_INLINE_VISIBILITY
2099 default_delete(const default_delete<_Up[]>&,
2100 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2101
2102 template <class _Up>
2103 _LIBCPP_INLINE_VISIBILITY
2104 typename _EnableIfConvertible<_Up>::type
2105 operator()(_Up* __ptr) const _NOEXCEPT {
2106 static_assert(sizeof(_Tp) > 0,
2107 "default_delete can not delete incomplete type");
2108 static_assert(!is_void<_Tp>::value,
2109 "default_delete can not delete void type");
2110 delete[] __ptr;
2111 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112};
2113
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002114template <class _Deleter>
2115struct __unique_ptr_deleter_sfinae {
2116 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2117 typedef const _Deleter& __lval_ref_type;
2118 typedef _Deleter&& __good_rval_ref_type;
2119 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120};
2121
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002122template <class _Deleter>
2123struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2124 typedef const _Deleter& __lval_ref_type;
2125 typedef const _Deleter&& __bad_rval_ref_type;
2126 typedef false_type __enable_rval_overload;
2127};
2128
2129template <class _Deleter>
2130struct __unique_ptr_deleter_sfinae<_Deleter&> {
2131 typedef _Deleter& __lval_ref_type;
2132 typedef _Deleter&& __bad_rval_ref_type;
2133 typedef false_type __enable_rval_overload;
2134};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002135
Vy Nguyene369bd92020-07-13 12:34:37 -04002136#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
2137# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2138#else
2139# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
2140#endif
2141
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002142template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04002143class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002144public:
2145 typedef _Tp element_type;
2146 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002147 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002148
2149 static_assert(!is_rvalue_reference<deleter_type>::value,
2150 "the specified deleter type cannot be an rvalue reference");
2151
2152private:
2153 __compressed_pair<pointer, deleter_type> __ptr_;
2154
2155 struct __nat { int __for_bool_; };
2156
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002157 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002158
2159 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002160 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002161 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002162
2163 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002164 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002165 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002166
2167 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002168 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002169 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002170
2171 template <bool _Dummy, class _Deleter = typename __dependent_type<
2172 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002173 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002174 typename enable_if<is_default_constructible<_Deleter>::value &&
2175 !is_pointer<_Deleter>::value>::type;
2176
2177 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002178 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002179 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2180
2181 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002182 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002183 is_convertible<typename _UPtr::pointer, pointer>::value &&
2184 !is_array<_Up>::value
2185 >::type;
2186
2187 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002188 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002189 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2190 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2191 >::type;
2192
2193 template <class _UDel>
2194 using _EnableIfDeleterAssignable = typename enable_if<
2195 is_assignable<_Dp&, _UDel&&>::value
2196 >::type;
2197
2198public:
2199 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002200 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002201 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002202 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002203
2204 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002205 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002206 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002207 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002208
2209 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002210 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002211 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002212 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002213
2214 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002215 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002216 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002217 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002218 : __ptr_(__p, __d) {}
2219
2220 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002221 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002222 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002223 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002224 : __ptr_(__p, _VSTD::move(__d)) {
2225 static_assert(!is_reference<deleter_type>::value,
2226 "rvalue deleter bound to reference");
2227 }
2228
2229 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002230 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002231 _LIBCPP_INLINE_VISIBILITY
2232 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2233
2234 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002235 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002236 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2237 }
2238
2239 template <class _Up, class _Ep,
2240 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2241 class = _EnableIfDeleterConvertible<_Ep>
2242 >
2243 _LIBCPP_INLINE_VISIBILITY
2244 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2245 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2246
2247#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2248 template <class _Up>
2249 _LIBCPP_INLINE_VISIBILITY
2250 unique_ptr(auto_ptr<_Up>&& __p,
2251 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002252 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002253 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002254 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002255#endif
2256
2257 _LIBCPP_INLINE_VISIBILITY
2258 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2259 reset(__u.release());
2260 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2261 return *this;
2262 }
2263
2264 template <class _Up, class _Ep,
2265 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2266 class = _EnableIfDeleterAssignable<_Ep>
2267 >
2268 _LIBCPP_INLINE_VISIBILITY
2269 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2270 reset(__u.release());
2271 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2272 return *this;
2273 }
2274
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002275#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2276 template <class _Up>
2277 _LIBCPP_INLINE_VISIBILITY
2278 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2279 is_same<_Dp, default_delete<_Tp> >::value,
2280 unique_ptr&>::type
2281 operator=(auto_ptr<_Up> __p) {
2282 reset(__p.release());
2283 return *this;
2284 }
2285#endif
2286
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002287#ifdef _LIBCPP_CXX03_LANG
2288 unique_ptr(unique_ptr const&) = delete;
2289 unique_ptr& operator=(unique_ptr const&) = delete;
2290#endif
2291
2292
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002293 _LIBCPP_INLINE_VISIBILITY
2294 ~unique_ptr() { reset(); }
2295
2296 _LIBCPP_INLINE_VISIBILITY
2297 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2298 reset();
2299 return *this;
2300 }
2301
2302 _LIBCPP_INLINE_VISIBILITY
2303 typename add_lvalue_reference<_Tp>::type
2304 operator*() const {
2305 return *__ptr_.first();
2306 }
2307 _LIBCPP_INLINE_VISIBILITY
2308 pointer operator->() const _NOEXCEPT {
2309 return __ptr_.first();
2310 }
2311 _LIBCPP_INLINE_VISIBILITY
2312 pointer get() const _NOEXCEPT {
2313 return __ptr_.first();
2314 }
2315 _LIBCPP_INLINE_VISIBILITY
2316 deleter_type& get_deleter() _NOEXCEPT {
2317 return __ptr_.second();
2318 }
2319 _LIBCPP_INLINE_VISIBILITY
2320 const deleter_type& get_deleter() const _NOEXCEPT {
2321 return __ptr_.second();
2322 }
2323 _LIBCPP_INLINE_VISIBILITY
2324 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2325 return __ptr_.first() != nullptr;
2326 }
2327
2328 _LIBCPP_INLINE_VISIBILITY
2329 pointer release() _NOEXCEPT {
2330 pointer __t = __ptr_.first();
2331 __ptr_.first() = pointer();
2332 return __t;
2333 }
2334
2335 _LIBCPP_INLINE_VISIBILITY
2336 void reset(pointer __p = pointer()) _NOEXCEPT {
2337 pointer __tmp = __ptr_.first();
2338 __ptr_.first() = __p;
2339 if (__tmp)
2340 __ptr_.second()(__tmp);
2341 }
2342
2343 _LIBCPP_INLINE_VISIBILITY
2344 void swap(unique_ptr& __u) _NOEXCEPT {
2345 __ptr_.swap(__u.__ptr_);
2346 }
2347};
2348
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002349
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002351class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002353 typedef _Tp element_type;
2354 typedef _Dp deleter_type;
2355 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2356
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002358 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359
Eric Fiselier31127cd2017-04-16 02:14:31 +00002360 template <class _From>
2361 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362
Eric Fiselier31127cd2017-04-16 02:14:31 +00002363 template <class _FromElem>
2364 struct _CheckArrayPointerConversion<_FromElem*>
2365 : integral_constant<bool,
2366 is_same<_FromElem*, pointer>::value ||
2367 (is_same<pointer, element_type*>::value &&
2368 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2369 >
2370 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371
Eric Fiselier31127cd2017-04-16 02:14:31 +00002372 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002373
2374 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002375 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002376 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002377
2378 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002379 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002380 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002381
2382 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002383 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002384 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002385
2386 template <bool _Dummy, class _Deleter = typename __dependent_type<
2387 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002388 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002389 typename enable_if<is_default_constructible<_Deleter>::value &&
2390 !is_pointer<_Deleter>::value>::type;
2391
2392 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002393 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002394 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2395
2396 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002397 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002398 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002399 >::type;
2400
2401 template <class _UPtr, class _Up,
2402 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002403 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002404 is_array<_Up>::value &&
2405 is_same<pointer, element_type*>::value &&
2406 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2407 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2408 >::type;
2409
2410 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002411 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002412 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2413 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2414 >::type;
2415
2416 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002417 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002418 is_assignable<_Dp&, _UDel&&>::value
2419 >::type;
2420
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002422 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002423 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002424 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002425 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002427 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002428 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002429 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002430 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002432 template <class _Pp, bool _Dummy = true,
2433 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002434 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002435 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002436 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002437 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002439 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002440 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2441 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002442 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002443 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002444 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002446 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002447 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002448 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002449 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002450 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002452 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002453 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2454 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002455 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002456 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002457 : __ptr_(__p, _VSTD::move(__d)) {
2458 static_assert(!is_reference<deleter_type>::value,
2459 "rvalue deleter bound to reference");
2460 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002462 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002463 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002464 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002465 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002466 : __ptr_(nullptr, _VSTD::move(__d)) {
2467 static_assert(!is_reference<deleter_type>::value,
2468 "rvalue deleter bound to reference");
2469 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002470
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002471 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002472 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2473 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002474 _LIBCPP_INLINE_VISIBILITY
2475 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002476
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002477 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002478 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002479 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2480 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002481
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002482 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002483 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002484 reset(__u.release());
2485 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2486 return *this;
2487 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002489 template <class _Up, class _Ep,
2490 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2491 class = _EnableIfDeleterConvertible<_Ep>
2492 >
2493 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002494 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002495 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002496 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002498 template <class _Up, class _Ep,
2499 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2500 class = _EnableIfDeleterAssignable<_Ep>
2501 >
2502 _LIBCPP_INLINE_VISIBILITY
2503 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002504 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002505 reset(__u.release());
2506 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2507 return *this;
2508 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002510#ifdef _LIBCPP_CXX03_LANG
2511 unique_ptr(unique_ptr const&) = delete;
2512 unique_ptr& operator=(unique_ptr const&) = delete;
2513#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002514
2515public:
2516 _LIBCPP_INLINE_VISIBILITY
2517 ~unique_ptr() { reset(); }
2518
2519 _LIBCPP_INLINE_VISIBILITY
2520 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2521 reset();
2522 return *this;
2523 }
2524
2525 _LIBCPP_INLINE_VISIBILITY
2526 typename add_lvalue_reference<_Tp>::type
2527 operator[](size_t __i) const {
2528 return __ptr_.first()[__i];
2529 }
2530 _LIBCPP_INLINE_VISIBILITY
2531 pointer get() const _NOEXCEPT {
2532 return __ptr_.first();
2533 }
2534
2535 _LIBCPP_INLINE_VISIBILITY
2536 deleter_type& get_deleter() _NOEXCEPT {
2537 return __ptr_.second();
2538 }
2539
2540 _LIBCPP_INLINE_VISIBILITY
2541 const deleter_type& get_deleter() const _NOEXCEPT {
2542 return __ptr_.second();
2543 }
2544 _LIBCPP_INLINE_VISIBILITY
2545 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2546 return __ptr_.first() != nullptr;
2547 }
2548
2549 _LIBCPP_INLINE_VISIBILITY
2550 pointer release() _NOEXCEPT {
2551 pointer __t = __ptr_.first();
2552 __ptr_.first() = pointer();
2553 return __t;
2554 }
2555
2556 template <class _Pp>
2557 _LIBCPP_INLINE_VISIBILITY
2558 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002559 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002560 >::type
2561 reset(_Pp __p) _NOEXCEPT {
2562 pointer __tmp = __ptr_.first();
2563 __ptr_.first() = __p;
2564 if (__tmp)
2565 __ptr_.second()(__tmp);
2566 }
2567
2568 _LIBCPP_INLINE_VISIBILITY
2569 void reset(nullptr_t = nullptr) _NOEXCEPT {
2570 pointer __tmp = __ptr_.first();
2571 __ptr_.first() = nullptr;
2572 if (__tmp)
2573 __ptr_.second()(__tmp);
2574 }
2575
2576 _LIBCPP_INLINE_VISIBILITY
2577 void swap(unique_ptr& __u) _NOEXCEPT {
2578 __ptr_.swap(__u.__ptr_);
2579 }
2580
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581};
2582
2583template <class _Tp, class _Dp>
2584inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002585typename enable_if<
2586 __is_swappable<_Dp>::value,
2587 void
2588>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002589swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590
2591template <class _T1, class _D1, class _T2, class _D2>
2592inline _LIBCPP_INLINE_VISIBILITY
2593bool
2594operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2595
2596template <class _T1, class _D1, class _T2, class _D2>
2597inline _LIBCPP_INLINE_VISIBILITY
2598bool
2599operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2600
2601template <class _T1, class _D1, class _T2, class _D2>
2602inline _LIBCPP_INLINE_VISIBILITY
2603bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002604operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2605{
2606 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2607 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002608 typedef typename common_type<_P1, _P2>::type _Vp;
2609 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002610}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611
2612template <class _T1, class _D1, class _T2, class _D2>
2613inline _LIBCPP_INLINE_VISIBILITY
2614bool
2615operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2616
2617template <class _T1, class _D1, class _T2, class _D2>
2618inline _LIBCPP_INLINE_VISIBILITY
2619bool
2620operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2621
2622template <class _T1, class _D1, class _T2, class _D2>
2623inline _LIBCPP_INLINE_VISIBILITY
2624bool
2625operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2626
Howard Hinnantb17caf92012-02-21 21:02:58 +00002627template <class _T1, class _D1>
2628inline _LIBCPP_INLINE_VISIBILITY
2629bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002630operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002631{
2632 return !__x;
2633}
2634
2635template <class _T1, class _D1>
2636inline _LIBCPP_INLINE_VISIBILITY
2637bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002638operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002639{
2640 return !__x;
2641}
2642
2643template <class _T1, class _D1>
2644inline _LIBCPP_INLINE_VISIBILITY
2645bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002646operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002647{
2648 return static_cast<bool>(__x);
2649}
2650
2651template <class _T1, class _D1>
2652inline _LIBCPP_INLINE_VISIBILITY
2653bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002654operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002655{
2656 return static_cast<bool>(__x);
2657}
2658
2659template <class _T1, class _D1>
2660inline _LIBCPP_INLINE_VISIBILITY
2661bool
2662operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2663{
2664 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2665 return less<_P1>()(__x.get(), nullptr);
2666}
2667
2668template <class _T1, class _D1>
2669inline _LIBCPP_INLINE_VISIBILITY
2670bool
2671operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2672{
2673 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2674 return less<_P1>()(nullptr, __x.get());
2675}
2676
2677template <class _T1, class _D1>
2678inline _LIBCPP_INLINE_VISIBILITY
2679bool
2680operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2681{
2682 return nullptr < __x;
2683}
2684
2685template <class _T1, class _D1>
2686inline _LIBCPP_INLINE_VISIBILITY
2687bool
2688operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2689{
2690 return __x < nullptr;
2691}
2692
2693template <class _T1, class _D1>
2694inline _LIBCPP_INLINE_VISIBILITY
2695bool
2696operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2697{
2698 return !(nullptr < __x);
2699}
2700
2701template <class _T1, class _D1>
2702inline _LIBCPP_INLINE_VISIBILITY
2703bool
2704operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2705{
2706 return !(__x < nullptr);
2707}
2708
2709template <class _T1, class _D1>
2710inline _LIBCPP_INLINE_VISIBILITY
2711bool
2712operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2713{
2714 return !(__x < nullptr);
2715}
2716
2717template <class _T1, class _D1>
2718inline _LIBCPP_INLINE_VISIBILITY
2719bool
2720operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2721{
2722 return !(nullptr < __x);
2723}
2724
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002725#if _LIBCPP_STD_VER > 11
2726
2727template<class _Tp>
2728struct __unique_if
2729{
2730 typedef unique_ptr<_Tp> __unique_single;
2731};
2732
2733template<class _Tp>
2734struct __unique_if<_Tp[]>
2735{
2736 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2737};
2738
2739template<class _Tp, size_t _Np>
2740struct __unique_if<_Tp[_Np]>
2741{
2742 typedef void __unique_array_known_bound;
2743};
2744
2745template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002746inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002747typename __unique_if<_Tp>::__unique_single
2748make_unique(_Args&&... __args)
2749{
2750 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2751}
2752
2753template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002754inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002755typename __unique_if<_Tp>::__unique_array_unknown_bound
2756make_unique(size_t __n)
2757{
2758 typedef typename remove_extent<_Tp>::type _Up;
2759 return unique_ptr<_Tp>(new _Up[__n]());
2760}
2761
2762template<class _Tp, class... _Args>
2763 typename __unique_if<_Tp>::__unique_array_known_bound
2764 make_unique(_Args&&...) = delete;
2765
2766#endif // _LIBCPP_STD_VER > 11
2767
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002768template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002769#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002770struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002771#else
2772struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002773 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002774#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002775{
2776 typedef unique_ptr<_Tp, _Dp> argument_type;
2777 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002778 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002779 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002780 {
2781 typedef typename argument_type::pointer pointer;
2782 return hash<pointer>()(__ptr.get());
2783 }
2784};
2785
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786struct __destruct_n
2787{
2788private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002789 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790
2791 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002792 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002793 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794
2795 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002796 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 {}
2798
Howard Hinnant719bda32011-05-28 14:41:13 +00002799 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002800 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002801 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 {}
2803
Howard Hinnant719bda32011-05-28 14:41:13 +00002804 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002805 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002806 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 {}
2808public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002809 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002810 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811
2812 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002813 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002814 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815
2816 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002817 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002818 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819
2820 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002821 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002822 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823};
2824
2825template <class _Alloc>
2826class __allocator_destructor
2827{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002828 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002830 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2831 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832private:
2833 _Alloc& __alloc_;
2834 size_type __s_;
2835public:
2836 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002837 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002840 void operator()(pointer __p) _NOEXCEPT
2841 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842};
2843
2844template <class _InputIterator, class _ForwardIterator>
2845_ForwardIterator
2846uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2847{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002849#ifndef _LIBCPP_NO_EXCEPTIONS
2850 _ForwardIterator __s = __r;
2851 try
2852 {
2853#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002854 for (; __f != __l; ++__f, (void) ++__r)
2855 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002856#ifndef _LIBCPP_NO_EXCEPTIONS
2857 }
2858 catch (...)
2859 {
2860 for (; __s != __r; ++__s)
2861 __s->~value_type();
2862 throw;
2863 }
2864#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865 return __r;
2866}
2867
2868template <class _InputIterator, class _Size, class _ForwardIterator>
2869_ForwardIterator
2870uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2871{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002873#ifndef _LIBCPP_NO_EXCEPTIONS
2874 _ForwardIterator __s = __r;
2875 try
2876 {
2877#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002878 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
2879 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002880#ifndef _LIBCPP_NO_EXCEPTIONS
2881 }
2882 catch (...)
2883 {
2884 for (; __s != __r; ++__s)
2885 __s->~value_type();
2886 throw;
2887 }
2888#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889 return __r;
2890}
2891
2892template <class _ForwardIterator, class _Tp>
2893void
2894uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2895{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002897#ifndef _LIBCPP_NO_EXCEPTIONS
2898 _ForwardIterator __s = __f;
2899 try
2900 {
2901#endif
2902 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002903 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002904#ifndef _LIBCPP_NO_EXCEPTIONS
2905 }
2906 catch (...)
2907 {
2908 for (; __s != __f; ++__s)
2909 __s->~value_type();
2910 throw;
2911 }
2912#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913}
2914
2915template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002916_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2918{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002920#ifndef _LIBCPP_NO_EXCEPTIONS
2921 _ForwardIterator __s = __f;
2922 try
2923 {
2924#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002925 for (; __n > 0; ++__f, (void) --__n)
2926 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002927#ifndef _LIBCPP_NO_EXCEPTIONS
2928 }
2929 catch (...)
2930 {
2931 for (; __s != __f; ++__s)
2932 __s->~value_type();
2933 throw;
2934 }
2935#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002936 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937}
2938
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002939#if _LIBCPP_STD_VER > 14
2940
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002941template <class _Tp>
2942inline _LIBCPP_INLINE_VISIBILITY
2943void destroy_at(_Tp* __loc) {
2944 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
2945 __loc->~_Tp();
2946}
2947
2948template <class _ForwardIterator>
2949inline _LIBCPP_INLINE_VISIBILITY
2950void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2951 for (; __first != __last; ++__first)
2952 _VSTD::destroy_at(_VSTD::addressof(*__first));
2953}
2954
2955template <class _ForwardIterator, class _Size>
2956inline _LIBCPP_INLINE_VISIBILITY
2957_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2958 for (; __n > 0; (void)++__first, --__n)
2959 _VSTD::destroy_at(_VSTD::addressof(*__first));
2960 return __first;
2961}
2962
Eric Fiselier290c07c2016-10-11 21:13:44 +00002963template <class _ForwardIterator>
2964inline _LIBCPP_INLINE_VISIBILITY
2965void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2966 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2967 auto __idx = __first;
2968#ifndef _LIBCPP_NO_EXCEPTIONS
2969 try {
2970#endif
2971 for (; __idx != __last; ++__idx)
2972 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
2973#ifndef _LIBCPP_NO_EXCEPTIONS
2974 } catch (...) {
2975 _VSTD::destroy(__first, __idx);
2976 throw;
2977 }
2978#endif
2979}
2980
2981template <class _ForwardIterator, class _Size>
2982inline _LIBCPP_INLINE_VISIBILITY
2983_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2984 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2985 auto __idx = __first;
2986#ifndef _LIBCPP_NO_EXCEPTIONS
2987 try {
2988#endif
2989 for (; __n > 0; (void)++__idx, --__n)
2990 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
2991 return __idx;
2992#ifndef _LIBCPP_NO_EXCEPTIONS
2993 } catch (...) {
2994 _VSTD::destroy(__first, __idx);
2995 throw;
2996 }
2997#endif
2998}
2999
3000
3001template <class _ForwardIterator>
3002inline _LIBCPP_INLINE_VISIBILITY
3003void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3004 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3005 auto __idx = __first;
3006#ifndef _LIBCPP_NO_EXCEPTIONS
3007 try {
3008#endif
3009 for (; __idx != __last; ++__idx)
3010 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3011#ifndef _LIBCPP_NO_EXCEPTIONS
3012 } catch (...) {
3013 _VSTD::destroy(__first, __idx);
3014 throw;
3015 }
3016#endif
3017}
3018
3019template <class _ForwardIterator, class _Size>
3020inline _LIBCPP_INLINE_VISIBILITY
3021_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3022 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3023 auto __idx = __first;
3024#ifndef _LIBCPP_NO_EXCEPTIONS
3025 try {
3026#endif
3027 for (; __n > 0; (void)++__idx, --__n)
3028 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3029 return __idx;
3030#ifndef _LIBCPP_NO_EXCEPTIONS
3031 } catch (...) {
3032 _VSTD::destroy(__first, __idx);
3033 throw;
3034 }
3035#endif
3036}
3037
3038
3039template <class _InputIt, class _ForwardIt>
3040inline _LIBCPP_INLINE_VISIBILITY
3041_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3042 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3043 auto __idx = __first_res;
3044#ifndef _LIBCPP_NO_EXCEPTIONS
3045 try {
3046#endif
3047 for (; __first != __last; (void)++__idx, ++__first)
3048 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3049 return __idx;
3050#ifndef _LIBCPP_NO_EXCEPTIONS
3051 } catch (...) {
3052 _VSTD::destroy(__first_res, __idx);
3053 throw;
3054 }
3055#endif
3056}
3057
3058template <class _InputIt, class _Size, class _ForwardIt>
3059inline _LIBCPP_INLINE_VISIBILITY
3060pair<_InputIt, _ForwardIt>
3061uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3062 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3063 auto __idx = __first_res;
3064#ifndef _LIBCPP_NO_EXCEPTIONS
3065 try {
3066#endif
3067 for (; __n > 0; ++__idx, (void)++__first, --__n)
3068 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3069 return {__first, __idx};
3070#ifndef _LIBCPP_NO_EXCEPTIONS
3071 } catch (...) {
3072 _VSTD::destroy(__first_res, __idx);
3073 throw;
3074 }
3075#endif
3076}
3077
3078
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003079#endif // _LIBCPP_STD_VER > 14
3080
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003081// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3082// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003083// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003084#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3085 && defined(__ATOMIC_RELAXED) \
3086 && defined(__ATOMIC_ACQ_REL)
3087# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003088#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003089# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3090#endif
3091
3092template <class _Tp>
3093inline _LIBCPP_INLINE_VISIBILITY _Tp
3094__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3095{
3096#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3097 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3098#else
3099 return __t += 1;
3100#endif
3101}
3102
3103template <class _Tp>
3104inline _LIBCPP_INLINE_VISIBILITY _Tp
3105__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3106{
3107#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3108 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3109#else
3110 return __t -= 1;
3111#endif
3112}
3113
Howard Hinnant756c69b2010-09-22 16:48:34 +00003114class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 : public std::exception
3116{
3117public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003118 bad_weak_ptr() _NOEXCEPT = default;
3119 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003120 virtual ~bad_weak_ptr() _NOEXCEPT;
3121 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122};
3123
Louis Dionne16fe2952018-07-11 23:14:33 +00003124_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003125void __throw_bad_weak_ptr()
3126{
3127#ifndef _LIBCPP_NO_EXCEPTIONS
3128 throw bad_weak_ptr();
3129#else
3130 _VSTD::abort();
3131#endif
3132}
3133
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003134template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003136class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137{
3138 __shared_count(const __shared_count&);
3139 __shared_count& operator=(const __shared_count&);
3140
3141protected:
3142 long __shared_owners_;
3143 virtual ~__shared_count();
3144private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003145 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146
3147public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003149 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150 : __shared_owners_(__refs) {}
3151
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003152#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003153 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003154 void __add_shared() _NOEXCEPT;
3155 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003156#else
3157 _LIBCPP_INLINE_VISIBILITY
3158 void __add_shared() _NOEXCEPT {
3159 __libcpp_atomic_refcount_increment(__shared_owners_);
3160 }
3161 _LIBCPP_INLINE_VISIBILITY
3162 bool __release_shared() _NOEXCEPT {
3163 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3164 __on_zero_shared();
3165 return true;
3166 }
3167 return false;
3168 }
3169#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003170 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003171 long use_count() const _NOEXCEPT {
3172 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3173 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174};
3175
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003176class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177 : private __shared_count
3178{
3179 long __shared_weak_owners_;
3180
3181public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003183 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184 : __shared_count(__refs),
3185 __shared_weak_owners_(__refs) {}
3186protected:
3187 virtual ~__shared_weak_count();
3188
3189public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003190#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003191 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003192 void __add_shared() _NOEXCEPT;
3193 void __add_weak() _NOEXCEPT;
3194 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003195#else
3196 _LIBCPP_INLINE_VISIBILITY
3197 void __add_shared() _NOEXCEPT {
3198 __shared_count::__add_shared();
3199 }
3200 _LIBCPP_INLINE_VISIBILITY
3201 void __add_weak() _NOEXCEPT {
3202 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3203 }
3204 _LIBCPP_INLINE_VISIBILITY
3205 void __release_shared() _NOEXCEPT {
3206 if (__shared_count::__release_shared())
3207 __release_weak();
3208 }
3209#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003210 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003212 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3213 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214
Howard Hinnant807d6332013-02-25 15:50:36 +00003215 // Define the function out only if we build static libc++ without RTTI.
3216 // Otherwise we may break clients who need to compile their projects with
3217 // -fno-rtti and yet link against a libc++.dylib compiled
3218 // without -fno-rtti.
3219#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003220 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003221#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003223 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224};
3225
3226template <class _Tp, class _Dp, class _Alloc>
3227class __shared_ptr_pointer
3228 : public __shared_weak_count
3229{
3230 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3231public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003234 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235
Howard Hinnant72f73582010-08-11 17:04:31 +00003236#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003237 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239
3240private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003241 virtual void __on_zero_shared() _NOEXCEPT;
3242 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243};
3244
Howard Hinnant72f73582010-08-11 17:04:31 +00003245#ifndef _LIBCPP_NO_RTTI
3246
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247template <class _Tp, class _Dp, class _Alloc>
3248const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003249__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003251 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252}
3253
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003254#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003255
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256template <class _Tp, class _Dp, class _Alloc>
3257void
Howard Hinnant719bda32011-05-28 14:41:13 +00003258__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259{
3260 __data_.first().second()(__data_.first().first());
3261 __data_.first().second().~_Dp();
3262}
3263
3264template <class _Tp, class _Dp, class _Alloc>
3265void
Howard Hinnant719bda32011-05-28 14:41:13 +00003266__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003268 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3269 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003270 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3271
Eric Fiselierf8898c82015-02-05 23:01:40 +00003272 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003274 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275}
3276
3277template <class _Tp, class _Alloc>
3278class __shared_ptr_emplace
3279 : public __shared_weak_count
3280{
3281 __compressed_pair<_Alloc, _Tp> __data_;
3282public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003283
Howard Hinnant756c69b2010-09-22 16:48:34 +00003284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003285 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003286 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003287
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003288#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003292 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3293 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003294#else
3295 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003296 _LIBCPP_INLINE_VISIBILITY
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003297 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
3298 : __data_(__a, _Tp(_VSTD::forward<_Args>(__args)...)) {}
3299#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300
3301private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003302 virtual void __on_zero_shared() _NOEXCEPT;
3303 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003305 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003306 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307};
3308
3309template <class _Tp, class _Alloc>
3310void
Howard Hinnant719bda32011-05-28 14:41:13 +00003311__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312{
3313 __data_.second().~_Tp();
3314}
3315
3316template <class _Tp, class _Alloc>
3317void
Howard Hinnant719bda32011-05-28 14:41:13 +00003318__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003320 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3321 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003322 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003323 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003325 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003326}
3327
Erik Pilkington2a398762017-05-25 15:43:31 +00003328struct __shared_ptr_dummy_rebind_allocator_type;
3329template <>
3330class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3331{
3332public:
3333 template <class _Other>
3334 struct rebind
3335 {
3336 typedef allocator<_Other> other;
3337 };
3338};
3339
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003340template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341
zoecarverd73f42a2020-05-11 18:42:50 -07003342template<class _Tp, class _Up>
3343struct __compatible_with
3344#if _LIBCPP_STD_VER > 14
3345 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3346#else
3347 : is_convertible<_Tp*, _Up*> {};
3348#endif // _LIBCPP_STD_VER > 14
3349
Vy Nguyene369bd92020-07-13 12:34:37 -04003350#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
3351# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
3352#else
3353# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
3354#endif
3355
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003357class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003359public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003360#if _LIBCPP_STD_VER > 14
3361 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003362 typedef remove_extent_t<_Tp> element_type;
3363#else
3364 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003365#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003366
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367private:
3368 element_type* __ptr_;
3369 __shared_weak_count* __cntrl_;
3370
3371 struct __nat {int __for_bool_;};
3372public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003374 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003376 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003377 template<class _Yp>
3378 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003379 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003380 template<class _Yp, class _Dp>
3381 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003382 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003383 template<class _Yp, class _Dp, class _Alloc>
3384 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003385 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3387 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003388 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003390 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003394 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003395 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003397 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003398 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003399 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003400 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003402 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003403#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00003404 template<class _Yp>
3405 shared_ptr(auto_ptr<_Yp>&& __r,
3406 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003407#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00003408 template <class _Yp, class _Dp>
3409 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3410 typename enable_if
3411 <
3412 !is_lvalue_reference<_Dp>::value &&
3413 !is_array<_Yp>::value &&
3414 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3415 __nat
3416 >::type = __nat());
3417 template <class _Yp, class _Dp>
3418 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3419 typename enable_if
3420 <
3421 is_lvalue_reference<_Dp>::value &&
3422 !is_array<_Yp>::value &&
3423 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3424 __nat
3425 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426
3427 ~shared_ptr();
3428
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003430 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003431 template<class _Yp>
3432 typename enable_if
3433 <
zoecarverd73f42a2020-05-11 18:42:50 -07003434 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003435 shared_ptr&
3436 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003438 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003440 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003441 template<class _Yp>
3442 typename enable_if
3443 <
zoecarverd73f42a2020-05-11 18:42:50 -07003444 __compatible_with<_Yp, element_type>::value,
3445 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003446 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003448 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003449#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003450 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003452 typename enable_if
3453 <
3454 !is_array<_Yp>::value &&
3455 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003456 shared_ptr
3457 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003458 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003459#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003460 template <class _Yp, class _Dp>
3461 typename enable_if
3462 <
3463 !is_array<_Yp>::value &&
3464 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3465 shared_ptr&
3466 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003468 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003471 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003473 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003474 template<class _Yp>
3475 typename enable_if
3476 <
zoecarverd73f42a2020-05-11 18:42:50 -07003477 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003478 void
3479 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003481 reset(_Yp* __p);
3482 template<class _Yp, class _Dp>
3483 typename enable_if
3484 <
zoecarverd73f42a2020-05-11 18:42:50 -07003485 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003486 void
3487 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003489 reset(_Yp* __p, _Dp __d);
3490 template<class _Yp, class _Dp, class _Alloc>
3491 typename enable_if
3492 <
zoecarverd73f42a2020-05-11 18:42:50 -07003493 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003494 void
3495 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003497 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498
Howard Hinnant756c69b2010-09-22 16:48:34 +00003499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003500 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003502 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3503 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003504 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003505 element_type* operator->() const _NOEXCEPT
3506 {
3507 static_assert(!_VSTD::is_array<_Tp>::value,
3508 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3509 return __ptr_;
3510 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003512 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003514 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003516 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003517 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003518 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003519 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003521 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003522 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003523 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003525 _LIBCPP_INLINE_VISIBILITY
3526 bool
3527 __owner_equivalent(const shared_ptr& __p) const
3528 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529
zoecarverd73f42a2020-05-11 18:42:50 -07003530#if _LIBCPP_STD_VER > 14
3531 typename add_lvalue_reference<element_type>::type
3532 _LIBCPP_INLINE_VISIBILITY
3533 operator[](ptrdiff_t __i) const
3534 {
3535 static_assert(_VSTD::is_array<_Tp>::value,
3536 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3537 return __ptr_[__i];
3538 }
3539#endif
3540
Howard Hinnant72f73582010-08-11 17:04:31 +00003541#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003544 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003545 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003546 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003547 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003548#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549
Zoe Carverd9040c72019-10-22 15:16:49 +00003550 template<class _Yp, class _CntrlBlk>
3551 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07003552 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00003553 {
3554 shared_ptr<_Tp> __r;
3555 __r.__ptr_ = __p;
3556 __r.__cntrl_ = __cntrl;
3557 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3558 return __r;
3559 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003560
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003562 template <class _Yp, bool = is_function<_Yp>::value>
3563 struct __shared_ptr_default_allocator
3564 {
3565 typedef allocator<_Yp> type;
3566 };
3567
3568 template <class _Yp>
3569 struct __shared_ptr_default_allocator<_Yp, true>
3570 {
3571 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3572 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003574 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003575 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003576 typename enable_if<is_convertible<_OrigPtr*,
3577 const enable_shared_from_this<_Yp>*
3578 >::value,
3579 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003580 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3581 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003583 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003584 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003585 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003586 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3587 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003588 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589 }
3590
Erik Pilkington2a398762017-05-25 15:43:31 +00003591 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592
zoecarverd73f42a2020-05-11 18:42:50 -07003593 template <class, class _Yp>
3594 struct __shared_ptr_default_delete
3595 : default_delete<_Yp> {};
3596
3597 template <class _Yp, class _Un, size_t _Sz>
3598 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3599 : default_delete<_Yp[]> {};
3600
3601 template <class _Yp, class _Un>
3602 struct __shared_ptr_default_delete<_Yp[], _Un>
3603 : default_delete<_Yp[]> {};
3604
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003605 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3606 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607};
3608
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003609#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3610template<class _Tp>
3611shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3612template<class _Tp, class _Dp>
3613shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3614#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003615
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003617inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003618_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003619shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620 : __ptr_(0),
3621 __cntrl_(0)
3622{
3623}
3624
3625template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003626inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003627_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003628shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629 : __ptr_(0),
3630 __cntrl_(0)
3631{
3632}
3633
3634template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003635template<class _Yp>
3636shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003637 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638 : __ptr_(__p)
3639{
3640 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003641 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07003642 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3643 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003645 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646}
3647
3648template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003649template<class _Yp, class _Dp>
3650shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003651 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652 : __ptr_(__p)
3653{
3654#ifndef _LIBCPP_NO_EXCEPTIONS
3655 try
3656 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003657#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003658 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3659 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3660 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003661 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662#ifndef _LIBCPP_NO_EXCEPTIONS
3663 }
3664 catch (...)
3665 {
3666 __d(__p);
3667 throw;
3668 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003669#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670}
3671
3672template<class _Tp>
3673template<class _Dp>
3674shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3675 : __ptr_(0)
3676{
3677#ifndef _LIBCPP_NO_EXCEPTIONS
3678 try
3679 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003680#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003681 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3682 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3683 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684#ifndef _LIBCPP_NO_EXCEPTIONS
3685 }
3686 catch (...)
3687 {
3688 __d(__p);
3689 throw;
3690 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003691#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692}
3693
3694template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003695template<class _Yp, class _Dp, class _Alloc>
3696shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003697 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698 : __ptr_(__p)
3699{
3700#ifndef _LIBCPP_NO_EXCEPTIONS
3701 try
3702 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003703#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003705 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706 typedef __allocator_destructor<_A2> _D2;
3707 _A2 __a2(__a);
3708 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003709 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3710 _CntrlBlk(__p, __d, __a);
3711 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003712 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713#ifndef _LIBCPP_NO_EXCEPTIONS
3714 }
3715 catch (...)
3716 {
3717 __d(__p);
3718 throw;
3719 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003720#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721}
3722
3723template<class _Tp>
3724template<class _Dp, class _Alloc>
3725shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3726 : __ptr_(0)
3727{
3728#ifndef _LIBCPP_NO_EXCEPTIONS
3729 try
3730 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003731#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003733 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734 typedef __allocator_destructor<_A2> _D2;
3735 _A2 __a2(__a);
3736 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003737 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3738 _CntrlBlk(__p, __d, __a);
3739 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740#ifndef _LIBCPP_NO_EXCEPTIONS
3741 }
3742 catch (...)
3743 {
3744 __d(__p);
3745 throw;
3746 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003747#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748}
3749
3750template<class _Tp>
3751template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003752inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003753shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754 : __ptr_(__p),
3755 __cntrl_(__r.__cntrl_)
3756{
3757 if (__cntrl_)
3758 __cntrl_->__add_shared();
3759}
3760
3761template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003762inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003763shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764 : __ptr_(__r.__ptr_),
3765 __cntrl_(__r.__cntrl_)
3766{
3767 if (__cntrl_)
3768 __cntrl_->__add_shared();
3769}
3770
3771template<class _Tp>
3772template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003773inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003775 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003776 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777 : __ptr_(__r.__ptr_),
3778 __cntrl_(__r.__cntrl_)
3779{
3780 if (__cntrl_)
3781 __cntrl_->__add_shared();
3782}
3783
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003785inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003786shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787 : __ptr_(__r.__ptr_),
3788 __cntrl_(__r.__cntrl_)
3789{
3790 __r.__ptr_ = 0;
3791 __r.__cntrl_ = 0;
3792}
3793
3794template<class _Tp>
3795template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003796inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003798 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003799 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003800 : __ptr_(__r.__ptr_),
3801 __cntrl_(__r.__cntrl_)
3802{
3803 __r.__ptr_ = 0;
3804 __r.__cntrl_ = 0;
3805}
3806
Marshall Clowb22274f2017-01-24 22:22:33 +00003807#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003809template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003810shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003811 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812 : __ptr_(__r.get())
3813{
3814 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3815 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003816 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817 __r.release();
3818}
Marshall Clowb22274f2017-01-24 22:22:33 +00003819#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820
3821template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003822template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003824 typename enable_if
3825 <
3826 !is_lvalue_reference<_Dp>::value &&
3827 !is_array<_Yp>::value &&
3828 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3829 __nat
3830 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003831 : __ptr_(__r.get())
3832{
Marshall Clow35cde742015-05-10 13:59:45 +00003833#if _LIBCPP_STD_VER > 11
3834 if (__ptr_ == nullptr)
3835 __cntrl_ = nullptr;
3836 else
3837#endif
3838 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003839 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3840 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3841 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003842 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003843 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844 __r.release();
3845}
3846
3847template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003848template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003850 typename enable_if
3851 <
3852 is_lvalue_reference<_Dp>::value &&
3853 !is_array<_Yp>::value &&
3854 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3855 __nat
3856 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857 : __ptr_(__r.get())
3858{
Marshall Clow35cde742015-05-10 13:59:45 +00003859#if _LIBCPP_STD_VER > 11
3860 if (__ptr_ == nullptr)
3861 __cntrl_ = nullptr;
3862 else
3863#endif
3864 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003865 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003866 typedef __shared_ptr_pointer<_Yp*,
3867 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003868 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003869 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003870 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003871 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872 __r.release();
3873}
3874
Zoe Carver6cd05c32019-08-19 15:47:16 +00003875template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876shared_ptr<_Tp>::~shared_ptr()
3877{
3878 if (__cntrl_)
3879 __cntrl_->__release_shared();
3880}
3881
3882template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003883inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003885shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886{
3887 shared_ptr(__r).swap(*this);
3888 return *this;
3889}
3890
3891template<class _Tp>
3892template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003893inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003894typename enable_if
3895<
zoecarverd73f42a2020-05-11 18:42:50 -07003896 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003897 shared_ptr<_Tp>&
3898>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003899shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900{
3901 shared_ptr(__r).swap(*this);
3902 return *this;
3903}
3904
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003906inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003908shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003910 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911 return *this;
3912}
3913
3914template<class _Tp>
3915template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003916inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003917typename enable_if
3918<
zoecarverd73f42a2020-05-11 18:42:50 -07003919 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003920 shared_ptr<_Tp>&
3921>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003922shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3923{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003924 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925 return *this;
3926}
3927
Marshall Clowb22274f2017-01-24 22:22:33 +00003928#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929template<class _Tp>
3930template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003931inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003932typename enable_if
3933<
3934 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003935 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003936 shared_ptr<_Tp>
3937>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003938shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3939{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003940 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941 return *this;
3942}
Marshall Clowb22274f2017-01-24 22:22:33 +00003943#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944
3945template<class _Tp>
3946template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003947inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003948typename enable_if
3949<
3950 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003951 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003952 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003953 shared_ptr<_Tp>&
3954>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3956{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003957 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958 return *this;
3959}
3960
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003962inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963void
Howard Hinnant719bda32011-05-28 14:41:13 +00003964shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003966 _VSTD::swap(__ptr_, __r.__ptr_);
3967 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968}
3969
3970template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003971inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972void
Howard Hinnant719bda32011-05-28 14:41:13 +00003973shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974{
3975 shared_ptr().swap(*this);
3976}
3977
3978template<class _Tp>
3979template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003980inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003981typename enable_if
3982<
zoecarverd73f42a2020-05-11 18:42:50 -07003983 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003984 void
3985>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986shared_ptr<_Tp>::reset(_Yp* __p)
3987{
3988 shared_ptr(__p).swap(*this);
3989}
3990
3991template<class _Tp>
3992template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003993inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003994typename enable_if
3995<
zoecarverd73f42a2020-05-11 18:42:50 -07003996 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003997 void
3998>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4000{
4001 shared_ptr(__p, __d).swap(*this);
4002}
4003
4004template<class _Tp>
4005template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004006inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004007typename enable_if
4008<
zoecarverd73f42a2020-05-11 18:42:50 -07004009 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004010 void
4011>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4013{
4014 shared_ptr(__p, __d, __a).swap(*this);
4015}
4016
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004017template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004019typename enable_if
4020<
4021 !is_array<_Tp>::value,
4022 shared_ptr<_Tp>
4023>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024make_shared(_Args&& ...__args)
4025{
Zoe Carverd9040c72019-10-22 15:16:49 +00004026 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4027 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4028 typedef allocator<_CntrlBlk> _A2;
4029 typedef __allocator_destructor<_A2> _D2;
4030
4031 _A2 __a2;
4032 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4033 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4034
4035 _Tp *__ptr = __hold2.get()->get();
4036 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037}
4038
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004039template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004041typename enable_if
4042<
4043 !is_array<_Tp>::value,
4044 shared_ptr<_Tp>
4045>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046allocate_shared(const _Alloc& __a, _Args&& ...__args)
4047{
zoecarver505730a2020-02-25 16:50:57 -08004048 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4049
4050 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4051 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4052 typedef __allocator_destructor<_A2> _D2;
4053
4054 _A2 __a2(__a);
4055 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4056 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4057 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4058
4059 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4060 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061}
4062
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063template<class _Tp, class _Up>
4064inline _LIBCPP_INLINE_VISIBILITY
4065bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004066operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067{
4068 return __x.get() == __y.get();
4069}
4070
4071template<class _Tp, class _Up>
4072inline _LIBCPP_INLINE_VISIBILITY
4073bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004074operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075{
4076 return !(__x == __y);
4077}
4078
4079template<class _Tp, class _Up>
4080inline _LIBCPP_INLINE_VISIBILITY
4081bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004082operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004084#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004085 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4086 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004087#else
4088 return less<>()(__x.get(), __y.get());
4089#endif
4090
Howard Hinnantb17caf92012-02-21 21:02:58 +00004091}
4092
4093template<class _Tp, class _Up>
4094inline _LIBCPP_INLINE_VISIBILITY
4095bool
4096operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4097{
4098 return __y < __x;
4099}
4100
4101template<class _Tp, class _Up>
4102inline _LIBCPP_INLINE_VISIBILITY
4103bool
4104operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4105{
4106 return !(__y < __x);
4107}
4108
4109template<class _Tp, class _Up>
4110inline _LIBCPP_INLINE_VISIBILITY
4111bool
4112operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4113{
4114 return !(__x < __y);
4115}
4116
4117template<class _Tp>
4118inline _LIBCPP_INLINE_VISIBILITY
4119bool
4120operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4121{
4122 return !__x;
4123}
4124
4125template<class _Tp>
4126inline _LIBCPP_INLINE_VISIBILITY
4127bool
4128operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4129{
4130 return !__x;
4131}
4132
4133template<class _Tp>
4134inline _LIBCPP_INLINE_VISIBILITY
4135bool
4136operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4137{
4138 return static_cast<bool>(__x);
4139}
4140
4141template<class _Tp>
4142inline _LIBCPP_INLINE_VISIBILITY
4143bool
4144operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4145{
4146 return static_cast<bool>(__x);
4147}
4148
4149template<class _Tp>
4150inline _LIBCPP_INLINE_VISIBILITY
4151bool
4152operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4153{
4154 return less<_Tp*>()(__x.get(), nullptr);
4155}
4156
4157template<class _Tp>
4158inline _LIBCPP_INLINE_VISIBILITY
4159bool
4160operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4161{
4162 return less<_Tp*>()(nullptr, __x.get());
4163}
4164
4165template<class _Tp>
4166inline _LIBCPP_INLINE_VISIBILITY
4167bool
4168operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4169{
4170 return nullptr < __x;
4171}
4172
4173template<class _Tp>
4174inline _LIBCPP_INLINE_VISIBILITY
4175bool
4176operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4177{
4178 return __x < nullptr;
4179}
4180
4181template<class _Tp>
4182inline _LIBCPP_INLINE_VISIBILITY
4183bool
4184operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4185{
4186 return !(nullptr < __x);
4187}
4188
4189template<class _Tp>
4190inline _LIBCPP_INLINE_VISIBILITY
4191bool
4192operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4193{
4194 return !(__x < nullptr);
4195}
4196
4197template<class _Tp>
4198inline _LIBCPP_INLINE_VISIBILITY
4199bool
4200operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4201{
4202 return !(__x < nullptr);
4203}
4204
4205template<class _Tp>
4206inline _LIBCPP_INLINE_VISIBILITY
4207bool
4208operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4209{
4210 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211}
4212
4213template<class _Tp>
4214inline _LIBCPP_INLINE_VISIBILITY
4215void
Howard Hinnant719bda32011-05-28 14:41:13 +00004216swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217{
4218 __x.swap(__y);
4219}
4220
4221template<class _Tp, class _Up>
4222inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004223shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004224static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225{
zoecarverd73f42a2020-05-11 18:42:50 -07004226 return shared_ptr<_Tp>(__r,
4227 static_cast<
4228 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229}
4230
4231template<class _Tp, class _Up>
4232inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004233shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004234dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235{
zoecarverd73f42a2020-05-11 18:42:50 -07004236 typedef typename shared_ptr<_Tp>::element_type _ET;
4237 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4239}
4240
4241template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004242shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004243const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244{
zoecarverd73f42a2020-05-11 18:42:50 -07004245 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004246 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247}
4248
zoecarverd73f42a2020-05-11 18:42:50 -07004249template<class _Tp, class _Up>
4250shared_ptr<_Tp>
4251reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4252{
4253 return shared_ptr<_Tp>(__r,
4254 reinterpret_cast<
4255 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4256}
4257
Howard Hinnant72f73582010-08-11 17:04:31 +00004258#ifndef _LIBCPP_NO_RTTI
4259
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260template<class _Dp, class _Tp>
4261inline _LIBCPP_INLINE_VISIBILITY
4262_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004263get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264{
4265 return __p.template __get_deleter<_Dp>();
4266}
4267
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004268#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004269
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04004271class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004273public:
4274 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275private:
4276 element_type* __ptr_;
4277 __shared_weak_count* __cntrl_;
4278
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004279public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004281 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004282 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004283 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4284 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004286 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004287 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004288 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4289 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004290
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004292 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004293 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004294 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4295 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004296 ~weak_ptr();
4297
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004299 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004300 template<class _Yp>
4301 typename enable_if
4302 <
4303 is_convertible<_Yp*, element_type*>::value,
4304 weak_ptr&
4305 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004307 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4308
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004310 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4311 template<class _Yp>
4312 typename enable_if
4313 <
4314 is_convertible<_Yp*, element_type*>::value,
4315 weak_ptr&
4316 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004318 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4319
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004320 template<class _Yp>
4321 typename enable_if
4322 <
4323 is_convertible<_Yp*, element_type*>::value,
4324 weak_ptr&
4325 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004327 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004328
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004330 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004332 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004333
Howard Hinnant756c69b2010-09-22 16:48:34 +00004334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004335 long use_count() const _NOEXCEPT
4336 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004338 bool expired() const _NOEXCEPT
4339 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4340 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004341 template<class _Up>
4342 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004343 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004345 template<class _Up>
4346 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004347 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348 {return __cntrl_ < __r.__cntrl_;}
4349
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004350 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4351 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352};
4353
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004354#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4355template<class _Tp>
4356weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4357#endif
4358
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004360inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004361_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004362weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363 : __ptr_(0),
4364 __cntrl_(0)
4365{
4366}
4367
4368template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004369inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004370weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371 : __ptr_(__r.__ptr_),
4372 __cntrl_(__r.__cntrl_)
4373{
4374 if (__cntrl_)
4375 __cntrl_->__add_weak();
4376}
4377
4378template<class _Tp>
4379template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004380inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004381weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004382 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004383 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384 : __ptr_(__r.__ptr_),
4385 __cntrl_(__r.__cntrl_)
4386{
4387 if (__cntrl_)
4388 __cntrl_->__add_weak();
4389}
4390
4391template<class _Tp>
4392template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004393inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004395 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004396 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004397 : __ptr_(__r.__ptr_),
4398 __cntrl_(__r.__cntrl_)
4399{
4400 if (__cntrl_)
4401 __cntrl_->__add_weak();
4402}
4403
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004404template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004405inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004406weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4407 : __ptr_(__r.__ptr_),
4408 __cntrl_(__r.__cntrl_)
4409{
4410 __r.__ptr_ = 0;
4411 __r.__cntrl_ = 0;
4412}
4413
4414template<class _Tp>
4415template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004416inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004417weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4418 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4419 _NOEXCEPT
4420 : __ptr_(__r.__ptr_),
4421 __cntrl_(__r.__cntrl_)
4422{
4423 __r.__ptr_ = 0;
4424 __r.__cntrl_ = 0;
4425}
4426
Howard Hinnantc51e1022010-05-11 19:42:16 +00004427template<class _Tp>
4428weak_ptr<_Tp>::~weak_ptr()
4429{
4430 if (__cntrl_)
4431 __cntrl_->__release_weak();
4432}
4433
4434template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004435inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004437weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438{
4439 weak_ptr(__r).swap(*this);
4440 return *this;
4441}
4442
4443template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004444template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004445inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004446typename enable_if
4447<
4448 is_convertible<_Yp*, _Tp*>::value,
4449 weak_ptr<_Tp>&
4450>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004451weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452{
4453 weak_ptr(__r).swap(*this);
4454 return *this;
4455}
4456
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004457template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004458inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004459weak_ptr<_Tp>&
4460weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4461{
4462 weak_ptr(_VSTD::move(__r)).swap(*this);
4463 return *this;
4464}
4465
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004467template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004468inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004469typename enable_if
4470<
4471 is_convertible<_Yp*, _Tp*>::value,
4472 weak_ptr<_Tp>&
4473>::type
4474weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4475{
4476 weak_ptr(_VSTD::move(__r)).swap(*this);
4477 return *this;
4478}
4479
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004480template<class _Tp>
4481template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004482inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004483typename enable_if
4484<
4485 is_convertible<_Yp*, _Tp*>::value,
4486 weak_ptr<_Tp>&
4487>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004488weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004489{
4490 weak_ptr(__r).swap(*this);
4491 return *this;
4492}
4493
4494template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004495inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496void
Howard Hinnant719bda32011-05-28 14:41:13 +00004497weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004499 _VSTD::swap(__ptr_, __r.__ptr_);
4500 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004501}
4502
4503template<class _Tp>
4504inline _LIBCPP_INLINE_VISIBILITY
4505void
Howard Hinnant719bda32011-05-28 14:41:13 +00004506swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507{
4508 __x.swap(__y);
4509}
4510
4511template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004512inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004513void
Howard Hinnant719bda32011-05-28 14:41:13 +00004514weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004515{
4516 weak_ptr().swap(*this);
4517}
4518
4519template<class _Tp>
4520template<class _Yp>
4521shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004522 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523 : __ptr_(__r.__ptr_),
4524 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4525{
4526 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004527 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528}
4529
4530template<class _Tp>
4531shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004532weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533{
4534 shared_ptr<_Tp> __r;
4535 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4536 if (__r.__cntrl_)
4537 __r.__ptr_ = __ptr_;
4538 return __r;
4539}
4540
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004541#if _LIBCPP_STD_VER > 14
4542template <class _Tp = void> struct owner_less;
4543#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004544template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004545#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004546
4547template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004548struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004549 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004550{
4551 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004552 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004553 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004555 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004556 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004558 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004559 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004560 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004561};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004562
4563template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004564struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4566{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004567 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004568 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004569 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004570 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004571 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004572 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004574 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004575 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004576 {return __x.owner_before(__y);}
4577};
4578
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004579#if _LIBCPP_STD_VER > 14
4580template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004581struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004582{
4583 template <class _Tp, class _Up>
4584 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004585 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004586 {return __x.owner_before(__y);}
4587 template <class _Tp, class _Up>
4588 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004589 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004590 {return __x.owner_before(__y);}
4591 template <class _Tp, class _Up>
4592 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004593 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004594 {return __x.owner_before(__y);}
4595 template <class _Tp, class _Up>
4596 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004597 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004598 {return __x.owner_before(__y);}
4599 typedef void is_transparent;
4600};
4601#endif
4602
Howard Hinnantc51e1022010-05-11 19:42:16 +00004603template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004604class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004605{
4606 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004607protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004608 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004609 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004611 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004613 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4614 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004616 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004617public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004619 shared_ptr<_Tp> shared_from_this()
4620 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004622 shared_ptr<_Tp const> shared_from_this() const
4623 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004624
Eric Fiselier84006862016-06-02 00:15:35 +00004625#if _LIBCPP_STD_VER > 14
4626 _LIBCPP_INLINE_VISIBILITY
4627 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4628 { return __weak_this_; }
4629
4630 _LIBCPP_INLINE_VISIBILITY
4631 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4632 { return __weak_this_; }
4633#endif // _LIBCPP_STD_VER > 14
4634
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635 template <class _Up> friend class shared_ptr;
4636};
4637
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004638template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004639struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004640{
4641 typedef shared_ptr<_Tp> argument_type;
4642 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00004643
Howard Hinnant756c69b2010-09-22 16:48:34 +00004644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004645 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004646 {
zoecarverd73f42a2020-05-11 18:42:50 -07004647 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004648 }
4649};
4650
Howard Hinnantc834c512011-11-29 18:15:50 +00004651template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00004652inline _LIBCPP_INLINE_VISIBILITY
4653basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00004654operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00004655
Eric Fiselier9b492672016-06-18 02:12:53 +00004656
4657#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004658
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004659class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00004660{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004661 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004662public:
4663 void lock() _NOEXCEPT;
4664 void unlock() _NOEXCEPT;
4665
4666private:
4667 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4668 __sp_mut(const __sp_mut&);
4669 __sp_mut& operator=(const __sp_mut&);
4670
Howard Hinnant8331b762013-03-06 23:30:19 +00004671 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004672};
4673
Mehdi Amini228053d2017-05-04 17:08:54 +00004674_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4675__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004676
4677template <class _Tp>
4678inline _LIBCPP_INLINE_VISIBILITY
4679bool
4680atomic_is_lock_free(const shared_ptr<_Tp>*)
4681{
4682 return false;
4683}
4684
4685template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004686_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004687shared_ptr<_Tp>
4688atomic_load(const shared_ptr<_Tp>* __p)
4689{
4690 __sp_mut& __m = __get_sp_mut(__p);
4691 __m.lock();
4692 shared_ptr<_Tp> __q = *__p;
4693 __m.unlock();
4694 return __q;
4695}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004696
Howard Hinnant9fa30202012-07-30 01:40:57 +00004697template <class _Tp>
4698inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004699_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004700shared_ptr<_Tp>
4701atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4702{
4703 return atomic_load(__p);
4704}
4705
4706template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004707_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004708void
4709atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4710{
4711 __sp_mut& __m = __get_sp_mut(__p);
4712 __m.lock();
4713 __p->swap(__r);
4714 __m.unlock();
4715}
4716
4717template <class _Tp>
4718inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004719_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004720void
4721atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4722{
4723 atomic_store(__p, __r);
4724}
4725
4726template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004727_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004728shared_ptr<_Tp>
4729atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4730{
4731 __sp_mut& __m = __get_sp_mut(__p);
4732 __m.lock();
4733 __p->swap(__r);
4734 __m.unlock();
4735 return __r;
4736}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004737
Howard Hinnant9fa30202012-07-30 01:40:57 +00004738template <class _Tp>
4739inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004740_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004741shared_ptr<_Tp>
4742atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4743{
4744 return atomic_exchange(__p, __r);
4745}
4746
4747template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004748_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004749bool
4750atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4751{
Marshall Clow4201ee82016-05-18 17:50:13 +00004752 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004753 __sp_mut& __m = __get_sp_mut(__p);
4754 __m.lock();
4755 if (__p->__owner_equivalent(*__v))
4756 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004757 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004758 *__p = __w;
4759 __m.unlock();
4760 return true;
4761 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004762 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004763 *__v = *__p;
4764 __m.unlock();
4765 return false;
4766}
4767
4768template <class _Tp>
4769inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004770_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004771bool
4772atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4773{
4774 return atomic_compare_exchange_strong(__p, __v, __w);
4775}
4776
4777template <class _Tp>
4778inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004779_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004780bool
4781atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4782 shared_ptr<_Tp> __w, memory_order, memory_order)
4783{
4784 return atomic_compare_exchange_strong(__p, __v, __w);
4785}
4786
4787template <class _Tp>
4788inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004789_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004790bool
4791atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4792 shared_ptr<_Tp> __w, memory_order, memory_order)
4793{
4794 return atomic_compare_exchange_weak(__p, __v, __w);
4795}
4796
Eric Fiselier9b492672016-06-18 02:12:53 +00004797#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004798
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004799//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004800#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4801# ifndef _LIBCPP_CXX03_LANG
4802enum class pointer_safety : unsigned char {
4803 relaxed,
4804 preferred,
4805 strict
4806};
4807# endif
4808#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004809struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004810{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004811 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004812 {
4813 relaxed,
4814 preferred,
4815 strict
4816 };
4817
Howard Hinnant49e145e2012-10-30 19:06:59 +00004818 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004819
Howard Hinnant756c69b2010-09-22 16:48:34 +00004820 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004821 pointer_safety() : __v_() {}
4822
4823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004824 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004826 operator int() const {return __v_;}
4827};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004828#endif
4829
4830#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004831 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004832_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4833#else
4834// This function is only offered in C++03 under ABI v1.
4835# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4836inline _LIBCPP_INLINE_VISIBILITY
4837pointer_safety get_pointer_safety() _NOEXCEPT {
4838 return pointer_safety::relaxed;
4839}
4840# endif
4841#endif
4842
Howard Hinnantc51e1022010-05-11 19:42:16 +00004843
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004844_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4845_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4846_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004847_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004848
4849template <class _Tp>
4850inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004851_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004852undeclare_reachable(_Tp* __p)
4853{
4854 return static_cast<_Tp*>(__undeclare_reachable(__p));
4855}
4856
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004857_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004858
Marshall Clow8982dcd2015-07-13 20:04:56 +00004859// --- Helper for container swap --
4860template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004861inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004862void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4863#if _LIBCPP_STD_VER >= 14
4864 _NOEXCEPT
4865#else
4866 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4867#endif
4868{
Aditya Kumar7c5db692017-08-20 10:38:55 +00004869 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00004870 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4871}
4872
4873template <typename _Alloc>
4874_LIBCPP_INLINE_VISIBILITY
4875void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4876#if _LIBCPP_STD_VER >= 14
4877 _NOEXCEPT
4878#else
4879 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4880#endif
4881{
4882 using _VSTD::swap;
4883 swap(__a1, __a2);
4884}
4885
4886template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004887inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004888void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4889
Marshall Clowff91de82015-08-18 19:51:37 +00004890template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004891struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004892 _Traits::propagate_on_container_move_assignment::value
4893#if _LIBCPP_STD_VER > 14
4894 || _Traits::is_always_equal::value
4895#else
4896 && is_nothrow_move_assignable<_Alloc>::value
4897#endif
4898 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004899
Marshall Clowa591b9a2016-07-11 21:38:08 +00004900
Marshall Clowa591b9a2016-07-11 21:38:08 +00004901template <class _Tp, class _Alloc>
4902struct __temp_value {
4903 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004904
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004905 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004906 _Alloc &__a;
4907
4908 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4909 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004910
Marshall Clowa591b9a2016-07-11 21:38:08 +00004911 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004912 _LIBCPP_NO_CFI
4913 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4914 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4915 _VSTD::forward<_Args>(__args)...);
4916 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004917
Marshall Clowa591b9a2016-07-11 21:38:08 +00004918 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4919 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004920
Marshall Clowe46031a2018-07-02 18:41:15 +00004921template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004922struct __is_allocator : false_type {};
4923
4924template<typename _Alloc>
4925struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004926 typename __void_t<typename _Alloc::value_type>::type,
4927 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4928 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004929 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004930
Eric Fiselier74ebee62019-06-08 01:31:19 +00004931// __builtin_new_allocator -- A non-templated helper for allocating and
4932// deallocating memory using __builtin_operator_new and
4933// __builtin_operator_delete. It should be used in preference to
4934// `std::allocator<T>` to avoid additional instantiations.
4935struct __builtin_new_allocator {
4936 struct __builtin_new_deleter {
4937 typedef void* pointer_type;
4938
4939 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4940 : __size_(__size), __align_(__align) {}
4941
4942 void operator()(void* p) const _NOEXCEPT {
4943 std::__libcpp_deallocate(p, __size_, __align_);
4944 }
4945
4946 private:
4947 size_t __size_;
4948 size_t __align_;
4949 };
4950
4951 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4952
4953 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
4954 return __holder_t(std::__libcpp_allocate(__s, __align),
4955 __builtin_new_deleter(__s, __align));
4956 }
4957
4958 static void __deallocate_bytes(void* __p, size_t __s,
4959 size_t __align) _NOEXCEPT {
4960 std::__libcpp_deallocate(__p, __s, __align);
4961 }
4962
4963 template <class _Tp>
4964 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4965 static __holder_t __allocate_type(size_t __n) {
4966 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4967 }
4968
4969 template <class _Tp>
4970 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4971 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4972 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4973 }
4974};
4975
4976
Howard Hinnantc51e1022010-05-11 19:42:16 +00004977_LIBCPP_END_NAMESPACE_STD
4978
Eric Fiselierf4433a32017-05-31 22:07:49 +00004979_LIBCPP_POP_MACROS
4980
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004981#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004982# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004983#endif
4984
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985#endif // _LIBCPP_MEMORY