blob: 1b48573eb2138305a8e4e2c263b17dd7db86a46b [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>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676#include <iterator>
677#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000678#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000679#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000680#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000681#include <cstring>
Louis Dionne1e38faa2021-04-09 12:48:34 -0400682#include <__memory/allocator.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500683#include <__memory/allocator_traits.h>
Louis Dionne26bc98e2021-04-09 12:44:26 -0400684#include <__memory/auto_ptr.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500685#include <__memory/base.h>
686#include <__memory/pointer_traits.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500687#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000688#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000689# include <atomic>
690#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000691#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000692
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000693#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000695#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696
Eric Fiselierf4433a32017-05-31 22:07:49 +0000697_LIBCPP_PUSH_MACROS
698#include <__undef_macros>
699
700
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701_LIBCPP_BEGIN_NAMESPACE_STD
702
Eric Fiselier89659d12015-07-07 00:27:16 +0000703template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000704inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000705_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
706#if !defined(_LIBCPP_HAS_NO_THREADS) && \
707 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000708 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000709 return __atomic_load_n(__value, __ATOMIC_RELAXED);
710#else
711 return *__value;
712#endif
713}
714
Kuba Breckade9d6792016-09-04 09:55:12 +0000715template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000716inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000717_ValueType __libcpp_acquire_load(_ValueType const* __value) {
718#if !defined(_LIBCPP_HAS_NO_THREADS) && \
719 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000720 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000721 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
722#else
723 return *__value;
724#endif
725}
726
Louis Dionned6651542020-11-03 12:05:55 -0500727template <class _Alloc, class _Ptr>
728_LIBCPP_INLINE_VISIBILITY
729void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
730 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500731 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500732 typedef allocator_traits<_Alloc> _Traits;
733 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
734 _Traits::construct(__a, _VSTD::__to_address(__begin2),
735#ifdef _LIBCPP_NO_EXCEPTIONS
736 _VSTD::move(*__begin1)
737#else
738 _VSTD::move_if_noexcept(*__begin1)
739#endif
740 );
741 }
742}
743
744template <class _Alloc, class _Tp, typename enable_if<
745 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
746 is_trivially_move_constructible<_Tp>::value
747>::type>
748_LIBCPP_INLINE_VISIBILITY
749void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
750 ptrdiff_t _Np = __end1 - __begin1;
751 if (_Np > 0) {
752 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
753 __begin2 += _Np;
754 }
755}
756
757template <class _Alloc, class _Iter, class _Ptr>
758_LIBCPP_INLINE_VISIBILITY
759void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
760 typedef allocator_traits<_Alloc> _Traits;
761 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
762 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
763 }
764}
765
766template <class _Alloc, class _Source, class _Dest,
767 class _RawSource = typename remove_const<_Source>::type,
768 class _RawDest = typename remove_const<_Dest>::type,
769 class =
770 typename enable_if<
771 is_trivially_copy_constructible<_Dest>::value &&
772 is_same<_RawSource, _RawDest>::value &&
773 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
774 >::type>
775_LIBCPP_INLINE_VISIBILITY
776void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
777 ptrdiff_t _Np = __end1 - __begin1;
778 if (_Np > 0) {
779 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
780 __begin2 += _Np;
781 }
782}
783
784template <class _Alloc, class _Ptr>
785_LIBCPP_INLINE_VISIBILITY
786void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
787 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
788 "The specified type does not meet the requirements of Cpp17MoveInsertable");
789 typedef allocator_traits<_Alloc> _Traits;
790 while (__end1 != __begin1) {
791 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
792#ifdef _LIBCPP_NO_EXCEPTIONS
793 _VSTD::move(*--__end1)
794#else
795 _VSTD::move_if_noexcept(*--__end1)
796#endif
797 );
798 --__end2;
799 }
800}
801
802template <class _Alloc, class _Tp, class = typename enable_if<
803 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
804 is_trivially_move_constructible<_Tp>::value
805>::type>
806_LIBCPP_INLINE_VISIBILITY
807void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
808 ptrdiff_t _Np = __end1 - __begin1;
809 __end2 -= _Np;
810 if (_Np > 0)
811 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
812}
813
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000815class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 : public iterator<output_iterator_tag,
817 _Tp, // purposefully not C++03
818 ptrdiff_t, // purposefully not C++03
819 _Tp*, // purposefully not C++03
820 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
821{
822private:
823 _OutputIterator __x_;
824public:
825 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
826 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
827 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500828 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +0000829#if _LIBCPP_STD_VER >= 14
830 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500831 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +0000832#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
834 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
835 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +0000836#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +0000837 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +0000838#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839};
840
841template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000842_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +0000843pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +0000844get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845{
846 pair<_Tp*, ptrdiff_t> __r(0, 0);
847 const ptrdiff_t __m = (~ptrdiff_t(0) ^
848 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
849 / sizeof(_Tp);
850 if (__n > __m)
851 __n = __m;
852 while (__n > 0)
853 {
Eric Fiselier6a07b342018-10-26 17:12:32 +0000854#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +0000855 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +0000856 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500857 align_val_t __al =
858 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +0000859 __r.first = static_cast<_Tp*>(::operator new(
860 __n * sizeof(_Tp), __al, nothrow));
861 } else {
862 __r.first = static_cast<_Tp*>(::operator new(
863 __n * sizeof(_Tp), nothrow));
864 }
865#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +0000866 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +0000867 {
868 // Since aligned operator new is unavailable, return an empty
869 // buffer rather than one with invalid alignment.
870 return __r;
871 }
872
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +0000874#endif
875
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876 if (__r.first)
877 {
878 __r.second = __n;
879 break;
880 }
881 __n /= 2;
882 }
883 return __r;
884}
885
886template <class _Tp>
887inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +0000888void return_temporary_buffer(_Tp* __p) _NOEXCEPT
889{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +0000890 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +0000891}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892
Eric Fiselier15eae3b2019-12-16 17:00:10 -0500893// Tag used to default initialize one or both of the pair's elements.
894struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500895struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -0500896
Eric Fiselier9d355982017-04-12 23:45:53 +0000897template <class _Tp, int _Idx,
898 bool _CanBeEmptyBase =
899 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
900struct __compressed_pair_elem {
901 typedef _Tp _ParamT;
902 typedef _Tp& reference;
903 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500905 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -0500906 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500907 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
908 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909
Eric Fiselier9d355982017-04-12 23:45:53 +0000910 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +0000911 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
912 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +0000913 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500914 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +0000915 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +0000916 : __value_(_VSTD::forward<_Up>(__u))
917 {
918 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500920
921#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +0000922 template <class... _Args, size_t... _Indexes>
923 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
924 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
925 __tuple_indices<_Indexes...>)
926 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +0000927#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500929
Alex Lorenz76132112017-11-09 17:54:49 +0000930 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
931 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +0000932 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934private:
Eric Fiselier9d355982017-04-12 23:45:53 +0000935 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936};
937
Eric Fiselier9d355982017-04-12 23:45:53 +0000938template <class _Tp, int _Idx>
939struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
940 typedef _Tp _ParamT;
941 typedef _Tp& reference;
942 typedef const _Tp& const_reference;
943 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500945 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
946 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
947 __compressed_pair_elem(__default_init_tag) {}
948 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
949 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
Eric Fiselier9d355982017-04-12 23:45:53 +0000951 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +0000952 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
953 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +0000954 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500955 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +0000956 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +0000957 : __value_type(_VSTD::forward<_Up>(__u))
958 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500960#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +0000961 template <class... _Args, size_t... _Indexes>
962 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
963 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
964 __tuple_indices<_Indexes...>)
965 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +0000966#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967
Alex Lorenz76132112017-11-09 17:54:49 +0000968 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
969 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +0000970 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971};
972
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +0000974class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
975 private __compressed_pair_elem<_T2, 1> {
Louis Dionneda463cb2020-12-11 12:22:16 -0500976public:
Eric Fiselier9d355982017-04-12 23:45:53 +0000977 // NOTE: This static assert should never fire because __compressed_pair
978 // is *almost never* used in a scenario where it's possible for T1 == T2.
979 // (The exception is std::function where it is possible that the function
980 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +0000981 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -0500982 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +0000983 "The current implementation is NOT ABI-compatible with the previous "
984 "implementation for this configuration");
985
Louis Dionneda463cb2020-12-11 12:22:16 -0500986 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
987 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
988
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500989 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +0000990 class = typename enable_if<
991 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
992 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
993 >::type
994 >
Eric Fiselier9d355982017-04-12 23:45:53 +0000995 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500996 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997
Eric Fiselier9d355982017-04-12 23:45:53 +0000998 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500999 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00001000 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001001 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001003#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001004 template <class... _Args1, class... _Args2>
1005 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1006 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1007 tuple<_Args2...> __second_args)
1008 : _Base1(__pc, _VSTD::move(__first_args),
1009 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1010 _Base2(__pc, _VSTD::move(__second_args),
1011 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001012#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013
Eric Fiselier9d355982017-04-12 23:45:53 +00001014 _LIBCPP_INLINE_VISIBILITY
1015 typename _Base1::reference first() _NOEXCEPT {
1016 return static_cast<_Base1&>(*this).__get();
1017 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018
Eric Fiselier9d355982017-04-12 23:45:53 +00001019 _LIBCPP_INLINE_VISIBILITY
1020 typename _Base1::const_reference first() const _NOEXCEPT {
1021 return static_cast<_Base1 const&>(*this).__get();
1022 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023
Eric Fiselier9d355982017-04-12 23:45:53 +00001024 _LIBCPP_INLINE_VISIBILITY
1025 typename _Base2::reference second() _NOEXCEPT {
1026 return static_cast<_Base2&>(*this).__get();
1027 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028
Eric Fiselier9d355982017-04-12 23:45:53 +00001029 _LIBCPP_INLINE_VISIBILITY
1030 typename _Base2::const_reference second() const _NOEXCEPT {
1031 return static_cast<_Base2 const&>(*this).__get();
1032 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033
Louis Dionneda463cb2020-12-11 12:22:16 -05001034 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1035 static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
1036 return static_cast<_Base1*>(__pair);
1037 }
1038 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1039 static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
1040 return static_cast<_Base2*>(__pair);
1041 }
1042
Eric Fiselier9d355982017-04-12 23:45:53 +00001043 _LIBCPP_INLINE_VISIBILITY
1044 void swap(__compressed_pair& __x)
1045 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1046 __is_nothrow_swappable<_T2>::value)
1047 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001048 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00001049 swap(first(), __x.first());
1050 swap(second(), __x.second());
1051 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052};
1053
1054template <class _T1, class _T2>
1055inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001056void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1057 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1058 __is_nothrow_swappable<_T2>::value) {
1059 __x.swap(__y);
1060}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001062// default_delete
1063
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001065struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001066 static_assert(!is_function<_Tp>::value,
1067 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001068#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001069 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001070#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001071 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001072#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001073 template <class _Up>
1074 _LIBCPP_INLINE_VISIBILITY
1075 default_delete(const default_delete<_Up>&,
1076 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1077 0) _NOEXCEPT {}
1078
1079 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1080 static_assert(sizeof(_Tp) > 0,
1081 "default_delete can not delete incomplete type");
1082 static_assert(!is_void<_Tp>::value,
1083 "default_delete can not delete incomplete type");
1084 delete __ptr;
1085 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086};
1087
1088template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001089struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1090private:
1091 template <class _Up>
1092 struct _EnableIfConvertible
1093 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1094
Howard Hinnant4500ca52011-12-18 21:19:44 +00001095public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001096#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001097 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001098#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001099 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001100#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001101
1102 template <class _Up>
1103 _LIBCPP_INLINE_VISIBILITY
1104 default_delete(const default_delete<_Up[]>&,
1105 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1106
1107 template <class _Up>
1108 _LIBCPP_INLINE_VISIBILITY
1109 typename _EnableIfConvertible<_Up>::type
1110 operator()(_Up* __ptr) const _NOEXCEPT {
1111 static_assert(sizeof(_Tp) > 0,
1112 "default_delete can not delete incomplete type");
1113 static_assert(!is_void<_Tp>::value,
1114 "default_delete can not delete void type");
1115 delete[] __ptr;
1116 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117};
1118
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001119template <class _Deleter>
1120struct __unique_ptr_deleter_sfinae {
1121 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1122 typedef const _Deleter& __lval_ref_type;
1123 typedef _Deleter&& __good_rval_ref_type;
1124 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125};
1126
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001127template <class _Deleter>
1128struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1129 typedef const _Deleter& __lval_ref_type;
1130 typedef const _Deleter&& __bad_rval_ref_type;
1131 typedef false_type __enable_rval_overload;
1132};
1133
1134template <class _Deleter>
1135struct __unique_ptr_deleter_sfinae<_Deleter&> {
1136 typedef _Deleter& __lval_ref_type;
1137 typedef _Deleter&& __bad_rval_ref_type;
1138 typedef false_type __enable_rval_overload;
1139};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001140
Vy Nguyene369bd92020-07-13 12:34:37 -04001141#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1142# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1143#else
1144# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1145#endif
1146
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001147template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001148class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001149public:
1150 typedef _Tp element_type;
1151 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001152 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001153
1154 static_assert(!is_rvalue_reference<deleter_type>::value,
1155 "the specified deleter type cannot be an rvalue reference");
1156
1157private:
1158 __compressed_pair<pointer, deleter_type> __ptr_;
1159
1160 struct __nat { int __for_bool_; };
1161
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001162 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001163
1164 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001165 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001166 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001167
1168 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001169 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001170 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001171
1172 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001173 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001174 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001175
1176 template <bool _Dummy, class _Deleter = typename __dependent_type<
1177 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001178 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001179 typename enable_if<is_default_constructible<_Deleter>::value &&
1180 !is_pointer<_Deleter>::value>::type;
1181
1182 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001183 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001184 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1185
1186 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001187 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001188 is_convertible<typename _UPtr::pointer, pointer>::value &&
1189 !is_array<_Up>::value
1190 >::type;
1191
1192 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001193 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001194 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1195 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1196 >::type;
1197
1198 template <class _UDel>
1199 using _EnableIfDeleterAssignable = typename enable_if<
1200 is_assignable<_Dp&, _UDel&&>::value
1201 >::type;
1202
1203public:
1204 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001205 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001206 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001207 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001208
1209 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001210 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001211 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001212 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001213
1214 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001215 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001216 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001217 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001218
1219 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001220 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001221 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001222 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001223 : __ptr_(__p, __d) {}
1224
1225 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001226 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001227 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001228 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001229 : __ptr_(__p, _VSTD::move(__d)) {
1230 static_assert(!is_reference<deleter_type>::value,
1231 "rvalue deleter bound to reference");
1232 }
1233
1234 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001235 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001236 _LIBCPP_INLINE_VISIBILITY
1237 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1238
1239 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001240 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001241 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1242 }
1243
1244 template <class _Up, class _Ep,
1245 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1246 class = _EnableIfDeleterConvertible<_Ep>
1247 >
1248 _LIBCPP_INLINE_VISIBILITY
1249 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1250 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1251
1252#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1253 template <class _Up>
1254 _LIBCPP_INLINE_VISIBILITY
1255 unique_ptr(auto_ptr<_Up>&& __p,
1256 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001257 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001258 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001259 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001260#endif
1261
1262 _LIBCPP_INLINE_VISIBILITY
1263 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1264 reset(__u.release());
1265 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1266 return *this;
1267 }
1268
1269 template <class _Up, class _Ep,
1270 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1271 class = _EnableIfDeleterAssignable<_Ep>
1272 >
1273 _LIBCPP_INLINE_VISIBILITY
1274 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1275 reset(__u.release());
1276 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1277 return *this;
1278 }
1279
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001280#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1281 template <class _Up>
1282 _LIBCPP_INLINE_VISIBILITY
1283 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1284 is_same<_Dp, default_delete<_Tp> >::value,
1285 unique_ptr&>::type
1286 operator=(auto_ptr<_Up> __p) {
1287 reset(__p.release());
1288 return *this;
1289 }
1290#endif
1291
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001292#ifdef _LIBCPP_CXX03_LANG
1293 unique_ptr(unique_ptr const&) = delete;
1294 unique_ptr& operator=(unique_ptr const&) = delete;
1295#endif
1296
1297
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001298 _LIBCPP_INLINE_VISIBILITY
1299 ~unique_ptr() { reset(); }
1300
1301 _LIBCPP_INLINE_VISIBILITY
1302 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1303 reset();
1304 return *this;
1305 }
1306
1307 _LIBCPP_INLINE_VISIBILITY
1308 typename add_lvalue_reference<_Tp>::type
1309 operator*() const {
1310 return *__ptr_.first();
1311 }
1312 _LIBCPP_INLINE_VISIBILITY
1313 pointer operator->() const _NOEXCEPT {
1314 return __ptr_.first();
1315 }
1316 _LIBCPP_INLINE_VISIBILITY
1317 pointer get() const _NOEXCEPT {
1318 return __ptr_.first();
1319 }
1320 _LIBCPP_INLINE_VISIBILITY
1321 deleter_type& get_deleter() _NOEXCEPT {
1322 return __ptr_.second();
1323 }
1324 _LIBCPP_INLINE_VISIBILITY
1325 const deleter_type& get_deleter() const _NOEXCEPT {
1326 return __ptr_.second();
1327 }
1328 _LIBCPP_INLINE_VISIBILITY
1329 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1330 return __ptr_.first() != nullptr;
1331 }
1332
1333 _LIBCPP_INLINE_VISIBILITY
1334 pointer release() _NOEXCEPT {
1335 pointer __t = __ptr_.first();
1336 __ptr_.first() = pointer();
1337 return __t;
1338 }
1339
1340 _LIBCPP_INLINE_VISIBILITY
1341 void reset(pointer __p = pointer()) _NOEXCEPT {
1342 pointer __tmp = __ptr_.first();
1343 __ptr_.first() = __p;
1344 if (__tmp)
1345 __ptr_.second()(__tmp);
1346 }
1347
1348 _LIBCPP_INLINE_VISIBILITY
1349 void swap(unique_ptr& __u) _NOEXCEPT {
1350 __ptr_.swap(__u.__ptr_);
1351 }
1352};
1353
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001354
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001356class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001358 typedef _Tp element_type;
1359 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001360 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001361
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001363 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364
Eric Fiselier31127cd2017-04-16 02:14:31 +00001365 template <class _From>
1366 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367
Eric Fiselier31127cd2017-04-16 02:14:31 +00001368 template <class _FromElem>
1369 struct _CheckArrayPointerConversion<_FromElem*>
1370 : integral_constant<bool,
1371 is_same<_FromElem*, pointer>::value ||
1372 (is_same<pointer, element_type*>::value &&
1373 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1374 >
1375 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376
Eric Fiselier31127cd2017-04-16 02:14:31 +00001377 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001378
1379 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001380 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001381 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001382
1383 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001384 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001385 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001386
1387 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001388 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001389 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001390
1391 template <bool _Dummy, class _Deleter = typename __dependent_type<
1392 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001393 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001394 typename enable_if<is_default_constructible<_Deleter>::value &&
1395 !is_pointer<_Deleter>::value>::type;
1396
1397 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001398 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001399 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1400
1401 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001402 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001403 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001404 >::type;
1405
1406 template <class _UPtr, class _Up,
1407 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001408 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001409 is_array<_Up>::value &&
1410 is_same<pointer, element_type*>::value &&
1411 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1412 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1413 >::type;
1414
1415 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001416 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001417 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1418 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1419 >::type;
1420
1421 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001422 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001423 is_assignable<_Dp&, _UDel&&>::value
1424 >::type;
1425
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001427 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001428 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001429 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001430 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001432 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001433 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001434 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001435 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001437 template <class _Pp, bool _Dummy = true,
1438 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001439 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001440 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001441 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001442 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001444 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001445 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1446 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001447 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001448 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001449 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001451 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001452 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001453 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001454 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001455 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001457 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001458 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1459 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001460 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001461 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001462 : __ptr_(__p, _VSTD::move(__d)) {
1463 static_assert(!is_reference<deleter_type>::value,
1464 "rvalue deleter bound to reference");
1465 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001467 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001468 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001469 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001470 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001471 : __ptr_(nullptr, _VSTD::move(__d)) {
1472 static_assert(!is_reference<deleter_type>::value,
1473 "rvalue deleter bound to reference");
1474 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001475
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001476 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001477 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1478 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001479 _LIBCPP_INLINE_VISIBILITY
1480 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001481
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001482 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001483 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001484 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1485 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001486
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001487 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001488 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001489 reset(__u.release());
1490 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1491 return *this;
1492 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001494 template <class _Up, class _Ep,
1495 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1496 class = _EnableIfDeleterConvertible<_Ep>
1497 >
1498 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001499 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001500 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001501 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001503 template <class _Up, class _Ep,
1504 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1505 class = _EnableIfDeleterAssignable<_Ep>
1506 >
1507 _LIBCPP_INLINE_VISIBILITY
1508 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001509 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001510 reset(__u.release());
1511 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1512 return *this;
1513 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001515#ifdef _LIBCPP_CXX03_LANG
1516 unique_ptr(unique_ptr const&) = delete;
1517 unique_ptr& operator=(unique_ptr const&) = delete;
1518#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001519
1520public:
1521 _LIBCPP_INLINE_VISIBILITY
1522 ~unique_ptr() { reset(); }
1523
1524 _LIBCPP_INLINE_VISIBILITY
1525 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1526 reset();
1527 return *this;
1528 }
1529
1530 _LIBCPP_INLINE_VISIBILITY
1531 typename add_lvalue_reference<_Tp>::type
1532 operator[](size_t __i) const {
1533 return __ptr_.first()[__i];
1534 }
1535 _LIBCPP_INLINE_VISIBILITY
1536 pointer get() const _NOEXCEPT {
1537 return __ptr_.first();
1538 }
1539
1540 _LIBCPP_INLINE_VISIBILITY
1541 deleter_type& get_deleter() _NOEXCEPT {
1542 return __ptr_.second();
1543 }
1544
1545 _LIBCPP_INLINE_VISIBILITY
1546 const deleter_type& get_deleter() const _NOEXCEPT {
1547 return __ptr_.second();
1548 }
1549 _LIBCPP_INLINE_VISIBILITY
1550 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1551 return __ptr_.first() != nullptr;
1552 }
1553
1554 _LIBCPP_INLINE_VISIBILITY
1555 pointer release() _NOEXCEPT {
1556 pointer __t = __ptr_.first();
1557 __ptr_.first() = pointer();
1558 return __t;
1559 }
1560
1561 template <class _Pp>
1562 _LIBCPP_INLINE_VISIBILITY
1563 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001564 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001565 >::type
1566 reset(_Pp __p) _NOEXCEPT {
1567 pointer __tmp = __ptr_.first();
1568 __ptr_.first() = __p;
1569 if (__tmp)
1570 __ptr_.second()(__tmp);
1571 }
1572
1573 _LIBCPP_INLINE_VISIBILITY
1574 void reset(nullptr_t = nullptr) _NOEXCEPT {
1575 pointer __tmp = __ptr_.first();
1576 __ptr_.first() = nullptr;
1577 if (__tmp)
1578 __ptr_.second()(__tmp);
1579 }
1580
1581 _LIBCPP_INLINE_VISIBILITY
1582 void swap(unique_ptr& __u) _NOEXCEPT {
1583 __ptr_.swap(__u.__ptr_);
1584 }
1585
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586};
1587
1588template <class _Tp, class _Dp>
1589inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001590typename enable_if<
1591 __is_swappable<_Dp>::value,
1592 void
1593>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001594swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595
1596template <class _T1, class _D1, class _T2, class _D2>
1597inline _LIBCPP_INLINE_VISIBILITY
1598bool
1599operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1600
1601template <class _T1, class _D1, class _T2, class _D2>
1602inline _LIBCPP_INLINE_VISIBILITY
1603bool
1604operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1605
1606template <class _T1, class _D1, class _T2, class _D2>
1607inline _LIBCPP_INLINE_VISIBILITY
1608bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001609operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1610{
1611 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1612 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001613 typedef typename common_type<_P1, _P2>::type _Vp;
1614 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001615}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616
1617template <class _T1, class _D1, class _T2, class _D2>
1618inline _LIBCPP_INLINE_VISIBILITY
1619bool
1620operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1621
1622template <class _T1, class _D1, class _T2, class _D2>
1623inline _LIBCPP_INLINE_VISIBILITY
1624bool
1625operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1626
1627template <class _T1, class _D1, class _T2, class _D2>
1628inline _LIBCPP_INLINE_VISIBILITY
1629bool
1630operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1631
Howard Hinnantb17caf92012-02-21 21:02:58 +00001632template <class _T1, class _D1>
1633inline _LIBCPP_INLINE_VISIBILITY
1634bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001635operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001636{
1637 return !__x;
1638}
1639
1640template <class _T1, class _D1>
1641inline _LIBCPP_INLINE_VISIBILITY
1642bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001643operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001644{
1645 return !__x;
1646}
1647
1648template <class _T1, class _D1>
1649inline _LIBCPP_INLINE_VISIBILITY
1650bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001651operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001652{
1653 return static_cast<bool>(__x);
1654}
1655
1656template <class _T1, class _D1>
1657inline _LIBCPP_INLINE_VISIBILITY
1658bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001659operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001660{
1661 return static_cast<bool>(__x);
1662}
1663
1664template <class _T1, class _D1>
1665inline _LIBCPP_INLINE_VISIBILITY
1666bool
1667operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1668{
1669 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1670 return less<_P1>()(__x.get(), nullptr);
1671}
1672
1673template <class _T1, class _D1>
1674inline _LIBCPP_INLINE_VISIBILITY
1675bool
1676operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1677{
1678 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1679 return less<_P1>()(nullptr, __x.get());
1680}
1681
1682template <class _T1, class _D1>
1683inline _LIBCPP_INLINE_VISIBILITY
1684bool
1685operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1686{
1687 return nullptr < __x;
1688}
1689
1690template <class _T1, class _D1>
1691inline _LIBCPP_INLINE_VISIBILITY
1692bool
1693operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1694{
1695 return __x < nullptr;
1696}
1697
1698template <class _T1, class _D1>
1699inline _LIBCPP_INLINE_VISIBILITY
1700bool
1701operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1702{
1703 return !(nullptr < __x);
1704}
1705
1706template <class _T1, class _D1>
1707inline _LIBCPP_INLINE_VISIBILITY
1708bool
1709operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1710{
1711 return !(__x < nullptr);
1712}
1713
1714template <class _T1, class _D1>
1715inline _LIBCPP_INLINE_VISIBILITY
1716bool
1717operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1718{
1719 return !(__x < nullptr);
1720}
1721
1722template <class _T1, class _D1>
1723inline _LIBCPP_INLINE_VISIBILITY
1724bool
1725operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1726{
1727 return !(nullptr < __x);
1728}
1729
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001730#if _LIBCPP_STD_VER > 11
1731
1732template<class _Tp>
1733struct __unique_if
1734{
1735 typedef unique_ptr<_Tp> __unique_single;
1736};
1737
1738template<class _Tp>
1739struct __unique_if<_Tp[]>
1740{
1741 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1742};
1743
1744template<class _Tp, size_t _Np>
1745struct __unique_if<_Tp[_Np]>
1746{
1747 typedef void __unique_array_known_bound;
1748};
1749
1750template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00001751inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001752typename __unique_if<_Tp>::__unique_single
1753make_unique(_Args&&... __args)
1754{
1755 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
1756}
1757
1758template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00001759inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001760typename __unique_if<_Tp>::__unique_array_unknown_bound
1761make_unique(size_t __n)
1762{
1763 typedef typename remove_extent<_Tp>::type _Up;
1764 return unique_ptr<_Tp>(new _Up[__n]());
1765}
1766
1767template<class _Tp, class... _Args>
1768 typename __unique_if<_Tp>::__unique_array_known_bound
1769 make_unique(_Args&&...) = delete;
1770
1771#endif // _LIBCPP_STD_VER > 11
1772
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001773template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001774#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001775struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001776#else
1777struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001778 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001779#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001780{
1781 typedef unique_ptr<_Tp, _Dp> argument_type;
1782 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001783 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001784 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001785 {
1786 typedef typename argument_type::pointer pointer;
1787 return hash<pointer>()(__ptr.get());
1788 }
1789};
1790
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791struct __destruct_n
1792{
1793private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001794 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795
1796 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001797 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001798 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799
1800 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001801 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 {}
1803
Howard Hinnant719bda32011-05-28 14:41:13 +00001804 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001805 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001806 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 {}
1808
Howard Hinnant719bda32011-05-28 14:41:13 +00001809 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001810 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001811 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 {}
1813public:
Howard Hinnant719bda32011-05-28 14:41:13 +00001814 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001815 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816
1817 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05001818 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001819 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820
1821 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001822 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001823 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824
1825 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001826 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001827 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828};
1829
1830template <class _Alloc>
1831class __allocator_destructor
1832{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001833 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001835 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
1836 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837private:
1838 _Alloc& __alloc_;
1839 size_type __s_;
1840public:
1841 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00001842 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001845 void operator()(pointer __p) _NOEXCEPT
1846 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847};
1848
1849template <class _InputIterator, class _ForwardIterator>
1850_ForwardIterator
1851uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
1852{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001854#ifndef _LIBCPP_NO_EXCEPTIONS
1855 _ForwardIterator __s = __r;
1856 try
1857 {
1858#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001859 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001860 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001861#ifndef _LIBCPP_NO_EXCEPTIONS
1862 }
1863 catch (...)
1864 {
1865 for (; __s != __r; ++__s)
1866 __s->~value_type();
1867 throw;
1868 }
1869#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 return __r;
1871}
1872
1873template <class _InputIterator, class _Size, class _ForwardIterator>
1874_ForwardIterator
1875uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
1876{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001878#ifndef _LIBCPP_NO_EXCEPTIONS
1879 _ForwardIterator __s = __r;
1880 try
1881 {
1882#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001883 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001884 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001885#ifndef _LIBCPP_NO_EXCEPTIONS
1886 }
1887 catch (...)
1888 {
1889 for (; __s != __r; ++__s)
1890 __s->~value_type();
1891 throw;
1892 }
1893#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 return __r;
1895}
1896
1897template <class _ForwardIterator, class _Tp>
1898void
1899uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
1900{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001902#ifndef _LIBCPP_NO_EXCEPTIONS
1903 _ForwardIterator __s = __f;
1904 try
1905 {
1906#endif
1907 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001908 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001909#ifndef _LIBCPP_NO_EXCEPTIONS
1910 }
1911 catch (...)
1912 {
1913 for (; __s != __f; ++__s)
1914 __s->~value_type();
1915 throw;
1916 }
1917#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918}
1919
1920template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00001921_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
1923{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001925#ifndef _LIBCPP_NO_EXCEPTIONS
1926 _ForwardIterator __s = __f;
1927 try
1928 {
1929#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001930 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001931 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001932#ifndef _LIBCPP_NO_EXCEPTIONS
1933 }
1934 catch (...)
1935 {
1936 for (; __s != __f; ++__s)
1937 __s->~value_type();
1938 throw;
1939 }
1940#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00001941 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942}
1943
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001944#if _LIBCPP_STD_VER > 14
1945
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001946template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04001947inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001948void destroy(_ForwardIterator __first, _ForwardIterator __last) {
1949 for (; __first != __last; ++__first)
1950 _VSTD::destroy_at(_VSTD::addressof(*__first));
1951}
1952
1953template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04001954inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001955_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
1956 for (; __n > 0; (void)++__first, --__n)
1957 _VSTD::destroy_at(_VSTD::addressof(*__first));
1958 return __first;
1959}
1960
Eric Fiselier290c07c2016-10-11 21:13:44 +00001961template <class _ForwardIterator>
1962inline _LIBCPP_INLINE_VISIBILITY
1963void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
1964 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1965 auto __idx = __first;
1966#ifndef _LIBCPP_NO_EXCEPTIONS
1967 try {
1968#endif
1969 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001970 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00001971#ifndef _LIBCPP_NO_EXCEPTIONS
1972 } catch (...) {
1973 _VSTD::destroy(__first, __idx);
1974 throw;
1975 }
1976#endif
1977}
1978
1979template <class _ForwardIterator, class _Size>
1980inline _LIBCPP_INLINE_VISIBILITY
1981_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
1982 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1983 auto __idx = __first;
1984#ifndef _LIBCPP_NO_EXCEPTIONS
1985 try {
1986#endif
1987 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001988 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00001989 return __idx;
1990#ifndef _LIBCPP_NO_EXCEPTIONS
1991 } catch (...) {
1992 _VSTD::destroy(__first, __idx);
1993 throw;
1994 }
1995#endif
1996}
1997
1998
1999template <class _ForwardIterator>
2000inline _LIBCPP_INLINE_VISIBILITY
2001void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2002 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2003 auto __idx = __first;
2004#ifndef _LIBCPP_NO_EXCEPTIONS
2005 try {
2006#endif
2007 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002008 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002009#ifndef _LIBCPP_NO_EXCEPTIONS
2010 } catch (...) {
2011 _VSTD::destroy(__first, __idx);
2012 throw;
2013 }
2014#endif
2015}
2016
2017template <class _ForwardIterator, class _Size>
2018inline _LIBCPP_INLINE_VISIBILITY
2019_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2020 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2021 auto __idx = __first;
2022#ifndef _LIBCPP_NO_EXCEPTIONS
2023 try {
2024#endif
2025 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002026 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002027 return __idx;
2028#ifndef _LIBCPP_NO_EXCEPTIONS
2029 } catch (...) {
2030 _VSTD::destroy(__first, __idx);
2031 throw;
2032 }
2033#endif
2034}
2035
2036
2037template <class _InputIt, class _ForwardIt>
2038inline _LIBCPP_INLINE_VISIBILITY
2039_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2040 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2041 auto __idx = __first_res;
2042#ifndef _LIBCPP_NO_EXCEPTIONS
2043 try {
2044#endif
2045 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002046 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002047 return __idx;
2048#ifndef _LIBCPP_NO_EXCEPTIONS
2049 } catch (...) {
2050 _VSTD::destroy(__first_res, __idx);
2051 throw;
2052 }
2053#endif
2054}
2055
2056template <class _InputIt, class _Size, class _ForwardIt>
2057inline _LIBCPP_INLINE_VISIBILITY
2058pair<_InputIt, _ForwardIt>
2059uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2060 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2061 auto __idx = __first_res;
2062#ifndef _LIBCPP_NO_EXCEPTIONS
2063 try {
2064#endif
2065 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002066 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002067 return {__first, __idx};
2068#ifndef _LIBCPP_NO_EXCEPTIONS
2069 } catch (...) {
2070 _VSTD::destroy(__first_res, __idx);
2071 throw;
2072 }
2073#endif
2074}
2075
2076
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002077#endif // _LIBCPP_STD_VER > 14
2078
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002079// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2080// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -05002081// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002082#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2083 && defined(__ATOMIC_RELAXED) \
2084 && defined(__ATOMIC_ACQ_REL)
2085# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002086#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002087# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2088#endif
2089
2090template <class _Tp>
2091inline _LIBCPP_INLINE_VISIBILITY _Tp
2092__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2093{
2094#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2095 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2096#else
2097 return __t += 1;
2098#endif
2099}
2100
2101template <class _Tp>
2102inline _LIBCPP_INLINE_VISIBILITY _Tp
2103__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2104{
2105#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2106 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2107#else
2108 return __t -= 1;
2109#endif
2110}
2111
Howard Hinnant756c69b2010-09-22 16:48:34 +00002112class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113 : public std::exception
2114{
2115public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002116 bad_weak_ptr() _NOEXCEPT = default;
2117 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002118 virtual ~bad_weak_ptr() _NOEXCEPT;
2119 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120};
2121
Louis Dionne16fe2952018-07-11 23:14:33 +00002122_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002123void __throw_bad_weak_ptr()
2124{
2125#ifndef _LIBCPP_NO_EXCEPTIONS
2126 throw bad_weak_ptr();
2127#else
2128 _VSTD::abort();
2129#endif
2130}
2131
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002132template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002134class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135{
2136 __shared_count(const __shared_count&);
2137 __shared_count& operator=(const __shared_count&);
2138
2139protected:
2140 long __shared_owners_;
2141 virtual ~__shared_count();
2142private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002143 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144
2145public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002147 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148 : __shared_owners_(__refs) {}
2149
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002150#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002151 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002152 void __add_shared() _NOEXCEPT;
2153 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002154#else
2155 _LIBCPP_INLINE_VISIBILITY
2156 void __add_shared() _NOEXCEPT {
2157 __libcpp_atomic_refcount_increment(__shared_owners_);
2158 }
2159 _LIBCPP_INLINE_VISIBILITY
2160 bool __release_shared() _NOEXCEPT {
2161 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2162 __on_zero_shared();
2163 return true;
2164 }
2165 return false;
2166 }
2167#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002168 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002169 long use_count() const _NOEXCEPT {
2170 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2171 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172};
2173
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002174class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175 : private __shared_count
2176{
2177 long __shared_weak_owners_;
2178
2179public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002181 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182 : __shared_count(__refs),
2183 __shared_weak_owners_(__refs) {}
2184protected:
2185 virtual ~__shared_weak_count();
2186
2187public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002188#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002189 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002190 void __add_shared() _NOEXCEPT;
2191 void __add_weak() _NOEXCEPT;
2192 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002193#else
2194 _LIBCPP_INLINE_VISIBILITY
2195 void __add_shared() _NOEXCEPT {
2196 __shared_count::__add_shared();
2197 }
2198 _LIBCPP_INLINE_VISIBILITY
2199 void __add_weak() _NOEXCEPT {
2200 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2201 }
2202 _LIBCPP_INLINE_VISIBILITY
2203 void __release_shared() _NOEXCEPT {
2204 if (__shared_count::__release_shared())
2205 __release_weak();
2206 }
2207#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002208 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002210 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2211 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212
Howard Hinnant719bda32011-05-28 14:41:13 +00002213 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002215 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216};
2217
2218template <class _Tp, class _Dp, class _Alloc>
2219class __shared_ptr_pointer
2220 : public __shared_weak_count
2221{
2222 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2223public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002226 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227
Howard Hinnant72f73582010-08-11 17:04:31 +00002228#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002229 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002230#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231
2232private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002233 virtual void __on_zero_shared() _NOEXCEPT;
2234 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235};
2236
Howard Hinnant72f73582010-08-11 17:04:31 +00002237#ifndef _LIBCPP_NO_RTTI
2238
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239template <class _Tp, class _Dp, class _Alloc>
2240const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002241__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002243 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244}
2245
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002246#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002247
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248template <class _Tp, class _Dp, class _Alloc>
2249void
Howard Hinnant719bda32011-05-28 14:41:13 +00002250__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251{
2252 __data_.first().second()(__data_.first().first());
2253 __data_.first().second().~_Dp();
2254}
2255
2256template <class _Tp, class _Dp, class _Alloc>
2257void
Howard Hinnant719bda32011-05-28 14:41:13 +00002258__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002260 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2261 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002262 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2263
Eric Fiselierf8898c82015-02-05 23:01:40 +00002264 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002266 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267}
2268
2269template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002270struct __shared_ptr_emplace
2271 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272{
Louis Dionneda463cb2020-12-11 12:22:16 -05002273 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002274 _LIBCPP_HIDE_FROM_ABI
2275 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002276 : __storage_(_VSTD::move(__a))
2277 {
2278#if _LIBCPP_STD_VER > 17
2279 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2280 _TpAlloc __tmp(*__get_alloc());
2281 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002282#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002283 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002284#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002285 }
Louis Dionne86549d72020-12-09 16:22:17 -05002286
2287 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002288 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002289
2290 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002291 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292
2293private:
Louis Dionne86549d72020-12-09 16:22:17 -05002294 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002295#if _LIBCPP_STD_VER > 17
2296 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2297 _TpAlloc __tmp(*__get_alloc());
2298 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2299#else
Louis Dionne86549d72020-12-09 16:22:17 -05002300 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002301#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002302 }
2303
2304 virtual void __on_zero_shared_weak() _NOEXCEPT {
2305 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2306 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002307 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002308 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002309 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2310 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2311 }
2312
Louis Dionneda463cb2020-12-11 12:22:16 -05002313 // This class implements the control block for non-array shared pointers created
2314 // through `std::allocate_shared` and `std::make_shared`.
2315 //
2316 // In previous versions of the library, we used a compressed pair to store
2317 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2318 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2319 // we now use a properly aligned char buffer while making sure that we maintain
2320 // the same layout that we had when we used a compressed pair.
2321 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2322 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2323 char __blob_[sizeof(_CompressedPair)];
2324
2325 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2326 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2327 }
2328 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2329 __get_alloc()->~_Alloc();
2330 }
2331 _Alloc* __get_alloc() _NOEXCEPT {
2332 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2333 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2334 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2335 return __alloc;
2336 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08002337 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002338 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2339 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2340 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2341 return __elem;
2342 }
2343 };
2344
2345 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2346 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2347 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348};
2349
Erik Pilkington2a398762017-05-25 15:43:31 +00002350struct __shared_ptr_dummy_rebind_allocator_type;
2351template <>
2352class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2353{
2354public:
2355 template <class _Other>
2356 struct rebind
2357 {
2358 typedef allocator<_Other> other;
2359 };
2360};
2361
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002362template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363
zoecarverd73f42a2020-05-11 18:42:50 -07002364template<class _Tp, class _Up>
2365struct __compatible_with
2366#if _LIBCPP_STD_VER > 14
2367 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2368#else
2369 : is_convertible<_Tp*, _Up*> {};
2370#endif // _LIBCPP_STD_VER > 14
2371
zoecarver9bc39102021-02-19 11:10:36 -08002372template <class _Dp, class _Pt,
2373 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
2374static true_type __well_formed_deleter_test(int);
2375
2376template <class, class>
2377static false_type __well_formed_deleter_test(...);
2378
2379template <class _Dp, class _Pt>
2380struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
2381
2382template<class _Dp, class _Tp, class _Yp>
2383struct __shared_ptr_deleter_ctor_reqs
2384{
2385 static const bool value = __compatible_with<_Tp, _Yp>::value &&
2386 is_move_constructible<_Dp>::value &&
2387 __well_formed_deleter<_Dp, _Tp*>::value;
2388};
2389
Vy Nguyene369bd92020-07-13 12:34:37 -04002390#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2391# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2392#else
2393# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2394#endif
2395
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002397class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002399public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002400#if _LIBCPP_STD_VER > 14
2401 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002402 typedef remove_extent_t<_Tp> element_type;
2403#else
2404 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002405#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002406
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407private:
2408 element_type* __ptr_;
2409 __shared_weak_count* __cntrl_;
2410
2411 struct __nat {int __for_bool_;};
2412public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002414 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002416 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002417 template<class _Yp>
2418 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002419 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002420 template<class _Yp, class _Dp>
2421 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002422 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002423 template<class _Yp, class _Dp, class _Alloc>
2424 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002425 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2427 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002428 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002430 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002434 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002435 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002437 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002438 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002439 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002440 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002442 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002443#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002444 template<class _Yp>
2445 shared_ptr(auto_ptr<_Yp>&& __r,
2446 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002447#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002448 template <class _Yp, class _Dp>
2449 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2450 typename enable_if
2451 <
2452 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002453 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2454 __nat
2455 >::type = __nat());
2456 template <class _Yp, class _Dp>
2457 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2458 typename enable_if
2459 <
2460 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002461 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2462 __nat
2463 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464
2465 ~shared_ptr();
2466
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002468 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002469 template<class _Yp>
2470 typename enable_if
2471 <
zoecarverd73f42a2020-05-11 18:42:50 -07002472 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002473 shared_ptr&
2474 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002476 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002478 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002479 template<class _Yp>
2480 typename enable_if
2481 <
zoecarverd73f42a2020-05-11 18:42:50 -07002482 __compatible_with<_Yp, element_type>::value,
2483 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002484 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002486 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002487#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002488 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002490 typename enable_if
2491 <
2492 !is_array<_Yp>::value &&
2493 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002494 shared_ptr
2495 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002496 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002497#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002498 template <class _Yp, class _Dp>
2499 typename enable_if
2500 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002501 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2502 shared_ptr&
2503 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002505 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002508 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002510 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002511 template<class _Yp>
2512 typename enable_if
2513 <
zoecarverd73f42a2020-05-11 18:42:50 -07002514 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002515 void
2516 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002518 reset(_Yp* __p);
2519 template<class _Yp, class _Dp>
2520 typename enable_if
2521 <
zoecarverd73f42a2020-05-11 18:42:50 -07002522 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002523 void
2524 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002526 reset(_Yp* __p, _Dp __d);
2527 template<class _Yp, class _Dp, class _Alloc>
2528 typename enable_if
2529 <
zoecarverd73f42a2020-05-11 18:42:50 -07002530 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002531 void
2532 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002534 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535
Howard Hinnant756c69b2010-09-22 16:48:34 +00002536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002537 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002539 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2540 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002541 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002542 element_type* operator->() const _NOEXCEPT
2543 {
2544 static_assert(!_VSTD::is_array<_Tp>::value,
2545 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2546 return __ptr_;
2547 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002549 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002551 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002552 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002553 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002554 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002555 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002556 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002558 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002559 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002560 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002562 _LIBCPP_INLINE_VISIBILITY
2563 bool
2564 __owner_equivalent(const shared_ptr& __p) const
2565 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566
zoecarverd73f42a2020-05-11 18:42:50 -07002567#if _LIBCPP_STD_VER > 14
2568 typename add_lvalue_reference<element_type>::type
2569 _LIBCPP_INLINE_VISIBILITY
2570 operator[](ptrdiff_t __i) const
2571 {
2572 static_assert(_VSTD::is_array<_Tp>::value,
2573 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2574 return __ptr_[__i];
2575 }
2576#endif
2577
Howard Hinnant72f73582010-08-11 17:04:31 +00002578#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002581 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002582 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002583 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002584 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002585#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586
Zoe Carverd9040c72019-10-22 15:16:49 +00002587 template<class _Yp, class _CntrlBlk>
2588 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002589 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002590 {
2591 shared_ptr<_Tp> __r;
2592 __r.__ptr_ = __p;
2593 __r.__cntrl_ = __cntrl;
2594 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2595 return __r;
2596 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002597
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002599 template <class _Yp, bool = is_function<_Yp>::value>
2600 struct __shared_ptr_default_allocator
2601 {
2602 typedef allocator<_Yp> type;
2603 };
2604
2605 template <class _Yp>
2606 struct __shared_ptr_default_allocator<_Yp, true>
2607 {
2608 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2609 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002611 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002612 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002613 typename enable_if<is_convertible<_OrigPtr*,
2614 const enable_shared_from_this<_Yp>*
2615 >::value,
2616 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002617 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2618 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002620 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002621 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002622 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002623 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2624 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002625 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626 }
2627
Erik Pilkington2a398762017-05-25 15:43:31 +00002628 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629
zoecarverd73f42a2020-05-11 18:42:50 -07002630 template <class, class _Yp>
2631 struct __shared_ptr_default_delete
2632 : default_delete<_Yp> {};
2633
2634 template <class _Yp, class _Un, size_t _Sz>
2635 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2636 : default_delete<_Yp[]> {};
2637
2638 template <class _Yp, class _Un>
2639 struct __shared_ptr_default_delete<_Yp[], _Un>
2640 : default_delete<_Yp[]> {};
2641
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002642 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2643 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644};
2645
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002646#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2647template<class _Tp>
2648shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2649template<class _Tp, class _Dp>
2650shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2651#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002652
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002654inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002655_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002656shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002657 : __ptr_(nullptr),
2658 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659{
2660}
2661
2662template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002663inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002664_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002665shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002666 : __ptr_(nullptr),
2667 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668{
2669}
2670
2671template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002672template<class _Yp>
2673shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002674 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675 : __ptr_(__p)
2676{
2677 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002678 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002679 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2680 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002682 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683}
2684
2685template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002686template<class _Yp, class _Dp>
2687shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002688 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 : __ptr_(__p)
2690{
2691#ifndef _LIBCPP_NO_EXCEPTIONS
2692 try
2693 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002694#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002695 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2696 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002697#ifndef _LIBCPP_CXX03_LANG
2698 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2699#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002700 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002701#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002702 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703#ifndef _LIBCPP_NO_EXCEPTIONS
2704 }
2705 catch (...)
2706 {
2707 __d(__p);
2708 throw;
2709 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002710#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711}
2712
2713template<class _Tp>
2714template<class _Dp>
2715shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002716 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717{
2718#ifndef _LIBCPP_NO_EXCEPTIONS
2719 try
2720 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002721#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002722 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2723 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002724#ifndef _LIBCPP_CXX03_LANG
2725 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2726#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002727 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002728#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729#ifndef _LIBCPP_NO_EXCEPTIONS
2730 }
2731 catch (...)
2732 {
2733 __d(__p);
2734 throw;
2735 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002736#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737}
2738
2739template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002740template<class _Yp, class _Dp, class _Alloc>
2741shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002742 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743 : __ptr_(__p)
2744{
2745#ifndef _LIBCPP_NO_EXCEPTIONS
2746 try
2747 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002748#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002750 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751 typedef __allocator_destructor<_A2> _D2;
2752 _A2 __a2(__a);
2753 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002754 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2755#ifndef _LIBCPP_CXX03_LANG
2756 _CntrlBlk(__p, _VSTD::move(__d), __a);
2757#else
2758 _CntrlBlk(__p, __d, __a);
2759#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002760 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002761 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762#ifndef _LIBCPP_NO_EXCEPTIONS
2763 }
2764 catch (...)
2765 {
2766 __d(__p);
2767 throw;
2768 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002769#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770}
2771
2772template<class _Tp>
2773template<class _Dp, class _Alloc>
2774shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002775 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776{
2777#ifndef _LIBCPP_NO_EXCEPTIONS
2778 try
2779 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002780#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002782 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 typedef __allocator_destructor<_A2> _D2;
2784 _A2 __a2(__a);
2785 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002786 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2787#ifndef _LIBCPP_CXX03_LANG
2788 _CntrlBlk(__p, _VSTD::move(__d), __a);
2789#else
2790 _CntrlBlk(__p, __d, __a);
2791#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002792 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793#ifndef _LIBCPP_NO_EXCEPTIONS
2794 }
2795 catch (...)
2796 {
2797 __d(__p);
2798 throw;
2799 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002800#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801}
2802
2803template<class _Tp>
2804template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002805inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002806shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 : __ptr_(__p),
2808 __cntrl_(__r.__cntrl_)
2809{
2810 if (__cntrl_)
2811 __cntrl_->__add_shared();
2812}
2813
2814template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002815inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002816shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817 : __ptr_(__r.__ptr_),
2818 __cntrl_(__r.__cntrl_)
2819{
2820 if (__cntrl_)
2821 __cntrl_->__add_shared();
2822}
2823
2824template<class _Tp>
2825template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002826inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002828 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002829 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 : __ptr_(__r.__ptr_),
2831 __cntrl_(__r.__cntrl_)
2832{
2833 if (__cntrl_)
2834 __cntrl_->__add_shared();
2835}
2836
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002838inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002839shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 : __ptr_(__r.__ptr_),
2841 __cntrl_(__r.__cntrl_)
2842{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002843 __r.__ptr_ = nullptr;
2844 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845}
2846
2847template<class _Tp>
2848template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002849inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002851 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002852 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853 : __ptr_(__r.__ptr_),
2854 __cntrl_(__r.__cntrl_)
2855{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002856 __r.__ptr_ = nullptr;
2857 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858}
2859
Marshall Clowb22274f2017-01-24 22:22:33 +00002860#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002862template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002863shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002864 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865 : __ptr_(__r.get())
2866{
2867 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2868 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002869 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870 __r.release();
2871}
Marshall Clowb22274f2017-01-24 22:22:33 +00002872#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873
2874template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002875template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002877 typename enable_if
2878 <
2879 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002880 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2881 __nat
2882 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 : __ptr_(__r.get())
2884{
Marshall Clow35cde742015-05-10 13:59:45 +00002885#if _LIBCPP_STD_VER > 11
2886 if (__ptr_ == nullptr)
2887 __cntrl_ = nullptr;
2888 else
2889#endif
2890 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002891 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002892 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
Erik Pilkington2a398762017-05-25 15:43:31 +00002893 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002894 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002895 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 __r.release();
2897}
2898
2899template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002900template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002902 typename enable_if
2903 <
2904 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002905 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2906 __nat
2907 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 : __ptr_(__r.get())
2909{
Marshall Clow35cde742015-05-10 13:59:45 +00002910#if _LIBCPP_STD_VER > 11
2911 if (__ptr_ == nullptr)
2912 __cntrl_ = nullptr;
2913 else
2914#endif
2915 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002916 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002917 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow35cde742015-05-10 13:59:45 +00002918 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00002919 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05002920 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002921 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002922 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 __r.release();
2924}
2925
Zoe Carver6cd05c32019-08-19 15:47:16 +00002926template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927shared_ptr<_Tp>::~shared_ptr()
2928{
2929 if (__cntrl_)
2930 __cntrl_->__release_shared();
2931}
2932
2933template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002934inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002936shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937{
2938 shared_ptr(__r).swap(*this);
2939 return *this;
2940}
2941
2942template<class _Tp>
2943template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002944inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002945typename enable_if
2946<
zoecarverd73f42a2020-05-11 18:42:50 -07002947 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002948 shared_ptr<_Tp>&
2949>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002950shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951{
2952 shared_ptr(__r).swap(*this);
2953 return *this;
2954}
2955
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002957inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002959shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002961 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 return *this;
2963}
2964
2965template<class _Tp>
2966template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002967inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002968typename enable_if
2969<
zoecarverd73f42a2020-05-11 18:42:50 -07002970 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002971 shared_ptr<_Tp>&
2972>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
2974{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002975 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 return *this;
2977}
2978
Marshall Clowb22274f2017-01-24 22:22:33 +00002979#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980template<class _Tp>
2981template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002982inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002983typename enable_if
2984<
2985 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00002986 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002987 shared_ptr<_Tp>
2988>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
2990{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002991 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992 return *this;
2993}
Marshall Clowb22274f2017-01-24 22:22:33 +00002994#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995
2996template<class _Tp>
2997template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002998inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002999typename enable_if
3000<
Aditya Kumar7c5db692017-08-20 10:38:55 +00003001 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003002 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003003 shared_ptr<_Tp>&
3004>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3006{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003007 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008 return *this;
3009}
3010
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003012inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013void
Howard Hinnant719bda32011-05-28 14:41:13 +00003014shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003016 _VSTD::swap(__ptr_, __r.__ptr_);
3017 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018}
3019
3020template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003021inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022void
Howard Hinnant719bda32011-05-28 14:41:13 +00003023shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024{
3025 shared_ptr().swap(*this);
3026}
3027
3028template<class _Tp>
3029template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003030inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003031typename enable_if
3032<
zoecarverd73f42a2020-05-11 18:42:50 -07003033 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003034 void
3035>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036shared_ptr<_Tp>::reset(_Yp* __p)
3037{
3038 shared_ptr(__p).swap(*this);
3039}
3040
3041template<class _Tp>
3042template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003043inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003044typename enable_if
3045<
zoecarverd73f42a2020-05-11 18:42:50 -07003046 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003047 void
3048>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3050{
3051 shared_ptr(__p, __d).swap(*this);
3052}
3053
3054template<class _Tp>
3055template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003056inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003057typename enable_if
3058<
zoecarverd73f42a2020-05-11 18:42:50 -07003059 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003060 void
3061>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3063{
3064 shared_ptr(__p, __d, __a).swap(*this);
3065}
3066
Louis Dionne74aef9f2020-12-11 12:20:06 -05003067//
3068// std::allocate_shared and std::make_shared
3069//
3070template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3071_LIBCPP_HIDE_FROM_ABI
3072shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073{
Louis Dionne74aef9f2020-12-11 12:20:06 -05003074 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3075 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3076 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3077 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3078 auto __control_block = __guard.__release_ptr();
3079 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080}
3081
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003082template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3083_LIBCPP_HIDE_FROM_ABI
3084shared_ptr<_Tp> make_shared(_Args&& ...__args)
3085{
3086 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3087}
3088
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089template<class _Tp, class _Up>
3090inline _LIBCPP_INLINE_VISIBILITY
3091bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003092operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003093{
3094 return __x.get() == __y.get();
3095}
3096
3097template<class _Tp, class _Up>
3098inline _LIBCPP_INLINE_VISIBILITY
3099bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003100operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101{
3102 return !(__x == __y);
3103}
3104
3105template<class _Tp, class _Up>
3106inline _LIBCPP_INLINE_VISIBILITY
3107bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003108operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003110#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003111 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3112 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003113#else
3114 return less<>()(__x.get(), __y.get());
3115#endif
3116
Howard Hinnantb17caf92012-02-21 21:02:58 +00003117}
3118
3119template<class _Tp, class _Up>
3120inline _LIBCPP_INLINE_VISIBILITY
3121bool
3122operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3123{
3124 return __y < __x;
3125}
3126
3127template<class _Tp, class _Up>
3128inline _LIBCPP_INLINE_VISIBILITY
3129bool
3130operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3131{
3132 return !(__y < __x);
3133}
3134
3135template<class _Tp, class _Up>
3136inline _LIBCPP_INLINE_VISIBILITY
3137bool
3138operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3139{
3140 return !(__x < __y);
3141}
3142
3143template<class _Tp>
3144inline _LIBCPP_INLINE_VISIBILITY
3145bool
3146operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3147{
3148 return !__x;
3149}
3150
3151template<class _Tp>
3152inline _LIBCPP_INLINE_VISIBILITY
3153bool
3154operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3155{
3156 return !__x;
3157}
3158
3159template<class _Tp>
3160inline _LIBCPP_INLINE_VISIBILITY
3161bool
3162operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3163{
3164 return static_cast<bool>(__x);
3165}
3166
3167template<class _Tp>
3168inline _LIBCPP_INLINE_VISIBILITY
3169bool
3170operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3171{
3172 return static_cast<bool>(__x);
3173}
3174
3175template<class _Tp>
3176inline _LIBCPP_INLINE_VISIBILITY
3177bool
3178operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3179{
3180 return less<_Tp*>()(__x.get(), nullptr);
3181}
3182
3183template<class _Tp>
3184inline _LIBCPP_INLINE_VISIBILITY
3185bool
3186operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3187{
3188 return less<_Tp*>()(nullptr, __x.get());
3189}
3190
3191template<class _Tp>
3192inline _LIBCPP_INLINE_VISIBILITY
3193bool
3194operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3195{
3196 return nullptr < __x;
3197}
3198
3199template<class _Tp>
3200inline _LIBCPP_INLINE_VISIBILITY
3201bool
3202operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3203{
3204 return __x < nullptr;
3205}
3206
3207template<class _Tp>
3208inline _LIBCPP_INLINE_VISIBILITY
3209bool
3210operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3211{
3212 return !(nullptr < __x);
3213}
3214
3215template<class _Tp>
3216inline _LIBCPP_INLINE_VISIBILITY
3217bool
3218operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3219{
3220 return !(__x < nullptr);
3221}
3222
3223template<class _Tp>
3224inline _LIBCPP_INLINE_VISIBILITY
3225bool
3226operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3227{
3228 return !(__x < nullptr);
3229}
3230
3231template<class _Tp>
3232inline _LIBCPP_INLINE_VISIBILITY
3233bool
3234operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3235{
3236 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237}
3238
3239template<class _Tp>
3240inline _LIBCPP_INLINE_VISIBILITY
3241void
Howard Hinnant719bda32011-05-28 14:41:13 +00003242swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243{
3244 __x.swap(__y);
3245}
3246
3247template<class _Tp, class _Up>
3248inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003249shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003250static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251{
zoecarverd73f42a2020-05-11 18:42:50 -07003252 return shared_ptr<_Tp>(__r,
3253 static_cast<
3254 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255}
3256
3257template<class _Tp, class _Up>
3258inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003259shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003260dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261{
zoecarverd73f42a2020-05-11 18:42:50 -07003262 typedef typename shared_ptr<_Tp>::element_type _ET;
3263 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3265}
3266
3267template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003268shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003269const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270{
zoecarverd73f42a2020-05-11 18:42:50 -07003271 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003272 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273}
3274
zoecarverd73f42a2020-05-11 18:42:50 -07003275template<class _Tp, class _Up>
3276shared_ptr<_Tp>
3277reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3278{
3279 return shared_ptr<_Tp>(__r,
3280 reinterpret_cast<
3281 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3282}
3283
Howard Hinnant72f73582010-08-11 17:04:31 +00003284#ifndef _LIBCPP_NO_RTTI
3285
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286template<class _Dp, class _Tp>
3287inline _LIBCPP_INLINE_VISIBILITY
3288_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003289get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290{
3291 return __p.template __get_deleter<_Dp>();
3292}
3293
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003294#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003295
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003297class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003299public:
3300 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003301private:
3302 element_type* __ptr_;
3303 __shared_weak_count* __cntrl_;
3304
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003305public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003307 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003308 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003309 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3310 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003312 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003313 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003314 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3315 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003316
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003318 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003319 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003320 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3321 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003322 ~weak_ptr();
3323
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003325 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003326 template<class _Yp>
3327 typename enable_if
3328 <
3329 is_convertible<_Yp*, element_type*>::value,
3330 weak_ptr&
3331 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003333 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3334
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003336 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3337 template<class _Yp>
3338 typename enable_if
3339 <
3340 is_convertible<_Yp*, element_type*>::value,
3341 weak_ptr&
3342 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003344 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3345
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003346 template<class _Yp>
3347 typename enable_if
3348 <
3349 is_convertible<_Yp*, element_type*>::value,
3350 weak_ptr&
3351 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003353 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003354
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003356 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003358 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003359
Howard Hinnant756c69b2010-09-22 16:48:34 +00003360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003361 long use_count() const _NOEXCEPT
3362 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003364 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003365 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003366 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003367 template<class _Up>
3368 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003369 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003371 template<class _Up>
3372 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003373 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374 {return __cntrl_ < __r.__cntrl_;}
3375
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003376 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3377 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378};
3379
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003380#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3381template<class _Tp>
3382weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3383#endif
3384
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003386inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003387_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003388weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003389 : __ptr_(nullptr),
3390 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391{
3392}
3393
3394template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003395inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003396weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397 : __ptr_(__r.__ptr_),
3398 __cntrl_(__r.__cntrl_)
3399{
3400 if (__cntrl_)
3401 __cntrl_->__add_weak();
3402}
3403
3404template<class _Tp>
3405template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003406inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003408 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003409 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410 : __ptr_(__r.__ptr_),
3411 __cntrl_(__r.__cntrl_)
3412{
3413 if (__cntrl_)
3414 __cntrl_->__add_weak();
3415}
3416
3417template<class _Tp>
3418template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003419inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003421 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003422 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423 : __ptr_(__r.__ptr_),
3424 __cntrl_(__r.__cntrl_)
3425{
3426 if (__cntrl_)
3427 __cntrl_->__add_weak();
3428}
3429
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003430template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003431inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003432weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3433 : __ptr_(__r.__ptr_),
3434 __cntrl_(__r.__cntrl_)
3435{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003436 __r.__ptr_ = nullptr;
3437 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003438}
3439
3440template<class _Tp>
3441template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003442inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003443weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3444 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3445 _NOEXCEPT
3446 : __ptr_(__r.__ptr_),
3447 __cntrl_(__r.__cntrl_)
3448{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003449 __r.__ptr_ = nullptr;
3450 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003451}
3452
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453template<class _Tp>
3454weak_ptr<_Tp>::~weak_ptr()
3455{
3456 if (__cntrl_)
3457 __cntrl_->__release_weak();
3458}
3459
3460template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003461inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003463weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464{
3465 weak_ptr(__r).swap(*this);
3466 return *this;
3467}
3468
3469template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003470template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003471inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003472typename enable_if
3473<
3474 is_convertible<_Yp*, _Tp*>::value,
3475 weak_ptr<_Tp>&
3476>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003477weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003478{
3479 weak_ptr(__r).swap(*this);
3480 return *this;
3481}
3482
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003483template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003484inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003485weak_ptr<_Tp>&
3486weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3487{
3488 weak_ptr(_VSTD::move(__r)).swap(*this);
3489 return *this;
3490}
3491
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003493template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003494inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003495typename enable_if
3496<
3497 is_convertible<_Yp*, _Tp*>::value,
3498 weak_ptr<_Tp>&
3499>::type
3500weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3501{
3502 weak_ptr(_VSTD::move(__r)).swap(*this);
3503 return *this;
3504}
3505
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003506template<class _Tp>
3507template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003508inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003509typename enable_if
3510<
3511 is_convertible<_Yp*, _Tp*>::value,
3512 weak_ptr<_Tp>&
3513>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003514weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515{
3516 weak_ptr(__r).swap(*this);
3517 return *this;
3518}
3519
3520template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003521inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522void
Howard Hinnant719bda32011-05-28 14:41:13 +00003523weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003525 _VSTD::swap(__ptr_, __r.__ptr_);
3526 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527}
3528
3529template<class _Tp>
3530inline _LIBCPP_INLINE_VISIBILITY
3531void
Howard Hinnant719bda32011-05-28 14:41:13 +00003532swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533{
3534 __x.swap(__y);
3535}
3536
3537template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003538inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539void
Howard Hinnant719bda32011-05-28 14:41:13 +00003540weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541{
3542 weak_ptr().swap(*this);
3543}
3544
3545template<class _Tp>
3546template<class _Yp>
3547shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003548 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549 : __ptr_(__r.__ptr_),
3550 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3551{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003552 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003553 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554}
3555
3556template<class _Tp>
3557shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003558weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
3560 shared_ptr<_Tp> __r;
3561 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3562 if (__r.__cntrl_)
3563 __r.__ptr_ = __ptr_;
3564 return __r;
3565}
3566
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003567#if _LIBCPP_STD_VER > 14
3568template <class _Tp = void> struct owner_less;
3569#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003570template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003571#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572
3573template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003574struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003576{
3577 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003578 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003579 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003581 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003582 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003584 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003585 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003587};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588
3589template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003590struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3592{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003593 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003594 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003595 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003596 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003597 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003598 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003600 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003601 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602 {return __x.owner_before(__y);}
3603};
3604
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003605#if _LIBCPP_STD_VER > 14
3606template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003607struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003608{
3609 template <class _Tp, class _Up>
3610 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003611 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003612 {return __x.owner_before(__y);}
3613 template <class _Tp, class _Up>
3614 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003615 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003616 {return __x.owner_before(__y);}
3617 template <class _Tp, class _Up>
3618 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003619 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003620 {return __x.owner_before(__y);}
3621 template <class _Tp, class _Up>
3622 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003623 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003624 {return __x.owner_before(__y);}
3625 typedef void is_transparent;
3626};
3627#endif
3628
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003630class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631{
3632 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003633protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003634 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003635 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003637 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003639 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3640 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003643public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003645 shared_ptr<_Tp> shared_from_this()
3646 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003648 shared_ptr<_Tp const> shared_from_this() const
3649 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650
Eric Fiselier84006862016-06-02 00:15:35 +00003651#if _LIBCPP_STD_VER > 14
3652 _LIBCPP_INLINE_VISIBILITY
3653 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3654 { return __weak_this_; }
3655
3656 _LIBCPP_INLINE_VISIBILITY
3657 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3658 { return __weak_this_; }
3659#endif // _LIBCPP_STD_VER > 14
3660
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661 template <class _Up> friend class shared_ptr;
3662};
3663
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003664template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003665struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003666{
3667 typedef shared_ptr<_Tp> argument_type;
3668 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003669
Howard Hinnant756c69b2010-09-22 16:48:34 +00003670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003671 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003672 {
zoecarverd73f42a2020-05-11 18:42:50 -07003673 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003674 }
3675};
3676
Howard Hinnantc834c512011-11-29 18:15:50 +00003677template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003678inline _LIBCPP_INLINE_VISIBILITY
3679basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003680operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003681
Eric Fiselier9b492672016-06-18 02:12:53 +00003682
3683#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003684
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003685class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003686{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003687 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003688public:
3689 void lock() _NOEXCEPT;
3690 void unlock() _NOEXCEPT;
3691
3692private:
3693 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3694 __sp_mut(const __sp_mut&);
3695 __sp_mut& operator=(const __sp_mut&);
3696
Howard Hinnant8331b762013-03-06 23:30:19 +00003697 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003698};
3699
Mehdi Amini228053d2017-05-04 17:08:54 +00003700_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3701__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003702
3703template <class _Tp>
3704inline _LIBCPP_INLINE_VISIBILITY
3705bool
3706atomic_is_lock_free(const shared_ptr<_Tp>*)
3707{
3708 return false;
3709}
3710
3711template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003712_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003713shared_ptr<_Tp>
3714atomic_load(const shared_ptr<_Tp>* __p)
3715{
3716 __sp_mut& __m = __get_sp_mut(__p);
3717 __m.lock();
3718 shared_ptr<_Tp> __q = *__p;
3719 __m.unlock();
3720 return __q;
3721}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003722
Howard Hinnant9fa30202012-07-30 01:40:57 +00003723template <class _Tp>
3724inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003725_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003726shared_ptr<_Tp>
3727atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3728{
3729 return atomic_load(__p);
3730}
3731
3732template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003733_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003734void
3735atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3736{
3737 __sp_mut& __m = __get_sp_mut(__p);
3738 __m.lock();
3739 __p->swap(__r);
3740 __m.unlock();
3741}
3742
3743template <class _Tp>
3744inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003745_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003746void
3747atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3748{
3749 atomic_store(__p, __r);
3750}
3751
3752template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003753_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003754shared_ptr<_Tp>
3755atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3756{
3757 __sp_mut& __m = __get_sp_mut(__p);
3758 __m.lock();
3759 __p->swap(__r);
3760 __m.unlock();
3761 return __r;
3762}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003763
Howard Hinnant9fa30202012-07-30 01:40:57 +00003764template <class _Tp>
3765inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003766_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003767shared_ptr<_Tp>
3768atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3769{
3770 return atomic_exchange(__p, __r);
3771}
3772
3773template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003774_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003775bool
3776atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3777{
Marshall Clow4201ee82016-05-18 17:50:13 +00003778 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003779 __sp_mut& __m = __get_sp_mut(__p);
3780 __m.lock();
3781 if (__p->__owner_equivalent(*__v))
3782 {
Marshall Clow4201ee82016-05-18 17:50:13 +00003783 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003784 *__p = __w;
3785 __m.unlock();
3786 return true;
3787 }
Marshall Clow4201ee82016-05-18 17:50:13 +00003788 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003789 *__v = *__p;
3790 __m.unlock();
3791 return false;
3792}
3793
3794template <class _Tp>
3795inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003796_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003797bool
3798atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3799{
3800 return atomic_compare_exchange_strong(__p, __v, __w);
3801}
3802
3803template <class _Tp>
3804inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003805_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003806bool
3807atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3808 shared_ptr<_Tp> __w, memory_order, memory_order)
3809{
3810 return atomic_compare_exchange_strong(__p, __v, __w);
3811}
3812
3813template <class _Tp>
3814inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003815_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003816bool
3817atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3818 shared_ptr<_Tp> __w, memory_order, memory_order)
3819{
3820 return atomic_compare_exchange_weak(__p, __v, __w);
3821}
3822
Eric Fiselier9b492672016-06-18 02:12:53 +00003823#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003824
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003825//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00003826#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
3827# ifndef _LIBCPP_CXX03_LANG
3828enum class pointer_safety : unsigned char {
3829 relaxed,
3830 preferred,
3831 strict
3832};
3833# endif
3834#else
Howard Hinnant8331b762013-03-06 23:30:19 +00003835struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003837 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 {
3839 relaxed,
3840 preferred,
3841 strict
3842 };
3843
Howard Hinnant49e145e2012-10-30 19:06:59 +00003844 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845
Howard Hinnant756c69b2010-09-22 16:48:34 +00003846 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00003847 pointer_safety() : __v_() {}
3848
3849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00003850 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852 operator int() const {return __v_;}
3853};
Eric Fiselierab6bb302017-01-05 01:15:42 +00003854#endif
3855
3856#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003857 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00003858_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
3859#else
3860// This function is only offered in C++03 under ABI v1.
3861# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
3862inline _LIBCPP_INLINE_VISIBILITY
3863pointer_safety get_pointer_safety() _NOEXCEPT {
3864 return pointer_safety::relaxed;
3865}
3866# endif
3867#endif
3868
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003870_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
3871_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
3872_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003873_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874
3875template <class _Tp>
3876inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003877_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878undeclare_reachable(_Tp* __p)
3879{
3880 return static_cast<_Tp*>(__undeclare_reachable(__p));
3881}
3882
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003883_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884
Marshall Clow8982dcd2015-07-13 20:04:56 +00003885// --- Helper for container swap --
3886template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00003887_LIBCPP_INLINE_VISIBILITY
3888void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
3889#if _LIBCPP_STD_VER >= 14
3890 _NOEXCEPT
3891#else
3892 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3893#endif
3894{
3895 using _VSTD::swap;
3896 swap(__a1, __a2);
3897}
3898
3899template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00003900inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00003901void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
3902
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003903template <typename _Alloc>
3904inline _LIBCPP_INLINE_VISIBILITY
3905void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
3906#if _LIBCPP_STD_VER >= 14
3907 _NOEXCEPT
3908#else
3909 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3910#endif
3911{
3912 _VSTD::__swap_allocator(__a1, __a2,
3913 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
3914}
3915
Marshall Clowff91de82015-08-18 19:51:37 +00003916template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00003917struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00003918 _Traits::propagate_on_container_move_assignment::value
3919#if _LIBCPP_STD_VER > 14
3920 || _Traits::is_always_equal::value
3921#else
3922 && is_nothrow_move_assignable<_Alloc>::value
3923#endif
3924 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00003925
Marshall Clowa591b9a2016-07-11 21:38:08 +00003926
Marshall Clowa591b9a2016-07-11 21:38:08 +00003927template <class _Tp, class _Alloc>
3928struct __temp_value {
3929 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00003930
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00003931 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00003932 _Alloc &__a;
3933
3934 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
3935 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003936
Marshall Clowa591b9a2016-07-11 21:38:08 +00003937 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00003938 _LIBCPP_NO_CFI
3939 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
3940 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
3941 _VSTD::forward<_Args>(__args)...);
3942 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003943
Marshall Clowa591b9a2016-07-11 21:38:08 +00003944 ~__temp_value() { _Traits::destroy(__a, __addr()); }
3945 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00003946
Marshall Clowe46031a2018-07-02 18:41:15 +00003947template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00003948struct __is_allocator : false_type {};
3949
3950template<typename _Alloc>
3951struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00003952 typename __void_t<typename _Alloc::value_type>::type,
3953 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
3954 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00003955 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00003956
Eric Fiselier74ebee62019-06-08 01:31:19 +00003957// __builtin_new_allocator -- A non-templated helper for allocating and
3958// deallocating memory using __builtin_operator_new and
3959// __builtin_operator_delete. It should be used in preference to
3960// `std::allocator<T>` to avoid additional instantiations.
3961struct __builtin_new_allocator {
3962 struct __builtin_new_deleter {
3963 typedef void* pointer_type;
3964
3965 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
3966 : __size_(__size), __align_(__align) {}
3967
3968 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003969 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003970 }
3971
3972 private:
3973 size_t __size_;
3974 size_t __align_;
3975 };
3976
3977 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
3978
3979 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003980 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00003981 __builtin_new_deleter(__s, __align));
3982 }
3983
3984 static void __deallocate_bytes(void* __p, size_t __s,
3985 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003986 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003987 }
3988
3989 template <class _Tp>
3990 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3991 static __holder_t __allocate_type(size_t __n) {
3992 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3993 }
3994
3995 template <class _Tp>
3996 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3997 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
3998 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3999 }
4000};
4001
4002
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003_LIBCPP_END_NAMESPACE_STD
4004
Eric Fiselierf4433a32017-05-31 22:07:49 +00004005_LIBCPP_POP_MACROS
4006
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004007#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004008# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004009#endif
4010
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011#endif // _LIBCPP_MEMORY