blob: 12573ca80116180730ef56aae28f9a2bd8b482f3 [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 Fiselier9d355982017-04-12 23:45:53 +0000665#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000666#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000667# include <atomic>
668#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000669#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000670
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000671#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000673#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674
Eric Fiselierf4433a32017-05-31 22:07:49 +0000675_LIBCPP_PUSH_MACROS
676#include <__undef_macros>
677
678
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679_LIBCPP_BEGIN_NAMESPACE_STD
680
Eric Fiselier89659d12015-07-07 00:27:16 +0000681template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000682inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000683_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
684#if !defined(_LIBCPP_HAS_NO_THREADS) && \
685 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000686 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000687 return __atomic_load_n(__value, __ATOMIC_RELAXED);
688#else
689 return *__value;
690#endif
691}
692
Kuba Breckade9d6792016-09-04 09:55:12 +0000693template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000694inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000695_ValueType __libcpp_acquire_load(_ValueType const* __value) {
696#if !defined(_LIBCPP_HAS_NO_THREADS) && \
697 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000698 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000699 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
700#else
701 return *__value;
702#endif
703}
704
Marshall Clow78dbe462016-11-14 18:22:19 +0000705// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000706
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707template <class _Tp> class allocator;
708
709template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000710class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711{
712public:
713 typedef void* pointer;
714 typedef const void* const_pointer;
715 typedef void value_type;
716
717 template <class _Up> struct rebind {typedef allocator<_Up> other;};
718};
719
Howard Hinnant9f771052012-01-19 23:15:22 +0000720template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000721class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000722{
723public:
724 typedef const void* pointer;
725 typedef const void* const_pointer;
726 typedef const void value_type;
727
728 template <class _Up> struct rebind {typedef allocator<_Up> other;};
729};
730
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731// pointer_traits
732
Marshall Clow0be70c32017-06-14 21:23:57 +0000733template <class _Tp, class = void>
734struct __has_element_type : false_type {};
735
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000737struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000738 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739
740template <class _Ptr, bool = __has_element_type<_Ptr>::value>
741struct __pointer_traits_element_type;
742
743template <class _Ptr>
744struct __pointer_traits_element_type<_Ptr, true>
745{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000746 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747};
748
749#ifndef _LIBCPP_HAS_NO_VARIADICS
750
751template <template <class, class...> class _Sp, class _Tp, class ..._Args>
752struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
753{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000754 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755};
756
757template <template <class, class...> class _Sp, class _Tp, class ..._Args>
758struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
759{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000760 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761};
762
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000763#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
765template <template <class> class _Sp, class _Tp>
766struct __pointer_traits_element_type<_Sp<_Tp>, true>
767{
768 typedef typename _Sp<_Tp>::element_type type;
769};
770
771template <template <class> class _Sp, class _Tp>
772struct __pointer_traits_element_type<_Sp<_Tp>, false>
773{
774 typedef _Tp type;
775};
776
777template <template <class, class> class _Sp, class _Tp, class _A0>
778struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
779{
780 typedef typename _Sp<_Tp, _A0>::element_type type;
781};
782
783template <template <class, class> class _Sp, class _Tp, class _A0>
784struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
785{
786 typedef _Tp type;
787};
788
789template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
790struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
791{
792 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
793};
794
795template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
796struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
797{
798 typedef _Tp type;
799};
800
801template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
802 class _A1, class _A2>
803struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
804{
805 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
806};
807
808template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
809 class _A1, class _A2>
810struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
811{
812 typedef _Tp type;
813};
814
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000815#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816
Marshall Clow0be70c32017-06-14 21:23:57 +0000817template <class _Tp, class = void>
818struct __has_difference_type : false_type {};
819
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000821struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000822 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823
824template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
825struct __pointer_traits_difference_type
826{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000827 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828};
829
830template <class _Ptr>
831struct __pointer_traits_difference_type<_Ptr, true>
832{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000833 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834};
835
836template <class _Tp, class _Up>
837struct __has_rebind
838{
839private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000840 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841 template <class _Xp> static __two __test(...);
842 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
843public:
844 static const bool value = sizeof(__test<_Tp>(0)) == 1;
845};
846
847template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
848struct __pointer_traits_rebind
849{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000850#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000851 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000853 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854#endif
855};
856
857#ifndef _LIBCPP_HAS_NO_VARIADICS
858
859template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
861{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000862#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000863 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000865 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866#endif
867};
868
869template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
870struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
871{
872 typedef _Sp<_Up, _Args...> type;
873};
874
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000875#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876
877template <template <class> class _Sp, class _Tp, class _Up>
878struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
879{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000880#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881 typedef typename _Sp<_Tp>::template rebind<_Up> type;
882#else
883 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
884#endif
885};
886
887template <template <class> class _Sp, class _Tp, class _Up>
888struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
889{
890 typedef _Sp<_Up> type;
891};
892
893template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
894struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
895{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000896#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
898#else
899 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
900#endif
901};
902
903template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
905{
906 typedef _Sp<_Up, _A0> type;
907};
908
909template <template <class, class, class> class _Sp, class _Tp, class _A0,
910 class _A1, class _Up>
911struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
912{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000913#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
915#else
916 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
917#endif
918};
919
920template <template <class, class, class> class _Sp, class _Tp, class _A0,
921 class _A1, class _Up>
922struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
923{
924 typedef _Sp<_Up, _A0, _A1> type;
925};
926
927template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
928 class _A1, class _A2, class _Up>
929struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
930{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000931#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
933#else
934 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
935#endif
936};
937
938template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
939 class _A1, class _A2, class _Up>
940struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
941{
942 typedef _Sp<_Up, _A0, _A1, _A2> type;
943};
944
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000945#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
947template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000948struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949{
950 typedef _Ptr pointer;
951 typedef typename __pointer_traits_element_type<pointer>::type element_type;
952 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
953
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000954#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000955 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956#else
957 template <class _Up> struct rebind
958 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000959#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960
961private:
962 struct __nat {};
963public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 static pointer pointer_to(typename conditional<is_void<element_type>::value,
966 __nat, element_type>::type& __r)
967 {return pointer::pointer_to(__r);}
968};
969
970template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000971struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972{
973 typedef _Tp* pointer;
974 typedef _Tp element_type;
975 typedef ptrdiff_t difference_type;
976
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000977#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978 template <class _Up> using rebind = _Up*;
979#else
980 template <class _Up> struct rebind {typedef _Up* other;};
981#endif
982
983private:
984 struct __nat {};
985public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000986 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000988 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000989 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990};
991
Eric Fiselierae3ab842015-08-23 02:56:05 +0000992template <class _From, class _To>
993struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000994#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000995 typedef typename pointer_traits<_From>::template rebind<_To> type;
996#else
997 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
998#endif
999};
1000
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001// allocator_traits
1002
Marshall Clow0be70c32017-06-14 21:23:57 +00001003template <class _Tp, class = void>
1004struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005
1006template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001007struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001008 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009
1010namespace __pointer_type_imp
1011{
1012
1013template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1014struct __pointer_type
1015{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001016 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017};
1018
1019template <class _Tp, class _Dp>
1020struct __pointer_type<_Tp, _Dp, false>
1021{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001022 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023};
1024
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001025} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026
1027template <class _Tp, class _Dp>
1028struct __pointer_type
1029{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001030 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 +00001031};
1032
Marshall Clow0be70c32017-06-14 21:23:57 +00001033template <class _Tp, class = void>
1034struct __has_const_pointer : false_type {};
1035
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001037struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001038 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039
1040template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1041struct __const_pointer
1042{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001043 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044};
1045
1046template <class _Tp, class _Ptr, class _Alloc>
1047struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1048{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001049#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001050 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051#else
1052 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1053#endif
1054};
1055
Marshall Clow0be70c32017-06-14 21:23:57 +00001056template <class _Tp, class = void>
1057struct __has_void_pointer : false_type {};
1058
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001060struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001061 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062
1063template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1064struct __void_pointer
1065{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001066 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067};
1068
1069template <class _Ptr, class _Alloc>
1070struct __void_pointer<_Ptr, _Alloc, false>
1071{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001072#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001073 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001075 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076#endif
1077};
1078
Marshall Clow0be70c32017-06-14 21:23:57 +00001079template <class _Tp, class = void>
1080struct __has_const_void_pointer : false_type {};
1081
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001083struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001084 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085
1086template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1087struct __const_void_pointer
1088{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001089 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090};
1091
1092template <class _Ptr, class _Alloc>
1093struct __const_void_pointer<_Ptr, _Alloc, false>
1094{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001095#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001096 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001098 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099#endif
1100};
1101
Howard Hinnantc834c512011-11-29 18:15:50 +00001102template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001103inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001104_Tp*
1105__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106{
1107 return __p;
1108}
1109
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001110#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111template <class _Pointer>
1112inline _LIBCPP_INLINE_VISIBILITY
1113typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001114__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001116 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117}
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001118#else
1119template <class _Pointer>
1120inline _LIBCPP_INLINE_VISIBILITY
1121auto
1122__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1123-> decltype(pointer_traits<_Pointer>::to_address(__p))
1124{
1125 return pointer_traits<_Pointer>::to_address(__p);
1126}
1127
1128template <class _Pointer, class... _None>
1129inline _LIBCPP_INLINE_VISIBILITY
1130auto
1131__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1132{
1133 return _VSTD::__to_raw_pointer(__p.operator->());
1134}
1135
1136template <class _Tp>
1137inline _LIBCPP_INLINE_VISIBILITY constexpr
1138_Tp*
1139to_address(_Tp* __p) _NOEXCEPT
1140{
1141 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1142 return __p;
1143}
1144
1145template <class _Pointer>
1146inline _LIBCPP_INLINE_VISIBILITY
1147auto
1148to_address(const _Pointer& __p) _NOEXCEPT
1149{
1150 return _VSTD::__to_raw_pointer(__p);
1151}
1152#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153
Marshall Clow0be70c32017-06-14 21:23:57 +00001154template <class _Tp, class = void>
1155struct __has_size_type : false_type {};
1156
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001158struct __has_size_type<_Tp,
1159 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001161template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162struct __size_type
1163{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001164 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165};
1166
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001167template <class _Alloc, class _DiffType>
1168struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001170 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171};
1172
Marshall Clow0be70c32017-06-14 21:23:57 +00001173template <class _Tp, class = void>
1174struct __has_propagate_on_container_copy_assignment : false_type {};
1175
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001177struct __has_propagate_on_container_copy_assignment<_Tp,
1178 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1179 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180
1181template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1182struct __propagate_on_container_copy_assignment
1183{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001184 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185};
1186
1187template <class _Alloc>
1188struct __propagate_on_container_copy_assignment<_Alloc, true>
1189{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001190 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191};
1192
Marshall Clow0be70c32017-06-14 21:23:57 +00001193template <class _Tp, class = void>
1194struct __has_propagate_on_container_move_assignment : false_type {};
1195
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001197struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001198 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1199 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200
1201template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1202struct __propagate_on_container_move_assignment
1203{
1204 typedef false_type type;
1205};
1206
1207template <class _Alloc>
1208struct __propagate_on_container_move_assignment<_Alloc, true>
1209{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001210 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211};
1212
Marshall Clow0be70c32017-06-14 21:23:57 +00001213template <class _Tp, class = void>
1214struct __has_propagate_on_container_swap : false_type {};
1215
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001217struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001218 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1219 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220
1221template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1222struct __propagate_on_container_swap
1223{
1224 typedef false_type type;
1225};
1226
1227template <class _Alloc>
1228struct __propagate_on_container_swap<_Alloc, true>
1229{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001230 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231};
1232
Marshall Clow0be70c32017-06-14 21:23:57 +00001233template <class _Tp, class = void>
1234struct __has_is_always_equal : false_type {};
1235
Marshall Clow0b587562015-06-02 16:34:03 +00001236template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001237struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001238 typename __void_t<typename _Tp::is_always_equal>::type>
1239 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001240
1241template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1242struct __is_always_equal
1243{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001244 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001245};
1246
1247template <class _Alloc>
1248struct __is_always_equal<_Alloc, true>
1249{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001250 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001251};
1252
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1254struct __has_rebind_other
1255{
1256private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001257 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258 template <class _Xp> static __two __test(...);
1259 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1260public:
1261 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1262};
1263
1264template <class _Tp, class _Up>
1265struct __has_rebind_other<_Tp, _Up, false>
1266{
1267 static const bool value = false;
1268};
1269
1270template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1271struct __allocator_traits_rebind
1272{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001273 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274};
1275
1276#ifndef _LIBCPP_HAS_NO_VARIADICS
1277
1278template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1279struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1280{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001281 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282};
1283
1284template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1285struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1286{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001287 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288};
1289
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001290#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291
1292template <template <class> class _Alloc, class _Tp, class _Up>
1293struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1294{
1295 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1296};
1297
1298template <template <class> class _Alloc, class _Tp, class _Up>
1299struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1300{
1301 typedef _Alloc<_Up> type;
1302};
1303
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1305struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1306{
1307 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1308};
1309
1310template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1312{
1313 typedef _Alloc<_Up, _A0> type;
1314};
1315
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1319{
1320 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1321};
1322
1323template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1326{
1327 typedef _Alloc<_Up, _A0, _A1> type;
1328};
1329
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1331 class _A1, class _A2, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1333{
1334 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1335};
1336
1337template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1338 class _A1, class _A2, class _Up>
1339struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1340{
1341 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1342};
1343
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001344#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001346#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347
1348template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1349auto
1350__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001351 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352
1353template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1354auto
1355__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1356 -> false_type;
1357
1358template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1359struct __has_allocate_hint
1360 : integral_constant<bool,
1361 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001362 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 declval<_SizeType>(),
1364 declval<_ConstVoidPtr>())),
1365 true_type>::value>
1366{
1367};
1368
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001369#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370
1371template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1372struct __has_allocate_hint
1373 : true_type
1374{
1375};
1376
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001377#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001379#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
1381template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001382decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1383 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384 true_type())
1385__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1386
1387template <class _Alloc, class _Pointer, class ..._Args>
1388false_type
1389__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1390
1391template <class _Alloc, class _Pointer, class ..._Args>
1392struct __has_construct
1393 : integral_constant<bool,
1394 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001395 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 declval<_Pointer>(),
1397 declval<_Args>()...)),
1398 true_type>::value>
1399{
1400};
1401
1402template <class _Alloc, class _Pointer>
1403auto
1404__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1405 -> decltype(__a.destroy(__p), true_type());
1406
1407template <class _Alloc, class _Pointer>
1408auto
1409__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1410 -> false_type;
1411
1412template <class _Alloc, class _Pointer>
1413struct __has_destroy
1414 : integral_constant<bool,
1415 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001416 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 declval<_Pointer>())),
1418 true_type>::value>
1419{
1420};
1421
1422template <class _Alloc>
1423auto
1424__has_max_size_test(_Alloc&& __a)
1425 -> decltype(__a.max_size(), true_type());
1426
1427template <class _Alloc>
1428auto
1429__has_max_size_test(const volatile _Alloc& __a)
1430 -> false_type;
1431
1432template <class _Alloc>
1433struct __has_max_size
1434 : integral_constant<bool,
1435 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001436 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 true_type>::value>
1438{
1439};
1440
1441template <class _Alloc>
1442auto
1443__has_select_on_container_copy_construction_test(_Alloc&& __a)
1444 -> decltype(__a.select_on_container_copy_construction(), true_type());
1445
1446template <class _Alloc>
1447auto
1448__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1449 -> false_type;
1450
1451template <class _Alloc>
1452struct __has_select_on_container_copy_construction
1453 : integral_constant<bool,
1454 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001455 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456 true_type>::value>
1457{
1458};
1459
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001460#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001462template <class _Alloc, class _Pointer, class _Tp, class = void>
1463struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001465template <class _Alloc, class _Pointer, class _Tp>
1466struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1467 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1468>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001470template <class _Alloc, class _Pointer, class = void>
1471struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001474struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1475 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1476>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
1478template <class _Alloc>
1479struct __has_max_size
1480 : true_type
1481{
1482};
1483
1484template <class _Alloc>
1485struct __has_select_on_container_copy_construction
1486 : false_type
1487{
1488};
1489
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001490#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001492template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1493struct __alloc_traits_difference_type
1494{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001495 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001496};
1497
1498template <class _Alloc, class _Ptr>
1499struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1500{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001501 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001502};
1503
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001504template <class _Tp>
1505struct __is_default_allocator : false_type {};
1506
1507template <class _Tp>
1508struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1509
Eric Fiselier909fe962019-09-13 16:09:33 +00001510
1511
1512template <class _Alloc,
1513 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1514 >
1515struct __is_cpp17_move_insertable;
1516template <class _Alloc>
1517struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1518template <class _Alloc>
1519struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1520
1521template <class _Alloc,
1522 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1523 >
1524struct __is_cpp17_copy_insertable;
1525template <class _Alloc>
1526struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1527template <class _Alloc>
1528struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1529 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1530 __is_cpp17_move_insertable<_Alloc>::value>
1531 {};
1532
1533
1534
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001536struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537{
1538 typedef _Alloc allocator_type;
1539 typedef typename allocator_type::value_type value_type;
1540
1541 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1542 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1543 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1544 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1545
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001546 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1547 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548
1549 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1550 propagate_on_container_copy_assignment;
1551 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1552 propagate_on_container_move_assignment;
1553 typedef typename __propagate_on_container_swap<allocator_type>::type
1554 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001555 typedef typename __is_always_equal<allocator_type>::type
1556 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001558#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001560 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001561 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001562#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 template <class _Tp> struct rebind_alloc
1564 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1565 template <class _Tp> struct rebind_traits
1566 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001567#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568
Marshall Clow0e58cae2017-11-26 02:55:38 +00001569 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 static pointer allocate(allocator_type& __a, size_type __n)
1571 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001572 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001574 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1576
Howard Hinnant756c69b2010-09-22 16:48:34 +00001577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001578 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 {__a.deallocate(__p, __n);}
1580
1581#ifndef _LIBCPP_HAS_NO_VARIADICS
1582 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001585 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001586 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001587#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001589 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001590 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 {
1592 ::new ((void*)__p) _Tp();
1593 }
1594 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001596 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001598 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1599 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 }
1601 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001602 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001603 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 const _A1& __a1)
1605 {
1606 ::new ((void*)__p) _Tp(__a0, __a1);
1607 }
1608 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001609 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001610 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 const _A1& __a1, const _A2& __a2)
1612 {
1613 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1614 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001615#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616
1617 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619 static void destroy(allocator_type& __a, _Tp* __p)
1620 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1621
Howard Hinnant756c69b2010-09-22 16:48:34 +00001622 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001623 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1625
Howard Hinnant756c69b2010-09-22 16:48:34 +00001626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627 static allocator_type
1628 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001629 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630 __has_select_on_container_copy_construction<const allocator_type>(),
1631 __a);}
1632
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001633 template <class _Ptr>
1634 _LIBCPP_INLINE_VISIBILITY
1635 static
1636 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001637 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001638 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001639 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1640 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001641 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselier909fe962019-09-13 16:09:33 +00001642 construct(__a, _VSTD::__to_raw_pointer(__begin2),
1643#ifdef _LIBCPP_NO_EXCEPTIONS
1644 _VSTD::move(*__begin1)
1645#else
1646 _VSTD::move_if_noexcept(*__begin1)
1647#endif
1648 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001649 }
1650
1651 template <class _Tp>
1652 _LIBCPP_INLINE_VISIBILITY
1653 static
1654 typename enable_if
1655 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001656 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001657 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1658 is_trivially_move_constructible<_Tp>::value,
1659 void
1660 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001661 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001662 {
1663 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001664 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001665 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001666 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001667 __begin2 += _Np;
1668 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001669 }
1670
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001671 template <class _Iter, class _Ptr>
1672 _LIBCPP_INLINE_VISIBILITY
1673 static
1674 void
1675 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1676 {
1677 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1678 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1679 }
1680
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001681 template <class _SourceTp, class _DestTp,
1682 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1683 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001684 _LIBCPP_INLINE_VISIBILITY
1685 static
1686 typename enable_if
1687 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001688 is_trivially_move_constructible<_DestTp>::value &&
1689 is_same<_RawSourceTp, _RawDestTp>::value &&
1690 (__is_default_allocator<allocator_type>::value ||
1691 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001692 void
1693 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001694 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001695 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001696 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001697 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001698 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001699 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001700 __begin2 += _Np;
1701 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001702 }
1703
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001704 template <class _Ptr>
1705 _LIBCPP_INLINE_VISIBILITY
1706 static
1707 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001708 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001709 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001710 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1711 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001712 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001713 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001714 construct(__a, _VSTD::__to_raw_pointer(__end2 - 1),
1715#ifdef _LIBCPP_NO_EXCEPTIONS
1716 _VSTD::move(*--__end1)
1717#else
1718 _VSTD::move_if_noexcept(*--__end1)
1719#endif
1720 );
1721 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001722 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001723 }
1724
1725 template <class _Tp>
1726 _LIBCPP_INLINE_VISIBILITY
1727 static
1728 typename enable_if
1729 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001730 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001731 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1732 is_trivially_move_constructible<_Tp>::value,
1733 void
1734 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001735 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001736 {
1737 ptrdiff_t _Np = __end1 - __begin1;
1738 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001739 if (_Np > 0)
1740 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001741 }
1742
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743private:
1744
Howard Hinnant756c69b2010-09-22 16:48:34 +00001745 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001746 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747 const_void_pointer __hint, true_type)
1748 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001749 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001750 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001751 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 {return __a.allocate(__n);}
1753
1754#ifndef _LIBCPP_HAS_NO_VARIADICS
1755 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001758 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1762 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001763 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001765#else // _LIBCPP_HAS_NO_VARIADICS
1766 template <class _Tp, class _A0>
1767 _LIBCPP_INLINE_VISIBILITY
1768 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1769 const _A0& __a0)
1770 {__a.construct(__p, __a0);}
1771 template <class _Tp, class _A0>
1772 _LIBCPP_INLINE_VISIBILITY
1773 static void __construct(false_type, allocator_type&, _Tp* __p,
1774 const _A0& __a0)
1775 {
1776 ::new ((void*)__p) _Tp(__a0);
1777 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001778#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779
1780 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1783 {__a.destroy(__p);}
1784 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 static void __destroy(false_type, allocator_type&, _Tp* __p)
1787 {
1788 __p->~_Tp();
1789 }
1790
Howard Hinnant756c69b2010-09-22 16:48:34 +00001791 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001792 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001794 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001795 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001796 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797
Howard Hinnant756c69b2010-09-22 16:48:34 +00001798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001800 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001804 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 {return __a;}
1806};
1807
Marshall Clow940e01c2015-04-07 05:21:38 +00001808template <class _Traits, class _Tp>
1809struct __rebind_alloc_helper
1810{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001811#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001812 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001813#else
1814 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1815#endif
1816};
1817
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818// allocator
1819
1820template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001821class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822{
1823public:
1824 typedef size_t size_type;
1825 typedef ptrdiff_t difference_type;
1826 typedef _Tp* pointer;
1827 typedef const _Tp* const_pointer;
1828 typedef _Tp& reference;
1829 typedef const _Tp& const_reference;
1830 typedef _Tp value_type;
1831
Howard Hinnant4931e092011-06-02 21:38:57 +00001832 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001833 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001834
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1836
Marshall Clowbc759762018-03-20 23:02:53 +00001837 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1838 allocator() _NOEXCEPT {}
1839
Louis Dionne481a2662018-09-23 18:35:00 +00001840 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001841 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1842 allocator(const allocator<_Up>&) _NOEXCEPT {}
1843
Howard Hinnant719bda32011-05-28 14:41:13 +00001844 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001845 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001846 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001847 {return _VSTD::addressof(__x);}
Louis Dionne481a2662018-09-23 18:35:00 +00001848 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0e58cae2017-11-26 02:55:38 +00001849 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001850 {
1851 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001852 __throw_length_error("allocator<T>::allocate(size_t n)"
1853 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001854 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001855 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001856 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001857 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001858 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1859 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001860#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 template <class _Up, class... _Args>
1862 _LIBCPP_INLINE_VISIBILITY
1863 void
1864 construct(_Up* __p, _Args&&... __args)
1865 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001866 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001868#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869 _LIBCPP_INLINE_VISIBILITY
1870 void
1871 construct(pointer __p)
1872 {
1873 ::new((void*)__p) _Tp();
1874 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001875# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001876
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 template <class _A0>
1878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001879 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 construct(pointer __p, _A0& __a0)
1881 {
1882 ::new((void*)__p) _Tp(__a0);
1883 }
1884 template <class _A0>
1885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001886 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 construct(pointer __p, const _A0& __a0)
1888 {
1889 ::new((void*)__p) _Tp(__a0);
1890 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001891# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 template <class _A0, class _A1>
1893 _LIBCPP_INLINE_VISIBILITY
1894 void
1895 construct(pointer __p, _A0& __a0, _A1& __a1)
1896 {
1897 ::new((void*)__p) _Tp(__a0, __a1);
1898 }
1899 template <class _A0, class _A1>
1900 _LIBCPP_INLINE_VISIBILITY
1901 void
1902 construct(pointer __p, const _A0& __a0, _A1& __a1)
1903 {
1904 ::new((void*)__p) _Tp(__a0, __a1);
1905 }
1906 template <class _A0, class _A1>
1907 _LIBCPP_INLINE_VISIBILITY
1908 void
1909 construct(pointer __p, _A0& __a0, const _A1& __a1)
1910 {
1911 ::new((void*)__p) _Tp(__a0, __a1);
1912 }
1913 template <class _A0, class _A1>
1914 _LIBCPP_INLINE_VISIBILITY
1915 void
1916 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1917 {
1918 ::new((void*)__p) _Tp(__a0, __a1);
1919 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001920#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1922};
1923
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001924template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001925class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001926{
1927public:
1928 typedef size_t size_type;
1929 typedef ptrdiff_t difference_type;
1930 typedef const _Tp* pointer;
1931 typedef const _Tp* const_pointer;
1932 typedef const _Tp& reference;
1933 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001934 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001935
1936 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001937 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001938
1939 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1940
Marshall Clowbc759762018-03-20 23:02:53 +00001941 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1942 allocator() _NOEXCEPT {}
1943
1944 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001945 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001946 allocator(const allocator<_Up>&) _NOEXCEPT {}
1947
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001948 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1949 {return _VSTD::addressof(__x);}
1950 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001951 {
1952 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001953 __throw_length_error("allocator<const T>::allocate(size_t n)"
1954 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001955 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001956 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001957 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001958 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001959 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1960 {return size_type(~0) / sizeof(_Tp);}
1961#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1962 template <class _Up, class... _Args>
1963 _LIBCPP_INLINE_VISIBILITY
1964 void
1965 construct(_Up* __p, _Args&&... __args)
1966 {
1967 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1968 }
1969#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1970 _LIBCPP_INLINE_VISIBILITY
1971 void
1972 construct(pointer __p)
1973 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001974 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001975 }
1976# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001977
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001978 template <class _A0>
1979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001980 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001981 construct(pointer __p, _A0& __a0)
1982 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001983 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001984 }
1985 template <class _A0>
1986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001987 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001988 construct(pointer __p, const _A0& __a0)
1989 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001990 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001991 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001992# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1993 template <class _A0, class _A1>
1994 _LIBCPP_INLINE_VISIBILITY
1995 void
1996 construct(pointer __p, _A0& __a0, _A1& __a1)
1997 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001998 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001999 }
2000 template <class _A0, class _A1>
2001 _LIBCPP_INLINE_VISIBILITY
2002 void
2003 construct(pointer __p, const _A0& __a0, _A1& __a1)
2004 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002005 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002006 }
2007 template <class _A0, class _A1>
2008 _LIBCPP_INLINE_VISIBILITY
2009 void
2010 construct(pointer __p, _A0& __a0, const _A1& __a1)
2011 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002012 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002013 }
2014 template <class _A0, class _A1>
2015 _LIBCPP_INLINE_VISIBILITY
2016 void
2017 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2018 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002019 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002020 }
2021#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2022 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2023};
2024
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025template <class _Tp, class _Up>
2026inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002027bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028
2029template <class _Tp, class _Up>
2030inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002031bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032
2033template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002034class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035 : public iterator<output_iterator_tag,
2036 _Tp, // purposefully not C++03
2037 ptrdiff_t, // purposefully not C++03
2038 _Tp*, // purposefully not C++03
2039 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2040{
2041private:
2042 _OutputIterator __x_;
2043public:
2044 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2045 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2046 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002047 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002048#if _LIBCPP_STD_VER >= 14
2049 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002050 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002051#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2053 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2054 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002055#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002056 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002057#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058};
2059
2060template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002061_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002063get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064{
2065 pair<_Tp*, ptrdiff_t> __r(0, 0);
2066 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2067 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2068 / sizeof(_Tp);
2069 if (__n > __m)
2070 __n = __m;
2071 while (__n > 0)
2072 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002073#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002074 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002075 {
2076 std::align_val_t __al =
2077 std::align_val_t(std::alignment_of<_Tp>::value);
2078 __r.first = static_cast<_Tp*>(::operator new(
2079 __n * sizeof(_Tp), __al, nothrow));
2080 } else {
2081 __r.first = static_cast<_Tp*>(::operator new(
2082 __n * sizeof(_Tp), nothrow));
2083 }
2084#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002085 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002086 {
2087 // Since aligned operator new is unavailable, return an empty
2088 // buffer rather than one with invalid alignment.
2089 return __r;
2090 }
2091
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002093#endif
2094
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095 if (__r.first)
2096 {
2097 __r.second = __n;
2098 break;
2099 }
2100 __n /= 2;
2101 }
2102 return __r;
2103}
2104
2105template <class _Tp>
2106inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002107void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2108{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002109 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002110}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111
Marshall Clowb22274f2017-01-24 22:22:33 +00002112#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002114struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115{
2116 _Tp* __ptr_;
2117};
2118
2119template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002120class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121{
2122private:
2123 _Tp* __ptr_;
2124public:
2125 typedef _Tp element_type;
2126
2127 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2128 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2129 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2130 : __ptr_(__p.release()) {}
2131 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2132 {reset(__p.release()); return *this;}
2133 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2134 {reset(__p.release()); return *this;}
2135 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2136 {reset(__p.__ptr_); return *this;}
2137 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2138
2139 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2140 {return *__ptr_;}
2141 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2142 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2143 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2144 {
2145 _Tp* __t = __ptr_;
2146 __ptr_ = 0;
2147 return __t;
2148 }
2149 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2150 {
2151 if (__ptr_ != __p)
2152 delete __ptr_;
2153 __ptr_ = __p;
2154 }
2155
2156 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2157 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2158 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2159 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2160 {return auto_ptr<_Up>(release());}
2161};
2162
2163template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002164class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165{
2166public:
2167 typedef void element_type;
2168};
Marshall Clowb22274f2017-01-24 22:22:33 +00002169#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170
Eric Fiselier9d355982017-04-12 23:45:53 +00002171template <class _Tp, int _Idx,
2172 bool _CanBeEmptyBase =
2173 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2174struct __compressed_pair_elem {
2175 typedef _Tp _ParamT;
2176 typedef _Tp& reference;
2177 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178
Eric Fiselier9d355982017-04-12 23:45:53 +00002179#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002180 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181
Eric Fiselier9d355982017-04-12 23:45:53 +00002182 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002183 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2184 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002185 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002186 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002187 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002188 : __value_(_VSTD::forward<_Up>(__u))
2189 {
2190 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191
Eric Fiselier9d355982017-04-12 23:45:53 +00002192 template <class... _Args, size_t... _Indexes>
2193 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2194 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2195 __tuple_indices<_Indexes...>)
2196 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2197#else
Alex Lorenz76132112017-11-09 17:54:49 +00002198 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2199 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002200 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2201#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202
Alex Lorenz76132112017-11-09 17:54:49 +00002203 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2204 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002205 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002208 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209};
2210
Eric Fiselier9d355982017-04-12 23:45:53 +00002211template <class _Tp, int _Idx>
2212struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2213 typedef _Tp _ParamT;
2214 typedef _Tp& reference;
2215 typedef const _Tp& const_reference;
2216 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217
Eric Fiselier9d355982017-04-12 23:45:53 +00002218#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002219 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
Eric Fiselier9d355982017-04-12 23:45:53 +00002221 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002222 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2223 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002224 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002225 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002226 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002227 : __value_type(_VSTD::forward<_Up>(__u))
2228 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229
Eric Fiselier9d355982017-04-12 23:45:53 +00002230 template <class... _Args, size_t... _Indexes>
2231 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2232 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2233 __tuple_indices<_Indexes...>)
2234 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2235#else
Alex Lorenz76132112017-11-09 17:54:49 +00002236 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2237 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002238 __compressed_pair_elem(_ParamT __p)
2239 : __value_type(std::forward<_ParamT>(__p)) {}
2240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241
Alex Lorenz76132112017-11-09 17:54:49 +00002242 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2243 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002244 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245};
2246
Eric Fiselier9d355982017-04-12 23:45:53 +00002247// Tag used to construct the second element of the compressed pair.
2248struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249
2250template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002251class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2252 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002253 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2254 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002255
2256 // NOTE: This static assert should never fire because __compressed_pair
2257 // is *almost never* used in a scenario where it's possible for T1 == T2.
2258 // (The exception is std::function where it is possible that the function
2259 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002260 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002261 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2262 "The current implementation is NOT ABI-compatible with the previous "
2263 "implementation for this configuration");
2264
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002266#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002267 template <bool _Dummy = true,
2268 class = typename enable_if<
2269 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2270 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2271 >::type
2272 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002273 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002274 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275
Eric Fiselier9d355982017-04-12 23:45:53 +00002276 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2277 __compressed_pair>::value,
2278 bool>::type = true>
2279 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2280 __compressed_pair(_Tp&& __t)
2281 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282
Eric Fiselier9d355982017-04-12 23:45:53 +00002283 template <class _Tp>
2284 _LIBCPP_INLINE_VISIBILITY constexpr
2285 __compressed_pair(__second_tag, _Tp&& __t)
2286 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287
Eric Fiselier9d355982017-04-12 23:45:53 +00002288 template <class _U1, class _U2>
2289 _LIBCPP_INLINE_VISIBILITY constexpr
2290 __compressed_pair(_U1&& __t1, _U2&& __t2)
2291 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292
Eric Fiselier9d355982017-04-12 23:45:53 +00002293 template <class... _Args1, class... _Args2>
2294 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2295 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2296 tuple<_Args2...> __second_args)
2297 : _Base1(__pc, _VSTD::move(__first_args),
2298 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2299 _Base2(__pc, _VSTD::move(__second_args),
2300 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002301
Eric Fiselier9d355982017-04-12 23:45:53 +00002302#else
2303 _LIBCPP_INLINE_VISIBILITY
2304 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002305
Eric Fiselier9d355982017-04-12 23:45:53 +00002306 _LIBCPP_INLINE_VISIBILITY explicit
2307 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002308
Eric Fiselier9d355982017-04-12 23:45:53 +00002309 _LIBCPP_INLINE_VISIBILITY
2310 __compressed_pair(__second_tag, _T2 __t2)
2311 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312
Eric Fiselier9d355982017-04-12 23:45:53 +00002313 _LIBCPP_INLINE_VISIBILITY
2314 __compressed_pair(_T1 __t1, _T2 __t2)
2315 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2316#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317
Eric Fiselier9d355982017-04-12 23:45:53 +00002318 _LIBCPP_INLINE_VISIBILITY
2319 typename _Base1::reference first() _NOEXCEPT {
2320 return static_cast<_Base1&>(*this).__get();
2321 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322
Eric Fiselier9d355982017-04-12 23:45:53 +00002323 _LIBCPP_INLINE_VISIBILITY
2324 typename _Base1::const_reference first() const _NOEXCEPT {
2325 return static_cast<_Base1 const&>(*this).__get();
2326 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327
Eric Fiselier9d355982017-04-12 23:45:53 +00002328 _LIBCPP_INLINE_VISIBILITY
2329 typename _Base2::reference second() _NOEXCEPT {
2330 return static_cast<_Base2&>(*this).__get();
2331 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002332
Eric Fiselier9d355982017-04-12 23:45:53 +00002333 _LIBCPP_INLINE_VISIBILITY
2334 typename _Base2::const_reference second() const _NOEXCEPT {
2335 return static_cast<_Base2 const&>(*this).__get();
2336 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337
Eric Fiselier9d355982017-04-12 23:45:53 +00002338 _LIBCPP_INLINE_VISIBILITY
2339 void swap(__compressed_pair& __x)
2340 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2341 __is_nothrow_swappable<_T2>::value)
2342 {
2343 using std::swap;
2344 swap(first(), __x.first());
2345 swap(second(), __x.second());
2346 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347};
2348
2349template <class _T1, class _T2>
2350inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002351void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2352 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2353 __is_nothrow_swappable<_T2>::value) {
2354 __x.swap(__y);
2355}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002357// default_delete
2358
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002360struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002361 static_assert(!is_function<_Tp>::value,
2362 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002363#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002364 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002365#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002366 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002367#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002368 template <class _Up>
2369 _LIBCPP_INLINE_VISIBILITY
2370 default_delete(const default_delete<_Up>&,
2371 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2372 0) _NOEXCEPT {}
2373
2374 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2375 static_assert(sizeof(_Tp) > 0,
2376 "default_delete can not delete incomplete type");
2377 static_assert(!is_void<_Tp>::value,
2378 "default_delete can not delete incomplete type");
2379 delete __ptr;
2380 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381};
2382
2383template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002384struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2385private:
2386 template <class _Up>
2387 struct _EnableIfConvertible
2388 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2389
Howard Hinnant4500ca52011-12-18 21:19:44 +00002390public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002391#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002392 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002393#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002394 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002395#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002396
2397 template <class _Up>
2398 _LIBCPP_INLINE_VISIBILITY
2399 default_delete(const default_delete<_Up[]>&,
2400 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2401
2402 template <class _Up>
2403 _LIBCPP_INLINE_VISIBILITY
2404 typename _EnableIfConvertible<_Up>::type
2405 operator()(_Up* __ptr) const _NOEXCEPT {
2406 static_assert(sizeof(_Tp) > 0,
2407 "default_delete can not delete incomplete type");
2408 static_assert(!is_void<_Tp>::value,
2409 "default_delete can not delete void type");
2410 delete[] __ptr;
2411 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412};
2413
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002414template <class _Deleter>
2415struct __unique_ptr_deleter_sfinae {
2416 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2417 typedef const _Deleter& __lval_ref_type;
2418 typedef _Deleter&& __good_rval_ref_type;
2419 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420};
2421
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002422template <class _Deleter>
2423struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2424 typedef const _Deleter& __lval_ref_type;
2425 typedef const _Deleter&& __bad_rval_ref_type;
2426 typedef false_type __enable_rval_overload;
2427};
2428
2429template <class _Deleter>
2430struct __unique_ptr_deleter_sfinae<_Deleter&> {
2431 typedef _Deleter& __lval_ref_type;
2432 typedef _Deleter&& __bad_rval_ref_type;
2433 typedef false_type __enable_rval_overload;
2434};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002435
2436template <class _Tp, class _Dp = default_delete<_Tp> >
2437class _LIBCPP_TEMPLATE_VIS unique_ptr {
2438public:
2439 typedef _Tp element_type;
2440 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002441 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002442
2443 static_assert(!is_rvalue_reference<deleter_type>::value,
2444 "the specified deleter type cannot be an rvalue reference");
2445
2446private:
2447 __compressed_pair<pointer, deleter_type> __ptr_;
2448
2449 struct __nat { int __for_bool_; };
2450
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002451 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002452
2453 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002454 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002455 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002456
2457 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002458 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002459 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002460
2461 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002462 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002463 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002464
2465 template <bool _Dummy, class _Deleter = typename __dependent_type<
2466 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002467 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002468 typename enable_if<is_default_constructible<_Deleter>::value &&
2469 !is_pointer<_Deleter>::value>::type;
2470
2471 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002472 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002473 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2474
2475 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002476 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002477 is_convertible<typename _UPtr::pointer, pointer>::value &&
2478 !is_array<_Up>::value
2479 >::type;
2480
2481 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002482 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002483 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2484 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2485 >::type;
2486
2487 template <class _UDel>
2488 using _EnableIfDeleterAssignable = typename enable_if<
2489 is_assignable<_Dp&, _UDel&&>::value
2490 >::type;
2491
2492public:
2493 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002494 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002495 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002496 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002497
2498 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002499 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002500 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002501 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002502
2503 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002504 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002505 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002506 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002507
2508 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002509 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002510 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002511 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002512 : __ptr_(__p, __d) {}
2513
2514 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002515 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002516 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002517 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002518 : __ptr_(__p, _VSTD::move(__d)) {
2519 static_assert(!is_reference<deleter_type>::value,
2520 "rvalue deleter bound to reference");
2521 }
2522
2523 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002524 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002525 _LIBCPP_INLINE_VISIBILITY
2526 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2527
2528 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002529 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002530 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2531 }
2532
2533 template <class _Up, class _Ep,
2534 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2535 class = _EnableIfDeleterConvertible<_Ep>
2536 >
2537 _LIBCPP_INLINE_VISIBILITY
2538 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2539 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2540
2541#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2542 template <class _Up>
2543 _LIBCPP_INLINE_VISIBILITY
2544 unique_ptr(auto_ptr<_Up>&& __p,
2545 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002546 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002547 __nat>::type = __nat()) _NOEXCEPT
2548 : __ptr_(__p.release()) {}
2549#endif
2550
2551 _LIBCPP_INLINE_VISIBILITY
2552 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2553 reset(__u.release());
2554 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2555 return *this;
2556 }
2557
2558 template <class _Up, class _Ep,
2559 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2560 class = _EnableIfDeleterAssignable<_Ep>
2561 >
2562 _LIBCPP_INLINE_VISIBILITY
2563 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2564 reset(__u.release());
2565 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2566 return *this;
2567 }
2568
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002569#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2570 template <class _Up>
2571 _LIBCPP_INLINE_VISIBILITY
2572 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2573 is_same<_Dp, default_delete<_Tp> >::value,
2574 unique_ptr&>::type
2575 operator=(auto_ptr<_Up> __p) {
2576 reset(__p.release());
2577 return *this;
2578 }
2579#endif
2580
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002581#ifdef _LIBCPP_CXX03_LANG
2582 unique_ptr(unique_ptr const&) = delete;
2583 unique_ptr& operator=(unique_ptr const&) = delete;
2584#endif
2585
2586
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002587 _LIBCPP_INLINE_VISIBILITY
2588 ~unique_ptr() { reset(); }
2589
2590 _LIBCPP_INLINE_VISIBILITY
2591 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2592 reset();
2593 return *this;
2594 }
2595
2596 _LIBCPP_INLINE_VISIBILITY
2597 typename add_lvalue_reference<_Tp>::type
2598 operator*() const {
2599 return *__ptr_.first();
2600 }
2601 _LIBCPP_INLINE_VISIBILITY
2602 pointer operator->() const _NOEXCEPT {
2603 return __ptr_.first();
2604 }
2605 _LIBCPP_INLINE_VISIBILITY
2606 pointer get() const _NOEXCEPT {
2607 return __ptr_.first();
2608 }
2609 _LIBCPP_INLINE_VISIBILITY
2610 deleter_type& get_deleter() _NOEXCEPT {
2611 return __ptr_.second();
2612 }
2613 _LIBCPP_INLINE_VISIBILITY
2614 const deleter_type& get_deleter() const _NOEXCEPT {
2615 return __ptr_.second();
2616 }
2617 _LIBCPP_INLINE_VISIBILITY
2618 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2619 return __ptr_.first() != nullptr;
2620 }
2621
2622 _LIBCPP_INLINE_VISIBILITY
2623 pointer release() _NOEXCEPT {
2624 pointer __t = __ptr_.first();
2625 __ptr_.first() = pointer();
2626 return __t;
2627 }
2628
2629 _LIBCPP_INLINE_VISIBILITY
2630 void reset(pointer __p = pointer()) _NOEXCEPT {
2631 pointer __tmp = __ptr_.first();
2632 __ptr_.first() = __p;
2633 if (__tmp)
2634 __ptr_.second()(__tmp);
2635 }
2636
2637 _LIBCPP_INLINE_VISIBILITY
2638 void swap(unique_ptr& __u) _NOEXCEPT {
2639 __ptr_.swap(__u.__ptr_);
2640 }
2641};
2642
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002643
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002645class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002647 typedef _Tp element_type;
2648 typedef _Dp deleter_type;
2649 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2650
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002652 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653
Eric Fiselier31127cd2017-04-16 02:14:31 +00002654 template <class _From>
2655 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656
Eric Fiselier31127cd2017-04-16 02:14:31 +00002657 template <class _FromElem>
2658 struct _CheckArrayPointerConversion<_FromElem*>
2659 : integral_constant<bool,
2660 is_same<_FromElem*, pointer>::value ||
2661 (is_same<pointer, element_type*>::value &&
2662 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2663 >
2664 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665
Eric Fiselier31127cd2017-04-16 02:14:31 +00002666 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002667
2668 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002669 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002670 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002671
2672 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002673 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002674 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002675
2676 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002677 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002678 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002679
2680 template <bool _Dummy, class _Deleter = typename __dependent_type<
2681 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002682 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002683 typename enable_if<is_default_constructible<_Deleter>::value &&
2684 !is_pointer<_Deleter>::value>::type;
2685
2686 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002687 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002688 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2689
2690 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002691 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002692 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002693 >::type;
2694
2695 template <class _UPtr, class _Up,
2696 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002697 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002698 is_array<_Up>::value &&
2699 is_same<pointer, element_type*>::value &&
2700 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2701 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2702 >::type;
2703
2704 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002705 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002706 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2707 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2708 >::type;
2709
2710 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002711 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002712 is_assignable<_Dp&, _UDel&&>::value
2713 >::type;
2714
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002716 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002717 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002718 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002719 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002721 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002722 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002723 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002724 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002726 template <class _Pp, bool _Dummy = true,
2727 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002728 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002729 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002730 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002731 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002733 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002734 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2735 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002736 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002737 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002738 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002740 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002741 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002742 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002743 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002746 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002747 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2748 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002749 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002750 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002751 : __ptr_(__p, _VSTD::move(__d)) {
2752 static_assert(!is_reference<deleter_type>::value,
2753 "rvalue deleter bound to reference");
2754 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002756 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002757 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002758 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002759 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002760 : __ptr_(nullptr, _VSTD::move(__d)) {
2761 static_assert(!is_reference<deleter_type>::value,
2762 "rvalue deleter bound to reference");
2763 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002764
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002765 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002766 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2767 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002768 _LIBCPP_INLINE_VISIBILITY
2769 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002770
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002771 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002772 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002773 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2774 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002775
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002776 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002777 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002778 reset(__u.release());
2779 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2780 return *this;
2781 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002783 template <class _Up, class _Ep,
2784 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2785 class = _EnableIfDeleterConvertible<_Ep>
2786 >
2787 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002788 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002789 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002790 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002792 template <class _Up, class _Ep,
2793 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2794 class = _EnableIfDeleterAssignable<_Ep>
2795 >
2796 _LIBCPP_INLINE_VISIBILITY
2797 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002798 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002799 reset(__u.release());
2800 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2801 return *this;
2802 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002804#ifdef _LIBCPP_CXX03_LANG
2805 unique_ptr(unique_ptr const&) = delete;
2806 unique_ptr& operator=(unique_ptr const&) = delete;
2807#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002808
2809public:
2810 _LIBCPP_INLINE_VISIBILITY
2811 ~unique_ptr() { reset(); }
2812
2813 _LIBCPP_INLINE_VISIBILITY
2814 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2815 reset();
2816 return *this;
2817 }
2818
2819 _LIBCPP_INLINE_VISIBILITY
2820 typename add_lvalue_reference<_Tp>::type
2821 operator[](size_t __i) const {
2822 return __ptr_.first()[__i];
2823 }
2824 _LIBCPP_INLINE_VISIBILITY
2825 pointer get() const _NOEXCEPT {
2826 return __ptr_.first();
2827 }
2828
2829 _LIBCPP_INLINE_VISIBILITY
2830 deleter_type& get_deleter() _NOEXCEPT {
2831 return __ptr_.second();
2832 }
2833
2834 _LIBCPP_INLINE_VISIBILITY
2835 const deleter_type& get_deleter() const _NOEXCEPT {
2836 return __ptr_.second();
2837 }
2838 _LIBCPP_INLINE_VISIBILITY
2839 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2840 return __ptr_.first() != nullptr;
2841 }
2842
2843 _LIBCPP_INLINE_VISIBILITY
2844 pointer release() _NOEXCEPT {
2845 pointer __t = __ptr_.first();
2846 __ptr_.first() = pointer();
2847 return __t;
2848 }
2849
2850 template <class _Pp>
2851 _LIBCPP_INLINE_VISIBILITY
2852 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002853 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002854 >::type
2855 reset(_Pp __p) _NOEXCEPT {
2856 pointer __tmp = __ptr_.first();
2857 __ptr_.first() = __p;
2858 if (__tmp)
2859 __ptr_.second()(__tmp);
2860 }
2861
2862 _LIBCPP_INLINE_VISIBILITY
2863 void reset(nullptr_t = nullptr) _NOEXCEPT {
2864 pointer __tmp = __ptr_.first();
2865 __ptr_.first() = nullptr;
2866 if (__tmp)
2867 __ptr_.second()(__tmp);
2868 }
2869
2870 _LIBCPP_INLINE_VISIBILITY
2871 void swap(unique_ptr& __u) _NOEXCEPT {
2872 __ptr_.swap(__u.__ptr_);
2873 }
2874
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875};
2876
2877template <class _Tp, class _Dp>
2878inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002879typename enable_if<
2880 __is_swappable<_Dp>::value,
2881 void
2882>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002883swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884
2885template <class _T1, class _D1, class _T2, class _D2>
2886inline _LIBCPP_INLINE_VISIBILITY
2887bool
2888operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2889
2890template <class _T1, class _D1, class _T2, class _D2>
2891inline _LIBCPP_INLINE_VISIBILITY
2892bool
2893operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2894
2895template <class _T1, class _D1, class _T2, class _D2>
2896inline _LIBCPP_INLINE_VISIBILITY
2897bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002898operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2899{
2900 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2901 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002902 typedef typename common_type<_P1, _P2>::type _Vp;
2903 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002904}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905
2906template <class _T1, class _D1, class _T2, class _D2>
2907inline _LIBCPP_INLINE_VISIBILITY
2908bool
2909operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2910
2911template <class _T1, class _D1, class _T2, class _D2>
2912inline _LIBCPP_INLINE_VISIBILITY
2913bool
2914operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2915
2916template <class _T1, class _D1, class _T2, class _D2>
2917inline _LIBCPP_INLINE_VISIBILITY
2918bool
2919operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2920
Howard Hinnantb17caf92012-02-21 21:02:58 +00002921template <class _T1, class _D1>
2922inline _LIBCPP_INLINE_VISIBILITY
2923bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002924operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002925{
2926 return !__x;
2927}
2928
2929template <class _T1, class _D1>
2930inline _LIBCPP_INLINE_VISIBILITY
2931bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002932operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002933{
2934 return !__x;
2935}
2936
2937template <class _T1, class _D1>
2938inline _LIBCPP_INLINE_VISIBILITY
2939bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002940operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002941{
2942 return static_cast<bool>(__x);
2943}
2944
2945template <class _T1, class _D1>
2946inline _LIBCPP_INLINE_VISIBILITY
2947bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002948operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002949{
2950 return static_cast<bool>(__x);
2951}
2952
2953template <class _T1, class _D1>
2954inline _LIBCPP_INLINE_VISIBILITY
2955bool
2956operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2957{
2958 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2959 return less<_P1>()(__x.get(), nullptr);
2960}
2961
2962template <class _T1, class _D1>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2966{
2967 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2968 return less<_P1>()(nullptr, __x.get());
2969}
2970
2971template <class _T1, class _D1>
2972inline _LIBCPP_INLINE_VISIBILITY
2973bool
2974operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2975{
2976 return nullptr < __x;
2977}
2978
2979template <class _T1, class _D1>
2980inline _LIBCPP_INLINE_VISIBILITY
2981bool
2982operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2983{
2984 return __x < nullptr;
2985}
2986
2987template <class _T1, class _D1>
2988inline _LIBCPP_INLINE_VISIBILITY
2989bool
2990operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2991{
2992 return !(nullptr < __x);
2993}
2994
2995template <class _T1, class _D1>
2996inline _LIBCPP_INLINE_VISIBILITY
2997bool
2998operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2999{
3000 return !(__x < nullptr);
3001}
3002
3003template <class _T1, class _D1>
3004inline _LIBCPP_INLINE_VISIBILITY
3005bool
3006operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3007{
3008 return !(__x < nullptr);
3009}
3010
3011template <class _T1, class _D1>
3012inline _LIBCPP_INLINE_VISIBILITY
3013bool
3014operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3015{
3016 return !(nullptr < __x);
3017}
3018
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003019#if _LIBCPP_STD_VER > 11
3020
3021template<class _Tp>
3022struct __unique_if
3023{
3024 typedef unique_ptr<_Tp> __unique_single;
3025};
3026
3027template<class _Tp>
3028struct __unique_if<_Tp[]>
3029{
3030 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3031};
3032
3033template<class _Tp, size_t _Np>
3034struct __unique_if<_Tp[_Np]>
3035{
3036 typedef void __unique_array_known_bound;
3037};
3038
3039template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003040inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003041typename __unique_if<_Tp>::__unique_single
3042make_unique(_Args&&... __args)
3043{
3044 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3045}
3046
3047template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003048inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003049typename __unique_if<_Tp>::__unique_array_unknown_bound
3050make_unique(size_t __n)
3051{
3052 typedef typename remove_extent<_Tp>::type _Up;
3053 return unique_ptr<_Tp>(new _Up[__n]());
3054}
3055
3056template<class _Tp, class... _Args>
3057 typename __unique_if<_Tp>::__unique_array_known_bound
3058 make_unique(_Args&&...) = delete;
3059
3060#endif // _LIBCPP_STD_VER > 11
3061
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003062template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003063#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003064struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003065#else
3066struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003067 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003068#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003069{
3070 typedef unique_ptr<_Tp, _Dp> argument_type;
3071 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003072 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003073 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003074 {
3075 typedef typename argument_type::pointer pointer;
3076 return hash<pointer>()(__ptr.get());
3077 }
3078};
3079
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080struct __destruct_n
3081{
3082private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003083 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084
3085 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003086 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003087 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088
3089 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003090 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 {}
3092
Howard Hinnant719bda32011-05-28 14:41:13 +00003093 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003094 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003095 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096 {}
3097
Howard Hinnant719bda32011-05-28 14:41:13 +00003098 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003099 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003100 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 {}
3102public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003103 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003104 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105
3106 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003107 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003108 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109
3110 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003111 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003112 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113
3114 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003115 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003116 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117};
3118
3119template <class _Alloc>
3120class __allocator_destructor
3121{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003122 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003124 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3125 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126private:
3127 _Alloc& __alloc_;
3128 size_type __s_;
3129public:
3130 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003131 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003134 void operator()(pointer __p) _NOEXCEPT
3135 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136};
3137
3138template <class _InputIterator, class _ForwardIterator>
3139_ForwardIterator
3140uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3141{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003143#ifndef _LIBCPP_NO_EXCEPTIONS
3144 _ForwardIterator __s = __r;
3145 try
3146 {
3147#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003148 for (; __f != __l; ++__f, (void) ++__r)
3149 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003150#ifndef _LIBCPP_NO_EXCEPTIONS
3151 }
3152 catch (...)
3153 {
3154 for (; __s != __r; ++__s)
3155 __s->~value_type();
3156 throw;
3157 }
3158#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159 return __r;
3160}
3161
3162template <class _InputIterator, class _Size, class _ForwardIterator>
3163_ForwardIterator
3164uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3165{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003167#ifndef _LIBCPP_NO_EXCEPTIONS
3168 _ForwardIterator __s = __r;
3169 try
3170 {
3171#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003172 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3173 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003174#ifndef _LIBCPP_NO_EXCEPTIONS
3175 }
3176 catch (...)
3177 {
3178 for (; __s != __r; ++__s)
3179 __s->~value_type();
3180 throw;
3181 }
3182#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183 return __r;
3184}
3185
3186template <class _ForwardIterator, class _Tp>
3187void
3188uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3189{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003191#ifndef _LIBCPP_NO_EXCEPTIONS
3192 _ForwardIterator __s = __f;
3193 try
3194 {
3195#endif
3196 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003197 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003198#ifndef _LIBCPP_NO_EXCEPTIONS
3199 }
3200 catch (...)
3201 {
3202 for (; __s != __f; ++__s)
3203 __s->~value_type();
3204 throw;
3205 }
3206#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207}
3208
3209template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003210_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3212{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003214#ifndef _LIBCPP_NO_EXCEPTIONS
3215 _ForwardIterator __s = __f;
3216 try
3217 {
3218#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003219 for (; __n > 0; ++__f, (void) --__n)
3220 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003221#ifndef _LIBCPP_NO_EXCEPTIONS
3222 }
3223 catch (...)
3224 {
3225 for (; __s != __f; ++__s)
3226 __s->~value_type();
3227 throw;
3228 }
3229#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003230 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231}
3232
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003233#if _LIBCPP_STD_VER > 14
3234
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003235template <class _Tp>
3236inline _LIBCPP_INLINE_VISIBILITY
3237void destroy_at(_Tp* __loc) {
3238 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3239 __loc->~_Tp();
3240}
3241
3242template <class _ForwardIterator>
3243inline _LIBCPP_INLINE_VISIBILITY
3244void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3245 for (; __first != __last; ++__first)
3246 _VSTD::destroy_at(_VSTD::addressof(*__first));
3247}
3248
3249template <class _ForwardIterator, class _Size>
3250inline _LIBCPP_INLINE_VISIBILITY
3251_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3252 for (; __n > 0; (void)++__first, --__n)
3253 _VSTD::destroy_at(_VSTD::addressof(*__first));
3254 return __first;
3255}
3256
Eric Fiselier290c07c2016-10-11 21:13:44 +00003257template <class _ForwardIterator>
3258inline _LIBCPP_INLINE_VISIBILITY
3259void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3260 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3261 auto __idx = __first;
3262#ifndef _LIBCPP_NO_EXCEPTIONS
3263 try {
3264#endif
3265 for (; __idx != __last; ++__idx)
3266 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3267#ifndef _LIBCPP_NO_EXCEPTIONS
3268 } catch (...) {
3269 _VSTD::destroy(__first, __idx);
3270 throw;
3271 }
3272#endif
3273}
3274
3275template <class _ForwardIterator, class _Size>
3276inline _LIBCPP_INLINE_VISIBILITY
3277_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3278 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3279 auto __idx = __first;
3280#ifndef _LIBCPP_NO_EXCEPTIONS
3281 try {
3282#endif
3283 for (; __n > 0; (void)++__idx, --__n)
3284 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3285 return __idx;
3286#ifndef _LIBCPP_NO_EXCEPTIONS
3287 } catch (...) {
3288 _VSTD::destroy(__first, __idx);
3289 throw;
3290 }
3291#endif
3292}
3293
3294
3295template <class _ForwardIterator>
3296inline _LIBCPP_INLINE_VISIBILITY
3297void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3298 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3299 auto __idx = __first;
3300#ifndef _LIBCPP_NO_EXCEPTIONS
3301 try {
3302#endif
3303 for (; __idx != __last; ++__idx)
3304 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3305#ifndef _LIBCPP_NO_EXCEPTIONS
3306 } catch (...) {
3307 _VSTD::destroy(__first, __idx);
3308 throw;
3309 }
3310#endif
3311}
3312
3313template <class _ForwardIterator, class _Size>
3314inline _LIBCPP_INLINE_VISIBILITY
3315_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3316 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3317 auto __idx = __first;
3318#ifndef _LIBCPP_NO_EXCEPTIONS
3319 try {
3320#endif
3321 for (; __n > 0; (void)++__idx, --__n)
3322 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3323 return __idx;
3324#ifndef _LIBCPP_NO_EXCEPTIONS
3325 } catch (...) {
3326 _VSTD::destroy(__first, __idx);
3327 throw;
3328 }
3329#endif
3330}
3331
3332
3333template <class _InputIt, class _ForwardIt>
3334inline _LIBCPP_INLINE_VISIBILITY
3335_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3336 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3337 auto __idx = __first_res;
3338#ifndef _LIBCPP_NO_EXCEPTIONS
3339 try {
3340#endif
3341 for (; __first != __last; (void)++__idx, ++__first)
3342 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3343 return __idx;
3344#ifndef _LIBCPP_NO_EXCEPTIONS
3345 } catch (...) {
3346 _VSTD::destroy(__first_res, __idx);
3347 throw;
3348 }
3349#endif
3350}
3351
3352template <class _InputIt, class _Size, class _ForwardIt>
3353inline _LIBCPP_INLINE_VISIBILITY
3354pair<_InputIt, _ForwardIt>
3355uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3356 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3357 auto __idx = __first_res;
3358#ifndef _LIBCPP_NO_EXCEPTIONS
3359 try {
3360#endif
3361 for (; __n > 0; ++__idx, (void)++__first, --__n)
3362 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3363 return {__first, __idx};
3364#ifndef _LIBCPP_NO_EXCEPTIONS
3365 } catch (...) {
3366 _VSTD::destroy(__first_res, __idx);
3367 throw;
3368 }
3369#endif
3370}
3371
3372
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003373#endif // _LIBCPP_STD_VER > 14
3374
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003375// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3376// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003377// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003378#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3379 && defined(__ATOMIC_RELAXED) \
3380 && defined(__ATOMIC_ACQ_REL)
3381# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003382#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003383# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3384#endif
3385
3386template <class _Tp>
3387inline _LIBCPP_INLINE_VISIBILITY _Tp
3388__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3389{
3390#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3391 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3392#else
3393 return __t += 1;
3394#endif
3395}
3396
3397template <class _Tp>
3398inline _LIBCPP_INLINE_VISIBILITY _Tp
3399__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3400{
3401#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3402 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3403#else
3404 return __t -= 1;
3405#endif
3406}
3407
Howard Hinnant756c69b2010-09-22 16:48:34 +00003408class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409 : public std::exception
3410{
3411public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003412 virtual ~bad_weak_ptr() _NOEXCEPT;
3413 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414};
3415
Louis Dionne16fe2952018-07-11 23:14:33 +00003416_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003417void __throw_bad_weak_ptr()
3418{
3419#ifndef _LIBCPP_NO_EXCEPTIONS
3420 throw bad_weak_ptr();
3421#else
3422 _VSTD::abort();
3423#endif
3424}
3425
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003426template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003428class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429{
3430 __shared_count(const __shared_count&);
3431 __shared_count& operator=(const __shared_count&);
3432
3433protected:
3434 long __shared_owners_;
3435 virtual ~__shared_count();
3436private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003437 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438
3439public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003441 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442 : __shared_owners_(__refs) {}
3443
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003444#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003445 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003446 void __add_shared() _NOEXCEPT;
3447 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003448#else
3449 _LIBCPP_INLINE_VISIBILITY
3450 void __add_shared() _NOEXCEPT {
3451 __libcpp_atomic_refcount_increment(__shared_owners_);
3452 }
3453 _LIBCPP_INLINE_VISIBILITY
3454 bool __release_shared() _NOEXCEPT {
3455 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3456 __on_zero_shared();
3457 return true;
3458 }
3459 return false;
3460 }
3461#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003462 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003463 long use_count() const _NOEXCEPT {
3464 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3465 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466};
3467
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003468class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469 : private __shared_count
3470{
3471 long __shared_weak_owners_;
3472
3473public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003475 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476 : __shared_count(__refs),
3477 __shared_weak_owners_(__refs) {}
3478protected:
3479 virtual ~__shared_weak_count();
3480
3481public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003482#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003483 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003484 void __add_shared() _NOEXCEPT;
3485 void __add_weak() _NOEXCEPT;
3486 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003487#else
3488 _LIBCPP_INLINE_VISIBILITY
3489 void __add_shared() _NOEXCEPT {
3490 __shared_count::__add_shared();
3491 }
3492 _LIBCPP_INLINE_VISIBILITY
3493 void __add_weak() _NOEXCEPT {
3494 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3495 }
3496 _LIBCPP_INLINE_VISIBILITY
3497 void __release_shared() _NOEXCEPT {
3498 if (__shared_count::__release_shared())
3499 __release_weak();
3500 }
3501#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003502 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003504 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3505 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506
Howard Hinnant807d6332013-02-25 15:50:36 +00003507 // Define the function out only if we build static libc++ without RTTI.
3508 // Otherwise we may break clients who need to compile their projects with
3509 // -fno-rtti and yet link against a libc++.dylib compiled
3510 // without -fno-rtti.
3511#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003512 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003513#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003515 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516};
3517
3518template <class _Tp, class _Dp, class _Alloc>
3519class __shared_ptr_pointer
3520 : public __shared_weak_count
3521{
3522 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3523public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003526 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527
Howard Hinnant72f73582010-08-11 17:04:31 +00003528#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003529 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003530#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531
3532private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003533 virtual void __on_zero_shared() _NOEXCEPT;
3534 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535};
3536
Howard Hinnant72f73582010-08-11 17:04:31 +00003537#ifndef _LIBCPP_NO_RTTI
3538
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539template <class _Tp, class _Dp, class _Alloc>
3540const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003541__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003543 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544}
3545
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003546#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003547
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548template <class _Tp, class _Dp, class _Alloc>
3549void
Howard Hinnant719bda32011-05-28 14:41:13 +00003550__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551{
3552 __data_.first().second()(__data_.first().first());
3553 __data_.first().second().~_Dp();
3554}
3555
3556template <class _Tp, class _Dp, class _Alloc>
3557void
Howard Hinnant719bda32011-05-28 14:41:13 +00003558__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003560 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3561 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003562 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3563
Eric Fiselierf8898c82015-02-05 23:01:40 +00003564 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003566 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567}
3568
3569template <class _Tp, class _Alloc>
3570class __shared_ptr_emplace
3571 : public __shared_weak_count
3572{
3573 __compressed_pair<_Alloc, _Tp> __data_;
3574public:
3575#ifndef _LIBCPP_HAS_NO_VARIADICS
3576
Howard Hinnant756c69b2010-09-22 16:48:34 +00003577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003579 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580
3581 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003584 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3585 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586
3587#else // _LIBCPP_HAS_NO_VARIADICS
3588
Howard Hinnant756c69b2010-09-22 16:48:34 +00003589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590 __shared_ptr_emplace(_Alloc __a)
3591 : __data_(__a) {}
3592
3593 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3596 : __data_(__a, _Tp(__a0)) {}
3597
3598 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3601 : __data_(__a, _Tp(__a0, __a1)) {}
3602
3603 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3606 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3607
3608#endif // _LIBCPP_HAS_NO_VARIADICS
3609
3610private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003611 virtual void __on_zero_shared() _NOEXCEPT;
3612 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003614 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003615 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616};
3617
3618template <class _Tp, class _Alloc>
3619void
Howard Hinnant719bda32011-05-28 14:41:13 +00003620__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621{
3622 __data_.second().~_Tp();
3623}
3624
3625template <class _Tp, class _Alloc>
3626void
Howard Hinnant719bda32011-05-28 14:41:13 +00003627__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003629 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3630 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003631 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003632 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003634 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635}
3636
Erik Pilkington2a398762017-05-25 15:43:31 +00003637struct __shared_ptr_dummy_rebind_allocator_type;
3638template <>
3639class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3640{
3641public:
3642 template <class _Other>
3643 struct rebind
3644 {
3645 typedef allocator<_Other> other;
3646 };
3647};
3648
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003649template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650
3651template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003652class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003654public:
3655 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003656
Eric Fiselierae5b6672016-06-27 01:02:43 +00003657#if _LIBCPP_STD_VER > 14
3658 typedef weak_ptr<_Tp> weak_type;
3659#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660private:
3661 element_type* __ptr_;
3662 __shared_weak_count* __cntrl_;
3663
3664 struct __nat {int __for_bool_;};
3665public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003667 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003669 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003670 template<class _Yp>
3671 explicit shared_ptr(_Yp* __p,
3672 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3673 template<class _Yp, class _Dp>
3674 shared_ptr(_Yp* __p, _Dp __d,
3675 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3676 template<class _Yp, class _Dp, class _Alloc>
3677 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3678 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3680 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003681 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003683 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003687 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003688 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003689#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003691 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003692 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003693 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003694 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003695#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003697 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003698#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003699#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003700 template<class _Yp>
3701 shared_ptr(auto_ptr<_Yp>&& __r,
3702 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003703#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003704 template<class _Yp>
3705 shared_ptr(auto_ptr<_Yp> __r,
3706 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003708#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003709#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003710 template <class _Yp, class _Dp>
3711 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3712 typename enable_if
3713 <
3714 !is_lvalue_reference<_Dp>::value &&
3715 !is_array<_Yp>::value &&
3716 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3717 __nat
3718 >::type = __nat());
3719 template <class _Yp, class _Dp>
3720 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3721 typename enable_if
3722 <
3723 is_lvalue_reference<_Dp>::value &&
3724 !is_array<_Yp>::value &&
3725 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3726 __nat
3727 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003728#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003729 template <class _Yp, class _Dp>
3730 shared_ptr(unique_ptr<_Yp, _Dp>,
3731 typename enable_if
3732 <
3733 !is_lvalue_reference<_Dp>::value &&
3734 !is_array<_Yp>::value &&
3735 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3736 __nat
3737 >::type = __nat());
3738 template <class _Yp, class _Dp>
3739 shared_ptr(unique_ptr<_Yp, _Dp>,
3740 typename enable_if
3741 <
3742 is_lvalue_reference<_Dp>::value &&
3743 !is_array<_Yp>::value &&
3744 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3745 __nat
3746 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003747#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748
3749 ~shared_ptr();
3750
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003752 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003753 template<class _Yp>
3754 typename enable_if
3755 <
3756 is_convertible<_Yp*, element_type*>::value,
3757 shared_ptr&
3758 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003760 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003761#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003763 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003764 template<class _Yp>
3765 typename enable_if
3766 <
3767 is_convertible<_Yp*, element_type*>::value,
3768 shared_ptr<_Tp>&
3769 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003771 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003772#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003773 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003775 typename enable_if
3776 <
3777 !is_array<_Yp>::value &&
3778 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003779 shared_ptr
3780 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003781 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003782#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003783#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003784#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003785 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003787 typename enable_if
3788 <
3789 !is_array<_Yp>::value &&
3790 is_convertible<_Yp*, element_type*>::value,
3791 shared_ptr&
3792 >::type
3793 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003795#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003796 template <class _Yp, class _Dp>
3797 typename enable_if
3798 <
3799 !is_array<_Yp>::value &&
3800 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3801 shared_ptr&
3802 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003803#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003805 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003806#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003808 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809#endif
3810
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003812 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003814 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003815 template<class _Yp>
3816 typename enable_if
3817 <
3818 is_convertible<_Yp*, element_type*>::value,
3819 void
3820 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003822 reset(_Yp* __p);
3823 template<class _Yp, class _Dp>
3824 typename enable_if
3825 <
3826 is_convertible<_Yp*, element_type*>::value,
3827 void
3828 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003830 reset(_Yp* __p, _Dp __d);
3831 template<class _Yp, class _Dp, class _Alloc>
3832 typename enable_if
3833 <
3834 is_convertible<_Yp*, element_type*>::value,
3835 void
3836 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003838 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839
Howard Hinnant756c69b2010-09-22 16:48:34 +00003840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003841 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003843 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3844 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003846 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003848 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003850 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003852 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003853 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003854 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003855 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003857 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003858 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003859 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003861 _LIBCPP_INLINE_VISIBILITY
3862 bool
3863 __owner_equivalent(const shared_ptr& __p) const
3864 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865
Howard Hinnant72f73582010-08-11 17:04:31 +00003866#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003869 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003870 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003871 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003872 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003873#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874
Zoe Carver6cd05c32019-08-19 15:47:16 +00003875#ifndef _LIBCPP_HAS_NO_VARIADICS
3876
3877 template<class ..._Args>
3878 static
3879 shared_ptr<_Tp>
3880 make_shared(_Args&& ...__args);
3881
3882 template<class _Alloc, class ..._Args>
3883 static
3884 shared_ptr<_Tp>
3885 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3886
3887#else // _LIBCPP_HAS_NO_VARIADICS
3888
3889 static shared_ptr<_Tp> make_shared();
3890
3891 template<class _A0>
3892 static shared_ptr<_Tp> make_shared(_A0&);
3893
3894 template<class _A0, class _A1>
3895 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3896
3897 template<class _A0, class _A1, class _A2>
3898 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3899
3900 template<class _Alloc>
3901 static shared_ptr<_Tp>
3902 allocate_shared(const _Alloc& __a);
3903
3904 template<class _Alloc, class _A0>
3905 static shared_ptr<_Tp>
3906 allocate_shared(const _Alloc& __a, _A0& __a0);
3907
3908 template<class _Alloc, class _A0, class _A1>
3909 static shared_ptr<_Tp>
3910 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3911
3912 template<class _Alloc, class _A0, class _A1, class _A2>
3913 static shared_ptr<_Tp>
3914 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3915
3916#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917
3918private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003919 template <class _Yp, bool = is_function<_Yp>::value>
3920 struct __shared_ptr_default_allocator
3921 {
3922 typedef allocator<_Yp> type;
3923 };
3924
3925 template <class _Yp>
3926 struct __shared_ptr_default_allocator<_Yp, true>
3927 {
3928 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3929 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003931 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003932 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003933 typename enable_if<is_convertible<_OrigPtr*,
3934 const enable_shared_from_this<_Yp>*
3935 >::value,
3936 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003937 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3938 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003940 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003941 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003942 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003943 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3944 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003945 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003946 }
3947
Erik Pilkington2a398762017-05-25 15:43:31 +00003948 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003950 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3951 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952};
3953
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003954
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003956inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003957_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003958shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959 : __ptr_(0),
3960 __cntrl_(0)
3961{
3962}
3963
3964template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003965inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003966_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003967shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968 : __ptr_(0),
3969 __cntrl_(0)
3970{
3971}
3972
3973template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003974template<class _Yp>
3975shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3976 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977 : __ptr_(__p)
3978{
3979 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003980 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3981 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3982 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003984 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985}
3986
3987template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003988template<class _Yp, class _Dp>
3989shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3990 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991 : __ptr_(__p)
3992{
3993#ifndef _LIBCPP_NO_EXCEPTIONS
3994 try
3995 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003996#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003997 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3998 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3999 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004000 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001#ifndef _LIBCPP_NO_EXCEPTIONS
4002 }
4003 catch (...)
4004 {
4005 __d(__p);
4006 throw;
4007 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004008#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009}
4010
4011template<class _Tp>
4012template<class _Dp>
4013shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4014 : __ptr_(0)
4015{
4016#ifndef _LIBCPP_NO_EXCEPTIONS
4017 try
4018 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004019#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004020 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4021 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4022 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023#ifndef _LIBCPP_NO_EXCEPTIONS
4024 }
4025 catch (...)
4026 {
4027 __d(__p);
4028 throw;
4029 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004030#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031}
4032
4033template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004034template<class _Yp, class _Dp, class _Alloc>
4035shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4036 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037 : __ptr_(__p)
4038{
4039#ifndef _LIBCPP_NO_EXCEPTIONS
4040 try
4041 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004042#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004044 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045 typedef __allocator_destructor<_A2> _D2;
4046 _A2 __a2(__a);
4047 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004048 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4049 _CntrlBlk(__p, __d, __a);
4050 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004051 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052#ifndef _LIBCPP_NO_EXCEPTIONS
4053 }
4054 catch (...)
4055 {
4056 __d(__p);
4057 throw;
4058 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004059#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060}
4061
4062template<class _Tp>
4063template<class _Dp, class _Alloc>
4064shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4065 : __ptr_(0)
4066{
4067#ifndef _LIBCPP_NO_EXCEPTIONS
4068 try
4069 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004070#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004072 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073 typedef __allocator_destructor<_A2> _D2;
4074 _A2 __a2(__a);
4075 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004076 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4077 _CntrlBlk(__p, __d, __a);
4078 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079#ifndef _LIBCPP_NO_EXCEPTIONS
4080 }
4081 catch (...)
4082 {
4083 __d(__p);
4084 throw;
4085 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004086#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087}
4088
4089template<class _Tp>
4090template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004091inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004092shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093 : __ptr_(__p),
4094 __cntrl_(__r.__cntrl_)
4095{
4096 if (__cntrl_)
4097 __cntrl_->__add_shared();
4098}
4099
4100template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004101inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004102shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103 : __ptr_(__r.__ptr_),
4104 __cntrl_(__r.__cntrl_)
4105{
4106 if (__cntrl_)
4107 __cntrl_->__add_shared();
4108}
4109
4110template<class _Tp>
4111template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004112inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004114 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004115 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116 : __ptr_(__r.__ptr_),
4117 __cntrl_(__r.__cntrl_)
4118{
4119 if (__cntrl_)
4120 __cntrl_->__add_shared();
4121}
4122
Howard Hinnant74279a52010-09-04 23:28:19 +00004123#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124
4125template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004126inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004127shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128 : __ptr_(__r.__ptr_),
4129 __cntrl_(__r.__cntrl_)
4130{
4131 __r.__ptr_ = 0;
4132 __r.__cntrl_ = 0;
4133}
4134
4135template<class _Tp>
4136template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004137inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004139 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004140 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141 : __ptr_(__r.__ptr_),
4142 __cntrl_(__r.__cntrl_)
4143{
4144 __r.__ptr_ = 0;
4145 __r.__cntrl_ = 0;
4146}
4147
Howard Hinnant74279a52010-09-04 23:28:19 +00004148#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149
Marshall Clowb22274f2017-01-24 22:22:33 +00004150#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004152template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004153#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004154shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004156shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004158 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159 : __ptr_(__r.get())
4160{
4161 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4162 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004163 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004164 __r.release();
4165}
Marshall Clowb22274f2017-01-24 22:22:33 +00004166#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167
4168template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004169template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004170#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4172#else
4173shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4174#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004175 typename enable_if
4176 <
4177 !is_lvalue_reference<_Dp>::value &&
4178 !is_array<_Yp>::value &&
4179 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4180 __nat
4181 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182 : __ptr_(__r.get())
4183{
Marshall Clow35cde742015-05-10 13:59:45 +00004184#if _LIBCPP_STD_VER > 11
4185 if (__ptr_ == nullptr)
4186 __cntrl_ = nullptr;
4187 else
4188#endif
4189 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004190 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4191 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4192 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004193 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004194 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195 __r.release();
4196}
4197
4198template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004199template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004200#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4202#else
4203shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4204#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004205 typename enable_if
4206 <
4207 is_lvalue_reference<_Dp>::value &&
4208 !is_array<_Yp>::value &&
4209 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4210 __nat
4211 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212 : __ptr_(__r.get())
4213{
Marshall Clow35cde742015-05-10 13:59:45 +00004214#if _LIBCPP_STD_VER > 11
4215 if (__ptr_ == nullptr)
4216 __cntrl_ = nullptr;
4217 else
4218#endif
4219 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004220 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004221 typedef __shared_ptr_pointer<_Yp*,
4222 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004223 _AllocT > _CntrlBlk;
4224 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004225 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004226 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227 __r.release();
4228}
4229
Zoe Carver6cd05c32019-08-19 15:47:16 +00004230#ifndef _LIBCPP_HAS_NO_VARIADICS
4231
4232template<class _Tp>
4233template<class ..._Args>
4234shared_ptr<_Tp>
4235shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4236{
4237 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared" );
4238 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4239 typedef allocator<_CntrlBlk> _A2;
4240 typedef __allocator_destructor<_A2> _D2;
4241 _A2 __a2;
4242 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4243 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4244 shared_ptr<_Tp> __r;
4245 __r.__ptr_ = __hold2.get()->get();
4246 __r.__cntrl_ = __hold2.release();
4247 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4248 return __r;
4249}
4250
4251template<class _Tp>
4252template<class _Alloc, class ..._Args>
4253shared_ptr<_Tp>
4254shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4255{
4256 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
4257 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4258 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4259 typedef __allocator_destructor<_A2> _D2;
4260 _A2 __a2(__a);
4261 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4262 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4263 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4264 shared_ptr<_Tp> __r;
4265 __r.__ptr_ = __hold2.get()->get();
4266 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4267 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4268 return __r;
4269}
4270
4271#else // _LIBCPP_HAS_NO_VARIADICS
4272
4273template<class _Tp>
4274shared_ptr<_Tp>
4275shared_ptr<_Tp>::make_shared()
4276{
4277 static_assert((is_constructible<_Tp>::value), "Can't construct object in make_shared" );
4278 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4279 typedef allocator<_CntrlBlk> _Alloc2;
4280 typedef __allocator_destructor<_Alloc2> _D2;
4281 _Alloc2 __alloc2;
4282 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4283 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4284 shared_ptr<_Tp> __r;
4285 __r.__ptr_ = __hold2.get()->get();
4286 __r.__cntrl_ = __hold2.release();
4287 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4288 return __r;
4289}
4290
4291template<class _Tp>
4292template<class _A0>
4293shared_ptr<_Tp>
4294shared_ptr<_Tp>::make_shared(_A0& __a0)
4295{
4296 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in make_shared" );
4297 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4298 typedef allocator<_CntrlBlk> _Alloc2;
4299 typedef __allocator_destructor<_Alloc2> _D2;
4300 _Alloc2 __alloc2;
4301 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4302 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4303 shared_ptr<_Tp> __r;
4304 __r.__ptr_ = __hold2.get()->get();
4305 __r.__cntrl_ = __hold2.release();
4306 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4307 return __r;
4308}
4309
4310template<class _Tp>
4311template<class _A0, class _A1>
4312shared_ptr<_Tp>
4313shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4314{
4315 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in make_shared" );
4316 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4317 typedef allocator<_CntrlBlk> _Alloc2;
4318 typedef __allocator_destructor<_Alloc2> _D2;
4319 _Alloc2 __alloc2;
4320 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4321 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4322 shared_ptr<_Tp> __r;
4323 __r.__ptr_ = __hold2.get()->get();
4324 __r.__cntrl_ = __hold2.release();
4325 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4326 return __r;
4327}
4328
4329template<class _Tp>
4330template<class _A0, class _A1, class _A2>
4331shared_ptr<_Tp>
4332shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4333{
4334 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in make_shared" );
4335 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4336 typedef allocator<_CntrlBlk> _Alloc2;
4337 typedef __allocator_destructor<_Alloc2> _D2;
4338 _Alloc2 __alloc2;
4339 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4340 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4341 shared_ptr<_Tp> __r;
4342 __r.__ptr_ = __hold2.get()->get();
4343 __r.__cntrl_ = __hold2.release();
4344 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4345 return __r;
4346}
4347
4348template<class _Tp>
4349template<class _Alloc>
4350shared_ptr<_Tp>
4351shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4352{
4353 static_assert((is_constructible<_Tp>::value), "Can't construct object in allocate_shared" );
4354 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4355 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4356 typedef __allocator_destructor<_Alloc2> _D2;
4357 _Alloc2 __alloc2(__a);
4358 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4359 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4360 _CntrlBlk(__a);
4361 shared_ptr<_Tp> __r;
4362 __r.__ptr_ = __hold2.get()->get();
4363 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4364 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4365 return __r;
4366}
4367
4368template<class _Tp>
4369template<class _Alloc, class _A0>
4370shared_ptr<_Tp>
4371shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4372{
4373 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in allocate_shared" );
4374 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4375 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4376 typedef __allocator_destructor<_Alloc2> _D2;
4377 _Alloc2 __alloc2(__a);
4378 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4379 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4380 _CntrlBlk(__a, __a0);
4381 shared_ptr<_Tp> __r;
4382 __r.__ptr_ = __hold2.get()->get();
4383 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4384 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4385 return __r;
4386}
4387
4388template<class _Tp>
4389template<class _Alloc, class _A0, class _A1>
4390shared_ptr<_Tp>
4391shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4392{
4393 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in allocate_shared" );
4394 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4395 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4396 typedef __allocator_destructor<_Alloc2> _D2;
4397 _Alloc2 __alloc2(__a);
4398 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4399 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4400 _CntrlBlk(__a, __a0, __a1);
4401 shared_ptr<_Tp> __r;
4402 __r.__ptr_ = __hold2.get()->get();
4403 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4404 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4405 return __r;
4406}
4407
4408template<class _Tp>
4409template<class _Alloc, class _A0, class _A1, class _A2>
4410shared_ptr<_Tp>
4411shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4412{
4413 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in allocate_shared" );
4414 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4415 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4416 typedef __allocator_destructor<_Alloc2> _D2;
4417 _Alloc2 __alloc2(__a);
4418 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4419 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4420 _CntrlBlk(__a, __a0, __a1, __a2);
4421 shared_ptr<_Tp> __r;
4422 __r.__ptr_ = __hold2.get()->get();
4423 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4424 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4425 return __r;
4426}
4427
4428#endif // _LIBCPP_HAS_NO_VARIADICS
4429
Howard Hinnantc51e1022010-05-11 19:42:16 +00004430template<class _Tp>
4431shared_ptr<_Tp>::~shared_ptr()
4432{
4433 if (__cntrl_)
4434 __cntrl_->__release_shared();
4435}
4436
4437template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004438inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004439shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004440shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441{
4442 shared_ptr(__r).swap(*this);
4443 return *this;
4444}
4445
4446template<class _Tp>
4447template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004448inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004449typename enable_if
4450<
Marshall Clow7e384b72017-01-10 16:59:33 +00004451 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004452 shared_ptr<_Tp>&
4453>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004454shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004455{
4456 shared_ptr(__r).swap(*this);
4457 return *this;
4458}
4459
Howard Hinnant74279a52010-09-04 23:28:19 +00004460#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004461
4462template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004463inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004464shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004465shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004467 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004468 return *this;
4469}
4470
4471template<class _Tp>
4472template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004473inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004474typename enable_if
4475<
Marshall Clow7e384b72017-01-10 16:59:33 +00004476 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004477 shared_ptr<_Tp>&
4478>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4480{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004481 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482 return *this;
4483}
4484
Marshall Clowb22274f2017-01-24 22:22:33 +00004485#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486template<class _Tp>
4487template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004488inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004489typename enable_if
4490<
4491 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004492 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004493 shared_ptr<_Tp>
4494>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4496{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004497 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498 return *this;
4499}
Marshall Clowb22274f2017-01-24 22:22:33 +00004500#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004501
4502template<class _Tp>
4503template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004504inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004505typename enable_if
4506<
4507 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004508 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004509 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004510 shared_ptr<_Tp>&
4511>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004512shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4513{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004514 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004515 return *this;
4516}
4517
Howard Hinnant74279a52010-09-04 23:28:19 +00004518#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519
Marshall Clowb22274f2017-01-24 22:22:33 +00004520#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521template<class _Tp>
4522template<class _Yp>
4523inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004524typename enable_if
4525<
4526 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004527 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004528 shared_ptr<_Tp>&
4529>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004530shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004531{
4532 shared_ptr(__r).swap(*this);
4533 return *this;
4534}
Marshall Clowb22274f2017-01-24 22:22:33 +00004535#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536
4537template<class _Tp>
4538template <class _Yp, class _Dp>
4539inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004540typename enable_if
4541<
4542 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004543 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004544 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004545 shared_ptr<_Tp>&
4546>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004547shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4548{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004549 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004550 return *this;
4551}
4552
Howard Hinnant74279a52010-09-04 23:28:19 +00004553#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554
4555template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004556inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557void
Howard Hinnant719bda32011-05-28 14:41:13 +00004558shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004559{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004560 _VSTD::swap(__ptr_, __r.__ptr_);
4561 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004562}
4563
4564template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004565inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566void
Howard Hinnant719bda32011-05-28 14:41:13 +00004567shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568{
4569 shared_ptr().swap(*this);
4570}
4571
4572template<class _Tp>
4573template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004574inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004575typename enable_if
4576<
Marshall Clow7e384b72017-01-10 16:59:33 +00004577 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004578 void
4579>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004580shared_ptr<_Tp>::reset(_Yp* __p)
4581{
4582 shared_ptr(__p).swap(*this);
4583}
4584
4585template<class _Tp>
4586template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004587inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004588typename enable_if
4589<
Marshall Clow7e384b72017-01-10 16:59:33 +00004590 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004591 void
4592>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004593shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4594{
4595 shared_ptr(__p, __d).swap(*this);
4596}
4597
4598template<class _Tp>
4599template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004600inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004601typename enable_if
4602<
Marshall Clow7e384b72017-01-10 16:59:33 +00004603 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004604 void
4605>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004606shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4607{
4608 shared_ptr(__p, __d, __a).swap(*this);
4609}
4610
4611#ifndef _LIBCPP_HAS_NO_VARIADICS
4612
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004613template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004614inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004615typename enable_if
4616<
4617 !is_array<_Tp>::value,
4618 shared_ptr<_Tp>
4619>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004620make_shared(_Args&& ...__args)
4621{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004622 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004623}
4624
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004625template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004627typename enable_if
4628<
4629 !is_array<_Tp>::value,
4630 shared_ptr<_Tp>
4631>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632allocate_shared(const _Alloc& __a, _Args&& ...__args)
4633{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004634 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635}
4636
4637#else // _LIBCPP_HAS_NO_VARIADICS
4638
4639template<class _Tp>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004640inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641shared_ptr<_Tp>
4642make_shared()
4643{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004644 return shared_ptr<_Tp>::make_shared();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004645}
4646
4647template<class _Tp, class _A0>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004648inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004649shared_ptr<_Tp>
4650make_shared(_A0& __a0)
4651{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004652 return shared_ptr<_Tp>::make_shared(__a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004653}
4654
4655template<class _Tp, class _A0, class _A1>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004656inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004657shared_ptr<_Tp>
4658make_shared(_A0& __a0, _A1& __a1)
4659{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004660 return shared_ptr<_Tp>::make_shared(__a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004661}
4662
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004663template<class _Tp, class _A0, class _A1, class _A2>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004664inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004665shared_ptr<_Tp>
4666make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4667{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004668 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004669}
4670
4671template<class _Tp, class _Alloc>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004672inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004673shared_ptr<_Tp>
4674allocate_shared(const _Alloc& __a)
4675{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004676 return shared_ptr<_Tp>::allocate_shared(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004677}
4678
4679template<class _Tp, class _Alloc, class _A0>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004680inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004681shared_ptr<_Tp>
4682allocate_shared(const _Alloc& __a, _A0& __a0)
4683{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004684 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004685}
4686
4687template<class _Tp, class _Alloc, class _A0, class _A1>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004688inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004689shared_ptr<_Tp>
4690allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4691{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004692 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004693}
4694
4695template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004696inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004697shared_ptr<_Tp>
4698allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4699{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004700 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004701}
4702
4703#endif // _LIBCPP_HAS_NO_VARIADICS
4704
4705template<class _Tp, class _Up>
4706inline _LIBCPP_INLINE_VISIBILITY
4707bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004708operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004709{
4710 return __x.get() == __y.get();
4711}
4712
4713template<class _Tp, class _Up>
4714inline _LIBCPP_INLINE_VISIBILITY
4715bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004716operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004717{
4718 return !(__x == __y);
4719}
4720
4721template<class _Tp, class _Up>
4722inline _LIBCPP_INLINE_VISIBILITY
4723bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004724operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004725{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004726#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004727 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4728 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004729#else
4730 return less<>()(__x.get(), __y.get());
4731#endif
4732
Howard Hinnantb17caf92012-02-21 21:02:58 +00004733}
4734
4735template<class _Tp, class _Up>
4736inline _LIBCPP_INLINE_VISIBILITY
4737bool
4738operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4739{
4740 return __y < __x;
4741}
4742
4743template<class _Tp, class _Up>
4744inline _LIBCPP_INLINE_VISIBILITY
4745bool
4746operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4747{
4748 return !(__y < __x);
4749}
4750
4751template<class _Tp, class _Up>
4752inline _LIBCPP_INLINE_VISIBILITY
4753bool
4754operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4755{
4756 return !(__x < __y);
4757}
4758
4759template<class _Tp>
4760inline _LIBCPP_INLINE_VISIBILITY
4761bool
4762operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4763{
4764 return !__x;
4765}
4766
4767template<class _Tp>
4768inline _LIBCPP_INLINE_VISIBILITY
4769bool
4770operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4771{
4772 return !__x;
4773}
4774
4775template<class _Tp>
4776inline _LIBCPP_INLINE_VISIBILITY
4777bool
4778operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4779{
4780 return static_cast<bool>(__x);
4781}
4782
4783template<class _Tp>
4784inline _LIBCPP_INLINE_VISIBILITY
4785bool
4786operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4787{
4788 return static_cast<bool>(__x);
4789}
4790
4791template<class _Tp>
4792inline _LIBCPP_INLINE_VISIBILITY
4793bool
4794operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4795{
4796 return less<_Tp*>()(__x.get(), nullptr);
4797}
4798
4799template<class _Tp>
4800inline _LIBCPP_INLINE_VISIBILITY
4801bool
4802operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4803{
4804 return less<_Tp*>()(nullptr, __x.get());
4805}
4806
4807template<class _Tp>
4808inline _LIBCPP_INLINE_VISIBILITY
4809bool
4810operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4811{
4812 return nullptr < __x;
4813}
4814
4815template<class _Tp>
4816inline _LIBCPP_INLINE_VISIBILITY
4817bool
4818operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4819{
4820 return __x < nullptr;
4821}
4822
4823template<class _Tp>
4824inline _LIBCPP_INLINE_VISIBILITY
4825bool
4826operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4827{
4828 return !(nullptr < __x);
4829}
4830
4831template<class _Tp>
4832inline _LIBCPP_INLINE_VISIBILITY
4833bool
4834operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4835{
4836 return !(__x < nullptr);
4837}
4838
4839template<class _Tp>
4840inline _LIBCPP_INLINE_VISIBILITY
4841bool
4842operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4843{
4844 return !(__x < nullptr);
4845}
4846
4847template<class _Tp>
4848inline _LIBCPP_INLINE_VISIBILITY
4849bool
4850operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4851{
4852 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004853}
4854
4855template<class _Tp>
4856inline _LIBCPP_INLINE_VISIBILITY
4857void
Howard Hinnant719bda32011-05-28 14:41:13 +00004858swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004859{
4860 __x.swap(__y);
4861}
4862
4863template<class _Tp, class _Up>
4864inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004865typename enable_if
4866<
4867 !is_array<_Tp>::value && !is_array<_Up>::value,
4868 shared_ptr<_Tp>
4869>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004870static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004871{
4872 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4873}
4874
4875template<class _Tp, class _Up>
4876inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004877typename enable_if
4878<
4879 !is_array<_Tp>::value && !is_array<_Up>::value,
4880 shared_ptr<_Tp>
4881>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004882dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004883{
4884 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4885 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4886}
4887
4888template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004889typename enable_if
4890<
4891 is_array<_Tp>::value == is_array<_Up>::value,
4892 shared_ptr<_Tp>
4893>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004894const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004895{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004896 typedef typename remove_extent<_Tp>::type _RTp;
4897 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004898}
4899
Howard Hinnant72f73582010-08-11 17:04:31 +00004900#ifndef _LIBCPP_NO_RTTI
4901
Howard Hinnantc51e1022010-05-11 19:42:16 +00004902template<class _Dp, class _Tp>
4903inline _LIBCPP_INLINE_VISIBILITY
4904_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004905get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004906{
4907 return __p.template __get_deleter<_Dp>();
4908}
4909
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004910#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004911
Howard Hinnantc51e1022010-05-11 19:42:16 +00004912template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004913class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004914{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004915public:
4916 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004917private:
4918 element_type* __ptr_;
4919 __shared_weak_count* __cntrl_;
4920
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004921public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004923 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004924 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004925 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4926 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004928 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004929 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004930 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4931 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004932
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004933#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004935 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004936 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004937 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4938 _NOEXCEPT;
4939#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004940 ~weak_ptr();
4941
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004943 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004944 template<class _Yp>
4945 typename enable_if
4946 <
4947 is_convertible<_Yp*, element_type*>::value,
4948 weak_ptr&
4949 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004951 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4952
4953#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4954
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004956 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4957 template<class _Yp>
4958 typename enable_if
4959 <
4960 is_convertible<_Yp*, element_type*>::value,
4961 weak_ptr&
4962 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004964 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4965
4966#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4967
4968 template<class _Yp>
4969 typename enable_if
4970 <
4971 is_convertible<_Yp*, element_type*>::value,
4972 weak_ptr&
4973 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004975 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004976
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004978 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004980 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004981
Howard Hinnant756c69b2010-09-22 16:48:34 +00004982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004983 long use_count() const _NOEXCEPT
4984 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004986 bool expired() const _NOEXCEPT
4987 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4988 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004989 template<class _Up>
4990 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004991 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004992 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004993 template<class _Up>
4994 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004995 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004996 {return __cntrl_ < __r.__cntrl_;}
4997
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004998 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4999 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005000};
5001
5002template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005003inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005004_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005005weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005006 : __ptr_(0),
5007 __cntrl_(0)
5008{
5009}
5010
5011template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005012inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005013weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005014 : __ptr_(__r.__ptr_),
5015 __cntrl_(__r.__cntrl_)
5016{
5017 if (__cntrl_)
5018 __cntrl_->__add_weak();
5019}
5020
5021template<class _Tp>
5022template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005023inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005024weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005025 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005026 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005027 : __ptr_(__r.__ptr_),
5028 __cntrl_(__r.__cntrl_)
5029{
5030 if (__cntrl_)
5031 __cntrl_->__add_weak();
5032}
5033
5034template<class _Tp>
5035template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005036inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005037weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005038 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005039 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005040 : __ptr_(__r.__ptr_),
5041 __cntrl_(__r.__cntrl_)
5042{
5043 if (__cntrl_)
5044 __cntrl_->__add_weak();
5045}
5046
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005047#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5048
5049template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005050inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005051weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5052 : __ptr_(__r.__ptr_),
5053 __cntrl_(__r.__cntrl_)
5054{
5055 __r.__ptr_ = 0;
5056 __r.__cntrl_ = 0;
5057}
5058
5059template<class _Tp>
5060template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005061inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005062weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5063 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5064 _NOEXCEPT
5065 : __ptr_(__r.__ptr_),
5066 __cntrl_(__r.__cntrl_)
5067{
5068 __r.__ptr_ = 0;
5069 __r.__cntrl_ = 0;
5070}
5071
5072#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5073
Howard Hinnantc51e1022010-05-11 19:42:16 +00005074template<class _Tp>
5075weak_ptr<_Tp>::~weak_ptr()
5076{
5077 if (__cntrl_)
5078 __cntrl_->__release_weak();
5079}
5080
5081template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005082inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005083weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005084weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005085{
5086 weak_ptr(__r).swap(*this);
5087 return *this;
5088}
5089
5090template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005091template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005092inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005093typename enable_if
5094<
5095 is_convertible<_Yp*, _Tp*>::value,
5096 weak_ptr<_Tp>&
5097>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005098weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005099{
5100 weak_ptr(__r).swap(*this);
5101 return *this;
5102}
5103
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005104#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5105
5106template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005107inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005108weak_ptr<_Tp>&
5109weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5110{
5111 weak_ptr(_VSTD::move(__r)).swap(*this);
5112 return *this;
5113}
5114
Howard Hinnantc51e1022010-05-11 19:42:16 +00005115template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005116template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005117inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005118typename enable_if
5119<
5120 is_convertible<_Yp*, _Tp*>::value,
5121 weak_ptr<_Tp>&
5122>::type
5123weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5124{
5125 weak_ptr(_VSTD::move(__r)).swap(*this);
5126 return *this;
5127}
5128
5129#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5130
5131template<class _Tp>
5132template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005133inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005134typename enable_if
5135<
5136 is_convertible<_Yp*, _Tp*>::value,
5137 weak_ptr<_Tp>&
5138>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005139weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005140{
5141 weak_ptr(__r).swap(*this);
5142 return *this;
5143}
5144
5145template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005146inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005147void
Howard Hinnant719bda32011-05-28 14:41:13 +00005148weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005149{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005150 _VSTD::swap(__ptr_, __r.__ptr_);
5151 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005152}
5153
5154template<class _Tp>
5155inline _LIBCPP_INLINE_VISIBILITY
5156void
Howard Hinnant719bda32011-05-28 14:41:13 +00005157swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005158{
5159 __x.swap(__y);
5160}
5161
5162template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005163inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005164void
Howard Hinnant719bda32011-05-28 14:41:13 +00005165weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005166{
5167 weak_ptr().swap(*this);
5168}
5169
5170template<class _Tp>
5171template<class _Yp>
5172shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005173 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005174 : __ptr_(__r.__ptr_),
5175 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5176{
5177 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005178 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005179}
5180
5181template<class _Tp>
5182shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005183weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005184{
5185 shared_ptr<_Tp> __r;
5186 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5187 if (__r.__cntrl_)
5188 __r.__ptr_ = __ptr_;
5189 return __r;
5190}
5191
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005192#if _LIBCPP_STD_VER > 14
5193template <class _Tp = void> struct owner_less;
5194#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005195template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005197
5198template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005199struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005200 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005201{
5202 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005203 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005204 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005205 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005206 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005207 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005208 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005209 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005210 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005211 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005212};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005213
5214template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005215struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005216 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5217{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005218 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005219 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005220 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005221 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005222 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005223 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005224 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005225 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005226 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227 {return __x.owner_before(__y);}
5228};
5229
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005230#if _LIBCPP_STD_VER > 14
5231template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005232struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005233{
5234 template <class _Tp, class _Up>
5235 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005236 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005237 {return __x.owner_before(__y);}
5238 template <class _Tp, class _Up>
5239 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005240 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005241 {return __x.owner_before(__y);}
5242 template <class _Tp, class _Up>
5243 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005244 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005245 {return __x.owner_before(__y);}
5246 template <class _Tp, class _Up>
5247 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005248 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005249 {return __x.owner_before(__y);}
5250 typedef void is_transparent;
5251};
5252#endif
5253
Howard Hinnantc51e1022010-05-11 19:42:16 +00005254template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005255class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005256{
5257 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005258protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005259 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005260 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005262 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005264 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5265 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005267 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005268public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005270 shared_ptr<_Tp> shared_from_this()
5271 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005273 shared_ptr<_Tp const> shared_from_this() const
5274 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005275
Eric Fiselier84006862016-06-02 00:15:35 +00005276#if _LIBCPP_STD_VER > 14
5277 _LIBCPP_INLINE_VISIBILITY
5278 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5279 { return __weak_this_; }
5280
5281 _LIBCPP_INLINE_VISIBILITY
5282 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5283 { return __weak_this_; }
5284#endif // _LIBCPP_STD_VER > 14
5285
Howard Hinnantc51e1022010-05-11 19:42:16 +00005286 template <class _Up> friend class shared_ptr;
5287};
5288
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005289template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005290struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005291{
5292 typedef shared_ptr<_Tp> argument_type;
5293 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005294
Howard Hinnant756c69b2010-09-22 16:48:34 +00005295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005296 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005297 {
5298 return hash<_Tp*>()(__ptr.get());
5299 }
5300};
5301
Howard Hinnantc834c512011-11-29 18:15:50 +00005302template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005303inline _LIBCPP_INLINE_VISIBILITY
5304basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005305operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005306
Eric Fiselier9b492672016-06-18 02:12:53 +00005307
5308#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005309
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005310class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005311{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005312 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005313public:
5314 void lock() _NOEXCEPT;
5315 void unlock() _NOEXCEPT;
5316
5317private:
5318 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5319 __sp_mut(const __sp_mut&);
5320 __sp_mut& operator=(const __sp_mut&);
5321
Howard Hinnant8331b762013-03-06 23:30:19 +00005322 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005323};
5324
Mehdi Amini228053d2017-05-04 17:08:54 +00005325_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5326__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005327
5328template <class _Tp>
5329inline _LIBCPP_INLINE_VISIBILITY
5330bool
5331atomic_is_lock_free(const shared_ptr<_Tp>*)
5332{
5333 return false;
5334}
5335
5336template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005337_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005338shared_ptr<_Tp>
5339atomic_load(const shared_ptr<_Tp>* __p)
5340{
5341 __sp_mut& __m = __get_sp_mut(__p);
5342 __m.lock();
5343 shared_ptr<_Tp> __q = *__p;
5344 __m.unlock();
5345 return __q;
5346}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005347
Howard Hinnant9fa30202012-07-30 01:40:57 +00005348template <class _Tp>
5349inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005350_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005351shared_ptr<_Tp>
5352atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5353{
5354 return atomic_load(__p);
5355}
5356
5357template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005358_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005359void
5360atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5361{
5362 __sp_mut& __m = __get_sp_mut(__p);
5363 __m.lock();
5364 __p->swap(__r);
5365 __m.unlock();
5366}
5367
5368template <class _Tp>
5369inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005370_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005371void
5372atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5373{
5374 atomic_store(__p, __r);
5375}
5376
5377template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005378_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005379shared_ptr<_Tp>
5380atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5381{
5382 __sp_mut& __m = __get_sp_mut(__p);
5383 __m.lock();
5384 __p->swap(__r);
5385 __m.unlock();
5386 return __r;
5387}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005388
Howard Hinnant9fa30202012-07-30 01:40:57 +00005389template <class _Tp>
5390inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005391_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005392shared_ptr<_Tp>
5393atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5394{
5395 return atomic_exchange(__p, __r);
5396}
5397
5398template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005399_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005400bool
5401atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5402{
Marshall Clow4201ee82016-05-18 17:50:13 +00005403 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005404 __sp_mut& __m = __get_sp_mut(__p);
5405 __m.lock();
5406 if (__p->__owner_equivalent(*__v))
5407 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005408 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005409 *__p = __w;
5410 __m.unlock();
5411 return true;
5412 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005413 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005414 *__v = *__p;
5415 __m.unlock();
5416 return false;
5417}
5418
5419template <class _Tp>
5420inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005421_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005422bool
5423atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5424{
5425 return atomic_compare_exchange_strong(__p, __v, __w);
5426}
5427
5428template <class _Tp>
5429inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005430_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005431bool
5432atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5433 shared_ptr<_Tp> __w, memory_order, memory_order)
5434{
5435 return atomic_compare_exchange_strong(__p, __v, __w);
5436}
5437
5438template <class _Tp>
5439inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005440_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005441bool
5442atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5443 shared_ptr<_Tp> __w, memory_order, memory_order)
5444{
5445 return atomic_compare_exchange_weak(__p, __v, __w);
5446}
5447
Eric Fiselier9b492672016-06-18 02:12:53 +00005448#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005449
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005450//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005451#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5452# ifndef _LIBCPP_CXX03_LANG
5453enum class pointer_safety : unsigned char {
5454 relaxed,
5455 preferred,
5456 strict
5457};
5458# endif
5459#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005460struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005461{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005462 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005463 {
5464 relaxed,
5465 preferred,
5466 strict
5467 };
5468
Howard Hinnant49e145e2012-10-30 19:06:59 +00005469 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005470
Howard Hinnant756c69b2010-09-22 16:48:34 +00005471 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005472 pointer_safety() : __v_() {}
5473
5474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005475 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005477 operator int() const {return __v_;}
5478};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005479#endif
5480
5481#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005482 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005483_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5484#else
5485// This function is only offered in C++03 under ABI v1.
5486# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5487inline _LIBCPP_INLINE_VISIBILITY
5488pointer_safety get_pointer_safety() _NOEXCEPT {
5489 return pointer_safety::relaxed;
5490}
5491# endif
5492#endif
5493
Howard Hinnantc51e1022010-05-11 19:42:16 +00005494
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005495_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5496_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5497_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005498_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005499
5500template <class _Tp>
5501inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005502_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005503undeclare_reachable(_Tp* __p)
5504{
5505 return static_cast<_Tp*>(__undeclare_reachable(__p));
5506}
5507
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005508_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005509
Marshall Clow8982dcd2015-07-13 20:04:56 +00005510// --- Helper for container swap --
5511template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005512inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005513void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5514#if _LIBCPP_STD_VER >= 14
5515 _NOEXCEPT
5516#else
5517 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5518#endif
5519{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005520 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005521 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5522}
5523
5524template <typename _Alloc>
5525_LIBCPP_INLINE_VISIBILITY
5526void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5527#if _LIBCPP_STD_VER >= 14
5528 _NOEXCEPT
5529#else
5530 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5531#endif
5532{
5533 using _VSTD::swap;
5534 swap(__a1, __a2);
5535}
5536
5537template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005538inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005539void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5540
Marshall Clowff91de82015-08-18 19:51:37 +00005541template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005542struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005543 _Traits::propagate_on_container_move_assignment::value
5544#if _LIBCPP_STD_VER > 14
5545 || _Traits::is_always_equal::value
5546#else
5547 && is_nothrow_move_assignable<_Alloc>::value
5548#endif
5549 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005550
Marshall Clowa591b9a2016-07-11 21:38:08 +00005551
5552#ifndef _LIBCPP_HAS_NO_VARIADICS
5553template <class _Tp, class _Alloc>
5554struct __temp_value {
5555 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005556
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005557 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005558 _Alloc &__a;
5559
5560 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5561 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005562
Marshall Clowa591b9a2016-07-11 21:38:08 +00005563 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005564 _LIBCPP_NO_CFI
5565 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5566 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5567 _VSTD::forward<_Args>(__args)...);
5568 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005569
Marshall Clowa591b9a2016-07-11 21:38:08 +00005570 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5571 };
5572#endif
5573
Marshall Clowe46031a2018-07-02 18:41:15 +00005574template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005575struct __is_allocator : false_type {};
5576
5577template<typename _Alloc>
5578struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005579 typename __void_t<typename _Alloc::value_type>::type,
5580 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5581 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005582 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005583
Eric Fiselier74ebee62019-06-08 01:31:19 +00005584// __builtin_new_allocator -- A non-templated helper for allocating and
5585// deallocating memory using __builtin_operator_new and
5586// __builtin_operator_delete. It should be used in preference to
5587// `std::allocator<T>` to avoid additional instantiations.
5588struct __builtin_new_allocator {
5589 struct __builtin_new_deleter {
5590 typedef void* pointer_type;
5591
5592 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5593 : __size_(__size), __align_(__align) {}
5594
5595 void operator()(void* p) const _NOEXCEPT {
5596 std::__libcpp_deallocate(p, __size_, __align_);
5597 }
5598
5599 private:
5600 size_t __size_;
5601 size_t __align_;
5602 };
5603
5604 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5605
5606 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5607 return __holder_t(std::__libcpp_allocate(__s, __align),
5608 __builtin_new_deleter(__s, __align));
5609 }
5610
5611 static void __deallocate_bytes(void* __p, size_t __s,
5612 size_t __align) _NOEXCEPT {
5613 std::__libcpp_deallocate(__p, __s, __align);
5614 }
5615
5616 template <class _Tp>
5617 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5618 static __holder_t __allocate_type(size_t __n) {
5619 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5620 }
5621
5622 template <class _Tp>
5623 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5624 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5625 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5626 }
5627};
5628
5629
Howard Hinnantc51e1022010-05-11 19:42:16 +00005630_LIBCPP_END_NAMESPACE_STD
5631
Eric Fiselierf4433a32017-05-31 22:07:49 +00005632_LIBCPP_POP_MACROS
5633
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005634#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005635# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005636#endif
5637
Howard Hinnantc51e1022010-05-11 19:42:16 +00005638#endif // _LIBCPP_MEMORY