blob: 9cdac6afc5eaf8d7926efe0c76ca1a23eb58f8f6 [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
49template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
50
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>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000682#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000683# include <atomic>
684#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000685#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000686
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000687#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000689#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690
Eric Fiselierf4433a32017-05-31 22:07:49 +0000691_LIBCPP_PUSH_MACROS
692#include <__undef_macros>
693
694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695_LIBCPP_BEGIN_NAMESPACE_STD
696
Eric Fiselier89659d12015-07-07 00:27:16 +0000697template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000698inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000699_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
700#if !defined(_LIBCPP_HAS_NO_THREADS) && \
701 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000702 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000703 return __atomic_load_n(__value, __ATOMIC_RELAXED);
704#else
705 return *__value;
706#endif
707}
708
Kuba Breckade9d6792016-09-04 09:55:12 +0000709template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000710inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000711_ValueType __libcpp_acquire_load(_ValueType const* __value) {
712#if !defined(_LIBCPP_HAS_NO_THREADS) && \
713 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000714 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000715 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
716#else
717 return *__value;
718#endif
719}
720
Marshall Clow78dbe462016-11-14 18:22:19 +0000721// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000722
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723template <class _Tp> class allocator;
724
Michael Park99f9e912020-03-04 11:27:14 -0500725#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726template <>
Michael Park99f9e912020-03-04 11:27:14 -0500727class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728{
729public:
730 typedef void* pointer;
731 typedef const void* const_pointer;
732 typedef void value_type;
733
734 template <class _Up> struct rebind {typedef allocator<_Up> other;};
735};
736
Howard Hinnant9f771052012-01-19 23:15:22 +0000737template <>
Michael Park99f9e912020-03-04 11:27:14 -0500738class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000739{
740public:
741 typedef const void* pointer;
742 typedef const void* const_pointer;
743 typedef const void value_type;
744
745 template <class _Up> struct rebind {typedef allocator<_Up> other;};
746};
Michael Park99f9e912020-03-04 11:27:14 -0500747#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000748
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749// pointer_traits
750
Marshall Clow0be70c32017-06-14 21:23:57 +0000751template <class _Tp, class = void>
752struct __has_element_type : false_type {};
753
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000755struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000756 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757
758template <class _Ptr, bool = __has_element_type<_Ptr>::value>
759struct __pointer_traits_element_type;
760
761template <class _Ptr>
762struct __pointer_traits_element_type<_Ptr, true>
763{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000764 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765};
766
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767template <template <class, class...> class _Sp, class _Tp, class ..._Args>
768struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
769{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000770 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771};
772
773template <template <class, class...> class _Sp, class _Tp, class ..._Args>
774struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
775{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000776 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777};
778
Marshall Clow0be70c32017-06-14 21:23:57 +0000779template <class _Tp, class = void>
780struct __has_difference_type : false_type {};
781
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000783struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000784 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785
786template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
787struct __pointer_traits_difference_type
788{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000789 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790};
791
792template <class _Ptr>
793struct __pointer_traits_difference_type<_Ptr, true>
794{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000795 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796};
797
798template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500799struct __has_rebind
800{
801private:
802 struct __two {char __lx; char __lxx;};
803 template <class _Xp> static __two __test(...);
Louis Dionne95f24792020-03-04 14:38:51 -0500804 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Louis Dionnef1bb8492020-03-04 13:54:58 -0500805 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
Louis Dionne95f24792020-03-04 14:38:51 -0500806 _LIBCPP_SUPPRESS_DEPRECATED_POP
Louis Dionnef1bb8492020-03-04 13:54:58 -0500807public:
808 static const bool value = sizeof(__test<_Tp>(0)) == 1;
809};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810
811template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
812struct __pointer_traits_rebind
813{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000814#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000815 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000817 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818#endif
819};
820
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
822struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
823{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000824#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000825 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000827 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828#endif
829};
830
831template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
832struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
833{
834 typedef _Sp<_Up, _Args...> type;
835};
836
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000838struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839{
840 typedef _Ptr pointer;
841 typedef typename __pointer_traits_element_type<pointer>::type element_type;
842 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
843
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000844#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000845 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846#else
847 template <class _Up> struct rebind
848 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000849#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850
851private:
852 struct __nat {};
853public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855 static pointer pointer_to(typename conditional<is_void<element_type>::value,
856 __nat, element_type>::type& __r)
857 {return pointer::pointer_to(__r);}
858};
859
860template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000861struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862{
863 typedef _Tp* pointer;
864 typedef _Tp element_type;
865 typedef ptrdiff_t difference_type;
866
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000867#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868 template <class _Up> using rebind = _Up*;
869#else
870 template <class _Up> struct rebind {typedef _Up* other;};
871#endif
872
873private:
874 struct __nat {};
875public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000876 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000878 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000879 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880};
881
Eric Fiselierae3ab842015-08-23 02:56:05 +0000882template <class _From, class _To>
883struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000884#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000885 typedef typename pointer_traits<_From>::template rebind<_To> type;
886#else
887 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
888#endif
889};
890
Louis Dionne99f59432020-09-17 12:06:13 -0400891// construct_at
892
893#if _LIBCPP_STD_VER > 17
894
895template<class _Tp>
896_LIBCPP_CONSTEXPR_AFTER_CXX17 void* __voidify(_Tp& __ptr) noexcept {
897 return const_cast<void*>(static_cast<const volatile void*>(_VSTD::addressof(__ptr)));
898}
899
900template<class _Tp, class ..._Args, class = decltype(
901 ::new (_VSTD::declval<void*>()) _Tp(_VSTD::declval<_Args>()...)
902)>
903_LIBCPP_INLINE_VISIBILITY
904constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
905 _LIBCPP_ASSERT(__location, "null pointer given to construct_at");
906 return ::new (_VSTD::__voidify(*__location)) _Tp(_VSTD::forward<_Args>(__args)...);
907}
908
909#endif
910
911// destroy_at
912
913#if _LIBCPP_STD_VER > 14
914
915template <class _Tp>
916inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
917void destroy_at(_Tp* __loc) {
918 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
919 __loc->~_Tp();
920}
921
922#endif
923
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924// allocator_traits
925
Marshall Clow0be70c32017-06-14 21:23:57 +0000926template <class _Tp, class = void>
927struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928
929template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000930struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000931 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932
933namespace __pointer_type_imp
934{
935
936template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
937struct __pointer_type
938{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000939 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940};
941
942template <class _Tp, class _Dp>
943struct __pointer_type<_Tp, _Dp, false>
944{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000945 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946};
947
Howard Hinnant8e4e6002010-11-18 01:40:00 +0000948} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
950template <class _Tp, class _Dp>
951struct __pointer_type
952{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000953 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954};
955
Marshall Clow0be70c32017-06-14 21:23:57 +0000956template <class _Tp, class = void>
957struct __has_const_pointer : false_type {};
958
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000960struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000961 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
963template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
964struct __const_pointer
965{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000966 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967};
968
969template <class _Tp, class _Ptr, class _Alloc>
970struct __const_pointer<_Tp, _Ptr, _Alloc, false>
971{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000972#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000973 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974#else
975 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
976#endif
977};
978
Marshall Clow0be70c32017-06-14 21:23:57 +0000979template <class _Tp, class = void>
980struct __has_void_pointer : false_type {};
981
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000983struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000984 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
986template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
987struct __void_pointer
988{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000989 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990};
991
992template <class _Ptr, class _Alloc>
993struct __void_pointer<_Ptr, _Alloc, false>
994{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000995#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000996 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000998 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999#endif
1000};
1001
Marshall Clow0be70c32017-06-14 21:23:57 +00001002template <class _Tp, class = void>
1003struct __has_const_void_pointer : false_type {};
1004
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001006struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001007 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
1009template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1010struct __const_void_pointer
1011{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001012 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013};
1014
1015template <class _Ptr, class _Alloc>
1016struct __const_void_pointer<_Ptr, _Alloc, false>
1017{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001018#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001019 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001021 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022#endif
1023};
1024
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001025
1026template <bool _UsePointerTraits> struct __to_address_helper;
1027
1028template <> struct __to_address_helper<true> {
1029 template <class _Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001030 using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001031
1032 template <class _Pointer>
1033 _LIBCPP_CONSTEXPR
1034 static __return_type<_Pointer>
1035 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1036};
1037
1038template <class _Pointer, bool _Dummy = true>
1039using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1040
1041
Howard Hinnantc834c512011-11-29 18:15:50 +00001042template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001043inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001044_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001045__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001047 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 return __p;
1049}
1050
1051template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001052inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1053typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1054__to_address(const _Pointer& __p) _NOEXCEPT {
1055 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001056}
1057
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001058template <> struct __to_address_helper<false> {
1059 template <class _Pointer>
1060 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001061
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001062 template <class _Pointer>
1063 _LIBCPP_CONSTEXPR
1064 static __return_type<_Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001065 __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001066};
1067
1068
1069#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001070template <class _Tp>
1071inline _LIBCPP_INLINE_VISIBILITY constexpr
1072_Tp*
1073to_address(_Tp* __p) _NOEXCEPT
1074{
1075 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1076 return __p;
1077}
1078
1079template <class _Pointer>
1080inline _LIBCPP_INLINE_VISIBILITY
1081auto
1082to_address(const _Pointer& __p) _NOEXCEPT
1083{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001084 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001085}
1086#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087
Marshall Clow0be70c32017-06-14 21:23:57 +00001088template <class _Tp, class = void>
1089struct __has_size_type : false_type {};
1090
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001092struct __has_size_type<_Tp,
1093 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001095template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096struct __size_type
1097{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001098 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099};
1100
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001101template <class _Alloc, class _DiffType>
1102struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001104 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105};
1106
Marshall Clow0be70c32017-06-14 21:23:57 +00001107template <class _Tp, class = void>
1108struct __has_propagate_on_container_copy_assignment : false_type {};
1109
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001111struct __has_propagate_on_container_copy_assignment<_Tp,
1112 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1113 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114
1115template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1116struct __propagate_on_container_copy_assignment
1117{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001118 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119};
1120
1121template <class _Alloc>
1122struct __propagate_on_container_copy_assignment<_Alloc, true>
1123{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001124 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125};
1126
Marshall Clow0be70c32017-06-14 21:23:57 +00001127template <class _Tp, class = void>
1128struct __has_propagate_on_container_move_assignment : false_type {};
1129
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001131struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001132 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1133 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134
1135template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1136struct __propagate_on_container_move_assignment
1137{
1138 typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_move_assignment<_Alloc, true>
1143{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001144 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145};
1146
Marshall Clow0be70c32017-06-14 21:23:57 +00001147template <class _Tp, class = void>
1148struct __has_propagate_on_container_swap : false_type {};
1149
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001151struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001152 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1153 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154
1155template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1156struct __propagate_on_container_swap
1157{
1158 typedef false_type type;
1159};
1160
1161template <class _Alloc>
1162struct __propagate_on_container_swap<_Alloc, true>
1163{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001164 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165};
1166
Marshall Clow0be70c32017-06-14 21:23:57 +00001167template <class _Tp, class = void>
1168struct __has_is_always_equal : false_type {};
1169
Marshall Clow0b587562015-06-02 16:34:03 +00001170template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001171struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001172 typename __void_t<typename _Tp::is_always_equal>::type>
1173 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001174
1175template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1176struct __is_always_equal
1177{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001178 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001179};
1180
1181template <class _Alloc>
1182struct __is_always_equal<_Alloc, true>
1183{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001184 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001185};
1186
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1188struct __has_rebind_other
1189{
1190private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001191 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001193 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001195 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196public:
1197 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1198};
1199
1200template <class _Tp, class _Up>
1201struct __has_rebind_other<_Tp, _Up, false>
1202{
1203 static const bool value = false;
1204};
1205
1206template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1207struct __allocator_traits_rebind
1208{
Michael Park99f9e912020-03-04 11:27:14 -05001209 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001210 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001211 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212};
1213
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1215struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1216{
Michael Park99f9e912020-03-04 11:27:14 -05001217 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001218 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001219 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220};
1221
1222template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1223struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1224{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001225 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226};
1227
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001228#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
Michael Park99f9e912020-03-04 11:27:14 -05001230_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1232auto
1233__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001234 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001235_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236
1237template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1238auto
1239__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1240 -> false_type;
1241
1242template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1243struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001244 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1245 declval<_SizeType>(),
1246 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247{
1248};
1249
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001250#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251
1252template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1253struct __has_allocate_hint
1254 : true_type
1255{
1256};
1257
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001258#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
zoecarver75816d42020-05-23 14:32:12 -07001260_LIBCPP_SUPPRESS_DEPRECATED_PUSH
zoecarver20590dd2020-05-23 14:03:04 -07001261template <class _Alloc, class ..._Args,
1262 class = decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Args>()...))>
1263static true_type __test_has_construct(int);
zoecarver75816d42020-05-23 14:32:12 -07001264_LIBCPP_SUPPRESS_DEPRECATED_POP
1265
zoecarver20590dd2020-05-23 14:03:04 -07001266template <class _Alloc, class...>
1267static false_type __test_has_construct(...);
1268
1269template <class _Alloc, class ..._Args>
1270struct __has_construct : decltype(__test_has_construct<_Alloc, _Args...>(0)) {};
1271
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001272#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273
Michael Park99f9e912020-03-04 11:27:14 -05001274_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275template <class _Alloc, class _Pointer>
1276auto
1277__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1278 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001279_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280
1281template <class _Alloc, class _Pointer>
1282auto
1283__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1284 -> false_type;
1285
1286template <class _Alloc, class _Pointer>
1287struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001288 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1289 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290{
1291};
1292
Michael Park99f9e912020-03-04 11:27:14 -05001293_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294template <class _Alloc>
1295auto
1296__has_max_size_test(_Alloc&& __a)
1297 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001298_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299
1300template <class _Alloc>
1301auto
1302__has_max_size_test(const volatile _Alloc& __a)
1303 -> false_type;
1304
1305template <class _Alloc>
1306struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001307 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308{
1309};
1310
1311template <class _Alloc>
1312auto
1313__has_select_on_container_copy_construction_test(_Alloc&& __a)
1314 -> decltype(__a.select_on_container_copy_construction(), true_type());
1315
1316template <class _Alloc>
1317auto
1318__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1319 -> false_type;
1320
1321template <class _Alloc>
1322struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001323 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324{
1325};
1326
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001327#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001329template <class _Alloc, class _Pointer, class = void>
1330struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
1332template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001333struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1334 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001335>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336
1337template <class _Alloc>
1338struct __has_max_size
1339 : true_type
1340{
1341};
1342
1343template <class _Alloc>
1344struct __has_select_on_container_copy_construction
1345 : false_type
1346{
1347};
1348
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001349#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001351template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1352struct __alloc_traits_difference_type
1353{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001354 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001355};
1356
1357template <class _Alloc, class _Ptr>
1358struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1359{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001360 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001361};
1362
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001363template <class _Tp>
1364struct __is_default_allocator : false_type {};
1365
1366template <class _Tp>
1367struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1368
Eric Fiselier909fe962019-09-13 16:09:33 +00001369
1370
1371template <class _Alloc,
1372 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1373 >
1374struct __is_cpp17_move_insertable;
1375template <class _Alloc>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001376struct __is_cpp17_move_insertable<_Alloc, true> : true_type {};
Eric Fiselier909fe962019-09-13 16:09:33 +00001377template <class _Alloc>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001378struct __is_cpp17_move_insertable<_Alloc, false> : is_move_constructible<typename _Alloc::value_type> {};
Eric Fiselier909fe962019-09-13 16:09:33 +00001379
1380template <class _Alloc,
1381 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1382 >
1383struct __is_cpp17_copy_insertable;
1384template <class _Alloc>
1385struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1386template <class _Alloc>
1387struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001388 is_copy_constructible<typename _Alloc::value_type>::value &&
Eric Fiselier909fe962019-09-13 16:09:33 +00001389 __is_cpp17_move_insertable<_Alloc>::value>
1390 {};
1391
1392
1393
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001395struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396{
1397 typedef _Alloc allocator_type;
1398 typedef typename allocator_type::value_type value_type;
1399
1400 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1401 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1402 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1403 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1404
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001405 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1406 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407
1408 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1409 propagate_on_container_copy_assignment;
1410 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1411 propagate_on_container_move_assignment;
1412 typedef typename __propagate_on_container_swap<allocator_type>::type
1413 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001414 typedef typename __is_always_equal<allocator_type>::type
1415 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001417#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001419 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001420 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001421#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 template <class _Tp> struct rebind_alloc
1423 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1424 template <class _Tp> struct rebind_traits
1425 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001426#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427
Louis Dionne99f59432020-09-17 12:06:13 -04001428 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 static pointer allocate(allocator_type& __a, size_type __n)
1430 {return __a.allocate(__n);}
Louis Dionne99f59432020-09-17 12:06:13 -04001431 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001433 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1435
Louis Dionne99f59432020-09-17 12:06:13 -04001436 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001437 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438 {__a.deallocate(__p, __n);}
1439
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001441 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001443 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001444 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445
1446 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001447 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 static void destroy(allocator_type& __a, _Tp* __p)
1449 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1450
Louis Dionne99f59432020-09-17 12:06:13 -04001451 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow4f834b52013-08-27 20:22:15 +00001452 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1454
Louis Dionne99f59432020-09-17 12:06:13 -04001455 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456 static allocator_type
1457 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001458 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 __has_select_on_container_copy_construction<const allocator_type>(),
1460 __a);}
1461
1462private:
Louis Dionne99f59432020-09-17 12:06:13 -04001463 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001464 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001466 {
1467 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1468 return __a.allocate(__n, __hint);
1469 _LIBCPP_SUPPRESS_DEPRECATED_POP
1470 }
Louis Dionne99f59432020-09-17 12:06:13 -04001471 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001472 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001473 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 {return __a.allocate(__n);}
1475
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001477 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001479 {
1480 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1481 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1482 _LIBCPP_SUPPRESS_DEPRECATED_POP
1483 }
1484
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001486 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1488 {
Louis Dionne99f59432020-09-17 12:06:13 -04001489#if _LIBCPP_STD_VER > 17
1490 _VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
1491#else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001492 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne99f59432020-09-17 12:06:13 -04001493#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495
1496 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001497 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001499 {
1500 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1501 __a.destroy(__p);
1502 _LIBCPP_SUPPRESS_DEPRECATED_POP
1503 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001505 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506 static void __destroy(false_type, allocator_type&, _Tp* __p)
1507 {
Louis Dionne99f59432020-09-17 12:06:13 -04001508#if _LIBCPP_STD_VER > 17
1509 _VSTD::destroy_at(__p);
1510#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511 __p->~_Tp();
Louis Dionne99f59432020-09-17 12:06:13 -04001512#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 }
1514
Louis Dionne99f59432020-09-17 12:06:13 -04001515 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowf817a352017-12-05 15:56:26 +00001516 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001517 {
1518 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1519 return __a.max_size();
1520 _LIBCPP_SUPPRESS_DEPRECATED_POP
1521 }
1522
Louis Dionne99f59432020-09-17 12:06:13 -04001523 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowf817a352017-12-05 15:56:26 +00001524 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001525 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526
Louis Dionne99f59432020-09-17 12:06:13 -04001527 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001529 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 {return __a.select_on_container_copy_construction();}
Louis Dionne99f59432020-09-17 12:06:13 -04001531 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001533 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 {return __a;}
1535};
1536
Marshall Clow940e01c2015-04-07 05:21:38 +00001537template <class _Traits, class _Tp>
1538struct __rebind_alloc_helper
1539{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001540#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001541 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001542#else
1543 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1544#endif
1545};
1546
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547// allocator
1548
1549template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001550class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551{
1552public:
Louis Dionnef8b6c022020-09-21 10:36:37 -04001553 typedef size_t size_type;
1554 typedef ptrdiff_t difference_type;
1555 typedef _Tp value_type;
1556 typedef true_type propagate_on_container_move_assignment;
1557 typedef true_type is_always_equal;
1558
1559 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1560 allocator() _NOEXCEPT { }
1561
1562 template <class _Up>
1563 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1564 allocator(const allocator<_Up>&) _NOEXCEPT { }
1565
1566 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1567 _Tp* allocate(size_t __n) {
1568 if (__n > allocator_traits<allocator>::max_size(*this))
1569 __throw_length_error("allocator<T>::allocate(size_t n)"
1570 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -04001571 if (__libcpp_is_constant_evaluated()) {
1572 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
1573 } else {
1574 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1575 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001576 }
1577
1578 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1579 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -04001580 if (__libcpp_is_constant_evaluated()) {
1581 ::operator delete(__p);
1582 } else {
1583 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1584 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001585 }
1586
1587 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -05001588#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -05001589 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1590 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1591 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1592 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1593
Louis Dionne481a2662018-09-23 18:35:00 +00001594 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001595 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1596 typedef allocator<_Up> other;
1597 };
Marshall Clowbc759762018-03-20 23:02:53 +00001598
Michael Park99f9e912020-03-04 11:27:14 -05001599 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -04001600 pointer address(reference __x) const _NOEXCEPT {
1601 return _VSTD::addressof(__x);
1602 }
Michael Park99f9e912020-03-04 11:27:14 -05001603 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -04001604 const_pointer address(const_reference __x) const _NOEXCEPT {
1605 return _VSTD::addressof(__x);
1606 }
Michael Park99f9e912020-03-04 11:27:14 -05001607
Michael Park99f9e912020-03-04 11:27:14 -05001608 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001609 _Tp* allocate(size_t __n, const void*) {
1610 return allocate(__n);
1611 }
Michael Park99f9e912020-03-04 11:27:14 -05001612
Louis Dionnef8b6c022020-09-21 10:36:37 -04001613 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1614 return size_type(~0) / sizeof(_Tp);
1615 }
Michael Park99f9e912020-03-04 11:27:14 -05001616
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001618 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1619 void construct(_Up* __p, _Args&&... __args) {
1620 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1621 }
1622
1623 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1624 void destroy(pointer __p) {
1625 __p->~_Tp();
1626 }
Michael Park99f9e912020-03-04 11:27:14 -05001627#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628};
1629
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001630template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001631class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001632{
1633public:
Louis Dionnef8b6c022020-09-21 10:36:37 -04001634 typedef size_t size_type;
1635 typedef ptrdiff_t difference_type;
1636 typedef const _Tp value_type;
1637 typedef true_type propagate_on_container_move_assignment;
1638 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001639
Marshall Clowbc759762018-03-20 23:02:53 +00001640 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001641 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +00001642
1643 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001644 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001645 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -05001646
Louis Dionne99f59432020-09-17 12:06:13 -04001647 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001648 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -04001649 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +00001650 __throw_length_error("allocator<const T>::allocate(size_t n)"
1651 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -04001652 if (__libcpp_is_constant_evaluated()) {
1653 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
1654 } else {
1655 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1656 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001657 }
Michael Park99f9e912020-03-04 11:27:14 -05001658
Louis Dionne99f59432020-09-17 12:06:13 -04001659 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001660 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -04001661 if (__libcpp_is_constant_evaluated()) {
1662 ::operator delete(const_cast<_Tp*>(__p));
1663 } else {
1664 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1665 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001666 }
Michael Park99f9e912020-03-04 11:27:14 -05001667
Louis Dionnef8b6c022020-09-21 10:36:37 -04001668 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -05001669#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -04001670 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1671 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1672 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1673 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1674
1675 template <class _Up>
1676 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1677 typedef allocator<_Up> other;
1678 };
1679
1680 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1681 const_pointer address(const_reference __x) const _NOEXCEPT {
1682 return _VSTD::addressof(__x);
1683 }
1684
1685 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1686 const _Tp* allocate(size_t __n, const void*) {
1687 return allocate(__n);
1688 }
1689
1690 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1691 return size_type(~0) / sizeof(_Tp);
1692 }
Michael Park99f9e912020-03-04 11:27:14 -05001693
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001694 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001695 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1696 void construct(_Up* __p, _Args&&... __args) {
1697 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1698 }
Howard Hinnant9e028f12012-05-01 15:37:54 +00001699
Louis Dionnef8b6c022020-09-21 10:36:37 -04001700 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1701 void destroy(pointer __p) {
1702 __p->~_Tp();
1703 }
Michael Park99f9e912020-03-04 11:27:14 -05001704#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001705};
1706
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -04001708inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001709bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710
1711template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -04001712inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001713bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714
Louis Dionned6651542020-11-03 12:05:55 -05001715template <class _Alloc, class _Ptr>
1716_LIBCPP_INLINE_VISIBILITY
1717void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
1718 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1719 "The specified type does not meet the requirements of Cpp17MoveInsertible");
1720 typedef allocator_traits<_Alloc> _Traits;
1721 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
1722 _Traits::construct(__a, _VSTD::__to_address(__begin2),
1723#ifdef _LIBCPP_NO_EXCEPTIONS
1724 _VSTD::move(*__begin1)
1725#else
1726 _VSTD::move_if_noexcept(*__begin1)
1727#endif
1728 );
1729 }
1730}
1731
1732template <class _Alloc, class _Tp, typename enable_if<
1733 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1734 is_trivially_move_constructible<_Tp>::value
1735>::type>
1736_LIBCPP_INLINE_VISIBILITY
1737void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1738 ptrdiff_t _Np = __end1 - __begin1;
1739 if (_Np > 0) {
1740 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1741 __begin2 += _Np;
1742 }
1743}
1744
1745template <class _Alloc, class _Iter, class _Ptr>
1746_LIBCPP_INLINE_VISIBILITY
1747void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1748 typedef allocator_traits<_Alloc> _Traits;
1749 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1750 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1751 }
1752}
1753
1754template <class _Alloc, class _Source, class _Dest,
1755 class _RawSource = typename remove_const<_Source>::type,
1756 class _RawDest = typename remove_const<_Dest>::type,
1757 class =
1758 typename enable_if<
1759 is_trivially_copy_constructible<_Dest>::value &&
1760 is_same<_RawSource, _RawDest>::value &&
1761 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1762 >::type>
1763_LIBCPP_INLINE_VISIBILITY
1764void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1765 ptrdiff_t _Np = __end1 - __begin1;
1766 if (_Np > 0) {
1767 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1768 __begin2 += _Np;
1769 }
1770}
1771
1772template <class _Alloc, class _Ptr>
1773_LIBCPP_INLINE_VISIBILITY
1774void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1775 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1776 "The specified type does not meet the requirements of Cpp17MoveInsertable");
1777 typedef allocator_traits<_Alloc> _Traits;
1778 while (__end1 != __begin1) {
1779 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1780#ifdef _LIBCPP_NO_EXCEPTIONS
1781 _VSTD::move(*--__end1)
1782#else
1783 _VSTD::move_if_noexcept(*--__end1)
1784#endif
1785 );
1786 --__end2;
1787 }
1788}
1789
1790template <class _Alloc, class _Tp, class = typename enable_if<
1791 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1792 is_trivially_move_constructible<_Tp>::value
1793>::type>
1794_LIBCPP_INLINE_VISIBILITY
1795void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1796 ptrdiff_t _Np = __end1 - __begin1;
1797 __end2 -= _Np;
1798 if (_Np > 0)
1799 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1800}
1801
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001803class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 : public iterator<output_iterator_tag,
1805 _Tp, // purposefully not C++03
1806 ptrdiff_t, // purposefully not C++03
1807 _Tp*, // purposefully not C++03
1808 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1809{
1810private:
1811 _OutputIterator __x_;
1812public:
1813 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1814 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1815 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001816 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001817#if _LIBCPP_STD_VER >= 14
1818 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001819 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001820#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1822 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1823 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001824#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001825 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001826#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827};
1828
1829template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001830_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001832get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833{
1834 pair<_Tp*, ptrdiff_t> __r(0, 0);
1835 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1836 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1837 / sizeof(_Tp);
1838 if (__n > __m)
1839 __n = __m;
1840 while (__n > 0)
1841 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001842#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001843 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001844 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001845 align_val_t __al =
1846 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001847 __r.first = static_cast<_Tp*>(::operator new(
1848 __n * sizeof(_Tp), __al, nothrow));
1849 } else {
1850 __r.first = static_cast<_Tp*>(::operator new(
1851 __n * sizeof(_Tp), nothrow));
1852 }
1853#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001854 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001855 {
1856 // Since aligned operator new is unavailable, return an empty
1857 // buffer rather than one with invalid alignment.
1858 return __r;
1859 }
1860
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001862#endif
1863
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864 if (__r.first)
1865 {
1866 __r.second = __n;
1867 break;
1868 }
1869 __n /= 2;
1870 }
1871 return __r;
1872}
1873
1874template <class _Tp>
1875inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001876void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1877{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001878 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001879}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880
Marshall Clowb22274f2017-01-24 22:22:33 +00001881#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001883struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884{
1885 _Tp* __ptr_;
1886};
1887
1888template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001889class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
1891private:
1892 _Tp* __ptr_;
1893public:
1894 typedef _Tp element_type;
1895
Dimitry Andric47269ce2020-03-13 19:36:26 +01001896 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1897 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1898 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001900 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001902 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001904 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001906 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907
Dimitry Andric47269ce2020-03-13 19:36:26 +01001908 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001910 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1911 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1912 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913 {
1914 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001915 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 return __t;
1917 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001918 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 {
1920 if (__ptr_ != __p)
1921 delete __ptr_;
1922 __ptr_ = __p;
1923 }
1924
Dimitry Andric47269ce2020-03-13 19:36:26 +01001925 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1926 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001928 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 {return auto_ptr<_Up>(release());}
1930};
1931
1932template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001933class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934{
1935public:
1936 typedef void element_type;
1937};
Marshall Clowb22274f2017-01-24 22:22:33 +00001938#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001940// Tag used to default initialize one or both of the pair's elements.
1941struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001942struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001943
Eric Fiselier9d355982017-04-12 23:45:53 +00001944template <class _Tp, int _Idx,
1945 bool _CanBeEmptyBase =
1946 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1947struct __compressed_pair_elem {
1948 typedef _Tp _ParamT;
1949 typedef _Tp& reference;
1950 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001952 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001953 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001954 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1955 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956
Eric Fiselier9d355982017-04-12 23:45:53 +00001957 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001958 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1959 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001960 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001961 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001962 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001963 : __value_(_VSTD::forward<_Up>(__u))
1964 {
1965 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001967
1968#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001969 template <class... _Args, size_t... _Indexes>
1970 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1971 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1972 __tuple_indices<_Indexes...>)
1973 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001974#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001976
Alex Lorenz76132112017-11-09 17:54:49 +00001977 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1978 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001979 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001982 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983};
1984
Eric Fiselier9d355982017-04-12 23:45:53 +00001985template <class _Tp, int _Idx>
1986struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1987 typedef _Tp _ParamT;
1988 typedef _Tp& reference;
1989 typedef const _Tp& const_reference;
1990 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001992 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1993 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1994 __compressed_pair_elem(__default_init_tag) {}
1995 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1996 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997
Eric Fiselier9d355982017-04-12 23:45:53 +00001998 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001999 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2000 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002001 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002002 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002003 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002004 : __value_type(_VSTD::forward<_Up>(__u))
2005 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002007#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002008 template <class... _Args, size_t... _Indexes>
2009 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2010 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2011 __tuple_indices<_Indexes...>)
2012 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002013#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014
Alex Lorenz76132112017-11-09 17:54:49 +00002015 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2016 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002017 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018};
2019
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002021class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2022 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002023 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2024 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002025
2026 // NOTE: This static assert should never fire because __compressed_pair
2027 // is *almost never* used in a scenario where it's possible for T1 == T2.
2028 // (The exception is std::function where it is possible that the function
2029 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002030 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002031 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2032 "The current implementation is NOT ABI-compatible with the previous "
2033 "implementation for this configuration");
2034
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002036 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002037 class = typename enable_if<
2038 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2039 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2040 >::type
2041 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002042 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002043 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044
Eric Fiselier9d355982017-04-12 23:45:53 +00002045 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002046 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002047 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002048 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002050#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002051 template <class... _Args1, class... _Args2>
2052 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2053 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2054 tuple<_Args2...> __second_args)
2055 : _Base1(__pc, _VSTD::move(__first_args),
2056 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2057 _Base2(__pc, _VSTD::move(__second_args),
2058 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002059#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060
Eric Fiselier9d355982017-04-12 23:45:53 +00002061 _LIBCPP_INLINE_VISIBILITY
2062 typename _Base1::reference first() _NOEXCEPT {
2063 return static_cast<_Base1&>(*this).__get();
2064 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065
Eric Fiselier9d355982017-04-12 23:45:53 +00002066 _LIBCPP_INLINE_VISIBILITY
2067 typename _Base1::const_reference first() const _NOEXCEPT {
2068 return static_cast<_Base1 const&>(*this).__get();
2069 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070
Eric Fiselier9d355982017-04-12 23:45:53 +00002071 _LIBCPP_INLINE_VISIBILITY
2072 typename _Base2::reference second() _NOEXCEPT {
2073 return static_cast<_Base2&>(*this).__get();
2074 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075
Eric Fiselier9d355982017-04-12 23:45:53 +00002076 _LIBCPP_INLINE_VISIBILITY
2077 typename _Base2::const_reference second() const _NOEXCEPT {
2078 return static_cast<_Base2 const&>(*this).__get();
2079 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080
Eric Fiselier9d355982017-04-12 23:45:53 +00002081 _LIBCPP_INLINE_VISIBILITY
2082 void swap(__compressed_pair& __x)
2083 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2084 __is_nothrow_swappable<_T2>::value)
2085 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002086 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00002087 swap(first(), __x.first());
2088 swap(second(), __x.second());
2089 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090};
2091
2092template <class _T1, class _T2>
2093inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002094void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2095 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2096 __is_nothrow_swappable<_T2>::value) {
2097 __x.swap(__y);
2098}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002100// default_delete
2101
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002103struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002104 static_assert(!is_function<_Tp>::value,
2105 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002106#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002107 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002108#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002109 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002110#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002111 template <class _Up>
2112 _LIBCPP_INLINE_VISIBILITY
2113 default_delete(const default_delete<_Up>&,
2114 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2115 0) _NOEXCEPT {}
2116
2117 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2118 static_assert(sizeof(_Tp) > 0,
2119 "default_delete can not delete incomplete type");
2120 static_assert(!is_void<_Tp>::value,
2121 "default_delete can not delete incomplete type");
2122 delete __ptr;
2123 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124};
2125
2126template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002127struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2128private:
2129 template <class _Up>
2130 struct _EnableIfConvertible
2131 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2132
Howard Hinnant4500ca52011-12-18 21:19:44 +00002133public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002134#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002135 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002136#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002137 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002138#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002139
2140 template <class _Up>
2141 _LIBCPP_INLINE_VISIBILITY
2142 default_delete(const default_delete<_Up[]>&,
2143 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2144
2145 template <class _Up>
2146 _LIBCPP_INLINE_VISIBILITY
2147 typename _EnableIfConvertible<_Up>::type
2148 operator()(_Up* __ptr) const _NOEXCEPT {
2149 static_assert(sizeof(_Tp) > 0,
2150 "default_delete can not delete incomplete type");
2151 static_assert(!is_void<_Tp>::value,
2152 "default_delete can not delete void type");
2153 delete[] __ptr;
2154 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155};
2156
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002157template <class _Deleter>
2158struct __unique_ptr_deleter_sfinae {
2159 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2160 typedef const _Deleter& __lval_ref_type;
2161 typedef _Deleter&& __good_rval_ref_type;
2162 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163};
2164
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002165template <class _Deleter>
2166struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2167 typedef const _Deleter& __lval_ref_type;
2168 typedef const _Deleter&& __bad_rval_ref_type;
2169 typedef false_type __enable_rval_overload;
2170};
2171
2172template <class _Deleter>
2173struct __unique_ptr_deleter_sfinae<_Deleter&> {
2174 typedef _Deleter& __lval_ref_type;
2175 typedef _Deleter&& __bad_rval_ref_type;
2176 typedef false_type __enable_rval_overload;
2177};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002178
Vy Nguyene369bd92020-07-13 12:34:37 -04002179#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
2180# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2181#else
2182# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
2183#endif
2184
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002185template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04002186class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002187public:
2188 typedef _Tp element_type;
2189 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002190 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002191
2192 static_assert(!is_rvalue_reference<deleter_type>::value,
2193 "the specified deleter type cannot be an rvalue reference");
2194
2195private:
2196 __compressed_pair<pointer, deleter_type> __ptr_;
2197
2198 struct __nat { int __for_bool_; };
2199
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002200 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002201
2202 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002203 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002204 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002205
2206 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002207 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002208 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002209
2210 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002211 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002212 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002213
2214 template <bool _Dummy, class _Deleter = typename __dependent_type<
2215 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002216 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002217 typename enable_if<is_default_constructible<_Deleter>::value &&
2218 !is_pointer<_Deleter>::value>::type;
2219
2220 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002221 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002222 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2223
2224 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002225 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002226 is_convertible<typename _UPtr::pointer, pointer>::value &&
2227 !is_array<_Up>::value
2228 >::type;
2229
2230 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002231 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002232 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2233 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2234 >::type;
2235
2236 template <class _UDel>
2237 using _EnableIfDeleterAssignable = typename enable_if<
2238 is_assignable<_Dp&, _UDel&&>::value
2239 >::type;
2240
2241public:
2242 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002243 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002244 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002245 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002246
2247 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002248 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002249 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002250 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002251
2252 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002253 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002254 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002255 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002256
2257 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002258 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002259 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002260 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002261 : __ptr_(__p, __d) {}
2262
2263 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002264 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002265 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002266 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002267 : __ptr_(__p, _VSTD::move(__d)) {
2268 static_assert(!is_reference<deleter_type>::value,
2269 "rvalue deleter bound to reference");
2270 }
2271
2272 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002273 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002274 _LIBCPP_INLINE_VISIBILITY
2275 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2276
2277 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002278 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002279 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2280 }
2281
2282 template <class _Up, class _Ep,
2283 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2284 class = _EnableIfDeleterConvertible<_Ep>
2285 >
2286 _LIBCPP_INLINE_VISIBILITY
2287 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2288 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2289
2290#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2291 template <class _Up>
2292 _LIBCPP_INLINE_VISIBILITY
2293 unique_ptr(auto_ptr<_Up>&& __p,
2294 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002295 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002296 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002297 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002298#endif
2299
2300 _LIBCPP_INLINE_VISIBILITY
2301 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2302 reset(__u.release());
2303 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2304 return *this;
2305 }
2306
2307 template <class _Up, class _Ep,
2308 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2309 class = _EnableIfDeleterAssignable<_Ep>
2310 >
2311 _LIBCPP_INLINE_VISIBILITY
2312 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2313 reset(__u.release());
2314 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2315 return *this;
2316 }
2317
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002318#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2319 template <class _Up>
2320 _LIBCPP_INLINE_VISIBILITY
2321 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2322 is_same<_Dp, default_delete<_Tp> >::value,
2323 unique_ptr&>::type
2324 operator=(auto_ptr<_Up> __p) {
2325 reset(__p.release());
2326 return *this;
2327 }
2328#endif
2329
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002330#ifdef _LIBCPP_CXX03_LANG
2331 unique_ptr(unique_ptr const&) = delete;
2332 unique_ptr& operator=(unique_ptr const&) = delete;
2333#endif
2334
2335
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002336 _LIBCPP_INLINE_VISIBILITY
2337 ~unique_ptr() { reset(); }
2338
2339 _LIBCPP_INLINE_VISIBILITY
2340 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2341 reset();
2342 return *this;
2343 }
2344
2345 _LIBCPP_INLINE_VISIBILITY
2346 typename add_lvalue_reference<_Tp>::type
2347 operator*() const {
2348 return *__ptr_.first();
2349 }
2350 _LIBCPP_INLINE_VISIBILITY
2351 pointer operator->() const _NOEXCEPT {
2352 return __ptr_.first();
2353 }
2354 _LIBCPP_INLINE_VISIBILITY
2355 pointer get() const _NOEXCEPT {
2356 return __ptr_.first();
2357 }
2358 _LIBCPP_INLINE_VISIBILITY
2359 deleter_type& get_deleter() _NOEXCEPT {
2360 return __ptr_.second();
2361 }
2362 _LIBCPP_INLINE_VISIBILITY
2363 const deleter_type& get_deleter() const _NOEXCEPT {
2364 return __ptr_.second();
2365 }
2366 _LIBCPP_INLINE_VISIBILITY
2367 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2368 return __ptr_.first() != nullptr;
2369 }
2370
2371 _LIBCPP_INLINE_VISIBILITY
2372 pointer release() _NOEXCEPT {
2373 pointer __t = __ptr_.first();
2374 __ptr_.first() = pointer();
2375 return __t;
2376 }
2377
2378 _LIBCPP_INLINE_VISIBILITY
2379 void reset(pointer __p = pointer()) _NOEXCEPT {
2380 pointer __tmp = __ptr_.first();
2381 __ptr_.first() = __p;
2382 if (__tmp)
2383 __ptr_.second()(__tmp);
2384 }
2385
2386 _LIBCPP_INLINE_VISIBILITY
2387 void swap(unique_ptr& __u) _NOEXCEPT {
2388 __ptr_.swap(__u.__ptr_);
2389 }
2390};
2391
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002392
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002394class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002396 typedef _Tp element_type;
2397 typedef _Dp deleter_type;
2398 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2399
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002401 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402
Eric Fiselier31127cd2017-04-16 02:14:31 +00002403 template <class _From>
2404 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405
Eric Fiselier31127cd2017-04-16 02:14:31 +00002406 template <class _FromElem>
2407 struct _CheckArrayPointerConversion<_FromElem*>
2408 : integral_constant<bool,
2409 is_same<_FromElem*, pointer>::value ||
2410 (is_same<pointer, element_type*>::value &&
2411 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2412 >
2413 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414
Eric Fiselier31127cd2017-04-16 02:14:31 +00002415 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002416
2417 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002418 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002419 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002420
2421 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002422 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002423 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002424
2425 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002426 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002427 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002428
2429 template <bool _Dummy, class _Deleter = typename __dependent_type<
2430 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002431 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002432 typename enable_if<is_default_constructible<_Deleter>::value &&
2433 !is_pointer<_Deleter>::value>::type;
2434
2435 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002436 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002437 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2438
2439 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002440 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002441 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002442 >::type;
2443
2444 template <class _UPtr, class _Up,
2445 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002446 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002447 is_array<_Up>::value &&
2448 is_same<pointer, element_type*>::value &&
2449 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2450 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2451 >::type;
2452
2453 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002454 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002455 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2456 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2457 >::type;
2458
2459 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002460 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002461 is_assignable<_Dp&, _UDel&&>::value
2462 >::type;
2463
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002465 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002466 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002467 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002468 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002470 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002471 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002472 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002473 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002475 template <class _Pp, bool _Dummy = true,
2476 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002477 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002478 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002479 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002480 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002482 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002483 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2484 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002485 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002486 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002487 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002489 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002490 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002491 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002492 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002493 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002495 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002496 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2497 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002498 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002499 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002500 : __ptr_(__p, _VSTD::move(__d)) {
2501 static_assert(!is_reference<deleter_type>::value,
2502 "rvalue deleter bound to reference");
2503 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002505 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002506 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002507 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002508 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002509 : __ptr_(nullptr, _VSTD::move(__d)) {
2510 static_assert(!is_reference<deleter_type>::value,
2511 "rvalue deleter bound to reference");
2512 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002513
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002514 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002515 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2516 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002517 _LIBCPP_INLINE_VISIBILITY
2518 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002519
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002520 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002521 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002522 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2523 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002524
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002525 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002526 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002527 reset(__u.release());
2528 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2529 return *this;
2530 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002532 template <class _Up, class _Ep,
2533 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2534 class = _EnableIfDeleterConvertible<_Ep>
2535 >
2536 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002537 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002538 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002539 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002541 template <class _Up, class _Ep,
2542 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2543 class = _EnableIfDeleterAssignable<_Ep>
2544 >
2545 _LIBCPP_INLINE_VISIBILITY
2546 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002547 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002548 reset(__u.release());
2549 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2550 return *this;
2551 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002553#ifdef _LIBCPP_CXX03_LANG
2554 unique_ptr(unique_ptr const&) = delete;
2555 unique_ptr& operator=(unique_ptr const&) = delete;
2556#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002557
2558public:
2559 _LIBCPP_INLINE_VISIBILITY
2560 ~unique_ptr() { reset(); }
2561
2562 _LIBCPP_INLINE_VISIBILITY
2563 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2564 reset();
2565 return *this;
2566 }
2567
2568 _LIBCPP_INLINE_VISIBILITY
2569 typename add_lvalue_reference<_Tp>::type
2570 operator[](size_t __i) const {
2571 return __ptr_.first()[__i];
2572 }
2573 _LIBCPP_INLINE_VISIBILITY
2574 pointer get() const _NOEXCEPT {
2575 return __ptr_.first();
2576 }
2577
2578 _LIBCPP_INLINE_VISIBILITY
2579 deleter_type& get_deleter() _NOEXCEPT {
2580 return __ptr_.second();
2581 }
2582
2583 _LIBCPP_INLINE_VISIBILITY
2584 const deleter_type& get_deleter() const _NOEXCEPT {
2585 return __ptr_.second();
2586 }
2587 _LIBCPP_INLINE_VISIBILITY
2588 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2589 return __ptr_.first() != nullptr;
2590 }
2591
2592 _LIBCPP_INLINE_VISIBILITY
2593 pointer release() _NOEXCEPT {
2594 pointer __t = __ptr_.first();
2595 __ptr_.first() = pointer();
2596 return __t;
2597 }
2598
2599 template <class _Pp>
2600 _LIBCPP_INLINE_VISIBILITY
2601 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002602 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002603 >::type
2604 reset(_Pp __p) _NOEXCEPT {
2605 pointer __tmp = __ptr_.first();
2606 __ptr_.first() = __p;
2607 if (__tmp)
2608 __ptr_.second()(__tmp);
2609 }
2610
2611 _LIBCPP_INLINE_VISIBILITY
2612 void reset(nullptr_t = nullptr) _NOEXCEPT {
2613 pointer __tmp = __ptr_.first();
2614 __ptr_.first() = nullptr;
2615 if (__tmp)
2616 __ptr_.second()(__tmp);
2617 }
2618
2619 _LIBCPP_INLINE_VISIBILITY
2620 void swap(unique_ptr& __u) _NOEXCEPT {
2621 __ptr_.swap(__u.__ptr_);
2622 }
2623
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624};
2625
2626template <class _Tp, class _Dp>
2627inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002628typename enable_if<
2629 __is_swappable<_Dp>::value,
2630 void
2631>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002632swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633
2634template <class _T1, class _D1, class _T2, class _D2>
2635inline _LIBCPP_INLINE_VISIBILITY
2636bool
2637operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2638
2639template <class _T1, class _D1, class _T2, class _D2>
2640inline _LIBCPP_INLINE_VISIBILITY
2641bool
2642operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2643
2644template <class _T1, class _D1, class _T2, class _D2>
2645inline _LIBCPP_INLINE_VISIBILITY
2646bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002647operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2648{
2649 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2650 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002651 typedef typename common_type<_P1, _P2>::type _Vp;
2652 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002653}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654
2655template <class _T1, class _D1, class _T2, class _D2>
2656inline _LIBCPP_INLINE_VISIBILITY
2657bool
2658operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2659
2660template <class _T1, class _D1, class _T2, class _D2>
2661inline _LIBCPP_INLINE_VISIBILITY
2662bool
2663operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2664
2665template <class _T1, class _D1, class _T2, class _D2>
2666inline _LIBCPP_INLINE_VISIBILITY
2667bool
2668operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2669
Howard Hinnantb17caf92012-02-21 21:02:58 +00002670template <class _T1, class _D1>
2671inline _LIBCPP_INLINE_VISIBILITY
2672bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002673operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002674{
2675 return !__x;
2676}
2677
2678template <class _T1, class _D1>
2679inline _LIBCPP_INLINE_VISIBILITY
2680bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002681operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002682{
2683 return !__x;
2684}
2685
2686template <class _T1, class _D1>
2687inline _LIBCPP_INLINE_VISIBILITY
2688bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002689operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002690{
2691 return static_cast<bool>(__x);
2692}
2693
2694template <class _T1, class _D1>
2695inline _LIBCPP_INLINE_VISIBILITY
2696bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002697operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002698{
2699 return static_cast<bool>(__x);
2700}
2701
2702template <class _T1, class _D1>
2703inline _LIBCPP_INLINE_VISIBILITY
2704bool
2705operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2706{
2707 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2708 return less<_P1>()(__x.get(), nullptr);
2709}
2710
2711template <class _T1, class _D1>
2712inline _LIBCPP_INLINE_VISIBILITY
2713bool
2714operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2715{
2716 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2717 return less<_P1>()(nullptr, __x.get());
2718}
2719
2720template <class _T1, class _D1>
2721inline _LIBCPP_INLINE_VISIBILITY
2722bool
2723operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2724{
2725 return nullptr < __x;
2726}
2727
2728template <class _T1, class _D1>
2729inline _LIBCPP_INLINE_VISIBILITY
2730bool
2731operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2732{
2733 return __x < nullptr;
2734}
2735
2736template <class _T1, class _D1>
2737inline _LIBCPP_INLINE_VISIBILITY
2738bool
2739operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2740{
2741 return !(nullptr < __x);
2742}
2743
2744template <class _T1, class _D1>
2745inline _LIBCPP_INLINE_VISIBILITY
2746bool
2747operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2748{
2749 return !(__x < nullptr);
2750}
2751
2752template <class _T1, class _D1>
2753inline _LIBCPP_INLINE_VISIBILITY
2754bool
2755operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2756{
2757 return !(__x < nullptr);
2758}
2759
2760template <class _T1, class _D1>
2761inline _LIBCPP_INLINE_VISIBILITY
2762bool
2763operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2764{
2765 return !(nullptr < __x);
2766}
2767
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002768#if _LIBCPP_STD_VER > 11
2769
2770template<class _Tp>
2771struct __unique_if
2772{
2773 typedef unique_ptr<_Tp> __unique_single;
2774};
2775
2776template<class _Tp>
2777struct __unique_if<_Tp[]>
2778{
2779 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2780};
2781
2782template<class _Tp, size_t _Np>
2783struct __unique_if<_Tp[_Np]>
2784{
2785 typedef void __unique_array_known_bound;
2786};
2787
2788template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002789inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002790typename __unique_if<_Tp>::__unique_single
2791make_unique(_Args&&... __args)
2792{
2793 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2794}
2795
2796template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002797inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002798typename __unique_if<_Tp>::__unique_array_unknown_bound
2799make_unique(size_t __n)
2800{
2801 typedef typename remove_extent<_Tp>::type _Up;
2802 return unique_ptr<_Tp>(new _Up[__n]());
2803}
2804
2805template<class _Tp, class... _Args>
2806 typename __unique_if<_Tp>::__unique_array_known_bound
2807 make_unique(_Args&&...) = delete;
2808
2809#endif // _LIBCPP_STD_VER > 11
2810
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002811template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002812#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002813struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002814#else
2815struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002816 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002817#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002818{
2819 typedef unique_ptr<_Tp, _Dp> argument_type;
2820 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002821 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002822 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002823 {
2824 typedef typename argument_type::pointer pointer;
2825 return hash<pointer>()(__ptr.get());
2826 }
2827};
2828
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829struct __destruct_n
2830{
2831private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002832 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833
2834 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002835 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002836 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837
2838 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002839 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 {}
2841
Howard Hinnant719bda32011-05-28 14:41:13 +00002842 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002843 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002844 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845 {}
2846
Howard Hinnant719bda32011-05-28 14:41:13 +00002847 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002848 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002849 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 {}
2851public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002852 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002853 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854
2855 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002856 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002857 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858
2859 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002860 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002861 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862
2863 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002864 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002865 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866};
2867
2868template <class _Alloc>
2869class __allocator_destructor
2870{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002871 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002873 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2874 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875private:
2876 _Alloc& __alloc_;
2877 size_type __s_;
2878public:
2879 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002880 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002883 void operator()(pointer __p) _NOEXCEPT
2884 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885};
2886
2887template <class _InputIterator, class _ForwardIterator>
2888_ForwardIterator
2889uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2890{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002892#ifndef _LIBCPP_NO_EXCEPTIONS
2893 _ForwardIterator __s = __r;
2894 try
2895 {
2896#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002897 for (; __f != __l; ++__f, (void) ++__r)
2898 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002899#ifndef _LIBCPP_NO_EXCEPTIONS
2900 }
2901 catch (...)
2902 {
2903 for (; __s != __r; ++__s)
2904 __s->~value_type();
2905 throw;
2906 }
2907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 return __r;
2909}
2910
2911template <class _InputIterator, class _Size, class _ForwardIterator>
2912_ForwardIterator
2913uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2914{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002916#ifndef _LIBCPP_NO_EXCEPTIONS
2917 _ForwardIterator __s = __r;
2918 try
2919 {
2920#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002921 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
2922 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002923#ifndef _LIBCPP_NO_EXCEPTIONS
2924 }
2925 catch (...)
2926 {
2927 for (; __s != __r; ++__s)
2928 __s->~value_type();
2929 throw;
2930 }
2931#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 return __r;
2933}
2934
2935template <class _ForwardIterator, class _Tp>
2936void
2937uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2938{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002940#ifndef _LIBCPP_NO_EXCEPTIONS
2941 _ForwardIterator __s = __f;
2942 try
2943 {
2944#endif
2945 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002946 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002947#ifndef _LIBCPP_NO_EXCEPTIONS
2948 }
2949 catch (...)
2950 {
2951 for (; __s != __f; ++__s)
2952 __s->~value_type();
2953 throw;
2954 }
2955#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956}
2957
2958template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002959_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2961{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002963#ifndef _LIBCPP_NO_EXCEPTIONS
2964 _ForwardIterator __s = __f;
2965 try
2966 {
2967#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002968 for (; __n > 0; ++__f, (void) --__n)
2969 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002970#ifndef _LIBCPP_NO_EXCEPTIONS
2971 }
2972 catch (...)
2973 {
2974 for (; __s != __f; ++__s)
2975 __s->~value_type();
2976 throw;
2977 }
2978#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002979 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980}
2981
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002982#if _LIBCPP_STD_VER > 14
2983
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002984template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002985inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002986void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2987 for (; __first != __last; ++__first)
2988 _VSTD::destroy_at(_VSTD::addressof(*__first));
2989}
2990
2991template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002992inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002993_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2994 for (; __n > 0; (void)++__first, --__n)
2995 _VSTD::destroy_at(_VSTD::addressof(*__first));
2996 return __first;
2997}
2998
Eric Fiselier290c07c2016-10-11 21:13:44 +00002999template <class _ForwardIterator>
3000inline _LIBCPP_INLINE_VISIBILITY
3001void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3002 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3003 auto __idx = __first;
3004#ifndef _LIBCPP_NO_EXCEPTIONS
3005 try {
3006#endif
3007 for (; __idx != __last; ++__idx)
3008 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3009#ifndef _LIBCPP_NO_EXCEPTIONS
3010 } catch (...) {
3011 _VSTD::destroy(__first, __idx);
3012 throw;
3013 }
3014#endif
3015}
3016
3017template <class _ForwardIterator, class _Size>
3018inline _LIBCPP_INLINE_VISIBILITY
3019_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3020 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3021 auto __idx = __first;
3022#ifndef _LIBCPP_NO_EXCEPTIONS
3023 try {
3024#endif
3025 for (; __n > 0; (void)++__idx, --__n)
3026 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3027 return __idx;
3028#ifndef _LIBCPP_NO_EXCEPTIONS
3029 } catch (...) {
3030 _VSTD::destroy(__first, __idx);
3031 throw;
3032 }
3033#endif
3034}
3035
3036
3037template <class _ForwardIterator>
3038inline _LIBCPP_INLINE_VISIBILITY
3039void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3040 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3041 auto __idx = __first;
3042#ifndef _LIBCPP_NO_EXCEPTIONS
3043 try {
3044#endif
3045 for (; __idx != __last; ++__idx)
3046 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3047#ifndef _LIBCPP_NO_EXCEPTIONS
3048 } catch (...) {
3049 _VSTD::destroy(__first, __idx);
3050 throw;
3051 }
3052#endif
3053}
3054
3055template <class _ForwardIterator, class _Size>
3056inline _LIBCPP_INLINE_VISIBILITY
3057_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3058 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3059 auto __idx = __first;
3060#ifndef _LIBCPP_NO_EXCEPTIONS
3061 try {
3062#endif
3063 for (; __n > 0; (void)++__idx, --__n)
3064 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3065 return __idx;
3066#ifndef _LIBCPP_NO_EXCEPTIONS
3067 } catch (...) {
3068 _VSTD::destroy(__first, __idx);
3069 throw;
3070 }
3071#endif
3072}
3073
3074
3075template <class _InputIt, class _ForwardIt>
3076inline _LIBCPP_INLINE_VISIBILITY
3077_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3078 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3079 auto __idx = __first_res;
3080#ifndef _LIBCPP_NO_EXCEPTIONS
3081 try {
3082#endif
3083 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003084 ::new((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00003085 return __idx;
3086#ifndef _LIBCPP_NO_EXCEPTIONS
3087 } catch (...) {
3088 _VSTD::destroy(__first_res, __idx);
3089 throw;
3090 }
3091#endif
3092}
3093
3094template <class _InputIt, class _Size, class _ForwardIt>
3095inline _LIBCPP_INLINE_VISIBILITY
3096pair<_InputIt, _ForwardIt>
3097uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3098 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3099 auto __idx = __first_res;
3100#ifndef _LIBCPP_NO_EXCEPTIONS
3101 try {
3102#endif
3103 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003104 ::new((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00003105 return {__first, __idx};
3106#ifndef _LIBCPP_NO_EXCEPTIONS
3107 } catch (...) {
3108 _VSTD::destroy(__first_res, __idx);
3109 throw;
3110 }
3111#endif
3112}
3113
3114
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003115#endif // _LIBCPP_STD_VER > 14
3116
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003117// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3118// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003119// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003120#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3121 && defined(__ATOMIC_RELAXED) \
3122 && defined(__ATOMIC_ACQ_REL)
3123# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003124#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003125# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3126#endif
3127
3128template <class _Tp>
3129inline _LIBCPP_INLINE_VISIBILITY _Tp
3130__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3131{
3132#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3133 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3134#else
3135 return __t += 1;
3136#endif
3137}
3138
3139template <class _Tp>
3140inline _LIBCPP_INLINE_VISIBILITY _Tp
3141__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3142{
3143#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3144 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3145#else
3146 return __t -= 1;
3147#endif
3148}
3149
Howard Hinnant756c69b2010-09-22 16:48:34 +00003150class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 : public std::exception
3152{
3153public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003154 bad_weak_ptr() _NOEXCEPT = default;
3155 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003156 virtual ~bad_weak_ptr() _NOEXCEPT;
3157 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158};
3159
Louis Dionne16fe2952018-07-11 23:14:33 +00003160_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003161void __throw_bad_weak_ptr()
3162{
3163#ifndef _LIBCPP_NO_EXCEPTIONS
3164 throw bad_weak_ptr();
3165#else
3166 _VSTD::abort();
3167#endif
3168}
3169
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003170template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003172class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173{
3174 __shared_count(const __shared_count&);
3175 __shared_count& operator=(const __shared_count&);
3176
3177protected:
3178 long __shared_owners_;
3179 virtual ~__shared_count();
3180private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003181 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182
3183public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003185 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186 : __shared_owners_(__refs) {}
3187
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003188#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003189 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003190 void __add_shared() _NOEXCEPT;
3191 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003192#else
3193 _LIBCPP_INLINE_VISIBILITY
3194 void __add_shared() _NOEXCEPT {
3195 __libcpp_atomic_refcount_increment(__shared_owners_);
3196 }
3197 _LIBCPP_INLINE_VISIBILITY
3198 bool __release_shared() _NOEXCEPT {
3199 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3200 __on_zero_shared();
3201 return true;
3202 }
3203 return false;
3204 }
3205#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003206 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003207 long use_count() const _NOEXCEPT {
3208 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3209 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210};
3211
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003212class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213 : private __shared_count
3214{
3215 long __shared_weak_owners_;
3216
3217public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003219 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220 : __shared_count(__refs),
3221 __shared_weak_owners_(__refs) {}
3222protected:
3223 virtual ~__shared_weak_count();
3224
3225public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003226#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003227 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003228 void __add_shared() _NOEXCEPT;
3229 void __add_weak() _NOEXCEPT;
3230 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003231#else
3232 _LIBCPP_INLINE_VISIBILITY
3233 void __add_shared() _NOEXCEPT {
3234 __shared_count::__add_shared();
3235 }
3236 _LIBCPP_INLINE_VISIBILITY
3237 void __add_weak() _NOEXCEPT {
3238 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3239 }
3240 _LIBCPP_INLINE_VISIBILITY
3241 void __release_shared() _NOEXCEPT {
3242 if (__shared_count::__release_shared())
3243 __release_weak();
3244 }
3245#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003246 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003248 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3249 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250
Howard Hinnant719bda32011-05-28 14:41:13 +00003251 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003253 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254};
3255
3256template <class _Tp, class _Dp, class _Alloc>
3257class __shared_ptr_pointer
3258 : public __shared_weak_count
3259{
3260 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3261public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003264 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265
Howard Hinnant72f73582010-08-11 17:04:31 +00003266#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003267 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003268#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269
3270private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003271 virtual void __on_zero_shared() _NOEXCEPT;
3272 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273};
3274
Howard Hinnant72f73582010-08-11 17:04:31 +00003275#ifndef _LIBCPP_NO_RTTI
3276
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277template <class _Tp, class _Dp, class _Alloc>
3278const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003279__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003281 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282}
3283
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003284#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003285
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286template <class _Tp, class _Dp, class _Alloc>
3287void
Howard Hinnant719bda32011-05-28 14:41:13 +00003288__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289{
3290 __data_.first().second()(__data_.first().first());
3291 __data_.first().second().~_Dp();
3292}
3293
3294template <class _Tp, class _Dp, class _Alloc>
3295void
Howard Hinnant719bda32011-05-28 14:41:13 +00003296__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003298 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3299 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003300 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3301
Eric Fiselierf8898c82015-02-05 23:01:40 +00003302 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003304 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305}
3306
3307template <class _Tp, class _Alloc>
3308class __shared_ptr_emplace
3309 : public __shared_weak_count
3310{
3311 __compressed_pair<_Alloc, _Tp> __data_;
3312public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313
Howard Hinnant756c69b2010-09-22 16:48:34 +00003314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003315 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003316 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003317
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003318#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003321 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003322 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3323 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003324#else
3325 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003326 _LIBCPP_INLINE_VISIBILITY
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003327 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
3328 : __data_(__a, _Tp(_VSTD::forward<_Args>(__args)...)) {}
3329#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330
3331private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003332 virtual void __on_zero_shared() _NOEXCEPT;
3333 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003335 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef34e5c82020-11-10 12:47:10 -05003336 _Alloc& __get_alloc() _NOEXCEPT {return __data_.first();}
3337 _LIBCPP_INLINE_VISIBILITY
3338 _Tp* __get_elem() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339};
3340
3341template <class _Tp, class _Alloc>
3342void
Howard Hinnant719bda32011-05-28 14:41:13 +00003343__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003344{
Louis Dionnef34e5c82020-11-10 12:47:10 -05003345 __get_elem()->~_Tp();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346}
3347
3348template <class _Tp, class _Alloc>
3349void
Howard Hinnant719bda32011-05-28 14:41:13 +00003350__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003352 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3353 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003354 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Louis Dionnef34e5c82020-11-10 12:47:10 -05003355 _Al __a(__get_alloc());
3356 __get_alloc().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003357 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358}
3359
Erik Pilkington2a398762017-05-25 15:43:31 +00003360struct __shared_ptr_dummy_rebind_allocator_type;
3361template <>
3362class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3363{
3364public:
3365 template <class _Other>
3366 struct rebind
3367 {
3368 typedef allocator<_Other> other;
3369 };
3370};
3371
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003372template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373
zoecarverd73f42a2020-05-11 18:42:50 -07003374template<class _Tp, class _Up>
3375struct __compatible_with
3376#if _LIBCPP_STD_VER > 14
3377 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3378#else
3379 : is_convertible<_Tp*, _Up*> {};
3380#endif // _LIBCPP_STD_VER > 14
3381
Vy Nguyene369bd92020-07-13 12:34:37 -04003382#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
3383# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
3384#else
3385# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
3386#endif
3387
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003389class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003391public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003392#if _LIBCPP_STD_VER > 14
3393 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003394 typedef remove_extent_t<_Tp> element_type;
3395#else
3396 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003397#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003398
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399private:
3400 element_type* __ptr_;
3401 __shared_weak_count* __cntrl_;
3402
3403 struct __nat {int __for_bool_;};
3404public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003406 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003408 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003409 template<class _Yp>
3410 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003411 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003412 template<class _Yp, class _Dp>
3413 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003414 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003415 template<class _Yp, class _Dp, class _Alloc>
3416 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003417 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3419 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003420 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003422 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003426 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003427 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003429 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003430 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003431 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003432 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003434 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003435#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00003436 template<class _Yp>
3437 shared_ptr(auto_ptr<_Yp>&& __r,
3438 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003439#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00003440 template <class _Yp, class _Dp>
3441 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3442 typename enable_if
3443 <
3444 !is_lvalue_reference<_Dp>::value &&
3445 !is_array<_Yp>::value &&
3446 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3447 __nat
3448 >::type = __nat());
3449 template <class _Yp, class _Dp>
3450 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3451 typename enable_if
3452 <
3453 is_lvalue_reference<_Dp>::value &&
3454 !is_array<_Yp>::value &&
3455 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3456 __nat
3457 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458
3459 ~shared_ptr();
3460
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003462 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003463 template<class _Yp>
3464 typename enable_if
3465 <
zoecarverd73f42a2020-05-11 18:42:50 -07003466 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003467 shared_ptr&
3468 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003470 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003472 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003473 template<class _Yp>
3474 typename enable_if
3475 <
zoecarverd73f42a2020-05-11 18:42:50 -07003476 __compatible_with<_Yp, element_type>::value,
3477 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003478 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003480 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003481#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003482 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003484 typename enable_if
3485 <
3486 !is_array<_Yp>::value &&
3487 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003488 shared_ptr
3489 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003490 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003491#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003492 template <class _Yp, class _Dp>
3493 typename enable_if
3494 <
3495 !is_array<_Yp>::value &&
3496 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3497 shared_ptr&
3498 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003500 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003503 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003505 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003506 template<class _Yp>
3507 typename enable_if
3508 <
zoecarverd73f42a2020-05-11 18:42:50 -07003509 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003510 void
3511 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003513 reset(_Yp* __p);
3514 template<class _Yp, class _Dp>
3515 typename enable_if
3516 <
zoecarverd73f42a2020-05-11 18:42:50 -07003517 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003518 void
3519 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003521 reset(_Yp* __p, _Dp __d);
3522 template<class _Yp, class _Dp, class _Alloc>
3523 typename enable_if
3524 <
zoecarverd73f42a2020-05-11 18:42:50 -07003525 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003526 void
3527 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003529 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530
Howard Hinnant756c69b2010-09-22 16:48:34 +00003531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003532 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003534 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3535 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003536 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003537 element_type* operator->() const _NOEXCEPT
3538 {
3539 static_assert(!_VSTD::is_array<_Tp>::value,
3540 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3541 return __ptr_;
3542 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003544 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003546 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003547 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05003548 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003549 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003550 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003551 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003553 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003554 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003555 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003557 _LIBCPP_INLINE_VISIBILITY
3558 bool
3559 __owner_equivalent(const shared_ptr& __p) const
3560 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561
zoecarverd73f42a2020-05-11 18:42:50 -07003562#if _LIBCPP_STD_VER > 14
3563 typename add_lvalue_reference<element_type>::type
3564 _LIBCPP_INLINE_VISIBILITY
3565 operator[](ptrdiff_t __i) const
3566 {
3567 static_assert(_VSTD::is_array<_Tp>::value,
3568 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3569 return __ptr_[__i];
3570 }
3571#endif
3572
Howard Hinnant72f73582010-08-11 17:04:31 +00003573#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003576 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003577 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003578 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003579 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003580#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581
Zoe Carverd9040c72019-10-22 15:16:49 +00003582 template<class _Yp, class _CntrlBlk>
3583 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07003584 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00003585 {
3586 shared_ptr<_Tp> __r;
3587 __r.__ptr_ = __p;
3588 __r.__cntrl_ = __cntrl;
3589 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3590 return __r;
3591 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003592
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003594 template <class _Yp, bool = is_function<_Yp>::value>
3595 struct __shared_ptr_default_allocator
3596 {
3597 typedef allocator<_Yp> type;
3598 };
3599
3600 template <class _Yp>
3601 struct __shared_ptr_default_allocator<_Yp, true>
3602 {
3603 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3604 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003606 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003607 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003608 typename enable_if<is_convertible<_OrigPtr*,
3609 const enable_shared_from_this<_Yp>*
3610 >::value,
3611 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003612 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3613 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003615 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003616 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003617 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003618 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3619 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003620 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621 }
3622
Erik Pilkington2a398762017-05-25 15:43:31 +00003623 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624
zoecarverd73f42a2020-05-11 18:42:50 -07003625 template <class, class _Yp>
3626 struct __shared_ptr_default_delete
3627 : default_delete<_Yp> {};
3628
3629 template <class _Yp, class _Un, size_t _Sz>
3630 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3631 : default_delete<_Yp[]> {};
3632
3633 template <class _Yp, class _Un>
3634 struct __shared_ptr_default_delete<_Yp[], _Un>
3635 : default_delete<_Yp[]> {};
3636
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003637 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3638 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639};
3640
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003641#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3642template<class _Tp>
3643shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3644template<class _Tp, class _Dp>
3645shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3646#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003647
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003649inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003650_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003651shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003652 : __ptr_(nullptr),
3653 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654{
3655}
3656
3657template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003658inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003659_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003660shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003661 : __ptr_(nullptr),
3662 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663{
3664}
3665
3666template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003667template<class _Yp>
3668shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003669 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670 : __ptr_(__p)
3671{
3672 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003673 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07003674 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3675 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003676 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003677 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678}
3679
3680template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003681template<class _Yp, class _Dp>
3682shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003683 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684 : __ptr_(__p)
3685{
3686#ifndef _LIBCPP_NO_EXCEPTIONS
3687 try
3688 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003689#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003690 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3691 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3692 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003693 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694#ifndef _LIBCPP_NO_EXCEPTIONS
3695 }
3696 catch (...)
3697 {
3698 __d(__p);
3699 throw;
3700 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003701#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702}
3703
3704template<class _Tp>
3705template<class _Dp>
3706shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003707 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
3709#ifndef _LIBCPP_NO_EXCEPTIONS
3710 try
3711 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003712#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003713 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3714 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3715 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716#ifndef _LIBCPP_NO_EXCEPTIONS
3717 }
3718 catch (...)
3719 {
3720 __d(__p);
3721 throw;
3722 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003723#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724}
3725
3726template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003727template<class _Yp, class _Dp, class _Alloc>
3728shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003729 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730 : __ptr_(__p)
3731{
3732#ifndef _LIBCPP_NO_EXCEPTIONS
3733 try
3734 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003735#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003737 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738 typedef __allocator_destructor<_A2> _D2;
3739 _A2 __a2(__a);
3740 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003741 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3742 _CntrlBlk(__p, __d, __a);
3743 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003744 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745#ifndef _LIBCPP_NO_EXCEPTIONS
3746 }
3747 catch (...)
3748 {
3749 __d(__p);
3750 throw;
3751 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003752#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753}
3754
3755template<class _Tp>
3756template<class _Dp, class _Alloc>
3757shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003758 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759{
3760#ifndef _LIBCPP_NO_EXCEPTIONS
3761 try
3762 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003763#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003765 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766 typedef __allocator_destructor<_A2> _D2;
3767 _A2 __a2(__a);
3768 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003769 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3770 _CntrlBlk(__p, __d, __a);
3771 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772#ifndef _LIBCPP_NO_EXCEPTIONS
3773 }
3774 catch (...)
3775 {
3776 __d(__p);
3777 throw;
3778 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003779#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780}
3781
3782template<class _Tp>
3783template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003784inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003785shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786 : __ptr_(__p),
3787 __cntrl_(__r.__cntrl_)
3788{
3789 if (__cntrl_)
3790 __cntrl_->__add_shared();
3791}
3792
3793template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003794inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003795shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796 : __ptr_(__r.__ptr_),
3797 __cntrl_(__r.__cntrl_)
3798{
3799 if (__cntrl_)
3800 __cntrl_->__add_shared();
3801}
3802
3803template<class _Tp>
3804template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003805inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003807 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003808 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809 : __ptr_(__r.__ptr_),
3810 __cntrl_(__r.__cntrl_)
3811{
3812 if (__cntrl_)
3813 __cntrl_->__add_shared();
3814}
3815
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003817inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003818shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819 : __ptr_(__r.__ptr_),
3820 __cntrl_(__r.__cntrl_)
3821{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003822 __r.__ptr_ = nullptr;
3823 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824}
3825
3826template<class _Tp>
3827template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003828inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003829shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003830 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003831 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 : __ptr_(__r.__ptr_),
3833 __cntrl_(__r.__cntrl_)
3834{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003835 __r.__ptr_ = nullptr;
3836 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837}
3838
Marshall Clowb22274f2017-01-24 22:22:33 +00003839#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003841template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003842shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003843 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844 : __ptr_(__r.get())
3845{
3846 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3847 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003848 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849 __r.release();
3850}
Marshall Clowb22274f2017-01-24 22:22:33 +00003851#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852
3853template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003854template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003856 typename enable_if
3857 <
3858 !is_lvalue_reference<_Dp>::value &&
3859 !is_array<_Yp>::value &&
3860 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3861 __nat
3862 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863 : __ptr_(__r.get())
3864{
Marshall Clow35cde742015-05-10 13:59:45 +00003865#if _LIBCPP_STD_VER > 11
3866 if (__ptr_ == nullptr)
3867 __cntrl_ = nullptr;
3868 else
3869#endif
3870 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003871 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3872 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3873 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003874 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003875 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876 __r.release();
3877}
3878
3879template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003880template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003882 typename enable_if
3883 <
3884 is_lvalue_reference<_Dp>::value &&
3885 !is_array<_Yp>::value &&
3886 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3887 __nat
3888 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889 : __ptr_(__r.get())
3890{
Marshall Clow35cde742015-05-10 13:59:45 +00003891#if _LIBCPP_STD_VER > 11
3892 if (__ptr_ == nullptr)
3893 __cntrl_ = nullptr;
3894 else
3895#endif
3896 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003897 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003898 typedef __shared_ptr_pointer<_Yp*,
3899 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003900 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003901 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003902 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003903 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904 __r.release();
3905}
3906
Zoe Carver6cd05c32019-08-19 15:47:16 +00003907template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908shared_ptr<_Tp>::~shared_ptr()
3909{
3910 if (__cntrl_)
3911 __cntrl_->__release_shared();
3912}
3913
3914template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003915inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003917shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918{
3919 shared_ptr(__r).swap(*this);
3920 return *this;
3921}
3922
3923template<class _Tp>
3924template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003925inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003926typename enable_if
3927<
zoecarverd73f42a2020-05-11 18:42:50 -07003928 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003929 shared_ptr<_Tp>&
3930>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003931shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932{
3933 shared_ptr(__r).swap(*this);
3934 return *this;
3935}
3936
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003938inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003940shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003942 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003943 return *this;
3944}
3945
3946template<class _Tp>
3947template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003948inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003949typename enable_if
3950<
zoecarverd73f42a2020-05-11 18:42:50 -07003951 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003952 shared_ptr<_Tp>&
3953>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3955{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003956 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957 return *this;
3958}
3959
Marshall Clowb22274f2017-01-24 22:22:33 +00003960#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961template<class _Tp>
3962template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003963inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003964typename enable_if
3965<
3966 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003967 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003968 shared_ptr<_Tp>
3969>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003970shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3971{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003972 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973 return *this;
3974}
Marshall Clowb22274f2017-01-24 22:22:33 +00003975#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976
3977template<class _Tp>
3978template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003979inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003980typename enable_if
3981<
3982 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003983 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003984 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003985 shared_ptr<_Tp>&
3986>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3988{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003989 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990 return *this;
3991}
3992
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003994inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995void
Howard Hinnant719bda32011-05-28 14:41:13 +00003996shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003998 _VSTD::swap(__ptr_, __r.__ptr_);
3999 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000}
4001
4002template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004003inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004void
Howard Hinnant719bda32011-05-28 14:41:13 +00004005shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006{
4007 shared_ptr().swap(*this);
4008}
4009
4010template<class _Tp>
4011template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004012inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004013typename enable_if
4014<
zoecarverd73f42a2020-05-11 18:42:50 -07004015 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004016 void
4017>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018shared_ptr<_Tp>::reset(_Yp* __p)
4019{
4020 shared_ptr(__p).swap(*this);
4021}
4022
4023template<class _Tp>
4024template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004025inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004026typename enable_if
4027<
zoecarverd73f42a2020-05-11 18:42:50 -07004028 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004029 void
4030>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4032{
4033 shared_ptr(__p, __d).swap(*this);
4034}
4035
4036template<class _Tp>
4037template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004038inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004039typename enable_if
4040<
zoecarverd73f42a2020-05-11 18:42:50 -07004041 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004042 void
4043>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4045{
4046 shared_ptr(__p, __d, __a).swap(*this);
4047}
4048
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004049template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004051typename enable_if
4052<
4053 !is_array<_Tp>::value,
4054 shared_ptr<_Tp>
4055>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056make_shared(_Args&& ...__args)
4057{
Zoe Carverd9040c72019-10-22 15:16:49 +00004058 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4059 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4060 typedef allocator<_CntrlBlk> _A2;
4061 typedef __allocator_destructor<_A2> _D2;
4062
4063 _A2 __a2;
4064 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4065 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4066
Louis Dionnef34e5c82020-11-10 12:47:10 -05004067 _Tp *__ptr = __hold2->__get_elem();
Zoe Carverd9040c72019-10-22 15:16:49 +00004068 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069}
4070
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004071template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004073typename enable_if
4074<
4075 !is_array<_Tp>::value,
4076 shared_ptr<_Tp>
4077>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078allocate_shared(const _Alloc& __a, _Args&& ...__args)
4079{
zoecarver505730a2020-02-25 16:50:57 -08004080 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4081
4082 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4083 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4084 typedef __allocator_destructor<_A2> _D2;
4085
4086 _A2 __a2(__a);
4087 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4088 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4089 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4090
Louis Dionnef34e5c82020-11-10 12:47:10 -05004091 typename shared_ptr<_Tp>::element_type *__p = __hold2->__get_elem();
zoecarver505730a2020-02-25 16:50:57 -08004092 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093}
4094
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095template<class _Tp, class _Up>
4096inline _LIBCPP_INLINE_VISIBILITY
4097bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004098operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099{
4100 return __x.get() == __y.get();
4101}
4102
4103template<class _Tp, class _Up>
4104inline _LIBCPP_INLINE_VISIBILITY
4105bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004106operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107{
4108 return !(__x == __y);
4109}
4110
4111template<class _Tp, class _Up>
4112inline _LIBCPP_INLINE_VISIBILITY
4113bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004114operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004116#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004117 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4118 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004119#else
4120 return less<>()(__x.get(), __y.get());
4121#endif
4122
Howard Hinnantb17caf92012-02-21 21:02:58 +00004123}
4124
4125template<class _Tp, class _Up>
4126inline _LIBCPP_INLINE_VISIBILITY
4127bool
4128operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4129{
4130 return __y < __x;
4131}
4132
4133template<class _Tp, class _Up>
4134inline _LIBCPP_INLINE_VISIBILITY
4135bool
4136operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4137{
4138 return !(__y < __x);
4139}
4140
4141template<class _Tp, class _Up>
4142inline _LIBCPP_INLINE_VISIBILITY
4143bool
4144operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4145{
4146 return !(__x < __y);
4147}
4148
4149template<class _Tp>
4150inline _LIBCPP_INLINE_VISIBILITY
4151bool
4152operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4153{
4154 return !__x;
4155}
4156
4157template<class _Tp>
4158inline _LIBCPP_INLINE_VISIBILITY
4159bool
4160operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4161{
4162 return !__x;
4163}
4164
4165template<class _Tp>
4166inline _LIBCPP_INLINE_VISIBILITY
4167bool
4168operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4169{
4170 return static_cast<bool>(__x);
4171}
4172
4173template<class _Tp>
4174inline _LIBCPP_INLINE_VISIBILITY
4175bool
4176operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4177{
4178 return static_cast<bool>(__x);
4179}
4180
4181template<class _Tp>
4182inline _LIBCPP_INLINE_VISIBILITY
4183bool
4184operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4185{
4186 return less<_Tp*>()(__x.get(), nullptr);
4187}
4188
4189template<class _Tp>
4190inline _LIBCPP_INLINE_VISIBILITY
4191bool
4192operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4193{
4194 return less<_Tp*>()(nullptr, __x.get());
4195}
4196
4197template<class _Tp>
4198inline _LIBCPP_INLINE_VISIBILITY
4199bool
4200operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4201{
4202 return nullptr < __x;
4203}
4204
4205template<class _Tp>
4206inline _LIBCPP_INLINE_VISIBILITY
4207bool
4208operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4209{
4210 return __x < nullptr;
4211}
4212
4213template<class _Tp>
4214inline _LIBCPP_INLINE_VISIBILITY
4215bool
4216operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4217{
4218 return !(nullptr < __x);
4219}
4220
4221template<class _Tp>
4222inline _LIBCPP_INLINE_VISIBILITY
4223bool
4224operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4225{
4226 return !(__x < nullptr);
4227}
4228
4229template<class _Tp>
4230inline _LIBCPP_INLINE_VISIBILITY
4231bool
4232operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4233{
4234 return !(__x < nullptr);
4235}
4236
4237template<class _Tp>
4238inline _LIBCPP_INLINE_VISIBILITY
4239bool
4240operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4241{
4242 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243}
4244
4245template<class _Tp>
4246inline _LIBCPP_INLINE_VISIBILITY
4247void
Howard Hinnant719bda32011-05-28 14:41:13 +00004248swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249{
4250 __x.swap(__y);
4251}
4252
4253template<class _Tp, class _Up>
4254inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004255shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004256static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257{
zoecarverd73f42a2020-05-11 18:42:50 -07004258 return shared_ptr<_Tp>(__r,
4259 static_cast<
4260 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261}
4262
4263template<class _Tp, class _Up>
4264inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004265shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004266dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267{
zoecarverd73f42a2020-05-11 18:42:50 -07004268 typedef typename shared_ptr<_Tp>::element_type _ET;
4269 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4271}
4272
4273template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004274shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004275const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276{
zoecarverd73f42a2020-05-11 18:42:50 -07004277 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004278 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279}
4280
zoecarverd73f42a2020-05-11 18:42:50 -07004281template<class _Tp, class _Up>
4282shared_ptr<_Tp>
4283reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4284{
4285 return shared_ptr<_Tp>(__r,
4286 reinterpret_cast<
4287 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4288}
4289
Howard Hinnant72f73582010-08-11 17:04:31 +00004290#ifndef _LIBCPP_NO_RTTI
4291
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292template<class _Dp, class _Tp>
4293inline _LIBCPP_INLINE_VISIBILITY
4294_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004295get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296{
4297 return __p.template __get_deleter<_Dp>();
4298}
4299
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004300#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004301
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04004303class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004305public:
4306 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307private:
4308 element_type* __ptr_;
4309 __shared_weak_count* __cntrl_;
4310
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004311public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004313 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004314 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004315 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4316 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004318 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004319 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004320 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4321 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004322
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004324 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004325 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004326 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4327 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004328 ~weak_ptr();
4329
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004331 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004332 template<class _Yp>
4333 typename enable_if
4334 <
4335 is_convertible<_Yp*, element_type*>::value,
4336 weak_ptr&
4337 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004339 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4340
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004342 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4343 template<class _Yp>
4344 typename enable_if
4345 <
4346 is_convertible<_Yp*, element_type*>::value,
4347 weak_ptr&
4348 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004350 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4351
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004352 template<class _Yp>
4353 typename enable_if
4354 <
4355 is_convertible<_Yp*, element_type*>::value,
4356 weak_ptr&
4357 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004359 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004360
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004362 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004364 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004365
Howard Hinnant756c69b2010-09-22 16:48:34 +00004366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004367 long use_count() const _NOEXCEPT
4368 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004370 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05004371 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00004372 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004373 template<class _Up>
4374 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004375 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004377 template<class _Up>
4378 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004379 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380 {return __cntrl_ < __r.__cntrl_;}
4381
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004382 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4383 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384};
4385
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004386#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4387template<class _Tp>
4388weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4389#endif
4390
Howard Hinnantc51e1022010-05-11 19:42:16 +00004391template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004392inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004393_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004394weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05004395 : __ptr_(nullptr),
4396 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004397{
4398}
4399
4400template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004401inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004402weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403 : __ptr_(__r.__ptr_),
4404 __cntrl_(__r.__cntrl_)
4405{
4406 if (__cntrl_)
4407 __cntrl_->__add_weak();
4408}
4409
4410template<class _Tp>
4411template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004412inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004414 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004415 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416 : __ptr_(__r.__ptr_),
4417 __cntrl_(__r.__cntrl_)
4418{
4419 if (__cntrl_)
4420 __cntrl_->__add_weak();
4421}
4422
4423template<class _Tp>
4424template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004425inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004426weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004427 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004428 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429 : __ptr_(__r.__ptr_),
4430 __cntrl_(__r.__cntrl_)
4431{
4432 if (__cntrl_)
4433 __cntrl_->__add_weak();
4434}
4435
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004436template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004437inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004438weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4439 : __ptr_(__r.__ptr_),
4440 __cntrl_(__r.__cntrl_)
4441{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004442 __r.__ptr_ = nullptr;
4443 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004444}
4445
4446template<class _Tp>
4447template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004448inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004449weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4450 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4451 _NOEXCEPT
4452 : __ptr_(__r.__ptr_),
4453 __cntrl_(__r.__cntrl_)
4454{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004455 __r.__ptr_ = nullptr;
4456 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004457}
4458
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459template<class _Tp>
4460weak_ptr<_Tp>::~weak_ptr()
4461{
4462 if (__cntrl_)
4463 __cntrl_->__release_weak();
4464}
4465
4466template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004467inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004468weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004469weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470{
4471 weak_ptr(__r).swap(*this);
4472 return *this;
4473}
4474
4475template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004476template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004477inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004478typename enable_if
4479<
4480 is_convertible<_Yp*, _Tp*>::value,
4481 weak_ptr<_Tp>&
4482>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004483weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484{
4485 weak_ptr(__r).swap(*this);
4486 return *this;
4487}
4488
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004489template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004490inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004491weak_ptr<_Tp>&
4492weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4493{
4494 weak_ptr(_VSTD::move(__r)).swap(*this);
4495 return *this;
4496}
4497
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004499template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004500inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004501typename enable_if
4502<
4503 is_convertible<_Yp*, _Tp*>::value,
4504 weak_ptr<_Tp>&
4505>::type
4506weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4507{
4508 weak_ptr(_VSTD::move(__r)).swap(*this);
4509 return *this;
4510}
4511
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004512template<class _Tp>
4513template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004514inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004515typename enable_if
4516<
4517 is_convertible<_Yp*, _Tp*>::value,
4518 weak_ptr<_Tp>&
4519>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004520weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521{
4522 weak_ptr(__r).swap(*this);
4523 return *this;
4524}
4525
4526template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004527inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528void
Howard Hinnant719bda32011-05-28 14:41:13 +00004529weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004531 _VSTD::swap(__ptr_, __r.__ptr_);
4532 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533}
4534
4535template<class _Tp>
4536inline _LIBCPP_INLINE_VISIBILITY
4537void
Howard Hinnant719bda32011-05-28 14:41:13 +00004538swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539{
4540 __x.swap(__y);
4541}
4542
4543template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004544inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004545void
Howard Hinnant719bda32011-05-28 14:41:13 +00004546weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004547{
4548 weak_ptr().swap(*this);
4549}
4550
4551template<class _Tp>
4552template<class _Yp>
4553shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004554 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004555 : __ptr_(__r.__ptr_),
4556 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4557{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004558 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00004559 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004560}
4561
4562template<class _Tp>
4563shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004564weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565{
4566 shared_ptr<_Tp> __r;
4567 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4568 if (__r.__cntrl_)
4569 __r.__ptr_ = __ptr_;
4570 return __r;
4571}
4572
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004573#if _LIBCPP_STD_VER > 14
4574template <class _Tp = void> struct owner_less;
4575#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004576template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004577#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578
4579template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004580struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004581 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004582{
4583 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004584 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004585 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004586 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004587 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004588 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004589 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004590 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004591 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004593};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004594
4595template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004596struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4598{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004599 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004600 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004601 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004602 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004603 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004604 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004605 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004606 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004607 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004608 {return __x.owner_before(__y);}
4609};
4610
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004611#if _LIBCPP_STD_VER > 14
4612template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004613struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004614{
4615 template <class _Tp, class _Up>
4616 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004617 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004618 {return __x.owner_before(__y);}
4619 template <class _Tp, class _Up>
4620 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004621 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004622 {return __x.owner_before(__y);}
4623 template <class _Tp, class _Up>
4624 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004625 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004626 {return __x.owner_before(__y);}
4627 template <class _Tp, class _Up>
4628 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004629 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004630 {return __x.owner_before(__y);}
4631 typedef void is_transparent;
4632};
4633#endif
4634
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004636class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004637{
4638 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004639protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004640 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004641 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004643 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004645 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4646 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004648 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004649public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004651 shared_ptr<_Tp> shared_from_this()
4652 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004654 shared_ptr<_Tp const> shared_from_this() const
4655 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004656
Eric Fiselier84006862016-06-02 00:15:35 +00004657#if _LIBCPP_STD_VER > 14
4658 _LIBCPP_INLINE_VISIBILITY
4659 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4660 { return __weak_this_; }
4661
4662 _LIBCPP_INLINE_VISIBILITY
4663 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4664 { return __weak_this_; }
4665#endif // _LIBCPP_STD_VER > 14
4666
Howard Hinnantc51e1022010-05-11 19:42:16 +00004667 template <class _Up> friend class shared_ptr;
4668};
4669
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004670template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004671struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004672{
4673 typedef shared_ptr<_Tp> argument_type;
4674 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00004675
Howard Hinnant756c69b2010-09-22 16:48:34 +00004676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004677 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004678 {
zoecarverd73f42a2020-05-11 18:42:50 -07004679 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004680 }
4681};
4682
Howard Hinnantc834c512011-11-29 18:15:50 +00004683template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00004684inline _LIBCPP_INLINE_VISIBILITY
4685basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00004686operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00004687
Eric Fiselier9b492672016-06-18 02:12:53 +00004688
4689#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004690
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004691class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00004692{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004693 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004694public:
4695 void lock() _NOEXCEPT;
4696 void unlock() _NOEXCEPT;
4697
4698private:
4699 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4700 __sp_mut(const __sp_mut&);
4701 __sp_mut& operator=(const __sp_mut&);
4702
Howard Hinnant8331b762013-03-06 23:30:19 +00004703 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004704};
4705
Mehdi Amini228053d2017-05-04 17:08:54 +00004706_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4707__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004708
4709template <class _Tp>
4710inline _LIBCPP_INLINE_VISIBILITY
4711bool
4712atomic_is_lock_free(const shared_ptr<_Tp>*)
4713{
4714 return false;
4715}
4716
4717template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004718_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004719shared_ptr<_Tp>
4720atomic_load(const shared_ptr<_Tp>* __p)
4721{
4722 __sp_mut& __m = __get_sp_mut(__p);
4723 __m.lock();
4724 shared_ptr<_Tp> __q = *__p;
4725 __m.unlock();
4726 return __q;
4727}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004728
Howard Hinnant9fa30202012-07-30 01:40:57 +00004729template <class _Tp>
4730inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004731_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004732shared_ptr<_Tp>
4733atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4734{
4735 return atomic_load(__p);
4736}
4737
4738template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004739_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004740void
4741atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4742{
4743 __sp_mut& __m = __get_sp_mut(__p);
4744 __m.lock();
4745 __p->swap(__r);
4746 __m.unlock();
4747}
4748
4749template <class _Tp>
4750inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004751_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004752void
4753atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4754{
4755 atomic_store(__p, __r);
4756}
4757
4758template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004759_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004760shared_ptr<_Tp>
4761atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4762{
4763 __sp_mut& __m = __get_sp_mut(__p);
4764 __m.lock();
4765 __p->swap(__r);
4766 __m.unlock();
4767 return __r;
4768}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004769
Howard Hinnant9fa30202012-07-30 01:40:57 +00004770template <class _Tp>
4771inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004772_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004773shared_ptr<_Tp>
4774atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4775{
4776 return atomic_exchange(__p, __r);
4777}
4778
4779template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004780_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004781bool
4782atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4783{
Marshall Clow4201ee82016-05-18 17:50:13 +00004784 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004785 __sp_mut& __m = __get_sp_mut(__p);
4786 __m.lock();
4787 if (__p->__owner_equivalent(*__v))
4788 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004789 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004790 *__p = __w;
4791 __m.unlock();
4792 return true;
4793 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004794 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004795 *__v = *__p;
4796 __m.unlock();
4797 return false;
4798}
4799
4800template <class _Tp>
4801inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004802_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004803bool
4804atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4805{
4806 return atomic_compare_exchange_strong(__p, __v, __w);
4807}
4808
4809template <class _Tp>
4810inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004811_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004812bool
4813atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4814 shared_ptr<_Tp> __w, memory_order, memory_order)
4815{
4816 return atomic_compare_exchange_strong(__p, __v, __w);
4817}
4818
4819template <class _Tp>
4820inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004821_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004822bool
4823atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4824 shared_ptr<_Tp> __w, memory_order, memory_order)
4825{
4826 return atomic_compare_exchange_weak(__p, __v, __w);
4827}
4828
Eric Fiselier9b492672016-06-18 02:12:53 +00004829#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004830
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004831//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004832#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4833# ifndef _LIBCPP_CXX03_LANG
4834enum class pointer_safety : unsigned char {
4835 relaxed,
4836 preferred,
4837 strict
4838};
4839# endif
4840#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004841struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004842{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004843 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004844 {
4845 relaxed,
4846 preferred,
4847 strict
4848 };
4849
Howard Hinnant49e145e2012-10-30 19:06:59 +00004850 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004851
Howard Hinnant756c69b2010-09-22 16:48:34 +00004852 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004853 pointer_safety() : __v_() {}
4854
4855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004856 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004858 operator int() const {return __v_;}
4859};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004860#endif
4861
4862#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004863 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004864_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4865#else
4866// This function is only offered in C++03 under ABI v1.
4867# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4868inline _LIBCPP_INLINE_VISIBILITY
4869pointer_safety get_pointer_safety() _NOEXCEPT {
4870 return pointer_safety::relaxed;
4871}
4872# endif
4873#endif
4874
Howard Hinnantc51e1022010-05-11 19:42:16 +00004875
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004876_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4877_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4878_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004879_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004880
4881template <class _Tp>
4882inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004883_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004884undeclare_reachable(_Tp* __p)
4885{
4886 return static_cast<_Tp*>(__undeclare_reachable(__p));
4887}
4888
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004889_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004890
Marshall Clow8982dcd2015-07-13 20:04:56 +00004891// --- Helper for container swap --
4892template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004893_LIBCPP_INLINE_VISIBILITY
4894void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4895#if _LIBCPP_STD_VER >= 14
4896 _NOEXCEPT
4897#else
4898 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4899#endif
4900{
4901 using _VSTD::swap;
4902 swap(__a1, __a2);
4903}
4904
4905template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004906inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004907void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4908
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004909template <typename _Alloc>
4910inline _LIBCPP_INLINE_VISIBILITY
4911void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4912#if _LIBCPP_STD_VER >= 14
4913 _NOEXCEPT
4914#else
4915 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4916#endif
4917{
4918 _VSTD::__swap_allocator(__a1, __a2,
4919 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4920}
4921
Marshall Clowff91de82015-08-18 19:51:37 +00004922template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004923struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004924 _Traits::propagate_on_container_move_assignment::value
4925#if _LIBCPP_STD_VER > 14
4926 || _Traits::is_always_equal::value
4927#else
4928 && is_nothrow_move_assignable<_Alloc>::value
4929#endif
4930 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004931
Marshall Clowa591b9a2016-07-11 21:38:08 +00004932
Marshall Clowa591b9a2016-07-11 21:38:08 +00004933template <class _Tp, class _Alloc>
4934struct __temp_value {
4935 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004936
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004937 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004938 _Alloc &__a;
4939
4940 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4941 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004942
Marshall Clowa591b9a2016-07-11 21:38:08 +00004943 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004944 _LIBCPP_NO_CFI
4945 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4946 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4947 _VSTD::forward<_Args>(__args)...);
4948 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004949
Marshall Clowa591b9a2016-07-11 21:38:08 +00004950 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4951 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004952
Marshall Clowe46031a2018-07-02 18:41:15 +00004953template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004954struct __is_allocator : false_type {};
4955
4956template<typename _Alloc>
4957struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004958 typename __void_t<typename _Alloc::value_type>::type,
4959 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4960 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004961 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004962
Eric Fiselier74ebee62019-06-08 01:31:19 +00004963// __builtin_new_allocator -- A non-templated helper for allocating and
4964// deallocating memory using __builtin_operator_new and
4965// __builtin_operator_delete. It should be used in preference to
4966// `std::allocator<T>` to avoid additional instantiations.
4967struct __builtin_new_allocator {
4968 struct __builtin_new_deleter {
4969 typedef void* pointer_type;
4970
4971 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4972 : __size_(__size), __align_(__align) {}
4973
4974 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004975 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004976 }
4977
4978 private:
4979 size_t __size_;
4980 size_t __align_;
4981 };
4982
4983 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4984
4985 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004986 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004987 __builtin_new_deleter(__s, __align));
4988 }
4989
4990 static void __deallocate_bytes(void* __p, size_t __s,
4991 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004992 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004993 }
4994
4995 template <class _Tp>
4996 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4997 static __holder_t __allocate_type(size_t __n) {
4998 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4999 }
5000
5001 template <class _Tp>
5002 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5003 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5004 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5005 }
5006};
5007
5008
Howard Hinnantc51e1022010-05-11 19:42:16 +00005009_LIBCPP_END_NAMESPACE_STD
5010
Eric Fiselierf4433a32017-05-31 22:07:49 +00005011_LIBCPP_POP_MACROS
5012
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005013#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005014# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005015#endif
5016
Howard Hinnantc51e1022010-05-11 19:42:16 +00005017#endif // _LIBCPP_MEMORY