blob: 8a9f8264109724118d20e4f16c297cdfd9d8c3e6 [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
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1310struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1311{
Michael Park99f9e912020-03-04 11:27:14 -05001312 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001313 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001314 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315};
1316
1317template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1319{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001320 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321};
1322
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001323#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324
Michael Park99f9e912020-03-04 11:27:14 -05001325_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1327auto
1328__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001329 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001330_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
1332template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1333auto
1334__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1335 -> false_type;
1336
1337template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1338struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001339 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1340 declval<_SizeType>(),
1341 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342{
1343};
1344
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001345#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
1347template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1348struct __has_allocate_hint
1349 : true_type
1350{
1351};
1352
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001353#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001355#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356
Michael Park99f9e912020-03-04 11:27:14 -05001357_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1358template <class _Alloc, class _Tp, class... _Args>
1359auto
1360__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&&... __args)
1361 -> decltype(__a.construct(__p, _VSTD::forward<_Args>(__args)...), true_type());
1362_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363
1364template <class _Alloc, class _Pointer, class ..._Args>
Michael Park99f9e912020-03-04 11:27:14 -05001365auto
1366__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args)
1367 -> false_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368
1369template <class _Alloc, class _Pointer, class ..._Args>
1370struct __has_construct
Michael Park99f9e912020-03-04 11:27:14 -05001371 : decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
1372 declval<_Pointer>(),
1373 declval<_Args>()...))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374{
1375};
1376
Michael Park99f9e912020-03-04 11:27:14 -05001377_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378template <class _Alloc, class _Pointer>
1379auto
1380__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1381 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001382_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383
1384template <class _Alloc, class _Pointer>
1385auto
1386__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1387 -> false_type;
1388
1389template <class _Alloc, class _Pointer>
1390struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001391 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1392 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393{
1394};
1395
Michael Park99f9e912020-03-04 11:27:14 -05001396_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397template <class _Alloc>
1398auto
1399__has_max_size_test(_Alloc&& __a)
1400 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001401_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402
1403template <class _Alloc>
1404auto
1405__has_max_size_test(const volatile _Alloc& __a)
1406 -> false_type;
1407
1408template <class _Alloc>
1409struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001410 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411{
1412};
1413
1414template <class _Alloc>
1415auto
1416__has_select_on_container_copy_construction_test(_Alloc&& __a)
1417 -> decltype(__a.select_on_container_copy_construction(), true_type());
1418
1419template <class _Alloc>
1420auto
1421__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1422 -> false_type;
1423
1424template <class _Alloc>
1425struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001426 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427{
1428};
1429
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001430#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001432template <class _Alloc, class _Pointer, class _Tp, class = void>
1433struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001435template <class _Alloc, class _Pointer, class _Tp>
1436struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1437 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1438>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001440template <class _Alloc, class _Pointer, class = void>
1441struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442
1443template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001444struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1445 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1446>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447
1448template <class _Alloc>
1449struct __has_max_size
1450 : true_type
1451{
1452};
1453
1454template <class _Alloc>
1455struct __has_select_on_container_copy_construction
1456 : false_type
1457{
1458};
1459
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001460#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001462template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1463struct __alloc_traits_difference_type
1464{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001465 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001466};
1467
1468template <class _Alloc, class _Ptr>
1469struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1470{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001471 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001472};
1473
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001474template <class _Tp>
1475struct __is_default_allocator : false_type {};
1476
1477template <class _Tp>
1478struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1479
Eric Fiselier909fe962019-09-13 16:09:33 +00001480
1481
1482template <class _Alloc,
1483 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1484 >
1485struct __is_cpp17_move_insertable;
1486template <class _Alloc>
1487struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1488template <class _Alloc>
1489struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1490
1491template <class _Alloc,
1492 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1493 >
1494struct __is_cpp17_copy_insertable;
1495template <class _Alloc>
1496struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1497template <class _Alloc>
1498struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1499 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1500 __is_cpp17_move_insertable<_Alloc>::value>
1501 {};
1502
1503
1504
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001506struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 typedef _Alloc allocator_type;
1509 typedef typename allocator_type::value_type value_type;
1510
1511 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1512 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1513 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1514 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1515
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1517 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1520 propagate_on_container_copy_assignment;
1521 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1522 propagate_on_container_move_assignment;
1523 typedef typename __propagate_on_container_swap<allocator_type>::type
1524 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001525 typedef typename __is_always_equal<allocator_type>::type
1526 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001528#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001530 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001531 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001532#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> struct rebind_alloc
1534 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1535 template <class _Tp> struct rebind_traits
1536 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Marshall Clow0e58cae2017-11-26 02:55:38 +00001539 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static pointer allocate(allocator_type& __a, size_type __n)
1541 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001542 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001544 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001548 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {__a.deallocate(__p, __n);}
1550
1551#ifndef _LIBCPP_HAS_NO_VARIADICS
1552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001555 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001556 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001557#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001560 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {
1562 ::new ((void*)__p) _Tp();
1563 }
1564 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001566 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001568 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1569 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 }
1571 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001572 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001573 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 const _A1& __a1)
1575 {
1576 ::new ((void*)__p) _Tp(__a0, __a1);
1577 }
1578 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001579 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001580 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 const _A1& __a1, const _A2& __a2)
1582 {
1583 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1584 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001585#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586
1587 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 static void destroy(allocator_type& __a, _Tp* __p)
1590 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1591
Howard Hinnant756c69b2010-09-22 16:48:34 +00001592 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001593 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1595
Howard Hinnant756c69b2010-09-22 16:48:34 +00001596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 static allocator_type
1598 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001599 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 __has_select_on_container_copy_construction<const allocator_type>(),
1601 __a);}
1602
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001603 template <class _Ptr>
1604 _LIBCPP_INLINE_VISIBILITY
1605 static
1606 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001607 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001608 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001609 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1610 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001611 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001612 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001613#ifdef _LIBCPP_NO_EXCEPTIONS
1614 _VSTD::move(*__begin1)
1615#else
1616 _VSTD::move_if_noexcept(*__begin1)
1617#endif
1618 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001619 }
1620
1621 template <class _Tp>
1622 _LIBCPP_INLINE_VISIBILITY
1623 static
1624 typename enable_if
1625 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001626 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001627 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1628 is_trivially_move_constructible<_Tp>::value,
1629 void
1630 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001631 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001632 {
1633 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001634 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001635 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001636 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001637 __begin2 += _Np;
1638 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001639 }
1640
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001641 template <class _Iter, class _Ptr>
1642 _LIBCPP_INLINE_VISIBILITY
1643 static
1644 void
1645 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1646 {
1647 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001648 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001649 }
1650
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001651 template <class _SourceTp, class _DestTp,
1652 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1653 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001654 _LIBCPP_INLINE_VISIBILITY
1655 static
1656 typename enable_if
1657 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001658 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001659 is_same<_RawSourceTp, _RawDestTp>::value &&
1660 (__is_default_allocator<allocator_type>::value ||
1661 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001662 void
1663 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001664 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001665 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001666 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001667 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001668 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001669 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001670 __begin2 += _Np;
1671 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001672 }
1673
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001674 template <class _Ptr>
1675 _LIBCPP_INLINE_VISIBILITY
1676 static
1677 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001678 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001679 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001680 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1681 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001682 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001683 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001684 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001685#ifdef _LIBCPP_NO_EXCEPTIONS
1686 _VSTD::move(*--__end1)
1687#else
1688 _VSTD::move_if_noexcept(*--__end1)
1689#endif
1690 );
1691 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001692 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001693 }
1694
1695 template <class _Tp>
1696 _LIBCPP_INLINE_VISIBILITY
1697 static
1698 typename enable_if
1699 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001700 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001701 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1702 is_trivially_move_constructible<_Tp>::value,
1703 void
1704 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001705 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001706 {
1707 ptrdiff_t _Np = __end1 - __begin1;
1708 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001709 if (_Np > 0)
1710 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001711 }
1712
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713private:
1714
Howard Hinnant756c69b2010-09-22 16:48:34 +00001715 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001716 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001718 {
1719 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1720 return __a.allocate(__n, __hint);
1721 _LIBCPP_SUPPRESS_DEPRECATED_POP
1722 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001723 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001724 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001725 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 {return __a.allocate(__n);}
1727
1728#ifndef _LIBCPP_HAS_NO_VARIADICS
1729 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001732 {
1733 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1734 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1735 _LIBCPP_SUPPRESS_DEPRECATED_POP
1736 }
1737
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1741 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001742 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001744#else // _LIBCPP_HAS_NO_VARIADICS
1745 template <class _Tp, class _A0>
1746 _LIBCPP_INLINE_VISIBILITY
1747 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1748 const _A0& __a0)
1749 {__a.construct(__p, __a0);}
1750 template <class _Tp, class _A0>
1751 _LIBCPP_INLINE_VISIBILITY
1752 static void __construct(false_type, allocator_type&, _Tp* __p,
1753 const _A0& __a0)
1754 {
1755 ::new ((void*)__p) _Tp(__a0);
1756 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001757#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758
1759 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001762 {
1763 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1764 __a.destroy(__p);
1765 _LIBCPP_SUPPRESS_DEPRECATED_POP
1766 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769 static void __destroy(false_type, allocator_type&, _Tp* __p)
1770 {
1771 __p->~_Tp();
1772 }
1773
Howard Hinnant756c69b2010-09-22 16:48:34 +00001774 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001775 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001776 {
1777 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1778 return __a.max_size();
1779 _LIBCPP_SUPPRESS_DEPRECATED_POP
1780 }
1781
Howard Hinnant756c69b2010-09-22 16:48:34 +00001782 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001783 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001784 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785
Howard Hinnant756c69b2010-09-22 16:48:34 +00001786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001788 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001792 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 {return __a;}
1794};
1795
Marshall Clow940e01c2015-04-07 05:21:38 +00001796template <class _Traits, class _Tp>
1797struct __rebind_alloc_helper
1798{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001799#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001800 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001801#else
1802 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1803#endif
1804};
1805
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806// allocator
1807
1808template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001809class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810{
1811public:
Michael Park99f9e912020-03-04 11:27:14 -05001812#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1813 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1814 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1815 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1816 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1817 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1818 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1819
1820 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1821#endif
1822
1823 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824
Howard Hinnant4931e092011-06-02 21:38:57 +00001825 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001826 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001827
Marshall Clowbc759762018-03-20 23:02:53 +00001828 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1829 allocator() _NOEXCEPT {}
1830
Louis Dionne481a2662018-09-23 18:35:00 +00001831 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001832 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1833 allocator(const allocator<_Up>&) _NOEXCEPT {}
1834
Michael Park99f9e912020-03-04 11:27:14 -05001835#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1836 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1837 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001838 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001839 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1840 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001841 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001842#endif
1843
1844 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001845 {
Michael Park99f9e912020-03-04 11:27:14 -05001846 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1847 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001848 __throw_length_error("allocator<T>::allocate(size_t n)"
1849 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001850 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001851 }
Michael Park99f9e912020-03-04 11:27:14 -05001852
1853#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1854 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1855 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1856#endif
1857
1858 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001859 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001860
1861#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1862 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001863 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001864
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001866 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 void
1868 construct(_Up* __p, _Args&&... __args)
1869 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001870 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 }
Michael Park99f9e912020-03-04 11:27:14 -05001872 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1873#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874};
1875
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001876template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001877class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001878{
1879public:
Michael Park99f9e912020-03-04 11:27:14 -05001880#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1881 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1882 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1883 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1884 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1885 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1886 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1887
1888 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1889#endif
1890
1891 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001892
1893 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001894 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001895
Marshall Clowbc759762018-03-20 23:02:53 +00001896 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1897 allocator() _NOEXCEPT {}
1898
1899 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001900 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001901 allocator(const allocator<_Up>&) _NOEXCEPT {}
1902
Michael Park99f9e912020-03-04 11:27:14 -05001903#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1904 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1905 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001906 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001907#endif
1908
1909 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001910 {
Michael Park99f9e912020-03-04 11:27:14 -05001911 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1912 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001913 __throw_length_error("allocator<const T>::allocate(size_t n)"
1914 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001915 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001916 }
Michael Park99f9e912020-03-04 11:27:14 -05001917
1918#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1919 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1920 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1921#endif
1922
1923 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001924 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001925
1926#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1927 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001928 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001929
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001930#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1931 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001932 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001933 void
1934 construct(_Up* __p, _Args&&... __args)
1935 {
1936 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1937 }
1938#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1939 _LIBCPP_INLINE_VISIBILITY
1940 void
1941 construct(pointer __p)
1942 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001943 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001944 }
1945# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001946
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001947 template <class _A0>
1948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001949 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001950 construct(pointer __p, _A0& __a0)
1951 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001952 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001953 }
1954 template <class _A0>
1955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001956 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001957 construct(pointer __p, const _A0& __a0)
1958 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001959 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001960 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001961# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1962 template <class _A0, class _A1>
1963 _LIBCPP_INLINE_VISIBILITY
1964 void
1965 construct(pointer __p, _A0& __a0, _A1& __a1)
1966 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001967 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001968 }
1969 template <class _A0, class _A1>
1970 _LIBCPP_INLINE_VISIBILITY
1971 void
1972 construct(pointer __p, const _A0& __a0, _A1& __a1)
1973 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001974 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001975 }
1976 template <class _A0, class _A1>
1977 _LIBCPP_INLINE_VISIBILITY
1978 void
1979 construct(pointer __p, _A0& __a0, const _A1& __a1)
1980 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001981 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001982 }
1983 template <class _A0, class _A1>
1984 _LIBCPP_INLINE_VISIBILITY
1985 void
1986 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1987 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001988 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001989 }
1990#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05001991 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1992#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001993};
1994
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995template <class _Tp, class _Up>
1996inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001997bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998
1999template <class _Tp, class _Up>
2000inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002001bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002
2003template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002004class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 : public iterator<output_iterator_tag,
2006 _Tp, // purposefully not C++03
2007 ptrdiff_t, // purposefully not C++03
2008 _Tp*, // purposefully not C++03
2009 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2010{
2011private:
2012 _OutputIterator __x_;
2013public:
2014 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2015 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2016 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002017 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002018#if _LIBCPP_STD_VER >= 14
2019 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002020 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002021#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2023 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2024 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002025#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002026 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002027#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028};
2029
2030template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002031_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002033get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034{
2035 pair<_Tp*, ptrdiff_t> __r(0, 0);
2036 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2037 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2038 / sizeof(_Tp);
2039 if (__n > __m)
2040 __n = __m;
2041 while (__n > 0)
2042 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002043#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002044 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002045 {
2046 std::align_val_t __al =
2047 std::align_val_t(std::alignment_of<_Tp>::value);
2048 __r.first = static_cast<_Tp*>(::operator new(
2049 __n * sizeof(_Tp), __al, nothrow));
2050 } else {
2051 __r.first = static_cast<_Tp*>(::operator new(
2052 __n * sizeof(_Tp), nothrow));
2053 }
2054#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002055 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002056 {
2057 // Since aligned operator new is unavailable, return an empty
2058 // buffer rather than one with invalid alignment.
2059 return __r;
2060 }
2061
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002063#endif
2064
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065 if (__r.first)
2066 {
2067 __r.second = __n;
2068 break;
2069 }
2070 __n /= 2;
2071 }
2072 return __r;
2073}
2074
2075template <class _Tp>
2076inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002077void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2078{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002079 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002080}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081
Marshall Clowb22274f2017-01-24 22:22:33 +00002082#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002084struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085{
2086 _Tp* __ptr_;
2087};
2088
2089template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002090class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091{
2092private:
2093 _Tp* __ptr_;
2094public:
2095 typedef _Tp element_type;
2096
Dimitry Andric47269ce2020-03-13 19:36:26 +01002097 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
2098 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
2099 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002101 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002103 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002105 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002107 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108
Dimitry Andric47269ce2020-03-13 19:36:26 +01002109 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002111 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
2112 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
2113 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114 {
2115 _Tp* __t = __ptr_;
2116 __ptr_ = 0;
2117 return __t;
2118 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01002119 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 {
2121 if (__ptr_ != __p)
2122 delete __ptr_;
2123 __ptr_ = __p;
2124 }
2125
Dimitry Andric47269ce2020-03-13 19:36:26 +01002126 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
2127 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002129 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130 {return auto_ptr<_Up>(release());}
2131};
2132
2133template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002134class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135{
2136public:
2137 typedef void element_type;
2138};
Marshall Clowb22274f2017-01-24 22:22:33 +00002139#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002141// Tag used to default initialize one or both of the pair's elements.
2142struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002143struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002144
Eric Fiselier9d355982017-04-12 23:45:53 +00002145template <class _Tp, int _Idx,
2146 bool _CanBeEmptyBase =
2147 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2148struct __compressed_pair_elem {
2149 typedef _Tp _ParamT;
2150 typedef _Tp& reference;
2151 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002153 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002154 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002155 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2156 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157
Eric Fiselier9d355982017-04-12 23:45:53 +00002158 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002159 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2160 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002161 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002162 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002163 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002164 : __value_(_VSTD::forward<_Up>(__u))
2165 {
2166 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002168
2169#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002170 template <class... _Args, size_t... _Indexes>
2171 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2172 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2173 __tuple_indices<_Indexes...>)
2174 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002175#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002177
Alex Lorenz76132112017-11-09 17:54:49 +00002178 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2179 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002180 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002183 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184};
2185
Eric Fiselier9d355982017-04-12 23:45:53 +00002186template <class _Tp, int _Idx>
2187struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2188 typedef _Tp _ParamT;
2189 typedef _Tp& reference;
2190 typedef const _Tp& const_reference;
2191 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002193 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2194 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2195 __compressed_pair_elem(__default_init_tag) {}
2196 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2197 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198
Eric Fiselier9d355982017-04-12 23:45:53 +00002199 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002200 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2201 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002202 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002203 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002204 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002205 : __value_type(_VSTD::forward<_Up>(__u))
2206 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002208#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002209 template <class... _Args, size_t... _Indexes>
2210 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2211 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2212 __tuple_indices<_Indexes...>)
2213 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002214#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215
Alex Lorenz76132112017-11-09 17:54:49 +00002216 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2217 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002218 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219};
2220
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002222class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2223 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002224 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2225 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002226
2227 // NOTE: This static assert should never fire because __compressed_pair
2228 // is *almost never* used in a scenario where it's possible for T1 == T2.
2229 // (The exception is std::function where it is possible that the function
2230 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002231 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002232 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2233 "The current implementation is NOT ABI-compatible with the previous "
2234 "implementation for this configuration");
2235
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002237 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002238 class = typename enable_if<
2239 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2240 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2241 >::type
2242 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002243 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002244 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245
Eric Fiselier9d355982017-04-12 23:45:53 +00002246 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002247 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002248 __compressed_pair(_U1&& __t1, _U2&& __t2)
2249 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002251#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002252 template <class... _Args1, class... _Args2>
2253 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2254 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2255 tuple<_Args2...> __second_args)
2256 : _Base1(__pc, _VSTD::move(__first_args),
2257 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2258 _Base2(__pc, _VSTD::move(__second_args),
2259 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002260#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261
Eric Fiselier9d355982017-04-12 23:45:53 +00002262 _LIBCPP_INLINE_VISIBILITY
2263 typename _Base1::reference first() _NOEXCEPT {
2264 return static_cast<_Base1&>(*this).__get();
2265 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266
Eric Fiselier9d355982017-04-12 23:45:53 +00002267 _LIBCPP_INLINE_VISIBILITY
2268 typename _Base1::const_reference first() const _NOEXCEPT {
2269 return static_cast<_Base1 const&>(*this).__get();
2270 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271
Eric Fiselier9d355982017-04-12 23:45:53 +00002272 _LIBCPP_INLINE_VISIBILITY
2273 typename _Base2::reference second() _NOEXCEPT {
2274 return static_cast<_Base2&>(*this).__get();
2275 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276
Eric Fiselier9d355982017-04-12 23:45:53 +00002277 _LIBCPP_INLINE_VISIBILITY
2278 typename _Base2::const_reference second() const _NOEXCEPT {
2279 return static_cast<_Base2 const&>(*this).__get();
2280 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281
Eric Fiselier9d355982017-04-12 23:45:53 +00002282 _LIBCPP_INLINE_VISIBILITY
2283 void swap(__compressed_pair& __x)
2284 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2285 __is_nothrow_swappable<_T2>::value)
2286 {
2287 using std::swap;
2288 swap(first(), __x.first());
2289 swap(second(), __x.second());
2290 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291};
2292
2293template <class _T1, class _T2>
2294inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002295void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2296 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2297 __is_nothrow_swappable<_T2>::value) {
2298 __x.swap(__y);
2299}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002301// default_delete
2302
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002304struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002305 static_assert(!is_function<_Tp>::value,
2306 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002307#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002308 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002309#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002310 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002311#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002312 template <class _Up>
2313 _LIBCPP_INLINE_VISIBILITY
2314 default_delete(const default_delete<_Up>&,
2315 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2316 0) _NOEXCEPT {}
2317
2318 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2319 static_assert(sizeof(_Tp) > 0,
2320 "default_delete can not delete incomplete type");
2321 static_assert(!is_void<_Tp>::value,
2322 "default_delete can not delete incomplete type");
2323 delete __ptr;
2324 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325};
2326
2327template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002328struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2329private:
2330 template <class _Up>
2331 struct _EnableIfConvertible
2332 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2333
Howard Hinnant4500ca52011-12-18 21:19:44 +00002334public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002335#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002336 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002337#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002338 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002339#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002340
2341 template <class _Up>
2342 _LIBCPP_INLINE_VISIBILITY
2343 default_delete(const default_delete<_Up[]>&,
2344 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2345
2346 template <class _Up>
2347 _LIBCPP_INLINE_VISIBILITY
2348 typename _EnableIfConvertible<_Up>::type
2349 operator()(_Up* __ptr) const _NOEXCEPT {
2350 static_assert(sizeof(_Tp) > 0,
2351 "default_delete can not delete incomplete type");
2352 static_assert(!is_void<_Tp>::value,
2353 "default_delete can not delete void type");
2354 delete[] __ptr;
2355 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356};
2357
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002358template <class _Deleter>
2359struct __unique_ptr_deleter_sfinae {
2360 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2361 typedef const _Deleter& __lval_ref_type;
2362 typedef _Deleter&& __good_rval_ref_type;
2363 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364};
2365
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002366template <class _Deleter>
2367struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2368 typedef const _Deleter& __lval_ref_type;
2369 typedef const _Deleter&& __bad_rval_ref_type;
2370 typedef false_type __enable_rval_overload;
2371};
2372
2373template <class _Deleter>
2374struct __unique_ptr_deleter_sfinae<_Deleter&> {
2375 typedef _Deleter& __lval_ref_type;
2376 typedef _Deleter&& __bad_rval_ref_type;
2377 typedef false_type __enable_rval_overload;
2378};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002379
2380template <class _Tp, class _Dp = default_delete<_Tp> >
2381class _LIBCPP_TEMPLATE_VIS unique_ptr {
2382public:
2383 typedef _Tp element_type;
2384 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002385 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002386
2387 static_assert(!is_rvalue_reference<deleter_type>::value,
2388 "the specified deleter type cannot be an rvalue reference");
2389
2390private:
2391 __compressed_pair<pointer, deleter_type> __ptr_;
2392
2393 struct __nat { int __for_bool_; };
2394
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002395 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002396
2397 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002398 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002399 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002400
2401 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002402 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002403 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002404
2405 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002406 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002407 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002408
2409 template <bool _Dummy, class _Deleter = typename __dependent_type<
2410 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002411 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002412 typename enable_if<is_default_constructible<_Deleter>::value &&
2413 !is_pointer<_Deleter>::value>::type;
2414
2415 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002416 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002417 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2418
2419 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002420 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002421 is_convertible<typename _UPtr::pointer, pointer>::value &&
2422 !is_array<_Up>::value
2423 >::type;
2424
2425 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002426 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002427 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2428 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2429 >::type;
2430
2431 template <class _UDel>
2432 using _EnableIfDeleterAssignable = typename enable_if<
2433 is_assignable<_Dp&, _UDel&&>::value
2434 >::type;
2435
2436public:
2437 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002438 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002439 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002440 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002441
2442 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002443 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002444 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002445 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002446
2447 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002448 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002449 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002450 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002451
2452 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002453 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002454 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002455 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002456 : __ptr_(__p, __d) {}
2457
2458 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002459 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002460 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002461 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002462 : __ptr_(__p, _VSTD::move(__d)) {
2463 static_assert(!is_reference<deleter_type>::value,
2464 "rvalue deleter bound to reference");
2465 }
2466
2467 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002468 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002469 _LIBCPP_INLINE_VISIBILITY
2470 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2471
2472 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002473 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002474 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2475 }
2476
2477 template <class _Up, class _Ep,
2478 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2479 class = _EnableIfDeleterConvertible<_Ep>
2480 >
2481 _LIBCPP_INLINE_VISIBILITY
2482 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2483 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2484
2485#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2486 template <class _Up>
2487 _LIBCPP_INLINE_VISIBILITY
2488 unique_ptr(auto_ptr<_Up>&& __p,
2489 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002490 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002491 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002492 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002493#endif
2494
2495 _LIBCPP_INLINE_VISIBILITY
2496 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2497 reset(__u.release());
2498 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2499 return *this;
2500 }
2501
2502 template <class _Up, class _Ep,
2503 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2504 class = _EnableIfDeleterAssignable<_Ep>
2505 >
2506 _LIBCPP_INLINE_VISIBILITY
2507 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2508 reset(__u.release());
2509 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2510 return *this;
2511 }
2512
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002513#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2514 template <class _Up>
2515 _LIBCPP_INLINE_VISIBILITY
2516 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2517 is_same<_Dp, default_delete<_Tp> >::value,
2518 unique_ptr&>::type
2519 operator=(auto_ptr<_Up> __p) {
2520 reset(__p.release());
2521 return *this;
2522 }
2523#endif
2524
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002525#ifdef _LIBCPP_CXX03_LANG
2526 unique_ptr(unique_ptr const&) = delete;
2527 unique_ptr& operator=(unique_ptr const&) = delete;
2528#endif
2529
2530
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002531 _LIBCPP_INLINE_VISIBILITY
2532 ~unique_ptr() { reset(); }
2533
2534 _LIBCPP_INLINE_VISIBILITY
2535 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2536 reset();
2537 return *this;
2538 }
2539
2540 _LIBCPP_INLINE_VISIBILITY
2541 typename add_lvalue_reference<_Tp>::type
2542 operator*() const {
2543 return *__ptr_.first();
2544 }
2545 _LIBCPP_INLINE_VISIBILITY
2546 pointer operator->() const _NOEXCEPT {
2547 return __ptr_.first();
2548 }
2549 _LIBCPP_INLINE_VISIBILITY
2550 pointer get() const _NOEXCEPT {
2551 return __ptr_.first();
2552 }
2553 _LIBCPP_INLINE_VISIBILITY
2554 deleter_type& get_deleter() _NOEXCEPT {
2555 return __ptr_.second();
2556 }
2557 _LIBCPP_INLINE_VISIBILITY
2558 const deleter_type& get_deleter() const _NOEXCEPT {
2559 return __ptr_.second();
2560 }
2561 _LIBCPP_INLINE_VISIBILITY
2562 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2563 return __ptr_.first() != nullptr;
2564 }
2565
2566 _LIBCPP_INLINE_VISIBILITY
2567 pointer release() _NOEXCEPT {
2568 pointer __t = __ptr_.first();
2569 __ptr_.first() = pointer();
2570 return __t;
2571 }
2572
2573 _LIBCPP_INLINE_VISIBILITY
2574 void reset(pointer __p = pointer()) _NOEXCEPT {
2575 pointer __tmp = __ptr_.first();
2576 __ptr_.first() = __p;
2577 if (__tmp)
2578 __ptr_.second()(__tmp);
2579 }
2580
2581 _LIBCPP_INLINE_VISIBILITY
2582 void swap(unique_ptr& __u) _NOEXCEPT {
2583 __ptr_.swap(__u.__ptr_);
2584 }
2585};
2586
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002587
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002589class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002591 typedef _Tp element_type;
2592 typedef _Dp deleter_type;
2593 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2594
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002596 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597
Eric Fiselier31127cd2017-04-16 02:14:31 +00002598 template <class _From>
2599 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600
Eric Fiselier31127cd2017-04-16 02:14:31 +00002601 template <class _FromElem>
2602 struct _CheckArrayPointerConversion<_FromElem*>
2603 : integral_constant<bool,
2604 is_same<_FromElem*, pointer>::value ||
2605 (is_same<pointer, element_type*>::value &&
2606 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2607 >
2608 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609
Eric Fiselier31127cd2017-04-16 02:14:31 +00002610 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002611
2612 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002613 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002614 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002615
2616 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002617 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002618 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002619
2620 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002621 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002622 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002623
2624 template <bool _Dummy, class _Deleter = typename __dependent_type<
2625 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002626 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002627 typename enable_if<is_default_constructible<_Deleter>::value &&
2628 !is_pointer<_Deleter>::value>::type;
2629
2630 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002631 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002632 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2633
2634 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002635 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002636 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002637 >::type;
2638
2639 template <class _UPtr, class _Up,
2640 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002641 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002642 is_array<_Up>::value &&
2643 is_same<pointer, element_type*>::value &&
2644 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2645 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2646 >::type;
2647
2648 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002649 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002650 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2651 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2652 >::type;
2653
2654 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002655 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002656 is_assignable<_Dp&, _UDel&&>::value
2657 >::type;
2658
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002660 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002661 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002662 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002663 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002665 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002666 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002667 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002668 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002670 template <class _Pp, bool _Dummy = true,
2671 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002672 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002673 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002674 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002675 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002677 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002678 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2679 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002680 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002681 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002682 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002684 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002685 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002686 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002687 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002688 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002690 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002691 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2692 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002693 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002694 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002695 : __ptr_(__p, _VSTD::move(__d)) {
2696 static_assert(!is_reference<deleter_type>::value,
2697 "rvalue deleter bound to reference");
2698 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002700 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002701 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002702 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002703 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002704 : __ptr_(nullptr, _VSTD::move(__d)) {
2705 static_assert(!is_reference<deleter_type>::value,
2706 "rvalue deleter bound to reference");
2707 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002708
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002709 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002710 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2711 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002712 _LIBCPP_INLINE_VISIBILITY
2713 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002714
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002716 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2718 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002719
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002720 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002721 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002722 reset(__u.release());
2723 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2724 return *this;
2725 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727 template <class _Up, class _Ep,
2728 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2729 class = _EnableIfDeleterConvertible<_Ep>
2730 >
2731 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002732 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002733 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002734 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002736 template <class _Up, class _Ep,
2737 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2738 class = _EnableIfDeleterAssignable<_Ep>
2739 >
2740 _LIBCPP_INLINE_VISIBILITY
2741 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002742 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002743 reset(__u.release());
2744 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2745 return *this;
2746 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002748#ifdef _LIBCPP_CXX03_LANG
2749 unique_ptr(unique_ptr const&) = delete;
2750 unique_ptr& operator=(unique_ptr const&) = delete;
2751#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002752
2753public:
2754 _LIBCPP_INLINE_VISIBILITY
2755 ~unique_ptr() { reset(); }
2756
2757 _LIBCPP_INLINE_VISIBILITY
2758 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2759 reset();
2760 return *this;
2761 }
2762
2763 _LIBCPP_INLINE_VISIBILITY
2764 typename add_lvalue_reference<_Tp>::type
2765 operator[](size_t __i) const {
2766 return __ptr_.first()[__i];
2767 }
2768 _LIBCPP_INLINE_VISIBILITY
2769 pointer get() const _NOEXCEPT {
2770 return __ptr_.first();
2771 }
2772
2773 _LIBCPP_INLINE_VISIBILITY
2774 deleter_type& get_deleter() _NOEXCEPT {
2775 return __ptr_.second();
2776 }
2777
2778 _LIBCPP_INLINE_VISIBILITY
2779 const deleter_type& get_deleter() const _NOEXCEPT {
2780 return __ptr_.second();
2781 }
2782 _LIBCPP_INLINE_VISIBILITY
2783 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2784 return __ptr_.first() != nullptr;
2785 }
2786
2787 _LIBCPP_INLINE_VISIBILITY
2788 pointer release() _NOEXCEPT {
2789 pointer __t = __ptr_.first();
2790 __ptr_.first() = pointer();
2791 return __t;
2792 }
2793
2794 template <class _Pp>
2795 _LIBCPP_INLINE_VISIBILITY
2796 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002797 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002798 >::type
2799 reset(_Pp __p) _NOEXCEPT {
2800 pointer __tmp = __ptr_.first();
2801 __ptr_.first() = __p;
2802 if (__tmp)
2803 __ptr_.second()(__tmp);
2804 }
2805
2806 _LIBCPP_INLINE_VISIBILITY
2807 void reset(nullptr_t = nullptr) _NOEXCEPT {
2808 pointer __tmp = __ptr_.first();
2809 __ptr_.first() = nullptr;
2810 if (__tmp)
2811 __ptr_.second()(__tmp);
2812 }
2813
2814 _LIBCPP_INLINE_VISIBILITY
2815 void swap(unique_ptr& __u) _NOEXCEPT {
2816 __ptr_.swap(__u.__ptr_);
2817 }
2818
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819};
2820
2821template <class _Tp, class _Dp>
2822inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002823typename enable_if<
2824 __is_swappable<_Dp>::value,
2825 void
2826>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002827swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828
2829template <class _T1, class _D1, class _T2, class _D2>
2830inline _LIBCPP_INLINE_VISIBILITY
2831bool
2832operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2833
2834template <class _T1, class _D1, class _T2, class _D2>
2835inline _LIBCPP_INLINE_VISIBILITY
2836bool
2837operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2838
2839template <class _T1, class _D1, class _T2, class _D2>
2840inline _LIBCPP_INLINE_VISIBILITY
2841bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002842operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2843{
2844 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2845 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002846 typedef typename common_type<_P1, _P2>::type _Vp;
2847 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002848}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849
2850template <class _T1, class _D1, class _T2, class _D2>
2851inline _LIBCPP_INLINE_VISIBILITY
2852bool
2853operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2854
2855template <class _T1, class _D1, class _T2, class _D2>
2856inline _LIBCPP_INLINE_VISIBILITY
2857bool
2858operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2859
2860template <class _T1, class _D1, class _T2, class _D2>
2861inline _LIBCPP_INLINE_VISIBILITY
2862bool
2863operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2864
Howard Hinnantb17caf92012-02-21 21:02:58 +00002865template <class _T1, class _D1>
2866inline _LIBCPP_INLINE_VISIBILITY
2867bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002868operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002869{
2870 return !__x;
2871}
2872
2873template <class _T1, class _D1>
2874inline _LIBCPP_INLINE_VISIBILITY
2875bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002876operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002877{
2878 return !__x;
2879}
2880
2881template <class _T1, class _D1>
2882inline _LIBCPP_INLINE_VISIBILITY
2883bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002884operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002885{
2886 return static_cast<bool>(__x);
2887}
2888
2889template <class _T1, class _D1>
2890inline _LIBCPP_INLINE_VISIBILITY
2891bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002892operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002893{
2894 return static_cast<bool>(__x);
2895}
2896
2897template <class _T1, class _D1>
2898inline _LIBCPP_INLINE_VISIBILITY
2899bool
2900operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2901{
2902 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2903 return less<_P1>()(__x.get(), nullptr);
2904}
2905
2906template <class _T1, class _D1>
2907inline _LIBCPP_INLINE_VISIBILITY
2908bool
2909operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2910{
2911 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2912 return less<_P1>()(nullptr, __x.get());
2913}
2914
2915template <class _T1, class _D1>
2916inline _LIBCPP_INLINE_VISIBILITY
2917bool
2918operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2919{
2920 return nullptr < __x;
2921}
2922
2923template <class _T1, class _D1>
2924inline _LIBCPP_INLINE_VISIBILITY
2925bool
2926operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2927{
2928 return __x < nullptr;
2929}
2930
2931template <class _T1, class _D1>
2932inline _LIBCPP_INLINE_VISIBILITY
2933bool
2934operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2935{
2936 return !(nullptr < __x);
2937}
2938
2939template <class _T1, class _D1>
2940inline _LIBCPP_INLINE_VISIBILITY
2941bool
2942operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2943{
2944 return !(__x < nullptr);
2945}
2946
2947template <class _T1, class _D1>
2948inline _LIBCPP_INLINE_VISIBILITY
2949bool
2950operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2951{
2952 return !(__x < nullptr);
2953}
2954
2955template <class _T1, class _D1>
2956inline _LIBCPP_INLINE_VISIBILITY
2957bool
2958operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2959{
2960 return !(nullptr < __x);
2961}
2962
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002963#if _LIBCPP_STD_VER > 11
2964
2965template<class _Tp>
2966struct __unique_if
2967{
2968 typedef unique_ptr<_Tp> __unique_single;
2969};
2970
2971template<class _Tp>
2972struct __unique_if<_Tp[]>
2973{
2974 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2975};
2976
2977template<class _Tp, size_t _Np>
2978struct __unique_if<_Tp[_Np]>
2979{
2980 typedef void __unique_array_known_bound;
2981};
2982
2983template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002984inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002985typename __unique_if<_Tp>::__unique_single
2986make_unique(_Args&&... __args)
2987{
2988 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2989}
2990
2991template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002992inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002993typename __unique_if<_Tp>::__unique_array_unknown_bound
2994make_unique(size_t __n)
2995{
2996 typedef typename remove_extent<_Tp>::type _Up;
2997 return unique_ptr<_Tp>(new _Up[__n]());
2998}
2999
3000template<class _Tp, class... _Args>
3001 typename __unique_if<_Tp>::__unique_array_known_bound
3002 make_unique(_Args&&...) = delete;
3003
3004#endif // _LIBCPP_STD_VER > 11
3005
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003006template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003007#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003008struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003009#else
3010struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003011 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003012#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003013{
3014 typedef unique_ptr<_Tp, _Dp> argument_type;
3015 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003016 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003017 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003018 {
3019 typedef typename argument_type::pointer pointer;
3020 return hash<pointer>()(__ptr.get());
3021 }
3022};
3023
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024struct __destruct_n
3025{
3026private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003027 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028
3029 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003030 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003031 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032
3033 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003034 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 {}
3036
Howard Hinnant719bda32011-05-28 14:41:13 +00003037 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003038 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003039 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 {}
3041
Howard Hinnant719bda32011-05-28 14:41:13 +00003042 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003043 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003044 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045 {}
3046public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003047 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003048 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049
3050 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003051 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003052 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053
3054 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003055 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003056 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057
3058 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003059 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003060 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061};
3062
3063template <class _Alloc>
3064class __allocator_destructor
3065{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003066 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003068 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3069 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070private:
3071 _Alloc& __alloc_;
3072 size_type __s_;
3073public:
3074 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003075 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003078 void operator()(pointer __p) _NOEXCEPT
3079 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080};
3081
3082template <class _InputIterator, class _ForwardIterator>
3083_ForwardIterator
3084uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3085{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003087#ifndef _LIBCPP_NO_EXCEPTIONS
3088 _ForwardIterator __s = __r;
3089 try
3090 {
3091#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003092 for (; __f != __l; ++__f, (void) ++__r)
3093 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003094#ifndef _LIBCPP_NO_EXCEPTIONS
3095 }
3096 catch (...)
3097 {
3098 for (; __s != __r; ++__s)
3099 __s->~value_type();
3100 throw;
3101 }
3102#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103 return __r;
3104}
3105
3106template <class _InputIterator, class _Size, class _ForwardIterator>
3107_ForwardIterator
3108uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3109{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003111#ifndef _LIBCPP_NO_EXCEPTIONS
3112 _ForwardIterator __s = __r;
3113 try
3114 {
3115#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003116 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3117 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003118#ifndef _LIBCPP_NO_EXCEPTIONS
3119 }
3120 catch (...)
3121 {
3122 for (; __s != __r; ++__s)
3123 __s->~value_type();
3124 throw;
3125 }
3126#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127 return __r;
3128}
3129
3130template <class _ForwardIterator, class _Tp>
3131void
3132uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3133{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003135#ifndef _LIBCPP_NO_EXCEPTIONS
3136 _ForwardIterator __s = __f;
3137 try
3138 {
3139#endif
3140 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003141 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003142#ifndef _LIBCPP_NO_EXCEPTIONS
3143 }
3144 catch (...)
3145 {
3146 for (; __s != __f; ++__s)
3147 __s->~value_type();
3148 throw;
3149 }
3150#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151}
3152
3153template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003154_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3156{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003158#ifndef _LIBCPP_NO_EXCEPTIONS
3159 _ForwardIterator __s = __f;
3160 try
3161 {
3162#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003163 for (; __n > 0; ++__f, (void) --__n)
3164 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003165#ifndef _LIBCPP_NO_EXCEPTIONS
3166 }
3167 catch (...)
3168 {
3169 for (; __s != __f; ++__s)
3170 __s->~value_type();
3171 throw;
3172 }
3173#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003174 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175}
3176
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003177#if _LIBCPP_STD_VER > 14
3178
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003179template <class _Tp>
3180inline _LIBCPP_INLINE_VISIBILITY
3181void destroy_at(_Tp* __loc) {
3182 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3183 __loc->~_Tp();
3184}
3185
3186template <class _ForwardIterator>
3187inline _LIBCPP_INLINE_VISIBILITY
3188void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3189 for (; __first != __last; ++__first)
3190 _VSTD::destroy_at(_VSTD::addressof(*__first));
3191}
3192
3193template <class _ForwardIterator, class _Size>
3194inline _LIBCPP_INLINE_VISIBILITY
3195_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3196 for (; __n > 0; (void)++__first, --__n)
3197 _VSTD::destroy_at(_VSTD::addressof(*__first));
3198 return __first;
3199}
3200
Eric Fiselier290c07c2016-10-11 21:13:44 +00003201template <class _ForwardIterator>
3202inline _LIBCPP_INLINE_VISIBILITY
3203void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3204 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3205 auto __idx = __first;
3206#ifndef _LIBCPP_NO_EXCEPTIONS
3207 try {
3208#endif
3209 for (; __idx != __last; ++__idx)
3210 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3211#ifndef _LIBCPP_NO_EXCEPTIONS
3212 } catch (...) {
3213 _VSTD::destroy(__first, __idx);
3214 throw;
3215 }
3216#endif
3217}
3218
3219template <class _ForwardIterator, class _Size>
3220inline _LIBCPP_INLINE_VISIBILITY
3221_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3222 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3223 auto __idx = __first;
3224#ifndef _LIBCPP_NO_EXCEPTIONS
3225 try {
3226#endif
3227 for (; __n > 0; (void)++__idx, --__n)
3228 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3229 return __idx;
3230#ifndef _LIBCPP_NO_EXCEPTIONS
3231 } catch (...) {
3232 _VSTD::destroy(__first, __idx);
3233 throw;
3234 }
3235#endif
3236}
3237
3238
3239template <class _ForwardIterator>
3240inline _LIBCPP_INLINE_VISIBILITY
3241void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3242 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3243 auto __idx = __first;
3244#ifndef _LIBCPP_NO_EXCEPTIONS
3245 try {
3246#endif
3247 for (; __idx != __last; ++__idx)
3248 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3249#ifndef _LIBCPP_NO_EXCEPTIONS
3250 } catch (...) {
3251 _VSTD::destroy(__first, __idx);
3252 throw;
3253 }
3254#endif
3255}
3256
3257template <class _ForwardIterator, class _Size>
3258inline _LIBCPP_INLINE_VISIBILITY
3259_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3260 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3261 auto __idx = __first;
3262#ifndef _LIBCPP_NO_EXCEPTIONS
3263 try {
3264#endif
3265 for (; __n > 0; (void)++__idx, --__n)
3266 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3267 return __idx;
3268#ifndef _LIBCPP_NO_EXCEPTIONS
3269 } catch (...) {
3270 _VSTD::destroy(__first, __idx);
3271 throw;
3272 }
3273#endif
3274}
3275
3276
3277template <class _InputIt, class _ForwardIt>
3278inline _LIBCPP_INLINE_VISIBILITY
3279_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3280 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3281 auto __idx = __first_res;
3282#ifndef _LIBCPP_NO_EXCEPTIONS
3283 try {
3284#endif
3285 for (; __first != __last; (void)++__idx, ++__first)
3286 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3287 return __idx;
3288#ifndef _LIBCPP_NO_EXCEPTIONS
3289 } catch (...) {
3290 _VSTD::destroy(__first_res, __idx);
3291 throw;
3292 }
3293#endif
3294}
3295
3296template <class _InputIt, class _Size, class _ForwardIt>
3297inline _LIBCPP_INLINE_VISIBILITY
3298pair<_InputIt, _ForwardIt>
3299uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3300 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3301 auto __idx = __first_res;
3302#ifndef _LIBCPP_NO_EXCEPTIONS
3303 try {
3304#endif
3305 for (; __n > 0; ++__idx, (void)++__first, --__n)
3306 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3307 return {__first, __idx};
3308#ifndef _LIBCPP_NO_EXCEPTIONS
3309 } catch (...) {
3310 _VSTD::destroy(__first_res, __idx);
3311 throw;
3312 }
3313#endif
3314}
3315
3316
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003317#endif // _LIBCPP_STD_VER > 14
3318
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003319// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3320// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003321// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003322#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3323 && defined(__ATOMIC_RELAXED) \
3324 && defined(__ATOMIC_ACQ_REL)
3325# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003326#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003327# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3328#endif
3329
3330template <class _Tp>
3331inline _LIBCPP_INLINE_VISIBILITY _Tp
3332__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3333{
3334#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3335 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3336#else
3337 return __t += 1;
3338#endif
3339}
3340
3341template <class _Tp>
3342inline _LIBCPP_INLINE_VISIBILITY _Tp
3343__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3344{
3345#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3346 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3347#else
3348 return __t -= 1;
3349#endif
3350}
3351
Howard Hinnant756c69b2010-09-22 16:48:34 +00003352class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353 : public std::exception
3354{
3355public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003356 bad_weak_ptr() _NOEXCEPT = default;
3357 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003358 virtual ~bad_weak_ptr() _NOEXCEPT;
3359 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360};
3361
Louis Dionne16fe2952018-07-11 23:14:33 +00003362_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003363void __throw_bad_weak_ptr()
3364{
3365#ifndef _LIBCPP_NO_EXCEPTIONS
3366 throw bad_weak_ptr();
3367#else
3368 _VSTD::abort();
3369#endif
3370}
3371
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003372template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003374class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003375{
3376 __shared_count(const __shared_count&);
3377 __shared_count& operator=(const __shared_count&);
3378
3379protected:
3380 long __shared_owners_;
3381 virtual ~__shared_count();
3382private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003383 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384
3385public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003387 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388 : __shared_owners_(__refs) {}
3389
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003390#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003391 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003392 void __add_shared() _NOEXCEPT;
3393 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003394#else
3395 _LIBCPP_INLINE_VISIBILITY
3396 void __add_shared() _NOEXCEPT {
3397 __libcpp_atomic_refcount_increment(__shared_owners_);
3398 }
3399 _LIBCPP_INLINE_VISIBILITY
3400 bool __release_shared() _NOEXCEPT {
3401 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3402 __on_zero_shared();
3403 return true;
3404 }
3405 return false;
3406 }
3407#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003408 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003409 long use_count() const _NOEXCEPT {
3410 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3411 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412};
3413
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003414class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415 : private __shared_count
3416{
3417 long __shared_weak_owners_;
3418
3419public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003421 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003422 : __shared_count(__refs),
3423 __shared_weak_owners_(__refs) {}
3424protected:
3425 virtual ~__shared_weak_count();
3426
3427public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003428#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003429 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003430 void __add_shared() _NOEXCEPT;
3431 void __add_weak() _NOEXCEPT;
3432 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003433#else
3434 _LIBCPP_INLINE_VISIBILITY
3435 void __add_shared() _NOEXCEPT {
3436 __shared_count::__add_shared();
3437 }
3438 _LIBCPP_INLINE_VISIBILITY
3439 void __add_weak() _NOEXCEPT {
3440 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3441 }
3442 _LIBCPP_INLINE_VISIBILITY
3443 void __release_shared() _NOEXCEPT {
3444 if (__shared_count::__release_shared())
3445 __release_weak();
3446 }
3447#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003448 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003450 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3451 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452
Howard Hinnant807d6332013-02-25 15:50:36 +00003453 // Define the function out only if we build static libc++ without RTTI.
3454 // Otherwise we may break clients who need to compile their projects with
3455 // -fno-rtti and yet link against a libc++.dylib compiled
3456 // without -fno-rtti.
3457#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003458 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003459#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003461 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462};
3463
3464template <class _Tp, class _Dp, class _Alloc>
3465class __shared_ptr_pointer
3466 : public __shared_weak_count
3467{
3468 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3469public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003471 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003472 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473
Howard Hinnant72f73582010-08-11 17:04:31 +00003474#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003475 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003476#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477
3478private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003479 virtual void __on_zero_shared() _NOEXCEPT;
3480 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481};
3482
Howard Hinnant72f73582010-08-11 17:04:31 +00003483#ifndef _LIBCPP_NO_RTTI
3484
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485template <class _Tp, class _Dp, class _Alloc>
3486const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003487__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003489 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490}
3491
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003492#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003493
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494template <class _Tp, class _Dp, class _Alloc>
3495void
Howard Hinnant719bda32011-05-28 14:41:13 +00003496__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497{
3498 __data_.first().second()(__data_.first().first());
3499 __data_.first().second().~_Dp();
3500}
3501
3502template <class _Tp, class _Dp, class _Alloc>
3503void
Howard Hinnant719bda32011-05-28 14:41:13 +00003504__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003506 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3507 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003508 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3509
Eric Fiselierf8898c82015-02-05 23:01:40 +00003510 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003512 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513}
3514
3515template <class _Tp, class _Alloc>
3516class __shared_ptr_emplace
3517 : public __shared_weak_count
3518{
3519 __compressed_pair<_Alloc, _Tp> __data_;
3520public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521
Howard Hinnant756c69b2010-09-22 16:48:34 +00003522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003524 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003526
3527#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003531 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3532 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533#else // _LIBCPP_HAS_NO_VARIADICS
3534
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3538 : __data_(__a, _Tp(__a0)) {}
3539
3540 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3543 : __data_(__a, _Tp(__a0, __a1)) {}
3544
3545 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3548 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3549
3550#endif // _LIBCPP_HAS_NO_VARIADICS
3551
3552private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003553 virtual void __on_zero_shared() _NOEXCEPT;
3554 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003556 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003557 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558};
3559
3560template <class _Tp, class _Alloc>
3561void
Howard Hinnant719bda32011-05-28 14:41:13 +00003562__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563{
3564 __data_.second().~_Tp();
3565}
3566
3567template <class _Tp, class _Alloc>
3568void
Howard Hinnant719bda32011-05-28 14:41:13 +00003569__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003571 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3572 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003573 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003574 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003576 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577}
3578
Erik Pilkington2a398762017-05-25 15:43:31 +00003579struct __shared_ptr_dummy_rebind_allocator_type;
3580template <>
3581class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3582{
3583public:
3584 template <class _Other>
3585 struct rebind
3586 {
3587 typedef allocator<_Other> other;
3588 };
3589};
3590
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003591template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592
zoecarverd73f42a2020-05-11 18:42:50 -07003593template<class _Tp, class _Up>
3594struct __compatible_with
3595#if _LIBCPP_STD_VER > 14
3596 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3597#else
3598 : is_convertible<_Tp*, _Up*> {};
3599#endif // _LIBCPP_STD_VER > 14
3600
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003602class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003604public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003605#if _LIBCPP_STD_VER > 14
3606 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003607 typedef remove_extent_t<_Tp> element_type;
3608#else
3609 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003610#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003611
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612private:
3613 element_type* __ptr_;
3614 __shared_weak_count* __cntrl_;
3615
3616 struct __nat {int __for_bool_;};
3617public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003619 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003621 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003622 template<class _Yp>
3623 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003624 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003625 template<class _Yp, class _Dp>
3626 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003627 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003628 template<class _Yp, class _Dp, class _Alloc>
3629 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003630 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3632 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003633 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003635 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003639 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003640 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003641#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003643 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003644 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003645 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003646 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003647#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003649 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003650#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003651#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003652 template<class _Yp>
3653 shared_ptr(auto_ptr<_Yp>&& __r,
3654 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003655#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003656 template<class _Yp>
3657 shared_ptr(auto_ptr<_Yp> __r,
3658 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003660#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003661#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003662 template <class _Yp, class _Dp>
3663 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3664 typename enable_if
3665 <
3666 !is_lvalue_reference<_Dp>::value &&
3667 !is_array<_Yp>::value &&
3668 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3669 __nat
3670 >::type = __nat());
3671 template <class _Yp, class _Dp>
3672 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3673 typename enable_if
3674 <
3675 is_lvalue_reference<_Dp>::value &&
3676 !is_array<_Yp>::value &&
3677 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3678 __nat
3679 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003680#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003681 template <class _Yp, class _Dp>
3682 shared_ptr(unique_ptr<_Yp, _Dp>,
3683 typename enable_if
3684 <
3685 !is_lvalue_reference<_Dp>::value &&
3686 !is_array<_Yp>::value &&
3687 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3688 __nat
3689 >::type = __nat());
3690 template <class _Yp, class _Dp>
3691 shared_ptr(unique_ptr<_Yp, _Dp>,
3692 typename enable_if
3693 <
3694 is_lvalue_reference<_Dp>::value &&
3695 !is_array<_Yp>::value &&
3696 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3697 __nat
3698 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003699#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700
3701 ~shared_ptr();
3702
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003704 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003705 template<class _Yp>
3706 typename enable_if
3707 <
zoecarverd73f42a2020-05-11 18:42:50 -07003708 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003709 shared_ptr&
3710 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003712 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003713#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003715 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003716 template<class _Yp>
3717 typename enable_if
3718 <
zoecarverd73f42a2020-05-11 18:42:50 -07003719 __compatible_with<_Yp, element_type>::value,
3720 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003721 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003723 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003724#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003725 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003727 typename enable_if
3728 <
3729 !is_array<_Yp>::value &&
3730 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003731 shared_ptr
3732 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003733 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003734#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003735#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003736#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003737 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003739 typename enable_if
3740 <
3741 !is_array<_Yp>::value &&
3742 is_convertible<_Yp*, element_type*>::value,
3743 shared_ptr&
3744 >::type
3745 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003746#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003747#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003748 template <class _Yp, class _Dp>
3749 typename enable_if
3750 <
3751 !is_array<_Yp>::value &&
3752 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3753 shared_ptr&
3754 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003755#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003757 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003758#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003760 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761#endif
3762
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003764 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003766 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003767 template<class _Yp>
3768 typename enable_if
3769 <
zoecarverd73f42a2020-05-11 18:42:50 -07003770 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003771 void
3772 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003774 reset(_Yp* __p);
3775 template<class _Yp, class _Dp>
3776 typename enable_if
3777 <
zoecarverd73f42a2020-05-11 18:42:50 -07003778 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003779 void
3780 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003782 reset(_Yp* __p, _Dp __d);
3783 template<class _Yp, class _Dp, class _Alloc>
3784 typename enable_if
3785 <
zoecarverd73f42a2020-05-11 18:42:50 -07003786 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003787 void
3788 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003790 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791
Howard Hinnant756c69b2010-09-22 16:48:34 +00003792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003793 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003795 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3796 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003797 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003798 element_type* operator->() const _NOEXCEPT
3799 {
3800 static_assert(!_VSTD::is_array<_Tp>::value,
3801 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3802 return __ptr_;
3803 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003805 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003807 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003809 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003810 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003811 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003812 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003814 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003815 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003816 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003818 _LIBCPP_INLINE_VISIBILITY
3819 bool
3820 __owner_equivalent(const shared_ptr& __p) const
3821 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822
zoecarverd73f42a2020-05-11 18:42:50 -07003823#if _LIBCPP_STD_VER > 14
3824 typename add_lvalue_reference<element_type>::type
3825 _LIBCPP_INLINE_VISIBILITY
3826 operator[](ptrdiff_t __i) const
3827 {
3828 static_assert(_VSTD::is_array<_Tp>::value,
3829 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3830 return __ptr_[__i];
3831 }
3832#endif
3833
Howard Hinnant72f73582010-08-11 17:04:31 +00003834#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003837 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003838 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003839 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003840 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003841#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842
Zoe Carverd9040c72019-10-22 15:16:49 +00003843 template<class _Yp, class _CntrlBlk>
3844 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07003845 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00003846 {
3847 shared_ptr<_Tp> __r;
3848 __r.__ptr_ = __p;
3849 __r.__cntrl_ = __cntrl;
3850 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3851 return __r;
3852 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003853
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003855 template <class _Yp, bool = is_function<_Yp>::value>
3856 struct __shared_ptr_default_allocator
3857 {
3858 typedef allocator<_Yp> type;
3859 };
3860
3861 template <class _Yp>
3862 struct __shared_ptr_default_allocator<_Yp, true>
3863 {
3864 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3865 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003867 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003868 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003869 typename enable_if<is_convertible<_OrigPtr*,
3870 const enable_shared_from_this<_Yp>*
3871 >::value,
3872 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003873 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3874 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003876 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003877 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003878 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003879 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3880 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003881 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882 }
3883
Erik Pilkington2a398762017-05-25 15:43:31 +00003884 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885
zoecarverd73f42a2020-05-11 18:42:50 -07003886 template <class, class _Yp>
3887 struct __shared_ptr_default_delete
3888 : default_delete<_Yp> {};
3889
3890 template <class _Yp, class _Un, size_t _Sz>
3891 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3892 : default_delete<_Yp[]> {};
3893
3894 template <class _Yp, class _Un>
3895 struct __shared_ptr_default_delete<_Yp[], _Un>
3896 : default_delete<_Yp[]> {};
3897
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003898 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3899 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900};
3901
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003902#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3903template<class _Tp>
3904shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3905template<class _Tp, class _Dp>
3906shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3907#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003908
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003910inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003911_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003912shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913 : __ptr_(0),
3914 __cntrl_(0)
3915{
3916}
3917
3918template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003919inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003920_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003921shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003922 : __ptr_(0),
3923 __cntrl_(0)
3924{
3925}
3926
3927template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003928template<class _Yp>
3929shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003930 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003931 : __ptr_(__p)
3932{
3933 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003934 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07003935 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3936 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003938 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939}
3940
3941template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003942template<class _Yp, class _Dp>
3943shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003944 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945 : __ptr_(__p)
3946{
3947#ifndef _LIBCPP_NO_EXCEPTIONS
3948 try
3949 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003950#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003951 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3952 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3953 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003954 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955#ifndef _LIBCPP_NO_EXCEPTIONS
3956 }
3957 catch (...)
3958 {
3959 __d(__p);
3960 throw;
3961 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003962#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963}
3964
3965template<class _Tp>
3966template<class _Dp>
3967shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3968 : __ptr_(0)
3969{
3970#ifndef _LIBCPP_NO_EXCEPTIONS
3971 try
3972 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003973#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003974 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3975 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3976 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977#ifndef _LIBCPP_NO_EXCEPTIONS
3978 }
3979 catch (...)
3980 {
3981 __d(__p);
3982 throw;
3983 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003984#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985}
3986
3987template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003988template<class _Yp, class _Dp, class _Alloc>
3989shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003990 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991 : __ptr_(__p)
3992{
3993#ifndef _LIBCPP_NO_EXCEPTIONS
3994 try
3995 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003996#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003998 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999 typedef __allocator_destructor<_A2> _D2;
4000 _A2 __a2(__a);
4001 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004002 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4003 _CntrlBlk(__p, __d, __a);
4004 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004005 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006#ifndef _LIBCPP_NO_EXCEPTIONS
4007 }
4008 catch (...)
4009 {
4010 __d(__p);
4011 throw;
4012 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004013#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014}
4015
4016template<class _Tp>
4017template<class _Dp, class _Alloc>
4018shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4019 : __ptr_(0)
4020{
4021#ifndef _LIBCPP_NO_EXCEPTIONS
4022 try
4023 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004024#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004026 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027 typedef __allocator_destructor<_A2> _D2;
4028 _A2 __a2(__a);
4029 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004030 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4031 _CntrlBlk(__p, __d, __a);
4032 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033#ifndef _LIBCPP_NO_EXCEPTIONS
4034 }
4035 catch (...)
4036 {
4037 __d(__p);
4038 throw;
4039 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004040#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041}
4042
4043template<class _Tp>
4044template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004045inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004046shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047 : __ptr_(__p),
4048 __cntrl_(__r.__cntrl_)
4049{
4050 if (__cntrl_)
4051 __cntrl_->__add_shared();
4052}
4053
4054template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004055inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004056shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057 : __ptr_(__r.__ptr_),
4058 __cntrl_(__r.__cntrl_)
4059{
4060 if (__cntrl_)
4061 __cntrl_->__add_shared();
4062}
4063
4064template<class _Tp>
4065template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004066inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07004068 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004069 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070 : __ptr_(__r.__ptr_),
4071 __cntrl_(__r.__cntrl_)
4072{
4073 if (__cntrl_)
4074 __cntrl_->__add_shared();
4075}
4076
Howard Hinnant74279a52010-09-04 23:28:19 +00004077#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078
4079template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004080inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004081shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082 : __ptr_(__r.__ptr_),
4083 __cntrl_(__r.__cntrl_)
4084{
4085 __r.__ptr_ = 0;
4086 __r.__cntrl_ = 0;
4087}
4088
4089template<class _Tp>
4090template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004091inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07004093 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004094 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095 : __ptr_(__r.__ptr_),
4096 __cntrl_(__r.__cntrl_)
4097{
4098 __r.__ptr_ = 0;
4099 __r.__cntrl_ = 0;
4100}
4101
Howard Hinnant74279a52010-09-04 23:28:19 +00004102#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103
Marshall Clowb22274f2017-01-24 22:22:33 +00004104#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004106template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004107#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004108shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004110shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004112 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113 : __ptr_(__r.get())
4114{
4115 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4116 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004117 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118 __r.release();
4119}
Marshall Clowb22274f2017-01-24 22:22:33 +00004120#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121
4122template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004123template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004124#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4126#else
4127shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4128#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004129 typename enable_if
4130 <
4131 !is_lvalue_reference<_Dp>::value &&
4132 !is_array<_Yp>::value &&
4133 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4134 __nat
4135 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136 : __ptr_(__r.get())
4137{
Marshall Clow35cde742015-05-10 13:59:45 +00004138#if _LIBCPP_STD_VER > 11
4139 if (__ptr_ == nullptr)
4140 __cntrl_ = nullptr;
4141 else
4142#endif
4143 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004144 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4145 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4146 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004147 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004148 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149 __r.release();
4150}
4151
4152template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004153template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004154#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4156#else
4157shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4158#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004159 typename enable_if
4160 <
4161 is_lvalue_reference<_Dp>::value &&
4162 !is_array<_Yp>::value &&
4163 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4164 __nat
4165 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166 : __ptr_(__r.get())
4167{
Marshall Clow35cde742015-05-10 13:59:45 +00004168#if _LIBCPP_STD_VER > 11
4169 if (__ptr_ == nullptr)
4170 __cntrl_ = nullptr;
4171 else
4172#endif
4173 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004174 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004175 typedef __shared_ptr_pointer<_Yp*,
4176 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004177 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004178 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004179 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004180 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181 __r.release();
4182}
4183
Zoe Carver6cd05c32019-08-19 15:47:16 +00004184template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185shared_ptr<_Tp>::~shared_ptr()
4186{
4187 if (__cntrl_)
4188 __cntrl_->__release_shared();
4189}
4190
4191template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004194shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195{
4196 shared_ptr(__r).swap(*this);
4197 return *this;
4198}
4199
4200template<class _Tp>
4201template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004202inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004203typename enable_if
4204<
zoecarverd73f42a2020-05-11 18:42:50 -07004205 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004206 shared_ptr<_Tp>&
4207>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004208shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209{
4210 shared_ptr(__r).swap(*this);
4211 return *this;
4212}
4213
Howard Hinnant74279a52010-09-04 23:28:19 +00004214#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215
4216template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004217inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004219shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004221 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222 return *this;
4223}
4224
4225template<class _Tp>
4226template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004227inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004228typename enable_if
4229<
zoecarverd73f42a2020-05-11 18:42:50 -07004230 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004231 shared_ptr<_Tp>&
4232>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4234{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004235 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004236 return *this;
4237}
4238
Marshall Clowb22274f2017-01-24 22:22:33 +00004239#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004240template<class _Tp>
4241template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004242inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004243typename enable_if
4244<
4245 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004246 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004247 shared_ptr<_Tp>
4248>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4250{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004251 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252 return *this;
4253}
Marshall Clowb22274f2017-01-24 22:22:33 +00004254#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255
4256template<class _Tp>
4257template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004258inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004259typename enable_if
4260<
4261 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004262 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004263 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004264 shared_ptr<_Tp>&
4265>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004266shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4267{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004268 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269 return *this;
4270}
4271
Howard Hinnant74279a52010-09-04 23:28:19 +00004272#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273
Marshall Clowb22274f2017-01-24 22:22:33 +00004274#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275template<class _Tp>
4276template<class _Yp>
4277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004278typename enable_if
4279<
4280 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004281 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004282 shared_ptr<_Tp>&
4283>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004284shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285{
4286 shared_ptr(__r).swap(*this);
4287 return *this;
4288}
Marshall Clowb22274f2017-01-24 22:22:33 +00004289#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290
4291template<class _Tp>
4292template <class _Yp, class _Dp>
4293inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004294typename enable_if
4295<
4296 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004297 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004298 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004299 shared_ptr<_Tp>&
4300>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4302{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004303 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304 return *this;
4305}
4306
Howard Hinnant74279a52010-09-04 23:28:19 +00004307#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308
4309template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004310inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311void
Howard Hinnant719bda32011-05-28 14:41:13 +00004312shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004314 _VSTD::swap(__ptr_, __r.__ptr_);
4315 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004316}
4317
4318template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004319inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004320void
Howard Hinnant719bda32011-05-28 14:41:13 +00004321shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322{
4323 shared_ptr().swap(*this);
4324}
4325
4326template<class _Tp>
4327template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004328inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004329typename enable_if
4330<
zoecarverd73f42a2020-05-11 18:42:50 -07004331 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004332 void
4333>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334shared_ptr<_Tp>::reset(_Yp* __p)
4335{
4336 shared_ptr(__p).swap(*this);
4337}
4338
4339template<class _Tp>
4340template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004341inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004342typename enable_if
4343<
zoecarverd73f42a2020-05-11 18:42:50 -07004344 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004345 void
4346>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4348{
4349 shared_ptr(__p, __d).swap(*this);
4350}
4351
4352template<class _Tp>
4353template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004354inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004355typename enable_if
4356<
zoecarverd73f42a2020-05-11 18:42:50 -07004357 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004358 void
4359>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4361{
4362 shared_ptr(__p, __d, __a).swap(*this);
4363}
4364
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004365template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004366inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004367typename enable_if
4368<
4369 !is_array<_Tp>::value,
4370 shared_ptr<_Tp>
4371>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372make_shared(_Args&& ...__args)
4373{
Zoe Carverd9040c72019-10-22 15:16:49 +00004374 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4375 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4376 typedef allocator<_CntrlBlk> _A2;
4377 typedef __allocator_destructor<_A2> _D2;
4378
4379 _A2 __a2;
4380 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4381 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4382
4383 _Tp *__ptr = __hold2.get()->get();
4384 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385}
4386
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004387template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004389typename enable_if
4390<
4391 !is_array<_Tp>::value,
4392 shared_ptr<_Tp>
4393>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394allocate_shared(const _Alloc& __a, _Args&& ...__args)
4395{
zoecarver505730a2020-02-25 16:50:57 -08004396 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4397
4398 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4399 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4400 typedef __allocator_destructor<_A2> _D2;
4401
4402 _A2 __a2(__a);
4403 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4404 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4405 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4406
4407 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4408 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004409}
4410
Howard Hinnantc51e1022010-05-11 19:42:16 +00004411template<class _Tp, class _Up>
4412inline _LIBCPP_INLINE_VISIBILITY
4413bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004414operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415{
4416 return __x.get() == __y.get();
4417}
4418
4419template<class _Tp, class _Up>
4420inline _LIBCPP_INLINE_VISIBILITY
4421bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004422operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423{
4424 return !(__x == __y);
4425}
4426
4427template<class _Tp, class _Up>
4428inline _LIBCPP_INLINE_VISIBILITY
4429bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004430operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004431{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004432#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004433 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4434 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004435#else
4436 return less<>()(__x.get(), __y.get());
4437#endif
4438
Howard Hinnantb17caf92012-02-21 21:02:58 +00004439}
4440
4441template<class _Tp, class _Up>
4442inline _LIBCPP_INLINE_VISIBILITY
4443bool
4444operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4445{
4446 return __y < __x;
4447}
4448
4449template<class _Tp, class _Up>
4450inline _LIBCPP_INLINE_VISIBILITY
4451bool
4452operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4453{
4454 return !(__y < __x);
4455}
4456
4457template<class _Tp, class _Up>
4458inline _LIBCPP_INLINE_VISIBILITY
4459bool
4460operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4461{
4462 return !(__x < __y);
4463}
4464
4465template<class _Tp>
4466inline _LIBCPP_INLINE_VISIBILITY
4467bool
4468operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4469{
4470 return !__x;
4471}
4472
4473template<class _Tp>
4474inline _LIBCPP_INLINE_VISIBILITY
4475bool
4476operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4477{
4478 return !__x;
4479}
4480
4481template<class _Tp>
4482inline _LIBCPP_INLINE_VISIBILITY
4483bool
4484operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4485{
4486 return static_cast<bool>(__x);
4487}
4488
4489template<class _Tp>
4490inline _LIBCPP_INLINE_VISIBILITY
4491bool
4492operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4493{
4494 return static_cast<bool>(__x);
4495}
4496
4497template<class _Tp>
4498inline _LIBCPP_INLINE_VISIBILITY
4499bool
4500operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4501{
4502 return less<_Tp*>()(__x.get(), nullptr);
4503}
4504
4505template<class _Tp>
4506inline _LIBCPP_INLINE_VISIBILITY
4507bool
4508operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4509{
4510 return less<_Tp*>()(nullptr, __x.get());
4511}
4512
4513template<class _Tp>
4514inline _LIBCPP_INLINE_VISIBILITY
4515bool
4516operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4517{
4518 return nullptr < __x;
4519}
4520
4521template<class _Tp>
4522inline _LIBCPP_INLINE_VISIBILITY
4523bool
4524operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4525{
4526 return __x < nullptr;
4527}
4528
4529template<class _Tp>
4530inline _LIBCPP_INLINE_VISIBILITY
4531bool
4532operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4533{
4534 return !(nullptr < __x);
4535}
4536
4537template<class _Tp>
4538inline _LIBCPP_INLINE_VISIBILITY
4539bool
4540operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4541{
4542 return !(__x < nullptr);
4543}
4544
4545template<class _Tp>
4546inline _LIBCPP_INLINE_VISIBILITY
4547bool
4548operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4549{
4550 return !(__x < nullptr);
4551}
4552
4553template<class _Tp>
4554inline _LIBCPP_INLINE_VISIBILITY
4555bool
4556operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4557{
4558 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004559}
4560
4561template<class _Tp>
4562inline _LIBCPP_INLINE_VISIBILITY
4563void
Howard Hinnant719bda32011-05-28 14:41:13 +00004564swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565{
4566 __x.swap(__y);
4567}
4568
4569template<class _Tp, class _Up>
4570inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004571shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004572static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573{
zoecarverd73f42a2020-05-11 18:42:50 -07004574 return shared_ptr<_Tp>(__r,
4575 static_cast<
4576 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577}
4578
4579template<class _Tp, class _Up>
4580inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004581shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004582dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004583{
zoecarverd73f42a2020-05-11 18:42:50 -07004584 typedef typename shared_ptr<_Tp>::element_type _ET;
4585 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004586 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4587}
4588
4589template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004590shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004591const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592{
zoecarverd73f42a2020-05-11 18:42:50 -07004593 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004594 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004595}
4596
zoecarverd73f42a2020-05-11 18:42:50 -07004597template<class _Tp, class _Up>
4598shared_ptr<_Tp>
4599reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4600{
4601 return shared_ptr<_Tp>(__r,
4602 reinterpret_cast<
4603 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4604}
4605
Howard Hinnant72f73582010-08-11 17:04:31 +00004606#ifndef _LIBCPP_NO_RTTI
4607
Howard Hinnantc51e1022010-05-11 19:42:16 +00004608template<class _Dp, class _Tp>
4609inline _LIBCPP_INLINE_VISIBILITY
4610_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004611get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004612{
4613 return __p.template __get_deleter<_Dp>();
4614}
4615
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004616#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004617
Howard Hinnantc51e1022010-05-11 19:42:16 +00004618template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004619class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004620{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004621public:
4622 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004623private:
4624 element_type* __ptr_;
4625 __shared_weak_count* __cntrl_;
4626
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004627public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004629 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004630 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004631 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4632 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004634 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004635 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004636 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4637 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004638
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004639#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004641 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004642 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004643 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4644 _NOEXCEPT;
4645#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004646 ~weak_ptr();
4647
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004649 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004650 template<class _Yp>
4651 typename enable_if
4652 <
4653 is_convertible<_Yp*, element_type*>::value,
4654 weak_ptr&
4655 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004657 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4658
4659#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4660
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004662 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4663 template<class _Yp>
4664 typename enable_if
4665 <
4666 is_convertible<_Yp*, element_type*>::value,
4667 weak_ptr&
4668 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004670 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4671
4672#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4673
4674 template<class _Yp>
4675 typename enable_if
4676 <
4677 is_convertible<_Yp*, element_type*>::value,
4678 weak_ptr&
4679 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004681 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004682
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004684 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004686 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004687
Howard Hinnant756c69b2010-09-22 16:48:34 +00004688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004689 long use_count() const _NOEXCEPT
4690 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004692 bool expired() const _NOEXCEPT
4693 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4694 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004695 template<class _Up>
4696 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004697 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004698 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004699 template<class _Up>
4700 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004701 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004702 {return __cntrl_ < __r.__cntrl_;}
4703
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004704 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4705 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004706};
4707
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004708#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4709template<class _Tp>
4710weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4711#endif
4712
Howard Hinnantc51e1022010-05-11 19:42:16 +00004713template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004714inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004715_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004716weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004717 : __ptr_(0),
4718 __cntrl_(0)
4719{
4720}
4721
4722template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004723inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004724weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004725 : __ptr_(__r.__ptr_),
4726 __cntrl_(__r.__cntrl_)
4727{
4728 if (__cntrl_)
4729 __cntrl_->__add_weak();
4730}
4731
4732template<class _Tp>
4733template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004734inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004735weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004736 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004737 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004738 : __ptr_(__r.__ptr_),
4739 __cntrl_(__r.__cntrl_)
4740{
4741 if (__cntrl_)
4742 __cntrl_->__add_weak();
4743}
4744
4745template<class _Tp>
4746template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004747inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004748weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004749 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004750 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004751 : __ptr_(__r.__ptr_),
4752 __cntrl_(__r.__cntrl_)
4753{
4754 if (__cntrl_)
4755 __cntrl_->__add_weak();
4756}
4757
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4759
4760template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004761inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004762weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4763 : __ptr_(__r.__ptr_),
4764 __cntrl_(__r.__cntrl_)
4765{
4766 __r.__ptr_ = 0;
4767 __r.__cntrl_ = 0;
4768}
4769
4770template<class _Tp>
4771template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004772inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004773weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4774 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4775 _NOEXCEPT
4776 : __ptr_(__r.__ptr_),
4777 __cntrl_(__r.__cntrl_)
4778{
4779 __r.__ptr_ = 0;
4780 __r.__cntrl_ = 0;
4781}
4782
4783#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4784
Howard Hinnantc51e1022010-05-11 19:42:16 +00004785template<class _Tp>
4786weak_ptr<_Tp>::~weak_ptr()
4787{
4788 if (__cntrl_)
4789 __cntrl_->__release_weak();
4790}
4791
4792template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004793inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004794weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004795weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004796{
4797 weak_ptr(__r).swap(*this);
4798 return *this;
4799}
4800
4801template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004802template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004803inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004804typename enable_if
4805<
4806 is_convertible<_Yp*, _Tp*>::value,
4807 weak_ptr<_Tp>&
4808>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004809weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004810{
4811 weak_ptr(__r).swap(*this);
4812 return *this;
4813}
4814
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004815#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4816
4817template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004818inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004819weak_ptr<_Tp>&
4820weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4821{
4822 weak_ptr(_VSTD::move(__r)).swap(*this);
4823 return *this;
4824}
4825
Howard Hinnantc51e1022010-05-11 19:42:16 +00004826template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004827template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004828inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004829typename enable_if
4830<
4831 is_convertible<_Yp*, _Tp*>::value,
4832 weak_ptr<_Tp>&
4833>::type
4834weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4835{
4836 weak_ptr(_VSTD::move(__r)).swap(*this);
4837 return *this;
4838}
4839
4840#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4841
4842template<class _Tp>
4843template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004844inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004845typename enable_if
4846<
4847 is_convertible<_Yp*, _Tp*>::value,
4848 weak_ptr<_Tp>&
4849>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004850weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004851{
4852 weak_ptr(__r).swap(*this);
4853 return *this;
4854}
4855
4856template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004857inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004858void
Howard Hinnant719bda32011-05-28 14:41:13 +00004859weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004860{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004861 _VSTD::swap(__ptr_, __r.__ptr_);
4862 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004863}
4864
4865template<class _Tp>
4866inline _LIBCPP_INLINE_VISIBILITY
4867void
Howard Hinnant719bda32011-05-28 14:41:13 +00004868swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869{
4870 __x.swap(__y);
4871}
4872
4873template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004874inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004875void
Howard Hinnant719bda32011-05-28 14:41:13 +00004876weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004877{
4878 weak_ptr().swap(*this);
4879}
4880
4881template<class _Tp>
4882template<class _Yp>
4883shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004884 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004885 : __ptr_(__r.__ptr_),
4886 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4887{
4888 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004889 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004890}
4891
4892template<class _Tp>
4893shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004894weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004895{
4896 shared_ptr<_Tp> __r;
4897 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4898 if (__r.__cntrl_)
4899 __r.__ptr_ = __ptr_;
4900 return __r;
4901}
4902
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004903#if _LIBCPP_STD_VER > 14
4904template <class _Tp = void> struct owner_less;
4905#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004906template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908
4909template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004910struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004911 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004912{
4913 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004914 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004915 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004916 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004917 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004918 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004919 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004920 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004921 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004922 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004923};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004924
4925template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004926struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004927 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4928{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004929 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004930 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004931 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004932 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004933 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004934 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004935 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004936 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004937 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004938 {return __x.owner_before(__y);}
4939};
4940
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004941#if _LIBCPP_STD_VER > 14
4942template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004943struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004944{
4945 template <class _Tp, class _Up>
4946 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004947 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004948 {return __x.owner_before(__y);}
4949 template <class _Tp, class _Up>
4950 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004951 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004952 {return __x.owner_before(__y);}
4953 template <class _Tp, class _Up>
4954 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004955 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004956 {return __x.owner_before(__y);}
4957 template <class _Tp, class _Up>
4958 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004959 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004960 {return __x.owner_before(__y);}
4961 typedef void is_transparent;
4962};
4963#endif
4964
Howard Hinnantc51e1022010-05-11 19:42:16 +00004965template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004966class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004967{
4968 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004969protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004970 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004971 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004973 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004975 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4976 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004978 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004979public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004981 shared_ptr<_Tp> shared_from_this()
4982 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004984 shared_ptr<_Tp const> shared_from_this() const
4985 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004986
Eric Fiselier84006862016-06-02 00:15:35 +00004987#if _LIBCPP_STD_VER > 14
4988 _LIBCPP_INLINE_VISIBILITY
4989 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4990 { return __weak_this_; }
4991
4992 _LIBCPP_INLINE_VISIBILITY
4993 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4994 { return __weak_this_; }
4995#endif // _LIBCPP_STD_VER > 14
4996
Howard Hinnantc51e1022010-05-11 19:42:16 +00004997 template <class _Up> friend class shared_ptr;
4998};
4999
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005000template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005001struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005002{
5003 typedef shared_ptr<_Tp> argument_type;
5004 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005005
Howard Hinnant756c69b2010-09-22 16:48:34 +00005006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005007 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005008 {
zoecarverd73f42a2020-05-11 18:42:50 -07005009 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005010 }
5011};
5012
Howard Hinnantc834c512011-11-29 18:15:50 +00005013template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005014inline _LIBCPP_INLINE_VISIBILITY
5015basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005016operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005017
Eric Fiselier9b492672016-06-18 02:12:53 +00005018
5019#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005020
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005021class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005022{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005023 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005024public:
5025 void lock() _NOEXCEPT;
5026 void unlock() _NOEXCEPT;
5027
5028private:
5029 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5030 __sp_mut(const __sp_mut&);
5031 __sp_mut& operator=(const __sp_mut&);
5032
Howard Hinnant8331b762013-03-06 23:30:19 +00005033 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005034};
5035
Mehdi Amini228053d2017-05-04 17:08:54 +00005036_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5037__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005038
5039template <class _Tp>
5040inline _LIBCPP_INLINE_VISIBILITY
5041bool
5042atomic_is_lock_free(const shared_ptr<_Tp>*)
5043{
5044 return false;
5045}
5046
5047template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005048_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005049shared_ptr<_Tp>
5050atomic_load(const shared_ptr<_Tp>* __p)
5051{
5052 __sp_mut& __m = __get_sp_mut(__p);
5053 __m.lock();
5054 shared_ptr<_Tp> __q = *__p;
5055 __m.unlock();
5056 return __q;
5057}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005058
Howard Hinnant9fa30202012-07-30 01:40:57 +00005059template <class _Tp>
5060inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005061_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005062shared_ptr<_Tp>
5063atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5064{
5065 return atomic_load(__p);
5066}
5067
5068template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005069_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005070void
5071atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5072{
5073 __sp_mut& __m = __get_sp_mut(__p);
5074 __m.lock();
5075 __p->swap(__r);
5076 __m.unlock();
5077}
5078
5079template <class _Tp>
5080inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005081_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005082void
5083atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5084{
5085 atomic_store(__p, __r);
5086}
5087
5088template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005089_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005090shared_ptr<_Tp>
5091atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5092{
5093 __sp_mut& __m = __get_sp_mut(__p);
5094 __m.lock();
5095 __p->swap(__r);
5096 __m.unlock();
5097 return __r;
5098}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005099
Howard Hinnant9fa30202012-07-30 01:40:57 +00005100template <class _Tp>
5101inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005102_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005103shared_ptr<_Tp>
5104atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5105{
5106 return atomic_exchange(__p, __r);
5107}
5108
5109template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005110_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005111bool
5112atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5113{
Marshall Clow4201ee82016-05-18 17:50:13 +00005114 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005115 __sp_mut& __m = __get_sp_mut(__p);
5116 __m.lock();
5117 if (__p->__owner_equivalent(*__v))
5118 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005119 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005120 *__p = __w;
5121 __m.unlock();
5122 return true;
5123 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005124 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005125 *__v = *__p;
5126 __m.unlock();
5127 return false;
5128}
5129
5130template <class _Tp>
5131inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005132_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005133bool
5134atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5135{
5136 return atomic_compare_exchange_strong(__p, __v, __w);
5137}
5138
5139template <class _Tp>
5140inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005141_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005142bool
5143atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5144 shared_ptr<_Tp> __w, memory_order, memory_order)
5145{
5146 return atomic_compare_exchange_strong(__p, __v, __w);
5147}
5148
5149template <class _Tp>
5150inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005151_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005152bool
5153atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5154 shared_ptr<_Tp> __w, memory_order, memory_order)
5155{
5156 return atomic_compare_exchange_weak(__p, __v, __w);
5157}
5158
Eric Fiselier9b492672016-06-18 02:12:53 +00005159#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005160
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005161//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005162#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5163# ifndef _LIBCPP_CXX03_LANG
5164enum class pointer_safety : unsigned char {
5165 relaxed,
5166 preferred,
5167 strict
5168};
5169# endif
5170#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005171struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005172{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005173 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005174 {
5175 relaxed,
5176 preferred,
5177 strict
5178 };
5179
Howard Hinnant49e145e2012-10-30 19:06:59 +00005180 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005181
Howard Hinnant756c69b2010-09-22 16:48:34 +00005182 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005183 pointer_safety() : __v_() {}
5184
5185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005186 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005188 operator int() const {return __v_;}
5189};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005190#endif
5191
5192#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005193 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005194_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5195#else
5196// This function is only offered in C++03 under ABI v1.
5197# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5198inline _LIBCPP_INLINE_VISIBILITY
5199pointer_safety get_pointer_safety() _NOEXCEPT {
5200 return pointer_safety::relaxed;
5201}
5202# endif
5203#endif
5204
Howard Hinnantc51e1022010-05-11 19:42:16 +00005205
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005206_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5207_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5208_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005209_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005210
5211template <class _Tp>
5212inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005213_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005214undeclare_reachable(_Tp* __p)
5215{
5216 return static_cast<_Tp*>(__undeclare_reachable(__p));
5217}
5218
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005219_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005220
Marshall Clow8982dcd2015-07-13 20:04:56 +00005221// --- Helper for container swap --
5222template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005223inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005224void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5225#if _LIBCPP_STD_VER >= 14
5226 _NOEXCEPT
5227#else
5228 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5229#endif
5230{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005231 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005232 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5233}
5234
5235template <typename _Alloc>
5236_LIBCPP_INLINE_VISIBILITY
5237void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5238#if _LIBCPP_STD_VER >= 14
5239 _NOEXCEPT
5240#else
5241 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5242#endif
5243{
5244 using _VSTD::swap;
5245 swap(__a1, __a2);
5246}
5247
5248template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005249inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005250void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5251
Marshall Clowff91de82015-08-18 19:51:37 +00005252template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005253struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005254 _Traits::propagate_on_container_move_assignment::value
5255#if _LIBCPP_STD_VER > 14
5256 || _Traits::is_always_equal::value
5257#else
5258 && is_nothrow_move_assignable<_Alloc>::value
5259#endif
5260 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005261
Marshall Clowa591b9a2016-07-11 21:38:08 +00005262
5263#ifndef _LIBCPP_HAS_NO_VARIADICS
5264template <class _Tp, class _Alloc>
5265struct __temp_value {
5266 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005267
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005268 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005269 _Alloc &__a;
5270
5271 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5272 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005273
Marshall Clowa591b9a2016-07-11 21:38:08 +00005274 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005275 _LIBCPP_NO_CFI
5276 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5277 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5278 _VSTD::forward<_Args>(__args)...);
5279 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005280
Marshall Clowa591b9a2016-07-11 21:38:08 +00005281 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5282 };
5283#endif
5284
Marshall Clowe46031a2018-07-02 18:41:15 +00005285template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005286struct __is_allocator : false_type {};
5287
5288template<typename _Alloc>
5289struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005290 typename __void_t<typename _Alloc::value_type>::type,
5291 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5292 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005293 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005294
Eric Fiselier74ebee62019-06-08 01:31:19 +00005295// __builtin_new_allocator -- A non-templated helper for allocating and
5296// deallocating memory using __builtin_operator_new and
5297// __builtin_operator_delete. It should be used in preference to
5298// `std::allocator<T>` to avoid additional instantiations.
5299struct __builtin_new_allocator {
5300 struct __builtin_new_deleter {
5301 typedef void* pointer_type;
5302
5303 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5304 : __size_(__size), __align_(__align) {}
5305
5306 void operator()(void* p) const _NOEXCEPT {
5307 std::__libcpp_deallocate(p, __size_, __align_);
5308 }
5309
5310 private:
5311 size_t __size_;
5312 size_t __align_;
5313 };
5314
5315 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5316
5317 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5318 return __holder_t(std::__libcpp_allocate(__s, __align),
5319 __builtin_new_deleter(__s, __align));
5320 }
5321
5322 static void __deallocate_bytes(void* __p, size_t __s,
5323 size_t __align) _NOEXCEPT {
5324 std::__libcpp_deallocate(__p, __s, __align);
5325 }
5326
5327 template <class _Tp>
5328 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5329 static __holder_t __allocate_type(size_t __n) {
5330 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5331 }
5332
5333 template <class _Tp>
5334 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5335 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5336 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5337 }
5338};
5339
5340
Howard Hinnantc51e1022010-05-11 19:42:16 +00005341_LIBCPP_END_NAMESPACE_STD
5342
Eric Fiselierf4433a32017-05-31 22:07:49 +00005343_LIBCPP_POP_MACROS
5344
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005345#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005346# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005347#endif
5348
Howard Hinnantc51e1022010-05-11 19:42:16 +00005349#endif // _LIBCPP_MEMORY