blob: ef1a62b15eac3529cd082260124271923f281c2e [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
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500725template <bool _UsePointerTraits> struct __to_address_helper;
726
727template <> struct __to_address_helper<true> {
728 template <class _Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500729 using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500730
731 template <class _Pointer>
732 _LIBCPP_CONSTEXPR
733 static __return_type<_Pointer>
734 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
735};
736
737template <class _Pointer, bool _Dummy = true>
738using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
739
740
Howard Hinnantc834c512011-11-29 18:15:50 +0000741template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000742inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +0000743_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500744__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745{
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500746 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 return __p;
748}
749
750template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500751inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
752typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
753__to_address(const _Pointer& __p) _NOEXCEPT {
754 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000755}
756
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500757template <> struct __to_address_helper<false> {
758 template <class _Pointer>
759 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000760
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500761 template <class _Pointer>
762 _LIBCPP_CONSTEXPR
763 static __return_type<_Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500764 __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500765};
766
767
768#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000769template <class _Tp>
770inline _LIBCPP_INLINE_VISIBILITY constexpr
771_Tp*
772to_address(_Tp* __p) _NOEXCEPT
773{
774 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
775 return __p;
776}
777
778template <class _Pointer>
Marek Kurdej1f0cde92020-12-06 15:23:46 +0100779inline _LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000780auto
781to_address(const _Pointer& __p) _NOEXCEPT
782{
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500783 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000784}
785#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786
Louis Dionnefa621d72020-12-10 18:28:13 -0500787template <class _Tp> class allocator;
Marshall Clow0be70c32017-06-14 21:23:57 +0000788
Louis Dionnefa621d72020-12-10 18:28:13 -0500789#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
790template <>
791class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793public:
Louis Dionnefa621d72020-12-10 18:28:13 -0500794 typedef void* pointer;
795 typedef const void* const_pointer;
796 typedef void value_type;
797
798 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799};
800
Louis Dionnefa621d72020-12-10 18:28:13 -0500801template <>
802class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803{
Louis Dionnefa621d72020-12-10 18:28:13 -0500804public:
805 typedef const void* pointer;
806 typedef const void* const_pointer;
807 typedef const void value_type;
808
809 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810};
Louis Dionne99f59432020-09-17 12:06:13 -0400811#endif
Marshall Clow940e01c2015-04-07 05:21:38 +0000812
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813// allocator
814
815template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000816class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817{
818public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400819 typedef size_t size_type;
820 typedef ptrdiff_t difference_type;
821 typedef _Tp value_type;
822 typedef true_type propagate_on_container_move_assignment;
823 typedef true_type is_always_equal;
824
825 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
826 allocator() _NOEXCEPT { }
827
828 template <class _Up>
829 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
830 allocator(const allocator<_Up>&) _NOEXCEPT { }
831
832 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
833 _Tp* allocate(size_t __n) {
834 if (__n > allocator_traits<allocator>::max_size(*this))
835 __throw_length_error("allocator<T>::allocate(size_t n)"
836 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400837 if (__libcpp_is_constant_evaluated()) {
838 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
839 } else {
840 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
841 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400842 }
843
844 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
845 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400846 if (__libcpp_is_constant_evaluated()) {
847 ::operator delete(__p);
848 } else {
849 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
850 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400851 }
852
853 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500854#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -0500855 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
856 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
857 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
858 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
859
Louis Dionne481a2662018-09-23 18:35:00 +0000860 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400861 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
862 typedef allocator<_Up> other;
863 };
Marshall Clowbc759762018-03-20 23:02:53 +0000864
Michael Park99f9e912020-03-04 11:27:14 -0500865 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400866 pointer address(reference __x) const _NOEXCEPT {
867 return _VSTD::addressof(__x);
868 }
Michael Park99f9e912020-03-04 11:27:14 -0500869 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400870 const_pointer address(const_reference __x) const _NOEXCEPT {
871 return _VSTD::addressof(__x);
872 }
Michael Park99f9e912020-03-04 11:27:14 -0500873
Michael Park99f9e912020-03-04 11:27:14 -0500874 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400875 _Tp* allocate(size_t __n, const void*) {
876 return allocate(__n);
877 }
Michael Park99f9e912020-03-04 11:27:14 -0500878
Louis Dionnef8b6c022020-09-21 10:36:37 -0400879 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
880 return size_type(~0) / sizeof(_Tp);
881 }
Michael Park99f9e912020-03-04 11:27:14 -0500882
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400884 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
885 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500886 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400887 }
888
889 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
890 void destroy(pointer __p) {
891 __p->~_Tp();
892 }
Michael Park99f9e912020-03-04 11:27:14 -0500893#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894};
895
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000896template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000897class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000898{
899public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400900 typedef size_t size_type;
901 typedef ptrdiff_t difference_type;
902 typedef const _Tp value_type;
903 typedef true_type propagate_on_container_move_assignment;
904 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000905
Marshall Clowbc759762018-03-20 23:02:53 +0000906 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400907 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +0000908
909 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +0000910 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400911 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -0500912
Louis Dionne99f59432020-09-17 12:06:13 -0400913 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400914 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -0400915 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +0000916 __throw_length_error("allocator<const T>::allocate(size_t n)"
917 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400918 if (__libcpp_is_constant_evaluated()) {
919 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
920 } else {
921 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
922 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000923 }
Michael Park99f9e912020-03-04 11:27:14 -0500924
Louis Dionne99f59432020-09-17 12:06:13 -0400925 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400926 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400927 if (__libcpp_is_constant_evaluated()) {
928 ::operator delete(const_cast<_Tp*>(__p));
929 } else {
930 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
931 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400932 }
Michael Park99f9e912020-03-04 11:27:14 -0500933
Louis Dionnef8b6c022020-09-21 10:36:37 -0400934 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500935#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -0400936 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
937 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
938 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
939 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
940
941 template <class _Up>
942 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
943 typedef allocator<_Up> other;
944 };
945
946 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
947 const_pointer address(const_reference __x) const _NOEXCEPT {
948 return _VSTD::addressof(__x);
949 }
950
951 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
952 const _Tp* allocate(size_t __n, const void*) {
953 return allocate(__n);
954 }
955
956 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
957 return size_type(~0) / sizeof(_Tp);
958 }
Michael Park99f9e912020-03-04 11:27:14 -0500959
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000960 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400961 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
962 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500963 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400964 }
Howard Hinnant9e028f12012-05-01 15:37:54 +0000965
Louis Dionnef8b6c022020-09-21 10:36:37 -0400966 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
967 void destroy(pointer __p) {
968 __p->~_Tp();
969 }
Michael Park99f9e912020-03-04 11:27:14 -0500970#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000971};
972
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400974inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000975bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976
977template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400978inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000979bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980
Louis Dionned6651542020-11-03 12:05:55 -0500981template <class _Alloc, class _Ptr>
982_LIBCPP_INLINE_VISIBILITY
983void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
984 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500985 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500986 typedef allocator_traits<_Alloc> _Traits;
987 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
988 _Traits::construct(__a, _VSTD::__to_address(__begin2),
989#ifdef _LIBCPP_NO_EXCEPTIONS
990 _VSTD::move(*__begin1)
991#else
992 _VSTD::move_if_noexcept(*__begin1)
993#endif
994 );
995 }
996}
997
998template <class _Alloc, class _Tp, typename enable_if<
999 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1000 is_trivially_move_constructible<_Tp>::value
1001>::type>
1002_LIBCPP_INLINE_VISIBILITY
1003void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1004 ptrdiff_t _Np = __end1 - __begin1;
1005 if (_Np > 0) {
1006 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1007 __begin2 += _Np;
1008 }
1009}
1010
1011template <class _Alloc, class _Iter, class _Ptr>
1012_LIBCPP_INLINE_VISIBILITY
1013void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1014 typedef allocator_traits<_Alloc> _Traits;
1015 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1016 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1017 }
1018}
1019
1020template <class _Alloc, class _Source, class _Dest,
1021 class _RawSource = typename remove_const<_Source>::type,
1022 class _RawDest = typename remove_const<_Dest>::type,
1023 class =
1024 typename enable_if<
1025 is_trivially_copy_constructible<_Dest>::value &&
1026 is_same<_RawSource, _RawDest>::value &&
1027 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1028 >::type>
1029_LIBCPP_INLINE_VISIBILITY
1030void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1031 ptrdiff_t _Np = __end1 - __begin1;
1032 if (_Np > 0) {
1033 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1034 __begin2 += _Np;
1035 }
1036}
1037
1038template <class _Alloc, class _Ptr>
1039_LIBCPP_INLINE_VISIBILITY
1040void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1041 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1042 "The specified type does not meet the requirements of Cpp17MoveInsertable");
1043 typedef allocator_traits<_Alloc> _Traits;
1044 while (__end1 != __begin1) {
1045 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1046#ifdef _LIBCPP_NO_EXCEPTIONS
1047 _VSTD::move(*--__end1)
1048#else
1049 _VSTD::move_if_noexcept(*--__end1)
1050#endif
1051 );
1052 --__end2;
1053 }
1054}
1055
1056template <class _Alloc, class _Tp, class = typename enable_if<
1057 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1058 is_trivially_move_constructible<_Tp>::value
1059>::type>
1060_LIBCPP_INLINE_VISIBILITY
1061void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1062 ptrdiff_t _Np = __end1 - __begin1;
1063 __end2 -= _Np;
1064 if (_Np > 0)
1065 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1066}
1067
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001069class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 : public iterator<output_iterator_tag,
1071 _Tp, // purposefully not C++03
1072 ptrdiff_t, // purposefully not C++03
1073 _Tp*, // purposefully not C++03
1074 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1075{
1076private:
1077 _OutputIterator __x_;
1078public:
1079 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1080 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1081 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001082 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001083#if _LIBCPP_STD_VER >= 14
1084 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001085 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001086#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1088 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1089 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001090#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001091 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001092#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093};
1094
1095template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001096_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001098get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099{
1100 pair<_Tp*, ptrdiff_t> __r(0, 0);
1101 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1102 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1103 / sizeof(_Tp);
1104 if (__n > __m)
1105 __n = __m;
1106 while (__n > 0)
1107 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001108#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001109 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001110 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001111 align_val_t __al =
1112 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001113 __r.first = static_cast<_Tp*>(::operator new(
1114 __n * sizeof(_Tp), __al, nothrow));
1115 } else {
1116 __r.first = static_cast<_Tp*>(::operator new(
1117 __n * sizeof(_Tp), nothrow));
1118 }
1119#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001120 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001121 {
1122 // Since aligned operator new is unavailable, return an empty
1123 // buffer rather than one with invalid alignment.
1124 return __r;
1125 }
1126
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001128#endif
1129
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 if (__r.first)
1131 {
1132 __r.second = __n;
1133 break;
1134 }
1135 __n /= 2;
1136 }
1137 return __r;
1138}
1139
1140template <class _Tp>
1141inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001142void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1143{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001144 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001145}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146
Marshall Clowb22274f2017-01-24 22:22:33 +00001147#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001149struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
1151 _Tp* __ptr_;
1152};
1153
1154template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001155class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156{
1157private:
1158 _Tp* __ptr_;
1159public:
1160 typedef _Tp element_type;
1161
Dimitry Andric47269ce2020-03-13 19:36:26 +01001162 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1163 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1164 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001166 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001168 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001170 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001172 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173
Dimitry Andric47269ce2020-03-13 19:36:26 +01001174 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001176 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1177 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1178 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 {
1180 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001181 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 return __t;
1183 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001184 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 {
1186 if (__ptr_ != __p)
1187 delete __ptr_;
1188 __ptr_ = __p;
1189 }
1190
Dimitry Andric47269ce2020-03-13 19:36:26 +01001191 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1192 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001194 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 {return auto_ptr<_Up>(release());}
1196};
1197
1198template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001199class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200{
1201public:
1202 typedef void element_type;
1203};
Marshall Clowb22274f2017-01-24 22:22:33 +00001204#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001206// Tag used to default initialize one or both of the pair's elements.
1207struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001208struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001209
Eric Fiselier9d355982017-04-12 23:45:53 +00001210template <class _Tp, int _Idx,
1211 bool _CanBeEmptyBase =
1212 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1213struct __compressed_pair_elem {
1214 typedef _Tp _ParamT;
1215 typedef _Tp& reference;
1216 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001218 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001219 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001220 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1221 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222
Eric Fiselier9d355982017-04-12 23:45:53 +00001223 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001224 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1225 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001226 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001227 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001228 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001229 : __value_(_VSTD::forward<_Up>(__u))
1230 {
1231 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001233
1234#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001235 template <class... _Args, size_t... _Indexes>
1236 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1237 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1238 __tuple_indices<_Indexes...>)
1239 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001242
Alex Lorenz76132112017-11-09 17:54:49 +00001243 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1244 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001245 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001248 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249};
1250
Eric Fiselier9d355982017-04-12 23:45:53 +00001251template <class _Tp, int _Idx>
1252struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1253 typedef _Tp _ParamT;
1254 typedef _Tp& reference;
1255 typedef const _Tp& const_reference;
1256 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001258 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1259 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1260 __compressed_pair_elem(__default_init_tag) {}
1261 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1262 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263
Eric Fiselier9d355982017-04-12 23:45:53 +00001264 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001265 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1266 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001267 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001268 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001269 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001270 : __value_type(_VSTD::forward<_Up>(__u))
1271 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001273#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001274 template <class... _Args, size_t... _Indexes>
1275 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1276 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1277 __tuple_indices<_Indexes...>)
1278 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001279#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280
Alex Lorenz76132112017-11-09 17:54:49 +00001281 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1282 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001283 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284};
1285
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00001287class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1288 private __compressed_pair_elem<_T2, 1> {
Louis Dionneda463cb2020-12-11 12:22:16 -05001289public:
Eric Fiselier9d355982017-04-12 23:45:53 +00001290 // NOTE: This static assert should never fire because __compressed_pair
1291 // is *almost never* used in a scenario where it's possible for T1 == T2.
1292 // (The exception is std::function where it is possible that the function
1293 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00001294 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05001295 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00001296 "The current implementation is NOT ABI-compatible with the previous "
1297 "implementation for this configuration");
1298
Louis Dionneda463cb2020-12-11 12:22:16 -05001299 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1300 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
1301
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001302 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00001303 class = typename enable_if<
1304 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1305 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1306 >::type
1307 >
Eric Fiselier9d355982017-04-12 23:45:53 +00001308 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001309 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310
Eric Fiselier9d355982017-04-12 23:45:53 +00001311 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001312 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00001313 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001314 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001316#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001317 template <class... _Args1, class... _Args2>
1318 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1319 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1320 tuple<_Args2...> __second_args)
1321 : _Base1(__pc, _VSTD::move(__first_args),
1322 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1323 _Base2(__pc, _VSTD::move(__second_args),
1324 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001325#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326
Eric Fiselier9d355982017-04-12 23:45:53 +00001327 _LIBCPP_INLINE_VISIBILITY
1328 typename _Base1::reference first() _NOEXCEPT {
1329 return static_cast<_Base1&>(*this).__get();
1330 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
Eric Fiselier9d355982017-04-12 23:45:53 +00001332 _LIBCPP_INLINE_VISIBILITY
1333 typename _Base1::const_reference first() const _NOEXCEPT {
1334 return static_cast<_Base1 const&>(*this).__get();
1335 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336
Eric Fiselier9d355982017-04-12 23:45:53 +00001337 _LIBCPP_INLINE_VISIBILITY
1338 typename _Base2::reference second() _NOEXCEPT {
1339 return static_cast<_Base2&>(*this).__get();
1340 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341
Eric Fiselier9d355982017-04-12 23:45:53 +00001342 _LIBCPP_INLINE_VISIBILITY
1343 typename _Base2::const_reference second() const _NOEXCEPT {
1344 return static_cast<_Base2 const&>(*this).__get();
1345 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
Louis Dionneda463cb2020-12-11 12:22:16 -05001347 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1348 static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
1349 return static_cast<_Base1*>(__pair);
1350 }
1351 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1352 static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
1353 return static_cast<_Base2*>(__pair);
1354 }
1355
Eric Fiselier9d355982017-04-12 23:45:53 +00001356 _LIBCPP_INLINE_VISIBILITY
1357 void swap(__compressed_pair& __x)
1358 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1359 __is_nothrow_swappable<_T2>::value)
1360 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001361 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00001362 swap(first(), __x.first());
1363 swap(second(), __x.second());
1364 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365};
1366
1367template <class _T1, class _T2>
1368inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001369void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1370 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1371 __is_nothrow_swappable<_T2>::value) {
1372 __x.swap(__y);
1373}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001375// default_delete
1376
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001378struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001379 static_assert(!is_function<_Tp>::value,
1380 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001381#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001382 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001383#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001384 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001385#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001386 template <class _Up>
1387 _LIBCPP_INLINE_VISIBILITY
1388 default_delete(const default_delete<_Up>&,
1389 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1390 0) _NOEXCEPT {}
1391
1392 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1393 static_assert(sizeof(_Tp) > 0,
1394 "default_delete can not delete incomplete type");
1395 static_assert(!is_void<_Tp>::value,
1396 "default_delete can not delete incomplete type");
1397 delete __ptr;
1398 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399};
1400
1401template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001402struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1403private:
1404 template <class _Up>
1405 struct _EnableIfConvertible
1406 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1407
Howard Hinnant4500ca52011-12-18 21:19:44 +00001408public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001409#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001410 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001411#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001412 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001413#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001414
1415 template <class _Up>
1416 _LIBCPP_INLINE_VISIBILITY
1417 default_delete(const default_delete<_Up[]>&,
1418 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1419
1420 template <class _Up>
1421 _LIBCPP_INLINE_VISIBILITY
1422 typename _EnableIfConvertible<_Up>::type
1423 operator()(_Up* __ptr) const _NOEXCEPT {
1424 static_assert(sizeof(_Tp) > 0,
1425 "default_delete can not delete incomplete type");
1426 static_assert(!is_void<_Tp>::value,
1427 "default_delete can not delete void type");
1428 delete[] __ptr;
1429 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430};
1431
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001432template <class _Deleter>
1433struct __unique_ptr_deleter_sfinae {
1434 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1435 typedef const _Deleter& __lval_ref_type;
1436 typedef _Deleter&& __good_rval_ref_type;
1437 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438};
1439
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001440template <class _Deleter>
1441struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1442 typedef const _Deleter& __lval_ref_type;
1443 typedef const _Deleter&& __bad_rval_ref_type;
1444 typedef false_type __enable_rval_overload;
1445};
1446
1447template <class _Deleter>
1448struct __unique_ptr_deleter_sfinae<_Deleter&> {
1449 typedef _Deleter& __lval_ref_type;
1450 typedef _Deleter&& __bad_rval_ref_type;
1451 typedef false_type __enable_rval_overload;
1452};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001453
Vy Nguyene369bd92020-07-13 12:34:37 -04001454#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1455# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1456#else
1457# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1458#endif
1459
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001460template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001461class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001462public:
1463 typedef _Tp element_type;
1464 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001465 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001466
1467 static_assert(!is_rvalue_reference<deleter_type>::value,
1468 "the specified deleter type cannot be an rvalue reference");
1469
1470private:
1471 __compressed_pair<pointer, deleter_type> __ptr_;
1472
1473 struct __nat { int __for_bool_; };
1474
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001475 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001476
1477 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001478 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001479 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001480
1481 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001482 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001483 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001484
1485 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001486 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001487 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001488
1489 template <bool _Dummy, class _Deleter = typename __dependent_type<
1490 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001491 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001492 typename enable_if<is_default_constructible<_Deleter>::value &&
1493 !is_pointer<_Deleter>::value>::type;
1494
1495 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001496 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001497 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1498
1499 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001500 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001501 is_convertible<typename _UPtr::pointer, pointer>::value &&
1502 !is_array<_Up>::value
1503 >::type;
1504
1505 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001506 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001507 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1508 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1509 >::type;
1510
1511 template <class _UDel>
1512 using _EnableIfDeleterAssignable = typename enable_if<
1513 is_assignable<_Dp&, _UDel&&>::value
1514 >::type;
1515
1516public:
1517 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001518 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001519 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001520 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001521
1522 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001523 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001524 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001525 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001526
1527 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001528 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001529 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001530 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001531
1532 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001533 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001534 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001535 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001536 : __ptr_(__p, __d) {}
1537
1538 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001539 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001540 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001541 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001542 : __ptr_(__p, _VSTD::move(__d)) {
1543 static_assert(!is_reference<deleter_type>::value,
1544 "rvalue deleter bound to reference");
1545 }
1546
1547 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001548 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001549 _LIBCPP_INLINE_VISIBILITY
1550 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1551
1552 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001553 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001554 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1555 }
1556
1557 template <class _Up, class _Ep,
1558 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1559 class = _EnableIfDeleterConvertible<_Ep>
1560 >
1561 _LIBCPP_INLINE_VISIBILITY
1562 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1563 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1564
1565#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1566 template <class _Up>
1567 _LIBCPP_INLINE_VISIBILITY
1568 unique_ptr(auto_ptr<_Up>&& __p,
1569 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001570 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001571 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001572 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001573#endif
1574
1575 _LIBCPP_INLINE_VISIBILITY
1576 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1577 reset(__u.release());
1578 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1579 return *this;
1580 }
1581
1582 template <class _Up, class _Ep,
1583 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1584 class = _EnableIfDeleterAssignable<_Ep>
1585 >
1586 _LIBCPP_INLINE_VISIBILITY
1587 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1588 reset(__u.release());
1589 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1590 return *this;
1591 }
1592
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001593#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1594 template <class _Up>
1595 _LIBCPP_INLINE_VISIBILITY
1596 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1597 is_same<_Dp, default_delete<_Tp> >::value,
1598 unique_ptr&>::type
1599 operator=(auto_ptr<_Up> __p) {
1600 reset(__p.release());
1601 return *this;
1602 }
1603#endif
1604
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001605#ifdef _LIBCPP_CXX03_LANG
1606 unique_ptr(unique_ptr const&) = delete;
1607 unique_ptr& operator=(unique_ptr const&) = delete;
1608#endif
1609
1610
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001611 _LIBCPP_INLINE_VISIBILITY
1612 ~unique_ptr() { reset(); }
1613
1614 _LIBCPP_INLINE_VISIBILITY
1615 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1616 reset();
1617 return *this;
1618 }
1619
1620 _LIBCPP_INLINE_VISIBILITY
1621 typename add_lvalue_reference<_Tp>::type
1622 operator*() const {
1623 return *__ptr_.first();
1624 }
1625 _LIBCPP_INLINE_VISIBILITY
1626 pointer operator->() const _NOEXCEPT {
1627 return __ptr_.first();
1628 }
1629 _LIBCPP_INLINE_VISIBILITY
1630 pointer get() const _NOEXCEPT {
1631 return __ptr_.first();
1632 }
1633 _LIBCPP_INLINE_VISIBILITY
1634 deleter_type& get_deleter() _NOEXCEPT {
1635 return __ptr_.second();
1636 }
1637 _LIBCPP_INLINE_VISIBILITY
1638 const deleter_type& get_deleter() const _NOEXCEPT {
1639 return __ptr_.second();
1640 }
1641 _LIBCPP_INLINE_VISIBILITY
1642 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1643 return __ptr_.first() != nullptr;
1644 }
1645
1646 _LIBCPP_INLINE_VISIBILITY
1647 pointer release() _NOEXCEPT {
1648 pointer __t = __ptr_.first();
1649 __ptr_.first() = pointer();
1650 return __t;
1651 }
1652
1653 _LIBCPP_INLINE_VISIBILITY
1654 void reset(pointer __p = pointer()) _NOEXCEPT {
1655 pointer __tmp = __ptr_.first();
1656 __ptr_.first() = __p;
1657 if (__tmp)
1658 __ptr_.second()(__tmp);
1659 }
1660
1661 _LIBCPP_INLINE_VISIBILITY
1662 void swap(unique_ptr& __u) _NOEXCEPT {
1663 __ptr_.swap(__u.__ptr_);
1664 }
1665};
1666
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001667
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001669class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001671 typedef _Tp element_type;
1672 typedef _Dp deleter_type;
1673 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1674
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001676 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677
Eric Fiselier31127cd2017-04-16 02:14:31 +00001678 template <class _From>
1679 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680
Eric Fiselier31127cd2017-04-16 02:14:31 +00001681 template <class _FromElem>
1682 struct _CheckArrayPointerConversion<_FromElem*>
1683 : integral_constant<bool,
1684 is_same<_FromElem*, pointer>::value ||
1685 (is_same<pointer, element_type*>::value &&
1686 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1687 >
1688 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689
Eric Fiselier31127cd2017-04-16 02:14:31 +00001690 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001691
1692 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001693 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001694 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001695
1696 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001697 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001698 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001699
1700 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001701 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001702 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001703
1704 template <bool _Dummy, class _Deleter = typename __dependent_type<
1705 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001706 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001707 typename enable_if<is_default_constructible<_Deleter>::value &&
1708 !is_pointer<_Deleter>::value>::type;
1709
1710 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001711 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001712 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1713
1714 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001715 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001716 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001717 >::type;
1718
1719 template <class _UPtr, class _Up,
1720 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001721 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001722 is_array<_Up>::value &&
1723 is_same<pointer, element_type*>::value &&
1724 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1725 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1726 >::type;
1727
1728 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001729 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001730 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1731 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1732 >::type;
1733
1734 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001735 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001736 is_assignable<_Dp&, _UDel&&>::value
1737 >::type;
1738
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001740 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001741 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001742 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001743 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001745 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001746 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001747 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001748 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001750 template <class _Pp, bool _Dummy = true,
1751 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001752 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001753 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001754 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001755 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001757 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001758 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1759 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001760 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001761 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001762 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001764 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001765 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001766 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001767 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001768 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001770 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001771 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1772 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001773 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001774 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001775 : __ptr_(__p, _VSTD::move(__d)) {
1776 static_assert(!is_reference<deleter_type>::value,
1777 "rvalue deleter bound to reference");
1778 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001780 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001781 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001782 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001783 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001784 : __ptr_(nullptr, _VSTD::move(__d)) {
1785 static_assert(!is_reference<deleter_type>::value,
1786 "rvalue deleter bound to reference");
1787 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001788
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001789 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001790 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1791 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001792 _LIBCPP_INLINE_VISIBILITY
1793 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001794
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001795 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001796 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001797 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1798 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001799
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001800 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001801 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001802 reset(__u.release());
1803 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1804 return *this;
1805 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001807 template <class _Up, class _Ep,
1808 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1809 class = _EnableIfDeleterConvertible<_Ep>
1810 >
1811 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001812 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001813 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001814 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001816 template <class _Up, class _Ep,
1817 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1818 class = _EnableIfDeleterAssignable<_Ep>
1819 >
1820 _LIBCPP_INLINE_VISIBILITY
1821 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001822 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001823 reset(__u.release());
1824 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1825 return *this;
1826 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001828#ifdef _LIBCPP_CXX03_LANG
1829 unique_ptr(unique_ptr const&) = delete;
1830 unique_ptr& operator=(unique_ptr const&) = delete;
1831#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001832
1833public:
1834 _LIBCPP_INLINE_VISIBILITY
1835 ~unique_ptr() { reset(); }
1836
1837 _LIBCPP_INLINE_VISIBILITY
1838 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1839 reset();
1840 return *this;
1841 }
1842
1843 _LIBCPP_INLINE_VISIBILITY
1844 typename add_lvalue_reference<_Tp>::type
1845 operator[](size_t __i) const {
1846 return __ptr_.first()[__i];
1847 }
1848 _LIBCPP_INLINE_VISIBILITY
1849 pointer get() const _NOEXCEPT {
1850 return __ptr_.first();
1851 }
1852
1853 _LIBCPP_INLINE_VISIBILITY
1854 deleter_type& get_deleter() _NOEXCEPT {
1855 return __ptr_.second();
1856 }
1857
1858 _LIBCPP_INLINE_VISIBILITY
1859 const deleter_type& get_deleter() const _NOEXCEPT {
1860 return __ptr_.second();
1861 }
1862 _LIBCPP_INLINE_VISIBILITY
1863 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1864 return __ptr_.first() != nullptr;
1865 }
1866
1867 _LIBCPP_INLINE_VISIBILITY
1868 pointer release() _NOEXCEPT {
1869 pointer __t = __ptr_.first();
1870 __ptr_.first() = pointer();
1871 return __t;
1872 }
1873
1874 template <class _Pp>
1875 _LIBCPP_INLINE_VISIBILITY
1876 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001877 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001878 >::type
1879 reset(_Pp __p) _NOEXCEPT {
1880 pointer __tmp = __ptr_.first();
1881 __ptr_.first() = __p;
1882 if (__tmp)
1883 __ptr_.second()(__tmp);
1884 }
1885
1886 _LIBCPP_INLINE_VISIBILITY
1887 void reset(nullptr_t = nullptr) _NOEXCEPT {
1888 pointer __tmp = __ptr_.first();
1889 __ptr_.first() = nullptr;
1890 if (__tmp)
1891 __ptr_.second()(__tmp);
1892 }
1893
1894 _LIBCPP_INLINE_VISIBILITY
1895 void swap(unique_ptr& __u) _NOEXCEPT {
1896 __ptr_.swap(__u.__ptr_);
1897 }
1898
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899};
1900
1901template <class _Tp, class _Dp>
1902inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001903typename enable_if<
1904 __is_swappable<_Dp>::value,
1905 void
1906>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001907swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908
1909template <class _T1, class _D1, class _T2, class _D2>
1910inline _LIBCPP_INLINE_VISIBILITY
1911bool
1912operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1913
1914template <class _T1, class _D1, class _T2, class _D2>
1915inline _LIBCPP_INLINE_VISIBILITY
1916bool
1917operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1918
1919template <class _T1, class _D1, class _T2, class _D2>
1920inline _LIBCPP_INLINE_VISIBILITY
1921bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001922operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1923{
1924 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1925 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001926 typedef typename common_type<_P1, _P2>::type _Vp;
1927 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001928}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929
1930template <class _T1, class _D1, class _T2, class _D2>
1931inline _LIBCPP_INLINE_VISIBILITY
1932bool
1933operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1934
1935template <class _T1, class _D1, class _T2, class _D2>
1936inline _LIBCPP_INLINE_VISIBILITY
1937bool
1938operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1939
1940template <class _T1, class _D1, class _T2, class _D2>
1941inline _LIBCPP_INLINE_VISIBILITY
1942bool
1943operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1944
Howard Hinnantb17caf92012-02-21 21:02:58 +00001945template <class _T1, class _D1>
1946inline _LIBCPP_INLINE_VISIBILITY
1947bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001948operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001949{
1950 return !__x;
1951}
1952
1953template <class _T1, class _D1>
1954inline _LIBCPP_INLINE_VISIBILITY
1955bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001956operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001957{
1958 return !__x;
1959}
1960
1961template <class _T1, class _D1>
1962inline _LIBCPP_INLINE_VISIBILITY
1963bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001964operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001965{
1966 return static_cast<bool>(__x);
1967}
1968
1969template <class _T1, class _D1>
1970inline _LIBCPP_INLINE_VISIBILITY
1971bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001972operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001973{
1974 return static_cast<bool>(__x);
1975}
1976
1977template <class _T1, class _D1>
1978inline _LIBCPP_INLINE_VISIBILITY
1979bool
1980operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1981{
1982 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1983 return less<_P1>()(__x.get(), nullptr);
1984}
1985
1986template <class _T1, class _D1>
1987inline _LIBCPP_INLINE_VISIBILITY
1988bool
1989operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1990{
1991 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1992 return less<_P1>()(nullptr, __x.get());
1993}
1994
1995template <class _T1, class _D1>
1996inline _LIBCPP_INLINE_VISIBILITY
1997bool
1998operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1999{
2000 return nullptr < __x;
2001}
2002
2003template <class _T1, class _D1>
2004inline _LIBCPP_INLINE_VISIBILITY
2005bool
2006operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2007{
2008 return __x < nullptr;
2009}
2010
2011template <class _T1, class _D1>
2012inline _LIBCPP_INLINE_VISIBILITY
2013bool
2014operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2015{
2016 return !(nullptr < __x);
2017}
2018
2019template <class _T1, class _D1>
2020inline _LIBCPP_INLINE_VISIBILITY
2021bool
2022operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2023{
2024 return !(__x < nullptr);
2025}
2026
2027template <class _T1, class _D1>
2028inline _LIBCPP_INLINE_VISIBILITY
2029bool
2030operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2031{
2032 return !(__x < nullptr);
2033}
2034
2035template <class _T1, class _D1>
2036inline _LIBCPP_INLINE_VISIBILITY
2037bool
2038operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2039{
2040 return !(nullptr < __x);
2041}
2042
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002043#if _LIBCPP_STD_VER > 11
2044
2045template<class _Tp>
2046struct __unique_if
2047{
2048 typedef unique_ptr<_Tp> __unique_single;
2049};
2050
2051template<class _Tp>
2052struct __unique_if<_Tp[]>
2053{
2054 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2055};
2056
2057template<class _Tp, size_t _Np>
2058struct __unique_if<_Tp[_Np]>
2059{
2060 typedef void __unique_array_known_bound;
2061};
2062
2063template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002064inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002065typename __unique_if<_Tp>::__unique_single
2066make_unique(_Args&&... __args)
2067{
2068 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2069}
2070
2071template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002072inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002073typename __unique_if<_Tp>::__unique_array_unknown_bound
2074make_unique(size_t __n)
2075{
2076 typedef typename remove_extent<_Tp>::type _Up;
2077 return unique_ptr<_Tp>(new _Up[__n]());
2078}
2079
2080template<class _Tp, class... _Args>
2081 typename __unique_if<_Tp>::__unique_array_known_bound
2082 make_unique(_Args&&...) = delete;
2083
2084#endif // _LIBCPP_STD_VER > 11
2085
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002086template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002087#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002088struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002089#else
2090struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002091 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002092#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002093{
2094 typedef unique_ptr<_Tp, _Dp> argument_type;
2095 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002096 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002097 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002098 {
2099 typedef typename argument_type::pointer pointer;
2100 return hash<pointer>()(__ptr.get());
2101 }
2102};
2103
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104struct __destruct_n
2105{
2106private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002107 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108
2109 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002110 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002111 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112
2113 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002114 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115 {}
2116
Howard Hinnant719bda32011-05-28 14:41:13 +00002117 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002118 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002119 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 {}
2121
Howard Hinnant719bda32011-05-28 14:41:13 +00002122 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002123 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002124 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125 {}
2126public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002127 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002128 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129
2130 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002131 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002132 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133
2134 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002135 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002136 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137
2138 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002139 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002140 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141};
2142
2143template <class _Alloc>
2144class __allocator_destructor
2145{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002146 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002148 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2149 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150private:
2151 _Alloc& __alloc_;
2152 size_type __s_;
2153public:
2154 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002155 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002158 void operator()(pointer __p) _NOEXCEPT
2159 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160};
2161
2162template <class _InputIterator, class _ForwardIterator>
2163_ForwardIterator
2164uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2165{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002167#ifndef _LIBCPP_NO_EXCEPTIONS
2168 _ForwardIterator __s = __r;
2169 try
2170 {
2171#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002172 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002173 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002174#ifndef _LIBCPP_NO_EXCEPTIONS
2175 }
2176 catch (...)
2177 {
2178 for (; __s != __r; ++__s)
2179 __s->~value_type();
2180 throw;
2181 }
2182#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183 return __r;
2184}
2185
2186template <class _InputIterator, class _Size, class _ForwardIterator>
2187_ForwardIterator
2188uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2189{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002191#ifndef _LIBCPP_NO_EXCEPTIONS
2192 _ForwardIterator __s = __r;
2193 try
2194 {
2195#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002196 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002197 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002198#ifndef _LIBCPP_NO_EXCEPTIONS
2199 }
2200 catch (...)
2201 {
2202 for (; __s != __r; ++__s)
2203 __s->~value_type();
2204 throw;
2205 }
2206#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 return __r;
2208}
2209
2210template <class _ForwardIterator, class _Tp>
2211void
2212uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2213{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002215#ifndef _LIBCPP_NO_EXCEPTIONS
2216 _ForwardIterator __s = __f;
2217 try
2218 {
2219#endif
2220 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002221 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002222#ifndef _LIBCPP_NO_EXCEPTIONS
2223 }
2224 catch (...)
2225 {
2226 for (; __s != __f; ++__s)
2227 __s->~value_type();
2228 throw;
2229 }
2230#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231}
2232
2233template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002234_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2236{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002238#ifndef _LIBCPP_NO_EXCEPTIONS
2239 _ForwardIterator __s = __f;
2240 try
2241 {
2242#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002243 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002244 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002245#ifndef _LIBCPP_NO_EXCEPTIONS
2246 }
2247 catch (...)
2248 {
2249 for (; __s != __f; ++__s)
2250 __s->~value_type();
2251 throw;
2252 }
2253#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002254 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255}
2256
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002257#if _LIBCPP_STD_VER > 14
2258
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002259template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002260inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002261void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2262 for (; __first != __last; ++__first)
2263 _VSTD::destroy_at(_VSTD::addressof(*__first));
2264}
2265
2266template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002267inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002268_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2269 for (; __n > 0; (void)++__first, --__n)
2270 _VSTD::destroy_at(_VSTD::addressof(*__first));
2271 return __first;
2272}
2273
Eric Fiselier290c07c2016-10-11 21:13:44 +00002274template <class _ForwardIterator>
2275inline _LIBCPP_INLINE_VISIBILITY
2276void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2277 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2278 auto __idx = __first;
2279#ifndef _LIBCPP_NO_EXCEPTIONS
2280 try {
2281#endif
2282 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002283 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002284#ifndef _LIBCPP_NO_EXCEPTIONS
2285 } catch (...) {
2286 _VSTD::destroy(__first, __idx);
2287 throw;
2288 }
2289#endif
2290}
2291
2292template <class _ForwardIterator, class _Size>
2293inline _LIBCPP_INLINE_VISIBILITY
2294_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2295 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2296 auto __idx = __first;
2297#ifndef _LIBCPP_NO_EXCEPTIONS
2298 try {
2299#endif
2300 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002301 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002302 return __idx;
2303#ifndef _LIBCPP_NO_EXCEPTIONS
2304 } catch (...) {
2305 _VSTD::destroy(__first, __idx);
2306 throw;
2307 }
2308#endif
2309}
2310
2311
2312template <class _ForwardIterator>
2313inline _LIBCPP_INLINE_VISIBILITY
2314void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2315 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2316 auto __idx = __first;
2317#ifndef _LIBCPP_NO_EXCEPTIONS
2318 try {
2319#endif
2320 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002321 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002322#ifndef _LIBCPP_NO_EXCEPTIONS
2323 } catch (...) {
2324 _VSTD::destroy(__first, __idx);
2325 throw;
2326 }
2327#endif
2328}
2329
2330template <class _ForwardIterator, class _Size>
2331inline _LIBCPP_INLINE_VISIBILITY
2332_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2333 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2334 auto __idx = __first;
2335#ifndef _LIBCPP_NO_EXCEPTIONS
2336 try {
2337#endif
2338 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002339 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002340 return __idx;
2341#ifndef _LIBCPP_NO_EXCEPTIONS
2342 } catch (...) {
2343 _VSTD::destroy(__first, __idx);
2344 throw;
2345 }
2346#endif
2347}
2348
2349
2350template <class _InputIt, class _ForwardIt>
2351inline _LIBCPP_INLINE_VISIBILITY
2352_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2353 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2354 auto __idx = __first_res;
2355#ifndef _LIBCPP_NO_EXCEPTIONS
2356 try {
2357#endif
2358 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002359 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002360 return __idx;
2361#ifndef _LIBCPP_NO_EXCEPTIONS
2362 } catch (...) {
2363 _VSTD::destroy(__first_res, __idx);
2364 throw;
2365 }
2366#endif
2367}
2368
2369template <class _InputIt, class _Size, class _ForwardIt>
2370inline _LIBCPP_INLINE_VISIBILITY
2371pair<_InputIt, _ForwardIt>
2372uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2373 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2374 auto __idx = __first_res;
2375#ifndef _LIBCPP_NO_EXCEPTIONS
2376 try {
2377#endif
2378 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002379 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002380 return {__first, __idx};
2381#ifndef _LIBCPP_NO_EXCEPTIONS
2382 } catch (...) {
2383 _VSTD::destroy(__first_res, __idx);
2384 throw;
2385 }
2386#endif
2387}
2388
2389
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002390#endif // _LIBCPP_STD_VER > 14
2391
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002392// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2393// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00002394// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002395#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2396 && defined(__ATOMIC_RELAXED) \
2397 && defined(__ATOMIC_ACQ_REL)
2398# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002399#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002400# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2401#endif
2402
2403template <class _Tp>
2404inline _LIBCPP_INLINE_VISIBILITY _Tp
2405__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2406{
2407#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2408 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2409#else
2410 return __t += 1;
2411#endif
2412}
2413
2414template <class _Tp>
2415inline _LIBCPP_INLINE_VISIBILITY _Tp
2416__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2417{
2418#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2419 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2420#else
2421 return __t -= 1;
2422#endif
2423}
2424
Howard Hinnant756c69b2010-09-22 16:48:34 +00002425class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426 : public std::exception
2427{
2428public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002429 bad_weak_ptr() _NOEXCEPT = default;
2430 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002431 virtual ~bad_weak_ptr() _NOEXCEPT;
2432 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433};
2434
Louis Dionne16fe2952018-07-11 23:14:33 +00002435_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002436void __throw_bad_weak_ptr()
2437{
2438#ifndef _LIBCPP_NO_EXCEPTIONS
2439 throw bad_weak_ptr();
2440#else
2441 _VSTD::abort();
2442#endif
2443}
2444
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002445template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002447class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448{
2449 __shared_count(const __shared_count&);
2450 __shared_count& operator=(const __shared_count&);
2451
2452protected:
2453 long __shared_owners_;
2454 virtual ~__shared_count();
2455private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002456 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457
2458public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002460 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 : __shared_owners_(__refs) {}
2462
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002463#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002464 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002465 void __add_shared() _NOEXCEPT;
2466 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002467#else
2468 _LIBCPP_INLINE_VISIBILITY
2469 void __add_shared() _NOEXCEPT {
2470 __libcpp_atomic_refcount_increment(__shared_owners_);
2471 }
2472 _LIBCPP_INLINE_VISIBILITY
2473 bool __release_shared() _NOEXCEPT {
2474 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2475 __on_zero_shared();
2476 return true;
2477 }
2478 return false;
2479 }
2480#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002481 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002482 long use_count() const _NOEXCEPT {
2483 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2484 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485};
2486
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002487class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488 : private __shared_count
2489{
2490 long __shared_weak_owners_;
2491
2492public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002494 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495 : __shared_count(__refs),
2496 __shared_weak_owners_(__refs) {}
2497protected:
2498 virtual ~__shared_weak_count();
2499
2500public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002501#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002502 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002503 void __add_shared() _NOEXCEPT;
2504 void __add_weak() _NOEXCEPT;
2505 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002506#else
2507 _LIBCPP_INLINE_VISIBILITY
2508 void __add_shared() _NOEXCEPT {
2509 __shared_count::__add_shared();
2510 }
2511 _LIBCPP_INLINE_VISIBILITY
2512 void __add_weak() _NOEXCEPT {
2513 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2514 }
2515 _LIBCPP_INLINE_VISIBILITY
2516 void __release_shared() _NOEXCEPT {
2517 if (__shared_count::__release_shared())
2518 __release_weak();
2519 }
2520#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002521 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002523 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2524 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525
Howard Hinnant719bda32011-05-28 14:41:13 +00002526 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002528 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529};
2530
2531template <class _Tp, class _Dp, class _Alloc>
2532class __shared_ptr_pointer
2533 : public __shared_weak_count
2534{
2535 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2536public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002539 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540
Howard Hinnant72f73582010-08-11 17:04:31 +00002541#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002542 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002543#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544
2545private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002546 virtual void __on_zero_shared() _NOEXCEPT;
2547 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548};
2549
Howard Hinnant72f73582010-08-11 17:04:31 +00002550#ifndef _LIBCPP_NO_RTTI
2551
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552template <class _Tp, class _Dp, class _Alloc>
2553const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002554__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002556 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557}
2558
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002559#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002560
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561template <class _Tp, class _Dp, class _Alloc>
2562void
Howard Hinnant719bda32011-05-28 14:41:13 +00002563__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564{
2565 __data_.first().second()(__data_.first().first());
2566 __data_.first().second().~_Dp();
2567}
2568
2569template <class _Tp, class _Dp, class _Alloc>
2570void
Howard Hinnant719bda32011-05-28 14:41:13 +00002571__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002573 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2574 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002575 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2576
Eric Fiselierf8898c82015-02-05 23:01:40 +00002577 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002579 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580}
2581
2582template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002583struct __shared_ptr_emplace
2584 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585{
Louis Dionneda463cb2020-12-11 12:22:16 -05002586 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002587 _LIBCPP_HIDE_FROM_ABI
2588 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002589 : __storage_(_VSTD::move(__a))
2590 {
2591#if _LIBCPP_STD_VER > 17
2592 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2593 _TpAlloc __tmp(*__get_alloc());
2594 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002595#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002596 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002597#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002598 }
Louis Dionne86549d72020-12-09 16:22:17 -05002599
2600 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002601 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002602
2603 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002604 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605
2606private:
Louis Dionne86549d72020-12-09 16:22:17 -05002607 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002608#if _LIBCPP_STD_VER > 17
2609 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2610 _TpAlloc __tmp(*__get_alloc());
2611 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2612#else
Louis Dionne86549d72020-12-09 16:22:17 -05002613 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002614#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002615 }
2616
2617 virtual void __on_zero_shared_weak() _NOEXCEPT {
2618 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2619 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002620 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002621 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002622 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2623 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2624 }
2625
Louis Dionneda463cb2020-12-11 12:22:16 -05002626 // This class implements the control block for non-array shared pointers created
2627 // through `std::allocate_shared` and `std::make_shared`.
2628 //
2629 // In previous versions of the library, we used a compressed pair to store
2630 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2631 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2632 // we now use a properly aligned char buffer while making sure that we maintain
2633 // the same layout that we had when we used a compressed pair.
2634 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2635 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2636 char __blob_[sizeof(_CompressedPair)];
2637
2638 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2639 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2640 }
2641 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2642 __get_alloc()->~_Alloc();
2643 }
2644 _Alloc* __get_alloc() _NOEXCEPT {
2645 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2646 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2647 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2648 return __alloc;
2649 }
2650 _Tp* __get_elem() _NOEXCEPT {
2651 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2652 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2653 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2654 return __elem;
2655 }
2656 };
2657
2658 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2659 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2660 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661};
2662
Erik Pilkington2a398762017-05-25 15:43:31 +00002663struct __shared_ptr_dummy_rebind_allocator_type;
2664template <>
2665class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2666{
2667public:
2668 template <class _Other>
2669 struct rebind
2670 {
2671 typedef allocator<_Other> other;
2672 };
2673};
2674
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002675template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676
zoecarverd73f42a2020-05-11 18:42:50 -07002677template<class _Tp, class _Up>
2678struct __compatible_with
2679#if _LIBCPP_STD_VER > 14
2680 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2681#else
2682 : is_convertible<_Tp*, _Up*> {};
2683#endif // _LIBCPP_STD_VER > 14
2684
Vy Nguyene369bd92020-07-13 12:34:37 -04002685#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2686# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2687#else
2688# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2689#endif
2690
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002692class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002694public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002695#if _LIBCPP_STD_VER > 14
2696 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002697 typedef remove_extent_t<_Tp> element_type;
2698#else
2699 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002700#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002701
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702private:
2703 element_type* __ptr_;
2704 __shared_weak_count* __cntrl_;
2705
2706 struct __nat {int __for_bool_;};
2707public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002709 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002711 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002712 template<class _Yp>
2713 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002714 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002715 template<class _Yp, class _Dp>
2716 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07002717 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002718 template<class _Yp, class _Dp, class _Alloc>
2719 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07002720 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2722 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002723 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002725 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002729 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002730 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002732 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002733 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002734 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002735 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002737 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002738#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002739 template<class _Yp>
2740 shared_ptr(auto_ptr<_Yp>&& __r,
2741 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002742#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002743 template <class _Yp, class _Dp>
2744 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2745 typename enable_if
2746 <
2747 !is_lvalue_reference<_Dp>::value &&
2748 !is_array<_Yp>::value &&
2749 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2750 __nat
2751 >::type = __nat());
2752 template <class _Yp, class _Dp>
2753 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2754 typename enable_if
2755 <
2756 is_lvalue_reference<_Dp>::value &&
2757 !is_array<_Yp>::value &&
2758 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2759 __nat
2760 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761
2762 ~shared_ptr();
2763
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002765 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002766 template<class _Yp>
2767 typename enable_if
2768 <
zoecarverd73f42a2020-05-11 18:42:50 -07002769 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002770 shared_ptr&
2771 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002773 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002775 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002776 template<class _Yp>
2777 typename enable_if
2778 <
zoecarverd73f42a2020-05-11 18:42:50 -07002779 __compatible_with<_Yp, element_type>::value,
2780 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002781 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002783 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002784#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002785 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002787 typename enable_if
2788 <
2789 !is_array<_Yp>::value &&
2790 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002791 shared_ptr
2792 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002793 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002794#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002795 template <class _Yp, class _Dp>
2796 typename enable_if
2797 <
2798 !is_array<_Yp>::value &&
2799 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2800 shared_ptr&
2801 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002803 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002806 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002808 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002809 template<class _Yp>
2810 typename enable_if
2811 <
zoecarverd73f42a2020-05-11 18:42:50 -07002812 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002813 void
2814 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002816 reset(_Yp* __p);
2817 template<class _Yp, class _Dp>
2818 typename enable_if
2819 <
zoecarverd73f42a2020-05-11 18:42:50 -07002820 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002821 void
2822 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002824 reset(_Yp* __p, _Dp __d);
2825 template<class _Yp, class _Dp, class _Alloc>
2826 typename enable_if
2827 <
zoecarverd73f42a2020-05-11 18:42:50 -07002828 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002829 void
2830 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002832 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833
Howard Hinnant756c69b2010-09-22 16:48:34 +00002834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002835 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002837 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2838 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002839 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002840 element_type* operator->() const _NOEXCEPT
2841 {
2842 static_assert(!_VSTD::is_array<_Tp>::value,
2843 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2844 return __ptr_;
2845 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002847 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002849 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002850 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002851 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002852 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002853 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002854 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002856 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002857 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002858 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002860 _LIBCPP_INLINE_VISIBILITY
2861 bool
2862 __owner_equivalent(const shared_ptr& __p) const
2863 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864
zoecarverd73f42a2020-05-11 18:42:50 -07002865#if _LIBCPP_STD_VER > 14
2866 typename add_lvalue_reference<element_type>::type
2867 _LIBCPP_INLINE_VISIBILITY
2868 operator[](ptrdiff_t __i) const
2869 {
2870 static_assert(_VSTD::is_array<_Tp>::value,
2871 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2872 return __ptr_[__i];
2873 }
2874#endif
2875
Howard Hinnant72f73582010-08-11 17:04:31 +00002876#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002879 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002880 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002881 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002882 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002883#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884
Zoe Carverd9040c72019-10-22 15:16:49 +00002885 template<class _Yp, class _CntrlBlk>
2886 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002887 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002888 {
2889 shared_ptr<_Tp> __r;
2890 __r.__ptr_ = __p;
2891 __r.__cntrl_ = __cntrl;
2892 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2893 return __r;
2894 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002895
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002897 template <class _Yp, bool = is_function<_Yp>::value>
2898 struct __shared_ptr_default_allocator
2899 {
2900 typedef allocator<_Yp> type;
2901 };
2902
2903 template <class _Yp>
2904 struct __shared_ptr_default_allocator<_Yp, true>
2905 {
2906 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2907 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002909 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002910 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002911 typename enable_if<is_convertible<_OrigPtr*,
2912 const enable_shared_from_this<_Yp>*
2913 >::value,
2914 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002915 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2916 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002918 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002919 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002920 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002921 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2922 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002923 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 }
2925
Erik Pilkington2a398762017-05-25 15:43:31 +00002926 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927
zoecarverd73f42a2020-05-11 18:42:50 -07002928 template <class, class _Yp>
2929 struct __shared_ptr_default_delete
2930 : default_delete<_Yp> {};
2931
2932 template <class _Yp, class _Un, size_t _Sz>
2933 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2934 : default_delete<_Yp[]> {};
2935
2936 template <class _Yp, class _Un>
2937 struct __shared_ptr_default_delete<_Yp[], _Un>
2938 : default_delete<_Yp[]> {};
2939
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002940 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2941 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942};
2943
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002944#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2945template<class _Tp>
2946shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2947template<class _Tp, class _Dp>
2948shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2949#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002950
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002952inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002953_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002954shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002955 : __ptr_(nullptr),
2956 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957{
2958}
2959
2960template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002961inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002962_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002963shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002964 : __ptr_(nullptr),
2965 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966{
2967}
2968
2969template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002970template<class _Yp>
2971shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002972 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973 : __ptr_(__p)
2974{
2975 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002976 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002977 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2978 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002980 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981}
2982
2983template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002984template<class _Yp, class _Dp>
2985shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07002986 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 : __ptr_(__p)
2988{
2989#ifndef _LIBCPP_NO_EXCEPTIONS
2990 try
2991 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002992#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002993 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2994 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
2995 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002996 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997#ifndef _LIBCPP_NO_EXCEPTIONS
2998 }
2999 catch (...)
3000 {
3001 __d(__p);
3002 throw;
3003 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003004#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005}
3006
3007template<class _Tp>
3008template<class _Dp>
3009shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003010 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011{
3012#ifndef _LIBCPP_NO_EXCEPTIONS
3013 try
3014 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003015#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003016 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3017 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3018 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019#ifndef _LIBCPP_NO_EXCEPTIONS
3020 }
3021 catch (...)
3022 {
3023 __d(__p);
3024 throw;
3025 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003026#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027}
3028
3029template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003030template<class _Yp, class _Dp, class _Alloc>
3031shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003032 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 : __ptr_(__p)
3034{
3035#ifndef _LIBCPP_NO_EXCEPTIONS
3036 try
3037 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003038#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003040 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041 typedef __allocator_destructor<_A2> _D2;
3042 _A2 __a2(__a);
3043 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003044 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003045 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003046 __enable_weak_this(__p, __p);
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 _Dp, class _Alloc>
3059shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003060 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061{
3062#ifndef _LIBCPP_NO_EXCEPTIONS
3063 try
3064 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003065#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003067 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068 typedef __allocator_destructor<_A2> _D2;
3069 _A2 __a2(__a);
3070 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003071 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003072 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073#ifndef _LIBCPP_NO_EXCEPTIONS
3074 }
3075 catch (...)
3076 {
3077 __d(__p);
3078 throw;
3079 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003080#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081}
3082
3083template<class _Tp>
3084template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003085inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003086shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 : __ptr_(__p),
3088 __cntrl_(__r.__cntrl_)
3089{
3090 if (__cntrl_)
3091 __cntrl_->__add_shared();
3092}
3093
3094template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003095inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003096shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 : __ptr_(__r.__ptr_),
3098 __cntrl_(__r.__cntrl_)
3099{
3100 if (__cntrl_)
3101 __cntrl_->__add_shared();
3102}
3103
3104template<class _Tp>
3105template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003106inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003108 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003109 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 : __ptr_(__r.__ptr_),
3111 __cntrl_(__r.__cntrl_)
3112{
3113 if (__cntrl_)
3114 __cntrl_->__add_shared();
3115}
3116
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003118inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003119shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120 : __ptr_(__r.__ptr_),
3121 __cntrl_(__r.__cntrl_)
3122{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003123 __r.__ptr_ = nullptr;
3124 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125}
3126
3127template<class _Tp>
3128template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003129inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003131 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003132 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 : __ptr_(__r.__ptr_),
3134 __cntrl_(__r.__cntrl_)
3135{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003136 __r.__ptr_ = nullptr;
3137 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138}
3139
Marshall Clowb22274f2017-01-24 22:22:33 +00003140#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003142template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003143shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003144 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145 : __ptr_(__r.get())
3146{
3147 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3148 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003149 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150 __r.release();
3151}
Marshall Clowb22274f2017-01-24 22:22:33 +00003152#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153
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;
3173 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3174 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003175 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003176 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177 __r.release();
3178}
3179
3180template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003181template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003183 typename enable_if
3184 <
3185 is_lvalue_reference<_Dp>::value &&
3186 !is_array<_Yp>::value &&
3187 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3188 __nat
3189 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190 : __ptr_(__r.get())
3191{
Marshall Clow35cde742015-05-10 13:59:45 +00003192#if _LIBCPP_STD_VER > 11
3193 if (__ptr_ == nullptr)
3194 __cntrl_ = nullptr;
3195 else
3196#endif
3197 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003198 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003199 typedef __shared_ptr_pointer<_Yp*,
3200 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003201 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003202 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003203 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003204 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205 __r.release();
3206}
3207
Zoe Carver6cd05c32019-08-19 15:47:16 +00003208template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209shared_ptr<_Tp>::~shared_ptr()
3210{
3211 if (__cntrl_)
3212 __cntrl_->__release_shared();
3213}
3214
3215template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003216inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003218shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219{
3220 shared_ptr(__r).swap(*this);
3221 return *this;
3222}
3223
3224template<class _Tp>
3225template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003226inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003227typename enable_if
3228<
zoecarverd73f42a2020-05-11 18:42:50 -07003229 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003230 shared_ptr<_Tp>&
3231>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003232shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233{
3234 shared_ptr(__r).swap(*this);
3235 return *this;
3236}
3237
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003239inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003241shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003243 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244 return *this;
3245}
3246
3247template<class _Tp>
3248template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003249inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003250typename enable_if
3251<
zoecarverd73f42a2020-05-11 18:42:50 -07003252 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003253 shared_ptr<_Tp>&
3254>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3256{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003257 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258 return *this;
3259}
3260
Marshall Clowb22274f2017-01-24 22:22:33 +00003261#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262template<class _Tp>
3263template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003264inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003265typename enable_if
3266<
3267 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003268 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003269 shared_ptr<_Tp>
3270>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3272{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003273 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003274 return *this;
3275}
Marshall Clowb22274f2017-01-24 22:22:33 +00003276#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277
3278template<class _Tp>
3279template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003280inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003281typename enable_if
3282<
3283 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003284 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003285 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003286 shared_ptr<_Tp>&
3287>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3289{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003290 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291 return *this;
3292}
3293
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003295inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296void
Howard Hinnant719bda32011-05-28 14:41:13 +00003297shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003299 _VSTD::swap(__ptr_, __r.__ptr_);
3300 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003301}
3302
3303template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003304inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305void
Howard Hinnant719bda32011-05-28 14:41:13 +00003306shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307{
3308 shared_ptr().swap(*this);
3309}
3310
3311template<class _Tp>
3312template<class _Yp>
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)
3320{
3321 shared_ptr(__p).swap(*this);
3322}
3323
3324template<class _Tp>
3325template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003326inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003327typename enable_if
3328<
zoecarverd73f42a2020-05-11 18:42:50 -07003329 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003330 void
3331>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3333{
3334 shared_ptr(__p, __d).swap(*this);
3335}
3336
3337template<class _Tp>
3338template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003339inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003340typename enable_if
3341<
zoecarverd73f42a2020-05-11 18:42:50 -07003342 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003343 void
3344>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3346{
3347 shared_ptr(__p, __d, __a).swap(*this);
3348}
3349
Louis Dionne74aef9f2020-12-11 12:20:06 -05003350//
3351// std::allocate_shared and std::make_shared
3352//
3353template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3354_LIBCPP_HIDE_FROM_ABI
3355shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356{
Louis Dionne74aef9f2020-12-11 12:20:06 -05003357 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3358 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3359 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3360 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3361 auto __control_block = __guard.__release_ptr();
3362 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363}
3364
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003365template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3366_LIBCPP_HIDE_FROM_ABI
3367shared_ptr<_Tp> make_shared(_Args&& ...__args)
3368{
3369 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3370}
3371
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372template<class _Tp, class _Up>
3373inline _LIBCPP_INLINE_VISIBILITY
3374bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003375operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376{
3377 return __x.get() == __y.get();
3378}
3379
3380template<class _Tp, class _Up>
3381inline _LIBCPP_INLINE_VISIBILITY
3382bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003383operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384{
3385 return !(__x == __y);
3386}
3387
3388template<class _Tp, class _Up>
3389inline _LIBCPP_INLINE_VISIBILITY
3390bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003391operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003393#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003394 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3395 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003396#else
3397 return less<>()(__x.get(), __y.get());
3398#endif
3399
Howard Hinnantb17caf92012-02-21 21:02:58 +00003400}
3401
3402template<class _Tp, class _Up>
3403inline _LIBCPP_INLINE_VISIBILITY
3404bool
3405operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3406{
3407 return __y < __x;
3408}
3409
3410template<class _Tp, class _Up>
3411inline _LIBCPP_INLINE_VISIBILITY
3412bool
3413operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3414{
3415 return !(__y < __x);
3416}
3417
3418template<class _Tp, class _Up>
3419inline _LIBCPP_INLINE_VISIBILITY
3420bool
3421operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3422{
3423 return !(__x < __y);
3424}
3425
3426template<class _Tp>
3427inline _LIBCPP_INLINE_VISIBILITY
3428bool
3429operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3430{
3431 return !__x;
3432}
3433
3434template<class _Tp>
3435inline _LIBCPP_INLINE_VISIBILITY
3436bool
3437operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3438{
3439 return !__x;
3440}
3441
3442template<class _Tp>
3443inline _LIBCPP_INLINE_VISIBILITY
3444bool
3445operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3446{
3447 return static_cast<bool>(__x);
3448}
3449
3450template<class _Tp>
3451inline _LIBCPP_INLINE_VISIBILITY
3452bool
3453operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3454{
3455 return static_cast<bool>(__x);
3456}
3457
3458template<class _Tp>
3459inline _LIBCPP_INLINE_VISIBILITY
3460bool
3461operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3462{
3463 return less<_Tp*>()(__x.get(), nullptr);
3464}
3465
3466template<class _Tp>
3467inline _LIBCPP_INLINE_VISIBILITY
3468bool
3469operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3470{
3471 return less<_Tp*>()(nullptr, __x.get());
3472}
3473
3474template<class _Tp>
3475inline _LIBCPP_INLINE_VISIBILITY
3476bool
3477operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3478{
3479 return nullptr < __x;
3480}
3481
3482template<class _Tp>
3483inline _LIBCPP_INLINE_VISIBILITY
3484bool
3485operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3486{
3487 return __x < nullptr;
3488}
3489
3490template<class _Tp>
3491inline _LIBCPP_INLINE_VISIBILITY
3492bool
3493operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3494{
3495 return !(nullptr < __x);
3496}
3497
3498template<class _Tp>
3499inline _LIBCPP_INLINE_VISIBILITY
3500bool
3501operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3502{
3503 return !(__x < nullptr);
3504}
3505
3506template<class _Tp>
3507inline _LIBCPP_INLINE_VISIBILITY
3508bool
3509operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3510{
3511 return !(__x < nullptr);
3512}
3513
3514template<class _Tp>
3515inline _LIBCPP_INLINE_VISIBILITY
3516bool
3517operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3518{
3519 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520}
3521
3522template<class _Tp>
3523inline _LIBCPP_INLINE_VISIBILITY
3524void
Howard Hinnant719bda32011-05-28 14:41:13 +00003525swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526{
3527 __x.swap(__y);
3528}
3529
3530template<class _Tp, class _Up>
3531inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003532shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003533static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534{
zoecarverd73f42a2020-05-11 18:42:50 -07003535 return shared_ptr<_Tp>(__r,
3536 static_cast<
3537 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538}
3539
3540template<class _Tp, class _Up>
3541inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003542shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003543dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544{
zoecarverd73f42a2020-05-11 18:42:50 -07003545 typedef typename shared_ptr<_Tp>::element_type _ET;
3546 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3548}
3549
3550template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003551shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003552const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553{
zoecarverd73f42a2020-05-11 18:42:50 -07003554 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003555 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556}
3557
zoecarverd73f42a2020-05-11 18:42:50 -07003558template<class _Tp, class _Up>
3559shared_ptr<_Tp>
3560reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3561{
3562 return shared_ptr<_Tp>(__r,
3563 reinterpret_cast<
3564 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3565}
3566
Howard Hinnant72f73582010-08-11 17:04:31 +00003567#ifndef _LIBCPP_NO_RTTI
3568
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569template<class _Dp, class _Tp>
3570inline _LIBCPP_INLINE_VISIBILITY
3571_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003572get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573{
3574 return __p.template __get_deleter<_Dp>();
3575}
3576
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003577#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003578
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003580class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003582public:
3583 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584private:
3585 element_type* __ptr_;
3586 __shared_weak_count* __cntrl_;
3587
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003588public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003590 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003591 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003592 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3593 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003595 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003596 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003597 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3598 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003599
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003601 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003602 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003603 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3604 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003605 ~weak_ptr();
3606
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003608 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003609 template<class _Yp>
3610 typename enable_if
3611 <
3612 is_convertible<_Yp*, element_type*>::value,
3613 weak_ptr&
3614 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003616 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3617
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003619 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3620 template<class _Yp>
3621 typename enable_if
3622 <
3623 is_convertible<_Yp*, element_type*>::value,
3624 weak_ptr&
3625 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003627 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3628
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003629 template<class _Yp>
3630 typename enable_if
3631 <
3632 is_convertible<_Yp*, element_type*>::value,
3633 weak_ptr&
3634 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003636 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003637
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003639 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003641 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003642
Howard Hinnant756c69b2010-09-22 16:48:34 +00003643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003644 long use_count() const _NOEXCEPT
3645 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003647 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003648 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003649 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003650 template<class _Up>
3651 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003652 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003654 template<class _Up>
3655 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003656 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657 {return __cntrl_ < __r.__cntrl_;}
3658
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003659 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3660 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661};
3662
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003663#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3664template<class _Tp>
3665weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3666#endif
3667
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003669inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003670_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003671weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003672 : __ptr_(nullptr),
3673 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674{
3675}
3676
3677template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003678inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003679weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _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
3687template<class _Tp>
3688template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003691 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003692 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693 : __ptr_(__r.__ptr_),
3694 __cntrl_(__r.__cntrl_)
3695{
3696 if (__cntrl_)
3697 __cntrl_->__add_weak();
3698}
3699
3700template<class _Tp>
3701template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003702inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003704 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003705 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706 : __ptr_(__r.__ptr_),
3707 __cntrl_(__r.__cntrl_)
3708{
3709 if (__cntrl_)
3710 __cntrl_->__add_weak();
3711}
3712
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003713template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003714inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003715weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3716 : __ptr_(__r.__ptr_),
3717 __cntrl_(__r.__cntrl_)
3718{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003719 __r.__ptr_ = nullptr;
3720 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003721}
3722
3723template<class _Tp>
3724template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003725inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003726weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3727 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3728 _NOEXCEPT
3729 : __ptr_(__r.__ptr_),
3730 __cntrl_(__r.__cntrl_)
3731{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003732 __r.__ptr_ = nullptr;
3733 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003734}
3735
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736template<class _Tp>
3737weak_ptr<_Tp>::~weak_ptr()
3738{
3739 if (__cntrl_)
3740 __cntrl_->__release_weak();
3741}
3742
3743template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003744inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003746weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747{
3748 weak_ptr(__r).swap(*this);
3749 return *this;
3750}
3751
3752template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003753template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003754inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003755typename enable_if
3756<
3757 is_convertible<_Yp*, _Tp*>::value,
3758 weak_ptr<_Tp>&
3759>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003760weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761{
3762 weak_ptr(__r).swap(*this);
3763 return *this;
3764}
3765
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003766template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003767inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003768weak_ptr<_Tp>&
3769weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3770{
3771 weak_ptr(_VSTD::move(__r)).swap(*this);
3772 return *this;
3773}
3774
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003776template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003777inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003778typename enable_if
3779<
3780 is_convertible<_Yp*, _Tp*>::value,
3781 weak_ptr<_Tp>&
3782>::type
3783weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3784{
3785 weak_ptr(_VSTD::move(__r)).swap(*this);
3786 return *this;
3787}
3788
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003789template<class _Tp>
3790template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003791inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003792typename enable_if
3793<
3794 is_convertible<_Yp*, _Tp*>::value,
3795 weak_ptr<_Tp>&
3796>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003797weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798{
3799 weak_ptr(__r).swap(*this);
3800 return *this;
3801}
3802
3803template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003804inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805void
Howard Hinnant719bda32011-05-28 14:41:13 +00003806weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003808 _VSTD::swap(__ptr_, __r.__ptr_);
3809 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810}
3811
3812template<class _Tp>
3813inline _LIBCPP_INLINE_VISIBILITY
3814void
Howard Hinnant719bda32011-05-28 14:41:13 +00003815swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816{
3817 __x.swap(__y);
3818}
3819
3820template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003821inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822void
Howard Hinnant719bda32011-05-28 14:41:13 +00003823weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824{
3825 weak_ptr().swap(*this);
3826}
3827
3828template<class _Tp>
3829template<class _Yp>
3830shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003831 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 : __ptr_(__r.__ptr_),
3833 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3834{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003835 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003836 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837}
3838
3839template<class _Tp>
3840shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003841weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842{
3843 shared_ptr<_Tp> __r;
3844 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3845 if (__r.__cntrl_)
3846 __r.__ptr_ = __ptr_;
3847 return __r;
3848}
3849
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003850#if _LIBCPP_STD_VER > 14
3851template <class _Tp = void> struct owner_less;
3852#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003853template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003854#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855
3856template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003857struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003859{
3860 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003861 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003862 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003864 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003865 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003867 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003868 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003870};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871
3872template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003873struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3875{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003876 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003877 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003878 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003880 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003881 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003883 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003884 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885 {return __x.owner_before(__y);}
3886};
3887
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003888#if _LIBCPP_STD_VER > 14
3889template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003890struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003891{
3892 template <class _Tp, class _Up>
3893 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003894 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003895 {return __x.owner_before(__y);}
3896 template <class _Tp, class _Up>
3897 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003898 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003899 {return __x.owner_before(__y);}
3900 template <class _Tp, class _Up>
3901 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003902 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003903 {return __x.owner_before(__y);}
3904 template <class _Tp, class _Up>
3905 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003906 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003907 {return __x.owner_before(__y);}
3908 typedef void is_transparent;
3909};
3910#endif
3911
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003913class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003914{
3915 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003916protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003917 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003918 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003920 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003922 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3923 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003926public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003928 shared_ptr<_Tp> shared_from_this()
3929 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003931 shared_ptr<_Tp const> shared_from_this() const
3932 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003933
Eric Fiselier84006862016-06-02 00:15:35 +00003934#if _LIBCPP_STD_VER > 14
3935 _LIBCPP_INLINE_VISIBILITY
3936 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3937 { return __weak_this_; }
3938
3939 _LIBCPP_INLINE_VISIBILITY
3940 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3941 { return __weak_this_; }
3942#endif // _LIBCPP_STD_VER > 14
3943
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944 template <class _Up> friend class shared_ptr;
3945};
3946
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003947template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003948struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003949{
3950 typedef shared_ptr<_Tp> argument_type;
3951 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003952
Howard Hinnant756c69b2010-09-22 16:48:34 +00003953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003954 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003955 {
zoecarverd73f42a2020-05-11 18:42:50 -07003956 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003957 }
3958};
3959
Howard Hinnantc834c512011-11-29 18:15:50 +00003960template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003961inline _LIBCPP_INLINE_VISIBILITY
3962basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003963operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003964
Eric Fiselier9b492672016-06-18 02:12:53 +00003965
3966#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003967
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003968class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003969{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003970 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003971public:
3972 void lock() _NOEXCEPT;
3973 void unlock() _NOEXCEPT;
3974
3975private:
3976 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3977 __sp_mut(const __sp_mut&);
3978 __sp_mut& operator=(const __sp_mut&);
3979
Howard Hinnant8331b762013-03-06 23:30:19 +00003980 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003981};
3982
Mehdi Amini228053d2017-05-04 17:08:54 +00003983_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3984__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003985
3986template <class _Tp>
3987inline _LIBCPP_INLINE_VISIBILITY
3988bool
3989atomic_is_lock_free(const shared_ptr<_Tp>*)
3990{
3991 return false;
3992}
3993
3994template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003995_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003996shared_ptr<_Tp>
3997atomic_load(const shared_ptr<_Tp>* __p)
3998{
3999 __sp_mut& __m = __get_sp_mut(__p);
4000 __m.lock();
4001 shared_ptr<_Tp> __q = *__p;
4002 __m.unlock();
4003 return __q;
4004}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004005
Howard Hinnant9fa30202012-07-30 01:40:57 +00004006template <class _Tp>
4007inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004008_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004009shared_ptr<_Tp>
4010atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4011{
4012 return atomic_load(__p);
4013}
4014
4015template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004016_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004017void
4018atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4019{
4020 __sp_mut& __m = __get_sp_mut(__p);
4021 __m.lock();
4022 __p->swap(__r);
4023 __m.unlock();
4024}
4025
4026template <class _Tp>
4027inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004028_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004029void
4030atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4031{
4032 atomic_store(__p, __r);
4033}
4034
4035template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004036_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004037shared_ptr<_Tp>
4038atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4039{
4040 __sp_mut& __m = __get_sp_mut(__p);
4041 __m.lock();
4042 __p->swap(__r);
4043 __m.unlock();
4044 return __r;
4045}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004046
Howard Hinnant9fa30202012-07-30 01:40:57 +00004047template <class _Tp>
4048inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004049_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004050shared_ptr<_Tp>
4051atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4052{
4053 return atomic_exchange(__p, __r);
4054}
4055
4056template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004057_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004058bool
4059atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4060{
Marshall Clow4201ee82016-05-18 17:50:13 +00004061 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004062 __sp_mut& __m = __get_sp_mut(__p);
4063 __m.lock();
4064 if (__p->__owner_equivalent(*__v))
4065 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004066 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004067 *__p = __w;
4068 __m.unlock();
4069 return true;
4070 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004071 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004072 *__v = *__p;
4073 __m.unlock();
4074 return false;
4075}
4076
4077template <class _Tp>
4078inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004079_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004080bool
4081atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4082{
4083 return atomic_compare_exchange_strong(__p, __v, __w);
4084}
4085
4086template <class _Tp>
4087inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004088_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004089bool
4090atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4091 shared_ptr<_Tp> __w, memory_order, memory_order)
4092{
4093 return atomic_compare_exchange_strong(__p, __v, __w);
4094}
4095
4096template <class _Tp>
4097inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004098_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004099bool
4100atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4101 shared_ptr<_Tp> __w, memory_order, memory_order)
4102{
4103 return atomic_compare_exchange_weak(__p, __v, __w);
4104}
4105
Eric Fiselier9b492672016-06-18 02:12:53 +00004106#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004107
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004108//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004109#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4110# ifndef _LIBCPP_CXX03_LANG
4111enum class pointer_safety : unsigned char {
4112 relaxed,
4113 preferred,
4114 strict
4115};
4116# endif
4117#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004118struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004120 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121 {
4122 relaxed,
4123 preferred,
4124 strict
4125 };
4126
Howard Hinnant49e145e2012-10-30 19:06:59 +00004127 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128
Howard Hinnant756c69b2010-09-22 16:48:34 +00004129 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004130 pointer_safety() : __v_() {}
4131
4132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004133 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135 operator int() const {return __v_;}
4136};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004137#endif
4138
4139#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004140 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004141_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4142#else
4143// This function is only offered in C++03 under ABI v1.
4144# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4145inline _LIBCPP_INLINE_VISIBILITY
4146pointer_safety get_pointer_safety() _NOEXCEPT {
4147 return pointer_safety::relaxed;
4148}
4149# endif
4150#endif
4151
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004153_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4154_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4155_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004156_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157
4158template <class _Tp>
4159inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004160_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161undeclare_reachable(_Tp* __p)
4162{
4163 return static_cast<_Tp*>(__undeclare_reachable(__p));
4164}
4165
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004166_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167
Marshall Clow8982dcd2015-07-13 20:04:56 +00004168// --- Helper for container swap --
4169template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004170_LIBCPP_INLINE_VISIBILITY
4171void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4172#if _LIBCPP_STD_VER >= 14
4173 _NOEXCEPT
4174#else
4175 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4176#endif
4177{
4178 using _VSTD::swap;
4179 swap(__a1, __a2);
4180}
4181
4182template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004183inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004184void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4185
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004186template <typename _Alloc>
4187inline _LIBCPP_INLINE_VISIBILITY
4188void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4189#if _LIBCPP_STD_VER >= 14
4190 _NOEXCEPT
4191#else
4192 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4193#endif
4194{
4195 _VSTD::__swap_allocator(__a1, __a2,
4196 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4197}
4198
Marshall Clowff91de82015-08-18 19:51:37 +00004199template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004200struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004201 _Traits::propagate_on_container_move_assignment::value
4202#if _LIBCPP_STD_VER > 14
4203 || _Traits::is_always_equal::value
4204#else
4205 && is_nothrow_move_assignable<_Alloc>::value
4206#endif
4207 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004208
Marshall Clowa591b9a2016-07-11 21:38:08 +00004209
Marshall Clowa591b9a2016-07-11 21:38:08 +00004210template <class _Tp, class _Alloc>
4211struct __temp_value {
4212 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004213
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004214 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004215 _Alloc &__a;
4216
4217 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4218 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004219
Marshall Clowa591b9a2016-07-11 21:38:08 +00004220 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004221 _LIBCPP_NO_CFI
4222 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4223 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4224 _VSTD::forward<_Args>(__args)...);
4225 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004226
Marshall Clowa591b9a2016-07-11 21:38:08 +00004227 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4228 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004229
Marshall Clowe46031a2018-07-02 18:41:15 +00004230template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004231struct __is_allocator : false_type {};
4232
4233template<typename _Alloc>
4234struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004235 typename __void_t<typename _Alloc::value_type>::type,
4236 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4237 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004238 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004239
Eric Fiselier74ebee62019-06-08 01:31:19 +00004240// __builtin_new_allocator -- A non-templated helper for allocating and
4241// deallocating memory using __builtin_operator_new and
4242// __builtin_operator_delete. It should be used in preference to
4243// `std::allocator<T>` to avoid additional instantiations.
4244struct __builtin_new_allocator {
4245 struct __builtin_new_deleter {
4246 typedef void* pointer_type;
4247
4248 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4249 : __size_(__size), __align_(__align) {}
4250
4251 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004252 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004253 }
4254
4255 private:
4256 size_t __size_;
4257 size_t __align_;
4258 };
4259
4260 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4261
4262 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004263 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004264 __builtin_new_deleter(__s, __align));
4265 }
4266
4267 static void __deallocate_bytes(void* __p, size_t __s,
4268 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004269 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004270 }
4271
4272 template <class _Tp>
4273 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4274 static __holder_t __allocate_type(size_t __n) {
4275 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4276 }
4277
4278 template <class _Tp>
4279 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4280 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4281 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4282 }
4283};
4284
4285
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286_LIBCPP_END_NAMESPACE_STD
4287
Eric Fiselierf4433a32017-05-31 22:07:49 +00004288_LIBCPP_POP_MACROS
4289
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004290#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004291# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004292#endif
4293
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294#endif // _LIBCPP_MEMORY