blob: bf31bfca5130d3d3c0a6f29c039816f511defd35 [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
453// shared_ptr comparisons:
454template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000455 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000456template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000457 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000459 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000460template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000461 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000462template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000463 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000464template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000465 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
466
467template <class T>
468 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
469template <class T>
470 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
471template <class T>
472 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
473template <class T>
474 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
475template <class T>
476 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
477template <class T>
478bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
479template <class T>
480 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
481template <class T>
482 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
483template <class T>
484 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
485template <class T>
486 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
487template <class T>
488 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
489template <class T>
490 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000491
492// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000493template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000494
495// shared_ptr casts:
496template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000497 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000498template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000499 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000501 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000502
503// shared_ptr I/O:
504template<class E, class T, class Y>
505 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
506
507// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000508template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000509
510template<class T, class... Args>
511 shared_ptr<T> make_shared(Args&&... args);
512template<class T, class A, class... Args>
513 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
514
515template<class T>
516class weak_ptr
517{
518public:
519 typedef T element_type;
520
521 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000522 constexpr weak_ptr() noexcept;
523 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
524 weak_ptr(weak_ptr const& r) noexcept;
525 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000526 weak_ptr(weak_ptr&& r) noexcept; // C++14
527 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000528
529 // destructor
530 ~weak_ptr();
531
532 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000533 weak_ptr& operator=(weak_ptr const& r) noexcept;
534 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
535 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000536 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
537 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000538
539 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000540 void swap(weak_ptr& r) noexcept;
541 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000542
543 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000544 long use_count() const noexcept;
545 bool expired() const noexcept;
546 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000547 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
548 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000549};
550
551// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000552template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000553
554// class owner_less:
555template<class T> struct owner_less;
556
557template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000558struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000559 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
560{
561 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000562 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
563 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
564 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000565};
566
567template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000568struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000569 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
570{
571 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000572 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
574 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
575};
576
577template <> // Added in C++14
578struct owner_less<void>
579{
580 template <class _Tp, class _Up>
581 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
582 template <class _Tp, class _Up>
583 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
584 template <class _Tp, class _Up>
585 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
586 template <class _Tp, class _Up>
587 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
588
589 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000590};
591
592template<class T>
593class enable_shared_from_this
594{
595protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000596 constexpr enable_shared_from_this() noexcept;
597 enable_shared_from_this(enable_shared_from_this const&) noexcept;
598 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599 ~enable_shared_from_this();
600public:
601 shared_ptr<T> shared_from_this();
602 shared_ptr<T const> shared_from_this() const;
603};
604
605template<class T>
606 bool atomic_is_lock_free(const shared_ptr<T>* p);
607template<class T>
608 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
609template<class T>
610 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
611template<class T>
612 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
613template<class T>
614 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
615template<class T>
616 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
617template<class T>
618 shared_ptr<T>
619 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
620template<class T>
621 bool
622 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
623template<class T>
624 bool
625 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
626template<class T>
627 bool
628 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
629 shared_ptr<T> w, memory_order success,
630 memory_order failure);
631template<class T>
632 bool
633 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
634 shared_ptr<T> w, memory_order success,
635 memory_order failure);
636// Hash support
637template <class T> struct hash;
638template <class T, class D> struct hash<unique_ptr<T, D> >;
639template <class T> struct hash<shared_ptr<T> >;
640
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000641template <class T, class Alloc>
642 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
643
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000644// Pointer safety
645enum class pointer_safety { relaxed, preferred, strict };
646void declare_reachable(void *p);
647template <class T> T *undeclare_reachable(T *p);
648void declare_no_pointers(char *p, size_t n);
649void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000650pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000651
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
653
654} // std
655
656*/
657
658#include <__config>
659#include <type_traits>
660#include <typeinfo>
661#include <cstddef>
662#include <cstdint>
663#include <new>
664#include <utility>
665#include <limits>
666#include <iterator>
667#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000668#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000669#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000670#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000671#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000672#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000673# include <atomic>
674#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000675#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000676
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000677#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000679#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680
Eric Fiselierf4433a32017-05-31 22:07:49 +0000681_LIBCPP_PUSH_MACROS
682#include <__undef_macros>
683
684
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685_LIBCPP_BEGIN_NAMESPACE_STD
686
Eric Fiselier89659d12015-07-07 00:27:16 +0000687template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000688inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000689_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
690#if !defined(_LIBCPP_HAS_NO_THREADS) && \
691 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000692 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000693 return __atomic_load_n(__value, __ATOMIC_RELAXED);
694#else
695 return *__value;
696#endif
697}
698
Kuba Breckade9d6792016-09-04 09:55:12 +0000699template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000700inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000701_ValueType __libcpp_acquire_load(_ValueType const* __value) {
702#if !defined(_LIBCPP_HAS_NO_THREADS) && \
703 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000704 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000705 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
706#else
707 return *__value;
708#endif
709}
710
Marshall Clow78dbe462016-11-14 18:22:19 +0000711// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000712
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713template <class _Tp> class allocator;
714
Michael Park99f9e912020-03-04 11:27:14 -0500715#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716template <>
Michael Park99f9e912020-03-04 11:27:14 -0500717class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718{
719public:
720 typedef void* pointer;
721 typedef const void* const_pointer;
722 typedef void value_type;
723
724 template <class _Up> struct rebind {typedef allocator<_Up> other;};
725};
726
Howard Hinnant9f771052012-01-19 23:15:22 +0000727template <>
Michael Park99f9e912020-03-04 11:27:14 -0500728class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000729{
730public:
731 typedef const void* pointer;
732 typedef const void* const_pointer;
733 typedef const void value_type;
734
735 template <class _Up> struct rebind {typedef allocator<_Up> other;};
736};
Michael Park99f9e912020-03-04 11:27:14 -0500737#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000738
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739// pointer_traits
740
Marshall Clow0be70c32017-06-14 21:23:57 +0000741template <class _Tp, class = void>
742struct __has_element_type : false_type {};
743
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000745struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000746 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
748template <class _Ptr, bool = __has_element_type<_Ptr>::value>
749struct __pointer_traits_element_type;
750
751template <class _Ptr>
752struct __pointer_traits_element_type<_Ptr, true>
753{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000754 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755};
756
757#ifndef _LIBCPP_HAS_NO_VARIADICS
758
759template <template <class, class...> class _Sp, class _Tp, class ..._Args>
760struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
761{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000762 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763};
764
765template <template <class, class...> class _Sp, class _Tp, class ..._Args>
766struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
767{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000768 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769};
770
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000771#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772
773template <template <class> class _Sp, class _Tp>
774struct __pointer_traits_element_type<_Sp<_Tp>, true>
775{
776 typedef typename _Sp<_Tp>::element_type type;
777};
778
779template <template <class> class _Sp, class _Tp>
780struct __pointer_traits_element_type<_Sp<_Tp>, false>
781{
782 typedef _Tp type;
783};
784
785template <template <class, class> class _Sp, class _Tp, class _A0>
786struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
787{
788 typedef typename _Sp<_Tp, _A0>::element_type type;
789};
790
791template <template <class, class> class _Sp, class _Tp, class _A0>
792struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
793{
794 typedef _Tp type;
795};
796
797template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
798struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
799{
800 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
801};
802
803template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
804struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
805{
806 typedef _Tp type;
807};
808
809template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
810 class _A1, class _A2>
811struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
812{
813 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
814};
815
816template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
817 class _A1, class _A2>
818struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
819{
820 typedef _Tp type;
821};
822
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000823#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824
Marshall Clow0be70c32017-06-14 21:23:57 +0000825template <class _Tp, class = void>
826struct __has_difference_type : false_type {};
827
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000829struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000830 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831
832template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
833struct __pointer_traits_difference_type
834{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000835 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836};
837
838template <class _Ptr>
839struct __pointer_traits_difference_type<_Ptr, true>
840{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000841 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842};
843
844template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500845struct __has_rebind
846{
847private:
848 struct __two {char __lx; char __lxx;};
849 template <class _Xp> static __two __test(...);
Louis Dionne95f24792020-03-04 14:38:51 -0500850 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Louis Dionnef1bb8492020-03-04 13:54:58 -0500851 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
Louis Dionne95f24792020-03-04 14:38:51 -0500852 _LIBCPP_SUPPRESS_DEPRECATED_POP
Louis Dionnef1bb8492020-03-04 13:54:58 -0500853public:
854 static const bool value = sizeof(__test<_Tp>(0)) == 1;
855};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856
857template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
858struct __pointer_traits_rebind
859{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000860#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000861 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000863 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864#endif
865};
866
867#ifndef _LIBCPP_HAS_NO_VARIADICS
868
869template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
870struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
871{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000872#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000873 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000875 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876#endif
877};
878
879template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
880struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
881{
882 typedef _Sp<_Up, _Args...> type;
883};
884
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000885#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886
887template <template <class> class _Sp, class _Tp, class _Up>
888struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
889{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000890#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 typedef typename _Sp<_Tp>::template rebind<_Up> type;
892#else
893 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
894#endif
895};
896
897template <template <class> class _Sp, class _Tp, class _Up>
898struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
899{
900 typedef _Sp<_Up> type;
901};
902
903template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
905{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000906#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
908#else
909 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
910#endif
911};
912
913template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
914struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
915{
916 typedef _Sp<_Up, _A0> type;
917};
918
919template <template <class, class, class> class _Sp, class _Tp, class _A0,
920 class _A1, class _Up>
921struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
922{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000923#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
925#else
926 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
927#endif
928};
929
930template <template <class, class, class> class _Sp, class _Tp, class _A0,
931 class _A1, class _Up>
932struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
933{
934 typedef _Sp<_Up, _A0, _A1> type;
935};
936
937template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
938 class _A1, class _A2, class _Up>
939struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
940{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000941#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
943#else
944 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
945#endif
946};
947
948template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
949 class _A1, class _A2, class _Up>
950struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
951{
952 typedef _Sp<_Up, _A0, _A1, _A2> type;
953};
954
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000955#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
957template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000958struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959{
960 typedef _Ptr pointer;
961 typedef typename __pointer_traits_element_type<pointer>::type element_type;
962 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
963
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000964#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000965 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966#else
967 template <class _Up> struct rebind
968 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000969#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
971private:
972 struct __nat {};
973public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 static pointer pointer_to(typename conditional<is_void<element_type>::value,
976 __nat, element_type>::type& __r)
977 {return pointer::pointer_to(__r);}
978};
979
980template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000981struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982{
983 typedef _Tp* pointer;
984 typedef _Tp element_type;
985 typedef ptrdiff_t difference_type;
986
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000987#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 template <class _Up> using rebind = _Up*;
989#else
990 template <class _Up> struct rebind {typedef _Up* other;};
991#endif
992
993private:
994 struct __nat {};
995public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000996 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000998 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000999 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000};
1001
Eric Fiselierae3ab842015-08-23 02:56:05 +00001002template <class _From, class _To>
1003struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001004#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +00001005 typedef typename pointer_traits<_From>::template rebind<_To> type;
1006#else
1007 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
1008#endif
1009};
1010
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011// allocator_traits
1012
Marshall Clow0be70c32017-06-14 21:23:57 +00001013template <class _Tp, class = void>
1014struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015
1016template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001017struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001018 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019
1020namespace __pointer_type_imp
1021{
1022
1023template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1024struct __pointer_type
1025{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001026 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027};
1028
1029template <class _Tp, class _Dp>
1030struct __pointer_type<_Tp, _Dp, false>
1031{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001032 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033};
1034
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001035} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036
1037template <class _Tp, class _Dp>
1038struct __pointer_type
1039{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001040 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 +00001041};
1042
Marshall Clow0be70c32017-06-14 21:23:57 +00001043template <class _Tp, class = void>
1044struct __has_const_pointer : false_type {};
1045
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001047struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001048 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049
1050template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1051struct __const_pointer
1052{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001053 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054};
1055
1056template <class _Tp, class _Ptr, class _Alloc>
1057struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1058{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001059#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001060 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061#else
1062 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1063#endif
1064};
1065
Marshall Clow0be70c32017-06-14 21:23:57 +00001066template <class _Tp, class = void>
1067struct __has_void_pointer : false_type {};
1068
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001070struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001071 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072
1073template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1074struct __void_pointer
1075{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001076 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077};
1078
1079template <class _Ptr, class _Alloc>
1080struct __void_pointer<_Ptr, _Alloc, false>
1081{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001082#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001083 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001085 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086#endif
1087};
1088
Marshall Clow0be70c32017-06-14 21:23:57 +00001089template <class _Tp, class = void>
1090struct __has_const_void_pointer : false_type {};
1091
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001093struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001094 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095
1096template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1097struct __const_void_pointer
1098{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001099 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100};
1101
1102template <class _Ptr, class _Alloc>
1103struct __const_void_pointer<_Ptr, _Alloc, false>
1104{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001105#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001106 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001108 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109#endif
1110};
1111
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001112
1113template <bool _UsePointerTraits> struct __to_address_helper;
1114
1115template <> struct __to_address_helper<true> {
1116 template <class _Pointer>
1117 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
1118
1119 template <class _Pointer>
1120 _LIBCPP_CONSTEXPR
1121 static __return_type<_Pointer>
1122 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1123};
1124
1125template <class _Pointer, bool _Dummy = true>
1126using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1127
1128
Howard Hinnantc834c512011-11-29 18:15:50 +00001129template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001130inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001131_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001132__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001134 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 return __p;
1136}
1137
1138template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001139inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1140typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1141__to_address(const _Pointer& __p) _NOEXCEPT {
1142 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001143}
1144
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001145template <> struct __to_address_helper<false> {
1146 template <class _Pointer>
1147 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001148
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001149 template <class _Pointer>
1150 _LIBCPP_CONSTEXPR
1151 static __return_type<_Pointer>
1152 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1153};
1154
1155
1156#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001157template <class _Tp>
1158inline _LIBCPP_INLINE_VISIBILITY constexpr
1159_Tp*
1160to_address(_Tp* __p) _NOEXCEPT
1161{
1162 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1163 return __p;
1164}
1165
1166template <class _Pointer>
1167inline _LIBCPP_INLINE_VISIBILITY
1168auto
1169to_address(const _Pointer& __p) _NOEXCEPT
1170{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001171 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001172}
1173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174
Marshall Clow0be70c32017-06-14 21:23:57 +00001175template <class _Tp, class = void>
1176struct __has_size_type : false_type {};
1177
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001179struct __has_size_type<_Tp,
1180 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001182template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183struct __size_type
1184{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001185 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186};
1187
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001188template <class _Alloc, class _DiffType>
1189struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001191 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192};
1193
Marshall Clow0be70c32017-06-14 21:23:57 +00001194template <class _Tp, class = void>
1195struct __has_propagate_on_container_copy_assignment : false_type {};
1196
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001198struct __has_propagate_on_container_copy_assignment<_Tp,
1199 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1200 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201
1202template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1203struct __propagate_on_container_copy_assignment
1204{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001205 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206};
1207
1208template <class _Alloc>
1209struct __propagate_on_container_copy_assignment<_Alloc, true>
1210{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001211 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212};
1213
Marshall Clow0be70c32017-06-14 21:23:57 +00001214template <class _Tp, class = void>
1215struct __has_propagate_on_container_move_assignment : false_type {};
1216
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001218struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001219 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1220 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221
1222template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1223struct __propagate_on_container_move_assignment
1224{
1225 typedef false_type type;
1226};
1227
1228template <class _Alloc>
1229struct __propagate_on_container_move_assignment<_Alloc, true>
1230{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001231 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232};
1233
Marshall Clow0be70c32017-06-14 21:23:57 +00001234template <class _Tp, class = void>
1235struct __has_propagate_on_container_swap : false_type {};
1236
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001238struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001239 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1240 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
1242template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1243struct __propagate_on_container_swap
1244{
1245 typedef false_type type;
1246};
1247
1248template <class _Alloc>
1249struct __propagate_on_container_swap<_Alloc, true>
1250{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001251 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252};
1253
Marshall Clow0be70c32017-06-14 21:23:57 +00001254template <class _Tp, class = void>
1255struct __has_is_always_equal : false_type {};
1256
Marshall Clow0b587562015-06-02 16:34:03 +00001257template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001258struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001259 typename __void_t<typename _Tp::is_always_equal>::type>
1260 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001261
1262template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1263struct __is_always_equal
1264{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001265 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001266};
1267
1268template <class _Alloc>
1269struct __is_always_equal<_Alloc, true>
1270{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001271 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001272};
1273
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1275struct __has_rebind_other
1276{
1277private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001278 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001280 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001282 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283public:
1284 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1285};
1286
1287template <class _Tp, class _Up>
1288struct __has_rebind_other<_Tp, _Up, false>
1289{
1290 static const bool value = false;
1291};
1292
1293template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1294struct __allocator_traits_rebind
1295{
Michael Park99f9e912020-03-04 11:27:14 -05001296 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001297 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001298 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299};
1300
1301#ifndef _LIBCPP_HAS_NO_VARIADICS
1302
1303template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1305{
Michael Park99f9e912020-03-04 11:27:14 -05001306 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001307 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001308 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309};
1310
1311template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1312struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1313{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001314 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315};
1316
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001317#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318
1319template <template <class> class _Alloc, class _Tp, class _Up>
1320struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1321{
1322 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1323};
1324
1325template <template <class> class _Alloc, class _Tp, class _Up>
1326struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1327{
1328 typedef _Alloc<_Up> type;
1329};
1330
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1333{
1334 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1335};
1336
1337template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1338struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1339{
1340 typedef _Alloc<_Up, _A0> type;
1341};
1342
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1344 class _A1, class _Up>
1345struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1346{
1347 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1348};
1349
1350template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1351 class _A1, class _Up>
1352struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1353{
1354 typedef _Alloc<_Up, _A0, _A1> type;
1355};
1356
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1358 class _A1, class _A2, class _Up>
1359struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1360{
1361 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1362};
1363
1364template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1365 class _A1, class _A2, class _Up>
1366struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1367{
1368 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1369};
1370
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001371#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001373#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374
Michael Park99f9e912020-03-04 11:27:14 -05001375_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1377auto
1378__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001379 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001380_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381
1382template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1383auto
1384__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1385 -> false_type;
1386
1387template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1388struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001389 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1390 declval<_SizeType>(),
1391 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392{
1393};
1394
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001395#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396
1397template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1398struct __has_allocate_hint
1399 : true_type
1400{
1401};
1402
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001403#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001405#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406
Michael Park99f9e912020-03-04 11:27:14 -05001407_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1408template <class _Alloc, class _Tp, class... _Args>
1409auto
1410__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&&... __args)
1411 -> decltype(__a.construct(__p, _VSTD::forward<_Args>(__args)...), true_type());
1412_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413
1414template <class _Alloc, class _Pointer, class ..._Args>
Michael Park99f9e912020-03-04 11:27:14 -05001415auto
1416__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args)
1417 -> false_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418
1419template <class _Alloc, class _Pointer, class ..._Args>
1420struct __has_construct
Michael Park99f9e912020-03-04 11:27:14 -05001421 : decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
1422 declval<_Pointer>(),
1423 declval<_Args>()...))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424{
1425};
1426
Michael Park99f9e912020-03-04 11:27:14 -05001427_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428template <class _Alloc, class _Pointer>
1429auto
1430__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1431 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001432_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433
1434template <class _Alloc, class _Pointer>
1435auto
1436__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1437 -> false_type;
1438
1439template <class _Alloc, class _Pointer>
1440struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001441 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1442 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443{
1444};
1445
Michael Park99f9e912020-03-04 11:27:14 -05001446_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447template <class _Alloc>
1448auto
1449__has_max_size_test(_Alloc&& __a)
1450 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001451_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452
1453template <class _Alloc>
1454auto
1455__has_max_size_test(const volatile _Alloc& __a)
1456 -> false_type;
1457
1458template <class _Alloc>
1459struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001460 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461{
1462};
1463
1464template <class _Alloc>
1465auto
1466__has_select_on_container_copy_construction_test(_Alloc&& __a)
1467 -> decltype(__a.select_on_container_copy_construction(), true_type());
1468
1469template <class _Alloc>
1470auto
1471__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1472 -> false_type;
1473
1474template <class _Alloc>
1475struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001476 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477{
1478};
1479
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001480#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001482template <class _Alloc, class _Pointer, class _Tp, class = void>
1483struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001485template <class _Alloc, class _Pointer, class _Tp>
1486struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1487 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1488>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001490template <class _Alloc, class _Pointer, class = void>
1491struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
1493template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001494struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1495 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1496>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497
1498template <class _Alloc>
1499struct __has_max_size
1500 : true_type
1501{
1502};
1503
1504template <class _Alloc>
1505struct __has_select_on_container_copy_construction
1506 : false_type
1507{
1508};
1509
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001510#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001512template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1513struct __alloc_traits_difference_type
1514{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001515 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516};
1517
1518template <class _Alloc, class _Ptr>
1519struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1520{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001521 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001522};
1523
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001524template <class _Tp>
1525struct __is_default_allocator : false_type {};
1526
1527template <class _Tp>
1528struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1529
Eric Fiselier909fe962019-09-13 16:09:33 +00001530
1531
1532template <class _Alloc,
1533 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1534 >
1535struct __is_cpp17_move_insertable;
1536template <class _Alloc>
1537struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1538template <class _Alloc>
1539struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1540
1541template <class _Alloc,
1542 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1543 >
1544struct __is_cpp17_copy_insertable;
1545template <class _Alloc>
1546struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1547template <class _Alloc>
1548struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1549 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1550 __is_cpp17_move_insertable<_Alloc>::value>
1551 {};
1552
1553
1554
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001556struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557{
1558 typedef _Alloc allocator_type;
1559 typedef typename allocator_type::value_type value_type;
1560
1561 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1562 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1563 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1564 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1565
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001566 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1567 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568
1569 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1570 propagate_on_container_copy_assignment;
1571 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1572 propagate_on_container_move_assignment;
1573 typedef typename __propagate_on_container_swap<allocator_type>::type
1574 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001575 typedef typename __is_always_equal<allocator_type>::type
1576 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001578#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001580 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001581 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001582#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 template <class _Tp> struct rebind_alloc
1584 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1585 template <class _Tp> struct rebind_traits
1586 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001587#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588
Marshall Clow0e58cae2017-11-26 02:55:38 +00001589 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 static pointer allocate(allocator_type& __a, size_type __n)
1591 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001592 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001594 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1596
Howard Hinnant756c69b2010-09-22 16:48:34 +00001597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001598 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 {__a.deallocate(__p, __n);}
1600
1601#ifndef _LIBCPP_HAS_NO_VARIADICS
1602 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001605 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001606 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001607#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001609 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001610 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 {
1612 ::new ((void*)__p) _Tp();
1613 }
1614 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001615 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001616 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001618 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1619 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620 }
1621 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001622 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001623 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 const _A1& __a1)
1625 {
1626 ::new ((void*)__p) _Tp(__a0, __a1);
1627 }
1628 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001629 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001630 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631 const _A1& __a1, const _A2& __a2)
1632 {
1633 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1634 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001635#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636
1637 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 static void destroy(allocator_type& __a, _Tp* __p)
1640 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1641
Howard Hinnant756c69b2010-09-22 16:48:34 +00001642 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001643 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1645
Howard Hinnant756c69b2010-09-22 16:48:34 +00001646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647 static allocator_type
1648 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001649 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650 __has_select_on_container_copy_construction<const allocator_type>(),
1651 __a);}
1652
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001653 template <class _Ptr>
1654 _LIBCPP_INLINE_VISIBILITY
1655 static
1656 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001657 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001658 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001659 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1660 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001661 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001662 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001663#ifdef _LIBCPP_NO_EXCEPTIONS
1664 _VSTD::move(*__begin1)
1665#else
1666 _VSTD::move_if_noexcept(*__begin1)
1667#endif
1668 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001669 }
1670
1671 template <class _Tp>
1672 _LIBCPP_INLINE_VISIBILITY
1673 static
1674 typename enable_if
1675 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001676 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1678 is_trivially_move_constructible<_Tp>::value,
1679 void
1680 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001681 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001682 {
1683 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001684 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001685 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001686 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001687 __begin2 += _Np;
1688 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001689 }
1690
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001691 template <class _Iter, class _Ptr>
1692 _LIBCPP_INLINE_VISIBILITY
1693 static
1694 void
1695 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1696 {
1697 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001698 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001699 }
1700
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001701 template <class _SourceTp, class _DestTp,
1702 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1703 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001704 _LIBCPP_INLINE_VISIBILITY
1705 static
1706 typename enable_if
1707 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001708 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001709 is_same<_RawSourceTp, _RawDestTp>::value &&
1710 (__is_default_allocator<allocator_type>::value ||
1711 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001712 void
1713 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001714 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001715 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001716 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001717 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001718 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001719 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001720 __begin2 += _Np;
1721 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001722 }
1723
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001724 template <class _Ptr>
1725 _LIBCPP_INLINE_VISIBILITY
1726 static
1727 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001728 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001729 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001730 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1731 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001732 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001733 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001734 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001735#ifdef _LIBCPP_NO_EXCEPTIONS
1736 _VSTD::move(*--__end1)
1737#else
1738 _VSTD::move_if_noexcept(*--__end1)
1739#endif
1740 );
1741 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001742 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001743 }
1744
1745 template <class _Tp>
1746 _LIBCPP_INLINE_VISIBILITY
1747 static
1748 typename enable_if
1749 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001750 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001751 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1752 is_trivially_move_constructible<_Tp>::value,
1753 void
1754 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001755 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001756 {
1757 ptrdiff_t _Np = __end1 - __begin1;
1758 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001759 if (_Np > 0)
1760 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001761 }
1762
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763private:
1764
Howard Hinnant756c69b2010-09-22 16:48:34 +00001765 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001766 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001768 {
1769 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1770 return __a.allocate(__n, __hint);
1771 _LIBCPP_SUPPRESS_DEPRECATED_POP
1772 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001773 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001774 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001775 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 {return __a.allocate(__n);}
1777
1778#ifndef _LIBCPP_HAS_NO_VARIADICS
1779 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001782 {
1783 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1784 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1785 _LIBCPP_SUPPRESS_DEPRECATED_POP
1786 }
1787
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1791 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001792 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001794#else // _LIBCPP_HAS_NO_VARIADICS
1795 template <class _Tp, class _A0>
1796 _LIBCPP_INLINE_VISIBILITY
1797 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1798 const _A0& __a0)
1799 {__a.construct(__p, __a0);}
1800 template <class _Tp, class _A0>
1801 _LIBCPP_INLINE_VISIBILITY
1802 static void __construct(false_type, allocator_type&, _Tp* __p,
1803 const _A0& __a0)
1804 {
1805 ::new ((void*)__p) _Tp(__a0);
1806 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001807#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808
1809 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001812 {
1813 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1814 __a.destroy(__p);
1815 _LIBCPP_SUPPRESS_DEPRECATED_POP
1816 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 static void __destroy(false_type, allocator_type&, _Tp* __p)
1820 {
1821 __p->~_Tp();
1822 }
1823
Howard Hinnant756c69b2010-09-22 16:48:34 +00001824 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001825 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001826 {
1827 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1828 return __a.max_size();
1829 _LIBCPP_SUPPRESS_DEPRECATED_POP
1830 }
1831
Howard Hinnant756c69b2010-09-22 16:48:34 +00001832 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001833 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001834 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835
Howard Hinnant756c69b2010-09-22 16:48:34 +00001836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001838 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001842 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 {return __a;}
1844};
1845
Marshall Clow940e01c2015-04-07 05:21:38 +00001846template <class _Traits, class _Tp>
1847struct __rebind_alloc_helper
1848{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001849#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001850 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001851#else
1852 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1853#endif
1854};
1855
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856// allocator
1857
1858template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001859class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860{
1861public:
Michael Park99f9e912020-03-04 11:27:14 -05001862#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1863 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1864 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1865 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1866 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1867 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1868 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1869
1870 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1871#endif
1872
1873 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874
Howard Hinnant4931e092011-06-02 21:38:57 +00001875 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001876 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001877
Marshall Clowbc759762018-03-20 23:02:53 +00001878 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1879 allocator() _NOEXCEPT {}
1880
Louis Dionne481a2662018-09-23 18:35:00 +00001881 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001882 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1883 allocator(const allocator<_Up>&) _NOEXCEPT {}
1884
Michael Park99f9e912020-03-04 11:27:14 -05001885#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1886 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1887 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001888 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001889 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1890 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001891 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001892#endif
1893
1894 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001895 {
Michael Park99f9e912020-03-04 11:27:14 -05001896 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1897 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001898 __throw_length_error("allocator<T>::allocate(size_t n)"
1899 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001900 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001901 }
Michael Park99f9e912020-03-04 11:27:14 -05001902
1903#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1904 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1905 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1906#endif
1907
1908 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001909 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001910
1911#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1912 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001913 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001914
Howard Hinnant74279a52010-09-04 23:28:19 +00001915#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001917 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918 void
1919 construct(_Up* __p, _Args&&... __args)
1920 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001921 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001923#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 _LIBCPP_INLINE_VISIBILITY
1925 void
1926 construct(pointer __p)
1927 {
1928 ::new((void*)__p) _Tp();
1929 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001930# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001931
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932 template <class _A0>
1933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001934 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 construct(pointer __p, _A0& __a0)
1936 {
1937 ::new((void*)__p) _Tp(__a0);
1938 }
1939 template <class _A0>
1940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001941 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942 construct(pointer __p, const _A0& __a0)
1943 {
1944 ::new((void*)__p) _Tp(__a0);
1945 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001946# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947 template <class _A0, class _A1>
1948 _LIBCPP_INLINE_VISIBILITY
1949 void
1950 construct(pointer __p, _A0& __a0, _A1& __a1)
1951 {
1952 ::new((void*)__p) _Tp(__a0, __a1);
1953 }
1954 template <class _A0, class _A1>
1955 _LIBCPP_INLINE_VISIBILITY
1956 void
1957 construct(pointer __p, const _A0& __a0, _A1& __a1)
1958 {
1959 ::new((void*)__p) _Tp(__a0, __a1);
1960 }
1961 template <class _A0, class _A1>
1962 _LIBCPP_INLINE_VISIBILITY
1963 void
1964 construct(pointer __p, _A0& __a0, const _A1& __a1)
1965 {
1966 ::new((void*)__p) _Tp(__a0, __a1);
1967 }
1968 template <class _A0, class _A1>
1969 _LIBCPP_INLINE_VISIBILITY
1970 void
1971 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1972 {
1973 ::new((void*)__p) _Tp(__a0, __a1);
1974 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001975#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05001976 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1977#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978};
1979
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001980template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001981class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001982{
1983public:
Michael Park99f9e912020-03-04 11:27:14 -05001984#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1985 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1986 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1987 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1988 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1989 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1990 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1991
1992 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1993#endif
1994
1995 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001996
1997 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001998 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001999
Marshall Clowbc759762018-03-20 23:02:53 +00002000 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2001 allocator() _NOEXCEPT {}
2002
2003 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00002004 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00002005 allocator(const allocator<_Up>&) _NOEXCEPT {}
2006
Michael Park99f9e912020-03-04 11:27:14 -05002007#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2008 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
2009 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002010 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05002011#endif
2012
2013 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00002014 {
Michael Park99f9e912020-03-04 11:27:14 -05002015 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
2016 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00002017 __throw_length_error("allocator<const T>::allocate(size_t n)"
2018 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05002019 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00002020 }
Michael Park99f9e912020-03-04 11:27:14 -05002021
2022#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2023 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
2024 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
2025#endif
2026
2027 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002028 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05002029
2030#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2031 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002032 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05002033
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002034#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2035 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05002036 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002037 void
2038 construct(_Up* __p, _Args&&... __args)
2039 {
2040 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
2041 }
2042#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2043 _LIBCPP_INLINE_VISIBILITY
2044 void
2045 construct(pointer __p)
2046 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002047 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002048 }
2049# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00002050
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002051 template <class _A0>
2052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002053 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002054 construct(pointer __p, _A0& __a0)
2055 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002056 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002057 }
2058 template <class _A0>
2059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002060 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002061 construct(pointer __p, const _A0& __a0)
2062 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002063 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002064 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002065# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
2066 template <class _A0, class _A1>
2067 _LIBCPP_INLINE_VISIBILITY
2068 void
2069 construct(pointer __p, _A0& __a0, _A1& __a1)
2070 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002071 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002072 }
2073 template <class _A0, class _A1>
2074 _LIBCPP_INLINE_VISIBILITY
2075 void
2076 construct(pointer __p, const _A0& __a0, _A1& __a1)
2077 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002078 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002079 }
2080 template <class _A0, class _A1>
2081 _LIBCPP_INLINE_VISIBILITY
2082 void
2083 construct(pointer __p, _A0& __a0, const _A1& __a1)
2084 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002085 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002086 }
2087 template <class _A0, class _A1>
2088 _LIBCPP_INLINE_VISIBILITY
2089 void
2090 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2091 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002092 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002093 }
2094#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05002095 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2096#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002097};
2098
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099template <class _Tp, class _Up>
2100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002101bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102
2103template <class _Tp, class _Up>
2104inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002105bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106
2107template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002108class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109 : public iterator<output_iterator_tag,
2110 _Tp, // purposefully not C++03
2111 ptrdiff_t, // purposefully not C++03
2112 _Tp*, // purposefully not C++03
2113 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2114{
2115private:
2116 _OutputIterator __x_;
2117public:
2118 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2119 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2120 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002121 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002122#if _LIBCPP_STD_VER >= 14
2123 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002124 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002125#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2127 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2128 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002129#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002130 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002131#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132};
2133
2134template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002135_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002137get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138{
2139 pair<_Tp*, ptrdiff_t> __r(0, 0);
2140 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2141 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2142 / sizeof(_Tp);
2143 if (__n > __m)
2144 __n = __m;
2145 while (__n > 0)
2146 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002147#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002148 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002149 {
2150 std::align_val_t __al =
2151 std::align_val_t(std::alignment_of<_Tp>::value);
2152 __r.first = static_cast<_Tp*>(::operator new(
2153 __n * sizeof(_Tp), __al, nothrow));
2154 } else {
2155 __r.first = static_cast<_Tp*>(::operator new(
2156 __n * sizeof(_Tp), nothrow));
2157 }
2158#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002159 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002160 {
2161 // Since aligned operator new is unavailable, return an empty
2162 // buffer rather than one with invalid alignment.
2163 return __r;
2164 }
2165
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002167#endif
2168
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169 if (__r.first)
2170 {
2171 __r.second = __n;
2172 break;
2173 }
2174 __n /= 2;
2175 }
2176 return __r;
2177}
2178
2179template <class _Tp>
2180inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002181void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2182{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002183 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002184}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185
Marshall Clowb22274f2017-01-24 22:22:33 +00002186#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002188struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189{
2190 _Tp* __ptr_;
2191};
2192
2193template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002194class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195{
2196private:
2197 _Tp* __ptr_;
2198public:
2199 typedef _Tp element_type;
2200
2201 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2202 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2203 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2204 : __ptr_(__p.release()) {}
2205 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2206 {reset(__p.release()); return *this;}
2207 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2208 {reset(__p.release()); return *this;}
2209 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2210 {reset(__p.__ptr_); return *this;}
2211 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2212
2213 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2214 {return *__ptr_;}
2215 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2216 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2217 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2218 {
2219 _Tp* __t = __ptr_;
2220 __ptr_ = 0;
2221 return __t;
2222 }
2223 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2224 {
2225 if (__ptr_ != __p)
2226 delete __ptr_;
2227 __ptr_ = __p;
2228 }
2229
2230 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2231 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2232 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2233 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2234 {return auto_ptr<_Up>(release());}
2235};
2236
2237template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002238class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239{
2240public:
2241 typedef void element_type;
2242};
Marshall Clowb22274f2017-01-24 22:22:33 +00002243#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002245// Tag used to default initialize one or both of the pair's elements.
2246struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002247struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002248
Eric Fiselier9d355982017-04-12 23:45:53 +00002249template <class _Tp, int _Idx,
2250 bool _CanBeEmptyBase =
2251 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2252struct __compressed_pair_elem {
2253 typedef _Tp _ParamT;
2254 typedef _Tp& reference;
2255 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002257 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002258 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002259 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2260 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261
Eric Fiselier9d355982017-04-12 23:45:53 +00002262 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002263 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2264 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002265 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002266 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002267 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002268 : __value_(_VSTD::forward<_Up>(__u))
2269 {
2270 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002272
2273#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002274 template <class... _Args, size_t... _Indexes>
2275 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2276 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2277 __tuple_indices<_Indexes...>)
2278 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002279#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002281
Alex Lorenz76132112017-11-09 17:54:49 +00002282 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2283 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002284 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002287 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288};
2289
Eric Fiselier9d355982017-04-12 23:45:53 +00002290template <class _Tp, int _Idx>
2291struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2292 typedef _Tp _ParamT;
2293 typedef _Tp& reference;
2294 typedef const _Tp& const_reference;
2295 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002297 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2298 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2299 __compressed_pair_elem(__default_init_tag) {}
2300 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2301 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302
Eric Fiselier9d355982017-04-12 23:45:53 +00002303 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002304 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2305 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002306 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002307 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002308 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002309 : __value_type(_VSTD::forward<_Up>(__u))
2310 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002312#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002313 template <class... _Args, size_t... _Indexes>
2314 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2315 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2316 __tuple_indices<_Indexes...>)
2317 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002318#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319
Alex Lorenz76132112017-11-09 17:54:49 +00002320 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2321 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002322 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323};
2324
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002326class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2327 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002328 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2329 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002330
2331 // NOTE: This static assert should never fire because __compressed_pair
2332 // is *almost never* used in a scenario where it's possible for T1 == T2.
2333 // (The exception is std::function where it is possible that the function
2334 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002335 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002336 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2337 "The current implementation is NOT ABI-compatible with the previous "
2338 "implementation for this configuration");
2339
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002341 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002342 class = typename enable_if<
2343 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2344 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2345 >::type
2346 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002347 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002348 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349
Eric Fiselier9d355982017-04-12 23:45:53 +00002350 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002351 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002352 __compressed_pair(_U1&& __t1, _U2&& __t2)
2353 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002355#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002356 template <class... _Args1, class... _Args2>
2357 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2358 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2359 tuple<_Args2...> __second_args)
2360 : _Base1(__pc, _VSTD::move(__first_args),
2361 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2362 _Base2(__pc, _VSTD::move(__second_args),
2363 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002364#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365
Eric Fiselier9d355982017-04-12 23:45:53 +00002366 _LIBCPP_INLINE_VISIBILITY
2367 typename _Base1::reference first() _NOEXCEPT {
2368 return static_cast<_Base1&>(*this).__get();
2369 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370
Eric Fiselier9d355982017-04-12 23:45:53 +00002371 _LIBCPP_INLINE_VISIBILITY
2372 typename _Base1::const_reference first() const _NOEXCEPT {
2373 return static_cast<_Base1 const&>(*this).__get();
2374 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375
Eric Fiselier9d355982017-04-12 23:45:53 +00002376 _LIBCPP_INLINE_VISIBILITY
2377 typename _Base2::reference second() _NOEXCEPT {
2378 return static_cast<_Base2&>(*this).__get();
2379 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380
Eric Fiselier9d355982017-04-12 23:45:53 +00002381 _LIBCPP_INLINE_VISIBILITY
2382 typename _Base2::const_reference second() const _NOEXCEPT {
2383 return static_cast<_Base2 const&>(*this).__get();
2384 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385
Eric Fiselier9d355982017-04-12 23:45:53 +00002386 _LIBCPP_INLINE_VISIBILITY
2387 void swap(__compressed_pair& __x)
2388 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2389 __is_nothrow_swappable<_T2>::value)
2390 {
2391 using std::swap;
2392 swap(first(), __x.first());
2393 swap(second(), __x.second());
2394 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395};
2396
2397template <class _T1, class _T2>
2398inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002399void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2400 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2401 __is_nothrow_swappable<_T2>::value) {
2402 __x.swap(__y);
2403}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002405// default_delete
2406
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002408struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002409 static_assert(!is_function<_Tp>::value,
2410 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002411#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002412 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002413#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002414 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002415#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002416 template <class _Up>
2417 _LIBCPP_INLINE_VISIBILITY
2418 default_delete(const default_delete<_Up>&,
2419 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2420 0) _NOEXCEPT {}
2421
2422 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2423 static_assert(sizeof(_Tp) > 0,
2424 "default_delete can not delete incomplete type");
2425 static_assert(!is_void<_Tp>::value,
2426 "default_delete can not delete incomplete type");
2427 delete __ptr;
2428 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429};
2430
2431template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002432struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2433private:
2434 template <class _Up>
2435 struct _EnableIfConvertible
2436 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2437
Howard Hinnant4500ca52011-12-18 21:19:44 +00002438public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002439#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002440 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002441#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002442 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002443#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002444
2445 template <class _Up>
2446 _LIBCPP_INLINE_VISIBILITY
2447 default_delete(const default_delete<_Up[]>&,
2448 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2449
2450 template <class _Up>
2451 _LIBCPP_INLINE_VISIBILITY
2452 typename _EnableIfConvertible<_Up>::type
2453 operator()(_Up* __ptr) const _NOEXCEPT {
2454 static_assert(sizeof(_Tp) > 0,
2455 "default_delete can not delete incomplete type");
2456 static_assert(!is_void<_Tp>::value,
2457 "default_delete can not delete void type");
2458 delete[] __ptr;
2459 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460};
2461
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002462template <class _Deleter>
2463struct __unique_ptr_deleter_sfinae {
2464 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2465 typedef const _Deleter& __lval_ref_type;
2466 typedef _Deleter&& __good_rval_ref_type;
2467 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468};
2469
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002470template <class _Deleter>
2471struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2472 typedef const _Deleter& __lval_ref_type;
2473 typedef const _Deleter&& __bad_rval_ref_type;
2474 typedef false_type __enable_rval_overload;
2475};
2476
2477template <class _Deleter>
2478struct __unique_ptr_deleter_sfinae<_Deleter&> {
2479 typedef _Deleter& __lval_ref_type;
2480 typedef _Deleter&& __bad_rval_ref_type;
2481 typedef false_type __enable_rval_overload;
2482};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002483
2484template <class _Tp, class _Dp = default_delete<_Tp> >
2485class _LIBCPP_TEMPLATE_VIS unique_ptr {
2486public:
2487 typedef _Tp element_type;
2488 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002489 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002490
2491 static_assert(!is_rvalue_reference<deleter_type>::value,
2492 "the specified deleter type cannot be an rvalue reference");
2493
2494private:
2495 __compressed_pair<pointer, deleter_type> __ptr_;
2496
2497 struct __nat { int __for_bool_; };
2498
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002499 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002500
2501 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002502 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002503 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002504
2505 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002506 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002507 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002508
2509 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002510 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002511 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002512
2513 template <bool _Dummy, class _Deleter = typename __dependent_type<
2514 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002515 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002516 typename enable_if<is_default_constructible<_Deleter>::value &&
2517 !is_pointer<_Deleter>::value>::type;
2518
2519 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002520 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002521 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2522
2523 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002524 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002525 is_convertible<typename _UPtr::pointer, pointer>::value &&
2526 !is_array<_Up>::value
2527 >::type;
2528
2529 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002530 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002531 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2532 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2533 >::type;
2534
2535 template <class _UDel>
2536 using _EnableIfDeleterAssignable = typename enable_if<
2537 is_assignable<_Dp&, _UDel&&>::value
2538 >::type;
2539
2540public:
2541 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002542 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002543 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002544 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002545
2546 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002547 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002548 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002549 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002550
2551 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002552 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002553 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002554 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002555
2556 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002557 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002558 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002559 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002560 : __ptr_(__p, __d) {}
2561
2562 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002563 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002564 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002565 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002566 : __ptr_(__p, _VSTD::move(__d)) {
2567 static_assert(!is_reference<deleter_type>::value,
2568 "rvalue deleter bound to reference");
2569 }
2570
2571 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002572 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002573 _LIBCPP_INLINE_VISIBILITY
2574 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2575
2576 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002577 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002578 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2579 }
2580
2581 template <class _Up, class _Ep,
2582 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2583 class = _EnableIfDeleterConvertible<_Ep>
2584 >
2585 _LIBCPP_INLINE_VISIBILITY
2586 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2587 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2588
2589#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2590 template <class _Up>
2591 _LIBCPP_INLINE_VISIBILITY
2592 unique_ptr(auto_ptr<_Up>&& __p,
2593 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002594 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002595 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002596 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002597#endif
2598
2599 _LIBCPP_INLINE_VISIBILITY
2600 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2601 reset(__u.release());
2602 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2603 return *this;
2604 }
2605
2606 template <class _Up, class _Ep,
2607 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2608 class = _EnableIfDeleterAssignable<_Ep>
2609 >
2610 _LIBCPP_INLINE_VISIBILITY
2611 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2612 reset(__u.release());
2613 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2614 return *this;
2615 }
2616
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002617#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2618 template <class _Up>
2619 _LIBCPP_INLINE_VISIBILITY
2620 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2621 is_same<_Dp, default_delete<_Tp> >::value,
2622 unique_ptr&>::type
2623 operator=(auto_ptr<_Up> __p) {
2624 reset(__p.release());
2625 return *this;
2626 }
2627#endif
2628
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002629#ifdef _LIBCPP_CXX03_LANG
2630 unique_ptr(unique_ptr const&) = delete;
2631 unique_ptr& operator=(unique_ptr const&) = delete;
2632#endif
2633
2634
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002635 _LIBCPP_INLINE_VISIBILITY
2636 ~unique_ptr() { reset(); }
2637
2638 _LIBCPP_INLINE_VISIBILITY
2639 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2640 reset();
2641 return *this;
2642 }
2643
2644 _LIBCPP_INLINE_VISIBILITY
2645 typename add_lvalue_reference<_Tp>::type
2646 operator*() const {
2647 return *__ptr_.first();
2648 }
2649 _LIBCPP_INLINE_VISIBILITY
2650 pointer operator->() const _NOEXCEPT {
2651 return __ptr_.first();
2652 }
2653 _LIBCPP_INLINE_VISIBILITY
2654 pointer get() const _NOEXCEPT {
2655 return __ptr_.first();
2656 }
2657 _LIBCPP_INLINE_VISIBILITY
2658 deleter_type& get_deleter() _NOEXCEPT {
2659 return __ptr_.second();
2660 }
2661 _LIBCPP_INLINE_VISIBILITY
2662 const deleter_type& get_deleter() const _NOEXCEPT {
2663 return __ptr_.second();
2664 }
2665 _LIBCPP_INLINE_VISIBILITY
2666 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2667 return __ptr_.first() != nullptr;
2668 }
2669
2670 _LIBCPP_INLINE_VISIBILITY
2671 pointer release() _NOEXCEPT {
2672 pointer __t = __ptr_.first();
2673 __ptr_.first() = pointer();
2674 return __t;
2675 }
2676
2677 _LIBCPP_INLINE_VISIBILITY
2678 void reset(pointer __p = pointer()) _NOEXCEPT {
2679 pointer __tmp = __ptr_.first();
2680 __ptr_.first() = __p;
2681 if (__tmp)
2682 __ptr_.second()(__tmp);
2683 }
2684
2685 _LIBCPP_INLINE_VISIBILITY
2686 void swap(unique_ptr& __u) _NOEXCEPT {
2687 __ptr_.swap(__u.__ptr_);
2688 }
2689};
2690
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002691
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002693class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002695 typedef _Tp element_type;
2696 typedef _Dp deleter_type;
2697 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2698
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002700 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701
Eric Fiselier31127cd2017-04-16 02:14:31 +00002702 template <class _From>
2703 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704
Eric Fiselier31127cd2017-04-16 02:14:31 +00002705 template <class _FromElem>
2706 struct _CheckArrayPointerConversion<_FromElem*>
2707 : integral_constant<bool,
2708 is_same<_FromElem*, pointer>::value ||
2709 (is_same<pointer, element_type*>::value &&
2710 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2711 >
2712 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713
Eric Fiselier31127cd2017-04-16 02:14:31 +00002714 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715
2716 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002717 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002718 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002719
2720 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002721 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002722 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002723
2724 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002725 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002726 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727
2728 template <bool _Dummy, class _Deleter = typename __dependent_type<
2729 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002730 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002731 typename enable_if<is_default_constructible<_Deleter>::value &&
2732 !is_pointer<_Deleter>::value>::type;
2733
2734 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002735 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002736 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2737
2738 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002739 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002740 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002741 >::type;
2742
2743 template <class _UPtr, class _Up,
2744 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002745 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002746 is_array<_Up>::value &&
2747 is_same<pointer, element_type*>::value &&
2748 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2749 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2750 >::type;
2751
2752 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002753 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2755 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2756 >::type;
2757
2758 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002759 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002760 is_assignable<_Dp&, _UDel&&>::value
2761 >::type;
2762
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002764 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002765 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002766 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002767 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002769 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002770 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002771 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002772 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002774 template <class _Pp, bool _Dummy = true,
2775 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002776 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002777 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002778 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002779 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002781 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002782 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2783 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002784 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002785 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002786 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002788 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002789 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002790 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002791 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002792 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002794 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002795 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2796 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002797 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002798 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002799 : __ptr_(__p, _VSTD::move(__d)) {
2800 static_assert(!is_reference<deleter_type>::value,
2801 "rvalue deleter bound to reference");
2802 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002804 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002805 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002806 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002807 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002808 : __ptr_(nullptr, _VSTD::move(__d)) {
2809 static_assert(!is_reference<deleter_type>::value,
2810 "rvalue deleter bound to reference");
2811 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002812
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002813 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002814 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2815 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002816 _LIBCPP_INLINE_VISIBILITY
2817 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002818
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002819 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002820 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002821 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2822 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002823
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002824 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002825 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002826 reset(__u.release());
2827 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2828 return *this;
2829 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002831 template <class _Up, class _Ep,
2832 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2833 class = _EnableIfDeleterConvertible<_Ep>
2834 >
2835 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002836 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002837 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002838 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002840 template <class _Up, class _Ep,
2841 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2842 class = _EnableIfDeleterAssignable<_Ep>
2843 >
2844 _LIBCPP_INLINE_VISIBILITY
2845 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002846 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002847 reset(__u.release());
2848 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2849 return *this;
2850 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002852#ifdef _LIBCPP_CXX03_LANG
2853 unique_ptr(unique_ptr const&) = delete;
2854 unique_ptr& operator=(unique_ptr const&) = delete;
2855#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002856
2857public:
2858 _LIBCPP_INLINE_VISIBILITY
2859 ~unique_ptr() { reset(); }
2860
2861 _LIBCPP_INLINE_VISIBILITY
2862 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2863 reset();
2864 return *this;
2865 }
2866
2867 _LIBCPP_INLINE_VISIBILITY
2868 typename add_lvalue_reference<_Tp>::type
2869 operator[](size_t __i) const {
2870 return __ptr_.first()[__i];
2871 }
2872 _LIBCPP_INLINE_VISIBILITY
2873 pointer get() const _NOEXCEPT {
2874 return __ptr_.first();
2875 }
2876
2877 _LIBCPP_INLINE_VISIBILITY
2878 deleter_type& get_deleter() _NOEXCEPT {
2879 return __ptr_.second();
2880 }
2881
2882 _LIBCPP_INLINE_VISIBILITY
2883 const deleter_type& get_deleter() const _NOEXCEPT {
2884 return __ptr_.second();
2885 }
2886 _LIBCPP_INLINE_VISIBILITY
2887 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2888 return __ptr_.first() != nullptr;
2889 }
2890
2891 _LIBCPP_INLINE_VISIBILITY
2892 pointer release() _NOEXCEPT {
2893 pointer __t = __ptr_.first();
2894 __ptr_.first() = pointer();
2895 return __t;
2896 }
2897
2898 template <class _Pp>
2899 _LIBCPP_INLINE_VISIBILITY
2900 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002901 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002902 >::type
2903 reset(_Pp __p) _NOEXCEPT {
2904 pointer __tmp = __ptr_.first();
2905 __ptr_.first() = __p;
2906 if (__tmp)
2907 __ptr_.second()(__tmp);
2908 }
2909
2910 _LIBCPP_INLINE_VISIBILITY
2911 void reset(nullptr_t = nullptr) _NOEXCEPT {
2912 pointer __tmp = __ptr_.first();
2913 __ptr_.first() = nullptr;
2914 if (__tmp)
2915 __ptr_.second()(__tmp);
2916 }
2917
2918 _LIBCPP_INLINE_VISIBILITY
2919 void swap(unique_ptr& __u) _NOEXCEPT {
2920 __ptr_.swap(__u.__ptr_);
2921 }
2922
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923};
2924
2925template <class _Tp, class _Dp>
2926inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002927typename enable_if<
2928 __is_swappable<_Dp>::value,
2929 void
2930>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002931swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932
2933template <class _T1, class _D1, class _T2, class _D2>
2934inline _LIBCPP_INLINE_VISIBILITY
2935bool
2936operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2937
2938template <class _T1, class _D1, class _T2, class _D2>
2939inline _LIBCPP_INLINE_VISIBILITY
2940bool
2941operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2942
2943template <class _T1, class _D1, class _T2, class _D2>
2944inline _LIBCPP_INLINE_VISIBILITY
2945bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002946operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2947{
2948 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2949 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002950 typedef typename common_type<_P1, _P2>::type _Vp;
2951 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002952}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953
2954template <class _T1, class _D1, class _T2, class _D2>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
2957operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2958
2959template <class _T1, class _D1, class _T2, class _D2>
2960inline _LIBCPP_INLINE_VISIBILITY
2961bool
2962operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2963
2964template <class _T1, class _D1, class _T2, class _D2>
2965inline _LIBCPP_INLINE_VISIBILITY
2966bool
2967operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2968
Howard Hinnantb17caf92012-02-21 21:02:58 +00002969template <class _T1, class _D1>
2970inline _LIBCPP_INLINE_VISIBILITY
2971bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002972operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002973{
2974 return !__x;
2975}
2976
2977template <class _T1, class _D1>
2978inline _LIBCPP_INLINE_VISIBILITY
2979bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002980operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002981{
2982 return !__x;
2983}
2984
2985template <class _T1, class _D1>
2986inline _LIBCPP_INLINE_VISIBILITY
2987bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002988operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002989{
2990 return static_cast<bool>(__x);
2991}
2992
2993template <class _T1, class _D1>
2994inline _LIBCPP_INLINE_VISIBILITY
2995bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002996operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002997{
2998 return static_cast<bool>(__x);
2999}
3000
3001template <class _T1, class _D1>
3002inline _LIBCPP_INLINE_VISIBILITY
3003bool
3004operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3005{
3006 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3007 return less<_P1>()(__x.get(), nullptr);
3008}
3009
3010template <class _T1, class _D1>
3011inline _LIBCPP_INLINE_VISIBILITY
3012bool
3013operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3014{
3015 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3016 return less<_P1>()(nullptr, __x.get());
3017}
3018
3019template <class _T1, class _D1>
3020inline _LIBCPP_INLINE_VISIBILITY
3021bool
3022operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3023{
3024 return nullptr < __x;
3025}
3026
3027template <class _T1, class _D1>
3028inline _LIBCPP_INLINE_VISIBILITY
3029bool
3030operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3031{
3032 return __x < nullptr;
3033}
3034
3035template <class _T1, class _D1>
3036inline _LIBCPP_INLINE_VISIBILITY
3037bool
3038operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3039{
3040 return !(nullptr < __x);
3041}
3042
3043template <class _T1, class _D1>
3044inline _LIBCPP_INLINE_VISIBILITY
3045bool
3046operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3047{
3048 return !(__x < nullptr);
3049}
3050
3051template <class _T1, class _D1>
3052inline _LIBCPP_INLINE_VISIBILITY
3053bool
3054operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3055{
3056 return !(__x < nullptr);
3057}
3058
3059template <class _T1, class _D1>
3060inline _LIBCPP_INLINE_VISIBILITY
3061bool
3062operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3063{
3064 return !(nullptr < __x);
3065}
3066
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003067#if _LIBCPP_STD_VER > 11
3068
3069template<class _Tp>
3070struct __unique_if
3071{
3072 typedef unique_ptr<_Tp> __unique_single;
3073};
3074
3075template<class _Tp>
3076struct __unique_if<_Tp[]>
3077{
3078 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3079};
3080
3081template<class _Tp, size_t _Np>
3082struct __unique_if<_Tp[_Np]>
3083{
3084 typedef void __unique_array_known_bound;
3085};
3086
3087template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003088inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003089typename __unique_if<_Tp>::__unique_single
3090make_unique(_Args&&... __args)
3091{
3092 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3093}
3094
3095template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003096inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003097typename __unique_if<_Tp>::__unique_array_unknown_bound
3098make_unique(size_t __n)
3099{
3100 typedef typename remove_extent<_Tp>::type _Up;
3101 return unique_ptr<_Tp>(new _Up[__n]());
3102}
3103
3104template<class _Tp, class... _Args>
3105 typename __unique_if<_Tp>::__unique_array_known_bound
3106 make_unique(_Args&&...) = delete;
3107
3108#endif // _LIBCPP_STD_VER > 11
3109
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003110template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003111#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003112struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003113#else
3114struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003115 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003116#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003117{
3118 typedef unique_ptr<_Tp, _Dp> argument_type;
3119 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003120 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003121 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003122 {
3123 typedef typename argument_type::pointer pointer;
3124 return hash<pointer>()(__ptr.get());
3125 }
3126};
3127
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128struct __destruct_n
3129{
3130private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003131 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132
3133 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003134 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003135 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136
3137 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003138 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139 {}
3140
Howard Hinnant719bda32011-05-28 14:41:13 +00003141 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003142 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003143 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144 {}
3145
Howard Hinnant719bda32011-05-28 14:41:13 +00003146 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003147 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003148 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 {}
3150public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003151 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003152 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153
3154 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003155 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003156 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157
3158 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003159 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003160 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161
3162 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003163 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003164 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165};
3166
3167template <class _Alloc>
3168class __allocator_destructor
3169{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003170 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003172 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3173 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174private:
3175 _Alloc& __alloc_;
3176 size_type __s_;
3177public:
3178 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003179 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003182 void operator()(pointer __p) _NOEXCEPT
3183 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184};
3185
3186template <class _InputIterator, class _ForwardIterator>
3187_ForwardIterator
3188uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3189{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003191#ifndef _LIBCPP_NO_EXCEPTIONS
3192 _ForwardIterator __s = __r;
3193 try
3194 {
3195#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003196 for (; __f != __l; ++__f, (void) ++__r)
3197 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003198#ifndef _LIBCPP_NO_EXCEPTIONS
3199 }
3200 catch (...)
3201 {
3202 for (; __s != __r; ++__s)
3203 __s->~value_type();
3204 throw;
3205 }
3206#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 return __r;
3208}
3209
3210template <class _InputIterator, class _Size, class _ForwardIterator>
3211_ForwardIterator
3212uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3213{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003215#ifndef _LIBCPP_NO_EXCEPTIONS
3216 _ForwardIterator __s = __r;
3217 try
3218 {
3219#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003220 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3221 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003222#ifndef _LIBCPP_NO_EXCEPTIONS
3223 }
3224 catch (...)
3225 {
3226 for (; __s != __r; ++__s)
3227 __s->~value_type();
3228 throw;
3229 }
3230#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231 return __r;
3232}
3233
3234template <class _ForwardIterator, class _Tp>
3235void
3236uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3237{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003239#ifndef _LIBCPP_NO_EXCEPTIONS
3240 _ForwardIterator __s = __f;
3241 try
3242 {
3243#endif
3244 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003245 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003246#ifndef _LIBCPP_NO_EXCEPTIONS
3247 }
3248 catch (...)
3249 {
3250 for (; __s != __f; ++__s)
3251 __s->~value_type();
3252 throw;
3253 }
3254#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255}
3256
3257template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003258_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3260{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003262#ifndef _LIBCPP_NO_EXCEPTIONS
3263 _ForwardIterator __s = __f;
3264 try
3265 {
3266#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003267 for (; __n > 0; ++__f, (void) --__n)
3268 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003269#ifndef _LIBCPP_NO_EXCEPTIONS
3270 }
3271 catch (...)
3272 {
3273 for (; __s != __f; ++__s)
3274 __s->~value_type();
3275 throw;
3276 }
3277#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003278 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279}
3280
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003281#if _LIBCPP_STD_VER > 14
3282
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003283template <class _Tp>
3284inline _LIBCPP_INLINE_VISIBILITY
3285void destroy_at(_Tp* __loc) {
3286 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3287 __loc->~_Tp();
3288}
3289
3290template <class _ForwardIterator>
3291inline _LIBCPP_INLINE_VISIBILITY
3292void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3293 for (; __first != __last; ++__first)
3294 _VSTD::destroy_at(_VSTD::addressof(*__first));
3295}
3296
3297template <class _ForwardIterator, class _Size>
3298inline _LIBCPP_INLINE_VISIBILITY
3299_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3300 for (; __n > 0; (void)++__first, --__n)
3301 _VSTD::destroy_at(_VSTD::addressof(*__first));
3302 return __first;
3303}
3304
Eric Fiselier290c07c2016-10-11 21:13:44 +00003305template <class _ForwardIterator>
3306inline _LIBCPP_INLINE_VISIBILITY
3307void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3308 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3309 auto __idx = __first;
3310#ifndef _LIBCPP_NO_EXCEPTIONS
3311 try {
3312#endif
3313 for (; __idx != __last; ++__idx)
3314 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3315#ifndef _LIBCPP_NO_EXCEPTIONS
3316 } catch (...) {
3317 _VSTD::destroy(__first, __idx);
3318 throw;
3319 }
3320#endif
3321}
3322
3323template <class _ForwardIterator, class _Size>
3324inline _LIBCPP_INLINE_VISIBILITY
3325_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3326 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3327 auto __idx = __first;
3328#ifndef _LIBCPP_NO_EXCEPTIONS
3329 try {
3330#endif
3331 for (; __n > 0; (void)++__idx, --__n)
3332 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3333 return __idx;
3334#ifndef _LIBCPP_NO_EXCEPTIONS
3335 } catch (...) {
3336 _VSTD::destroy(__first, __idx);
3337 throw;
3338 }
3339#endif
3340}
3341
3342
3343template <class _ForwardIterator>
3344inline _LIBCPP_INLINE_VISIBILITY
3345void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3346 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3347 auto __idx = __first;
3348#ifndef _LIBCPP_NO_EXCEPTIONS
3349 try {
3350#endif
3351 for (; __idx != __last; ++__idx)
3352 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3353#ifndef _LIBCPP_NO_EXCEPTIONS
3354 } catch (...) {
3355 _VSTD::destroy(__first, __idx);
3356 throw;
3357 }
3358#endif
3359}
3360
3361template <class _ForwardIterator, class _Size>
3362inline _LIBCPP_INLINE_VISIBILITY
3363_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3364 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3365 auto __idx = __first;
3366#ifndef _LIBCPP_NO_EXCEPTIONS
3367 try {
3368#endif
3369 for (; __n > 0; (void)++__idx, --__n)
3370 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3371 return __idx;
3372#ifndef _LIBCPP_NO_EXCEPTIONS
3373 } catch (...) {
3374 _VSTD::destroy(__first, __idx);
3375 throw;
3376 }
3377#endif
3378}
3379
3380
3381template <class _InputIt, class _ForwardIt>
3382inline _LIBCPP_INLINE_VISIBILITY
3383_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3384 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3385 auto __idx = __first_res;
3386#ifndef _LIBCPP_NO_EXCEPTIONS
3387 try {
3388#endif
3389 for (; __first != __last; (void)++__idx, ++__first)
3390 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3391 return __idx;
3392#ifndef _LIBCPP_NO_EXCEPTIONS
3393 } catch (...) {
3394 _VSTD::destroy(__first_res, __idx);
3395 throw;
3396 }
3397#endif
3398}
3399
3400template <class _InputIt, class _Size, class _ForwardIt>
3401inline _LIBCPP_INLINE_VISIBILITY
3402pair<_InputIt, _ForwardIt>
3403uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3404 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3405 auto __idx = __first_res;
3406#ifndef _LIBCPP_NO_EXCEPTIONS
3407 try {
3408#endif
3409 for (; __n > 0; ++__idx, (void)++__first, --__n)
3410 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3411 return {__first, __idx};
3412#ifndef _LIBCPP_NO_EXCEPTIONS
3413 } catch (...) {
3414 _VSTD::destroy(__first_res, __idx);
3415 throw;
3416 }
3417#endif
3418}
3419
3420
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003421#endif // _LIBCPP_STD_VER > 14
3422
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003423// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3424// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003425// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003426#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3427 && defined(__ATOMIC_RELAXED) \
3428 && defined(__ATOMIC_ACQ_REL)
3429# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003430#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003431# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3432#endif
3433
3434template <class _Tp>
3435inline _LIBCPP_INLINE_VISIBILITY _Tp
3436__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3437{
3438#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3439 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3440#else
3441 return __t += 1;
3442#endif
3443}
3444
3445template <class _Tp>
3446inline _LIBCPP_INLINE_VISIBILITY _Tp
3447__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3448{
3449#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3450 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3451#else
3452 return __t -= 1;
3453#endif
3454}
3455
Howard Hinnant756c69b2010-09-22 16:48:34 +00003456class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457 : public std::exception
3458{
3459public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003460 virtual ~bad_weak_ptr() _NOEXCEPT;
3461 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462};
3463
Louis Dionne16fe2952018-07-11 23:14:33 +00003464_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003465void __throw_bad_weak_ptr()
3466{
3467#ifndef _LIBCPP_NO_EXCEPTIONS
3468 throw bad_weak_ptr();
3469#else
3470 _VSTD::abort();
3471#endif
3472}
3473
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003474template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003476class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477{
3478 __shared_count(const __shared_count&);
3479 __shared_count& operator=(const __shared_count&);
3480
3481protected:
3482 long __shared_owners_;
3483 virtual ~__shared_count();
3484private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003485 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486
3487public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003489 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490 : __shared_owners_(__refs) {}
3491
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003492#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003493 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003494 void __add_shared() _NOEXCEPT;
3495 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003496#else
3497 _LIBCPP_INLINE_VISIBILITY
3498 void __add_shared() _NOEXCEPT {
3499 __libcpp_atomic_refcount_increment(__shared_owners_);
3500 }
3501 _LIBCPP_INLINE_VISIBILITY
3502 bool __release_shared() _NOEXCEPT {
3503 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3504 __on_zero_shared();
3505 return true;
3506 }
3507 return false;
3508 }
3509#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003510 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003511 long use_count() const _NOEXCEPT {
3512 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3513 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514};
3515
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003516class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517 : private __shared_count
3518{
3519 long __shared_weak_owners_;
3520
3521public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003523 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524 : __shared_count(__refs),
3525 __shared_weak_owners_(__refs) {}
3526protected:
3527 virtual ~__shared_weak_count();
3528
3529public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003530#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003531 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003532 void __add_shared() _NOEXCEPT;
3533 void __add_weak() _NOEXCEPT;
3534 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003535#else
3536 _LIBCPP_INLINE_VISIBILITY
3537 void __add_shared() _NOEXCEPT {
3538 __shared_count::__add_shared();
3539 }
3540 _LIBCPP_INLINE_VISIBILITY
3541 void __add_weak() _NOEXCEPT {
3542 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3543 }
3544 _LIBCPP_INLINE_VISIBILITY
3545 void __release_shared() _NOEXCEPT {
3546 if (__shared_count::__release_shared())
3547 __release_weak();
3548 }
3549#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003550 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003552 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3553 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554
Howard Hinnant807d6332013-02-25 15:50:36 +00003555 // Define the function out only if we build static libc++ without RTTI.
3556 // Otherwise we may break clients who need to compile their projects with
3557 // -fno-rtti and yet link against a libc++.dylib compiled
3558 // without -fno-rtti.
3559#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003560 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003561#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003563 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564};
3565
3566template <class _Tp, class _Dp, class _Alloc>
3567class __shared_ptr_pointer
3568 : public __shared_weak_count
3569{
3570 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3571public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003574 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575
Howard Hinnant72f73582010-08-11 17:04:31 +00003576#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003577 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003578#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579
3580private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003581 virtual void __on_zero_shared() _NOEXCEPT;
3582 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583};
3584
Howard Hinnant72f73582010-08-11 17:04:31 +00003585#ifndef _LIBCPP_NO_RTTI
3586
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587template <class _Tp, class _Dp, class _Alloc>
3588const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003589__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003591 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592}
3593
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003594#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003595
Howard Hinnantc51e1022010-05-11 19:42:16 +00003596template <class _Tp, class _Dp, class _Alloc>
3597void
Howard Hinnant719bda32011-05-28 14:41:13 +00003598__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599{
3600 __data_.first().second()(__data_.first().first());
3601 __data_.first().second().~_Dp();
3602}
3603
3604template <class _Tp, class _Dp, class _Alloc>
3605void
Howard Hinnant719bda32011-05-28 14:41:13 +00003606__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003608 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3609 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003610 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3611
Eric Fiselierf8898c82015-02-05 23:01:40 +00003612 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003614 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615}
3616
3617template <class _Tp, class _Alloc>
3618class __shared_ptr_emplace
3619 : public __shared_weak_count
3620{
3621 __compressed_pair<_Alloc, _Tp> __data_;
3622public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623
Howard Hinnant756c69b2010-09-22 16:48:34 +00003624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003626 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003628
3629#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003633 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3634 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635#else // _LIBCPP_HAS_NO_VARIADICS
3636
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3640 : __data_(__a, _Tp(__a0)) {}
3641
3642 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3645 : __data_(__a, _Tp(__a0, __a1)) {}
3646
3647 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3650 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3651
3652#endif // _LIBCPP_HAS_NO_VARIADICS
3653
3654private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003655 virtual void __on_zero_shared() _NOEXCEPT;
3656 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003658 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003659 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660};
3661
3662template <class _Tp, class _Alloc>
3663void
Howard Hinnant719bda32011-05-28 14:41:13 +00003664__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665{
3666 __data_.second().~_Tp();
3667}
3668
3669template <class _Tp, class _Alloc>
3670void
Howard Hinnant719bda32011-05-28 14:41:13 +00003671__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003673 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3674 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003675 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003676 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003678 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679}
3680
Erik Pilkington2a398762017-05-25 15:43:31 +00003681struct __shared_ptr_dummy_rebind_allocator_type;
3682template <>
3683class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3684{
3685public:
3686 template <class _Other>
3687 struct rebind
3688 {
3689 typedef allocator<_Other> other;
3690 };
3691};
3692
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003693template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694
3695template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003696class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003698public:
3699 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003700
Eric Fiselierae5b6672016-06-27 01:02:43 +00003701#if _LIBCPP_STD_VER > 14
3702 typedef weak_ptr<_Tp> weak_type;
3703#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704private:
3705 element_type* __ptr_;
3706 __shared_weak_count* __cntrl_;
3707
3708 struct __nat {int __for_bool_;};
3709public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003711 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003713 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003714 template<class _Yp>
3715 explicit shared_ptr(_Yp* __p,
3716 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3717 template<class _Yp, class _Dp>
3718 shared_ptr(_Yp* __p, _Dp __d,
3719 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3720 template<class _Yp, class _Dp, class _Alloc>
3721 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3722 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3724 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003725 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003727 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003731 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003732 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003733#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003735 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003736 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003737 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003738 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003739#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003741 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003742#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003743#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003744 template<class _Yp>
3745 shared_ptr(auto_ptr<_Yp>&& __r,
3746 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003747#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003748 template<class _Yp>
3749 shared_ptr(auto_ptr<_Yp> __r,
3750 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003752#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003753#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003754 template <class _Yp, class _Dp>
3755 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3756 typename enable_if
3757 <
3758 !is_lvalue_reference<_Dp>::value &&
3759 !is_array<_Yp>::value &&
3760 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3761 __nat
3762 >::type = __nat());
3763 template <class _Yp, class _Dp>
3764 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3765 typename enable_if
3766 <
3767 is_lvalue_reference<_Dp>::value &&
3768 !is_array<_Yp>::value &&
3769 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3770 __nat
3771 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003772#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003773 template <class _Yp, class _Dp>
3774 shared_ptr(unique_ptr<_Yp, _Dp>,
3775 typename enable_if
3776 <
3777 !is_lvalue_reference<_Dp>::value &&
3778 !is_array<_Yp>::value &&
3779 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3780 __nat
3781 >::type = __nat());
3782 template <class _Yp, class _Dp>
3783 shared_ptr(unique_ptr<_Yp, _Dp>,
3784 typename enable_if
3785 <
3786 is_lvalue_reference<_Dp>::value &&
3787 !is_array<_Yp>::value &&
3788 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3789 __nat
3790 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003791#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792
3793 ~shared_ptr();
3794
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003796 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003797 template<class _Yp>
3798 typename enable_if
3799 <
3800 is_convertible<_Yp*, element_type*>::value,
3801 shared_ptr&
3802 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003804 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003805#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003807 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003808 template<class _Yp>
3809 typename enable_if
3810 <
3811 is_convertible<_Yp*, element_type*>::value,
3812 shared_ptr<_Tp>&
3813 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003815 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003816#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003817 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003819 typename enable_if
3820 <
3821 !is_array<_Yp>::value &&
3822 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003823 shared_ptr
3824 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003825 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003826#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003827#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003828#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003829 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003831 typename enable_if
3832 <
3833 !is_array<_Yp>::value &&
3834 is_convertible<_Yp*, element_type*>::value,
3835 shared_ptr&
3836 >::type
3837 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003839#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003840 template <class _Yp, class _Dp>
3841 typename enable_if
3842 <
3843 !is_array<_Yp>::value &&
3844 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3845 shared_ptr&
3846 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003847#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003849 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003850#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003852 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853#endif
3854
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003856 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003858 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003859 template<class _Yp>
3860 typename enable_if
3861 <
3862 is_convertible<_Yp*, element_type*>::value,
3863 void
3864 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003866 reset(_Yp* __p);
3867 template<class _Yp, class _Dp>
3868 typename enable_if
3869 <
3870 is_convertible<_Yp*, element_type*>::value,
3871 void
3872 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003874 reset(_Yp* __p, _Dp __d);
3875 template<class _Yp, class _Dp, class _Alloc>
3876 typename enable_if
3877 <
3878 is_convertible<_Yp*, element_type*>::value,
3879 void
3880 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003882 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003883
Howard Hinnant756c69b2010-09-22 16:48:34 +00003884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003885 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003887 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3888 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003890 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003892 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003894 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003896 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003897 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003898 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003899 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003901 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003902 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003903 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003905 _LIBCPP_INLINE_VISIBILITY
3906 bool
3907 __owner_equivalent(const shared_ptr& __p) const
3908 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909
Howard Hinnant72f73582010-08-11 17:04:31 +00003910#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003913 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003914 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003915 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003916 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003917#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918
Zoe Carverd9040c72019-10-22 15:16:49 +00003919 template<class _Yp, class _CntrlBlk>
3920 static shared_ptr<_Tp>
3921 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3922 {
3923 shared_ptr<_Tp> __r;
3924 __r.__ptr_ = __p;
3925 __r.__cntrl_ = __cntrl;
3926 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3927 return __r;
3928 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003929
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003931 template <class _Yp, bool = is_function<_Yp>::value>
3932 struct __shared_ptr_default_allocator
3933 {
3934 typedef allocator<_Yp> type;
3935 };
3936
3937 template <class _Yp>
3938 struct __shared_ptr_default_allocator<_Yp, true>
3939 {
3940 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3941 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003943 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003944 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003945 typename enable_if<is_convertible<_OrigPtr*,
3946 const enable_shared_from_this<_Yp>*
3947 >::value,
3948 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003949 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3950 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003952 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003953 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003954 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003955 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3956 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003957 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958 }
3959
Erik Pilkington2a398762017-05-25 15:43:31 +00003960 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003962 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3963 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003964};
3965
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003966
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003968inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003969_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003970shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971 : __ptr_(0),
3972 __cntrl_(0)
3973{
3974}
3975
3976template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003977inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003978_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003979shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980 : __ptr_(0),
3981 __cntrl_(0)
3982{
3983}
3984
3985template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003986template<class _Yp>
3987shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3988 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989 : __ptr_(__p)
3990{
3991 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003992 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3993 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3994 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003996 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997}
3998
3999template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004000template<class _Yp, class _Dp>
4001shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4002 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003 : __ptr_(__p)
4004{
4005#ifndef _LIBCPP_NO_EXCEPTIONS
4006 try
4007 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004008#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004009 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4010 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4011 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004012 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013#ifndef _LIBCPP_NO_EXCEPTIONS
4014 }
4015 catch (...)
4016 {
4017 __d(__p);
4018 throw;
4019 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004020#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004021}
4022
4023template<class _Tp>
4024template<class _Dp>
4025shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4026 : __ptr_(0)
4027{
4028#ifndef _LIBCPP_NO_EXCEPTIONS
4029 try
4030 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004031#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004032 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4033 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4034 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035#ifndef _LIBCPP_NO_EXCEPTIONS
4036 }
4037 catch (...)
4038 {
4039 __d(__p);
4040 throw;
4041 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004042#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043}
4044
4045template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004046template<class _Yp, class _Dp, class _Alloc>
4047shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4048 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049 : __ptr_(__p)
4050{
4051#ifndef _LIBCPP_NO_EXCEPTIONS
4052 try
4053 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004054#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004056 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057 typedef __allocator_destructor<_A2> _D2;
4058 _A2 __a2(__a);
4059 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004060 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4061 _CntrlBlk(__p, __d, __a);
4062 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004063 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064#ifndef _LIBCPP_NO_EXCEPTIONS
4065 }
4066 catch (...)
4067 {
4068 __d(__p);
4069 throw;
4070 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004071#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072}
4073
4074template<class _Tp>
4075template<class _Dp, class _Alloc>
4076shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4077 : __ptr_(0)
4078{
4079#ifndef _LIBCPP_NO_EXCEPTIONS
4080 try
4081 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004082#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004084 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085 typedef __allocator_destructor<_A2> _D2;
4086 _A2 __a2(__a);
4087 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004088 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4089 _CntrlBlk(__p, __d, __a);
4090 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091#ifndef _LIBCPP_NO_EXCEPTIONS
4092 }
4093 catch (...)
4094 {
4095 __d(__p);
4096 throw;
4097 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004098#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099}
4100
4101template<class _Tp>
4102template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004103inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004104shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105 : __ptr_(__p),
4106 __cntrl_(__r.__cntrl_)
4107{
4108 if (__cntrl_)
4109 __cntrl_->__add_shared();
4110}
4111
4112template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004113inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004114shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115 : __ptr_(__r.__ptr_),
4116 __cntrl_(__r.__cntrl_)
4117{
4118 if (__cntrl_)
4119 __cntrl_->__add_shared();
4120}
4121
4122template<class _Tp>
4123template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004124inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004126 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004127 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128 : __ptr_(__r.__ptr_),
4129 __cntrl_(__r.__cntrl_)
4130{
4131 if (__cntrl_)
4132 __cntrl_->__add_shared();
4133}
4134
Howard Hinnant74279a52010-09-04 23:28:19 +00004135#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136
4137template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004138inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004139shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140 : __ptr_(__r.__ptr_),
4141 __cntrl_(__r.__cntrl_)
4142{
4143 __r.__ptr_ = 0;
4144 __r.__cntrl_ = 0;
4145}
4146
4147template<class _Tp>
4148template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004149inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004151 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004152 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153 : __ptr_(__r.__ptr_),
4154 __cntrl_(__r.__cntrl_)
4155{
4156 __r.__ptr_ = 0;
4157 __r.__cntrl_ = 0;
4158}
4159
Howard Hinnant74279a52010-09-04 23:28:19 +00004160#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161
Marshall Clowb22274f2017-01-24 22:22:33 +00004162#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004164template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004165#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004166shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004168shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004170 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171 : __ptr_(__r.get())
4172{
4173 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4174 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004175 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176 __r.release();
4177}
Marshall Clowb22274f2017-01-24 22:22:33 +00004178#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179
4180template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004181template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004182#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4184#else
4185shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4186#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004187 typename enable_if
4188 <
4189 !is_lvalue_reference<_Dp>::value &&
4190 !is_array<_Yp>::value &&
4191 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4192 __nat
4193 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194 : __ptr_(__r.get())
4195{
Marshall Clow35cde742015-05-10 13:59:45 +00004196#if _LIBCPP_STD_VER > 11
4197 if (__ptr_ == nullptr)
4198 __cntrl_ = nullptr;
4199 else
4200#endif
4201 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004202 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4203 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4204 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004205 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004206 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207 __r.release();
4208}
4209
4210template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004211template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004212#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4214#else
4215shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4216#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004217 typename enable_if
4218 <
4219 is_lvalue_reference<_Dp>::value &&
4220 !is_array<_Yp>::value &&
4221 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4222 __nat
4223 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224 : __ptr_(__r.get())
4225{
Marshall Clow35cde742015-05-10 13:59:45 +00004226#if _LIBCPP_STD_VER > 11
4227 if (__ptr_ == nullptr)
4228 __cntrl_ = nullptr;
4229 else
4230#endif
4231 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004232 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004233 typedef __shared_ptr_pointer<_Yp*,
4234 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004235 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004236 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004237 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004238 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239 __r.release();
4240}
4241
Zoe Carver6cd05c32019-08-19 15:47:16 +00004242template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243shared_ptr<_Tp>::~shared_ptr()
4244{
4245 if (__cntrl_)
4246 __cntrl_->__release_shared();
4247}
4248
4249template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004250inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004252shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253{
4254 shared_ptr(__r).swap(*this);
4255 return *this;
4256}
4257
4258template<class _Tp>
4259template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004260inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004261typename enable_if
4262<
Marshall Clow7e384b72017-01-10 16:59:33 +00004263 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004264 shared_ptr<_Tp>&
4265>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004266shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267{
4268 shared_ptr(__r).swap(*this);
4269 return *this;
4270}
4271
Howard Hinnant74279a52010-09-04 23:28:19 +00004272#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273
4274template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004275inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004277shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004279 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280 return *this;
4281}
4282
4283template<class _Tp>
4284template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004285inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004286typename enable_if
4287<
Marshall Clow7e384b72017-01-10 16:59:33 +00004288 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004289 shared_ptr<_Tp>&
4290>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004291shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4292{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004293 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294 return *this;
4295}
4296
Marshall Clowb22274f2017-01-24 22:22:33 +00004297#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004298template<class _Tp>
4299template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004300inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004301typename enable_if
4302<
4303 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004304 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004305 shared_ptr<_Tp>
4306>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4308{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004309 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310 return *this;
4311}
Marshall Clowb22274f2017-01-24 22:22:33 +00004312#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313
4314template<class _Tp>
4315template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004316inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004317typename enable_if
4318<
4319 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004320 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004321 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004322 shared_ptr<_Tp>&
4323>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4325{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004326 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327 return *this;
4328}
4329
Howard Hinnant74279a52010-09-04 23:28:19 +00004330#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331
Marshall Clowb22274f2017-01-24 22:22:33 +00004332#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333template<class _Tp>
4334template<class _Yp>
4335inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004336typename enable_if
4337<
4338 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004339 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004340 shared_ptr<_Tp>&
4341>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004342shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343{
4344 shared_ptr(__r).swap(*this);
4345 return *this;
4346}
Marshall Clowb22274f2017-01-24 22:22:33 +00004347#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348
4349template<class _Tp>
4350template <class _Yp, class _Dp>
4351inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004352typename enable_if
4353<
4354 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004355 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004356 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004357 shared_ptr<_Tp>&
4358>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4360{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004361 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004362 return *this;
4363}
4364
Howard Hinnant74279a52010-09-04 23:28:19 +00004365#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004366
4367template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004368inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369void
Howard Hinnant719bda32011-05-28 14:41:13 +00004370shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004372 _VSTD::swap(__ptr_, __r.__ptr_);
4373 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004374}
4375
4376template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004377inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378void
Howard Hinnant719bda32011-05-28 14:41:13 +00004379shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380{
4381 shared_ptr().swap(*this);
4382}
4383
4384template<class _Tp>
4385template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004386inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004387typename enable_if
4388<
Marshall Clow7e384b72017-01-10 16:59:33 +00004389 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004390 void
4391>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392shared_ptr<_Tp>::reset(_Yp* __p)
4393{
4394 shared_ptr(__p).swap(*this);
4395}
4396
4397template<class _Tp>
4398template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004399inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004400typename enable_if
4401<
Marshall Clow7e384b72017-01-10 16:59:33 +00004402 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004403 void
4404>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004405shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4406{
4407 shared_ptr(__p, __d).swap(*this);
4408}
4409
4410template<class _Tp>
4411template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004412inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004413typename enable_if
4414<
Marshall Clow7e384b72017-01-10 16:59:33 +00004415 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004416 void
4417>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004418shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4419{
4420 shared_ptr(__p, __d, __a).swap(*this);
4421}
4422
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004423template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004425typename enable_if
4426<
4427 !is_array<_Tp>::value,
4428 shared_ptr<_Tp>
4429>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004430make_shared(_Args&& ...__args)
4431{
Zoe Carverd9040c72019-10-22 15:16:49 +00004432 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4433 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4434 typedef allocator<_CntrlBlk> _A2;
4435 typedef __allocator_destructor<_A2> _D2;
4436
4437 _A2 __a2;
4438 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4439 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4440
4441 _Tp *__ptr = __hold2.get()->get();
4442 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443}
4444
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004445template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004447typename enable_if
4448<
4449 !is_array<_Tp>::value,
4450 shared_ptr<_Tp>
4451>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452allocate_shared(const _Alloc& __a, _Args&& ...__args)
4453{
zoecarver505730a2020-02-25 16:50:57 -08004454 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4455
4456 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4457 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4458 typedef __allocator_destructor<_A2> _D2;
4459
4460 _A2 __a2(__a);
4461 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4462 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4463 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4464
4465 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4466 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467}
4468
Howard Hinnantc51e1022010-05-11 19:42:16 +00004469template<class _Tp, class _Up>
4470inline _LIBCPP_INLINE_VISIBILITY
4471bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004472operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473{
4474 return __x.get() == __y.get();
4475}
4476
4477template<class _Tp, class _Up>
4478inline _LIBCPP_INLINE_VISIBILITY
4479bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004480operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481{
4482 return !(__x == __y);
4483}
4484
4485template<class _Tp, class _Up>
4486inline _LIBCPP_INLINE_VISIBILITY
4487bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004488operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004489{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004490#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004491 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4492 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004493#else
4494 return less<>()(__x.get(), __y.get());
4495#endif
4496
Howard Hinnantb17caf92012-02-21 21:02:58 +00004497}
4498
4499template<class _Tp, class _Up>
4500inline _LIBCPP_INLINE_VISIBILITY
4501bool
4502operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4503{
4504 return __y < __x;
4505}
4506
4507template<class _Tp, class _Up>
4508inline _LIBCPP_INLINE_VISIBILITY
4509bool
4510operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4511{
4512 return !(__y < __x);
4513}
4514
4515template<class _Tp, class _Up>
4516inline _LIBCPP_INLINE_VISIBILITY
4517bool
4518operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4519{
4520 return !(__x < __y);
4521}
4522
4523template<class _Tp>
4524inline _LIBCPP_INLINE_VISIBILITY
4525bool
4526operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4527{
4528 return !__x;
4529}
4530
4531template<class _Tp>
4532inline _LIBCPP_INLINE_VISIBILITY
4533bool
4534operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4535{
4536 return !__x;
4537}
4538
4539template<class _Tp>
4540inline _LIBCPP_INLINE_VISIBILITY
4541bool
4542operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4543{
4544 return static_cast<bool>(__x);
4545}
4546
4547template<class _Tp>
4548inline _LIBCPP_INLINE_VISIBILITY
4549bool
4550operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4551{
4552 return static_cast<bool>(__x);
4553}
4554
4555template<class _Tp>
4556inline _LIBCPP_INLINE_VISIBILITY
4557bool
4558operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4559{
4560 return less<_Tp*>()(__x.get(), nullptr);
4561}
4562
4563template<class _Tp>
4564inline _LIBCPP_INLINE_VISIBILITY
4565bool
4566operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4567{
4568 return less<_Tp*>()(nullptr, __x.get());
4569}
4570
4571template<class _Tp>
4572inline _LIBCPP_INLINE_VISIBILITY
4573bool
4574operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4575{
4576 return nullptr < __x;
4577}
4578
4579template<class _Tp>
4580inline _LIBCPP_INLINE_VISIBILITY
4581bool
4582operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4583{
4584 return __x < nullptr;
4585}
4586
4587template<class _Tp>
4588inline _LIBCPP_INLINE_VISIBILITY
4589bool
4590operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4591{
4592 return !(nullptr < __x);
4593}
4594
4595template<class _Tp>
4596inline _LIBCPP_INLINE_VISIBILITY
4597bool
4598operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4599{
4600 return !(__x < nullptr);
4601}
4602
4603template<class _Tp>
4604inline _LIBCPP_INLINE_VISIBILITY
4605bool
4606operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4607{
4608 return !(__x < nullptr);
4609}
4610
4611template<class _Tp>
4612inline _LIBCPP_INLINE_VISIBILITY
4613bool
4614operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4615{
4616 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004617}
4618
4619template<class _Tp>
4620inline _LIBCPP_INLINE_VISIBILITY
4621void
Howard Hinnant719bda32011-05-28 14:41:13 +00004622swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004623{
4624 __x.swap(__y);
4625}
4626
4627template<class _Tp, class _Up>
4628inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004629typename enable_if
4630<
4631 !is_array<_Tp>::value && !is_array<_Up>::value,
4632 shared_ptr<_Tp>
4633>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004634static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635{
4636 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4637}
4638
4639template<class _Tp, class _Up>
4640inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004641typename enable_if
4642<
4643 !is_array<_Tp>::value && !is_array<_Up>::value,
4644 shared_ptr<_Tp>
4645>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004646dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004647{
4648 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4649 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4650}
4651
4652template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004653typename enable_if
4654<
4655 is_array<_Tp>::value == is_array<_Up>::value,
4656 shared_ptr<_Tp>
4657>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004658const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004659{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004660 typedef typename remove_extent<_Tp>::type _RTp;
4661 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004662}
4663
Howard Hinnant72f73582010-08-11 17:04:31 +00004664#ifndef _LIBCPP_NO_RTTI
4665
Howard Hinnantc51e1022010-05-11 19:42:16 +00004666template<class _Dp, class _Tp>
4667inline _LIBCPP_INLINE_VISIBILITY
4668_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004669get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004670{
4671 return __p.template __get_deleter<_Dp>();
4672}
4673
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004674#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004675
Howard Hinnantc51e1022010-05-11 19:42:16 +00004676template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004677class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004678{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004679public:
4680 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004681private:
4682 element_type* __ptr_;
4683 __shared_weak_count* __cntrl_;
4684
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004685public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004687 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004688 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004689 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4690 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004692 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004693 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004694 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4695 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004696
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004699 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004700 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004701 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4702 _NOEXCEPT;
4703#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004704 ~weak_ptr();
4705
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004707 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004708 template<class _Yp>
4709 typename enable_if
4710 <
4711 is_convertible<_Yp*, element_type*>::value,
4712 weak_ptr&
4713 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004715 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4716
4717#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4718
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004720 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4721 template<class _Yp>
4722 typename enable_if
4723 <
4724 is_convertible<_Yp*, element_type*>::value,
4725 weak_ptr&
4726 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004728 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4729
4730#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4731
4732 template<class _Yp>
4733 typename enable_if
4734 <
4735 is_convertible<_Yp*, element_type*>::value,
4736 weak_ptr&
4737 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004739 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004740
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004742 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004744 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004745
Howard Hinnant756c69b2010-09-22 16:48:34 +00004746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004747 long use_count() const _NOEXCEPT
4748 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004750 bool expired() const _NOEXCEPT
4751 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4752 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004753 template<class _Up>
4754 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004755 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004756 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004757 template<class _Up>
4758 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004759 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004760 {return __cntrl_ < __r.__cntrl_;}
4761
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004762 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4763 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004764};
4765
4766template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004767inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004768_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004769weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004770 : __ptr_(0),
4771 __cntrl_(0)
4772{
4773}
4774
4775template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004776inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004777weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004778 : __ptr_(__r.__ptr_),
4779 __cntrl_(__r.__cntrl_)
4780{
4781 if (__cntrl_)
4782 __cntrl_->__add_weak();
4783}
4784
4785template<class _Tp>
4786template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004787inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004788weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004789 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004790 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004791 : __ptr_(__r.__ptr_),
4792 __cntrl_(__r.__cntrl_)
4793{
4794 if (__cntrl_)
4795 __cntrl_->__add_weak();
4796}
4797
4798template<class _Tp>
4799template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004800inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004801weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004802 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004803 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004804 : __ptr_(__r.__ptr_),
4805 __cntrl_(__r.__cntrl_)
4806{
4807 if (__cntrl_)
4808 __cntrl_->__add_weak();
4809}
4810
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004811#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4812
4813template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004814inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004815weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4816 : __ptr_(__r.__ptr_),
4817 __cntrl_(__r.__cntrl_)
4818{
4819 __r.__ptr_ = 0;
4820 __r.__cntrl_ = 0;
4821}
4822
4823template<class _Tp>
4824template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004825inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004826weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4827 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4828 _NOEXCEPT
4829 : __ptr_(__r.__ptr_),
4830 __cntrl_(__r.__cntrl_)
4831{
4832 __r.__ptr_ = 0;
4833 __r.__cntrl_ = 0;
4834}
4835
4836#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4837
Howard Hinnantc51e1022010-05-11 19:42:16 +00004838template<class _Tp>
4839weak_ptr<_Tp>::~weak_ptr()
4840{
4841 if (__cntrl_)
4842 __cntrl_->__release_weak();
4843}
4844
4845template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004846inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004847weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004848weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004849{
4850 weak_ptr(__r).swap(*this);
4851 return *this;
4852}
4853
4854template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004855template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004856inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004857typename enable_if
4858<
4859 is_convertible<_Yp*, _Tp*>::value,
4860 weak_ptr<_Tp>&
4861>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004862weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004863{
4864 weak_ptr(__r).swap(*this);
4865 return *this;
4866}
4867
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004868#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4869
4870template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004871inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004872weak_ptr<_Tp>&
4873weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4874{
4875 weak_ptr(_VSTD::move(__r)).swap(*this);
4876 return *this;
4877}
4878
Howard Hinnantc51e1022010-05-11 19:42:16 +00004879template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004880template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004881inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004882typename enable_if
4883<
4884 is_convertible<_Yp*, _Tp*>::value,
4885 weak_ptr<_Tp>&
4886>::type
4887weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4888{
4889 weak_ptr(_VSTD::move(__r)).swap(*this);
4890 return *this;
4891}
4892
4893#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4894
4895template<class _Tp>
4896template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004897inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004898typename enable_if
4899<
4900 is_convertible<_Yp*, _Tp*>::value,
4901 weak_ptr<_Tp>&
4902>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004903weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004904{
4905 weak_ptr(__r).swap(*this);
4906 return *this;
4907}
4908
4909template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004910inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004911void
Howard Hinnant719bda32011-05-28 14:41:13 +00004912weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004913{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004914 _VSTD::swap(__ptr_, __r.__ptr_);
4915 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004916}
4917
4918template<class _Tp>
4919inline _LIBCPP_INLINE_VISIBILITY
4920void
Howard Hinnant719bda32011-05-28 14:41:13 +00004921swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004922{
4923 __x.swap(__y);
4924}
4925
4926template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004927inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004928void
Howard Hinnant719bda32011-05-28 14:41:13 +00004929weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004930{
4931 weak_ptr().swap(*this);
4932}
4933
4934template<class _Tp>
4935template<class _Yp>
4936shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004937 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004938 : __ptr_(__r.__ptr_),
4939 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4940{
4941 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004942 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004943}
4944
4945template<class _Tp>
4946shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004947weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004948{
4949 shared_ptr<_Tp> __r;
4950 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4951 if (__r.__cntrl_)
4952 __r.__ptr_ = __ptr_;
4953 return __r;
4954}
4955
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004956#if _LIBCPP_STD_VER > 14
4957template <class _Tp = void> struct owner_less;
4958#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004959template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004960#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004961
4962template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004963struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004964 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004965{
4966 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004967 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004968 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004969 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004970 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004971 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004972 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004973 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004974 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004976};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004977
4978template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004979struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004980 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4981{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004982 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004983 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004984 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004986 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004987 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004988 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004989 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004990 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004991 {return __x.owner_before(__y);}
4992};
4993
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004994#if _LIBCPP_STD_VER > 14
4995template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004996struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004997{
4998 template <class _Tp, class _Up>
4999 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005000 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005001 {return __x.owner_before(__y);}
5002 template <class _Tp, class _Up>
5003 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005004 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005005 {return __x.owner_before(__y);}
5006 template <class _Tp, class _Up>
5007 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005008 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005009 {return __x.owner_before(__y);}
5010 template <class _Tp, class _Up>
5011 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005012 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005013 {return __x.owner_before(__y);}
5014 typedef void is_transparent;
5015};
5016#endif
5017
Howard Hinnantc51e1022010-05-11 19:42:16 +00005018template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005019class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005020{
5021 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005022protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005023 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005024 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005026 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005028 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5029 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005031 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005032public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005034 shared_ptr<_Tp> shared_from_this()
5035 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005037 shared_ptr<_Tp const> shared_from_this() const
5038 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005039
Eric Fiselier84006862016-06-02 00:15:35 +00005040#if _LIBCPP_STD_VER > 14
5041 _LIBCPP_INLINE_VISIBILITY
5042 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5043 { return __weak_this_; }
5044
5045 _LIBCPP_INLINE_VISIBILITY
5046 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5047 { return __weak_this_; }
5048#endif // _LIBCPP_STD_VER > 14
5049
Howard Hinnantc51e1022010-05-11 19:42:16 +00005050 template <class _Up> friend class shared_ptr;
5051};
5052
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005053template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005054struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005055{
5056 typedef shared_ptr<_Tp> argument_type;
5057 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005058
Howard Hinnant756c69b2010-09-22 16:48:34 +00005059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005060 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005061 {
5062 return hash<_Tp*>()(__ptr.get());
5063 }
5064};
5065
Howard Hinnantc834c512011-11-29 18:15:50 +00005066template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005067inline _LIBCPP_INLINE_VISIBILITY
5068basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005069operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005070
Eric Fiselier9b492672016-06-18 02:12:53 +00005071
5072#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005073
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005074class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005075{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005076 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005077public:
5078 void lock() _NOEXCEPT;
5079 void unlock() _NOEXCEPT;
5080
5081private:
5082 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5083 __sp_mut(const __sp_mut&);
5084 __sp_mut& operator=(const __sp_mut&);
5085
Howard Hinnant8331b762013-03-06 23:30:19 +00005086 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005087};
5088
Mehdi Amini228053d2017-05-04 17:08:54 +00005089_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5090__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005091
5092template <class _Tp>
5093inline _LIBCPP_INLINE_VISIBILITY
5094bool
5095atomic_is_lock_free(const shared_ptr<_Tp>*)
5096{
5097 return false;
5098}
5099
5100template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005101_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005102shared_ptr<_Tp>
5103atomic_load(const shared_ptr<_Tp>* __p)
5104{
5105 __sp_mut& __m = __get_sp_mut(__p);
5106 __m.lock();
5107 shared_ptr<_Tp> __q = *__p;
5108 __m.unlock();
5109 return __q;
5110}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005111
Howard Hinnant9fa30202012-07-30 01:40:57 +00005112template <class _Tp>
5113inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005114_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005115shared_ptr<_Tp>
5116atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5117{
5118 return atomic_load(__p);
5119}
5120
5121template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005122_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005123void
5124atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5125{
5126 __sp_mut& __m = __get_sp_mut(__p);
5127 __m.lock();
5128 __p->swap(__r);
5129 __m.unlock();
5130}
5131
5132template <class _Tp>
5133inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005134_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005135void
5136atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5137{
5138 atomic_store(__p, __r);
5139}
5140
5141template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005142_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005143shared_ptr<_Tp>
5144atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5145{
5146 __sp_mut& __m = __get_sp_mut(__p);
5147 __m.lock();
5148 __p->swap(__r);
5149 __m.unlock();
5150 return __r;
5151}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005152
Howard Hinnant9fa30202012-07-30 01:40:57 +00005153template <class _Tp>
5154inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005155_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005156shared_ptr<_Tp>
5157atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5158{
5159 return atomic_exchange(__p, __r);
5160}
5161
5162template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005163_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005164bool
5165atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5166{
Marshall Clow4201ee82016-05-18 17:50:13 +00005167 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005168 __sp_mut& __m = __get_sp_mut(__p);
5169 __m.lock();
5170 if (__p->__owner_equivalent(*__v))
5171 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005172 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005173 *__p = __w;
5174 __m.unlock();
5175 return true;
5176 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005177 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005178 *__v = *__p;
5179 __m.unlock();
5180 return false;
5181}
5182
5183template <class _Tp>
5184inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005185_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005186bool
5187atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5188{
5189 return atomic_compare_exchange_strong(__p, __v, __w);
5190}
5191
5192template <class _Tp>
5193inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005194_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005195bool
5196atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5197 shared_ptr<_Tp> __w, memory_order, memory_order)
5198{
5199 return atomic_compare_exchange_strong(__p, __v, __w);
5200}
5201
5202template <class _Tp>
5203inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005204_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005205bool
5206atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5207 shared_ptr<_Tp> __w, memory_order, memory_order)
5208{
5209 return atomic_compare_exchange_weak(__p, __v, __w);
5210}
5211
Eric Fiselier9b492672016-06-18 02:12:53 +00005212#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005213
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005214//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005215#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5216# ifndef _LIBCPP_CXX03_LANG
5217enum class pointer_safety : unsigned char {
5218 relaxed,
5219 preferred,
5220 strict
5221};
5222# endif
5223#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005224struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005225{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005226 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227 {
5228 relaxed,
5229 preferred,
5230 strict
5231 };
5232
Howard Hinnant49e145e2012-10-30 19:06:59 +00005233 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005234
Howard Hinnant756c69b2010-09-22 16:48:34 +00005235 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005236 pointer_safety() : __v_() {}
5237
5238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005239 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005241 operator int() const {return __v_;}
5242};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005243#endif
5244
5245#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005246 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005247_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5248#else
5249// This function is only offered in C++03 under ABI v1.
5250# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5251inline _LIBCPP_INLINE_VISIBILITY
5252pointer_safety get_pointer_safety() _NOEXCEPT {
5253 return pointer_safety::relaxed;
5254}
5255# endif
5256#endif
5257
Howard Hinnantc51e1022010-05-11 19:42:16 +00005258
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005259_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5260_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5261_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005262_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005263
5264template <class _Tp>
5265inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005266_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005267undeclare_reachable(_Tp* __p)
5268{
5269 return static_cast<_Tp*>(__undeclare_reachable(__p));
5270}
5271
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005272_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005273
Marshall Clow8982dcd2015-07-13 20:04:56 +00005274// --- Helper for container swap --
5275template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005276inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005277void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5278#if _LIBCPP_STD_VER >= 14
5279 _NOEXCEPT
5280#else
5281 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5282#endif
5283{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005284 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005285 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5286}
5287
5288template <typename _Alloc>
5289_LIBCPP_INLINE_VISIBILITY
5290void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5291#if _LIBCPP_STD_VER >= 14
5292 _NOEXCEPT
5293#else
5294 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5295#endif
5296{
5297 using _VSTD::swap;
5298 swap(__a1, __a2);
5299}
5300
5301template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005302inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005303void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5304
Marshall Clowff91de82015-08-18 19:51:37 +00005305template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005306struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005307 _Traits::propagate_on_container_move_assignment::value
5308#if _LIBCPP_STD_VER > 14
5309 || _Traits::is_always_equal::value
5310#else
5311 && is_nothrow_move_assignable<_Alloc>::value
5312#endif
5313 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005314
Marshall Clowa591b9a2016-07-11 21:38:08 +00005315
5316#ifndef _LIBCPP_HAS_NO_VARIADICS
5317template <class _Tp, class _Alloc>
5318struct __temp_value {
5319 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005320
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005321 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005322 _Alloc &__a;
5323
5324 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5325 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005326
Marshall Clowa591b9a2016-07-11 21:38:08 +00005327 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005328 _LIBCPP_NO_CFI
5329 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5330 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5331 _VSTD::forward<_Args>(__args)...);
5332 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005333
Marshall Clowa591b9a2016-07-11 21:38:08 +00005334 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5335 };
5336#endif
5337
Marshall Clowe46031a2018-07-02 18:41:15 +00005338template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005339struct __is_allocator : false_type {};
5340
5341template<typename _Alloc>
5342struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005343 typename __void_t<typename _Alloc::value_type>::type,
5344 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5345 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005346 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005347
Eric Fiselier74ebee62019-06-08 01:31:19 +00005348// __builtin_new_allocator -- A non-templated helper for allocating and
5349// deallocating memory using __builtin_operator_new and
5350// __builtin_operator_delete. It should be used in preference to
5351// `std::allocator<T>` to avoid additional instantiations.
5352struct __builtin_new_allocator {
5353 struct __builtin_new_deleter {
5354 typedef void* pointer_type;
5355
5356 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5357 : __size_(__size), __align_(__align) {}
5358
5359 void operator()(void* p) const _NOEXCEPT {
5360 std::__libcpp_deallocate(p, __size_, __align_);
5361 }
5362
5363 private:
5364 size_t __size_;
5365 size_t __align_;
5366 };
5367
5368 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5369
5370 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5371 return __holder_t(std::__libcpp_allocate(__s, __align),
5372 __builtin_new_deleter(__s, __align));
5373 }
5374
5375 static void __deallocate_bytes(void* __p, size_t __s,
5376 size_t __align) _NOEXCEPT {
5377 std::__libcpp_deallocate(__p, __s, __align);
5378 }
5379
5380 template <class _Tp>
5381 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5382 static __holder_t __allocate_type(size_t __n) {
5383 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5384 }
5385
5386 template <class _Tp>
5387 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5388 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5389 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5390 }
5391};
5392
5393
Howard Hinnantc51e1022010-05-11 19:42:16 +00005394_LIBCPP_END_NAMESPACE_STD
5395
Eric Fiselierf4433a32017-05-31 22:07:49 +00005396_LIBCPP_POP_MACROS
5397
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005398#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005399# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005400#endif
5401
Howard Hinnantc51e1022010-05-11 19:42:16 +00005402#endif // _LIBCPP_MEMORY