blob: 01cbced19315f6f087987273655fe94a4e681426 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MEMORY
11#define _LIBCPP_MEMORY
12
13/*
14 memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000020inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27 typedef Ptr pointer;
28 typedef <details> element_type;
29 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000030
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000032
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 static pointer pointer_to(<details>);
34};
35
Howard Hinnant719bda32011-05-28 14:41:13 +000036template <class T>
37struct pointer_traits<T*>
38{
39 typedef T* pointer;
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
42
43 template <class U> using rebind = U*;
44
Louis Dionne44e1ea82018-11-13 17:04:05 +000045 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000046};
47
Eric Fiselier45c9aac2017-11-22 19:49:21 +000048template <class T> constexpr T* to_address(T* p) noexcept; // C++20
Marek Kurdej1f0cde92020-12-06 15:23:46 +010049template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
Eric Fiselier45c9aac2017-11-22 19:49:21 +000050
Howard Hinnantc51e1022010-05-11 19:42:16 +000051template <class Alloc>
52struct allocator_traits
53{
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
56 value_type;
57
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
61 const_pointer;
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
64 void_pointer;
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
67 const_void_pointer;
68 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000069 | pointer_traits<pointer>::difference_type
70 difference_type;
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
73 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000080 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Michael Park99f9e912020-03-04 11:27:14 -050083 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
Louis Dionne99f59432020-09-17 12:06:13 -040086 static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Louis Dionne99f59432020-09-17 12:06:13 -040089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
Louis Dionne99f59432020-09-17 12:06:13 -040092 static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000093
94 template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -040095 static void destroy(allocator_type& a, T* p); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
Louis Dionne99f59432020-09-17 12:06:13 -040097 static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
98 static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000099};
100
101template <>
Michael Park99f9e912020-03-04 11:27:14 -0500102class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
Louis Dionnec0056af2020-08-28 12:31:16 -0400116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef T* pointer; // deprecated in C++17, removed in C++20
119 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
120 typedef typename add_lvalue_reference<T>::type
121 reference; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<const T>::type
123 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
Michael Park99f9e912020-03-04 11:27:14 -0500125 typedef T value_type;
126
127 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
128
129 typedef true_type propagate_on_container_move_assignment;
130 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131
Marshall Clowbc759762018-03-20 23:02:53 +0000132 constexpr allocator() noexcept; // constexpr in C++20
133 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
134 template <class U>
135 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400136 ~allocator(); // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500137 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
138 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
139 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400140 T* allocate(size_t n); // constexpr in C++20
141 void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500142 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000143 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500144 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147};
148
149template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400150bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151
152template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400153bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
155template <class OutputIterator, class T>
156class raw_storage_iterator
157 : public iterator<output_iterator_tag,
158 T, // purposefully not C++03
159 ptrdiff_t, // purposefully not C++03
160 T*, // purposefully not C++03
161 raw_storage_iterator&> // purposefully not C++03
162{
163public:
164 explicit raw_storage_iterator(OutputIterator x);
165 raw_storage_iterator& operator*();
166 raw_storage_iterator& operator=(const T& element);
167 raw_storage_iterator& operator++();
168 raw_storage_iterator operator++(int);
169};
170
Howard Hinnant719bda32011-05-28 14:41:13 +0000171template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
172template <class T> void return_temporary_buffer(T* p) noexcept;
173
174template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000175template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176
177template <class InputIterator, class ForwardIterator>
178ForwardIterator
179uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
180
Howard Hinnant719bda32011-05-28 14:41:13 +0000181template <class InputIterator, class Size, class ForwardIterator>
182ForwardIterator
183uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
184
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185template <class ForwardIterator, class T>
186void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
187
188template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000189ForwardIterator
190uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Louis Dionne99f59432020-09-17 12:06:13 -0400192template <class T, class ...Args>
193constexpr T* construct_at(T* location, Args&& ...args); // since C++20
194
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000195template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -0400196void destroy_at(T* location); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000197
198template <class ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -0400199void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000200
201template <class ForwardIterator, class Size>
Louis Dionne99f59432020-09-17 12:06:13 -0400202ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000203
204template <class InputIterator, class ForwardIterator>
205 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
206
207template <class InputIterator, class Size, class ForwardIterator>
208 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
209
210template <class ForwardIterator>
211 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
212
213template <class ForwardIterator, class Size>
214 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
215
216template <class ForwardIterator>
217 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
218
219template <class ForwardIterator, class Size>
220 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
221
Louis Dionne481a2662018-09-23 18:35:00 +0000222template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223
224template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000225class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226{
227public:
228 typedef X element_type;
229
230 explicit auto_ptr(X* p =0) throw();
231 auto_ptr(auto_ptr&) throw();
232 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
233 auto_ptr& operator=(auto_ptr&) throw();
234 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
235 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
236 ~auto_ptr() throw();
237
238 typename add_lvalue_reference<X>::type operator*() const throw();
239 X* operator->() const throw();
240 X* get() const throw();
241 X* release() throw();
242 void reset(X* p =0) throw();
243
244 auto_ptr(auto_ptr_ref<X>) throw();
245 template<class Y> operator auto_ptr_ref<Y>() throw();
246 template<class Y> operator auto_ptr<Y>() throw();
247};
248
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000249template <class T>
250struct default_delete
251{
Howard Hinnant719bda32011-05-28 14:41:13 +0000252 constexpr default_delete() noexcept = default;
253 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000254
Howard Hinnant719bda32011-05-28 14:41:13 +0000255 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000256};
257
258template <class T>
259struct default_delete<T[]>
260{
Howard Hinnant719bda32011-05-28 14:41:13 +0000261 constexpr default_delete() noexcept = default;
262 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000263 template <class U> void operator()(U*) const = delete;
264};
265
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000266template <class T, class D = default_delete<T>>
267class unique_ptr
268{
269public:
270 typedef see below pointer;
271 typedef T element_type;
272 typedef D deleter_type;
273
274 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000275 constexpr unique_ptr() noexcept;
276 explicit unique_ptr(pointer p) noexcept;
277 unique_ptr(pointer p, see below d1) noexcept;
278 unique_ptr(pointer p, see below d2) noexcept;
279 unique_ptr(unique_ptr&& u) noexcept;
280 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000281 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000283 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000284 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // destructor
287 ~unique_ptr();
288
289 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000290 unique_ptr& operator=(unique_ptr&& u) noexcept;
291 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
292 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // observers
295 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000296 pointer operator->() const noexcept;
297 pointer get() const noexcept;
298 deleter_type& get_deleter() noexcept;
299 const deleter_type& get_deleter() const noexcept;
300 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000301
302 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000303 pointer release() noexcept;
304 void reset(pointer p = pointer()) noexcept;
305 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000306};
307
308template <class T, class D>
309class unique_ptr<T[], D>
310{
311public:
312 typedef implementation-defined pointer;
313 typedef T element_type;
314 typedef D deleter_type;
315
316 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 constexpr unique_ptr() noexcept;
318 explicit unique_ptr(pointer p) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(pointer p, see below d) noexcept;
321 unique_ptr(unique_ptr&& u) noexcept;
322 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000323
324 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000325 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 unique_ptr& operator=(unique_ptr&& u) noexcept;
329 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000330
331 // observers
332 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000333 pointer get() const noexcept;
334 deleter_type& get_deleter() noexcept;
335 const deleter_type& get_deleter() const noexcept;
336 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 pointer release() noexcept;
340 void reset(pointer p = pointer()) noexcept;
341 void reset(nullptr_t) noexcept;
Vy Nguyene369bd92020-07-13 12:34:37 -0400342 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000343 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000344};
345
346template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000347 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000348
349template <class T1, class D1, class T2, class D2>
350 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353template <class T1, class D1, class T2, class D2>
354 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
355template <class T1, class D1, class T2, class D2>
356 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
357template <class T1, class D1, class T2, class D2>
358 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
359template <class T1, class D1, class T2, class D2>
360 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
361
Howard Hinnant719bda32011-05-28 14:41:13 +0000362template <class T, class D>
363 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
364template <class T, class D>
365 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
366template <class T, class D>
367 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
368template <class T, class D>
369 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
370
371template <class T, class D>
372 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
379template <class T, class D>
380 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
381template <class T, class D>
382 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
383template <class T, class D>
384 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
385template <class T, class D>
386 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
387
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000388class bad_weak_ptr
389 : public std::exception
390{
Howard Hinnant719bda32011-05-28 14:41:13 +0000391 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000392};
393
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000394template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
395template<class T> unique_ptr<T> make_unique(size_t n); // C++14
396template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
397
Marshall Clowe52a3242017-11-27 15:51:36 +0000398template<class E, class T, class Y, class D>
399 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
400
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000401template<class T>
402class shared_ptr
403{
404public:
405 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000406 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000407
408 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000409 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000410 template<class Y> explicit shared_ptr(Y* p);
411 template<class Y, class D> shared_ptr(Y* p, D d);
412 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
413 template <class D> shared_ptr(nullptr_t p, D d);
414 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
416 shared_ptr(const shared_ptr& r) noexcept;
417 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
418 shared_ptr(shared_ptr&& r) noexcept;
419 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000421 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000422 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
423 shared_ptr(nullptr_t) : shared_ptr() { }
424
425 // destructor:
426 ~shared_ptr();
427
428 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 shared_ptr& operator=(const shared_ptr& r) noexcept;
430 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
431 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000432 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000433 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000434 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
435
436 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000437 void swap(shared_ptr& r) noexcept;
438 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000439 template<class Y> void reset(Y* p);
440 template<class Y, class D> void reset(Y* p, D d);
441 template<class Y, class D, class A> void reset(Y* p, D d, A a);
442
Howard Hinnant719bda32011-05-28 14:41:13 +0000443 // observers:
444 T* get() const noexcept;
445 T& operator*() const noexcept;
446 T* operator->() const noexcept;
447 long use_count() const noexcept;
448 bool unique() const noexcept;
449 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000450 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
451 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000452};
453
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400454template<class T>
455shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
456template<class T, class D>
457shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
458
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000459// shared_ptr comparisons:
460template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000461 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000462template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000463 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000464template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000465 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000466template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000467 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000468template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000469 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000470template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000471 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
472
473template <class T>
474 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
481template <class T>
482 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
483template <class T>
484bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
485template <class T>
486 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
487template <class T>
488 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
489template <class T>
490 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
491template <class T>
492 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
493template <class T>
494 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
495template <class T>
496 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000497
498// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000499template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500
501// shared_ptr casts:
502template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000503 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000504template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000505 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000506template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000507 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000508
509// shared_ptr I/O:
510template<class E, class T, class Y>
511 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
512
513// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000514template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516template<class T, class... Args>
517 shared_ptr<T> make_shared(Args&&... args);
518template<class T, class A, class... Args>
519 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
520
521template<class T>
522class weak_ptr
523{
524public:
525 typedef T element_type;
526
527 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000528 constexpr weak_ptr() noexcept;
529 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
530 weak_ptr(weak_ptr const& r) noexcept;
531 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000532 weak_ptr(weak_ptr&& r) noexcept; // C++14
533 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000534
535 // destructor
536 ~weak_ptr();
537
538 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000539 weak_ptr& operator=(weak_ptr const& r) noexcept;
540 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
541 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000542 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
543 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000544
545 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000546 void swap(weak_ptr& r) noexcept;
547 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000548
549 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000550 long use_count() const noexcept;
551 bool expired() const noexcept;
552 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000553 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
554 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000555};
556
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400557template<class T>
558weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
559
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000560// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000561template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000562
563// class owner_less:
564template<class T> struct owner_less;
565
566template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000567struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000568 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
569{
570 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000571 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
572 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000574};
575
576template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000577struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000578 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
579{
580 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000581 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
583 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
584};
585
586template <> // Added in C++14
587struct owner_less<void>
588{
589 template <class _Tp, class _Up>
590 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
591 template <class _Tp, class _Up>
592 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
593 template <class _Tp, class _Up>
594 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
595 template <class _Tp, class _Up>
596 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
597
598 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599};
600
601template<class T>
602class enable_shared_from_this
603{
604protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000605 constexpr enable_shared_from_this() noexcept;
606 enable_shared_from_this(enable_shared_from_this const&) noexcept;
607 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000608 ~enable_shared_from_this();
609public:
610 shared_ptr<T> shared_from_this();
611 shared_ptr<T const> shared_from_this() const;
612};
613
614template<class T>
615 bool atomic_is_lock_free(const shared_ptr<T>* p);
616template<class T>
617 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
618template<class T>
619 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
620template<class T>
621 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
622template<class T>
623 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
624template<class T>
625 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
626template<class T>
627 shared_ptr<T>
628 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
629template<class T>
630 bool
631 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
632template<class T>
633 bool
634 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
635template<class T>
636 bool
637 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
638 shared_ptr<T> w, memory_order success,
639 memory_order failure);
640template<class T>
641 bool
642 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
643 shared_ptr<T> w, memory_order success,
644 memory_order failure);
645// Hash support
646template <class T> struct hash;
647template <class T, class D> struct hash<unique_ptr<T, D> >;
648template <class T> struct hash<shared_ptr<T> >;
649
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000650template <class T, class Alloc>
651 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
652
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000653// Pointer safety
654enum class pointer_safety { relaxed, preferred, strict };
655void declare_reachable(void *p);
656template <class T> T *undeclare_reachable(T *p);
657void declare_no_pointers(char *p, size_t n);
658void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000659pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000660
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
662
663} // std
664
665*/
666
667#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500668#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669#include <type_traits>
670#include <typeinfo>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400671#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672#include <cstddef>
673#include <cstdint>
674#include <new>
675#include <utility>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676#include <iterator>
677#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000678#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000679#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000680#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000681#include <cstring>
Louis Dionne1e38faa2021-04-09 12:48:34 -0400682#include <__memory/allocator.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500683#include <__memory/allocator_traits.h>
Louis Dionne26bc98e2021-04-09 12:44:26 -0400684#include <__memory/auto_ptr.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500685#include <__memory/base.h>
Louis Dionne556fcd02021-04-09 14:43:01 -0400686#include <__memory/compressed_pair.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500687#include <__memory/pointer_traits.h>
Louis Dionnee0103192021-04-09 14:45:18 -0400688#include <__memory/raw_storage_iterator.h>
Louis Dionne00180872021-04-09 12:58:00 -0400689#include <__memory/temporary_buffer.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500690#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000691#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000692# include <atomic>
693#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000694#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000695
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000696#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000698#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699
Eric Fiselierf4433a32017-05-31 22:07:49 +0000700_LIBCPP_PUSH_MACROS
701#include <__undef_macros>
702
703
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704_LIBCPP_BEGIN_NAMESPACE_STD
705
Eric Fiselier89659d12015-07-07 00:27:16 +0000706template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000707inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000708_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
709#if !defined(_LIBCPP_HAS_NO_THREADS) && \
710 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000711 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000712 return __atomic_load_n(__value, __ATOMIC_RELAXED);
713#else
714 return *__value;
715#endif
716}
717
Kuba Breckade9d6792016-09-04 09:55:12 +0000718template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000719inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000720_ValueType __libcpp_acquire_load(_ValueType const* __value) {
721#if !defined(_LIBCPP_HAS_NO_THREADS) && \
722 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000723 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000724 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
725#else
726 return *__value;
727#endif
728}
729
Louis Dionned6651542020-11-03 12:05:55 -0500730template <class _Alloc, class _Ptr>
731_LIBCPP_INLINE_VISIBILITY
732void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
733 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500734 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500735 typedef allocator_traits<_Alloc> _Traits;
736 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
737 _Traits::construct(__a, _VSTD::__to_address(__begin2),
738#ifdef _LIBCPP_NO_EXCEPTIONS
739 _VSTD::move(*__begin1)
740#else
741 _VSTD::move_if_noexcept(*__begin1)
742#endif
743 );
744 }
745}
746
747template <class _Alloc, class _Tp, typename enable_if<
748 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
749 is_trivially_move_constructible<_Tp>::value
750>::type>
751_LIBCPP_INLINE_VISIBILITY
752void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
753 ptrdiff_t _Np = __end1 - __begin1;
754 if (_Np > 0) {
755 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
756 __begin2 += _Np;
757 }
758}
759
760template <class _Alloc, class _Iter, class _Ptr>
761_LIBCPP_INLINE_VISIBILITY
762void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
763 typedef allocator_traits<_Alloc> _Traits;
764 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
765 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
766 }
767}
768
769template <class _Alloc, class _Source, class _Dest,
770 class _RawSource = typename remove_const<_Source>::type,
771 class _RawDest = typename remove_const<_Dest>::type,
772 class =
773 typename enable_if<
774 is_trivially_copy_constructible<_Dest>::value &&
775 is_same<_RawSource, _RawDest>::value &&
776 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
777 >::type>
778_LIBCPP_INLINE_VISIBILITY
779void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
780 ptrdiff_t _Np = __end1 - __begin1;
781 if (_Np > 0) {
782 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
783 __begin2 += _Np;
784 }
785}
786
787template <class _Alloc, class _Ptr>
788_LIBCPP_INLINE_VISIBILITY
789void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
790 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
791 "The specified type does not meet the requirements of Cpp17MoveInsertable");
792 typedef allocator_traits<_Alloc> _Traits;
793 while (__end1 != __begin1) {
794 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
795#ifdef _LIBCPP_NO_EXCEPTIONS
796 _VSTD::move(*--__end1)
797#else
798 _VSTD::move_if_noexcept(*--__end1)
799#endif
800 );
801 --__end2;
802 }
803}
804
805template <class _Alloc, class _Tp, class = typename enable_if<
806 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
807 is_trivially_move_constructible<_Tp>::value
808>::type>
809_LIBCPP_INLINE_VISIBILITY
810void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
811 ptrdiff_t _Np = __end1 - __begin1;
812 __end2 -= _Np;
813 if (_Np > 0)
814 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
815}
816
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000817// default_delete
818
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +0000820struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +0000821 static_assert(!is_function<_Tp>::value,
822 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +0000823#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000824 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000825#else
Eric Fiselier6779b062017-04-16 02:06:25 +0000826 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000827#endif
Eric Fiselier6779b062017-04-16 02:06:25 +0000828 template <class _Up>
829 _LIBCPP_INLINE_VISIBILITY
830 default_delete(const default_delete<_Up>&,
831 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
832 0) _NOEXCEPT {}
833
834 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
835 static_assert(sizeof(_Tp) > 0,
836 "default_delete can not delete incomplete type");
837 static_assert(!is_void<_Tp>::value,
838 "default_delete can not delete incomplete type");
839 delete __ptr;
840 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841};
842
843template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +0000844struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
845private:
846 template <class _Up>
847 struct _EnableIfConvertible
848 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
849
Howard Hinnant4500ca52011-12-18 21:19:44 +0000850public:
Eric Fiselier6779b062017-04-16 02:06:25 +0000851#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000852 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000853#else
Eric Fiselier6779b062017-04-16 02:06:25 +0000854 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000855#endif
Eric Fiselier6779b062017-04-16 02:06:25 +0000856
857 template <class _Up>
858 _LIBCPP_INLINE_VISIBILITY
859 default_delete(const default_delete<_Up[]>&,
860 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
861
862 template <class _Up>
863 _LIBCPP_INLINE_VISIBILITY
864 typename _EnableIfConvertible<_Up>::type
865 operator()(_Up* __ptr) const _NOEXCEPT {
866 static_assert(sizeof(_Tp) > 0,
867 "default_delete can not delete incomplete type");
868 static_assert(!is_void<_Tp>::value,
869 "default_delete can not delete void type");
870 delete[] __ptr;
871 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872};
873
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000874template <class _Deleter>
875struct __unique_ptr_deleter_sfinae {
876 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
877 typedef const _Deleter& __lval_ref_type;
878 typedef _Deleter&& __good_rval_ref_type;
879 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880};
881
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000882template <class _Deleter>
883struct __unique_ptr_deleter_sfinae<_Deleter const&> {
884 typedef const _Deleter& __lval_ref_type;
885 typedef const _Deleter&& __bad_rval_ref_type;
886 typedef false_type __enable_rval_overload;
887};
888
889template <class _Deleter>
890struct __unique_ptr_deleter_sfinae<_Deleter&> {
891 typedef _Deleter& __lval_ref_type;
892 typedef _Deleter&& __bad_rval_ref_type;
893 typedef false_type __enable_rval_overload;
894};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000895
Vy Nguyene369bd92020-07-13 12:34:37 -0400896#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
897# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
898#else
899# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
900#endif
901
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000902template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -0400903class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000904public:
905 typedef _Tp element_type;
906 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -0500907 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000908
909 static_assert(!is_rvalue_reference<deleter_type>::value,
910 "the specified deleter type cannot be an rvalue reference");
911
912private:
913 __compressed_pair<pointer, deleter_type> __ptr_;
914
915 struct __nat { int __for_bool_; };
916
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000917 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000918
919 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000920 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000921 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000922
923 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000924 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000925 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000926
927 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000928 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000929 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000930
931 template <bool _Dummy, class _Deleter = typename __dependent_type<
932 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000933 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000934 typename enable_if<is_default_constructible<_Deleter>::value &&
935 !is_pointer<_Deleter>::value>::type;
936
937 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000938 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000939 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
940
941 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000942 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000943 is_convertible<typename _UPtr::pointer, pointer>::value &&
944 !is_array<_Up>::value
945 >::type;
946
947 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000948 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000949 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
950 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
951 >::type;
952
953 template <class _UDel>
954 using _EnableIfDeleterAssignable = typename enable_if<
955 is_assignable<_Dp&, _UDel&&>::value
956 >::type;
957
958public:
959 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000960 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000961 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500962 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000963
964 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000965 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000966 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500967 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000968
969 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000970 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000971 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500972 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000973
974 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000975 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000976 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000977 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000978 : __ptr_(__p, __d) {}
979
980 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000981 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000982 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000983 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000984 : __ptr_(__p, _VSTD::move(__d)) {
985 static_assert(!is_reference<deleter_type>::value,
986 "rvalue deleter bound to reference");
987 }
988
989 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000990 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000991 _LIBCPP_INLINE_VISIBILITY
992 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
993
994 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000995 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000996 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
997 }
998
999 template <class _Up, class _Ep,
1000 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1001 class = _EnableIfDeleterConvertible<_Ep>
1002 >
1003 _LIBCPP_INLINE_VISIBILITY
1004 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1005 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1006
1007#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1008 template <class _Up>
1009 _LIBCPP_INLINE_VISIBILITY
1010 unique_ptr(auto_ptr<_Up>&& __p,
1011 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001012 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001013 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001014 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001015#endif
1016
1017 _LIBCPP_INLINE_VISIBILITY
1018 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1019 reset(__u.release());
1020 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1021 return *this;
1022 }
1023
1024 template <class _Up, class _Ep,
1025 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1026 class = _EnableIfDeleterAssignable<_Ep>
1027 >
1028 _LIBCPP_INLINE_VISIBILITY
1029 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1030 reset(__u.release());
1031 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1032 return *this;
1033 }
1034
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001035#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1036 template <class _Up>
1037 _LIBCPP_INLINE_VISIBILITY
1038 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1039 is_same<_Dp, default_delete<_Tp> >::value,
1040 unique_ptr&>::type
1041 operator=(auto_ptr<_Up> __p) {
1042 reset(__p.release());
1043 return *this;
1044 }
1045#endif
1046
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001047#ifdef _LIBCPP_CXX03_LANG
1048 unique_ptr(unique_ptr const&) = delete;
1049 unique_ptr& operator=(unique_ptr const&) = delete;
1050#endif
1051
1052
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001053 _LIBCPP_INLINE_VISIBILITY
1054 ~unique_ptr() { reset(); }
1055
1056 _LIBCPP_INLINE_VISIBILITY
1057 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1058 reset();
1059 return *this;
1060 }
1061
1062 _LIBCPP_INLINE_VISIBILITY
1063 typename add_lvalue_reference<_Tp>::type
1064 operator*() const {
1065 return *__ptr_.first();
1066 }
1067 _LIBCPP_INLINE_VISIBILITY
1068 pointer operator->() const _NOEXCEPT {
1069 return __ptr_.first();
1070 }
1071 _LIBCPP_INLINE_VISIBILITY
1072 pointer get() const _NOEXCEPT {
1073 return __ptr_.first();
1074 }
1075 _LIBCPP_INLINE_VISIBILITY
1076 deleter_type& get_deleter() _NOEXCEPT {
1077 return __ptr_.second();
1078 }
1079 _LIBCPP_INLINE_VISIBILITY
1080 const deleter_type& get_deleter() const _NOEXCEPT {
1081 return __ptr_.second();
1082 }
1083 _LIBCPP_INLINE_VISIBILITY
1084 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1085 return __ptr_.first() != nullptr;
1086 }
1087
1088 _LIBCPP_INLINE_VISIBILITY
1089 pointer release() _NOEXCEPT {
1090 pointer __t = __ptr_.first();
1091 __ptr_.first() = pointer();
1092 return __t;
1093 }
1094
1095 _LIBCPP_INLINE_VISIBILITY
1096 void reset(pointer __p = pointer()) _NOEXCEPT {
1097 pointer __tmp = __ptr_.first();
1098 __ptr_.first() = __p;
1099 if (__tmp)
1100 __ptr_.second()(__tmp);
1101 }
1102
1103 _LIBCPP_INLINE_VISIBILITY
1104 void swap(unique_ptr& __u) _NOEXCEPT {
1105 __ptr_.swap(__u.__ptr_);
1106 }
1107};
1108
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001109
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001111class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001113 typedef _Tp element_type;
1114 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001115 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001116
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001118 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119
Eric Fiselier31127cd2017-04-16 02:14:31 +00001120 template <class _From>
1121 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122
Eric Fiselier31127cd2017-04-16 02:14:31 +00001123 template <class _FromElem>
1124 struct _CheckArrayPointerConversion<_FromElem*>
1125 : integral_constant<bool,
1126 is_same<_FromElem*, pointer>::value ||
1127 (is_same<pointer, element_type*>::value &&
1128 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1129 >
1130 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131
Eric Fiselier31127cd2017-04-16 02:14:31 +00001132 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001133
1134 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001135 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001136 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001137
1138 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001139 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001140 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001141
1142 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001143 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001144 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001145
1146 template <bool _Dummy, class _Deleter = typename __dependent_type<
1147 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001148 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001149 typename enable_if<is_default_constructible<_Deleter>::value &&
1150 !is_pointer<_Deleter>::value>::type;
1151
1152 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001153 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001154 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1155
1156 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001157 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001158 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001159 >::type;
1160
1161 template <class _UPtr, class _Up,
1162 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001163 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001164 is_array<_Up>::value &&
1165 is_same<pointer, element_type*>::value &&
1166 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1167 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1168 >::type;
1169
1170 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001171 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001172 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1173 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1174 >::type;
1175
1176 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001177 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001178 is_assignable<_Dp&, _UDel&&>::value
1179 >::type;
1180
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001182 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001183 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001184 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001185 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001187 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001188 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001189 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001190 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001192 template <class _Pp, bool _Dummy = true,
1193 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001194 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001195 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001196 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001197 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001199 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001200 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1201 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001202 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001203 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001204 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001206 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001207 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001208 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001209 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001210 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001212 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001213 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1214 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001215 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001216 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001217 : __ptr_(__p, _VSTD::move(__d)) {
1218 static_assert(!is_reference<deleter_type>::value,
1219 "rvalue deleter bound to reference");
1220 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001222 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001223 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001224 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001225 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001226 : __ptr_(nullptr, _VSTD::move(__d)) {
1227 static_assert(!is_reference<deleter_type>::value,
1228 "rvalue deleter bound to reference");
1229 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001230
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001231 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001232 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1233 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001234 _LIBCPP_INLINE_VISIBILITY
1235 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001236
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001237 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001238 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001239 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1240 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001241
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001242 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001243 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001244 reset(__u.release());
1245 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1246 return *this;
1247 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001249 template <class _Up, class _Ep,
1250 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1251 class = _EnableIfDeleterConvertible<_Ep>
1252 >
1253 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001254 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001255 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001256 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001258 template <class _Up, class _Ep,
1259 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1260 class = _EnableIfDeleterAssignable<_Ep>
1261 >
1262 _LIBCPP_INLINE_VISIBILITY
1263 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001264 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001265 reset(__u.release());
1266 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1267 return *this;
1268 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001270#ifdef _LIBCPP_CXX03_LANG
1271 unique_ptr(unique_ptr const&) = delete;
1272 unique_ptr& operator=(unique_ptr const&) = delete;
1273#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001274
1275public:
1276 _LIBCPP_INLINE_VISIBILITY
1277 ~unique_ptr() { reset(); }
1278
1279 _LIBCPP_INLINE_VISIBILITY
1280 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1281 reset();
1282 return *this;
1283 }
1284
1285 _LIBCPP_INLINE_VISIBILITY
1286 typename add_lvalue_reference<_Tp>::type
1287 operator[](size_t __i) const {
1288 return __ptr_.first()[__i];
1289 }
1290 _LIBCPP_INLINE_VISIBILITY
1291 pointer get() const _NOEXCEPT {
1292 return __ptr_.first();
1293 }
1294
1295 _LIBCPP_INLINE_VISIBILITY
1296 deleter_type& get_deleter() _NOEXCEPT {
1297 return __ptr_.second();
1298 }
1299
1300 _LIBCPP_INLINE_VISIBILITY
1301 const deleter_type& get_deleter() const _NOEXCEPT {
1302 return __ptr_.second();
1303 }
1304 _LIBCPP_INLINE_VISIBILITY
1305 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1306 return __ptr_.first() != nullptr;
1307 }
1308
1309 _LIBCPP_INLINE_VISIBILITY
1310 pointer release() _NOEXCEPT {
1311 pointer __t = __ptr_.first();
1312 __ptr_.first() = pointer();
1313 return __t;
1314 }
1315
1316 template <class _Pp>
1317 _LIBCPP_INLINE_VISIBILITY
1318 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001319 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001320 >::type
1321 reset(_Pp __p) _NOEXCEPT {
1322 pointer __tmp = __ptr_.first();
1323 __ptr_.first() = __p;
1324 if (__tmp)
1325 __ptr_.second()(__tmp);
1326 }
1327
1328 _LIBCPP_INLINE_VISIBILITY
1329 void reset(nullptr_t = nullptr) _NOEXCEPT {
1330 pointer __tmp = __ptr_.first();
1331 __ptr_.first() = nullptr;
1332 if (__tmp)
1333 __ptr_.second()(__tmp);
1334 }
1335
1336 _LIBCPP_INLINE_VISIBILITY
1337 void swap(unique_ptr& __u) _NOEXCEPT {
1338 __ptr_.swap(__u.__ptr_);
1339 }
1340
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341};
1342
1343template <class _Tp, class _Dp>
1344inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001345typename enable_if<
1346 __is_swappable<_Dp>::value,
1347 void
1348>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001349swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350
1351template <class _T1, class _D1, class _T2, class _D2>
1352inline _LIBCPP_INLINE_VISIBILITY
1353bool
1354operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1355
1356template <class _T1, class _D1, class _T2, class _D2>
1357inline _LIBCPP_INLINE_VISIBILITY
1358bool
1359operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1360
1361template <class _T1, class _D1, class _T2, class _D2>
1362inline _LIBCPP_INLINE_VISIBILITY
1363bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001364operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1365{
1366 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1367 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001368 typedef typename common_type<_P1, _P2>::type _Vp;
1369 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001370}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
1372template <class _T1, class _D1, class _T2, class _D2>
1373inline _LIBCPP_INLINE_VISIBILITY
1374bool
1375operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1376
1377template <class _T1, class _D1, class _T2, class _D2>
1378inline _LIBCPP_INLINE_VISIBILITY
1379bool
1380operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1381
1382template <class _T1, class _D1, class _T2, class _D2>
1383inline _LIBCPP_INLINE_VISIBILITY
1384bool
1385operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1386
Howard Hinnantb17caf92012-02-21 21:02:58 +00001387template <class _T1, class _D1>
1388inline _LIBCPP_INLINE_VISIBILITY
1389bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001390operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001391{
1392 return !__x;
1393}
1394
1395template <class _T1, class _D1>
1396inline _LIBCPP_INLINE_VISIBILITY
1397bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001398operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001399{
1400 return !__x;
1401}
1402
1403template <class _T1, class _D1>
1404inline _LIBCPP_INLINE_VISIBILITY
1405bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001406operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001407{
1408 return static_cast<bool>(__x);
1409}
1410
1411template <class _T1, class _D1>
1412inline _LIBCPP_INLINE_VISIBILITY
1413bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001414operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001415{
1416 return static_cast<bool>(__x);
1417}
1418
1419template <class _T1, class _D1>
1420inline _LIBCPP_INLINE_VISIBILITY
1421bool
1422operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1423{
1424 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1425 return less<_P1>()(__x.get(), nullptr);
1426}
1427
1428template <class _T1, class _D1>
1429inline _LIBCPP_INLINE_VISIBILITY
1430bool
1431operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1432{
1433 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1434 return less<_P1>()(nullptr, __x.get());
1435}
1436
1437template <class _T1, class _D1>
1438inline _LIBCPP_INLINE_VISIBILITY
1439bool
1440operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1441{
1442 return nullptr < __x;
1443}
1444
1445template <class _T1, class _D1>
1446inline _LIBCPP_INLINE_VISIBILITY
1447bool
1448operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1449{
1450 return __x < nullptr;
1451}
1452
1453template <class _T1, class _D1>
1454inline _LIBCPP_INLINE_VISIBILITY
1455bool
1456operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1457{
1458 return !(nullptr < __x);
1459}
1460
1461template <class _T1, class _D1>
1462inline _LIBCPP_INLINE_VISIBILITY
1463bool
1464operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1465{
1466 return !(__x < nullptr);
1467}
1468
1469template <class _T1, class _D1>
1470inline _LIBCPP_INLINE_VISIBILITY
1471bool
1472operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1473{
1474 return !(__x < nullptr);
1475}
1476
1477template <class _T1, class _D1>
1478inline _LIBCPP_INLINE_VISIBILITY
1479bool
1480operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1481{
1482 return !(nullptr < __x);
1483}
1484
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001485#if _LIBCPP_STD_VER > 11
1486
1487template<class _Tp>
1488struct __unique_if
1489{
1490 typedef unique_ptr<_Tp> __unique_single;
1491};
1492
1493template<class _Tp>
1494struct __unique_if<_Tp[]>
1495{
1496 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1497};
1498
1499template<class _Tp, size_t _Np>
1500struct __unique_if<_Tp[_Np]>
1501{
1502 typedef void __unique_array_known_bound;
1503};
1504
1505template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00001506inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001507typename __unique_if<_Tp>::__unique_single
1508make_unique(_Args&&... __args)
1509{
1510 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
1511}
1512
1513template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00001514inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001515typename __unique_if<_Tp>::__unique_array_unknown_bound
1516make_unique(size_t __n)
1517{
1518 typedef typename remove_extent<_Tp>::type _Up;
1519 return unique_ptr<_Tp>(new _Up[__n]());
1520}
1521
1522template<class _Tp, class... _Args>
1523 typename __unique_if<_Tp>::__unique_array_known_bound
1524 make_unique(_Args&&...) = delete;
1525
1526#endif // _LIBCPP_STD_VER > 11
1527
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001528template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001529#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001530struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001531#else
1532struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001533 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001534#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001535{
1536 typedef unique_ptr<_Tp, _Dp> argument_type;
1537 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001538 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001539 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001540 {
1541 typedef typename argument_type::pointer pointer;
1542 return hash<pointer>()(__ptr.get());
1543 }
1544};
1545
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546struct __destruct_n
1547{
1548private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001549 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550
1551 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001552 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001553 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554
1555 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001556 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 {}
1558
Howard Hinnant719bda32011-05-28 14:41:13 +00001559 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001560 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001561 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 {}
1563
Howard Hinnant719bda32011-05-28 14:41:13 +00001564 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001565 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001566 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {}
1568public:
Howard Hinnant719bda32011-05-28 14:41:13 +00001569 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001570 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571
1572 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05001573 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001574 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575
1576 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001577 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001578 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579
1580 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001581 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001582 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583};
1584
1585template <class _Alloc>
1586class __allocator_destructor
1587{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001588 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001590 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
1591 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592private:
1593 _Alloc& __alloc_;
1594 size_type __s_;
1595public:
1596 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00001597 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001600 void operator()(pointer __p) _NOEXCEPT
1601 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602};
1603
1604template <class _InputIterator, class _ForwardIterator>
1605_ForwardIterator
1606uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
1607{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001609#ifndef _LIBCPP_NO_EXCEPTIONS
1610 _ForwardIterator __s = __r;
1611 try
1612 {
1613#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001614 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001615 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001616#ifndef _LIBCPP_NO_EXCEPTIONS
1617 }
1618 catch (...)
1619 {
1620 for (; __s != __r; ++__s)
1621 __s->~value_type();
1622 throw;
1623 }
1624#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 return __r;
1626}
1627
1628template <class _InputIterator, class _Size, class _ForwardIterator>
1629_ForwardIterator
1630uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
1631{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001633#ifndef _LIBCPP_NO_EXCEPTIONS
1634 _ForwardIterator __s = __r;
1635 try
1636 {
1637#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001638 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001639 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001640#ifndef _LIBCPP_NO_EXCEPTIONS
1641 }
1642 catch (...)
1643 {
1644 for (; __s != __r; ++__s)
1645 __s->~value_type();
1646 throw;
1647 }
1648#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649 return __r;
1650}
1651
1652template <class _ForwardIterator, class _Tp>
1653void
1654uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
1655{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001657#ifndef _LIBCPP_NO_EXCEPTIONS
1658 _ForwardIterator __s = __f;
1659 try
1660 {
1661#endif
1662 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001663 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001664#ifndef _LIBCPP_NO_EXCEPTIONS
1665 }
1666 catch (...)
1667 {
1668 for (; __s != __f; ++__s)
1669 __s->~value_type();
1670 throw;
1671 }
1672#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673}
1674
1675template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00001676_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
1678{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001680#ifndef _LIBCPP_NO_EXCEPTIONS
1681 _ForwardIterator __s = __f;
1682 try
1683 {
1684#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001685 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001686 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001687#ifndef _LIBCPP_NO_EXCEPTIONS
1688 }
1689 catch (...)
1690 {
1691 for (; __s != __f; ++__s)
1692 __s->~value_type();
1693 throw;
1694 }
1695#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00001696 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697}
1698
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001699#if _LIBCPP_STD_VER > 14
1700
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001701template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04001702inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001703void destroy(_ForwardIterator __first, _ForwardIterator __last) {
1704 for (; __first != __last; ++__first)
1705 _VSTD::destroy_at(_VSTD::addressof(*__first));
1706}
1707
1708template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04001709inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001710_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
1711 for (; __n > 0; (void)++__first, --__n)
1712 _VSTD::destroy_at(_VSTD::addressof(*__first));
1713 return __first;
1714}
1715
Eric Fiselier290c07c2016-10-11 21:13:44 +00001716template <class _ForwardIterator>
1717inline _LIBCPP_INLINE_VISIBILITY
1718void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
1719 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1720 auto __idx = __first;
1721#ifndef _LIBCPP_NO_EXCEPTIONS
1722 try {
1723#endif
1724 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001725 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00001726#ifndef _LIBCPP_NO_EXCEPTIONS
1727 } catch (...) {
1728 _VSTD::destroy(__first, __idx);
1729 throw;
1730 }
1731#endif
1732}
1733
1734template <class _ForwardIterator, class _Size>
1735inline _LIBCPP_INLINE_VISIBILITY
1736_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
1737 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1738 auto __idx = __first;
1739#ifndef _LIBCPP_NO_EXCEPTIONS
1740 try {
1741#endif
1742 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001743 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00001744 return __idx;
1745#ifndef _LIBCPP_NO_EXCEPTIONS
1746 } catch (...) {
1747 _VSTD::destroy(__first, __idx);
1748 throw;
1749 }
1750#endif
1751}
1752
1753
1754template <class _ForwardIterator>
1755inline _LIBCPP_INLINE_VISIBILITY
1756void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
1757 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1758 auto __idx = __first;
1759#ifndef _LIBCPP_NO_EXCEPTIONS
1760 try {
1761#endif
1762 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001763 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00001764#ifndef _LIBCPP_NO_EXCEPTIONS
1765 } catch (...) {
1766 _VSTD::destroy(__first, __idx);
1767 throw;
1768 }
1769#endif
1770}
1771
1772template <class _ForwardIterator, class _Size>
1773inline _LIBCPP_INLINE_VISIBILITY
1774_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
1775 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1776 auto __idx = __first;
1777#ifndef _LIBCPP_NO_EXCEPTIONS
1778 try {
1779#endif
1780 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001781 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00001782 return __idx;
1783#ifndef _LIBCPP_NO_EXCEPTIONS
1784 } catch (...) {
1785 _VSTD::destroy(__first, __idx);
1786 throw;
1787 }
1788#endif
1789}
1790
1791
1792template <class _InputIt, class _ForwardIt>
1793inline _LIBCPP_INLINE_VISIBILITY
1794_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
1795 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
1796 auto __idx = __first_res;
1797#ifndef _LIBCPP_NO_EXCEPTIONS
1798 try {
1799#endif
1800 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001801 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00001802 return __idx;
1803#ifndef _LIBCPP_NO_EXCEPTIONS
1804 } catch (...) {
1805 _VSTD::destroy(__first_res, __idx);
1806 throw;
1807 }
1808#endif
1809}
1810
1811template <class _InputIt, class _Size, class _ForwardIt>
1812inline _LIBCPP_INLINE_VISIBILITY
1813pair<_InputIt, _ForwardIt>
1814uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
1815 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
1816 auto __idx = __first_res;
1817#ifndef _LIBCPP_NO_EXCEPTIONS
1818 try {
1819#endif
1820 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001821 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00001822 return {__first, __idx};
1823#ifndef _LIBCPP_NO_EXCEPTIONS
1824 } catch (...) {
1825 _VSTD::destroy(__first_res, __idx);
1826 throw;
1827 }
1828#endif
1829}
1830
1831
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001832#endif // _LIBCPP_STD_VER > 14
1833
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001834// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
1835// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -05001836// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001837#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
1838 && defined(__ATOMIC_RELAXED) \
1839 && defined(__ATOMIC_ACQ_REL)
1840# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00001841#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001842# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
1843#endif
1844
1845template <class _Tp>
1846inline _LIBCPP_INLINE_VISIBILITY _Tp
1847__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
1848{
1849#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
1850 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
1851#else
1852 return __t += 1;
1853#endif
1854}
1855
1856template <class _Tp>
1857inline _LIBCPP_INLINE_VISIBILITY _Tp
1858__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
1859{
1860#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
1861 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
1862#else
1863 return __t -= 1;
1864#endif
1865}
1866
Howard Hinnant756c69b2010-09-22 16:48:34 +00001867class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 : public std::exception
1869{
1870public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01001871 bad_weak_ptr() _NOEXCEPT = default;
1872 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00001873 virtual ~bad_weak_ptr() _NOEXCEPT;
1874 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875};
1876
Louis Dionne16fe2952018-07-11 23:14:33 +00001877_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001878void __throw_bad_weak_ptr()
1879{
1880#ifndef _LIBCPP_NO_EXCEPTIONS
1881 throw bad_weak_ptr();
1882#else
1883 _VSTD::abort();
1884#endif
1885}
1886
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001887template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001889class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
1891 __shared_count(const __shared_count&);
1892 __shared_count& operator=(const __shared_count&);
1893
1894protected:
1895 long __shared_owners_;
1896 virtual ~__shared_count();
1897private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001898 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899
1900public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001902 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 : __shared_owners_(__refs) {}
1904
Louis Dionne5e0eadd2018-08-01 02:08:59 +00001905#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00001906 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00001907 void __add_shared() _NOEXCEPT;
1908 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001909#else
1910 _LIBCPP_INLINE_VISIBILITY
1911 void __add_shared() _NOEXCEPT {
1912 __libcpp_atomic_refcount_increment(__shared_owners_);
1913 }
1914 _LIBCPP_INLINE_VISIBILITY
1915 bool __release_shared() _NOEXCEPT {
1916 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
1917 __on_zero_shared();
1918 return true;
1919 }
1920 return false;
1921 }
1922#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00001923 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00001924 long use_count() const _NOEXCEPT {
1925 return __libcpp_relaxed_load(&__shared_owners_) + 1;
1926 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927};
1928
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001929class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 : private __shared_count
1931{
1932 long __shared_weak_owners_;
1933
1934public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001936 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 : __shared_count(__refs),
1938 __shared_weak_owners_(__refs) {}
1939protected:
1940 virtual ~__shared_weak_count();
1941
1942public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00001943#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00001944 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00001945 void __add_shared() _NOEXCEPT;
1946 void __add_weak() _NOEXCEPT;
1947 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001948#else
1949 _LIBCPP_INLINE_VISIBILITY
1950 void __add_shared() _NOEXCEPT {
1951 __shared_count::__add_shared();
1952 }
1953 _LIBCPP_INLINE_VISIBILITY
1954 void __add_weak() _NOEXCEPT {
1955 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
1956 }
1957 _LIBCPP_INLINE_VISIBILITY
1958 void __release_shared() _NOEXCEPT {
1959 if (__shared_count::__release_shared())
1960 __release_weak();
1961 }
1962#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00001963 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001965 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
1966 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967
Howard Hinnant719bda32011-05-28 14:41:13 +00001968 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001970 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971};
1972
1973template <class _Tp, class _Dp, class _Alloc>
1974class __shared_ptr_pointer
1975 : public __shared_weak_count
1976{
1977 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
1978public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001981 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982
Howard Hinnant72f73582010-08-11 17:04:31 +00001983#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00001984 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00001985#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986
1987private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001988 virtual void __on_zero_shared() _NOEXCEPT;
1989 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990};
1991
Howard Hinnant72f73582010-08-11 17:04:31 +00001992#ifndef _LIBCPP_NO_RTTI
1993
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994template <class _Tp, class _Dp, class _Alloc>
1995const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00001996__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997{
Eric Fiselierc7490d02017-05-04 01:06:56 +00001998 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999}
2000
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002001#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002002
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003template <class _Tp, class _Dp, class _Alloc>
2004void
Howard Hinnant719bda32011-05-28 14:41:13 +00002005__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006{
2007 __data_.first().second()(__data_.first().first());
2008 __data_.first().second().~_Dp();
2009}
2010
2011template <class _Tp, class _Dp, class _Alloc>
2012void
Howard Hinnant719bda32011-05-28 14:41:13 +00002013__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002015 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2016 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002017 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2018
Eric Fiselierf8898c82015-02-05 23:01:40 +00002019 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002021 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022}
2023
2024template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002025struct __shared_ptr_emplace
2026 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027{
Louis Dionneda463cb2020-12-11 12:22:16 -05002028 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002029 _LIBCPP_HIDE_FROM_ABI
2030 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002031 : __storage_(_VSTD::move(__a))
2032 {
2033#if _LIBCPP_STD_VER > 17
2034 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2035 _TpAlloc __tmp(*__get_alloc());
2036 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002037#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002038 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002039#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002040 }
Louis Dionne86549d72020-12-09 16:22:17 -05002041
2042 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002043 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002044
2045 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002046 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047
2048private:
Louis Dionne86549d72020-12-09 16:22:17 -05002049 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002050#if _LIBCPP_STD_VER > 17
2051 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2052 _TpAlloc __tmp(*__get_alloc());
2053 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2054#else
Louis Dionne86549d72020-12-09 16:22:17 -05002055 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002056#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002057 }
2058
2059 virtual void __on_zero_shared_weak() _NOEXCEPT {
2060 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2061 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002062 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002063 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002064 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2065 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2066 }
2067
Louis Dionneda463cb2020-12-11 12:22:16 -05002068 // This class implements the control block for non-array shared pointers created
2069 // through `std::allocate_shared` and `std::make_shared`.
2070 //
2071 // In previous versions of the library, we used a compressed pair to store
2072 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2073 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2074 // we now use a properly aligned char buffer while making sure that we maintain
2075 // the same layout that we had when we used a compressed pair.
2076 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2077 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2078 char __blob_[sizeof(_CompressedPair)];
2079
2080 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2081 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2082 }
2083 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2084 __get_alloc()->~_Alloc();
2085 }
2086 _Alloc* __get_alloc() _NOEXCEPT {
2087 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2088 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2089 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2090 return __alloc;
2091 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08002092 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002093 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2094 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2095 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2096 return __elem;
2097 }
2098 };
2099
2100 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2101 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2102 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103};
2104
Erik Pilkington2a398762017-05-25 15:43:31 +00002105struct __shared_ptr_dummy_rebind_allocator_type;
2106template <>
2107class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2108{
2109public:
2110 template <class _Other>
2111 struct rebind
2112 {
2113 typedef allocator<_Other> other;
2114 };
2115};
2116
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002117template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118
zoecarverd73f42a2020-05-11 18:42:50 -07002119template<class _Tp, class _Up>
2120struct __compatible_with
2121#if _LIBCPP_STD_VER > 14
2122 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2123#else
2124 : is_convertible<_Tp*, _Up*> {};
2125#endif // _LIBCPP_STD_VER > 14
2126
zoecarver9bc39102021-02-19 11:10:36 -08002127template <class _Dp, class _Pt,
2128 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
2129static true_type __well_formed_deleter_test(int);
2130
2131template <class, class>
2132static false_type __well_formed_deleter_test(...);
2133
2134template <class _Dp, class _Pt>
2135struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
2136
2137template<class _Dp, class _Tp, class _Yp>
2138struct __shared_ptr_deleter_ctor_reqs
2139{
2140 static const bool value = __compatible_with<_Tp, _Yp>::value &&
2141 is_move_constructible<_Dp>::value &&
2142 __well_formed_deleter<_Dp, _Tp*>::value;
2143};
2144
Vy Nguyene369bd92020-07-13 12:34:37 -04002145#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2146# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2147#else
2148# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2149#endif
2150
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002152class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002154public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002155#if _LIBCPP_STD_VER > 14
2156 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002157 typedef remove_extent_t<_Tp> element_type;
2158#else
2159 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002160#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002161
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162private:
2163 element_type* __ptr_;
2164 __shared_weak_count* __cntrl_;
2165
2166 struct __nat {int __for_bool_;};
2167public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002169 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002171 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002172 template<class _Yp>
2173 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002174 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002175 template<class _Yp, class _Dp>
2176 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002177 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002178 template<class _Yp, class _Dp, class _Alloc>
2179 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002180 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2182 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002183 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002185 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002189 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002190 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002192 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002193 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002194 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002195 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002197 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002198#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002199 template<class _Yp>
2200 shared_ptr(auto_ptr<_Yp>&& __r,
2201 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002202#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002203 template <class _Yp, class _Dp>
2204 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2205 typename enable_if
2206 <
2207 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002208 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2209 __nat
2210 >::type = __nat());
2211 template <class _Yp, class _Dp>
2212 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2213 typename enable_if
2214 <
2215 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002216 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2217 __nat
2218 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219
2220 ~shared_ptr();
2221
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002223 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002224 template<class _Yp>
2225 typename enable_if
2226 <
zoecarverd73f42a2020-05-11 18:42:50 -07002227 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002228 shared_ptr&
2229 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002231 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002233 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002234 template<class _Yp>
2235 typename enable_if
2236 <
zoecarverd73f42a2020-05-11 18:42:50 -07002237 __compatible_with<_Yp, element_type>::value,
2238 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002239 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002241 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002242#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002243 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002245 typename enable_if
2246 <
2247 !is_array<_Yp>::value &&
2248 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002249 shared_ptr
2250 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002251 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002252#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002253 template <class _Yp, class _Dp>
2254 typename enable_if
2255 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002256 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2257 shared_ptr&
2258 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002260 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002263 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002265 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002266 template<class _Yp>
2267 typename enable_if
2268 <
zoecarverd73f42a2020-05-11 18:42:50 -07002269 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002270 void
2271 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002273 reset(_Yp* __p);
2274 template<class _Yp, class _Dp>
2275 typename enable_if
2276 <
zoecarverd73f42a2020-05-11 18:42:50 -07002277 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002278 void
2279 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002281 reset(_Yp* __p, _Dp __d);
2282 template<class _Yp, class _Dp, class _Alloc>
2283 typename enable_if
2284 <
zoecarverd73f42a2020-05-11 18:42:50 -07002285 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002286 void
2287 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002289 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290
Howard Hinnant756c69b2010-09-22 16:48:34 +00002291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002292 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002294 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2295 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002296 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002297 element_type* operator->() const _NOEXCEPT
2298 {
2299 static_assert(!_VSTD::is_array<_Tp>::value,
2300 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2301 return __ptr_;
2302 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002304 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002306 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002307 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002308 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002309 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002310 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002311 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002313 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002314 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002315 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002317 _LIBCPP_INLINE_VISIBILITY
2318 bool
2319 __owner_equivalent(const shared_ptr& __p) const
2320 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321
zoecarverd73f42a2020-05-11 18:42:50 -07002322#if _LIBCPP_STD_VER > 14
2323 typename add_lvalue_reference<element_type>::type
2324 _LIBCPP_INLINE_VISIBILITY
2325 operator[](ptrdiff_t __i) const
2326 {
2327 static_assert(_VSTD::is_array<_Tp>::value,
2328 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2329 return __ptr_[__i];
2330 }
2331#endif
2332
Howard Hinnant72f73582010-08-11 17:04:31 +00002333#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002336 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002337 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002338 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002339 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002340#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341
Zoe Carverd9040c72019-10-22 15:16:49 +00002342 template<class _Yp, class _CntrlBlk>
2343 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002344 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002345 {
2346 shared_ptr<_Tp> __r;
2347 __r.__ptr_ = __p;
2348 __r.__cntrl_ = __cntrl;
2349 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2350 return __r;
2351 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002352
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002354 template <class _Yp, bool = is_function<_Yp>::value>
2355 struct __shared_ptr_default_allocator
2356 {
2357 typedef allocator<_Yp> type;
2358 };
2359
2360 template <class _Yp>
2361 struct __shared_ptr_default_allocator<_Yp, true>
2362 {
2363 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2364 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002366 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002367 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002368 typename enable_if<is_convertible<_OrigPtr*,
2369 const enable_shared_from_this<_Yp>*
2370 >::value,
2371 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002372 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2373 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002375 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002376 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002377 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002378 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2379 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002380 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 }
2382
Erik Pilkington2a398762017-05-25 15:43:31 +00002383 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002384
zoecarverd73f42a2020-05-11 18:42:50 -07002385 template <class, class _Yp>
2386 struct __shared_ptr_default_delete
2387 : default_delete<_Yp> {};
2388
2389 template <class _Yp, class _Un, size_t _Sz>
2390 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2391 : default_delete<_Yp[]> {};
2392
2393 template <class _Yp, class _Un>
2394 struct __shared_ptr_default_delete<_Yp[], _Un>
2395 : default_delete<_Yp[]> {};
2396
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002397 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2398 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399};
2400
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002401#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2402template<class _Tp>
2403shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2404template<class _Tp, class _Dp>
2405shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2406#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002407
Howard Hinnantc51e1022010-05-11 19:42:16 +00002408template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002409inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002410_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002411shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002412 : __ptr_(nullptr),
2413 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414{
2415}
2416
2417template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002418inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002419_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002420shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002421 : __ptr_(nullptr),
2422 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423{
2424}
2425
2426template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002427template<class _Yp>
2428shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002429 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430 : __ptr_(__p)
2431{
2432 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002433 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002434 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2435 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002437 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438}
2439
2440template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002441template<class _Yp, class _Dp>
2442shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002443 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 : __ptr_(__p)
2445{
2446#ifndef _LIBCPP_NO_EXCEPTIONS
2447 try
2448 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002449#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002450 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2451 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002452#ifndef _LIBCPP_CXX03_LANG
2453 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2454#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002455 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002456#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002457 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458#ifndef _LIBCPP_NO_EXCEPTIONS
2459 }
2460 catch (...)
2461 {
2462 __d(__p);
2463 throw;
2464 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002465#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466}
2467
2468template<class _Tp>
2469template<class _Dp>
2470shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002471 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472{
2473#ifndef _LIBCPP_NO_EXCEPTIONS
2474 try
2475 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002476#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002477 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2478 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002479#ifndef _LIBCPP_CXX03_LANG
2480 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2481#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002482 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002483#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484#ifndef _LIBCPP_NO_EXCEPTIONS
2485 }
2486 catch (...)
2487 {
2488 __d(__p);
2489 throw;
2490 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002491#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492}
2493
2494template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002495template<class _Yp, class _Dp, class _Alloc>
2496shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002497 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 : __ptr_(__p)
2499{
2500#ifndef _LIBCPP_NO_EXCEPTIONS
2501 try
2502 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002503#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002505 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506 typedef __allocator_destructor<_A2> _D2;
2507 _A2 __a2(__a);
2508 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002509 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2510#ifndef _LIBCPP_CXX03_LANG
2511 _CntrlBlk(__p, _VSTD::move(__d), __a);
2512#else
2513 _CntrlBlk(__p, __d, __a);
2514#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002515 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002516 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517#ifndef _LIBCPP_NO_EXCEPTIONS
2518 }
2519 catch (...)
2520 {
2521 __d(__p);
2522 throw;
2523 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002524#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525}
2526
2527template<class _Tp>
2528template<class _Dp, class _Alloc>
2529shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002530 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531{
2532#ifndef _LIBCPP_NO_EXCEPTIONS
2533 try
2534 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002535#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002537 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538 typedef __allocator_destructor<_A2> _D2;
2539 _A2 __a2(__a);
2540 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002541 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2542#ifndef _LIBCPP_CXX03_LANG
2543 _CntrlBlk(__p, _VSTD::move(__d), __a);
2544#else
2545 _CntrlBlk(__p, __d, __a);
2546#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002547 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548#ifndef _LIBCPP_NO_EXCEPTIONS
2549 }
2550 catch (...)
2551 {
2552 __d(__p);
2553 throw;
2554 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002555#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556}
2557
2558template<class _Tp>
2559template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002560inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002561shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562 : __ptr_(__p),
2563 __cntrl_(__r.__cntrl_)
2564{
2565 if (__cntrl_)
2566 __cntrl_->__add_shared();
2567}
2568
2569template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002570inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002571shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572 : __ptr_(__r.__ptr_),
2573 __cntrl_(__r.__cntrl_)
2574{
2575 if (__cntrl_)
2576 __cntrl_->__add_shared();
2577}
2578
2579template<class _Tp>
2580template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002581inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002583 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002584 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585 : __ptr_(__r.__ptr_),
2586 __cntrl_(__r.__cntrl_)
2587{
2588 if (__cntrl_)
2589 __cntrl_->__add_shared();
2590}
2591
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002593inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002594shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595 : __ptr_(__r.__ptr_),
2596 __cntrl_(__r.__cntrl_)
2597{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002598 __r.__ptr_ = nullptr;
2599 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600}
2601
2602template<class _Tp>
2603template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002604inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002606 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002607 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608 : __ptr_(__r.__ptr_),
2609 __cntrl_(__r.__cntrl_)
2610{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002611 __r.__ptr_ = nullptr;
2612 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613}
2614
Marshall Clowb22274f2017-01-24 22:22:33 +00002615#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002617template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002618shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002619 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620 : __ptr_(__r.get())
2621{
2622 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2623 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002624 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625 __r.release();
2626}
Marshall Clowb22274f2017-01-24 22:22:33 +00002627#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628
2629template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002630template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002632 typename enable_if
2633 <
2634 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002635 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2636 __nat
2637 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638 : __ptr_(__r.get())
2639{
Marshall Clow35cde742015-05-10 13:59:45 +00002640#if _LIBCPP_STD_VER > 11
2641 if (__ptr_ == nullptr)
2642 __cntrl_ = nullptr;
2643 else
2644#endif
2645 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002646 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002647 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
Erik Pilkington2a398762017-05-25 15:43:31 +00002648 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002649 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002650 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 __r.release();
2652}
2653
2654template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002655template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002657 typename enable_if
2658 <
2659 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002660 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2661 __nat
2662 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 : __ptr_(__r.get())
2664{
Marshall Clow35cde742015-05-10 13:59:45 +00002665#if _LIBCPP_STD_VER > 11
2666 if (__ptr_ == nullptr)
2667 __cntrl_ = nullptr;
2668 else
2669#endif
2670 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002671 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002672 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow35cde742015-05-10 13:59:45 +00002673 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00002674 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05002675 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002676 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002677 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 __r.release();
2679}
2680
Zoe Carver6cd05c32019-08-19 15:47:16 +00002681template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682shared_ptr<_Tp>::~shared_ptr()
2683{
2684 if (__cntrl_)
2685 __cntrl_->__release_shared();
2686}
2687
2688template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002691shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692{
2693 shared_ptr(__r).swap(*this);
2694 return *this;
2695}
2696
2697template<class _Tp>
2698template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002699inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002700typename enable_if
2701<
zoecarverd73f42a2020-05-11 18:42:50 -07002702 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002703 shared_ptr<_Tp>&
2704>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002705shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706{
2707 shared_ptr(__r).swap(*this);
2708 return *this;
2709}
2710
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002712inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002714shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002716 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 return *this;
2718}
2719
2720template<class _Tp>
2721template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002722inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002723typename enable_if
2724<
zoecarverd73f42a2020-05-11 18:42:50 -07002725 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002726 shared_ptr<_Tp>&
2727>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
2729{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002730 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731 return *this;
2732}
2733
Marshall Clowb22274f2017-01-24 22:22:33 +00002734#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735template<class _Tp>
2736template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002737inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002738typename enable_if
2739<
2740 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00002741 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002742 shared_ptr<_Tp>
2743>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
2745{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002746 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 return *this;
2748}
Marshall Clowb22274f2017-01-24 22:22:33 +00002749#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750
2751template<class _Tp>
2752template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002753inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002754typename enable_if
2755<
Aditya Kumar7c5db692017-08-20 10:38:55 +00002756 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00002757 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002758 shared_ptr<_Tp>&
2759>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
2761{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002762 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 return *this;
2764}
2765
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002767inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768void
Howard Hinnant719bda32011-05-28 14:41:13 +00002769shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002771 _VSTD::swap(__ptr_, __r.__ptr_);
2772 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773}
2774
2775template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002776inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777void
Howard Hinnant719bda32011-05-28 14:41:13 +00002778shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779{
2780 shared_ptr().swap(*this);
2781}
2782
2783template<class _Tp>
2784template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002785inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002786typename enable_if
2787<
zoecarverd73f42a2020-05-11 18:42:50 -07002788 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002789 void
2790>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791shared_ptr<_Tp>::reset(_Yp* __p)
2792{
2793 shared_ptr(__p).swap(*this);
2794}
2795
2796template<class _Tp>
2797template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002798inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002799typename enable_if
2800<
zoecarverd73f42a2020-05-11 18:42:50 -07002801 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002802 void
2803>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
2805{
2806 shared_ptr(__p, __d).swap(*this);
2807}
2808
2809template<class _Tp>
2810template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002811inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002812typename enable_if
2813<
zoecarverd73f42a2020-05-11 18:42:50 -07002814 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002815 void
2816>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
2818{
2819 shared_ptr(__p, __d, __a).swap(*this);
2820}
2821
Louis Dionne74aef9f2020-12-11 12:20:06 -05002822//
2823// std::allocate_shared and std::make_shared
2824//
2825template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
2826_LIBCPP_HIDE_FROM_ABI
2827shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828{
Louis Dionne74aef9f2020-12-11 12:20:06 -05002829 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
2830 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
2831 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
2832 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
2833 auto __control_block = __guard.__release_ptr();
2834 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835}
2836
Louis Dionne43c9f8f2020-12-09 16:57:28 -05002837template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
2838_LIBCPP_HIDE_FROM_ABI
2839shared_ptr<_Tp> make_shared(_Args&& ...__args)
2840{
2841 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
2842}
2843
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844template<class _Tp, class _Up>
2845inline _LIBCPP_INLINE_VISIBILITY
2846bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002847operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848{
2849 return __x.get() == __y.get();
2850}
2851
2852template<class _Tp, class _Up>
2853inline _LIBCPP_INLINE_VISIBILITY
2854bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002855operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856{
2857 return !(__x == __y);
2858}
2859
2860template<class _Tp, class _Up>
2861inline _LIBCPP_INLINE_VISIBILITY
2862bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002863operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00002865#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00002866 typedef typename common_type<_Tp*, _Up*>::type _Vp;
2867 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00002868#else
2869 return less<>()(__x.get(), __y.get());
2870#endif
2871
Howard Hinnantb17caf92012-02-21 21:02:58 +00002872}
2873
2874template<class _Tp, class _Up>
2875inline _LIBCPP_INLINE_VISIBILITY
2876bool
2877operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2878{
2879 return __y < __x;
2880}
2881
2882template<class _Tp, class _Up>
2883inline _LIBCPP_INLINE_VISIBILITY
2884bool
2885operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2886{
2887 return !(__y < __x);
2888}
2889
2890template<class _Tp, class _Up>
2891inline _LIBCPP_INLINE_VISIBILITY
2892bool
2893operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2894{
2895 return !(__x < __y);
2896}
2897
2898template<class _Tp>
2899inline _LIBCPP_INLINE_VISIBILITY
2900bool
2901operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2902{
2903 return !__x;
2904}
2905
2906template<class _Tp>
2907inline _LIBCPP_INLINE_VISIBILITY
2908bool
2909operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2910{
2911 return !__x;
2912}
2913
2914template<class _Tp>
2915inline _LIBCPP_INLINE_VISIBILITY
2916bool
2917operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2918{
2919 return static_cast<bool>(__x);
2920}
2921
2922template<class _Tp>
2923inline _LIBCPP_INLINE_VISIBILITY
2924bool
2925operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2926{
2927 return static_cast<bool>(__x);
2928}
2929
2930template<class _Tp>
2931inline _LIBCPP_INLINE_VISIBILITY
2932bool
2933operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2934{
2935 return less<_Tp*>()(__x.get(), nullptr);
2936}
2937
2938template<class _Tp>
2939inline _LIBCPP_INLINE_VISIBILITY
2940bool
2941operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2942{
2943 return less<_Tp*>()(nullptr, __x.get());
2944}
2945
2946template<class _Tp>
2947inline _LIBCPP_INLINE_VISIBILITY
2948bool
2949operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2950{
2951 return nullptr < __x;
2952}
2953
2954template<class _Tp>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
2957operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2958{
2959 return __x < nullptr;
2960}
2961
2962template<class _Tp>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2966{
2967 return !(nullptr < __x);
2968}
2969
2970template<class _Tp>
2971inline _LIBCPP_INLINE_VISIBILITY
2972bool
2973operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2974{
2975 return !(__x < nullptr);
2976}
2977
2978template<class _Tp>
2979inline _LIBCPP_INLINE_VISIBILITY
2980bool
2981operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2982{
2983 return !(__x < nullptr);
2984}
2985
2986template<class _Tp>
2987inline _LIBCPP_INLINE_VISIBILITY
2988bool
2989operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2990{
2991 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992}
2993
2994template<class _Tp>
2995inline _LIBCPP_INLINE_VISIBILITY
2996void
Howard Hinnant719bda32011-05-28 14:41:13 +00002997swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998{
2999 __x.swap(__y);
3000}
3001
3002template<class _Tp, class _Up>
3003inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003004shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003005static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006{
zoecarverd73f42a2020-05-11 18:42:50 -07003007 return shared_ptr<_Tp>(__r,
3008 static_cast<
3009 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010}
3011
3012template<class _Tp, class _Up>
3013inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003014shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003015dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016{
zoecarverd73f42a2020-05-11 18:42:50 -07003017 typedef typename shared_ptr<_Tp>::element_type _ET;
3018 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3020}
3021
3022template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003023shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003024const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025{
zoecarverd73f42a2020-05-11 18:42:50 -07003026 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003027 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028}
3029
zoecarverd73f42a2020-05-11 18:42:50 -07003030template<class _Tp, class _Up>
3031shared_ptr<_Tp>
3032reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3033{
3034 return shared_ptr<_Tp>(__r,
3035 reinterpret_cast<
3036 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3037}
3038
Howard Hinnant72f73582010-08-11 17:04:31 +00003039#ifndef _LIBCPP_NO_RTTI
3040
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041template<class _Dp, class _Tp>
3042inline _LIBCPP_INLINE_VISIBILITY
3043_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003044get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045{
3046 return __p.template __get_deleter<_Dp>();
3047}
3048
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003049#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003050
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003052class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003054public:
3055 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056private:
3057 element_type* __ptr_;
3058 __shared_weak_count* __cntrl_;
3059
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003060public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003062 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003063 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003064 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3065 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003067 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003068 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003069 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3070 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003071
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003073 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003074 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003075 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3076 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003077 ~weak_ptr();
3078
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003079 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003080 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003081 template<class _Yp>
3082 typename enable_if
3083 <
3084 is_convertible<_Yp*, element_type*>::value,
3085 weak_ptr&
3086 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003088 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3089
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003091 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3092 template<class _Yp>
3093 typename enable_if
3094 <
3095 is_convertible<_Yp*, element_type*>::value,
3096 weak_ptr&
3097 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003099 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3100
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003101 template<class _Yp>
3102 typename enable_if
3103 <
3104 is_convertible<_Yp*, element_type*>::value,
3105 weak_ptr&
3106 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003108 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003109
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003111 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003113 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003114
Howard Hinnant756c69b2010-09-22 16:48:34 +00003115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003116 long use_count() const _NOEXCEPT
3117 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003119 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003120 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003121 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003122 template<class _Up>
3123 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003124 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003126 template<class _Up>
3127 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003128 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129 {return __cntrl_ < __r.__cntrl_;}
3130
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003131 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3132 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133};
3134
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003135#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3136template<class _Tp>
3137weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3138#endif
3139
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003141inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003142_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003143weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003144 : __ptr_(nullptr),
3145 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146{
3147}
3148
3149template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003150inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003151weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 : __ptr_(__r.__ptr_),
3153 __cntrl_(__r.__cntrl_)
3154{
3155 if (__cntrl_)
3156 __cntrl_->__add_weak();
3157}
3158
3159template<class _Tp>
3160template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003161inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003163 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003164 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 : __ptr_(__r.__ptr_),
3166 __cntrl_(__r.__cntrl_)
3167{
3168 if (__cntrl_)
3169 __cntrl_->__add_weak();
3170}
3171
3172template<class _Tp>
3173template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003174inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003176 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003177 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 : __ptr_(__r.__ptr_),
3179 __cntrl_(__r.__cntrl_)
3180{
3181 if (__cntrl_)
3182 __cntrl_->__add_weak();
3183}
3184
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003185template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003186inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003187weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3188 : __ptr_(__r.__ptr_),
3189 __cntrl_(__r.__cntrl_)
3190{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003191 __r.__ptr_ = nullptr;
3192 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003193}
3194
3195template<class _Tp>
3196template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003197inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003198weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3199 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3200 _NOEXCEPT
3201 : __ptr_(__r.__ptr_),
3202 __cntrl_(__r.__cntrl_)
3203{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003204 __r.__ptr_ = nullptr;
3205 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003206}
3207
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208template<class _Tp>
3209weak_ptr<_Tp>::~weak_ptr()
3210{
3211 if (__cntrl_)
3212 __cntrl_->__release_weak();
3213}
3214
3215template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003216inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003218weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219{
3220 weak_ptr(__r).swap(*this);
3221 return *this;
3222}
3223
3224template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003225template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003226inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003227typename enable_if
3228<
3229 is_convertible<_Yp*, _Tp*>::value,
3230 weak_ptr<_Tp>&
3231>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003232weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233{
3234 weak_ptr(__r).swap(*this);
3235 return *this;
3236}
3237
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003238template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003239inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003240weak_ptr<_Tp>&
3241weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3242{
3243 weak_ptr(_VSTD::move(__r)).swap(*this);
3244 return *this;
3245}
3246
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003248template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003249inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003250typename enable_if
3251<
3252 is_convertible<_Yp*, _Tp*>::value,
3253 weak_ptr<_Tp>&
3254>::type
3255weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3256{
3257 weak_ptr(_VSTD::move(__r)).swap(*this);
3258 return *this;
3259}
3260
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003261template<class _Tp>
3262template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003263inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003264typename enable_if
3265<
3266 is_convertible<_Yp*, _Tp*>::value,
3267 weak_ptr<_Tp>&
3268>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003269weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270{
3271 weak_ptr(__r).swap(*this);
3272 return *this;
3273}
3274
3275template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003276inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277void
Howard Hinnant719bda32011-05-28 14:41:13 +00003278weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003280 _VSTD::swap(__ptr_, __r.__ptr_);
3281 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282}
3283
3284template<class _Tp>
3285inline _LIBCPP_INLINE_VISIBILITY
3286void
Howard Hinnant719bda32011-05-28 14:41:13 +00003287swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288{
3289 __x.swap(__y);
3290}
3291
3292template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003293inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294void
Howard Hinnant719bda32011-05-28 14:41:13 +00003295weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296{
3297 weak_ptr().swap(*this);
3298}
3299
3300template<class _Tp>
3301template<class _Yp>
3302shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003303 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304 : __ptr_(__r.__ptr_),
3305 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3306{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003307 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003308 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309}
3310
3311template<class _Tp>
3312shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003313weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314{
3315 shared_ptr<_Tp> __r;
3316 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3317 if (__r.__cntrl_)
3318 __r.__ptr_ = __ptr_;
3319 return __r;
3320}
3321
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003322#if _LIBCPP_STD_VER > 14
3323template <class _Tp = void> struct owner_less;
3324#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003325template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003326#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327
3328template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003329struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003331{
3332 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003333 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003334 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003336 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003337 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003339 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003340 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003342};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343
3344template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003345struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3347{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003348 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003349 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003350 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003352 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003353 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003355 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003356 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357 {return __x.owner_before(__y);}
3358};
3359
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003360#if _LIBCPP_STD_VER > 14
3361template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003362struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003363{
3364 template <class _Tp, class _Up>
3365 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003366 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003367 {return __x.owner_before(__y);}
3368 template <class _Tp, class _Up>
3369 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003370 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003371 {return __x.owner_before(__y);}
3372 template <class _Tp, class _Up>
3373 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003374 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003375 {return __x.owner_before(__y);}
3376 template <class _Tp, class _Up>
3377 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003378 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003379 {return __x.owner_before(__y);}
3380 typedef void is_transparent;
3381};
3382#endif
3383
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003385class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386{
3387 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003388protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003389 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003390 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003392 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003394 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3395 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003398public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003400 shared_ptr<_Tp> shared_from_this()
3401 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003403 shared_ptr<_Tp const> shared_from_this() const
3404 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405
Eric Fiselier84006862016-06-02 00:15:35 +00003406#if _LIBCPP_STD_VER > 14
3407 _LIBCPP_INLINE_VISIBILITY
3408 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3409 { return __weak_this_; }
3410
3411 _LIBCPP_INLINE_VISIBILITY
3412 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3413 { return __weak_this_; }
3414#endif // _LIBCPP_STD_VER > 14
3415
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416 template <class _Up> friend class shared_ptr;
3417};
3418
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003419template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003420struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003421{
3422 typedef shared_ptr<_Tp> argument_type;
3423 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003424
Howard Hinnant756c69b2010-09-22 16:48:34 +00003425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003426 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003427 {
zoecarverd73f42a2020-05-11 18:42:50 -07003428 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003429 }
3430};
3431
Howard Hinnantc834c512011-11-29 18:15:50 +00003432template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003433inline _LIBCPP_INLINE_VISIBILITY
3434basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003435operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003436
Eric Fiselier9b492672016-06-18 02:12:53 +00003437
3438#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003439
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003440class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003441{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003442 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003443public:
3444 void lock() _NOEXCEPT;
3445 void unlock() _NOEXCEPT;
3446
3447private:
3448 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3449 __sp_mut(const __sp_mut&);
3450 __sp_mut& operator=(const __sp_mut&);
3451
Howard Hinnant8331b762013-03-06 23:30:19 +00003452 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003453};
3454
Mehdi Amini228053d2017-05-04 17:08:54 +00003455_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3456__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003457
3458template <class _Tp>
3459inline _LIBCPP_INLINE_VISIBILITY
3460bool
3461atomic_is_lock_free(const shared_ptr<_Tp>*)
3462{
3463 return false;
3464}
3465
3466template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003467_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003468shared_ptr<_Tp>
3469atomic_load(const shared_ptr<_Tp>* __p)
3470{
3471 __sp_mut& __m = __get_sp_mut(__p);
3472 __m.lock();
3473 shared_ptr<_Tp> __q = *__p;
3474 __m.unlock();
3475 return __q;
3476}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003477
Howard Hinnant9fa30202012-07-30 01:40:57 +00003478template <class _Tp>
3479inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003480_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003481shared_ptr<_Tp>
3482atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3483{
3484 return atomic_load(__p);
3485}
3486
3487template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003488_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003489void
3490atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3491{
3492 __sp_mut& __m = __get_sp_mut(__p);
3493 __m.lock();
3494 __p->swap(__r);
3495 __m.unlock();
3496}
3497
3498template <class _Tp>
3499inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003500_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003501void
3502atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3503{
3504 atomic_store(__p, __r);
3505}
3506
3507template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003508_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003509shared_ptr<_Tp>
3510atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3511{
3512 __sp_mut& __m = __get_sp_mut(__p);
3513 __m.lock();
3514 __p->swap(__r);
3515 __m.unlock();
3516 return __r;
3517}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003518
Howard Hinnant9fa30202012-07-30 01:40:57 +00003519template <class _Tp>
3520inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003521_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003522shared_ptr<_Tp>
3523atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3524{
3525 return atomic_exchange(__p, __r);
3526}
3527
3528template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003529_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003530bool
3531atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3532{
Marshall Clow4201ee82016-05-18 17:50:13 +00003533 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003534 __sp_mut& __m = __get_sp_mut(__p);
3535 __m.lock();
3536 if (__p->__owner_equivalent(*__v))
3537 {
Marshall Clow4201ee82016-05-18 17:50:13 +00003538 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003539 *__p = __w;
3540 __m.unlock();
3541 return true;
3542 }
Marshall Clow4201ee82016-05-18 17:50:13 +00003543 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003544 *__v = *__p;
3545 __m.unlock();
3546 return false;
3547}
3548
3549template <class _Tp>
3550inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003551_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003552bool
3553atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3554{
3555 return atomic_compare_exchange_strong(__p, __v, __w);
3556}
3557
3558template <class _Tp>
3559inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003560_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003561bool
3562atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3563 shared_ptr<_Tp> __w, memory_order, memory_order)
3564{
3565 return atomic_compare_exchange_strong(__p, __v, __w);
3566}
3567
3568template <class _Tp>
3569inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003570_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003571bool
3572atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3573 shared_ptr<_Tp> __w, memory_order, memory_order)
3574{
3575 return atomic_compare_exchange_weak(__p, __v, __w);
3576}
3577
Eric Fiselier9b492672016-06-18 02:12:53 +00003578#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003579
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003580//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00003581#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
3582# ifndef _LIBCPP_CXX03_LANG
3583enum class pointer_safety : unsigned char {
3584 relaxed,
3585 preferred,
3586 strict
3587};
3588# endif
3589#else
Howard Hinnant8331b762013-03-06 23:30:19 +00003590struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003592 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593 {
3594 relaxed,
3595 preferred,
3596 strict
3597 };
3598
Howard Hinnant49e145e2012-10-30 19:06:59 +00003599 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600
Howard Hinnant756c69b2010-09-22 16:48:34 +00003601 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00003602 pointer_safety() : __v_() {}
3603
3604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00003605 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607 operator int() const {return __v_;}
3608};
Eric Fiselierab6bb302017-01-05 01:15:42 +00003609#endif
3610
3611#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003612 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00003613_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
3614#else
3615// This function is only offered in C++03 under ABI v1.
3616# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
3617inline _LIBCPP_INLINE_VISIBILITY
3618pointer_safety get_pointer_safety() _NOEXCEPT {
3619 return pointer_safety::relaxed;
3620}
3621# endif
3622#endif
3623
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003625_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
3626_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
3627_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003628_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629
3630template <class _Tp>
3631inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003632_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633undeclare_reachable(_Tp* __p)
3634{
3635 return static_cast<_Tp*>(__undeclare_reachable(__p));
3636}
3637
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003638_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639
Marshall Clow8982dcd2015-07-13 20:04:56 +00003640// --- Helper for container swap --
3641template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00003642_LIBCPP_INLINE_VISIBILITY
3643void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
3644#if _LIBCPP_STD_VER >= 14
3645 _NOEXCEPT
3646#else
3647 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3648#endif
3649{
3650 using _VSTD::swap;
3651 swap(__a1, __a2);
3652}
3653
3654template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00003655inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00003656void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
3657
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003658template <typename _Alloc>
3659inline _LIBCPP_INLINE_VISIBILITY
3660void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
3661#if _LIBCPP_STD_VER >= 14
3662 _NOEXCEPT
3663#else
3664 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3665#endif
3666{
3667 _VSTD::__swap_allocator(__a1, __a2,
3668 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
3669}
3670
Marshall Clowff91de82015-08-18 19:51:37 +00003671template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00003672struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00003673 _Traits::propagate_on_container_move_assignment::value
3674#if _LIBCPP_STD_VER > 14
3675 || _Traits::is_always_equal::value
3676#else
3677 && is_nothrow_move_assignable<_Alloc>::value
3678#endif
3679 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00003680
Marshall Clowa591b9a2016-07-11 21:38:08 +00003681
Marshall Clowa591b9a2016-07-11 21:38:08 +00003682template <class _Tp, class _Alloc>
3683struct __temp_value {
3684 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00003685
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00003686 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00003687 _Alloc &__a;
3688
3689 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
3690 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003691
Marshall Clowa591b9a2016-07-11 21:38:08 +00003692 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00003693 _LIBCPP_NO_CFI
3694 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
3695 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
3696 _VSTD::forward<_Args>(__args)...);
3697 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003698
Marshall Clowa591b9a2016-07-11 21:38:08 +00003699 ~__temp_value() { _Traits::destroy(__a, __addr()); }
3700 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00003701
Marshall Clowe46031a2018-07-02 18:41:15 +00003702template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00003703struct __is_allocator : false_type {};
3704
3705template<typename _Alloc>
3706struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00003707 typename __void_t<typename _Alloc::value_type>::type,
3708 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
3709 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00003710 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00003711
Eric Fiselier74ebee62019-06-08 01:31:19 +00003712// __builtin_new_allocator -- A non-templated helper for allocating and
3713// deallocating memory using __builtin_operator_new and
3714// __builtin_operator_delete. It should be used in preference to
3715// `std::allocator<T>` to avoid additional instantiations.
3716struct __builtin_new_allocator {
3717 struct __builtin_new_deleter {
3718 typedef void* pointer_type;
3719
3720 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
3721 : __size_(__size), __align_(__align) {}
3722
3723 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003724 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003725 }
3726
3727 private:
3728 size_t __size_;
3729 size_t __align_;
3730 };
3731
3732 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
3733
3734 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003735 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00003736 __builtin_new_deleter(__s, __align));
3737 }
3738
3739 static void __deallocate_bytes(void* __p, size_t __s,
3740 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003741 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003742 }
3743
3744 template <class _Tp>
3745 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3746 static __holder_t __allocate_type(size_t __n) {
3747 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3748 }
3749
3750 template <class _Tp>
3751 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3752 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
3753 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3754 }
3755};
3756
3757
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758_LIBCPP_END_NAMESPACE_STD
3759
Eric Fiselierf4433a32017-05-31 22:07:49 +00003760_LIBCPP_POP_MACROS
3761
Louis Dionne59d0b3c2019-08-05 18:29:14 +00003762#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00003763# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00003764#endif
3765
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766#endif // _LIBCPP_MEMORY