blob: d326476ca1fefd1ba624ca068f8f89761dc8c2cf [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
Marek Kurdej1f0cde92020-12-06 15:23:46 +010049template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
Eric Fiselier45c9aac2017-11-22 19:49:21 +000050
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
Louis Dionne99f59432020-09-17 12:06:13 -040086 static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Louis Dionne99f59432020-09-17 12:06:13 -040089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
Louis Dionne99f59432020-09-17 12:06:13 -040092 static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000093
94 template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -040095 static void destroy(allocator_type& a, T* p); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
Louis Dionne99f59432020-09-17 12:06:13 -040097 static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
98 static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000099};
100
101template <>
Michael Park99f9e912020-03-04 11:27:14 -0500102class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
Louis Dionnec0056af2020-08-28 12:31:16 -0400116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef T* pointer; // deprecated in C++17, removed in C++20
119 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
120 typedef typename add_lvalue_reference<T>::type
121 reference; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<const T>::type
123 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
Michael Park99f9e912020-03-04 11:27:14 -0500125 typedef T value_type;
126
127 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
128
129 typedef true_type propagate_on_container_move_assignment;
130 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131
Marshall Clowbc759762018-03-20 23:02:53 +0000132 constexpr allocator() noexcept; // constexpr in C++20
133 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
134 template <class U>
135 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400136 ~allocator(); // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500137 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
138 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
139 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400140 T* allocate(size_t n); // constexpr in C++20
141 void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500142 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000143 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500144 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147};
148
149template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400150bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151
152template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400153bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
155template <class OutputIterator, class T>
156class raw_storage_iterator
157 : public iterator<output_iterator_tag,
158 T, // purposefully not C++03
159 ptrdiff_t, // purposefully not C++03
160 T*, // purposefully not C++03
161 raw_storage_iterator&> // purposefully not C++03
162{
163public:
164 explicit raw_storage_iterator(OutputIterator x);
165 raw_storage_iterator& operator*();
166 raw_storage_iterator& operator=(const T& element);
167 raw_storage_iterator& operator++();
168 raw_storage_iterator operator++(int);
169};
170
Howard Hinnant719bda32011-05-28 14:41:13 +0000171template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
172template <class T> void return_temporary_buffer(T* p) noexcept;
173
174template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000175template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176
177template <class InputIterator, class ForwardIterator>
178ForwardIterator
179uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
180
Howard Hinnant719bda32011-05-28 14:41:13 +0000181template <class InputIterator, class Size, class ForwardIterator>
182ForwardIterator
183uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
184
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185template <class ForwardIterator, class T>
186void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
187
188template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000189ForwardIterator
190uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Louis Dionne99f59432020-09-17 12:06:13 -0400192template <class T, class ...Args>
193constexpr T* construct_at(T* location, Args&& ...args); // since C++20
194
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000195template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -0400196void destroy_at(T* location); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000197
198template <class ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -0400199void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000200
201template <class ForwardIterator, class Size>
Louis Dionne99f59432020-09-17 12:06:13 -0400202ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000203
204template <class InputIterator, class ForwardIterator>
205 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
206
207template <class InputIterator, class Size, class ForwardIterator>
208 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
209
210template <class ForwardIterator>
211 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
212
213template <class ForwardIterator, class Size>
214 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
215
216template <class ForwardIterator>
217 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
218
219template <class ForwardIterator, class Size>
220 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
221
Louis Dionne481a2662018-09-23 18:35:00 +0000222template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223
224template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000225class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226{
227public:
228 typedef X element_type;
229
230 explicit auto_ptr(X* p =0) throw();
231 auto_ptr(auto_ptr&) throw();
232 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
233 auto_ptr& operator=(auto_ptr&) throw();
234 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
235 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
236 ~auto_ptr() throw();
237
238 typename add_lvalue_reference<X>::type operator*() const throw();
239 X* operator->() const throw();
240 X* get() const throw();
241 X* release() throw();
242 void reset(X* p =0) throw();
243
244 auto_ptr(auto_ptr_ref<X>) throw();
245 template<class Y> operator auto_ptr_ref<Y>() throw();
246 template<class Y> operator auto_ptr<Y>() throw();
247};
248
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000249template <class T>
250struct default_delete
251{
Howard Hinnant719bda32011-05-28 14:41:13 +0000252 constexpr default_delete() noexcept = default;
253 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000254
Howard Hinnant719bda32011-05-28 14:41:13 +0000255 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000256};
257
258template <class T>
259struct default_delete<T[]>
260{
Howard Hinnant719bda32011-05-28 14:41:13 +0000261 constexpr default_delete() noexcept = default;
262 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000263 template <class U> void operator()(U*) const = delete;
264};
265
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000266template <class T, class D = default_delete<T>>
267class unique_ptr
268{
269public:
270 typedef see below pointer;
271 typedef T element_type;
272 typedef D deleter_type;
273
274 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000275 constexpr unique_ptr() noexcept;
276 explicit unique_ptr(pointer p) noexcept;
277 unique_ptr(pointer p, see below d1) noexcept;
278 unique_ptr(pointer p, see below d2) noexcept;
279 unique_ptr(unique_ptr&& u) noexcept;
280 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000281 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000283 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000284 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // destructor
287 ~unique_ptr();
288
289 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000290 unique_ptr& operator=(unique_ptr&& u) noexcept;
291 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
292 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // observers
295 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000296 pointer operator->() const noexcept;
297 pointer get() const noexcept;
298 deleter_type& get_deleter() noexcept;
299 const deleter_type& get_deleter() const noexcept;
300 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000301
302 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000303 pointer release() noexcept;
304 void reset(pointer p = pointer()) noexcept;
305 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000306};
307
308template <class T, class D>
309class unique_ptr<T[], D>
310{
311public:
312 typedef implementation-defined pointer;
313 typedef T element_type;
314 typedef D deleter_type;
315
316 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 constexpr unique_ptr() noexcept;
318 explicit unique_ptr(pointer p) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(pointer p, see below d) noexcept;
321 unique_ptr(unique_ptr&& u) noexcept;
322 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000323
324 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000325 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 unique_ptr& operator=(unique_ptr&& u) noexcept;
329 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000330
331 // observers
332 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000333 pointer get() const noexcept;
334 deleter_type& get_deleter() noexcept;
335 const deleter_type& get_deleter() const noexcept;
336 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 pointer release() noexcept;
340 void reset(pointer p = pointer()) noexcept;
341 void reset(nullptr_t) noexcept;
Vy Nguyene369bd92020-07-13 12:34:37 -0400342 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000343 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000344};
345
346template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000347 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000348
349template <class T1, class D1, class T2, class D2>
350 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353template <class T1, class D1, class T2, class D2>
354 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
355template <class T1, class D1, class T2, class D2>
356 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
357template <class T1, class D1, class T2, class D2>
358 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
359template <class T1, class D1, class T2, class D2>
360 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
361
Howard Hinnant719bda32011-05-28 14:41:13 +0000362template <class T, class D>
363 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
364template <class T, class D>
365 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
366template <class T, class D>
367 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
368template <class T, class D>
369 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
370
371template <class T, class D>
372 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
379template <class T, class D>
380 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
381template <class T, class D>
382 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
383template <class T, class D>
384 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
385template <class T, class D>
386 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
387
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000388class bad_weak_ptr
389 : public std::exception
390{
Howard Hinnant719bda32011-05-28 14:41:13 +0000391 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000392};
393
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000394template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
395template<class T> unique_ptr<T> make_unique(size_t n); // C++14
396template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
397
Marshall Clowe52a3242017-11-27 15:51:36 +0000398template<class E, class T, class Y, class D>
399 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
400
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000401template<class T>
402class shared_ptr
403{
404public:
405 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000406 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000407
408 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000409 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000410 template<class Y> explicit shared_ptr(Y* p);
411 template<class Y, class D> shared_ptr(Y* p, D d);
412 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
413 template <class D> shared_ptr(nullptr_t p, D d);
414 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
416 shared_ptr(const shared_ptr& r) noexcept;
417 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
418 shared_ptr(shared_ptr&& r) noexcept;
419 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000421 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000422 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
423 shared_ptr(nullptr_t) : shared_ptr() { }
424
425 // destructor:
426 ~shared_ptr();
427
428 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 shared_ptr& operator=(const shared_ptr& r) noexcept;
430 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
431 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000432 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000433 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000434 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
435
436 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000437 void swap(shared_ptr& r) noexcept;
438 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000439 template<class Y> void reset(Y* p);
440 template<class Y, class D> void reset(Y* p, D d);
441 template<class Y, class D, class A> void reset(Y* p, D d, A a);
442
Howard Hinnant719bda32011-05-28 14:41:13 +0000443 // observers:
444 T* get() const noexcept;
445 T& operator*() const noexcept;
446 T* operator->() const noexcept;
447 long use_count() const noexcept;
448 bool unique() const noexcept;
449 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000450 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
451 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000452};
453
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400454template<class T>
455shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
456template<class T, class D>
457shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
458
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000459// shared_ptr comparisons:
460template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000461 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000462template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000463 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000464template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000465 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000466template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000467 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000468template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000469 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000470template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000471 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
472
473template <class T>
474 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
481template <class T>
482 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
483template <class T>
484bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
485template <class T>
486 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
487template <class T>
488 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
489template <class T>
490 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
491template <class T>
492 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
493template <class T>
494 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
495template <class T>
496 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000497
498// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000499template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500
501// shared_ptr casts:
502template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000503 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000504template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000505 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000506template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000507 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000508
509// shared_ptr I/O:
510template<class E, class T, class Y>
511 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
512
513// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000514template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516template<class T, class... Args>
517 shared_ptr<T> make_shared(Args&&... args);
518template<class T, class A, class... Args>
519 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
520
521template<class T>
522class weak_ptr
523{
524public:
525 typedef T element_type;
526
527 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000528 constexpr weak_ptr() noexcept;
529 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
530 weak_ptr(weak_ptr const& r) noexcept;
531 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000532 weak_ptr(weak_ptr&& r) noexcept; // C++14
533 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000534
535 // destructor
536 ~weak_ptr();
537
538 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000539 weak_ptr& operator=(weak_ptr const& r) noexcept;
540 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
541 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000542 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
543 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000544
545 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000546 void swap(weak_ptr& r) noexcept;
547 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000548
549 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000550 long use_count() const noexcept;
551 bool expired() const noexcept;
552 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000553 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
554 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000555};
556
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400557template<class T>
558weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
559
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000560// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000561template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000562
563// class owner_less:
564template<class T> struct owner_less;
565
566template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000567struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000568 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
569{
570 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000571 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
572 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000574};
575
576template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000577struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000578 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
579{
580 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000581 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
583 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
584};
585
586template <> // Added in C++14
587struct owner_less<void>
588{
589 template <class _Tp, class _Up>
590 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
591 template <class _Tp, class _Up>
592 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
593 template <class _Tp, class _Up>
594 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
595 template <class _Tp, class _Up>
596 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
597
598 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599};
600
601template<class T>
602class enable_shared_from_this
603{
604protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000605 constexpr enable_shared_from_this() noexcept;
606 enable_shared_from_this(enable_shared_from_this const&) noexcept;
607 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000608 ~enable_shared_from_this();
609public:
610 shared_ptr<T> shared_from_this();
611 shared_ptr<T const> shared_from_this() const;
612};
613
614template<class T>
615 bool atomic_is_lock_free(const shared_ptr<T>* p);
616template<class T>
617 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
618template<class T>
619 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
620template<class T>
621 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
622template<class T>
623 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
624template<class T>
625 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
626template<class T>
627 shared_ptr<T>
628 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
629template<class T>
630 bool
631 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
632template<class T>
633 bool
634 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
635template<class T>
636 bool
637 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
638 shared_ptr<T> w, memory_order success,
639 memory_order failure);
640template<class T>
641 bool
642 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
643 shared_ptr<T> w, memory_order success,
644 memory_order failure);
645// Hash support
646template <class T> struct hash;
647template <class T, class D> struct hash<unique_ptr<T, D> >;
648template <class T> struct hash<shared_ptr<T> >;
649
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000650template <class T, class Alloc>
651 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
652
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000653// Pointer safety
654enum class pointer_safety { relaxed, preferred, strict };
655void declare_reachable(void *p);
656template <class T> T *undeclare_reachable(T *p);
657void declare_no_pointers(char *p, size_t n);
658void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000659pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000660
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
662
663} // std
664
665*/
666
667#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500668#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669#include <type_traits>
670#include <typeinfo>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400671#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672#include <cstddef>
673#include <cstdint>
674#include <new>
675#include <utility>
676#include <limits>
677#include <iterator>
678#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000679#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000680#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000681#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000682#include <cstring>
Louis Dionnefa621d72020-12-10 18:28:13 -0500683#include <__memory/allocator_traits.h>
684#include <__memory/base.h>
685#include <__memory/pointer_traits.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500686#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000687#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000688# include <atomic>
689#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000690#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000691
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000692#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000694#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695
Eric Fiselierf4433a32017-05-31 22:07:49 +0000696_LIBCPP_PUSH_MACROS
697#include <__undef_macros>
698
699
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700_LIBCPP_BEGIN_NAMESPACE_STD
701
Eric Fiselier89659d12015-07-07 00:27:16 +0000702template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000703inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000704_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
705#if !defined(_LIBCPP_HAS_NO_THREADS) && \
706 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000707 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000708 return __atomic_load_n(__value, __ATOMIC_RELAXED);
709#else
710 return *__value;
711#endif
712}
713
Kuba Breckade9d6792016-09-04 09:55:12 +0000714template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000715inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000716_ValueType __libcpp_acquire_load(_ValueType const* __value) {
717#if !defined(_LIBCPP_HAS_NO_THREADS) && \
718 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000719 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000720 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
721#else
722 return *__value;
723#endif
724}
725
Louis Dionnefa621d72020-12-10 18:28:13 -0500726template <class _Tp> class allocator;
Marshall Clow0be70c32017-06-14 21:23:57 +0000727
Louis Dionnefa621d72020-12-10 18:28:13 -0500728#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
729template <>
730class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732public:
Louis Dionnefa621d72020-12-10 18:28:13 -0500733 typedef void* pointer;
734 typedef const void* const_pointer;
735 typedef void value_type;
736
737 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738};
739
Louis Dionnefa621d72020-12-10 18:28:13 -0500740template <>
741class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742{
Louis Dionnefa621d72020-12-10 18:28:13 -0500743public:
744 typedef const void* pointer;
745 typedef const void* const_pointer;
746 typedef const void value_type;
747
748 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749};
Louis Dionne99f59432020-09-17 12:06:13 -0400750#endif
Marshall Clow940e01c2015-04-07 05:21:38 +0000751
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752// allocator
753
754template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000755class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756{
757public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400758 typedef size_t size_type;
759 typedef ptrdiff_t difference_type;
760 typedef _Tp value_type;
761 typedef true_type propagate_on_container_move_assignment;
762 typedef true_type is_always_equal;
763
764 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
765 allocator() _NOEXCEPT { }
766
767 template <class _Up>
768 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
769 allocator(const allocator<_Up>&) _NOEXCEPT { }
770
771 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
772 _Tp* allocate(size_t __n) {
773 if (__n > allocator_traits<allocator>::max_size(*this))
774 __throw_length_error("allocator<T>::allocate(size_t n)"
775 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400776 if (__libcpp_is_constant_evaluated()) {
777 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
778 } else {
779 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
780 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400781 }
782
783 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
784 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400785 if (__libcpp_is_constant_evaluated()) {
786 ::operator delete(__p);
787 } else {
788 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
789 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400790 }
791
792 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500793#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -0500794 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
795 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
796 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
797 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
798
Louis Dionne481a2662018-09-23 18:35:00 +0000799 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400800 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
801 typedef allocator<_Up> other;
802 };
Marshall Clowbc759762018-03-20 23:02:53 +0000803
Michael Park99f9e912020-03-04 11:27:14 -0500804 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400805 pointer address(reference __x) const _NOEXCEPT {
806 return _VSTD::addressof(__x);
807 }
Michael Park99f9e912020-03-04 11:27:14 -0500808 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400809 const_pointer address(const_reference __x) const _NOEXCEPT {
810 return _VSTD::addressof(__x);
811 }
Michael Park99f9e912020-03-04 11:27:14 -0500812
Michael Park99f9e912020-03-04 11:27:14 -0500813 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400814 _Tp* allocate(size_t __n, const void*) {
815 return allocate(__n);
816 }
Michael Park99f9e912020-03-04 11:27:14 -0500817
Louis Dionnef8b6c022020-09-21 10:36:37 -0400818 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
819 return size_type(~0) / sizeof(_Tp);
820 }
Michael Park99f9e912020-03-04 11:27:14 -0500821
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400823 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
824 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500825 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400826 }
827
828 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
829 void destroy(pointer __p) {
830 __p->~_Tp();
831 }
Michael Park99f9e912020-03-04 11:27:14 -0500832#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833};
834
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000835template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000836class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000837{
838public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400839 typedef size_t size_type;
840 typedef ptrdiff_t difference_type;
841 typedef const _Tp value_type;
842 typedef true_type propagate_on_container_move_assignment;
843 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000844
Marshall Clowbc759762018-03-20 23:02:53 +0000845 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400846 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +0000847
848 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +0000849 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400850 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -0500851
Louis Dionne99f59432020-09-17 12:06:13 -0400852 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400853 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -0400854 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +0000855 __throw_length_error("allocator<const T>::allocate(size_t n)"
856 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400857 if (__libcpp_is_constant_evaluated()) {
858 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
859 } else {
860 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
861 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000862 }
Michael Park99f9e912020-03-04 11:27:14 -0500863
Louis Dionne99f59432020-09-17 12:06:13 -0400864 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400865 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400866 if (__libcpp_is_constant_evaluated()) {
867 ::operator delete(const_cast<_Tp*>(__p));
868 } else {
869 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
870 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400871 }
Michael Park99f9e912020-03-04 11:27:14 -0500872
Louis Dionnef8b6c022020-09-21 10:36:37 -0400873 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500874#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -0400875 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
876 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
877 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
878 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
879
880 template <class _Up>
881 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
882 typedef allocator<_Up> other;
883 };
884
885 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
886 const_pointer address(const_reference __x) const _NOEXCEPT {
887 return _VSTD::addressof(__x);
888 }
889
890 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
891 const _Tp* allocate(size_t __n, const void*) {
892 return allocate(__n);
893 }
894
895 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
896 return size_type(~0) / sizeof(_Tp);
897 }
Michael Park99f9e912020-03-04 11:27:14 -0500898
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000899 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400900 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
901 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500902 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400903 }
Howard Hinnant9e028f12012-05-01 15:37:54 +0000904
Louis Dionnef8b6c022020-09-21 10:36:37 -0400905 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
906 void destroy(pointer __p) {
907 __p->~_Tp();
908 }
Michael Park99f9e912020-03-04 11:27:14 -0500909#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000910};
911
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400913inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000914bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915
916template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400917inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000918bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919
Louis Dionned6651542020-11-03 12:05:55 -0500920template <class _Alloc, class _Ptr>
921_LIBCPP_INLINE_VISIBILITY
922void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
923 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500924 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500925 typedef allocator_traits<_Alloc> _Traits;
926 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
927 _Traits::construct(__a, _VSTD::__to_address(__begin2),
928#ifdef _LIBCPP_NO_EXCEPTIONS
929 _VSTD::move(*__begin1)
930#else
931 _VSTD::move_if_noexcept(*__begin1)
932#endif
933 );
934 }
935}
936
937template <class _Alloc, class _Tp, typename enable_if<
938 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
939 is_trivially_move_constructible<_Tp>::value
940>::type>
941_LIBCPP_INLINE_VISIBILITY
942void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
943 ptrdiff_t _Np = __end1 - __begin1;
944 if (_Np > 0) {
945 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
946 __begin2 += _Np;
947 }
948}
949
950template <class _Alloc, class _Iter, class _Ptr>
951_LIBCPP_INLINE_VISIBILITY
952void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
953 typedef allocator_traits<_Alloc> _Traits;
954 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
955 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
956 }
957}
958
959template <class _Alloc, class _Source, class _Dest,
960 class _RawSource = typename remove_const<_Source>::type,
961 class _RawDest = typename remove_const<_Dest>::type,
962 class =
963 typename enable_if<
964 is_trivially_copy_constructible<_Dest>::value &&
965 is_same<_RawSource, _RawDest>::value &&
966 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
967 >::type>
968_LIBCPP_INLINE_VISIBILITY
969void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
970 ptrdiff_t _Np = __end1 - __begin1;
971 if (_Np > 0) {
972 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
973 __begin2 += _Np;
974 }
975}
976
977template <class _Alloc, class _Ptr>
978_LIBCPP_INLINE_VISIBILITY
979void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
980 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
981 "The specified type does not meet the requirements of Cpp17MoveInsertable");
982 typedef allocator_traits<_Alloc> _Traits;
983 while (__end1 != __begin1) {
984 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
985#ifdef _LIBCPP_NO_EXCEPTIONS
986 _VSTD::move(*--__end1)
987#else
988 _VSTD::move_if_noexcept(*--__end1)
989#endif
990 );
991 --__end2;
992 }
993}
994
995template <class _Alloc, class _Tp, class = typename enable_if<
996 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
997 is_trivially_move_constructible<_Tp>::value
998>::type>
999_LIBCPP_INLINE_VISIBILITY
1000void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1001 ptrdiff_t _Np = __end1 - __begin1;
1002 __end2 -= _Np;
1003 if (_Np > 0)
1004 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1005}
1006
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001008class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009 : public iterator<output_iterator_tag,
1010 _Tp, // purposefully not C++03
1011 ptrdiff_t, // purposefully not C++03
1012 _Tp*, // purposefully not C++03
1013 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1014{
1015private:
1016 _OutputIterator __x_;
1017public:
1018 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1019 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1020 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001021 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001022#if _LIBCPP_STD_VER >= 14
1023 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001024 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001025#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1027 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1028 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001029#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001030 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001031#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032};
1033
1034template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001035_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001037get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038{
1039 pair<_Tp*, ptrdiff_t> __r(0, 0);
1040 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1041 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1042 / sizeof(_Tp);
1043 if (__n > __m)
1044 __n = __m;
1045 while (__n > 0)
1046 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001047#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001048 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001049 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001050 align_val_t __al =
1051 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001052 __r.first = static_cast<_Tp*>(::operator new(
1053 __n * sizeof(_Tp), __al, nothrow));
1054 } else {
1055 __r.first = static_cast<_Tp*>(::operator new(
1056 __n * sizeof(_Tp), nothrow));
1057 }
1058#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001059 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001060 {
1061 // Since aligned operator new is unavailable, return an empty
1062 // buffer rather than one with invalid alignment.
1063 return __r;
1064 }
1065
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001067#endif
1068
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 if (__r.first)
1070 {
1071 __r.second = __n;
1072 break;
1073 }
1074 __n /= 2;
1075 }
1076 return __r;
1077}
1078
1079template <class _Tp>
1080inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001081void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1082{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001083 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001084}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085
Marshall Clowb22274f2017-01-24 22:22:33 +00001086#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001088struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089{
1090 _Tp* __ptr_;
1091};
1092
1093template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001094class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095{
1096private:
1097 _Tp* __ptr_;
1098public:
1099 typedef _Tp element_type;
1100
Dimitry Andric47269ce2020-03-13 19:36:26 +01001101 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1102 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1103 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001105 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001107 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001109 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001111 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112
Dimitry Andric47269ce2020-03-13 19:36:26 +01001113 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001115 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1116 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1117 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 {
1119 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001120 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 return __t;
1122 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001123 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 {
1125 if (__ptr_ != __p)
1126 delete __ptr_;
1127 __ptr_ = __p;
1128 }
1129
Dimitry Andric47269ce2020-03-13 19:36:26 +01001130 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1131 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001133 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 {return auto_ptr<_Up>(release());}
1135};
1136
1137template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001138class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139{
1140public:
1141 typedef void element_type;
1142};
Marshall Clowb22274f2017-01-24 22:22:33 +00001143#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001145// Tag used to default initialize one or both of the pair's elements.
1146struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001147struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001148
Eric Fiselier9d355982017-04-12 23:45:53 +00001149template <class _Tp, int _Idx,
1150 bool _CanBeEmptyBase =
1151 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1152struct __compressed_pair_elem {
1153 typedef _Tp _ParamT;
1154 typedef _Tp& reference;
1155 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001157 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001158 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001159 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1160 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161
Eric Fiselier9d355982017-04-12 23:45:53 +00001162 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001163 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1164 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001165 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001166 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001167 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001168 : __value_(_VSTD::forward<_Up>(__u))
1169 {
1170 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001172
1173#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001174 template <class... _Args, size_t... _Indexes>
1175 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1176 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1177 __tuple_indices<_Indexes...>)
1178 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001179#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001181
Alex Lorenz76132112017-11-09 17:54:49 +00001182 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1183 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001184 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001187 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188};
1189
Eric Fiselier9d355982017-04-12 23:45:53 +00001190template <class _Tp, int _Idx>
1191struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1192 typedef _Tp _ParamT;
1193 typedef _Tp& reference;
1194 typedef const _Tp& const_reference;
1195 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001197 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1198 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1199 __compressed_pair_elem(__default_init_tag) {}
1200 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1201 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202
Eric Fiselier9d355982017-04-12 23:45:53 +00001203 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001204 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1205 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001206 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001207 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001208 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001209 : __value_type(_VSTD::forward<_Up>(__u))
1210 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001212#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001213 template <class... _Args, size_t... _Indexes>
1214 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1215 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1216 __tuple_indices<_Indexes...>)
1217 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001218#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219
Alex Lorenz76132112017-11-09 17:54:49 +00001220 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1221 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001222 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223};
1224
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00001226class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1227 private __compressed_pair_elem<_T2, 1> {
Louis Dionneda463cb2020-12-11 12:22:16 -05001228public:
Eric Fiselier9d355982017-04-12 23:45:53 +00001229 // NOTE: This static assert should never fire because __compressed_pair
1230 // is *almost never* used in a scenario where it's possible for T1 == T2.
1231 // (The exception is std::function where it is possible that the function
1232 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00001233 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05001234 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00001235 "The current implementation is NOT ABI-compatible with the previous "
1236 "implementation for this configuration");
1237
Louis Dionneda463cb2020-12-11 12:22:16 -05001238 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1239 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
1240
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001241 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00001242 class = typename enable_if<
1243 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1244 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1245 >::type
1246 >
Eric Fiselier9d355982017-04-12 23:45:53 +00001247 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001248 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249
Eric Fiselier9d355982017-04-12 23:45:53 +00001250 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001251 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00001252 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001253 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001255#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001256 template <class... _Args1, class... _Args2>
1257 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1258 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1259 tuple<_Args2...> __second_args)
1260 : _Base1(__pc, _VSTD::move(__first_args),
1261 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1262 _Base2(__pc, _VSTD::move(__second_args),
1263 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001264#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265
Eric Fiselier9d355982017-04-12 23:45:53 +00001266 _LIBCPP_INLINE_VISIBILITY
1267 typename _Base1::reference first() _NOEXCEPT {
1268 return static_cast<_Base1&>(*this).__get();
1269 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Eric Fiselier9d355982017-04-12 23:45:53 +00001271 _LIBCPP_INLINE_VISIBILITY
1272 typename _Base1::const_reference first() const _NOEXCEPT {
1273 return static_cast<_Base1 const&>(*this).__get();
1274 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Eric Fiselier9d355982017-04-12 23:45:53 +00001276 _LIBCPP_INLINE_VISIBILITY
1277 typename _Base2::reference second() _NOEXCEPT {
1278 return static_cast<_Base2&>(*this).__get();
1279 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280
Eric Fiselier9d355982017-04-12 23:45:53 +00001281 _LIBCPP_INLINE_VISIBILITY
1282 typename _Base2::const_reference second() const _NOEXCEPT {
1283 return static_cast<_Base2 const&>(*this).__get();
1284 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Louis Dionneda463cb2020-12-11 12:22:16 -05001286 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1287 static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
1288 return static_cast<_Base1*>(__pair);
1289 }
1290 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1291 static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
1292 return static_cast<_Base2*>(__pair);
1293 }
1294
Eric Fiselier9d355982017-04-12 23:45:53 +00001295 _LIBCPP_INLINE_VISIBILITY
1296 void swap(__compressed_pair& __x)
1297 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1298 __is_nothrow_swappable<_T2>::value)
1299 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001300 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00001301 swap(first(), __x.first());
1302 swap(second(), __x.second());
1303 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304};
1305
1306template <class _T1, class _T2>
1307inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001308void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1309 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1310 __is_nothrow_swappable<_T2>::value) {
1311 __x.swap(__y);
1312}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001314// default_delete
1315
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001317struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001318 static_assert(!is_function<_Tp>::value,
1319 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001320#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001321 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001322#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001323 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001324#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001325 template <class _Up>
1326 _LIBCPP_INLINE_VISIBILITY
1327 default_delete(const default_delete<_Up>&,
1328 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1329 0) _NOEXCEPT {}
1330
1331 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1332 static_assert(sizeof(_Tp) > 0,
1333 "default_delete can not delete incomplete type");
1334 static_assert(!is_void<_Tp>::value,
1335 "default_delete can not delete incomplete type");
1336 delete __ptr;
1337 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338};
1339
1340template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001341struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1342private:
1343 template <class _Up>
1344 struct _EnableIfConvertible
1345 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1346
Howard Hinnant4500ca52011-12-18 21:19:44 +00001347public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001348#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001349 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001350#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001351 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001352#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001353
1354 template <class _Up>
1355 _LIBCPP_INLINE_VISIBILITY
1356 default_delete(const default_delete<_Up[]>&,
1357 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1358
1359 template <class _Up>
1360 _LIBCPP_INLINE_VISIBILITY
1361 typename _EnableIfConvertible<_Up>::type
1362 operator()(_Up* __ptr) const _NOEXCEPT {
1363 static_assert(sizeof(_Tp) > 0,
1364 "default_delete can not delete incomplete type");
1365 static_assert(!is_void<_Tp>::value,
1366 "default_delete can not delete void type");
1367 delete[] __ptr;
1368 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369};
1370
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001371template <class _Deleter>
1372struct __unique_ptr_deleter_sfinae {
1373 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1374 typedef const _Deleter& __lval_ref_type;
1375 typedef _Deleter&& __good_rval_ref_type;
1376 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377};
1378
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001379template <class _Deleter>
1380struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1381 typedef const _Deleter& __lval_ref_type;
1382 typedef const _Deleter&& __bad_rval_ref_type;
1383 typedef false_type __enable_rval_overload;
1384};
1385
1386template <class _Deleter>
1387struct __unique_ptr_deleter_sfinae<_Deleter&> {
1388 typedef _Deleter& __lval_ref_type;
1389 typedef _Deleter&& __bad_rval_ref_type;
1390 typedef false_type __enable_rval_overload;
1391};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001392
Vy Nguyene369bd92020-07-13 12:34:37 -04001393#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1394# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1395#else
1396# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1397#endif
1398
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001399template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001400class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001401public:
1402 typedef _Tp element_type;
1403 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001404 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001405
1406 static_assert(!is_rvalue_reference<deleter_type>::value,
1407 "the specified deleter type cannot be an rvalue reference");
1408
1409private:
1410 __compressed_pair<pointer, deleter_type> __ptr_;
1411
1412 struct __nat { int __for_bool_; };
1413
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001414 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001415
1416 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001417 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001418 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001419
1420 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001421 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001422 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001423
1424 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001425 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001426 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001427
1428 template <bool _Dummy, class _Deleter = typename __dependent_type<
1429 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001430 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001431 typename enable_if<is_default_constructible<_Deleter>::value &&
1432 !is_pointer<_Deleter>::value>::type;
1433
1434 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001435 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001436 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1437
1438 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001439 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001440 is_convertible<typename _UPtr::pointer, pointer>::value &&
1441 !is_array<_Up>::value
1442 >::type;
1443
1444 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001445 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001446 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1447 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1448 >::type;
1449
1450 template <class _UDel>
1451 using _EnableIfDeleterAssignable = typename enable_if<
1452 is_assignable<_Dp&, _UDel&&>::value
1453 >::type;
1454
1455public:
1456 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001457 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001458 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001459 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001460
1461 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001462 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001463 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001464 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001465
1466 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001467 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001468 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001469 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001470
1471 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001472 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001473 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001474 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001475 : __ptr_(__p, __d) {}
1476
1477 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001478 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001479 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001480 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001481 : __ptr_(__p, _VSTD::move(__d)) {
1482 static_assert(!is_reference<deleter_type>::value,
1483 "rvalue deleter bound to reference");
1484 }
1485
1486 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001487 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001488 _LIBCPP_INLINE_VISIBILITY
1489 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1490
1491 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001492 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001493 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1494 }
1495
1496 template <class _Up, class _Ep,
1497 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1498 class = _EnableIfDeleterConvertible<_Ep>
1499 >
1500 _LIBCPP_INLINE_VISIBILITY
1501 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1502 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1503
1504#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1505 template <class _Up>
1506 _LIBCPP_INLINE_VISIBILITY
1507 unique_ptr(auto_ptr<_Up>&& __p,
1508 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001509 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001510 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001511 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001512#endif
1513
1514 _LIBCPP_INLINE_VISIBILITY
1515 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1516 reset(__u.release());
1517 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1518 return *this;
1519 }
1520
1521 template <class _Up, class _Ep,
1522 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1523 class = _EnableIfDeleterAssignable<_Ep>
1524 >
1525 _LIBCPP_INLINE_VISIBILITY
1526 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1527 reset(__u.release());
1528 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1529 return *this;
1530 }
1531
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001532#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1533 template <class _Up>
1534 _LIBCPP_INLINE_VISIBILITY
1535 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1536 is_same<_Dp, default_delete<_Tp> >::value,
1537 unique_ptr&>::type
1538 operator=(auto_ptr<_Up> __p) {
1539 reset(__p.release());
1540 return *this;
1541 }
1542#endif
1543
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001544#ifdef _LIBCPP_CXX03_LANG
1545 unique_ptr(unique_ptr const&) = delete;
1546 unique_ptr& operator=(unique_ptr const&) = delete;
1547#endif
1548
1549
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001550 _LIBCPP_INLINE_VISIBILITY
1551 ~unique_ptr() { reset(); }
1552
1553 _LIBCPP_INLINE_VISIBILITY
1554 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1555 reset();
1556 return *this;
1557 }
1558
1559 _LIBCPP_INLINE_VISIBILITY
1560 typename add_lvalue_reference<_Tp>::type
1561 operator*() const {
1562 return *__ptr_.first();
1563 }
1564 _LIBCPP_INLINE_VISIBILITY
1565 pointer operator->() const _NOEXCEPT {
1566 return __ptr_.first();
1567 }
1568 _LIBCPP_INLINE_VISIBILITY
1569 pointer get() const _NOEXCEPT {
1570 return __ptr_.first();
1571 }
1572 _LIBCPP_INLINE_VISIBILITY
1573 deleter_type& get_deleter() _NOEXCEPT {
1574 return __ptr_.second();
1575 }
1576 _LIBCPP_INLINE_VISIBILITY
1577 const deleter_type& get_deleter() const _NOEXCEPT {
1578 return __ptr_.second();
1579 }
1580 _LIBCPP_INLINE_VISIBILITY
1581 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1582 return __ptr_.first() != nullptr;
1583 }
1584
1585 _LIBCPP_INLINE_VISIBILITY
1586 pointer release() _NOEXCEPT {
1587 pointer __t = __ptr_.first();
1588 __ptr_.first() = pointer();
1589 return __t;
1590 }
1591
1592 _LIBCPP_INLINE_VISIBILITY
1593 void reset(pointer __p = pointer()) _NOEXCEPT {
1594 pointer __tmp = __ptr_.first();
1595 __ptr_.first() = __p;
1596 if (__tmp)
1597 __ptr_.second()(__tmp);
1598 }
1599
1600 _LIBCPP_INLINE_VISIBILITY
1601 void swap(unique_ptr& __u) _NOEXCEPT {
1602 __ptr_.swap(__u.__ptr_);
1603 }
1604};
1605
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001606
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001608class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001610 typedef _Tp element_type;
1611 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001612 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001613
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001615 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616
Eric Fiselier31127cd2017-04-16 02:14:31 +00001617 template <class _From>
1618 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619
Eric Fiselier31127cd2017-04-16 02:14:31 +00001620 template <class _FromElem>
1621 struct _CheckArrayPointerConversion<_FromElem*>
1622 : integral_constant<bool,
1623 is_same<_FromElem*, pointer>::value ||
1624 (is_same<pointer, element_type*>::value &&
1625 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1626 >
1627 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628
Eric Fiselier31127cd2017-04-16 02:14:31 +00001629 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001630
1631 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001632 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001633 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001634
1635 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001636 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001637 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001638
1639 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001640 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001641 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001642
1643 template <bool _Dummy, class _Deleter = typename __dependent_type<
1644 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001645 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001646 typename enable_if<is_default_constructible<_Deleter>::value &&
1647 !is_pointer<_Deleter>::value>::type;
1648
1649 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001650 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001651 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1652
1653 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001654 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001655 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001656 >::type;
1657
1658 template <class _UPtr, class _Up,
1659 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001660 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001661 is_array<_Up>::value &&
1662 is_same<pointer, element_type*>::value &&
1663 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1664 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1665 >::type;
1666
1667 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001668 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001669 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1670 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1671 >::type;
1672
1673 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001674 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001675 is_assignable<_Dp&, _UDel&&>::value
1676 >::type;
1677
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001679 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001680 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001681 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001682 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001684 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001685 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001686 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001687 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001689 template <class _Pp, bool _Dummy = true,
1690 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001691 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001692 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001693 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001694 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001696 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001697 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1698 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001699 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001700 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001701 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001703 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001704 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001705 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001706 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001707 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001709 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001710 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1711 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001712 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001713 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001714 : __ptr_(__p, _VSTD::move(__d)) {
1715 static_assert(!is_reference<deleter_type>::value,
1716 "rvalue deleter bound to reference");
1717 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001719 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001720 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001721 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001722 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001723 : __ptr_(nullptr, _VSTD::move(__d)) {
1724 static_assert(!is_reference<deleter_type>::value,
1725 "rvalue deleter bound to reference");
1726 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001727
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001728 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001729 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1730 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001731 _LIBCPP_INLINE_VISIBILITY
1732 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001733
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001734 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001735 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001736 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1737 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001738
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001739 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001740 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001741 reset(__u.release());
1742 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1743 return *this;
1744 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001746 template <class _Up, class _Ep,
1747 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1748 class = _EnableIfDeleterConvertible<_Ep>
1749 >
1750 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001751 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001752 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001753 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001755 template <class _Up, class _Ep,
1756 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1757 class = _EnableIfDeleterAssignable<_Ep>
1758 >
1759 _LIBCPP_INLINE_VISIBILITY
1760 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001761 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001762 reset(__u.release());
1763 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1764 return *this;
1765 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001767#ifdef _LIBCPP_CXX03_LANG
1768 unique_ptr(unique_ptr const&) = delete;
1769 unique_ptr& operator=(unique_ptr const&) = delete;
1770#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001771
1772public:
1773 _LIBCPP_INLINE_VISIBILITY
1774 ~unique_ptr() { reset(); }
1775
1776 _LIBCPP_INLINE_VISIBILITY
1777 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1778 reset();
1779 return *this;
1780 }
1781
1782 _LIBCPP_INLINE_VISIBILITY
1783 typename add_lvalue_reference<_Tp>::type
1784 operator[](size_t __i) const {
1785 return __ptr_.first()[__i];
1786 }
1787 _LIBCPP_INLINE_VISIBILITY
1788 pointer get() const _NOEXCEPT {
1789 return __ptr_.first();
1790 }
1791
1792 _LIBCPP_INLINE_VISIBILITY
1793 deleter_type& get_deleter() _NOEXCEPT {
1794 return __ptr_.second();
1795 }
1796
1797 _LIBCPP_INLINE_VISIBILITY
1798 const deleter_type& get_deleter() const _NOEXCEPT {
1799 return __ptr_.second();
1800 }
1801 _LIBCPP_INLINE_VISIBILITY
1802 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1803 return __ptr_.first() != nullptr;
1804 }
1805
1806 _LIBCPP_INLINE_VISIBILITY
1807 pointer release() _NOEXCEPT {
1808 pointer __t = __ptr_.first();
1809 __ptr_.first() = pointer();
1810 return __t;
1811 }
1812
1813 template <class _Pp>
1814 _LIBCPP_INLINE_VISIBILITY
1815 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001816 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001817 >::type
1818 reset(_Pp __p) _NOEXCEPT {
1819 pointer __tmp = __ptr_.first();
1820 __ptr_.first() = __p;
1821 if (__tmp)
1822 __ptr_.second()(__tmp);
1823 }
1824
1825 _LIBCPP_INLINE_VISIBILITY
1826 void reset(nullptr_t = nullptr) _NOEXCEPT {
1827 pointer __tmp = __ptr_.first();
1828 __ptr_.first() = nullptr;
1829 if (__tmp)
1830 __ptr_.second()(__tmp);
1831 }
1832
1833 _LIBCPP_INLINE_VISIBILITY
1834 void swap(unique_ptr& __u) _NOEXCEPT {
1835 __ptr_.swap(__u.__ptr_);
1836 }
1837
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838};
1839
1840template <class _Tp, class _Dp>
1841inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001842typename enable_if<
1843 __is_swappable<_Dp>::value,
1844 void
1845>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001846swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847
1848template <class _T1, class _D1, class _T2, class _D2>
1849inline _LIBCPP_INLINE_VISIBILITY
1850bool
1851operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1852
1853template <class _T1, class _D1, class _T2, class _D2>
1854inline _LIBCPP_INLINE_VISIBILITY
1855bool
1856operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1857
1858template <class _T1, class _D1, class _T2, class _D2>
1859inline _LIBCPP_INLINE_VISIBILITY
1860bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001861operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1862{
1863 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1864 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001865 typedef typename common_type<_P1, _P2>::type _Vp;
1866 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001867}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868
1869template <class _T1, class _D1, class _T2, class _D2>
1870inline _LIBCPP_INLINE_VISIBILITY
1871bool
1872operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1873
1874template <class _T1, class _D1, class _T2, class _D2>
1875inline _LIBCPP_INLINE_VISIBILITY
1876bool
1877operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1878
1879template <class _T1, class _D1, class _T2, class _D2>
1880inline _LIBCPP_INLINE_VISIBILITY
1881bool
1882operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1883
Howard Hinnantb17caf92012-02-21 21:02:58 +00001884template <class _T1, class _D1>
1885inline _LIBCPP_INLINE_VISIBILITY
1886bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001887operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001888{
1889 return !__x;
1890}
1891
1892template <class _T1, class _D1>
1893inline _LIBCPP_INLINE_VISIBILITY
1894bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001895operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001896{
1897 return !__x;
1898}
1899
1900template <class _T1, class _D1>
1901inline _LIBCPP_INLINE_VISIBILITY
1902bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001903operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001904{
1905 return static_cast<bool>(__x);
1906}
1907
1908template <class _T1, class _D1>
1909inline _LIBCPP_INLINE_VISIBILITY
1910bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001911operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001912{
1913 return static_cast<bool>(__x);
1914}
1915
1916template <class _T1, class _D1>
1917inline _LIBCPP_INLINE_VISIBILITY
1918bool
1919operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1920{
1921 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1922 return less<_P1>()(__x.get(), nullptr);
1923}
1924
1925template <class _T1, class _D1>
1926inline _LIBCPP_INLINE_VISIBILITY
1927bool
1928operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1929{
1930 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1931 return less<_P1>()(nullptr, __x.get());
1932}
1933
1934template <class _T1, class _D1>
1935inline _LIBCPP_INLINE_VISIBILITY
1936bool
1937operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1938{
1939 return nullptr < __x;
1940}
1941
1942template <class _T1, class _D1>
1943inline _LIBCPP_INLINE_VISIBILITY
1944bool
1945operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1946{
1947 return __x < nullptr;
1948}
1949
1950template <class _T1, class _D1>
1951inline _LIBCPP_INLINE_VISIBILITY
1952bool
1953operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1954{
1955 return !(nullptr < __x);
1956}
1957
1958template <class _T1, class _D1>
1959inline _LIBCPP_INLINE_VISIBILITY
1960bool
1961operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1962{
1963 return !(__x < nullptr);
1964}
1965
1966template <class _T1, class _D1>
1967inline _LIBCPP_INLINE_VISIBILITY
1968bool
1969operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1970{
1971 return !(__x < nullptr);
1972}
1973
1974template <class _T1, class _D1>
1975inline _LIBCPP_INLINE_VISIBILITY
1976bool
1977operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1978{
1979 return !(nullptr < __x);
1980}
1981
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001982#if _LIBCPP_STD_VER > 11
1983
1984template<class _Tp>
1985struct __unique_if
1986{
1987 typedef unique_ptr<_Tp> __unique_single;
1988};
1989
1990template<class _Tp>
1991struct __unique_if<_Tp[]>
1992{
1993 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1994};
1995
1996template<class _Tp, size_t _Np>
1997struct __unique_if<_Tp[_Np]>
1998{
1999 typedef void __unique_array_known_bound;
2000};
2001
2002template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002003inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002004typename __unique_if<_Tp>::__unique_single
2005make_unique(_Args&&... __args)
2006{
2007 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2008}
2009
2010template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002011inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002012typename __unique_if<_Tp>::__unique_array_unknown_bound
2013make_unique(size_t __n)
2014{
2015 typedef typename remove_extent<_Tp>::type _Up;
2016 return unique_ptr<_Tp>(new _Up[__n]());
2017}
2018
2019template<class _Tp, class... _Args>
2020 typename __unique_if<_Tp>::__unique_array_known_bound
2021 make_unique(_Args&&...) = delete;
2022
2023#endif // _LIBCPP_STD_VER > 11
2024
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002025template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002026#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002027struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002028#else
2029struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002030 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002031#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002032{
2033 typedef unique_ptr<_Tp, _Dp> argument_type;
2034 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002035 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002036 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002037 {
2038 typedef typename argument_type::pointer pointer;
2039 return hash<pointer>()(__ptr.get());
2040 }
2041};
2042
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043struct __destruct_n
2044{
2045private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002046 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047
2048 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002049 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002050 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051
2052 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002053 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054 {}
2055
Howard Hinnant719bda32011-05-28 14:41:13 +00002056 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002057 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002058 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059 {}
2060
Howard Hinnant719bda32011-05-28 14:41:13 +00002061 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002062 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002063 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064 {}
2065public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002066 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002067 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068
2069 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002070 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002071 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
2073 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002074 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002075 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076
2077 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002078 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002079 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080};
2081
2082template <class _Alloc>
2083class __allocator_destructor
2084{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002085 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002087 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2088 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089private:
2090 _Alloc& __alloc_;
2091 size_type __s_;
2092public:
2093 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002094 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002097 void operator()(pointer __p) _NOEXCEPT
2098 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099};
2100
2101template <class _InputIterator, class _ForwardIterator>
2102_ForwardIterator
2103uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2104{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002106#ifndef _LIBCPP_NO_EXCEPTIONS
2107 _ForwardIterator __s = __r;
2108 try
2109 {
2110#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002111 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002112 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002113#ifndef _LIBCPP_NO_EXCEPTIONS
2114 }
2115 catch (...)
2116 {
2117 for (; __s != __r; ++__s)
2118 __s->~value_type();
2119 throw;
2120 }
2121#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122 return __r;
2123}
2124
2125template <class _InputIterator, class _Size, class _ForwardIterator>
2126_ForwardIterator
2127uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2128{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002130#ifndef _LIBCPP_NO_EXCEPTIONS
2131 _ForwardIterator __s = __r;
2132 try
2133 {
2134#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002135 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002136 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002137#ifndef _LIBCPP_NO_EXCEPTIONS
2138 }
2139 catch (...)
2140 {
2141 for (; __s != __r; ++__s)
2142 __s->~value_type();
2143 throw;
2144 }
2145#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146 return __r;
2147}
2148
2149template <class _ForwardIterator, class _Tp>
2150void
2151uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2152{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002154#ifndef _LIBCPP_NO_EXCEPTIONS
2155 _ForwardIterator __s = __f;
2156 try
2157 {
2158#endif
2159 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002160 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002161#ifndef _LIBCPP_NO_EXCEPTIONS
2162 }
2163 catch (...)
2164 {
2165 for (; __s != __f; ++__s)
2166 __s->~value_type();
2167 throw;
2168 }
2169#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170}
2171
2172template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002173_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2175{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002177#ifndef _LIBCPP_NO_EXCEPTIONS
2178 _ForwardIterator __s = __f;
2179 try
2180 {
2181#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002182 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002183 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002184#ifndef _LIBCPP_NO_EXCEPTIONS
2185 }
2186 catch (...)
2187 {
2188 for (; __s != __f; ++__s)
2189 __s->~value_type();
2190 throw;
2191 }
2192#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002193 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194}
2195
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002196#if _LIBCPP_STD_VER > 14
2197
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002198template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002199inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002200void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2201 for (; __first != __last; ++__first)
2202 _VSTD::destroy_at(_VSTD::addressof(*__first));
2203}
2204
2205template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002206inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002207_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2208 for (; __n > 0; (void)++__first, --__n)
2209 _VSTD::destroy_at(_VSTD::addressof(*__first));
2210 return __first;
2211}
2212
Eric Fiselier290c07c2016-10-11 21:13:44 +00002213template <class _ForwardIterator>
2214inline _LIBCPP_INLINE_VISIBILITY
2215void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2216 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2217 auto __idx = __first;
2218#ifndef _LIBCPP_NO_EXCEPTIONS
2219 try {
2220#endif
2221 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002222 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002223#ifndef _LIBCPP_NO_EXCEPTIONS
2224 } catch (...) {
2225 _VSTD::destroy(__first, __idx);
2226 throw;
2227 }
2228#endif
2229}
2230
2231template <class _ForwardIterator, class _Size>
2232inline _LIBCPP_INLINE_VISIBILITY
2233_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2234 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2235 auto __idx = __first;
2236#ifndef _LIBCPP_NO_EXCEPTIONS
2237 try {
2238#endif
2239 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002240 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002241 return __idx;
2242#ifndef _LIBCPP_NO_EXCEPTIONS
2243 } catch (...) {
2244 _VSTD::destroy(__first, __idx);
2245 throw;
2246 }
2247#endif
2248}
2249
2250
2251template <class _ForwardIterator>
2252inline _LIBCPP_INLINE_VISIBILITY
2253void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2254 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2255 auto __idx = __first;
2256#ifndef _LIBCPP_NO_EXCEPTIONS
2257 try {
2258#endif
2259 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002260 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002261#ifndef _LIBCPP_NO_EXCEPTIONS
2262 } catch (...) {
2263 _VSTD::destroy(__first, __idx);
2264 throw;
2265 }
2266#endif
2267}
2268
2269template <class _ForwardIterator, class _Size>
2270inline _LIBCPP_INLINE_VISIBILITY
2271_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2272 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2273 auto __idx = __first;
2274#ifndef _LIBCPP_NO_EXCEPTIONS
2275 try {
2276#endif
2277 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002278 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002279 return __idx;
2280#ifndef _LIBCPP_NO_EXCEPTIONS
2281 } catch (...) {
2282 _VSTD::destroy(__first, __idx);
2283 throw;
2284 }
2285#endif
2286}
2287
2288
2289template <class _InputIt, class _ForwardIt>
2290inline _LIBCPP_INLINE_VISIBILITY
2291_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2292 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2293 auto __idx = __first_res;
2294#ifndef _LIBCPP_NO_EXCEPTIONS
2295 try {
2296#endif
2297 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002298 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002299 return __idx;
2300#ifndef _LIBCPP_NO_EXCEPTIONS
2301 } catch (...) {
2302 _VSTD::destroy(__first_res, __idx);
2303 throw;
2304 }
2305#endif
2306}
2307
2308template <class _InputIt, class _Size, class _ForwardIt>
2309inline _LIBCPP_INLINE_VISIBILITY
2310pair<_InputIt, _ForwardIt>
2311uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2312 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2313 auto __idx = __first_res;
2314#ifndef _LIBCPP_NO_EXCEPTIONS
2315 try {
2316#endif
2317 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002318 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002319 return {__first, __idx};
2320#ifndef _LIBCPP_NO_EXCEPTIONS
2321 } catch (...) {
2322 _VSTD::destroy(__first_res, __idx);
2323 throw;
2324 }
2325#endif
2326}
2327
2328
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002329#endif // _LIBCPP_STD_VER > 14
2330
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002331// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2332// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -05002333// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002334#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2335 && defined(__ATOMIC_RELAXED) \
2336 && defined(__ATOMIC_ACQ_REL)
2337# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002338#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002339# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2340#endif
2341
2342template <class _Tp>
2343inline _LIBCPP_INLINE_VISIBILITY _Tp
2344__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2345{
2346#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2347 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2348#else
2349 return __t += 1;
2350#endif
2351}
2352
2353template <class _Tp>
2354inline _LIBCPP_INLINE_VISIBILITY _Tp
2355__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2356{
2357#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2358 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2359#else
2360 return __t -= 1;
2361#endif
2362}
2363
Howard Hinnant756c69b2010-09-22 16:48:34 +00002364class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365 : public std::exception
2366{
2367public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002368 bad_weak_ptr() _NOEXCEPT = default;
2369 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002370 virtual ~bad_weak_ptr() _NOEXCEPT;
2371 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372};
2373
Louis Dionne16fe2952018-07-11 23:14:33 +00002374_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002375void __throw_bad_weak_ptr()
2376{
2377#ifndef _LIBCPP_NO_EXCEPTIONS
2378 throw bad_weak_ptr();
2379#else
2380 _VSTD::abort();
2381#endif
2382}
2383
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002384template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002386class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387{
2388 __shared_count(const __shared_count&);
2389 __shared_count& operator=(const __shared_count&);
2390
2391protected:
2392 long __shared_owners_;
2393 virtual ~__shared_count();
2394private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002395 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396
2397public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002399 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 : __shared_owners_(__refs) {}
2401
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002402#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002403 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002404 void __add_shared() _NOEXCEPT;
2405 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002406#else
2407 _LIBCPP_INLINE_VISIBILITY
2408 void __add_shared() _NOEXCEPT {
2409 __libcpp_atomic_refcount_increment(__shared_owners_);
2410 }
2411 _LIBCPP_INLINE_VISIBILITY
2412 bool __release_shared() _NOEXCEPT {
2413 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2414 __on_zero_shared();
2415 return true;
2416 }
2417 return false;
2418 }
2419#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002420 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002421 long use_count() const _NOEXCEPT {
2422 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2423 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424};
2425
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002426class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427 : private __shared_count
2428{
2429 long __shared_weak_owners_;
2430
2431public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002433 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434 : __shared_count(__refs),
2435 __shared_weak_owners_(__refs) {}
2436protected:
2437 virtual ~__shared_weak_count();
2438
2439public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002440#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002441 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002442 void __add_shared() _NOEXCEPT;
2443 void __add_weak() _NOEXCEPT;
2444 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002445#else
2446 _LIBCPP_INLINE_VISIBILITY
2447 void __add_shared() _NOEXCEPT {
2448 __shared_count::__add_shared();
2449 }
2450 _LIBCPP_INLINE_VISIBILITY
2451 void __add_weak() _NOEXCEPT {
2452 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2453 }
2454 _LIBCPP_INLINE_VISIBILITY
2455 void __release_shared() _NOEXCEPT {
2456 if (__shared_count::__release_shared())
2457 __release_weak();
2458 }
2459#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002460 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002462 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2463 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464
Howard Hinnant719bda32011-05-28 14:41:13 +00002465 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002467 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468};
2469
2470template <class _Tp, class _Dp, class _Alloc>
2471class __shared_ptr_pointer
2472 : public __shared_weak_count
2473{
2474 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2475public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002478 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479
Howard Hinnant72f73582010-08-11 17:04:31 +00002480#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002481 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002482#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483
2484private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002485 virtual void __on_zero_shared() _NOEXCEPT;
2486 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487};
2488
Howard Hinnant72f73582010-08-11 17:04:31 +00002489#ifndef _LIBCPP_NO_RTTI
2490
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491template <class _Tp, class _Dp, class _Alloc>
2492const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002493__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002495 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496}
2497
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002498#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002499
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500template <class _Tp, class _Dp, class _Alloc>
2501void
Howard Hinnant719bda32011-05-28 14:41:13 +00002502__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503{
2504 __data_.first().second()(__data_.first().first());
2505 __data_.first().second().~_Dp();
2506}
2507
2508template <class _Tp, class _Dp, class _Alloc>
2509void
Howard Hinnant719bda32011-05-28 14:41:13 +00002510__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002512 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2513 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002514 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2515
Eric Fiselierf8898c82015-02-05 23:01:40 +00002516 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002518 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519}
2520
2521template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002522struct __shared_ptr_emplace
2523 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524{
Louis Dionneda463cb2020-12-11 12:22:16 -05002525 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002526 _LIBCPP_HIDE_FROM_ABI
2527 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002528 : __storage_(_VSTD::move(__a))
2529 {
2530#if _LIBCPP_STD_VER > 17
2531 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2532 _TpAlloc __tmp(*__get_alloc());
2533 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002534#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002535 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002536#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002537 }
Louis Dionne86549d72020-12-09 16:22:17 -05002538
2539 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002540 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002541
2542 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002543 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544
2545private:
Louis Dionne86549d72020-12-09 16:22:17 -05002546 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002547#if _LIBCPP_STD_VER > 17
2548 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2549 _TpAlloc __tmp(*__get_alloc());
2550 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2551#else
Louis Dionne86549d72020-12-09 16:22:17 -05002552 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002553#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002554 }
2555
2556 virtual void __on_zero_shared_weak() _NOEXCEPT {
2557 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2558 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002559 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002560 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002561 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2562 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2563 }
2564
Louis Dionneda463cb2020-12-11 12:22:16 -05002565 // This class implements the control block for non-array shared pointers created
2566 // through `std::allocate_shared` and `std::make_shared`.
2567 //
2568 // In previous versions of the library, we used a compressed pair to store
2569 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2570 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2571 // we now use a properly aligned char buffer while making sure that we maintain
2572 // the same layout that we had when we used a compressed pair.
2573 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2574 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2575 char __blob_[sizeof(_CompressedPair)];
2576
2577 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2578 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2579 }
2580 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2581 __get_alloc()->~_Alloc();
2582 }
2583 _Alloc* __get_alloc() _NOEXCEPT {
2584 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2585 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2586 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2587 return __alloc;
2588 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08002589 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002590 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2591 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2592 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2593 return __elem;
2594 }
2595 };
2596
2597 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2598 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2599 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600};
2601
Erik Pilkington2a398762017-05-25 15:43:31 +00002602struct __shared_ptr_dummy_rebind_allocator_type;
2603template <>
2604class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2605{
2606public:
2607 template <class _Other>
2608 struct rebind
2609 {
2610 typedef allocator<_Other> other;
2611 };
2612};
2613
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002614template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615
zoecarverd73f42a2020-05-11 18:42:50 -07002616template<class _Tp, class _Up>
2617struct __compatible_with
2618#if _LIBCPP_STD_VER > 14
2619 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2620#else
2621 : is_convertible<_Tp*, _Up*> {};
2622#endif // _LIBCPP_STD_VER > 14
2623
zoecarver9bc39102021-02-19 11:10:36 -08002624template <class _Dp, class _Pt,
2625 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
2626static true_type __well_formed_deleter_test(int);
2627
2628template <class, class>
2629static false_type __well_formed_deleter_test(...);
2630
2631template <class _Dp, class _Pt>
2632struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
2633
2634template<class _Dp, class _Tp, class _Yp>
2635struct __shared_ptr_deleter_ctor_reqs
2636{
2637 static const bool value = __compatible_with<_Tp, _Yp>::value &&
2638 is_move_constructible<_Dp>::value &&
2639 __well_formed_deleter<_Dp, _Tp*>::value;
2640};
2641
Vy Nguyene369bd92020-07-13 12:34:37 -04002642#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2643# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2644#else
2645# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2646#endif
2647
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002649class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002651public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002652#if _LIBCPP_STD_VER > 14
2653 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002654 typedef remove_extent_t<_Tp> element_type;
2655#else
2656 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002657#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002658
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659private:
2660 element_type* __ptr_;
2661 __shared_weak_count* __cntrl_;
2662
2663 struct __nat {int __for_bool_;};
2664public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002666 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002668 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002669 template<class _Yp>
2670 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002671 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002672 template<class _Yp, class _Dp>
2673 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002674 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002675 template<class _Yp, class _Dp, class _Alloc>
2676 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002677 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2679 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002680 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002682 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002686 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002687 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002689 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002690 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002691 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002692 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002694 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002695#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002696 template<class _Yp>
2697 shared_ptr(auto_ptr<_Yp>&& __r,
2698 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002699#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002700 template <class _Yp, class _Dp>
2701 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2702 typename enable_if
2703 <
2704 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002705 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2706 __nat
2707 >::type = __nat());
2708 template <class _Yp, class _Dp>
2709 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2710 typename enable_if
2711 <
2712 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002713 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2714 __nat
2715 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716
2717 ~shared_ptr();
2718
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002720 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002721 template<class _Yp>
2722 typename enable_if
2723 <
zoecarverd73f42a2020-05-11 18:42:50 -07002724 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002725 shared_ptr&
2726 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002728 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002730 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002731 template<class _Yp>
2732 typename enable_if
2733 <
zoecarverd73f42a2020-05-11 18:42:50 -07002734 __compatible_with<_Yp, element_type>::value,
2735 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002736 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002738 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002739#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002740 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002742 typename enable_if
2743 <
2744 !is_array<_Yp>::value &&
2745 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002746 shared_ptr
2747 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002748 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002749#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002750 template <class _Yp, class _Dp>
2751 typename enable_if
2752 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002753 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2754 shared_ptr&
2755 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002757 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002760 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002762 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002763 template<class _Yp>
2764 typename enable_if
2765 <
zoecarverd73f42a2020-05-11 18:42:50 -07002766 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002767 void
2768 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002770 reset(_Yp* __p);
2771 template<class _Yp, class _Dp>
2772 typename enable_if
2773 <
zoecarverd73f42a2020-05-11 18:42:50 -07002774 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002775 void
2776 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002778 reset(_Yp* __p, _Dp __d);
2779 template<class _Yp, class _Dp, class _Alloc>
2780 typename enable_if
2781 <
zoecarverd73f42a2020-05-11 18:42:50 -07002782 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002783 void
2784 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002786 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787
Howard Hinnant756c69b2010-09-22 16:48:34 +00002788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002789 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002791 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2792 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002793 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002794 element_type* operator->() const _NOEXCEPT
2795 {
2796 static_assert(!_VSTD::is_array<_Tp>::value,
2797 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2798 return __ptr_;
2799 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002801 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002803 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002804 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002805 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002806 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002807 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002808 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002810 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002811 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002812 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002814 _LIBCPP_INLINE_VISIBILITY
2815 bool
2816 __owner_equivalent(const shared_ptr& __p) const
2817 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818
zoecarverd73f42a2020-05-11 18:42:50 -07002819#if _LIBCPP_STD_VER > 14
2820 typename add_lvalue_reference<element_type>::type
2821 _LIBCPP_INLINE_VISIBILITY
2822 operator[](ptrdiff_t __i) const
2823 {
2824 static_assert(_VSTD::is_array<_Tp>::value,
2825 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2826 return __ptr_[__i];
2827 }
2828#endif
2829
Howard Hinnant72f73582010-08-11 17:04:31 +00002830#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002833 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002834 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002835 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002836 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002837#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838
Zoe Carverd9040c72019-10-22 15:16:49 +00002839 template<class _Yp, class _CntrlBlk>
2840 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002841 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002842 {
2843 shared_ptr<_Tp> __r;
2844 __r.__ptr_ = __p;
2845 __r.__cntrl_ = __cntrl;
2846 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2847 return __r;
2848 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002849
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002851 template <class _Yp, bool = is_function<_Yp>::value>
2852 struct __shared_ptr_default_allocator
2853 {
2854 typedef allocator<_Yp> type;
2855 };
2856
2857 template <class _Yp>
2858 struct __shared_ptr_default_allocator<_Yp, true>
2859 {
2860 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2861 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002863 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002864 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002865 typename enable_if<is_convertible<_OrigPtr*,
2866 const enable_shared_from_this<_Yp>*
2867 >::value,
2868 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002869 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2870 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002872 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002873 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002874 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002875 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2876 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002877 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 }
2879
Erik Pilkington2a398762017-05-25 15:43:31 +00002880 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881
zoecarverd73f42a2020-05-11 18:42:50 -07002882 template <class, class _Yp>
2883 struct __shared_ptr_default_delete
2884 : default_delete<_Yp> {};
2885
2886 template <class _Yp, class _Un, size_t _Sz>
2887 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2888 : default_delete<_Yp[]> {};
2889
2890 template <class _Yp, class _Un>
2891 struct __shared_ptr_default_delete<_Yp[], _Un>
2892 : default_delete<_Yp[]> {};
2893
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002894 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2895 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896};
2897
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002898#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2899template<class _Tp>
2900shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2901template<class _Tp, class _Dp>
2902shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2903#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002904
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002906inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002907_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002908shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002909 : __ptr_(nullptr),
2910 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911{
2912}
2913
2914template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002915inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002916_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002917shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002918 : __ptr_(nullptr),
2919 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920{
2921}
2922
2923template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002924template<class _Yp>
2925shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002926 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 : __ptr_(__p)
2928{
2929 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002930 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002931 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2932 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002934 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935}
2936
2937template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002938template<class _Yp, class _Dp>
2939shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002940 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 : __ptr_(__p)
2942{
2943#ifndef _LIBCPP_NO_EXCEPTIONS
2944 try
2945 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002946#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002947 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2948 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002949#ifndef _LIBCPP_CXX03_LANG
2950 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2951#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002952 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002953#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002954 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955#ifndef _LIBCPP_NO_EXCEPTIONS
2956 }
2957 catch (...)
2958 {
2959 __d(__p);
2960 throw;
2961 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002962#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963}
2964
2965template<class _Tp>
2966template<class _Dp>
2967shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002968 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969{
2970#ifndef _LIBCPP_NO_EXCEPTIONS
2971 try
2972 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002973#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002974 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2975 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002976#ifndef _LIBCPP_CXX03_LANG
2977 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2978#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002979 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002980#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981#ifndef _LIBCPP_NO_EXCEPTIONS
2982 }
2983 catch (...)
2984 {
2985 __d(__p);
2986 throw;
2987 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002988#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989}
2990
2991template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002992template<class _Yp, class _Dp, class _Alloc>
2993shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002994 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995 : __ptr_(__p)
2996{
2997#ifndef _LIBCPP_NO_EXCEPTIONS
2998 try
2999 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003000#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003002 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003 typedef __allocator_destructor<_A2> _D2;
3004 _A2 __a2(__a);
3005 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08003006 ::new ((void*)_VSTD::addressof(*__hold2.get()))
3007#ifndef _LIBCPP_CXX03_LANG
3008 _CntrlBlk(__p, _VSTD::move(__d), __a);
3009#else
3010 _CntrlBlk(__p, __d, __a);
3011#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003012 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003013 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014#ifndef _LIBCPP_NO_EXCEPTIONS
3015 }
3016 catch (...)
3017 {
3018 __d(__p);
3019 throw;
3020 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003021#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022}
3023
3024template<class _Tp>
3025template<class _Dp, class _Alloc>
3026shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003027 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028{
3029#ifndef _LIBCPP_NO_EXCEPTIONS
3030 try
3031 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003032#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003034 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 typedef __allocator_destructor<_A2> _D2;
3036 _A2 __a2(__a);
3037 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08003038 ::new ((void*)_VSTD::addressof(*__hold2.get()))
3039#ifndef _LIBCPP_CXX03_LANG
3040 _CntrlBlk(__p, _VSTD::move(__d), __a);
3041#else
3042 _CntrlBlk(__p, __d, __a);
3043#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003044 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045#ifndef _LIBCPP_NO_EXCEPTIONS
3046 }
3047 catch (...)
3048 {
3049 __d(__p);
3050 throw;
3051 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003052#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053}
3054
3055template<class _Tp>
3056template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003057inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003058shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059 : __ptr_(__p),
3060 __cntrl_(__r.__cntrl_)
3061{
3062 if (__cntrl_)
3063 __cntrl_->__add_shared();
3064}
3065
3066template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003067inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003068shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069 : __ptr_(__r.__ptr_),
3070 __cntrl_(__r.__cntrl_)
3071{
3072 if (__cntrl_)
3073 __cntrl_->__add_shared();
3074}
3075
3076template<class _Tp>
3077template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003078inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003080 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003081 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082 : __ptr_(__r.__ptr_),
3083 __cntrl_(__r.__cntrl_)
3084{
3085 if (__cntrl_)
3086 __cntrl_->__add_shared();
3087}
3088
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003090inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003091shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092 : __ptr_(__r.__ptr_),
3093 __cntrl_(__r.__cntrl_)
3094{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003095 __r.__ptr_ = nullptr;
3096 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097}
3098
3099template<class _Tp>
3100template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003101inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003103 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003104 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105 : __ptr_(__r.__ptr_),
3106 __cntrl_(__r.__cntrl_)
3107{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003108 __r.__ptr_ = nullptr;
3109 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110}
3111
Marshall Clowb22274f2017-01-24 22:22:33 +00003112#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003114template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003115shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003116 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 : __ptr_(__r.get())
3118{
3119 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3120 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003121 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122 __r.release();
3123}
Marshall Clowb22274f2017-01-24 22:22:33 +00003124#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125
3126template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003127template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003129 typename enable_if
3130 <
3131 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00003132 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3133 __nat
3134 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135 : __ptr_(__r.get())
3136{
Marshall Clow35cde742015-05-10 13:59:45 +00003137#if _LIBCPP_STD_VER > 11
3138 if (__ptr_ == nullptr)
3139 __cntrl_ = nullptr;
3140 else
3141#endif
3142 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003143 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07003144 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
Erik Pilkington2a398762017-05-25 15:43:31 +00003145 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003146 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003147 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148 __r.release();
3149}
3150
3151template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003152template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003154 typename enable_if
3155 <
3156 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00003157 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3158 __nat
3159 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160 : __ptr_(__r.get())
3161{
Marshall Clow35cde742015-05-10 13:59:45 +00003162#if _LIBCPP_STD_VER > 11
3163 if (__ptr_ == nullptr)
3164 __cntrl_ = nullptr;
3165 else
3166#endif
3167 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003168 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07003169 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow35cde742015-05-10 13:59:45 +00003170 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003171 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003172 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003173 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003174 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175 __r.release();
3176}
3177
Zoe Carver6cd05c32019-08-19 15:47:16 +00003178template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179shared_ptr<_Tp>::~shared_ptr()
3180{
3181 if (__cntrl_)
3182 __cntrl_->__release_shared();
3183}
3184
3185template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003186inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003188shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189{
3190 shared_ptr(__r).swap(*this);
3191 return *this;
3192}
3193
3194template<class _Tp>
3195template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003196inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003197typename enable_if
3198<
zoecarverd73f42a2020-05-11 18:42:50 -07003199 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003200 shared_ptr<_Tp>&
3201>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003202shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203{
3204 shared_ptr(__r).swap(*this);
3205 return *this;
3206}
3207
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003209inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003211shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003213 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214 return *this;
3215}
3216
3217template<class _Tp>
3218template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003219inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003220typename enable_if
3221<
zoecarverd73f42a2020-05-11 18:42:50 -07003222 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003223 shared_ptr<_Tp>&
3224>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3226{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003227 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003228 return *this;
3229}
3230
Marshall Clowb22274f2017-01-24 22:22:33 +00003231#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232template<class _Tp>
3233template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003234inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003235typename enable_if
3236<
3237 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003238 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003239 shared_ptr<_Tp>
3240>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3242{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003243 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244 return *this;
3245}
Marshall Clowb22274f2017-01-24 22:22:33 +00003246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247
3248template<class _Tp>
3249template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003250inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003251typename enable_if
3252<
Aditya Kumar7c5db692017-08-20 10:38:55 +00003253 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003254 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003255 shared_ptr<_Tp>&
3256>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3258{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003259 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260 return *this;
3261}
3262
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003264inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265void
Howard Hinnant719bda32011-05-28 14:41:13 +00003266shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003268 _VSTD::swap(__ptr_, __r.__ptr_);
3269 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270}
3271
3272template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003273inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003274void
Howard Hinnant719bda32011-05-28 14:41:13 +00003275shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276{
3277 shared_ptr().swap(*this);
3278}
3279
3280template<class _Tp>
3281template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003282inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003283typename enable_if
3284<
zoecarverd73f42a2020-05-11 18:42:50 -07003285 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003286 void
3287>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288shared_ptr<_Tp>::reset(_Yp* __p)
3289{
3290 shared_ptr(__p).swap(*this);
3291}
3292
3293template<class _Tp>
3294template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003295inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003296typename enable_if
3297<
zoecarverd73f42a2020-05-11 18:42:50 -07003298 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003299 void
3300>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003301shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3302{
3303 shared_ptr(__p, __d).swap(*this);
3304}
3305
3306template<class _Tp>
3307template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003308inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003309typename enable_if
3310<
zoecarverd73f42a2020-05-11 18:42:50 -07003311 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003312 void
3313>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3315{
3316 shared_ptr(__p, __d, __a).swap(*this);
3317}
3318
Louis Dionne74aef9f2020-12-11 12:20:06 -05003319//
3320// std::allocate_shared and std::make_shared
3321//
3322template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3323_LIBCPP_HIDE_FROM_ABI
3324shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325{
Louis Dionne74aef9f2020-12-11 12:20:06 -05003326 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3327 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3328 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3329 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3330 auto __control_block = __guard.__release_ptr();
3331 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332}
3333
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003334template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3335_LIBCPP_HIDE_FROM_ABI
3336shared_ptr<_Tp> make_shared(_Args&& ...__args)
3337{
3338 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3339}
3340
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341template<class _Tp, class _Up>
3342inline _LIBCPP_INLINE_VISIBILITY
3343bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003344operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345{
3346 return __x.get() == __y.get();
3347}
3348
3349template<class _Tp, class _Up>
3350inline _LIBCPP_INLINE_VISIBILITY
3351bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003352operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353{
3354 return !(__x == __y);
3355}
3356
3357template<class _Tp, class _Up>
3358inline _LIBCPP_INLINE_VISIBILITY
3359bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003360operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003362#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003363 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3364 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003365#else
3366 return less<>()(__x.get(), __y.get());
3367#endif
3368
Howard Hinnantb17caf92012-02-21 21:02:58 +00003369}
3370
3371template<class _Tp, class _Up>
3372inline _LIBCPP_INLINE_VISIBILITY
3373bool
3374operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3375{
3376 return __y < __x;
3377}
3378
3379template<class _Tp, class _Up>
3380inline _LIBCPP_INLINE_VISIBILITY
3381bool
3382operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3383{
3384 return !(__y < __x);
3385}
3386
3387template<class _Tp, class _Up>
3388inline _LIBCPP_INLINE_VISIBILITY
3389bool
3390operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3391{
3392 return !(__x < __y);
3393}
3394
3395template<class _Tp>
3396inline _LIBCPP_INLINE_VISIBILITY
3397bool
3398operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3399{
3400 return !__x;
3401}
3402
3403template<class _Tp>
3404inline _LIBCPP_INLINE_VISIBILITY
3405bool
3406operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3407{
3408 return !__x;
3409}
3410
3411template<class _Tp>
3412inline _LIBCPP_INLINE_VISIBILITY
3413bool
3414operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3415{
3416 return static_cast<bool>(__x);
3417}
3418
3419template<class _Tp>
3420inline _LIBCPP_INLINE_VISIBILITY
3421bool
3422operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3423{
3424 return static_cast<bool>(__x);
3425}
3426
3427template<class _Tp>
3428inline _LIBCPP_INLINE_VISIBILITY
3429bool
3430operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3431{
3432 return less<_Tp*>()(__x.get(), nullptr);
3433}
3434
3435template<class _Tp>
3436inline _LIBCPP_INLINE_VISIBILITY
3437bool
3438operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3439{
3440 return less<_Tp*>()(nullptr, __x.get());
3441}
3442
3443template<class _Tp>
3444inline _LIBCPP_INLINE_VISIBILITY
3445bool
3446operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3447{
3448 return nullptr < __x;
3449}
3450
3451template<class _Tp>
3452inline _LIBCPP_INLINE_VISIBILITY
3453bool
3454operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3455{
3456 return __x < nullptr;
3457}
3458
3459template<class _Tp>
3460inline _LIBCPP_INLINE_VISIBILITY
3461bool
3462operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3463{
3464 return !(nullptr < __x);
3465}
3466
3467template<class _Tp>
3468inline _LIBCPP_INLINE_VISIBILITY
3469bool
3470operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3471{
3472 return !(__x < nullptr);
3473}
3474
3475template<class _Tp>
3476inline _LIBCPP_INLINE_VISIBILITY
3477bool
3478operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3479{
3480 return !(__x < nullptr);
3481}
3482
3483template<class _Tp>
3484inline _LIBCPP_INLINE_VISIBILITY
3485bool
3486operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3487{
3488 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489}
3490
3491template<class _Tp>
3492inline _LIBCPP_INLINE_VISIBILITY
3493void
Howard Hinnant719bda32011-05-28 14:41:13 +00003494swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495{
3496 __x.swap(__y);
3497}
3498
3499template<class _Tp, class _Up>
3500inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003501shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003502static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503{
zoecarverd73f42a2020-05-11 18:42:50 -07003504 return shared_ptr<_Tp>(__r,
3505 static_cast<
3506 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507}
3508
3509template<class _Tp, class _Up>
3510inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003511shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003512dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513{
zoecarverd73f42a2020-05-11 18:42:50 -07003514 typedef typename shared_ptr<_Tp>::element_type _ET;
3515 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3517}
3518
3519template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003520shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003521const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522{
zoecarverd73f42a2020-05-11 18:42:50 -07003523 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003524 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525}
3526
zoecarverd73f42a2020-05-11 18:42:50 -07003527template<class _Tp, class _Up>
3528shared_ptr<_Tp>
3529reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3530{
3531 return shared_ptr<_Tp>(__r,
3532 reinterpret_cast<
3533 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3534}
3535
Howard Hinnant72f73582010-08-11 17:04:31 +00003536#ifndef _LIBCPP_NO_RTTI
3537
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538template<class _Dp, class _Tp>
3539inline _LIBCPP_INLINE_VISIBILITY
3540_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003541get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542{
3543 return __p.template __get_deleter<_Dp>();
3544}
3545
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003546#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003547
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003549class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003551public:
3552 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553private:
3554 element_type* __ptr_;
3555 __shared_weak_count* __cntrl_;
3556
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003557public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003559 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003560 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003561 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3562 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003564 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003565 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003566 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3567 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003568
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003570 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003571 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003572 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3573 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003574 ~weak_ptr();
3575
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003577 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003578 template<class _Yp>
3579 typename enable_if
3580 <
3581 is_convertible<_Yp*, element_type*>::value,
3582 weak_ptr&
3583 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003585 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3586
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003588 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3589 template<class _Yp>
3590 typename enable_if
3591 <
3592 is_convertible<_Yp*, element_type*>::value,
3593 weak_ptr&
3594 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003596 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3597
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003598 template<class _Yp>
3599 typename enable_if
3600 <
3601 is_convertible<_Yp*, element_type*>::value,
3602 weak_ptr&
3603 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003605 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003606
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003608 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003610 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003611
Howard Hinnant756c69b2010-09-22 16:48:34 +00003612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003613 long use_count() const _NOEXCEPT
3614 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003616 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003617 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003618 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003619 template<class _Up>
3620 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003621 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003623 template<class _Up>
3624 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003625 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626 {return __cntrl_ < __r.__cntrl_;}
3627
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003628 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3629 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630};
3631
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003632#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3633template<class _Tp>
3634weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3635#endif
3636
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003638inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003639_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003640weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003641 : __ptr_(nullptr),
3642 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643{
3644}
3645
3646template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003647inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003648weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649 : __ptr_(__r.__ptr_),
3650 __cntrl_(__r.__cntrl_)
3651{
3652 if (__cntrl_)
3653 __cntrl_->__add_weak();
3654}
3655
3656template<class _Tp>
3657template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003658inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003660 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003661 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662 : __ptr_(__r.__ptr_),
3663 __cntrl_(__r.__cntrl_)
3664{
3665 if (__cntrl_)
3666 __cntrl_->__add_weak();
3667}
3668
3669template<class _Tp>
3670template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003671inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003673 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003674 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675 : __ptr_(__r.__ptr_),
3676 __cntrl_(__r.__cntrl_)
3677{
3678 if (__cntrl_)
3679 __cntrl_->__add_weak();
3680}
3681
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003682template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003683inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003684weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3685 : __ptr_(__r.__ptr_),
3686 __cntrl_(__r.__cntrl_)
3687{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003688 __r.__ptr_ = nullptr;
3689 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003690}
3691
3692template<class _Tp>
3693template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003694inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003695weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3696 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3697 _NOEXCEPT
3698 : __ptr_(__r.__ptr_),
3699 __cntrl_(__r.__cntrl_)
3700{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003701 __r.__ptr_ = nullptr;
3702 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003703}
3704
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705template<class _Tp>
3706weak_ptr<_Tp>::~weak_ptr()
3707{
3708 if (__cntrl_)
3709 __cntrl_->__release_weak();
3710}
3711
3712template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003713inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003715weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716{
3717 weak_ptr(__r).swap(*this);
3718 return *this;
3719}
3720
3721template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003722template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003723inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003724typename enable_if
3725<
3726 is_convertible<_Yp*, _Tp*>::value,
3727 weak_ptr<_Tp>&
3728>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003729weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730{
3731 weak_ptr(__r).swap(*this);
3732 return *this;
3733}
3734
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003735template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003736inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003737weak_ptr<_Tp>&
3738weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3739{
3740 weak_ptr(_VSTD::move(__r)).swap(*this);
3741 return *this;
3742}
3743
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003745template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003746inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003747typename enable_if
3748<
3749 is_convertible<_Yp*, _Tp*>::value,
3750 weak_ptr<_Tp>&
3751>::type
3752weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3753{
3754 weak_ptr(_VSTD::move(__r)).swap(*this);
3755 return *this;
3756}
3757
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003758template<class _Tp>
3759template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003760inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003761typename enable_if
3762<
3763 is_convertible<_Yp*, _Tp*>::value,
3764 weak_ptr<_Tp>&
3765>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003766weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767{
3768 weak_ptr(__r).swap(*this);
3769 return *this;
3770}
3771
3772template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003773inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774void
Howard Hinnant719bda32011-05-28 14:41:13 +00003775weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003777 _VSTD::swap(__ptr_, __r.__ptr_);
3778 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779}
3780
3781template<class _Tp>
3782inline _LIBCPP_INLINE_VISIBILITY
3783void
Howard Hinnant719bda32011-05-28 14:41:13 +00003784swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785{
3786 __x.swap(__y);
3787}
3788
3789template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003790inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791void
Howard Hinnant719bda32011-05-28 14:41:13 +00003792weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793{
3794 weak_ptr().swap(*this);
3795}
3796
3797template<class _Tp>
3798template<class _Yp>
3799shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003800 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801 : __ptr_(__r.__ptr_),
3802 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3803{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003804 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003805 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806}
3807
3808template<class _Tp>
3809shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003810weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811{
3812 shared_ptr<_Tp> __r;
3813 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3814 if (__r.__cntrl_)
3815 __r.__ptr_ = __ptr_;
3816 return __r;
3817}
3818
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003819#if _LIBCPP_STD_VER > 14
3820template <class _Tp = void> struct owner_less;
3821#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003822template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003823#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824
3825template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003826struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003828{
3829 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003830 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003831 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003833 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003834 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003836 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003837 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003839};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840
3841template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003842struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3844{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003845 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003846 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003847 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003849 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003850 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003852 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003853 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854 {return __x.owner_before(__y);}
3855};
3856
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003857#if _LIBCPP_STD_VER > 14
3858template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003859struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003860{
3861 template <class _Tp, class _Up>
3862 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003863 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003864 {return __x.owner_before(__y);}
3865 template <class _Tp, class _Up>
3866 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003867 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003868 {return __x.owner_before(__y);}
3869 template <class _Tp, class _Up>
3870 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003871 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003872 {return __x.owner_before(__y);}
3873 template <class _Tp, class _Up>
3874 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003875 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003876 {return __x.owner_before(__y);}
3877 typedef void is_transparent;
3878};
3879#endif
3880
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003882class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003883{
3884 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003885protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003886 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003887 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003889 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003891 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3892 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003895public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003897 shared_ptr<_Tp> shared_from_this()
3898 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003900 shared_ptr<_Tp const> shared_from_this() const
3901 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902
Eric Fiselier84006862016-06-02 00:15:35 +00003903#if _LIBCPP_STD_VER > 14
3904 _LIBCPP_INLINE_VISIBILITY
3905 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3906 { return __weak_this_; }
3907
3908 _LIBCPP_INLINE_VISIBILITY
3909 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3910 { return __weak_this_; }
3911#endif // _LIBCPP_STD_VER > 14
3912
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913 template <class _Up> friend class shared_ptr;
3914};
3915
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003916template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003917struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003918{
3919 typedef shared_ptr<_Tp> argument_type;
3920 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003921
Howard Hinnant756c69b2010-09-22 16:48:34 +00003922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003923 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003924 {
zoecarverd73f42a2020-05-11 18:42:50 -07003925 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003926 }
3927};
3928
Howard Hinnantc834c512011-11-29 18:15:50 +00003929template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003930inline _LIBCPP_INLINE_VISIBILITY
3931basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003932operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003933
Eric Fiselier9b492672016-06-18 02:12:53 +00003934
3935#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003936
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003937class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003938{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003939 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003940public:
3941 void lock() _NOEXCEPT;
3942 void unlock() _NOEXCEPT;
3943
3944private:
3945 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3946 __sp_mut(const __sp_mut&);
3947 __sp_mut& operator=(const __sp_mut&);
3948
Howard Hinnant8331b762013-03-06 23:30:19 +00003949 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003950};
3951
Mehdi Amini228053d2017-05-04 17:08:54 +00003952_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3953__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003954
3955template <class _Tp>
3956inline _LIBCPP_INLINE_VISIBILITY
3957bool
3958atomic_is_lock_free(const shared_ptr<_Tp>*)
3959{
3960 return false;
3961}
3962
3963template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003964_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003965shared_ptr<_Tp>
3966atomic_load(const shared_ptr<_Tp>* __p)
3967{
3968 __sp_mut& __m = __get_sp_mut(__p);
3969 __m.lock();
3970 shared_ptr<_Tp> __q = *__p;
3971 __m.unlock();
3972 return __q;
3973}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003974
Howard Hinnant9fa30202012-07-30 01:40:57 +00003975template <class _Tp>
3976inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003977_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003978shared_ptr<_Tp>
3979atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3980{
3981 return atomic_load(__p);
3982}
3983
3984template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003985_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003986void
3987atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3988{
3989 __sp_mut& __m = __get_sp_mut(__p);
3990 __m.lock();
3991 __p->swap(__r);
3992 __m.unlock();
3993}
3994
3995template <class _Tp>
3996inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003997_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003998void
3999atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4000{
4001 atomic_store(__p, __r);
4002}
4003
4004template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004005_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004006shared_ptr<_Tp>
4007atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4008{
4009 __sp_mut& __m = __get_sp_mut(__p);
4010 __m.lock();
4011 __p->swap(__r);
4012 __m.unlock();
4013 return __r;
4014}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004015
Howard Hinnant9fa30202012-07-30 01:40:57 +00004016template <class _Tp>
4017inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004018_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004019shared_ptr<_Tp>
4020atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4021{
4022 return atomic_exchange(__p, __r);
4023}
4024
4025template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004026_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004027bool
4028atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4029{
Marshall Clow4201ee82016-05-18 17:50:13 +00004030 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004031 __sp_mut& __m = __get_sp_mut(__p);
4032 __m.lock();
4033 if (__p->__owner_equivalent(*__v))
4034 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004035 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004036 *__p = __w;
4037 __m.unlock();
4038 return true;
4039 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004040 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004041 *__v = *__p;
4042 __m.unlock();
4043 return false;
4044}
4045
4046template <class _Tp>
4047inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004048_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004049bool
4050atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4051{
4052 return atomic_compare_exchange_strong(__p, __v, __w);
4053}
4054
4055template <class _Tp>
4056inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004057_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004058bool
4059atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4060 shared_ptr<_Tp> __w, memory_order, memory_order)
4061{
4062 return atomic_compare_exchange_strong(__p, __v, __w);
4063}
4064
4065template <class _Tp>
4066inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004067_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004068bool
4069atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4070 shared_ptr<_Tp> __w, memory_order, memory_order)
4071{
4072 return atomic_compare_exchange_weak(__p, __v, __w);
4073}
4074
Eric Fiselier9b492672016-06-18 02:12:53 +00004075#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004076
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004077//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004078#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4079# ifndef _LIBCPP_CXX03_LANG
4080enum class pointer_safety : unsigned char {
4081 relaxed,
4082 preferred,
4083 strict
4084};
4085# endif
4086#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004087struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004089 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090 {
4091 relaxed,
4092 preferred,
4093 strict
4094 };
4095
Howard Hinnant49e145e2012-10-30 19:06:59 +00004096 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097
Howard Hinnant756c69b2010-09-22 16:48:34 +00004098 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004099 pointer_safety() : __v_() {}
4100
4101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004102 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104 operator int() const {return __v_;}
4105};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004106#endif
4107
4108#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004109 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004110_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4111#else
4112// This function is only offered in C++03 under ABI v1.
4113# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4114inline _LIBCPP_INLINE_VISIBILITY
4115pointer_safety get_pointer_safety() _NOEXCEPT {
4116 return pointer_safety::relaxed;
4117}
4118# endif
4119#endif
4120
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004122_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4123_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4124_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004125_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126
4127template <class _Tp>
4128inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004129_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130undeclare_reachable(_Tp* __p)
4131{
4132 return static_cast<_Tp*>(__undeclare_reachable(__p));
4133}
4134
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004135_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136
Marshall Clow8982dcd2015-07-13 20:04:56 +00004137// --- Helper for container swap --
4138template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004139_LIBCPP_INLINE_VISIBILITY
4140void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4141#if _LIBCPP_STD_VER >= 14
4142 _NOEXCEPT
4143#else
4144 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4145#endif
4146{
4147 using _VSTD::swap;
4148 swap(__a1, __a2);
4149}
4150
4151template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004152inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004153void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4154
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004155template <typename _Alloc>
4156inline _LIBCPP_INLINE_VISIBILITY
4157void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4158#if _LIBCPP_STD_VER >= 14
4159 _NOEXCEPT
4160#else
4161 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4162#endif
4163{
4164 _VSTD::__swap_allocator(__a1, __a2,
4165 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4166}
4167
Marshall Clowff91de82015-08-18 19:51:37 +00004168template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004169struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004170 _Traits::propagate_on_container_move_assignment::value
4171#if _LIBCPP_STD_VER > 14
4172 || _Traits::is_always_equal::value
4173#else
4174 && is_nothrow_move_assignable<_Alloc>::value
4175#endif
4176 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004177
Marshall Clowa591b9a2016-07-11 21:38:08 +00004178
Marshall Clowa591b9a2016-07-11 21:38:08 +00004179template <class _Tp, class _Alloc>
4180struct __temp_value {
4181 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004182
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004183 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004184 _Alloc &__a;
4185
4186 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4187 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004188
Marshall Clowa591b9a2016-07-11 21:38:08 +00004189 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004190 _LIBCPP_NO_CFI
4191 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4192 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4193 _VSTD::forward<_Args>(__args)...);
4194 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004195
Marshall Clowa591b9a2016-07-11 21:38:08 +00004196 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4197 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004198
Marshall Clowe46031a2018-07-02 18:41:15 +00004199template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004200struct __is_allocator : false_type {};
4201
4202template<typename _Alloc>
4203struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004204 typename __void_t<typename _Alloc::value_type>::type,
4205 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4206 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004207 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004208
Eric Fiselier74ebee62019-06-08 01:31:19 +00004209// __builtin_new_allocator -- A non-templated helper for allocating and
4210// deallocating memory using __builtin_operator_new and
4211// __builtin_operator_delete. It should be used in preference to
4212// `std::allocator<T>` to avoid additional instantiations.
4213struct __builtin_new_allocator {
4214 struct __builtin_new_deleter {
4215 typedef void* pointer_type;
4216
4217 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4218 : __size_(__size), __align_(__align) {}
4219
4220 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004221 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004222 }
4223
4224 private:
4225 size_t __size_;
4226 size_t __align_;
4227 };
4228
4229 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4230
4231 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004232 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004233 __builtin_new_deleter(__s, __align));
4234 }
4235
4236 static void __deallocate_bytes(void* __p, size_t __s,
4237 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004238 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004239 }
4240
4241 template <class _Tp>
4242 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4243 static __holder_t __allocate_type(size_t __n) {
4244 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4245 }
4246
4247 template <class _Tp>
4248 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4249 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4250 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4251 }
4252};
4253
4254
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255_LIBCPP_END_NAMESPACE_STD
4256
Eric Fiselierf4433a32017-05-31 22:07:49 +00004257_LIBCPP_POP_MACROS
4258
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004259#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004260# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004261#endif
4262
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263#endif // _LIBCPP_MEMORY