blob: 29c5e98b3b7cd734bca898bb373b842ad526c68b [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:
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef size_t size_type; // deprecated in C++17, removed in C++20
119 typedef ptrdiff_t difference_type; // deprecated in C++17, removed in C++20
120 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000341 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
1309#ifndef _LIBCPP_HAS_NO_VARIADICS
1310
1311template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1312struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1313{
Michael Park99f9e912020-03-04 11:27:14 -05001314 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001315 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001316 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317};
1318
1319template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1320struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1321{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001322 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323};
1324
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001325#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326
1327template <template <class> class _Alloc, class _Tp, class _Up>
1328struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1329{
1330 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1331};
1332
1333template <template <class> class _Alloc, class _Tp, class _Up>
1334struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1335{
1336 typedef _Alloc<_Up> type;
1337};
1338
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1340struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1341{
1342 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1343};
1344
1345template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1346struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1347{
1348 typedef _Alloc<_Up, _A0> type;
1349};
1350
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1352 class _A1, class _Up>
1353struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1354{
1355 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1356};
1357
1358template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1359 class _A1, class _Up>
1360struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1361{
1362 typedef _Alloc<_Up, _A0, _A1> type;
1363};
1364
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1366 class _A1, class _A2, class _Up>
1367struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1368{
1369 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1370};
1371
1372template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1373 class _A1, class _A2, class _Up>
1374struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1375{
1376 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1377};
1378
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001379#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001381#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382
Michael Park99f9e912020-03-04 11:27:14 -05001383_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1385auto
1386__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001387 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001388_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389
1390template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1391auto
1392__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1393 -> false_type;
1394
1395template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1396struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001397 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1398 declval<_SizeType>(),
1399 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400{
1401};
1402
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001403#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404
1405template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1406struct __has_allocate_hint
1407 : true_type
1408{
1409};
1410
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001411#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001413#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414
Michael Park99f9e912020-03-04 11:27:14 -05001415_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1416template <class _Alloc, class _Tp, class... _Args>
1417auto
1418__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&&... __args)
1419 -> decltype(__a.construct(__p, _VSTD::forward<_Args>(__args)...), true_type());
1420_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421
1422template <class _Alloc, class _Pointer, class ..._Args>
Michael Park99f9e912020-03-04 11:27:14 -05001423auto
1424__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args)
1425 -> false_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426
1427template <class _Alloc, class _Pointer, class ..._Args>
1428struct __has_construct
Michael Park99f9e912020-03-04 11:27:14 -05001429 : decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
1430 declval<_Pointer>(),
1431 declval<_Args>()...))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432{
1433};
1434
Michael Park99f9e912020-03-04 11:27:14 -05001435_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436template <class _Alloc, class _Pointer>
1437auto
1438__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1439 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001440_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441
1442template <class _Alloc, class _Pointer>
1443auto
1444__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1445 -> false_type;
1446
1447template <class _Alloc, class _Pointer>
1448struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001449 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1450 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451{
1452};
1453
Michael Park99f9e912020-03-04 11:27:14 -05001454_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455template <class _Alloc>
1456auto
1457__has_max_size_test(_Alloc&& __a)
1458 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001459_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460
1461template <class _Alloc>
1462auto
1463__has_max_size_test(const volatile _Alloc& __a)
1464 -> false_type;
1465
1466template <class _Alloc>
1467struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001468 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469{
1470};
1471
1472template <class _Alloc>
1473auto
1474__has_select_on_container_copy_construction_test(_Alloc&& __a)
1475 -> decltype(__a.select_on_container_copy_construction(), true_type());
1476
1477template <class _Alloc>
1478auto
1479__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1480 -> false_type;
1481
1482template <class _Alloc>
1483struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001484 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485{
1486};
1487
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001488#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001490template <class _Alloc, class _Pointer, class _Tp, class = void>
1491struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001493template <class _Alloc, class _Pointer, class _Tp>
1494struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1495 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1496>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001498template <class _Alloc, class _Pointer, class = void>
1499struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500
1501template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001502struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1503 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1504>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505
1506template <class _Alloc>
1507struct __has_max_size
1508 : true_type
1509{
1510};
1511
1512template <class _Alloc>
1513struct __has_select_on_container_copy_construction
1514 : false_type
1515{
1516};
1517
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001518#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001520template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1521struct __alloc_traits_difference_type
1522{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001523 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001524};
1525
1526template <class _Alloc, class _Ptr>
1527struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1528{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001529 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001530};
1531
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001532template <class _Tp>
1533struct __is_default_allocator : false_type {};
1534
1535template <class _Tp>
1536struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1537
Eric Fiselier909fe962019-09-13 16:09:33 +00001538
1539
1540template <class _Alloc,
1541 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1542 >
1543struct __is_cpp17_move_insertable;
1544template <class _Alloc>
1545struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1546template <class _Alloc>
1547struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1548
1549template <class _Alloc,
1550 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1551 >
1552struct __is_cpp17_copy_insertable;
1553template <class _Alloc>
1554struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1555template <class _Alloc>
1556struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1557 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1558 __is_cpp17_move_insertable<_Alloc>::value>
1559 {};
1560
1561
1562
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001564struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565{
1566 typedef _Alloc allocator_type;
1567 typedef typename allocator_type::value_type value_type;
1568
1569 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1570 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1571 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1572 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1573
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001574 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1575 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576
1577 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1578 propagate_on_container_copy_assignment;
1579 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1580 propagate_on_container_move_assignment;
1581 typedef typename __propagate_on_container_swap<allocator_type>::type
1582 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001583 typedef typename __is_always_equal<allocator_type>::type
1584 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001586#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001588 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001589 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001590#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 template <class _Tp> struct rebind_alloc
1592 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1593 template <class _Tp> struct rebind_traits
1594 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001595#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596
Marshall Clow0e58cae2017-11-26 02:55:38 +00001597 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 static pointer allocate(allocator_type& __a, size_type __n)
1599 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001600 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001602 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1604
Howard Hinnant756c69b2010-09-22 16:48:34 +00001605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001606 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 {__a.deallocate(__p, __n);}
1608
1609#ifndef _LIBCPP_HAS_NO_VARIADICS
1610 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001613 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001614 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001615#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001617 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001618 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619 {
1620 ::new ((void*)__p) _Tp();
1621 }
1622 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001623 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001624 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001626 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1627 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628 }
1629 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001630 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001631 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632 const _A1& __a1)
1633 {
1634 ::new ((void*)__p) _Tp(__a0, __a1);
1635 }
1636 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001637 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001638 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 const _A1& __a1, const _A2& __a2)
1640 {
1641 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1642 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001643#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644
1645 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647 static void destroy(allocator_type& __a, _Tp* __p)
1648 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1649
Howard Hinnant756c69b2010-09-22 16:48:34 +00001650 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001651 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1653
Howard Hinnant756c69b2010-09-22 16:48:34 +00001654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655 static allocator_type
1656 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001657 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 __has_select_on_container_copy_construction<const allocator_type>(),
1659 __a);}
1660
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001661 template <class _Ptr>
1662 _LIBCPP_INLINE_VISIBILITY
1663 static
1664 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001665 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001666 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001667 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1668 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001669 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001670 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001671#ifdef _LIBCPP_NO_EXCEPTIONS
1672 _VSTD::move(*__begin1)
1673#else
1674 _VSTD::move_if_noexcept(*__begin1)
1675#endif
1676 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 }
1678
1679 template <class _Tp>
1680 _LIBCPP_INLINE_VISIBILITY
1681 static
1682 typename enable_if
1683 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001684 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001685 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1686 is_trivially_move_constructible<_Tp>::value,
1687 void
1688 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001689 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001690 {
1691 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001692 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001693 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001694 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001695 __begin2 += _Np;
1696 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001697 }
1698
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001699 template <class _Iter, class _Ptr>
1700 _LIBCPP_INLINE_VISIBILITY
1701 static
1702 void
1703 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1704 {
1705 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001706 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001707 }
1708
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001709 template <class _SourceTp, class _DestTp,
1710 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1711 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001712 _LIBCPP_INLINE_VISIBILITY
1713 static
1714 typename enable_if
1715 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001716 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001717 is_same<_RawSourceTp, _RawDestTp>::value &&
1718 (__is_default_allocator<allocator_type>::value ||
1719 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001720 void
1721 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001722 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001723 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001724 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001725 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001726 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001727 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001728 __begin2 += _Np;
1729 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001730 }
1731
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001732 template <class _Ptr>
1733 _LIBCPP_INLINE_VISIBILITY
1734 static
1735 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001736 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001737 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001738 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1739 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001740 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001741 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001742 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001743#ifdef _LIBCPP_NO_EXCEPTIONS
1744 _VSTD::move(*--__end1)
1745#else
1746 _VSTD::move_if_noexcept(*--__end1)
1747#endif
1748 );
1749 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001750 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001751 }
1752
1753 template <class _Tp>
1754 _LIBCPP_INLINE_VISIBILITY
1755 static
1756 typename enable_if
1757 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001758 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001759 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1760 is_trivially_move_constructible<_Tp>::value,
1761 void
1762 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001763 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001764 {
1765 ptrdiff_t _Np = __end1 - __begin1;
1766 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001767 if (_Np > 0)
1768 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001769 }
1770
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771private:
1772
Howard Hinnant756c69b2010-09-22 16:48:34 +00001773 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001774 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001776 {
1777 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1778 return __a.allocate(__n, __hint);
1779 _LIBCPP_SUPPRESS_DEPRECATED_POP
1780 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001781 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001782 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001783 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 {return __a.allocate(__n);}
1785
1786#ifndef _LIBCPP_HAS_NO_VARIADICS
1787 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001790 {
1791 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1792 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1793 _LIBCPP_SUPPRESS_DEPRECATED_POP
1794 }
1795
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1799 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001800 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001802#else // _LIBCPP_HAS_NO_VARIADICS
1803 template <class _Tp, class _A0>
1804 _LIBCPP_INLINE_VISIBILITY
1805 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1806 const _A0& __a0)
1807 {__a.construct(__p, __a0);}
1808 template <class _Tp, class _A0>
1809 _LIBCPP_INLINE_VISIBILITY
1810 static void __construct(false_type, allocator_type&, _Tp* __p,
1811 const _A0& __a0)
1812 {
1813 ::new ((void*)__p) _Tp(__a0);
1814 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001815#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816
1817 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001820 {
1821 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1822 __a.destroy(__p);
1823 _LIBCPP_SUPPRESS_DEPRECATED_POP
1824 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827 static void __destroy(false_type, allocator_type&, _Tp* __p)
1828 {
1829 __p->~_Tp();
1830 }
1831
Howard Hinnant756c69b2010-09-22 16:48:34 +00001832 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001833 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001834 {
1835 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1836 return __a.max_size();
1837 _LIBCPP_SUPPRESS_DEPRECATED_POP
1838 }
1839
Howard Hinnant756c69b2010-09-22 16:48:34 +00001840 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001841 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001842 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843
Howard Hinnant756c69b2010-09-22 16:48:34 +00001844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001846 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001850 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 {return __a;}
1852};
1853
Marshall Clow940e01c2015-04-07 05:21:38 +00001854template <class _Traits, class _Tp>
1855struct __rebind_alloc_helper
1856{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001857#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001858 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001859#else
1860 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1861#endif
1862};
1863
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864// allocator
1865
1866template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001867class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868{
1869public:
Michael Park99f9e912020-03-04 11:27:14 -05001870#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1871 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1872 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1873 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1874 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1875 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1876 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1877
1878 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1879#endif
1880
1881 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882
Howard Hinnant4931e092011-06-02 21:38:57 +00001883 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001884 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001885
Marshall Clowbc759762018-03-20 23:02:53 +00001886 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1887 allocator() _NOEXCEPT {}
1888
Louis Dionne481a2662018-09-23 18:35:00 +00001889 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001890 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1891 allocator(const allocator<_Up>&) _NOEXCEPT {}
1892
Michael Park99f9e912020-03-04 11:27:14 -05001893#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1894 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1895 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001896 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001897 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1898 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001899 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001900#endif
1901
1902 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001903 {
Michael Park99f9e912020-03-04 11:27:14 -05001904 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1905 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001906 __throw_length_error("allocator<T>::allocate(size_t n)"
1907 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001908 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001909 }
Michael Park99f9e912020-03-04 11:27:14 -05001910
1911#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1912 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1913 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1914#endif
1915
1916 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001917 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001918
1919#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1920 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001921 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001922
Howard Hinnant74279a52010-09-04 23:28:19 +00001923#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001925 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 void
1927 construct(_Up* __p, _Args&&... __args)
1928 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001929 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001931#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932 _LIBCPP_INLINE_VISIBILITY
1933 void
1934 construct(pointer __p)
1935 {
1936 ::new((void*)__p) _Tp();
1937 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001938# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001939
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 template <class _A0>
1941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001942 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943 construct(pointer __p, _A0& __a0)
1944 {
1945 ::new((void*)__p) _Tp(__a0);
1946 }
1947 template <class _A0>
1948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001949 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950 construct(pointer __p, const _A0& __a0)
1951 {
1952 ::new((void*)__p) _Tp(__a0);
1953 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001954# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 template <class _A0, class _A1>
1956 _LIBCPP_INLINE_VISIBILITY
1957 void
1958 construct(pointer __p, _A0& __a0, _A1& __a1)
1959 {
1960 ::new((void*)__p) _Tp(__a0, __a1);
1961 }
1962 template <class _A0, class _A1>
1963 _LIBCPP_INLINE_VISIBILITY
1964 void
1965 construct(pointer __p, const _A0& __a0, _A1& __a1)
1966 {
1967 ::new((void*)__p) _Tp(__a0, __a1);
1968 }
1969 template <class _A0, class _A1>
1970 _LIBCPP_INLINE_VISIBILITY
1971 void
1972 construct(pointer __p, _A0& __a0, const _A1& __a1)
1973 {
1974 ::new((void*)__p) _Tp(__a0, __a1);
1975 }
1976 template <class _A0, class _A1>
1977 _LIBCPP_INLINE_VISIBILITY
1978 void
1979 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1980 {
1981 ::new((void*)__p) _Tp(__a0, __a1);
1982 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001983#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05001984 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1985#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986};
1987
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001988template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001989class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001990{
1991public:
Michael Park99f9e912020-03-04 11:27:14 -05001992#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1993 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1994 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1995 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1996 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1997 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1998 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1999
2000 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
2001#endif
2002
2003 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002004
2005 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00002006 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002007
Marshall Clowbc759762018-03-20 23:02:53 +00002008 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2009 allocator() _NOEXCEPT {}
2010
2011 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00002012 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00002013 allocator(const allocator<_Up>&) _NOEXCEPT {}
2014
Michael Park99f9e912020-03-04 11:27:14 -05002015#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2016 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
2017 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002018 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05002019#endif
2020
2021 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00002022 {
Michael Park99f9e912020-03-04 11:27:14 -05002023 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
2024 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00002025 __throw_length_error("allocator<const T>::allocate(size_t n)"
2026 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05002027 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00002028 }
Michael Park99f9e912020-03-04 11:27:14 -05002029
2030#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2031 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
2032 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
2033#endif
2034
2035 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002036 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05002037
2038#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2039 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002040 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05002041
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002042#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2043 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05002044 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002045 void
2046 construct(_Up* __p, _Args&&... __args)
2047 {
2048 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
2049 }
2050#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2051 _LIBCPP_INLINE_VISIBILITY
2052 void
2053 construct(pointer __p)
2054 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002055 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002056 }
2057# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00002058
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002059 template <class _A0>
2060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002061 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002062 construct(pointer __p, _A0& __a0)
2063 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002064 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002065 }
2066 template <class _A0>
2067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002068 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002069 construct(pointer __p, const _A0& __a0)
2070 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002071 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002072 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002073# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
2074 template <class _A0, class _A1>
2075 _LIBCPP_INLINE_VISIBILITY
2076 void
2077 construct(pointer __p, _A0& __a0, _A1& __a1)
2078 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002079 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002080 }
2081 template <class _A0, class _A1>
2082 _LIBCPP_INLINE_VISIBILITY
2083 void
2084 construct(pointer __p, const _A0& __a0, _A1& __a1)
2085 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002086 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002087 }
2088 template <class _A0, class _A1>
2089 _LIBCPP_INLINE_VISIBILITY
2090 void
2091 construct(pointer __p, _A0& __a0, const _A1& __a1)
2092 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002093 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002094 }
2095 template <class _A0, class _A1>
2096 _LIBCPP_INLINE_VISIBILITY
2097 void
2098 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2099 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002100 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002101 }
2102#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05002103 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2104#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002105};
2106
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107template <class _Tp, class _Up>
2108inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002109bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110
2111template <class _Tp, class _Up>
2112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002113bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114
2115template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002116class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117 : public iterator<output_iterator_tag,
2118 _Tp, // purposefully not C++03
2119 ptrdiff_t, // purposefully not C++03
2120 _Tp*, // purposefully not C++03
2121 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2122{
2123private:
2124 _OutputIterator __x_;
2125public:
2126 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2127 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2128 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002129 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002130#if _LIBCPP_STD_VER >= 14
2131 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002132 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002133#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2135 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2136 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002137#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002138 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002139#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140};
2141
2142template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002143_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002145get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146{
2147 pair<_Tp*, ptrdiff_t> __r(0, 0);
2148 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2149 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2150 / sizeof(_Tp);
2151 if (__n > __m)
2152 __n = __m;
2153 while (__n > 0)
2154 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002155#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002156 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002157 {
2158 std::align_val_t __al =
2159 std::align_val_t(std::alignment_of<_Tp>::value);
2160 __r.first = static_cast<_Tp*>(::operator new(
2161 __n * sizeof(_Tp), __al, nothrow));
2162 } else {
2163 __r.first = static_cast<_Tp*>(::operator new(
2164 __n * sizeof(_Tp), nothrow));
2165 }
2166#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002167 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002168 {
2169 // Since aligned operator new is unavailable, return an empty
2170 // buffer rather than one with invalid alignment.
2171 return __r;
2172 }
2173
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002175#endif
2176
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 if (__r.first)
2178 {
2179 __r.second = __n;
2180 break;
2181 }
2182 __n /= 2;
2183 }
2184 return __r;
2185}
2186
2187template <class _Tp>
2188inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002189void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2190{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002191 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002192}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193
Marshall Clowb22274f2017-01-24 22:22:33 +00002194#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002196struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197{
2198 _Tp* __ptr_;
2199};
2200
2201template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002202class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203{
2204private:
2205 _Tp* __ptr_;
2206public:
2207 typedef _Tp element_type;
2208
Dimitry Andric47269ce2020-03-13 19:36:26 +01002209 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
2210 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
2211 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002213 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002215 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002217 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002219 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
Dimitry Andric47269ce2020-03-13 19:36:26 +01002221 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002223 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
2224 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
2225 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226 {
2227 _Tp* __t = __ptr_;
2228 __ptr_ = 0;
2229 return __t;
2230 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01002231 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 {
2233 if (__ptr_ != __p)
2234 delete __ptr_;
2235 __ptr_ = __p;
2236 }
2237
Dimitry Andric47269ce2020-03-13 19:36:26 +01002238 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
2239 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002241 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 {return auto_ptr<_Up>(release());}
2243};
2244
2245template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002246class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247{
2248public:
2249 typedef void element_type;
2250};
Marshall Clowb22274f2017-01-24 22:22:33 +00002251#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002253// Tag used to default initialize one or both of the pair's elements.
2254struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002255struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002256
Eric Fiselier9d355982017-04-12 23:45:53 +00002257template <class _Tp, int _Idx,
2258 bool _CanBeEmptyBase =
2259 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2260struct __compressed_pair_elem {
2261 typedef _Tp _ParamT;
2262 typedef _Tp& reference;
2263 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002266 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002267 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2268 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269
Eric Fiselier9d355982017-04-12 23:45:53 +00002270 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002271 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2272 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002273 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002274 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002275 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002276 : __value_(_VSTD::forward<_Up>(__u))
2277 {
2278 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002280
2281#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002282 template <class... _Args, size_t... _Indexes>
2283 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2284 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2285 __tuple_indices<_Indexes...>)
2286 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002287#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002289
Alex Lorenz76132112017-11-09 17:54:49 +00002290 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2291 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002292 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002295 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296};
2297
Eric Fiselier9d355982017-04-12 23:45:53 +00002298template <class _Tp, int _Idx>
2299struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2300 typedef _Tp _ParamT;
2301 typedef _Tp& reference;
2302 typedef const _Tp& const_reference;
2303 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002305 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2306 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2307 __compressed_pair_elem(__default_init_tag) {}
2308 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2309 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310
Eric Fiselier9d355982017-04-12 23:45:53 +00002311 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002312 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2313 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002314 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002315 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002316 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002317 : __value_type(_VSTD::forward<_Up>(__u))
2318 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002320#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002321 template <class... _Args, size_t... _Indexes>
2322 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2323 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2324 __tuple_indices<_Indexes...>)
2325 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002326#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327
Alex Lorenz76132112017-11-09 17:54:49 +00002328 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2329 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002330 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331};
2332
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002334class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2335 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002336 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2337 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002338
2339 // NOTE: This static assert should never fire because __compressed_pair
2340 // is *almost never* used in a scenario where it's possible for T1 == T2.
2341 // (The exception is std::function where it is possible that the function
2342 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002343 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002344 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2345 "The current implementation is NOT ABI-compatible with the previous "
2346 "implementation for this configuration");
2347
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002349 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002350 class = typename enable_if<
2351 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2352 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2353 >::type
2354 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002355 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002356 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357
Eric Fiselier9d355982017-04-12 23:45:53 +00002358 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002359 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002360 __compressed_pair(_U1&& __t1, _U2&& __t2)
2361 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002363#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002364 template <class... _Args1, class... _Args2>
2365 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2366 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2367 tuple<_Args2...> __second_args)
2368 : _Base1(__pc, _VSTD::move(__first_args),
2369 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2370 _Base2(__pc, _VSTD::move(__second_args),
2371 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002372#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373
Eric Fiselier9d355982017-04-12 23:45:53 +00002374 _LIBCPP_INLINE_VISIBILITY
2375 typename _Base1::reference first() _NOEXCEPT {
2376 return static_cast<_Base1&>(*this).__get();
2377 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378
Eric Fiselier9d355982017-04-12 23:45:53 +00002379 _LIBCPP_INLINE_VISIBILITY
2380 typename _Base1::const_reference first() const _NOEXCEPT {
2381 return static_cast<_Base1 const&>(*this).__get();
2382 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383
Eric Fiselier9d355982017-04-12 23:45:53 +00002384 _LIBCPP_INLINE_VISIBILITY
2385 typename _Base2::reference second() _NOEXCEPT {
2386 return static_cast<_Base2&>(*this).__get();
2387 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002388
Eric Fiselier9d355982017-04-12 23:45:53 +00002389 _LIBCPP_INLINE_VISIBILITY
2390 typename _Base2::const_reference second() const _NOEXCEPT {
2391 return static_cast<_Base2 const&>(*this).__get();
2392 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393
Eric Fiselier9d355982017-04-12 23:45:53 +00002394 _LIBCPP_INLINE_VISIBILITY
2395 void swap(__compressed_pair& __x)
2396 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2397 __is_nothrow_swappable<_T2>::value)
2398 {
2399 using std::swap;
2400 swap(first(), __x.first());
2401 swap(second(), __x.second());
2402 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403};
2404
2405template <class _T1, class _T2>
2406inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002407void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2408 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2409 __is_nothrow_swappable<_T2>::value) {
2410 __x.swap(__y);
2411}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002413// default_delete
2414
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002416struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002417 static_assert(!is_function<_Tp>::value,
2418 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002419#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002420 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002421#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002422 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002423#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002424 template <class _Up>
2425 _LIBCPP_INLINE_VISIBILITY
2426 default_delete(const default_delete<_Up>&,
2427 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2428 0) _NOEXCEPT {}
2429
2430 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2431 static_assert(sizeof(_Tp) > 0,
2432 "default_delete can not delete incomplete type");
2433 static_assert(!is_void<_Tp>::value,
2434 "default_delete can not delete incomplete type");
2435 delete __ptr;
2436 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437};
2438
2439template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002440struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2441private:
2442 template <class _Up>
2443 struct _EnableIfConvertible
2444 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2445
Howard Hinnant4500ca52011-12-18 21:19:44 +00002446public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002447#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002448 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002449#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002450 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002451#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002452
2453 template <class _Up>
2454 _LIBCPP_INLINE_VISIBILITY
2455 default_delete(const default_delete<_Up[]>&,
2456 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2457
2458 template <class _Up>
2459 _LIBCPP_INLINE_VISIBILITY
2460 typename _EnableIfConvertible<_Up>::type
2461 operator()(_Up* __ptr) const _NOEXCEPT {
2462 static_assert(sizeof(_Tp) > 0,
2463 "default_delete can not delete incomplete type");
2464 static_assert(!is_void<_Tp>::value,
2465 "default_delete can not delete void type");
2466 delete[] __ptr;
2467 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468};
2469
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002470template <class _Deleter>
2471struct __unique_ptr_deleter_sfinae {
2472 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2473 typedef const _Deleter& __lval_ref_type;
2474 typedef _Deleter&& __good_rval_ref_type;
2475 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476};
2477
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002478template <class _Deleter>
2479struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2480 typedef const _Deleter& __lval_ref_type;
2481 typedef const _Deleter&& __bad_rval_ref_type;
2482 typedef false_type __enable_rval_overload;
2483};
2484
2485template <class _Deleter>
2486struct __unique_ptr_deleter_sfinae<_Deleter&> {
2487 typedef _Deleter& __lval_ref_type;
2488 typedef _Deleter&& __bad_rval_ref_type;
2489 typedef false_type __enable_rval_overload;
2490};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002491
2492template <class _Tp, class _Dp = default_delete<_Tp> >
2493class _LIBCPP_TEMPLATE_VIS unique_ptr {
2494public:
2495 typedef _Tp element_type;
2496 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002497 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002498
2499 static_assert(!is_rvalue_reference<deleter_type>::value,
2500 "the specified deleter type cannot be an rvalue reference");
2501
2502private:
2503 __compressed_pair<pointer, deleter_type> __ptr_;
2504
2505 struct __nat { int __for_bool_; };
2506
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002507 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002508
2509 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002510 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002511 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002512
2513 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002514 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002515 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002516
2517 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002518 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002519 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002520
2521 template <bool _Dummy, class _Deleter = typename __dependent_type<
2522 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002523 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002524 typename enable_if<is_default_constructible<_Deleter>::value &&
2525 !is_pointer<_Deleter>::value>::type;
2526
2527 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002528 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002529 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2530
2531 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002532 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002533 is_convertible<typename _UPtr::pointer, pointer>::value &&
2534 !is_array<_Up>::value
2535 >::type;
2536
2537 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002538 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002539 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2540 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2541 >::type;
2542
2543 template <class _UDel>
2544 using _EnableIfDeleterAssignable = typename enable_if<
2545 is_assignable<_Dp&, _UDel&&>::value
2546 >::type;
2547
2548public:
2549 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002550 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002551 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002552 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002553
2554 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002555 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002556 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002557 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002558
2559 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002560 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002561 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002562 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002563
2564 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002565 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002566 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002567 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002568 : __ptr_(__p, __d) {}
2569
2570 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002571 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002572 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002573 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002574 : __ptr_(__p, _VSTD::move(__d)) {
2575 static_assert(!is_reference<deleter_type>::value,
2576 "rvalue deleter bound to reference");
2577 }
2578
2579 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002580 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002581 _LIBCPP_INLINE_VISIBILITY
2582 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2583
2584 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002585 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002586 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2587 }
2588
2589 template <class _Up, class _Ep,
2590 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2591 class = _EnableIfDeleterConvertible<_Ep>
2592 >
2593 _LIBCPP_INLINE_VISIBILITY
2594 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2595 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2596
2597#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2598 template <class _Up>
2599 _LIBCPP_INLINE_VISIBILITY
2600 unique_ptr(auto_ptr<_Up>&& __p,
2601 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002602 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002603 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002604 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002605#endif
2606
2607 _LIBCPP_INLINE_VISIBILITY
2608 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2609 reset(__u.release());
2610 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2611 return *this;
2612 }
2613
2614 template <class _Up, class _Ep,
2615 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2616 class = _EnableIfDeleterAssignable<_Ep>
2617 >
2618 _LIBCPP_INLINE_VISIBILITY
2619 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2620 reset(__u.release());
2621 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2622 return *this;
2623 }
2624
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002625#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2626 template <class _Up>
2627 _LIBCPP_INLINE_VISIBILITY
2628 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2629 is_same<_Dp, default_delete<_Tp> >::value,
2630 unique_ptr&>::type
2631 operator=(auto_ptr<_Up> __p) {
2632 reset(__p.release());
2633 return *this;
2634 }
2635#endif
2636
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002637#ifdef _LIBCPP_CXX03_LANG
2638 unique_ptr(unique_ptr const&) = delete;
2639 unique_ptr& operator=(unique_ptr const&) = delete;
2640#endif
2641
2642
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002643 _LIBCPP_INLINE_VISIBILITY
2644 ~unique_ptr() { reset(); }
2645
2646 _LIBCPP_INLINE_VISIBILITY
2647 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2648 reset();
2649 return *this;
2650 }
2651
2652 _LIBCPP_INLINE_VISIBILITY
2653 typename add_lvalue_reference<_Tp>::type
2654 operator*() const {
2655 return *__ptr_.first();
2656 }
2657 _LIBCPP_INLINE_VISIBILITY
2658 pointer operator->() const _NOEXCEPT {
2659 return __ptr_.first();
2660 }
2661 _LIBCPP_INLINE_VISIBILITY
2662 pointer get() const _NOEXCEPT {
2663 return __ptr_.first();
2664 }
2665 _LIBCPP_INLINE_VISIBILITY
2666 deleter_type& get_deleter() _NOEXCEPT {
2667 return __ptr_.second();
2668 }
2669 _LIBCPP_INLINE_VISIBILITY
2670 const deleter_type& get_deleter() const _NOEXCEPT {
2671 return __ptr_.second();
2672 }
2673 _LIBCPP_INLINE_VISIBILITY
2674 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2675 return __ptr_.first() != nullptr;
2676 }
2677
2678 _LIBCPP_INLINE_VISIBILITY
2679 pointer release() _NOEXCEPT {
2680 pointer __t = __ptr_.first();
2681 __ptr_.first() = pointer();
2682 return __t;
2683 }
2684
2685 _LIBCPP_INLINE_VISIBILITY
2686 void reset(pointer __p = pointer()) _NOEXCEPT {
2687 pointer __tmp = __ptr_.first();
2688 __ptr_.first() = __p;
2689 if (__tmp)
2690 __ptr_.second()(__tmp);
2691 }
2692
2693 _LIBCPP_INLINE_VISIBILITY
2694 void swap(unique_ptr& __u) _NOEXCEPT {
2695 __ptr_.swap(__u.__ptr_);
2696 }
2697};
2698
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002699
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002701class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002703 typedef _Tp element_type;
2704 typedef _Dp deleter_type;
2705 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2706
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002708 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709
Eric Fiselier31127cd2017-04-16 02:14:31 +00002710 template <class _From>
2711 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712
Eric Fiselier31127cd2017-04-16 02:14:31 +00002713 template <class _FromElem>
2714 struct _CheckArrayPointerConversion<_FromElem*>
2715 : integral_constant<bool,
2716 is_same<_FromElem*, pointer>::value ||
2717 (is_same<pointer, element_type*>::value &&
2718 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2719 >
2720 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721
Eric Fiselier31127cd2017-04-16 02:14:31 +00002722 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002723
2724 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002725 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002726 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727
2728 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002729 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002730 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002731
2732 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002733 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002734 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002735
2736 template <bool _Dummy, class _Deleter = typename __dependent_type<
2737 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002738 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002739 typename enable_if<is_default_constructible<_Deleter>::value &&
2740 !is_pointer<_Deleter>::value>::type;
2741
2742 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002743 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2745
2746 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002747 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002748 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002749 >::type;
2750
2751 template <class _UPtr, class _Up,
2752 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002753 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754 is_array<_Up>::value &&
2755 is_same<pointer, element_type*>::value &&
2756 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2757 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2758 >::type;
2759
2760 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002761 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002762 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2763 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2764 >::type;
2765
2766 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002767 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002768 is_assignable<_Dp&, _UDel&&>::value
2769 >::type;
2770
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002772 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002773 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002774 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002775 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002777 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002778 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002779 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002780 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002782 template <class _Pp, bool _Dummy = true,
2783 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002784 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002785 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002786 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002787 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002789 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002790 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2791 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002792 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002793 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002794 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002796 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002797 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002799 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002800 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002802 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002803 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2804 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002805 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002806 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002807 : __ptr_(__p, _VSTD::move(__d)) {
2808 static_assert(!is_reference<deleter_type>::value,
2809 "rvalue deleter bound to reference");
2810 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002812 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002813 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002814 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002815 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002816 : __ptr_(nullptr, _VSTD::move(__d)) {
2817 static_assert(!is_reference<deleter_type>::value,
2818 "rvalue deleter bound to reference");
2819 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002820
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002821 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002822 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2823 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002824 _LIBCPP_INLINE_VISIBILITY
2825 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002826
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002827 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002828 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002829 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2830 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002831
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002833 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002834 reset(__u.release());
2835 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2836 return *this;
2837 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002839 template <class _Up, class _Ep,
2840 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2841 class = _EnableIfDeleterConvertible<_Ep>
2842 >
2843 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002844 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002845 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002846 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002848 template <class _Up, class _Ep,
2849 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2850 class = _EnableIfDeleterAssignable<_Ep>
2851 >
2852 _LIBCPP_INLINE_VISIBILITY
2853 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002854 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002855 reset(__u.release());
2856 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2857 return *this;
2858 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002860#ifdef _LIBCPP_CXX03_LANG
2861 unique_ptr(unique_ptr const&) = delete;
2862 unique_ptr& operator=(unique_ptr const&) = delete;
2863#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002864
2865public:
2866 _LIBCPP_INLINE_VISIBILITY
2867 ~unique_ptr() { reset(); }
2868
2869 _LIBCPP_INLINE_VISIBILITY
2870 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2871 reset();
2872 return *this;
2873 }
2874
2875 _LIBCPP_INLINE_VISIBILITY
2876 typename add_lvalue_reference<_Tp>::type
2877 operator[](size_t __i) const {
2878 return __ptr_.first()[__i];
2879 }
2880 _LIBCPP_INLINE_VISIBILITY
2881 pointer get() const _NOEXCEPT {
2882 return __ptr_.first();
2883 }
2884
2885 _LIBCPP_INLINE_VISIBILITY
2886 deleter_type& get_deleter() _NOEXCEPT {
2887 return __ptr_.second();
2888 }
2889
2890 _LIBCPP_INLINE_VISIBILITY
2891 const deleter_type& get_deleter() const _NOEXCEPT {
2892 return __ptr_.second();
2893 }
2894 _LIBCPP_INLINE_VISIBILITY
2895 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2896 return __ptr_.first() != nullptr;
2897 }
2898
2899 _LIBCPP_INLINE_VISIBILITY
2900 pointer release() _NOEXCEPT {
2901 pointer __t = __ptr_.first();
2902 __ptr_.first() = pointer();
2903 return __t;
2904 }
2905
2906 template <class _Pp>
2907 _LIBCPP_INLINE_VISIBILITY
2908 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002909 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002910 >::type
2911 reset(_Pp __p) _NOEXCEPT {
2912 pointer __tmp = __ptr_.first();
2913 __ptr_.first() = __p;
2914 if (__tmp)
2915 __ptr_.second()(__tmp);
2916 }
2917
2918 _LIBCPP_INLINE_VISIBILITY
2919 void reset(nullptr_t = nullptr) _NOEXCEPT {
2920 pointer __tmp = __ptr_.first();
2921 __ptr_.first() = nullptr;
2922 if (__tmp)
2923 __ptr_.second()(__tmp);
2924 }
2925
2926 _LIBCPP_INLINE_VISIBILITY
2927 void swap(unique_ptr& __u) _NOEXCEPT {
2928 __ptr_.swap(__u.__ptr_);
2929 }
2930
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931};
2932
2933template <class _Tp, class _Dp>
2934inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002935typename enable_if<
2936 __is_swappable<_Dp>::value,
2937 void
2938>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002939swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940
2941template <class _T1, class _D1, class _T2, class _D2>
2942inline _LIBCPP_INLINE_VISIBILITY
2943bool
2944operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2945
2946template <class _T1, class _D1, class _T2, class _D2>
2947inline _LIBCPP_INLINE_VISIBILITY
2948bool
2949operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2950
2951template <class _T1, class _D1, class _T2, class _D2>
2952inline _LIBCPP_INLINE_VISIBILITY
2953bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002954operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2955{
2956 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2957 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002958 typedef typename common_type<_P1, _P2>::type _Vp;
2959 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002960}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961
2962template <class _T1, class _D1, class _T2, class _D2>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2966
2967template <class _T1, class _D1, class _T2, class _D2>
2968inline _LIBCPP_INLINE_VISIBILITY
2969bool
2970operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2971
2972template <class _T1, class _D1, class _T2, class _D2>
2973inline _LIBCPP_INLINE_VISIBILITY
2974bool
2975operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2976
Howard Hinnantb17caf92012-02-21 21:02:58 +00002977template <class _T1, class _D1>
2978inline _LIBCPP_INLINE_VISIBILITY
2979bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002980operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002981{
2982 return !__x;
2983}
2984
2985template <class _T1, class _D1>
2986inline _LIBCPP_INLINE_VISIBILITY
2987bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002988operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002989{
2990 return !__x;
2991}
2992
2993template <class _T1, class _D1>
2994inline _LIBCPP_INLINE_VISIBILITY
2995bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002996operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002997{
2998 return static_cast<bool>(__x);
2999}
3000
3001template <class _T1, class _D1>
3002inline _LIBCPP_INLINE_VISIBILITY
3003bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003004operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003005{
3006 return static_cast<bool>(__x);
3007}
3008
3009template <class _T1, class _D1>
3010inline _LIBCPP_INLINE_VISIBILITY
3011bool
3012operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3013{
3014 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3015 return less<_P1>()(__x.get(), nullptr);
3016}
3017
3018template <class _T1, class _D1>
3019inline _LIBCPP_INLINE_VISIBILITY
3020bool
3021operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3022{
3023 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3024 return less<_P1>()(nullptr, __x.get());
3025}
3026
3027template <class _T1, class _D1>
3028inline _LIBCPP_INLINE_VISIBILITY
3029bool
3030operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3031{
3032 return nullptr < __x;
3033}
3034
3035template <class _T1, class _D1>
3036inline _LIBCPP_INLINE_VISIBILITY
3037bool
3038operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3039{
3040 return __x < nullptr;
3041}
3042
3043template <class _T1, class _D1>
3044inline _LIBCPP_INLINE_VISIBILITY
3045bool
3046operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3047{
3048 return !(nullptr < __x);
3049}
3050
3051template <class _T1, class _D1>
3052inline _LIBCPP_INLINE_VISIBILITY
3053bool
3054operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3055{
3056 return !(__x < nullptr);
3057}
3058
3059template <class _T1, class _D1>
3060inline _LIBCPP_INLINE_VISIBILITY
3061bool
3062operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3063{
3064 return !(__x < nullptr);
3065}
3066
3067template <class _T1, class _D1>
3068inline _LIBCPP_INLINE_VISIBILITY
3069bool
3070operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3071{
3072 return !(nullptr < __x);
3073}
3074
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003075#if _LIBCPP_STD_VER > 11
3076
3077template<class _Tp>
3078struct __unique_if
3079{
3080 typedef unique_ptr<_Tp> __unique_single;
3081};
3082
3083template<class _Tp>
3084struct __unique_if<_Tp[]>
3085{
3086 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3087};
3088
3089template<class _Tp, size_t _Np>
3090struct __unique_if<_Tp[_Np]>
3091{
3092 typedef void __unique_array_known_bound;
3093};
3094
3095template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003096inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003097typename __unique_if<_Tp>::__unique_single
3098make_unique(_Args&&... __args)
3099{
3100 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3101}
3102
3103template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003104inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003105typename __unique_if<_Tp>::__unique_array_unknown_bound
3106make_unique(size_t __n)
3107{
3108 typedef typename remove_extent<_Tp>::type _Up;
3109 return unique_ptr<_Tp>(new _Up[__n]());
3110}
3111
3112template<class _Tp, class... _Args>
3113 typename __unique_if<_Tp>::__unique_array_known_bound
3114 make_unique(_Args&&...) = delete;
3115
3116#endif // _LIBCPP_STD_VER > 11
3117
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003118template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003119#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003120struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003121#else
3122struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003123 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003124#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003125{
3126 typedef unique_ptr<_Tp, _Dp> argument_type;
3127 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003128 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003129 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003130 {
3131 typedef typename argument_type::pointer pointer;
3132 return hash<pointer>()(__ptr.get());
3133 }
3134};
3135
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136struct __destruct_n
3137{
3138private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003139 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140
3141 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003142 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003143 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144
3145 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003146 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 {}
3148
Howard Hinnant719bda32011-05-28 14:41:13 +00003149 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003150 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003151 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 {}
3153
Howard Hinnant719bda32011-05-28 14:41:13 +00003154 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003155 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003156 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 {}
3158public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003159 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003160 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161
3162 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003163 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003164 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165
3166 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003167 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003168 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169
3170 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003171 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003172 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173};
3174
3175template <class _Alloc>
3176class __allocator_destructor
3177{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003178 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003180 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3181 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182private:
3183 _Alloc& __alloc_;
3184 size_type __s_;
3185public:
3186 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003187 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003190 void operator()(pointer __p) _NOEXCEPT
3191 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192};
3193
3194template <class _InputIterator, class _ForwardIterator>
3195_ForwardIterator
3196uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3197{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003199#ifndef _LIBCPP_NO_EXCEPTIONS
3200 _ForwardIterator __s = __r;
3201 try
3202 {
3203#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003204 for (; __f != __l; ++__f, (void) ++__r)
3205 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003206#ifndef _LIBCPP_NO_EXCEPTIONS
3207 }
3208 catch (...)
3209 {
3210 for (; __s != __r; ++__s)
3211 __s->~value_type();
3212 throw;
3213 }
3214#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215 return __r;
3216}
3217
3218template <class _InputIterator, class _Size, class _ForwardIterator>
3219_ForwardIterator
3220uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3221{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003223#ifndef _LIBCPP_NO_EXCEPTIONS
3224 _ForwardIterator __s = __r;
3225 try
3226 {
3227#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003228 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3229 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003230#ifndef _LIBCPP_NO_EXCEPTIONS
3231 }
3232 catch (...)
3233 {
3234 for (; __s != __r; ++__s)
3235 __s->~value_type();
3236 throw;
3237 }
3238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239 return __r;
3240}
3241
3242template <class _ForwardIterator, class _Tp>
3243void
3244uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3245{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003247#ifndef _LIBCPP_NO_EXCEPTIONS
3248 _ForwardIterator __s = __f;
3249 try
3250 {
3251#endif
3252 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003253 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003254#ifndef _LIBCPP_NO_EXCEPTIONS
3255 }
3256 catch (...)
3257 {
3258 for (; __s != __f; ++__s)
3259 __s->~value_type();
3260 throw;
3261 }
3262#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263}
3264
3265template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003266_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3268{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003270#ifndef _LIBCPP_NO_EXCEPTIONS
3271 _ForwardIterator __s = __f;
3272 try
3273 {
3274#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003275 for (; __n > 0; ++__f, (void) --__n)
3276 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003277#ifndef _LIBCPP_NO_EXCEPTIONS
3278 }
3279 catch (...)
3280 {
3281 for (; __s != __f; ++__s)
3282 __s->~value_type();
3283 throw;
3284 }
3285#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003286 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003287}
3288
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003289#if _LIBCPP_STD_VER > 14
3290
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003291template <class _Tp>
3292inline _LIBCPP_INLINE_VISIBILITY
3293void destroy_at(_Tp* __loc) {
3294 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3295 __loc->~_Tp();
3296}
3297
3298template <class _ForwardIterator>
3299inline _LIBCPP_INLINE_VISIBILITY
3300void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3301 for (; __first != __last; ++__first)
3302 _VSTD::destroy_at(_VSTD::addressof(*__first));
3303}
3304
3305template <class _ForwardIterator, class _Size>
3306inline _LIBCPP_INLINE_VISIBILITY
3307_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3308 for (; __n > 0; (void)++__first, --__n)
3309 _VSTD::destroy_at(_VSTD::addressof(*__first));
3310 return __first;
3311}
3312
Eric Fiselier290c07c2016-10-11 21:13:44 +00003313template <class _ForwardIterator>
3314inline _LIBCPP_INLINE_VISIBILITY
3315void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3316 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3317 auto __idx = __first;
3318#ifndef _LIBCPP_NO_EXCEPTIONS
3319 try {
3320#endif
3321 for (; __idx != __last; ++__idx)
3322 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3323#ifndef _LIBCPP_NO_EXCEPTIONS
3324 } catch (...) {
3325 _VSTD::destroy(__first, __idx);
3326 throw;
3327 }
3328#endif
3329}
3330
3331template <class _ForwardIterator, class _Size>
3332inline _LIBCPP_INLINE_VISIBILITY
3333_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3334 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3335 auto __idx = __first;
3336#ifndef _LIBCPP_NO_EXCEPTIONS
3337 try {
3338#endif
3339 for (; __n > 0; (void)++__idx, --__n)
3340 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3341 return __idx;
3342#ifndef _LIBCPP_NO_EXCEPTIONS
3343 } catch (...) {
3344 _VSTD::destroy(__first, __idx);
3345 throw;
3346 }
3347#endif
3348}
3349
3350
3351template <class _ForwardIterator>
3352inline _LIBCPP_INLINE_VISIBILITY
3353void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3354 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3355 auto __idx = __first;
3356#ifndef _LIBCPP_NO_EXCEPTIONS
3357 try {
3358#endif
3359 for (; __idx != __last; ++__idx)
3360 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3361#ifndef _LIBCPP_NO_EXCEPTIONS
3362 } catch (...) {
3363 _VSTD::destroy(__first, __idx);
3364 throw;
3365 }
3366#endif
3367}
3368
3369template <class _ForwardIterator, class _Size>
3370inline _LIBCPP_INLINE_VISIBILITY
3371_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3372 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3373 auto __idx = __first;
3374#ifndef _LIBCPP_NO_EXCEPTIONS
3375 try {
3376#endif
3377 for (; __n > 0; (void)++__idx, --__n)
3378 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3379 return __idx;
3380#ifndef _LIBCPP_NO_EXCEPTIONS
3381 } catch (...) {
3382 _VSTD::destroy(__first, __idx);
3383 throw;
3384 }
3385#endif
3386}
3387
3388
3389template <class _InputIt, class _ForwardIt>
3390inline _LIBCPP_INLINE_VISIBILITY
3391_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3392 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3393 auto __idx = __first_res;
3394#ifndef _LIBCPP_NO_EXCEPTIONS
3395 try {
3396#endif
3397 for (; __first != __last; (void)++__idx, ++__first)
3398 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3399 return __idx;
3400#ifndef _LIBCPP_NO_EXCEPTIONS
3401 } catch (...) {
3402 _VSTD::destroy(__first_res, __idx);
3403 throw;
3404 }
3405#endif
3406}
3407
3408template <class _InputIt, class _Size, class _ForwardIt>
3409inline _LIBCPP_INLINE_VISIBILITY
3410pair<_InputIt, _ForwardIt>
3411uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3412 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3413 auto __idx = __first_res;
3414#ifndef _LIBCPP_NO_EXCEPTIONS
3415 try {
3416#endif
3417 for (; __n > 0; ++__idx, (void)++__first, --__n)
3418 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3419 return {__first, __idx};
3420#ifndef _LIBCPP_NO_EXCEPTIONS
3421 } catch (...) {
3422 _VSTD::destroy(__first_res, __idx);
3423 throw;
3424 }
3425#endif
3426}
3427
3428
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003429#endif // _LIBCPP_STD_VER > 14
3430
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003431// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3432// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003433// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003434#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3435 && defined(__ATOMIC_RELAXED) \
3436 && defined(__ATOMIC_ACQ_REL)
3437# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003438#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003439# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3440#endif
3441
3442template <class _Tp>
3443inline _LIBCPP_INLINE_VISIBILITY _Tp
3444__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3445{
3446#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3447 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3448#else
3449 return __t += 1;
3450#endif
3451}
3452
3453template <class _Tp>
3454inline _LIBCPP_INLINE_VISIBILITY _Tp
3455__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3456{
3457#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3458 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3459#else
3460 return __t -= 1;
3461#endif
3462}
3463
Howard Hinnant756c69b2010-09-22 16:48:34 +00003464class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465 : public std::exception
3466{
3467public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003468 bad_weak_ptr() _NOEXCEPT = default;
3469 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003470 virtual ~bad_weak_ptr() _NOEXCEPT;
3471 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472};
3473
Louis Dionne16fe2952018-07-11 23:14:33 +00003474_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003475void __throw_bad_weak_ptr()
3476{
3477#ifndef _LIBCPP_NO_EXCEPTIONS
3478 throw bad_weak_ptr();
3479#else
3480 _VSTD::abort();
3481#endif
3482}
3483
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003484template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003486class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487{
3488 __shared_count(const __shared_count&);
3489 __shared_count& operator=(const __shared_count&);
3490
3491protected:
3492 long __shared_owners_;
3493 virtual ~__shared_count();
3494private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003495 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496
3497public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003499 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500 : __shared_owners_(__refs) {}
3501
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003502#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003503 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003504 void __add_shared() _NOEXCEPT;
3505 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003506#else
3507 _LIBCPP_INLINE_VISIBILITY
3508 void __add_shared() _NOEXCEPT {
3509 __libcpp_atomic_refcount_increment(__shared_owners_);
3510 }
3511 _LIBCPP_INLINE_VISIBILITY
3512 bool __release_shared() _NOEXCEPT {
3513 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3514 __on_zero_shared();
3515 return true;
3516 }
3517 return false;
3518 }
3519#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003520 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003521 long use_count() const _NOEXCEPT {
3522 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3523 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524};
3525
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003526class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527 : private __shared_count
3528{
3529 long __shared_weak_owners_;
3530
3531public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003533 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534 : __shared_count(__refs),
3535 __shared_weak_owners_(__refs) {}
3536protected:
3537 virtual ~__shared_weak_count();
3538
3539public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003540#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003541 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003542 void __add_shared() _NOEXCEPT;
3543 void __add_weak() _NOEXCEPT;
3544 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003545#else
3546 _LIBCPP_INLINE_VISIBILITY
3547 void __add_shared() _NOEXCEPT {
3548 __shared_count::__add_shared();
3549 }
3550 _LIBCPP_INLINE_VISIBILITY
3551 void __add_weak() _NOEXCEPT {
3552 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3553 }
3554 _LIBCPP_INLINE_VISIBILITY
3555 void __release_shared() _NOEXCEPT {
3556 if (__shared_count::__release_shared())
3557 __release_weak();
3558 }
3559#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003560 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003562 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3563 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564
Howard Hinnant807d6332013-02-25 15:50:36 +00003565 // Define the function out only if we build static libc++ without RTTI.
3566 // Otherwise we may break clients who need to compile their projects with
3567 // -fno-rtti and yet link against a libc++.dylib compiled
3568 // without -fno-rtti.
3569#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003570 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003571#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003573 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574};
3575
3576template <class _Tp, class _Dp, class _Alloc>
3577class __shared_ptr_pointer
3578 : public __shared_weak_count
3579{
3580 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3581public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003584 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585
Howard Hinnant72f73582010-08-11 17:04:31 +00003586#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003587 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003588#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589
3590private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003591 virtual void __on_zero_shared() _NOEXCEPT;
3592 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593};
3594
Howard Hinnant72f73582010-08-11 17:04:31 +00003595#ifndef _LIBCPP_NO_RTTI
3596
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597template <class _Tp, class _Dp, class _Alloc>
3598const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003599__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003601 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602}
3603
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003604#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003605
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606template <class _Tp, class _Dp, class _Alloc>
3607void
Howard Hinnant719bda32011-05-28 14:41:13 +00003608__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609{
3610 __data_.first().second()(__data_.first().first());
3611 __data_.first().second().~_Dp();
3612}
3613
3614template <class _Tp, class _Dp, class _Alloc>
3615void
Howard Hinnant719bda32011-05-28 14:41:13 +00003616__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003618 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3619 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003620 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3621
Eric Fiselierf8898c82015-02-05 23:01:40 +00003622 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003624 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625}
3626
3627template <class _Tp, class _Alloc>
3628class __shared_ptr_emplace
3629 : public __shared_weak_count
3630{
3631 __compressed_pair<_Alloc, _Tp> __data_;
3632public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633
Howard Hinnant756c69b2010-09-22 16:48:34 +00003634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003636 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003638
3639#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003640 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003643 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3644 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645#else // _LIBCPP_HAS_NO_VARIADICS
3646
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3650 : __data_(__a, _Tp(__a0)) {}
3651
3652 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3655 : __data_(__a, _Tp(__a0, __a1)) {}
3656
3657 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3660 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3661
3662#endif // _LIBCPP_HAS_NO_VARIADICS
3663
3664private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003665 virtual void __on_zero_shared() _NOEXCEPT;
3666 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003668 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003669 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670};
3671
3672template <class _Tp, class _Alloc>
3673void
Howard Hinnant719bda32011-05-28 14:41:13 +00003674__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
3676 __data_.second().~_Tp();
3677}
3678
3679template <class _Tp, class _Alloc>
3680void
Howard Hinnant719bda32011-05-28 14:41:13 +00003681__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003683 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3684 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003685 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003686 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003688 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689}
3690
Erik Pilkington2a398762017-05-25 15:43:31 +00003691struct __shared_ptr_dummy_rebind_allocator_type;
3692template <>
3693class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3694{
3695public:
3696 template <class _Other>
3697 struct rebind
3698 {
3699 typedef allocator<_Other> other;
3700 };
3701};
3702
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003703template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704
3705template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003706class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003708public:
zoecarver6d87d7c2020-05-11 22:42:49 -07003709 typedef _Tp element_type;
3710
Eric Fiselierae5b6672016-06-27 01:02:43 +00003711#if _LIBCPP_STD_VER > 14
3712 typedef weak_ptr<_Tp> weak_type;
3713#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714private:
3715 element_type* __ptr_;
3716 __shared_weak_count* __cntrl_;
3717
3718 struct __nat {int __for_bool_;};
3719public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003721 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003723 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003724 template<class _Yp>
3725 explicit shared_ptr(_Yp* __p,
zoecarver6d87d7c2020-05-11 22:42:49 -07003726 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003727 template<class _Yp, class _Dp>
3728 shared_ptr(_Yp* __p, _Dp __d,
zoecarver6d87d7c2020-05-11 22:42:49 -07003729 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003730 template<class _Yp, class _Dp, class _Alloc>
3731 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver6d87d7c2020-05-11 22:42:49 -07003732 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3734 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003735 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003737 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarver6d87d7c2020-05-11 22:42:49 -07003741 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003742 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003743#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003745 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003746 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarver6d87d7c2020-05-11 22:42:49 -07003747 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003748 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003749#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003751 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003752#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003753#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003754 template<class _Yp>
3755 shared_ptr(auto_ptr<_Yp>&& __r,
3756 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003757#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003758 template<class _Yp>
3759 shared_ptr(auto_ptr<_Yp> __r,
3760 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003762#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003763#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003764 template <class _Yp, class _Dp>
3765 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3766 typename enable_if
3767 <
3768 !is_lvalue_reference<_Dp>::value &&
3769 !is_array<_Yp>::value &&
3770 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3771 __nat
3772 >::type = __nat());
3773 template <class _Yp, class _Dp>
3774 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3775 typename enable_if
3776 <
3777 is_lvalue_reference<_Dp>::value &&
3778 !is_array<_Yp>::value &&
3779 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3780 __nat
3781 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003782#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003783 template <class _Yp, class _Dp>
3784 shared_ptr(unique_ptr<_Yp, _Dp>,
3785 typename enable_if
3786 <
3787 !is_lvalue_reference<_Dp>::value &&
3788 !is_array<_Yp>::value &&
3789 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3790 __nat
3791 >::type = __nat());
3792 template <class _Yp, class _Dp>
3793 shared_ptr(unique_ptr<_Yp, _Dp>,
3794 typename enable_if
3795 <
3796 is_lvalue_reference<_Dp>::value &&
3797 !is_array<_Yp>::value &&
3798 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3799 __nat
3800 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003801#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802
3803 ~shared_ptr();
3804
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003806 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003807 template<class _Yp>
3808 typename enable_if
3809 <
zoecarver6d87d7c2020-05-11 22:42:49 -07003810 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003811 shared_ptr&
3812 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003814 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003815#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003817 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003818 template<class _Yp>
3819 typename enable_if
3820 <
zoecarver6d87d7c2020-05-11 22:42:49 -07003821 is_convertible<_Yp*, element_type*>::value,
3822 shared_ptr<_Tp>&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003823 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003825 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003826#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003827 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003829 typename enable_if
3830 <
3831 !is_array<_Yp>::value &&
3832 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003833 shared_ptr
3834 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003835 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003836#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003837#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003838#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003839 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003841 typename enable_if
3842 <
3843 !is_array<_Yp>::value &&
3844 is_convertible<_Yp*, element_type*>::value,
3845 shared_ptr&
3846 >::type
3847 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003849#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003850 template <class _Yp, class _Dp>
3851 typename enable_if
3852 <
3853 !is_array<_Yp>::value &&
3854 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3855 shared_ptr&
3856 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003857#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003859 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003860#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003862 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863#endif
3864
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003866 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003868 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003869 template<class _Yp>
3870 typename enable_if
3871 <
zoecarver6d87d7c2020-05-11 22:42:49 -07003872 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003873 void
3874 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003876 reset(_Yp* __p);
3877 template<class _Yp, class _Dp>
3878 typename enable_if
3879 <
zoecarver6d87d7c2020-05-11 22:42:49 -07003880 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003881 void
3882 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003884 reset(_Yp* __p, _Dp __d);
3885 template<class _Yp, class _Dp, class _Alloc>
3886 typename enable_if
3887 <
zoecarver6d87d7c2020-05-11 22:42:49 -07003888 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003889 void
3890 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003892 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893
Howard Hinnant756c69b2010-09-22 16:48:34 +00003894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003895 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003897 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3898 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003899 _LIBCPP_INLINE_VISIBILITY
zoecarver6d87d7c2020-05-11 22:42:49 -07003900 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003902 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003904 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003906 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003907 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003908 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003909 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003910 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003911 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003912 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003913 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003914 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003915 _LIBCPP_INLINE_VISIBILITY
3916 bool
3917 __owner_equivalent(const shared_ptr& __p) const
3918 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919
Howard Hinnant72f73582010-08-11 17:04:31 +00003920#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003923 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003924 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003925 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003926 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003927#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928
Zoe Carverd9040c72019-10-22 15:16:49 +00003929 template<class _Yp, class _CntrlBlk>
3930 static shared_ptr<_Tp>
3931 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3932 {
3933 shared_ptr<_Tp> __r;
3934 __r.__ptr_ = __p;
3935 __r.__cntrl_ = __cntrl;
3936 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3937 return __r;
3938 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003939
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003941 template <class _Yp, bool = is_function<_Yp>::value>
3942 struct __shared_ptr_default_allocator
3943 {
3944 typedef allocator<_Yp> type;
3945 };
3946
3947 template <class _Yp>
3948 struct __shared_ptr_default_allocator<_Yp, true>
3949 {
3950 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3951 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003953 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003954 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003955 typename enable_if<is_convertible<_OrigPtr*,
3956 const enable_shared_from_this<_Yp>*
3957 >::value,
3958 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003959 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3960 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003962 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003963 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003964 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003965 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3966 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003967 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968 }
3969
Erik Pilkington2a398762017-05-25 15:43:31 +00003970 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003972 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3973 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974};
3975
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003976#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3977template<class _Tp>
3978shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3979template<class _Tp, class _Dp>
3980shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3981#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003982
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003984inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003985_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003986shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987 : __ptr_(0),
3988 __cntrl_(0)
3989{
3990}
3991
3992template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003993inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003994_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003995shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996 : __ptr_(0),
3997 __cntrl_(0)
3998{
3999}
4000
4001template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004002template<class _Yp>
4003shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarver6d87d7c2020-05-11 22:42:49 -07004004 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005 : __ptr_(__p)
4006{
4007 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004008 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarver6d87d7c2020-05-11 22:42:49 -07004009 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4010 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004012 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013}
4014
4015template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004016template<class _Yp, class _Dp>
4017shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver6d87d7c2020-05-11 22:42:49 -07004018 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019 : __ptr_(__p)
4020{
4021#ifndef _LIBCPP_NO_EXCEPTIONS
4022 try
4023 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004024#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004025 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4026 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4027 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004028 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029#ifndef _LIBCPP_NO_EXCEPTIONS
4030 }
4031 catch (...)
4032 {
4033 __d(__p);
4034 throw;
4035 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004036#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037}
4038
4039template<class _Tp>
4040template<class _Dp>
4041shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4042 : __ptr_(0)
4043{
4044#ifndef _LIBCPP_NO_EXCEPTIONS
4045 try
4046 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004047#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004048 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4049 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4050 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051#ifndef _LIBCPP_NO_EXCEPTIONS
4052 }
4053 catch (...)
4054 {
4055 __d(__p);
4056 throw;
4057 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004058#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059}
4060
4061template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004062template<class _Yp, class _Dp, class _Alloc>
4063shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver6d87d7c2020-05-11 22:42:49 -07004064 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065 : __ptr_(__p)
4066{
4067#ifndef _LIBCPP_NO_EXCEPTIONS
4068 try
4069 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004070#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004072 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073 typedef __allocator_destructor<_A2> _D2;
4074 _A2 __a2(__a);
4075 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004076 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4077 _CntrlBlk(__p, __d, __a);
4078 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004079 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080#ifndef _LIBCPP_NO_EXCEPTIONS
4081 }
4082 catch (...)
4083 {
4084 __d(__p);
4085 throw;
4086 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004087#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088}
4089
4090template<class _Tp>
4091template<class _Dp, class _Alloc>
4092shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4093 : __ptr_(0)
4094{
4095#ifndef _LIBCPP_NO_EXCEPTIONS
4096 try
4097 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004098#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004100 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101 typedef __allocator_destructor<_A2> _D2;
4102 _A2 __a2(__a);
4103 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004104 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4105 _CntrlBlk(__p, __d, __a);
4106 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107#ifndef _LIBCPP_NO_EXCEPTIONS
4108 }
4109 catch (...)
4110 {
4111 __d(__p);
4112 throw;
4113 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004114#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115}
4116
4117template<class _Tp>
4118template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004119inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004120shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121 : __ptr_(__p),
4122 __cntrl_(__r.__cntrl_)
4123{
4124 if (__cntrl_)
4125 __cntrl_->__add_shared();
4126}
4127
4128template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004129inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004130shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131 : __ptr_(__r.__ptr_),
4132 __cntrl_(__r.__cntrl_)
4133{
4134 if (__cntrl_)
4135 __cntrl_->__add_shared();
4136}
4137
4138template<class _Tp>
4139template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004140inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarver6d87d7c2020-05-11 22:42:49 -07004142 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004143 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144 : __ptr_(__r.__ptr_),
4145 __cntrl_(__r.__cntrl_)
4146{
4147 if (__cntrl_)
4148 __cntrl_->__add_shared();
4149}
4150
Howard Hinnant74279a52010-09-04 23:28:19 +00004151#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152
4153template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004154inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004155shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156 : __ptr_(__r.__ptr_),
4157 __cntrl_(__r.__cntrl_)
4158{
4159 __r.__ptr_ = 0;
4160 __r.__cntrl_ = 0;
4161}
4162
4163template<class _Tp>
4164template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004165inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarver6d87d7c2020-05-11 22:42:49 -07004167 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004168 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169 : __ptr_(__r.__ptr_),
4170 __cntrl_(__r.__cntrl_)
4171{
4172 __r.__ptr_ = 0;
4173 __r.__cntrl_ = 0;
4174}
4175
Howard Hinnant74279a52010-09-04 23:28:19 +00004176#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177
Marshall Clowb22274f2017-01-24 22:22:33 +00004178#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004180template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004181#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004182shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004184shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004186 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187 : __ptr_(__r.get())
4188{
4189 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4190 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004191 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192 __r.release();
4193}
Marshall Clowb22274f2017-01-24 22:22:33 +00004194#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195
4196template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004197template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004198#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004199shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4200#else
4201shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4202#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004203 typename enable_if
4204 <
4205 !is_lvalue_reference<_Dp>::value &&
4206 !is_array<_Yp>::value &&
4207 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4208 __nat
4209 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210 : __ptr_(__r.get())
4211{
Marshall Clow35cde742015-05-10 13:59:45 +00004212#if _LIBCPP_STD_VER > 11
4213 if (__ptr_ == nullptr)
4214 __cntrl_ = nullptr;
4215 else
4216#endif
4217 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004218 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4219 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4220 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004221 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004222 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223 __r.release();
4224}
4225
4226template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004227template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004228#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4230#else
4231shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4232#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004233 typename enable_if
4234 <
4235 is_lvalue_reference<_Dp>::value &&
4236 !is_array<_Yp>::value &&
4237 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4238 __nat
4239 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004240 : __ptr_(__r.get())
4241{
Marshall Clow35cde742015-05-10 13:59:45 +00004242#if _LIBCPP_STD_VER > 11
4243 if (__ptr_ == nullptr)
4244 __cntrl_ = nullptr;
4245 else
4246#endif
4247 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004248 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004249 typedef __shared_ptr_pointer<_Yp*,
4250 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004251 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004252 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004253 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004254 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255 __r.release();
4256}
4257
Zoe Carver6cd05c32019-08-19 15:47:16 +00004258template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259shared_ptr<_Tp>::~shared_ptr()
4260{
4261 if (__cntrl_)
4262 __cntrl_->__release_shared();
4263}
4264
4265template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004266inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004268shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269{
4270 shared_ptr(__r).swap(*this);
4271 return *this;
4272}
4273
4274template<class _Tp>
4275template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004276inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004277typename enable_if
4278<
zoecarver6d87d7c2020-05-11 22:42:49 -07004279 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004280 shared_ptr<_Tp>&
4281>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004282shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283{
4284 shared_ptr(__r).swap(*this);
4285 return *this;
4286}
4287
Howard Hinnant74279a52010-09-04 23:28:19 +00004288#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289
4290template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004291inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004293shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004295 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296 return *this;
4297}
4298
4299template<class _Tp>
4300template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004301inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004302typename enable_if
4303<
zoecarver6d87d7c2020-05-11 22:42:49 -07004304 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004305 shared_ptr<_Tp>&
4306>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4308{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004309 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310 return *this;
4311}
4312
Marshall Clowb22274f2017-01-24 22:22:33 +00004313#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314template<class _Tp>
4315template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004316inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004317typename enable_if
4318<
4319 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004320 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004321 shared_ptr<_Tp>
4322>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4324{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004325 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326 return *this;
4327}
Marshall Clowb22274f2017-01-24 22:22:33 +00004328#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329
4330template<class _Tp>
4331template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004332inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004333typename enable_if
4334<
4335 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004336 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004337 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004338 shared_ptr<_Tp>&
4339>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4341{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004342 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343 return *this;
4344}
4345
Howard Hinnant74279a52010-09-04 23:28:19 +00004346#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347
Marshall Clowb22274f2017-01-24 22:22:33 +00004348#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349template<class _Tp>
4350template<class _Yp>
4351inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004352typename enable_if
4353<
4354 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004355 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004356 shared_ptr<_Tp>&
4357>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004358shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359{
4360 shared_ptr(__r).swap(*this);
4361 return *this;
4362}
Marshall Clowb22274f2017-01-24 22:22:33 +00004363#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364
4365template<class _Tp>
4366template <class _Yp, class _Dp>
4367inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004368typename enable_if
4369<
4370 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004371 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004372 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004373 shared_ptr<_Tp>&
4374>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004375shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4376{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004377 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378 return *this;
4379}
4380
Howard Hinnant74279a52010-09-04 23:28:19 +00004381#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382
4383template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004384inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385void
Howard Hinnant719bda32011-05-28 14:41:13 +00004386shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004388 _VSTD::swap(__ptr_, __r.__ptr_);
4389 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390}
4391
4392template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004393inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394void
Howard Hinnant719bda32011-05-28 14:41:13 +00004395shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004396{
4397 shared_ptr().swap(*this);
4398}
4399
4400template<class _Tp>
4401template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004402inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004403typename enable_if
4404<
zoecarver6d87d7c2020-05-11 22:42:49 -07004405 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004406 void
4407>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004408shared_ptr<_Tp>::reset(_Yp* __p)
4409{
4410 shared_ptr(__p).swap(*this);
4411}
4412
4413template<class _Tp>
4414template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004415inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004416typename enable_if
4417<
zoecarver6d87d7c2020-05-11 22:42:49 -07004418 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004419 void
4420>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4422{
4423 shared_ptr(__p, __d).swap(*this);
4424}
4425
4426template<class _Tp>
4427template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004428inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004429typename enable_if
4430<
zoecarver6d87d7c2020-05-11 22:42:49 -07004431 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004432 void
4433>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4435{
4436 shared_ptr(__p, __d, __a).swap(*this);
4437}
4438
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004439template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004441typename enable_if
4442<
4443 !is_array<_Tp>::value,
4444 shared_ptr<_Tp>
4445>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446make_shared(_Args&& ...__args)
4447{
Zoe Carverd9040c72019-10-22 15:16:49 +00004448 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4449 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4450 typedef allocator<_CntrlBlk> _A2;
4451 typedef __allocator_destructor<_A2> _D2;
4452
4453 _A2 __a2;
4454 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4455 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4456
4457 _Tp *__ptr = __hold2.get()->get();
4458 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459}
4460
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004461template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004463typename enable_if
4464<
4465 !is_array<_Tp>::value,
4466 shared_ptr<_Tp>
4467>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004468allocate_shared(const _Alloc& __a, _Args&& ...__args)
4469{
zoecarver505730a2020-02-25 16:50:57 -08004470 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4471
4472 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4473 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4474 typedef __allocator_destructor<_A2> _D2;
4475
4476 _A2 __a2(__a);
4477 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4478 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4479 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4480
4481 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4482 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004483}
4484
Howard Hinnantc51e1022010-05-11 19:42:16 +00004485template<class _Tp, class _Up>
4486inline _LIBCPP_INLINE_VISIBILITY
4487bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004488operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004489{
4490 return __x.get() == __y.get();
4491}
4492
4493template<class _Tp, class _Up>
4494inline _LIBCPP_INLINE_VISIBILITY
4495bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004496operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497{
4498 return !(__x == __y);
4499}
4500
4501template<class _Tp, class _Up>
4502inline _LIBCPP_INLINE_VISIBILITY
4503bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004504operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004505{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004506#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004507 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4508 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004509#else
4510 return less<>()(__x.get(), __y.get());
4511#endif
4512
Howard Hinnantb17caf92012-02-21 21:02:58 +00004513}
4514
4515template<class _Tp, class _Up>
4516inline _LIBCPP_INLINE_VISIBILITY
4517bool
4518operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4519{
4520 return __y < __x;
4521}
4522
4523template<class _Tp, class _Up>
4524inline _LIBCPP_INLINE_VISIBILITY
4525bool
4526operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4527{
4528 return !(__y < __x);
4529}
4530
4531template<class _Tp, class _Up>
4532inline _LIBCPP_INLINE_VISIBILITY
4533bool
4534operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4535{
4536 return !(__x < __y);
4537}
4538
4539template<class _Tp>
4540inline _LIBCPP_INLINE_VISIBILITY
4541bool
4542operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4543{
4544 return !__x;
4545}
4546
4547template<class _Tp>
4548inline _LIBCPP_INLINE_VISIBILITY
4549bool
4550operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4551{
4552 return !__x;
4553}
4554
4555template<class _Tp>
4556inline _LIBCPP_INLINE_VISIBILITY
4557bool
4558operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4559{
4560 return static_cast<bool>(__x);
4561}
4562
4563template<class _Tp>
4564inline _LIBCPP_INLINE_VISIBILITY
4565bool
4566operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4567{
4568 return static_cast<bool>(__x);
4569}
4570
4571template<class _Tp>
4572inline _LIBCPP_INLINE_VISIBILITY
4573bool
4574operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4575{
4576 return less<_Tp*>()(__x.get(), nullptr);
4577}
4578
4579template<class _Tp>
4580inline _LIBCPP_INLINE_VISIBILITY
4581bool
4582operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4583{
4584 return less<_Tp*>()(nullptr, __x.get());
4585}
4586
4587template<class _Tp>
4588inline _LIBCPP_INLINE_VISIBILITY
4589bool
4590operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4591{
4592 return nullptr < __x;
4593}
4594
4595template<class _Tp>
4596inline _LIBCPP_INLINE_VISIBILITY
4597bool
4598operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4599{
4600 return __x < nullptr;
4601}
4602
4603template<class _Tp>
4604inline _LIBCPP_INLINE_VISIBILITY
4605bool
4606operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4607{
4608 return !(nullptr < __x);
4609}
4610
4611template<class _Tp>
4612inline _LIBCPP_INLINE_VISIBILITY
4613bool
4614operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4615{
4616 return !(__x < nullptr);
4617}
4618
4619template<class _Tp>
4620inline _LIBCPP_INLINE_VISIBILITY
4621bool
4622operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4623{
4624 return !(__x < nullptr);
4625}
4626
4627template<class _Tp>
4628inline _LIBCPP_INLINE_VISIBILITY
4629bool
4630operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4631{
4632 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004633}
4634
4635template<class _Tp>
4636inline _LIBCPP_INLINE_VISIBILITY
4637void
Howard Hinnant719bda32011-05-28 14:41:13 +00004638swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004639{
4640 __x.swap(__y);
4641}
4642
4643template<class _Tp, class _Up>
4644inline _LIBCPP_INLINE_VISIBILITY
zoecarver6d87d7c2020-05-11 22:42:49 -07004645typename enable_if
4646<
4647 !is_array<_Tp>::value && !is_array<_Up>::value,
4648 shared_ptr<_Tp>
4649>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004650static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004651{
zoecarver6d87d7c2020-05-11 22:42:49 -07004652 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004653}
4654
4655template<class _Tp, class _Up>
4656inline _LIBCPP_INLINE_VISIBILITY
zoecarver6d87d7c2020-05-11 22:42:49 -07004657typename enable_if
4658<
4659 !is_array<_Tp>::value && !is_array<_Up>::value,
4660 shared_ptr<_Tp>
4661>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004662dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004663{
zoecarver6d87d7c2020-05-11 22:42:49 -07004664 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004665 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4666}
4667
4668template<class _Tp, class _Up>
zoecarver6d87d7c2020-05-11 22:42:49 -07004669typename enable_if
4670<
4671 is_array<_Tp>::value == is_array<_Up>::value,
4672 shared_ptr<_Tp>
4673>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004674const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004675{
zoecarver6d87d7c2020-05-11 22:42:49 -07004676 typedef typename remove_extent<_Tp>::type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004677 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004678}
4679
Howard Hinnant72f73582010-08-11 17:04:31 +00004680#ifndef _LIBCPP_NO_RTTI
4681
Howard Hinnantc51e1022010-05-11 19:42:16 +00004682template<class _Dp, class _Tp>
4683inline _LIBCPP_INLINE_VISIBILITY
4684_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004685get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004686{
4687 return __p.template __get_deleter<_Dp>();
4688}
4689
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004690#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004691
Howard Hinnantc51e1022010-05-11 19:42:16 +00004692template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004693class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004694{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004695public:
4696 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004697private:
4698 element_type* __ptr_;
4699 __shared_weak_count* __cntrl_;
4700
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004701public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004703 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004704 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004705 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4706 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004708 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004709 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004710 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4711 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004712
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004713#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004715 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004716 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004717 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4718 _NOEXCEPT;
4719#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004720 ~weak_ptr();
4721
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004723 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004724 template<class _Yp>
4725 typename enable_if
4726 <
4727 is_convertible<_Yp*, element_type*>::value,
4728 weak_ptr&
4729 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004731 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4732
4733#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4734
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004736 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4737 template<class _Yp>
4738 typename enable_if
4739 <
4740 is_convertible<_Yp*, element_type*>::value,
4741 weak_ptr&
4742 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004744 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4745
4746#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4747
4748 template<class _Yp>
4749 typename enable_if
4750 <
4751 is_convertible<_Yp*, element_type*>::value,
4752 weak_ptr&
4753 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004755 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004756
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004758 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004760 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004761
Howard Hinnant756c69b2010-09-22 16:48:34 +00004762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004763 long use_count() const _NOEXCEPT
4764 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004766 bool expired() const _NOEXCEPT
4767 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4768 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004769 template<class _Up>
4770 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004771 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004772 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004773 template<class _Up>
4774 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004775 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004776 {return __cntrl_ < __r.__cntrl_;}
4777
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004778 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4779 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004780};
4781
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004782#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4783template<class _Tp>
4784weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4785#endif
4786
Howard Hinnantc51e1022010-05-11 19:42:16 +00004787template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004788inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004789_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004790weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004791 : __ptr_(0),
4792 __cntrl_(0)
4793{
4794}
4795
4796template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004797inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004798weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004799 : __ptr_(__r.__ptr_),
4800 __cntrl_(__r.__cntrl_)
4801{
4802 if (__cntrl_)
4803 __cntrl_->__add_weak();
4804}
4805
4806template<class _Tp>
4807template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004808inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004809weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004810 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004811 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004812 : __ptr_(__r.__ptr_),
4813 __cntrl_(__r.__cntrl_)
4814{
4815 if (__cntrl_)
4816 __cntrl_->__add_weak();
4817}
4818
4819template<class _Tp>
4820template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004821inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004822weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004823 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004824 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004825 : __ptr_(__r.__ptr_),
4826 __cntrl_(__r.__cntrl_)
4827{
4828 if (__cntrl_)
4829 __cntrl_->__add_weak();
4830}
4831
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004832#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4833
4834template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004835inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004836weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4837 : __ptr_(__r.__ptr_),
4838 __cntrl_(__r.__cntrl_)
4839{
4840 __r.__ptr_ = 0;
4841 __r.__cntrl_ = 0;
4842}
4843
4844template<class _Tp>
4845template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004846inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004847weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4848 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4849 _NOEXCEPT
4850 : __ptr_(__r.__ptr_),
4851 __cntrl_(__r.__cntrl_)
4852{
4853 __r.__ptr_ = 0;
4854 __r.__cntrl_ = 0;
4855}
4856
4857#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4858
Howard Hinnantc51e1022010-05-11 19:42:16 +00004859template<class _Tp>
4860weak_ptr<_Tp>::~weak_ptr()
4861{
4862 if (__cntrl_)
4863 __cntrl_->__release_weak();
4864}
4865
4866template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004867inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004868weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004869weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004870{
4871 weak_ptr(__r).swap(*this);
4872 return *this;
4873}
4874
4875template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004876template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004877inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004878typename enable_if
4879<
4880 is_convertible<_Yp*, _Tp*>::value,
4881 weak_ptr<_Tp>&
4882>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004883weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004884{
4885 weak_ptr(__r).swap(*this);
4886 return *this;
4887}
4888
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004889#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4890
4891template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004892inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004893weak_ptr<_Tp>&
4894weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4895{
4896 weak_ptr(_VSTD::move(__r)).swap(*this);
4897 return *this;
4898}
4899
Howard Hinnantc51e1022010-05-11 19:42:16 +00004900template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004901template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004902inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004903typename enable_if
4904<
4905 is_convertible<_Yp*, _Tp*>::value,
4906 weak_ptr<_Tp>&
4907>::type
4908weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4909{
4910 weak_ptr(_VSTD::move(__r)).swap(*this);
4911 return *this;
4912}
4913
4914#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4915
4916template<class _Tp>
4917template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004918inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004919typename enable_if
4920<
4921 is_convertible<_Yp*, _Tp*>::value,
4922 weak_ptr<_Tp>&
4923>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004924weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004925{
4926 weak_ptr(__r).swap(*this);
4927 return *this;
4928}
4929
4930template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004931inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004932void
Howard Hinnant719bda32011-05-28 14:41:13 +00004933weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004934{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004935 _VSTD::swap(__ptr_, __r.__ptr_);
4936 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004937}
4938
4939template<class _Tp>
4940inline _LIBCPP_INLINE_VISIBILITY
4941void
Howard Hinnant719bda32011-05-28 14:41:13 +00004942swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004943{
4944 __x.swap(__y);
4945}
4946
4947template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004948inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004949void
Howard Hinnant719bda32011-05-28 14:41:13 +00004950weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004951{
4952 weak_ptr().swap(*this);
4953}
4954
4955template<class _Tp>
4956template<class _Yp>
4957shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004958 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004959 : __ptr_(__r.__ptr_),
4960 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4961{
4962 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004963 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004964}
4965
4966template<class _Tp>
4967shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004968weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004969{
4970 shared_ptr<_Tp> __r;
4971 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4972 if (__r.__cntrl_)
4973 __r.__ptr_ = __ptr_;
4974 return __r;
4975}
4976
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004977#if _LIBCPP_STD_VER > 14
4978template <class _Tp = void> struct owner_less;
4979#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004980template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004981#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004982
4983template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004984struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004986{
4987 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004988 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004989 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004990 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004991 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004992 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004993 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004994 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004995 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004996 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004997};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004998
4999template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005000struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005001 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5002{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005003 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005004 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005005 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005006 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005007 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005008 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005009 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005010 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005011 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012 {return __x.owner_before(__y);}
5013};
5014
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005015#if _LIBCPP_STD_VER > 14
5016template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005017struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005018{
5019 template <class _Tp, class _Up>
5020 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005021 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005022 {return __x.owner_before(__y);}
5023 template <class _Tp, class _Up>
5024 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005025 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005026 {return __x.owner_before(__y);}
5027 template <class _Tp, class _Up>
5028 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005029 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005030 {return __x.owner_before(__y);}
5031 template <class _Tp, class _Up>
5032 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005033 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005034 {return __x.owner_before(__y);}
5035 typedef void is_transparent;
5036};
5037#endif
5038
Howard Hinnantc51e1022010-05-11 19:42:16 +00005039template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005040class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005041{
5042 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005043protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005044 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005045 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005047 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005049 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5050 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005052 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005053public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005055 shared_ptr<_Tp> shared_from_this()
5056 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005058 shared_ptr<_Tp const> shared_from_this() const
5059 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005060
Eric Fiselier84006862016-06-02 00:15:35 +00005061#if _LIBCPP_STD_VER > 14
5062 _LIBCPP_INLINE_VISIBILITY
5063 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5064 { return __weak_this_; }
5065
5066 _LIBCPP_INLINE_VISIBILITY
5067 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5068 { return __weak_this_; }
5069#endif // _LIBCPP_STD_VER > 14
5070
Howard Hinnantc51e1022010-05-11 19:42:16 +00005071 template <class _Up> friend class shared_ptr;
5072};
5073
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005074template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005075struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005076{
5077 typedef shared_ptr<_Tp> argument_type;
5078 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005079
Howard Hinnant756c69b2010-09-22 16:48:34 +00005080 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005081 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005082 {
zoecarver6d87d7c2020-05-11 22:42:49 -07005083 return hash<_Tp*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005084 }
5085};
5086
Howard Hinnantc834c512011-11-29 18:15:50 +00005087template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005088inline _LIBCPP_INLINE_VISIBILITY
5089basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005090operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005091
Eric Fiselier9b492672016-06-18 02:12:53 +00005092
5093#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005094
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005095class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005096{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005097 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005098public:
5099 void lock() _NOEXCEPT;
5100 void unlock() _NOEXCEPT;
5101
5102private:
5103 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5104 __sp_mut(const __sp_mut&);
5105 __sp_mut& operator=(const __sp_mut&);
5106
Howard Hinnant8331b762013-03-06 23:30:19 +00005107 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005108};
5109
Mehdi Amini228053d2017-05-04 17:08:54 +00005110_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5111__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005112
5113template <class _Tp>
5114inline _LIBCPP_INLINE_VISIBILITY
5115bool
5116atomic_is_lock_free(const shared_ptr<_Tp>*)
5117{
5118 return false;
5119}
5120
5121template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005122_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005123shared_ptr<_Tp>
5124atomic_load(const shared_ptr<_Tp>* __p)
5125{
5126 __sp_mut& __m = __get_sp_mut(__p);
5127 __m.lock();
5128 shared_ptr<_Tp> __q = *__p;
5129 __m.unlock();
5130 return __q;
5131}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005132
Howard Hinnant9fa30202012-07-30 01:40:57 +00005133template <class _Tp>
5134inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005135_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005136shared_ptr<_Tp>
5137atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5138{
5139 return atomic_load(__p);
5140}
5141
5142template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005143_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005144void
5145atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5146{
5147 __sp_mut& __m = __get_sp_mut(__p);
5148 __m.lock();
5149 __p->swap(__r);
5150 __m.unlock();
5151}
5152
5153template <class _Tp>
5154inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005155_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005156void
5157atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5158{
5159 atomic_store(__p, __r);
5160}
5161
5162template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005163_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005164shared_ptr<_Tp>
5165atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5166{
5167 __sp_mut& __m = __get_sp_mut(__p);
5168 __m.lock();
5169 __p->swap(__r);
5170 __m.unlock();
5171 return __r;
5172}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005173
Howard Hinnant9fa30202012-07-30 01:40:57 +00005174template <class _Tp>
5175inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005176_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005177shared_ptr<_Tp>
5178atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5179{
5180 return atomic_exchange(__p, __r);
5181}
5182
5183template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005184_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005185bool
5186atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5187{
Marshall Clow4201ee82016-05-18 17:50:13 +00005188 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005189 __sp_mut& __m = __get_sp_mut(__p);
5190 __m.lock();
5191 if (__p->__owner_equivalent(*__v))
5192 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005193 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005194 *__p = __w;
5195 __m.unlock();
5196 return true;
5197 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005198 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005199 *__v = *__p;
5200 __m.unlock();
5201 return false;
5202}
5203
5204template <class _Tp>
5205inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005206_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005207bool
5208atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5209{
5210 return atomic_compare_exchange_strong(__p, __v, __w);
5211}
5212
5213template <class _Tp>
5214inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005215_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005216bool
5217atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5218 shared_ptr<_Tp> __w, memory_order, memory_order)
5219{
5220 return atomic_compare_exchange_strong(__p, __v, __w);
5221}
5222
5223template <class _Tp>
5224inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005225_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005226bool
5227atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5228 shared_ptr<_Tp> __w, memory_order, memory_order)
5229{
5230 return atomic_compare_exchange_weak(__p, __v, __w);
5231}
5232
Eric Fiselier9b492672016-06-18 02:12:53 +00005233#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005234
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005235//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005236#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5237# ifndef _LIBCPP_CXX03_LANG
5238enum class pointer_safety : unsigned char {
5239 relaxed,
5240 preferred,
5241 strict
5242};
5243# endif
5244#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005245struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005246{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005247 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005248 {
5249 relaxed,
5250 preferred,
5251 strict
5252 };
5253
Howard Hinnant49e145e2012-10-30 19:06:59 +00005254 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005255
Howard Hinnant756c69b2010-09-22 16:48:34 +00005256 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005257 pointer_safety() : __v_() {}
5258
5259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005260 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005262 operator int() const {return __v_;}
5263};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005264#endif
5265
5266#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005267 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005268_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5269#else
5270// This function is only offered in C++03 under ABI v1.
5271# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5272inline _LIBCPP_INLINE_VISIBILITY
5273pointer_safety get_pointer_safety() _NOEXCEPT {
5274 return pointer_safety::relaxed;
5275}
5276# endif
5277#endif
5278
Howard Hinnantc51e1022010-05-11 19:42:16 +00005279
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005280_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5281_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5282_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005283_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005284
5285template <class _Tp>
5286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005287_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005288undeclare_reachable(_Tp* __p)
5289{
5290 return static_cast<_Tp*>(__undeclare_reachable(__p));
5291}
5292
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005293_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005294
Marshall Clow8982dcd2015-07-13 20:04:56 +00005295// --- Helper for container swap --
5296template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005297inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005298void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5299#if _LIBCPP_STD_VER >= 14
5300 _NOEXCEPT
5301#else
5302 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5303#endif
5304{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005305 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005306 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5307}
5308
5309template <typename _Alloc>
5310_LIBCPP_INLINE_VISIBILITY
5311void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5312#if _LIBCPP_STD_VER >= 14
5313 _NOEXCEPT
5314#else
5315 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5316#endif
5317{
5318 using _VSTD::swap;
5319 swap(__a1, __a2);
5320}
5321
5322template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005323inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005324void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5325
Marshall Clowff91de82015-08-18 19:51:37 +00005326template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005327struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005328 _Traits::propagate_on_container_move_assignment::value
5329#if _LIBCPP_STD_VER > 14
5330 || _Traits::is_always_equal::value
5331#else
5332 && is_nothrow_move_assignable<_Alloc>::value
5333#endif
5334 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005335
Marshall Clowa591b9a2016-07-11 21:38:08 +00005336
5337#ifndef _LIBCPP_HAS_NO_VARIADICS
5338template <class _Tp, class _Alloc>
5339struct __temp_value {
5340 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005341
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005342 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005343 _Alloc &__a;
5344
5345 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5346 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005347
Marshall Clowa591b9a2016-07-11 21:38:08 +00005348 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005349 _LIBCPP_NO_CFI
5350 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5351 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5352 _VSTD::forward<_Args>(__args)...);
5353 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005354
Marshall Clowa591b9a2016-07-11 21:38:08 +00005355 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5356 };
5357#endif
5358
Marshall Clowe46031a2018-07-02 18:41:15 +00005359template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005360struct __is_allocator : false_type {};
5361
5362template<typename _Alloc>
5363struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005364 typename __void_t<typename _Alloc::value_type>::type,
5365 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5366 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005367 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005368
Eric Fiselier74ebee62019-06-08 01:31:19 +00005369// __builtin_new_allocator -- A non-templated helper for allocating and
5370// deallocating memory using __builtin_operator_new and
5371// __builtin_operator_delete. It should be used in preference to
5372// `std::allocator<T>` to avoid additional instantiations.
5373struct __builtin_new_allocator {
5374 struct __builtin_new_deleter {
5375 typedef void* pointer_type;
5376
5377 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5378 : __size_(__size), __align_(__align) {}
5379
5380 void operator()(void* p) const _NOEXCEPT {
5381 std::__libcpp_deallocate(p, __size_, __align_);
5382 }
5383
5384 private:
5385 size_t __size_;
5386 size_t __align_;
5387 };
5388
5389 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5390
5391 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5392 return __holder_t(std::__libcpp_allocate(__s, __align),
5393 __builtin_new_deleter(__s, __align));
5394 }
5395
5396 static void __deallocate_bytes(void* __p, size_t __s,
5397 size_t __align) _NOEXCEPT {
5398 std::__libcpp_deallocate(__p, __s, __align);
5399 }
5400
5401 template <class _Tp>
5402 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5403 static __holder_t __allocate_type(size_t __n) {
5404 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5405 }
5406
5407 template <class _Tp>
5408 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5409 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5410 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5411 }
5412};
5413
5414
Howard Hinnantc51e1022010-05-11 19:42:16 +00005415_LIBCPP_END_NAMESPACE_STD
5416
Eric Fiselierf4433a32017-05-31 22:07:49 +00005417_LIBCPP_POP_MACROS
5418
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005419#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005420# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005421#endif
5422
Howard Hinnantc51e1022010-05-11 19:42:16 +00005423#endif // _LIBCPP_MEMORY