blob: c676cd01c0f51025cfe207be762c50ace404ebae [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MEMORY
11#define _LIBCPP_MEMORY
12
13/*
14 memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000020inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27 typedef Ptr pointer;
28 typedef <details> element_type;
29 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000030
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000032
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 static pointer pointer_to(<details>);
34};
35
Howard Hinnant719bda32011-05-28 14:41:13 +000036template <class T>
37struct pointer_traits<T*>
38{
39 typedef T* pointer;
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
42
43 template <class U> using rebind = U*;
44
Louis Dionne44e1ea82018-11-13 17:04:05 +000045 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000046};
47
Eric Fiselier45c9aac2017-11-22 19:49:21 +000048template <class T> constexpr T* to_address(T* p) noexcept; // C++20
Marek Kurdej1f0cde92020-12-06 15:23:46 +010049template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
Eric Fiselier45c9aac2017-11-22 19:49:21 +000050
Howard Hinnantc51e1022010-05-11 19:42:16 +000051template <class Alloc>
52struct allocator_traits
53{
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
56 value_type;
57
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
61 const_pointer;
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
64 void_pointer;
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
67 const_void_pointer;
68 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000069 | pointer_traits<pointer>::difference_type
70 difference_type;
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
73 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000080 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Michael Park99f9e912020-03-04 11:27:14 -050083 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
Louis Dionne99f59432020-09-17 12:06:13 -040086 static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Louis Dionne99f59432020-09-17 12:06:13 -040089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
Louis Dionne99f59432020-09-17 12:06:13 -040092 static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000093
94 template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -040095 static void destroy(allocator_type& a, T* p); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
Louis Dionne99f59432020-09-17 12:06:13 -040097 static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
98 static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000099};
100
101template <>
Michael Park99f9e912020-03-04 11:27:14 -0500102class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
Louis Dionnec0056af2020-08-28 12:31:16 -0400116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef T* pointer; // deprecated in C++17, removed in C++20
119 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
120 typedef typename add_lvalue_reference<T>::type
121 reference; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<const T>::type
123 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
Michael Park99f9e912020-03-04 11:27:14 -0500125 typedef T value_type;
126
127 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
128
129 typedef true_type propagate_on_container_move_assignment;
130 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131
Marshall Clowbc759762018-03-20 23:02:53 +0000132 constexpr allocator() noexcept; // constexpr in C++20
133 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
134 template <class U>
135 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400136 ~allocator(); // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500137 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
138 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
139 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400140 T* allocate(size_t n); // constexpr in C++20
141 void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500142 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000143 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500144 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147};
148
149template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400150bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151
152template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400153bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
155template <class OutputIterator, class T>
156class raw_storage_iterator
157 : public iterator<output_iterator_tag,
158 T, // purposefully not C++03
159 ptrdiff_t, // purposefully not C++03
160 T*, // purposefully not C++03
161 raw_storage_iterator&> // purposefully not C++03
162{
163public:
164 explicit raw_storage_iterator(OutputIterator x);
165 raw_storage_iterator& operator*();
166 raw_storage_iterator& operator=(const T& element);
167 raw_storage_iterator& operator++();
168 raw_storage_iterator operator++(int);
169};
170
Howard Hinnant719bda32011-05-28 14:41:13 +0000171template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
172template <class T> void return_temporary_buffer(T* p) noexcept;
173
174template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000175template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176
177template <class InputIterator, class ForwardIterator>
178ForwardIterator
179uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
180
Howard Hinnant719bda32011-05-28 14:41:13 +0000181template <class InputIterator, class Size, class ForwardIterator>
182ForwardIterator
183uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
184
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185template <class ForwardIterator, class T>
186void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
187
188template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000189ForwardIterator
190uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Louis Dionne99f59432020-09-17 12:06:13 -0400192template <class T, class ...Args>
193constexpr T* construct_at(T* location, Args&& ...args); // since C++20
194
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000195template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -0400196void destroy_at(T* location); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000197
198template <class ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -0400199void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000200
201template <class ForwardIterator, class Size>
Louis Dionne99f59432020-09-17 12:06:13 -0400202ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000203
204template <class InputIterator, class ForwardIterator>
205 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
206
207template <class InputIterator, class Size, class ForwardIterator>
208 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
209
210template <class ForwardIterator>
211 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
212
213template <class ForwardIterator, class Size>
214 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
215
216template <class ForwardIterator>
217 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
218
219template <class ForwardIterator, class Size>
220 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
221
Louis Dionne481a2662018-09-23 18:35:00 +0000222template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223
224template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000225class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226{
227public:
228 typedef X element_type;
229
230 explicit auto_ptr(X* p =0) throw();
231 auto_ptr(auto_ptr&) throw();
232 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
233 auto_ptr& operator=(auto_ptr&) throw();
234 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
235 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
236 ~auto_ptr() throw();
237
238 typename add_lvalue_reference<X>::type operator*() const throw();
239 X* operator->() const throw();
240 X* get() const throw();
241 X* release() throw();
242 void reset(X* p =0) throw();
243
244 auto_ptr(auto_ptr_ref<X>) throw();
245 template<class Y> operator auto_ptr_ref<Y>() throw();
246 template<class Y> operator auto_ptr<Y>() throw();
247};
248
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000249template <class T>
250struct default_delete
251{
Howard Hinnant719bda32011-05-28 14:41:13 +0000252 constexpr default_delete() noexcept = default;
253 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000254
Howard Hinnant719bda32011-05-28 14:41:13 +0000255 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000256};
257
258template <class T>
259struct default_delete<T[]>
260{
Howard Hinnant719bda32011-05-28 14:41:13 +0000261 constexpr default_delete() noexcept = default;
262 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000263 template <class U> void operator()(U*) const = delete;
264};
265
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000266template <class T, class D = default_delete<T>>
267class unique_ptr
268{
269public:
270 typedef see below pointer;
271 typedef T element_type;
272 typedef D deleter_type;
273
274 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000275 constexpr unique_ptr() noexcept;
276 explicit unique_ptr(pointer p) noexcept;
277 unique_ptr(pointer p, see below d1) noexcept;
278 unique_ptr(pointer p, see below d2) noexcept;
279 unique_ptr(unique_ptr&& u) noexcept;
280 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000281 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000283 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000284 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // destructor
287 ~unique_ptr();
288
289 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000290 unique_ptr& operator=(unique_ptr&& u) noexcept;
291 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
292 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // observers
295 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000296 pointer operator->() const noexcept;
297 pointer get() const noexcept;
298 deleter_type& get_deleter() noexcept;
299 const deleter_type& get_deleter() const noexcept;
300 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000301
302 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000303 pointer release() noexcept;
304 void reset(pointer p = pointer()) noexcept;
305 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000306};
307
308template <class T, class D>
309class unique_ptr<T[], D>
310{
311public:
312 typedef implementation-defined pointer;
313 typedef T element_type;
314 typedef D deleter_type;
315
316 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 constexpr unique_ptr() noexcept;
318 explicit unique_ptr(pointer p) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(pointer p, see below d) noexcept;
321 unique_ptr(unique_ptr&& u) noexcept;
322 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000323
324 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000325 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 unique_ptr& operator=(unique_ptr&& u) noexcept;
329 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000330
331 // observers
332 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000333 pointer get() const noexcept;
334 deleter_type& get_deleter() noexcept;
335 const deleter_type& get_deleter() const noexcept;
336 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 pointer release() noexcept;
340 void reset(pointer p = pointer()) noexcept;
341 void reset(nullptr_t) noexcept;
Vy Nguyene369bd92020-07-13 12:34:37 -0400342 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000343 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000344};
345
346template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000347 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000348
349template <class T1, class D1, class T2, class D2>
350 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353template <class T1, class D1, class T2, class D2>
354 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
355template <class T1, class D1, class T2, class D2>
356 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
357template <class T1, class D1, class T2, class D2>
358 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
359template <class T1, class D1, class T2, class D2>
360 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
361
Howard Hinnant719bda32011-05-28 14:41:13 +0000362template <class T, class D>
363 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
364template <class T, class D>
365 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
366template <class T, class D>
367 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
368template <class T, class D>
369 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
370
371template <class T, class D>
372 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
379template <class T, class D>
380 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
381template <class T, class D>
382 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
383template <class T, class D>
384 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
385template <class T, class D>
386 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
387
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000388class bad_weak_ptr
389 : public std::exception
390{
Howard Hinnant719bda32011-05-28 14:41:13 +0000391 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000392};
393
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000394template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
395template<class T> unique_ptr<T> make_unique(size_t n); // C++14
396template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
397
Marshall Clowe52a3242017-11-27 15:51:36 +0000398template<class E, class T, class Y, class D>
399 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
400
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000401template<class T>
402class shared_ptr
403{
404public:
405 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000406 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000407
408 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000409 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000410 template<class Y> explicit shared_ptr(Y* p);
411 template<class Y, class D> shared_ptr(Y* p, D d);
412 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
413 template <class D> shared_ptr(nullptr_t p, D d);
414 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
416 shared_ptr(const shared_ptr& r) noexcept;
417 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
418 shared_ptr(shared_ptr&& r) noexcept;
419 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000421 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000422 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
423 shared_ptr(nullptr_t) : shared_ptr() { }
424
425 // destructor:
426 ~shared_ptr();
427
428 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 shared_ptr& operator=(const shared_ptr& r) noexcept;
430 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
431 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000432 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000433 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000434 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
435
436 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000437 void swap(shared_ptr& r) noexcept;
438 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000439 template<class Y> void reset(Y* p);
440 template<class Y, class D> void reset(Y* p, D d);
441 template<class Y, class D, class A> void reset(Y* p, D d, A a);
442
Howard Hinnant719bda32011-05-28 14:41:13 +0000443 // observers:
444 T* get() const noexcept;
445 T& operator*() const noexcept;
446 T* operator->() const noexcept;
447 long use_count() const noexcept;
448 bool unique() const noexcept;
449 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000450 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
451 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000452};
453
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400454template<class T>
455shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
456template<class T, class D>
457shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
458
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000459// shared_ptr comparisons:
460template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000461 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000462template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000463 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000464template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000465 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000466template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000467 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000468template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000469 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000470template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000471 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
472
473template <class T>
474 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
481template <class T>
482 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
483template <class T>
484bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
485template <class T>
486 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
487template <class T>
488 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
489template <class T>
490 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
491template <class T>
492 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
493template <class T>
494 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
495template <class T>
496 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000497
498// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000499template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500
501// shared_ptr casts:
502template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000503 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000504template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000505 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000506template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000507 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000508
509// shared_ptr I/O:
510template<class E, class T, class Y>
511 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
512
513// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000514template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516template<class T, class... Args>
517 shared_ptr<T> make_shared(Args&&... args);
518template<class T, class A, class... Args>
519 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
520
521template<class T>
522class weak_ptr
523{
524public:
525 typedef T element_type;
526
527 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000528 constexpr weak_ptr() noexcept;
529 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
530 weak_ptr(weak_ptr const& r) noexcept;
531 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000532 weak_ptr(weak_ptr&& r) noexcept; // C++14
533 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000534
535 // destructor
536 ~weak_ptr();
537
538 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000539 weak_ptr& operator=(weak_ptr const& r) noexcept;
540 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
541 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000542 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
543 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000544
545 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000546 void swap(weak_ptr& r) noexcept;
547 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000548
549 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000550 long use_count() const noexcept;
551 bool expired() const noexcept;
552 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000553 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
554 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000555};
556
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400557template<class T>
558weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
559
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000560// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000561template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000562
563// class owner_less:
564template<class T> struct owner_less;
565
566template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000567struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000568 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
569{
570 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000571 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
572 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000574};
575
576template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000577struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000578 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
579{
580 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000581 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
583 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
584};
585
586template <> // Added in C++14
587struct owner_less<void>
588{
589 template <class _Tp, class _Up>
590 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
591 template <class _Tp, class _Up>
592 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
593 template <class _Tp, class _Up>
594 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
595 template <class _Tp, class _Up>
596 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
597
598 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599};
600
601template<class T>
602class enable_shared_from_this
603{
604protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000605 constexpr enable_shared_from_this() noexcept;
606 enable_shared_from_this(enable_shared_from_this const&) noexcept;
607 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000608 ~enable_shared_from_this();
609public:
610 shared_ptr<T> shared_from_this();
611 shared_ptr<T const> shared_from_this() const;
612};
613
614template<class T>
615 bool atomic_is_lock_free(const shared_ptr<T>* p);
616template<class T>
617 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
618template<class T>
619 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
620template<class T>
621 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
622template<class T>
623 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
624template<class T>
625 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
626template<class T>
627 shared_ptr<T>
628 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
629template<class T>
630 bool
631 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
632template<class T>
633 bool
634 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
635template<class T>
636 bool
637 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
638 shared_ptr<T> w, memory_order success,
639 memory_order failure);
640template<class T>
641 bool
642 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
643 shared_ptr<T> w, memory_order success,
644 memory_order failure);
645// Hash support
646template <class T> struct hash;
647template <class T, class D> struct hash<unique_ptr<T, D> >;
648template <class T> struct hash<shared_ptr<T> >;
649
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000650template <class T, class Alloc>
651 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
652
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000653// Pointer safety
654enum class pointer_safety { relaxed, preferred, strict };
655void declare_reachable(void *p);
656template <class T> T *undeclare_reachable(T *p);
657void declare_no_pointers(char *p, size_t n);
658void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000659pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000660
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
662
663} // std
664
665*/
666
667#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500668#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669#include <type_traits>
670#include <typeinfo>
671#include <cstddef>
672#include <cstdint>
673#include <new>
674#include <utility>
675#include <limits>
676#include <iterator>
677#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000678#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000679#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000680#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000681#include <cstring>
Louis Dionnefa621d72020-12-10 18:28:13 -0500682#include <__memory/allocator_traits.h>
683#include <__memory/base.h>
684#include <__memory/pointer_traits.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500685#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000686#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000687# include <atomic>
688#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000689#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000690
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000691#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000693#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694
Eric Fiselierf4433a32017-05-31 22:07:49 +0000695_LIBCPP_PUSH_MACROS
696#include <__undef_macros>
697
698
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699_LIBCPP_BEGIN_NAMESPACE_STD
700
Eric Fiselier89659d12015-07-07 00:27:16 +0000701template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000702inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000703_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
704#if !defined(_LIBCPP_HAS_NO_THREADS) && \
705 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000706 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000707 return __atomic_load_n(__value, __ATOMIC_RELAXED);
708#else
709 return *__value;
710#endif
711}
712
Kuba Breckade9d6792016-09-04 09:55:12 +0000713template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000714inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000715_ValueType __libcpp_acquire_load(_ValueType const* __value) {
716#if !defined(_LIBCPP_HAS_NO_THREADS) && \
717 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000718 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000719 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
720#else
721 return *__value;
722#endif
723}
724
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500725template <bool _UsePointerTraits> struct __to_address_helper;
726
727template <> struct __to_address_helper<true> {
728 template <class _Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500729 using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500730
731 template <class _Pointer>
732 _LIBCPP_CONSTEXPR
733 static __return_type<_Pointer>
734 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
735};
736
737template <class _Pointer, bool _Dummy = true>
738using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
739
740
Howard Hinnantc834c512011-11-29 18:15:50 +0000741template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000742inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +0000743_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500744__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745{
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500746 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 return __p;
748}
749
750template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500751inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
752typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
753__to_address(const _Pointer& __p) _NOEXCEPT {
754 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000755}
756
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500757template <> struct __to_address_helper<false> {
758 template <class _Pointer>
759 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000760
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500761 template <class _Pointer>
762 _LIBCPP_CONSTEXPR
763 static __return_type<_Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500764 __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500765};
766
767
768#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000769template <class _Tp>
770inline _LIBCPP_INLINE_VISIBILITY constexpr
771_Tp*
772to_address(_Tp* __p) _NOEXCEPT
773{
774 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
775 return __p;
776}
777
778template <class _Pointer>
Marek Kurdej1f0cde92020-12-06 15:23:46 +0100779inline _LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000780auto
781to_address(const _Pointer& __p) _NOEXCEPT
782{
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500783 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000784}
785#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786
Louis Dionnefa621d72020-12-10 18:28:13 -0500787template <class _Tp> class allocator;
Marshall Clow0be70c32017-06-14 21:23:57 +0000788
Louis Dionnefa621d72020-12-10 18:28:13 -0500789#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
790template <>
791class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793public:
Louis Dionnefa621d72020-12-10 18:28:13 -0500794 typedef void* pointer;
795 typedef const void* const_pointer;
796 typedef void value_type;
797
798 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799};
800
Louis Dionnefa621d72020-12-10 18:28:13 -0500801template <>
802class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803{
Louis Dionnefa621d72020-12-10 18:28:13 -0500804public:
805 typedef const void* pointer;
806 typedef const void* const_pointer;
807 typedef const void value_type;
808
809 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810};
Louis Dionne99f59432020-09-17 12:06:13 -0400811#endif
Marshall Clow940e01c2015-04-07 05:21:38 +0000812
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813// allocator
814
815template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000816class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817{
818public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400819 typedef size_t size_type;
820 typedef ptrdiff_t difference_type;
821 typedef _Tp value_type;
822 typedef true_type propagate_on_container_move_assignment;
823 typedef true_type is_always_equal;
824
825 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
826 allocator() _NOEXCEPT { }
827
828 template <class _Up>
829 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
830 allocator(const allocator<_Up>&) _NOEXCEPT { }
831
832 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
833 _Tp* allocate(size_t __n) {
834 if (__n > allocator_traits<allocator>::max_size(*this))
835 __throw_length_error("allocator<T>::allocate(size_t n)"
836 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400837 if (__libcpp_is_constant_evaluated()) {
838 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
839 } else {
840 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
841 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400842 }
843
844 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
845 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400846 if (__libcpp_is_constant_evaluated()) {
847 ::operator delete(__p);
848 } else {
849 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
850 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400851 }
852
853 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500854#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -0500855 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
856 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
857 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
858 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
859
Louis Dionne481a2662018-09-23 18:35:00 +0000860 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400861 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
862 typedef allocator<_Up> other;
863 };
Marshall Clowbc759762018-03-20 23:02:53 +0000864
Michael Park99f9e912020-03-04 11:27:14 -0500865 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400866 pointer address(reference __x) const _NOEXCEPT {
867 return _VSTD::addressof(__x);
868 }
Michael Park99f9e912020-03-04 11:27:14 -0500869 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400870 const_pointer address(const_reference __x) const _NOEXCEPT {
871 return _VSTD::addressof(__x);
872 }
Michael Park99f9e912020-03-04 11:27:14 -0500873
Michael Park99f9e912020-03-04 11:27:14 -0500874 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400875 _Tp* allocate(size_t __n, const void*) {
876 return allocate(__n);
877 }
Michael Park99f9e912020-03-04 11:27:14 -0500878
Louis Dionnef8b6c022020-09-21 10:36:37 -0400879 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
880 return size_type(~0) / sizeof(_Tp);
881 }
Michael Park99f9e912020-03-04 11:27:14 -0500882
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400884 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
885 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500886 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400887 }
888
889 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
890 void destroy(pointer __p) {
891 __p->~_Tp();
892 }
Michael Park99f9e912020-03-04 11:27:14 -0500893#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894};
895
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000896template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000897class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000898{
899public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400900 typedef size_t size_type;
901 typedef ptrdiff_t difference_type;
902 typedef const _Tp value_type;
903 typedef true_type propagate_on_container_move_assignment;
904 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000905
Marshall Clowbc759762018-03-20 23:02:53 +0000906 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400907 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +0000908
909 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +0000910 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400911 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -0500912
Louis Dionne99f59432020-09-17 12:06:13 -0400913 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400914 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -0400915 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +0000916 __throw_length_error("allocator<const T>::allocate(size_t n)"
917 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400918 if (__libcpp_is_constant_evaluated()) {
919 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
920 } else {
921 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
922 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000923 }
Michael Park99f9e912020-03-04 11:27:14 -0500924
Louis Dionne99f59432020-09-17 12:06:13 -0400925 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400926 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400927 if (__libcpp_is_constant_evaluated()) {
928 ::operator delete(const_cast<_Tp*>(__p));
929 } else {
930 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
931 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400932 }
Michael Park99f9e912020-03-04 11:27:14 -0500933
Louis Dionnef8b6c022020-09-21 10:36:37 -0400934 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500935#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -0400936 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
937 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
938 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
939 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
940
941 template <class _Up>
942 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
943 typedef allocator<_Up> other;
944 };
945
946 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
947 const_pointer address(const_reference __x) const _NOEXCEPT {
948 return _VSTD::addressof(__x);
949 }
950
951 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
952 const _Tp* allocate(size_t __n, const void*) {
953 return allocate(__n);
954 }
955
956 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
957 return size_type(~0) / sizeof(_Tp);
958 }
Michael Park99f9e912020-03-04 11:27:14 -0500959
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000960 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400961 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
962 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500963 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400964 }
Howard Hinnant9e028f12012-05-01 15:37:54 +0000965
Louis Dionnef8b6c022020-09-21 10:36:37 -0400966 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
967 void destroy(pointer __p) {
968 __p->~_Tp();
969 }
Michael Park99f9e912020-03-04 11:27:14 -0500970#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000971};
972
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400974inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000975bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976
977template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400978inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000979bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980
Louis Dionned6651542020-11-03 12:05:55 -0500981template <class _Alloc, class _Ptr>
982_LIBCPP_INLINE_VISIBILITY
983void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
984 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500985 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500986 typedef allocator_traits<_Alloc> _Traits;
987 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
988 _Traits::construct(__a, _VSTD::__to_address(__begin2),
989#ifdef _LIBCPP_NO_EXCEPTIONS
990 _VSTD::move(*__begin1)
991#else
992 _VSTD::move_if_noexcept(*__begin1)
993#endif
994 );
995 }
996}
997
998template <class _Alloc, class _Tp, typename enable_if<
999 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1000 is_trivially_move_constructible<_Tp>::value
1001>::type>
1002_LIBCPP_INLINE_VISIBILITY
1003void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1004 ptrdiff_t _Np = __end1 - __begin1;
1005 if (_Np > 0) {
1006 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1007 __begin2 += _Np;
1008 }
1009}
1010
1011template <class _Alloc, class _Iter, class _Ptr>
1012_LIBCPP_INLINE_VISIBILITY
1013void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1014 typedef allocator_traits<_Alloc> _Traits;
1015 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1016 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1017 }
1018}
1019
1020template <class _Alloc, class _Source, class _Dest,
1021 class _RawSource = typename remove_const<_Source>::type,
1022 class _RawDest = typename remove_const<_Dest>::type,
1023 class =
1024 typename enable_if<
1025 is_trivially_copy_constructible<_Dest>::value &&
1026 is_same<_RawSource, _RawDest>::value &&
1027 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1028 >::type>
1029_LIBCPP_INLINE_VISIBILITY
1030void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1031 ptrdiff_t _Np = __end1 - __begin1;
1032 if (_Np > 0) {
1033 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1034 __begin2 += _Np;
1035 }
1036}
1037
1038template <class _Alloc, class _Ptr>
1039_LIBCPP_INLINE_VISIBILITY
1040void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1041 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1042 "The specified type does not meet the requirements of Cpp17MoveInsertable");
1043 typedef allocator_traits<_Alloc> _Traits;
1044 while (__end1 != __begin1) {
1045 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1046#ifdef _LIBCPP_NO_EXCEPTIONS
1047 _VSTD::move(*--__end1)
1048#else
1049 _VSTD::move_if_noexcept(*--__end1)
1050#endif
1051 );
1052 --__end2;
1053 }
1054}
1055
1056template <class _Alloc, class _Tp, class = typename enable_if<
1057 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1058 is_trivially_move_constructible<_Tp>::value
1059>::type>
1060_LIBCPP_INLINE_VISIBILITY
1061void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1062 ptrdiff_t _Np = __end1 - __begin1;
1063 __end2 -= _Np;
1064 if (_Np > 0)
1065 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1066}
1067
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001069class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 : public iterator<output_iterator_tag,
1071 _Tp, // purposefully not C++03
1072 ptrdiff_t, // purposefully not C++03
1073 _Tp*, // purposefully not C++03
1074 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1075{
1076private:
1077 _OutputIterator __x_;
1078public:
1079 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1080 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1081 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001082 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001083#if _LIBCPP_STD_VER >= 14
1084 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001085 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001086#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1088 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1089 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001090#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001091 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001092#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093};
1094
1095template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001096_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001098get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099{
1100 pair<_Tp*, ptrdiff_t> __r(0, 0);
1101 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1102 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1103 / sizeof(_Tp);
1104 if (__n > __m)
1105 __n = __m;
1106 while (__n > 0)
1107 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001108#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001109 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001110 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001111 align_val_t __al =
1112 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001113 __r.first = static_cast<_Tp*>(::operator new(
1114 __n * sizeof(_Tp), __al, nothrow));
1115 } else {
1116 __r.first = static_cast<_Tp*>(::operator new(
1117 __n * sizeof(_Tp), nothrow));
1118 }
1119#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001120 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001121 {
1122 // Since aligned operator new is unavailable, return an empty
1123 // buffer rather than one with invalid alignment.
1124 return __r;
1125 }
1126
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001128#endif
1129
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 if (__r.first)
1131 {
1132 __r.second = __n;
1133 break;
1134 }
1135 __n /= 2;
1136 }
1137 return __r;
1138}
1139
1140template <class _Tp>
1141inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001142void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1143{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001144 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001145}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146
Marshall Clowb22274f2017-01-24 22:22:33 +00001147#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001149struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
1151 _Tp* __ptr_;
1152};
1153
1154template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001155class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156{
1157private:
1158 _Tp* __ptr_;
1159public:
1160 typedef _Tp element_type;
1161
Dimitry Andric47269ce2020-03-13 19:36:26 +01001162 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1163 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1164 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001166 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001168 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001170 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001172 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173
Dimitry Andric47269ce2020-03-13 19:36:26 +01001174 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001176 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1177 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1178 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 {
1180 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001181 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 return __t;
1183 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001184 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 {
1186 if (__ptr_ != __p)
1187 delete __ptr_;
1188 __ptr_ = __p;
1189 }
1190
Dimitry Andric47269ce2020-03-13 19:36:26 +01001191 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1192 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001194 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 {return auto_ptr<_Up>(release());}
1196};
1197
1198template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001199class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200{
1201public:
1202 typedef void element_type;
1203};
Marshall Clowb22274f2017-01-24 22:22:33 +00001204#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001206// Tag used to default initialize one or both of the pair's elements.
1207struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001208struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001209
Eric Fiselier9d355982017-04-12 23:45:53 +00001210template <class _Tp, int _Idx,
1211 bool _CanBeEmptyBase =
1212 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1213struct __compressed_pair_elem {
1214 typedef _Tp _ParamT;
1215 typedef _Tp& reference;
1216 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001218 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001219 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001220 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1221 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222
Eric Fiselier9d355982017-04-12 23:45:53 +00001223 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001224 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1225 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001226 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001227 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001228 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001229 : __value_(_VSTD::forward<_Up>(__u))
1230 {
1231 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001233
1234#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001235 template <class... _Args, size_t... _Indexes>
1236 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1237 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1238 __tuple_indices<_Indexes...>)
1239 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001242
Alex Lorenz76132112017-11-09 17:54:49 +00001243 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1244 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001245 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001248 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249};
1250
Eric Fiselier9d355982017-04-12 23:45:53 +00001251template <class _Tp, int _Idx>
1252struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1253 typedef _Tp _ParamT;
1254 typedef _Tp& reference;
1255 typedef const _Tp& const_reference;
1256 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001258 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1259 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1260 __compressed_pair_elem(__default_init_tag) {}
1261 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1262 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263
Eric Fiselier9d355982017-04-12 23:45:53 +00001264 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001265 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1266 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001267 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001268 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001269 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001270 : __value_type(_VSTD::forward<_Up>(__u))
1271 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001273#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001274 template <class... _Args, size_t... _Indexes>
1275 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1276 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1277 __tuple_indices<_Indexes...>)
1278 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001279#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280
Alex Lorenz76132112017-11-09 17:54:49 +00001281 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1282 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001283 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284};
1285
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00001287class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1288 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001289 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1290 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00001291
1292 // NOTE: This static assert should never fire because __compressed_pair
1293 // is *almost never* used in a scenario where it's possible for T1 == T2.
1294 // (The exception is std::function where it is possible that the function
1295 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00001296 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05001297 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00001298 "The current implementation is NOT ABI-compatible with the previous "
1299 "implementation for this configuration");
1300
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001302 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00001303 class = typename enable_if<
1304 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1305 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1306 >::type
1307 >
Eric Fiselier9d355982017-04-12 23:45:53 +00001308 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001309 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310
Eric Fiselier9d355982017-04-12 23:45:53 +00001311 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001312 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00001313 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001314 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001316#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001317 template <class... _Args1, class... _Args2>
1318 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1319 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1320 tuple<_Args2...> __second_args)
1321 : _Base1(__pc, _VSTD::move(__first_args),
1322 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1323 _Base2(__pc, _VSTD::move(__second_args),
1324 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001325#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326
Eric Fiselier9d355982017-04-12 23:45:53 +00001327 _LIBCPP_INLINE_VISIBILITY
1328 typename _Base1::reference first() _NOEXCEPT {
1329 return static_cast<_Base1&>(*this).__get();
1330 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
Eric Fiselier9d355982017-04-12 23:45:53 +00001332 _LIBCPP_INLINE_VISIBILITY
1333 typename _Base1::const_reference first() const _NOEXCEPT {
1334 return static_cast<_Base1 const&>(*this).__get();
1335 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336
Eric Fiselier9d355982017-04-12 23:45:53 +00001337 _LIBCPP_INLINE_VISIBILITY
1338 typename _Base2::reference second() _NOEXCEPT {
1339 return static_cast<_Base2&>(*this).__get();
1340 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341
Eric Fiselier9d355982017-04-12 23:45:53 +00001342 _LIBCPP_INLINE_VISIBILITY
1343 typename _Base2::const_reference second() const _NOEXCEPT {
1344 return static_cast<_Base2 const&>(*this).__get();
1345 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
Eric Fiselier9d355982017-04-12 23:45:53 +00001347 _LIBCPP_INLINE_VISIBILITY
1348 void swap(__compressed_pair& __x)
1349 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1350 __is_nothrow_swappable<_T2>::value)
1351 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001352 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00001353 swap(first(), __x.first());
1354 swap(second(), __x.second());
1355 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356};
1357
1358template <class _T1, class _T2>
1359inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001360void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1361 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1362 __is_nothrow_swappable<_T2>::value) {
1363 __x.swap(__y);
1364}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001366// default_delete
1367
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001369struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001370 static_assert(!is_function<_Tp>::value,
1371 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001372#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001373 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001374#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001375 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001376#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001377 template <class _Up>
1378 _LIBCPP_INLINE_VISIBILITY
1379 default_delete(const default_delete<_Up>&,
1380 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1381 0) _NOEXCEPT {}
1382
1383 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1384 static_assert(sizeof(_Tp) > 0,
1385 "default_delete can not delete incomplete type");
1386 static_assert(!is_void<_Tp>::value,
1387 "default_delete can not delete incomplete type");
1388 delete __ptr;
1389 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390};
1391
1392template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001393struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1394private:
1395 template <class _Up>
1396 struct _EnableIfConvertible
1397 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1398
Howard Hinnant4500ca52011-12-18 21:19:44 +00001399public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001400#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001401 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001402#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001403 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001404#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001405
1406 template <class _Up>
1407 _LIBCPP_INLINE_VISIBILITY
1408 default_delete(const default_delete<_Up[]>&,
1409 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1410
1411 template <class _Up>
1412 _LIBCPP_INLINE_VISIBILITY
1413 typename _EnableIfConvertible<_Up>::type
1414 operator()(_Up* __ptr) const _NOEXCEPT {
1415 static_assert(sizeof(_Tp) > 0,
1416 "default_delete can not delete incomplete type");
1417 static_assert(!is_void<_Tp>::value,
1418 "default_delete can not delete void type");
1419 delete[] __ptr;
1420 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421};
1422
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001423template <class _Deleter>
1424struct __unique_ptr_deleter_sfinae {
1425 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1426 typedef const _Deleter& __lval_ref_type;
1427 typedef _Deleter&& __good_rval_ref_type;
1428 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429};
1430
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001431template <class _Deleter>
1432struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1433 typedef const _Deleter& __lval_ref_type;
1434 typedef const _Deleter&& __bad_rval_ref_type;
1435 typedef false_type __enable_rval_overload;
1436};
1437
1438template <class _Deleter>
1439struct __unique_ptr_deleter_sfinae<_Deleter&> {
1440 typedef _Deleter& __lval_ref_type;
1441 typedef _Deleter&& __bad_rval_ref_type;
1442 typedef false_type __enable_rval_overload;
1443};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001444
Vy Nguyene369bd92020-07-13 12:34:37 -04001445#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1446# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1447#else
1448# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1449#endif
1450
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001451template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001452class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001453public:
1454 typedef _Tp element_type;
1455 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001456 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001457
1458 static_assert(!is_rvalue_reference<deleter_type>::value,
1459 "the specified deleter type cannot be an rvalue reference");
1460
1461private:
1462 __compressed_pair<pointer, deleter_type> __ptr_;
1463
1464 struct __nat { int __for_bool_; };
1465
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001466 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001467
1468 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001469 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001470 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001471
1472 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001473 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001474 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001475
1476 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001477 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001478 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001479
1480 template <bool _Dummy, class _Deleter = typename __dependent_type<
1481 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001482 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001483 typename enable_if<is_default_constructible<_Deleter>::value &&
1484 !is_pointer<_Deleter>::value>::type;
1485
1486 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001487 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001488 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1489
1490 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001491 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001492 is_convertible<typename _UPtr::pointer, pointer>::value &&
1493 !is_array<_Up>::value
1494 >::type;
1495
1496 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001497 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001498 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1499 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1500 >::type;
1501
1502 template <class _UDel>
1503 using _EnableIfDeleterAssignable = typename enable_if<
1504 is_assignable<_Dp&, _UDel&&>::value
1505 >::type;
1506
1507public:
1508 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001509 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001510 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001511 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001512
1513 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001514 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001515 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001516 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001517
1518 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001519 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001520 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001521 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001522
1523 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001524 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001525 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001526 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001527 : __ptr_(__p, __d) {}
1528
1529 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001530 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001531 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001532 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001533 : __ptr_(__p, _VSTD::move(__d)) {
1534 static_assert(!is_reference<deleter_type>::value,
1535 "rvalue deleter bound to reference");
1536 }
1537
1538 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001539 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001540 _LIBCPP_INLINE_VISIBILITY
1541 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1542
1543 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001544 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001545 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1546 }
1547
1548 template <class _Up, class _Ep,
1549 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1550 class = _EnableIfDeleterConvertible<_Ep>
1551 >
1552 _LIBCPP_INLINE_VISIBILITY
1553 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1554 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1555
1556#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1557 template <class _Up>
1558 _LIBCPP_INLINE_VISIBILITY
1559 unique_ptr(auto_ptr<_Up>&& __p,
1560 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001561 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001562 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001563 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001564#endif
1565
1566 _LIBCPP_INLINE_VISIBILITY
1567 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1568 reset(__u.release());
1569 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1570 return *this;
1571 }
1572
1573 template <class _Up, class _Ep,
1574 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1575 class = _EnableIfDeleterAssignable<_Ep>
1576 >
1577 _LIBCPP_INLINE_VISIBILITY
1578 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1579 reset(__u.release());
1580 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1581 return *this;
1582 }
1583
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001584#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1585 template <class _Up>
1586 _LIBCPP_INLINE_VISIBILITY
1587 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1588 is_same<_Dp, default_delete<_Tp> >::value,
1589 unique_ptr&>::type
1590 operator=(auto_ptr<_Up> __p) {
1591 reset(__p.release());
1592 return *this;
1593 }
1594#endif
1595
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001596#ifdef _LIBCPP_CXX03_LANG
1597 unique_ptr(unique_ptr const&) = delete;
1598 unique_ptr& operator=(unique_ptr const&) = delete;
1599#endif
1600
1601
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001602 _LIBCPP_INLINE_VISIBILITY
1603 ~unique_ptr() { reset(); }
1604
1605 _LIBCPP_INLINE_VISIBILITY
1606 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1607 reset();
1608 return *this;
1609 }
1610
1611 _LIBCPP_INLINE_VISIBILITY
1612 typename add_lvalue_reference<_Tp>::type
1613 operator*() const {
1614 return *__ptr_.first();
1615 }
1616 _LIBCPP_INLINE_VISIBILITY
1617 pointer operator->() const _NOEXCEPT {
1618 return __ptr_.first();
1619 }
1620 _LIBCPP_INLINE_VISIBILITY
1621 pointer get() const _NOEXCEPT {
1622 return __ptr_.first();
1623 }
1624 _LIBCPP_INLINE_VISIBILITY
1625 deleter_type& get_deleter() _NOEXCEPT {
1626 return __ptr_.second();
1627 }
1628 _LIBCPP_INLINE_VISIBILITY
1629 const deleter_type& get_deleter() const _NOEXCEPT {
1630 return __ptr_.second();
1631 }
1632 _LIBCPP_INLINE_VISIBILITY
1633 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1634 return __ptr_.first() != nullptr;
1635 }
1636
1637 _LIBCPP_INLINE_VISIBILITY
1638 pointer release() _NOEXCEPT {
1639 pointer __t = __ptr_.first();
1640 __ptr_.first() = pointer();
1641 return __t;
1642 }
1643
1644 _LIBCPP_INLINE_VISIBILITY
1645 void reset(pointer __p = pointer()) _NOEXCEPT {
1646 pointer __tmp = __ptr_.first();
1647 __ptr_.first() = __p;
1648 if (__tmp)
1649 __ptr_.second()(__tmp);
1650 }
1651
1652 _LIBCPP_INLINE_VISIBILITY
1653 void swap(unique_ptr& __u) _NOEXCEPT {
1654 __ptr_.swap(__u.__ptr_);
1655 }
1656};
1657
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001658
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001660class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001662 typedef _Tp element_type;
1663 typedef _Dp deleter_type;
1664 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1665
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001667 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668
Eric Fiselier31127cd2017-04-16 02:14:31 +00001669 template <class _From>
1670 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001671
Eric Fiselier31127cd2017-04-16 02:14:31 +00001672 template <class _FromElem>
1673 struct _CheckArrayPointerConversion<_FromElem*>
1674 : integral_constant<bool,
1675 is_same<_FromElem*, pointer>::value ||
1676 (is_same<pointer, element_type*>::value &&
1677 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1678 >
1679 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680
Eric Fiselier31127cd2017-04-16 02:14:31 +00001681 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001682
1683 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001684 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001685 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001686
1687 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001688 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001689 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001690
1691 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001692 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001693 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001694
1695 template <bool _Dummy, class _Deleter = typename __dependent_type<
1696 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001697 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001698 typename enable_if<is_default_constructible<_Deleter>::value &&
1699 !is_pointer<_Deleter>::value>::type;
1700
1701 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001702 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001703 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1704
1705 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001706 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001707 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001708 >::type;
1709
1710 template <class _UPtr, class _Up,
1711 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001712 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001713 is_array<_Up>::value &&
1714 is_same<pointer, element_type*>::value &&
1715 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1716 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1717 >::type;
1718
1719 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001720 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001721 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1722 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1723 >::type;
1724
1725 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001726 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001727 is_assignable<_Dp&, _UDel&&>::value
1728 >::type;
1729
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001731 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001732 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001733 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001734 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001736 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001737 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001738 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001739 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001741 template <class _Pp, bool _Dummy = true,
1742 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001743 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001744 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001745 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001746 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001748 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001749 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1750 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001751 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001752 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001753 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001755 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001756 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001757 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001758 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001759 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001761 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001762 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1763 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001764 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001765 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001766 : __ptr_(__p, _VSTD::move(__d)) {
1767 static_assert(!is_reference<deleter_type>::value,
1768 "rvalue deleter bound to reference");
1769 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001771 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001772 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001773 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001774 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001775 : __ptr_(nullptr, _VSTD::move(__d)) {
1776 static_assert(!is_reference<deleter_type>::value,
1777 "rvalue deleter bound to reference");
1778 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001779
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001780 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001781 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1782 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001783 _LIBCPP_INLINE_VISIBILITY
1784 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001785
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001786 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001787 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001788 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1789 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001790
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001791 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001792 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001793 reset(__u.release());
1794 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1795 return *this;
1796 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001798 template <class _Up, class _Ep,
1799 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1800 class = _EnableIfDeleterConvertible<_Ep>
1801 >
1802 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001803 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001804 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001805 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001807 template <class _Up, class _Ep,
1808 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1809 class = _EnableIfDeleterAssignable<_Ep>
1810 >
1811 _LIBCPP_INLINE_VISIBILITY
1812 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001813 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001814 reset(__u.release());
1815 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1816 return *this;
1817 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001819#ifdef _LIBCPP_CXX03_LANG
1820 unique_ptr(unique_ptr const&) = delete;
1821 unique_ptr& operator=(unique_ptr const&) = delete;
1822#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001823
1824public:
1825 _LIBCPP_INLINE_VISIBILITY
1826 ~unique_ptr() { reset(); }
1827
1828 _LIBCPP_INLINE_VISIBILITY
1829 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1830 reset();
1831 return *this;
1832 }
1833
1834 _LIBCPP_INLINE_VISIBILITY
1835 typename add_lvalue_reference<_Tp>::type
1836 operator[](size_t __i) const {
1837 return __ptr_.first()[__i];
1838 }
1839 _LIBCPP_INLINE_VISIBILITY
1840 pointer get() const _NOEXCEPT {
1841 return __ptr_.first();
1842 }
1843
1844 _LIBCPP_INLINE_VISIBILITY
1845 deleter_type& get_deleter() _NOEXCEPT {
1846 return __ptr_.second();
1847 }
1848
1849 _LIBCPP_INLINE_VISIBILITY
1850 const deleter_type& get_deleter() const _NOEXCEPT {
1851 return __ptr_.second();
1852 }
1853 _LIBCPP_INLINE_VISIBILITY
1854 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1855 return __ptr_.first() != nullptr;
1856 }
1857
1858 _LIBCPP_INLINE_VISIBILITY
1859 pointer release() _NOEXCEPT {
1860 pointer __t = __ptr_.first();
1861 __ptr_.first() = pointer();
1862 return __t;
1863 }
1864
1865 template <class _Pp>
1866 _LIBCPP_INLINE_VISIBILITY
1867 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001868 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001869 >::type
1870 reset(_Pp __p) _NOEXCEPT {
1871 pointer __tmp = __ptr_.first();
1872 __ptr_.first() = __p;
1873 if (__tmp)
1874 __ptr_.second()(__tmp);
1875 }
1876
1877 _LIBCPP_INLINE_VISIBILITY
1878 void reset(nullptr_t = nullptr) _NOEXCEPT {
1879 pointer __tmp = __ptr_.first();
1880 __ptr_.first() = nullptr;
1881 if (__tmp)
1882 __ptr_.second()(__tmp);
1883 }
1884
1885 _LIBCPP_INLINE_VISIBILITY
1886 void swap(unique_ptr& __u) _NOEXCEPT {
1887 __ptr_.swap(__u.__ptr_);
1888 }
1889
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890};
1891
1892template <class _Tp, class _Dp>
1893inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001894typename enable_if<
1895 __is_swappable<_Dp>::value,
1896 void
1897>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001898swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899
1900template <class _T1, class _D1, class _T2, class _D2>
1901inline _LIBCPP_INLINE_VISIBILITY
1902bool
1903operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1904
1905template <class _T1, class _D1, class _T2, class _D2>
1906inline _LIBCPP_INLINE_VISIBILITY
1907bool
1908operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1909
1910template <class _T1, class _D1, class _T2, class _D2>
1911inline _LIBCPP_INLINE_VISIBILITY
1912bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001913operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1914{
1915 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1916 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001917 typedef typename common_type<_P1, _P2>::type _Vp;
1918 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001919}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920
1921template <class _T1, class _D1, class _T2, class _D2>
1922inline _LIBCPP_INLINE_VISIBILITY
1923bool
1924operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1925
1926template <class _T1, class _D1, class _T2, class _D2>
1927inline _LIBCPP_INLINE_VISIBILITY
1928bool
1929operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1930
1931template <class _T1, class _D1, class _T2, class _D2>
1932inline _LIBCPP_INLINE_VISIBILITY
1933bool
1934operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1935
Howard Hinnantb17caf92012-02-21 21:02:58 +00001936template <class _T1, class _D1>
1937inline _LIBCPP_INLINE_VISIBILITY
1938bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001939operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001940{
1941 return !__x;
1942}
1943
1944template <class _T1, class _D1>
1945inline _LIBCPP_INLINE_VISIBILITY
1946bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001947operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001948{
1949 return !__x;
1950}
1951
1952template <class _T1, class _D1>
1953inline _LIBCPP_INLINE_VISIBILITY
1954bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001955operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001956{
1957 return static_cast<bool>(__x);
1958}
1959
1960template <class _T1, class _D1>
1961inline _LIBCPP_INLINE_VISIBILITY
1962bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001963operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001964{
1965 return static_cast<bool>(__x);
1966}
1967
1968template <class _T1, class _D1>
1969inline _LIBCPP_INLINE_VISIBILITY
1970bool
1971operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1972{
1973 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1974 return less<_P1>()(__x.get(), nullptr);
1975}
1976
1977template <class _T1, class _D1>
1978inline _LIBCPP_INLINE_VISIBILITY
1979bool
1980operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1981{
1982 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1983 return less<_P1>()(nullptr, __x.get());
1984}
1985
1986template <class _T1, class _D1>
1987inline _LIBCPP_INLINE_VISIBILITY
1988bool
1989operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1990{
1991 return nullptr < __x;
1992}
1993
1994template <class _T1, class _D1>
1995inline _LIBCPP_INLINE_VISIBILITY
1996bool
1997operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1998{
1999 return __x < nullptr;
2000}
2001
2002template <class _T1, class _D1>
2003inline _LIBCPP_INLINE_VISIBILITY
2004bool
2005operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2006{
2007 return !(nullptr < __x);
2008}
2009
2010template <class _T1, class _D1>
2011inline _LIBCPP_INLINE_VISIBILITY
2012bool
2013operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2014{
2015 return !(__x < nullptr);
2016}
2017
2018template <class _T1, class _D1>
2019inline _LIBCPP_INLINE_VISIBILITY
2020bool
2021operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2022{
2023 return !(__x < nullptr);
2024}
2025
2026template <class _T1, class _D1>
2027inline _LIBCPP_INLINE_VISIBILITY
2028bool
2029operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2030{
2031 return !(nullptr < __x);
2032}
2033
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002034#if _LIBCPP_STD_VER > 11
2035
2036template<class _Tp>
2037struct __unique_if
2038{
2039 typedef unique_ptr<_Tp> __unique_single;
2040};
2041
2042template<class _Tp>
2043struct __unique_if<_Tp[]>
2044{
2045 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2046};
2047
2048template<class _Tp, size_t _Np>
2049struct __unique_if<_Tp[_Np]>
2050{
2051 typedef void __unique_array_known_bound;
2052};
2053
2054template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002055inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002056typename __unique_if<_Tp>::__unique_single
2057make_unique(_Args&&... __args)
2058{
2059 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2060}
2061
2062template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002063inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002064typename __unique_if<_Tp>::__unique_array_unknown_bound
2065make_unique(size_t __n)
2066{
2067 typedef typename remove_extent<_Tp>::type _Up;
2068 return unique_ptr<_Tp>(new _Up[__n]());
2069}
2070
2071template<class _Tp, class... _Args>
2072 typename __unique_if<_Tp>::__unique_array_known_bound
2073 make_unique(_Args&&...) = delete;
2074
2075#endif // _LIBCPP_STD_VER > 11
2076
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002077template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002078#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002079struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002080#else
2081struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002082 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002083#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002084{
2085 typedef unique_ptr<_Tp, _Dp> argument_type;
2086 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002087 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002088 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002089 {
2090 typedef typename argument_type::pointer pointer;
2091 return hash<pointer>()(__ptr.get());
2092 }
2093};
2094
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095struct __destruct_n
2096{
2097private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002098 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099
2100 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002101 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002102 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103
2104 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002105 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 {}
2107
Howard Hinnant719bda32011-05-28 14:41:13 +00002108 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002109 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002110 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111 {}
2112
Howard Hinnant719bda32011-05-28 14:41:13 +00002113 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002114 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002115 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116 {}
2117public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002118 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002119 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120
2121 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002122 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002123 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124
2125 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002126 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002127 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128
2129 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002130 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002131 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132};
2133
2134template <class _Alloc>
2135class __allocator_destructor
2136{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002137 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002139 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2140 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141private:
2142 _Alloc& __alloc_;
2143 size_type __s_;
2144public:
2145 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002146 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002149 void operator()(pointer __p) _NOEXCEPT
2150 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151};
2152
2153template <class _InputIterator, class _ForwardIterator>
2154_ForwardIterator
2155uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2156{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002158#ifndef _LIBCPP_NO_EXCEPTIONS
2159 _ForwardIterator __s = __r;
2160 try
2161 {
2162#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002163 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002164 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002165#ifndef _LIBCPP_NO_EXCEPTIONS
2166 }
2167 catch (...)
2168 {
2169 for (; __s != __r; ++__s)
2170 __s->~value_type();
2171 throw;
2172 }
2173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 return __r;
2175}
2176
2177template <class _InputIterator, class _Size, class _ForwardIterator>
2178_ForwardIterator
2179uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2180{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002182#ifndef _LIBCPP_NO_EXCEPTIONS
2183 _ForwardIterator __s = __r;
2184 try
2185 {
2186#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002187 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002188 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002189#ifndef _LIBCPP_NO_EXCEPTIONS
2190 }
2191 catch (...)
2192 {
2193 for (; __s != __r; ++__s)
2194 __s->~value_type();
2195 throw;
2196 }
2197#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198 return __r;
2199}
2200
2201template <class _ForwardIterator, class _Tp>
2202void
2203uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2204{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002206#ifndef _LIBCPP_NO_EXCEPTIONS
2207 _ForwardIterator __s = __f;
2208 try
2209 {
2210#endif
2211 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002212 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002213#ifndef _LIBCPP_NO_EXCEPTIONS
2214 }
2215 catch (...)
2216 {
2217 for (; __s != __f; ++__s)
2218 __s->~value_type();
2219 throw;
2220 }
2221#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222}
2223
2224template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002225_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2227{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002229#ifndef _LIBCPP_NO_EXCEPTIONS
2230 _ForwardIterator __s = __f;
2231 try
2232 {
2233#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002234 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002235 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002236#ifndef _LIBCPP_NO_EXCEPTIONS
2237 }
2238 catch (...)
2239 {
2240 for (; __s != __f; ++__s)
2241 __s->~value_type();
2242 throw;
2243 }
2244#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002245 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246}
2247
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002248#if _LIBCPP_STD_VER > 14
2249
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002250template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002251inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002252void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2253 for (; __first != __last; ++__first)
2254 _VSTD::destroy_at(_VSTD::addressof(*__first));
2255}
2256
2257template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002258inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002259_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2260 for (; __n > 0; (void)++__first, --__n)
2261 _VSTD::destroy_at(_VSTD::addressof(*__first));
2262 return __first;
2263}
2264
Eric Fiselier290c07c2016-10-11 21:13:44 +00002265template <class _ForwardIterator>
2266inline _LIBCPP_INLINE_VISIBILITY
2267void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2268 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2269 auto __idx = __first;
2270#ifndef _LIBCPP_NO_EXCEPTIONS
2271 try {
2272#endif
2273 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002274 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002275#ifndef _LIBCPP_NO_EXCEPTIONS
2276 } catch (...) {
2277 _VSTD::destroy(__first, __idx);
2278 throw;
2279 }
2280#endif
2281}
2282
2283template <class _ForwardIterator, class _Size>
2284inline _LIBCPP_INLINE_VISIBILITY
2285_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2286 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2287 auto __idx = __first;
2288#ifndef _LIBCPP_NO_EXCEPTIONS
2289 try {
2290#endif
2291 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002292 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002293 return __idx;
2294#ifndef _LIBCPP_NO_EXCEPTIONS
2295 } catch (...) {
2296 _VSTD::destroy(__first, __idx);
2297 throw;
2298 }
2299#endif
2300}
2301
2302
2303template <class _ForwardIterator>
2304inline _LIBCPP_INLINE_VISIBILITY
2305void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2306 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2307 auto __idx = __first;
2308#ifndef _LIBCPP_NO_EXCEPTIONS
2309 try {
2310#endif
2311 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002312 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002313#ifndef _LIBCPP_NO_EXCEPTIONS
2314 } catch (...) {
2315 _VSTD::destroy(__first, __idx);
2316 throw;
2317 }
2318#endif
2319}
2320
2321template <class _ForwardIterator, class _Size>
2322inline _LIBCPP_INLINE_VISIBILITY
2323_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2324 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2325 auto __idx = __first;
2326#ifndef _LIBCPP_NO_EXCEPTIONS
2327 try {
2328#endif
2329 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002330 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002331 return __idx;
2332#ifndef _LIBCPP_NO_EXCEPTIONS
2333 } catch (...) {
2334 _VSTD::destroy(__first, __idx);
2335 throw;
2336 }
2337#endif
2338}
2339
2340
2341template <class _InputIt, class _ForwardIt>
2342inline _LIBCPP_INLINE_VISIBILITY
2343_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2344 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2345 auto __idx = __first_res;
2346#ifndef _LIBCPP_NO_EXCEPTIONS
2347 try {
2348#endif
2349 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002350 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002351 return __idx;
2352#ifndef _LIBCPP_NO_EXCEPTIONS
2353 } catch (...) {
2354 _VSTD::destroy(__first_res, __idx);
2355 throw;
2356 }
2357#endif
2358}
2359
2360template <class _InputIt, class _Size, class _ForwardIt>
2361inline _LIBCPP_INLINE_VISIBILITY
2362pair<_InputIt, _ForwardIt>
2363uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2364 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2365 auto __idx = __first_res;
2366#ifndef _LIBCPP_NO_EXCEPTIONS
2367 try {
2368#endif
2369 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002370 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002371 return {__first, __idx};
2372#ifndef _LIBCPP_NO_EXCEPTIONS
2373 } catch (...) {
2374 _VSTD::destroy(__first_res, __idx);
2375 throw;
2376 }
2377#endif
2378}
2379
2380
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002381#endif // _LIBCPP_STD_VER > 14
2382
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002383// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2384// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00002385// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002386#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2387 && defined(__ATOMIC_RELAXED) \
2388 && defined(__ATOMIC_ACQ_REL)
2389# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002390#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002391# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2392#endif
2393
2394template <class _Tp>
2395inline _LIBCPP_INLINE_VISIBILITY _Tp
2396__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2397{
2398#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2399 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2400#else
2401 return __t += 1;
2402#endif
2403}
2404
2405template <class _Tp>
2406inline _LIBCPP_INLINE_VISIBILITY _Tp
2407__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2408{
2409#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2410 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2411#else
2412 return __t -= 1;
2413#endif
2414}
2415
Howard Hinnant756c69b2010-09-22 16:48:34 +00002416class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417 : public std::exception
2418{
2419public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002420 bad_weak_ptr() _NOEXCEPT = default;
2421 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002422 virtual ~bad_weak_ptr() _NOEXCEPT;
2423 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424};
2425
Louis Dionne16fe2952018-07-11 23:14:33 +00002426_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002427void __throw_bad_weak_ptr()
2428{
2429#ifndef _LIBCPP_NO_EXCEPTIONS
2430 throw bad_weak_ptr();
2431#else
2432 _VSTD::abort();
2433#endif
2434}
2435
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002436template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002438class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439{
2440 __shared_count(const __shared_count&);
2441 __shared_count& operator=(const __shared_count&);
2442
2443protected:
2444 long __shared_owners_;
2445 virtual ~__shared_count();
2446private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002447 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448
2449public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002451 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452 : __shared_owners_(__refs) {}
2453
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002454#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002455 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002456 void __add_shared() _NOEXCEPT;
2457 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002458#else
2459 _LIBCPP_INLINE_VISIBILITY
2460 void __add_shared() _NOEXCEPT {
2461 __libcpp_atomic_refcount_increment(__shared_owners_);
2462 }
2463 _LIBCPP_INLINE_VISIBILITY
2464 bool __release_shared() _NOEXCEPT {
2465 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2466 __on_zero_shared();
2467 return true;
2468 }
2469 return false;
2470 }
2471#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002472 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002473 long use_count() const _NOEXCEPT {
2474 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2475 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476};
2477
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002478class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479 : private __shared_count
2480{
2481 long __shared_weak_owners_;
2482
2483public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002485 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486 : __shared_count(__refs),
2487 __shared_weak_owners_(__refs) {}
2488protected:
2489 virtual ~__shared_weak_count();
2490
2491public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002492#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002493 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002494 void __add_shared() _NOEXCEPT;
2495 void __add_weak() _NOEXCEPT;
2496 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002497#else
2498 _LIBCPP_INLINE_VISIBILITY
2499 void __add_shared() _NOEXCEPT {
2500 __shared_count::__add_shared();
2501 }
2502 _LIBCPP_INLINE_VISIBILITY
2503 void __add_weak() _NOEXCEPT {
2504 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2505 }
2506 _LIBCPP_INLINE_VISIBILITY
2507 void __release_shared() _NOEXCEPT {
2508 if (__shared_count::__release_shared())
2509 __release_weak();
2510 }
2511#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002512 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002514 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2515 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516
Howard Hinnant719bda32011-05-28 14:41:13 +00002517 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002519 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520};
2521
2522template <class _Tp, class _Dp, class _Alloc>
2523class __shared_ptr_pointer
2524 : public __shared_weak_count
2525{
2526 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2527public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002530 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531
Howard Hinnant72f73582010-08-11 17:04:31 +00002532#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002533 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002534#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535
2536private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002537 virtual void __on_zero_shared() _NOEXCEPT;
2538 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539};
2540
Howard Hinnant72f73582010-08-11 17:04:31 +00002541#ifndef _LIBCPP_NO_RTTI
2542
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543template <class _Tp, class _Dp, class _Alloc>
2544const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002545__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002547 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548}
2549
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002550#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002551
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552template <class _Tp, class _Dp, class _Alloc>
2553void
Howard Hinnant719bda32011-05-28 14:41:13 +00002554__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555{
2556 __data_.first().second()(__data_.first().first());
2557 __data_.first().second().~_Dp();
2558}
2559
2560template <class _Tp, class _Dp, class _Alloc>
2561void
Howard Hinnant719bda32011-05-28 14:41:13 +00002562__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002564 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2565 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002566 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2567
Eric Fiselierf8898c82015-02-05 23:01:40 +00002568 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002570 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571}
2572
2573template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002574struct __shared_ptr_emplace
2575 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576{
Louis Dionne86549d72020-12-09 16:22:17 -05002577 _LIBCPP_HIDE_FROM_ABI
2578 explicit __shared_ptr_emplace(_Alloc __a)
2579 : __data_(_VSTD::move(__a), __value_init_tag())
2580 { }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581
Louis Dionne86549d72020-12-09 16:22:17 -05002582 template <class ..._Args>
2583 _LIBCPP_HIDE_FROM_ABI
2584 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002585#ifndef _LIBCPP_CXX03_LANG
Louis Dionne86549d72020-12-09 16:22:17 -05002586 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
2587 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...))
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002588#else
Louis Dionne86549d72020-12-09 16:22:17 -05002589 : __data_(__a, _Tp(_VSTD::forward<_Args>(__args)...))
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002590#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002591 { }
2592
2593 _LIBCPP_HIDE_FROM_ABI
2594 _Tp* __get_elem() _NOEXCEPT { return _VSTD::addressof(__data_.second()); }
2595
2596 _LIBCPP_HIDE_FROM_ABI
Louis Dionneca117a22020-12-14 17:40:56 -05002597 _Alloc* __get_alloc() _NOEXCEPT { return _VSTD::addressof(__data_.first()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598
2599private:
Louis Dionne86549d72020-12-09 16:22:17 -05002600 virtual void __on_zero_shared() _NOEXCEPT {
2601 __get_elem()->~_Tp();
2602 }
2603
2604 virtual void __on_zero_shared_weak() _NOEXCEPT {
2605 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2606 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002607 _ControlBlockAlloc __tmp(*__get_alloc());
2608 __get_alloc()->~_Alloc();
Louis Dionne86549d72020-12-09 16:22:17 -05002609 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2610 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2611 }
2612
2613 __compressed_pair<_Alloc, _Tp> __data_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614};
2615
Erik Pilkington2a398762017-05-25 15:43:31 +00002616struct __shared_ptr_dummy_rebind_allocator_type;
2617template <>
2618class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2619{
2620public:
2621 template <class _Other>
2622 struct rebind
2623 {
2624 typedef allocator<_Other> other;
2625 };
2626};
2627
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002628template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629
zoecarverd73f42a2020-05-11 18:42:50 -07002630template<class _Tp, class _Up>
2631struct __compatible_with
2632#if _LIBCPP_STD_VER > 14
2633 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2634#else
2635 : is_convertible<_Tp*, _Up*> {};
2636#endif // _LIBCPP_STD_VER > 14
2637
Vy Nguyene369bd92020-07-13 12:34:37 -04002638#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2639# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2640#else
2641# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2642#endif
2643
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002645class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002647public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002648#if _LIBCPP_STD_VER > 14
2649 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002650 typedef remove_extent_t<_Tp> element_type;
2651#else
2652 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002653#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002654
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655private:
2656 element_type* __ptr_;
2657 __shared_weak_count* __cntrl_;
2658
2659 struct __nat {int __for_bool_;};
2660public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002662 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002664 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002665 template<class _Yp>
2666 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002667 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002668 template<class _Yp, class _Dp>
2669 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07002670 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002671 template<class _Yp, class _Dp, class _Alloc>
2672 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07002673 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2675 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002676 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002678 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002682 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002683 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002685 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002686 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002687 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002688 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002690 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002691#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002692 template<class _Yp>
2693 shared_ptr(auto_ptr<_Yp>&& __r,
2694 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002695#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002696 template <class _Yp, class _Dp>
2697 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2698 typename enable_if
2699 <
2700 !is_lvalue_reference<_Dp>::value &&
2701 !is_array<_Yp>::value &&
2702 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2703 __nat
2704 >::type = __nat());
2705 template <class _Yp, class _Dp>
2706 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2707 typename enable_if
2708 <
2709 is_lvalue_reference<_Dp>::value &&
2710 !is_array<_Yp>::value &&
2711 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2712 __nat
2713 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714
2715 ~shared_ptr();
2716
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002718 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002719 template<class _Yp>
2720 typename enable_if
2721 <
zoecarverd73f42a2020-05-11 18:42:50 -07002722 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002723 shared_ptr&
2724 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002726 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002728 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002729 template<class _Yp>
2730 typename enable_if
2731 <
zoecarverd73f42a2020-05-11 18:42:50 -07002732 __compatible_with<_Yp, element_type>::value,
2733 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002734 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002736 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002737#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002738 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002740 typename enable_if
2741 <
2742 !is_array<_Yp>::value &&
2743 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002744 shared_ptr
2745 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002746 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002747#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002748 template <class _Yp, class _Dp>
2749 typename enable_if
2750 <
2751 !is_array<_Yp>::value &&
2752 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2753 shared_ptr&
2754 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002756 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002759 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002761 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002762 template<class _Yp>
2763 typename enable_if
2764 <
zoecarverd73f42a2020-05-11 18:42:50 -07002765 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002766 void
2767 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002769 reset(_Yp* __p);
2770 template<class _Yp, class _Dp>
2771 typename enable_if
2772 <
zoecarverd73f42a2020-05-11 18:42:50 -07002773 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002774 void
2775 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002777 reset(_Yp* __p, _Dp __d);
2778 template<class _Yp, class _Dp, class _Alloc>
2779 typename enable_if
2780 <
zoecarverd73f42a2020-05-11 18:42:50 -07002781 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002782 void
2783 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002785 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786
Howard Hinnant756c69b2010-09-22 16:48:34 +00002787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002788 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002790 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2791 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002792 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002793 element_type* operator->() const _NOEXCEPT
2794 {
2795 static_assert(!_VSTD::is_array<_Tp>::value,
2796 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2797 return __ptr_;
2798 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002800 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002802 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002803 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002804 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002805 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002806 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002807 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002809 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002810 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002811 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002813 _LIBCPP_INLINE_VISIBILITY
2814 bool
2815 __owner_equivalent(const shared_ptr& __p) const
2816 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817
zoecarverd73f42a2020-05-11 18:42:50 -07002818#if _LIBCPP_STD_VER > 14
2819 typename add_lvalue_reference<element_type>::type
2820 _LIBCPP_INLINE_VISIBILITY
2821 operator[](ptrdiff_t __i) const
2822 {
2823 static_assert(_VSTD::is_array<_Tp>::value,
2824 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2825 return __ptr_[__i];
2826 }
2827#endif
2828
Howard Hinnant72f73582010-08-11 17:04:31 +00002829#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002832 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002833 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002834 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002835 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002836#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837
Zoe Carverd9040c72019-10-22 15:16:49 +00002838 template<class _Yp, class _CntrlBlk>
2839 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002840 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002841 {
2842 shared_ptr<_Tp> __r;
2843 __r.__ptr_ = __p;
2844 __r.__cntrl_ = __cntrl;
2845 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2846 return __r;
2847 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002848
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002850 template <class _Yp, bool = is_function<_Yp>::value>
2851 struct __shared_ptr_default_allocator
2852 {
2853 typedef allocator<_Yp> type;
2854 };
2855
2856 template <class _Yp>
2857 struct __shared_ptr_default_allocator<_Yp, true>
2858 {
2859 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2860 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002862 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002863 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002864 typename enable_if<is_convertible<_OrigPtr*,
2865 const enable_shared_from_this<_Yp>*
2866 >::value,
2867 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002868 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2869 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002871 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002872 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002873 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002874 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2875 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002876 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 }
2878
Erik Pilkington2a398762017-05-25 15:43:31 +00002879 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880
zoecarverd73f42a2020-05-11 18:42:50 -07002881 template <class, class _Yp>
2882 struct __shared_ptr_default_delete
2883 : default_delete<_Yp> {};
2884
2885 template <class _Yp, class _Un, size_t _Sz>
2886 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2887 : default_delete<_Yp[]> {};
2888
2889 template <class _Yp, class _Un>
2890 struct __shared_ptr_default_delete<_Yp[], _Un>
2891 : default_delete<_Yp[]> {};
2892
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002893 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2894 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002895};
2896
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002897#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2898template<class _Tp>
2899shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2900template<class _Tp, class _Dp>
2901shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2902#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002903
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002905inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002906_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002907shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002908 : __ptr_(nullptr),
2909 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910{
2911}
2912
2913template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002914inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002915_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002916shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002917 : __ptr_(nullptr),
2918 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919{
2920}
2921
2922template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002923template<class _Yp>
2924shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002925 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002926 : __ptr_(__p)
2927{
2928 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002929 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002930 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2931 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002933 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934}
2935
2936template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002937template<class _Yp, class _Dp>
2938shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07002939 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940 : __ptr_(__p)
2941{
2942#ifndef _LIBCPP_NO_EXCEPTIONS
2943 try
2944 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002945#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002946 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2947 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
2948 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002949 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950#ifndef _LIBCPP_NO_EXCEPTIONS
2951 }
2952 catch (...)
2953 {
2954 __d(__p);
2955 throw;
2956 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002957#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958}
2959
2960template<class _Tp>
2961template<class _Dp>
2962shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002963 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964{
2965#ifndef _LIBCPP_NO_EXCEPTIONS
2966 try
2967 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002968#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002969 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2970 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
2971 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972#ifndef _LIBCPP_NO_EXCEPTIONS
2973 }
2974 catch (...)
2975 {
2976 __d(__p);
2977 throw;
2978 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002979#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980}
2981
2982template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002983template<class _Yp, class _Dp, class _Alloc>
2984shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07002985 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986 : __ptr_(__p)
2987{
2988#ifndef _LIBCPP_NO_EXCEPTIONS
2989 try
2990 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002991#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002993 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994 typedef __allocator_destructor<_A2> _D2;
2995 _A2 __a2(__a);
2996 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002997 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002998 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002999 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000#ifndef _LIBCPP_NO_EXCEPTIONS
3001 }
3002 catch (...)
3003 {
3004 __d(__p);
3005 throw;
3006 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003007#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008}
3009
3010template<class _Tp>
3011template<class _Dp, class _Alloc>
3012shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003013 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014{
3015#ifndef _LIBCPP_NO_EXCEPTIONS
3016 try
3017 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003018#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003020 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021 typedef __allocator_destructor<_A2> _D2;
3022 _A2 __a2(__a);
3023 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003024 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003025 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026#ifndef _LIBCPP_NO_EXCEPTIONS
3027 }
3028 catch (...)
3029 {
3030 __d(__p);
3031 throw;
3032 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003033#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034}
3035
3036template<class _Tp>
3037template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003038inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003039shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 : __ptr_(__p),
3041 __cntrl_(__r.__cntrl_)
3042{
3043 if (__cntrl_)
3044 __cntrl_->__add_shared();
3045}
3046
3047template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003048inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003049shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050 : __ptr_(__r.__ptr_),
3051 __cntrl_(__r.__cntrl_)
3052{
3053 if (__cntrl_)
3054 __cntrl_->__add_shared();
3055}
3056
3057template<class _Tp>
3058template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003059inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003061 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003062 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 : __ptr_(__r.__ptr_),
3064 __cntrl_(__r.__cntrl_)
3065{
3066 if (__cntrl_)
3067 __cntrl_->__add_shared();
3068}
3069
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003071inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003072shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 : __ptr_(__r.__ptr_),
3074 __cntrl_(__r.__cntrl_)
3075{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003076 __r.__ptr_ = nullptr;
3077 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078}
3079
3080template<class _Tp>
3081template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003082inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003084 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003085 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086 : __ptr_(__r.__ptr_),
3087 __cntrl_(__r.__cntrl_)
3088{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003089 __r.__ptr_ = nullptr;
3090 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091}
3092
Marshall Clowb22274f2017-01-24 22:22:33 +00003093#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003095template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003096shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003097 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098 : __ptr_(__r.get())
3099{
3100 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3101 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003102 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103 __r.release();
3104}
Marshall Clowb22274f2017-01-24 22:22:33 +00003105#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106
3107template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003108template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003110 typename enable_if
3111 <
3112 !is_lvalue_reference<_Dp>::value &&
3113 !is_array<_Yp>::value &&
3114 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3115 __nat
3116 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 : __ptr_(__r.get())
3118{
Marshall Clow35cde742015-05-10 13:59:45 +00003119#if _LIBCPP_STD_VER > 11
3120 if (__ptr_ == nullptr)
3121 __cntrl_ = nullptr;
3122 else
3123#endif
3124 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003125 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3126 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3127 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003128 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003129 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 __r.release();
3131}
3132
3133template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003134template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003136 typename enable_if
3137 <
3138 is_lvalue_reference<_Dp>::value &&
3139 !is_array<_Yp>::value &&
3140 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3141 __nat
3142 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143 : __ptr_(__r.get())
3144{
Marshall Clow35cde742015-05-10 13:59:45 +00003145#if _LIBCPP_STD_VER > 11
3146 if (__ptr_ == nullptr)
3147 __cntrl_ = nullptr;
3148 else
3149#endif
3150 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003151 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003152 typedef __shared_ptr_pointer<_Yp*,
3153 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003154 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003155 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003156 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003157 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 __r.release();
3159}
3160
Zoe Carver6cd05c32019-08-19 15:47:16 +00003161template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162shared_ptr<_Tp>::~shared_ptr()
3163{
3164 if (__cntrl_)
3165 __cntrl_->__release_shared();
3166}
3167
3168template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003169inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003171shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172{
3173 shared_ptr(__r).swap(*this);
3174 return *this;
3175}
3176
3177template<class _Tp>
3178template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003179inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003180typename enable_if
3181<
zoecarverd73f42a2020-05-11 18:42:50 -07003182 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003183 shared_ptr<_Tp>&
3184>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003185shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186{
3187 shared_ptr(__r).swap(*this);
3188 return *this;
3189}
3190
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003194shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003196 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197 return *this;
3198}
3199
3200template<class _Tp>
3201template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003202inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003203typename enable_if
3204<
zoecarverd73f42a2020-05-11 18:42:50 -07003205 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003206 shared_ptr<_Tp>&
3207>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3209{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003210 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211 return *this;
3212}
3213
Marshall Clowb22274f2017-01-24 22:22:33 +00003214#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215template<class _Tp>
3216template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003217inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003218typename enable_if
3219<
3220 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003221 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003222 shared_ptr<_Tp>
3223>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3225{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003226 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227 return *this;
3228}
Marshall Clowb22274f2017-01-24 22:22:33 +00003229#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230
3231template<class _Tp>
3232template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003233inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003234typename enable_if
3235<
3236 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003237 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003238 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003239 shared_ptr<_Tp>&
3240>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3242{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003243 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244 return *this;
3245}
3246
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003248inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249void
Howard Hinnant719bda32011-05-28 14:41:13 +00003250shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003252 _VSTD::swap(__ptr_, __r.__ptr_);
3253 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254}
3255
3256template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003257inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258void
Howard Hinnant719bda32011-05-28 14:41:13 +00003259shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260{
3261 shared_ptr().swap(*this);
3262}
3263
3264template<class _Tp>
3265template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003266inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003267typename enable_if
3268<
zoecarverd73f42a2020-05-11 18:42:50 -07003269 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003270 void
3271>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272shared_ptr<_Tp>::reset(_Yp* __p)
3273{
3274 shared_ptr(__p).swap(*this);
3275}
3276
3277template<class _Tp>
3278template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003279inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003280typename enable_if
3281<
zoecarverd73f42a2020-05-11 18:42:50 -07003282 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003283 void
3284>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003285shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3286{
3287 shared_ptr(__p, __d).swap(*this);
3288}
3289
3290template<class _Tp>
3291template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003292inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003293typename enable_if
3294<
zoecarverd73f42a2020-05-11 18:42:50 -07003295 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003296 void
3297>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3299{
3300 shared_ptr(__p, __d, __a).swap(*this);
3301}
3302
Louis Dionne74aef9f2020-12-11 12:20:06 -05003303//
3304// std::allocate_shared and std::make_shared
3305//
3306template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3307_LIBCPP_HIDE_FROM_ABI
3308shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309{
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003310 static_assert(is_constructible<_Tp, _Args...>::value,
3311 "allocate_shared/make_shared: the type is not constructible from the provided arguments");
Louis Dionne74aef9f2020-12-11 12:20:06 -05003312 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3313 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3314 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3315 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3316 auto __control_block = __guard.__release_ptr();
3317 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318}
3319
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003320template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3321_LIBCPP_HIDE_FROM_ABI
3322shared_ptr<_Tp> make_shared(_Args&& ...__args)
3323{
3324 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3325}
3326
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327template<class _Tp, class _Up>
3328inline _LIBCPP_INLINE_VISIBILITY
3329bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003330operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331{
3332 return __x.get() == __y.get();
3333}
3334
3335template<class _Tp, class _Up>
3336inline _LIBCPP_INLINE_VISIBILITY
3337bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003338operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339{
3340 return !(__x == __y);
3341}
3342
3343template<class _Tp, class _Up>
3344inline _LIBCPP_INLINE_VISIBILITY
3345bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003346operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003348#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003349 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3350 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003351#else
3352 return less<>()(__x.get(), __y.get());
3353#endif
3354
Howard Hinnantb17caf92012-02-21 21:02:58 +00003355}
3356
3357template<class _Tp, class _Up>
3358inline _LIBCPP_INLINE_VISIBILITY
3359bool
3360operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3361{
3362 return __y < __x;
3363}
3364
3365template<class _Tp, class _Up>
3366inline _LIBCPP_INLINE_VISIBILITY
3367bool
3368operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3369{
3370 return !(__y < __x);
3371}
3372
3373template<class _Tp, class _Up>
3374inline _LIBCPP_INLINE_VISIBILITY
3375bool
3376operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3377{
3378 return !(__x < __y);
3379}
3380
3381template<class _Tp>
3382inline _LIBCPP_INLINE_VISIBILITY
3383bool
3384operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3385{
3386 return !__x;
3387}
3388
3389template<class _Tp>
3390inline _LIBCPP_INLINE_VISIBILITY
3391bool
3392operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3393{
3394 return !__x;
3395}
3396
3397template<class _Tp>
3398inline _LIBCPP_INLINE_VISIBILITY
3399bool
3400operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3401{
3402 return static_cast<bool>(__x);
3403}
3404
3405template<class _Tp>
3406inline _LIBCPP_INLINE_VISIBILITY
3407bool
3408operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3409{
3410 return static_cast<bool>(__x);
3411}
3412
3413template<class _Tp>
3414inline _LIBCPP_INLINE_VISIBILITY
3415bool
3416operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3417{
3418 return less<_Tp*>()(__x.get(), nullptr);
3419}
3420
3421template<class _Tp>
3422inline _LIBCPP_INLINE_VISIBILITY
3423bool
3424operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3425{
3426 return less<_Tp*>()(nullptr, __x.get());
3427}
3428
3429template<class _Tp>
3430inline _LIBCPP_INLINE_VISIBILITY
3431bool
3432operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3433{
3434 return nullptr < __x;
3435}
3436
3437template<class _Tp>
3438inline _LIBCPP_INLINE_VISIBILITY
3439bool
3440operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3441{
3442 return __x < nullptr;
3443}
3444
3445template<class _Tp>
3446inline _LIBCPP_INLINE_VISIBILITY
3447bool
3448operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3449{
3450 return !(nullptr < __x);
3451}
3452
3453template<class _Tp>
3454inline _LIBCPP_INLINE_VISIBILITY
3455bool
3456operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3457{
3458 return !(__x < nullptr);
3459}
3460
3461template<class _Tp>
3462inline _LIBCPP_INLINE_VISIBILITY
3463bool
3464operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3465{
3466 return !(__x < nullptr);
3467}
3468
3469template<class _Tp>
3470inline _LIBCPP_INLINE_VISIBILITY
3471bool
3472operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3473{
3474 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475}
3476
3477template<class _Tp>
3478inline _LIBCPP_INLINE_VISIBILITY
3479void
Howard Hinnant719bda32011-05-28 14:41:13 +00003480swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481{
3482 __x.swap(__y);
3483}
3484
3485template<class _Tp, class _Up>
3486inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003487shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003488static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489{
zoecarverd73f42a2020-05-11 18:42:50 -07003490 return shared_ptr<_Tp>(__r,
3491 static_cast<
3492 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493}
3494
3495template<class _Tp, class _Up>
3496inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003497shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003498dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499{
zoecarverd73f42a2020-05-11 18:42:50 -07003500 typedef typename shared_ptr<_Tp>::element_type _ET;
3501 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3503}
3504
3505template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003506shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003507const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508{
zoecarverd73f42a2020-05-11 18:42:50 -07003509 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003510 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511}
3512
zoecarverd73f42a2020-05-11 18:42:50 -07003513template<class _Tp, class _Up>
3514shared_ptr<_Tp>
3515reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3516{
3517 return shared_ptr<_Tp>(__r,
3518 reinterpret_cast<
3519 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3520}
3521
Howard Hinnant72f73582010-08-11 17:04:31 +00003522#ifndef _LIBCPP_NO_RTTI
3523
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524template<class _Dp, class _Tp>
3525inline _LIBCPP_INLINE_VISIBILITY
3526_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003527get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528{
3529 return __p.template __get_deleter<_Dp>();
3530}
3531
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003532#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003533
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003535class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003537public:
3538 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539private:
3540 element_type* __ptr_;
3541 __shared_weak_count* __cntrl_;
3542
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003543public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003545 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003546 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003547 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3548 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003550 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003551 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003552 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3553 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003554
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003556 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003557 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003558 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3559 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003560 ~weak_ptr();
3561
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003563 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003564 template<class _Yp>
3565 typename enable_if
3566 <
3567 is_convertible<_Yp*, element_type*>::value,
3568 weak_ptr&
3569 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003571 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3572
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003574 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3575 template<class _Yp>
3576 typename enable_if
3577 <
3578 is_convertible<_Yp*, element_type*>::value,
3579 weak_ptr&
3580 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003582 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3583
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003584 template<class _Yp>
3585 typename enable_if
3586 <
3587 is_convertible<_Yp*, element_type*>::value,
3588 weak_ptr&
3589 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003591 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003592
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003594 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003596 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003597
Howard Hinnant756c69b2010-09-22 16:48:34 +00003598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003599 long use_count() const _NOEXCEPT
3600 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003602 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003603 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003604 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003605 template<class _Up>
3606 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003607 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003609 template<class _Up>
3610 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003611 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612 {return __cntrl_ < __r.__cntrl_;}
3613
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003614 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3615 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616};
3617
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003618#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3619template<class _Tp>
3620weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3621#endif
3622
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003624inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003625_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003626weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003627 : __ptr_(nullptr),
3628 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629{
3630}
3631
3632template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003633inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003634weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635 : __ptr_(__r.__ptr_),
3636 __cntrl_(__r.__cntrl_)
3637{
3638 if (__cntrl_)
3639 __cntrl_->__add_weak();
3640}
3641
3642template<class _Tp>
3643template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003644inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003646 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003647 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648 : __ptr_(__r.__ptr_),
3649 __cntrl_(__r.__cntrl_)
3650{
3651 if (__cntrl_)
3652 __cntrl_->__add_weak();
3653}
3654
3655template<class _Tp>
3656template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003657inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003659 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003660 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661 : __ptr_(__r.__ptr_),
3662 __cntrl_(__r.__cntrl_)
3663{
3664 if (__cntrl_)
3665 __cntrl_->__add_weak();
3666}
3667
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003668template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003669inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003670weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3671 : __ptr_(__r.__ptr_),
3672 __cntrl_(__r.__cntrl_)
3673{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003674 __r.__ptr_ = nullptr;
3675 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003676}
3677
3678template<class _Tp>
3679template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003680inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003681weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3682 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3683 _NOEXCEPT
3684 : __ptr_(__r.__ptr_),
3685 __cntrl_(__r.__cntrl_)
3686{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003687 __r.__ptr_ = nullptr;
3688 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003689}
3690
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691template<class _Tp>
3692weak_ptr<_Tp>::~weak_ptr()
3693{
3694 if (__cntrl_)
3695 __cntrl_->__release_weak();
3696}
3697
3698template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003699inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003701weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702{
3703 weak_ptr(__r).swap(*this);
3704 return *this;
3705}
3706
3707template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003708template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003709inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003710typename enable_if
3711<
3712 is_convertible<_Yp*, _Tp*>::value,
3713 weak_ptr<_Tp>&
3714>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003715weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716{
3717 weak_ptr(__r).swap(*this);
3718 return *this;
3719}
3720
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003721template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003722inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003723weak_ptr<_Tp>&
3724weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3725{
3726 weak_ptr(_VSTD::move(__r)).swap(*this);
3727 return *this;
3728}
3729
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003731template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003732inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003733typename enable_if
3734<
3735 is_convertible<_Yp*, _Tp*>::value,
3736 weak_ptr<_Tp>&
3737>::type
3738weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3739{
3740 weak_ptr(_VSTD::move(__r)).swap(*this);
3741 return *this;
3742}
3743
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003744template<class _Tp>
3745template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003746inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003747typename enable_if
3748<
3749 is_convertible<_Yp*, _Tp*>::value,
3750 weak_ptr<_Tp>&
3751>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003752weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753{
3754 weak_ptr(__r).swap(*this);
3755 return *this;
3756}
3757
3758template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003759inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003760void
Howard Hinnant719bda32011-05-28 14:41:13 +00003761weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003763 _VSTD::swap(__ptr_, __r.__ptr_);
3764 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765}
3766
3767template<class _Tp>
3768inline _LIBCPP_INLINE_VISIBILITY
3769void
Howard Hinnant719bda32011-05-28 14:41:13 +00003770swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771{
3772 __x.swap(__y);
3773}
3774
3775template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003776inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777void
Howard Hinnant719bda32011-05-28 14:41:13 +00003778weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779{
3780 weak_ptr().swap(*this);
3781}
3782
3783template<class _Tp>
3784template<class _Yp>
3785shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003786 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787 : __ptr_(__r.__ptr_),
3788 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3789{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003790 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003791 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792}
3793
3794template<class _Tp>
3795shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003796weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797{
3798 shared_ptr<_Tp> __r;
3799 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3800 if (__r.__cntrl_)
3801 __r.__ptr_ = __ptr_;
3802 return __r;
3803}
3804
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003805#if _LIBCPP_STD_VER > 14
3806template <class _Tp = void> struct owner_less;
3807#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003808template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003809#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810
3811template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003812struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003814{
3815 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003816 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003817 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003819 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003820 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003822 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003823 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003825};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826
3827template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003828struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003829 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3830{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003831 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003832 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003833 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003835 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003836 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003838 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003839 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840 {return __x.owner_before(__y);}
3841};
3842
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003843#if _LIBCPP_STD_VER > 14
3844template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003845struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003846{
3847 template <class _Tp, class _Up>
3848 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003849 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003850 {return __x.owner_before(__y);}
3851 template <class _Tp, class _Up>
3852 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003853 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003854 {return __x.owner_before(__y);}
3855 template <class _Tp, class _Up>
3856 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003857 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003858 {return __x.owner_before(__y);}
3859 template <class _Tp, class _Up>
3860 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003861 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003862 {return __x.owner_before(__y);}
3863 typedef void is_transparent;
3864};
3865#endif
3866
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003868class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869{
3870 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003871protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003872 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003873 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003875 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003877 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3878 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003880 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003881public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003883 shared_ptr<_Tp> shared_from_this()
3884 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003886 shared_ptr<_Tp const> shared_from_this() const
3887 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888
Eric Fiselier84006862016-06-02 00:15:35 +00003889#if _LIBCPP_STD_VER > 14
3890 _LIBCPP_INLINE_VISIBILITY
3891 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3892 { return __weak_this_; }
3893
3894 _LIBCPP_INLINE_VISIBILITY
3895 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3896 { return __weak_this_; }
3897#endif // _LIBCPP_STD_VER > 14
3898
Howard Hinnantc51e1022010-05-11 19:42:16 +00003899 template <class _Up> friend class shared_ptr;
3900};
3901
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003902template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003903struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003904{
3905 typedef shared_ptr<_Tp> argument_type;
3906 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003907
Howard Hinnant756c69b2010-09-22 16:48:34 +00003908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003909 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003910 {
zoecarverd73f42a2020-05-11 18:42:50 -07003911 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003912 }
3913};
3914
Howard Hinnantc834c512011-11-29 18:15:50 +00003915template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003916inline _LIBCPP_INLINE_VISIBILITY
3917basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003918operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003919
Eric Fiselier9b492672016-06-18 02:12:53 +00003920
3921#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003922
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003923class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003924{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003925 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003926public:
3927 void lock() _NOEXCEPT;
3928 void unlock() _NOEXCEPT;
3929
3930private:
3931 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3932 __sp_mut(const __sp_mut&);
3933 __sp_mut& operator=(const __sp_mut&);
3934
Howard Hinnant8331b762013-03-06 23:30:19 +00003935 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003936};
3937
Mehdi Amini228053d2017-05-04 17:08:54 +00003938_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3939__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003940
3941template <class _Tp>
3942inline _LIBCPP_INLINE_VISIBILITY
3943bool
3944atomic_is_lock_free(const shared_ptr<_Tp>*)
3945{
3946 return false;
3947}
3948
3949template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003950_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003951shared_ptr<_Tp>
3952atomic_load(const shared_ptr<_Tp>* __p)
3953{
3954 __sp_mut& __m = __get_sp_mut(__p);
3955 __m.lock();
3956 shared_ptr<_Tp> __q = *__p;
3957 __m.unlock();
3958 return __q;
3959}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003960
Howard Hinnant9fa30202012-07-30 01:40:57 +00003961template <class _Tp>
3962inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003963_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003964shared_ptr<_Tp>
3965atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3966{
3967 return atomic_load(__p);
3968}
3969
3970template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003971_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003972void
3973atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3974{
3975 __sp_mut& __m = __get_sp_mut(__p);
3976 __m.lock();
3977 __p->swap(__r);
3978 __m.unlock();
3979}
3980
3981template <class _Tp>
3982inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003983_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003984void
3985atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3986{
3987 atomic_store(__p, __r);
3988}
3989
3990template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003991_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003992shared_ptr<_Tp>
3993atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3994{
3995 __sp_mut& __m = __get_sp_mut(__p);
3996 __m.lock();
3997 __p->swap(__r);
3998 __m.unlock();
3999 return __r;
4000}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004001
Howard Hinnant9fa30202012-07-30 01:40:57 +00004002template <class _Tp>
4003inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004004_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004005shared_ptr<_Tp>
4006atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4007{
4008 return atomic_exchange(__p, __r);
4009}
4010
4011template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004012_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004013bool
4014atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4015{
Marshall Clow4201ee82016-05-18 17:50:13 +00004016 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004017 __sp_mut& __m = __get_sp_mut(__p);
4018 __m.lock();
4019 if (__p->__owner_equivalent(*__v))
4020 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004021 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004022 *__p = __w;
4023 __m.unlock();
4024 return true;
4025 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004026 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004027 *__v = *__p;
4028 __m.unlock();
4029 return false;
4030}
4031
4032template <class _Tp>
4033inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004034_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004035bool
4036atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4037{
4038 return atomic_compare_exchange_strong(__p, __v, __w);
4039}
4040
4041template <class _Tp>
4042inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004043_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004044bool
4045atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4046 shared_ptr<_Tp> __w, memory_order, memory_order)
4047{
4048 return atomic_compare_exchange_strong(__p, __v, __w);
4049}
4050
4051template <class _Tp>
4052inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004053_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004054bool
4055atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4056 shared_ptr<_Tp> __w, memory_order, memory_order)
4057{
4058 return atomic_compare_exchange_weak(__p, __v, __w);
4059}
4060
Eric Fiselier9b492672016-06-18 02:12:53 +00004061#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004062
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004063//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004064#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4065# ifndef _LIBCPP_CXX03_LANG
4066enum class pointer_safety : unsigned char {
4067 relaxed,
4068 preferred,
4069 strict
4070};
4071# endif
4072#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004073struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004075 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076 {
4077 relaxed,
4078 preferred,
4079 strict
4080 };
4081
Howard Hinnant49e145e2012-10-30 19:06:59 +00004082 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083
Howard Hinnant756c69b2010-09-22 16:48:34 +00004084 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004085 pointer_safety() : __v_() {}
4086
4087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004088 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090 operator int() const {return __v_;}
4091};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004092#endif
4093
4094#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004095 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004096_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4097#else
4098// This function is only offered in C++03 under ABI v1.
4099# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4100inline _LIBCPP_INLINE_VISIBILITY
4101pointer_safety get_pointer_safety() _NOEXCEPT {
4102 return pointer_safety::relaxed;
4103}
4104# endif
4105#endif
4106
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004108_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4109_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4110_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004111_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112
4113template <class _Tp>
4114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004115_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116undeclare_reachable(_Tp* __p)
4117{
4118 return static_cast<_Tp*>(__undeclare_reachable(__p));
4119}
4120
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004121_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122
Marshall Clow8982dcd2015-07-13 20:04:56 +00004123// --- Helper for container swap --
4124template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004125_LIBCPP_INLINE_VISIBILITY
4126void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4127#if _LIBCPP_STD_VER >= 14
4128 _NOEXCEPT
4129#else
4130 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4131#endif
4132{
4133 using _VSTD::swap;
4134 swap(__a1, __a2);
4135}
4136
4137template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004138inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004139void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4140
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004141template <typename _Alloc>
4142inline _LIBCPP_INLINE_VISIBILITY
4143void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4144#if _LIBCPP_STD_VER >= 14
4145 _NOEXCEPT
4146#else
4147 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4148#endif
4149{
4150 _VSTD::__swap_allocator(__a1, __a2,
4151 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4152}
4153
Marshall Clowff91de82015-08-18 19:51:37 +00004154template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004155struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004156 _Traits::propagate_on_container_move_assignment::value
4157#if _LIBCPP_STD_VER > 14
4158 || _Traits::is_always_equal::value
4159#else
4160 && is_nothrow_move_assignable<_Alloc>::value
4161#endif
4162 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004163
Marshall Clowa591b9a2016-07-11 21:38:08 +00004164
Marshall Clowa591b9a2016-07-11 21:38:08 +00004165template <class _Tp, class _Alloc>
4166struct __temp_value {
4167 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004168
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004169 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004170 _Alloc &__a;
4171
4172 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4173 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004174
Marshall Clowa591b9a2016-07-11 21:38:08 +00004175 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004176 _LIBCPP_NO_CFI
4177 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4178 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4179 _VSTD::forward<_Args>(__args)...);
4180 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004181
Marshall Clowa591b9a2016-07-11 21:38:08 +00004182 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4183 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004184
Marshall Clowe46031a2018-07-02 18:41:15 +00004185template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004186struct __is_allocator : false_type {};
4187
4188template<typename _Alloc>
4189struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004190 typename __void_t<typename _Alloc::value_type>::type,
4191 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4192 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004193 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004194
Eric Fiselier74ebee62019-06-08 01:31:19 +00004195// __builtin_new_allocator -- A non-templated helper for allocating and
4196// deallocating memory using __builtin_operator_new and
4197// __builtin_operator_delete. It should be used in preference to
4198// `std::allocator<T>` to avoid additional instantiations.
4199struct __builtin_new_allocator {
4200 struct __builtin_new_deleter {
4201 typedef void* pointer_type;
4202
4203 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4204 : __size_(__size), __align_(__align) {}
4205
4206 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004207 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004208 }
4209
4210 private:
4211 size_t __size_;
4212 size_t __align_;
4213 };
4214
4215 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4216
4217 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004218 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004219 __builtin_new_deleter(__s, __align));
4220 }
4221
4222 static void __deallocate_bytes(void* __p, size_t __s,
4223 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004224 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004225 }
4226
4227 template <class _Tp>
4228 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4229 static __holder_t __allocate_type(size_t __n) {
4230 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4231 }
4232
4233 template <class _Tp>
4234 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4235 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4236 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4237 }
4238};
4239
4240
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241_LIBCPP_END_NAMESPACE_STD
4242
Eric Fiselierf4433a32017-05-31 22:07:49 +00004243_LIBCPP_POP_MACROS
4244
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004245#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004246# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004247#endif
4248
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249#endif // _LIBCPP_MEMORY