blob: aabbfdc7200b7e62d7660706ba4252a7258b0ceb [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 Dionne00180872021-04-09 12:58:00 -0400687#include <__memory/temporary_buffer.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500688#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000689#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000690# include <atomic>
691#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000692#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000693
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000694#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000696#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697
Eric Fiselierf4433a32017-05-31 22:07:49 +0000698_LIBCPP_PUSH_MACROS
699#include <__undef_macros>
700
701
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702_LIBCPP_BEGIN_NAMESPACE_STD
703
Eric Fiselier89659d12015-07-07 00:27:16 +0000704template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000705inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000706_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
707#if !defined(_LIBCPP_HAS_NO_THREADS) && \
708 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000709 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000710 return __atomic_load_n(__value, __ATOMIC_RELAXED);
711#else
712 return *__value;
713#endif
714}
715
Kuba Breckade9d6792016-09-04 09:55:12 +0000716template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000717inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000718_ValueType __libcpp_acquire_load(_ValueType const* __value) {
719#if !defined(_LIBCPP_HAS_NO_THREADS) && \
720 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000721 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000722 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
723#else
724 return *__value;
725#endif
726}
727
Louis Dionned6651542020-11-03 12:05:55 -0500728template <class _Alloc, class _Ptr>
729_LIBCPP_INLINE_VISIBILITY
730void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
731 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500732 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500733 typedef allocator_traits<_Alloc> _Traits;
734 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
735 _Traits::construct(__a, _VSTD::__to_address(__begin2),
736#ifdef _LIBCPP_NO_EXCEPTIONS
737 _VSTD::move(*__begin1)
738#else
739 _VSTD::move_if_noexcept(*__begin1)
740#endif
741 );
742 }
743}
744
745template <class _Alloc, class _Tp, typename enable_if<
746 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
747 is_trivially_move_constructible<_Tp>::value
748>::type>
749_LIBCPP_INLINE_VISIBILITY
750void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
751 ptrdiff_t _Np = __end1 - __begin1;
752 if (_Np > 0) {
753 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
754 __begin2 += _Np;
755 }
756}
757
758template <class _Alloc, class _Iter, class _Ptr>
759_LIBCPP_INLINE_VISIBILITY
760void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
761 typedef allocator_traits<_Alloc> _Traits;
762 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
763 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
764 }
765}
766
767template <class _Alloc, class _Source, class _Dest,
768 class _RawSource = typename remove_const<_Source>::type,
769 class _RawDest = typename remove_const<_Dest>::type,
770 class =
771 typename enable_if<
772 is_trivially_copy_constructible<_Dest>::value &&
773 is_same<_RawSource, _RawDest>::value &&
774 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
775 >::type>
776_LIBCPP_INLINE_VISIBILITY
777void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
778 ptrdiff_t _Np = __end1 - __begin1;
779 if (_Np > 0) {
780 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
781 __begin2 += _Np;
782 }
783}
784
785template <class _Alloc, class _Ptr>
786_LIBCPP_INLINE_VISIBILITY
787void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
788 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
789 "The specified type does not meet the requirements of Cpp17MoveInsertable");
790 typedef allocator_traits<_Alloc> _Traits;
791 while (__end1 != __begin1) {
792 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
793#ifdef _LIBCPP_NO_EXCEPTIONS
794 _VSTD::move(*--__end1)
795#else
796 _VSTD::move_if_noexcept(*--__end1)
797#endif
798 );
799 --__end2;
800 }
801}
802
803template <class _Alloc, class _Tp, class = typename enable_if<
804 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
805 is_trivially_move_constructible<_Tp>::value
806>::type>
807_LIBCPP_INLINE_VISIBILITY
808void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
809 ptrdiff_t _Np = __end1 - __begin1;
810 __end2 -= _Np;
811 if (_Np > 0)
812 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
813}
814
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000816class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 : public iterator<output_iterator_tag,
818 _Tp, // purposefully not C++03
819 ptrdiff_t, // purposefully not C++03
820 _Tp*, // purposefully not C++03
821 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
822{
823private:
824 _OutputIterator __x_;
825public:
826 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
827 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
828 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500829 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +0000830#if _LIBCPP_STD_VER >= 14
831 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500832 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +0000833#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
835 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
836 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +0000837#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +0000838 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +0000839#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000840};
841
Eric Fiselier15eae3b2019-12-16 17:00:10 -0500842// Tag used to default initialize one or both of the pair's elements.
843struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500844struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -0500845
Eric Fiselier9d355982017-04-12 23:45:53 +0000846template <class _Tp, int _Idx,
847 bool _CanBeEmptyBase =
848 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
849struct __compressed_pair_elem {
850 typedef _Tp _ParamT;
851 typedef _Tp& reference;
852 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500854 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -0500855 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500856 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
857 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000858
Eric Fiselier9d355982017-04-12 23:45:53 +0000859 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +0000860 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
861 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +0000862 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500863 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +0000864 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +0000865 : __value_(_VSTD::forward<_Up>(__u))
866 {
867 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500869
870#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +0000871 template <class... _Args, size_t... _Indexes>
872 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
873 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
874 __tuple_indices<_Indexes...>)
875 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +0000876#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500878
Alex Lorenz76132112017-11-09 17:54:49 +0000879 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
880 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +0000881 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883private:
Eric Fiselier9d355982017-04-12 23:45:53 +0000884 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885};
886
Eric Fiselier9d355982017-04-12 23:45:53 +0000887template <class _Tp, int _Idx>
888struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
889 typedef _Tp _ParamT;
890 typedef _Tp& reference;
891 typedef const _Tp& const_reference;
892 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500894 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
895 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
896 __compressed_pair_elem(__default_init_tag) {}
897 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
898 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899
Eric Fiselier9d355982017-04-12 23:45:53 +0000900 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +0000901 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
902 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +0000903 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500904 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +0000905 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +0000906 : __value_type(_VSTD::forward<_Up>(__u))
907 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500909#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +0000910 template <class... _Args, size_t... _Indexes>
911 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
912 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
913 __tuple_indices<_Indexes...>)
914 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +0000915#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916
Alex Lorenz76132112017-11-09 17:54:49 +0000917 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
918 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +0000919 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920};
921
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +0000923class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
924 private __compressed_pair_elem<_T2, 1> {
Louis Dionneda463cb2020-12-11 12:22:16 -0500925public:
Eric Fiselier9d355982017-04-12 23:45:53 +0000926 // NOTE: This static assert should never fire because __compressed_pair
927 // is *almost never* used in a scenario where it's possible for T1 == T2.
928 // (The exception is std::function where it is possible that the function
929 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +0000930 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -0500931 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +0000932 "The current implementation is NOT ABI-compatible with the previous "
933 "implementation for this configuration");
934
Louis Dionneda463cb2020-12-11 12:22:16 -0500935 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
936 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
937
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500938 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +0000939 class = typename enable_if<
940 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
941 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
942 >::type
943 >
Eric Fiselier9d355982017-04-12 23:45:53 +0000944 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500945 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
Eric Fiselier9d355982017-04-12 23:45:53 +0000947 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500948 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +0000949 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500950 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500952#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +0000953 template <class... _Args1, class... _Args2>
954 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
955 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
956 tuple<_Args2...> __second_args)
957 : _Base1(__pc, _VSTD::move(__first_args),
958 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
959 _Base2(__pc, _VSTD::move(__second_args),
960 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +0000961#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Eric Fiselier9d355982017-04-12 23:45:53 +0000963 _LIBCPP_INLINE_VISIBILITY
964 typename _Base1::reference first() _NOEXCEPT {
965 return static_cast<_Base1&>(*this).__get();
966 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967
Eric Fiselier9d355982017-04-12 23:45:53 +0000968 _LIBCPP_INLINE_VISIBILITY
969 typename _Base1::const_reference first() const _NOEXCEPT {
970 return static_cast<_Base1 const&>(*this).__get();
971 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972
Eric Fiselier9d355982017-04-12 23:45:53 +0000973 _LIBCPP_INLINE_VISIBILITY
974 typename _Base2::reference second() _NOEXCEPT {
975 return static_cast<_Base2&>(*this).__get();
976 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977
Eric Fiselier9d355982017-04-12 23:45:53 +0000978 _LIBCPP_INLINE_VISIBILITY
979 typename _Base2::const_reference second() const _NOEXCEPT {
980 return static_cast<_Base2 const&>(*this).__get();
981 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982
Louis Dionneda463cb2020-12-11 12:22:16 -0500983 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
984 static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
985 return static_cast<_Base1*>(__pair);
986 }
987 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
988 static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
989 return static_cast<_Base2*>(__pair);
990 }
991
Eric Fiselier9d355982017-04-12 23:45:53 +0000992 _LIBCPP_INLINE_VISIBILITY
993 void swap(__compressed_pair& __x)
994 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
995 __is_nothrow_swappable<_T2>::value)
996 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500997 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +0000998 swap(first(), __x.first());
999 swap(second(), __x.second());
1000 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001};
1002
1003template <class _T1, class _T2>
1004inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001005void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1006 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1007 __is_nothrow_swappable<_T2>::value) {
1008 __x.swap(__y);
1009}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001011// default_delete
1012
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001014struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001015 static_assert(!is_function<_Tp>::value,
1016 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001017#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001018 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001019#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001020 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001021#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001022 template <class _Up>
1023 _LIBCPP_INLINE_VISIBILITY
1024 default_delete(const default_delete<_Up>&,
1025 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1026 0) _NOEXCEPT {}
1027
1028 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1029 static_assert(sizeof(_Tp) > 0,
1030 "default_delete can not delete incomplete type");
1031 static_assert(!is_void<_Tp>::value,
1032 "default_delete can not delete incomplete type");
1033 delete __ptr;
1034 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035};
1036
1037template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001038struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1039private:
1040 template <class _Up>
1041 struct _EnableIfConvertible
1042 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1043
Howard Hinnant4500ca52011-12-18 21:19:44 +00001044public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001045#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001046 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001047#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001048 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001049#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001050
1051 template <class _Up>
1052 _LIBCPP_INLINE_VISIBILITY
1053 default_delete(const default_delete<_Up[]>&,
1054 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1055
1056 template <class _Up>
1057 _LIBCPP_INLINE_VISIBILITY
1058 typename _EnableIfConvertible<_Up>::type
1059 operator()(_Up* __ptr) const _NOEXCEPT {
1060 static_assert(sizeof(_Tp) > 0,
1061 "default_delete can not delete incomplete type");
1062 static_assert(!is_void<_Tp>::value,
1063 "default_delete can not delete void type");
1064 delete[] __ptr;
1065 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066};
1067
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001068template <class _Deleter>
1069struct __unique_ptr_deleter_sfinae {
1070 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1071 typedef const _Deleter& __lval_ref_type;
1072 typedef _Deleter&& __good_rval_ref_type;
1073 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074};
1075
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001076template <class _Deleter>
1077struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1078 typedef const _Deleter& __lval_ref_type;
1079 typedef const _Deleter&& __bad_rval_ref_type;
1080 typedef false_type __enable_rval_overload;
1081};
1082
1083template <class _Deleter>
1084struct __unique_ptr_deleter_sfinae<_Deleter&> {
1085 typedef _Deleter& __lval_ref_type;
1086 typedef _Deleter&& __bad_rval_ref_type;
1087 typedef false_type __enable_rval_overload;
1088};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001089
Vy Nguyene369bd92020-07-13 12:34:37 -04001090#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1091# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1092#else
1093# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1094#endif
1095
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001096template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001097class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001098public:
1099 typedef _Tp element_type;
1100 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001101 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001102
1103 static_assert(!is_rvalue_reference<deleter_type>::value,
1104 "the specified deleter type cannot be an rvalue reference");
1105
1106private:
1107 __compressed_pair<pointer, deleter_type> __ptr_;
1108
1109 struct __nat { int __for_bool_; };
1110
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001111 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001112
1113 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001114 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001115 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001116
1117 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001118 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001119 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001120
1121 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001122 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001123 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001124
1125 template <bool _Dummy, class _Deleter = typename __dependent_type<
1126 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001127 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001128 typename enable_if<is_default_constructible<_Deleter>::value &&
1129 !is_pointer<_Deleter>::value>::type;
1130
1131 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001132 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001133 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1134
1135 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001136 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001137 is_convertible<typename _UPtr::pointer, pointer>::value &&
1138 !is_array<_Up>::value
1139 >::type;
1140
1141 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001142 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001143 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1144 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1145 >::type;
1146
1147 template <class _UDel>
1148 using _EnableIfDeleterAssignable = typename enable_if<
1149 is_assignable<_Dp&, _UDel&&>::value
1150 >::type;
1151
1152public:
1153 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001154 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001155 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001156 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001157
1158 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001159 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001160 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001161 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001162
1163 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001164 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001165 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001166 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001167
1168 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001169 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001170 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001171 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001172 : __ptr_(__p, __d) {}
1173
1174 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001175 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001176 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001177 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001178 : __ptr_(__p, _VSTD::move(__d)) {
1179 static_assert(!is_reference<deleter_type>::value,
1180 "rvalue deleter bound to reference");
1181 }
1182
1183 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001184 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001185 _LIBCPP_INLINE_VISIBILITY
1186 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1187
1188 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001189 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001190 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1191 }
1192
1193 template <class _Up, class _Ep,
1194 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1195 class = _EnableIfDeleterConvertible<_Ep>
1196 >
1197 _LIBCPP_INLINE_VISIBILITY
1198 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1199 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1200
1201#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1202 template <class _Up>
1203 _LIBCPP_INLINE_VISIBILITY
1204 unique_ptr(auto_ptr<_Up>&& __p,
1205 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001206 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001207 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001208 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001209#endif
1210
1211 _LIBCPP_INLINE_VISIBILITY
1212 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1213 reset(__u.release());
1214 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1215 return *this;
1216 }
1217
1218 template <class _Up, class _Ep,
1219 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1220 class = _EnableIfDeleterAssignable<_Ep>
1221 >
1222 _LIBCPP_INLINE_VISIBILITY
1223 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1224 reset(__u.release());
1225 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1226 return *this;
1227 }
1228
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001229#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1230 template <class _Up>
1231 _LIBCPP_INLINE_VISIBILITY
1232 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1233 is_same<_Dp, default_delete<_Tp> >::value,
1234 unique_ptr&>::type
1235 operator=(auto_ptr<_Up> __p) {
1236 reset(__p.release());
1237 return *this;
1238 }
1239#endif
1240
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001241#ifdef _LIBCPP_CXX03_LANG
1242 unique_ptr(unique_ptr const&) = delete;
1243 unique_ptr& operator=(unique_ptr const&) = delete;
1244#endif
1245
1246
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001247 _LIBCPP_INLINE_VISIBILITY
1248 ~unique_ptr() { reset(); }
1249
1250 _LIBCPP_INLINE_VISIBILITY
1251 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1252 reset();
1253 return *this;
1254 }
1255
1256 _LIBCPP_INLINE_VISIBILITY
1257 typename add_lvalue_reference<_Tp>::type
1258 operator*() const {
1259 return *__ptr_.first();
1260 }
1261 _LIBCPP_INLINE_VISIBILITY
1262 pointer operator->() const _NOEXCEPT {
1263 return __ptr_.first();
1264 }
1265 _LIBCPP_INLINE_VISIBILITY
1266 pointer get() const _NOEXCEPT {
1267 return __ptr_.first();
1268 }
1269 _LIBCPP_INLINE_VISIBILITY
1270 deleter_type& get_deleter() _NOEXCEPT {
1271 return __ptr_.second();
1272 }
1273 _LIBCPP_INLINE_VISIBILITY
1274 const deleter_type& get_deleter() const _NOEXCEPT {
1275 return __ptr_.second();
1276 }
1277 _LIBCPP_INLINE_VISIBILITY
1278 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1279 return __ptr_.first() != nullptr;
1280 }
1281
1282 _LIBCPP_INLINE_VISIBILITY
1283 pointer release() _NOEXCEPT {
1284 pointer __t = __ptr_.first();
1285 __ptr_.first() = pointer();
1286 return __t;
1287 }
1288
1289 _LIBCPP_INLINE_VISIBILITY
1290 void reset(pointer __p = pointer()) _NOEXCEPT {
1291 pointer __tmp = __ptr_.first();
1292 __ptr_.first() = __p;
1293 if (__tmp)
1294 __ptr_.second()(__tmp);
1295 }
1296
1297 _LIBCPP_INLINE_VISIBILITY
1298 void swap(unique_ptr& __u) _NOEXCEPT {
1299 __ptr_.swap(__u.__ptr_);
1300 }
1301};
1302
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001303
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001305class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001306public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001307 typedef _Tp element_type;
1308 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001309 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001310
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001312 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313
Eric Fiselier31127cd2017-04-16 02:14:31 +00001314 template <class _From>
1315 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316
Eric Fiselier31127cd2017-04-16 02:14:31 +00001317 template <class _FromElem>
1318 struct _CheckArrayPointerConversion<_FromElem*>
1319 : integral_constant<bool,
1320 is_same<_FromElem*, pointer>::value ||
1321 (is_same<pointer, element_type*>::value &&
1322 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1323 >
1324 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
Eric Fiselier31127cd2017-04-16 02:14:31 +00001326 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001327
1328 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001329 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001330 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001331
1332 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001333 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001334 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001335
1336 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001337 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001338 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001339
1340 template <bool _Dummy, class _Deleter = typename __dependent_type<
1341 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001342 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001343 typename enable_if<is_default_constructible<_Deleter>::value &&
1344 !is_pointer<_Deleter>::value>::type;
1345
1346 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001347 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001348 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1349
1350 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001351 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001352 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001353 >::type;
1354
1355 template <class _UPtr, class _Up,
1356 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001357 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001358 is_array<_Up>::value &&
1359 is_same<pointer, element_type*>::value &&
1360 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1361 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1362 >::type;
1363
1364 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001365 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001366 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1367 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1368 >::type;
1369
1370 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001371 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001372 is_assignable<_Dp&, _UDel&&>::value
1373 >::type;
1374
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001376 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001377 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001378 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001379 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001381 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001382 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001383 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001384 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001386 template <class _Pp, bool _Dummy = true,
1387 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001388 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001389 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001390 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001391 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001393 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001394 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1395 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001396 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001397 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001398 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001400 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001401 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001402 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001403 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001404 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001406 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001407 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1408 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001409 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001410 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001411 : __ptr_(__p, _VSTD::move(__d)) {
1412 static_assert(!is_reference<deleter_type>::value,
1413 "rvalue deleter bound to reference");
1414 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001416 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001417 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001418 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001419 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001420 : __ptr_(nullptr, _VSTD::move(__d)) {
1421 static_assert(!is_reference<deleter_type>::value,
1422 "rvalue deleter bound to reference");
1423 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001424
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001425 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001426 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1427 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001428 _LIBCPP_INLINE_VISIBILITY
1429 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001430
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001431 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001432 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001433 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1434 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001435
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001436 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001437 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001438 reset(__u.release());
1439 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1440 return *this;
1441 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001443 template <class _Up, class _Ep,
1444 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1445 class = _EnableIfDeleterConvertible<_Ep>
1446 >
1447 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001448 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001449 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001450 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001452 template <class _Up, class _Ep,
1453 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1454 class = _EnableIfDeleterAssignable<_Ep>
1455 >
1456 _LIBCPP_INLINE_VISIBILITY
1457 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001458 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001459 reset(__u.release());
1460 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1461 return *this;
1462 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001464#ifdef _LIBCPP_CXX03_LANG
1465 unique_ptr(unique_ptr const&) = delete;
1466 unique_ptr& operator=(unique_ptr const&) = delete;
1467#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001468
1469public:
1470 _LIBCPP_INLINE_VISIBILITY
1471 ~unique_ptr() { reset(); }
1472
1473 _LIBCPP_INLINE_VISIBILITY
1474 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1475 reset();
1476 return *this;
1477 }
1478
1479 _LIBCPP_INLINE_VISIBILITY
1480 typename add_lvalue_reference<_Tp>::type
1481 operator[](size_t __i) const {
1482 return __ptr_.first()[__i];
1483 }
1484 _LIBCPP_INLINE_VISIBILITY
1485 pointer get() const _NOEXCEPT {
1486 return __ptr_.first();
1487 }
1488
1489 _LIBCPP_INLINE_VISIBILITY
1490 deleter_type& get_deleter() _NOEXCEPT {
1491 return __ptr_.second();
1492 }
1493
1494 _LIBCPP_INLINE_VISIBILITY
1495 const deleter_type& get_deleter() const _NOEXCEPT {
1496 return __ptr_.second();
1497 }
1498 _LIBCPP_INLINE_VISIBILITY
1499 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1500 return __ptr_.first() != nullptr;
1501 }
1502
1503 _LIBCPP_INLINE_VISIBILITY
1504 pointer release() _NOEXCEPT {
1505 pointer __t = __ptr_.first();
1506 __ptr_.first() = pointer();
1507 return __t;
1508 }
1509
1510 template <class _Pp>
1511 _LIBCPP_INLINE_VISIBILITY
1512 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001513 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001514 >::type
1515 reset(_Pp __p) _NOEXCEPT {
1516 pointer __tmp = __ptr_.first();
1517 __ptr_.first() = __p;
1518 if (__tmp)
1519 __ptr_.second()(__tmp);
1520 }
1521
1522 _LIBCPP_INLINE_VISIBILITY
1523 void reset(nullptr_t = nullptr) _NOEXCEPT {
1524 pointer __tmp = __ptr_.first();
1525 __ptr_.first() = nullptr;
1526 if (__tmp)
1527 __ptr_.second()(__tmp);
1528 }
1529
1530 _LIBCPP_INLINE_VISIBILITY
1531 void swap(unique_ptr& __u) _NOEXCEPT {
1532 __ptr_.swap(__u.__ptr_);
1533 }
1534
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535};
1536
1537template <class _Tp, class _Dp>
1538inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001539typename enable_if<
1540 __is_swappable<_Dp>::value,
1541 void
1542>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001543swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544
1545template <class _T1, class _D1, class _T2, class _D2>
1546inline _LIBCPP_INLINE_VISIBILITY
1547bool
1548operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1549
1550template <class _T1, class _D1, class _T2, class _D2>
1551inline _LIBCPP_INLINE_VISIBILITY
1552bool
1553operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1554
1555template <class _T1, class _D1, class _T2, class _D2>
1556inline _LIBCPP_INLINE_VISIBILITY
1557bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001558operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1559{
1560 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1561 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001562 typedef typename common_type<_P1, _P2>::type _Vp;
1563 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001564}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565
1566template <class _T1, class _D1, class _T2, class _D2>
1567inline _LIBCPP_INLINE_VISIBILITY
1568bool
1569operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1570
1571template <class _T1, class _D1, class _T2, class _D2>
1572inline _LIBCPP_INLINE_VISIBILITY
1573bool
1574operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1575
1576template <class _T1, class _D1, class _T2, class _D2>
1577inline _LIBCPP_INLINE_VISIBILITY
1578bool
1579operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1580
Howard Hinnantb17caf92012-02-21 21:02:58 +00001581template <class _T1, class _D1>
1582inline _LIBCPP_INLINE_VISIBILITY
1583bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001584operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001585{
1586 return !__x;
1587}
1588
1589template <class _T1, class _D1>
1590inline _LIBCPP_INLINE_VISIBILITY
1591bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001592operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001593{
1594 return !__x;
1595}
1596
1597template <class _T1, class _D1>
1598inline _LIBCPP_INLINE_VISIBILITY
1599bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001600operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001601{
1602 return static_cast<bool>(__x);
1603}
1604
1605template <class _T1, class _D1>
1606inline _LIBCPP_INLINE_VISIBILITY
1607bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001608operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001609{
1610 return static_cast<bool>(__x);
1611}
1612
1613template <class _T1, class _D1>
1614inline _LIBCPP_INLINE_VISIBILITY
1615bool
1616operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1617{
1618 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1619 return less<_P1>()(__x.get(), nullptr);
1620}
1621
1622template <class _T1, class _D1>
1623inline _LIBCPP_INLINE_VISIBILITY
1624bool
1625operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1626{
1627 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1628 return less<_P1>()(nullptr, __x.get());
1629}
1630
1631template <class _T1, class _D1>
1632inline _LIBCPP_INLINE_VISIBILITY
1633bool
1634operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1635{
1636 return nullptr < __x;
1637}
1638
1639template <class _T1, class _D1>
1640inline _LIBCPP_INLINE_VISIBILITY
1641bool
1642operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1643{
1644 return __x < nullptr;
1645}
1646
1647template <class _T1, class _D1>
1648inline _LIBCPP_INLINE_VISIBILITY
1649bool
1650operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1651{
1652 return !(nullptr < __x);
1653}
1654
1655template <class _T1, class _D1>
1656inline _LIBCPP_INLINE_VISIBILITY
1657bool
1658operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1659{
1660 return !(__x < nullptr);
1661}
1662
1663template <class _T1, class _D1>
1664inline _LIBCPP_INLINE_VISIBILITY
1665bool
1666operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1667{
1668 return !(__x < nullptr);
1669}
1670
1671template <class _T1, class _D1>
1672inline _LIBCPP_INLINE_VISIBILITY
1673bool
1674operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1675{
1676 return !(nullptr < __x);
1677}
1678
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001679#if _LIBCPP_STD_VER > 11
1680
1681template<class _Tp>
1682struct __unique_if
1683{
1684 typedef unique_ptr<_Tp> __unique_single;
1685};
1686
1687template<class _Tp>
1688struct __unique_if<_Tp[]>
1689{
1690 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1691};
1692
1693template<class _Tp, size_t _Np>
1694struct __unique_if<_Tp[_Np]>
1695{
1696 typedef void __unique_array_known_bound;
1697};
1698
1699template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00001700inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001701typename __unique_if<_Tp>::__unique_single
1702make_unique(_Args&&... __args)
1703{
1704 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
1705}
1706
1707template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00001708inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001709typename __unique_if<_Tp>::__unique_array_unknown_bound
1710make_unique(size_t __n)
1711{
1712 typedef typename remove_extent<_Tp>::type _Up;
1713 return unique_ptr<_Tp>(new _Up[__n]());
1714}
1715
1716template<class _Tp, class... _Args>
1717 typename __unique_if<_Tp>::__unique_array_known_bound
1718 make_unique(_Args&&...) = delete;
1719
1720#endif // _LIBCPP_STD_VER > 11
1721
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001722template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001723#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001724struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001725#else
1726struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001727 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001728#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001729{
1730 typedef unique_ptr<_Tp, _Dp> argument_type;
1731 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001732 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001733 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001734 {
1735 typedef typename argument_type::pointer pointer;
1736 return hash<pointer>()(__ptr.get());
1737 }
1738};
1739
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740struct __destruct_n
1741{
1742private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001743 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744
1745 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001746 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001747 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748
1749 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001750 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751 {}
1752
Howard Hinnant719bda32011-05-28 14:41:13 +00001753 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001754 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001755 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756 {}
1757
Howard Hinnant719bda32011-05-28 14:41:13 +00001758 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001759 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001760 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 {}
1762public:
Howard Hinnant719bda32011-05-28 14:41:13 +00001763 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001764 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765
1766 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05001767 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001768 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769
1770 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001771 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001772 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773
1774 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001776 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777};
1778
1779template <class _Alloc>
1780class __allocator_destructor
1781{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001782 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001784 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
1785 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786private:
1787 _Alloc& __alloc_;
1788 size_type __s_;
1789public:
1790 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00001791 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001794 void operator()(pointer __p) _NOEXCEPT
1795 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796};
1797
1798template <class _InputIterator, class _ForwardIterator>
1799_ForwardIterator
1800uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
1801{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001803#ifndef _LIBCPP_NO_EXCEPTIONS
1804 _ForwardIterator __s = __r;
1805 try
1806 {
1807#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001808 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001809 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001810#ifndef _LIBCPP_NO_EXCEPTIONS
1811 }
1812 catch (...)
1813 {
1814 for (; __s != __r; ++__s)
1815 __s->~value_type();
1816 throw;
1817 }
1818#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 return __r;
1820}
1821
1822template <class _InputIterator, class _Size, class _ForwardIterator>
1823_ForwardIterator
1824uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
1825{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001827#ifndef _LIBCPP_NO_EXCEPTIONS
1828 _ForwardIterator __s = __r;
1829 try
1830 {
1831#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001832 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001833 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001834#ifndef _LIBCPP_NO_EXCEPTIONS
1835 }
1836 catch (...)
1837 {
1838 for (; __s != __r; ++__s)
1839 __s->~value_type();
1840 throw;
1841 }
1842#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 return __r;
1844}
1845
1846template <class _ForwardIterator, class _Tp>
1847void
1848uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
1849{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001851#ifndef _LIBCPP_NO_EXCEPTIONS
1852 _ForwardIterator __s = __f;
1853 try
1854 {
1855#endif
1856 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001857 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001858#ifndef _LIBCPP_NO_EXCEPTIONS
1859 }
1860 catch (...)
1861 {
1862 for (; __s != __f; ++__s)
1863 __s->~value_type();
1864 throw;
1865 }
1866#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867}
1868
1869template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00001870_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
1872{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001874#ifndef _LIBCPP_NO_EXCEPTIONS
1875 _ForwardIterator __s = __f;
1876 try
1877 {
1878#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001879 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001880 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001881#ifndef _LIBCPP_NO_EXCEPTIONS
1882 }
1883 catch (...)
1884 {
1885 for (; __s != __f; ++__s)
1886 __s->~value_type();
1887 throw;
1888 }
1889#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00001890 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891}
1892
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001893#if _LIBCPP_STD_VER > 14
1894
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001895template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04001896inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001897void destroy(_ForwardIterator __first, _ForwardIterator __last) {
1898 for (; __first != __last; ++__first)
1899 _VSTD::destroy_at(_VSTD::addressof(*__first));
1900}
1901
1902template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04001903inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001904_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
1905 for (; __n > 0; (void)++__first, --__n)
1906 _VSTD::destroy_at(_VSTD::addressof(*__first));
1907 return __first;
1908}
1909
Eric Fiselier290c07c2016-10-11 21:13:44 +00001910template <class _ForwardIterator>
1911inline _LIBCPP_INLINE_VISIBILITY
1912void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
1913 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1914 auto __idx = __first;
1915#ifndef _LIBCPP_NO_EXCEPTIONS
1916 try {
1917#endif
1918 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001919 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00001920#ifndef _LIBCPP_NO_EXCEPTIONS
1921 } catch (...) {
1922 _VSTD::destroy(__first, __idx);
1923 throw;
1924 }
1925#endif
1926}
1927
1928template <class _ForwardIterator, class _Size>
1929inline _LIBCPP_INLINE_VISIBILITY
1930_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
1931 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1932 auto __idx = __first;
1933#ifndef _LIBCPP_NO_EXCEPTIONS
1934 try {
1935#endif
1936 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001937 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00001938 return __idx;
1939#ifndef _LIBCPP_NO_EXCEPTIONS
1940 } catch (...) {
1941 _VSTD::destroy(__first, __idx);
1942 throw;
1943 }
1944#endif
1945}
1946
1947
1948template <class _ForwardIterator>
1949inline _LIBCPP_INLINE_VISIBILITY
1950void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
1951 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1952 auto __idx = __first;
1953#ifndef _LIBCPP_NO_EXCEPTIONS
1954 try {
1955#endif
1956 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001957 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00001958#ifndef _LIBCPP_NO_EXCEPTIONS
1959 } catch (...) {
1960 _VSTD::destroy(__first, __idx);
1961 throw;
1962 }
1963#endif
1964}
1965
1966template <class _ForwardIterator, class _Size>
1967inline _LIBCPP_INLINE_VISIBILITY
1968_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
1969 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1970 auto __idx = __first;
1971#ifndef _LIBCPP_NO_EXCEPTIONS
1972 try {
1973#endif
1974 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001975 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00001976 return __idx;
1977#ifndef _LIBCPP_NO_EXCEPTIONS
1978 } catch (...) {
1979 _VSTD::destroy(__first, __idx);
1980 throw;
1981 }
1982#endif
1983}
1984
1985
1986template <class _InputIt, class _ForwardIt>
1987inline _LIBCPP_INLINE_VISIBILITY
1988_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
1989 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
1990 auto __idx = __first_res;
1991#ifndef _LIBCPP_NO_EXCEPTIONS
1992 try {
1993#endif
1994 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001995 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00001996 return __idx;
1997#ifndef _LIBCPP_NO_EXCEPTIONS
1998 } catch (...) {
1999 _VSTD::destroy(__first_res, __idx);
2000 throw;
2001 }
2002#endif
2003}
2004
2005template <class _InputIt, class _Size, class _ForwardIt>
2006inline _LIBCPP_INLINE_VISIBILITY
2007pair<_InputIt, _ForwardIt>
2008uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2009 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2010 auto __idx = __first_res;
2011#ifndef _LIBCPP_NO_EXCEPTIONS
2012 try {
2013#endif
2014 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002015 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002016 return {__first, __idx};
2017#ifndef _LIBCPP_NO_EXCEPTIONS
2018 } catch (...) {
2019 _VSTD::destroy(__first_res, __idx);
2020 throw;
2021 }
2022#endif
2023}
2024
2025
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002026#endif // _LIBCPP_STD_VER > 14
2027
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002028// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2029// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -05002030// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002031#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2032 && defined(__ATOMIC_RELAXED) \
2033 && defined(__ATOMIC_ACQ_REL)
2034# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002035#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002036# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2037#endif
2038
2039template <class _Tp>
2040inline _LIBCPP_INLINE_VISIBILITY _Tp
2041__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2042{
2043#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2044 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2045#else
2046 return __t += 1;
2047#endif
2048}
2049
2050template <class _Tp>
2051inline _LIBCPP_INLINE_VISIBILITY _Tp
2052__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2053{
2054#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2055 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2056#else
2057 return __t -= 1;
2058#endif
2059}
2060
Howard Hinnant756c69b2010-09-22 16:48:34 +00002061class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062 : public std::exception
2063{
2064public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002065 bad_weak_ptr() _NOEXCEPT = default;
2066 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002067 virtual ~bad_weak_ptr() _NOEXCEPT;
2068 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069};
2070
Louis Dionne16fe2952018-07-11 23:14:33 +00002071_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002072void __throw_bad_weak_ptr()
2073{
2074#ifndef _LIBCPP_NO_EXCEPTIONS
2075 throw bad_weak_ptr();
2076#else
2077 _VSTD::abort();
2078#endif
2079}
2080
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002081template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002083class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084{
2085 __shared_count(const __shared_count&);
2086 __shared_count& operator=(const __shared_count&);
2087
2088protected:
2089 long __shared_owners_;
2090 virtual ~__shared_count();
2091private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002092 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093
2094public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002096 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097 : __shared_owners_(__refs) {}
2098
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002099#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002100 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002101 void __add_shared() _NOEXCEPT;
2102 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002103#else
2104 _LIBCPP_INLINE_VISIBILITY
2105 void __add_shared() _NOEXCEPT {
2106 __libcpp_atomic_refcount_increment(__shared_owners_);
2107 }
2108 _LIBCPP_INLINE_VISIBILITY
2109 bool __release_shared() _NOEXCEPT {
2110 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2111 __on_zero_shared();
2112 return true;
2113 }
2114 return false;
2115 }
2116#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002117 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002118 long use_count() const _NOEXCEPT {
2119 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2120 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121};
2122
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002123class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124 : private __shared_count
2125{
2126 long __shared_weak_owners_;
2127
2128public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002130 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 : __shared_count(__refs),
2132 __shared_weak_owners_(__refs) {}
2133protected:
2134 virtual ~__shared_weak_count();
2135
2136public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002137#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002138 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002139 void __add_shared() _NOEXCEPT;
2140 void __add_weak() _NOEXCEPT;
2141 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002142#else
2143 _LIBCPP_INLINE_VISIBILITY
2144 void __add_shared() _NOEXCEPT {
2145 __shared_count::__add_shared();
2146 }
2147 _LIBCPP_INLINE_VISIBILITY
2148 void __add_weak() _NOEXCEPT {
2149 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2150 }
2151 _LIBCPP_INLINE_VISIBILITY
2152 void __release_shared() _NOEXCEPT {
2153 if (__shared_count::__release_shared())
2154 __release_weak();
2155 }
2156#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002157 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002159 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2160 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161
Howard Hinnant719bda32011-05-28 14:41:13 +00002162 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002164 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165};
2166
2167template <class _Tp, class _Dp, class _Alloc>
2168class __shared_ptr_pointer
2169 : public __shared_weak_count
2170{
2171 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2172public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002175 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176
Howard Hinnant72f73582010-08-11 17:04:31 +00002177#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002178 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002179#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180
2181private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002182 virtual void __on_zero_shared() _NOEXCEPT;
2183 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184};
2185
Howard Hinnant72f73582010-08-11 17:04:31 +00002186#ifndef _LIBCPP_NO_RTTI
2187
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188template <class _Tp, class _Dp, class _Alloc>
2189const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002190__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002192 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193}
2194
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002195#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002196
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197template <class _Tp, class _Dp, class _Alloc>
2198void
Howard Hinnant719bda32011-05-28 14:41:13 +00002199__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200{
2201 __data_.first().second()(__data_.first().first());
2202 __data_.first().second().~_Dp();
2203}
2204
2205template <class _Tp, class _Dp, class _Alloc>
2206void
Howard Hinnant719bda32011-05-28 14:41:13 +00002207__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002209 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2210 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002211 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2212
Eric Fiselierf8898c82015-02-05 23:01:40 +00002213 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002215 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216}
2217
2218template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002219struct __shared_ptr_emplace
2220 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221{
Louis Dionneda463cb2020-12-11 12:22:16 -05002222 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002223 _LIBCPP_HIDE_FROM_ABI
2224 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002225 : __storage_(_VSTD::move(__a))
2226 {
2227#if _LIBCPP_STD_VER > 17
2228 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2229 _TpAlloc __tmp(*__get_alloc());
2230 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002231#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002232 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002233#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002234 }
Louis Dionne86549d72020-12-09 16:22:17 -05002235
2236 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002237 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002238
2239 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002240 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241
2242private:
Louis Dionne86549d72020-12-09 16:22:17 -05002243 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002244#if _LIBCPP_STD_VER > 17
2245 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2246 _TpAlloc __tmp(*__get_alloc());
2247 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2248#else
Louis Dionne86549d72020-12-09 16:22:17 -05002249 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002250#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002251 }
2252
2253 virtual void __on_zero_shared_weak() _NOEXCEPT {
2254 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2255 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002256 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002257 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002258 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2259 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2260 }
2261
Louis Dionneda463cb2020-12-11 12:22:16 -05002262 // This class implements the control block for non-array shared pointers created
2263 // through `std::allocate_shared` and `std::make_shared`.
2264 //
2265 // In previous versions of the library, we used a compressed pair to store
2266 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2267 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2268 // we now use a properly aligned char buffer while making sure that we maintain
2269 // the same layout that we had when we used a compressed pair.
2270 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2271 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2272 char __blob_[sizeof(_CompressedPair)];
2273
2274 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2275 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2276 }
2277 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2278 __get_alloc()->~_Alloc();
2279 }
2280 _Alloc* __get_alloc() _NOEXCEPT {
2281 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2282 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2283 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2284 return __alloc;
2285 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08002286 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002287 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2288 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2289 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2290 return __elem;
2291 }
2292 };
2293
2294 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2295 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2296 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297};
2298
Erik Pilkington2a398762017-05-25 15:43:31 +00002299struct __shared_ptr_dummy_rebind_allocator_type;
2300template <>
2301class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2302{
2303public:
2304 template <class _Other>
2305 struct rebind
2306 {
2307 typedef allocator<_Other> other;
2308 };
2309};
2310
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002311template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312
zoecarverd73f42a2020-05-11 18:42:50 -07002313template<class _Tp, class _Up>
2314struct __compatible_with
2315#if _LIBCPP_STD_VER > 14
2316 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2317#else
2318 : is_convertible<_Tp*, _Up*> {};
2319#endif // _LIBCPP_STD_VER > 14
2320
zoecarver9bc39102021-02-19 11:10:36 -08002321template <class _Dp, class _Pt,
2322 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
2323static true_type __well_formed_deleter_test(int);
2324
2325template <class, class>
2326static false_type __well_formed_deleter_test(...);
2327
2328template <class _Dp, class _Pt>
2329struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
2330
2331template<class _Dp, class _Tp, class _Yp>
2332struct __shared_ptr_deleter_ctor_reqs
2333{
2334 static const bool value = __compatible_with<_Tp, _Yp>::value &&
2335 is_move_constructible<_Dp>::value &&
2336 __well_formed_deleter<_Dp, _Tp*>::value;
2337};
2338
Vy Nguyene369bd92020-07-13 12:34:37 -04002339#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2340# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2341#else
2342# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2343#endif
2344
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002346class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002348public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002349#if _LIBCPP_STD_VER > 14
2350 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002351 typedef remove_extent_t<_Tp> element_type;
2352#else
2353 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002354#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002355
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356private:
2357 element_type* __ptr_;
2358 __shared_weak_count* __cntrl_;
2359
2360 struct __nat {int __for_bool_;};
2361public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002363 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002365 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002366 template<class _Yp>
2367 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002368 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002369 template<class _Yp, class _Dp>
2370 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002371 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002372 template<class _Yp, class _Dp, class _Alloc>
2373 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002374 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2376 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002377 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002379 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002383 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002384 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002386 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002387 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002388 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002389 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002391 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002392#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002393 template<class _Yp>
2394 shared_ptr(auto_ptr<_Yp>&& __r,
2395 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002396#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002397 template <class _Yp, class _Dp>
2398 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2399 typename enable_if
2400 <
2401 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002402 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2403 __nat
2404 >::type = __nat());
2405 template <class _Yp, class _Dp>
2406 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2407 typename enable_if
2408 <
2409 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002410 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2411 __nat
2412 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413
2414 ~shared_ptr();
2415
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002417 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002418 template<class _Yp>
2419 typename enable_if
2420 <
zoecarverd73f42a2020-05-11 18:42:50 -07002421 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002422 shared_ptr&
2423 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002425 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002427 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002428 template<class _Yp>
2429 typename enable_if
2430 <
zoecarverd73f42a2020-05-11 18:42:50 -07002431 __compatible_with<_Yp, element_type>::value,
2432 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002433 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002435 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002436#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002437 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002439 typename enable_if
2440 <
2441 !is_array<_Yp>::value &&
2442 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002443 shared_ptr
2444 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002445 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002446#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002447 template <class _Yp, class _Dp>
2448 typename enable_if
2449 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002450 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2451 shared_ptr&
2452 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002454 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002457 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002459 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002460 template<class _Yp>
2461 typename enable_if
2462 <
zoecarverd73f42a2020-05-11 18:42:50 -07002463 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002464 void
2465 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002467 reset(_Yp* __p);
2468 template<class _Yp, class _Dp>
2469 typename enable_if
2470 <
zoecarverd73f42a2020-05-11 18:42:50 -07002471 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002472 void
2473 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002475 reset(_Yp* __p, _Dp __d);
2476 template<class _Yp, class _Dp, class _Alloc>
2477 typename enable_if
2478 <
zoecarverd73f42a2020-05-11 18:42:50 -07002479 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002480 void
2481 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002483 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484
Howard Hinnant756c69b2010-09-22 16:48:34 +00002485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002486 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002488 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2489 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002490 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002491 element_type* operator->() const _NOEXCEPT
2492 {
2493 static_assert(!_VSTD::is_array<_Tp>::value,
2494 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2495 return __ptr_;
2496 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002498 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002500 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002501 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002502 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002503 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002504 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002505 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002507 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002508 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002509 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002511 _LIBCPP_INLINE_VISIBILITY
2512 bool
2513 __owner_equivalent(const shared_ptr& __p) const
2514 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515
zoecarverd73f42a2020-05-11 18:42:50 -07002516#if _LIBCPP_STD_VER > 14
2517 typename add_lvalue_reference<element_type>::type
2518 _LIBCPP_INLINE_VISIBILITY
2519 operator[](ptrdiff_t __i) const
2520 {
2521 static_assert(_VSTD::is_array<_Tp>::value,
2522 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2523 return __ptr_[__i];
2524 }
2525#endif
2526
Howard Hinnant72f73582010-08-11 17:04:31 +00002527#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002530 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002531 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002532 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002533 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002534#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535
Zoe Carverd9040c72019-10-22 15:16:49 +00002536 template<class _Yp, class _CntrlBlk>
2537 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002538 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002539 {
2540 shared_ptr<_Tp> __r;
2541 __r.__ptr_ = __p;
2542 __r.__cntrl_ = __cntrl;
2543 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2544 return __r;
2545 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002546
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002548 template <class _Yp, bool = is_function<_Yp>::value>
2549 struct __shared_ptr_default_allocator
2550 {
2551 typedef allocator<_Yp> type;
2552 };
2553
2554 template <class _Yp>
2555 struct __shared_ptr_default_allocator<_Yp, true>
2556 {
2557 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2558 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002560 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002561 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002562 typename enable_if<is_convertible<_OrigPtr*,
2563 const enable_shared_from_this<_Yp>*
2564 >::value,
2565 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002566 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2567 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002569 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002570 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002571 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002572 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2573 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002574 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575 }
2576
Erik Pilkington2a398762017-05-25 15:43:31 +00002577 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578
zoecarverd73f42a2020-05-11 18:42:50 -07002579 template <class, class _Yp>
2580 struct __shared_ptr_default_delete
2581 : default_delete<_Yp> {};
2582
2583 template <class _Yp, class _Un, size_t _Sz>
2584 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2585 : default_delete<_Yp[]> {};
2586
2587 template <class _Yp, class _Un>
2588 struct __shared_ptr_default_delete<_Yp[], _Un>
2589 : default_delete<_Yp[]> {};
2590
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002591 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2592 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593};
2594
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002595#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2596template<class _Tp>
2597shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2598template<class _Tp, class _Dp>
2599shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2600#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002601
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002603inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002604_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002605shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002606 : __ptr_(nullptr),
2607 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608{
2609}
2610
2611template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002612inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002613_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002614shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002615 : __ptr_(nullptr),
2616 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617{
2618}
2619
2620template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002621template<class _Yp>
2622shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002623 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 : __ptr_(__p)
2625{
2626 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002627 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002628 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2629 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002631 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002632}
2633
2634template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002635template<class _Yp, class _Dp>
2636shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002637 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638 : __ptr_(__p)
2639{
2640#ifndef _LIBCPP_NO_EXCEPTIONS
2641 try
2642 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002643#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002644 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2645 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002646#ifndef _LIBCPP_CXX03_LANG
2647 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2648#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002649 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002650#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002651 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652#ifndef _LIBCPP_NO_EXCEPTIONS
2653 }
2654 catch (...)
2655 {
2656 __d(__p);
2657 throw;
2658 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002659#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660}
2661
2662template<class _Tp>
2663template<class _Dp>
2664shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002665 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666{
2667#ifndef _LIBCPP_NO_EXCEPTIONS
2668 try
2669 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002670#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002671 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2672 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002673#ifndef _LIBCPP_CXX03_LANG
2674 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2675#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002676 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002677#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678#ifndef _LIBCPP_NO_EXCEPTIONS
2679 }
2680 catch (...)
2681 {
2682 __d(__p);
2683 throw;
2684 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002685#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686}
2687
2688template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002689template<class _Yp, class _Dp, class _Alloc>
2690shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002691 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 : __ptr_(__p)
2693{
2694#ifndef _LIBCPP_NO_EXCEPTIONS
2695 try
2696 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002697#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002699 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700 typedef __allocator_destructor<_A2> _D2;
2701 _A2 __a2(__a);
2702 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002703 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2704#ifndef _LIBCPP_CXX03_LANG
2705 _CntrlBlk(__p, _VSTD::move(__d), __a);
2706#else
2707 _CntrlBlk(__p, __d, __a);
2708#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002709 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002710 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711#ifndef _LIBCPP_NO_EXCEPTIONS
2712 }
2713 catch (...)
2714 {
2715 __d(__p);
2716 throw;
2717 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002718#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719}
2720
2721template<class _Tp>
2722template<class _Dp, class _Alloc>
2723shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002724 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725{
2726#ifndef _LIBCPP_NO_EXCEPTIONS
2727 try
2728 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002729#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002731 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 typedef __allocator_destructor<_A2> _D2;
2733 _A2 __a2(__a);
2734 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002735 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2736#ifndef _LIBCPP_CXX03_LANG
2737 _CntrlBlk(__p, _VSTD::move(__d), __a);
2738#else
2739 _CntrlBlk(__p, __d, __a);
2740#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002741 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742#ifndef _LIBCPP_NO_EXCEPTIONS
2743 }
2744 catch (...)
2745 {
2746 __d(__p);
2747 throw;
2748 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002749#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750}
2751
2752template<class _Tp>
2753template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002754inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002755shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 : __ptr_(__p),
2757 __cntrl_(__r.__cntrl_)
2758{
2759 if (__cntrl_)
2760 __cntrl_->__add_shared();
2761}
2762
2763template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002764inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002765shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766 : __ptr_(__r.__ptr_),
2767 __cntrl_(__r.__cntrl_)
2768{
2769 if (__cntrl_)
2770 __cntrl_->__add_shared();
2771}
2772
2773template<class _Tp>
2774template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002775inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002777 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002778 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 : __ptr_(__r.__ptr_),
2780 __cntrl_(__r.__cntrl_)
2781{
2782 if (__cntrl_)
2783 __cntrl_->__add_shared();
2784}
2785
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002787inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002788shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 : __ptr_(__r.__ptr_),
2790 __cntrl_(__r.__cntrl_)
2791{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002792 __r.__ptr_ = nullptr;
2793 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794}
2795
2796template<class _Tp>
2797template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002798inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002800 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002801 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 : __ptr_(__r.__ptr_),
2803 __cntrl_(__r.__cntrl_)
2804{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002805 __r.__ptr_ = nullptr;
2806 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807}
2808
Marshall Clowb22274f2017-01-24 22:22:33 +00002809#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002811template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002812shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002813 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814 : __ptr_(__r.get())
2815{
2816 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2817 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002818 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 __r.release();
2820}
Marshall Clowb22274f2017-01-24 22:22:33 +00002821#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822
2823template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002824template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002826 typename enable_if
2827 <
2828 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002829 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2830 __nat
2831 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 : __ptr_(__r.get())
2833{
Marshall Clow35cde742015-05-10 13:59:45 +00002834#if _LIBCPP_STD_VER > 11
2835 if (__ptr_ == nullptr)
2836 __cntrl_ = nullptr;
2837 else
2838#endif
2839 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002840 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002841 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
Erik Pilkington2a398762017-05-25 15:43:31 +00002842 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002843 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002844 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845 __r.release();
2846}
2847
2848template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002849template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002851 typename enable_if
2852 <
2853 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002854 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2855 __nat
2856 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 : __ptr_(__r.get())
2858{
Marshall Clow35cde742015-05-10 13:59:45 +00002859#if _LIBCPP_STD_VER > 11
2860 if (__ptr_ == nullptr)
2861 __cntrl_ = nullptr;
2862 else
2863#endif
2864 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002865 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002866 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow35cde742015-05-10 13:59:45 +00002867 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00002868 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05002869 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002870 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002871 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872 __r.release();
2873}
2874
Zoe Carver6cd05c32019-08-19 15:47:16 +00002875template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876shared_ptr<_Tp>::~shared_ptr()
2877{
2878 if (__cntrl_)
2879 __cntrl_->__release_shared();
2880}
2881
2882template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002883inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002885shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886{
2887 shared_ptr(__r).swap(*this);
2888 return *this;
2889}
2890
2891template<class _Tp>
2892template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002893inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002894typename enable_if
2895<
zoecarverd73f42a2020-05-11 18:42:50 -07002896 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002897 shared_ptr<_Tp>&
2898>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002899shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900{
2901 shared_ptr(__r).swap(*this);
2902 return *this;
2903}
2904
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002906inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002908shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002910 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 return *this;
2912}
2913
2914template<class _Tp>
2915template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002916inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002917typename enable_if
2918<
zoecarverd73f42a2020-05-11 18:42:50 -07002919 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002920 shared_ptr<_Tp>&
2921>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
2923{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002924 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925 return *this;
2926}
2927
Marshall Clowb22274f2017-01-24 22:22:33 +00002928#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002929template<class _Tp>
2930template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002931inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002932typename enable_if
2933<
2934 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00002935 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002936 shared_ptr<_Tp>
2937>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
2939{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002940 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 return *this;
2942}
Marshall Clowb22274f2017-01-24 22:22:33 +00002943#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944
2945template<class _Tp>
2946template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002947inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002948typename enable_if
2949<
Aditya Kumar7c5db692017-08-20 10:38:55 +00002950 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00002951 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002952 shared_ptr<_Tp>&
2953>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
2955{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002956 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957 return *this;
2958}
2959
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002961inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962void
Howard Hinnant719bda32011-05-28 14:41:13 +00002963shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002965 _VSTD::swap(__ptr_, __r.__ptr_);
2966 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967}
2968
2969template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002970inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971void
Howard Hinnant719bda32011-05-28 14:41:13 +00002972shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973{
2974 shared_ptr().swap(*this);
2975}
2976
2977template<class _Tp>
2978template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002979inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002980typename enable_if
2981<
zoecarverd73f42a2020-05-11 18:42:50 -07002982 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002983 void
2984>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985shared_ptr<_Tp>::reset(_Yp* __p)
2986{
2987 shared_ptr(__p).swap(*this);
2988}
2989
2990template<class _Tp>
2991template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002992inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002993typename enable_if
2994<
zoecarverd73f42a2020-05-11 18:42:50 -07002995 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002996 void
2997>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
2999{
3000 shared_ptr(__p, __d).swap(*this);
3001}
3002
3003template<class _Tp>
3004template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003005inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003006typename enable_if
3007<
zoecarverd73f42a2020-05-11 18:42:50 -07003008 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003009 void
3010>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3012{
3013 shared_ptr(__p, __d, __a).swap(*this);
3014}
3015
Louis Dionne74aef9f2020-12-11 12:20:06 -05003016//
3017// std::allocate_shared and std::make_shared
3018//
3019template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3020_LIBCPP_HIDE_FROM_ABI
3021shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022{
Louis Dionne74aef9f2020-12-11 12:20:06 -05003023 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3024 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3025 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3026 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3027 auto __control_block = __guard.__release_ptr();
3028 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029}
3030
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003031template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3032_LIBCPP_HIDE_FROM_ABI
3033shared_ptr<_Tp> make_shared(_Args&& ...__args)
3034{
3035 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3036}
3037
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038template<class _Tp, class _Up>
3039inline _LIBCPP_INLINE_VISIBILITY
3040bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003041operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042{
3043 return __x.get() == __y.get();
3044}
3045
3046template<class _Tp, class _Up>
3047inline _LIBCPP_INLINE_VISIBILITY
3048bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003049operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050{
3051 return !(__x == __y);
3052}
3053
3054template<class _Tp, class _Up>
3055inline _LIBCPP_INLINE_VISIBILITY
3056bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003057operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003059#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003060 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3061 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003062#else
3063 return less<>()(__x.get(), __y.get());
3064#endif
3065
Howard Hinnantb17caf92012-02-21 21:02:58 +00003066}
3067
3068template<class _Tp, class _Up>
3069inline _LIBCPP_INLINE_VISIBILITY
3070bool
3071operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3072{
3073 return __y < __x;
3074}
3075
3076template<class _Tp, class _Up>
3077inline _LIBCPP_INLINE_VISIBILITY
3078bool
3079operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3080{
3081 return !(__y < __x);
3082}
3083
3084template<class _Tp, class _Up>
3085inline _LIBCPP_INLINE_VISIBILITY
3086bool
3087operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3088{
3089 return !(__x < __y);
3090}
3091
3092template<class _Tp>
3093inline _LIBCPP_INLINE_VISIBILITY
3094bool
3095operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3096{
3097 return !__x;
3098}
3099
3100template<class _Tp>
3101inline _LIBCPP_INLINE_VISIBILITY
3102bool
3103operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3104{
3105 return !__x;
3106}
3107
3108template<class _Tp>
3109inline _LIBCPP_INLINE_VISIBILITY
3110bool
3111operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3112{
3113 return static_cast<bool>(__x);
3114}
3115
3116template<class _Tp>
3117inline _LIBCPP_INLINE_VISIBILITY
3118bool
3119operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3120{
3121 return static_cast<bool>(__x);
3122}
3123
3124template<class _Tp>
3125inline _LIBCPP_INLINE_VISIBILITY
3126bool
3127operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3128{
3129 return less<_Tp*>()(__x.get(), nullptr);
3130}
3131
3132template<class _Tp>
3133inline _LIBCPP_INLINE_VISIBILITY
3134bool
3135operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3136{
3137 return less<_Tp*>()(nullptr, __x.get());
3138}
3139
3140template<class _Tp>
3141inline _LIBCPP_INLINE_VISIBILITY
3142bool
3143operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3144{
3145 return nullptr < __x;
3146}
3147
3148template<class _Tp>
3149inline _LIBCPP_INLINE_VISIBILITY
3150bool
3151operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3152{
3153 return __x < nullptr;
3154}
3155
3156template<class _Tp>
3157inline _LIBCPP_INLINE_VISIBILITY
3158bool
3159operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3160{
3161 return !(nullptr < __x);
3162}
3163
3164template<class _Tp>
3165inline _LIBCPP_INLINE_VISIBILITY
3166bool
3167operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3168{
3169 return !(__x < nullptr);
3170}
3171
3172template<class _Tp>
3173inline _LIBCPP_INLINE_VISIBILITY
3174bool
3175operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3176{
3177 return !(__x < nullptr);
3178}
3179
3180template<class _Tp>
3181inline _LIBCPP_INLINE_VISIBILITY
3182bool
3183operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3184{
3185 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186}
3187
3188template<class _Tp>
3189inline _LIBCPP_INLINE_VISIBILITY
3190void
Howard Hinnant719bda32011-05-28 14:41:13 +00003191swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192{
3193 __x.swap(__y);
3194}
3195
3196template<class _Tp, class _Up>
3197inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003198shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003199static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200{
zoecarverd73f42a2020-05-11 18:42:50 -07003201 return shared_ptr<_Tp>(__r,
3202 static_cast<
3203 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204}
3205
3206template<class _Tp, class _Up>
3207inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003208shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003209dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210{
zoecarverd73f42a2020-05-11 18:42:50 -07003211 typedef typename shared_ptr<_Tp>::element_type _ET;
3212 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3214}
3215
3216template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003217shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003218const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219{
zoecarverd73f42a2020-05-11 18:42:50 -07003220 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003221 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222}
3223
zoecarverd73f42a2020-05-11 18:42:50 -07003224template<class _Tp, class _Up>
3225shared_ptr<_Tp>
3226reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3227{
3228 return shared_ptr<_Tp>(__r,
3229 reinterpret_cast<
3230 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3231}
3232
Howard Hinnant72f73582010-08-11 17:04:31 +00003233#ifndef _LIBCPP_NO_RTTI
3234
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235template<class _Dp, class _Tp>
3236inline _LIBCPP_INLINE_VISIBILITY
3237_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003238get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239{
3240 return __p.template __get_deleter<_Dp>();
3241}
3242
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003243#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003244
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003246class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003248public:
3249 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250private:
3251 element_type* __ptr_;
3252 __shared_weak_count* __cntrl_;
3253
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003254public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003256 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003257 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003258 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3259 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003261 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003262 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003263 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3264 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003265
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003267 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003268 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003269 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3270 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003271 ~weak_ptr();
3272
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003274 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003275 template<class _Yp>
3276 typename enable_if
3277 <
3278 is_convertible<_Yp*, element_type*>::value,
3279 weak_ptr&
3280 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003282 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3283
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003285 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3286 template<class _Yp>
3287 typename enable_if
3288 <
3289 is_convertible<_Yp*, element_type*>::value,
3290 weak_ptr&
3291 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003293 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3294
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003295 template<class _Yp>
3296 typename enable_if
3297 <
3298 is_convertible<_Yp*, element_type*>::value,
3299 weak_ptr&
3300 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003302 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003303
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003305 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003307 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003308
Howard Hinnant756c69b2010-09-22 16:48:34 +00003309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003310 long use_count() const _NOEXCEPT
3311 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003313 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003314 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003315 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003316 template<class _Up>
3317 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003318 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003320 template<class _Up>
3321 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003322 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323 {return __cntrl_ < __r.__cntrl_;}
3324
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003325 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3326 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327};
3328
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003329#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3330template<class _Tp>
3331weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3332#endif
3333
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003335inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003336_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003337weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003338 : __ptr_(nullptr),
3339 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340{
3341}
3342
3343template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003344inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003345weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346 : __ptr_(__r.__ptr_),
3347 __cntrl_(__r.__cntrl_)
3348{
3349 if (__cntrl_)
3350 __cntrl_->__add_weak();
3351}
3352
3353template<class _Tp>
3354template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003355inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003357 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003358 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359 : __ptr_(__r.__ptr_),
3360 __cntrl_(__r.__cntrl_)
3361{
3362 if (__cntrl_)
3363 __cntrl_->__add_weak();
3364}
3365
3366template<class _Tp>
3367template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003368inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003370 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003371 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372 : __ptr_(__r.__ptr_),
3373 __cntrl_(__r.__cntrl_)
3374{
3375 if (__cntrl_)
3376 __cntrl_->__add_weak();
3377}
3378
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003379template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003380inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003381weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3382 : __ptr_(__r.__ptr_),
3383 __cntrl_(__r.__cntrl_)
3384{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003385 __r.__ptr_ = nullptr;
3386 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003387}
3388
3389template<class _Tp>
3390template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003391inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003392weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3393 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3394 _NOEXCEPT
3395 : __ptr_(__r.__ptr_),
3396 __cntrl_(__r.__cntrl_)
3397{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003398 __r.__ptr_ = nullptr;
3399 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003400}
3401
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402template<class _Tp>
3403weak_ptr<_Tp>::~weak_ptr()
3404{
3405 if (__cntrl_)
3406 __cntrl_->__release_weak();
3407}
3408
3409template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003410inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003412weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413{
3414 weak_ptr(__r).swap(*this);
3415 return *this;
3416}
3417
3418template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003419template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003420inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003421typename enable_if
3422<
3423 is_convertible<_Yp*, _Tp*>::value,
3424 weak_ptr<_Tp>&
3425>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003426weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427{
3428 weak_ptr(__r).swap(*this);
3429 return *this;
3430}
3431
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003432template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003433inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003434weak_ptr<_Tp>&
3435weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3436{
3437 weak_ptr(_VSTD::move(__r)).swap(*this);
3438 return *this;
3439}
3440
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003442template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003443inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003444typename enable_if
3445<
3446 is_convertible<_Yp*, _Tp*>::value,
3447 weak_ptr<_Tp>&
3448>::type
3449weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3450{
3451 weak_ptr(_VSTD::move(__r)).swap(*this);
3452 return *this;
3453}
3454
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003455template<class _Tp>
3456template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003457inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003458typename enable_if
3459<
3460 is_convertible<_Yp*, _Tp*>::value,
3461 weak_ptr<_Tp>&
3462>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003463weak_ptr<_Tp>::operator=(shared_ptr<_Yp> 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>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003470inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003471void
Howard Hinnant719bda32011-05-28 14:41:13 +00003472weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003474 _VSTD::swap(__ptr_, __r.__ptr_);
3475 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476}
3477
3478template<class _Tp>
3479inline _LIBCPP_INLINE_VISIBILITY
3480void
Howard Hinnant719bda32011-05-28 14:41:13 +00003481swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482{
3483 __x.swap(__y);
3484}
3485
3486template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003487inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488void
Howard Hinnant719bda32011-05-28 14:41:13 +00003489weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490{
3491 weak_ptr().swap(*this);
3492}
3493
3494template<class _Tp>
3495template<class _Yp>
3496shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003497 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498 : __ptr_(__r.__ptr_),
3499 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3500{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003501 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003502 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503}
3504
3505template<class _Tp>
3506shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003507weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508{
3509 shared_ptr<_Tp> __r;
3510 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3511 if (__r.__cntrl_)
3512 __r.__ptr_ = __ptr_;
3513 return __r;
3514}
3515
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003516#if _LIBCPP_STD_VER > 14
3517template <class _Tp = void> struct owner_less;
3518#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003519template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003520#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521
3522template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003523struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003525{
3526 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003527 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003528 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003530 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003531 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003533 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003534 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003536};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537
3538template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003539struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3541{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003542 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003543 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003544 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003546 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003547 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003549 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003550 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551 {return __x.owner_before(__y);}
3552};
3553
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003554#if _LIBCPP_STD_VER > 14
3555template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003556struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003557{
3558 template <class _Tp, class _Up>
3559 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003560 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003561 {return __x.owner_before(__y);}
3562 template <class _Tp, class _Up>
3563 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003564 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003565 {return __x.owner_before(__y);}
3566 template <class _Tp, class _Up>
3567 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003568 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003569 {return __x.owner_before(__y);}
3570 template <class _Tp, class _Up>
3571 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003572 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003573 {return __x.owner_before(__y);}
3574 typedef void is_transparent;
3575};
3576#endif
3577
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003579class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580{
3581 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003582protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003583 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003584 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003586 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003588 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3589 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003592public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003594 shared_ptr<_Tp> shared_from_this()
3595 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003597 shared_ptr<_Tp const> shared_from_this() const
3598 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599
Eric Fiselier84006862016-06-02 00:15:35 +00003600#if _LIBCPP_STD_VER > 14
3601 _LIBCPP_INLINE_VISIBILITY
3602 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3603 { return __weak_this_; }
3604
3605 _LIBCPP_INLINE_VISIBILITY
3606 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3607 { return __weak_this_; }
3608#endif // _LIBCPP_STD_VER > 14
3609
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610 template <class _Up> friend class shared_ptr;
3611};
3612
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003613template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003614struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003615{
3616 typedef shared_ptr<_Tp> argument_type;
3617 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003618
Howard Hinnant756c69b2010-09-22 16:48:34 +00003619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003620 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003621 {
zoecarverd73f42a2020-05-11 18:42:50 -07003622 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003623 }
3624};
3625
Howard Hinnantc834c512011-11-29 18:15:50 +00003626template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003627inline _LIBCPP_INLINE_VISIBILITY
3628basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003629operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003630
Eric Fiselier9b492672016-06-18 02:12:53 +00003631
3632#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003633
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003634class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003635{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003636 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003637public:
3638 void lock() _NOEXCEPT;
3639 void unlock() _NOEXCEPT;
3640
3641private:
3642 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3643 __sp_mut(const __sp_mut&);
3644 __sp_mut& operator=(const __sp_mut&);
3645
Howard Hinnant8331b762013-03-06 23:30:19 +00003646 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003647};
3648
Mehdi Amini228053d2017-05-04 17:08:54 +00003649_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3650__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003651
3652template <class _Tp>
3653inline _LIBCPP_INLINE_VISIBILITY
3654bool
3655atomic_is_lock_free(const shared_ptr<_Tp>*)
3656{
3657 return false;
3658}
3659
3660template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003661_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003662shared_ptr<_Tp>
3663atomic_load(const shared_ptr<_Tp>* __p)
3664{
3665 __sp_mut& __m = __get_sp_mut(__p);
3666 __m.lock();
3667 shared_ptr<_Tp> __q = *__p;
3668 __m.unlock();
3669 return __q;
3670}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003671
Howard Hinnant9fa30202012-07-30 01:40:57 +00003672template <class _Tp>
3673inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003674_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003675shared_ptr<_Tp>
3676atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3677{
3678 return atomic_load(__p);
3679}
3680
3681template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003682_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003683void
3684atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3685{
3686 __sp_mut& __m = __get_sp_mut(__p);
3687 __m.lock();
3688 __p->swap(__r);
3689 __m.unlock();
3690}
3691
3692template <class _Tp>
3693inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003694_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003695void
3696atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3697{
3698 atomic_store(__p, __r);
3699}
3700
3701template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003702_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003703shared_ptr<_Tp>
3704atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3705{
3706 __sp_mut& __m = __get_sp_mut(__p);
3707 __m.lock();
3708 __p->swap(__r);
3709 __m.unlock();
3710 return __r;
3711}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003712
Howard Hinnant9fa30202012-07-30 01:40:57 +00003713template <class _Tp>
3714inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003715_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003716shared_ptr<_Tp>
3717atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3718{
3719 return atomic_exchange(__p, __r);
3720}
3721
3722template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003723_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003724bool
3725atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3726{
Marshall Clow4201ee82016-05-18 17:50:13 +00003727 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003728 __sp_mut& __m = __get_sp_mut(__p);
3729 __m.lock();
3730 if (__p->__owner_equivalent(*__v))
3731 {
Marshall Clow4201ee82016-05-18 17:50:13 +00003732 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003733 *__p = __w;
3734 __m.unlock();
3735 return true;
3736 }
Marshall Clow4201ee82016-05-18 17:50:13 +00003737 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003738 *__v = *__p;
3739 __m.unlock();
3740 return false;
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 +00003746bool
3747atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3748{
3749 return atomic_compare_exchange_strong(__p, __v, __w);
3750}
3751
3752template <class _Tp>
3753inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003754_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003755bool
3756atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3757 shared_ptr<_Tp> __w, memory_order, memory_order)
3758{
3759 return atomic_compare_exchange_strong(__p, __v, __w);
3760}
3761
3762template <class _Tp>
3763inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003764_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003765bool
3766atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3767 shared_ptr<_Tp> __w, memory_order, memory_order)
3768{
3769 return atomic_compare_exchange_weak(__p, __v, __w);
3770}
3771
Eric Fiselier9b492672016-06-18 02:12:53 +00003772#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003773
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003774//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00003775#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
3776# ifndef _LIBCPP_CXX03_LANG
3777enum class pointer_safety : unsigned char {
3778 relaxed,
3779 preferred,
3780 strict
3781};
3782# endif
3783#else
Howard Hinnant8331b762013-03-06 23:30:19 +00003784struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003786 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787 {
3788 relaxed,
3789 preferred,
3790 strict
3791 };
3792
Howard Hinnant49e145e2012-10-30 19:06:59 +00003793 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794
Howard Hinnant756c69b2010-09-22 16:48:34 +00003795 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00003796 pointer_safety() : __v_() {}
3797
3798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00003799 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801 operator int() const {return __v_;}
3802};
Eric Fiselierab6bb302017-01-05 01:15:42 +00003803#endif
3804
3805#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003806 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00003807_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
3808#else
3809// This function is only offered in C++03 under ABI v1.
3810# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
3811inline _LIBCPP_INLINE_VISIBILITY
3812pointer_safety get_pointer_safety() _NOEXCEPT {
3813 return pointer_safety::relaxed;
3814}
3815# endif
3816#endif
3817
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003819_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
3820_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
3821_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003822_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823
3824template <class _Tp>
3825inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003826_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827undeclare_reachable(_Tp* __p)
3828{
3829 return static_cast<_Tp*>(__undeclare_reachable(__p));
3830}
3831
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003832_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003833
Marshall Clow8982dcd2015-07-13 20:04:56 +00003834// --- Helper for container swap --
3835template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00003836_LIBCPP_INLINE_VISIBILITY
3837void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
3838#if _LIBCPP_STD_VER >= 14
3839 _NOEXCEPT
3840#else
3841 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3842#endif
3843{
3844 using _VSTD::swap;
3845 swap(__a1, __a2);
3846}
3847
3848template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00003849inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00003850void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
3851
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003852template <typename _Alloc>
3853inline _LIBCPP_INLINE_VISIBILITY
3854void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
3855#if _LIBCPP_STD_VER >= 14
3856 _NOEXCEPT
3857#else
3858 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3859#endif
3860{
3861 _VSTD::__swap_allocator(__a1, __a2,
3862 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
3863}
3864
Marshall Clowff91de82015-08-18 19:51:37 +00003865template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00003866struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00003867 _Traits::propagate_on_container_move_assignment::value
3868#if _LIBCPP_STD_VER > 14
3869 || _Traits::is_always_equal::value
3870#else
3871 && is_nothrow_move_assignable<_Alloc>::value
3872#endif
3873 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00003874
Marshall Clowa591b9a2016-07-11 21:38:08 +00003875
Marshall Clowa591b9a2016-07-11 21:38:08 +00003876template <class _Tp, class _Alloc>
3877struct __temp_value {
3878 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00003879
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00003880 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00003881 _Alloc &__a;
3882
3883 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
3884 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003885
Marshall Clowa591b9a2016-07-11 21:38:08 +00003886 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00003887 _LIBCPP_NO_CFI
3888 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
3889 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
3890 _VSTD::forward<_Args>(__args)...);
3891 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003892
Marshall Clowa591b9a2016-07-11 21:38:08 +00003893 ~__temp_value() { _Traits::destroy(__a, __addr()); }
3894 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00003895
Marshall Clowe46031a2018-07-02 18:41:15 +00003896template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00003897struct __is_allocator : false_type {};
3898
3899template<typename _Alloc>
3900struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00003901 typename __void_t<typename _Alloc::value_type>::type,
3902 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
3903 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00003904 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00003905
Eric Fiselier74ebee62019-06-08 01:31:19 +00003906// __builtin_new_allocator -- A non-templated helper for allocating and
3907// deallocating memory using __builtin_operator_new and
3908// __builtin_operator_delete. It should be used in preference to
3909// `std::allocator<T>` to avoid additional instantiations.
3910struct __builtin_new_allocator {
3911 struct __builtin_new_deleter {
3912 typedef void* pointer_type;
3913
3914 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
3915 : __size_(__size), __align_(__align) {}
3916
3917 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003918 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003919 }
3920
3921 private:
3922 size_t __size_;
3923 size_t __align_;
3924 };
3925
3926 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
3927
3928 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003929 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00003930 __builtin_new_deleter(__s, __align));
3931 }
3932
3933 static void __deallocate_bytes(void* __p, size_t __s,
3934 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003935 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003936 }
3937
3938 template <class _Tp>
3939 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3940 static __holder_t __allocate_type(size_t __n) {
3941 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3942 }
3943
3944 template <class _Tp>
3945 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3946 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
3947 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3948 }
3949};
3950
3951
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952_LIBCPP_END_NAMESPACE_STD
3953
Eric Fiselierf4433a32017-05-31 22:07:49 +00003954_LIBCPP_POP_MACROS
3955
Louis Dionne59d0b3c2019-08-05 18:29:14 +00003956#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00003957# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00003958#endif
3959
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960#endif // _LIBCPP_MEMORY