blob: f81ff6680f9fdfd0c4866f91c6f62ddaebf66c9c [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>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000685#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000686# include <atomic>
687#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000688#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000689
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000690#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000691#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000692#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693
Eric Fiselierf4433a32017-05-31 22:07:49 +0000694_LIBCPP_PUSH_MACROS
695#include <__undef_macros>
696
697
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698_LIBCPP_BEGIN_NAMESPACE_STD
699
Eric Fiselier89659d12015-07-07 00:27:16 +0000700template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000701inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000702_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
703#if !defined(_LIBCPP_HAS_NO_THREADS) && \
704 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000705 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000706 return __atomic_load_n(__value, __ATOMIC_RELAXED);
707#else
708 return *__value;
709#endif
710}
711
Kuba Breckade9d6792016-09-04 09:55:12 +0000712template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000713inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000714_ValueType __libcpp_acquire_load(_ValueType const* __value) {
715#if !defined(_LIBCPP_HAS_NO_THREADS) && \
716 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000717 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000718 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
719#else
720 return *__value;
721#endif
722}
723
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500724template <bool _UsePointerTraits> struct __to_address_helper;
725
726template <> struct __to_address_helper<true> {
727 template <class _Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500728 using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500729
730 template <class _Pointer>
731 _LIBCPP_CONSTEXPR
732 static __return_type<_Pointer>
733 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
734};
735
736template <class _Pointer, bool _Dummy = true>
737using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
738
739
Howard Hinnantc834c512011-11-29 18:15:50 +0000740template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000741inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +0000742_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500743__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744{
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500745 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 return __p;
747}
748
749template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500750inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
751typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
752__to_address(const _Pointer& __p) _NOEXCEPT {
753 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000754}
755
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500756template <> struct __to_address_helper<false> {
757 template <class _Pointer>
758 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000759
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500760 template <class _Pointer>
761 _LIBCPP_CONSTEXPR
762 static __return_type<_Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500763 __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500764};
765
766
767#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000768template <class _Tp>
769inline _LIBCPP_INLINE_VISIBILITY constexpr
770_Tp*
771to_address(_Tp* __p) _NOEXCEPT
772{
773 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
774 return __p;
775}
776
777template <class _Pointer>
Marek Kurdej1f0cde92020-12-06 15:23:46 +0100778inline _LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000779auto
780to_address(const _Pointer& __p) _NOEXCEPT
781{
Eric Fiselierc1b87a72019-11-16 17:13:26 -0500782 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +0000783}
784#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785
Louis Dionnefa621d72020-12-10 18:28:13 -0500786template <class _Tp> class allocator;
Marshall Clow0be70c32017-06-14 21:23:57 +0000787
Louis Dionnefa621d72020-12-10 18:28:13 -0500788#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
789template <>
790class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792public:
Louis Dionnefa621d72020-12-10 18:28:13 -0500793 typedef void* pointer;
794 typedef const void* const_pointer;
795 typedef void value_type;
796
797 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798};
799
Louis Dionnefa621d72020-12-10 18:28:13 -0500800template <>
801class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802{
Louis Dionnefa621d72020-12-10 18:28:13 -0500803public:
804 typedef const void* pointer;
805 typedef const void* const_pointer;
806 typedef const void value_type;
807
808 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809};
Louis Dionne99f59432020-09-17 12:06:13 -0400810#endif
Marshall Clow940e01c2015-04-07 05:21:38 +0000811
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812// allocator
813
814template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000815class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816{
817public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400818 typedef size_t size_type;
819 typedef ptrdiff_t difference_type;
820 typedef _Tp value_type;
821 typedef true_type propagate_on_container_move_assignment;
822 typedef true_type is_always_equal;
823
824 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
825 allocator() _NOEXCEPT { }
826
827 template <class _Up>
828 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
829 allocator(const allocator<_Up>&) _NOEXCEPT { }
830
831 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
832 _Tp* allocate(size_t __n) {
833 if (__n > allocator_traits<allocator>::max_size(*this))
834 __throw_length_error("allocator<T>::allocate(size_t n)"
835 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400836 if (__libcpp_is_constant_evaluated()) {
837 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
838 } else {
839 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
840 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400841 }
842
843 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
844 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400845 if (__libcpp_is_constant_evaluated()) {
846 ::operator delete(__p);
847 } else {
848 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
849 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400850 }
851
852 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500853#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -0500854 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
855 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
856 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
857 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
858
Louis Dionne481a2662018-09-23 18:35:00 +0000859 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400860 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
861 typedef allocator<_Up> other;
862 };
Marshall Clowbc759762018-03-20 23:02:53 +0000863
Michael Park99f9e912020-03-04 11:27:14 -0500864 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400865 pointer address(reference __x) const _NOEXCEPT {
866 return _VSTD::addressof(__x);
867 }
Michael Park99f9e912020-03-04 11:27:14 -0500868 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400869 const_pointer address(const_reference __x) const _NOEXCEPT {
870 return _VSTD::addressof(__x);
871 }
Michael Park99f9e912020-03-04 11:27:14 -0500872
Michael Park99f9e912020-03-04 11:27:14 -0500873 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400874 _Tp* allocate(size_t __n, const void*) {
875 return allocate(__n);
876 }
Michael Park99f9e912020-03-04 11:27:14 -0500877
Louis Dionnef8b6c022020-09-21 10:36:37 -0400878 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
879 return size_type(~0) / sizeof(_Tp);
880 }
Michael Park99f9e912020-03-04 11:27:14 -0500881
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400883 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
884 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500885 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400886 }
887
888 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
889 void destroy(pointer __p) {
890 __p->~_Tp();
891 }
Michael Park99f9e912020-03-04 11:27:14 -0500892#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893};
894
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000895template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000896class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000897{
898public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400899 typedef size_t size_type;
900 typedef ptrdiff_t difference_type;
901 typedef const _Tp value_type;
902 typedef true_type propagate_on_container_move_assignment;
903 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000904
Marshall Clowbc759762018-03-20 23:02:53 +0000905 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400906 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +0000907
908 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +0000909 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400910 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -0500911
Louis Dionne99f59432020-09-17 12:06:13 -0400912 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400913 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -0400914 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +0000915 __throw_length_error("allocator<const T>::allocate(size_t n)"
916 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400917 if (__libcpp_is_constant_evaluated()) {
918 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
919 } else {
920 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
921 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000922 }
Michael Park99f9e912020-03-04 11:27:14 -0500923
Louis Dionne99f59432020-09-17 12:06:13 -0400924 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400925 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400926 if (__libcpp_is_constant_evaluated()) {
927 ::operator delete(const_cast<_Tp*>(__p));
928 } else {
929 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
930 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400931 }
Michael Park99f9e912020-03-04 11:27:14 -0500932
Louis Dionnef8b6c022020-09-21 10:36:37 -0400933 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500934#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -0400935 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
936 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
937 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
938 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
939
940 template <class _Up>
941 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
942 typedef allocator<_Up> other;
943 };
944
945 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
946 const_pointer address(const_reference __x) const _NOEXCEPT {
947 return _VSTD::addressof(__x);
948 }
949
950 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
951 const _Tp* allocate(size_t __n, const void*) {
952 return allocate(__n);
953 }
954
955 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
956 return size_type(~0) / sizeof(_Tp);
957 }
Michael Park99f9e912020-03-04 11:27:14 -0500958
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000959 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400960 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
961 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500962 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400963 }
Howard Hinnant9e028f12012-05-01 15:37:54 +0000964
Louis Dionnef8b6c022020-09-21 10:36:37 -0400965 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
966 void destroy(pointer __p) {
967 __p->~_Tp();
968 }
Michael Park99f9e912020-03-04 11:27:14 -0500969#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000970};
971
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400973inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000974bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975
976template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400977inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000978bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979
Louis Dionned6651542020-11-03 12:05:55 -0500980template <class _Alloc, class _Ptr>
981_LIBCPP_INLINE_VISIBILITY
982void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
983 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500984 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500985 typedef allocator_traits<_Alloc> _Traits;
986 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
987 _Traits::construct(__a, _VSTD::__to_address(__begin2),
988#ifdef _LIBCPP_NO_EXCEPTIONS
989 _VSTD::move(*__begin1)
990#else
991 _VSTD::move_if_noexcept(*__begin1)
992#endif
993 );
994 }
995}
996
997template <class _Alloc, class _Tp, typename enable_if<
998 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
999 is_trivially_move_constructible<_Tp>::value
1000>::type>
1001_LIBCPP_INLINE_VISIBILITY
1002void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1003 ptrdiff_t _Np = __end1 - __begin1;
1004 if (_Np > 0) {
1005 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1006 __begin2 += _Np;
1007 }
1008}
1009
1010template <class _Alloc, class _Iter, class _Ptr>
1011_LIBCPP_INLINE_VISIBILITY
1012void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1013 typedef allocator_traits<_Alloc> _Traits;
1014 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1015 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1016 }
1017}
1018
1019template <class _Alloc, class _Source, class _Dest,
1020 class _RawSource = typename remove_const<_Source>::type,
1021 class _RawDest = typename remove_const<_Dest>::type,
1022 class =
1023 typename enable_if<
1024 is_trivially_copy_constructible<_Dest>::value &&
1025 is_same<_RawSource, _RawDest>::value &&
1026 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1027 >::type>
1028_LIBCPP_INLINE_VISIBILITY
1029void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1030 ptrdiff_t _Np = __end1 - __begin1;
1031 if (_Np > 0) {
1032 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1033 __begin2 += _Np;
1034 }
1035}
1036
1037template <class _Alloc, class _Ptr>
1038_LIBCPP_INLINE_VISIBILITY
1039void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1040 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1041 "The specified type does not meet the requirements of Cpp17MoveInsertable");
1042 typedef allocator_traits<_Alloc> _Traits;
1043 while (__end1 != __begin1) {
1044 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1045#ifdef _LIBCPP_NO_EXCEPTIONS
1046 _VSTD::move(*--__end1)
1047#else
1048 _VSTD::move_if_noexcept(*--__end1)
1049#endif
1050 );
1051 --__end2;
1052 }
1053}
1054
1055template <class _Alloc, class _Tp, class = typename enable_if<
1056 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1057 is_trivially_move_constructible<_Tp>::value
1058>::type>
1059_LIBCPP_INLINE_VISIBILITY
1060void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1061 ptrdiff_t _Np = __end1 - __begin1;
1062 __end2 -= _Np;
1063 if (_Np > 0)
1064 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1065}
1066
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001068class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 : public iterator<output_iterator_tag,
1070 _Tp, // purposefully not C++03
1071 ptrdiff_t, // purposefully not C++03
1072 _Tp*, // purposefully not C++03
1073 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1074{
1075private:
1076 _OutputIterator __x_;
1077public:
1078 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1079 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1080 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001081 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001082#if _LIBCPP_STD_VER >= 14
1083 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001084 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001085#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1087 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1088 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001089#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001090 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001091#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092};
1093
1094template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001095_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001097get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098{
1099 pair<_Tp*, ptrdiff_t> __r(0, 0);
1100 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1101 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1102 / sizeof(_Tp);
1103 if (__n > __m)
1104 __n = __m;
1105 while (__n > 0)
1106 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001107#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001108 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001109 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001110 align_val_t __al =
1111 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001112 __r.first = static_cast<_Tp*>(::operator new(
1113 __n * sizeof(_Tp), __al, nothrow));
1114 } else {
1115 __r.first = static_cast<_Tp*>(::operator new(
1116 __n * sizeof(_Tp), nothrow));
1117 }
1118#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001119 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001120 {
1121 // Since aligned operator new is unavailable, return an empty
1122 // buffer rather than one with invalid alignment.
1123 return __r;
1124 }
1125
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001127#endif
1128
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 if (__r.first)
1130 {
1131 __r.second = __n;
1132 break;
1133 }
1134 __n /= 2;
1135 }
1136 return __r;
1137}
1138
1139template <class _Tp>
1140inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001141void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1142{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001143 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001144}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145
Marshall Clowb22274f2017-01-24 22:22:33 +00001146#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001148struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149{
1150 _Tp* __ptr_;
1151};
1152
1153template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001154class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155{
1156private:
1157 _Tp* __ptr_;
1158public:
1159 typedef _Tp element_type;
1160
Dimitry Andric47269ce2020-03-13 19:36:26 +01001161 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1162 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1163 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001165 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001167 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001169 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001171 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172
Dimitry Andric47269ce2020-03-13 19:36:26 +01001173 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001175 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1176 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1177 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 {
1179 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001180 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 return __t;
1182 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001183 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 {
1185 if (__ptr_ != __p)
1186 delete __ptr_;
1187 __ptr_ = __p;
1188 }
1189
Dimitry Andric47269ce2020-03-13 19:36:26 +01001190 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1191 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001193 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 {return auto_ptr<_Up>(release());}
1195};
1196
1197template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001198class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199{
1200public:
1201 typedef void element_type;
1202};
Marshall Clowb22274f2017-01-24 22:22:33 +00001203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001205// Tag used to default initialize one or both of the pair's elements.
1206struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001207struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001208
Eric Fiselier9d355982017-04-12 23:45:53 +00001209template <class _Tp, int _Idx,
1210 bool _CanBeEmptyBase =
1211 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1212struct __compressed_pair_elem {
1213 typedef _Tp _ParamT;
1214 typedef _Tp& reference;
1215 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001217 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001218 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001219 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1220 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221
Eric Fiselier9d355982017-04-12 23:45:53 +00001222 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001223 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1224 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001225 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001226 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001227 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001228 : __value_(_VSTD::forward<_Up>(__u))
1229 {
1230 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001232
1233#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001234 template <class... _Args, size_t... _Indexes>
1235 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1236 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1237 __tuple_indices<_Indexes...>)
1238 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001239#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001241
Alex Lorenz76132112017-11-09 17:54:49 +00001242 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1243 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001244 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001247 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248};
1249
Eric Fiselier9d355982017-04-12 23:45:53 +00001250template <class _Tp, int _Idx>
1251struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1252 typedef _Tp _ParamT;
1253 typedef _Tp& reference;
1254 typedef const _Tp& const_reference;
1255 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001257 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1258 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1259 __compressed_pair_elem(__default_init_tag) {}
1260 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1261 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262
Eric Fiselier9d355982017-04-12 23:45:53 +00001263 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001264 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1265 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001266 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001267 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001268 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001269 : __value_type(_VSTD::forward<_Up>(__u))
1270 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001272#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001273 template <class... _Args, size_t... _Indexes>
1274 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1275 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1276 __tuple_indices<_Indexes...>)
1277 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001278#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279
Alex Lorenz76132112017-11-09 17:54:49 +00001280 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1281 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001282 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283};
1284
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00001286class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1287 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001288 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1289 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00001290
1291 // NOTE: This static assert should never fire because __compressed_pair
1292 // is *almost never* used in a scenario where it's possible for T1 == T2.
1293 // (The exception is std::function where it is possible that the function
1294 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00001295 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05001296 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00001297 "The current implementation is NOT ABI-compatible with the previous "
1298 "implementation for this configuration");
1299
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001301 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00001302 class = typename enable_if<
1303 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1304 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1305 >::type
1306 >
Eric Fiselier9d355982017-04-12 23:45:53 +00001307 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001308 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309
Eric Fiselier9d355982017-04-12 23:45:53 +00001310 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001311 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00001312 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001313 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001314
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001315#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001316 template <class... _Args1, class... _Args2>
1317 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1318 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1319 tuple<_Args2...> __second_args)
1320 : _Base1(__pc, _VSTD::move(__first_args),
1321 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1322 _Base2(__pc, _VSTD::move(__second_args),
1323 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001324#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
Eric Fiselier9d355982017-04-12 23:45:53 +00001326 _LIBCPP_INLINE_VISIBILITY
1327 typename _Base1::reference first() _NOEXCEPT {
1328 return static_cast<_Base1&>(*this).__get();
1329 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330
Eric Fiselier9d355982017-04-12 23:45:53 +00001331 _LIBCPP_INLINE_VISIBILITY
1332 typename _Base1::const_reference first() const _NOEXCEPT {
1333 return static_cast<_Base1 const&>(*this).__get();
1334 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335
Eric Fiselier9d355982017-04-12 23:45:53 +00001336 _LIBCPP_INLINE_VISIBILITY
1337 typename _Base2::reference second() _NOEXCEPT {
1338 return static_cast<_Base2&>(*this).__get();
1339 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
Eric Fiselier9d355982017-04-12 23:45:53 +00001341 _LIBCPP_INLINE_VISIBILITY
1342 typename _Base2::const_reference second() const _NOEXCEPT {
1343 return static_cast<_Base2 const&>(*this).__get();
1344 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
Eric Fiselier9d355982017-04-12 23:45:53 +00001346 _LIBCPP_INLINE_VISIBILITY
1347 void swap(__compressed_pair& __x)
1348 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1349 __is_nothrow_swappable<_T2>::value)
1350 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001351 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00001352 swap(first(), __x.first());
1353 swap(second(), __x.second());
1354 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355};
1356
1357template <class _T1, class _T2>
1358inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001359void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1360 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1361 __is_nothrow_swappable<_T2>::value) {
1362 __x.swap(__y);
1363}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001365// default_delete
1366
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001368struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001369 static_assert(!is_function<_Tp>::value,
1370 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001371#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001372 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001373#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001374 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001375#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001376 template <class _Up>
1377 _LIBCPP_INLINE_VISIBILITY
1378 default_delete(const default_delete<_Up>&,
1379 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1380 0) _NOEXCEPT {}
1381
1382 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1383 static_assert(sizeof(_Tp) > 0,
1384 "default_delete can not delete incomplete type");
1385 static_assert(!is_void<_Tp>::value,
1386 "default_delete can not delete incomplete type");
1387 delete __ptr;
1388 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389};
1390
1391template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001392struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1393private:
1394 template <class _Up>
1395 struct _EnableIfConvertible
1396 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1397
Howard Hinnant4500ca52011-12-18 21:19:44 +00001398public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001399#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001400 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001401#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001402 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001403#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001404
1405 template <class _Up>
1406 _LIBCPP_INLINE_VISIBILITY
1407 default_delete(const default_delete<_Up[]>&,
1408 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1409
1410 template <class _Up>
1411 _LIBCPP_INLINE_VISIBILITY
1412 typename _EnableIfConvertible<_Up>::type
1413 operator()(_Up* __ptr) const _NOEXCEPT {
1414 static_assert(sizeof(_Tp) > 0,
1415 "default_delete can not delete incomplete type");
1416 static_assert(!is_void<_Tp>::value,
1417 "default_delete can not delete void type");
1418 delete[] __ptr;
1419 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420};
1421
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001422template <class _Deleter>
1423struct __unique_ptr_deleter_sfinae {
1424 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1425 typedef const _Deleter& __lval_ref_type;
1426 typedef _Deleter&& __good_rval_ref_type;
1427 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428};
1429
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001430template <class _Deleter>
1431struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1432 typedef const _Deleter& __lval_ref_type;
1433 typedef const _Deleter&& __bad_rval_ref_type;
1434 typedef false_type __enable_rval_overload;
1435};
1436
1437template <class _Deleter>
1438struct __unique_ptr_deleter_sfinae<_Deleter&> {
1439 typedef _Deleter& __lval_ref_type;
1440 typedef _Deleter&& __bad_rval_ref_type;
1441 typedef false_type __enable_rval_overload;
1442};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001443
Vy Nguyene369bd92020-07-13 12:34:37 -04001444#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1445# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1446#else
1447# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1448#endif
1449
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001450template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001451class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001452public:
1453 typedef _Tp element_type;
1454 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001455 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001456
1457 static_assert(!is_rvalue_reference<deleter_type>::value,
1458 "the specified deleter type cannot be an rvalue reference");
1459
1460private:
1461 __compressed_pair<pointer, deleter_type> __ptr_;
1462
1463 struct __nat { int __for_bool_; };
1464
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001465 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001466
1467 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001468 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001469 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001470
1471 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001472 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001473 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001474
1475 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001476 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001477 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001478
1479 template <bool _Dummy, class _Deleter = typename __dependent_type<
1480 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001481 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001482 typename enable_if<is_default_constructible<_Deleter>::value &&
1483 !is_pointer<_Deleter>::value>::type;
1484
1485 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001486 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001487 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1488
1489 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001490 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001491 is_convertible<typename _UPtr::pointer, pointer>::value &&
1492 !is_array<_Up>::value
1493 >::type;
1494
1495 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001496 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001497 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1498 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1499 >::type;
1500
1501 template <class _UDel>
1502 using _EnableIfDeleterAssignable = typename enable_if<
1503 is_assignable<_Dp&, _UDel&&>::value
1504 >::type;
1505
1506public:
1507 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001508 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001509 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001510 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001511
1512 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001513 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001514 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001515 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001516
1517 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001518 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001519 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001520 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001521
1522 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001523 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001524 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001525 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001526 : __ptr_(__p, __d) {}
1527
1528 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001529 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001530 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001531 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001532 : __ptr_(__p, _VSTD::move(__d)) {
1533 static_assert(!is_reference<deleter_type>::value,
1534 "rvalue deleter bound to reference");
1535 }
1536
1537 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001538 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001539 _LIBCPP_INLINE_VISIBILITY
1540 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1541
1542 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001543 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001544 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1545 }
1546
1547 template <class _Up, class _Ep,
1548 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1549 class = _EnableIfDeleterConvertible<_Ep>
1550 >
1551 _LIBCPP_INLINE_VISIBILITY
1552 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1553 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1554
1555#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1556 template <class _Up>
1557 _LIBCPP_INLINE_VISIBILITY
1558 unique_ptr(auto_ptr<_Up>&& __p,
1559 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001560 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001561 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001562 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001563#endif
1564
1565 _LIBCPP_INLINE_VISIBILITY
1566 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1567 reset(__u.release());
1568 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1569 return *this;
1570 }
1571
1572 template <class _Up, class _Ep,
1573 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1574 class = _EnableIfDeleterAssignable<_Ep>
1575 >
1576 _LIBCPP_INLINE_VISIBILITY
1577 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1578 reset(__u.release());
1579 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1580 return *this;
1581 }
1582
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001583#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1584 template <class _Up>
1585 _LIBCPP_INLINE_VISIBILITY
1586 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1587 is_same<_Dp, default_delete<_Tp> >::value,
1588 unique_ptr&>::type
1589 operator=(auto_ptr<_Up> __p) {
1590 reset(__p.release());
1591 return *this;
1592 }
1593#endif
1594
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001595#ifdef _LIBCPP_CXX03_LANG
1596 unique_ptr(unique_ptr const&) = delete;
1597 unique_ptr& operator=(unique_ptr const&) = delete;
1598#endif
1599
1600
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001601 _LIBCPP_INLINE_VISIBILITY
1602 ~unique_ptr() { reset(); }
1603
1604 _LIBCPP_INLINE_VISIBILITY
1605 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1606 reset();
1607 return *this;
1608 }
1609
1610 _LIBCPP_INLINE_VISIBILITY
1611 typename add_lvalue_reference<_Tp>::type
1612 operator*() const {
1613 return *__ptr_.first();
1614 }
1615 _LIBCPP_INLINE_VISIBILITY
1616 pointer operator->() const _NOEXCEPT {
1617 return __ptr_.first();
1618 }
1619 _LIBCPP_INLINE_VISIBILITY
1620 pointer get() const _NOEXCEPT {
1621 return __ptr_.first();
1622 }
1623 _LIBCPP_INLINE_VISIBILITY
1624 deleter_type& get_deleter() _NOEXCEPT {
1625 return __ptr_.second();
1626 }
1627 _LIBCPP_INLINE_VISIBILITY
1628 const deleter_type& get_deleter() const _NOEXCEPT {
1629 return __ptr_.second();
1630 }
1631 _LIBCPP_INLINE_VISIBILITY
1632 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1633 return __ptr_.first() != nullptr;
1634 }
1635
1636 _LIBCPP_INLINE_VISIBILITY
1637 pointer release() _NOEXCEPT {
1638 pointer __t = __ptr_.first();
1639 __ptr_.first() = pointer();
1640 return __t;
1641 }
1642
1643 _LIBCPP_INLINE_VISIBILITY
1644 void reset(pointer __p = pointer()) _NOEXCEPT {
1645 pointer __tmp = __ptr_.first();
1646 __ptr_.first() = __p;
1647 if (__tmp)
1648 __ptr_.second()(__tmp);
1649 }
1650
1651 _LIBCPP_INLINE_VISIBILITY
1652 void swap(unique_ptr& __u) _NOEXCEPT {
1653 __ptr_.swap(__u.__ptr_);
1654 }
1655};
1656
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001657
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001659class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001661 typedef _Tp element_type;
1662 typedef _Dp deleter_type;
1663 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1664
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001666 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667
Eric Fiselier31127cd2017-04-16 02:14:31 +00001668 template <class _From>
1669 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670
Eric Fiselier31127cd2017-04-16 02:14:31 +00001671 template <class _FromElem>
1672 struct _CheckArrayPointerConversion<_FromElem*>
1673 : integral_constant<bool,
1674 is_same<_FromElem*, pointer>::value ||
1675 (is_same<pointer, element_type*>::value &&
1676 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1677 >
1678 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679
Eric Fiselier31127cd2017-04-16 02:14:31 +00001680 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001681
1682 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001683 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001684 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001685
1686 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001687 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001688 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001689
1690 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001691 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001692 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001693
1694 template <bool _Dummy, class _Deleter = typename __dependent_type<
1695 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001696 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001697 typename enable_if<is_default_constructible<_Deleter>::value &&
1698 !is_pointer<_Deleter>::value>::type;
1699
1700 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001701 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001702 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1703
1704 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001705 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001706 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001707 >::type;
1708
1709 template <class _UPtr, class _Up,
1710 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001711 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001712 is_array<_Up>::value &&
1713 is_same<pointer, element_type*>::value &&
1714 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1715 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1716 >::type;
1717
1718 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001719 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001720 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1721 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1722 >::type;
1723
1724 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001725 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001726 is_assignable<_Dp&, _UDel&&>::value
1727 >::type;
1728
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001730 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001731 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001732 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001733 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001735 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001736 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001737 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001738 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001740 template <class _Pp, bool _Dummy = true,
1741 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001742 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001743 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001744 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001745 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001747 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001748 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1749 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001750 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001751 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001752 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001754 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001755 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001756 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001757 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001758 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001760 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001761 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1762 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001763 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001764 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001765 : __ptr_(__p, _VSTD::move(__d)) {
1766 static_assert(!is_reference<deleter_type>::value,
1767 "rvalue deleter bound to reference");
1768 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001770 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001771 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001772 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001773 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001774 : __ptr_(nullptr, _VSTD::move(__d)) {
1775 static_assert(!is_reference<deleter_type>::value,
1776 "rvalue deleter bound to reference");
1777 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001778
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001779 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001780 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1781 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001782 _LIBCPP_INLINE_VISIBILITY
1783 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001784
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001785 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001786 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001787 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1788 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001789
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001790 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001791 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001792 reset(__u.release());
1793 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1794 return *this;
1795 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001797 template <class _Up, class _Ep,
1798 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1799 class = _EnableIfDeleterConvertible<_Ep>
1800 >
1801 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001802 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001803 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001804 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001806 template <class _Up, class _Ep,
1807 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1808 class = _EnableIfDeleterAssignable<_Ep>
1809 >
1810 _LIBCPP_INLINE_VISIBILITY
1811 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001812 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001813 reset(__u.release());
1814 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1815 return *this;
1816 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001818#ifdef _LIBCPP_CXX03_LANG
1819 unique_ptr(unique_ptr const&) = delete;
1820 unique_ptr& operator=(unique_ptr const&) = delete;
1821#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001822
1823public:
1824 _LIBCPP_INLINE_VISIBILITY
1825 ~unique_ptr() { reset(); }
1826
1827 _LIBCPP_INLINE_VISIBILITY
1828 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1829 reset();
1830 return *this;
1831 }
1832
1833 _LIBCPP_INLINE_VISIBILITY
1834 typename add_lvalue_reference<_Tp>::type
1835 operator[](size_t __i) const {
1836 return __ptr_.first()[__i];
1837 }
1838 _LIBCPP_INLINE_VISIBILITY
1839 pointer get() const _NOEXCEPT {
1840 return __ptr_.first();
1841 }
1842
1843 _LIBCPP_INLINE_VISIBILITY
1844 deleter_type& get_deleter() _NOEXCEPT {
1845 return __ptr_.second();
1846 }
1847
1848 _LIBCPP_INLINE_VISIBILITY
1849 const deleter_type& get_deleter() const _NOEXCEPT {
1850 return __ptr_.second();
1851 }
1852 _LIBCPP_INLINE_VISIBILITY
1853 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1854 return __ptr_.first() != nullptr;
1855 }
1856
1857 _LIBCPP_INLINE_VISIBILITY
1858 pointer release() _NOEXCEPT {
1859 pointer __t = __ptr_.first();
1860 __ptr_.first() = pointer();
1861 return __t;
1862 }
1863
1864 template <class _Pp>
1865 _LIBCPP_INLINE_VISIBILITY
1866 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001867 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001868 >::type
1869 reset(_Pp __p) _NOEXCEPT {
1870 pointer __tmp = __ptr_.first();
1871 __ptr_.first() = __p;
1872 if (__tmp)
1873 __ptr_.second()(__tmp);
1874 }
1875
1876 _LIBCPP_INLINE_VISIBILITY
1877 void reset(nullptr_t = nullptr) _NOEXCEPT {
1878 pointer __tmp = __ptr_.first();
1879 __ptr_.first() = nullptr;
1880 if (__tmp)
1881 __ptr_.second()(__tmp);
1882 }
1883
1884 _LIBCPP_INLINE_VISIBILITY
1885 void swap(unique_ptr& __u) _NOEXCEPT {
1886 __ptr_.swap(__u.__ptr_);
1887 }
1888
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889};
1890
1891template <class _Tp, class _Dp>
1892inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001893typename enable_if<
1894 __is_swappable<_Dp>::value,
1895 void
1896>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001897swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898
1899template <class _T1, class _D1, class _T2, class _D2>
1900inline _LIBCPP_INLINE_VISIBILITY
1901bool
1902operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1903
1904template <class _T1, class _D1, class _T2, class _D2>
1905inline _LIBCPP_INLINE_VISIBILITY
1906bool
1907operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1908
1909template <class _T1, class _D1, class _T2, class _D2>
1910inline _LIBCPP_INLINE_VISIBILITY
1911bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001912operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1913{
1914 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1915 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001916 typedef typename common_type<_P1, _P2>::type _Vp;
1917 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001918}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919
1920template <class _T1, class _D1, class _T2, class _D2>
1921inline _LIBCPP_INLINE_VISIBILITY
1922bool
1923operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1924
1925template <class _T1, class _D1, class _T2, class _D2>
1926inline _LIBCPP_INLINE_VISIBILITY
1927bool
1928operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1929
1930template <class _T1, class _D1, class _T2, class _D2>
1931inline _LIBCPP_INLINE_VISIBILITY
1932bool
1933operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1934
Howard Hinnantb17caf92012-02-21 21:02:58 +00001935template <class _T1, class _D1>
1936inline _LIBCPP_INLINE_VISIBILITY
1937bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001938operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001939{
1940 return !__x;
1941}
1942
1943template <class _T1, class _D1>
1944inline _LIBCPP_INLINE_VISIBILITY
1945bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001946operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001947{
1948 return !__x;
1949}
1950
1951template <class _T1, class _D1>
1952inline _LIBCPP_INLINE_VISIBILITY
1953bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001954operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001955{
1956 return static_cast<bool>(__x);
1957}
1958
1959template <class _T1, class _D1>
1960inline _LIBCPP_INLINE_VISIBILITY
1961bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001962operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001963{
1964 return static_cast<bool>(__x);
1965}
1966
1967template <class _T1, class _D1>
1968inline _LIBCPP_INLINE_VISIBILITY
1969bool
1970operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1971{
1972 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1973 return less<_P1>()(__x.get(), nullptr);
1974}
1975
1976template <class _T1, class _D1>
1977inline _LIBCPP_INLINE_VISIBILITY
1978bool
1979operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1980{
1981 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1982 return less<_P1>()(nullptr, __x.get());
1983}
1984
1985template <class _T1, class _D1>
1986inline _LIBCPP_INLINE_VISIBILITY
1987bool
1988operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1989{
1990 return nullptr < __x;
1991}
1992
1993template <class _T1, class _D1>
1994inline _LIBCPP_INLINE_VISIBILITY
1995bool
1996operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1997{
1998 return __x < nullptr;
1999}
2000
2001template <class _T1, class _D1>
2002inline _LIBCPP_INLINE_VISIBILITY
2003bool
2004operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2005{
2006 return !(nullptr < __x);
2007}
2008
2009template <class _T1, class _D1>
2010inline _LIBCPP_INLINE_VISIBILITY
2011bool
2012operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2013{
2014 return !(__x < nullptr);
2015}
2016
2017template <class _T1, class _D1>
2018inline _LIBCPP_INLINE_VISIBILITY
2019bool
2020operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2021{
2022 return !(__x < nullptr);
2023}
2024
2025template <class _T1, class _D1>
2026inline _LIBCPP_INLINE_VISIBILITY
2027bool
2028operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2029{
2030 return !(nullptr < __x);
2031}
2032
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002033#if _LIBCPP_STD_VER > 11
2034
2035template<class _Tp>
2036struct __unique_if
2037{
2038 typedef unique_ptr<_Tp> __unique_single;
2039};
2040
2041template<class _Tp>
2042struct __unique_if<_Tp[]>
2043{
2044 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2045};
2046
2047template<class _Tp, size_t _Np>
2048struct __unique_if<_Tp[_Np]>
2049{
2050 typedef void __unique_array_known_bound;
2051};
2052
2053template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002054inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002055typename __unique_if<_Tp>::__unique_single
2056make_unique(_Args&&... __args)
2057{
2058 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2059}
2060
2061template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002062inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002063typename __unique_if<_Tp>::__unique_array_unknown_bound
2064make_unique(size_t __n)
2065{
2066 typedef typename remove_extent<_Tp>::type _Up;
2067 return unique_ptr<_Tp>(new _Up[__n]());
2068}
2069
2070template<class _Tp, class... _Args>
2071 typename __unique_if<_Tp>::__unique_array_known_bound
2072 make_unique(_Args&&...) = delete;
2073
2074#endif // _LIBCPP_STD_VER > 11
2075
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002076template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002077#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002078struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002079#else
2080struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002081 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002082#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002083{
2084 typedef unique_ptr<_Tp, _Dp> argument_type;
2085 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002086 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002087 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002088 {
2089 typedef typename argument_type::pointer pointer;
2090 return hash<pointer>()(__ptr.get());
2091 }
2092};
2093
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094struct __destruct_n
2095{
2096private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002097 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098
2099 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002100 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002101 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102
2103 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002104 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 {}
2106
Howard Hinnant719bda32011-05-28 14:41:13 +00002107 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002108 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002109 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110 {}
2111
Howard Hinnant719bda32011-05-28 14:41:13 +00002112 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002113 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002114 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115 {}
2116public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002117 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002118 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119
2120 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002121 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002122 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123
2124 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002125 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002126 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127
2128 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002129 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002130 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131};
2132
2133template <class _Alloc>
2134class __allocator_destructor
2135{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002136 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002138 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2139 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140private:
2141 _Alloc& __alloc_;
2142 size_type __s_;
2143public:
2144 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002145 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002148 void operator()(pointer __p) _NOEXCEPT
2149 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150};
2151
2152template <class _InputIterator, class _ForwardIterator>
2153_ForwardIterator
2154uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2155{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002157#ifndef _LIBCPP_NO_EXCEPTIONS
2158 _ForwardIterator __s = __r;
2159 try
2160 {
2161#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002162 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002163 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002164#ifndef _LIBCPP_NO_EXCEPTIONS
2165 }
2166 catch (...)
2167 {
2168 for (; __s != __r; ++__s)
2169 __s->~value_type();
2170 throw;
2171 }
2172#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173 return __r;
2174}
2175
2176template <class _InputIterator, class _Size, class _ForwardIterator>
2177_ForwardIterator
2178uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2179{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002181#ifndef _LIBCPP_NO_EXCEPTIONS
2182 _ForwardIterator __s = __r;
2183 try
2184 {
2185#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002186 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002187 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002188#ifndef _LIBCPP_NO_EXCEPTIONS
2189 }
2190 catch (...)
2191 {
2192 for (; __s != __r; ++__s)
2193 __s->~value_type();
2194 throw;
2195 }
2196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197 return __r;
2198}
2199
2200template <class _ForwardIterator, class _Tp>
2201void
2202uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2203{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002205#ifndef _LIBCPP_NO_EXCEPTIONS
2206 _ForwardIterator __s = __f;
2207 try
2208 {
2209#endif
2210 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002211 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002212#ifndef _LIBCPP_NO_EXCEPTIONS
2213 }
2214 catch (...)
2215 {
2216 for (; __s != __f; ++__s)
2217 __s->~value_type();
2218 throw;
2219 }
2220#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221}
2222
2223template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002224_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2226{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002228#ifndef _LIBCPP_NO_EXCEPTIONS
2229 _ForwardIterator __s = __f;
2230 try
2231 {
2232#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002233 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002234 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002235#ifndef _LIBCPP_NO_EXCEPTIONS
2236 }
2237 catch (...)
2238 {
2239 for (; __s != __f; ++__s)
2240 __s->~value_type();
2241 throw;
2242 }
2243#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002244 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245}
2246
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002247#if _LIBCPP_STD_VER > 14
2248
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002249template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002250inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002251void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2252 for (; __first != __last; ++__first)
2253 _VSTD::destroy_at(_VSTD::addressof(*__first));
2254}
2255
2256template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002257inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002258_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2259 for (; __n > 0; (void)++__first, --__n)
2260 _VSTD::destroy_at(_VSTD::addressof(*__first));
2261 return __first;
2262}
2263
Eric Fiselier290c07c2016-10-11 21:13:44 +00002264template <class _ForwardIterator>
2265inline _LIBCPP_INLINE_VISIBILITY
2266void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2267 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2268 auto __idx = __first;
2269#ifndef _LIBCPP_NO_EXCEPTIONS
2270 try {
2271#endif
2272 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002273 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002274#ifndef _LIBCPP_NO_EXCEPTIONS
2275 } catch (...) {
2276 _VSTD::destroy(__first, __idx);
2277 throw;
2278 }
2279#endif
2280}
2281
2282template <class _ForwardIterator, class _Size>
2283inline _LIBCPP_INLINE_VISIBILITY
2284_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2285 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2286 auto __idx = __first;
2287#ifndef _LIBCPP_NO_EXCEPTIONS
2288 try {
2289#endif
2290 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002291 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002292 return __idx;
2293#ifndef _LIBCPP_NO_EXCEPTIONS
2294 } catch (...) {
2295 _VSTD::destroy(__first, __idx);
2296 throw;
2297 }
2298#endif
2299}
2300
2301
2302template <class _ForwardIterator>
2303inline _LIBCPP_INLINE_VISIBILITY
2304void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2305 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2306 auto __idx = __first;
2307#ifndef _LIBCPP_NO_EXCEPTIONS
2308 try {
2309#endif
2310 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002311 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002312#ifndef _LIBCPP_NO_EXCEPTIONS
2313 } catch (...) {
2314 _VSTD::destroy(__first, __idx);
2315 throw;
2316 }
2317#endif
2318}
2319
2320template <class _ForwardIterator, class _Size>
2321inline _LIBCPP_INLINE_VISIBILITY
2322_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2323 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2324 auto __idx = __first;
2325#ifndef _LIBCPP_NO_EXCEPTIONS
2326 try {
2327#endif
2328 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002329 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002330 return __idx;
2331#ifndef _LIBCPP_NO_EXCEPTIONS
2332 } catch (...) {
2333 _VSTD::destroy(__first, __idx);
2334 throw;
2335 }
2336#endif
2337}
2338
2339
2340template <class _InputIt, class _ForwardIt>
2341inline _LIBCPP_INLINE_VISIBILITY
2342_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2343 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2344 auto __idx = __first_res;
2345#ifndef _LIBCPP_NO_EXCEPTIONS
2346 try {
2347#endif
2348 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002349 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002350 return __idx;
2351#ifndef _LIBCPP_NO_EXCEPTIONS
2352 } catch (...) {
2353 _VSTD::destroy(__first_res, __idx);
2354 throw;
2355 }
2356#endif
2357}
2358
2359template <class _InputIt, class _Size, class _ForwardIt>
2360inline _LIBCPP_INLINE_VISIBILITY
2361pair<_InputIt, _ForwardIt>
2362uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2363 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2364 auto __idx = __first_res;
2365#ifndef _LIBCPP_NO_EXCEPTIONS
2366 try {
2367#endif
2368 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002369 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002370 return {__first, __idx};
2371#ifndef _LIBCPP_NO_EXCEPTIONS
2372 } catch (...) {
2373 _VSTD::destroy(__first_res, __idx);
2374 throw;
2375 }
2376#endif
2377}
2378
2379
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002380#endif // _LIBCPP_STD_VER > 14
2381
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002382// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2383// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00002384// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002385#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2386 && defined(__ATOMIC_RELAXED) \
2387 && defined(__ATOMIC_ACQ_REL)
2388# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002389#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002390# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2391#endif
2392
2393template <class _Tp>
2394inline _LIBCPP_INLINE_VISIBILITY _Tp
2395__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2396{
2397#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2398 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2399#else
2400 return __t += 1;
2401#endif
2402}
2403
2404template <class _Tp>
2405inline _LIBCPP_INLINE_VISIBILITY _Tp
2406__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2407{
2408#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2409 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2410#else
2411 return __t -= 1;
2412#endif
2413}
2414
Howard Hinnant756c69b2010-09-22 16:48:34 +00002415class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 : public std::exception
2417{
2418public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002419 bad_weak_ptr() _NOEXCEPT = default;
2420 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002421 virtual ~bad_weak_ptr() _NOEXCEPT;
2422 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423};
2424
Louis Dionne16fe2952018-07-11 23:14:33 +00002425_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002426void __throw_bad_weak_ptr()
2427{
2428#ifndef _LIBCPP_NO_EXCEPTIONS
2429 throw bad_weak_ptr();
2430#else
2431 _VSTD::abort();
2432#endif
2433}
2434
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002435template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002437class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438{
2439 __shared_count(const __shared_count&);
2440 __shared_count& operator=(const __shared_count&);
2441
2442protected:
2443 long __shared_owners_;
2444 virtual ~__shared_count();
2445private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002446 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447
2448public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002450 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 : __shared_owners_(__refs) {}
2452
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002453#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002454 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002455 void __add_shared() _NOEXCEPT;
2456 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002457#else
2458 _LIBCPP_INLINE_VISIBILITY
2459 void __add_shared() _NOEXCEPT {
2460 __libcpp_atomic_refcount_increment(__shared_owners_);
2461 }
2462 _LIBCPP_INLINE_VISIBILITY
2463 bool __release_shared() _NOEXCEPT {
2464 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2465 __on_zero_shared();
2466 return true;
2467 }
2468 return false;
2469 }
2470#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002471 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002472 long use_count() const _NOEXCEPT {
2473 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2474 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475};
2476
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002477class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 : private __shared_count
2479{
2480 long __shared_weak_owners_;
2481
2482public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002484 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485 : __shared_count(__refs),
2486 __shared_weak_owners_(__refs) {}
2487protected:
2488 virtual ~__shared_weak_count();
2489
2490public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002491#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002492 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002493 void __add_shared() _NOEXCEPT;
2494 void __add_weak() _NOEXCEPT;
2495 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002496#else
2497 _LIBCPP_INLINE_VISIBILITY
2498 void __add_shared() _NOEXCEPT {
2499 __shared_count::__add_shared();
2500 }
2501 _LIBCPP_INLINE_VISIBILITY
2502 void __add_weak() _NOEXCEPT {
2503 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2504 }
2505 _LIBCPP_INLINE_VISIBILITY
2506 void __release_shared() _NOEXCEPT {
2507 if (__shared_count::__release_shared())
2508 __release_weak();
2509 }
2510#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002511 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002513 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2514 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515
Howard Hinnant719bda32011-05-28 14:41:13 +00002516 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002518 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519};
2520
2521template <class _Tp, class _Dp, class _Alloc>
2522class __shared_ptr_pointer
2523 : public __shared_weak_count
2524{
2525 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2526public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002529 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530
Howard Hinnant72f73582010-08-11 17:04:31 +00002531#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002532 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002533#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534
2535private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002536 virtual void __on_zero_shared() _NOEXCEPT;
2537 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538};
2539
Howard Hinnant72f73582010-08-11 17:04:31 +00002540#ifndef _LIBCPP_NO_RTTI
2541
Howard Hinnantc51e1022010-05-11 19:42:16 +00002542template <class _Tp, class _Dp, class _Alloc>
2543const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002544__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002546 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547}
2548
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002549#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002550
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551template <class _Tp, class _Dp, class _Alloc>
2552void
Howard Hinnant719bda32011-05-28 14:41:13 +00002553__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554{
2555 __data_.first().second()(__data_.first().first());
2556 __data_.first().second().~_Dp();
2557}
2558
2559template <class _Tp, class _Dp, class _Alloc>
2560void
Howard Hinnant719bda32011-05-28 14:41:13 +00002561__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002563 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2564 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002565 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2566
Eric Fiselierf8898c82015-02-05 23:01:40 +00002567 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002569 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570}
2571
2572template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002573struct __shared_ptr_emplace
2574 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575{
Louis Dionne86549d72020-12-09 16:22:17 -05002576 _LIBCPP_HIDE_FROM_ABI
2577 explicit __shared_ptr_emplace(_Alloc __a)
2578 : __data_(_VSTD::move(__a), __value_init_tag())
2579 { }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580
Louis Dionne86549d72020-12-09 16:22:17 -05002581 template <class ..._Args>
2582 _LIBCPP_HIDE_FROM_ABI
2583 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002584#ifndef _LIBCPP_CXX03_LANG
Louis Dionne86549d72020-12-09 16:22:17 -05002585 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
2586 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...))
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002587#else
Louis Dionne86549d72020-12-09 16:22:17 -05002588 : __data_(__a, _Tp(_VSTD::forward<_Args>(__args)...))
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002589#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002590 { }
2591
2592 _LIBCPP_HIDE_FROM_ABI
2593 _Tp* __get_elem() _NOEXCEPT { return _VSTD::addressof(__data_.second()); }
2594
2595 _LIBCPP_HIDE_FROM_ABI
2596 _Alloc& __get_alloc() _NOEXCEPT { return __data_.first(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597
2598private:
Louis Dionne86549d72020-12-09 16:22:17 -05002599 virtual void __on_zero_shared() _NOEXCEPT {
2600 __get_elem()->~_Tp();
2601 }
2602
2603 virtual void __on_zero_shared_weak() _NOEXCEPT {
2604 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2605 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
2606 _ControlBlockAlloc __tmp(__get_alloc());
2607 __get_alloc().~_Alloc();
2608 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2609 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2610 }
2611
2612 __compressed_pair<_Alloc, _Tp> __data_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613};
2614
Erik Pilkington2a398762017-05-25 15:43:31 +00002615struct __shared_ptr_dummy_rebind_allocator_type;
2616template <>
2617class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2618{
2619public:
2620 template <class _Other>
2621 struct rebind
2622 {
2623 typedef allocator<_Other> other;
2624 };
2625};
2626
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002627template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628
zoecarverd73f42a2020-05-11 18:42:50 -07002629template<class _Tp, class _Up>
2630struct __compatible_with
2631#if _LIBCPP_STD_VER > 14
2632 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2633#else
2634 : is_convertible<_Tp*, _Up*> {};
2635#endif // _LIBCPP_STD_VER > 14
2636
Vy Nguyene369bd92020-07-13 12:34:37 -04002637#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2638# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2639#else
2640# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2641#endif
2642
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002644class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002646public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002647#if _LIBCPP_STD_VER > 14
2648 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002649 typedef remove_extent_t<_Tp> element_type;
2650#else
2651 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002652#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002653
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654private:
2655 element_type* __ptr_;
2656 __shared_weak_count* __cntrl_;
2657
2658 struct __nat {int __for_bool_;};
2659public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002661 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002663 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002664 template<class _Yp>
2665 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002666 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002667 template<class _Yp, class _Dp>
2668 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07002669 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002670 template<class _Yp, class _Dp, class _Alloc>
2671 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07002672 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2674 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002675 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002677 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002681 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002682 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002684 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002685 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002686 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002687 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002689 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002690#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002691 template<class _Yp>
2692 shared_ptr(auto_ptr<_Yp>&& __r,
2693 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002694#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002695 template <class _Yp, class _Dp>
2696 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2697 typename enable_if
2698 <
2699 !is_lvalue_reference<_Dp>::value &&
2700 !is_array<_Yp>::value &&
2701 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2702 __nat
2703 >::type = __nat());
2704 template <class _Yp, class _Dp>
2705 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2706 typename enable_if
2707 <
2708 is_lvalue_reference<_Dp>::value &&
2709 !is_array<_Yp>::value &&
2710 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2711 __nat
2712 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713
2714 ~shared_ptr();
2715
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002717 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002718 template<class _Yp>
2719 typename enable_if
2720 <
zoecarverd73f42a2020-05-11 18:42:50 -07002721 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002722 shared_ptr&
2723 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002725 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002727 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002728 template<class _Yp>
2729 typename enable_if
2730 <
zoecarverd73f42a2020-05-11 18:42:50 -07002731 __compatible_with<_Yp, element_type>::value,
2732 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002733 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002735 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002736#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002737 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002739 typename enable_if
2740 <
2741 !is_array<_Yp>::value &&
2742 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002743 shared_ptr
2744 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002745 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002746#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002747 template <class _Yp, class _Dp>
2748 typename enable_if
2749 <
2750 !is_array<_Yp>::value &&
2751 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2752 shared_ptr&
2753 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002755 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002758 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002760 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002761 template<class _Yp>
2762 typename enable_if
2763 <
zoecarverd73f42a2020-05-11 18:42:50 -07002764 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002765 void
2766 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002768 reset(_Yp* __p);
2769 template<class _Yp, class _Dp>
2770 typename enable_if
2771 <
zoecarverd73f42a2020-05-11 18:42:50 -07002772 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002773 void
2774 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002776 reset(_Yp* __p, _Dp __d);
2777 template<class _Yp, class _Dp, class _Alloc>
2778 typename enable_if
2779 <
zoecarverd73f42a2020-05-11 18:42:50 -07002780 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002781 void
2782 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002784 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785
Howard Hinnant756c69b2010-09-22 16:48:34 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002787 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002789 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2790 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002791 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002792 element_type* operator->() const _NOEXCEPT
2793 {
2794 static_assert(!_VSTD::is_array<_Tp>::value,
2795 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2796 return __ptr_;
2797 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002799 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002801 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002802 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002803 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002804 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002805 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002806 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002808 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002809 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002810 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002812 _LIBCPP_INLINE_VISIBILITY
2813 bool
2814 __owner_equivalent(const shared_ptr& __p) const
2815 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816
zoecarverd73f42a2020-05-11 18:42:50 -07002817#if _LIBCPP_STD_VER > 14
2818 typename add_lvalue_reference<element_type>::type
2819 _LIBCPP_INLINE_VISIBILITY
2820 operator[](ptrdiff_t __i) const
2821 {
2822 static_assert(_VSTD::is_array<_Tp>::value,
2823 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2824 return __ptr_[__i];
2825 }
2826#endif
2827
Howard Hinnant72f73582010-08-11 17:04:31 +00002828#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002831 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002832 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002833 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002834 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002835#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836
Zoe Carverd9040c72019-10-22 15:16:49 +00002837 template<class _Yp, class _CntrlBlk>
2838 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002839 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002840 {
2841 shared_ptr<_Tp> __r;
2842 __r.__ptr_ = __p;
2843 __r.__cntrl_ = __cntrl;
2844 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2845 return __r;
2846 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002847
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002849 template <class _Yp, bool = is_function<_Yp>::value>
2850 struct __shared_ptr_default_allocator
2851 {
2852 typedef allocator<_Yp> type;
2853 };
2854
2855 template <class _Yp>
2856 struct __shared_ptr_default_allocator<_Yp, true>
2857 {
2858 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2859 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002861 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002862 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002863 typename enable_if<is_convertible<_OrigPtr*,
2864 const enable_shared_from_this<_Yp>*
2865 >::value,
2866 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002867 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2868 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002870 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002871 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002872 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002873 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2874 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002875 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876 }
2877
Erik Pilkington2a398762017-05-25 15:43:31 +00002878 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879
zoecarverd73f42a2020-05-11 18:42:50 -07002880 template <class, class _Yp>
2881 struct __shared_ptr_default_delete
2882 : default_delete<_Yp> {};
2883
2884 template <class _Yp, class _Un, size_t _Sz>
2885 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2886 : default_delete<_Yp[]> {};
2887
2888 template <class _Yp, class _Un>
2889 struct __shared_ptr_default_delete<_Yp[], _Un>
2890 : default_delete<_Yp[]> {};
2891
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002892 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2893 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894};
2895
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002896#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2897template<class _Tp>
2898shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2899template<class _Tp, class _Dp>
2900shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2901#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002902
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002904inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002905_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002906shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002907 : __ptr_(nullptr),
2908 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909{
2910}
2911
2912template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002913inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002914_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002915shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002916 : __ptr_(nullptr),
2917 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918{
2919}
2920
2921template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002922template<class _Yp>
2923shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002924 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925 : __ptr_(__p)
2926{
2927 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002928 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002929 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2930 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002932 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933}
2934
2935template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002936template<class _Yp, class _Dp>
2937shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07002938 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 : __ptr_(__p)
2940{
2941#ifndef _LIBCPP_NO_EXCEPTIONS
2942 try
2943 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002944#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002945 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2946 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
2947 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002948 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949#ifndef _LIBCPP_NO_EXCEPTIONS
2950 }
2951 catch (...)
2952 {
2953 __d(__p);
2954 throw;
2955 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002956#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957}
2958
2959template<class _Tp>
2960template<class _Dp>
2961shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002962 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963{
2964#ifndef _LIBCPP_NO_EXCEPTIONS
2965 try
2966 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002967#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002968 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2969 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
2970 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971#ifndef _LIBCPP_NO_EXCEPTIONS
2972 }
2973 catch (...)
2974 {
2975 __d(__p);
2976 throw;
2977 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002978#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979}
2980
2981template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002982template<class _Yp, class _Dp, class _Alloc>
2983shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07002984 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985 : __ptr_(__p)
2986{
2987#ifndef _LIBCPP_NO_EXCEPTIONS
2988 try
2989 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002990#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002992 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993 typedef __allocator_destructor<_A2> _D2;
2994 _A2 __a2(__a);
2995 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002996 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002997 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002998 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999#ifndef _LIBCPP_NO_EXCEPTIONS
3000 }
3001 catch (...)
3002 {
3003 __d(__p);
3004 throw;
3005 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003006#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007}
3008
3009template<class _Tp>
3010template<class _Dp, class _Alloc>
3011shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003012 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013{
3014#ifndef _LIBCPP_NO_EXCEPTIONS
3015 try
3016 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003017#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003019 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020 typedef __allocator_destructor<_A2> _D2;
3021 _A2 __a2(__a);
3022 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003023 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003024 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025#ifndef _LIBCPP_NO_EXCEPTIONS
3026 }
3027 catch (...)
3028 {
3029 __d(__p);
3030 throw;
3031 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003032#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033}
3034
3035template<class _Tp>
3036template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003037inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003038shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 : __ptr_(__p),
3040 __cntrl_(__r.__cntrl_)
3041{
3042 if (__cntrl_)
3043 __cntrl_->__add_shared();
3044}
3045
3046template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003047inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003048shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 : __ptr_(__r.__ptr_),
3050 __cntrl_(__r.__cntrl_)
3051{
3052 if (__cntrl_)
3053 __cntrl_->__add_shared();
3054}
3055
3056template<class _Tp>
3057template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003058inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003060 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003061 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 : __ptr_(__r.__ptr_),
3063 __cntrl_(__r.__cntrl_)
3064{
3065 if (__cntrl_)
3066 __cntrl_->__add_shared();
3067}
3068
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003070inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003071shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072 : __ptr_(__r.__ptr_),
3073 __cntrl_(__r.__cntrl_)
3074{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003075 __r.__ptr_ = nullptr;
3076 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077}
3078
3079template<class _Tp>
3080template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003081inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003083 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003084 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085 : __ptr_(__r.__ptr_),
3086 __cntrl_(__r.__cntrl_)
3087{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003088 __r.__ptr_ = nullptr;
3089 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090}
3091
Marshall Clowb22274f2017-01-24 22:22:33 +00003092#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003093template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003094template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003095shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003096 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 : __ptr_(__r.get())
3098{
3099 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3100 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003101 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102 __r.release();
3103}
Marshall Clowb22274f2017-01-24 22:22:33 +00003104#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105
3106template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003107template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003109 typename enable_if
3110 <
3111 !is_lvalue_reference<_Dp>::value &&
3112 !is_array<_Yp>::value &&
3113 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3114 __nat
3115 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 : __ptr_(__r.get())
3117{
Marshall Clow35cde742015-05-10 13:59:45 +00003118#if _LIBCPP_STD_VER > 11
3119 if (__ptr_ == nullptr)
3120 __cntrl_ = nullptr;
3121 else
3122#endif
3123 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003124 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3125 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3126 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003127 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003128 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129 __r.release();
3130}
3131
3132template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003133template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003135 typename enable_if
3136 <
3137 is_lvalue_reference<_Dp>::value &&
3138 !is_array<_Yp>::value &&
3139 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3140 __nat
3141 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142 : __ptr_(__r.get())
3143{
Marshall Clow35cde742015-05-10 13:59:45 +00003144#if _LIBCPP_STD_VER > 11
3145 if (__ptr_ == nullptr)
3146 __cntrl_ = nullptr;
3147 else
3148#endif
3149 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003150 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003151 typedef __shared_ptr_pointer<_Yp*,
3152 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003153 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003154 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003155 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003156 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 __r.release();
3158}
3159
Zoe Carver6cd05c32019-08-19 15:47:16 +00003160template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161shared_ptr<_Tp>::~shared_ptr()
3162{
3163 if (__cntrl_)
3164 __cntrl_->__release_shared();
3165}
3166
3167template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003168inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003170shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171{
3172 shared_ptr(__r).swap(*this);
3173 return *this;
3174}
3175
3176template<class _Tp>
3177template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003178inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003179typename enable_if
3180<
zoecarverd73f42a2020-05-11 18:42:50 -07003181 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003182 shared_ptr<_Tp>&
3183>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003184shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185{
3186 shared_ptr(__r).swap(*this);
3187 return *this;
3188}
3189
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003191inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003193shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003195 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196 return *this;
3197}
3198
3199template<class _Tp>
3200template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003201inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003202typename enable_if
3203<
zoecarverd73f42a2020-05-11 18:42:50 -07003204 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003205 shared_ptr<_Tp>&
3206>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3208{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003209 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210 return *this;
3211}
3212
Marshall Clowb22274f2017-01-24 22:22:33 +00003213#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214template<class _Tp>
3215template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003216inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003217typename enable_if
3218<
3219 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003220 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003221 shared_ptr<_Tp>
3222>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3224{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003225 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226 return *this;
3227}
Marshall Clowb22274f2017-01-24 22:22:33 +00003228#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229
3230template<class _Tp>
3231template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003232inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003233typename enable_if
3234<
3235 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003236 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003237 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003238 shared_ptr<_Tp>&
3239>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3241{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003242 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243 return *this;
3244}
3245
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003247inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248void
Howard Hinnant719bda32011-05-28 14:41:13 +00003249shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003251 _VSTD::swap(__ptr_, __r.__ptr_);
3252 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253}
3254
3255template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003256inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257void
Howard Hinnant719bda32011-05-28 14:41:13 +00003258shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259{
3260 shared_ptr().swap(*this);
3261}
3262
3263template<class _Tp>
3264template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003265inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003266typename enable_if
3267<
zoecarverd73f42a2020-05-11 18:42:50 -07003268 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003269 void
3270>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271shared_ptr<_Tp>::reset(_Yp* __p)
3272{
3273 shared_ptr(__p).swap(*this);
3274}
3275
3276template<class _Tp>
3277template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003278inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003279typename enable_if
3280<
zoecarverd73f42a2020-05-11 18:42:50 -07003281 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003282 void
3283>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003284shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3285{
3286 shared_ptr(__p, __d).swap(*this);
3287}
3288
3289template<class _Tp>
3290template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003291inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003292typename enable_if
3293<
zoecarverd73f42a2020-05-11 18:42:50 -07003294 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003295 void
3296>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3298{
3299 shared_ptr(__p, __d, __a).swap(*this);
3300}
3301
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003302template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003304typename enable_if
3305<
3306 !is_array<_Tp>::value,
3307 shared_ptr<_Tp>
3308>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309allocate_shared(const _Alloc& __a, _Args&& ...__args)
3310{
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003311 static_assert(is_constructible<_Tp, _Args...>::value,
3312 "allocate_shared/make_shared: the type is not constructible from the provided arguments");
zoecarver505730a2020-02-25 16:50:57 -08003313
3314 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3315 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3316 typedef __allocator_destructor<_A2> _D2;
3317
3318 _A2 __a2(__a);
3319 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003320 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
zoecarver505730a2020-02-25 16:50:57 -08003321
Louis Dionnef34e5c82020-11-10 12:47:10 -05003322 typename shared_ptr<_Tp>::element_type *__p = __hold2->__get_elem();
zoecarver505730a2020-02-25 16:50:57 -08003323 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324}
3325
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003326template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3327_LIBCPP_HIDE_FROM_ABI
3328shared_ptr<_Tp> make_shared(_Args&& ...__args)
3329{
3330 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3331}
3332
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333template<class _Tp, class _Up>
3334inline _LIBCPP_INLINE_VISIBILITY
3335bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003336operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337{
3338 return __x.get() == __y.get();
3339}
3340
3341template<class _Tp, class _Up>
3342inline _LIBCPP_INLINE_VISIBILITY
3343bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003344operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345{
3346 return !(__x == __y);
3347}
3348
3349template<class _Tp, class _Up>
3350inline _LIBCPP_INLINE_VISIBILITY
3351bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003352operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003354#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003355 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3356 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003357#else
3358 return less<>()(__x.get(), __y.get());
3359#endif
3360
Howard Hinnantb17caf92012-02-21 21:02:58 +00003361}
3362
3363template<class _Tp, class _Up>
3364inline _LIBCPP_INLINE_VISIBILITY
3365bool
3366operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3367{
3368 return __y < __x;
3369}
3370
3371template<class _Tp, class _Up>
3372inline _LIBCPP_INLINE_VISIBILITY
3373bool
3374operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3375{
3376 return !(__y < __x);
3377}
3378
3379template<class _Tp, class _Up>
3380inline _LIBCPP_INLINE_VISIBILITY
3381bool
3382operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3383{
3384 return !(__x < __y);
3385}
3386
3387template<class _Tp>
3388inline _LIBCPP_INLINE_VISIBILITY
3389bool
3390operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3391{
3392 return !__x;
3393}
3394
3395template<class _Tp>
3396inline _LIBCPP_INLINE_VISIBILITY
3397bool
3398operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3399{
3400 return !__x;
3401}
3402
3403template<class _Tp>
3404inline _LIBCPP_INLINE_VISIBILITY
3405bool
3406operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3407{
3408 return static_cast<bool>(__x);
3409}
3410
3411template<class _Tp>
3412inline _LIBCPP_INLINE_VISIBILITY
3413bool
3414operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3415{
3416 return static_cast<bool>(__x);
3417}
3418
3419template<class _Tp>
3420inline _LIBCPP_INLINE_VISIBILITY
3421bool
3422operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3423{
3424 return less<_Tp*>()(__x.get(), nullptr);
3425}
3426
3427template<class _Tp>
3428inline _LIBCPP_INLINE_VISIBILITY
3429bool
3430operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3431{
3432 return less<_Tp*>()(nullptr, __x.get());
3433}
3434
3435template<class _Tp>
3436inline _LIBCPP_INLINE_VISIBILITY
3437bool
3438operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3439{
3440 return nullptr < __x;
3441}
3442
3443template<class _Tp>
3444inline _LIBCPP_INLINE_VISIBILITY
3445bool
3446operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3447{
3448 return __x < nullptr;
3449}
3450
3451template<class _Tp>
3452inline _LIBCPP_INLINE_VISIBILITY
3453bool
3454operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3455{
3456 return !(nullptr < __x);
3457}
3458
3459template<class _Tp>
3460inline _LIBCPP_INLINE_VISIBILITY
3461bool
3462operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3463{
3464 return !(__x < nullptr);
3465}
3466
3467template<class _Tp>
3468inline _LIBCPP_INLINE_VISIBILITY
3469bool
3470operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3471{
3472 return !(__x < nullptr);
3473}
3474
3475template<class _Tp>
3476inline _LIBCPP_INLINE_VISIBILITY
3477bool
3478operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3479{
3480 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481}
3482
3483template<class _Tp>
3484inline _LIBCPP_INLINE_VISIBILITY
3485void
Howard Hinnant719bda32011-05-28 14:41:13 +00003486swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487{
3488 __x.swap(__y);
3489}
3490
3491template<class _Tp, class _Up>
3492inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003493shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003494static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495{
zoecarverd73f42a2020-05-11 18:42:50 -07003496 return shared_ptr<_Tp>(__r,
3497 static_cast<
3498 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499}
3500
3501template<class _Tp, class _Up>
3502inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003503shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003504dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505{
zoecarverd73f42a2020-05-11 18:42:50 -07003506 typedef typename shared_ptr<_Tp>::element_type _ET;
3507 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3509}
3510
3511template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003512shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003513const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514{
zoecarverd73f42a2020-05-11 18:42:50 -07003515 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003516 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517}
3518
zoecarverd73f42a2020-05-11 18:42:50 -07003519template<class _Tp, class _Up>
3520shared_ptr<_Tp>
3521reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3522{
3523 return shared_ptr<_Tp>(__r,
3524 reinterpret_cast<
3525 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3526}
3527
Howard Hinnant72f73582010-08-11 17:04:31 +00003528#ifndef _LIBCPP_NO_RTTI
3529
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530template<class _Dp, class _Tp>
3531inline _LIBCPP_INLINE_VISIBILITY
3532_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003533get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534{
3535 return __p.template __get_deleter<_Dp>();
3536}
3537
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003538#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003539
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003541class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003543public:
3544 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545private:
3546 element_type* __ptr_;
3547 __shared_weak_count* __cntrl_;
3548
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003549public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003551 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003552 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003553 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3554 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003556 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003557 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003558 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3559 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003560
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003562 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003563 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003564 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3565 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003566 ~weak_ptr();
3567
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003569 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003570 template<class _Yp>
3571 typename enable_if
3572 <
3573 is_convertible<_Yp*, element_type*>::value,
3574 weak_ptr&
3575 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003577 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3578
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003580 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3581 template<class _Yp>
3582 typename enable_if
3583 <
3584 is_convertible<_Yp*, element_type*>::value,
3585 weak_ptr&
3586 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003588 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3589
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003590 template<class _Yp>
3591 typename enable_if
3592 <
3593 is_convertible<_Yp*, element_type*>::value,
3594 weak_ptr&
3595 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003597 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003598
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003600 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003602 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003603
Howard Hinnant756c69b2010-09-22 16:48:34 +00003604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003605 long use_count() const _NOEXCEPT
3606 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003608 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003609 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003610 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003611 template<class _Up>
3612 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003613 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003615 template<class _Up>
3616 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003617 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618 {return __cntrl_ < __r.__cntrl_;}
3619
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003620 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3621 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622};
3623
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003624#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3625template<class _Tp>
3626weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3627#endif
3628
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003630inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003631_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003632weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003633 : __ptr_(nullptr),
3634 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635{
3636}
3637
3638template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003639inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003640weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641 : __ptr_(__r.__ptr_),
3642 __cntrl_(__r.__cntrl_)
3643{
3644 if (__cntrl_)
3645 __cntrl_->__add_weak();
3646}
3647
3648template<class _Tp>
3649template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003650inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003652 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003653 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654 : __ptr_(__r.__ptr_),
3655 __cntrl_(__r.__cntrl_)
3656{
3657 if (__cntrl_)
3658 __cntrl_->__add_weak();
3659}
3660
3661template<class _Tp>
3662template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003663inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003665 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003666 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667 : __ptr_(__r.__ptr_),
3668 __cntrl_(__r.__cntrl_)
3669{
3670 if (__cntrl_)
3671 __cntrl_->__add_weak();
3672}
3673
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003674template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003675inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003676weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3677 : __ptr_(__r.__ptr_),
3678 __cntrl_(__r.__cntrl_)
3679{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003680 __r.__ptr_ = nullptr;
3681 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003682}
3683
3684template<class _Tp>
3685template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003686inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003687weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3688 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3689 _NOEXCEPT
3690 : __ptr_(__r.__ptr_),
3691 __cntrl_(__r.__cntrl_)
3692{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003693 __r.__ptr_ = nullptr;
3694 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003695}
3696
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697template<class _Tp>
3698weak_ptr<_Tp>::~weak_ptr()
3699{
3700 if (__cntrl_)
3701 __cntrl_->__release_weak();
3702}
3703
3704template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003705inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003707weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
3709 weak_ptr(__r).swap(*this);
3710 return *this;
3711}
3712
3713template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003714template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003715inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003716typename enable_if
3717<
3718 is_convertible<_Yp*, _Tp*>::value,
3719 weak_ptr<_Tp>&
3720>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003721weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722{
3723 weak_ptr(__r).swap(*this);
3724 return *this;
3725}
3726
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003727template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003728inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003729weak_ptr<_Tp>&
3730weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3731{
3732 weak_ptr(_VSTD::move(__r)).swap(*this);
3733 return *this;
3734}
3735
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003737template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003738inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003739typename enable_if
3740<
3741 is_convertible<_Yp*, _Tp*>::value,
3742 weak_ptr<_Tp>&
3743>::type
3744weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3745{
3746 weak_ptr(_VSTD::move(__r)).swap(*this);
3747 return *this;
3748}
3749
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003750template<class _Tp>
3751template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003752inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003753typename enable_if
3754<
3755 is_convertible<_Yp*, _Tp*>::value,
3756 weak_ptr<_Tp>&
3757>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003758weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759{
3760 weak_ptr(__r).swap(*this);
3761 return *this;
3762}
3763
3764template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003765inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766void
Howard Hinnant719bda32011-05-28 14:41:13 +00003767weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003769 _VSTD::swap(__ptr_, __r.__ptr_);
3770 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771}
3772
3773template<class _Tp>
3774inline _LIBCPP_INLINE_VISIBILITY
3775void
Howard Hinnant719bda32011-05-28 14:41:13 +00003776swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777{
3778 __x.swap(__y);
3779}
3780
3781template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003782inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783void
Howard Hinnant719bda32011-05-28 14:41:13 +00003784weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785{
3786 weak_ptr().swap(*this);
3787}
3788
3789template<class _Tp>
3790template<class _Yp>
3791shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003792 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793 : __ptr_(__r.__ptr_),
3794 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3795{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003796 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003797 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798}
3799
3800template<class _Tp>
3801shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003802weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803{
3804 shared_ptr<_Tp> __r;
3805 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3806 if (__r.__cntrl_)
3807 __r.__ptr_ = __ptr_;
3808 return __r;
3809}
3810
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003811#if _LIBCPP_STD_VER > 14
3812template <class _Tp = void> struct owner_less;
3813#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003814template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003815#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816
3817template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003818struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003820{
3821 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003822 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003823 bool operator()(shared_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 Hinnant756c69b2010-09-22 16:48:34 +00003825 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003826 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003828 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003829 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003831};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832
3833template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003834struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3836{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003837 typedef bool result_type;
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, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003841 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003842 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003844 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003845 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846 {return __x.owner_before(__y);}
3847};
3848
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003849#if _LIBCPP_STD_VER > 14
3850template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003851struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003852{
3853 template <class _Tp, class _Up>
3854 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003855 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003856 {return __x.owner_before(__y);}
3857 template <class _Tp, class _Up>
3858 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003859 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003860 {return __x.owner_before(__y);}
3861 template <class _Tp, class _Up>
3862 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003863 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003864 {return __x.owner_before(__y);}
3865 template <class _Tp, class _Up>
3866 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003867 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003868 {return __x.owner_before(__y);}
3869 typedef void is_transparent;
3870};
3871#endif
3872
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003874class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875{
3876 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003877protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003878 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003879 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003881 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003883 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3884 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003887public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003889 shared_ptr<_Tp> shared_from_this()
3890 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003892 shared_ptr<_Tp const> shared_from_this() const
3893 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894
Eric Fiselier84006862016-06-02 00:15:35 +00003895#if _LIBCPP_STD_VER > 14
3896 _LIBCPP_INLINE_VISIBILITY
3897 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3898 { return __weak_this_; }
3899
3900 _LIBCPP_INLINE_VISIBILITY
3901 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3902 { return __weak_this_; }
3903#endif // _LIBCPP_STD_VER > 14
3904
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905 template <class _Up> friend class shared_ptr;
3906};
3907
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003908template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003909struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003910{
3911 typedef shared_ptr<_Tp> argument_type;
3912 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003913
Howard Hinnant756c69b2010-09-22 16:48:34 +00003914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003915 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003916 {
zoecarverd73f42a2020-05-11 18:42:50 -07003917 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003918 }
3919};
3920
Howard Hinnantc834c512011-11-29 18:15:50 +00003921template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003922inline _LIBCPP_INLINE_VISIBILITY
3923basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003924operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003925
Eric Fiselier9b492672016-06-18 02:12:53 +00003926
3927#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003928
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003929class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003930{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003931 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003932public:
3933 void lock() _NOEXCEPT;
3934 void unlock() _NOEXCEPT;
3935
3936private:
3937 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3938 __sp_mut(const __sp_mut&);
3939 __sp_mut& operator=(const __sp_mut&);
3940
Howard Hinnant8331b762013-03-06 23:30:19 +00003941 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003942};
3943
Mehdi Amini228053d2017-05-04 17:08:54 +00003944_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3945__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003946
3947template <class _Tp>
3948inline _LIBCPP_INLINE_VISIBILITY
3949bool
3950atomic_is_lock_free(const shared_ptr<_Tp>*)
3951{
3952 return false;
3953}
3954
3955template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003956_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003957shared_ptr<_Tp>
3958atomic_load(const shared_ptr<_Tp>* __p)
3959{
3960 __sp_mut& __m = __get_sp_mut(__p);
3961 __m.lock();
3962 shared_ptr<_Tp> __q = *__p;
3963 __m.unlock();
3964 return __q;
3965}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003966
Howard Hinnant9fa30202012-07-30 01:40:57 +00003967template <class _Tp>
3968inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003969_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003970shared_ptr<_Tp>
3971atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3972{
3973 return atomic_load(__p);
3974}
3975
3976template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003977_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003978void
3979atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3980{
3981 __sp_mut& __m = __get_sp_mut(__p);
3982 __m.lock();
3983 __p->swap(__r);
3984 __m.unlock();
3985}
3986
3987template <class _Tp>
3988inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003989_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003990void
3991atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3992{
3993 atomic_store(__p, __r);
3994}
3995
3996template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003997_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003998shared_ptr<_Tp>
3999atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4000{
4001 __sp_mut& __m = __get_sp_mut(__p);
4002 __m.lock();
4003 __p->swap(__r);
4004 __m.unlock();
4005 return __r;
4006}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004007
Howard Hinnant9fa30202012-07-30 01:40:57 +00004008template <class _Tp>
4009inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004010_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004011shared_ptr<_Tp>
4012atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4013{
4014 return atomic_exchange(__p, __r);
4015}
4016
4017template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004018_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004019bool
4020atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4021{
Marshall Clow4201ee82016-05-18 17:50:13 +00004022 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004023 __sp_mut& __m = __get_sp_mut(__p);
4024 __m.lock();
4025 if (__p->__owner_equivalent(*__v))
4026 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004027 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004028 *__p = __w;
4029 __m.unlock();
4030 return true;
4031 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004032 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004033 *__v = *__p;
4034 __m.unlock();
4035 return false;
4036}
4037
4038template <class _Tp>
4039inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004040_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004041bool
4042atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4043{
4044 return atomic_compare_exchange_strong(__p, __v, __w);
4045}
4046
4047template <class _Tp>
4048inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004049_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004050bool
4051atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4052 shared_ptr<_Tp> __w, memory_order, memory_order)
4053{
4054 return atomic_compare_exchange_strong(__p, __v, __w);
4055}
4056
4057template <class _Tp>
4058inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004059_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004060bool
4061atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4062 shared_ptr<_Tp> __w, memory_order, memory_order)
4063{
4064 return atomic_compare_exchange_weak(__p, __v, __w);
4065}
4066
Eric Fiselier9b492672016-06-18 02:12:53 +00004067#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004068
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004069//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004070#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4071# ifndef _LIBCPP_CXX03_LANG
4072enum class pointer_safety : unsigned char {
4073 relaxed,
4074 preferred,
4075 strict
4076};
4077# endif
4078#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004079struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004081 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082 {
4083 relaxed,
4084 preferred,
4085 strict
4086 };
4087
Howard Hinnant49e145e2012-10-30 19:06:59 +00004088 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089
Howard Hinnant756c69b2010-09-22 16:48:34 +00004090 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004091 pointer_safety() : __v_() {}
4092
4093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004094 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096 operator int() const {return __v_;}
4097};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004098#endif
4099
4100#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004101 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004102_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4103#else
4104// This function is only offered in C++03 under ABI v1.
4105# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4106inline _LIBCPP_INLINE_VISIBILITY
4107pointer_safety get_pointer_safety() _NOEXCEPT {
4108 return pointer_safety::relaxed;
4109}
4110# endif
4111#endif
4112
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004114_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4115_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4116_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004117_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118
4119template <class _Tp>
4120inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004121_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122undeclare_reachable(_Tp* __p)
4123{
4124 return static_cast<_Tp*>(__undeclare_reachable(__p));
4125}
4126
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004127_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128
Marshall Clow8982dcd2015-07-13 20:04:56 +00004129// --- Helper for container swap --
4130template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004131_LIBCPP_INLINE_VISIBILITY
4132void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4133#if _LIBCPP_STD_VER >= 14
4134 _NOEXCEPT
4135#else
4136 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4137#endif
4138{
4139 using _VSTD::swap;
4140 swap(__a1, __a2);
4141}
4142
4143template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004144inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004145void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4146
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004147template <typename _Alloc>
4148inline _LIBCPP_INLINE_VISIBILITY
4149void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4150#if _LIBCPP_STD_VER >= 14
4151 _NOEXCEPT
4152#else
4153 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4154#endif
4155{
4156 _VSTD::__swap_allocator(__a1, __a2,
4157 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4158}
4159
Marshall Clowff91de82015-08-18 19:51:37 +00004160template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004161struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004162 _Traits::propagate_on_container_move_assignment::value
4163#if _LIBCPP_STD_VER > 14
4164 || _Traits::is_always_equal::value
4165#else
4166 && is_nothrow_move_assignable<_Alloc>::value
4167#endif
4168 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004169
Marshall Clowa591b9a2016-07-11 21:38:08 +00004170
Marshall Clowa591b9a2016-07-11 21:38:08 +00004171template <class _Tp, class _Alloc>
4172struct __temp_value {
4173 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004174
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004175 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004176 _Alloc &__a;
4177
4178 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4179 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004180
Marshall Clowa591b9a2016-07-11 21:38:08 +00004181 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004182 _LIBCPP_NO_CFI
4183 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4184 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4185 _VSTD::forward<_Args>(__args)...);
4186 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004187
Marshall Clowa591b9a2016-07-11 21:38:08 +00004188 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4189 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004190
Marshall Clowe46031a2018-07-02 18:41:15 +00004191template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004192struct __is_allocator : false_type {};
4193
4194template<typename _Alloc>
4195struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004196 typename __void_t<typename _Alloc::value_type>::type,
4197 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4198 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004199 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004200
Eric Fiselier74ebee62019-06-08 01:31:19 +00004201// __builtin_new_allocator -- A non-templated helper for allocating and
4202// deallocating memory using __builtin_operator_new and
4203// __builtin_operator_delete. It should be used in preference to
4204// `std::allocator<T>` to avoid additional instantiations.
4205struct __builtin_new_allocator {
4206 struct __builtin_new_deleter {
4207 typedef void* pointer_type;
4208
4209 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4210 : __size_(__size), __align_(__align) {}
4211
4212 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004213 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004214 }
4215
4216 private:
4217 size_t __size_;
4218 size_t __align_;
4219 };
4220
4221 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4222
4223 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004224 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004225 __builtin_new_deleter(__s, __align));
4226 }
4227
4228 static void __deallocate_bytes(void* __p, size_t __s,
4229 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004230 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004231 }
4232
4233 template <class _Tp>
4234 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4235 static __holder_t __allocate_type(size_t __n) {
4236 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4237 }
4238
4239 template <class _Tp>
4240 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4241 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4242 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4243 }
4244};
4245
4246
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247_LIBCPP_END_NAMESPACE_STD
4248
Eric Fiselierf4433a32017-05-31 22:07:49 +00004249_LIBCPP_POP_MACROS
4250
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004251#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004252# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004253#endif
4254
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255#endif // _LIBCPP_MEMORY