blob: e15f736ffb63d330c94994d470556cb43f31af9a [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>
671#include <cstddef>
672#include <cstdint>
673#include <new>
674#include <utility>
675#include <limits>
676#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 Dionnefa621d72020-12-10 18:28:13 -0500682#include <__memory/allocator_traits.h>
683#include <__memory/base.h>
684#include <__memory/pointer_traits.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500685#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000686#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000687# include <atomic>
688#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000689#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000690
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000691#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000693#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694
Eric Fiselierf4433a32017-05-31 22:07:49 +0000695_LIBCPP_PUSH_MACROS
696#include <__undef_macros>
697
698
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699_LIBCPP_BEGIN_NAMESPACE_STD
700
Eric Fiselier89659d12015-07-07 00:27:16 +0000701template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000702inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000703_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
704#if !defined(_LIBCPP_HAS_NO_THREADS) && \
705 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000706 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000707 return __atomic_load_n(__value, __ATOMIC_RELAXED);
708#else
709 return *__value;
710#endif
711}
712
Kuba Breckade9d6792016-09-04 09:55:12 +0000713template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000714inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000715_ValueType __libcpp_acquire_load(_ValueType const* __value) {
716#if !defined(_LIBCPP_HAS_NO_THREADS) && \
717 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000718 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000719 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
720#else
721 return *__value;
722#endif
723}
724
Louis Dionnefa621d72020-12-10 18:28:13 -0500725template <class _Tp> class allocator;
Marshall Clow0be70c32017-06-14 21:23:57 +0000726
Louis Dionnefa621d72020-12-10 18:28:13 -0500727#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
728template <>
729class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731public:
Louis Dionnefa621d72020-12-10 18:28:13 -0500732 typedef void* pointer;
733 typedef const void* const_pointer;
734 typedef void value_type;
735
736 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737};
738
Louis Dionnefa621d72020-12-10 18:28:13 -0500739template <>
740class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741{
Louis Dionnefa621d72020-12-10 18:28:13 -0500742public:
743 typedef const void* pointer;
744 typedef const void* const_pointer;
745 typedef const void value_type;
746
747 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748};
Louis Dionne99f59432020-09-17 12:06:13 -0400749#endif
Marshall Clow940e01c2015-04-07 05:21:38 +0000750
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751// allocator
752
753template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000754class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755{
756public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400757 typedef size_t size_type;
758 typedef ptrdiff_t difference_type;
759 typedef _Tp value_type;
760 typedef true_type propagate_on_container_move_assignment;
761 typedef true_type is_always_equal;
762
763 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
764 allocator() _NOEXCEPT { }
765
766 template <class _Up>
767 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
768 allocator(const allocator<_Up>&) _NOEXCEPT { }
769
770 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
771 _Tp* allocate(size_t __n) {
772 if (__n > allocator_traits<allocator>::max_size(*this))
773 __throw_length_error("allocator<T>::allocate(size_t n)"
774 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400775 if (__libcpp_is_constant_evaluated()) {
776 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
777 } else {
778 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
779 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400780 }
781
782 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
783 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400784 if (__libcpp_is_constant_evaluated()) {
785 ::operator delete(__p);
786 } else {
787 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
788 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400789 }
790
791 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500792#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -0500793 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
794 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
795 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
796 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
797
Louis Dionne481a2662018-09-23 18:35:00 +0000798 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400799 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
800 typedef allocator<_Up> other;
801 };
Marshall Clowbc759762018-03-20 23:02:53 +0000802
Michael Park99f9e912020-03-04 11:27:14 -0500803 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400804 pointer address(reference __x) const _NOEXCEPT {
805 return _VSTD::addressof(__x);
806 }
Michael Park99f9e912020-03-04 11:27:14 -0500807 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400808 const_pointer address(const_reference __x) const _NOEXCEPT {
809 return _VSTD::addressof(__x);
810 }
Michael Park99f9e912020-03-04 11:27:14 -0500811
Michael Park99f9e912020-03-04 11:27:14 -0500812 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400813 _Tp* allocate(size_t __n, const void*) {
814 return allocate(__n);
815 }
Michael Park99f9e912020-03-04 11:27:14 -0500816
Louis Dionnef8b6c022020-09-21 10:36:37 -0400817 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
818 return size_type(~0) / sizeof(_Tp);
819 }
Michael Park99f9e912020-03-04 11:27:14 -0500820
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400822 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
823 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500824 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400825 }
826
827 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
828 void destroy(pointer __p) {
829 __p->~_Tp();
830 }
Michael Park99f9e912020-03-04 11:27:14 -0500831#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832};
833
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000834template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000835class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000836{
837public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400838 typedef size_t size_type;
839 typedef ptrdiff_t difference_type;
840 typedef const _Tp value_type;
841 typedef true_type propagate_on_container_move_assignment;
842 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000843
Marshall Clowbc759762018-03-20 23:02:53 +0000844 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400845 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +0000846
847 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +0000848 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400849 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -0500850
Louis Dionne99f59432020-09-17 12:06:13 -0400851 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400852 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -0400853 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +0000854 __throw_length_error("allocator<const T>::allocate(size_t n)"
855 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400856 if (__libcpp_is_constant_evaluated()) {
857 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
858 } else {
859 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
860 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000861 }
Michael Park99f9e912020-03-04 11:27:14 -0500862
Louis Dionne99f59432020-09-17 12:06:13 -0400863 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400864 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400865 if (__libcpp_is_constant_evaluated()) {
866 ::operator delete(const_cast<_Tp*>(__p));
867 } else {
868 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
869 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400870 }
Michael Park99f9e912020-03-04 11:27:14 -0500871
Louis Dionnef8b6c022020-09-21 10:36:37 -0400872 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500873#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -0400874 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
875 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
876 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
877 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
878
879 template <class _Up>
880 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
881 typedef allocator<_Up> other;
882 };
883
884 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
885 const_pointer address(const_reference __x) const _NOEXCEPT {
886 return _VSTD::addressof(__x);
887 }
888
889 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
890 const _Tp* allocate(size_t __n, const void*) {
891 return allocate(__n);
892 }
893
894 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
895 return size_type(~0) / sizeof(_Tp);
896 }
Michael Park99f9e912020-03-04 11:27:14 -0500897
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000898 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400899 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
900 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500901 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400902 }
Howard Hinnant9e028f12012-05-01 15:37:54 +0000903
Louis Dionnef8b6c022020-09-21 10:36:37 -0400904 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
905 void destroy(pointer __p) {
906 __p->~_Tp();
907 }
Michael Park99f9e912020-03-04 11:27:14 -0500908#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000909};
910
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400912inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000913bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914
915template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400916inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000917bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918
Louis Dionned6651542020-11-03 12:05:55 -0500919template <class _Alloc, class _Ptr>
920_LIBCPP_INLINE_VISIBILITY
921void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
922 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500923 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500924 typedef allocator_traits<_Alloc> _Traits;
925 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
926 _Traits::construct(__a, _VSTD::__to_address(__begin2),
927#ifdef _LIBCPP_NO_EXCEPTIONS
928 _VSTD::move(*__begin1)
929#else
930 _VSTD::move_if_noexcept(*__begin1)
931#endif
932 );
933 }
934}
935
936template <class _Alloc, class _Tp, typename enable_if<
937 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
938 is_trivially_move_constructible<_Tp>::value
939>::type>
940_LIBCPP_INLINE_VISIBILITY
941void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
942 ptrdiff_t _Np = __end1 - __begin1;
943 if (_Np > 0) {
944 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
945 __begin2 += _Np;
946 }
947}
948
949template <class _Alloc, class _Iter, class _Ptr>
950_LIBCPP_INLINE_VISIBILITY
951void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
952 typedef allocator_traits<_Alloc> _Traits;
953 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
954 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
955 }
956}
957
958template <class _Alloc, class _Source, class _Dest,
959 class _RawSource = typename remove_const<_Source>::type,
960 class _RawDest = typename remove_const<_Dest>::type,
961 class =
962 typename enable_if<
963 is_trivially_copy_constructible<_Dest>::value &&
964 is_same<_RawSource, _RawDest>::value &&
965 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
966 >::type>
967_LIBCPP_INLINE_VISIBILITY
968void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
969 ptrdiff_t _Np = __end1 - __begin1;
970 if (_Np > 0) {
971 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
972 __begin2 += _Np;
973 }
974}
975
976template <class _Alloc, class _Ptr>
977_LIBCPP_INLINE_VISIBILITY
978void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
979 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
980 "The specified type does not meet the requirements of Cpp17MoveInsertable");
981 typedef allocator_traits<_Alloc> _Traits;
982 while (__end1 != __begin1) {
983 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
984#ifdef _LIBCPP_NO_EXCEPTIONS
985 _VSTD::move(*--__end1)
986#else
987 _VSTD::move_if_noexcept(*--__end1)
988#endif
989 );
990 --__end2;
991 }
992}
993
994template <class _Alloc, class _Tp, class = typename enable_if<
995 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
996 is_trivially_move_constructible<_Tp>::value
997>::type>
998_LIBCPP_INLINE_VISIBILITY
999void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1000 ptrdiff_t _Np = __end1 - __begin1;
1001 __end2 -= _Np;
1002 if (_Np > 0)
1003 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1004}
1005
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001007class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 : public iterator<output_iterator_tag,
1009 _Tp, // purposefully not C++03
1010 ptrdiff_t, // purposefully not C++03
1011 _Tp*, // purposefully not C++03
1012 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1013{
1014private:
1015 _OutputIterator __x_;
1016public:
1017 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1018 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1019 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001020 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001021#if _LIBCPP_STD_VER >= 14
1022 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001023 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001024#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1026 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1027 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001028#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001029 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001030#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031};
1032
1033template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001034_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001036get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037{
1038 pair<_Tp*, ptrdiff_t> __r(0, 0);
1039 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1040 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1041 / sizeof(_Tp);
1042 if (__n > __m)
1043 __n = __m;
1044 while (__n > 0)
1045 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001046#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001047 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001048 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001049 align_val_t __al =
1050 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001051 __r.first = static_cast<_Tp*>(::operator new(
1052 __n * sizeof(_Tp), __al, nothrow));
1053 } else {
1054 __r.first = static_cast<_Tp*>(::operator new(
1055 __n * sizeof(_Tp), nothrow));
1056 }
1057#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001058 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001059 {
1060 // Since aligned operator new is unavailable, return an empty
1061 // buffer rather than one with invalid alignment.
1062 return __r;
1063 }
1064
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001066#endif
1067
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 if (__r.first)
1069 {
1070 __r.second = __n;
1071 break;
1072 }
1073 __n /= 2;
1074 }
1075 return __r;
1076}
1077
1078template <class _Tp>
1079inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001080void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1081{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001082 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001083}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084
Marshall Clowb22274f2017-01-24 22:22:33 +00001085#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001087struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088{
1089 _Tp* __ptr_;
1090};
1091
1092template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001093class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094{
1095private:
1096 _Tp* __ptr_;
1097public:
1098 typedef _Tp element_type;
1099
Dimitry Andric47269ce2020-03-13 19:36:26 +01001100 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1101 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1102 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001104 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001106 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001108 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001110 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111
Dimitry Andric47269ce2020-03-13 19:36:26 +01001112 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001114 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1115 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1116 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 {
1118 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001119 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 return __t;
1121 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001122 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123 {
1124 if (__ptr_ != __p)
1125 delete __ptr_;
1126 __ptr_ = __p;
1127 }
1128
Dimitry Andric47269ce2020-03-13 19:36:26 +01001129 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1130 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001132 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 {return auto_ptr<_Up>(release());}
1134};
1135
1136template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001137class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138{
1139public:
1140 typedef void element_type;
1141};
Marshall Clowb22274f2017-01-24 22:22:33 +00001142#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001144// Tag used to default initialize one or both of the pair's elements.
1145struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001146struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001147
Eric Fiselier9d355982017-04-12 23:45:53 +00001148template <class _Tp, int _Idx,
1149 bool _CanBeEmptyBase =
1150 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1151struct __compressed_pair_elem {
1152 typedef _Tp _ParamT;
1153 typedef _Tp& reference;
1154 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001156 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001157 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001158 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1159 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160
Eric Fiselier9d355982017-04-12 23:45:53 +00001161 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001162 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1163 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001164 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001165 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001166 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001167 : __value_(_VSTD::forward<_Up>(__u))
1168 {
1169 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001171
1172#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001173 template <class... _Args, size_t... _Indexes>
1174 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1175 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1176 __tuple_indices<_Indexes...>)
1177 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001178#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001180
Alex Lorenz76132112017-11-09 17:54:49 +00001181 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1182 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001183 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001186 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187};
1188
Eric Fiselier9d355982017-04-12 23:45:53 +00001189template <class _Tp, int _Idx>
1190struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1191 typedef _Tp _ParamT;
1192 typedef _Tp& reference;
1193 typedef const _Tp& const_reference;
1194 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001196 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1197 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1198 __compressed_pair_elem(__default_init_tag) {}
1199 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1200 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201
Eric Fiselier9d355982017-04-12 23:45:53 +00001202 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001203 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1204 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001205 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001206 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001207 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001208 : __value_type(_VSTD::forward<_Up>(__u))
1209 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001211#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001212 template <class... _Args, size_t... _Indexes>
1213 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1214 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1215 __tuple_indices<_Indexes...>)
1216 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001217#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218
Alex Lorenz76132112017-11-09 17:54:49 +00001219 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1220 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001221 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222};
1223
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00001225class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1226 private __compressed_pair_elem<_T2, 1> {
Louis Dionneda463cb2020-12-11 12:22:16 -05001227public:
Eric Fiselier9d355982017-04-12 23:45:53 +00001228 // NOTE: This static assert should never fire because __compressed_pair
1229 // is *almost never* used in a scenario where it's possible for T1 == T2.
1230 // (The exception is std::function where it is possible that the function
1231 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00001232 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05001233 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00001234 "The current implementation is NOT ABI-compatible with the previous "
1235 "implementation for this configuration");
1236
Louis Dionneda463cb2020-12-11 12:22:16 -05001237 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1238 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
1239
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001240 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00001241 class = typename enable_if<
1242 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1243 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1244 >::type
1245 >
Eric Fiselier9d355982017-04-12 23:45:53 +00001246 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001247 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248
Eric Fiselier9d355982017-04-12 23:45:53 +00001249 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001250 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00001251 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001252 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001254#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001255 template <class... _Args1, class... _Args2>
1256 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1257 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1258 tuple<_Args2...> __second_args)
1259 : _Base1(__pc, _VSTD::move(__first_args),
1260 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1261 _Base2(__pc, _VSTD::move(__second_args),
1262 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001263#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264
Eric Fiselier9d355982017-04-12 23:45:53 +00001265 _LIBCPP_INLINE_VISIBILITY
1266 typename _Base1::reference first() _NOEXCEPT {
1267 return static_cast<_Base1&>(*this).__get();
1268 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
Eric Fiselier9d355982017-04-12 23:45:53 +00001270 _LIBCPP_INLINE_VISIBILITY
1271 typename _Base1::const_reference first() const _NOEXCEPT {
1272 return static_cast<_Base1 const&>(*this).__get();
1273 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274
Eric Fiselier9d355982017-04-12 23:45:53 +00001275 _LIBCPP_INLINE_VISIBILITY
1276 typename _Base2::reference second() _NOEXCEPT {
1277 return static_cast<_Base2&>(*this).__get();
1278 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279
Eric Fiselier9d355982017-04-12 23:45:53 +00001280 _LIBCPP_INLINE_VISIBILITY
1281 typename _Base2::const_reference second() const _NOEXCEPT {
1282 return static_cast<_Base2 const&>(*this).__get();
1283 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
Louis Dionneda463cb2020-12-11 12:22:16 -05001285 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1286 static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
1287 return static_cast<_Base1*>(__pair);
1288 }
1289 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1290 static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
1291 return static_cast<_Base2*>(__pair);
1292 }
1293
Eric Fiselier9d355982017-04-12 23:45:53 +00001294 _LIBCPP_INLINE_VISIBILITY
1295 void swap(__compressed_pair& __x)
1296 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1297 __is_nothrow_swappable<_T2>::value)
1298 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001299 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00001300 swap(first(), __x.first());
1301 swap(second(), __x.second());
1302 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303};
1304
1305template <class _T1, class _T2>
1306inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001307void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1308 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1309 __is_nothrow_swappable<_T2>::value) {
1310 __x.swap(__y);
1311}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001313// default_delete
1314
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001316struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001317 static_assert(!is_function<_Tp>::value,
1318 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001319#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001320 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001321#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001322 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001323#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001324 template <class _Up>
1325 _LIBCPP_INLINE_VISIBILITY
1326 default_delete(const default_delete<_Up>&,
1327 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1328 0) _NOEXCEPT {}
1329
1330 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1331 static_assert(sizeof(_Tp) > 0,
1332 "default_delete can not delete incomplete type");
1333 static_assert(!is_void<_Tp>::value,
1334 "default_delete can not delete incomplete type");
1335 delete __ptr;
1336 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337};
1338
1339template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001340struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1341private:
1342 template <class _Up>
1343 struct _EnableIfConvertible
1344 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1345
Howard Hinnant4500ca52011-12-18 21:19:44 +00001346public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001347#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001348 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001349#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001350 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001351#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001352
1353 template <class _Up>
1354 _LIBCPP_INLINE_VISIBILITY
1355 default_delete(const default_delete<_Up[]>&,
1356 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1357
1358 template <class _Up>
1359 _LIBCPP_INLINE_VISIBILITY
1360 typename _EnableIfConvertible<_Up>::type
1361 operator()(_Up* __ptr) const _NOEXCEPT {
1362 static_assert(sizeof(_Tp) > 0,
1363 "default_delete can not delete incomplete type");
1364 static_assert(!is_void<_Tp>::value,
1365 "default_delete can not delete void type");
1366 delete[] __ptr;
1367 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368};
1369
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001370template <class _Deleter>
1371struct __unique_ptr_deleter_sfinae {
1372 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1373 typedef const _Deleter& __lval_ref_type;
1374 typedef _Deleter&& __good_rval_ref_type;
1375 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376};
1377
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001378template <class _Deleter>
1379struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1380 typedef const _Deleter& __lval_ref_type;
1381 typedef const _Deleter&& __bad_rval_ref_type;
1382 typedef false_type __enable_rval_overload;
1383};
1384
1385template <class _Deleter>
1386struct __unique_ptr_deleter_sfinae<_Deleter&> {
1387 typedef _Deleter& __lval_ref_type;
1388 typedef _Deleter&& __bad_rval_ref_type;
1389 typedef false_type __enable_rval_overload;
1390};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001391
Vy Nguyene369bd92020-07-13 12:34:37 -04001392#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1393# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1394#else
1395# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1396#endif
1397
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001398template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001399class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001400public:
1401 typedef _Tp element_type;
1402 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001403 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001404
1405 static_assert(!is_rvalue_reference<deleter_type>::value,
1406 "the specified deleter type cannot be an rvalue reference");
1407
1408private:
1409 __compressed_pair<pointer, deleter_type> __ptr_;
1410
1411 struct __nat { int __for_bool_; };
1412
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001413 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001414
1415 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001416 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001417 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001418
1419 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001420 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001421 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001422
1423 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001424 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001425 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001426
1427 template <bool _Dummy, class _Deleter = typename __dependent_type<
1428 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001429 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001430 typename enable_if<is_default_constructible<_Deleter>::value &&
1431 !is_pointer<_Deleter>::value>::type;
1432
1433 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001434 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001435 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1436
1437 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001438 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001439 is_convertible<typename _UPtr::pointer, pointer>::value &&
1440 !is_array<_Up>::value
1441 >::type;
1442
1443 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001444 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001445 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1446 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1447 >::type;
1448
1449 template <class _UDel>
1450 using _EnableIfDeleterAssignable = typename enable_if<
1451 is_assignable<_Dp&, _UDel&&>::value
1452 >::type;
1453
1454public:
1455 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001456 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001457 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001458 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001459
1460 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001461 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001462 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001463 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001464
1465 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001466 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001467 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001468 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001469
1470 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001471 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001472 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001473 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001474 : __ptr_(__p, __d) {}
1475
1476 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001477 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001478 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001479 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001480 : __ptr_(__p, _VSTD::move(__d)) {
1481 static_assert(!is_reference<deleter_type>::value,
1482 "rvalue deleter bound to reference");
1483 }
1484
1485 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001486 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001487 _LIBCPP_INLINE_VISIBILITY
1488 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1489
1490 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001491 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001492 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1493 }
1494
1495 template <class _Up, class _Ep,
1496 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1497 class = _EnableIfDeleterConvertible<_Ep>
1498 >
1499 _LIBCPP_INLINE_VISIBILITY
1500 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1501 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1502
1503#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1504 template <class _Up>
1505 _LIBCPP_INLINE_VISIBILITY
1506 unique_ptr(auto_ptr<_Up>&& __p,
1507 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001508 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001509 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001510 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001511#endif
1512
1513 _LIBCPP_INLINE_VISIBILITY
1514 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1515 reset(__u.release());
1516 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1517 return *this;
1518 }
1519
1520 template <class _Up, class _Ep,
1521 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1522 class = _EnableIfDeleterAssignable<_Ep>
1523 >
1524 _LIBCPP_INLINE_VISIBILITY
1525 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1526 reset(__u.release());
1527 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1528 return *this;
1529 }
1530
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001531#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1532 template <class _Up>
1533 _LIBCPP_INLINE_VISIBILITY
1534 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1535 is_same<_Dp, default_delete<_Tp> >::value,
1536 unique_ptr&>::type
1537 operator=(auto_ptr<_Up> __p) {
1538 reset(__p.release());
1539 return *this;
1540 }
1541#endif
1542
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001543#ifdef _LIBCPP_CXX03_LANG
1544 unique_ptr(unique_ptr const&) = delete;
1545 unique_ptr& operator=(unique_ptr const&) = delete;
1546#endif
1547
1548
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001549 _LIBCPP_INLINE_VISIBILITY
1550 ~unique_ptr() { reset(); }
1551
1552 _LIBCPP_INLINE_VISIBILITY
1553 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1554 reset();
1555 return *this;
1556 }
1557
1558 _LIBCPP_INLINE_VISIBILITY
1559 typename add_lvalue_reference<_Tp>::type
1560 operator*() const {
1561 return *__ptr_.first();
1562 }
1563 _LIBCPP_INLINE_VISIBILITY
1564 pointer operator->() const _NOEXCEPT {
1565 return __ptr_.first();
1566 }
1567 _LIBCPP_INLINE_VISIBILITY
1568 pointer get() const _NOEXCEPT {
1569 return __ptr_.first();
1570 }
1571 _LIBCPP_INLINE_VISIBILITY
1572 deleter_type& get_deleter() _NOEXCEPT {
1573 return __ptr_.second();
1574 }
1575 _LIBCPP_INLINE_VISIBILITY
1576 const deleter_type& get_deleter() const _NOEXCEPT {
1577 return __ptr_.second();
1578 }
1579 _LIBCPP_INLINE_VISIBILITY
1580 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1581 return __ptr_.first() != nullptr;
1582 }
1583
1584 _LIBCPP_INLINE_VISIBILITY
1585 pointer release() _NOEXCEPT {
1586 pointer __t = __ptr_.first();
1587 __ptr_.first() = pointer();
1588 return __t;
1589 }
1590
1591 _LIBCPP_INLINE_VISIBILITY
1592 void reset(pointer __p = pointer()) _NOEXCEPT {
1593 pointer __tmp = __ptr_.first();
1594 __ptr_.first() = __p;
1595 if (__tmp)
1596 __ptr_.second()(__tmp);
1597 }
1598
1599 _LIBCPP_INLINE_VISIBILITY
1600 void swap(unique_ptr& __u) _NOEXCEPT {
1601 __ptr_.swap(__u.__ptr_);
1602 }
1603};
1604
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001605
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001607class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001609 typedef _Tp element_type;
1610 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001611 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001612
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001614 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615
Eric Fiselier31127cd2017-04-16 02:14:31 +00001616 template <class _From>
1617 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618
Eric Fiselier31127cd2017-04-16 02:14:31 +00001619 template <class _FromElem>
1620 struct _CheckArrayPointerConversion<_FromElem*>
1621 : integral_constant<bool,
1622 is_same<_FromElem*, pointer>::value ||
1623 (is_same<pointer, element_type*>::value &&
1624 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1625 >
1626 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627
Eric Fiselier31127cd2017-04-16 02:14:31 +00001628 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001629
1630 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001631 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001632 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001633
1634 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001635 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001636 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001637
1638 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001639 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001640 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001641
1642 template <bool _Dummy, class _Deleter = typename __dependent_type<
1643 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001644 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001645 typename enable_if<is_default_constructible<_Deleter>::value &&
1646 !is_pointer<_Deleter>::value>::type;
1647
1648 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001649 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001650 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1651
1652 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001653 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001654 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001655 >::type;
1656
1657 template <class _UPtr, class _Up,
1658 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001659 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001660 is_array<_Up>::value &&
1661 is_same<pointer, element_type*>::value &&
1662 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1663 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1664 >::type;
1665
1666 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001667 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001668 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1669 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1670 >::type;
1671
1672 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001673 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001674 is_assignable<_Dp&, _UDel&&>::value
1675 >::type;
1676
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001678 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001679 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001680 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001681 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001683 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001684 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001685 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001686 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001688 template <class _Pp, bool _Dummy = true,
1689 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001690 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001691 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001692 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001693 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001695 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001696 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1697 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001698 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001699 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001700 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001702 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001703 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001704 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001705 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001706 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001708 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001709 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1710 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001711 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001712 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001713 : __ptr_(__p, _VSTD::move(__d)) {
1714 static_assert(!is_reference<deleter_type>::value,
1715 "rvalue deleter bound to reference");
1716 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001718 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001719 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001720 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001721 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001722 : __ptr_(nullptr, _VSTD::move(__d)) {
1723 static_assert(!is_reference<deleter_type>::value,
1724 "rvalue deleter bound to reference");
1725 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001726
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001727 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001728 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1729 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001730 _LIBCPP_INLINE_VISIBILITY
1731 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001732
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001733 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001734 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001735 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1736 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001737
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001738 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001739 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001740 reset(__u.release());
1741 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1742 return *this;
1743 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001745 template <class _Up, class _Ep,
1746 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1747 class = _EnableIfDeleterConvertible<_Ep>
1748 >
1749 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001750 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001751 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001752 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001754 template <class _Up, class _Ep,
1755 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1756 class = _EnableIfDeleterAssignable<_Ep>
1757 >
1758 _LIBCPP_INLINE_VISIBILITY
1759 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001760 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001761 reset(__u.release());
1762 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1763 return *this;
1764 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001766#ifdef _LIBCPP_CXX03_LANG
1767 unique_ptr(unique_ptr const&) = delete;
1768 unique_ptr& operator=(unique_ptr const&) = delete;
1769#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001770
1771public:
1772 _LIBCPP_INLINE_VISIBILITY
1773 ~unique_ptr() { reset(); }
1774
1775 _LIBCPP_INLINE_VISIBILITY
1776 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1777 reset();
1778 return *this;
1779 }
1780
1781 _LIBCPP_INLINE_VISIBILITY
1782 typename add_lvalue_reference<_Tp>::type
1783 operator[](size_t __i) const {
1784 return __ptr_.first()[__i];
1785 }
1786 _LIBCPP_INLINE_VISIBILITY
1787 pointer get() const _NOEXCEPT {
1788 return __ptr_.first();
1789 }
1790
1791 _LIBCPP_INLINE_VISIBILITY
1792 deleter_type& get_deleter() _NOEXCEPT {
1793 return __ptr_.second();
1794 }
1795
1796 _LIBCPP_INLINE_VISIBILITY
1797 const deleter_type& get_deleter() const _NOEXCEPT {
1798 return __ptr_.second();
1799 }
1800 _LIBCPP_INLINE_VISIBILITY
1801 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1802 return __ptr_.first() != nullptr;
1803 }
1804
1805 _LIBCPP_INLINE_VISIBILITY
1806 pointer release() _NOEXCEPT {
1807 pointer __t = __ptr_.first();
1808 __ptr_.first() = pointer();
1809 return __t;
1810 }
1811
1812 template <class _Pp>
1813 _LIBCPP_INLINE_VISIBILITY
1814 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001815 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001816 >::type
1817 reset(_Pp __p) _NOEXCEPT {
1818 pointer __tmp = __ptr_.first();
1819 __ptr_.first() = __p;
1820 if (__tmp)
1821 __ptr_.second()(__tmp);
1822 }
1823
1824 _LIBCPP_INLINE_VISIBILITY
1825 void reset(nullptr_t = nullptr) _NOEXCEPT {
1826 pointer __tmp = __ptr_.first();
1827 __ptr_.first() = nullptr;
1828 if (__tmp)
1829 __ptr_.second()(__tmp);
1830 }
1831
1832 _LIBCPP_INLINE_VISIBILITY
1833 void swap(unique_ptr& __u) _NOEXCEPT {
1834 __ptr_.swap(__u.__ptr_);
1835 }
1836
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837};
1838
1839template <class _Tp, class _Dp>
1840inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001841typename enable_if<
1842 __is_swappable<_Dp>::value,
1843 void
1844>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001845swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846
1847template <class _T1, class _D1, class _T2, class _D2>
1848inline _LIBCPP_INLINE_VISIBILITY
1849bool
1850operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1851
1852template <class _T1, class _D1, class _T2, class _D2>
1853inline _LIBCPP_INLINE_VISIBILITY
1854bool
1855operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1856
1857template <class _T1, class _D1, class _T2, class _D2>
1858inline _LIBCPP_INLINE_VISIBILITY
1859bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001860operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1861{
1862 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1863 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001864 typedef typename common_type<_P1, _P2>::type _Vp;
1865 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001866}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867
1868template <class _T1, class _D1, class _T2, class _D2>
1869inline _LIBCPP_INLINE_VISIBILITY
1870bool
1871operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1872
1873template <class _T1, class _D1, class _T2, class _D2>
1874inline _LIBCPP_INLINE_VISIBILITY
1875bool
1876operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1877
1878template <class _T1, class _D1, class _T2, class _D2>
1879inline _LIBCPP_INLINE_VISIBILITY
1880bool
1881operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1882
Howard Hinnantb17caf92012-02-21 21:02:58 +00001883template <class _T1, class _D1>
1884inline _LIBCPP_INLINE_VISIBILITY
1885bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001886operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001887{
1888 return !__x;
1889}
1890
1891template <class _T1, class _D1>
1892inline _LIBCPP_INLINE_VISIBILITY
1893bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001894operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001895{
1896 return !__x;
1897}
1898
1899template <class _T1, class _D1>
1900inline _LIBCPP_INLINE_VISIBILITY
1901bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001902operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001903{
1904 return static_cast<bool>(__x);
1905}
1906
1907template <class _T1, class _D1>
1908inline _LIBCPP_INLINE_VISIBILITY
1909bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001910operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001911{
1912 return static_cast<bool>(__x);
1913}
1914
1915template <class _T1, class _D1>
1916inline _LIBCPP_INLINE_VISIBILITY
1917bool
1918operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1919{
1920 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1921 return less<_P1>()(__x.get(), nullptr);
1922}
1923
1924template <class _T1, class _D1>
1925inline _LIBCPP_INLINE_VISIBILITY
1926bool
1927operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1928{
1929 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1930 return less<_P1>()(nullptr, __x.get());
1931}
1932
1933template <class _T1, class _D1>
1934inline _LIBCPP_INLINE_VISIBILITY
1935bool
1936operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1937{
1938 return nullptr < __x;
1939}
1940
1941template <class _T1, class _D1>
1942inline _LIBCPP_INLINE_VISIBILITY
1943bool
1944operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1945{
1946 return __x < nullptr;
1947}
1948
1949template <class _T1, class _D1>
1950inline _LIBCPP_INLINE_VISIBILITY
1951bool
1952operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1953{
1954 return !(nullptr < __x);
1955}
1956
1957template <class _T1, class _D1>
1958inline _LIBCPP_INLINE_VISIBILITY
1959bool
1960operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1961{
1962 return !(__x < nullptr);
1963}
1964
1965template <class _T1, class _D1>
1966inline _LIBCPP_INLINE_VISIBILITY
1967bool
1968operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1969{
1970 return !(__x < nullptr);
1971}
1972
1973template <class _T1, class _D1>
1974inline _LIBCPP_INLINE_VISIBILITY
1975bool
1976operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1977{
1978 return !(nullptr < __x);
1979}
1980
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001981#if _LIBCPP_STD_VER > 11
1982
1983template<class _Tp>
1984struct __unique_if
1985{
1986 typedef unique_ptr<_Tp> __unique_single;
1987};
1988
1989template<class _Tp>
1990struct __unique_if<_Tp[]>
1991{
1992 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1993};
1994
1995template<class _Tp, size_t _Np>
1996struct __unique_if<_Tp[_Np]>
1997{
1998 typedef void __unique_array_known_bound;
1999};
2000
2001template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002002inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002003typename __unique_if<_Tp>::__unique_single
2004make_unique(_Args&&... __args)
2005{
2006 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2007}
2008
2009template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002010inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002011typename __unique_if<_Tp>::__unique_array_unknown_bound
2012make_unique(size_t __n)
2013{
2014 typedef typename remove_extent<_Tp>::type _Up;
2015 return unique_ptr<_Tp>(new _Up[__n]());
2016}
2017
2018template<class _Tp, class... _Args>
2019 typename __unique_if<_Tp>::__unique_array_known_bound
2020 make_unique(_Args&&...) = delete;
2021
2022#endif // _LIBCPP_STD_VER > 11
2023
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002024template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002025#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002026struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002027#else
2028struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002029 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002030#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002031{
2032 typedef unique_ptr<_Tp, _Dp> argument_type;
2033 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002034 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002035 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002036 {
2037 typedef typename argument_type::pointer pointer;
2038 return hash<pointer>()(__ptr.get());
2039 }
2040};
2041
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042struct __destruct_n
2043{
2044private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002045 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046
2047 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002048 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002049 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050
2051 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002052 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053 {}
2054
Howard Hinnant719bda32011-05-28 14:41:13 +00002055 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002056 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002057 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058 {}
2059
Howard Hinnant719bda32011-05-28 14:41:13 +00002060 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002061 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002062 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063 {}
2064public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002065 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002066 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067
2068 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002069 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002070 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071
2072 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002073 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002074 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075
2076 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002077 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002078 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079};
2080
2081template <class _Alloc>
2082class __allocator_destructor
2083{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002084 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002086 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2087 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088private:
2089 _Alloc& __alloc_;
2090 size_type __s_;
2091public:
2092 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002093 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002096 void operator()(pointer __p) _NOEXCEPT
2097 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098};
2099
2100template <class _InputIterator, class _ForwardIterator>
2101_ForwardIterator
2102uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2103{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002105#ifndef _LIBCPP_NO_EXCEPTIONS
2106 _ForwardIterator __s = __r;
2107 try
2108 {
2109#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002110 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002111 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002112#ifndef _LIBCPP_NO_EXCEPTIONS
2113 }
2114 catch (...)
2115 {
2116 for (; __s != __r; ++__s)
2117 __s->~value_type();
2118 throw;
2119 }
2120#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 return __r;
2122}
2123
2124template <class _InputIterator, class _Size, class _ForwardIterator>
2125_ForwardIterator
2126uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2127{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002129#ifndef _LIBCPP_NO_EXCEPTIONS
2130 _ForwardIterator __s = __r;
2131 try
2132 {
2133#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002134 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002135 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002136#ifndef _LIBCPP_NO_EXCEPTIONS
2137 }
2138 catch (...)
2139 {
2140 for (; __s != __r; ++__s)
2141 __s->~value_type();
2142 throw;
2143 }
2144#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 return __r;
2146}
2147
2148template <class _ForwardIterator, class _Tp>
2149void
2150uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2151{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002153#ifndef _LIBCPP_NO_EXCEPTIONS
2154 _ForwardIterator __s = __f;
2155 try
2156 {
2157#endif
2158 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002159 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002160#ifndef _LIBCPP_NO_EXCEPTIONS
2161 }
2162 catch (...)
2163 {
2164 for (; __s != __f; ++__s)
2165 __s->~value_type();
2166 throw;
2167 }
2168#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169}
2170
2171template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002172_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2174{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002176#ifndef _LIBCPP_NO_EXCEPTIONS
2177 _ForwardIterator __s = __f;
2178 try
2179 {
2180#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002181 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002182 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002183#ifndef _LIBCPP_NO_EXCEPTIONS
2184 }
2185 catch (...)
2186 {
2187 for (; __s != __f; ++__s)
2188 __s->~value_type();
2189 throw;
2190 }
2191#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002192 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193}
2194
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002195#if _LIBCPP_STD_VER > 14
2196
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002197template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002198inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002199void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2200 for (; __first != __last; ++__first)
2201 _VSTD::destroy_at(_VSTD::addressof(*__first));
2202}
2203
2204template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002205inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002206_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2207 for (; __n > 0; (void)++__first, --__n)
2208 _VSTD::destroy_at(_VSTD::addressof(*__first));
2209 return __first;
2210}
2211
Eric Fiselier290c07c2016-10-11 21:13:44 +00002212template <class _ForwardIterator>
2213inline _LIBCPP_INLINE_VISIBILITY
2214void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2215 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2216 auto __idx = __first;
2217#ifndef _LIBCPP_NO_EXCEPTIONS
2218 try {
2219#endif
2220 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002221 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002222#ifndef _LIBCPP_NO_EXCEPTIONS
2223 } catch (...) {
2224 _VSTD::destroy(__first, __idx);
2225 throw;
2226 }
2227#endif
2228}
2229
2230template <class _ForwardIterator, class _Size>
2231inline _LIBCPP_INLINE_VISIBILITY
2232_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2233 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2234 auto __idx = __first;
2235#ifndef _LIBCPP_NO_EXCEPTIONS
2236 try {
2237#endif
2238 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002239 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002240 return __idx;
2241#ifndef _LIBCPP_NO_EXCEPTIONS
2242 } catch (...) {
2243 _VSTD::destroy(__first, __idx);
2244 throw;
2245 }
2246#endif
2247}
2248
2249
2250template <class _ForwardIterator>
2251inline _LIBCPP_INLINE_VISIBILITY
2252void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2253 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2254 auto __idx = __first;
2255#ifndef _LIBCPP_NO_EXCEPTIONS
2256 try {
2257#endif
2258 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002259 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002260#ifndef _LIBCPP_NO_EXCEPTIONS
2261 } catch (...) {
2262 _VSTD::destroy(__first, __idx);
2263 throw;
2264 }
2265#endif
2266}
2267
2268template <class _ForwardIterator, class _Size>
2269inline _LIBCPP_INLINE_VISIBILITY
2270_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2271 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2272 auto __idx = __first;
2273#ifndef _LIBCPP_NO_EXCEPTIONS
2274 try {
2275#endif
2276 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002277 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002278 return __idx;
2279#ifndef _LIBCPP_NO_EXCEPTIONS
2280 } catch (...) {
2281 _VSTD::destroy(__first, __idx);
2282 throw;
2283 }
2284#endif
2285}
2286
2287
2288template <class _InputIt, class _ForwardIt>
2289inline _LIBCPP_INLINE_VISIBILITY
2290_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2291 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2292 auto __idx = __first_res;
2293#ifndef _LIBCPP_NO_EXCEPTIONS
2294 try {
2295#endif
2296 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002297 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002298 return __idx;
2299#ifndef _LIBCPP_NO_EXCEPTIONS
2300 } catch (...) {
2301 _VSTD::destroy(__first_res, __idx);
2302 throw;
2303 }
2304#endif
2305}
2306
2307template <class _InputIt, class _Size, class _ForwardIt>
2308inline _LIBCPP_INLINE_VISIBILITY
2309pair<_InputIt, _ForwardIt>
2310uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2311 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2312 auto __idx = __first_res;
2313#ifndef _LIBCPP_NO_EXCEPTIONS
2314 try {
2315#endif
2316 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002317 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002318 return {__first, __idx};
2319#ifndef _LIBCPP_NO_EXCEPTIONS
2320 } catch (...) {
2321 _VSTD::destroy(__first_res, __idx);
2322 throw;
2323 }
2324#endif
2325}
2326
2327
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002328#endif // _LIBCPP_STD_VER > 14
2329
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002330// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2331// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00002332// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002333#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2334 && defined(__ATOMIC_RELAXED) \
2335 && defined(__ATOMIC_ACQ_REL)
2336# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002337#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002338# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2339#endif
2340
2341template <class _Tp>
2342inline _LIBCPP_INLINE_VISIBILITY _Tp
2343__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2344{
2345#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2346 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2347#else
2348 return __t += 1;
2349#endif
2350}
2351
2352template <class _Tp>
2353inline _LIBCPP_INLINE_VISIBILITY _Tp
2354__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2355{
2356#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2357 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2358#else
2359 return __t -= 1;
2360#endif
2361}
2362
Howard Hinnant756c69b2010-09-22 16:48:34 +00002363class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364 : public std::exception
2365{
2366public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002367 bad_weak_ptr() _NOEXCEPT = default;
2368 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002369 virtual ~bad_weak_ptr() _NOEXCEPT;
2370 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371};
2372
Louis Dionne16fe2952018-07-11 23:14:33 +00002373_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002374void __throw_bad_weak_ptr()
2375{
2376#ifndef _LIBCPP_NO_EXCEPTIONS
2377 throw bad_weak_ptr();
2378#else
2379 _VSTD::abort();
2380#endif
2381}
2382
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002383template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002384
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002385class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386{
2387 __shared_count(const __shared_count&);
2388 __shared_count& operator=(const __shared_count&);
2389
2390protected:
2391 long __shared_owners_;
2392 virtual ~__shared_count();
2393private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002394 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395
2396public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002398 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399 : __shared_owners_(__refs) {}
2400
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002401#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002402 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002403 void __add_shared() _NOEXCEPT;
2404 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002405#else
2406 _LIBCPP_INLINE_VISIBILITY
2407 void __add_shared() _NOEXCEPT {
2408 __libcpp_atomic_refcount_increment(__shared_owners_);
2409 }
2410 _LIBCPP_INLINE_VISIBILITY
2411 bool __release_shared() _NOEXCEPT {
2412 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2413 __on_zero_shared();
2414 return true;
2415 }
2416 return false;
2417 }
2418#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002419 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002420 long use_count() const _NOEXCEPT {
2421 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2422 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423};
2424
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002425class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426 : private __shared_count
2427{
2428 long __shared_weak_owners_;
2429
2430public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002432 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433 : __shared_count(__refs),
2434 __shared_weak_owners_(__refs) {}
2435protected:
2436 virtual ~__shared_weak_count();
2437
2438public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002439#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002440 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002441 void __add_shared() _NOEXCEPT;
2442 void __add_weak() _NOEXCEPT;
2443 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002444#else
2445 _LIBCPP_INLINE_VISIBILITY
2446 void __add_shared() _NOEXCEPT {
2447 __shared_count::__add_shared();
2448 }
2449 _LIBCPP_INLINE_VISIBILITY
2450 void __add_weak() _NOEXCEPT {
2451 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2452 }
2453 _LIBCPP_INLINE_VISIBILITY
2454 void __release_shared() _NOEXCEPT {
2455 if (__shared_count::__release_shared())
2456 __release_weak();
2457 }
2458#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002459 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002461 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2462 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463
Howard Hinnant719bda32011-05-28 14:41:13 +00002464 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002466 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467};
2468
2469template <class _Tp, class _Dp, class _Alloc>
2470class __shared_ptr_pointer
2471 : public __shared_weak_count
2472{
2473 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2474public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002477 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478
Howard Hinnant72f73582010-08-11 17:04:31 +00002479#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002480 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002481#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482
2483private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002484 virtual void __on_zero_shared() _NOEXCEPT;
2485 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486};
2487
Howard Hinnant72f73582010-08-11 17:04:31 +00002488#ifndef _LIBCPP_NO_RTTI
2489
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490template <class _Tp, class _Dp, class _Alloc>
2491const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002492__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002494 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495}
2496
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002497#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002498
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499template <class _Tp, class _Dp, class _Alloc>
2500void
Howard Hinnant719bda32011-05-28 14:41:13 +00002501__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502{
2503 __data_.first().second()(__data_.first().first());
2504 __data_.first().second().~_Dp();
2505}
2506
2507template <class _Tp, class _Dp, class _Alloc>
2508void
Howard Hinnant719bda32011-05-28 14:41:13 +00002509__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002511 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2512 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002513 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2514
Eric Fiselierf8898c82015-02-05 23:01:40 +00002515 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002517 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518}
2519
2520template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002521struct __shared_ptr_emplace
2522 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523{
Louis Dionneda463cb2020-12-11 12:22:16 -05002524 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002525 _LIBCPP_HIDE_FROM_ABI
2526 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002527 : __storage_(_VSTD::move(__a))
2528 {
2529#if _LIBCPP_STD_VER > 17
2530 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2531 _TpAlloc __tmp(*__get_alloc());
2532 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002533#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002534 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002535#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002536 }
Louis Dionne86549d72020-12-09 16:22:17 -05002537
2538 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002539 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002540
2541 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002542 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543
2544private:
Louis Dionne86549d72020-12-09 16:22:17 -05002545 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002546#if _LIBCPP_STD_VER > 17
2547 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2548 _TpAlloc __tmp(*__get_alloc());
2549 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2550#else
Louis Dionne86549d72020-12-09 16:22:17 -05002551 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002552#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002553 }
2554
2555 virtual void __on_zero_shared_weak() _NOEXCEPT {
2556 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2557 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002558 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002559 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002560 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2561 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2562 }
2563
Louis Dionneda463cb2020-12-11 12:22:16 -05002564 // This class implements the control block for non-array shared pointers created
2565 // through `std::allocate_shared` and `std::make_shared`.
2566 //
2567 // In previous versions of the library, we used a compressed pair to store
2568 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2569 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2570 // we now use a properly aligned char buffer while making sure that we maintain
2571 // the same layout that we had when we used a compressed pair.
2572 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2573 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2574 char __blob_[sizeof(_CompressedPair)];
2575
2576 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2577 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2578 }
2579 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2580 __get_alloc()->~_Alloc();
2581 }
2582 _Alloc* __get_alloc() _NOEXCEPT {
2583 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2584 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2585 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2586 return __alloc;
2587 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08002588 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002589 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2590 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2591 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2592 return __elem;
2593 }
2594 };
2595
2596 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2597 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2598 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599};
2600
Erik Pilkington2a398762017-05-25 15:43:31 +00002601struct __shared_ptr_dummy_rebind_allocator_type;
2602template <>
2603class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2604{
2605public:
2606 template <class _Other>
2607 struct rebind
2608 {
2609 typedef allocator<_Other> other;
2610 };
2611};
2612
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002613template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614
zoecarverd73f42a2020-05-11 18:42:50 -07002615template<class _Tp, class _Up>
2616struct __compatible_with
2617#if _LIBCPP_STD_VER > 14
2618 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2619#else
2620 : is_convertible<_Tp*, _Up*> {};
2621#endif // _LIBCPP_STD_VER > 14
2622
zoecarver9bc39102021-02-19 11:10:36 -08002623template <class _Dp, class _Pt,
2624 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
2625static true_type __well_formed_deleter_test(int);
2626
2627template <class, class>
2628static false_type __well_formed_deleter_test(...);
2629
2630template <class _Dp, class _Pt>
2631struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
2632
2633template<class _Dp, class _Tp, class _Yp>
2634struct __shared_ptr_deleter_ctor_reqs
2635{
2636 static const bool value = __compatible_with<_Tp, _Yp>::value &&
2637 is_move_constructible<_Dp>::value &&
2638 __well_formed_deleter<_Dp, _Tp*>::value;
2639};
2640
Vy Nguyene369bd92020-07-13 12:34:37 -04002641#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2642# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2643#else
2644# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2645#endif
2646
Howard Hinnantc51e1022010-05-11 19:42:16 +00002647template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002648class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002650public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002651#if _LIBCPP_STD_VER > 14
2652 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002653 typedef remove_extent_t<_Tp> element_type;
2654#else
2655 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002656#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002657
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658private:
2659 element_type* __ptr_;
2660 __shared_weak_count* __cntrl_;
2661
2662 struct __nat {int __for_bool_;};
2663public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002665 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002667 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002668 template<class _Yp>
2669 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002670 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002671 template<class _Yp, class _Dp>
2672 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002673 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002674 template<class _Yp, class _Dp, class _Alloc>
2675 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002676 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2678 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002679 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002681 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002685 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002686 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002688 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002689 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002690 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002691 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002693 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002694#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002695 template<class _Yp>
2696 shared_ptr(auto_ptr<_Yp>&& __r,
2697 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002698#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002699 template <class _Yp, class _Dp>
2700 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2701 typename enable_if
2702 <
2703 !is_lvalue_reference<_Dp>::value &&
2704 !is_array<_Yp>::value &&
2705 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2706 __nat
2707 >::type = __nat());
2708 template <class _Yp, class _Dp>
2709 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2710 typename enable_if
2711 <
2712 is_lvalue_reference<_Dp>::value &&
2713 !is_array<_Yp>::value &&
2714 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2715 __nat
2716 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717
2718 ~shared_ptr();
2719
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002721 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002722 template<class _Yp>
2723 typename enable_if
2724 <
zoecarverd73f42a2020-05-11 18:42:50 -07002725 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002726 shared_ptr&
2727 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002729 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002731 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002732 template<class _Yp>
2733 typename enable_if
2734 <
zoecarverd73f42a2020-05-11 18:42:50 -07002735 __compatible_with<_Yp, element_type>::value,
2736 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002737 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002739 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002740#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002741 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002743 typename enable_if
2744 <
2745 !is_array<_Yp>::value &&
2746 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002747 shared_ptr
2748 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002749 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002750#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002751 template <class _Yp, class _Dp>
2752 typename enable_if
2753 <
2754 !is_array<_Yp>::value &&
2755 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2756 shared_ptr&
2757 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002759 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002762 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002764 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002765 template<class _Yp>
2766 typename enable_if
2767 <
zoecarverd73f42a2020-05-11 18:42:50 -07002768 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002769 void
2770 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002772 reset(_Yp* __p);
2773 template<class _Yp, class _Dp>
2774 typename enable_if
2775 <
zoecarverd73f42a2020-05-11 18:42:50 -07002776 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002777 void
2778 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002780 reset(_Yp* __p, _Dp __d);
2781 template<class _Yp, class _Dp, class _Alloc>
2782 typename enable_if
2783 <
zoecarverd73f42a2020-05-11 18:42:50 -07002784 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002785 void
2786 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002788 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789
Howard Hinnant756c69b2010-09-22 16:48:34 +00002790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002791 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002793 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2794 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002795 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002796 element_type* operator->() const _NOEXCEPT
2797 {
2798 static_assert(!_VSTD::is_array<_Tp>::value,
2799 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2800 return __ptr_;
2801 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002803 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002805 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002806 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002807 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002808 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002809 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002810 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002812 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002813 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002814 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002816 _LIBCPP_INLINE_VISIBILITY
2817 bool
2818 __owner_equivalent(const shared_ptr& __p) const
2819 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820
zoecarverd73f42a2020-05-11 18:42:50 -07002821#if _LIBCPP_STD_VER > 14
2822 typename add_lvalue_reference<element_type>::type
2823 _LIBCPP_INLINE_VISIBILITY
2824 operator[](ptrdiff_t __i) const
2825 {
2826 static_assert(_VSTD::is_array<_Tp>::value,
2827 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2828 return __ptr_[__i];
2829 }
2830#endif
2831
Howard Hinnant72f73582010-08-11 17:04:31 +00002832#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002835 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002836 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002837 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002838 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002839#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840
Zoe Carverd9040c72019-10-22 15:16:49 +00002841 template<class _Yp, class _CntrlBlk>
2842 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002843 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002844 {
2845 shared_ptr<_Tp> __r;
2846 __r.__ptr_ = __p;
2847 __r.__cntrl_ = __cntrl;
2848 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2849 return __r;
2850 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002851
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002853 template <class _Yp, bool = is_function<_Yp>::value>
2854 struct __shared_ptr_default_allocator
2855 {
2856 typedef allocator<_Yp> type;
2857 };
2858
2859 template <class _Yp>
2860 struct __shared_ptr_default_allocator<_Yp, true>
2861 {
2862 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2863 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002865 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002866 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002867 typename enable_if<is_convertible<_OrigPtr*,
2868 const enable_shared_from_this<_Yp>*
2869 >::value,
2870 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002871 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2872 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002874 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002875 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002876 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002877 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2878 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002879 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880 }
2881
Erik Pilkington2a398762017-05-25 15:43:31 +00002882 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883
zoecarverd73f42a2020-05-11 18:42:50 -07002884 template <class, class _Yp>
2885 struct __shared_ptr_default_delete
2886 : default_delete<_Yp> {};
2887
2888 template <class _Yp, class _Un, size_t _Sz>
2889 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2890 : default_delete<_Yp[]> {};
2891
2892 template <class _Yp, class _Un>
2893 struct __shared_ptr_default_delete<_Yp[], _Un>
2894 : default_delete<_Yp[]> {};
2895
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002896 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2897 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898};
2899
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002900#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2901template<class _Tp>
2902shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2903template<class _Tp, class _Dp>
2904shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2905#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002906
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002908inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002909_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002910shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002911 : __ptr_(nullptr),
2912 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913{
2914}
2915
2916template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002917inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002918_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002919shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002920 : __ptr_(nullptr),
2921 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922{
2923}
2924
2925template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002926template<class _Yp>
2927shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002928 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002929 : __ptr_(__p)
2930{
2931 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002932 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002933 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2934 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002936 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937}
2938
2939template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002940template<class _Yp, class _Dp>
2941shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002942 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 : __ptr_(__p)
2944{
2945#ifndef _LIBCPP_NO_EXCEPTIONS
2946 try
2947 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002948#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002949 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2950 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002951#ifndef _LIBCPP_CXX03_LANG
2952 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2953#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002954 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002955#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002956 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957#ifndef _LIBCPP_NO_EXCEPTIONS
2958 }
2959 catch (...)
2960 {
2961 __d(__p);
2962 throw;
2963 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002964#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965}
2966
2967template<class _Tp>
2968template<class _Dp>
2969shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002970 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971{
2972#ifndef _LIBCPP_NO_EXCEPTIONS
2973 try
2974 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002975#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002976 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2977 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002978#ifndef _LIBCPP_CXX03_LANG
2979 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2980#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002981 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002982#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983#ifndef _LIBCPP_NO_EXCEPTIONS
2984 }
2985 catch (...)
2986 {
2987 __d(__p);
2988 throw;
2989 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002990#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991}
2992
2993template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002994template<class _Yp, class _Dp, class _Alloc>
2995shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002996 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997 : __ptr_(__p)
2998{
2999#ifndef _LIBCPP_NO_EXCEPTIONS
3000 try
3001 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003002#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003004 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005 typedef __allocator_destructor<_A2> _D2;
3006 _A2 __a2(__a);
3007 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08003008 ::new ((void*)_VSTD::addressof(*__hold2.get()))
3009#ifndef _LIBCPP_CXX03_LANG
3010 _CntrlBlk(__p, _VSTD::move(__d), __a);
3011#else
3012 _CntrlBlk(__p, __d, __a);
3013#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003014 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003015 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016#ifndef _LIBCPP_NO_EXCEPTIONS
3017 }
3018 catch (...)
3019 {
3020 __d(__p);
3021 throw;
3022 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003023#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024}
3025
3026template<class _Tp>
3027template<class _Dp, class _Alloc>
3028shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003029 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003030{
3031#ifndef _LIBCPP_NO_EXCEPTIONS
3032 try
3033 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003034#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003036 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037 typedef __allocator_destructor<_A2> _D2;
3038 _A2 __a2(__a);
3039 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08003040 ::new ((void*)_VSTD::addressof(*__hold2.get()))
3041#ifndef _LIBCPP_CXX03_LANG
3042 _CntrlBlk(__p, _VSTD::move(__d), __a);
3043#else
3044 _CntrlBlk(__p, __d, __a);
3045#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003046 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047#ifndef _LIBCPP_NO_EXCEPTIONS
3048 }
3049 catch (...)
3050 {
3051 __d(__p);
3052 throw;
3053 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003054#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055}
3056
3057template<class _Tp>
3058template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003059inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003060shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061 : __ptr_(__p),
3062 __cntrl_(__r.__cntrl_)
3063{
3064 if (__cntrl_)
3065 __cntrl_->__add_shared();
3066}
3067
3068template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003069inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003070shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003071 : __ptr_(__r.__ptr_),
3072 __cntrl_(__r.__cntrl_)
3073{
3074 if (__cntrl_)
3075 __cntrl_->__add_shared();
3076}
3077
3078template<class _Tp>
3079template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003080inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003082 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003083 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 : __ptr_(__r.__ptr_),
3085 __cntrl_(__r.__cntrl_)
3086{
3087 if (__cntrl_)
3088 __cntrl_->__add_shared();
3089}
3090
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003092inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003093shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094 : __ptr_(__r.__ptr_),
3095 __cntrl_(__r.__cntrl_)
3096{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003097 __r.__ptr_ = nullptr;
3098 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099}
3100
3101template<class _Tp>
3102template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003103inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003105 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003106 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107 : __ptr_(__r.__ptr_),
3108 __cntrl_(__r.__cntrl_)
3109{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003110 __r.__ptr_ = nullptr;
3111 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112}
3113
Marshall Clowb22274f2017-01-24 22:22:33 +00003114#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003116template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003117shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003118 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 : __ptr_(__r.get())
3120{
3121 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3122 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003123 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124 __r.release();
3125}
Marshall Clowb22274f2017-01-24 22:22:33 +00003126#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127
3128template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003129template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003131 typename enable_if
3132 <
3133 !is_lvalue_reference<_Dp>::value &&
3134 !is_array<_Yp>::value &&
3135 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3136 __nat
3137 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138 : __ptr_(__r.get())
3139{
Marshall Clow35cde742015-05-10 13:59:45 +00003140#if _LIBCPP_STD_VER > 11
3141 if (__ptr_ == nullptr)
3142 __cntrl_ = nullptr;
3143 else
3144#endif
3145 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003146 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3147 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3148 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003149 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003150 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 __r.release();
3152}
3153
3154template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003155template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003156shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003157 typename enable_if
3158 <
3159 is_lvalue_reference<_Dp>::value &&
3160 !is_array<_Yp>::value &&
3161 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3162 __nat
3163 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164 : __ptr_(__r.get())
3165{
Marshall Clow35cde742015-05-10 13:59:45 +00003166#if _LIBCPP_STD_VER > 11
3167 if (__ptr_ == nullptr)
3168 __cntrl_ = nullptr;
3169 else
3170#endif
3171 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003172 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003173 typedef __shared_ptr_pointer<_Yp*,
3174 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003175 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003176 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003177 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003178 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179 __r.release();
3180}
3181
Zoe Carver6cd05c32019-08-19 15:47:16 +00003182template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183shared_ptr<_Tp>::~shared_ptr()
3184{
3185 if (__cntrl_)
3186 __cntrl_->__release_shared();
3187}
3188
3189template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003190inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003192shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193{
3194 shared_ptr(__r).swap(*this);
3195 return *this;
3196}
3197
3198template<class _Tp>
3199template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003200inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003201typename enable_if
3202<
zoecarverd73f42a2020-05-11 18:42:50 -07003203 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003204 shared_ptr<_Tp>&
3205>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003206shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207{
3208 shared_ptr(__r).swap(*this);
3209 return *this;
3210}
3211
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003213inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003215shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003217 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218 return *this;
3219}
3220
3221template<class _Tp>
3222template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003223inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003224typename enable_if
3225<
zoecarverd73f42a2020-05-11 18:42:50 -07003226 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003227 shared_ptr<_Tp>&
3228>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3230{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003231 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232 return *this;
3233}
3234
Marshall Clowb22274f2017-01-24 22:22:33 +00003235#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003236template<class _Tp>
3237template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003238inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003239typename enable_if
3240<
3241 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003242 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003243 shared_ptr<_Tp>
3244>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3246{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003247 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248 return *this;
3249}
Marshall Clowb22274f2017-01-24 22:22:33 +00003250#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251
3252template<class _Tp>
3253template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003254inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003255typename enable_if
3256<
3257 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003258 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003259 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003260 shared_ptr<_Tp>&
3261>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3263{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003264 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265 return *this;
3266}
3267
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003269inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270void
Howard Hinnant719bda32011-05-28 14:41:13 +00003271shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003273 _VSTD::swap(__ptr_, __r.__ptr_);
3274 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275}
3276
3277template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003278inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279void
Howard Hinnant719bda32011-05-28 14:41:13 +00003280shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281{
3282 shared_ptr().swap(*this);
3283}
3284
3285template<class _Tp>
3286template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003287inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003288typename enable_if
3289<
zoecarverd73f42a2020-05-11 18:42:50 -07003290 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003291 void
3292>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003293shared_ptr<_Tp>::reset(_Yp* __p)
3294{
3295 shared_ptr(__p).swap(*this);
3296}
3297
3298template<class _Tp>
3299template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003300inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003301typename enable_if
3302<
zoecarverd73f42a2020-05-11 18:42:50 -07003303 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003304 void
3305>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3307{
3308 shared_ptr(__p, __d).swap(*this);
3309}
3310
3311template<class _Tp>
3312template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003313inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003314typename enable_if
3315<
zoecarverd73f42a2020-05-11 18:42:50 -07003316 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003317 void
3318>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3320{
3321 shared_ptr(__p, __d, __a).swap(*this);
3322}
3323
Louis Dionne74aef9f2020-12-11 12:20:06 -05003324//
3325// std::allocate_shared and std::make_shared
3326//
3327template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3328_LIBCPP_HIDE_FROM_ABI
3329shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330{
Louis Dionne74aef9f2020-12-11 12:20:06 -05003331 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3332 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3333 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3334 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3335 auto __control_block = __guard.__release_ptr();
3336 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337}
3338
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003339template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3340_LIBCPP_HIDE_FROM_ABI
3341shared_ptr<_Tp> make_shared(_Args&& ...__args)
3342{
3343 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3344}
3345
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346template<class _Tp, class _Up>
3347inline _LIBCPP_INLINE_VISIBILITY
3348bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003349operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350{
3351 return __x.get() == __y.get();
3352}
3353
3354template<class _Tp, class _Up>
3355inline _LIBCPP_INLINE_VISIBILITY
3356bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003357operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358{
3359 return !(__x == __y);
3360}
3361
3362template<class _Tp, class _Up>
3363inline _LIBCPP_INLINE_VISIBILITY
3364bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003365operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003366{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003367#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003368 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3369 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003370#else
3371 return less<>()(__x.get(), __y.get());
3372#endif
3373
Howard Hinnantb17caf92012-02-21 21:02:58 +00003374}
3375
3376template<class _Tp, class _Up>
3377inline _LIBCPP_INLINE_VISIBILITY
3378bool
3379operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3380{
3381 return __y < __x;
3382}
3383
3384template<class _Tp, class _Up>
3385inline _LIBCPP_INLINE_VISIBILITY
3386bool
3387operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3388{
3389 return !(__y < __x);
3390}
3391
3392template<class _Tp, class _Up>
3393inline _LIBCPP_INLINE_VISIBILITY
3394bool
3395operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3396{
3397 return !(__x < __y);
3398}
3399
3400template<class _Tp>
3401inline _LIBCPP_INLINE_VISIBILITY
3402bool
3403operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3404{
3405 return !__x;
3406}
3407
3408template<class _Tp>
3409inline _LIBCPP_INLINE_VISIBILITY
3410bool
3411operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3412{
3413 return !__x;
3414}
3415
3416template<class _Tp>
3417inline _LIBCPP_INLINE_VISIBILITY
3418bool
3419operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3420{
3421 return static_cast<bool>(__x);
3422}
3423
3424template<class _Tp>
3425inline _LIBCPP_INLINE_VISIBILITY
3426bool
3427operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3428{
3429 return static_cast<bool>(__x);
3430}
3431
3432template<class _Tp>
3433inline _LIBCPP_INLINE_VISIBILITY
3434bool
3435operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3436{
3437 return less<_Tp*>()(__x.get(), nullptr);
3438}
3439
3440template<class _Tp>
3441inline _LIBCPP_INLINE_VISIBILITY
3442bool
3443operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3444{
3445 return less<_Tp*>()(nullptr, __x.get());
3446}
3447
3448template<class _Tp>
3449inline _LIBCPP_INLINE_VISIBILITY
3450bool
3451operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3452{
3453 return nullptr < __x;
3454}
3455
3456template<class _Tp>
3457inline _LIBCPP_INLINE_VISIBILITY
3458bool
3459operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3460{
3461 return __x < nullptr;
3462}
3463
3464template<class _Tp>
3465inline _LIBCPP_INLINE_VISIBILITY
3466bool
3467operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3468{
3469 return !(nullptr < __x);
3470}
3471
3472template<class _Tp>
3473inline _LIBCPP_INLINE_VISIBILITY
3474bool
3475operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3476{
3477 return !(__x < nullptr);
3478}
3479
3480template<class _Tp>
3481inline _LIBCPP_INLINE_VISIBILITY
3482bool
3483operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3484{
3485 return !(__x < nullptr);
3486}
3487
3488template<class _Tp>
3489inline _LIBCPP_INLINE_VISIBILITY
3490bool
3491operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3492{
3493 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494}
3495
3496template<class _Tp>
3497inline _LIBCPP_INLINE_VISIBILITY
3498void
Howard Hinnant719bda32011-05-28 14:41:13 +00003499swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500{
3501 __x.swap(__y);
3502}
3503
3504template<class _Tp, class _Up>
3505inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003506shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003507static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508{
zoecarverd73f42a2020-05-11 18:42:50 -07003509 return shared_ptr<_Tp>(__r,
3510 static_cast<
3511 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512}
3513
3514template<class _Tp, class _Up>
3515inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003516shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003517dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518{
zoecarverd73f42a2020-05-11 18:42:50 -07003519 typedef typename shared_ptr<_Tp>::element_type _ET;
3520 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3522}
3523
3524template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003525shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003526const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527{
zoecarverd73f42a2020-05-11 18:42:50 -07003528 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003529 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530}
3531
zoecarverd73f42a2020-05-11 18:42:50 -07003532template<class _Tp, class _Up>
3533shared_ptr<_Tp>
3534reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3535{
3536 return shared_ptr<_Tp>(__r,
3537 reinterpret_cast<
3538 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3539}
3540
Howard Hinnant72f73582010-08-11 17:04:31 +00003541#ifndef _LIBCPP_NO_RTTI
3542
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543template<class _Dp, class _Tp>
3544inline _LIBCPP_INLINE_VISIBILITY
3545_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003546get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547{
3548 return __p.template __get_deleter<_Dp>();
3549}
3550
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003551#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003552
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003554class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003556public:
3557 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558private:
3559 element_type* __ptr_;
3560 __shared_weak_count* __cntrl_;
3561
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003562public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003564 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003565 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003566 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3567 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003569 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003570 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003571 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3572 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003573
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003575 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003576 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003577 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3578 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003579 ~weak_ptr();
3580
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003582 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003583 template<class _Yp>
3584 typename enable_if
3585 <
3586 is_convertible<_Yp*, element_type*>::value,
3587 weak_ptr&
3588 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003590 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3591
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003593 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3594 template<class _Yp>
3595 typename enable_if
3596 <
3597 is_convertible<_Yp*, element_type*>::value,
3598 weak_ptr&
3599 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003601 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3602
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003603 template<class _Yp>
3604 typename enable_if
3605 <
3606 is_convertible<_Yp*, element_type*>::value,
3607 weak_ptr&
3608 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003610 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003611
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003613 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003615 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003616
Howard Hinnant756c69b2010-09-22 16:48:34 +00003617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003618 long use_count() const _NOEXCEPT
3619 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003621 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003622 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003623 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003624 template<class _Up>
3625 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003626 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003628 template<class _Up>
3629 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003630 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631 {return __cntrl_ < __r.__cntrl_;}
3632
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003633 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3634 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635};
3636
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003637#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3638template<class _Tp>
3639weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3640#endif
3641
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003643inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003644_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003645weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003646 : __ptr_(nullptr),
3647 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648{
3649}
3650
3651template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003652inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003653weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654 : __ptr_(__r.__ptr_),
3655 __cntrl_(__r.__cntrl_)
3656{
3657 if (__cntrl_)
3658 __cntrl_->__add_weak();
3659}
3660
3661template<class _Tp>
3662template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003663inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003665 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003666 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667 : __ptr_(__r.__ptr_),
3668 __cntrl_(__r.__cntrl_)
3669{
3670 if (__cntrl_)
3671 __cntrl_->__add_weak();
3672}
3673
3674template<class _Tp>
3675template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003676inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003678 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003679 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680 : __ptr_(__r.__ptr_),
3681 __cntrl_(__r.__cntrl_)
3682{
3683 if (__cntrl_)
3684 __cntrl_->__add_weak();
3685}
3686
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003687template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003688inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003689weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3690 : __ptr_(__r.__ptr_),
3691 __cntrl_(__r.__cntrl_)
3692{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003693 __r.__ptr_ = nullptr;
3694 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003695}
3696
3697template<class _Tp>
3698template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003699inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003700weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3701 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3702 _NOEXCEPT
3703 : __ptr_(__r.__ptr_),
3704 __cntrl_(__r.__cntrl_)
3705{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003706 __r.__ptr_ = nullptr;
3707 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003708}
3709
Howard Hinnantc51e1022010-05-11 19:42:16 +00003710template<class _Tp>
3711weak_ptr<_Tp>::~weak_ptr()
3712{
3713 if (__cntrl_)
3714 __cntrl_->__release_weak();
3715}
3716
3717template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003718inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003720weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721{
3722 weak_ptr(__r).swap(*this);
3723 return *this;
3724}
3725
3726template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003727template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003728inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003729typename enable_if
3730<
3731 is_convertible<_Yp*, _Tp*>::value,
3732 weak_ptr<_Tp>&
3733>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003734weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735{
3736 weak_ptr(__r).swap(*this);
3737 return *this;
3738}
3739
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003740template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003741inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003742weak_ptr<_Tp>&
3743weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3744{
3745 weak_ptr(_VSTD::move(__r)).swap(*this);
3746 return *this;
3747}
3748
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003750template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003751inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003752typename enable_if
3753<
3754 is_convertible<_Yp*, _Tp*>::value,
3755 weak_ptr<_Tp>&
3756>::type
3757weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3758{
3759 weak_ptr(_VSTD::move(__r)).swap(*this);
3760 return *this;
3761}
3762
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003763template<class _Tp>
3764template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003765inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003766typename enable_if
3767<
3768 is_convertible<_Yp*, _Tp*>::value,
3769 weak_ptr<_Tp>&
3770>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003771weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772{
3773 weak_ptr(__r).swap(*this);
3774 return *this;
3775}
3776
3777template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003778inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779void
Howard Hinnant719bda32011-05-28 14:41:13 +00003780weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003782 _VSTD::swap(__ptr_, __r.__ptr_);
3783 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784}
3785
3786template<class _Tp>
3787inline _LIBCPP_INLINE_VISIBILITY
3788void
Howard Hinnant719bda32011-05-28 14:41:13 +00003789swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790{
3791 __x.swap(__y);
3792}
3793
3794template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003795inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796void
Howard Hinnant719bda32011-05-28 14:41:13 +00003797weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798{
3799 weak_ptr().swap(*this);
3800}
3801
3802template<class _Tp>
3803template<class _Yp>
3804shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003805 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806 : __ptr_(__r.__ptr_),
3807 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3808{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003809 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003810 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811}
3812
3813template<class _Tp>
3814shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003815weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816{
3817 shared_ptr<_Tp> __r;
3818 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3819 if (__r.__cntrl_)
3820 __r.__ptr_ = __ptr_;
3821 return __r;
3822}
3823
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003824#if _LIBCPP_STD_VER > 14
3825template <class _Tp = void> struct owner_less;
3826#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003827template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003828#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003829
3830template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003831struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003833{
3834 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003835 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003836 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003838 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003839 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003841 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003842 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003844};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845
3846template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003847struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3849{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003850 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003851 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003852 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003854 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003855 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003857 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003858 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859 {return __x.owner_before(__y);}
3860};
3861
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003862#if _LIBCPP_STD_VER > 14
3863template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003864struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003865{
3866 template <class _Tp, class _Up>
3867 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003868 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003869 {return __x.owner_before(__y);}
3870 template <class _Tp, class _Up>
3871 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003872 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003873 {return __x.owner_before(__y);}
3874 template <class _Tp, class _Up>
3875 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003876 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003877 {return __x.owner_before(__y);}
3878 template <class _Tp, class _Up>
3879 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003880 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003881 {return __x.owner_before(__y);}
3882 typedef void is_transparent;
3883};
3884#endif
3885
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003887class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888{
3889 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003890protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003891 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003892 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003894 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003896 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3897 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003899 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003900public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003902 shared_ptr<_Tp> shared_from_this()
3903 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003905 shared_ptr<_Tp const> shared_from_this() const
3906 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907
Eric Fiselier84006862016-06-02 00:15:35 +00003908#if _LIBCPP_STD_VER > 14
3909 _LIBCPP_INLINE_VISIBILITY
3910 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3911 { return __weak_this_; }
3912
3913 _LIBCPP_INLINE_VISIBILITY
3914 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3915 { return __weak_this_; }
3916#endif // _LIBCPP_STD_VER > 14
3917
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918 template <class _Up> friend class shared_ptr;
3919};
3920
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003921template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003922struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003923{
3924 typedef shared_ptr<_Tp> argument_type;
3925 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003926
Howard Hinnant756c69b2010-09-22 16:48:34 +00003927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003928 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003929 {
zoecarverd73f42a2020-05-11 18:42:50 -07003930 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003931 }
3932};
3933
Howard Hinnantc834c512011-11-29 18:15:50 +00003934template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003935inline _LIBCPP_INLINE_VISIBILITY
3936basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003937operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003938
Eric Fiselier9b492672016-06-18 02:12:53 +00003939
3940#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003941
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003942class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003943{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003944 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003945public:
3946 void lock() _NOEXCEPT;
3947 void unlock() _NOEXCEPT;
3948
3949private:
3950 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3951 __sp_mut(const __sp_mut&);
3952 __sp_mut& operator=(const __sp_mut&);
3953
Howard Hinnant8331b762013-03-06 23:30:19 +00003954 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003955};
3956
Mehdi Amini228053d2017-05-04 17:08:54 +00003957_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3958__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003959
3960template <class _Tp>
3961inline _LIBCPP_INLINE_VISIBILITY
3962bool
3963atomic_is_lock_free(const shared_ptr<_Tp>*)
3964{
3965 return false;
3966}
3967
3968template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003969_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003970shared_ptr<_Tp>
3971atomic_load(const shared_ptr<_Tp>* __p)
3972{
3973 __sp_mut& __m = __get_sp_mut(__p);
3974 __m.lock();
3975 shared_ptr<_Tp> __q = *__p;
3976 __m.unlock();
3977 return __q;
3978}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003979
Howard Hinnant9fa30202012-07-30 01:40:57 +00003980template <class _Tp>
3981inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003982_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003983shared_ptr<_Tp>
3984atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3985{
3986 return atomic_load(__p);
3987}
3988
3989template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003990_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003991void
3992atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3993{
3994 __sp_mut& __m = __get_sp_mut(__p);
3995 __m.lock();
3996 __p->swap(__r);
3997 __m.unlock();
3998}
3999
4000template <class _Tp>
4001inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004002_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004003void
4004atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4005{
4006 atomic_store(__p, __r);
4007}
4008
4009template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004010_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004011shared_ptr<_Tp>
4012atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4013{
4014 __sp_mut& __m = __get_sp_mut(__p);
4015 __m.lock();
4016 __p->swap(__r);
4017 __m.unlock();
4018 return __r;
4019}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004020
Howard Hinnant9fa30202012-07-30 01:40:57 +00004021template <class _Tp>
4022inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004023_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004024shared_ptr<_Tp>
4025atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4026{
4027 return atomic_exchange(__p, __r);
4028}
4029
4030template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004031_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004032bool
4033atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4034{
Marshall Clow4201ee82016-05-18 17:50:13 +00004035 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004036 __sp_mut& __m = __get_sp_mut(__p);
4037 __m.lock();
4038 if (__p->__owner_equivalent(*__v))
4039 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004040 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004041 *__p = __w;
4042 __m.unlock();
4043 return true;
4044 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004045 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004046 *__v = *__p;
4047 __m.unlock();
4048 return false;
4049}
4050
4051template <class _Tp>
4052inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004053_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004054bool
4055atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4056{
4057 return atomic_compare_exchange_strong(__p, __v, __w);
4058}
4059
4060template <class _Tp>
4061inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004062_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004063bool
4064atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4065 shared_ptr<_Tp> __w, memory_order, memory_order)
4066{
4067 return atomic_compare_exchange_strong(__p, __v, __w);
4068}
4069
4070template <class _Tp>
4071inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004072_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004073bool
4074atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4075 shared_ptr<_Tp> __w, memory_order, memory_order)
4076{
4077 return atomic_compare_exchange_weak(__p, __v, __w);
4078}
4079
Eric Fiselier9b492672016-06-18 02:12:53 +00004080#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004081
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004082//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004083#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4084# ifndef _LIBCPP_CXX03_LANG
4085enum class pointer_safety : unsigned char {
4086 relaxed,
4087 preferred,
4088 strict
4089};
4090# endif
4091#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004092struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004094 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095 {
4096 relaxed,
4097 preferred,
4098 strict
4099 };
4100
Howard Hinnant49e145e2012-10-30 19:06:59 +00004101 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102
Howard Hinnant756c69b2010-09-22 16:48:34 +00004103 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004104 pointer_safety() : __v_() {}
4105
4106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004107 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109 operator int() const {return __v_;}
4110};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004111#endif
4112
4113#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004114 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004115_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4116#else
4117// This function is only offered in C++03 under ABI v1.
4118# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4119inline _LIBCPP_INLINE_VISIBILITY
4120pointer_safety get_pointer_safety() _NOEXCEPT {
4121 return pointer_safety::relaxed;
4122}
4123# endif
4124#endif
4125
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004127_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4128_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4129_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004130_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131
4132template <class _Tp>
4133inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004134_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135undeclare_reachable(_Tp* __p)
4136{
4137 return static_cast<_Tp*>(__undeclare_reachable(__p));
4138}
4139
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004140_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141
Marshall Clow8982dcd2015-07-13 20:04:56 +00004142// --- Helper for container swap --
4143template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004144_LIBCPP_INLINE_VISIBILITY
4145void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4146#if _LIBCPP_STD_VER >= 14
4147 _NOEXCEPT
4148#else
4149 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4150#endif
4151{
4152 using _VSTD::swap;
4153 swap(__a1, __a2);
4154}
4155
4156template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004157inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004158void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4159
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004160template <typename _Alloc>
4161inline _LIBCPP_INLINE_VISIBILITY
4162void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4163#if _LIBCPP_STD_VER >= 14
4164 _NOEXCEPT
4165#else
4166 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4167#endif
4168{
4169 _VSTD::__swap_allocator(__a1, __a2,
4170 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4171}
4172
Marshall Clowff91de82015-08-18 19:51:37 +00004173template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004174struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004175 _Traits::propagate_on_container_move_assignment::value
4176#if _LIBCPP_STD_VER > 14
4177 || _Traits::is_always_equal::value
4178#else
4179 && is_nothrow_move_assignable<_Alloc>::value
4180#endif
4181 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004182
Marshall Clowa591b9a2016-07-11 21:38:08 +00004183
Marshall Clowa591b9a2016-07-11 21:38:08 +00004184template <class _Tp, class _Alloc>
4185struct __temp_value {
4186 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004187
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004188 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004189 _Alloc &__a;
4190
4191 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4192 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004193
Marshall Clowa591b9a2016-07-11 21:38:08 +00004194 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004195 _LIBCPP_NO_CFI
4196 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4197 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4198 _VSTD::forward<_Args>(__args)...);
4199 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004200
Marshall Clowa591b9a2016-07-11 21:38:08 +00004201 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4202 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004203
Marshall Clowe46031a2018-07-02 18:41:15 +00004204template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004205struct __is_allocator : false_type {};
4206
4207template<typename _Alloc>
4208struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004209 typename __void_t<typename _Alloc::value_type>::type,
4210 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4211 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004212 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004213
Eric Fiselier74ebee62019-06-08 01:31:19 +00004214// __builtin_new_allocator -- A non-templated helper for allocating and
4215// deallocating memory using __builtin_operator_new and
4216// __builtin_operator_delete. It should be used in preference to
4217// `std::allocator<T>` to avoid additional instantiations.
4218struct __builtin_new_allocator {
4219 struct __builtin_new_deleter {
4220 typedef void* pointer_type;
4221
4222 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4223 : __size_(__size), __align_(__align) {}
4224
4225 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004226 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004227 }
4228
4229 private:
4230 size_t __size_;
4231 size_t __align_;
4232 };
4233
4234 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4235
4236 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004237 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004238 __builtin_new_deleter(__s, __align));
4239 }
4240
4241 static void __deallocate_bytes(void* __p, size_t __s,
4242 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004243 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004244 }
4245
4246 template <class _Tp>
4247 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4248 static __holder_t __allocate_type(size_t __n) {
4249 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4250 }
4251
4252 template <class _Tp>
4253 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4254 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4255 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4256 }
4257};
4258
4259
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260_LIBCPP_END_NAMESPACE_STD
4261
Eric Fiselierf4433a32017-05-31 22:07:49 +00004262_LIBCPP_POP_MACROS
4263
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004264#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004265# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004266#endif
4267
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268#endif // _LIBCPP_MEMORY