blob: ebb0a723a162a40c7e656714e52103ff494b70c0 [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
Marshall Clow0e58cae2017-11-26 02:55:38 +000086 static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Howard Hinnant719bda32011-05-28 14:41:13 +000089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
92 static void construct(allocator_type& a, T* p, Args&&... args);
93
94 template <class T>
95 static void destroy(allocator_type& a, T* p);
96
Marshall Clow4f834b52013-08-27 20:22:15 +000097 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 static allocator_type
100 select_on_container_copy_construction(const allocator_type& a);
101};
102
103template <>
Michael Park99f9e912020-03-04 11:27:14 -0500104class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105{
106public:
107 typedef void* pointer;
108 typedef const void* const_pointer;
109 typedef void value_type;
110
111 template <class _Up> struct rebind {typedef allocator<_Up> other;};
112};
113
114template <class T>
115class allocator
116{
117public:
Louis Dionnec0056af2020-08-28 12:31:16 -0400118 typedef size_t size_type;
119 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -0500120 typedef T* pointer; // deprecated in C++17, removed in C++20
121 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<T>::type
123 reference; // deprecated in C++17, removed in C++20
124 typedef typename add_lvalue_reference<const T>::type
125 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Michael Park99f9e912020-03-04 11:27:14 -0500127 typedef T value_type;
128
129 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
130
131 typedef true_type propagate_on_container_move_assignment;
132 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
Marshall Clowbc759762018-03-20 23:02:53 +0000134 constexpr allocator() noexcept; // constexpr in C++20
135 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
136 template <class U>
137 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000138 ~allocator();
Michael Park99f9e912020-03-04 11:27:14 -0500139 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
140 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
141 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
142 T* allocate(size_t n);
143 void deallocate(T* p, size_t n) noexcept;
144 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000147 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500148 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149};
150
151template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000152bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153
154template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000155bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
157template <class OutputIterator, class T>
158class raw_storage_iterator
159 : public iterator<output_iterator_tag,
160 T, // purposefully not C++03
161 ptrdiff_t, // purposefully not C++03
162 T*, // purposefully not C++03
163 raw_storage_iterator&> // purposefully not C++03
164{
165public:
166 explicit raw_storage_iterator(OutputIterator x);
167 raw_storage_iterator& operator*();
168 raw_storage_iterator& operator=(const T& element);
169 raw_storage_iterator& operator++();
170 raw_storage_iterator operator++(int);
171};
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
174template <class T> void return_temporary_buffer(T* p) noexcept;
175
176template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000177template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
179template <class InputIterator, class ForwardIterator>
180ForwardIterator
181uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
182
Howard Hinnant719bda32011-05-28 14:41:13 +0000183template <class InputIterator, class Size, class ForwardIterator>
184ForwardIterator
185uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
186
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187template <class ForwardIterator, class T>
188void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
189
190template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000191ForwardIterator
192uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000194template <class T>
195void destroy_at(T* location);
196
197template <class ForwardIterator>
198 void destroy(ForwardIterator first, ForwardIterator last);
199
200template <class ForwardIterator, class Size>
201 ForwardIterator destroy_n(ForwardIterator first, Size n);
202
203template <class InputIterator, class ForwardIterator>
204 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
205
206template <class InputIterator, class Size, class ForwardIterator>
207 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
208
209template <class ForwardIterator>
210 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
211
212template <class ForwardIterator, class Size>
213 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
214
215template <class ForwardIterator>
216 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
217
218template <class ForwardIterator, class Size>
219 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
220
Louis Dionne481a2662018-09-23 18:35:00 +0000221template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222
223template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000224class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225{
226public:
227 typedef X element_type;
228
229 explicit auto_ptr(X* p =0) throw();
230 auto_ptr(auto_ptr&) throw();
231 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
232 auto_ptr& operator=(auto_ptr&) throw();
233 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
234 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
235 ~auto_ptr() throw();
236
237 typename add_lvalue_reference<X>::type operator*() const throw();
238 X* operator->() const throw();
239 X* get() const throw();
240 X* release() throw();
241 void reset(X* p =0) throw();
242
243 auto_ptr(auto_ptr_ref<X>) throw();
244 template<class Y> operator auto_ptr_ref<Y>() throw();
245 template<class Y> operator auto_ptr<Y>() throw();
246};
247
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248template <class T>
249struct default_delete
250{
Howard Hinnant719bda32011-05-28 14:41:13 +0000251 constexpr default_delete() noexcept = default;
252 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000253
Howard Hinnant719bda32011-05-28 14:41:13 +0000254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255};
256
257template <class T>
258struct default_delete<T[]>
259{
Howard Hinnant719bda32011-05-28 14:41:13 +0000260 constexpr default_delete() noexcept = default;
261 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000262 template <class U> void operator()(U*) const = delete;
263};
264
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000265template <class T, class D = default_delete<T>>
266class unique_ptr
267{
268public:
269 typedef see below pointer;
270 typedef T element_type;
271 typedef D deleter_type;
272
273 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 constexpr unique_ptr() noexcept;
275 explicit unique_ptr(pointer p) noexcept;
276 unique_ptr(pointer p, see below d1) noexcept;
277 unique_ptr(pointer p, see below d2) noexcept;
278 unique_ptr(unique_ptr&& u) noexcept;
279 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000280 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000281 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000283 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000284
285 // destructor
286 ~unique_ptr();
287
288 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000289 unique_ptr& operator=(unique_ptr&& u) noexcept;
290 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
291 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000292
293 // observers
294 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer operator->() const noexcept;
296 pointer get() const noexcept;
297 deleter_type& get_deleter() noexcept;
298 const deleter_type& get_deleter() const noexcept;
299 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000300
301 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000302 pointer release() noexcept;
303 void reset(pointer p = pointer()) noexcept;
304 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000305};
306
307template <class T, class D>
308class unique_ptr<T[], D>
309{
310public:
311 typedef implementation-defined pointer;
312 typedef T element_type;
313 typedef D deleter_type;
314
315 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000316 constexpr unique_ptr() noexcept;
317 explicit unique_ptr(pointer p) noexcept;
318 unique_ptr(pointer p, see below d) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(unique_ptr&& u) noexcept;
321 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000324 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000325
326 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000327 unique_ptr& operator=(unique_ptr&& u) noexcept;
328 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // observers
331 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 pointer get() const noexcept;
333 deleter_type& get_deleter() noexcept;
334 const deleter_type& get_deleter() const noexcept;
335 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336
337 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000338 pointer release() noexcept;
339 void reset(pointer p = pointer()) noexcept;
340 void reset(nullptr_t) noexcept;
Vy Nguyene369bd92020-07-13 12:34:37 -0400341 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000342 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000343};
344
345template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000346 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000347
348template <class T1, class D1, class T2, class D2>
349 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350template <class T1, class D1, class T2, class D2>
351 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
352template <class T1, class D1, class T2, class D2>
353 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
354template <class T1, class D1, class T2, class D2>
355 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
356template <class T1, class D1, class T2, class D2>
357 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
358template <class T1, class D1, class T2, class D2>
359 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
360
Howard Hinnant719bda32011-05-28 14:41:13 +0000361template <class T, class D>
362 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
363template <class T, class D>
364 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
365template <class T, class D>
366 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
367template <class T, class D>
368 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
369
370template <class T, class D>
371 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
372template <class T, class D>
373 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
374template <class T, class D>
375 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
376template <class T, class D>
377 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
378template <class T, class D>
379 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
380template <class T, class D>
381 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
382template <class T, class D>
383 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
384template <class T, class D>
385 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387class bad_weak_ptr
388 : public std::exception
389{
Howard Hinnant719bda32011-05-28 14:41:13 +0000390 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000391};
392
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000393template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
394template<class T> unique_ptr<T> make_unique(size_t n); // C++14
395template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
396
Marshall Clowe52a3242017-11-27 15:51:36 +0000397template<class E, class T, class Y, class D>
398 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
399
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000400template<class T>
401class shared_ptr
402{
403public:
404 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000405 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406
407 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000408 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000409 template<class Y> explicit shared_ptr(Y* p);
410 template<class Y, class D> shared_ptr(Y* p, D d);
411 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
412 template <class D> shared_ptr(nullptr_t p, D d);
413 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000414 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
415 shared_ptr(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
417 shared_ptr(shared_ptr&& r) noexcept;
418 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000419 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000420 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000421 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
422 shared_ptr(nullptr_t) : shared_ptr() { }
423
424 // destructor:
425 ~shared_ptr();
426
427 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000428 shared_ptr& operator=(const shared_ptr& r) noexcept;
429 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
430 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000431 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000432 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000433 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
434
435 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000436 void swap(shared_ptr& r) noexcept;
437 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438 template<class Y> void reset(Y* p);
439 template<class Y, class D> void reset(Y* p, D d);
440 template<class Y, class D, class A> void reset(Y* p, D d, A a);
441
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 // observers:
443 T* get() const noexcept;
444 T& operator*() const noexcept;
445 T* operator->() const noexcept;
446 long use_count() const noexcept;
447 bool unique() const noexcept;
448 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000449 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
450 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451};
452
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400453template<class T>
454shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
455template<class T, class D>
456shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
457
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458// shared_ptr comparisons:
459template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000460 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000461template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000462 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000463template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000464 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000465template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000466 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000467template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000468 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000469template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000470 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
471
472template <class T>
473 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
474template <class T>
475 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
476template <class T>
477 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
478template <class T>
479 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
480template <class T>
481 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
482template <class T>
483bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
484template <class T>
485 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
486template <class T>
487 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
488template <class T>
489 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
490template <class T>
491 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
492template <class T>
493 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
494template <class T>
495 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000498template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000499
500// shared_ptr casts:
501template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000502 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000503template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000504 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000505template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000506 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000507
508// shared_ptr I/O:
509template<class E, class T, class Y>
510 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
511
512// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000513template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000514
515template<class T, class... Args>
516 shared_ptr<T> make_shared(Args&&... args);
517template<class T, class A, class... Args>
518 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
519
520template<class T>
521class weak_ptr
522{
523public:
524 typedef T element_type;
525
526 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 constexpr weak_ptr() noexcept;
528 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
529 weak_ptr(weak_ptr const& r) noexcept;
530 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000531 weak_ptr(weak_ptr&& r) noexcept; // C++14
532 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000533
534 // destructor
535 ~weak_ptr();
536
537 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000538 weak_ptr& operator=(weak_ptr const& r) noexcept;
539 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
540 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000541 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
542 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000543
544 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000545 void swap(weak_ptr& r) noexcept;
546 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000547
548 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000549 long use_count() const noexcept;
550 bool expired() const noexcept;
551 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000552 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
553 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000554};
555
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400556template<class T>
557weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
558
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000559// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000560template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000561
562// class owner_less:
563template<class T> struct owner_less;
564
565template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000566struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000567 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
568{
569 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000570 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
571 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
572 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000573};
574
575template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000576struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000577 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
578{
579 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000580 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
581 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
583};
584
585template <> // Added in C++14
586struct owner_less<void>
587{
588 template <class _Tp, class _Up>
589 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
590 template <class _Tp, class _Up>
591 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
592 template <class _Tp, class _Up>
593 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
594 template <class _Tp, class _Up>
595 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
596
597 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000598};
599
600template<class T>
601class enable_shared_from_this
602{
603protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000604 constexpr enable_shared_from_this() noexcept;
605 enable_shared_from_this(enable_shared_from_this const&) noexcept;
606 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000607 ~enable_shared_from_this();
608public:
609 shared_ptr<T> shared_from_this();
610 shared_ptr<T const> shared_from_this() const;
611};
612
613template<class T>
614 bool atomic_is_lock_free(const shared_ptr<T>* p);
615template<class T>
616 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
617template<class T>
618 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
619template<class T>
620 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
621template<class T>
622 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
623template<class T>
624 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
625template<class T>
626 shared_ptr<T>
627 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
628template<class T>
629 bool
630 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
631template<class T>
632 bool
633 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
634template<class T>
635 bool
636 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
637 shared_ptr<T> w, memory_order success,
638 memory_order failure);
639template<class T>
640 bool
641 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
642 shared_ptr<T> w, memory_order success,
643 memory_order failure);
644// Hash support
645template <class T> struct hash;
646template <class T, class D> struct hash<unique_ptr<T, D> >;
647template <class T> struct hash<shared_ptr<T> >;
648
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000649template <class T, class Alloc>
650 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
651
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000652// Pointer safety
653enum class pointer_safety { relaxed, preferred, strict };
654void declare_reachable(void *p);
655template <class T> T *undeclare_reachable(T *p);
656void declare_no_pointers(char *p, size_t n);
657void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000658pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000659
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
661
662} // std
663
664*/
665
666#include <__config>
667#include <type_traits>
668#include <typeinfo>
669#include <cstddef>
670#include <cstdint>
671#include <new>
672#include <utility>
673#include <limits>
674#include <iterator>
675#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000676#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000677#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000678#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000679#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000680#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000681# include <atomic>
682#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000683#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000684
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000685#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000687#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688
Eric Fiselierf4433a32017-05-31 22:07:49 +0000689_LIBCPP_PUSH_MACROS
690#include <__undef_macros>
691
692
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693_LIBCPP_BEGIN_NAMESPACE_STD
694
Eric Fiselier89659d12015-07-07 00:27:16 +0000695template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000696inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000697_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
698#if !defined(_LIBCPP_HAS_NO_THREADS) && \
699 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000700 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000701 return __atomic_load_n(__value, __ATOMIC_RELAXED);
702#else
703 return *__value;
704#endif
705}
706
Kuba Breckade9d6792016-09-04 09:55:12 +0000707template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000708inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000709_ValueType __libcpp_acquire_load(_ValueType const* __value) {
710#if !defined(_LIBCPP_HAS_NO_THREADS) && \
711 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000712 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000713 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
714#else
715 return *__value;
716#endif
717}
718
Marshall Clow78dbe462016-11-14 18:22:19 +0000719// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000720
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721template <class _Tp> class allocator;
722
Michael Park99f9e912020-03-04 11:27:14 -0500723#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724template <>
Michael Park99f9e912020-03-04 11:27:14 -0500725class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726{
727public:
728 typedef void* pointer;
729 typedef const void* const_pointer;
730 typedef void value_type;
731
732 template <class _Up> struct rebind {typedef allocator<_Up> other;};
733};
734
Howard Hinnant9f771052012-01-19 23:15:22 +0000735template <>
Michael Park99f9e912020-03-04 11:27:14 -0500736class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000737{
738public:
739 typedef const void* pointer;
740 typedef const void* const_pointer;
741 typedef const void value_type;
742
743 template <class _Up> struct rebind {typedef allocator<_Up> other;};
744};
Michael Park99f9e912020-03-04 11:27:14 -0500745#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000746
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747// pointer_traits
748
Marshall Clow0be70c32017-06-14 21:23:57 +0000749template <class _Tp, class = void>
750struct __has_element_type : false_type {};
751
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000753struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000754 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755
756template <class _Ptr, bool = __has_element_type<_Ptr>::value>
757struct __pointer_traits_element_type;
758
759template <class _Ptr>
760struct __pointer_traits_element_type<_Ptr, true>
761{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000762 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763};
764
765#ifndef _LIBCPP_HAS_NO_VARIADICS
766
767template <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
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000779#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780
781template <template <class> class _Sp, class _Tp>
782struct __pointer_traits_element_type<_Sp<_Tp>, true>
783{
784 typedef typename _Sp<_Tp>::element_type type;
785};
786
787template <template <class> class _Sp, class _Tp>
788struct __pointer_traits_element_type<_Sp<_Tp>, false>
789{
790 typedef _Tp type;
791};
792
793template <template <class, class> class _Sp, class _Tp, class _A0>
794struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
795{
796 typedef typename _Sp<_Tp, _A0>::element_type type;
797};
798
799template <template <class, class> class _Sp, class _Tp, class _A0>
800struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
801{
802 typedef _Tp type;
803};
804
805template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
806struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
807{
808 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
809};
810
811template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
812struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
813{
814 typedef _Tp type;
815};
816
817template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
818 class _A1, class _A2>
819struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
820{
821 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
822};
823
824template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
825 class _A1, class _A2>
826struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
827{
828 typedef _Tp type;
829};
830
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000831#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832
Marshall Clow0be70c32017-06-14 21:23:57 +0000833template <class _Tp, class = void>
834struct __has_difference_type : false_type {};
835
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000837struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000838 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839
840template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
841struct __pointer_traits_difference_type
842{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000843 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844};
845
846template <class _Ptr>
847struct __pointer_traits_difference_type<_Ptr, true>
848{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000849 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850};
851
852template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500853struct __has_rebind
854{
855private:
856 struct __two {char __lx; char __lxx;};
857 template <class _Xp> static __two __test(...);
Louis Dionne95f24792020-03-04 14:38:51 -0500858 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Louis Dionnef1bb8492020-03-04 13:54:58 -0500859 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
Louis Dionne95f24792020-03-04 14:38:51 -0500860 _LIBCPP_SUPPRESS_DEPRECATED_POP
Louis Dionnef1bb8492020-03-04 13:54:58 -0500861public:
862 static const bool value = sizeof(__test<_Tp>(0)) == 1;
863};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864
865template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
866struct __pointer_traits_rebind
867{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000868#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000869 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000871 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872#endif
873};
874
875#ifndef _LIBCPP_HAS_NO_VARIADICS
876
877template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
878struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
879{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000880#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000881 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000883 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884#endif
885};
886
887template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
888struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
889{
890 typedef _Sp<_Up, _Args...> type;
891};
892
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000893#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894
895template <template <class> class _Sp, class _Tp, class _Up>
896struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
897{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000898#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 typedef typename _Sp<_Tp>::template rebind<_Up> type;
900#else
901 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
902#endif
903};
904
905template <template <class> class _Sp, class _Tp, class _Up>
906struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
907{
908 typedef _Sp<_Up> type;
909};
910
911template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
912struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
913{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000914#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
916#else
917 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
918#endif
919};
920
921template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
922struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
923{
924 typedef _Sp<_Up, _A0> type;
925};
926
927template <template <class, class, class> class _Sp, class _Tp, class _A0,
928 class _A1, class _Up>
929struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
930{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000931#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
933#else
934 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
935#endif
936};
937
938template <template <class, class, class> class _Sp, class _Tp, class _A0,
939 class _A1, class _Up>
940struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
941{
942 typedef _Sp<_Up, _A0, _A1> type;
943};
944
945template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
946 class _A1, class _A2, class _Up>
947struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
948{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000949#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
951#else
952 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
953#endif
954};
955
956template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
957 class _A1, class _A2, class _Up>
958struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
959{
960 typedef _Sp<_Up, _A0, _A1, _A2> type;
961};
962
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000963#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
965template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000966struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967{
968 typedef _Ptr pointer;
969 typedef typename __pointer_traits_element_type<pointer>::type element_type;
970 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
971
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000972#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000973 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974#else
975 template <class _Up> struct rebind
976 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000977#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978
979private:
980 struct __nat {};
981public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 static pointer pointer_to(typename conditional<is_void<element_type>::value,
984 __nat, element_type>::type& __r)
985 {return pointer::pointer_to(__r);}
986};
987
988template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000989struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990{
991 typedef _Tp* pointer;
992 typedef _Tp element_type;
993 typedef ptrdiff_t difference_type;
994
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000995#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 template <class _Up> using rebind = _Up*;
997#else
998 template <class _Up> struct rebind {typedef _Up* other;};
999#endif
1000
1001private:
1002 struct __nat {};
1003public:
Louis Dionne44e1ea82018-11-13 17:04:05 +00001004 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +00001006 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001007 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008};
1009
Eric Fiselierae3ab842015-08-23 02:56:05 +00001010template <class _From, class _To>
1011struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001012#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +00001013 typedef typename pointer_traits<_From>::template rebind<_To> type;
1014#else
1015 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
1016#endif
1017};
1018
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019// allocator_traits
1020
Marshall Clow0be70c32017-06-14 21:23:57 +00001021template <class _Tp, class = void>
1022struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023
1024template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001025struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001026 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028namespace __pointer_type_imp
1029{
1030
1031template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1032struct __pointer_type
1033{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001034 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035};
1036
1037template <class _Tp, class _Dp>
1038struct __pointer_type<_Tp, _Dp, false>
1039{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001040 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041};
1042
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001043} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044
1045template <class _Tp, class _Dp>
1046struct __pointer_type
1047{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001048 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 +00001049};
1050
Marshall Clow0be70c32017-06-14 21:23:57 +00001051template <class _Tp, class = void>
1052struct __has_const_pointer : false_type {};
1053
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001055struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001056 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057
1058template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1059struct __const_pointer
1060{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001061 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062};
1063
1064template <class _Tp, class _Ptr, class _Alloc>
1065struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1066{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001067#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001068 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069#else
1070 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1071#endif
1072};
1073
Marshall Clow0be70c32017-06-14 21:23:57 +00001074template <class _Tp, class = void>
1075struct __has_void_pointer : false_type {};
1076
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001078struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001079 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080
1081template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1082struct __void_pointer
1083{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001084 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085};
1086
1087template <class _Ptr, class _Alloc>
1088struct __void_pointer<_Ptr, _Alloc, false>
1089{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001090#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001091 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001093 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094#endif
1095};
1096
Marshall Clow0be70c32017-06-14 21:23:57 +00001097template <class _Tp, class = void>
1098struct __has_const_void_pointer : false_type {};
1099
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001101struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001102 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103
1104template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1105struct __const_void_pointer
1106{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001107 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108};
1109
1110template <class _Ptr, class _Alloc>
1111struct __const_void_pointer<_Ptr, _Alloc, false>
1112{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001113#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001114 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001116 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117#endif
1118};
1119
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001120
1121template <bool _UsePointerTraits> struct __to_address_helper;
1122
1123template <> struct __to_address_helper<true> {
1124 template <class _Pointer>
1125 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
1126
1127 template <class _Pointer>
1128 _LIBCPP_CONSTEXPR
1129 static __return_type<_Pointer>
1130 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1131};
1132
1133template <class _Pointer, bool _Dummy = true>
1134using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1135
1136
Howard Hinnantc834c512011-11-29 18:15:50 +00001137template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001138inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001139_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001140__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001142 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 return __p;
1144}
1145
1146template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001147inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1148typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1149__to_address(const _Pointer& __p) _NOEXCEPT {
1150 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001151}
1152
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001153template <> struct __to_address_helper<false> {
1154 template <class _Pointer>
1155 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001156
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001157 template <class _Pointer>
1158 _LIBCPP_CONSTEXPR
1159 static __return_type<_Pointer>
1160 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1161};
1162
1163
1164#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001165template <class _Tp>
1166inline _LIBCPP_INLINE_VISIBILITY constexpr
1167_Tp*
1168to_address(_Tp* __p) _NOEXCEPT
1169{
1170 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1171 return __p;
1172}
1173
1174template <class _Pointer>
1175inline _LIBCPP_INLINE_VISIBILITY
1176auto
1177to_address(const _Pointer& __p) _NOEXCEPT
1178{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001179 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001180}
1181#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182
Marshall Clow0be70c32017-06-14 21:23:57 +00001183template <class _Tp, class = void>
1184struct __has_size_type : false_type {};
1185
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001187struct __has_size_type<_Tp,
1188 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001190template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191struct __size_type
1192{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001193 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194};
1195
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001196template <class _Alloc, class _DiffType>
1197struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001199 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200};
1201
Marshall Clow0be70c32017-06-14 21:23:57 +00001202template <class _Tp, class = void>
1203struct __has_propagate_on_container_copy_assignment : false_type {};
1204
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001206struct __has_propagate_on_container_copy_assignment<_Tp,
1207 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1208 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209
1210template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1211struct __propagate_on_container_copy_assignment
1212{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001213 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214};
1215
1216template <class _Alloc>
1217struct __propagate_on_container_copy_assignment<_Alloc, true>
1218{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001219 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220};
1221
Marshall Clow0be70c32017-06-14 21:23:57 +00001222template <class _Tp, class = void>
1223struct __has_propagate_on_container_move_assignment : false_type {};
1224
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001226struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001227 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1228 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
1230template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1231struct __propagate_on_container_move_assignment
1232{
1233 typedef false_type type;
1234};
1235
1236template <class _Alloc>
1237struct __propagate_on_container_move_assignment<_Alloc, true>
1238{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001239 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240};
1241
Marshall Clow0be70c32017-06-14 21:23:57 +00001242template <class _Tp, class = void>
1243struct __has_propagate_on_container_swap : false_type {};
1244
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001246struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001247 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1248 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249
1250template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1251struct __propagate_on_container_swap
1252{
1253 typedef false_type type;
1254};
1255
1256template <class _Alloc>
1257struct __propagate_on_container_swap<_Alloc, true>
1258{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001259 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260};
1261
Marshall Clow0be70c32017-06-14 21:23:57 +00001262template <class _Tp, class = void>
1263struct __has_is_always_equal : false_type {};
1264
Marshall Clow0b587562015-06-02 16:34:03 +00001265template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001266struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001267 typename __void_t<typename _Tp::is_always_equal>::type>
1268 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001269
1270template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1271struct __is_always_equal
1272{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001273 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001274};
1275
1276template <class _Alloc>
1277struct __is_always_equal<_Alloc, true>
1278{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001279 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001280};
1281
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1283struct __has_rebind_other
1284{
1285private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001286 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001288 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001290 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291public:
1292 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1293};
1294
1295template <class _Tp, class _Up>
1296struct __has_rebind_other<_Tp, _Up, false>
1297{
1298 static const bool value = false;
1299};
1300
1301template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1302struct __allocator_traits_rebind
1303{
Michael Park99f9e912020-03-04 11:27:14 -05001304 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001305 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001306 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307};
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1310struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1311{
Michael Park99f9e912020-03-04 11:27:14 -05001312 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001313 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001314 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315};
1316
1317template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1319{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001320 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321};
1322
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001323#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324
Michael Park99f9e912020-03-04 11:27:14 -05001325_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1327auto
1328__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001329 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001330_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
1332template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1333auto
1334__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1335 -> false_type;
1336
1337template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1338struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001339 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1340 declval<_SizeType>(),
1341 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342{
1343};
1344
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001345#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
1347template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1348struct __has_allocate_hint
1349 : true_type
1350{
1351};
1352
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001353#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354
zoecarver75816d42020-05-23 14:32:12 -07001355_LIBCPP_SUPPRESS_DEPRECATED_PUSH
zoecarver20590dd2020-05-23 14:03:04 -07001356template <class _Alloc, class ..._Args,
1357 class = decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Args>()...))>
1358static true_type __test_has_construct(int);
zoecarver75816d42020-05-23 14:32:12 -07001359_LIBCPP_SUPPRESS_DEPRECATED_POP
1360
zoecarver20590dd2020-05-23 14:03:04 -07001361template <class _Alloc, class...>
1362static false_type __test_has_construct(...);
1363
1364template <class _Alloc, class ..._Args>
1365struct __has_construct : decltype(__test_has_construct<_Alloc, _Args...>(0)) {};
1366
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001367#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368
Michael Park99f9e912020-03-04 11:27:14 -05001369_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370template <class _Alloc, class _Pointer>
1371auto
1372__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1373 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001374_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375
1376template <class _Alloc, class _Pointer>
1377auto
1378__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1379 -> false_type;
1380
1381template <class _Alloc, class _Pointer>
1382struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001383 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1384 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385{
1386};
1387
Michael Park99f9e912020-03-04 11:27:14 -05001388_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389template <class _Alloc>
1390auto
1391__has_max_size_test(_Alloc&& __a)
1392 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001393_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394
1395template <class _Alloc>
1396auto
1397__has_max_size_test(const volatile _Alloc& __a)
1398 -> false_type;
1399
1400template <class _Alloc>
1401struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001402 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403{
1404};
1405
1406template <class _Alloc>
1407auto
1408__has_select_on_container_copy_construction_test(_Alloc&& __a)
1409 -> decltype(__a.select_on_container_copy_construction(), true_type());
1410
1411template <class _Alloc>
1412auto
1413__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1414 -> false_type;
1415
1416template <class _Alloc>
1417struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001418 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419{
1420};
1421
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001422#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001424template <class _Alloc, class _Pointer, class = void>
1425struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426
1427template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001428struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1429 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1430>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431
1432template <class _Alloc>
1433struct __has_max_size
1434 : true_type
1435{
1436};
1437
1438template <class _Alloc>
1439struct __has_select_on_container_copy_construction
1440 : false_type
1441{
1442};
1443
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001444#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001446template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1447struct __alloc_traits_difference_type
1448{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001449 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001450};
1451
1452template <class _Alloc, class _Ptr>
1453struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1454{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001455 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001456};
1457
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001458template <class _Tp>
1459struct __is_default_allocator : false_type {};
1460
1461template <class _Tp>
1462struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1463
Eric Fiselier909fe962019-09-13 16:09:33 +00001464
1465
1466template <class _Alloc,
1467 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1468 >
1469struct __is_cpp17_move_insertable;
1470template <class _Alloc>
1471struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1472template <class _Alloc>
1473struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1474
1475template <class _Alloc,
1476 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1477 >
1478struct __is_cpp17_copy_insertable;
1479template <class _Alloc>
1480struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1481template <class _Alloc>
1482struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1483 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1484 __is_cpp17_move_insertable<_Alloc>::value>
1485 {};
1486
1487
1488
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001490struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491{
1492 typedef _Alloc allocator_type;
1493 typedef typename allocator_type::value_type value_type;
1494
1495 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1496 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1497 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1498 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1499
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001500 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1501 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502
1503 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1504 propagate_on_container_copy_assignment;
1505 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1506 propagate_on_container_move_assignment;
1507 typedef typename __propagate_on_container_swap<allocator_type>::type
1508 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001509 typedef typename __is_always_equal<allocator_type>::type
1510 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001512#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001514 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001515 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001516#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 template <class _Tp> struct rebind_alloc
1518 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1519 template <class _Tp> struct rebind_traits
1520 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001521#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522
Marshall Clow0e58cae2017-11-26 02:55:38 +00001523 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524 static pointer allocate(allocator_type& __a, size_type __n)
1525 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001526 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001528 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1530
Howard Hinnant756c69b2010-09-22 16:48:34 +00001531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001532 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 {__a.deallocate(__p, __n);}
1534
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001538 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001539 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540
1541 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static void destroy(allocator_type& __a, _Tp* __p)
1544 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1545
Howard Hinnant756c69b2010-09-22 16:48:34 +00001546 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001547 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1549
Howard Hinnant756c69b2010-09-22 16:48:34 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 static allocator_type
1552 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001553 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 __has_select_on_container_copy_construction<const allocator_type>(),
1555 __a);}
1556
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001557 template <class _Ptr>
1558 _LIBCPP_INLINE_VISIBILITY
1559 static
1560 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001561 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001562 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001563 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1564 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001565 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001566 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001567#ifdef _LIBCPP_NO_EXCEPTIONS
1568 _VSTD::move(*__begin1)
1569#else
1570 _VSTD::move_if_noexcept(*__begin1)
1571#endif
1572 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001573 }
1574
1575 template <class _Tp>
1576 _LIBCPP_INLINE_VISIBILITY
1577 static
1578 typename enable_if
1579 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001580 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001581 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1582 is_trivially_move_constructible<_Tp>::value,
1583 void
1584 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001585 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001586 {
1587 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001588 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001589 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001590 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001591 __begin2 += _Np;
1592 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001593 }
1594
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001595 template <class _Iter, class _Ptr>
1596 _LIBCPP_INLINE_VISIBILITY
1597 static
1598 void
1599 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1600 {
1601 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001602 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001603 }
1604
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001605 template <class _SourceTp, class _DestTp,
1606 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1607 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001608 _LIBCPP_INLINE_VISIBILITY
1609 static
1610 typename enable_if
1611 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001612 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001613 is_same<_RawSourceTp, _RawDestTp>::value &&
1614 (__is_default_allocator<allocator_type>::value ||
1615 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001616 void
1617 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001618 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001619 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001620 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001621 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001622 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001623 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001624 __begin2 += _Np;
1625 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001626 }
1627
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001628 template <class _Ptr>
1629 _LIBCPP_INLINE_VISIBILITY
1630 static
1631 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001632 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001633 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001634 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1635 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001636 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001637 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001638 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001639#ifdef _LIBCPP_NO_EXCEPTIONS
1640 _VSTD::move(*--__end1)
1641#else
1642 _VSTD::move_if_noexcept(*--__end1)
1643#endif
1644 );
1645 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001646 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001647 }
1648
1649 template <class _Tp>
1650 _LIBCPP_INLINE_VISIBILITY
1651 static
1652 typename enable_if
1653 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001654 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001655 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1656 is_trivially_move_constructible<_Tp>::value,
1657 void
1658 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001659 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001660 {
1661 ptrdiff_t _Np = __end1 - __begin1;
1662 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001663 if (_Np > 0)
1664 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001665 }
1666
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667private:
1668
Howard Hinnant756c69b2010-09-22 16:48:34 +00001669 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001670 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001671 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001672 {
1673 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1674 return __a.allocate(__n, __hint);
1675 _LIBCPP_SUPPRESS_DEPRECATED_POP
1676 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001677 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001678 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001679 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 {return __a.allocate(__n);}
1681
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001685 {
1686 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1687 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1688 _LIBCPP_SUPPRESS_DEPRECATED_POP
1689 }
1690
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1694 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001695 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697
1698 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001701 {
1702 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1703 __a.destroy(__p);
1704 _LIBCPP_SUPPRESS_DEPRECATED_POP
1705 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 static void __destroy(false_type, allocator_type&, _Tp* __p)
1709 {
1710 __p->~_Tp();
1711 }
1712
Howard Hinnant756c69b2010-09-22 16:48:34 +00001713 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001714 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001715 {
1716 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1717 return __a.max_size();
1718 _LIBCPP_SUPPRESS_DEPRECATED_POP
1719 }
1720
Howard Hinnant756c69b2010-09-22 16:48:34 +00001721 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001722 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001723 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724
Howard Hinnant756c69b2010-09-22 16:48:34 +00001725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001727 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001731 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732 {return __a;}
1733};
1734
Marshall Clow940e01c2015-04-07 05:21:38 +00001735template <class _Traits, class _Tp>
1736struct __rebind_alloc_helper
1737{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001738#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001739 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001740#else
1741 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1742#endif
1743};
1744
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745// allocator
1746
1747template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001748class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749{
1750public:
Louis Dionnec0056af2020-08-28 12:31:16 -04001751 typedef size_t size_type;
1752 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -05001753#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -05001754 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1755 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1756 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1757 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1758
1759 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1760#endif
1761
1762 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763
Howard Hinnant4931e092011-06-02 21:38:57 +00001764 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001765 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001766
Marshall Clowbc759762018-03-20 23:02:53 +00001767 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1768 allocator() _NOEXCEPT {}
1769
Louis Dionne481a2662018-09-23 18:35:00 +00001770 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001771 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1772 allocator(const allocator<_Up>&) _NOEXCEPT {}
1773
Michael Park99f9e912020-03-04 11:27:14 -05001774#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1775 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1776 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001777 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001778 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1779 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001781#endif
1782
1783 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001784 {
Michael Park99f9e912020-03-04 11:27:14 -05001785 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1786 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001787 __throw_length_error("allocator<T>::allocate(size_t n)"
1788 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001789 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001790 }
Michael Park99f9e912020-03-04 11:27:14 -05001791
1792#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1793 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1794 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1795#endif
1796
1797 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001798 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001799
1800#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1801 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001802 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001803
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001805 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 void
1807 construct(_Up* __p, _Args&&... __args)
1808 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001809 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810 }
Michael Park99f9e912020-03-04 11:27:14 -05001811 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1812#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813};
1814
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001815template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001816class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001817{
1818public:
Louis Dionnec0056af2020-08-28 12:31:16 -04001819 typedef size_t size_type;
1820 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -05001821#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -05001822 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1823 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1824 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1825 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1826
1827 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1828#endif
1829
1830 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001831
1832 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001833 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001834
Marshall Clowbc759762018-03-20 23:02:53 +00001835 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1836 allocator() _NOEXCEPT {}
1837
1838 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001839 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001840 allocator(const allocator<_Up>&) _NOEXCEPT {}
1841
Michael Park99f9e912020-03-04 11:27:14 -05001842#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1843 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1844 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001845 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001846#endif
1847
1848 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001849 {
Michael Park99f9e912020-03-04 11:27:14 -05001850 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1851 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001852 __throw_length_error("allocator<const T>::allocate(size_t n)"
1853 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001854 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001855 }
Michael Park99f9e912020-03-04 11:27:14 -05001856
1857#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1858 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1859 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1860#endif
1861
1862 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001863 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001864
1865#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1866 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001867 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001868
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001869 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001870 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001871 void
1872 construct(_Up* __p, _Args&&... __args)
1873 {
1874 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1875 }
Howard Hinnant9e028f12012-05-01 15:37:54 +00001876
Michael Park99f9e912020-03-04 11:27:14 -05001877 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1878#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001879};
1880
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881template <class _Tp, class _Up>
1882inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001883bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884
1885template <class _Tp, class _Up>
1886inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001887bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888
1889template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001890class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891 : public iterator<output_iterator_tag,
1892 _Tp, // purposefully not C++03
1893 ptrdiff_t, // purposefully not C++03
1894 _Tp*, // purposefully not C++03
1895 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1896{
1897private:
1898 _OutputIterator __x_;
1899public:
1900 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1901 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1902 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001903 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001904#if _LIBCPP_STD_VER >= 14
1905 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001906 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1909 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1910 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001911#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001912 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001913#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914};
1915
1916template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001917_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001919get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920{
1921 pair<_Tp*, ptrdiff_t> __r(0, 0);
1922 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1923 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1924 / sizeof(_Tp);
1925 if (__n > __m)
1926 __n = __m;
1927 while (__n > 0)
1928 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001929#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001930 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001931 {
1932 std::align_val_t __al =
1933 std::align_val_t(std::alignment_of<_Tp>::value);
1934 __r.first = static_cast<_Tp*>(::operator new(
1935 __n * sizeof(_Tp), __al, nothrow));
1936 } else {
1937 __r.first = static_cast<_Tp*>(::operator new(
1938 __n * sizeof(_Tp), nothrow));
1939 }
1940#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001941 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001942 {
1943 // Since aligned operator new is unavailable, return an empty
1944 // buffer rather than one with invalid alignment.
1945 return __r;
1946 }
1947
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001949#endif
1950
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951 if (__r.first)
1952 {
1953 __r.second = __n;
1954 break;
1955 }
1956 __n /= 2;
1957 }
1958 return __r;
1959}
1960
1961template <class _Tp>
1962inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001963void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1964{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001965 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001966}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967
Marshall Clowb22274f2017-01-24 22:22:33 +00001968#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001970struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971{
1972 _Tp* __ptr_;
1973};
1974
1975template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001976class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977{
1978private:
1979 _Tp* __ptr_;
1980public:
1981 typedef _Tp element_type;
1982
Dimitry Andric47269ce2020-03-13 19:36:26 +01001983 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1984 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1985 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001987 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001989 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001991 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001993 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994
Dimitry Andric47269ce2020-03-13 19:36:26 +01001995 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001997 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1998 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1999 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 {
2001 _Tp* __t = __ptr_;
2002 __ptr_ = 0;
2003 return __t;
2004 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01002005 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006 {
2007 if (__ptr_ != __p)
2008 delete __ptr_;
2009 __ptr_ = __p;
2010 }
2011
Dimitry Andric47269ce2020-03-13 19:36:26 +01002012 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
2013 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002015 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 {return auto_ptr<_Up>(release());}
2017};
2018
2019template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002020class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021{
2022public:
2023 typedef void element_type;
2024};
Marshall Clowb22274f2017-01-24 22:22:33 +00002025#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002027// Tag used to default initialize one or both of the pair's elements.
2028struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002029struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002030
Eric Fiselier9d355982017-04-12 23:45:53 +00002031template <class _Tp, int _Idx,
2032 bool _CanBeEmptyBase =
2033 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2034struct __compressed_pair_elem {
2035 typedef _Tp _ParamT;
2036 typedef _Tp& reference;
2037 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002039 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002040 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002041 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2042 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043
Eric Fiselier9d355982017-04-12 23:45:53 +00002044 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002045 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2046 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002047 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002048 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002049 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002050 : __value_(_VSTD::forward<_Up>(__u))
2051 {
2052 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002054
2055#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002056 template <class... _Args, size_t... _Indexes>
2057 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2058 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2059 __tuple_indices<_Indexes...>)
2060 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002061#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002063
Alex Lorenz76132112017-11-09 17:54:49 +00002064 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2065 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002066 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002069 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070};
2071
Eric Fiselier9d355982017-04-12 23:45:53 +00002072template <class _Tp, int _Idx>
2073struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2074 typedef _Tp _ParamT;
2075 typedef _Tp& reference;
2076 typedef const _Tp& const_reference;
2077 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002079 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2080 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2081 __compressed_pair_elem(__default_init_tag) {}
2082 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2083 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084
Eric Fiselier9d355982017-04-12 23:45:53 +00002085 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002086 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2087 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002088 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002089 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002090 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002091 : __value_type(_VSTD::forward<_Up>(__u))
2092 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002094#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002095 template <class... _Args, size_t... _Indexes>
2096 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2097 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2098 __tuple_indices<_Indexes...>)
2099 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002100#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002101
Alex Lorenz76132112017-11-09 17:54:49 +00002102 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2103 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002104 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105};
2106
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002108class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2109 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002110 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2111 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002112
2113 // NOTE: This static assert should never fire because __compressed_pair
2114 // is *almost never* used in a scenario where it's possible for T1 == T2.
2115 // (The exception is std::function where it is possible that the function
2116 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002117 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002118 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2119 "The current implementation is NOT ABI-compatible with the previous "
2120 "implementation for this configuration");
2121
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002123 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002124 class = typename enable_if<
2125 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2126 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2127 >::type
2128 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002129 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002130 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131
Eric Fiselier9d355982017-04-12 23:45:53 +00002132 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002133 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002134 __compressed_pair(_U1&& __t1, _U2&& __t2)
2135 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002137#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002138 template <class... _Args1, class... _Args2>
2139 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2140 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2141 tuple<_Args2...> __second_args)
2142 : _Base1(__pc, _VSTD::move(__first_args),
2143 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2144 _Base2(__pc, _VSTD::move(__second_args),
2145 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002146#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147
Eric Fiselier9d355982017-04-12 23:45:53 +00002148 _LIBCPP_INLINE_VISIBILITY
2149 typename _Base1::reference first() _NOEXCEPT {
2150 return static_cast<_Base1&>(*this).__get();
2151 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152
Eric Fiselier9d355982017-04-12 23:45:53 +00002153 _LIBCPP_INLINE_VISIBILITY
2154 typename _Base1::const_reference first() const _NOEXCEPT {
2155 return static_cast<_Base1 const&>(*this).__get();
2156 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157
Eric Fiselier9d355982017-04-12 23:45:53 +00002158 _LIBCPP_INLINE_VISIBILITY
2159 typename _Base2::reference second() _NOEXCEPT {
2160 return static_cast<_Base2&>(*this).__get();
2161 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162
Eric Fiselier9d355982017-04-12 23:45:53 +00002163 _LIBCPP_INLINE_VISIBILITY
2164 typename _Base2::const_reference second() const _NOEXCEPT {
2165 return static_cast<_Base2 const&>(*this).__get();
2166 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167
Eric Fiselier9d355982017-04-12 23:45:53 +00002168 _LIBCPP_INLINE_VISIBILITY
2169 void swap(__compressed_pair& __x)
2170 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2171 __is_nothrow_swappable<_T2>::value)
2172 {
2173 using std::swap;
2174 swap(first(), __x.first());
2175 swap(second(), __x.second());
2176 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177};
2178
2179template <class _T1, class _T2>
2180inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002181void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2182 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2183 __is_nothrow_swappable<_T2>::value) {
2184 __x.swap(__y);
2185}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002187// default_delete
2188
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002190struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002191 static_assert(!is_function<_Tp>::value,
2192 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002193#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002194 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002195#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002196 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002197#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002198 template <class _Up>
2199 _LIBCPP_INLINE_VISIBILITY
2200 default_delete(const default_delete<_Up>&,
2201 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2202 0) _NOEXCEPT {}
2203
2204 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2205 static_assert(sizeof(_Tp) > 0,
2206 "default_delete can not delete incomplete type");
2207 static_assert(!is_void<_Tp>::value,
2208 "default_delete can not delete incomplete type");
2209 delete __ptr;
2210 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211};
2212
2213template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002214struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2215private:
2216 template <class _Up>
2217 struct _EnableIfConvertible
2218 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2219
Howard Hinnant4500ca52011-12-18 21:19:44 +00002220public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002221#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002222 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002223#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002224 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002225#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002226
2227 template <class _Up>
2228 _LIBCPP_INLINE_VISIBILITY
2229 default_delete(const default_delete<_Up[]>&,
2230 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2231
2232 template <class _Up>
2233 _LIBCPP_INLINE_VISIBILITY
2234 typename _EnableIfConvertible<_Up>::type
2235 operator()(_Up* __ptr) const _NOEXCEPT {
2236 static_assert(sizeof(_Tp) > 0,
2237 "default_delete can not delete incomplete type");
2238 static_assert(!is_void<_Tp>::value,
2239 "default_delete can not delete void type");
2240 delete[] __ptr;
2241 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242};
2243
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002244template <class _Deleter>
2245struct __unique_ptr_deleter_sfinae {
2246 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2247 typedef const _Deleter& __lval_ref_type;
2248 typedef _Deleter&& __good_rval_ref_type;
2249 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250};
2251
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002252template <class _Deleter>
2253struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2254 typedef const _Deleter& __lval_ref_type;
2255 typedef const _Deleter&& __bad_rval_ref_type;
2256 typedef false_type __enable_rval_overload;
2257};
2258
2259template <class _Deleter>
2260struct __unique_ptr_deleter_sfinae<_Deleter&> {
2261 typedef _Deleter& __lval_ref_type;
2262 typedef _Deleter&& __bad_rval_ref_type;
2263 typedef false_type __enable_rval_overload;
2264};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002265
Vy Nguyene369bd92020-07-13 12:34:37 -04002266#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
2267# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2268#else
2269# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
2270#endif
2271
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002272template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04002273class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002274public:
2275 typedef _Tp element_type;
2276 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002277 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002278
2279 static_assert(!is_rvalue_reference<deleter_type>::value,
2280 "the specified deleter type cannot be an rvalue reference");
2281
2282private:
2283 __compressed_pair<pointer, deleter_type> __ptr_;
2284
2285 struct __nat { int __for_bool_; };
2286
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002287 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002288
2289 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002290 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002291 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002292
2293 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002294 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002295 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002296
2297 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002298 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002299 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002300
2301 template <bool _Dummy, class _Deleter = typename __dependent_type<
2302 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002303 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002304 typename enable_if<is_default_constructible<_Deleter>::value &&
2305 !is_pointer<_Deleter>::value>::type;
2306
2307 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002308 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002309 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2310
2311 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002312 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002313 is_convertible<typename _UPtr::pointer, pointer>::value &&
2314 !is_array<_Up>::value
2315 >::type;
2316
2317 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002318 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002319 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2320 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2321 >::type;
2322
2323 template <class _UDel>
2324 using _EnableIfDeleterAssignable = typename enable_if<
2325 is_assignable<_Dp&, _UDel&&>::value
2326 >::type;
2327
2328public:
2329 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002330 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002331 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002332 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002333
2334 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002335 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002336 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002337 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002338
2339 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002340 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002341 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002342 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002343
2344 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002345 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002346 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002347 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002348 : __ptr_(__p, __d) {}
2349
2350 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002351 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002352 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002353 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002354 : __ptr_(__p, _VSTD::move(__d)) {
2355 static_assert(!is_reference<deleter_type>::value,
2356 "rvalue deleter bound to reference");
2357 }
2358
2359 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002360 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002361 _LIBCPP_INLINE_VISIBILITY
2362 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2363
2364 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002365 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002366 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2367 }
2368
2369 template <class _Up, class _Ep,
2370 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2371 class = _EnableIfDeleterConvertible<_Ep>
2372 >
2373 _LIBCPP_INLINE_VISIBILITY
2374 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2375 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2376
2377#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2378 template <class _Up>
2379 _LIBCPP_INLINE_VISIBILITY
2380 unique_ptr(auto_ptr<_Up>&& __p,
2381 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002382 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002383 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002384 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002385#endif
2386
2387 _LIBCPP_INLINE_VISIBILITY
2388 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2389 reset(__u.release());
2390 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2391 return *this;
2392 }
2393
2394 template <class _Up, class _Ep,
2395 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2396 class = _EnableIfDeleterAssignable<_Ep>
2397 >
2398 _LIBCPP_INLINE_VISIBILITY
2399 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2400 reset(__u.release());
2401 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2402 return *this;
2403 }
2404
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002405#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2406 template <class _Up>
2407 _LIBCPP_INLINE_VISIBILITY
2408 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2409 is_same<_Dp, default_delete<_Tp> >::value,
2410 unique_ptr&>::type
2411 operator=(auto_ptr<_Up> __p) {
2412 reset(__p.release());
2413 return *this;
2414 }
2415#endif
2416
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002417#ifdef _LIBCPP_CXX03_LANG
2418 unique_ptr(unique_ptr const&) = delete;
2419 unique_ptr& operator=(unique_ptr const&) = delete;
2420#endif
2421
2422
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002423 _LIBCPP_INLINE_VISIBILITY
2424 ~unique_ptr() { reset(); }
2425
2426 _LIBCPP_INLINE_VISIBILITY
2427 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2428 reset();
2429 return *this;
2430 }
2431
2432 _LIBCPP_INLINE_VISIBILITY
2433 typename add_lvalue_reference<_Tp>::type
2434 operator*() const {
2435 return *__ptr_.first();
2436 }
2437 _LIBCPP_INLINE_VISIBILITY
2438 pointer operator->() const _NOEXCEPT {
2439 return __ptr_.first();
2440 }
2441 _LIBCPP_INLINE_VISIBILITY
2442 pointer get() const _NOEXCEPT {
2443 return __ptr_.first();
2444 }
2445 _LIBCPP_INLINE_VISIBILITY
2446 deleter_type& get_deleter() _NOEXCEPT {
2447 return __ptr_.second();
2448 }
2449 _LIBCPP_INLINE_VISIBILITY
2450 const deleter_type& get_deleter() const _NOEXCEPT {
2451 return __ptr_.second();
2452 }
2453 _LIBCPP_INLINE_VISIBILITY
2454 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2455 return __ptr_.first() != nullptr;
2456 }
2457
2458 _LIBCPP_INLINE_VISIBILITY
2459 pointer release() _NOEXCEPT {
2460 pointer __t = __ptr_.first();
2461 __ptr_.first() = pointer();
2462 return __t;
2463 }
2464
2465 _LIBCPP_INLINE_VISIBILITY
2466 void reset(pointer __p = pointer()) _NOEXCEPT {
2467 pointer __tmp = __ptr_.first();
2468 __ptr_.first() = __p;
2469 if (__tmp)
2470 __ptr_.second()(__tmp);
2471 }
2472
2473 _LIBCPP_INLINE_VISIBILITY
2474 void swap(unique_ptr& __u) _NOEXCEPT {
2475 __ptr_.swap(__u.__ptr_);
2476 }
2477};
2478
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002479
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002481class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002483 typedef _Tp element_type;
2484 typedef _Dp deleter_type;
2485 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2486
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002488 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489
Eric Fiselier31127cd2017-04-16 02:14:31 +00002490 template <class _From>
2491 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492
Eric Fiselier31127cd2017-04-16 02:14:31 +00002493 template <class _FromElem>
2494 struct _CheckArrayPointerConversion<_FromElem*>
2495 : integral_constant<bool,
2496 is_same<_FromElem*, pointer>::value ||
2497 (is_same<pointer, element_type*>::value &&
2498 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2499 >
2500 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501
Eric Fiselier31127cd2017-04-16 02:14:31 +00002502 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002503
2504 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002505 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002506 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002507
2508 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002509 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002510 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002511
2512 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002513 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002514 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002515
2516 template <bool _Dummy, class _Deleter = typename __dependent_type<
2517 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002518 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002519 typename enable_if<is_default_constructible<_Deleter>::value &&
2520 !is_pointer<_Deleter>::value>::type;
2521
2522 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002523 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002524 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2525
2526 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002527 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002528 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002529 >::type;
2530
2531 template <class _UPtr, class _Up,
2532 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002533 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002534 is_array<_Up>::value &&
2535 is_same<pointer, element_type*>::value &&
2536 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2537 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2538 >::type;
2539
2540 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002541 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002542 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2543 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2544 >::type;
2545
2546 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002547 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002548 is_assignable<_Dp&, _UDel&&>::value
2549 >::type;
2550
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002552 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002553 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002554 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002555 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002557 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002558 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002560 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002562 template <class _Pp, bool _Dummy = true,
2563 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002564 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002565 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002566 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002567 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002569 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002570 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2571 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002572 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002573 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002574 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002576 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002577 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002579 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002580 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002582 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002583 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2584 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002585 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002586 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002587 : __ptr_(__p, _VSTD::move(__d)) {
2588 static_assert(!is_reference<deleter_type>::value,
2589 "rvalue deleter bound to reference");
2590 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002592 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002593 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002594 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002595 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002596 : __ptr_(nullptr, _VSTD::move(__d)) {
2597 static_assert(!is_reference<deleter_type>::value,
2598 "rvalue deleter bound to reference");
2599 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002600
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002601 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002602 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2603 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002604 _LIBCPP_INLINE_VISIBILITY
2605 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002606
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002607 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002608 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002609 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2610 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002611
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002612 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002613 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002614 reset(__u.release());
2615 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2616 return *this;
2617 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002619 template <class _Up, class _Ep,
2620 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2621 class = _EnableIfDeleterConvertible<_Ep>
2622 >
2623 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002624 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002625 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002626 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002628 template <class _Up, class _Ep,
2629 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2630 class = _EnableIfDeleterAssignable<_Ep>
2631 >
2632 _LIBCPP_INLINE_VISIBILITY
2633 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002634 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002635 reset(__u.release());
2636 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2637 return *this;
2638 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002640#ifdef _LIBCPP_CXX03_LANG
2641 unique_ptr(unique_ptr const&) = delete;
2642 unique_ptr& operator=(unique_ptr const&) = delete;
2643#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002644
2645public:
2646 _LIBCPP_INLINE_VISIBILITY
2647 ~unique_ptr() { reset(); }
2648
2649 _LIBCPP_INLINE_VISIBILITY
2650 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2651 reset();
2652 return *this;
2653 }
2654
2655 _LIBCPP_INLINE_VISIBILITY
2656 typename add_lvalue_reference<_Tp>::type
2657 operator[](size_t __i) const {
2658 return __ptr_.first()[__i];
2659 }
2660 _LIBCPP_INLINE_VISIBILITY
2661 pointer get() const _NOEXCEPT {
2662 return __ptr_.first();
2663 }
2664
2665 _LIBCPP_INLINE_VISIBILITY
2666 deleter_type& get_deleter() _NOEXCEPT {
2667 return __ptr_.second();
2668 }
2669
2670 _LIBCPP_INLINE_VISIBILITY
2671 const deleter_type& get_deleter() const _NOEXCEPT {
2672 return __ptr_.second();
2673 }
2674 _LIBCPP_INLINE_VISIBILITY
2675 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2676 return __ptr_.first() != nullptr;
2677 }
2678
2679 _LIBCPP_INLINE_VISIBILITY
2680 pointer release() _NOEXCEPT {
2681 pointer __t = __ptr_.first();
2682 __ptr_.first() = pointer();
2683 return __t;
2684 }
2685
2686 template <class _Pp>
2687 _LIBCPP_INLINE_VISIBILITY
2688 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002689 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002690 >::type
2691 reset(_Pp __p) _NOEXCEPT {
2692 pointer __tmp = __ptr_.first();
2693 __ptr_.first() = __p;
2694 if (__tmp)
2695 __ptr_.second()(__tmp);
2696 }
2697
2698 _LIBCPP_INLINE_VISIBILITY
2699 void reset(nullptr_t = nullptr) _NOEXCEPT {
2700 pointer __tmp = __ptr_.first();
2701 __ptr_.first() = nullptr;
2702 if (__tmp)
2703 __ptr_.second()(__tmp);
2704 }
2705
2706 _LIBCPP_INLINE_VISIBILITY
2707 void swap(unique_ptr& __u) _NOEXCEPT {
2708 __ptr_.swap(__u.__ptr_);
2709 }
2710
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711};
2712
2713template <class _Tp, class _Dp>
2714inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002715typename enable_if<
2716 __is_swappable<_Dp>::value,
2717 void
2718>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002719swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720
2721template <class _T1, class _D1, class _T2, class _D2>
2722inline _LIBCPP_INLINE_VISIBILITY
2723bool
2724operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2725
2726template <class _T1, class _D1, class _T2, class _D2>
2727inline _LIBCPP_INLINE_VISIBILITY
2728bool
2729operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2730
2731template <class _T1, class _D1, class _T2, class _D2>
2732inline _LIBCPP_INLINE_VISIBILITY
2733bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002734operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2735{
2736 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2737 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002738 typedef typename common_type<_P1, _P2>::type _Vp;
2739 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002740}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741
2742template <class _T1, class _D1, class _T2, class _D2>
2743inline _LIBCPP_INLINE_VISIBILITY
2744bool
2745operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2746
2747template <class _T1, class _D1, class _T2, class _D2>
2748inline _LIBCPP_INLINE_VISIBILITY
2749bool
2750operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2751
2752template <class _T1, class _D1, class _T2, class _D2>
2753inline _LIBCPP_INLINE_VISIBILITY
2754bool
2755operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2756
Howard Hinnantb17caf92012-02-21 21:02:58 +00002757template <class _T1, class _D1>
2758inline _LIBCPP_INLINE_VISIBILITY
2759bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002760operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002761{
2762 return !__x;
2763}
2764
2765template <class _T1, class _D1>
2766inline _LIBCPP_INLINE_VISIBILITY
2767bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002768operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002769{
2770 return !__x;
2771}
2772
2773template <class _T1, class _D1>
2774inline _LIBCPP_INLINE_VISIBILITY
2775bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002776operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002777{
2778 return static_cast<bool>(__x);
2779}
2780
2781template <class _T1, class _D1>
2782inline _LIBCPP_INLINE_VISIBILITY
2783bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002784operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002785{
2786 return static_cast<bool>(__x);
2787}
2788
2789template <class _T1, class _D1>
2790inline _LIBCPP_INLINE_VISIBILITY
2791bool
2792operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2793{
2794 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2795 return less<_P1>()(__x.get(), nullptr);
2796}
2797
2798template <class _T1, class _D1>
2799inline _LIBCPP_INLINE_VISIBILITY
2800bool
2801operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2802{
2803 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2804 return less<_P1>()(nullptr, __x.get());
2805}
2806
2807template <class _T1, class _D1>
2808inline _LIBCPP_INLINE_VISIBILITY
2809bool
2810operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2811{
2812 return nullptr < __x;
2813}
2814
2815template <class _T1, class _D1>
2816inline _LIBCPP_INLINE_VISIBILITY
2817bool
2818operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2819{
2820 return __x < nullptr;
2821}
2822
2823template <class _T1, class _D1>
2824inline _LIBCPP_INLINE_VISIBILITY
2825bool
2826operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2827{
2828 return !(nullptr < __x);
2829}
2830
2831template <class _T1, class _D1>
2832inline _LIBCPP_INLINE_VISIBILITY
2833bool
2834operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2835{
2836 return !(__x < nullptr);
2837}
2838
2839template <class _T1, class _D1>
2840inline _LIBCPP_INLINE_VISIBILITY
2841bool
2842operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2843{
2844 return !(__x < nullptr);
2845}
2846
2847template <class _T1, class _D1>
2848inline _LIBCPP_INLINE_VISIBILITY
2849bool
2850operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2851{
2852 return !(nullptr < __x);
2853}
2854
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002855#if _LIBCPP_STD_VER > 11
2856
2857template<class _Tp>
2858struct __unique_if
2859{
2860 typedef unique_ptr<_Tp> __unique_single;
2861};
2862
2863template<class _Tp>
2864struct __unique_if<_Tp[]>
2865{
2866 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2867};
2868
2869template<class _Tp, size_t _Np>
2870struct __unique_if<_Tp[_Np]>
2871{
2872 typedef void __unique_array_known_bound;
2873};
2874
2875template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002876inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002877typename __unique_if<_Tp>::__unique_single
2878make_unique(_Args&&... __args)
2879{
2880 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2881}
2882
2883template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002884inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002885typename __unique_if<_Tp>::__unique_array_unknown_bound
2886make_unique(size_t __n)
2887{
2888 typedef typename remove_extent<_Tp>::type _Up;
2889 return unique_ptr<_Tp>(new _Up[__n]());
2890}
2891
2892template<class _Tp, class... _Args>
2893 typename __unique_if<_Tp>::__unique_array_known_bound
2894 make_unique(_Args&&...) = delete;
2895
2896#endif // _LIBCPP_STD_VER > 11
2897
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002898template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002899#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002900struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002901#else
2902struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002903 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002904#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002905{
2906 typedef unique_ptr<_Tp, _Dp> argument_type;
2907 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002908 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002909 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002910 {
2911 typedef typename argument_type::pointer pointer;
2912 return hash<pointer>()(__ptr.get());
2913 }
2914};
2915
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916struct __destruct_n
2917{
2918private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002919 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920
2921 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002922 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002923 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924
2925 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002926 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 {}
2928
Howard Hinnant719bda32011-05-28 14:41:13 +00002929 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002930 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002931 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 {}
2933
Howard Hinnant719bda32011-05-28 14:41:13 +00002934 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002935 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002936 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937 {}
2938public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002939 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002940 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941
2942 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002943 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002944 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945
2946 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002947 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002948 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949
2950 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002951 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002952 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953};
2954
2955template <class _Alloc>
2956class __allocator_destructor
2957{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002958 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002960 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2961 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962private:
2963 _Alloc& __alloc_;
2964 size_type __s_;
2965public:
2966 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002967 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002970 void operator()(pointer __p) _NOEXCEPT
2971 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972};
2973
2974template <class _InputIterator, class _ForwardIterator>
2975_ForwardIterator
2976uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2977{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002979#ifndef _LIBCPP_NO_EXCEPTIONS
2980 _ForwardIterator __s = __r;
2981 try
2982 {
2983#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002984 for (; __f != __l; ++__f, (void) ++__r)
2985 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002986#ifndef _LIBCPP_NO_EXCEPTIONS
2987 }
2988 catch (...)
2989 {
2990 for (; __s != __r; ++__s)
2991 __s->~value_type();
2992 throw;
2993 }
2994#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995 return __r;
2996}
2997
2998template <class _InputIterator, class _Size, class _ForwardIterator>
2999_ForwardIterator
3000uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3001{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003003#ifndef _LIBCPP_NO_EXCEPTIONS
3004 _ForwardIterator __s = __r;
3005 try
3006 {
3007#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003008 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3009 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003010#ifndef _LIBCPP_NO_EXCEPTIONS
3011 }
3012 catch (...)
3013 {
3014 for (; __s != __r; ++__s)
3015 __s->~value_type();
3016 throw;
3017 }
3018#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 return __r;
3020}
3021
3022template <class _ForwardIterator, class _Tp>
3023void
3024uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3025{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003027#ifndef _LIBCPP_NO_EXCEPTIONS
3028 _ForwardIterator __s = __f;
3029 try
3030 {
3031#endif
3032 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003033 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003034#ifndef _LIBCPP_NO_EXCEPTIONS
3035 }
3036 catch (...)
3037 {
3038 for (; __s != __f; ++__s)
3039 __s->~value_type();
3040 throw;
3041 }
3042#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043}
3044
3045template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003046_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3048{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003050#ifndef _LIBCPP_NO_EXCEPTIONS
3051 _ForwardIterator __s = __f;
3052 try
3053 {
3054#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003055 for (; __n > 0; ++__f, (void) --__n)
3056 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003057#ifndef _LIBCPP_NO_EXCEPTIONS
3058 }
3059 catch (...)
3060 {
3061 for (; __s != __f; ++__s)
3062 __s->~value_type();
3063 throw;
3064 }
3065#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003066 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067}
3068
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003069#if _LIBCPP_STD_VER > 14
3070
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003071template <class _Tp>
3072inline _LIBCPP_INLINE_VISIBILITY
3073void destroy_at(_Tp* __loc) {
3074 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3075 __loc->~_Tp();
3076}
3077
3078template <class _ForwardIterator>
3079inline _LIBCPP_INLINE_VISIBILITY
3080void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3081 for (; __first != __last; ++__first)
3082 _VSTD::destroy_at(_VSTD::addressof(*__first));
3083}
3084
3085template <class _ForwardIterator, class _Size>
3086inline _LIBCPP_INLINE_VISIBILITY
3087_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3088 for (; __n > 0; (void)++__first, --__n)
3089 _VSTD::destroy_at(_VSTD::addressof(*__first));
3090 return __first;
3091}
3092
Eric Fiselier290c07c2016-10-11 21:13:44 +00003093template <class _ForwardIterator>
3094inline _LIBCPP_INLINE_VISIBILITY
3095void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3096 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3097 auto __idx = __first;
3098#ifndef _LIBCPP_NO_EXCEPTIONS
3099 try {
3100#endif
3101 for (; __idx != __last; ++__idx)
3102 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3103#ifndef _LIBCPP_NO_EXCEPTIONS
3104 } catch (...) {
3105 _VSTD::destroy(__first, __idx);
3106 throw;
3107 }
3108#endif
3109}
3110
3111template <class _ForwardIterator, class _Size>
3112inline _LIBCPP_INLINE_VISIBILITY
3113_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3114 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3115 auto __idx = __first;
3116#ifndef _LIBCPP_NO_EXCEPTIONS
3117 try {
3118#endif
3119 for (; __n > 0; (void)++__idx, --__n)
3120 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3121 return __idx;
3122#ifndef _LIBCPP_NO_EXCEPTIONS
3123 } catch (...) {
3124 _VSTD::destroy(__first, __idx);
3125 throw;
3126 }
3127#endif
3128}
3129
3130
3131template <class _ForwardIterator>
3132inline _LIBCPP_INLINE_VISIBILITY
3133void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3134 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3135 auto __idx = __first;
3136#ifndef _LIBCPP_NO_EXCEPTIONS
3137 try {
3138#endif
3139 for (; __idx != __last; ++__idx)
3140 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3141#ifndef _LIBCPP_NO_EXCEPTIONS
3142 } catch (...) {
3143 _VSTD::destroy(__first, __idx);
3144 throw;
3145 }
3146#endif
3147}
3148
3149template <class _ForwardIterator, class _Size>
3150inline _LIBCPP_INLINE_VISIBILITY
3151_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3152 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3153 auto __idx = __first;
3154#ifndef _LIBCPP_NO_EXCEPTIONS
3155 try {
3156#endif
3157 for (; __n > 0; (void)++__idx, --__n)
3158 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3159 return __idx;
3160#ifndef _LIBCPP_NO_EXCEPTIONS
3161 } catch (...) {
3162 _VSTD::destroy(__first, __idx);
3163 throw;
3164 }
3165#endif
3166}
3167
3168
3169template <class _InputIt, class _ForwardIt>
3170inline _LIBCPP_INLINE_VISIBILITY
3171_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3172 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3173 auto __idx = __first_res;
3174#ifndef _LIBCPP_NO_EXCEPTIONS
3175 try {
3176#endif
3177 for (; __first != __last; (void)++__idx, ++__first)
3178 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3179 return __idx;
3180#ifndef _LIBCPP_NO_EXCEPTIONS
3181 } catch (...) {
3182 _VSTD::destroy(__first_res, __idx);
3183 throw;
3184 }
3185#endif
3186}
3187
3188template <class _InputIt, class _Size, class _ForwardIt>
3189inline _LIBCPP_INLINE_VISIBILITY
3190pair<_InputIt, _ForwardIt>
3191uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3192 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3193 auto __idx = __first_res;
3194#ifndef _LIBCPP_NO_EXCEPTIONS
3195 try {
3196#endif
3197 for (; __n > 0; ++__idx, (void)++__first, --__n)
3198 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3199 return {__first, __idx};
3200#ifndef _LIBCPP_NO_EXCEPTIONS
3201 } catch (...) {
3202 _VSTD::destroy(__first_res, __idx);
3203 throw;
3204 }
3205#endif
3206}
3207
3208
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003209#endif // _LIBCPP_STD_VER > 14
3210
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003211// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3212// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003213// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003214#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3215 && defined(__ATOMIC_RELAXED) \
3216 && defined(__ATOMIC_ACQ_REL)
3217# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003218#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003219# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3220#endif
3221
3222template <class _Tp>
3223inline _LIBCPP_INLINE_VISIBILITY _Tp
3224__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3225{
3226#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3227 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3228#else
3229 return __t += 1;
3230#endif
3231}
3232
3233template <class _Tp>
3234inline _LIBCPP_INLINE_VISIBILITY _Tp
3235__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3236{
3237#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3238 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3239#else
3240 return __t -= 1;
3241#endif
3242}
3243
Howard Hinnant756c69b2010-09-22 16:48:34 +00003244class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245 : public std::exception
3246{
3247public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003248 bad_weak_ptr() _NOEXCEPT = default;
3249 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003250 virtual ~bad_weak_ptr() _NOEXCEPT;
3251 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252};
3253
Louis Dionne16fe2952018-07-11 23:14:33 +00003254_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003255void __throw_bad_weak_ptr()
3256{
3257#ifndef _LIBCPP_NO_EXCEPTIONS
3258 throw bad_weak_ptr();
3259#else
3260 _VSTD::abort();
3261#endif
3262}
3263
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003264template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003266class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267{
3268 __shared_count(const __shared_count&);
3269 __shared_count& operator=(const __shared_count&);
3270
3271protected:
3272 long __shared_owners_;
3273 virtual ~__shared_count();
3274private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003275 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276
3277public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003279 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280 : __shared_owners_(__refs) {}
3281
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003282#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003283 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003284 void __add_shared() _NOEXCEPT;
3285 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003286#else
3287 _LIBCPP_INLINE_VISIBILITY
3288 void __add_shared() _NOEXCEPT {
3289 __libcpp_atomic_refcount_increment(__shared_owners_);
3290 }
3291 _LIBCPP_INLINE_VISIBILITY
3292 bool __release_shared() _NOEXCEPT {
3293 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3294 __on_zero_shared();
3295 return true;
3296 }
3297 return false;
3298 }
3299#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003300 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003301 long use_count() const _NOEXCEPT {
3302 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3303 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304};
3305
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003306class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307 : private __shared_count
3308{
3309 long __shared_weak_owners_;
3310
3311public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003313 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314 : __shared_count(__refs),
3315 __shared_weak_owners_(__refs) {}
3316protected:
3317 virtual ~__shared_weak_count();
3318
3319public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003320#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003321 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003322 void __add_shared() _NOEXCEPT;
3323 void __add_weak() _NOEXCEPT;
3324 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003325#else
3326 _LIBCPP_INLINE_VISIBILITY
3327 void __add_shared() _NOEXCEPT {
3328 __shared_count::__add_shared();
3329 }
3330 _LIBCPP_INLINE_VISIBILITY
3331 void __add_weak() _NOEXCEPT {
3332 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3333 }
3334 _LIBCPP_INLINE_VISIBILITY
3335 void __release_shared() _NOEXCEPT {
3336 if (__shared_count::__release_shared())
3337 __release_weak();
3338 }
3339#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003340 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003342 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3343 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003344
Howard Hinnant807d6332013-02-25 15:50:36 +00003345 // Define the function out only if we build static libc++ without RTTI.
3346 // Otherwise we may break clients who need to compile their projects with
3347 // -fno-rtti and yet link against a libc++.dylib compiled
3348 // without -fno-rtti.
3349#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003350 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003351#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003353 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354};
3355
3356template <class _Tp, class _Dp, class _Alloc>
3357class __shared_ptr_pointer
3358 : public __shared_weak_count
3359{
3360 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3361public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003364 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365
Howard Hinnant72f73582010-08-11 17:04:31 +00003366#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003367 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003368#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369
3370private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003371 virtual void __on_zero_shared() _NOEXCEPT;
3372 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373};
3374
Howard Hinnant72f73582010-08-11 17:04:31 +00003375#ifndef _LIBCPP_NO_RTTI
3376
Howard Hinnantc51e1022010-05-11 19:42:16 +00003377template <class _Tp, class _Dp, class _Alloc>
3378const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003379__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003381 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382}
3383
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003384#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003385
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386template <class _Tp, class _Dp, class _Alloc>
3387void
Howard Hinnant719bda32011-05-28 14:41:13 +00003388__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389{
3390 __data_.first().second()(__data_.first().first());
3391 __data_.first().second().~_Dp();
3392}
3393
3394template <class _Tp, class _Dp, class _Alloc>
3395void
Howard Hinnant719bda32011-05-28 14:41:13 +00003396__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003398 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3399 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003400 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3401
Eric Fiselierf8898c82015-02-05 23:01:40 +00003402 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003404 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405}
3406
3407template <class _Tp, class _Alloc>
3408class __shared_ptr_emplace
3409 : public __shared_weak_count
3410{
3411 __compressed_pair<_Alloc, _Tp> __data_;
3412public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413
Howard Hinnant756c69b2010-09-22 16:48:34 +00003414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003416 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003418
3419#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003422 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003423 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3424 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425#else // _LIBCPP_HAS_NO_VARIADICS
3426
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3430 : __data_(__a, _Tp(__a0)) {}
3431
3432 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3435 : __data_(__a, _Tp(__a0, __a1)) {}
3436
3437 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3440 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3441
3442#endif // _LIBCPP_HAS_NO_VARIADICS
3443
3444private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003445 virtual void __on_zero_shared() _NOEXCEPT;
3446 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003448 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003449 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450};
3451
3452template <class _Tp, class _Alloc>
3453void
Howard Hinnant719bda32011-05-28 14:41:13 +00003454__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455{
3456 __data_.second().~_Tp();
3457}
3458
3459template <class _Tp, class _Alloc>
3460void
Howard Hinnant719bda32011-05-28 14:41:13 +00003461__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003463 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3464 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003465 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003466 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003467 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003468 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469}
3470
Erik Pilkington2a398762017-05-25 15:43:31 +00003471struct __shared_ptr_dummy_rebind_allocator_type;
3472template <>
3473class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3474{
3475public:
3476 template <class _Other>
3477 struct rebind
3478 {
3479 typedef allocator<_Other> other;
3480 };
3481};
3482
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003483template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484
zoecarverd73f42a2020-05-11 18:42:50 -07003485template<class _Tp, class _Up>
3486struct __compatible_with
3487#if _LIBCPP_STD_VER > 14
3488 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3489#else
3490 : is_convertible<_Tp*, _Up*> {};
3491#endif // _LIBCPP_STD_VER > 14
3492
Vy Nguyene369bd92020-07-13 12:34:37 -04003493#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
3494# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
3495#else
3496# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
3497#endif
3498
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003500class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003502public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003503#if _LIBCPP_STD_VER > 14
3504 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003505 typedef remove_extent_t<_Tp> element_type;
3506#else
3507 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003508#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003509
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510private:
3511 element_type* __ptr_;
3512 __shared_weak_count* __cntrl_;
3513
3514 struct __nat {int __for_bool_;};
3515public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003517 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003519 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003520 template<class _Yp>
3521 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003522 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003523 template<class _Yp, class _Dp>
3524 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003525 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003526 template<class _Yp, class _Dp, class _Alloc>
3527 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003528 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3530 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003531 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003533 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003537 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003538 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003540 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003541 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003542 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003543 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003545 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003546#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00003547 template<class _Yp>
3548 shared_ptr(auto_ptr<_Yp>&& __r,
3549 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003550#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00003551 template <class _Yp, class _Dp>
3552 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3553 typename enable_if
3554 <
3555 !is_lvalue_reference<_Dp>::value &&
3556 !is_array<_Yp>::value &&
3557 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3558 __nat
3559 >::type = __nat());
3560 template <class _Yp, class _Dp>
3561 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3562 typename enable_if
3563 <
3564 is_lvalue_reference<_Dp>::value &&
3565 !is_array<_Yp>::value &&
3566 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3567 __nat
3568 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569
3570 ~shared_ptr();
3571
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003573 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003574 template<class _Yp>
3575 typename enable_if
3576 <
zoecarverd73f42a2020-05-11 18:42:50 -07003577 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003578 shared_ptr&
3579 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003581 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003583 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003584 template<class _Yp>
3585 typename enable_if
3586 <
zoecarverd73f42a2020-05-11 18:42:50 -07003587 __compatible_with<_Yp, element_type>::value,
3588 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003589 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003591 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003592#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003593 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003595 typename enable_if
3596 <
3597 !is_array<_Yp>::value &&
3598 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003599 shared_ptr
3600 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003601 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003602#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003603 template <class _Yp, class _Dp>
3604 typename enable_if
3605 <
3606 !is_array<_Yp>::value &&
3607 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3608 shared_ptr&
3609 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003611 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003614 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003616 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003617 template<class _Yp>
3618 typename enable_if
3619 <
zoecarverd73f42a2020-05-11 18:42:50 -07003620 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003621 void
3622 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003624 reset(_Yp* __p);
3625 template<class _Yp, class _Dp>
3626 typename enable_if
3627 <
zoecarverd73f42a2020-05-11 18:42:50 -07003628 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003629 void
3630 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003632 reset(_Yp* __p, _Dp __d);
3633 template<class _Yp, class _Dp, class _Alloc>
3634 typename enable_if
3635 <
zoecarverd73f42a2020-05-11 18:42:50 -07003636 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003637 void
3638 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003640 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641
Howard Hinnant756c69b2010-09-22 16:48:34 +00003642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003643 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003645 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3646 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003647 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003648 element_type* operator->() const _NOEXCEPT
3649 {
3650 static_assert(!_VSTD::is_array<_Tp>::value,
3651 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3652 return __ptr_;
3653 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003655 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003657 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003659 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003660 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003661 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003662 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003664 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003665 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003666 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003668 _LIBCPP_INLINE_VISIBILITY
3669 bool
3670 __owner_equivalent(const shared_ptr& __p) const
3671 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672
zoecarverd73f42a2020-05-11 18:42:50 -07003673#if _LIBCPP_STD_VER > 14
3674 typename add_lvalue_reference<element_type>::type
3675 _LIBCPP_INLINE_VISIBILITY
3676 operator[](ptrdiff_t __i) const
3677 {
3678 static_assert(_VSTD::is_array<_Tp>::value,
3679 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3680 return __ptr_[__i];
3681 }
3682#endif
3683
Howard Hinnant72f73582010-08-11 17:04:31 +00003684#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003687 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003688 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003689 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003690 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003691#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692
Zoe Carverd9040c72019-10-22 15:16:49 +00003693 template<class _Yp, class _CntrlBlk>
3694 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07003695 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00003696 {
3697 shared_ptr<_Tp> __r;
3698 __r.__ptr_ = __p;
3699 __r.__cntrl_ = __cntrl;
3700 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3701 return __r;
3702 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003703
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003705 template <class _Yp, bool = is_function<_Yp>::value>
3706 struct __shared_ptr_default_allocator
3707 {
3708 typedef allocator<_Yp> type;
3709 };
3710
3711 template <class _Yp>
3712 struct __shared_ptr_default_allocator<_Yp, true>
3713 {
3714 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3715 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003717 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003718 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003719 typename enable_if<is_convertible<_OrigPtr*,
3720 const enable_shared_from_this<_Yp>*
3721 >::value,
3722 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003723 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3724 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003726 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003727 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003728 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003729 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3730 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003731 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732 }
3733
Erik Pilkington2a398762017-05-25 15:43:31 +00003734 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735
zoecarverd73f42a2020-05-11 18:42:50 -07003736 template <class, class _Yp>
3737 struct __shared_ptr_default_delete
3738 : default_delete<_Yp> {};
3739
3740 template <class _Yp, class _Un, size_t _Sz>
3741 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3742 : default_delete<_Yp[]> {};
3743
3744 template <class _Yp, class _Un>
3745 struct __shared_ptr_default_delete<_Yp[], _Un>
3746 : default_delete<_Yp[]> {};
3747
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003748 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3749 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750};
3751
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003752#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3753template<class _Tp>
3754shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3755template<class _Tp, class _Dp>
3756shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3757#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003758
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003760inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003761_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003762shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763 : __ptr_(0),
3764 __cntrl_(0)
3765{
3766}
3767
3768template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003769inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003770_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003771shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772 : __ptr_(0),
3773 __cntrl_(0)
3774{
3775}
3776
3777template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003778template<class _Yp>
3779shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003780 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781 : __ptr_(__p)
3782{
3783 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003784 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07003785 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3786 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003788 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789}
3790
3791template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003792template<class _Yp, class _Dp>
3793shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003794 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795 : __ptr_(__p)
3796{
3797#ifndef _LIBCPP_NO_EXCEPTIONS
3798 try
3799 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003800#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003801 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3802 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3803 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003804 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805#ifndef _LIBCPP_NO_EXCEPTIONS
3806 }
3807 catch (...)
3808 {
3809 __d(__p);
3810 throw;
3811 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003812#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813}
3814
3815template<class _Tp>
3816template<class _Dp>
3817shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3818 : __ptr_(0)
3819{
3820#ifndef _LIBCPP_NO_EXCEPTIONS
3821 try
3822 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003823#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003824 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3825 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3826 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827#ifndef _LIBCPP_NO_EXCEPTIONS
3828 }
3829 catch (...)
3830 {
3831 __d(__p);
3832 throw;
3833 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003834#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835}
3836
3837template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003838template<class _Yp, class _Dp, class _Alloc>
3839shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003840 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003841 : __ptr_(__p)
3842{
3843#ifndef _LIBCPP_NO_EXCEPTIONS
3844 try
3845 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003846#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003848 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849 typedef __allocator_destructor<_A2> _D2;
3850 _A2 __a2(__a);
3851 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003852 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3853 _CntrlBlk(__p, __d, __a);
3854 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003855 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856#ifndef _LIBCPP_NO_EXCEPTIONS
3857 }
3858 catch (...)
3859 {
3860 __d(__p);
3861 throw;
3862 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003863#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864}
3865
3866template<class _Tp>
3867template<class _Dp, class _Alloc>
3868shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3869 : __ptr_(0)
3870{
3871#ifndef _LIBCPP_NO_EXCEPTIONS
3872 try
3873 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003874#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003876 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877 typedef __allocator_destructor<_A2> _D2;
3878 _A2 __a2(__a);
3879 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003880 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3881 _CntrlBlk(__p, __d, __a);
3882 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003883#ifndef _LIBCPP_NO_EXCEPTIONS
3884 }
3885 catch (...)
3886 {
3887 __d(__p);
3888 throw;
3889 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003890#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891}
3892
3893template<class _Tp>
3894template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003895inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003896shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003897 : __ptr_(__p),
3898 __cntrl_(__r.__cntrl_)
3899{
3900 if (__cntrl_)
3901 __cntrl_->__add_shared();
3902}
3903
3904template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003905inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003906shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907 : __ptr_(__r.__ptr_),
3908 __cntrl_(__r.__cntrl_)
3909{
3910 if (__cntrl_)
3911 __cntrl_->__add_shared();
3912}
3913
3914template<class _Tp>
3915template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003916inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003918 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003919 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920 : __ptr_(__r.__ptr_),
3921 __cntrl_(__r.__cntrl_)
3922{
3923 if (__cntrl_)
3924 __cntrl_->__add_shared();
3925}
3926
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003928inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003929shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930 : __ptr_(__r.__ptr_),
3931 __cntrl_(__r.__cntrl_)
3932{
3933 __r.__ptr_ = 0;
3934 __r.__cntrl_ = 0;
3935}
3936
3937template<class _Tp>
3938template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003939inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003941 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003942 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003943 : __ptr_(__r.__ptr_),
3944 __cntrl_(__r.__cntrl_)
3945{
3946 __r.__ptr_ = 0;
3947 __r.__cntrl_ = 0;
3948}
3949
Marshall Clowb22274f2017-01-24 22:22:33 +00003950#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003952template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003953shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003954 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955 : __ptr_(__r.get())
3956{
3957 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3958 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003959 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960 __r.release();
3961}
Marshall Clowb22274f2017-01-24 22:22:33 +00003962#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963
3964template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003965template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003967 typename enable_if
3968 <
3969 !is_lvalue_reference<_Dp>::value &&
3970 !is_array<_Yp>::value &&
3971 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3972 __nat
3973 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974 : __ptr_(__r.get())
3975{
Marshall Clow35cde742015-05-10 13:59:45 +00003976#if _LIBCPP_STD_VER > 11
3977 if (__ptr_ == nullptr)
3978 __cntrl_ = nullptr;
3979 else
3980#endif
3981 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003982 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3983 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3984 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003985 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003986 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987 __r.release();
3988}
3989
3990template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003991template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003993 typename enable_if
3994 <
3995 is_lvalue_reference<_Dp>::value &&
3996 !is_array<_Yp>::value &&
3997 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3998 __nat
3999 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000 : __ptr_(__r.get())
4001{
Marshall Clow35cde742015-05-10 13:59:45 +00004002#if _LIBCPP_STD_VER > 11
4003 if (__ptr_ == nullptr)
4004 __cntrl_ = nullptr;
4005 else
4006#endif
4007 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004008 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004009 typedef __shared_ptr_pointer<_Yp*,
4010 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004011 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004012 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004013 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004014 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015 __r.release();
4016}
4017
Zoe Carver6cd05c32019-08-19 15:47:16 +00004018template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019shared_ptr<_Tp>::~shared_ptr()
4020{
4021 if (__cntrl_)
4022 __cntrl_->__release_shared();
4023}
4024
4025template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004026inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004028shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029{
4030 shared_ptr(__r).swap(*this);
4031 return *this;
4032}
4033
4034template<class _Tp>
4035template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004036inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004037typename enable_if
4038<
zoecarverd73f42a2020-05-11 18:42:50 -07004039 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004040 shared_ptr<_Tp>&
4041>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004042shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043{
4044 shared_ptr(__r).swap(*this);
4045 return *this;
4046}
4047
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004049inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004051shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004053 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054 return *this;
4055}
4056
4057template<class _Tp>
4058template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004059inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004060typename enable_if
4061<
zoecarverd73f42a2020-05-11 18:42:50 -07004062 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004063 shared_ptr<_Tp>&
4064>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4066{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004067 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068 return *this;
4069}
4070
Marshall Clowb22274f2017-01-24 22:22:33 +00004071#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072template<class _Tp>
4073template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004074inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004075typename enable_if
4076<
4077 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004078 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004079 shared_ptr<_Tp>
4080>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004081shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4082{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004083 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084 return *this;
4085}
Marshall Clowb22274f2017-01-24 22:22:33 +00004086#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087
4088template<class _Tp>
4089template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004090inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004091typename enable_if
4092<
4093 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004094 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004095 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004096 shared_ptr<_Tp>&
4097>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4099{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004100 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101 return *this;
4102}
4103
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004105inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106void
Howard Hinnant719bda32011-05-28 14:41:13 +00004107shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004109 _VSTD::swap(__ptr_, __r.__ptr_);
4110 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111}
4112
4113template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004114inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115void
Howard Hinnant719bda32011-05-28 14:41:13 +00004116shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117{
4118 shared_ptr().swap(*this);
4119}
4120
4121template<class _Tp>
4122template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004123inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004124typename enable_if
4125<
zoecarverd73f42a2020-05-11 18:42:50 -07004126 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004127 void
4128>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129shared_ptr<_Tp>::reset(_Yp* __p)
4130{
4131 shared_ptr(__p).swap(*this);
4132}
4133
4134template<class _Tp>
4135template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004136inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004137typename enable_if
4138<
zoecarverd73f42a2020-05-11 18:42:50 -07004139 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004140 void
4141>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4143{
4144 shared_ptr(__p, __d).swap(*this);
4145}
4146
4147template<class _Tp>
4148template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004149inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004150typename enable_if
4151<
zoecarverd73f42a2020-05-11 18:42:50 -07004152 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004153 void
4154>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4156{
4157 shared_ptr(__p, __d, __a).swap(*this);
4158}
4159
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004160template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004162typename enable_if
4163<
4164 !is_array<_Tp>::value,
4165 shared_ptr<_Tp>
4166>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167make_shared(_Args&& ...__args)
4168{
Zoe Carverd9040c72019-10-22 15:16:49 +00004169 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4170 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4171 typedef allocator<_CntrlBlk> _A2;
4172 typedef __allocator_destructor<_A2> _D2;
4173
4174 _A2 __a2;
4175 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4176 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4177
4178 _Tp *__ptr = __hold2.get()->get();
4179 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180}
4181
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004182template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004184typename enable_if
4185<
4186 !is_array<_Tp>::value,
4187 shared_ptr<_Tp>
4188>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004189allocate_shared(const _Alloc& __a, _Args&& ...__args)
4190{
zoecarver505730a2020-02-25 16:50:57 -08004191 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4192
4193 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4194 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4195 typedef __allocator_destructor<_A2> _D2;
4196
4197 _A2 __a2(__a);
4198 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4199 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4200 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4201
4202 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4203 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204}
4205
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206template<class _Tp, class _Up>
4207inline _LIBCPP_INLINE_VISIBILITY
4208bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004209operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210{
4211 return __x.get() == __y.get();
4212}
4213
4214template<class _Tp, class _Up>
4215inline _LIBCPP_INLINE_VISIBILITY
4216bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004217operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218{
4219 return !(__x == __y);
4220}
4221
4222template<class _Tp, class _Up>
4223inline _LIBCPP_INLINE_VISIBILITY
4224bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004225operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004227#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004228 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4229 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004230#else
4231 return less<>()(__x.get(), __y.get());
4232#endif
4233
Howard Hinnantb17caf92012-02-21 21:02:58 +00004234}
4235
4236template<class _Tp, class _Up>
4237inline _LIBCPP_INLINE_VISIBILITY
4238bool
4239operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4240{
4241 return __y < __x;
4242}
4243
4244template<class _Tp, class _Up>
4245inline _LIBCPP_INLINE_VISIBILITY
4246bool
4247operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4248{
4249 return !(__y < __x);
4250}
4251
4252template<class _Tp, class _Up>
4253inline _LIBCPP_INLINE_VISIBILITY
4254bool
4255operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4256{
4257 return !(__x < __y);
4258}
4259
4260template<class _Tp>
4261inline _LIBCPP_INLINE_VISIBILITY
4262bool
4263operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4264{
4265 return !__x;
4266}
4267
4268template<class _Tp>
4269inline _LIBCPP_INLINE_VISIBILITY
4270bool
4271operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4272{
4273 return !__x;
4274}
4275
4276template<class _Tp>
4277inline _LIBCPP_INLINE_VISIBILITY
4278bool
4279operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4280{
4281 return static_cast<bool>(__x);
4282}
4283
4284template<class _Tp>
4285inline _LIBCPP_INLINE_VISIBILITY
4286bool
4287operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4288{
4289 return static_cast<bool>(__x);
4290}
4291
4292template<class _Tp>
4293inline _LIBCPP_INLINE_VISIBILITY
4294bool
4295operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4296{
4297 return less<_Tp*>()(__x.get(), nullptr);
4298}
4299
4300template<class _Tp>
4301inline _LIBCPP_INLINE_VISIBILITY
4302bool
4303operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4304{
4305 return less<_Tp*>()(nullptr, __x.get());
4306}
4307
4308template<class _Tp>
4309inline _LIBCPP_INLINE_VISIBILITY
4310bool
4311operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4312{
4313 return nullptr < __x;
4314}
4315
4316template<class _Tp>
4317inline _LIBCPP_INLINE_VISIBILITY
4318bool
4319operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4320{
4321 return __x < nullptr;
4322}
4323
4324template<class _Tp>
4325inline _LIBCPP_INLINE_VISIBILITY
4326bool
4327operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4328{
4329 return !(nullptr < __x);
4330}
4331
4332template<class _Tp>
4333inline _LIBCPP_INLINE_VISIBILITY
4334bool
4335operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4336{
4337 return !(__x < nullptr);
4338}
4339
4340template<class _Tp>
4341inline _LIBCPP_INLINE_VISIBILITY
4342bool
4343operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4344{
4345 return !(__x < nullptr);
4346}
4347
4348template<class _Tp>
4349inline _LIBCPP_INLINE_VISIBILITY
4350bool
4351operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4352{
4353 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354}
4355
4356template<class _Tp>
4357inline _LIBCPP_INLINE_VISIBILITY
4358void
Howard Hinnant719bda32011-05-28 14:41:13 +00004359swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360{
4361 __x.swap(__y);
4362}
4363
4364template<class _Tp, class _Up>
4365inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004366shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004367static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368{
zoecarverd73f42a2020-05-11 18:42:50 -07004369 return shared_ptr<_Tp>(__r,
4370 static_cast<
4371 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372}
4373
4374template<class _Tp, class _Up>
4375inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004376shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004377dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378{
zoecarverd73f42a2020-05-11 18:42:50 -07004379 typedef typename shared_ptr<_Tp>::element_type _ET;
4380 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004381 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4382}
4383
4384template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004385shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004386const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387{
zoecarverd73f42a2020-05-11 18:42:50 -07004388 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004389 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390}
4391
zoecarverd73f42a2020-05-11 18:42:50 -07004392template<class _Tp, class _Up>
4393shared_ptr<_Tp>
4394reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4395{
4396 return shared_ptr<_Tp>(__r,
4397 reinterpret_cast<
4398 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4399}
4400
Howard Hinnant72f73582010-08-11 17:04:31 +00004401#ifndef _LIBCPP_NO_RTTI
4402
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403template<class _Dp, class _Tp>
4404inline _LIBCPP_INLINE_VISIBILITY
4405_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004406get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004407{
4408 return __p.template __get_deleter<_Dp>();
4409}
4410
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004411#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004412
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04004414class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004416public:
4417 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004418private:
4419 element_type* __ptr_;
4420 __shared_weak_count* __cntrl_;
4421
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004422public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004424 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004425 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004426 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4427 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004429 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004430 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004431 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4432 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004433
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004435 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004436 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004437 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4438 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004439 ~weak_ptr();
4440
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004442 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004443 template<class _Yp>
4444 typename enable_if
4445 <
4446 is_convertible<_Yp*, element_type*>::value,
4447 weak_ptr&
4448 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004450 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4451
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004453 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4454 template<class _Yp>
4455 typename enable_if
4456 <
4457 is_convertible<_Yp*, element_type*>::value,
4458 weak_ptr&
4459 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004461 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4462
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004463 template<class _Yp>
4464 typename enable_if
4465 <
4466 is_convertible<_Yp*, element_type*>::value,
4467 weak_ptr&
4468 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004470 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004471
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004473 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004475 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004476
Howard Hinnant756c69b2010-09-22 16:48:34 +00004477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004478 long use_count() const _NOEXCEPT
4479 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004481 bool expired() const _NOEXCEPT
4482 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4483 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004484 template<class _Up>
4485 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004486 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004487 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004488 template<class _Up>
4489 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004490 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004491 {return __cntrl_ < __r.__cntrl_;}
4492
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004493 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4494 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495};
4496
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004497#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4498template<class _Tp>
4499weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4500#endif
4501
Howard Hinnantc51e1022010-05-11 19:42:16 +00004502template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004503inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004504_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004505weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004506 : __ptr_(0),
4507 __cntrl_(0)
4508{
4509}
4510
4511template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004512inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004513weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004514 : __ptr_(__r.__ptr_),
4515 __cntrl_(__r.__cntrl_)
4516{
4517 if (__cntrl_)
4518 __cntrl_->__add_weak();
4519}
4520
4521template<class _Tp>
4522template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004523inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004524weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004525 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004526 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004527 : __ptr_(__r.__ptr_),
4528 __cntrl_(__r.__cntrl_)
4529{
4530 if (__cntrl_)
4531 __cntrl_->__add_weak();
4532}
4533
4534template<class _Tp>
4535template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004536inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004537weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004538 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004539 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004540 : __ptr_(__r.__ptr_),
4541 __cntrl_(__r.__cntrl_)
4542{
4543 if (__cntrl_)
4544 __cntrl_->__add_weak();
4545}
4546
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004547template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004548inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004549weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4550 : __ptr_(__r.__ptr_),
4551 __cntrl_(__r.__cntrl_)
4552{
4553 __r.__ptr_ = 0;
4554 __r.__cntrl_ = 0;
4555}
4556
4557template<class _Tp>
4558template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004559inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004560weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4561 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4562 _NOEXCEPT
4563 : __ptr_(__r.__ptr_),
4564 __cntrl_(__r.__cntrl_)
4565{
4566 __r.__ptr_ = 0;
4567 __r.__cntrl_ = 0;
4568}
4569
Howard Hinnantc51e1022010-05-11 19:42:16 +00004570template<class _Tp>
4571weak_ptr<_Tp>::~weak_ptr()
4572{
4573 if (__cntrl_)
4574 __cntrl_->__release_weak();
4575}
4576
4577template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004578inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004579weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004580weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004581{
4582 weak_ptr(__r).swap(*this);
4583 return *this;
4584}
4585
4586template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004587template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004588inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004589typename enable_if
4590<
4591 is_convertible<_Yp*, _Tp*>::value,
4592 weak_ptr<_Tp>&
4593>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004594weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004595{
4596 weak_ptr(__r).swap(*this);
4597 return *this;
4598}
4599
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004600template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004601inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004602weak_ptr<_Tp>&
4603weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4604{
4605 weak_ptr(_VSTD::move(__r)).swap(*this);
4606 return *this;
4607}
4608
Howard Hinnantc51e1022010-05-11 19:42:16 +00004609template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004610template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004611inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004612typename enable_if
4613<
4614 is_convertible<_Yp*, _Tp*>::value,
4615 weak_ptr<_Tp>&
4616>::type
4617weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4618{
4619 weak_ptr(_VSTD::move(__r)).swap(*this);
4620 return *this;
4621}
4622
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004623template<class _Tp>
4624template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004625inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004626typename enable_if
4627<
4628 is_convertible<_Yp*, _Tp*>::value,
4629 weak_ptr<_Tp>&
4630>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004631weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632{
4633 weak_ptr(__r).swap(*this);
4634 return *this;
4635}
4636
4637template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004638inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004639void
Howard Hinnant719bda32011-05-28 14:41:13 +00004640weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004642 _VSTD::swap(__ptr_, __r.__ptr_);
4643 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644}
4645
4646template<class _Tp>
4647inline _LIBCPP_INLINE_VISIBILITY
4648void
Howard Hinnant719bda32011-05-28 14:41:13 +00004649swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004650{
4651 __x.swap(__y);
4652}
4653
4654template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004655inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004656void
Howard Hinnant719bda32011-05-28 14:41:13 +00004657weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004658{
4659 weak_ptr().swap(*this);
4660}
4661
4662template<class _Tp>
4663template<class _Yp>
4664shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004665 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004666 : __ptr_(__r.__ptr_),
4667 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4668{
4669 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004670 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004671}
4672
4673template<class _Tp>
4674shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004675weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004676{
4677 shared_ptr<_Tp> __r;
4678 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4679 if (__r.__cntrl_)
4680 __r.__ptr_ = __ptr_;
4681 return __r;
4682}
4683
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004684#if _LIBCPP_STD_VER > 14
4685template <class _Tp = void> struct owner_less;
4686#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004687template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004688#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004689
4690template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004691struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004692 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004693{
4694 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004695 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004696 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004697 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004698 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004699 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004700 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004701 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004702 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004703 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004704};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004705
4706template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004707struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004708 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4709{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004710 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004711 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004712 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004713 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004714 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004715 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004716 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004717 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004718 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004719 {return __x.owner_before(__y);}
4720};
4721
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004722#if _LIBCPP_STD_VER > 14
4723template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004724struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004725{
4726 template <class _Tp, class _Up>
4727 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004728 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004729 {return __x.owner_before(__y);}
4730 template <class _Tp, class _Up>
4731 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004732 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004733 {return __x.owner_before(__y);}
4734 template <class _Tp, class _Up>
4735 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004736 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004737 {return __x.owner_before(__y);}
4738 template <class _Tp, class _Up>
4739 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004740 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004741 {return __x.owner_before(__y);}
4742 typedef void is_transparent;
4743};
4744#endif
4745
Howard Hinnantc51e1022010-05-11 19:42:16 +00004746template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004747class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004748{
4749 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004750protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004751 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004752 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004754 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004756 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4757 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004759 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004760public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004762 shared_ptr<_Tp> shared_from_this()
4763 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004765 shared_ptr<_Tp const> shared_from_this() const
4766 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004767
Eric Fiselier84006862016-06-02 00:15:35 +00004768#if _LIBCPP_STD_VER > 14
4769 _LIBCPP_INLINE_VISIBILITY
4770 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4771 { return __weak_this_; }
4772
4773 _LIBCPP_INLINE_VISIBILITY
4774 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4775 { return __weak_this_; }
4776#endif // _LIBCPP_STD_VER > 14
4777
Howard Hinnantc51e1022010-05-11 19:42:16 +00004778 template <class _Up> friend class shared_ptr;
4779};
4780
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004781template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004782struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004783{
4784 typedef shared_ptr<_Tp> argument_type;
4785 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00004786
Howard Hinnant756c69b2010-09-22 16:48:34 +00004787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004788 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004789 {
zoecarverd73f42a2020-05-11 18:42:50 -07004790 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004791 }
4792};
4793
Howard Hinnantc834c512011-11-29 18:15:50 +00004794template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00004795inline _LIBCPP_INLINE_VISIBILITY
4796basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00004797operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00004798
Eric Fiselier9b492672016-06-18 02:12:53 +00004799
4800#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004801
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004802class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00004803{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004804 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004805public:
4806 void lock() _NOEXCEPT;
4807 void unlock() _NOEXCEPT;
4808
4809private:
4810 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4811 __sp_mut(const __sp_mut&);
4812 __sp_mut& operator=(const __sp_mut&);
4813
Howard Hinnant8331b762013-03-06 23:30:19 +00004814 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004815};
4816
Mehdi Amini228053d2017-05-04 17:08:54 +00004817_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4818__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004819
4820template <class _Tp>
4821inline _LIBCPP_INLINE_VISIBILITY
4822bool
4823atomic_is_lock_free(const shared_ptr<_Tp>*)
4824{
4825 return false;
4826}
4827
4828template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004829_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004830shared_ptr<_Tp>
4831atomic_load(const shared_ptr<_Tp>* __p)
4832{
4833 __sp_mut& __m = __get_sp_mut(__p);
4834 __m.lock();
4835 shared_ptr<_Tp> __q = *__p;
4836 __m.unlock();
4837 return __q;
4838}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004839
Howard Hinnant9fa30202012-07-30 01:40:57 +00004840template <class _Tp>
4841inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004842_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004843shared_ptr<_Tp>
4844atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4845{
4846 return atomic_load(__p);
4847}
4848
4849template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004850_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004851void
4852atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4853{
4854 __sp_mut& __m = __get_sp_mut(__p);
4855 __m.lock();
4856 __p->swap(__r);
4857 __m.unlock();
4858}
4859
4860template <class _Tp>
4861inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004862_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004863void
4864atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4865{
4866 atomic_store(__p, __r);
4867}
4868
4869template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004870_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004871shared_ptr<_Tp>
4872atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4873{
4874 __sp_mut& __m = __get_sp_mut(__p);
4875 __m.lock();
4876 __p->swap(__r);
4877 __m.unlock();
4878 return __r;
4879}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004880
Howard Hinnant9fa30202012-07-30 01:40:57 +00004881template <class _Tp>
4882inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004883_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004884shared_ptr<_Tp>
4885atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4886{
4887 return atomic_exchange(__p, __r);
4888}
4889
4890template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004891_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004892bool
4893atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4894{
Marshall Clow4201ee82016-05-18 17:50:13 +00004895 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004896 __sp_mut& __m = __get_sp_mut(__p);
4897 __m.lock();
4898 if (__p->__owner_equivalent(*__v))
4899 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004900 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004901 *__p = __w;
4902 __m.unlock();
4903 return true;
4904 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004905 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004906 *__v = *__p;
4907 __m.unlock();
4908 return false;
4909}
4910
4911template <class _Tp>
4912inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004913_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004914bool
4915atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4916{
4917 return atomic_compare_exchange_strong(__p, __v, __w);
4918}
4919
4920template <class _Tp>
4921inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004922_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004923bool
4924atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4925 shared_ptr<_Tp> __w, memory_order, memory_order)
4926{
4927 return atomic_compare_exchange_strong(__p, __v, __w);
4928}
4929
4930template <class _Tp>
4931inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004932_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004933bool
4934atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4935 shared_ptr<_Tp> __w, memory_order, memory_order)
4936{
4937 return atomic_compare_exchange_weak(__p, __v, __w);
4938}
4939
Eric Fiselier9b492672016-06-18 02:12:53 +00004940#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004941
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004942//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004943#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4944# ifndef _LIBCPP_CXX03_LANG
4945enum class pointer_safety : unsigned char {
4946 relaxed,
4947 preferred,
4948 strict
4949};
4950# endif
4951#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004952struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004953{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004954 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004955 {
4956 relaxed,
4957 preferred,
4958 strict
4959 };
4960
Howard Hinnant49e145e2012-10-30 19:06:59 +00004961 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004962
Howard Hinnant756c69b2010-09-22 16:48:34 +00004963 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004964 pointer_safety() : __v_() {}
4965
4966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004967 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004969 operator int() const {return __v_;}
4970};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004971#endif
4972
4973#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004974 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004975_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4976#else
4977// This function is only offered in C++03 under ABI v1.
4978# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4979inline _LIBCPP_INLINE_VISIBILITY
4980pointer_safety get_pointer_safety() _NOEXCEPT {
4981 return pointer_safety::relaxed;
4982}
4983# endif
4984#endif
4985
Howard Hinnantc51e1022010-05-11 19:42:16 +00004986
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004987_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4988_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4989_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004990_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004991
4992template <class _Tp>
4993inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004994_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004995undeclare_reachable(_Tp* __p)
4996{
4997 return static_cast<_Tp*>(__undeclare_reachable(__p));
4998}
4999
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005000_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005001
Marshall Clow8982dcd2015-07-13 20:04:56 +00005002// --- Helper for container swap --
5003template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005004inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005005void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5006#if _LIBCPP_STD_VER >= 14
5007 _NOEXCEPT
5008#else
5009 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5010#endif
5011{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005012 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005013 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5014}
5015
5016template <typename _Alloc>
5017_LIBCPP_INLINE_VISIBILITY
5018void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5019#if _LIBCPP_STD_VER >= 14
5020 _NOEXCEPT
5021#else
5022 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5023#endif
5024{
5025 using _VSTD::swap;
5026 swap(__a1, __a2);
5027}
5028
5029template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005030inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005031void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5032
Marshall Clowff91de82015-08-18 19:51:37 +00005033template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005034struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005035 _Traits::propagate_on_container_move_assignment::value
5036#if _LIBCPP_STD_VER > 14
5037 || _Traits::is_always_equal::value
5038#else
5039 && is_nothrow_move_assignable<_Alloc>::value
5040#endif
5041 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005042
Marshall Clowa591b9a2016-07-11 21:38:08 +00005043
5044#ifndef _LIBCPP_HAS_NO_VARIADICS
5045template <class _Tp, class _Alloc>
5046struct __temp_value {
5047 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005048
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005049 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005050 _Alloc &__a;
5051
5052 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5053 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005054
Marshall Clowa591b9a2016-07-11 21:38:08 +00005055 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005056 _LIBCPP_NO_CFI
5057 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5058 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5059 _VSTD::forward<_Args>(__args)...);
5060 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005061
Marshall Clowa591b9a2016-07-11 21:38:08 +00005062 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5063 };
5064#endif
5065
Marshall Clowe46031a2018-07-02 18:41:15 +00005066template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005067struct __is_allocator : false_type {};
5068
5069template<typename _Alloc>
5070struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005071 typename __void_t<typename _Alloc::value_type>::type,
5072 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5073 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005074 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005075
Eric Fiselier74ebee62019-06-08 01:31:19 +00005076// __builtin_new_allocator -- A non-templated helper for allocating and
5077// deallocating memory using __builtin_operator_new and
5078// __builtin_operator_delete. It should be used in preference to
5079// `std::allocator<T>` to avoid additional instantiations.
5080struct __builtin_new_allocator {
5081 struct __builtin_new_deleter {
5082 typedef void* pointer_type;
5083
5084 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5085 : __size_(__size), __align_(__align) {}
5086
5087 void operator()(void* p) const _NOEXCEPT {
5088 std::__libcpp_deallocate(p, __size_, __align_);
5089 }
5090
5091 private:
5092 size_t __size_;
5093 size_t __align_;
5094 };
5095
5096 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5097
5098 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5099 return __holder_t(std::__libcpp_allocate(__s, __align),
5100 __builtin_new_deleter(__s, __align));
5101 }
5102
5103 static void __deallocate_bytes(void* __p, size_t __s,
5104 size_t __align) _NOEXCEPT {
5105 std::__libcpp_deallocate(__p, __s, __align);
5106 }
5107
5108 template <class _Tp>
5109 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5110 static __holder_t __allocate_type(size_t __n) {
5111 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5112 }
5113
5114 template <class _Tp>
5115 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5116 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5117 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5118 }
5119};
5120
5121
Howard Hinnantc51e1022010-05-11 19:42:16 +00005122_LIBCPP_END_NAMESPACE_STD
5123
Eric Fiselierf4433a32017-05-31 22:07:49 +00005124_LIBCPP_POP_MACROS
5125
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005126#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005127# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005128#endif
5129
Howard Hinnantc51e1022010-05-11 19:42:16 +00005130#endif // _LIBCPP_MEMORY