blob: 45e032ae96fdd70fc40b959b2149234ed702f545 [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
83 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
84 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 <>
104class allocator<void>
105{
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:
118 typedef size_t size_type;
119 typedef ptrdiff_t difference_type;
120 typedef T* pointer;
121 typedef const T* const_pointer;
122 typedef typename add_lvalue_reference<T>::type reference;
123 typedef typename add_lvalue_reference<const T>::type const_reference;
124 typedef T value_type;
125
126 template <class U> struct rebind {typedef allocator<U> other;};
127
Marshall Clowbc759762018-03-20 23:02:53 +0000128 constexpr allocator() noexcept; // constexpr in C++20
129 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
130 template <class U>
131 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000132 ~allocator();
133 pointer address(reference x) const noexcept;
134 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000136 void deallocate(pointer p, size_type n) noexcept;
137 size_type max_size() const noexcept;
138 template<class U, class... Args>
139 void construct(U* p, Args&&... args);
140 template <class U>
141 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142};
143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000148bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149
150template <class OutputIterator, class T>
151class raw_storage_iterator
152 : public iterator<output_iterator_tag,
153 T, // purposefully not C++03
154 ptrdiff_t, // purposefully not C++03
155 T*, // purposefully not C++03
156 raw_storage_iterator&> // purposefully not C++03
157{
158public:
159 explicit raw_storage_iterator(OutputIterator x);
160 raw_storage_iterator& operator*();
161 raw_storage_iterator& operator=(const T& element);
162 raw_storage_iterator& operator++();
163 raw_storage_iterator operator++(int);
164};
165
Howard Hinnant719bda32011-05-28 14:41:13 +0000166template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
167template <class T> void return_temporary_buffer(T* p) noexcept;
168
169template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000170template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
172template <class InputIterator, class ForwardIterator>
173ForwardIterator
174uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
175
Howard Hinnant719bda32011-05-28 14:41:13 +0000176template <class InputIterator, class Size, class ForwardIterator>
177ForwardIterator
178uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
179
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180template <class ForwardIterator, class T>
181void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
182
183template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000184ForwardIterator
185uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000187template <class T>
188void destroy_at(T* location);
189
190template <class ForwardIterator>
191 void destroy(ForwardIterator first, ForwardIterator last);
192
193template <class ForwardIterator, class Size>
194 ForwardIterator destroy_n(ForwardIterator first, Size n);
195
196template <class InputIterator, class ForwardIterator>
197 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
198
199template <class InputIterator, class Size, class ForwardIterator>
200 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
201
202template <class ForwardIterator>
203 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
204
205template <class ForwardIterator, class Size>
206 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
207
208template <class ForwardIterator>
209 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
210
211template <class ForwardIterator, class Size>
212 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
213
Louis Dionne481a2662018-09-23 18:35:00 +0000214template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
216template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000217class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218{
219public:
220 typedef X element_type;
221
222 explicit auto_ptr(X* p =0) throw();
223 auto_ptr(auto_ptr&) throw();
224 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
225 auto_ptr& operator=(auto_ptr&) throw();
226 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
227 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
228 ~auto_ptr() throw();
229
230 typename add_lvalue_reference<X>::type operator*() const throw();
231 X* operator->() const throw();
232 X* get() const throw();
233 X* release() throw();
234 void reset(X* p =0) throw();
235
236 auto_ptr(auto_ptr_ref<X>) throw();
237 template<class Y> operator auto_ptr_ref<Y>() throw();
238 template<class Y> operator auto_ptr<Y>() throw();
239};
240
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000241template <class T>
242struct default_delete
243{
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 constexpr default_delete() noexcept = default;
245 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000246
Howard Hinnant719bda32011-05-28 14:41:13 +0000247 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248};
249
250template <class T>
251struct default_delete<T[]>
252{
Howard Hinnant719bda32011-05-28 14:41:13 +0000253 constexpr default_delete() noexcept = default;
254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255 template <class U> void operator()(U*) const = delete;
256};
257
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000258template <class T, class D = default_delete<T>>
259class unique_ptr
260{
261public:
262 typedef see below pointer;
263 typedef T element_type;
264 typedef D deleter_type;
265
266 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000267 constexpr unique_ptr() noexcept;
268 explicit unique_ptr(pointer p) noexcept;
269 unique_ptr(pointer p, see below d1) noexcept;
270 unique_ptr(pointer p, see below d2) noexcept;
271 unique_ptr(unique_ptr&& u) noexcept;
272 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000273 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000275 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000276 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000277
278 // destructor
279 ~unique_ptr();
280
281 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr& operator=(unique_ptr&& u) noexcept;
283 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
284 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // observers
287 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000288 pointer operator->() const noexcept;
289 pointer get() const noexcept;
290 deleter_type& get_deleter() noexcept;
291 const deleter_type& get_deleter() const noexcept;
292 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer release() noexcept;
296 void reset(pointer p = pointer()) noexcept;
297 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000298};
299
300template <class T, class D>
301class unique_ptr<T[], D>
302{
303public:
304 typedef implementation-defined pointer;
305 typedef T element_type;
306 typedef D deleter_type;
307
308 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000309 constexpr unique_ptr() noexcept;
310 explicit unique_ptr(pointer p) noexcept;
311 unique_ptr(pointer p, see below d) noexcept;
312 unique_ptr(pointer p, see below d) noexcept;
313 unique_ptr(unique_ptr&& u) noexcept;
314 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000317 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000318
319 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000320 unique_ptr& operator=(unique_ptr&& u) noexcept;
321 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // observers
324 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000325 pointer get() const noexcept;
326 deleter_type& get_deleter() noexcept;
327 const deleter_type& get_deleter() const noexcept;
328 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000331 pointer release() noexcept;
332 void reset(pointer p = pointer()) noexcept;
333 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000334 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000335 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336};
337
338template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000340
341template <class T1, class D1, class T2, class D2>
342 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
343template <class T1, class D1, class T2, class D2>
344 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
345template <class T1, class D1, class T2, class D2>
346 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
347template <class T1, class D1, class T2, class D2>
348 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
349template <class T1, class D1, class T2, class D2>
350 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353
Howard Hinnant719bda32011-05-28 14:41:13 +0000354template <class T, class D>
355 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
356template <class T, class D>
357 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
358template <class T, class D>
359 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
360template <class T, class D>
361 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
362
363template <class T, class D>
364 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
365template <class T, class D>
366 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
367template <class T, class D>
368 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
369template <class T, class D>
370 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
371template <class T, class D>
372 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
379
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000380class bad_weak_ptr
381 : public std::exception
382{
Howard Hinnant719bda32011-05-28 14:41:13 +0000383 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000384};
385
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000386template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
387template<class T> unique_ptr<T> make_unique(size_t n); // C++14
388template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
389
Marshall Clowe52a3242017-11-27 15:51:36 +0000390template<class E, class T, class Y, class D>
391 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
392
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393template<class T>
394class shared_ptr
395{
396public:
397 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000398 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000399
400 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000402 template<class Y> explicit shared_ptr(Y* p);
403 template<class Y, class D> shared_ptr(Y* p, D d);
404 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
405 template <class D> shared_ptr(nullptr_t p, D d);
406 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000407 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
408 shared_ptr(const shared_ptr& r) noexcept;
409 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
410 shared_ptr(shared_ptr&& r) noexcept;
411 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000412 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000413 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000414 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
415 shared_ptr(nullptr_t) : shared_ptr() { }
416
417 // destructor:
418 ~shared_ptr();
419
420 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000421 shared_ptr& operator=(const shared_ptr& r) noexcept;
422 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
423 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000424 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000425 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000426 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
427
428 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 void swap(shared_ptr& r) noexcept;
430 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000431 template<class Y> void reset(Y* p);
432 template<class Y, class D> void reset(Y* p, D d);
433 template<class Y, class D, class A> void reset(Y* p, D d, A a);
434
Howard Hinnant719bda32011-05-28 14:41:13 +0000435 // observers:
436 T* get() const noexcept;
437 T& operator*() const noexcept;
438 T* operator->() const noexcept;
439 long use_count() const noexcept;
440 bool unique() const noexcept;
441 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000442 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
443 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000444};
445
446// shared_ptr comparisons:
447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000453template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000454 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000455template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000456 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000457template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000458 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
459
460template <class T>
461 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
462template <class T>
463 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
464template <class T>
465 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
466template <class T>
467 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
468template <class T>
469 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
470template <class T>
471bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
472template <class T>
473 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
474template <class T>
475 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
476template <class T>
477 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
478template <class T>
479 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
480template <class T>
481 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
482template <class T>
483 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000484
485// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000486template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487
488// shared_ptr casts:
489template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000490 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000491template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000492 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000493template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000494 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000495
496// shared_ptr I/O:
497template<class E, class T, class Y>
498 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
499
500// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000501template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000502
503template<class T, class... Args>
504 shared_ptr<T> make_shared(Args&&... args);
505template<class T, class A, class... Args>
506 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
507
508template<class T>
509class weak_ptr
510{
511public:
512 typedef T element_type;
513
514 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000515 constexpr weak_ptr() noexcept;
516 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
517 weak_ptr(weak_ptr const& r) noexcept;
518 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000519 weak_ptr(weak_ptr&& r) noexcept; // C++14
520 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000521
522 // destructor
523 ~weak_ptr();
524
525 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000526 weak_ptr& operator=(weak_ptr const& r) noexcept;
527 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
528 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000529 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
530 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000531
532 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000533 void swap(weak_ptr& r) noexcept;
534 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000535
536 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000537 long use_count() const noexcept;
538 bool expired() const noexcept;
539 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000540 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
541 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000542};
543
544// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000545template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000546
547// class owner_less:
548template<class T> struct owner_less;
549
550template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000551struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000552 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
553{
554 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000555 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
556 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
557 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000558};
559
560template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000561struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000562 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
563{
564 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000565 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
566 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
567 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
568};
569
570template <> // Added in C++14
571struct owner_less<void>
572{
573 template <class _Tp, class _Up>
574 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
575 template <class _Tp, class _Up>
576 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
577 template <class _Tp, class _Up>
578 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
579 template <class _Tp, class _Up>
580 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
581
582 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000583};
584
585template<class T>
586class enable_shared_from_this
587{
588protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000589 constexpr enable_shared_from_this() noexcept;
590 enable_shared_from_this(enable_shared_from_this const&) noexcept;
591 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000592 ~enable_shared_from_this();
593public:
594 shared_ptr<T> shared_from_this();
595 shared_ptr<T const> shared_from_this() const;
596};
597
598template<class T>
599 bool atomic_is_lock_free(const shared_ptr<T>* p);
600template<class T>
601 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
602template<class T>
603 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
604template<class T>
605 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
606template<class T>
607 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
608template<class T>
609 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
610template<class T>
611 shared_ptr<T>
612 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
613template<class T>
614 bool
615 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
616template<class T>
617 bool
618 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
619template<class T>
620 bool
621 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
622 shared_ptr<T> w, memory_order success,
623 memory_order failure);
624template<class T>
625 bool
626 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
627 shared_ptr<T> w, memory_order success,
628 memory_order failure);
629// Hash support
630template <class T> struct hash;
631template <class T, class D> struct hash<unique_ptr<T, D> >;
632template <class T> struct hash<shared_ptr<T> >;
633
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000634template <class T, class Alloc>
635 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
636
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000637// Pointer safety
638enum class pointer_safety { relaxed, preferred, strict };
639void declare_reachable(void *p);
640template <class T> T *undeclare_reachable(T *p);
641void declare_no_pointers(char *p, size_t n);
642void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000643pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000644
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
646
647} // std
648
649*/
650
651#include <__config>
652#include <type_traits>
653#include <typeinfo>
654#include <cstddef>
655#include <cstdint>
656#include <new>
657#include <utility>
658#include <limits>
659#include <iterator>
660#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000661#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000662#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000663#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000664#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000665#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000666# include <atomic>
667#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000668#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000669
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000670#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000672#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673
Eric Fiselierf4433a32017-05-31 22:07:49 +0000674_LIBCPP_PUSH_MACROS
675#include <__undef_macros>
676
677
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678_LIBCPP_BEGIN_NAMESPACE_STD
679
Eric Fiselier89659d12015-07-07 00:27:16 +0000680template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000681inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000682_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
683#if !defined(_LIBCPP_HAS_NO_THREADS) && \
684 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000685 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000686 return __atomic_load_n(__value, __ATOMIC_RELAXED);
687#else
688 return *__value;
689#endif
690}
691
Kuba Breckade9d6792016-09-04 09:55:12 +0000692template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000693inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000694_ValueType __libcpp_acquire_load(_ValueType const* __value) {
695#if !defined(_LIBCPP_HAS_NO_THREADS) && \
696 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000697 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000698 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
699#else
700 return *__value;
701#endif
702}
703
Marshall Clow78dbe462016-11-14 18:22:19 +0000704// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000705
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706template <class _Tp> class allocator;
707
708template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000709class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710{
711public:
712 typedef void* pointer;
713 typedef const void* const_pointer;
714 typedef void value_type;
715
716 template <class _Up> struct rebind {typedef allocator<_Up> other;};
717};
718
Howard Hinnant9f771052012-01-19 23:15:22 +0000719template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000720class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000721{
722public:
723 typedef const void* pointer;
724 typedef const void* const_pointer;
725 typedef const void value_type;
726
727 template <class _Up> struct rebind {typedef allocator<_Up> other;};
728};
729
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730// pointer_traits
731
Marshall Clow0be70c32017-06-14 21:23:57 +0000732template <class _Tp, class = void>
733struct __has_element_type : false_type {};
734
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000736struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000737 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738
739template <class _Ptr, bool = __has_element_type<_Ptr>::value>
740struct __pointer_traits_element_type;
741
742template <class _Ptr>
743struct __pointer_traits_element_type<_Ptr, true>
744{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000745 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746};
747
748#ifndef _LIBCPP_HAS_NO_VARIADICS
749
750template <template <class, class...> class _Sp, class _Tp, class ..._Args>
751struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
752{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000753 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754};
755
756template <template <class, class...> class _Sp, class _Tp, class ..._Args>
757struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
758{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000759 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760};
761
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000762#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763
764template <template <class> class _Sp, class _Tp>
765struct __pointer_traits_element_type<_Sp<_Tp>, true>
766{
767 typedef typename _Sp<_Tp>::element_type type;
768};
769
770template <template <class> class _Sp, class _Tp>
771struct __pointer_traits_element_type<_Sp<_Tp>, false>
772{
773 typedef _Tp type;
774};
775
776template <template <class, class> class _Sp, class _Tp, class _A0>
777struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
778{
779 typedef typename _Sp<_Tp, _A0>::element_type type;
780};
781
782template <template <class, class> class _Sp, class _Tp, class _A0>
783struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
784{
785 typedef _Tp type;
786};
787
788template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
789struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
790{
791 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
792};
793
794template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
795struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
796{
797 typedef _Tp type;
798};
799
800template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
801 class _A1, class _A2>
802struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
803{
804 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
805};
806
807template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
808 class _A1, class _A2>
809struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
810{
811 typedef _Tp type;
812};
813
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000814#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815
Marshall Clow0be70c32017-06-14 21:23:57 +0000816template <class _Tp, class = void>
817struct __has_difference_type : false_type {};
818
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000820struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000821 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822
823template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
824struct __pointer_traits_difference_type
825{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000826 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827};
828
829template <class _Ptr>
830struct __pointer_traits_difference_type<_Ptr, true>
831{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000832 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833};
834
835template <class _Tp, class _Up>
836struct __has_rebind
837{
838private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000839 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000840 template <class _Xp> static __two __test(...);
841 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
842public:
843 static const bool value = sizeof(__test<_Tp>(0)) == 1;
844};
845
846template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
847struct __pointer_traits_rebind
848{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000849#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000850 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000852 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853#endif
854};
855
856#ifndef _LIBCPP_HAS_NO_VARIADICS
857
858template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
859struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
860{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000861#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000862 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000864 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865#endif
866};
867
868template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
869struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
870{
871 typedef _Sp<_Up, _Args...> type;
872};
873
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000874#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875
876template <template <class> class _Sp, class _Tp, class _Up>
877struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
878{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000879#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880 typedef typename _Sp<_Tp>::template rebind<_Up> type;
881#else
882 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
883#endif
884};
885
886template <template <class> class _Sp, class _Tp, class _Up>
887struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
888{
889 typedef _Sp<_Up> type;
890};
891
892template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
893struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
894{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000895#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
897#else
898 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
899#endif
900};
901
902template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
903struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
904{
905 typedef _Sp<_Up, _A0> type;
906};
907
908template <template <class, class, class> class _Sp, class _Tp, class _A0,
909 class _A1, class _Up>
910struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
911{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000912#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
914#else
915 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
916#endif
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, false>
922{
923 typedef _Sp<_Up, _A0, _A1> type;
924};
925
926template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
927 class _A1, class _A2, class _Up>
928struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
929{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000930#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
932#else
933 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
934#endif
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, false>
940{
941 typedef _Sp<_Up, _A0, _A1, _A2> type;
942};
943
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000944#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945
946template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000947struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948{
949 typedef _Ptr pointer;
950 typedef typename __pointer_traits_element_type<pointer>::type element_type;
951 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
952
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000953#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000954 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955#else
956 template <class _Up> struct rebind
957 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000958#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959
960private:
961 struct __nat {};
962public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 static pointer pointer_to(typename conditional<is_void<element_type>::value,
965 __nat, element_type>::type& __r)
966 {return pointer::pointer_to(__r);}
967};
968
969template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000970struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971{
972 typedef _Tp* pointer;
973 typedef _Tp element_type;
974 typedef ptrdiff_t difference_type;
975
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000976#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 template <class _Up> using rebind = _Up*;
978#else
979 template <class _Up> struct rebind {typedef _Up* other;};
980#endif
981
982private:
983 struct __nat {};
984public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000985 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000987 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000988 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989};
990
Eric Fiselierae3ab842015-08-23 02:56:05 +0000991template <class _From, class _To>
992struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000993#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000994 typedef typename pointer_traits<_From>::template rebind<_To> type;
995#else
996 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
997#endif
998};
999
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000// allocator_traits
1001
Marshall Clow0be70c32017-06-14 21:23:57 +00001002template <class _Tp, class = void>
1003struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004
1005template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001006struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001007 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
1009namespace __pointer_type_imp
1010{
1011
1012template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1013struct __pointer_type
1014{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001015 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016};
1017
1018template <class _Tp, class _Dp>
1019struct __pointer_type<_Tp, _Dp, false>
1020{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001021 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022};
1023
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001024} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025
1026template <class _Tp, class _Dp>
1027struct __pointer_type
1028{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001029 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 +00001030};
1031
Marshall Clow0be70c32017-06-14 21:23:57 +00001032template <class _Tp, class = void>
1033struct __has_const_pointer : false_type {};
1034
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001036struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001037 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038
1039template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1040struct __const_pointer
1041{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001042 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043};
1044
1045template <class _Tp, class _Ptr, class _Alloc>
1046struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1047{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001048#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001049 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050#else
1051 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1052#endif
1053};
1054
Marshall Clow0be70c32017-06-14 21:23:57 +00001055template <class _Tp, class = void>
1056struct __has_void_pointer : false_type {};
1057
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001059struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001060 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061
1062template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1063struct __void_pointer
1064{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001065 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066};
1067
1068template <class _Ptr, class _Alloc>
1069struct __void_pointer<_Ptr, _Alloc, false>
1070{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001071#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001072 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001074 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075#endif
1076};
1077
Marshall Clow0be70c32017-06-14 21:23:57 +00001078template <class _Tp, class = void>
1079struct __has_const_void_pointer : false_type {};
1080
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001082struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001083 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084
1085template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1086struct __const_void_pointer
1087{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001088 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089};
1090
1091template <class _Ptr, class _Alloc>
1092struct __const_void_pointer<_Ptr, _Alloc, false>
1093{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001094#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001095 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001097 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098#endif
1099};
1100
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001101
1102template <bool _UsePointerTraits> struct __to_address_helper;
1103
1104template <> struct __to_address_helper<true> {
1105 template <class _Pointer>
1106 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
1107
1108 template <class _Pointer>
1109 _LIBCPP_CONSTEXPR
1110 static __return_type<_Pointer>
1111 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1112};
1113
1114template <class _Pointer, bool _Dummy = true>
1115using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1116
1117
Howard Hinnantc834c512011-11-29 18:15:50 +00001118template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001119inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001120_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001121__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001123 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 return __p;
1125}
1126
1127template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001128inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1129typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1130__to_address(const _Pointer& __p) _NOEXCEPT {
1131 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001132}
1133
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001134template <> struct __to_address_helper<false> {
1135 template <class _Pointer>
1136 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001137
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001138 template <class _Pointer>
1139 _LIBCPP_CONSTEXPR
1140 static __return_type<_Pointer>
1141 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1142};
1143
1144
1145#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001146template <class _Tp>
1147inline _LIBCPP_INLINE_VISIBILITY constexpr
1148_Tp*
1149to_address(_Tp* __p) _NOEXCEPT
1150{
1151 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1152 return __p;
1153}
1154
1155template <class _Pointer>
1156inline _LIBCPP_INLINE_VISIBILITY
1157auto
1158to_address(const _Pointer& __p) _NOEXCEPT
1159{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001160 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001161}
1162#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163
Marshall Clow0be70c32017-06-14 21:23:57 +00001164template <class _Tp, class = void>
1165struct __has_size_type : false_type {};
1166
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001168struct __has_size_type<_Tp,
1169 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001171template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172struct __size_type
1173{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001174 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175};
1176
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001177template <class _Alloc, class _DiffType>
1178struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001180 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181};
1182
Marshall Clow0be70c32017-06-14 21:23:57 +00001183template <class _Tp, class = void>
1184struct __has_propagate_on_container_copy_assignment : false_type {};
1185
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001187struct __has_propagate_on_container_copy_assignment<_Tp,
1188 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1189 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190
1191template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1192struct __propagate_on_container_copy_assignment
1193{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001194 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195};
1196
1197template <class _Alloc>
1198struct __propagate_on_container_copy_assignment<_Alloc, true>
1199{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001200 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201};
1202
Marshall Clow0be70c32017-06-14 21:23:57 +00001203template <class _Tp, class = void>
1204struct __has_propagate_on_container_move_assignment : false_type {};
1205
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001207struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001208 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1209 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210
1211template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1212struct __propagate_on_container_move_assignment
1213{
1214 typedef false_type type;
1215};
1216
1217template <class _Alloc>
1218struct __propagate_on_container_move_assignment<_Alloc, true>
1219{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001220 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221};
1222
Marshall Clow0be70c32017-06-14 21:23:57 +00001223template <class _Tp, class = void>
1224struct __has_propagate_on_container_swap : false_type {};
1225
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001227struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001228 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1229 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230
1231template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1232struct __propagate_on_container_swap
1233{
1234 typedef false_type type;
1235};
1236
1237template <class _Alloc>
1238struct __propagate_on_container_swap<_Alloc, true>
1239{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001240 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241};
1242
Marshall Clow0be70c32017-06-14 21:23:57 +00001243template <class _Tp, class = void>
1244struct __has_is_always_equal : false_type {};
1245
Marshall Clow0b587562015-06-02 16:34:03 +00001246template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001247struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001248 typename __void_t<typename _Tp::is_always_equal>::type>
1249 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001250
1251template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1252struct __is_always_equal
1253{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001254 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001255};
1256
1257template <class _Alloc>
1258struct __is_always_equal<_Alloc, true>
1259{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001260 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001261};
1262
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1264struct __has_rebind_other
1265{
1266private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001267 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268 template <class _Xp> static __two __test(...);
1269 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1270public:
1271 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1272};
1273
1274template <class _Tp, class _Up>
1275struct __has_rebind_other<_Tp, _Up, false>
1276{
1277 static const bool value = false;
1278};
1279
1280template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1281struct __allocator_traits_rebind
1282{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001283 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284};
1285
1286#ifndef _LIBCPP_HAS_NO_VARIADICS
1287
1288template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1289struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1290{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001291 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292};
1293
1294template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1295struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1296{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001297 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298};
1299
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001300#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301
1302template <template <class> class _Alloc, class _Tp, class _Up>
1303struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1304{
1305 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1306};
1307
1308template <template <class> class _Alloc, class _Tp, class _Up>
1309struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1310{
1311 typedef _Alloc<_Up> type;
1312};
1313
Howard Hinnantc51e1022010-05-11 19:42:16 +00001314template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1315struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1316{
1317 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1318};
1319
1320template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1321struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1322{
1323 typedef _Alloc<_Up, _A0> type;
1324};
1325
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1327 class _A1, class _Up>
1328struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1329{
1330 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1331};
1332
1333template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1334 class _A1, class _Up>
1335struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1336{
1337 typedef _Alloc<_Up, _A0, _A1> type;
1338};
1339
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1341 class _A1, class _A2, class _Up>
1342struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1343{
1344 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1345};
1346
1347template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1348 class _A1, class _A2, class _Up>
1349struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1350{
1351 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1352};
1353
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001354#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001356#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357
1358template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1359auto
1360__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001361 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362
1363template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1364auto
1365__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1366 -> false_type;
1367
1368template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1369struct __has_allocate_hint
1370 : integral_constant<bool,
1371 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001372 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373 declval<_SizeType>(),
1374 declval<_ConstVoidPtr>())),
1375 true_type>::value>
1376{
1377};
1378
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001379#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
1381template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1382struct __has_allocate_hint
1383 : true_type
1384{
1385};
1386
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001387#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001389#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390
1391template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001392decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1393 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 true_type())
1395__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1396
1397template <class _Alloc, class _Pointer, class ..._Args>
1398false_type
1399__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1400
1401template <class _Alloc, class _Pointer, class ..._Args>
1402struct __has_construct
1403 : integral_constant<bool,
1404 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001405 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 declval<_Pointer>(),
1407 declval<_Args>()...)),
1408 true_type>::value>
1409{
1410};
1411
1412template <class _Alloc, class _Pointer>
1413auto
1414__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1415 -> decltype(__a.destroy(__p), true_type());
1416
1417template <class _Alloc, class _Pointer>
1418auto
1419__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1420 -> false_type;
1421
1422template <class _Alloc, class _Pointer>
1423struct __has_destroy
1424 : integral_constant<bool,
1425 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001426 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427 declval<_Pointer>())),
1428 true_type>::value>
1429{
1430};
1431
1432template <class _Alloc>
1433auto
1434__has_max_size_test(_Alloc&& __a)
1435 -> decltype(__a.max_size(), true_type());
1436
1437template <class _Alloc>
1438auto
1439__has_max_size_test(const volatile _Alloc& __a)
1440 -> false_type;
1441
1442template <class _Alloc>
1443struct __has_max_size
1444 : integral_constant<bool,
1445 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001446 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 true_type>::value>
1448{
1449};
1450
1451template <class _Alloc>
1452auto
1453__has_select_on_container_copy_construction_test(_Alloc&& __a)
1454 -> decltype(__a.select_on_container_copy_construction(), true_type());
1455
1456template <class _Alloc>
1457auto
1458__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1459 -> false_type;
1460
1461template <class _Alloc>
1462struct __has_select_on_container_copy_construction
1463 : integral_constant<bool,
1464 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001465 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466 true_type>::value>
1467{
1468};
1469
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001470#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001472template <class _Alloc, class _Pointer, class _Tp, class = void>
1473struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001475template <class _Alloc, class _Pointer, class _Tp>
1476struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1477 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1478>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001480template <class _Alloc, class _Pointer, class = void>
1481struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482
1483template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001484struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1485 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1486>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487
1488template <class _Alloc>
1489struct __has_max_size
1490 : true_type
1491{
1492};
1493
1494template <class _Alloc>
1495struct __has_select_on_container_copy_construction
1496 : false_type
1497{
1498};
1499
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001500#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001502template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1503struct __alloc_traits_difference_type
1504{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001505 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001506};
1507
1508template <class _Alloc, class _Ptr>
1509struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1510{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001511 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001512};
1513
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001514template <class _Tp>
1515struct __is_default_allocator : false_type {};
1516
1517template <class _Tp>
1518struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1519
Eric Fiselier909fe962019-09-13 16:09:33 +00001520
1521
1522template <class _Alloc,
1523 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1524 >
1525struct __is_cpp17_move_insertable;
1526template <class _Alloc>
1527struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1528template <class _Alloc>
1529struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1530
1531template <class _Alloc,
1532 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1533 >
1534struct __is_cpp17_copy_insertable;
1535template <class _Alloc>
1536struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1537template <class _Alloc>
1538struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1539 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1540 __is_cpp17_move_insertable<_Alloc>::value>
1541 {};
1542
1543
1544
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001546struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547{
1548 typedef _Alloc allocator_type;
1549 typedef typename allocator_type::value_type value_type;
1550
1551 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1552 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1553 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1554 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1555
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001556 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1557 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558
1559 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1560 propagate_on_container_copy_assignment;
1561 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1562 propagate_on_container_move_assignment;
1563 typedef typename __propagate_on_container_swap<allocator_type>::type
1564 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001565 typedef typename __is_always_equal<allocator_type>::type
1566 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001568#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001570 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001571 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001572#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 template <class _Tp> struct rebind_alloc
1574 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1575 template <class _Tp> struct rebind_traits
1576 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001577#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578
Marshall Clow0e58cae2017-11-26 02:55:38 +00001579 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 static pointer allocate(allocator_type& __a, size_type __n)
1581 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001582 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001584 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1586
Howard Hinnant756c69b2010-09-22 16:48:34 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001588 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 {__a.deallocate(__p, __n);}
1590
1591#ifndef _LIBCPP_HAS_NO_VARIADICS
1592 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001595 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001596 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001597#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001599 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001600 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 {
1602 ::new ((void*)__p) _Tp();
1603 }
1604 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001605 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001606 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001608 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1609 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 }
1611 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001612 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001613 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 const _A1& __a1)
1615 {
1616 ::new ((void*)__p) _Tp(__a0, __a1);
1617 }
1618 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001619 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001620 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621 const _A1& __a1, const _A2& __a2)
1622 {
1623 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1624 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001625#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626
1627 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 static void destroy(allocator_type& __a, _Tp* __p)
1630 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1631
Howard Hinnant756c69b2010-09-22 16:48:34 +00001632 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001633 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1635
Howard Hinnant756c69b2010-09-22 16:48:34 +00001636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637 static allocator_type
1638 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001639 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640 __has_select_on_container_copy_construction<const allocator_type>(),
1641 __a);}
1642
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001643 template <class _Ptr>
1644 _LIBCPP_INLINE_VISIBILITY
1645 static
1646 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001647 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001648 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001649 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1650 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001651 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001652 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001653#ifdef _LIBCPP_NO_EXCEPTIONS
1654 _VSTD::move(*__begin1)
1655#else
1656 _VSTD::move_if_noexcept(*__begin1)
1657#endif
1658 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001659 }
1660
1661 template <class _Tp>
1662 _LIBCPP_INLINE_VISIBILITY
1663 static
1664 typename enable_if
1665 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001666 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001667 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1668 is_trivially_move_constructible<_Tp>::value,
1669 void
1670 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001671 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001672 {
1673 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001674 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001675 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001676 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001677 __begin2 += _Np;
1678 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001679 }
1680
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001681 template <class _Iter, class _Ptr>
1682 _LIBCPP_INLINE_VISIBILITY
1683 static
1684 void
1685 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1686 {
1687 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001688 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001689 }
1690
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001691 template <class _SourceTp, class _DestTp,
1692 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1693 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001694 _LIBCPP_INLINE_VISIBILITY
1695 static
1696 typename enable_if
1697 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001698 is_trivially_move_constructible<_DestTp>::value &&
1699 is_same<_RawSourceTp, _RawDestTp>::value &&
1700 (__is_default_allocator<allocator_type>::value ||
1701 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001702 void
1703 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001704 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001705 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001706 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001707 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001708 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001709 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001710 __begin2 += _Np;
1711 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001712 }
1713
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001714 template <class _Ptr>
1715 _LIBCPP_INLINE_VISIBILITY
1716 static
1717 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001718 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001719 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001720 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1721 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001722 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001723 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001724 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001725#ifdef _LIBCPP_NO_EXCEPTIONS
1726 _VSTD::move(*--__end1)
1727#else
1728 _VSTD::move_if_noexcept(*--__end1)
1729#endif
1730 );
1731 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001732 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001733 }
1734
1735 template <class _Tp>
1736 _LIBCPP_INLINE_VISIBILITY
1737 static
1738 typename enable_if
1739 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001740 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001741 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1742 is_trivially_move_constructible<_Tp>::value,
1743 void
1744 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001745 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001746 {
1747 ptrdiff_t _Np = __end1 - __begin1;
1748 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001749 if (_Np > 0)
1750 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001751 }
1752
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753private:
1754
Howard Hinnant756c69b2010-09-22 16:48:34 +00001755 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001756 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 const_void_pointer __hint, true_type)
1758 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001759 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001760 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001761 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762 {return __a.allocate(__n);}
1763
1764#ifndef _LIBCPP_HAS_NO_VARIADICS
1765 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001768 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1772 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001773 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001775#else // _LIBCPP_HAS_NO_VARIADICS
1776 template <class _Tp, class _A0>
1777 _LIBCPP_INLINE_VISIBILITY
1778 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1779 const _A0& __a0)
1780 {__a.construct(__p, __a0);}
1781 template <class _Tp, class _A0>
1782 _LIBCPP_INLINE_VISIBILITY
1783 static void __construct(false_type, allocator_type&, _Tp* __p,
1784 const _A0& __a0)
1785 {
1786 ::new ((void*)__p) _Tp(__a0);
1787 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001788#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789
1790 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1793 {__a.destroy(__p);}
1794 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 static void __destroy(false_type, allocator_type&, _Tp* __p)
1797 {
1798 __p->~_Tp();
1799 }
1800
Howard Hinnant756c69b2010-09-22 16:48:34 +00001801 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001802 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001804 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001805 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001806 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807
Howard Hinnant756c69b2010-09-22 16:48:34 +00001808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001810 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001814 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 {return __a;}
1816};
1817
Marshall Clow940e01c2015-04-07 05:21:38 +00001818template <class _Traits, class _Tp>
1819struct __rebind_alloc_helper
1820{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001821#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001822 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001823#else
1824 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1825#endif
1826};
1827
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828// allocator
1829
1830template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001831class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832{
1833public:
1834 typedef size_t size_type;
1835 typedef ptrdiff_t difference_type;
1836 typedef _Tp* pointer;
1837 typedef const _Tp* const_pointer;
1838 typedef _Tp& reference;
1839 typedef const _Tp& const_reference;
1840 typedef _Tp value_type;
1841
Howard Hinnant4931e092011-06-02 21:38:57 +00001842 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001843 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001844
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1846
Marshall Clowbc759762018-03-20 23:02:53 +00001847 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1848 allocator() _NOEXCEPT {}
1849
Louis Dionne481a2662018-09-23 18:35:00 +00001850 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001851 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1852 allocator(const allocator<_Up>&) _NOEXCEPT {}
1853
Howard Hinnant719bda32011-05-28 14:41:13 +00001854 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001855 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001856 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001857 {return _VSTD::addressof(__x);}
Louis Dionne481a2662018-09-23 18:35:00 +00001858 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0e58cae2017-11-26 02:55:38 +00001859 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001860 {
1861 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001862 __throw_length_error("allocator<T>::allocate(size_t n)"
1863 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001864 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001865 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001866 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001867 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001868 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1869 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001870#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 template <class _Up, class... _Args>
1872 _LIBCPP_INLINE_VISIBILITY
1873 void
1874 construct(_Up* __p, _Args&&... __args)
1875 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001876 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001878#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879 _LIBCPP_INLINE_VISIBILITY
1880 void
1881 construct(pointer __p)
1882 {
1883 ::new((void*)__p) _Tp();
1884 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001885# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001886
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 template <class _A0>
1888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001889 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890 construct(pointer __p, _A0& __a0)
1891 {
1892 ::new((void*)__p) _Tp(__a0);
1893 }
1894 template <class _A0>
1895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001896 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897 construct(pointer __p, const _A0& __a0)
1898 {
1899 ::new((void*)__p) _Tp(__a0);
1900 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001901# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902 template <class _A0, class _A1>
1903 _LIBCPP_INLINE_VISIBILITY
1904 void
1905 construct(pointer __p, _A0& __a0, _A1& __a1)
1906 {
1907 ::new((void*)__p) _Tp(__a0, __a1);
1908 }
1909 template <class _A0, class _A1>
1910 _LIBCPP_INLINE_VISIBILITY
1911 void
1912 construct(pointer __p, const _A0& __a0, _A1& __a1)
1913 {
1914 ::new((void*)__p) _Tp(__a0, __a1);
1915 }
1916 template <class _A0, class _A1>
1917 _LIBCPP_INLINE_VISIBILITY
1918 void
1919 construct(pointer __p, _A0& __a0, const _A1& __a1)
1920 {
1921 ::new((void*)__p) _Tp(__a0, __a1);
1922 }
1923 template <class _A0, class _A1>
1924 _LIBCPP_INLINE_VISIBILITY
1925 void
1926 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1927 {
1928 ::new((void*)__p) _Tp(__a0, __a1);
1929 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001930#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1932};
1933
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001934template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001935class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001936{
1937public:
1938 typedef size_t size_type;
1939 typedef ptrdiff_t difference_type;
1940 typedef const _Tp* pointer;
1941 typedef const _Tp* const_pointer;
1942 typedef const _Tp& reference;
1943 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001944 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001945
1946 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001947 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001948
1949 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1950
Marshall Clowbc759762018-03-20 23:02:53 +00001951 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1952 allocator() _NOEXCEPT {}
1953
1954 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001955 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001956 allocator(const allocator<_Up>&) _NOEXCEPT {}
1957
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001958 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1959 {return _VSTD::addressof(__x);}
1960 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001961 {
1962 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001963 __throw_length_error("allocator<const T>::allocate(size_t n)"
1964 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001965 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001966 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001967 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001968 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001969 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1970 {return size_type(~0) / sizeof(_Tp);}
1971#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1972 template <class _Up, class... _Args>
1973 _LIBCPP_INLINE_VISIBILITY
1974 void
1975 construct(_Up* __p, _Args&&... __args)
1976 {
1977 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1978 }
1979#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1980 _LIBCPP_INLINE_VISIBILITY
1981 void
1982 construct(pointer __p)
1983 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001984 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001985 }
1986# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001987
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001988 template <class _A0>
1989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001990 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001991 construct(pointer __p, _A0& __a0)
1992 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001993 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001994 }
1995 template <class _A0>
1996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001997 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001998 construct(pointer __p, const _A0& __a0)
1999 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002000 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002001 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002002# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
2003 template <class _A0, class _A1>
2004 _LIBCPP_INLINE_VISIBILITY
2005 void
2006 construct(pointer __p, _A0& __a0, _A1& __a1)
2007 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002008 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002009 }
2010 template <class _A0, class _A1>
2011 _LIBCPP_INLINE_VISIBILITY
2012 void
2013 construct(pointer __p, const _A0& __a0, _A1& __a1)
2014 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002015 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002016 }
2017 template <class _A0, class _A1>
2018 _LIBCPP_INLINE_VISIBILITY
2019 void
2020 construct(pointer __p, _A0& __a0, const _A1& __a1)
2021 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002022 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002023 }
2024 template <class _A0, class _A1>
2025 _LIBCPP_INLINE_VISIBILITY
2026 void
2027 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2028 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002029 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002030 }
2031#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2032 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2033};
2034
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035template <class _Tp, class _Up>
2036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002037bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038
2039template <class _Tp, class _Up>
2040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002041bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042
2043template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002044class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045 : public iterator<output_iterator_tag,
2046 _Tp, // purposefully not C++03
2047 ptrdiff_t, // purposefully not C++03
2048 _Tp*, // purposefully not C++03
2049 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2050{
2051private:
2052 _OutputIterator __x_;
2053public:
2054 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2055 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2056 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002057 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002058#if _LIBCPP_STD_VER >= 14
2059 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002060 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002061#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2063 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2064 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002065#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002066 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002067#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068};
2069
2070template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002071_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002073get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074{
2075 pair<_Tp*, ptrdiff_t> __r(0, 0);
2076 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2077 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2078 / sizeof(_Tp);
2079 if (__n > __m)
2080 __n = __m;
2081 while (__n > 0)
2082 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002083#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002084 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002085 {
2086 std::align_val_t __al =
2087 std::align_val_t(std::alignment_of<_Tp>::value);
2088 __r.first = static_cast<_Tp*>(::operator new(
2089 __n * sizeof(_Tp), __al, nothrow));
2090 } else {
2091 __r.first = static_cast<_Tp*>(::operator new(
2092 __n * sizeof(_Tp), nothrow));
2093 }
2094#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002095 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002096 {
2097 // Since aligned operator new is unavailable, return an empty
2098 // buffer rather than one with invalid alignment.
2099 return __r;
2100 }
2101
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002103#endif
2104
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 if (__r.first)
2106 {
2107 __r.second = __n;
2108 break;
2109 }
2110 __n /= 2;
2111 }
2112 return __r;
2113}
2114
2115template <class _Tp>
2116inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002117void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2118{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002119 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002120}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121
Marshall Clowb22274f2017-01-24 22:22:33 +00002122#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002124struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125{
2126 _Tp* __ptr_;
2127};
2128
2129template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002130class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131{
2132private:
2133 _Tp* __ptr_;
2134public:
2135 typedef _Tp element_type;
2136
2137 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2138 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2139 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2140 : __ptr_(__p.release()) {}
2141 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2142 {reset(__p.release()); return *this;}
2143 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2144 {reset(__p.release()); return *this;}
2145 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2146 {reset(__p.__ptr_); return *this;}
2147 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2148
2149 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2150 {return *__ptr_;}
2151 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2152 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2153 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2154 {
2155 _Tp* __t = __ptr_;
2156 __ptr_ = 0;
2157 return __t;
2158 }
2159 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2160 {
2161 if (__ptr_ != __p)
2162 delete __ptr_;
2163 __ptr_ = __p;
2164 }
2165
2166 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2167 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2168 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2169 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2170 {return auto_ptr<_Up>(release());}
2171};
2172
2173template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002174class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175{
2176public:
2177 typedef void element_type;
2178};
Marshall Clowb22274f2017-01-24 22:22:33 +00002179#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002181// Tag used to default initialize one or both of the pair's elements.
2182struct __default_init_tag {};
2183
Eric Fiselier9d355982017-04-12 23:45:53 +00002184template <class _Tp, int _Idx,
2185 bool _CanBeEmptyBase =
2186 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2187struct __compressed_pair_elem {
2188 typedef _Tp _ParamT;
2189 typedef _Tp& reference;
2190 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191
Eric Fiselier9d355982017-04-12 23:45:53 +00002192#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002193 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002194 _LIBCPP_INLINE_VISIBILITY constexpr
2195 __compressed_pair_elem(__default_init_tag) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196
Eric Fiselier9d355982017-04-12 23:45:53 +00002197 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002198 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2199 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002200 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002201 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002202 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002203 : __value_(_VSTD::forward<_Up>(__u))
2204 {
2205 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206
Eric Fiselier9d355982017-04-12 23:45:53 +00002207 template <class... _Args, size_t... _Indexes>
2208 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2209 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2210 __tuple_indices<_Indexes...>)
2211 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2212#else
Alex Lorenz76132112017-11-09 17:54:49 +00002213 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2214 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002215 __compressed_pair_elem(__default_init_tag) {}
2216 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002217 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2218#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219
Alex Lorenz76132112017-11-09 17:54:49 +00002220 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2221 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002222 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002225 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226};
2227
Eric Fiselier9d355982017-04-12 23:45:53 +00002228template <class _Tp, int _Idx>
2229struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2230 typedef _Tp _ParamT;
2231 typedef _Tp& reference;
2232 typedef const _Tp& const_reference;
2233 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234
Eric Fiselier9d355982017-04-12 23:45:53 +00002235#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002236 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237
Eric Fiselier9d355982017-04-12 23:45:53 +00002238 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002239 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2240 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002241 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002242 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002243 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002244 : __value_type(_VSTD::forward<_Up>(__u))
2245 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246
Eric Fiselier9d355982017-04-12 23:45:53 +00002247 template <class... _Args, size_t... _Indexes>
2248 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2249 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2250 __tuple_indices<_Indexes...>)
2251 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2252#else
Alex Lorenz76132112017-11-09 17:54:49 +00002253 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2254 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002255 __compressed_pair_elem(_ParamT __p)
2256 : __value_type(std::forward<_ParamT>(__p)) {}
2257#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258
Alex Lorenz76132112017-11-09 17:54:49 +00002259 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2260 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002261 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262};
2263
Eric Fiselier9d355982017-04-12 23:45:53 +00002264// Tag used to construct the second element of the compressed pair.
2265struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266
2267template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002268class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2269 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002270 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2271 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002272
2273 // NOTE: This static assert should never fire because __compressed_pair
2274 // is *almost never* used in a scenario where it's possible for T1 == T2.
2275 // (The exception is std::function where it is possible that the function
2276 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002277 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002278 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2279 "The current implementation is NOT ABI-compatible with the previous "
2280 "implementation for this configuration");
2281
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002283#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002284 template <bool _Dummy = true,
2285 class = typename enable_if<
2286 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2287 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2288 >::type
2289 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002290 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002291 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292
Eric Fiselier9d355982017-04-12 23:45:53 +00002293 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2294 __compressed_pair>::value,
2295 bool>::type = true>
2296 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2297 __compressed_pair(_Tp&& __t)
2298 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299
Eric Fiselier9d355982017-04-12 23:45:53 +00002300 template <class _Tp>
2301 _LIBCPP_INLINE_VISIBILITY constexpr
2302 __compressed_pair(__second_tag, _Tp&& __t)
2303 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304
Eric Fiselier9d355982017-04-12 23:45:53 +00002305 template <class _U1, class _U2>
2306 _LIBCPP_INLINE_VISIBILITY constexpr
2307 __compressed_pair(_U1&& __t1, _U2&& __t2)
2308 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309
Eric Fiselier9d355982017-04-12 23:45:53 +00002310 template <class... _Args1, class... _Args2>
2311 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2312 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2313 tuple<_Args2...> __second_args)
2314 : _Base1(__pc, _VSTD::move(__first_args),
2315 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2316 _Base2(__pc, _VSTD::move(__second_args),
2317 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002318
Eric Fiselier9d355982017-04-12 23:45:53 +00002319#else
2320 _LIBCPP_INLINE_VISIBILITY
2321 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002322
Eric Fiselier9d355982017-04-12 23:45:53 +00002323 _LIBCPP_INLINE_VISIBILITY explicit
2324 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002325
Eric Fiselier9d355982017-04-12 23:45:53 +00002326 _LIBCPP_INLINE_VISIBILITY
2327 __compressed_pair(__second_tag, _T2 __t2)
2328 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329
Eric Fiselier9d355982017-04-12 23:45:53 +00002330 _LIBCPP_INLINE_VISIBILITY
2331 __compressed_pair(_T1 __t1, _T2 __t2)
2332 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2333#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334
Eric Fiselier9d355982017-04-12 23:45:53 +00002335 _LIBCPP_INLINE_VISIBILITY
2336 typename _Base1::reference first() _NOEXCEPT {
2337 return static_cast<_Base1&>(*this).__get();
2338 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339
Eric Fiselier9d355982017-04-12 23:45:53 +00002340 _LIBCPP_INLINE_VISIBILITY
2341 typename _Base1::const_reference first() const _NOEXCEPT {
2342 return static_cast<_Base1 const&>(*this).__get();
2343 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344
Eric Fiselier9d355982017-04-12 23:45:53 +00002345 _LIBCPP_INLINE_VISIBILITY
2346 typename _Base2::reference second() _NOEXCEPT {
2347 return static_cast<_Base2&>(*this).__get();
2348 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349
Eric Fiselier9d355982017-04-12 23:45:53 +00002350 _LIBCPP_INLINE_VISIBILITY
2351 typename _Base2::const_reference second() const _NOEXCEPT {
2352 return static_cast<_Base2 const&>(*this).__get();
2353 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354
Eric Fiselier9d355982017-04-12 23:45:53 +00002355 _LIBCPP_INLINE_VISIBILITY
2356 void swap(__compressed_pair& __x)
2357 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2358 __is_nothrow_swappable<_T2>::value)
2359 {
2360 using std::swap;
2361 swap(first(), __x.first());
2362 swap(second(), __x.second());
2363 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364};
2365
2366template <class _T1, class _T2>
2367inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002368void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2369 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2370 __is_nothrow_swappable<_T2>::value) {
2371 __x.swap(__y);
2372}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002374// default_delete
2375
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002377struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002378 static_assert(!is_function<_Tp>::value,
2379 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002380#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002381 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002382#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002383 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002384#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002385 template <class _Up>
2386 _LIBCPP_INLINE_VISIBILITY
2387 default_delete(const default_delete<_Up>&,
2388 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2389 0) _NOEXCEPT {}
2390
2391 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2392 static_assert(sizeof(_Tp) > 0,
2393 "default_delete can not delete incomplete type");
2394 static_assert(!is_void<_Tp>::value,
2395 "default_delete can not delete incomplete type");
2396 delete __ptr;
2397 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398};
2399
2400template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002401struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2402private:
2403 template <class _Up>
2404 struct _EnableIfConvertible
2405 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2406
Howard Hinnant4500ca52011-12-18 21:19:44 +00002407public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002408#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002409 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002410#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002411 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002412#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002413
2414 template <class _Up>
2415 _LIBCPP_INLINE_VISIBILITY
2416 default_delete(const default_delete<_Up[]>&,
2417 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2418
2419 template <class _Up>
2420 _LIBCPP_INLINE_VISIBILITY
2421 typename _EnableIfConvertible<_Up>::type
2422 operator()(_Up* __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 void type");
2427 delete[] __ptr;
2428 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429};
2430
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002431template <class _Deleter>
2432struct __unique_ptr_deleter_sfinae {
2433 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2434 typedef const _Deleter& __lval_ref_type;
2435 typedef _Deleter&& __good_rval_ref_type;
2436 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437};
2438
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002439template <class _Deleter>
2440struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2441 typedef const _Deleter& __lval_ref_type;
2442 typedef const _Deleter&& __bad_rval_ref_type;
2443 typedef false_type __enable_rval_overload;
2444};
2445
2446template <class _Deleter>
2447struct __unique_ptr_deleter_sfinae<_Deleter&> {
2448 typedef _Deleter& __lval_ref_type;
2449 typedef _Deleter&& __bad_rval_ref_type;
2450 typedef false_type __enable_rval_overload;
2451};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002452
2453template <class _Tp, class _Dp = default_delete<_Tp> >
2454class _LIBCPP_TEMPLATE_VIS unique_ptr {
2455public:
2456 typedef _Tp element_type;
2457 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002458 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002459
2460 static_assert(!is_rvalue_reference<deleter_type>::value,
2461 "the specified deleter type cannot be an rvalue reference");
2462
2463private:
2464 __compressed_pair<pointer, deleter_type> __ptr_;
2465
2466 struct __nat { int __for_bool_; };
2467
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002468 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002469
2470 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002471 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002472 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002473
2474 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002475 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002476 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002477
2478 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002479 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002480 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002481
2482 template <bool _Dummy, class _Deleter = typename __dependent_type<
2483 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002484 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002485 typename enable_if<is_default_constructible<_Deleter>::value &&
2486 !is_pointer<_Deleter>::value>::type;
2487
2488 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002489 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002490 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2491
2492 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002493 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002494 is_convertible<typename _UPtr::pointer, pointer>::value &&
2495 !is_array<_Up>::value
2496 >::type;
2497
2498 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002499 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002500 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2501 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2502 >::type;
2503
2504 template <class _UDel>
2505 using _EnableIfDeleterAssignable = typename enable_if<
2506 is_assignable<_Dp&, _UDel&&>::value
2507 >::type;
2508
2509public:
2510 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002511 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002512 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002513 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002514
2515 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002516 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002517 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002518 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002519
2520 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002521 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002522 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002523 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002524
2525 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002526 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002527 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002528 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002529 : __ptr_(__p, __d) {}
2530
2531 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002532 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002533 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002534 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002535 : __ptr_(__p, _VSTD::move(__d)) {
2536 static_assert(!is_reference<deleter_type>::value,
2537 "rvalue deleter bound to reference");
2538 }
2539
2540 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002541 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002542 _LIBCPP_INLINE_VISIBILITY
2543 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2544
2545 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002546 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002547 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2548 }
2549
2550 template <class _Up, class _Ep,
2551 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2552 class = _EnableIfDeleterConvertible<_Ep>
2553 >
2554 _LIBCPP_INLINE_VISIBILITY
2555 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2556 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2557
2558#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2559 template <class _Up>
2560 _LIBCPP_INLINE_VISIBILITY
2561 unique_ptr(auto_ptr<_Up>&& __p,
2562 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002563 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002564 __nat>::type = __nat()) _NOEXCEPT
2565 : __ptr_(__p.release()) {}
2566#endif
2567
2568 _LIBCPP_INLINE_VISIBILITY
2569 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2570 reset(__u.release());
2571 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2572 return *this;
2573 }
2574
2575 template <class _Up, class _Ep,
2576 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2577 class = _EnableIfDeleterAssignable<_Ep>
2578 >
2579 _LIBCPP_INLINE_VISIBILITY
2580 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2581 reset(__u.release());
2582 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2583 return *this;
2584 }
2585
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002586#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2587 template <class _Up>
2588 _LIBCPP_INLINE_VISIBILITY
2589 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2590 is_same<_Dp, default_delete<_Tp> >::value,
2591 unique_ptr&>::type
2592 operator=(auto_ptr<_Up> __p) {
2593 reset(__p.release());
2594 return *this;
2595 }
2596#endif
2597
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002598#ifdef _LIBCPP_CXX03_LANG
2599 unique_ptr(unique_ptr const&) = delete;
2600 unique_ptr& operator=(unique_ptr const&) = delete;
2601#endif
2602
2603
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002604 _LIBCPP_INLINE_VISIBILITY
2605 ~unique_ptr() { reset(); }
2606
2607 _LIBCPP_INLINE_VISIBILITY
2608 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2609 reset();
2610 return *this;
2611 }
2612
2613 _LIBCPP_INLINE_VISIBILITY
2614 typename add_lvalue_reference<_Tp>::type
2615 operator*() const {
2616 return *__ptr_.first();
2617 }
2618 _LIBCPP_INLINE_VISIBILITY
2619 pointer operator->() const _NOEXCEPT {
2620 return __ptr_.first();
2621 }
2622 _LIBCPP_INLINE_VISIBILITY
2623 pointer get() const _NOEXCEPT {
2624 return __ptr_.first();
2625 }
2626 _LIBCPP_INLINE_VISIBILITY
2627 deleter_type& get_deleter() _NOEXCEPT {
2628 return __ptr_.second();
2629 }
2630 _LIBCPP_INLINE_VISIBILITY
2631 const deleter_type& get_deleter() const _NOEXCEPT {
2632 return __ptr_.second();
2633 }
2634 _LIBCPP_INLINE_VISIBILITY
2635 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2636 return __ptr_.first() != nullptr;
2637 }
2638
2639 _LIBCPP_INLINE_VISIBILITY
2640 pointer release() _NOEXCEPT {
2641 pointer __t = __ptr_.first();
2642 __ptr_.first() = pointer();
2643 return __t;
2644 }
2645
2646 _LIBCPP_INLINE_VISIBILITY
2647 void reset(pointer __p = pointer()) _NOEXCEPT {
2648 pointer __tmp = __ptr_.first();
2649 __ptr_.first() = __p;
2650 if (__tmp)
2651 __ptr_.second()(__tmp);
2652 }
2653
2654 _LIBCPP_INLINE_VISIBILITY
2655 void swap(unique_ptr& __u) _NOEXCEPT {
2656 __ptr_.swap(__u.__ptr_);
2657 }
2658};
2659
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002660
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002662class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002664 typedef _Tp element_type;
2665 typedef _Dp deleter_type;
2666 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2667
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002669 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670
Eric Fiselier31127cd2017-04-16 02:14:31 +00002671 template <class _From>
2672 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673
Eric Fiselier31127cd2017-04-16 02:14:31 +00002674 template <class _FromElem>
2675 struct _CheckArrayPointerConversion<_FromElem*>
2676 : integral_constant<bool,
2677 is_same<_FromElem*, pointer>::value ||
2678 (is_same<pointer, element_type*>::value &&
2679 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2680 >
2681 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682
Eric Fiselier31127cd2017-04-16 02:14:31 +00002683 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002684
2685 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002686 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002687 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002688
2689 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002690 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002691 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002692
2693 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002694 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002695 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002696
2697 template <bool _Dummy, class _Deleter = typename __dependent_type<
2698 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002699 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002700 typename enable_if<is_default_constructible<_Deleter>::value &&
2701 !is_pointer<_Deleter>::value>::type;
2702
2703 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002704 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002705 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2706
2707 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002708 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002709 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002710 >::type;
2711
2712 template <class _UPtr, class _Up,
2713 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002714 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715 is_array<_Up>::value &&
2716 is_same<pointer, element_type*>::value &&
2717 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2718 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2719 >::type;
2720
2721 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002722 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002723 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2724 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2725 >::type;
2726
2727 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002728 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002729 is_assignable<_Dp&, _UDel&&>::value
2730 >::type;
2731
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002733 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002734 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002735 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002736 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002738 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002739 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002740 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002741 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002743 template <class _Pp, bool _Dummy = true,
2744 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002745 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002746 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002747 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002748 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002750 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002751 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2752 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002753 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002754 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002755 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002757 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002758 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002759 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002760 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002761 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002763 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002764 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2765 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002766 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002767 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002768 : __ptr_(__p, _VSTD::move(__d)) {
2769 static_assert(!is_reference<deleter_type>::value,
2770 "rvalue deleter bound to reference");
2771 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002773 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002774 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002775 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002776 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002777 : __ptr_(nullptr, _VSTD::move(__d)) {
2778 static_assert(!is_reference<deleter_type>::value,
2779 "rvalue deleter bound to reference");
2780 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002781
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002782 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002783 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2784 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002785 _LIBCPP_INLINE_VISIBILITY
2786 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002787
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002788 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002789 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002790 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2791 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002792
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002793 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002794 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002795 reset(__u.release());
2796 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2797 return *this;
2798 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002800 template <class _Up, class _Ep,
2801 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2802 class = _EnableIfDeleterConvertible<_Ep>
2803 >
2804 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002805 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002806 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002807 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002809 template <class _Up, class _Ep,
2810 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2811 class = _EnableIfDeleterAssignable<_Ep>
2812 >
2813 _LIBCPP_INLINE_VISIBILITY
2814 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002815 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002816 reset(__u.release());
2817 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2818 return *this;
2819 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002821#ifdef _LIBCPP_CXX03_LANG
2822 unique_ptr(unique_ptr const&) = delete;
2823 unique_ptr& operator=(unique_ptr const&) = delete;
2824#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002825
2826public:
2827 _LIBCPP_INLINE_VISIBILITY
2828 ~unique_ptr() { reset(); }
2829
2830 _LIBCPP_INLINE_VISIBILITY
2831 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2832 reset();
2833 return *this;
2834 }
2835
2836 _LIBCPP_INLINE_VISIBILITY
2837 typename add_lvalue_reference<_Tp>::type
2838 operator[](size_t __i) const {
2839 return __ptr_.first()[__i];
2840 }
2841 _LIBCPP_INLINE_VISIBILITY
2842 pointer get() const _NOEXCEPT {
2843 return __ptr_.first();
2844 }
2845
2846 _LIBCPP_INLINE_VISIBILITY
2847 deleter_type& get_deleter() _NOEXCEPT {
2848 return __ptr_.second();
2849 }
2850
2851 _LIBCPP_INLINE_VISIBILITY
2852 const deleter_type& get_deleter() const _NOEXCEPT {
2853 return __ptr_.second();
2854 }
2855 _LIBCPP_INLINE_VISIBILITY
2856 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2857 return __ptr_.first() != nullptr;
2858 }
2859
2860 _LIBCPP_INLINE_VISIBILITY
2861 pointer release() _NOEXCEPT {
2862 pointer __t = __ptr_.first();
2863 __ptr_.first() = pointer();
2864 return __t;
2865 }
2866
2867 template <class _Pp>
2868 _LIBCPP_INLINE_VISIBILITY
2869 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002870 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002871 >::type
2872 reset(_Pp __p) _NOEXCEPT {
2873 pointer __tmp = __ptr_.first();
2874 __ptr_.first() = __p;
2875 if (__tmp)
2876 __ptr_.second()(__tmp);
2877 }
2878
2879 _LIBCPP_INLINE_VISIBILITY
2880 void reset(nullptr_t = nullptr) _NOEXCEPT {
2881 pointer __tmp = __ptr_.first();
2882 __ptr_.first() = nullptr;
2883 if (__tmp)
2884 __ptr_.second()(__tmp);
2885 }
2886
2887 _LIBCPP_INLINE_VISIBILITY
2888 void swap(unique_ptr& __u) _NOEXCEPT {
2889 __ptr_.swap(__u.__ptr_);
2890 }
2891
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892};
2893
2894template <class _Tp, class _Dp>
2895inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002896typename enable_if<
2897 __is_swappable<_Dp>::value,
2898 void
2899>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002900swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901
2902template <class _T1, class _D1, class _T2, class _D2>
2903inline _LIBCPP_INLINE_VISIBILITY
2904bool
2905operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2906
2907template <class _T1, class _D1, class _T2, class _D2>
2908inline _LIBCPP_INLINE_VISIBILITY
2909bool
2910operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2911
2912template <class _T1, class _D1, class _T2, class _D2>
2913inline _LIBCPP_INLINE_VISIBILITY
2914bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002915operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2916{
2917 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2918 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002919 typedef typename common_type<_P1, _P2>::type _Vp;
2920 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002921}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922
2923template <class _T1, class _D1, class _T2, class _D2>
2924inline _LIBCPP_INLINE_VISIBILITY
2925bool
2926operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2927
2928template <class _T1, class _D1, class _T2, class _D2>
2929inline _LIBCPP_INLINE_VISIBILITY
2930bool
2931operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2932
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 < __y);}
2937
Howard Hinnantb17caf92012-02-21 21:02:58 +00002938template <class _T1, class _D1>
2939inline _LIBCPP_INLINE_VISIBILITY
2940bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002941operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002942{
2943 return !__x;
2944}
2945
2946template <class _T1, class _D1>
2947inline _LIBCPP_INLINE_VISIBILITY
2948bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002949operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002950{
2951 return !__x;
2952}
2953
2954template <class _T1, class _D1>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002957operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002958{
2959 return static_cast<bool>(__x);
2960}
2961
2962template <class _T1, class _D1>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002965operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002966{
2967 return static_cast<bool>(__x);
2968}
2969
2970template <class _T1, class _D1>
2971inline _LIBCPP_INLINE_VISIBILITY
2972bool
2973operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2974{
2975 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2976 return less<_P1>()(__x.get(), nullptr);
2977}
2978
2979template <class _T1, class _D1>
2980inline _LIBCPP_INLINE_VISIBILITY
2981bool
2982operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2983{
2984 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2985 return less<_P1>()(nullptr, __x.get());
2986}
2987
2988template <class _T1, class _D1>
2989inline _LIBCPP_INLINE_VISIBILITY
2990bool
2991operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2992{
2993 return nullptr < __x;
2994}
2995
2996template <class _T1, class _D1>
2997inline _LIBCPP_INLINE_VISIBILITY
2998bool
2999operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3000{
3001 return __x < nullptr;
3002}
3003
3004template <class _T1, class _D1>
3005inline _LIBCPP_INLINE_VISIBILITY
3006bool
3007operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3008{
3009 return !(nullptr < __x);
3010}
3011
3012template <class _T1, class _D1>
3013inline _LIBCPP_INLINE_VISIBILITY
3014bool
3015operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3016{
3017 return !(__x < nullptr);
3018}
3019
3020template <class _T1, class _D1>
3021inline _LIBCPP_INLINE_VISIBILITY
3022bool
3023operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3024{
3025 return !(__x < nullptr);
3026}
3027
3028template <class _T1, class _D1>
3029inline _LIBCPP_INLINE_VISIBILITY
3030bool
3031operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3032{
3033 return !(nullptr < __x);
3034}
3035
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003036#if _LIBCPP_STD_VER > 11
3037
3038template<class _Tp>
3039struct __unique_if
3040{
3041 typedef unique_ptr<_Tp> __unique_single;
3042};
3043
3044template<class _Tp>
3045struct __unique_if<_Tp[]>
3046{
3047 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3048};
3049
3050template<class _Tp, size_t _Np>
3051struct __unique_if<_Tp[_Np]>
3052{
3053 typedef void __unique_array_known_bound;
3054};
3055
3056template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003057inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003058typename __unique_if<_Tp>::__unique_single
3059make_unique(_Args&&... __args)
3060{
3061 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3062}
3063
3064template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003065inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003066typename __unique_if<_Tp>::__unique_array_unknown_bound
3067make_unique(size_t __n)
3068{
3069 typedef typename remove_extent<_Tp>::type _Up;
3070 return unique_ptr<_Tp>(new _Up[__n]());
3071}
3072
3073template<class _Tp, class... _Args>
3074 typename __unique_if<_Tp>::__unique_array_known_bound
3075 make_unique(_Args&&...) = delete;
3076
3077#endif // _LIBCPP_STD_VER > 11
3078
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003079template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003080#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003081struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003082#else
3083struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003084 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003085#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003086{
3087 typedef unique_ptr<_Tp, _Dp> argument_type;
3088 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003089 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003090 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003091 {
3092 typedef typename argument_type::pointer pointer;
3093 return hash<pointer>()(__ptr.get());
3094 }
3095};
3096
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097struct __destruct_n
3098{
3099private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003100 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101
3102 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003103 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003104 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105
3106 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003107 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108 {}
3109
Howard Hinnant719bda32011-05-28 14:41:13 +00003110 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003111 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003112 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 {}
3114
Howard Hinnant719bda32011-05-28 14:41:13 +00003115 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003116 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003117 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 {}
3119public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003120 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003121 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122
3123 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003124 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003125 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126
3127 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003128 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003129 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130
3131 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003132 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003133 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134};
3135
3136template <class _Alloc>
3137class __allocator_destructor
3138{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003139 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003141 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3142 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143private:
3144 _Alloc& __alloc_;
3145 size_type __s_;
3146public:
3147 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003148 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003151 void operator()(pointer __p) _NOEXCEPT
3152 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153};
3154
3155template <class _InputIterator, class _ForwardIterator>
3156_ForwardIterator
3157uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3158{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003160#ifndef _LIBCPP_NO_EXCEPTIONS
3161 _ForwardIterator __s = __r;
3162 try
3163 {
3164#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003165 for (; __f != __l; ++__f, (void) ++__r)
3166 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003167#ifndef _LIBCPP_NO_EXCEPTIONS
3168 }
3169 catch (...)
3170 {
3171 for (; __s != __r; ++__s)
3172 __s->~value_type();
3173 throw;
3174 }
3175#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176 return __r;
3177}
3178
3179template <class _InputIterator, class _Size, class _ForwardIterator>
3180_ForwardIterator
3181uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3182{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003184#ifndef _LIBCPP_NO_EXCEPTIONS
3185 _ForwardIterator __s = __r;
3186 try
3187 {
3188#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003189 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3190 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003191#ifndef _LIBCPP_NO_EXCEPTIONS
3192 }
3193 catch (...)
3194 {
3195 for (; __s != __r; ++__s)
3196 __s->~value_type();
3197 throw;
3198 }
3199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 return __r;
3201}
3202
3203template <class _ForwardIterator, class _Tp>
3204void
3205uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3206{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003208#ifndef _LIBCPP_NO_EXCEPTIONS
3209 _ForwardIterator __s = __f;
3210 try
3211 {
3212#endif
3213 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003214 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003215#ifndef _LIBCPP_NO_EXCEPTIONS
3216 }
3217 catch (...)
3218 {
3219 for (; __s != __f; ++__s)
3220 __s->~value_type();
3221 throw;
3222 }
3223#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224}
3225
3226template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003227_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003228uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3229{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003231#ifndef _LIBCPP_NO_EXCEPTIONS
3232 _ForwardIterator __s = __f;
3233 try
3234 {
3235#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003236 for (; __n > 0; ++__f, (void) --__n)
3237 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003238#ifndef _LIBCPP_NO_EXCEPTIONS
3239 }
3240 catch (...)
3241 {
3242 for (; __s != __f; ++__s)
3243 __s->~value_type();
3244 throw;
3245 }
3246#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003247 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248}
3249
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003250#if _LIBCPP_STD_VER > 14
3251
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003252template <class _Tp>
3253inline _LIBCPP_INLINE_VISIBILITY
3254void destroy_at(_Tp* __loc) {
3255 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3256 __loc->~_Tp();
3257}
3258
3259template <class _ForwardIterator>
3260inline _LIBCPP_INLINE_VISIBILITY
3261void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3262 for (; __first != __last; ++__first)
3263 _VSTD::destroy_at(_VSTD::addressof(*__first));
3264}
3265
3266template <class _ForwardIterator, class _Size>
3267inline _LIBCPP_INLINE_VISIBILITY
3268_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3269 for (; __n > 0; (void)++__first, --__n)
3270 _VSTD::destroy_at(_VSTD::addressof(*__first));
3271 return __first;
3272}
3273
Eric Fiselier290c07c2016-10-11 21:13:44 +00003274template <class _ForwardIterator>
3275inline _LIBCPP_INLINE_VISIBILITY
3276void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3277 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3278 auto __idx = __first;
3279#ifndef _LIBCPP_NO_EXCEPTIONS
3280 try {
3281#endif
3282 for (; __idx != __last; ++__idx)
3283 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3284#ifndef _LIBCPP_NO_EXCEPTIONS
3285 } catch (...) {
3286 _VSTD::destroy(__first, __idx);
3287 throw;
3288 }
3289#endif
3290}
3291
3292template <class _ForwardIterator, class _Size>
3293inline _LIBCPP_INLINE_VISIBILITY
3294_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3295 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3296 auto __idx = __first;
3297#ifndef _LIBCPP_NO_EXCEPTIONS
3298 try {
3299#endif
3300 for (; __n > 0; (void)++__idx, --__n)
3301 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3302 return __idx;
3303#ifndef _LIBCPP_NO_EXCEPTIONS
3304 } catch (...) {
3305 _VSTD::destroy(__first, __idx);
3306 throw;
3307 }
3308#endif
3309}
3310
3311
3312template <class _ForwardIterator>
3313inline _LIBCPP_INLINE_VISIBILITY
3314void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3315 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3316 auto __idx = __first;
3317#ifndef _LIBCPP_NO_EXCEPTIONS
3318 try {
3319#endif
3320 for (; __idx != __last; ++__idx)
3321 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3322#ifndef _LIBCPP_NO_EXCEPTIONS
3323 } catch (...) {
3324 _VSTD::destroy(__first, __idx);
3325 throw;
3326 }
3327#endif
3328}
3329
3330template <class _ForwardIterator, class _Size>
3331inline _LIBCPP_INLINE_VISIBILITY
3332_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3333 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3334 auto __idx = __first;
3335#ifndef _LIBCPP_NO_EXCEPTIONS
3336 try {
3337#endif
3338 for (; __n > 0; (void)++__idx, --__n)
3339 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3340 return __idx;
3341#ifndef _LIBCPP_NO_EXCEPTIONS
3342 } catch (...) {
3343 _VSTD::destroy(__first, __idx);
3344 throw;
3345 }
3346#endif
3347}
3348
3349
3350template <class _InputIt, class _ForwardIt>
3351inline _LIBCPP_INLINE_VISIBILITY
3352_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3353 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3354 auto __idx = __first_res;
3355#ifndef _LIBCPP_NO_EXCEPTIONS
3356 try {
3357#endif
3358 for (; __first != __last; (void)++__idx, ++__first)
3359 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3360 return __idx;
3361#ifndef _LIBCPP_NO_EXCEPTIONS
3362 } catch (...) {
3363 _VSTD::destroy(__first_res, __idx);
3364 throw;
3365 }
3366#endif
3367}
3368
3369template <class _InputIt, class _Size, class _ForwardIt>
3370inline _LIBCPP_INLINE_VISIBILITY
3371pair<_InputIt, _ForwardIt>
3372uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3373 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3374 auto __idx = __first_res;
3375#ifndef _LIBCPP_NO_EXCEPTIONS
3376 try {
3377#endif
3378 for (; __n > 0; ++__idx, (void)++__first, --__n)
3379 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3380 return {__first, __idx};
3381#ifndef _LIBCPP_NO_EXCEPTIONS
3382 } catch (...) {
3383 _VSTD::destroy(__first_res, __idx);
3384 throw;
3385 }
3386#endif
3387}
3388
3389
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003390#endif // _LIBCPP_STD_VER > 14
3391
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003392// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3393// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003394// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003395#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3396 && defined(__ATOMIC_RELAXED) \
3397 && defined(__ATOMIC_ACQ_REL)
3398# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003399#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003400# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3401#endif
3402
3403template <class _Tp>
3404inline _LIBCPP_INLINE_VISIBILITY _Tp
3405__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3406{
3407#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3408 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3409#else
3410 return __t += 1;
3411#endif
3412}
3413
3414template <class _Tp>
3415inline _LIBCPP_INLINE_VISIBILITY _Tp
3416__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3417{
3418#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3419 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3420#else
3421 return __t -= 1;
3422#endif
3423}
3424
Howard Hinnant756c69b2010-09-22 16:48:34 +00003425class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426 : public std::exception
3427{
3428public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003429 virtual ~bad_weak_ptr() _NOEXCEPT;
3430 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431};
3432
Louis Dionne16fe2952018-07-11 23:14:33 +00003433_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003434void __throw_bad_weak_ptr()
3435{
3436#ifndef _LIBCPP_NO_EXCEPTIONS
3437 throw bad_weak_ptr();
3438#else
3439 _VSTD::abort();
3440#endif
3441}
3442
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003443template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003445class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446{
3447 __shared_count(const __shared_count&);
3448 __shared_count& operator=(const __shared_count&);
3449
3450protected:
3451 long __shared_owners_;
3452 virtual ~__shared_count();
3453private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003454 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455
3456public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003458 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459 : __shared_owners_(__refs) {}
3460
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003461#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003462 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003463 void __add_shared() _NOEXCEPT;
3464 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003465#else
3466 _LIBCPP_INLINE_VISIBILITY
3467 void __add_shared() _NOEXCEPT {
3468 __libcpp_atomic_refcount_increment(__shared_owners_);
3469 }
3470 _LIBCPP_INLINE_VISIBILITY
3471 bool __release_shared() _NOEXCEPT {
3472 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3473 __on_zero_shared();
3474 return true;
3475 }
3476 return false;
3477 }
3478#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003479 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003480 long use_count() const _NOEXCEPT {
3481 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3482 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483};
3484
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003485class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486 : private __shared_count
3487{
3488 long __shared_weak_owners_;
3489
3490public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003492 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493 : __shared_count(__refs),
3494 __shared_weak_owners_(__refs) {}
3495protected:
3496 virtual ~__shared_weak_count();
3497
3498public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003499#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003500 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003501 void __add_shared() _NOEXCEPT;
3502 void __add_weak() _NOEXCEPT;
3503 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003504#else
3505 _LIBCPP_INLINE_VISIBILITY
3506 void __add_shared() _NOEXCEPT {
3507 __shared_count::__add_shared();
3508 }
3509 _LIBCPP_INLINE_VISIBILITY
3510 void __add_weak() _NOEXCEPT {
3511 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3512 }
3513 _LIBCPP_INLINE_VISIBILITY
3514 void __release_shared() _NOEXCEPT {
3515 if (__shared_count::__release_shared())
3516 __release_weak();
3517 }
3518#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003519 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003521 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3522 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523
Howard Hinnant807d6332013-02-25 15:50:36 +00003524 // Define the function out only if we build static libc++ without RTTI.
3525 // Otherwise we may break clients who need to compile their projects with
3526 // -fno-rtti and yet link against a libc++.dylib compiled
3527 // without -fno-rtti.
3528#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003529 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003530#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003532 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533};
3534
3535template <class _Tp, class _Dp, class _Alloc>
3536class __shared_ptr_pointer
3537 : public __shared_weak_count
3538{
3539 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3540public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003543 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544
Howard Hinnant72f73582010-08-11 17:04:31 +00003545#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003546 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003547#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548
3549private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003550 virtual void __on_zero_shared() _NOEXCEPT;
3551 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552};
3553
Howard Hinnant72f73582010-08-11 17:04:31 +00003554#ifndef _LIBCPP_NO_RTTI
3555
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556template <class _Tp, class _Dp, class _Alloc>
3557const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003558__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003560 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561}
3562
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003563#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003564
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565template <class _Tp, class _Dp, class _Alloc>
3566void
Howard Hinnant719bda32011-05-28 14:41:13 +00003567__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568{
3569 __data_.first().second()(__data_.first().first());
3570 __data_.first().second().~_Dp();
3571}
3572
3573template <class _Tp, class _Dp, class _Alloc>
3574void
Howard Hinnant719bda32011-05-28 14:41:13 +00003575__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003577 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3578 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003579 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3580
Eric Fiselierf8898c82015-02-05 23:01:40 +00003581 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003583 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584}
3585
3586template <class _Tp, class _Alloc>
3587class __shared_ptr_emplace
3588 : public __shared_weak_count
3589{
3590 __compressed_pair<_Alloc, _Tp> __data_;
3591public:
3592#ifndef _LIBCPP_HAS_NO_VARIADICS
3593
Howard Hinnant756c69b2010-09-22 16:48:34 +00003594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003596 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597
3598 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003601 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3602 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603
3604#else // _LIBCPP_HAS_NO_VARIADICS
3605
Howard Hinnant756c69b2010-09-22 16:48:34 +00003606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607 __shared_ptr_emplace(_Alloc __a)
3608 : __data_(__a) {}
3609
3610 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3613 : __data_(__a, _Tp(__a0)) {}
3614
3615 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3618 : __data_(__a, _Tp(__a0, __a1)) {}
3619
3620 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3623 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3624
3625#endif // _LIBCPP_HAS_NO_VARIADICS
3626
3627private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003628 virtual void __on_zero_shared() _NOEXCEPT;
3629 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003631 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003632 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633};
3634
3635template <class _Tp, class _Alloc>
3636void
Howard Hinnant719bda32011-05-28 14:41:13 +00003637__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
3639 __data_.second().~_Tp();
3640}
3641
3642template <class _Tp, class _Alloc>
3643void
Howard Hinnant719bda32011-05-28 14:41:13 +00003644__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003646 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3647 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003648 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003649 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003651 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652}
3653
Erik Pilkington2a398762017-05-25 15:43:31 +00003654struct __shared_ptr_dummy_rebind_allocator_type;
3655template <>
3656class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3657{
3658public:
3659 template <class _Other>
3660 struct rebind
3661 {
3662 typedef allocator<_Other> other;
3663 };
3664};
3665
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003666template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667
3668template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003669class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003671public:
3672 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003673
Eric Fiselierae5b6672016-06-27 01:02:43 +00003674#if _LIBCPP_STD_VER > 14
3675 typedef weak_ptr<_Tp> weak_type;
3676#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677private:
3678 element_type* __ptr_;
3679 __shared_weak_count* __cntrl_;
3680
3681 struct __nat {int __for_bool_;};
3682public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003684 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003686 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003687 template<class _Yp>
3688 explicit shared_ptr(_Yp* __p,
3689 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3690 template<class _Yp, class _Dp>
3691 shared_ptr(_Yp* __p, _Dp __d,
3692 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3693 template<class _Yp, class _Dp, class _Alloc>
3694 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3695 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3697 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003698 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003700 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003704 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003705 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003706#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003708 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003709 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003710 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003711 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003712#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003714 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003715#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003716#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003717 template<class _Yp>
3718 shared_ptr(auto_ptr<_Yp>&& __r,
3719 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003720#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003721 template<class _Yp>
3722 shared_ptr(auto_ptr<_Yp> __r,
3723 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003725#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003726#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003727 template <class _Yp, class _Dp>
3728 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3729 typename enable_if
3730 <
3731 !is_lvalue_reference<_Dp>::value &&
3732 !is_array<_Yp>::value &&
3733 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3734 __nat
3735 >::type = __nat());
3736 template <class _Yp, class _Dp>
3737 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3738 typename enable_if
3739 <
3740 is_lvalue_reference<_Dp>::value &&
3741 !is_array<_Yp>::value &&
3742 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3743 __nat
3744 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003745#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003746 template <class _Yp, class _Dp>
3747 shared_ptr(unique_ptr<_Yp, _Dp>,
3748 typename enable_if
3749 <
3750 !is_lvalue_reference<_Dp>::value &&
3751 !is_array<_Yp>::value &&
3752 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3753 __nat
3754 >::type = __nat());
3755 template <class _Yp, class _Dp>
3756 shared_ptr(unique_ptr<_Yp, _Dp>,
3757 typename enable_if
3758 <
3759 is_lvalue_reference<_Dp>::value &&
3760 !is_array<_Yp>::value &&
3761 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3762 __nat
3763 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003764#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765
3766 ~shared_ptr();
3767
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003769 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003770 template<class _Yp>
3771 typename enable_if
3772 <
3773 is_convertible<_Yp*, element_type*>::value,
3774 shared_ptr&
3775 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003777 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003778#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003780 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003781 template<class _Yp>
3782 typename enable_if
3783 <
3784 is_convertible<_Yp*, element_type*>::value,
3785 shared_ptr<_Tp>&
3786 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003788 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003789#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003790 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003792 typename enable_if
3793 <
3794 !is_array<_Yp>::value &&
3795 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003796 shared_ptr
3797 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003798 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003799#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003800#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003801#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003802 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003804 typename enable_if
3805 <
3806 !is_array<_Yp>::value &&
3807 is_convertible<_Yp*, element_type*>::value,
3808 shared_ptr&
3809 >::type
3810 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003812#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003813 template <class _Yp, class _Dp>
3814 typename enable_if
3815 <
3816 !is_array<_Yp>::value &&
3817 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3818 shared_ptr&
3819 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003820#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003822 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003823#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003825 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826#endif
3827
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003829 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003831 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003832 template<class _Yp>
3833 typename enable_if
3834 <
3835 is_convertible<_Yp*, element_type*>::value,
3836 void
3837 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003839 reset(_Yp* __p);
3840 template<class _Yp, class _Dp>
3841 typename enable_if
3842 <
3843 is_convertible<_Yp*, element_type*>::value,
3844 void
3845 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003847 reset(_Yp* __p, _Dp __d);
3848 template<class _Yp, class _Dp, class _Alloc>
3849 typename enable_if
3850 <
3851 is_convertible<_Yp*, element_type*>::value,
3852 void
3853 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003855 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856
Howard Hinnant756c69b2010-09-22 16:48:34 +00003857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003858 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003860 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3861 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003863 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003865 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003867 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003869 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003870 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003871 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003872 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003874 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003875 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003876 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003878 _LIBCPP_INLINE_VISIBILITY
3879 bool
3880 __owner_equivalent(const shared_ptr& __p) const
3881 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882
Howard Hinnant72f73582010-08-11 17:04:31 +00003883#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003886 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003887 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003888 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003889 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003890#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891
Zoe Carverd9040c72019-10-22 15:16:49 +00003892 template<class _Yp, class _CntrlBlk>
3893 static shared_ptr<_Tp>
3894 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3895 {
3896 shared_ptr<_Tp> __r;
3897 __r.__ptr_ = __p;
3898 __r.__cntrl_ = __cntrl;
3899 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3900 return __r;
3901 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003902
3903 template<class _Alloc, class ..._Args>
3904 static
3905 shared_ptr<_Tp>
3906 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3907
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003909 template <class _Yp, bool = is_function<_Yp>::value>
3910 struct __shared_ptr_default_allocator
3911 {
3912 typedef allocator<_Yp> type;
3913 };
3914
3915 template <class _Yp>
3916 struct __shared_ptr_default_allocator<_Yp, true>
3917 {
3918 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3919 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003921 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003922 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003923 typename enable_if<is_convertible<_OrigPtr*,
3924 const enable_shared_from_this<_Yp>*
3925 >::value,
3926 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003927 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3928 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003930 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003931 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003932 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003933 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3934 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003935 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936 }
3937
Erik Pilkington2a398762017-05-25 15:43:31 +00003938 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003940 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3941 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942};
3943
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003944
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003946inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003947_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003948shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949 : __ptr_(0),
3950 __cntrl_(0)
3951{
3952}
3953
3954template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003955inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003956_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003957shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958 : __ptr_(0),
3959 __cntrl_(0)
3960{
3961}
3962
3963template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003964template<class _Yp>
3965shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3966 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967 : __ptr_(__p)
3968{
3969 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003970 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3971 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3972 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003974 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975}
3976
3977template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003978template<class _Yp, class _Dp>
3979shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3980 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981 : __ptr_(__p)
3982{
3983#ifndef _LIBCPP_NO_EXCEPTIONS
3984 try
3985 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003986#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003987 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3988 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3989 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003990 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991#ifndef _LIBCPP_NO_EXCEPTIONS
3992 }
3993 catch (...)
3994 {
3995 __d(__p);
3996 throw;
3997 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003998#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999}
4000
4001template<class _Tp>
4002template<class _Dp>
4003shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4004 : __ptr_(0)
4005{
4006#ifndef _LIBCPP_NO_EXCEPTIONS
4007 try
4008 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004009#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004010 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4011 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4012 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
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>
Logan Chiend435f8b2014-01-31 09:30:46 +00004024template<class _Yp, class _Dp, class _Alloc>
4025shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4026 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027 : __ptr_(__p)
4028{
4029#ifndef _LIBCPP_NO_EXCEPTIONS
4030 try
4031 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004032#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004034 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035 typedef __allocator_destructor<_A2> _D2;
4036 _A2 __a2(__a);
4037 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004038 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4039 _CntrlBlk(__p, __d, __a);
4040 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004041 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042#ifndef _LIBCPP_NO_EXCEPTIONS
4043 }
4044 catch (...)
4045 {
4046 __d(__p);
4047 throw;
4048 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004049#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050}
4051
4052template<class _Tp>
4053template<class _Dp, class _Alloc>
4054shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4055 : __ptr_(0)
4056{
4057#ifndef _LIBCPP_NO_EXCEPTIONS
4058 try
4059 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004060#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004062 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063 typedef __allocator_destructor<_A2> _D2;
4064 _A2 __a2(__a);
4065 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004066 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4067 _CntrlBlk(__p, __d, __a);
4068 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069#ifndef _LIBCPP_NO_EXCEPTIONS
4070 }
4071 catch (...)
4072 {
4073 __d(__p);
4074 throw;
4075 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004076#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077}
4078
4079template<class _Tp>
4080template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004081inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004082shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083 : __ptr_(__p),
4084 __cntrl_(__r.__cntrl_)
4085{
4086 if (__cntrl_)
4087 __cntrl_->__add_shared();
4088}
4089
4090template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004091inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004092shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093 : __ptr_(__r.__ptr_),
4094 __cntrl_(__r.__cntrl_)
4095{
4096 if (__cntrl_)
4097 __cntrl_->__add_shared();
4098}
4099
4100template<class _Tp>
4101template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004102inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004104 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004105 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106 : __ptr_(__r.__ptr_),
4107 __cntrl_(__r.__cntrl_)
4108{
4109 if (__cntrl_)
4110 __cntrl_->__add_shared();
4111}
4112
Howard Hinnant74279a52010-09-04 23:28:19 +00004113#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114
4115template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004116inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004117shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118 : __ptr_(__r.__ptr_),
4119 __cntrl_(__r.__cntrl_)
4120{
4121 __r.__ptr_ = 0;
4122 __r.__cntrl_ = 0;
4123}
4124
4125template<class _Tp>
4126template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004127inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004129 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004130 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131 : __ptr_(__r.__ptr_),
4132 __cntrl_(__r.__cntrl_)
4133{
4134 __r.__ptr_ = 0;
4135 __r.__cntrl_ = 0;
4136}
4137
Howard Hinnant74279a52010-09-04 23:28:19 +00004138#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139
Marshall Clowb22274f2017-01-24 22:22:33 +00004140#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004142template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004143#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004144shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004146shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004148 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149 : __ptr_(__r.get())
4150{
4151 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4152 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004153 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154 __r.release();
4155}
Marshall Clowb22274f2017-01-24 22:22:33 +00004156#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157
4158template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004159template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004160#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4162#else
4163shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4164#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004165 typename enable_if
4166 <
4167 !is_lvalue_reference<_Dp>::value &&
4168 !is_array<_Yp>::value &&
4169 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4170 __nat
4171 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172 : __ptr_(__r.get())
4173{
Marshall Clow35cde742015-05-10 13:59:45 +00004174#if _LIBCPP_STD_VER > 11
4175 if (__ptr_ == nullptr)
4176 __cntrl_ = nullptr;
4177 else
4178#endif
4179 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004180 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4181 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4182 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004183 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004184 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185 __r.release();
4186}
4187
4188template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004189template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004190#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4192#else
4193shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4194#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004195 typename enable_if
4196 <
4197 is_lvalue_reference<_Dp>::value &&
4198 !is_array<_Yp>::value &&
4199 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4200 __nat
4201 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202 : __ptr_(__r.get())
4203{
Marshall Clow35cde742015-05-10 13:59:45 +00004204#if _LIBCPP_STD_VER > 11
4205 if (__ptr_ == nullptr)
4206 __cntrl_ = nullptr;
4207 else
4208#endif
4209 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004210 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004211 typedef __shared_ptr_pointer<_Yp*,
4212 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004213 _AllocT > _CntrlBlk;
4214 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004215 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004216 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217 __r.release();
4218}
4219
Zoe Carver6cd05c32019-08-19 15:47:16 +00004220template<class _Tp>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004221template<class _Alloc, class ..._Args>
4222shared_ptr<_Tp>
4223shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4224{
4225 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
4226 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4227 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4228 typedef __allocator_destructor<_A2> _D2;
4229 _A2 __a2(__a);
4230 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4231 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4232 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4233 shared_ptr<_Tp> __r;
4234 __r.__ptr_ = __hold2.get()->get();
4235 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4236 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4237 return __r;
4238}
4239
Howard Hinnantc51e1022010-05-11 19:42:16 +00004240template<class _Tp>
4241shared_ptr<_Tp>::~shared_ptr()
4242{
4243 if (__cntrl_)
4244 __cntrl_->__release_shared();
4245}
4246
4247template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004248inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004250shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251{
4252 shared_ptr(__r).swap(*this);
4253 return *this;
4254}
4255
4256template<class _Tp>
4257template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004258inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004259typename enable_if
4260<
Marshall Clow7e384b72017-01-10 16:59:33 +00004261 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004262 shared_ptr<_Tp>&
4263>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004264shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265{
4266 shared_ptr(__r).swap(*this);
4267 return *this;
4268}
4269
Howard Hinnant74279a52010-09-04 23:28:19 +00004270#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271
4272template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004273inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004275shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004277 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 return *this;
4279}
4280
4281template<class _Tp>
4282template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004283inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004284typename enable_if
4285<
Marshall Clow7e384b72017-01-10 16:59:33 +00004286 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004287 shared_ptr<_Tp>&
4288>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4290{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004291 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292 return *this;
4293}
4294
Marshall Clowb22274f2017-01-24 22:22:33 +00004295#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296template<class _Tp>
4297template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004298inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004299typename enable_if
4300<
4301 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004302 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004303 shared_ptr<_Tp>
4304>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4306{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004307 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308 return *this;
4309}
Marshall Clowb22274f2017-01-24 22:22:33 +00004310#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311
4312template<class _Tp>
4313template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004314inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004315typename enable_if
4316<
4317 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004318 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004319 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004320 shared_ptr<_Tp>&
4321>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4323{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004324 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325 return *this;
4326}
4327
Howard Hinnant74279a52010-09-04 23:28:19 +00004328#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329
Marshall Clowb22274f2017-01-24 22:22:33 +00004330#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331template<class _Tp>
4332template<class _Yp>
4333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004334typename enable_if
4335<
4336 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004337 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004338 shared_ptr<_Tp>&
4339>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004340shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341{
4342 shared_ptr(__r).swap(*this);
4343 return *this;
4344}
Marshall Clowb22274f2017-01-24 22:22:33 +00004345#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004346
4347template<class _Tp>
4348template <class _Yp, class _Dp>
4349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004350typename enable_if
4351<
4352 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004353 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004354 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004355 shared_ptr<_Tp>&
4356>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4358{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004359 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360 return *this;
4361}
4362
Howard Hinnant74279a52010-09-04 23:28:19 +00004363#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364
4365template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004366inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367void
Howard Hinnant719bda32011-05-28 14:41:13 +00004368shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004370 _VSTD::swap(__ptr_, __r.__ptr_);
4371 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372}
4373
4374template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004375inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376void
Howard Hinnant719bda32011-05-28 14:41:13 +00004377shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378{
4379 shared_ptr().swap(*this);
4380}
4381
4382template<class _Tp>
4383template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004384inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004385typename enable_if
4386<
Marshall Clow7e384b72017-01-10 16:59:33 +00004387 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004388 void
4389>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390shared_ptr<_Tp>::reset(_Yp* __p)
4391{
4392 shared_ptr(__p).swap(*this);
4393}
4394
4395template<class _Tp>
4396template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004397inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004398typename enable_if
4399<
Marshall Clow7e384b72017-01-10 16:59:33 +00004400 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004401 void
4402>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4404{
4405 shared_ptr(__p, __d).swap(*this);
4406}
4407
4408template<class _Tp>
4409template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004410inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004411typename enable_if
4412<
Marshall Clow7e384b72017-01-10 16:59:33 +00004413 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004414 void
4415>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4417{
4418 shared_ptr(__p, __d, __a).swap(*this);
4419}
4420
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004421template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004423typename enable_if
4424<
4425 !is_array<_Tp>::value,
4426 shared_ptr<_Tp>
4427>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428make_shared(_Args&& ...__args)
4429{
Zoe Carverd9040c72019-10-22 15:16:49 +00004430 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4431 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4432 typedef allocator<_CntrlBlk> _A2;
4433 typedef __allocator_destructor<_A2> _D2;
4434
4435 _A2 __a2;
4436 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4437 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4438
4439 _Tp *__ptr = __hold2.get()->get();
4440 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441}
4442
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004443template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004445typename enable_if
4446<
4447 !is_array<_Tp>::value,
4448 shared_ptr<_Tp>
4449>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450allocate_shared(const _Alloc& __a, _Args&& ...__args)
4451{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004452 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004453}
4454
Howard Hinnantc51e1022010-05-11 19:42:16 +00004455template<class _Tp, class _Up>
4456inline _LIBCPP_INLINE_VISIBILITY
4457bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004458operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459{
4460 return __x.get() == __y.get();
4461}
4462
4463template<class _Tp, class _Up>
4464inline _LIBCPP_INLINE_VISIBILITY
4465bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004466operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467{
4468 return !(__x == __y);
4469}
4470
4471template<class _Tp, class _Up>
4472inline _LIBCPP_INLINE_VISIBILITY
4473bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004474operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004476#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004477 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4478 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004479#else
4480 return less<>()(__x.get(), __y.get());
4481#endif
4482
Howard Hinnantb17caf92012-02-21 21:02:58 +00004483}
4484
4485template<class _Tp, class _Up>
4486inline _LIBCPP_INLINE_VISIBILITY
4487bool
4488operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4489{
4490 return __y < __x;
4491}
4492
4493template<class _Tp, class _Up>
4494inline _LIBCPP_INLINE_VISIBILITY
4495bool
4496operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4497{
4498 return !(__y < __x);
4499}
4500
4501template<class _Tp, class _Up>
4502inline _LIBCPP_INLINE_VISIBILITY
4503bool
4504operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4505{
4506 return !(__x < __y);
4507}
4508
4509template<class _Tp>
4510inline _LIBCPP_INLINE_VISIBILITY
4511bool
4512operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4513{
4514 return !__x;
4515}
4516
4517template<class _Tp>
4518inline _LIBCPP_INLINE_VISIBILITY
4519bool
4520operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4521{
4522 return !__x;
4523}
4524
4525template<class _Tp>
4526inline _LIBCPP_INLINE_VISIBILITY
4527bool
4528operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4529{
4530 return static_cast<bool>(__x);
4531}
4532
4533template<class _Tp>
4534inline _LIBCPP_INLINE_VISIBILITY
4535bool
4536operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4537{
4538 return static_cast<bool>(__x);
4539}
4540
4541template<class _Tp>
4542inline _LIBCPP_INLINE_VISIBILITY
4543bool
4544operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4545{
4546 return less<_Tp*>()(__x.get(), nullptr);
4547}
4548
4549template<class _Tp>
4550inline _LIBCPP_INLINE_VISIBILITY
4551bool
4552operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4553{
4554 return less<_Tp*>()(nullptr, __x.get());
4555}
4556
4557template<class _Tp>
4558inline _LIBCPP_INLINE_VISIBILITY
4559bool
4560operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4561{
4562 return nullptr < __x;
4563}
4564
4565template<class _Tp>
4566inline _LIBCPP_INLINE_VISIBILITY
4567bool
4568operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4569{
4570 return __x < nullptr;
4571}
4572
4573template<class _Tp>
4574inline _LIBCPP_INLINE_VISIBILITY
4575bool
4576operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4577{
4578 return !(nullptr < __x);
4579}
4580
4581template<class _Tp>
4582inline _LIBCPP_INLINE_VISIBILITY
4583bool
4584operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4585{
4586 return !(__x < nullptr);
4587}
4588
4589template<class _Tp>
4590inline _LIBCPP_INLINE_VISIBILITY
4591bool
4592operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4593{
4594 return !(__x < nullptr);
4595}
4596
4597template<class _Tp>
4598inline _LIBCPP_INLINE_VISIBILITY
4599bool
4600operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4601{
4602 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004603}
4604
4605template<class _Tp>
4606inline _LIBCPP_INLINE_VISIBILITY
4607void
Howard Hinnant719bda32011-05-28 14:41:13 +00004608swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004609{
4610 __x.swap(__y);
4611}
4612
4613template<class _Tp, class _Up>
4614inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004615typename enable_if
4616<
4617 !is_array<_Tp>::value && !is_array<_Up>::value,
4618 shared_ptr<_Tp>
4619>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004620static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004621{
4622 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4623}
4624
4625template<class _Tp, class _Up>
4626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004627typename enable_if
4628<
4629 !is_array<_Tp>::value && !is_array<_Up>::value,
4630 shared_ptr<_Tp>
4631>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004632dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004633{
4634 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4635 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4636}
4637
4638template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004639typename enable_if
4640<
4641 is_array<_Tp>::value == is_array<_Up>::value,
4642 shared_ptr<_Tp>
4643>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004644const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004645{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004646 typedef typename remove_extent<_Tp>::type _RTp;
4647 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004648}
4649
Howard Hinnant72f73582010-08-11 17:04:31 +00004650#ifndef _LIBCPP_NO_RTTI
4651
Howard Hinnantc51e1022010-05-11 19:42:16 +00004652template<class _Dp, class _Tp>
4653inline _LIBCPP_INLINE_VISIBILITY
4654_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004655get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004656{
4657 return __p.template __get_deleter<_Dp>();
4658}
4659
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004660#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004661
Howard Hinnantc51e1022010-05-11 19:42:16 +00004662template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004663class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004664{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004665public:
4666 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004667private:
4668 element_type* __ptr_;
4669 __shared_weak_count* __cntrl_;
4670
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004671public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004673 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004674 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004675 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4676 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004678 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004679 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004680 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4681 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004682
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004683#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004685 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004686 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004687 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4688 _NOEXCEPT;
4689#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004690 ~weak_ptr();
4691
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004693 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004694 template<class _Yp>
4695 typename enable_if
4696 <
4697 is_convertible<_Yp*, element_type*>::value,
4698 weak_ptr&
4699 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004701 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4702
4703#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4704
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004706 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4707 template<class _Yp>
4708 typename enable_if
4709 <
4710 is_convertible<_Yp*, element_type*>::value,
4711 weak_ptr&
4712 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004714 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4715
4716#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4717
4718 template<class _Yp>
4719 typename enable_if
4720 <
4721 is_convertible<_Yp*, element_type*>::value,
4722 weak_ptr&
4723 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004725 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004726
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004728 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004730 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004731
Howard Hinnant756c69b2010-09-22 16:48:34 +00004732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004733 long use_count() const _NOEXCEPT
4734 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004736 bool expired() const _NOEXCEPT
4737 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4738 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004739 template<class _Up>
4740 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004741 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004742 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004743 template<class _Up>
4744 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004745 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004746 {return __cntrl_ < __r.__cntrl_;}
4747
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004748 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4749 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004750};
4751
4752template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004753inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004754_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004755weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004756 : __ptr_(0),
4757 __cntrl_(0)
4758{
4759}
4760
4761template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004762inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004763weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004764 : __ptr_(__r.__ptr_),
4765 __cntrl_(__r.__cntrl_)
4766{
4767 if (__cntrl_)
4768 __cntrl_->__add_weak();
4769}
4770
4771template<class _Tp>
4772template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004773inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004774weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004775 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004776 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004777 : __ptr_(__r.__ptr_),
4778 __cntrl_(__r.__cntrl_)
4779{
4780 if (__cntrl_)
4781 __cntrl_->__add_weak();
4782}
4783
4784template<class _Tp>
4785template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004786inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004787weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004788 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004789 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004790 : __ptr_(__r.__ptr_),
4791 __cntrl_(__r.__cntrl_)
4792{
4793 if (__cntrl_)
4794 __cntrl_->__add_weak();
4795}
4796
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004797#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4798
4799template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004800inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004801weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4802 : __ptr_(__r.__ptr_),
4803 __cntrl_(__r.__cntrl_)
4804{
4805 __r.__ptr_ = 0;
4806 __r.__cntrl_ = 0;
4807}
4808
4809template<class _Tp>
4810template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004811inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004812weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4813 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4814 _NOEXCEPT
4815 : __ptr_(__r.__ptr_),
4816 __cntrl_(__r.__cntrl_)
4817{
4818 __r.__ptr_ = 0;
4819 __r.__cntrl_ = 0;
4820}
4821
4822#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4823
Howard Hinnantc51e1022010-05-11 19:42:16 +00004824template<class _Tp>
4825weak_ptr<_Tp>::~weak_ptr()
4826{
4827 if (__cntrl_)
4828 __cntrl_->__release_weak();
4829}
4830
4831template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004832inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004833weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004834weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004835{
4836 weak_ptr(__r).swap(*this);
4837 return *this;
4838}
4839
4840template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004841template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004842inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004843typename enable_if
4844<
4845 is_convertible<_Yp*, _Tp*>::value,
4846 weak_ptr<_Tp>&
4847>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004848weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004849{
4850 weak_ptr(__r).swap(*this);
4851 return *this;
4852}
4853
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004854#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4855
4856template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004857inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004858weak_ptr<_Tp>&
4859weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4860{
4861 weak_ptr(_VSTD::move(__r)).swap(*this);
4862 return *this;
4863}
4864
Howard Hinnantc51e1022010-05-11 19:42:16 +00004865template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004866template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004867inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004868typename enable_if
4869<
4870 is_convertible<_Yp*, _Tp*>::value,
4871 weak_ptr<_Tp>&
4872>::type
4873weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4874{
4875 weak_ptr(_VSTD::move(__r)).swap(*this);
4876 return *this;
4877}
4878
4879#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4880
4881template<class _Tp>
4882template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004883inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004884typename enable_if
4885<
4886 is_convertible<_Yp*, _Tp*>::value,
4887 weak_ptr<_Tp>&
4888>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004889weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004890{
4891 weak_ptr(__r).swap(*this);
4892 return *this;
4893}
4894
4895template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004896inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004897void
Howard Hinnant719bda32011-05-28 14:41:13 +00004898weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004899{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004900 _VSTD::swap(__ptr_, __r.__ptr_);
4901 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004902}
4903
4904template<class _Tp>
4905inline _LIBCPP_INLINE_VISIBILITY
4906void
Howard Hinnant719bda32011-05-28 14:41:13 +00004907swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908{
4909 __x.swap(__y);
4910}
4911
4912template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004913inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004914void
Howard Hinnant719bda32011-05-28 14:41:13 +00004915weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004916{
4917 weak_ptr().swap(*this);
4918}
4919
4920template<class _Tp>
4921template<class _Yp>
4922shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004923 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004924 : __ptr_(__r.__ptr_),
4925 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4926{
4927 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004928 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004929}
4930
4931template<class _Tp>
4932shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004933weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004934{
4935 shared_ptr<_Tp> __r;
4936 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4937 if (__r.__cntrl_)
4938 __r.__ptr_ = __ptr_;
4939 return __r;
4940}
4941
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004942#if _LIBCPP_STD_VER > 14
4943template <class _Tp = void> struct owner_less;
4944#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004945template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004946#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004947
4948template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004949struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004950 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004951{
4952 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004953 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004954 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004955 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004956 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004957 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004958 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004959 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004960 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004961 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004962};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963
4964template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004965struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004966 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4967{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004968 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004969 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004970 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004971 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004972 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004973 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004974 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004975 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004976 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004977 {return __x.owner_before(__y);}
4978};
4979
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004980#if _LIBCPP_STD_VER > 14
4981template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004982struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004983{
4984 template <class _Tp, class _Up>
4985 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004986 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004987 {return __x.owner_before(__y);}
4988 template <class _Tp, class _Up>
4989 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004990 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004991 {return __x.owner_before(__y);}
4992 template <class _Tp, class _Up>
4993 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004994 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004995 {return __x.owner_before(__y);}
4996 template <class _Tp, class _Up>
4997 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004998 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004999 {return __x.owner_before(__y);}
5000 typedef void is_transparent;
5001};
5002#endif
5003
Howard Hinnantc51e1022010-05-11 19:42:16 +00005004template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005005class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005006{
5007 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005008protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005009 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005010 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005012 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005014 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5015 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005017 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005018public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005020 shared_ptr<_Tp> shared_from_this()
5021 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005023 shared_ptr<_Tp const> shared_from_this() const
5024 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005025
Eric Fiselier84006862016-06-02 00:15:35 +00005026#if _LIBCPP_STD_VER > 14
5027 _LIBCPP_INLINE_VISIBILITY
5028 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5029 { return __weak_this_; }
5030
5031 _LIBCPP_INLINE_VISIBILITY
5032 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5033 { return __weak_this_; }
5034#endif // _LIBCPP_STD_VER > 14
5035
Howard Hinnantc51e1022010-05-11 19:42:16 +00005036 template <class _Up> friend class shared_ptr;
5037};
5038
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005039template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005040struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005041{
5042 typedef shared_ptr<_Tp> argument_type;
5043 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005044
Howard Hinnant756c69b2010-09-22 16:48:34 +00005045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005046 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005047 {
5048 return hash<_Tp*>()(__ptr.get());
5049 }
5050};
5051
Howard Hinnantc834c512011-11-29 18:15:50 +00005052template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005053inline _LIBCPP_INLINE_VISIBILITY
5054basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005055operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005056
Eric Fiselier9b492672016-06-18 02:12:53 +00005057
5058#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005059
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005060class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005061{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005062 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005063public:
5064 void lock() _NOEXCEPT;
5065 void unlock() _NOEXCEPT;
5066
5067private:
5068 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5069 __sp_mut(const __sp_mut&);
5070 __sp_mut& operator=(const __sp_mut&);
5071
Howard Hinnant8331b762013-03-06 23:30:19 +00005072 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005073};
5074
Mehdi Amini228053d2017-05-04 17:08:54 +00005075_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5076__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005077
5078template <class _Tp>
5079inline _LIBCPP_INLINE_VISIBILITY
5080bool
5081atomic_is_lock_free(const shared_ptr<_Tp>*)
5082{
5083 return false;
5084}
5085
5086template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005087_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005088shared_ptr<_Tp>
5089atomic_load(const shared_ptr<_Tp>* __p)
5090{
5091 __sp_mut& __m = __get_sp_mut(__p);
5092 __m.lock();
5093 shared_ptr<_Tp> __q = *__p;
5094 __m.unlock();
5095 return __q;
5096}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005097
Howard Hinnant9fa30202012-07-30 01:40:57 +00005098template <class _Tp>
5099inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005100_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005101shared_ptr<_Tp>
5102atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5103{
5104 return atomic_load(__p);
5105}
5106
5107template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005108_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005109void
5110atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5111{
5112 __sp_mut& __m = __get_sp_mut(__p);
5113 __m.lock();
5114 __p->swap(__r);
5115 __m.unlock();
5116}
5117
5118template <class _Tp>
5119inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005120_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005121void
5122atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5123{
5124 atomic_store(__p, __r);
5125}
5126
5127template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005128_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005129shared_ptr<_Tp>
5130atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5131{
5132 __sp_mut& __m = __get_sp_mut(__p);
5133 __m.lock();
5134 __p->swap(__r);
5135 __m.unlock();
5136 return __r;
5137}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005138
Howard Hinnant9fa30202012-07-30 01:40:57 +00005139template <class _Tp>
5140inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005141_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005142shared_ptr<_Tp>
5143atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5144{
5145 return atomic_exchange(__p, __r);
5146}
5147
5148template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005149_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005150bool
5151atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5152{
Marshall Clow4201ee82016-05-18 17:50:13 +00005153 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005154 __sp_mut& __m = __get_sp_mut(__p);
5155 __m.lock();
5156 if (__p->__owner_equivalent(*__v))
5157 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005158 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005159 *__p = __w;
5160 __m.unlock();
5161 return true;
5162 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005163 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005164 *__v = *__p;
5165 __m.unlock();
5166 return false;
5167}
5168
5169template <class _Tp>
5170inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005171_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005172bool
5173atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5174{
5175 return atomic_compare_exchange_strong(__p, __v, __w);
5176}
5177
5178template <class _Tp>
5179inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005180_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005181bool
5182atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5183 shared_ptr<_Tp> __w, memory_order, memory_order)
5184{
5185 return atomic_compare_exchange_strong(__p, __v, __w);
5186}
5187
5188template <class _Tp>
5189inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005190_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005191bool
5192atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5193 shared_ptr<_Tp> __w, memory_order, memory_order)
5194{
5195 return atomic_compare_exchange_weak(__p, __v, __w);
5196}
5197
Eric Fiselier9b492672016-06-18 02:12:53 +00005198#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005199
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005200//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005201#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5202# ifndef _LIBCPP_CXX03_LANG
5203enum class pointer_safety : unsigned char {
5204 relaxed,
5205 preferred,
5206 strict
5207};
5208# endif
5209#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005210struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005211{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005212 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005213 {
5214 relaxed,
5215 preferred,
5216 strict
5217 };
5218
Howard Hinnant49e145e2012-10-30 19:06:59 +00005219 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005220
Howard Hinnant756c69b2010-09-22 16:48:34 +00005221 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005222 pointer_safety() : __v_() {}
5223
5224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005225 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227 operator int() const {return __v_;}
5228};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005229#endif
5230
5231#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005232 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005233_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5234#else
5235// This function is only offered in C++03 under ABI v1.
5236# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5237inline _LIBCPP_INLINE_VISIBILITY
5238pointer_safety get_pointer_safety() _NOEXCEPT {
5239 return pointer_safety::relaxed;
5240}
5241# endif
5242#endif
5243
Howard Hinnantc51e1022010-05-11 19:42:16 +00005244
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005245_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5246_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5247_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005248_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005249
5250template <class _Tp>
5251inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005252_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005253undeclare_reachable(_Tp* __p)
5254{
5255 return static_cast<_Tp*>(__undeclare_reachable(__p));
5256}
5257
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005258_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005259
Marshall Clow8982dcd2015-07-13 20:04:56 +00005260// --- Helper for container swap --
5261template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005262inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005263void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5264#if _LIBCPP_STD_VER >= 14
5265 _NOEXCEPT
5266#else
5267 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5268#endif
5269{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005270 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005271 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5272}
5273
5274template <typename _Alloc>
5275_LIBCPP_INLINE_VISIBILITY
5276void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5277#if _LIBCPP_STD_VER >= 14
5278 _NOEXCEPT
5279#else
5280 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5281#endif
5282{
5283 using _VSTD::swap;
5284 swap(__a1, __a2);
5285}
5286
5287template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005288inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005289void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5290
Marshall Clowff91de82015-08-18 19:51:37 +00005291template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005292struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005293 _Traits::propagate_on_container_move_assignment::value
5294#if _LIBCPP_STD_VER > 14
5295 || _Traits::is_always_equal::value
5296#else
5297 && is_nothrow_move_assignable<_Alloc>::value
5298#endif
5299 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005300
Marshall Clowa591b9a2016-07-11 21:38:08 +00005301
5302#ifndef _LIBCPP_HAS_NO_VARIADICS
5303template <class _Tp, class _Alloc>
5304struct __temp_value {
5305 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005306
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005307 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005308 _Alloc &__a;
5309
5310 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5311 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005312
Marshall Clowa591b9a2016-07-11 21:38:08 +00005313 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005314 _LIBCPP_NO_CFI
5315 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5316 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5317 _VSTD::forward<_Args>(__args)...);
5318 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005319
Marshall Clowa591b9a2016-07-11 21:38:08 +00005320 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5321 };
5322#endif
5323
Marshall Clowe46031a2018-07-02 18:41:15 +00005324template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005325struct __is_allocator : false_type {};
5326
5327template<typename _Alloc>
5328struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005329 typename __void_t<typename _Alloc::value_type>::type,
5330 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5331 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005332 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005333
Eric Fiselier74ebee62019-06-08 01:31:19 +00005334// __builtin_new_allocator -- A non-templated helper for allocating and
5335// deallocating memory using __builtin_operator_new and
5336// __builtin_operator_delete. It should be used in preference to
5337// `std::allocator<T>` to avoid additional instantiations.
5338struct __builtin_new_allocator {
5339 struct __builtin_new_deleter {
5340 typedef void* pointer_type;
5341
5342 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5343 : __size_(__size), __align_(__align) {}
5344
5345 void operator()(void* p) const _NOEXCEPT {
5346 std::__libcpp_deallocate(p, __size_, __align_);
5347 }
5348
5349 private:
5350 size_t __size_;
5351 size_t __align_;
5352 };
5353
5354 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5355
5356 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5357 return __holder_t(std::__libcpp_allocate(__s, __align),
5358 __builtin_new_deleter(__s, __align));
5359 }
5360
5361 static void __deallocate_bytes(void* __p, size_t __s,
5362 size_t __align) _NOEXCEPT {
5363 std::__libcpp_deallocate(__p, __s, __align);
5364 }
5365
5366 template <class _Tp>
5367 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5368 static __holder_t __allocate_type(size_t __n) {
5369 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5370 }
5371
5372 template <class _Tp>
5373 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5374 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5375 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5376 }
5377};
5378
5379
Howard Hinnantc51e1022010-05-11 19:42:16 +00005380_LIBCPP_END_NAMESPACE_STD
5381
Eric Fiselierf4433a32017-05-31 22:07:49 +00005382_LIBCPP_POP_MACROS
5383
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005384#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005385# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005386#endif
5387
Howard Hinnantc51e1022010-05-11 19:42:16 +00005388#endif // _LIBCPP_MEMORY