blob: 609439e0687d630447e0f63d0b53bd230d3b02eb [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 Dionnec7ef68c2021-04-09 14:47:46 -0400690#include <__memory/uninitialized_algorithms.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500691#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000692#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000693# include <atomic>
694#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000695#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000696
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000697#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000699#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700
Eric Fiselierf4433a32017-05-31 22:07:49 +0000701_LIBCPP_PUSH_MACROS
702#include <__undef_macros>
703
704
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705_LIBCPP_BEGIN_NAMESPACE_STD
706
Eric Fiselier89659d12015-07-07 00:27:16 +0000707template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000708inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000709_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
710#if !defined(_LIBCPP_HAS_NO_THREADS) && \
711 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000712 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000713 return __atomic_load_n(__value, __ATOMIC_RELAXED);
714#else
715 return *__value;
716#endif
717}
718
Kuba Breckade9d6792016-09-04 09:55:12 +0000719template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000720inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000721_ValueType __libcpp_acquire_load(_ValueType const* __value) {
722#if !defined(_LIBCPP_HAS_NO_THREADS) && \
723 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000724 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000725 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
726#else
727 return *__value;
728#endif
729}
730
Louis Dionned6651542020-11-03 12:05:55 -0500731template <class _Alloc, class _Ptr>
732_LIBCPP_INLINE_VISIBILITY
733void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
734 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500735 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500736 typedef allocator_traits<_Alloc> _Traits;
737 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
738 _Traits::construct(__a, _VSTD::__to_address(__begin2),
739#ifdef _LIBCPP_NO_EXCEPTIONS
740 _VSTD::move(*__begin1)
741#else
742 _VSTD::move_if_noexcept(*__begin1)
743#endif
744 );
745 }
746}
747
748template <class _Alloc, class _Tp, typename enable_if<
749 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
750 is_trivially_move_constructible<_Tp>::value
751>::type>
752_LIBCPP_INLINE_VISIBILITY
753void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
754 ptrdiff_t _Np = __end1 - __begin1;
755 if (_Np > 0) {
756 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
757 __begin2 += _Np;
758 }
759}
760
761template <class _Alloc, class _Iter, class _Ptr>
762_LIBCPP_INLINE_VISIBILITY
763void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
764 typedef allocator_traits<_Alloc> _Traits;
765 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
766 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
767 }
768}
769
770template <class _Alloc, class _Source, class _Dest,
771 class _RawSource = typename remove_const<_Source>::type,
772 class _RawDest = typename remove_const<_Dest>::type,
773 class =
774 typename enable_if<
775 is_trivially_copy_constructible<_Dest>::value &&
776 is_same<_RawSource, _RawDest>::value &&
777 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
778 >::type>
779_LIBCPP_INLINE_VISIBILITY
780void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
781 ptrdiff_t _Np = __end1 - __begin1;
782 if (_Np > 0) {
783 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
784 __begin2 += _Np;
785 }
786}
787
788template <class _Alloc, class _Ptr>
789_LIBCPP_INLINE_VISIBILITY
790void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
791 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
792 "The specified type does not meet the requirements of Cpp17MoveInsertable");
793 typedef allocator_traits<_Alloc> _Traits;
794 while (__end1 != __begin1) {
795 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
796#ifdef _LIBCPP_NO_EXCEPTIONS
797 _VSTD::move(*--__end1)
798#else
799 _VSTD::move_if_noexcept(*--__end1)
800#endif
801 );
802 --__end2;
803 }
804}
805
806template <class _Alloc, class _Tp, class = typename enable_if<
807 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
808 is_trivially_move_constructible<_Tp>::value
809>::type>
810_LIBCPP_INLINE_VISIBILITY
811void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
812 ptrdiff_t _Np = __end1 - __begin1;
813 __end2 -= _Np;
814 if (_Np > 0)
815 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
816}
817
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000818// default_delete
819
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +0000821struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +0000822 static_assert(!is_function<_Tp>::value,
823 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +0000824#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000825 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000826#else
Eric Fiselier6779b062017-04-16 02:06:25 +0000827 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000828#endif
Eric Fiselier6779b062017-04-16 02:06:25 +0000829 template <class _Up>
830 _LIBCPP_INLINE_VISIBILITY
831 default_delete(const default_delete<_Up>&,
832 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
833 0) _NOEXCEPT {}
834
835 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
836 static_assert(sizeof(_Tp) > 0,
837 "default_delete can not delete incomplete type");
838 static_assert(!is_void<_Tp>::value,
839 "default_delete can not delete incomplete type");
840 delete __ptr;
841 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842};
843
844template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +0000845struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
846private:
847 template <class _Up>
848 struct _EnableIfConvertible
849 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
850
Howard Hinnant4500ca52011-12-18 21:19:44 +0000851public:
Eric Fiselier6779b062017-04-16 02:06:25 +0000852#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000853 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000854#else
Eric Fiselier6779b062017-04-16 02:06:25 +0000855 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000856#endif
Eric Fiselier6779b062017-04-16 02:06:25 +0000857
858 template <class _Up>
859 _LIBCPP_INLINE_VISIBILITY
860 default_delete(const default_delete<_Up[]>&,
861 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
862
863 template <class _Up>
864 _LIBCPP_INLINE_VISIBILITY
865 typename _EnableIfConvertible<_Up>::type
866 operator()(_Up* __ptr) const _NOEXCEPT {
867 static_assert(sizeof(_Tp) > 0,
868 "default_delete can not delete incomplete type");
869 static_assert(!is_void<_Tp>::value,
870 "default_delete can not delete void type");
871 delete[] __ptr;
872 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873};
874
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000875template <class _Deleter>
876struct __unique_ptr_deleter_sfinae {
877 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
878 typedef const _Deleter& __lval_ref_type;
879 typedef _Deleter&& __good_rval_ref_type;
880 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881};
882
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000883template <class _Deleter>
884struct __unique_ptr_deleter_sfinae<_Deleter const&> {
885 typedef const _Deleter& __lval_ref_type;
886 typedef const _Deleter&& __bad_rval_ref_type;
887 typedef false_type __enable_rval_overload;
888};
889
890template <class _Deleter>
891struct __unique_ptr_deleter_sfinae<_Deleter&> {
892 typedef _Deleter& __lval_ref_type;
893 typedef _Deleter&& __bad_rval_ref_type;
894 typedef false_type __enable_rval_overload;
895};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000896
Vy Nguyene369bd92020-07-13 12:34:37 -0400897#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
898# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
899#else
900# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
901#endif
902
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000903template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -0400904class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000905public:
906 typedef _Tp element_type;
907 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -0500908 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000909
910 static_assert(!is_rvalue_reference<deleter_type>::value,
911 "the specified deleter type cannot be an rvalue reference");
912
913private:
914 __compressed_pair<pointer, deleter_type> __ptr_;
915
916 struct __nat { int __for_bool_; };
917
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000918 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000919
920 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000921 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000922 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000923
924 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000925 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000926 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000927
928 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000929 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000930 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000931
932 template <bool _Dummy, class _Deleter = typename __dependent_type<
933 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000934 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000935 typename enable_if<is_default_constructible<_Deleter>::value &&
936 !is_pointer<_Deleter>::value>::type;
937
938 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000939 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000940 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
941
942 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000943 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000944 is_convertible<typename _UPtr::pointer, pointer>::value &&
945 !is_array<_Up>::value
946 >::type;
947
948 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000949 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000950 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
951 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
952 >::type;
953
954 template <class _UDel>
955 using _EnableIfDeleterAssignable = typename enable_if<
956 is_assignable<_Dp&, _UDel&&>::value
957 >::type;
958
959public:
960 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000961 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000962 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500963 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000964
965 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000966 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000967 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500968 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000969
970 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000971 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000972 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500973 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000974
975 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000976 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000977 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000978 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000979 : __ptr_(__p, __d) {}
980
981 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000982 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000983 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000984 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000985 : __ptr_(__p, _VSTD::move(__d)) {
986 static_assert(!is_reference<deleter_type>::value,
987 "rvalue deleter bound to reference");
988 }
989
990 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000991 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000992 _LIBCPP_INLINE_VISIBILITY
993 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
994
995 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000996 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000997 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
998 }
999
1000 template <class _Up, class _Ep,
1001 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1002 class = _EnableIfDeleterConvertible<_Ep>
1003 >
1004 _LIBCPP_INLINE_VISIBILITY
1005 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1006 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1007
1008#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1009 template <class _Up>
1010 _LIBCPP_INLINE_VISIBILITY
1011 unique_ptr(auto_ptr<_Up>&& __p,
1012 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001013 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001014 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001015 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001016#endif
1017
1018 _LIBCPP_INLINE_VISIBILITY
1019 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1020 reset(__u.release());
1021 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1022 return *this;
1023 }
1024
1025 template <class _Up, class _Ep,
1026 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1027 class = _EnableIfDeleterAssignable<_Ep>
1028 >
1029 _LIBCPP_INLINE_VISIBILITY
1030 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1031 reset(__u.release());
1032 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1033 return *this;
1034 }
1035
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001036#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1037 template <class _Up>
1038 _LIBCPP_INLINE_VISIBILITY
1039 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1040 is_same<_Dp, default_delete<_Tp> >::value,
1041 unique_ptr&>::type
1042 operator=(auto_ptr<_Up> __p) {
1043 reset(__p.release());
1044 return *this;
1045 }
1046#endif
1047
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001048#ifdef _LIBCPP_CXX03_LANG
1049 unique_ptr(unique_ptr const&) = delete;
1050 unique_ptr& operator=(unique_ptr const&) = delete;
1051#endif
1052
1053
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001054 _LIBCPP_INLINE_VISIBILITY
1055 ~unique_ptr() { reset(); }
1056
1057 _LIBCPP_INLINE_VISIBILITY
1058 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1059 reset();
1060 return *this;
1061 }
1062
1063 _LIBCPP_INLINE_VISIBILITY
1064 typename add_lvalue_reference<_Tp>::type
1065 operator*() const {
1066 return *__ptr_.first();
1067 }
1068 _LIBCPP_INLINE_VISIBILITY
1069 pointer operator->() const _NOEXCEPT {
1070 return __ptr_.first();
1071 }
1072 _LIBCPP_INLINE_VISIBILITY
1073 pointer get() const _NOEXCEPT {
1074 return __ptr_.first();
1075 }
1076 _LIBCPP_INLINE_VISIBILITY
1077 deleter_type& get_deleter() _NOEXCEPT {
1078 return __ptr_.second();
1079 }
1080 _LIBCPP_INLINE_VISIBILITY
1081 const deleter_type& get_deleter() const _NOEXCEPT {
1082 return __ptr_.second();
1083 }
1084 _LIBCPP_INLINE_VISIBILITY
1085 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1086 return __ptr_.first() != nullptr;
1087 }
1088
1089 _LIBCPP_INLINE_VISIBILITY
1090 pointer release() _NOEXCEPT {
1091 pointer __t = __ptr_.first();
1092 __ptr_.first() = pointer();
1093 return __t;
1094 }
1095
1096 _LIBCPP_INLINE_VISIBILITY
1097 void reset(pointer __p = pointer()) _NOEXCEPT {
1098 pointer __tmp = __ptr_.first();
1099 __ptr_.first() = __p;
1100 if (__tmp)
1101 __ptr_.second()(__tmp);
1102 }
1103
1104 _LIBCPP_INLINE_VISIBILITY
1105 void swap(unique_ptr& __u) _NOEXCEPT {
1106 __ptr_.swap(__u.__ptr_);
1107 }
1108};
1109
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001110
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001112class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001114 typedef _Tp element_type;
1115 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001116 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001117
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001119 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120
Eric Fiselier31127cd2017-04-16 02:14:31 +00001121 template <class _From>
1122 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123
Eric Fiselier31127cd2017-04-16 02:14:31 +00001124 template <class _FromElem>
1125 struct _CheckArrayPointerConversion<_FromElem*>
1126 : integral_constant<bool,
1127 is_same<_FromElem*, pointer>::value ||
1128 (is_same<pointer, element_type*>::value &&
1129 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1130 >
1131 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132
Eric Fiselier31127cd2017-04-16 02:14:31 +00001133 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001134
1135 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001136 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001137 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001138
1139 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001140 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001141 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001142
1143 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001144 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001145 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001146
1147 template <bool _Dummy, class _Deleter = typename __dependent_type<
1148 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001149 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001150 typename enable_if<is_default_constructible<_Deleter>::value &&
1151 !is_pointer<_Deleter>::value>::type;
1152
1153 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001154 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001155 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1156
1157 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001158 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001159 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001160 >::type;
1161
1162 template <class _UPtr, class _Up,
1163 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001164 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001165 is_array<_Up>::value &&
1166 is_same<pointer, element_type*>::value &&
1167 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1168 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1169 >::type;
1170
1171 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001172 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001173 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1174 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1175 >::type;
1176
1177 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001178 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001179 is_assignable<_Dp&, _UDel&&>::value
1180 >::type;
1181
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001183 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001184 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001185 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001186 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001188 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001189 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001190 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001191 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001193 template <class _Pp, bool _Dummy = true,
1194 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001195 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001196 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001197 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001198 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001200 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001201 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1202 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001203 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001204 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001205 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001207 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001208 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001209 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001210 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001211 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001213 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001214 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1215 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001216 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001217 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001218 : __ptr_(__p, _VSTD::move(__d)) {
1219 static_assert(!is_reference<deleter_type>::value,
1220 "rvalue deleter bound to reference");
1221 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001223 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001224 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001225 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001226 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001227 : __ptr_(nullptr, _VSTD::move(__d)) {
1228 static_assert(!is_reference<deleter_type>::value,
1229 "rvalue deleter bound to reference");
1230 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001231
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001232 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001233 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1234 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001235 _LIBCPP_INLINE_VISIBILITY
1236 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001237
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001238 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001239 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001240 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1241 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001242
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001243 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001244 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001245 reset(__u.release());
1246 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1247 return *this;
1248 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001250 template <class _Up, class _Ep,
1251 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1252 class = _EnableIfDeleterConvertible<_Ep>
1253 >
1254 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001255 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001256 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001257 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001259 template <class _Up, class _Ep,
1260 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1261 class = _EnableIfDeleterAssignable<_Ep>
1262 >
1263 _LIBCPP_INLINE_VISIBILITY
1264 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001265 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001266 reset(__u.release());
1267 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1268 return *this;
1269 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001271#ifdef _LIBCPP_CXX03_LANG
1272 unique_ptr(unique_ptr const&) = delete;
1273 unique_ptr& operator=(unique_ptr const&) = delete;
1274#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001275
1276public:
1277 _LIBCPP_INLINE_VISIBILITY
1278 ~unique_ptr() { reset(); }
1279
1280 _LIBCPP_INLINE_VISIBILITY
1281 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1282 reset();
1283 return *this;
1284 }
1285
1286 _LIBCPP_INLINE_VISIBILITY
1287 typename add_lvalue_reference<_Tp>::type
1288 operator[](size_t __i) const {
1289 return __ptr_.first()[__i];
1290 }
1291 _LIBCPP_INLINE_VISIBILITY
1292 pointer get() const _NOEXCEPT {
1293 return __ptr_.first();
1294 }
1295
1296 _LIBCPP_INLINE_VISIBILITY
1297 deleter_type& get_deleter() _NOEXCEPT {
1298 return __ptr_.second();
1299 }
1300
1301 _LIBCPP_INLINE_VISIBILITY
1302 const deleter_type& get_deleter() const _NOEXCEPT {
1303 return __ptr_.second();
1304 }
1305 _LIBCPP_INLINE_VISIBILITY
1306 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1307 return __ptr_.first() != nullptr;
1308 }
1309
1310 _LIBCPP_INLINE_VISIBILITY
1311 pointer release() _NOEXCEPT {
1312 pointer __t = __ptr_.first();
1313 __ptr_.first() = pointer();
1314 return __t;
1315 }
1316
1317 template <class _Pp>
1318 _LIBCPP_INLINE_VISIBILITY
1319 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001320 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001321 >::type
1322 reset(_Pp __p) _NOEXCEPT {
1323 pointer __tmp = __ptr_.first();
1324 __ptr_.first() = __p;
1325 if (__tmp)
1326 __ptr_.second()(__tmp);
1327 }
1328
1329 _LIBCPP_INLINE_VISIBILITY
1330 void reset(nullptr_t = nullptr) _NOEXCEPT {
1331 pointer __tmp = __ptr_.first();
1332 __ptr_.first() = nullptr;
1333 if (__tmp)
1334 __ptr_.second()(__tmp);
1335 }
1336
1337 _LIBCPP_INLINE_VISIBILITY
1338 void swap(unique_ptr& __u) _NOEXCEPT {
1339 __ptr_.swap(__u.__ptr_);
1340 }
1341
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342};
1343
1344template <class _Tp, class _Dp>
1345inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001346typename enable_if<
1347 __is_swappable<_Dp>::value,
1348 void
1349>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001350swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351
1352template <class _T1, class _D1, class _T2, class _D2>
1353inline _LIBCPP_INLINE_VISIBILITY
1354bool
1355operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1356
1357template <class _T1, class _D1, class _T2, class _D2>
1358inline _LIBCPP_INLINE_VISIBILITY
1359bool
1360operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1361
1362template <class _T1, class _D1, class _T2, class _D2>
1363inline _LIBCPP_INLINE_VISIBILITY
1364bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001365operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1366{
1367 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1368 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001369 typedef typename common_type<_P1, _P2>::type _Vp;
1370 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001371}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372
1373template <class _T1, class _D1, class _T2, class _D2>
1374inline _LIBCPP_INLINE_VISIBILITY
1375bool
1376operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1377
1378template <class _T1, class _D1, class _T2, class _D2>
1379inline _LIBCPP_INLINE_VISIBILITY
1380bool
1381operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1382
1383template <class _T1, class _D1, class _T2, class _D2>
1384inline _LIBCPP_INLINE_VISIBILITY
1385bool
1386operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1387
Howard Hinnantb17caf92012-02-21 21:02:58 +00001388template <class _T1, class _D1>
1389inline _LIBCPP_INLINE_VISIBILITY
1390bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001391operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001392{
1393 return !__x;
1394}
1395
1396template <class _T1, class _D1>
1397inline _LIBCPP_INLINE_VISIBILITY
1398bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001399operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001400{
1401 return !__x;
1402}
1403
1404template <class _T1, class _D1>
1405inline _LIBCPP_INLINE_VISIBILITY
1406bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001407operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001408{
1409 return static_cast<bool>(__x);
1410}
1411
1412template <class _T1, class _D1>
1413inline _LIBCPP_INLINE_VISIBILITY
1414bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001415operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001416{
1417 return static_cast<bool>(__x);
1418}
1419
1420template <class _T1, class _D1>
1421inline _LIBCPP_INLINE_VISIBILITY
1422bool
1423operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1424{
1425 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1426 return less<_P1>()(__x.get(), nullptr);
1427}
1428
1429template <class _T1, class _D1>
1430inline _LIBCPP_INLINE_VISIBILITY
1431bool
1432operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1433{
1434 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1435 return less<_P1>()(nullptr, __x.get());
1436}
1437
1438template <class _T1, class _D1>
1439inline _LIBCPP_INLINE_VISIBILITY
1440bool
1441operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1442{
1443 return nullptr < __x;
1444}
1445
1446template <class _T1, class _D1>
1447inline _LIBCPP_INLINE_VISIBILITY
1448bool
1449operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1450{
1451 return __x < nullptr;
1452}
1453
1454template <class _T1, class _D1>
1455inline _LIBCPP_INLINE_VISIBILITY
1456bool
1457operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1458{
1459 return !(nullptr < __x);
1460}
1461
1462template <class _T1, class _D1>
1463inline _LIBCPP_INLINE_VISIBILITY
1464bool
1465operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1466{
1467 return !(__x < nullptr);
1468}
1469
1470template <class _T1, class _D1>
1471inline _LIBCPP_INLINE_VISIBILITY
1472bool
1473operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1474{
1475 return !(__x < nullptr);
1476}
1477
1478template <class _T1, class _D1>
1479inline _LIBCPP_INLINE_VISIBILITY
1480bool
1481operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1482{
1483 return !(nullptr < __x);
1484}
1485
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001486#if _LIBCPP_STD_VER > 11
1487
1488template<class _Tp>
1489struct __unique_if
1490{
1491 typedef unique_ptr<_Tp> __unique_single;
1492};
1493
1494template<class _Tp>
1495struct __unique_if<_Tp[]>
1496{
1497 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1498};
1499
1500template<class _Tp, size_t _Np>
1501struct __unique_if<_Tp[_Np]>
1502{
1503 typedef void __unique_array_known_bound;
1504};
1505
1506template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00001507inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001508typename __unique_if<_Tp>::__unique_single
1509make_unique(_Args&&... __args)
1510{
1511 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
1512}
1513
1514template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00001515inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001516typename __unique_if<_Tp>::__unique_array_unknown_bound
1517make_unique(size_t __n)
1518{
1519 typedef typename remove_extent<_Tp>::type _Up;
1520 return unique_ptr<_Tp>(new _Up[__n]());
1521}
1522
1523template<class _Tp, class... _Args>
1524 typename __unique_if<_Tp>::__unique_array_known_bound
1525 make_unique(_Args&&...) = delete;
1526
1527#endif // _LIBCPP_STD_VER > 11
1528
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001529template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001530#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001531struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001532#else
1533struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001534 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001535#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001536{
1537 typedef unique_ptr<_Tp, _Dp> argument_type;
1538 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001540 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001541 {
1542 typedef typename argument_type::pointer pointer;
1543 return hash<pointer>()(__ptr.get());
1544 }
1545};
1546
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547struct __destruct_n
1548{
1549private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001550 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551
1552 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001553 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001554 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555
1556 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001557 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 {}
1559
Howard Hinnant719bda32011-05-28 14:41:13 +00001560 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001561 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001562 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 {}
1564
Howard Hinnant719bda32011-05-28 14:41:13 +00001565 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001566 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001567 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 {}
1569public:
Howard Hinnant719bda32011-05-28 14:41:13 +00001570 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001571 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572
1573 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05001574 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001575 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576
1577 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001578 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001579 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
1581 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001582 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001583 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584};
1585
1586template <class _Alloc>
1587class __allocator_destructor
1588{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001589 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001591 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
1592 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593private:
1594 _Alloc& __alloc_;
1595 size_type __s_;
1596public:
1597 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00001598 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001601 void operator()(pointer __p) _NOEXCEPT
1602 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603};
1604
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001605// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
1606// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -05001607// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001608#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
1609 && defined(__ATOMIC_RELAXED) \
1610 && defined(__ATOMIC_ACQ_REL)
1611# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00001612#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001613# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
1614#endif
1615
1616template <class _Tp>
1617inline _LIBCPP_INLINE_VISIBILITY _Tp
1618__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
1619{
1620#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
1621 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
1622#else
1623 return __t += 1;
1624#endif
1625}
1626
1627template <class _Tp>
1628inline _LIBCPP_INLINE_VISIBILITY _Tp
1629__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
1630{
1631#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
1632 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
1633#else
1634 return __t -= 1;
1635#endif
1636}
1637
Howard Hinnant756c69b2010-09-22 16:48:34 +00001638class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 : public std::exception
1640{
1641public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01001642 bad_weak_ptr() _NOEXCEPT = default;
1643 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00001644 virtual ~bad_weak_ptr() _NOEXCEPT;
1645 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646};
1647
Louis Dionne16fe2952018-07-11 23:14:33 +00001648_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001649void __throw_bad_weak_ptr()
1650{
1651#ifndef _LIBCPP_NO_EXCEPTIONS
1652 throw bad_weak_ptr();
1653#else
1654 _VSTD::abort();
1655#endif
1656}
1657
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001658template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001660class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661{
1662 __shared_count(const __shared_count&);
1663 __shared_count& operator=(const __shared_count&);
1664
1665protected:
1666 long __shared_owners_;
1667 virtual ~__shared_count();
1668private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001669 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670
1671public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001673 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674 : __shared_owners_(__refs) {}
1675
Louis Dionne5e0eadd2018-08-01 02:08:59 +00001676#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00001677 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00001678 void __add_shared() _NOEXCEPT;
1679 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001680#else
1681 _LIBCPP_INLINE_VISIBILITY
1682 void __add_shared() _NOEXCEPT {
1683 __libcpp_atomic_refcount_increment(__shared_owners_);
1684 }
1685 _LIBCPP_INLINE_VISIBILITY
1686 bool __release_shared() _NOEXCEPT {
1687 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
1688 __on_zero_shared();
1689 return true;
1690 }
1691 return false;
1692 }
1693#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00001694 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00001695 long use_count() const _NOEXCEPT {
1696 return __libcpp_relaxed_load(&__shared_owners_) + 1;
1697 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698};
1699
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001700class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 : private __shared_count
1702{
1703 long __shared_weak_owners_;
1704
1705public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001707 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 : __shared_count(__refs),
1709 __shared_weak_owners_(__refs) {}
1710protected:
1711 virtual ~__shared_weak_count();
1712
1713public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00001714#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00001715 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00001716 void __add_shared() _NOEXCEPT;
1717 void __add_weak() _NOEXCEPT;
1718 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001719#else
1720 _LIBCPP_INLINE_VISIBILITY
1721 void __add_shared() _NOEXCEPT {
1722 __shared_count::__add_shared();
1723 }
1724 _LIBCPP_INLINE_VISIBILITY
1725 void __add_weak() _NOEXCEPT {
1726 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
1727 }
1728 _LIBCPP_INLINE_VISIBILITY
1729 void __release_shared() _NOEXCEPT {
1730 if (__shared_count::__release_shared())
1731 __release_weak();
1732 }
1733#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00001734 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001736 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
1737 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738
Howard Hinnant719bda32011-05-28 14:41:13 +00001739 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001741 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742};
1743
1744template <class _Tp, class _Dp, class _Alloc>
1745class __shared_ptr_pointer
1746 : public __shared_weak_count
1747{
1748 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
1749public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001752 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753
Howard Hinnant72f73582010-08-11 17:04:31 +00001754#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00001755 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00001756#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757
1758private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001759 virtual void __on_zero_shared() _NOEXCEPT;
1760 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761};
1762
Howard Hinnant72f73582010-08-11 17:04:31 +00001763#ifndef _LIBCPP_NO_RTTI
1764
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765template <class _Tp, class _Dp, class _Alloc>
1766const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00001767__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768{
Eric Fiselierc7490d02017-05-04 01:06:56 +00001769 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770}
1771
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001772#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001773
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774template <class _Tp, class _Dp, class _Alloc>
1775void
Howard Hinnant719bda32011-05-28 14:41:13 +00001776__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777{
1778 __data_.first().second()(__data_.first().first());
1779 __data_.first().second().~_Dp();
1780}
1781
1782template <class _Tp, class _Dp, class _Alloc>
1783void
Howard Hinnant719bda32011-05-28 14:41:13 +00001784__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785{
Eric Fiselierf8898c82015-02-05 23:01:40 +00001786 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
1787 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00001788 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1789
Eric Fiselierf8898c82015-02-05 23:01:40 +00001790 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00001792 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793}
1794
1795template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05001796struct __shared_ptr_emplace
1797 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798{
Louis Dionneda463cb2020-12-11 12:22:16 -05001799 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05001800 _LIBCPP_HIDE_FROM_ABI
1801 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05001802 : __storage_(_VSTD::move(__a))
1803 {
1804#if _LIBCPP_STD_VER > 17
1805 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
1806 _TpAlloc __tmp(*__get_alloc());
1807 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04001808#else
Louis Dionneda463cb2020-12-11 12:22:16 -05001809 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04001810#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05001811 }
Louis Dionne86549d72020-12-09 16:22:17 -05001812
1813 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05001814 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05001815
1816 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05001817 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818
1819private:
Louis Dionne86549d72020-12-09 16:22:17 -05001820 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05001821#if _LIBCPP_STD_VER > 17
1822 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
1823 _TpAlloc __tmp(*__get_alloc());
1824 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
1825#else
Louis Dionne86549d72020-12-09 16:22:17 -05001826 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05001827#endif
Louis Dionne86549d72020-12-09 16:22:17 -05001828 }
1829
1830 virtual void __on_zero_shared_weak() _NOEXCEPT {
1831 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
1832 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05001833 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05001834 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05001835 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
1836 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
1837 }
1838
Louis Dionneda463cb2020-12-11 12:22:16 -05001839 // This class implements the control block for non-array shared pointers created
1840 // through `std::allocate_shared` and `std::make_shared`.
1841 //
1842 // In previous versions of the library, we used a compressed pair to store
1843 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
1844 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
1845 // we now use a properly aligned char buffer while making sure that we maintain
1846 // the same layout that we had when we used a compressed pair.
1847 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
1848 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
1849 char __blob_[sizeof(_CompressedPair)];
1850
1851 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
1852 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
1853 }
1854 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
1855 __get_alloc()->~_Alloc();
1856 }
1857 _Alloc* __get_alloc() _NOEXCEPT {
1858 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
1859 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
1860 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
1861 return __alloc;
1862 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08001863 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05001864 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
1865 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
1866 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
1867 return __elem;
1868 }
1869 };
1870
1871 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
1872 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
1873 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874};
1875
Erik Pilkington2a398762017-05-25 15:43:31 +00001876struct __shared_ptr_dummy_rebind_allocator_type;
1877template <>
1878class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
1879{
1880public:
1881 template <class _Other>
1882 struct rebind
1883 {
1884 typedef allocator<_Other> other;
1885 };
1886};
1887
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001888template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889
zoecarverd73f42a2020-05-11 18:42:50 -07001890template<class _Tp, class _Up>
1891struct __compatible_with
1892#if _LIBCPP_STD_VER > 14
1893 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
1894#else
1895 : is_convertible<_Tp*, _Up*> {};
1896#endif // _LIBCPP_STD_VER > 14
1897
zoecarver9bc39102021-02-19 11:10:36 -08001898template <class _Dp, class _Pt,
1899 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
1900static true_type __well_formed_deleter_test(int);
1901
1902template <class, class>
1903static false_type __well_formed_deleter_test(...);
1904
1905template <class _Dp, class _Pt>
1906struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
1907
1908template<class _Dp, class _Tp, class _Yp>
1909struct __shared_ptr_deleter_ctor_reqs
1910{
1911 static const bool value = __compatible_with<_Tp, _Yp>::value &&
1912 is_move_constructible<_Dp>::value &&
1913 __well_formed_deleter<_Dp, _Tp*>::value;
1914};
1915
Vy Nguyene369bd92020-07-13 12:34:37 -04001916#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
1917# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1918#else
1919# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
1920#endif
1921
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001923class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001925public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00001926#if _LIBCPP_STD_VER > 14
1927 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07001928 typedef remove_extent_t<_Tp> element_type;
1929#else
1930 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00001931#endif
zoecarverd73f42a2020-05-11 18:42:50 -07001932
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933private:
1934 element_type* __ptr_;
1935 __shared_weak_count* __cntrl_;
1936
1937 struct __nat {int __for_bool_;};
1938public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001940 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001942 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00001943 template<class _Yp>
1944 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07001945 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00001946 template<class _Yp, class _Dp>
1947 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08001948 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00001949 template<class _Yp, class _Dp, class _Alloc>
1950 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08001951 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
1953 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001954 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
1955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001956 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001957 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07001960 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00001961 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001963 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001964 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07001965 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00001966 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00001968 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00001969#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00001970 template<class _Yp>
1971 shared_ptr(auto_ptr<_Yp>&& __r,
1972 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00001973#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00001974 template <class _Yp, class _Dp>
1975 shared_ptr(unique_ptr<_Yp, _Dp>&&,
1976 typename enable_if
1977 <
1978 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00001979 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
1980 __nat
1981 >::type = __nat());
1982 template <class _Yp, class _Dp>
1983 shared_ptr(unique_ptr<_Yp, _Dp>&&,
1984 typename enable_if
1985 <
1986 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00001987 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
1988 __nat
1989 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990
1991 ~shared_ptr();
1992
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001994 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001995 template<class _Yp>
1996 typename enable_if
1997 <
zoecarverd73f42a2020-05-11 18:42:50 -07001998 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001999 shared_ptr&
2000 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002002 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002004 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002005 template<class _Yp>
2006 typename enable_if
2007 <
zoecarverd73f42a2020-05-11 18:42:50 -07002008 __compatible_with<_Yp, element_type>::value,
2009 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002010 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002012 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002013#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002014 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002016 typename enable_if
2017 <
2018 !is_array<_Yp>::value &&
2019 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002020 shared_ptr
2021 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002022 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002023#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002024 template <class _Yp, class _Dp>
2025 typename enable_if
2026 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002027 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2028 shared_ptr&
2029 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002031 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002034 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002036 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002037 template<class _Yp>
2038 typename enable_if
2039 <
zoecarverd73f42a2020-05-11 18:42:50 -07002040 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002041 void
2042 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002044 reset(_Yp* __p);
2045 template<class _Yp, class _Dp>
2046 typename enable_if
2047 <
zoecarverd73f42a2020-05-11 18:42:50 -07002048 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002049 void
2050 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002052 reset(_Yp* __p, _Dp __d);
2053 template<class _Yp, class _Dp, class _Alloc>
2054 typename enable_if
2055 <
zoecarverd73f42a2020-05-11 18:42:50 -07002056 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002057 void
2058 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002060 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002061
Howard Hinnant756c69b2010-09-22 16:48:34 +00002062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002063 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002065 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2066 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002067 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002068 element_type* operator->() const _NOEXCEPT
2069 {
2070 static_assert(!_VSTD::is_array<_Tp>::value,
2071 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2072 return __ptr_;
2073 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002075 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002077 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002078 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002079 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002080 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002081 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002082 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002084 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002085 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002086 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002088 _LIBCPP_INLINE_VISIBILITY
2089 bool
2090 __owner_equivalent(const shared_ptr& __p) const
2091 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092
zoecarverd73f42a2020-05-11 18:42:50 -07002093#if _LIBCPP_STD_VER > 14
2094 typename add_lvalue_reference<element_type>::type
2095 _LIBCPP_INLINE_VISIBILITY
2096 operator[](ptrdiff_t __i) const
2097 {
2098 static_assert(_VSTD::is_array<_Tp>::value,
2099 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2100 return __ptr_[__i];
2101 }
2102#endif
2103
Howard Hinnant72f73582010-08-11 17:04:31 +00002104#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002107 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002108 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002109 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002110 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002111#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112
Zoe Carverd9040c72019-10-22 15:16:49 +00002113 template<class _Yp, class _CntrlBlk>
2114 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002115 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002116 {
2117 shared_ptr<_Tp> __r;
2118 __r.__ptr_ = __p;
2119 __r.__cntrl_ = __cntrl;
2120 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2121 return __r;
2122 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002123
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002125 template <class _Yp, bool = is_function<_Yp>::value>
2126 struct __shared_ptr_default_allocator
2127 {
2128 typedef allocator<_Yp> type;
2129 };
2130
2131 template <class _Yp>
2132 struct __shared_ptr_default_allocator<_Yp, true>
2133 {
2134 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2135 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002137 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002138 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002139 typename enable_if<is_convertible<_OrigPtr*,
2140 const enable_shared_from_this<_Yp>*
2141 >::value,
2142 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002143 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2144 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002146 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002147 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002148 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002149 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2150 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002151 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152 }
2153
Erik Pilkington2a398762017-05-25 15:43:31 +00002154 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155
zoecarverd73f42a2020-05-11 18:42:50 -07002156 template <class, class _Yp>
2157 struct __shared_ptr_default_delete
2158 : default_delete<_Yp> {};
2159
2160 template <class _Yp, class _Un, size_t _Sz>
2161 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2162 : default_delete<_Yp[]> {};
2163
2164 template <class _Yp, class _Un>
2165 struct __shared_ptr_default_delete<_Yp[], _Un>
2166 : default_delete<_Yp[]> {};
2167
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002168 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2169 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170};
2171
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002172#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2173template<class _Tp>
2174shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2175template<class _Tp, class _Dp>
2176shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2177#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002178
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002180inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002181_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002182shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002183 : __ptr_(nullptr),
2184 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185{
2186}
2187
2188template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002189inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002190_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002191shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002192 : __ptr_(nullptr),
2193 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194{
2195}
2196
2197template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002198template<class _Yp>
2199shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002200 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201 : __ptr_(__p)
2202{
2203 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002204 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002205 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2206 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002208 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209}
2210
2211template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002212template<class _Yp, class _Dp>
2213shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002214 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215 : __ptr_(__p)
2216{
2217#ifndef _LIBCPP_NO_EXCEPTIONS
2218 try
2219 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002220#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002221 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2222 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002223#ifndef _LIBCPP_CXX03_LANG
2224 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2225#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002226 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002227#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002228 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229#ifndef _LIBCPP_NO_EXCEPTIONS
2230 }
2231 catch (...)
2232 {
2233 __d(__p);
2234 throw;
2235 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002236#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237}
2238
2239template<class _Tp>
2240template<class _Dp>
2241shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002242 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243{
2244#ifndef _LIBCPP_NO_EXCEPTIONS
2245 try
2246 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002247#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002248 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2249 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002250#ifndef _LIBCPP_CXX03_LANG
2251 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2252#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002253 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002254#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255#ifndef _LIBCPP_NO_EXCEPTIONS
2256 }
2257 catch (...)
2258 {
2259 __d(__p);
2260 throw;
2261 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002262#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263}
2264
2265template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002266template<class _Yp, class _Dp, class _Alloc>
2267shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002268 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 : __ptr_(__p)
2270{
2271#ifndef _LIBCPP_NO_EXCEPTIONS
2272 try
2273 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002274#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002276 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 typedef __allocator_destructor<_A2> _D2;
2278 _A2 __a2(__a);
2279 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002280 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2281#ifndef _LIBCPP_CXX03_LANG
2282 _CntrlBlk(__p, _VSTD::move(__d), __a);
2283#else
2284 _CntrlBlk(__p, __d, __a);
2285#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002286 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002287 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288#ifndef _LIBCPP_NO_EXCEPTIONS
2289 }
2290 catch (...)
2291 {
2292 __d(__p);
2293 throw;
2294 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002295#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296}
2297
2298template<class _Tp>
2299template<class _Dp, class _Alloc>
2300shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002301 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302{
2303#ifndef _LIBCPP_NO_EXCEPTIONS
2304 try
2305 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002306#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002308 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309 typedef __allocator_destructor<_A2> _D2;
2310 _A2 __a2(__a);
2311 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002312 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2313#ifndef _LIBCPP_CXX03_LANG
2314 _CntrlBlk(__p, _VSTD::move(__d), __a);
2315#else
2316 _CntrlBlk(__p, __d, __a);
2317#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002318 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319#ifndef _LIBCPP_NO_EXCEPTIONS
2320 }
2321 catch (...)
2322 {
2323 __d(__p);
2324 throw;
2325 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002326#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327}
2328
2329template<class _Tp>
2330template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002331inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002332shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333 : __ptr_(__p),
2334 __cntrl_(__r.__cntrl_)
2335{
2336 if (__cntrl_)
2337 __cntrl_->__add_shared();
2338}
2339
2340template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002341inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002342shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343 : __ptr_(__r.__ptr_),
2344 __cntrl_(__r.__cntrl_)
2345{
2346 if (__cntrl_)
2347 __cntrl_->__add_shared();
2348}
2349
2350template<class _Tp>
2351template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002352inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002354 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002355 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356 : __ptr_(__r.__ptr_),
2357 __cntrl_(__r.__cntrl_)
2358{
2359 if (__cntrl_)
2360 __cntrl_->__add_shared();
2361}
2362
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002364inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002365shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366 : __ptr_(__r.__ptr_),
2367 __cntrl_(__r.__cntrl_)
2368{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002369 __r.__ptr_ = nullptr;
2370 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371}
2372
2373template<class _Tp>
2374template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002375inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002377 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002378 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379 : __ptr_(__r.__ptr_),
2380 __cntrl_(__r.__cntrl_)
2381{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002382 __r.__ptr_ = nullptr;
2383 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002384}
2385
Marshall Clowb22274f2017-01-24 22:22:33 +00002386#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002388template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002389shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002390 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391 : __ptr_(__r.get())
2392{
2393 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2394 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002395 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 __r.release();
2397}
Marshall Clowb22274f2017-01-24 22:22:33 +00002398#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399
2400template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002401template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002403 typename enable_if
2404 <
2405 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002406 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2407 __nat
2408 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409 : __ptr_(__r.get())
2410{
Marshall Clow35cde742015-05-10 13:59:45 +00002411#if _LIBCPP_STD_VER > 11
2412 if (__ptr_ == nullptr)
2413 __cntrl_ = nullptr;
2414 else
2415#endif
2416 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002417 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002418 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
Erik Pilkington2a398762017-05-25 15:43:31 +00002419 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002420 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002421 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422 __r.release();
2423}
2424
2425template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002426template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002428 typename enable_if
2429 <
2430 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002431 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2432 __nat
2433 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434 : __ptr_(__r.get())
2435{
Marshall Clow35cde742015-05-10 13:59:45 +00002436#if _LIBCPP_STD_VER > 11
2437 if (__ptr_ == nullptr)
2438 __cntrl_ = nullptr;
2439 else
2440#endif
2441 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002442 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002443 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow35cde742015-05-10 13:59:45 +00002444 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00002445 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05002446 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002447 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002448 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 __r.release();
2450}
2451
Zoe Carver6cd05c32019-08-19 15:47:16 +00002452template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453shared_ptr<_Tp>::~shared_ptr()
2454{
2455 if (__cntrl_)
2456 __cntrl_->__release_shared();
2457}
2458
2459template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002460inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002462shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463{
2464 shared_ptr(__r).swap(*this);
2465 return *this;
2466}
2467
2468template<class _Tp>
2469template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002470inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002471typename enable_if
2472<
zoecarverd73f42a2020-05-11 18:42:50 -07002473 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002474 shared_ptr<_Tp>&
2475>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002476shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477{
2478 shared_ptr(__r).swap(*this);
2479 return *this;
2480}
2481
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002483inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002485shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002487 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488 return *this;
2489}
2490
2491template<class _Tp>
2492template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002493inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002494typename enable_if
2495<
zoecarverd73f42a2020-05-11 18:42:50 -07002496 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002497 shared_ptr<_Tp>&
2498>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
2500{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002501 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502 return *this;
2503}
2504
Marshall Clowb22274f2017-01-24 22:22:33 +00002505#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506template<class _Tp>
2507template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002508inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002509typename enable_if
2510<
2511 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00002512 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002513 shared_ptr<_Tp>
2514>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
2516{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002517 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518 return *this;
2519}
Marshall Clowb22274f2017-01-24 22:22:33 +00002520#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521
2522template<class _Tp>
2523template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002524inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002525typename enable_if
2526<
Aditya Kumar7c5db692017-08-20 10:38:55 +00002527 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00002528 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002529 shared_ptr<_Tp>&
2530>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
2532{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002533 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534 return *this;
2535}
2536
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002538inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539void
Howard Hinnant719bda32011-05-28 14:41:13 +00002540shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002542 _VSTD::swap(__ptr_, __r.__ptr_);
2543 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544}
2545
2546template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002547inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548void
Howard Hinnant719bda32011-05-28 14:41:13 +00002549shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550{
2551 shared_ptr().swap(*this);
2552}
2553
2554template<class _Tp>
2555template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002556inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002557typename enable_if
2558<
zoecarverd73f42a2020-05-11 18:42:50 -07002559 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002560 void
2561>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562shared_ptr<_Tp>::reset(_Yp* __p)
2563{
2564 shared_ptr(__p).swap(*this);
2565}
2566
2567template<class _Tp>
2568template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002569inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002570typename enable_if
2571<
zoecarverd73f42a2020-05-11 18:42:50 -07002572 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002573 void
2574>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
2576{
2577 shared_ptr(__p, __d).swap(*this);
2578}
2579
2580template<class _Tp>
2581template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002582inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002583typename enable_if
2584<
zoecarverd73f42a2020-05-11 18:42:50 -07002585 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002586 void
2587>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
2589{
2590 shared_ptr(__p, __d, __a).swap(*this);
2591}
2592
Louis Dionne74aef9f2020-12-11 12:20:06 -05002593//
2594// std::allocate_shared and std::make_shared
2595//
2596template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
2597_LIBCPP_HIDE_FROM_ABI
2598shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599{
Louis Dionne74aef9f2020-12-11 12:20:06 -05002600 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
2601 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
2602 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
2603 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
2604 auto __control_block = __guard.__release_ptr();
2605 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606}
2607
Louis Dionne43c9f8f2020-12-09 16:57:28 -05002608template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
2609_LIBCPP_HIDE_FROM_ABI
2610shared_ptr<_Tp> make_shared(_Args&& ...__args)
2611{
2612 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
2613}
2614
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615template<class _Tp, class _Up>
2616inline _LIBCPP_INLINE_VISIBILITY
2617bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002618operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619{
2620 return __x.get() == __y.get();
2621}
2622
2623template<class _Tp, class _Up>
2624inline _LIBCPP_INLINE_VISIBILITY
2625bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002626operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627{
2628 return !(__x == __y);
2629}
2630
2631template<class _Tp, class _Up>
2632inline _LIBCPP_INLINE_VISIBILITY
2633bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002634operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00002636#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00002637 typedef typename common_type<_Tp*, _Up*>::type _Vp;
2638 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00002639#else
2640 return less<>()(__x.get(), __y.get());
2641#endif
2642
Howard Hinnantb17caf92012-02-21 21:02:58 +00002643}
2644
2645template<class _Tp, class _Up>
2646inline _LIBCPP_INLINE_VISIBILITY
2647bool
2648operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2649{
2650 return __y < __x;
2651}
2652
2653template<class _Tp, class _Up>
2654inline _LIBCPP_INLINE_VISIBILITY
2655bool
2656operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2657{
2658 return !(__y < __x);
2659}
2660
2661template<class _Tp, class _Up>
2662inline _LIBCPP_INLINE_VISIBILITY
2663bool
2664operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2665{
2666 return !(__x < __y);
2667}
2668
2669template<class _Tp>
2670inline _LIBCPP_INLINE_VISIBILITY
2671bool
2672operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2673{
2674 return !__x;
2675}
2676
2677template<class _Tp>
2678inline _LIBCPP_INLINE_VISIBILITY
2679bool
2680operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2681{
2682 return !__x;
2683}
2684
2685template<class _Tp>
2686inline _LIBCPP_INLINE_VISIBILITY
2687bool
2688operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2689{
2690 return static_cast<bool>(__x);
2691}
2692
2693template<class _Tp>
2694inline _LIBCPP_INLINE_VISIBILITY
2695bool
2696operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2697{
2698 return static_cast<bool>(__x);
2699}
2700
2701template<class _Tp>
2702inline _LIBCPP_INLINE_VISIBILITY
2703bool
2704operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2705{
2706 return less<_Tp*>()(__x.get(), nullptr);
2707}
2708
2709template<class _Tp>
2710inline _LIBCPP_INLINE_VISIBILITY
2711bool
2712operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2713{
2714 return less<_Tp*>()(nullptr, __x.get());
2715}
2716
2717template<class _Tp>
2718inline _LIBCPP_INLINE_VISIBILITY
2719bool
2720operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2721{
2722 return nullptr < __x;
2723}
2724
2725template<class _Tp>
2726inline _LIBCPP_INLINE_VISIBILITY
2727bool
2728operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2729{
2730 return __x < nullptr;
2731}
2732
2733template<class _Tp>
2734inline _LIBCPP_INLINE_VISIBILITY
2735bool
2736operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2737{
2738 return !(nullptr < __x);
2739}
2740
2741template<class _Tp>
2742inline _LIBCPP_INLINE_VISIBILITY
2743bool
2744operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2745{
2746 return !(__x < nullptr);
2747}
2748
2749template<class _Tp>
2750inline _LIBCPP_INLINE_VISIBILITY
2751bool
2752operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2753{
2754 return !(__x < nullptr);
2755}
2756
2757template<class _Tp>
2758inline _LIBCPP_INLINE_VISIBILITY
2759bool
2760operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2761{
2762 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763}
2764
2765template<class _Tp>
2766inline _LIBCPP_INLINE_VISIBILITY
2767void
Howard Hinnant719bda32011-05-28 14:41:13 +00002768swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769{
2770 __x.swap(__y);
2771}
2772
2773template<class _Tp, class _Up>
2774inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002775shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002776static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777{
zoecarverd73f42a2020-05-11 18:42:50 -07002778 return shared_ptr<_Tp>(__r,
2779 static_cast<
2780 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781}
2782
2783template<class _Tp, class _Up>
2784inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002785shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002786dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787{
zoecarverd73f42a2020-05-11 18:42:50 -07002788 typedef typename shared_ptr<_Tp>::element_type _ET;
2789 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
2791}
2792
2793template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07002794shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002795const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796{
zoecarverd73f42a2020-05-11 18:42:50 -07002797 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002798 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799}
2800
zoecarverd73f42a2020-05-11 18:42:50 -07002801template<class _Tp, class _Up>
2802shared_ptr<_Tp>
2803reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
2804{
2805 return shared_ptr<_Tp>(__r,
2806 reinterpret_cast<
2807 typename shared_ptr<_Tp>::element_type*>(__r.get()));
2808}
2809
Howard Hinnant72f73582010-08-11 17:04:31 +00002810#ifndef _LIBCPP_NO_RTTI
2811
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812template<class _Dp, class _Tp>
2813inline _LIBCPP_INLINE_VISIBILITY
2814_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00002815get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816{
2817 return __p.template __get_deleter<_Dp>();
2818}
2819
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002820#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002821
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002823class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002825public:
2826 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827private:
2828 element_type* __ptr_;
2829 __shared_weak_count* __cntrl_;
2830
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002831public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002833 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002834 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00002835 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
2836 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002838 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002839 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00002840 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
2841 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002842
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002844 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002845 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002846 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
2847 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002848 ~weak_ptr();
2849
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002851 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002852 template<class _Yp>
2853 typename enable_if
2854 <
2855 is_convertible<_Yp*, element_type*>::value,
2856 weak_ptr&
2857 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002859 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
2860
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002862 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
2863 template<class _Yp>
2864 typename enable_if
2865 <
2866 is_convertible<_Yp*, element_type*>::value,
2867 weak_ptr&
2868 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002870 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
2871
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002872 template<class _Yp>
2873 typename enable_if
2874 <
2875 is_convertible<_Yp*, element_type*>::value,
2876 weak_ptr&
2877 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002879 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002880
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002882 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002884 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002885
Howard Hinnant756c69b2010-09-22 16:48:34 +00002886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002887 long use_count() const _NOEXCEPT
2888 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002890 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002891 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002892 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002893 template<class _Up>
2894 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002895 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002897 template<class _Up>
2898 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002899 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900 {return __cntrl_ < __r.__cntrl_;}
2901
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002902 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
2903 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904};
2905
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002906#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2907template<class _Tp>
2908weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
2909#endif
2910
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002912inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002913_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002914weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002915 : __ptr_(nullptr),
2916 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917{
2918}
2919
2920template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002921inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002922weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 : __ptr_(__r.__ptr_),
2924 __cntrl_(__r.__cntrl_)
2925{
2926 if (__cntrl_)
2927 __cntrl_->__add_weak();
2928}
2929
2930template<class _Tp>
2931template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002932inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00002934 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002935 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936 : __ptr_(__r.__ptr_),
2937 __cntrl_(__r.__cntrl_)
2938{
2939 if (__cntrl_)
2940 __cntrl_->__add_weak();
2941}
2942
2943template<class _Tp>
2944template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002945inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00002947 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002948 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949 : __ptr_(__r.__ptr_),
2950 __cntrl_(__r.__cntrl_)
2951{
2952 if (__cntrl_)
2953 __cntrl_->__add_weak();
2954}
2955
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002956template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002957inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002958weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
2959 : __ptr_(__r.__ptr_),
2960 __cntrl_(__r.__cntrl_)
2961{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002962 __r.__ptr_ = nullptr;
2963 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002964}
2965
2966template<class _Tp>
2967template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002968inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002969weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
2970 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
2971 _NOEXCEPT
2972 : __ptr_(__r.__ptr_),
2973 __cntrl_(__r.__cntrl_)
2974{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002975 __r.__ptr_ = nullptr;
2976 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002977}
2978
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979template<class _Tp>
2980weak_ptr<_Tp>::~weak_ptr()
2981{
2982 if (__cntrl_)
2983 __cntrl_->__release_weak();
2984}
2985
2986template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002987inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002989weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990{
2991 weak_ptr(__r).swap(*this);
2992 return *this;
2993}
2994
2995template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002996template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002997inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002998typename enable_if
2999<
3000 is_convertible<_Yp*, _Tp*>::value,
3001 weak_ptr<_Tp>&
3002>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003003weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004{
3005 weak_ptr(__r).swap(*this);
3006 return *this;
3007}
3008
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003009template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003010inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003011weak_ptr<_Tp>&
3012weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3013{
3014 weak_ptr(_VSTD::move(__r)).swap(*this);
3015 return *this;
3016}
3017
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003019template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003020inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003021typename enable_if
3022<
3023 is_convertible<_Yp*, _Tp*>::value,
3024 weak_ptr<_Tp>&
3025>::type
3026weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3027{
3028 weak_ptr(_VSTD::move(__r)).swap(*this);
3029 return *this;
3030}
3031
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003032template<class _Tp>
3033template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003034inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003035typename enable_if
3036<
3037 is_convertible<_Yp*, _Tp*>::value,
3038 weak_ptr<_Tp>&
3039>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003040weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041{
3042 weak_ptr(__r).swap(*this);
3043 return *this;
3044}
3045
3046template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003047inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048void
Howard Hinnant719bda32011-05-28 14:41:13 +00003049weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003051 _VSTD::swap(__ptr_, __r.__ptr_);
3052 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053}
3054
3055template<class _Tp>
3056inline _LIBCPP_INLINE_VISIBILITY
3057void
Howard Hinnant719bda32011-05-28 14:41:13 +00003058swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059{
3060 __x.swap(__y);
3061}
3062
3063template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003064inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065void
Howard Hinnant719bda32011-05-28 14:41:13 +00003066weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067{
3068 weak_ptr().swap(*this);
3069}
3070
3071template<class _Tp>
3072template<class _Yp>
3073shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003074 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075 : __ptr_(__r.__ptr_),
3076 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3077{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003078 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003079 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080}
3081
3082template<class _Tp>
3083shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003084weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085{
3086 shared_ptr<_Tp> __r;
3087 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3088 if (__r.__cntrl_)
3089 __r.__ptr_ = __ptr_;
3090 return __r;
3091}
3092
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003093#if _LIBCPP_STD_VER > 14
3094template <class _Tp = void> struct owner_less;
3095#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003096template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003097#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098
3099template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003100struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003102{
3103 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003104 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003105 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003107 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003108 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003110 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003111 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003113};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114
3115template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003116struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3118{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003119 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003120 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003121 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003123 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003124 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003126 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003127 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 {return __x.owner_before(__y);}
3129};
3130
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003131#if _LIBCPP_STD_VER > 14
3132template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003133struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003134{
3135 template <class _Tp, class _Up>
3136 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003137 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003138 {return __x.owner_before(__y);}
3139 template <class _Tp, class _Up>
3140 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003141 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003142 {return __x.owner_before(__y);}
3143 template <class _Tp, class _Up>
3144 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003145 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003146 {return __x.owner_before(__y);}
3147 template <class _Tp, class _Up>
3148 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003149 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003150 {return __x.owner_before(__y);}
3151 typedef void is_transparent;
3152};
3153#endif
3154
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003156class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157{
3158 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003159protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003160 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003161 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003163 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003165 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3166 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003169public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003171 shared_ptr<_Tp> shared_from_this()
3172 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003174 shared_ptr<_Tp const> shared_from_this() const
3175 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176
Eric Fiselier84006862016-06-02 00:15:35 +00003177#if _LIBCPP_STD_VER > 14
3178 _LIBCPP_INLINE_VISIBILITY
3179 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3180 { return __weak_this_; }
3181
3182 _LIBCPP_INLINE_VISIBILITY
3183 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3184 { return __weak_this_; }
3185#endif // _LIBCPP_STD_VER > 14
3186
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 template <class _Up> friend class shared_ptr;
3188};
3189
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003190template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003191struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003192{
3193 typedef shared_ptr<_Tp> argument_type;
3194 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003195
Howard Hinnant756c69b2010-09-22 16:48:34 +00003196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003197 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003198 {
zoecarverd73f42a2020-05-11 18:42:50 -07003199 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003200 }
3201};
3202
Howard Hinnantc834c512011-11-29 18:15:50 +00003203template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003204inline _LIBCPP_INLINE_VISIBILITY
3205basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003206operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003207
Eric Fiselier9b492672016-06-18 02:12:53 +00003208
3209#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003210
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003211class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003212{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003213 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003214public:
3215 void lock() _NOEXCEPT;
3216 void unlock() _NOEXCEPT;
3217
3218private:
3219 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3220 __sp_mut(const __sp_mut&);
3221 __sp_mut& operator=(const __sp_mut&);
3222
Howard Hinnant8331b762013-03-06 23:30:19 +00003223 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003224};
3225
Mehdi Amini228053d2017-05-04 17:08:54 +00003226_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3227__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003228
3229template <class _Tp>
3230inline _LIBCPP_INLINE_VISIBILITY
3231bool
3232atomic_is_lock_free(const shared_ptr<_Tp>*)
3233{
3234 return false;
3235}
3236
3237template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003238_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003239shared_ptr<_Tp>
3240atomic_load(const shared_ptr<_Tp>* __p)
3241{
3242 __sp_mut& __m = __get_sp_mut(__p);
3243 __m.lock();
3244 shared_ptr<_Tp> __q = *__p;
3245 __m.unlock();
3246 return __q;
3247}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003248
Howard Hinnant9fa30202012-07-30 01:40:57 +00003249template <class _Tp>
3250inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003251_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003252shared_ptr<_Tp>
3253atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3254{
3255 return atomic_load(__p);
3256}
3257
3258template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003259_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003260void
3261atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3262{
3263 __sp_mut& __m = __get_sp_mut(__p);
3264 __m.lock();
3265 __p->swap(__r);
3266 __m.unlock();
3267}
3268
3269template <class _Tp>
3270inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003271_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003272void
3273atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3274{
3275 atomic_store(__p, __r);
3276}
3277
3278template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003279_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003280shared_ptr<_Tp>
3281atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3282{
3283 __sp_mut& __m = __get_sp_mut(__p);
3284 __m.lock();
3285 __p->swap(__r);
3286 __m.unlock();
3287 return __r;
3288}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003289
Howard Hinnant9fa30202012-07-30 01:40:57 +00003290template <class _Tp>
3291inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003292_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003293shared_ptr<_Tp>
3294atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3295{
3296 return atomic_exchange(__p, __r);
3297}
3298
3299template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003300_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003301bool
3302atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3303{
Marshall Clow4201ee82016-05-18 17:50:13 +00003304 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003305 __sp_mut& __m = __get_sp_mut(__p);
3306 __m.lock();
3307 if (__p->__owner_equivalent(*__v))
3308 {
Marshall Clow4201ee82016-05-18 17:50:13 +00003309 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003310 *__p = __w;
3311 __m.unlock();
3312 return true;
3313 }
Marshall Clow4201ee82016-05-18 17:50:13 +00003314 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003315 *__v = *__p;
3316 __m.unlock();
3317 return false;
3318}
3319
3320template <class _Tp>
3321inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003322_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003323bool
3324atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3325{
3326 return atomic_compare_exchange_strong(__p, __v, __w);
3327}
3328
3329template <class _Tp>
3330inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003331_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003332bool
3333atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3334 shared_ptr<_Tp> __w, memory_order, memory_order)
3335{
3336 return atomic_compare_exchange_strong(__p, __v, __w);
3337}
3338
3339template <class _Tp>
3340inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003341_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003342bool
3343atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3344 shared_ptr<_Tp> __w, memory_order, memory_order)
3345{
3346 return atomic_compare_exchange_weak(__p, __v, __w);
3347}
3348
Eric Fiselier9b492672016-06-18 02:12:53 +00003349#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003350
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003351//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00003352#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
3353# ifndef _LIBCPP_CXX03_LANG
3354enum class pointer_safety : unsigned char {
3355 relaxed,
3356 preferred,
3357 strict
3358};
3359# endif
3360#else
Howard Hinnant8331b762013-03-06 23:30:19 +00003361struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003363 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364 {
3365 relaxed,
3366 preferred,
3367 strict
3368 };
3369
Howard Hinnant49e145e2012-10-30 19:06:59 +00003370 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371
Howard Hinnant756c69b2010-09-22 16:48:34 +00003372 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00003373 pointer_safety() : __v_() {}
3374
3375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00003376 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378 operator int() const {return __v_;}
3379};
Eric Fiselierab6bb302017-01-05 01:15:42 +00003380#endif
3381
3382#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003383 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00003384_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
3385#else
3386// This function is only offered in C++03 under ABI v1.
3387# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
3388inline _LIBCPP_INLINE_VISIBILITY
3389pointer_safety get_pointer_safety() _NOEXCEPT {
3390 return pointer_safety::relaxed;
3391}
3392# endif
3393#endif
3394
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003396_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
3397_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
3398_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003399_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400
3401template <class _Tp>
3402inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003403_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404undeclare_reachable(_Tp* __p)
3405{
3406 return static_cast<_Tp*>(__undeclare_reachable(__p));
3407}
3408
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003409_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410
Marshall Clow8982dcd2015-07-13 20:04:56 +00003411// --- Helper for container swap --
3412template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00003413_LIBCPP_INLINE_VISIBILITY
3414void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
3415#if _LIBCPP_STD_VER >= 14
3416 _NOEXCEPT
3417#else
3418 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3419#endif
3420{
3421 using _VSTD::swap;
3422 swap(__a1, __a2);
3423}
3424
3425template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00003426inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00003427void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
3428
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003429template <typename _Alloc>
3430inline _LIBCPP_INLINE_VISIBILITY
3431void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
3432#if _LIBCPP_STD_VER >= 14
3433 _NOEXCEPT
3434#else
3435 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3436#endif
3437{
3438 _VSTD::__swap_allocator(__a1, __a2,
3439 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
3440}
3441
Marshall Clowff91de82015-08-18 19:51:37 +00003442template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00003443struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00003444 _Traits::propagate_on_container_move_assignment::value
3445#if _LIBCPP_STD_VER > 14
3446 || _Traits::is_always_equal::value
3447#else
3448 && is_nothrow_move_assignable<_Alloc>::value
3449#endif
3450 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00003451
Marshall Clowa591b9a2016-07-11 21:38:08 +00003452
Marshall Clowa591b9a2016-07-11 21:38:08 +00003453template <class _Tp, class _Alloc>
3454struct __temp_value {
3455 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00003456
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00003457 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00003458 _Alloc &__a;
3459
3460 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
3461 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003462
Marshall Clowa591b9a2016-07-11 21:38:08 +00003463 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00003464 _LIBCPP_NO_CFI
3465 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
3466 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
3467 _VSTD::forward<_Args>(__args)...);
3468 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003469
Marshall Clowa591b9a2016-07-11 21:38:08 +00003470 ~__temp_value() { _Traits::destroy(__a, __addr()); }
3471 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00003472
Marshall Clowe46031a2018-07-02 18:41:15 +00003473template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00003474struct __is_allocator : false_type {};
3475
3476template<typename _Alloc>
3477struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00003478 typename __void_t<typename _Alloc::value_type>::type,
3479 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
3480 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00003481 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00003482
Eric Fiselier74ebee62019-06-08 01:31:19 +00003483// __builtin_new_allocator -- A non-templated helper for allocating and
3484// deallocating memory using __builtin_operator_new and
3485// __builtin_operator_delete. It should be used in preference to
3486// `std::allocator<T>` to avoid additional instantiations.
3487struct __builtin_new_allocator {
3488 struct __builtin_new_deleter {
3489 typedef void* pointer_type;
3490
3491 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
3492 : __size_(__size), __align_(__align) {}
3493
3494 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003495 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003496 }
3497
3498 private:
3499 size_t __size_;
3500 size_t __align_;
3501 };
3502
3503 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
3504
3505 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003506 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00003507 __builtin_new_deleter(__s, __align));
3508 }
3509
3510 static void __deallocate_bytes(void* __p, size_t __s,
3511 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003512 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003513 }
3514
3515 template <class _Tp>
3516 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3517 static __holder_t __allocate_type(size_t __n) {
3518 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3519 }
3520
3521 template <class _Tp>
3522 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3523 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
3524 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3525 }
3526};
3527
3528
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529_LIBCPP_END_NAMESPACE_STD
3530
Eric Fiselierf4433a32017-05-31 22:07:49 +00003531_LIBCPP_POP_MACROS
3532
Louis Dionne59d0b3c2019-08-05 18:29:14 +00003533#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00003534# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00003535#endif
3536
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537#endif // _LIBCPP_MEMORY