blob: ec1f3d91403ebea8db256cfe72f92f4f4a885afa [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
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001511struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512{
1513 typedef _Alloc allocator_type;
1514 typedef typename allocator_type::value_type value_type;
1515
1516 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1517 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1518 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1519 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1520
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001521 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1522 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
1524 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1525 propagate_on_container_copy_assignment;
1526 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1527 propagate_on_container_move_assignment;
1528 typedef typename __propagate_on_container_swap<allocator_type>::type
1529 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001530 typedef typename __is_always_equal<allocator_type>::type
1531 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001533#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001535 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001536 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538 template <class _Tp> struct rebind_alloc
1539 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1540 template <class _Tp> struct rebind_traits
1541 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001542#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543
Marshall Clow0e58cae2017-11-26 02:55:38 +00001544 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 static pointer allocate(allocator_type& __a, size_type __n)
1546 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001547 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001549 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1551
Howard Hinnant756c69b2010-09-22 16:48:34 +00001552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001553 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 {__a.deallocate(__p, __n);}
1555
1556#ifndef _LIBCPP_HAS_NO_VARIADICS
1557 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001560 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001561 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001562#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001564 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001565 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 {
1567 ::new ((void*)__p) _Tp();
1568 }
1569 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001570 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001571 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001573 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1574 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 }
1576 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001577 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001578 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 const _A1& __a1)
1580 {
1581 ::new ((void*)__p) _Tp(__a0, __a1);
1582 }
1583 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001584 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001585 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 const _A1& __a1, const _A2& __a2)
1587 {
1588 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1589 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001590#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591
1592 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 static void destroy(allocator_type& __a, _Tp* __p)
1595 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1596
Howard Hinnant756c69b2010-09-22 16:48:34 +00001597 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001598 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1600
Howard Hinnant756c69b2010-09-22 16:48:34 +00001601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 static allocator_type
1603 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001604 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 __has_select_on_container_copy_construction<const allocator_type>(),
1606 __a);}
1607
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001608 template <class _Ptr>
1609 _LIBCPP_INLINE_VISIBILITY
1610 static
1611 void
Eric Fiselier84de91c2019-08-30 19:01:03 +00001612 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001613 {
Louis Dionne301a6772018-12-07 16:42:28 +00001614 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselier84de91c2019-08-30 19:01:03 +00001615 construct(__a, _VSTD::__to_raw_pointer(__begin2),
1616#ifdef _LIBCPP_NO_EXCEPTIONS
1617 _VSTD::move(*__begin1)
1618#else
1619 _VSTD::move_if_noexcept(*__begin1)
1620#endif
1621 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001622 }
1623
1624 template <class _Tp>
1625 _LIBCPP_INLINE_VISIBILITY
1626 static
1627 typename enable_if
1628 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001629 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1631 is_trivially_move_constructible<_Tp>::value,
1632 void
1633 >::type
Eric Fiselier84de91c2019-08-30 19:01:03 +00001634 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001635 {
1636 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001637 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001638 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001639 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001640 __begin2 += _Np;
1641 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001642 }
1643
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001644 template <class _Iter, class _Ptr>
1645 _LIBCPP_INLINE_VISIBILITY
1646 static
1647 void
1648 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1649 {
1650 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1651 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1652 }
1653
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001654 template <class _SourceTp, class _DestTp,
1655 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1656 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001657 _LIBCPP_INLINE_VISIBILITY
1658 static
1659 typename enable_if
1660 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001661 is_trivially_move_constructible<_DestTp>::value &&
1662 is_same<_RawSourceTp, _RawDestTp>::value &&
1663 (__is_default_allocator<allocator_type>::value ||
1664 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001665 void
1666 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001667 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001668 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001669 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001670 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001671 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001672 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001673 __begin2 += _Np;
1674 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001675 }
1676
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 template <class _Ptr>
1678 _LIBCPP_INLINE_VISIBILITY
1679 static
1680 void
Eric Fiselier84de91c2019-08-30 19:01:03 +00001681 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001682 {
1683 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001684 {
Eric Fiselier84de91c2019-08-30 19:01:03 +00001685 construct(__a, _VSTD::__to_raw_pointer(__end2 - 1),
1686#ifdef _LIBCPP_NO_EXCEPTIONS
1687 _VSTD::move(*--__end1)
1688#else
1689 _VSTD::move_if_noexcept(*--__end1)
1690#endif
1691 );
1692 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001693 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001694 }
1695
1696 template <class _Tp>
1697 _LIBCPP_INLINE_VISIBILITY
1698 static
1699 typename enable_if
1700 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001701 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001702 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1703 is_trivially_move_constructible<_Tp>::value,
1704 void
1705 >::type
Eric Fiselier84de91c2019-08-30 19:01:03 +00001706 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001707 {
1708 ptrdiff_t _Np = __end1 - __begin1;
1709 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001710 if (_Np > 0)
1711 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001712 }
1713
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714private:
1715
Howard Hinnant756c69b2010-09-22 16:48:34 +00001716 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001717 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 const_void_pointer __hint, true_type)
1719 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001720 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001721 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001722 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 {return __a.allocate(__n);}
1724
1725#ifndef _LIBCPP_HAS_NO_VARIADICS
1726 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001729 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1733 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001734 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001736#else // _LIBCPP_HAS_NO_VARIADICS
1737 template <class _Tp, class _A0>
1738 _LIBCPP_INLINE_VISIBILITY
1739 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1740 const _A0& __a0)
1741 {__a.construct(__p, __a0);}
1742 template <class _Tp, class _A0>
1743 _LIBCPP_INLINE_VISIBILITY
1744 static void __construct(false_type, allocator_type&, _Tp* __p,
1745 const _A0& __a0)
1746 {
1747 ::new ((void*)__p) _Tp(__a0);
1748 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001749#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750
1751 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1754 {__a.destroy(__p);}
1755 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 static void __destroy(false_type, allocator_type&, _Tp* __p)
1758 {
1759 __p->~_Tp();
1760 }
1761
Howard Hinnant756c69b2010-09-22 16:48:34 +00001762 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001763 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001765 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001766 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001767 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768
Howard Hinnant756c69b2010-09-22 16:48:34 +00001769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001771 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001775 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 {return __a;}
1777};
1778
Marshall Clow940e01c2015-04-07 05:21:38 +00001779template <class _Traits, class _Tp>
1780struct __rebind_alloc_helper
1781{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001782#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001783 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001784#else
1785 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1786#endif
1787};
1788
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789// allocator
1790
1791template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001792class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793{
1794public:
1795 typedef size_t size_type;
1796 typedef ptrdiff_t difference_type;
1797 typedef _Tp* pointer;
1798 typedef const _Tp* const_pointer;
1799 typedef _Tp& reference;
1800 typedef const _Tp& const_reference;
1801 typedef _Tp value_type;
1802
Howard Hinnant4931e092011-06-02 21:38:57 +00001803 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001804 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001805
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1807
Marshall Clowbc759762018-03-20 23:02:53 +00001808 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1809 allocator() _NOEXCEPT {}
1810
Louis Dionne481a2662018-09-23 18:35:00 +00001811 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001812 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1813 allocator(const allocator<_Up>&) _NOEXCEPT {}
1814
Howard Hinnant719bda32011-05-28 14:41:13 +00001815 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001816 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001817 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001818 {return _VSTD::addressof(__x);}
Louis Dionne481a2662018-09-23 18:35:00 +00001819 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0e58cae2017-11-26 02:55:38 +00001820 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001821 {
1822 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001823 __throw_length_error("allocator<T>::allocate(size_t n)"
1824 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001825 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001826 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001827 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001828 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001829 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1830 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001831#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 template <class _Up, class... _Args>
1833 _LIBCPP_INLINE_VISIBILITY
1834 void
1835 construct(_Up* __p, _Args&&... __args)
1836 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001837 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001839#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840 _LIBCPP_INLINE_VISIBILITY
1841 void
1842 construct(pointer __p)
1843 {
1844 ::new((void*)__p) _Tp();
1845 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001846# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001847
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 template <class _A0>
1849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001850 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 construct(pointer __p, _A0& __a0)
1852 {
1853 ::new((void*)__p) _Tp(__a0);
1854 }
1855 template <class _A0>
1856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001857 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 construct(pointer __p, const _A0& __a0)
1859 {
1860 ::new((void*)__p) _Tp(__a0);
1861 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001862# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 template <class _A0, class _A1>
1864 _LIBCPP_INLINE_VISIBILITY
1865 void
1866 construct(pointer __p, _A0& __a0, _A1& __a1)
1867 {
1868 ::new((void*)__p) _Tp(__a0, __a1);
1869 }
1870 template <class _A0, class _A1>
1871 _LIBCPP_INLINE_VISIBILITY
1872 void
1873 construct(pointer __p, const _A0& __a0, _A1& __a1)
1874 {
1875 ::new((void*)__p) _Tp(__a0, __a1);
1876 }
1877 template <class _A0, class _A1>
1878 _LIBCPP_INLINE_VISIBILITY
1879 void
1880 construct(pointer __p, _A0& __a0, const _A1& __a1)
1881 {
1882 ::new((void*)__p) _Tp(__a0, __a1);
1883 }
1884 template <class _A0, class _A1>
1885 _LIBCPP_INLINE_VISIBILITY
1886 void
1887 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1888 {
1889 ::new((void*)__p) _Tp(__a0, __a1);
1890 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001891#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1893};
1894
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001895template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001896class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001897{
1898public:
1899 typedef size_t size_type;
1900 typedef ptrdiff_t difference_type;
1901 typedef const _Tp* pointer;
1902 typedef const _Tp* const_pointer;
1903 typedef const _Tp& reference;
1904 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001905 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001906
1907 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001908 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001909
1910 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1911
Marshall Clowbc759762018-03-20 23:02:53 +00001912 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1913 allocator() _NOEXCEPT {}
1914
1915 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001916 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001917 allocator(const allocator<_Up>&) _NOEXCEPT {}
1918
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1920 {return _VSTD::addressof(__x);}
1921 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001922 {
1923 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001924 __throw_length_error("allocator<const T>::allocate(size_t n)"
1925 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001926 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001927 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001928 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001929 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001930 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1931 {return size_type(~0) / sizeof(_Tp);}
1932#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1933 template <class _Up, class... _Args>
1934 _LIBCPP_INLINE_VISIBILITY
1935 void
1936 construct(_Up* __p, _Args&&... __args)
1937 {
1938 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1939 }
1940#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1941 _LIBCPP_INLINE_VISIBILITY
1942 void
1943 construct(pointer __p)
1944 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001945 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001946 }
1947# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001948
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001949 template <class _A0>
1950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001951 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001952 construct(pointer __p, _A0& __a0)
1953 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001954 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001955 }
1956 template <class _A0>
1957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001958 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001959 construct(pointer __p, const _A0& __a0)
1960 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001961 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001962 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001963# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1964 template <class _A0, class _A1>
1965 _LIBCPP_INLINE_VISIBILITY
1966 void
1967 construct(pointer __p, _A0& __a0, _A1& __a1)
1968 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001969 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001970 }
1971 template <class _A0, class _A1>
1972 _LIBCPP_INLINE_VISIBILITY
1973 void
1974 construct(pointer __p, const _A0& __a0, _A1& __a1)
1975 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001976 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001977 }
1978 template <class _A0, class _A1>
1979 _LIBCPP_INLINE_VISIBILITY
1980 void
1981 construct(pointer __p, _A0& __a0, const _A1& __a1)
1982 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001983 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001984 }
1985 template <class _A0, class _A1>
1986 _LIBCPP_INLINE_VISIBILITY
1987 void
1988 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1989 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001990 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001991 }
1992#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1993 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1994};
1995
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996template <class _Tp, class _Up>
1997inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001998bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999
2000template <class _Tp, class _Up>
2001inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002002bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003
2004template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002005class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006 : public iterator<output_iterator_tag,
2007 _Tp, // purposefully not C++03
2008 ptrdiff_t, // purposefully not C++03
2009 _Tp*, // purposefully not C++03
2010 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2011{
2012private:
2013 _OutputIterator __x_;
2014public:
2015 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2016 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2017 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002018 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002019#if _LIBCPP_STD_VER >= 14
2020 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002021 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002022#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2024 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2025 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002026#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002027 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002028#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029};
2030
2031template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002032_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002034get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035{
2036 pair<_Tp*, ptrdiff_t> __r(0, 0);
2037 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2038 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2039 / sizeof(_Tp);
2040 if (__n > __m)
2041 __n = __m;
2042 while (__n > 0)
2043 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002044#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002045 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002046 {
2047 std::align_val_t __al =
2048 std::align_val_t(std::alignment_of<_Tp>::value);
2049 __r.first = static_cast<_Tp*>(::operator new(
2050 __n * sizeof(_Tp), __al, nothrow));
2051 } else {
2052 __r.first = static_cast<_Tp*>(::operator new(
2053 __n * sizeof(_Tp), nothrow));
2054 }
2055#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002056 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002057 {
2058 // Since aligned operator new is unavailable, return an empty
2059 // buffer rather than one with invalid alignment.
2060 return __r;
2061 }
2062
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002064#endif
2065
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066 if (__r.first)
2067 {
2068 __r.second = __n;
2069 break;
2070 }
2071 __n /= 2;
2072 }
2073 return __r;
2074}
2075
2076template <class _Tp>
2077inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002078void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2079{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002080 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002081}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082
Marshall Clowb22274f2017-01-24 22:22:33 +00002083#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002085struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086{
2087 _Tp* __ptr_;
2088};
2089
2090template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002091class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092{
2093private:
2094 _Tp* __ptr_;
2095public:
2096 typedef _Tp element_type;
2097
2098 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2099 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2100 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2101 : __ptr_(__p.release()) {}
2102 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2103 {reset(__p.release()); return *this;}
2104 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2105 {reset(__p.release()); return *this;}
2106 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2107 {reset(__p.__ptr_); return *this;}
2108 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2109
2110 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2111 {return *__ptr_;}
2112 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2113 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2114 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2115 {
2116 _Tp* __t = __ptr_;
2117 __ptr_ = 0;
2118 return __t;
2119 }
2120 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2121 {
2122 if (__ptr_ != __p)
2123 delete __ptr_;
2124 __ptr_ = __p;
2125 }
2126
2127 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2128 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2129 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2130 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2131 {return auto_ptr<_Up>(release());}
2132};
2133
2134template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002135class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136{
2137public:
2138 typedef void element_type;
2139};
Marshall Clowb22274f2017-01-24 22:22:33 +00002140#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141
Eric Fiselier9d355982017-04-12 23:45:53 +00002142template <class _Tp, int _Idx,
2143 bool _CanBeEmptyBase =
2144 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2145struct __compressed_pair_elem {
2146 typedef _Tp _ParamT;
2147 typedef _Tp& reference;
2148 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149
Eric Fiselier9d355982017-04-12 23:45:53 +00002150#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002151 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152
Eric Fiselier9d355982017-04-12 23:45:53 +00002153 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002154 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2155 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002156 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002157 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002158 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002159 : __value_(_VSTD::forward<_Up>(__u))
2160 {
2161 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162
Eric Fiselier9d355982017-04-12 23:45:53 +00002163 template <class... _Args, size_t... _Indexes>
2164 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2165 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2166 __tuple_indices<_Indexes...>)
2167 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2168#else
Alex Lorenz76132112017-11-09 17:54:49 +00002169 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2170 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002171 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2172#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173
Alex Lorenz76132112017-11-09 17:54:49 +00002174 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2175 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002176 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002179 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180};
2181
Eric Fiselier9d355982017-04-12 23:45:53 +00002182template <class _Tp, int _Idx>
2183struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2184 typedef _Tp _ParamT;
2185 typedef _Tp& reference;
2186 typedef const _Tp& const_reference;
2187 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188
Eric Fiselier9d355982017-04-12 23:45:53 +00002189#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002190 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191
Eric Fiselier9d355982017-04-12 23:45:53 +00002192 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002193 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2194 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002195 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002196 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002197 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002198 : __value_type(_VSTD::forward<_Up>(__u))
2199 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200
Eric Fiselier9d355982017-04-12 23:45:53 +00002201 template <class... _Args, size_t... _Indexes>
2202 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2203 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2204 __tuple_indices<_Indexes...>)
2205 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2206#else
Alex Lorenz76132112017-11-09 17:54:49 +00002207 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2208 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002209 __compressed_pair_elem(_ParamT __p)
2210 : __value_type(std::forward<_ParamT>(__p)) {}
2211#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212
Alex Lorenz76132112017-11-09 17:54:49 +00002213 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2214 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002215 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216};
2217
Eric Fiselier9d355982017-04-12 23:45:53 +00002218// Tag used to construct the second element of the compressed pair.
2219struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
2221template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002222class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2223 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002224 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2225 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002226
2227 // NOTE: This static assert should never fire because __compressed_pair
2228 // is *almost never* used in a scenario where it's possible for T1 == T2.
2229 // (The exception is std::function where it is possible that the function
2230 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002231 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002232 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2233 "The current implementation is NOT ABI-compatible with the previous "
2234 "implementation for this configuration");
2235
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002237#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002238 template <bool _Dummy = true,
2239 class = typename enable_if<
2240 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2241 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2242 >::type
2243 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002244 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002245 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246
Eric Fiselier9d355982017-04-12 23:45:53 +00002247 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2248 __compressed_pair>::value,
2249 bool>::type = true>
2250 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2251 __compressed_pair(_Tp&& __t)
2252 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253
Eric Fiselier9d355982017-04-12 23:45:53 +00002254 template <class _Tp>
2255 _LIBCPP_INLINE_VISIBILITY constexpr
2256 __compressed_pair(__second_tag, _Tp&& __t)
2257 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258
Eric Fiselier9d355982017-04-12 23:45:53 +00002259 template <class _U1, class _U2>
2260 _LIBCPP_INLINE_VISIBILITY constexpr
2261 __compressed_pair(_U1&& __t1, _U2&& __t2)
2262 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263
Eric Fiselier9d355982017-04-12 23:45:53 +00002264 template <class... _Args1, class... _Args2>
2265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2266 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2267 tuple<_Args2...> __second_args)
2268 : _Base1(__pc, _VSTD::move(__first_args),
2269 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2270 _Base2(__pc, _VSTD::move(__second_args),
2271 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002272
Eric Fiselier9d355982017-04-12 23:45:53 +00002273#else
2274 _LIBCPP_INLINE_VISIBILITY
2275 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002276
Eric Fiselier9d355982017-04-12 23:45:53 +00002277 _LIBCPP_INLINE_VISIBILITY explicit
2278 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002279
Eric Fiselier9d355982017-04-12 23:45:53 +00002280 _LIBCPP_INLINE_VISIBILITY
2281 __compressed_pair(__second_tag, _T2 __t2)
2282 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283
Eric Fiselier9d355982017-04-12 23:45:53 +00002284 _LIBCPP_INLINE_VISIBILITY
2285 __compressed_pair(_T1 __t1, _T2 __t2)
2286 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2287#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288
Eric Fiselier9d355982017-04-12 23:45:53 +00002289 _LIBCPP_INLINE_VISIBILITY
2290 typename _Base1::reference first() _NOEXCEPT {
2291 return static_cast<_Base1&>(*this).__get();
2292 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293
Eric Fiselier9d355982017-04-12 23:45:53 +00002294 _LIBCPP_INLINE_VISIBILITY
2295 typename _Base1::const_reference first() const _NOEXCEPT {
2296 return static_cast<_Base1 const&>(*this).__get();
2297 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298
Eric Fiselier9d355982017-04-12 23:45:53 +00002299 _LIBCPP_INLINE_VISIBILITY
2300 typename _Base2::reference second() _NOEXCEPT {
2301 return static_cast<_Base2&>(*this).__get();
2302 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303
Eric Fiselier9d355982017-04-12 23:45:53 +00002304 _LIBCPP_INLINE_VISIBILITY
2305 typename _Base2::const_reference second() const _NOEXCEPT {
2306 return static_cast<_Base2 const&>(*this).__get();
2307 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308
Eric Fiselier9d355982017-04-12 23:45:53 +00002309 _LIBCPP_INLINE_VISIBILITY
2310 void swap(__compressed_pair& __x)
2311 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2312 __is_nothrow_swappable<_T2>::value)
2313 {
2314 using std::swap;
2315 swap(first(), __x.first());
2316 swap(second(), __x.second());
2317 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318};
2319
2320template <class _T1, class _T2>
2321inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002322void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2323 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2324 __is_nothrow_swappable<_T2>::value) {
2325 __x.swap(__y);
2326}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002328// default_delete
2329
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002331struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002332 static_assert(!is_function<_Tp>::value,
2333 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002334#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002335 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002336#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002337 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002338#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002339 template <class _Up>
2340 _LIBCPP_INLINE_VISIBILITY
2341 default_delete(const default_delete<_Up>&,
2342 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2343 0) _NOEXCEPT {}
2344
2345 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2346 static_assert(sizeof(_Tp) > 0,
2347 "default_delete can not delete incomplete type");
2348 static_assert(!is_void<_Tp>::value,
2349 "default_delete can not delete incomplete type");
2350 delete __ptr;
2351 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352};
2353
2354template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002355struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2356private:
2357 template <class _Up>
2358 struct _EnableIfConvertible
2359 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2360
Howard Hinnant4500ca52011-12-18 21:19:44 +00002361public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002362#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002363 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002364#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002365 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002366#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002367
2368 template <class _Up>
2369 _LIBCPP_INLINE_VISIBILITY
2370 default_delete(const default_delete<_Up[]>&,
2371 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2372
2373 template <class _Up>
2374 _LIBCPP_INLINE_VISIBILITY
2375 typename _EnableIfConvertible<_Up>::type
2376 operator()(_Up* __ptr) const _NOEXCEPT {
2377 static_assert(sizeof(_Tp) > 0,
2378 "default_delete can not delete incomplete type");
2379 static_assert(!is_void<_Tp>::value,
2380 "default_delete can not delete void type");
2381 delete[] __ptr;
2382 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383};
2384
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002385template <class _Deleter>
2386struct __unique_ptr_deleter_sfinae {
2387 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2388 typedef const _Deleter& __lval_ref_type;
2389 typedef _Deleter&& __good_rval_ref_type;
2390 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391};
2392
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002393template <class _Deleter>
2394struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2395 typedef const _Deleter& __lval_ref_type;
2396 typedef const _Deleter&& __bad_rval_ref_type;
2397 typedef false_type __enable_rval_overload;
2398};
2399
2400template <class _Deleter>
2401struct __unique_ptr_deleter_sfinae<_Deleter&> {
2402 typedef _Deleter& __lval_ref_type;
2403 typedef _Deleter&& __bad_rval_ref_type;
2404 typedef false_type __enable_rval_overload;
2405};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002406
2407template <class _Tp, class _Dp = default_delete<_Tp> >
2408class _LIBCPP_TEMPLATE_VIS unique_ptr {
2409public:
2410 typedef _Tp element_type;
2411 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002412 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002413
2414 static_assert(!is_rvalue_reference<deleter_type>::value,
2415 "the specified deleter type cannot be an rvalue reference");
2416
2417private:
2418 __compressed_pair<pointer, deleter_type> __ptr_;
2419
2420 struct __nat { int __for_bool_; };
2421
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002422 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002423
2424 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002425 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002426 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002427
2428 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002429 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002430 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002431
2432 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002433 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002434 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002435
2436 template <bool _Dummy, class _Deleter = typename __dependent_type<
2437 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002438 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002439 typename enable_if<is_default_constructible<_Deleter>::value &&
2440 !is_pointer<_Deleter>::value>::type;
2441
2442 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002443 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002444 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2445
2446 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002447 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002448 is_convertible<typename _UPtr::pointer, pointer>::value &&
2449 !is_array<_Up>::value
2450 >::type;
2451
2452 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002453 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002454 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2455 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2456 >::type;
2457
2458 template <class _UDel>
2459 using _EnableIfDeleterAssignable = typename enable_if<
2460 is_assignable<_Dp&, _UDel&&>::value
2461 >::type;
2462
2463public:
2464 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002465 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002466 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002467 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002468
2469 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002470 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002471 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002472 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002473
2474 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002475 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002476 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002477 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002478
2479 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002480 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002481 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002482 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002483 : __ptr_(__p, __d) {}
2484
2485 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002486 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002487 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002488 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002489 : __ptr_(__p, _VSTD::move(__d)) {
2490 static_assert(!is_reference<deleter_type>::value,
2491 "rvalue deleter bound to reference");
2492 }
2493
2494 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002495 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002496 _LIBCPP_INLINE_VISIBILITY
2497 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2498
2499 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002500 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002501 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2502 }
2503
2504 template <class _Up, class _Ep,
2505 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2506 class = _EnableIfDeleterConvertible<_Ep>
2507 >
2508 _LIBCPP_INLINE_VISIBILITY
2509 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2510 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2511
2512#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2513 template <class _Up>
2514 _LIBCPP_INLINE_VISIBILITY
2515 unique_ptr(auto_ptr<_Up>&& __p,
2516 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002517 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002518 __nat>::type = __nat()) _NOEXCEPT
2519 : __ptr_(__p.release()) {}
2520#endif
2521
2522 _LIBCPP_INLINE_VISIBILITY
2523 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2524 reset(__u.release());
2525 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2526 return *this;
2527 }
2528
2529 template <class _Up, class _Ep,
2530 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2531 class = _EnableIfDeleterAssignable<_Ep>
2532 >
2533 _LIBCPP_INLINE_VISIBILITY
2534 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2535 reset(__u.release());
2536 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2537 return *this;
2538 }
2539
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002540#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2541 template <class _Up>
2542 _LIBCPP_INLINE_VISIBILITY
2543 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2544 is_same<_Dp, default_delete<_Tp> >::value,
2545 unique_ptr&>::type
2546 operator=(auto_ptr<_Up> __p) {
2547 reset(__p.release());
2548 return *this;
2549 }
2550#endif
2551
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002552#ifdef _LIBCPP_CXX03_LANG
2553 unique_ptr(unique_ptr const&) = delete;
2554 unique_ptr& operator=(unique_ptr const&) = delete;
2555#endif
2556
2557
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002558 _LIBCPP_INLINE_VISIBILITY
2559 ~unique_ptr() { reset(); }
2560
2561 _LIBCPP_INLINE_VISIBILITY
2562 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2563 reset();
2564 return *this;
2565 }
2566
2567 _LIBCPP_INLINE_VISIBILITY
2568 typename add_lvalue_reference<_Tp>::type
2569 operator*() const {
2570 return *__ptr_.first();
2571 }
2572 _LIBCPP_INLINE_VISIBILITY
2573 pointer operator->() const _NOEXCEPT {
2574 return __ptr_.first();
2575 }
2576 _LIBCPP_INLINE_VISIBILITY
2577 pointer get() const _NOEXCEPT {
2578 return __ptr_.first();
2579 }
2580 _LIBCPP_INLINE_VISIBILITY
2581 deleter_type& get_deleter() _NOEXCEPT {
2582 return __ptr_.second();
2583 }
2584 _LIBCPP_INLINE_VISIBILITY
2585 const deleter_type& get_deleter() const _NOEXCEPT {
2586 return __ptr_.second();
2587 }
2588 _LIBCPP_INLINE_VISIBILITY
2589 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2590 return __ptr_.first() != nullptr;
2591 }
2592
2593 _LIBCPP_INLINE_VISIBILITY
2594 pointer release() _NOEXCEPT {
2595 pointer __t = __ptr_.first();
2596 __ptr_.first() = pointer();
2597 return __t;
2598 }
2599
2600 _LIBCPP_INLINE_VISIBILITY
2601 void reset(pointer __p = pointer()) _NOEXCEPT {
2602 pointer __tmp = __ptr_.first();
2603 __ptr_.first() = __p;
2604 if (__tmp)
2605 __ptr_.second()(__tmp);
2606 }
2607
2608 _LIBCPP_INLINE_VISIBILITY
2609 void swap(unique_ptr& __u) _NOEXCEPT {
2610 __ptr_.swap(__u.__ptr_);
2611 }
2612};
2613
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002614
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002616class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002618 typedef _Tp element_type;
2619 typedef _Dp deleter_type;
2620 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2621
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002623 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624
Eric Fiselier31127cd2017-04-16 02:14:31 +00002625 template <class _From>
2626 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627
Eric Fiselier31127cd2017-04-16 02:14:31 +00002628 template <class _FromElem>
2629 struct _CheckArrayPointerConversion<_FromElem*>
2630 : integral_constant<bool,
2631 is_same<_FromElem*, pointer>::value ||
2632 (is_same<pointer, element_type*>::value &&
2633 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2634 >
2635 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636
Eric Fiselier31127cd2017-04-16 02:14:31 +00002637 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002638
2639 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002640 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002641 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002642
2643 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002644 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002645 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002646
2647 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002648 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002649 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002650
2651 template <bool _Dummy, class _Deleter = typename __dependent_type<
2652 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002653 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002654 typename enable_if<is_default_constructible<_Deleter>::value &&
2655 !is_pointer<_Deleter>::value>::type;
2656
2657 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002658 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002659 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2660
2661 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002662 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002663 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002664 >::type;
2665
2666 template <class _UPtr, class _Up,
2667 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002668 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002669 is_array<_Up>::value &&
2670 is_same<pointer, element_type*>::value &&
2671 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2672 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2673 >::type;
2674
2675 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002676 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002677 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2678 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2679 >::type;
2680
2681 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002682 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002683 is_assignable<_Dp&, _UDel&&>::value
2684 >::type;
2685
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002687 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002688 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002689 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002690 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002692 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002693 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002694 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002695 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002697 template <class _Pp, bool _Dummy = true,
2698 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002699 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002700 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002701 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002702 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002704 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002705 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2706 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002707 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002708 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002709 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002711 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002712 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002713 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002714 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002718 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2719 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002720 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002721 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002722 : __ptr_(__p, _VSTD::move(__d)) {
2723 static_assert(!is_reference<deleter_type>::value,
2724 "rvalue deleter bound to reference");
2725 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002728 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002729 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002730 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002731 : __ptr_(nullptr, _VSTD::move(__d)) {
2732 static_assert(!is_reference<deleter_type>::value,
2733 "rvalue deleter bound to reference");
2734 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002735
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002736 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002737 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2738 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002739 _LIBCPP_INLINE_VISIBILITY
2740 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002741
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002742 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002743 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2745 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002746
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002747 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002748 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002749 reset(__u.release());
2750 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2751 return *this;
2752 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754 template <class _Up, class _Ep,
2755 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2756 class = _EnableIfDeleterConvertible<_Ep>
2757 >
2758 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002759 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002760 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002761 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002763 template <class _Up, class _Ep,
2764 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2765 class = _EnableIfDeleterAssignable<_Ep>
2766 >
2767 _LIBCPP_INLINE_VISIBILITY
2768 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002769 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002770 reset(__u.release());
2771 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2772 return *this;
2773 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002775#ifdef _LIBCPP_CXX03_LANG
2776 unique_ptr(unique_ptr const&) = delete;
2777 unique_ptr& operator=(unique_ptr const&) = delete;
2778#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002779
2780public:
2781 _LIBCPP_INLINE_VISIBILITY
2782 ~unique_ptr() { reset(); }
2783
2784 _LIBCPP_INLINE_VISIBILITY
2785 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2786 reset();
2787 return *this;
2788 }
2789
2790 _LIBCPP_INLINE_VISIBILITY
2791 typename add_lvalue_reference<_Tp>::type
2792 operator[](size_t __i) const {
2793 return __ptr_.first()[__i];
2794 }
2795 _LIBCPP_INLINE_VISIBILITY
2796 pointer get() const _NOEXCEPT {
2797 return __ptr_.first();
2798 }
2799
2800 _LIBCPP_INLINE_VISIBILITY
2801 deleter_type& get_deleter() _NOEXCEPT {
2802 return __ptr_.second();
2803 }
2804
2805 _LIBCPP_INLINE_VISIBILITY
2806 const deleter_type& get_deleter() const _NOEXCEPT {
2807 return __ptr_.second();
2808 }
2809 _LIBCPP_INLINE_VISIBILITY
2810 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2811 return __ptr_.first() != nullptr;
2812 }
2813
2814 _LIBCPP_INLINE_VISIBILITY
2815 pointer release() _NOEXCEPT {
2816 pointer __t = __ptr_.first();
2817 __ptr_.first() = pointer();
2818 return __t;
2819 }
2820
2821 template <class _Pp>
2822 _LIBCPP_INLINE_VISIBILITY
2823 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002824 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002825 >::type
2826 reset(_Pp __p) _NOEXCEPT {
2827 pointer __tmp = __ptr_.first();
2828 __ptr_.first() = __p;
2829 if (__tmp)
2830 __ptr_.second()(__tmp);
2831 }
2832
2833 _LIBCPP_INLINE_VISIBILITY
2834 void reset(nullptr_t = nullptr) _NOEXCEPT {
2835 pointer __tmp = __ptr_.first();
2836 __ptr_.first() = nullptr;
2837 if (__tmp)
2838 __ptr_.second()(__tmp);
2839 }
2840
2841 _LIBCPP_INLINE_VISIBILITY
2842 void swap(unique_ptr& __u) _NOEXCEPT {
2843 __ptr_.swap(__u.__ptr_);
2844 }
2845
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846};
2847
2848template <class _Tp, class _Dp>
2849inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002850typename enable_if<
2851 __is_swappable<_Dp>::value,
2852 void
2853>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002854swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855
2856template <class _T1, class _D1, class _T2, class _D2>
2857inline _LIBCPP_INLINE_VISIBILITY
2858bool
2859operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2860
2861template <class _T1, class _D1, class _T2, class _D2>
2862inline _LIBCPP_INLINE_VISIBILITY
2863bool
2864operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2865
2866template <class _T1, class _D1, class _T2, class _D2>
2867inline _LIBCPP_INLINE_VISIBILITY
2868bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002869operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2870{
2871 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2872 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002873 typedef typename common_type<_P1, _P2>::type _Vp;
2874 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002875}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876
2877template <class _T1, class _D1, class _T2, class _D2>
2878inline _LIBCPP_INLINE_VISIBILITY
2879bool
2880operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2881
2882template <class _T1, class _D1, class _T2, class _D2>
2883inline _LIBCPP_INLINE_VISIBILITY
2884bool
2885operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2886
2887template <class _T1, class _D1, class _T2, class _D2>
2888inline _LIBCPP_INLINE_VISIBILITY
2889bool
2890operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2891
Howard Hinnantb17caf92012-02-21 21:02:58 +00002892template <class _T1, class _D1>
2893inline _LIBCPP_INLINE_VISIBILITY
2894bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002895operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002896{
2897 return !__x;
2898}
2899
2900template <class _T1, class _D1>
2901inline _LIBCPP_INLINE_VISIBILITY
2902bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002903operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002904{
2905 return !__x;
2906}
2907
2908template <class _T1, class _D1>
2909inline _LIBCPP_INLINE_VISIBILITY
2910bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002911operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002912{
2913 return static_cast<bool>(__x);
2914}
2915
2916template <class _T1, class _D1>
2917inline _LIBCPP_INLINE_VISIBILITY
2918bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002919operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002920{
2921 return static_cast<bool>(__x);
2922}
2923
2924template <class _T1, class _D1>
2925inline _LIBCPP_INLINE_VISIBILITY
2926bool
2927operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2928{
2929 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2930 return less<_P1>()(__x.get(), nullptr);
2931}
2932
2933template <class _T1, class _D1>
2934inline _LIBCPP_INLINE_VISIBILITY
2935bool
2936operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2937{
2938 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2939 return less<_P1>()(nullptr, __x.get());
2940}
2941
2942template <class _T1, class _D1>
2943inline _LIBCPP_INLINE_VISIBILITY
2944bool
2945operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2946{
2947 return nullptr < __x;
2948}
2949
2950template <class _T1, class _D1>
2951inline _LIBCPP_INLINE_VISIBILITY
2952bool
2953operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2954{
2955 return __x < nullptr;
2956}
2957
2958template <class _T1, class _D1>
2959inline _LIBCPP_INLINE_VISIBILITY
2960bool
2961operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2962{
2963 return !(nullptr < __x);
2964}
2965
2966template <class _T1, class _D1>
2967inline _LIBCPP_INLINE_VISIBILITY
2968bool
2969operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2970{
2971 return !(__x < nullptr);
2972}
2973
2974template <class _T1, class _D1>
2975inline _LIBCPP_INLINE_VISIBILITY
2976bool
2977operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2978{
2979 return !(__x < nullptr);
2980}
2981
2982template <class _T1, class _D1>
2983inline _LIBCPP_INLINE_VISIBILITY
2984bool
2985operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2986{
2987 return !(nullptr < __x);
2988}
2989
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002990#if _LIBCPP_STD_VER > 11
2991
2992template<class _Tp>
2993struct __unique_if
2994{
2995 typedef unique_ptr<_Tp> __unique_single;
2996};
2997
2998template<class _Tp>
2999struct __unique_if<_Tp[]>
3000{
3001 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3002};
3003
3004template<class _Tp, size_t _Np>
3005struct __unique_if<_Tp[_Np]>
3006{
3007 typedef void __unique_array_known_bound;
3008};
3009
3010template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003011inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003012typename __unique_if<_Tp>::__unique_single
3013make_unique(_Args&&... __args)
3014{
3015 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3016}
3017
3018template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003019inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003020typename __unique_if<_Tp>::__unique_array_unknown_bound
3021make_unique(size_t __n)
3022{
3023 typedef typename remove_extent<_Tp>::type _Up;
3024 return unique_ptr<_Tp>(new _Up[__n]());
3025}
3026
3027template<class _Tp, class... _Args>
3028 typename __unique_if<_Tp>::__unique_array_known_bound
3029 make_unique(_Args&&...) = delete;
3030
3031#endif // _LIBCPP_STD_VER > 11
3032
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003033template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003034#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003035struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003036#else
3037struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003038 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003039#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003040{
3041 typedef unique_ptr<_Tp, _Dp> argument_type;
3042 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003043 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003044 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003045 {
3046 typedef typename argument_type::pointer pointer;
3047 return hash<pointer>()(__ptr.get());
3048 }
3049};
3050
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051struct __destruct_n
3052{
3053private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003054 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055
3056 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003057 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003058 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059
3060 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003061 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 {}
3063
Howard Hinnant719bda32011-05-28 14:41:13 +00003064 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003065 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003066 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067 {}
3068
Howard Hinnant719bda32011-05-28 14:41:13 +00003069 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003070 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003071 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072 {}
3073public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003074 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003075 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076
3077 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003078 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003079 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080
3081 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003082 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003083 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084
3085 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003086 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003087 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088};
3089
3090template <class _Alloc>
3091class __allocator_destructor
3092{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003093 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003095 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3096 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097private:
3098 _Alloc& __alloc_;
3099 size_type __s_;
3100public:
3101 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003102 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003105 void operator()(pointer __p) _NOEXCEPT
3106 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107};
3108
3109template <class _InputIterator, class _ForwardIterator>
3110_ForwardIterator
3111uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3112{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003114#ifndef _LIBCPP_NO_EXCEPTIONS
3115 _ForwardIterator __s = __r;
3116 try
3117 {
3118#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003119 for (; __f != __l; ++__f, (void) ++__r)
3120 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003121#ifndef _LIBCPP_NO_EXCEPTIONS
3122 }
3123 catch (...)
3124 {
3125 for (; __s != __r; ++__s)
3126 __s->~value_type();
3127 throw;
3128 }
3129#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 return __r;
3131}
3132
3133template <class _InputIterator, class _Size, class _ForwardIterator>
3134_ForwardIterator
3135uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3136{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003138#ifndef _LIBCPP_NO_EXCEPTIONS
3139 _ForwardIterator __s = __r;
3140 try
3141 {
3142#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003143 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3144 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003145#ifndef _LIBCPP_NO_EXCEPTIONS
3146 }
3147 catch (...)
3148 {
3149 for (; __s != __r; ++__s)
3150 __s->~value_type();
3151 throw;
3152 }
3153#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154 return __r;
3155}
3156
3157template <class _ForwardIterator, class _Tp>
3158void
3159uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3160{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003162#ifndef _LIBCPP_NO_EXCEPTIONS
3163 _ForwardIterator __s = __f;
3164 try
3165 {
3166#endif
3167 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003168 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003169#ifndef _LIBCPP_NO_EXCEPTIONS
3170 }
3171 catch (...)
3172 {
3173 for (; __s != __f; ++__s)
3174 __s->~value_type();
3175 throw;
3176 }
3177#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178}
3179
3180template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003181_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3183{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003185#ifndef _LIBCPP_NO_EXCEPTIONS
3186 _ForwardIterator __s = __f;
3187 try
3188 {
3189#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003190 for (; __n > 0; ++__f, (void) --__n)
3191 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003192#ifndef _LIBCPP_NO_EXCEPTIONS
3193 }
3194 catch (...)
3195 {
3196 for (; __s != __f; ++__s)
3197 __s->~value_type();
3198 throw;
3199 }
3200#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003201 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202}
3203
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003204#if _LIBCPP_STD_VER > 14
3205
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003206template <class _Tp>
3207inline _LIBCPP_INLINE_VISIBILITY
3208void destroy_at(_Tp* __loc) {
3209 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3210 __loc->~_Tp();
3211}
3212
3213template <class _ForwardIterator>
3214inline _LIBCPP_INLINE_VISIBILITY
3215void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3216 for (; __first != __last; ++__first)
3217 _VSTD::destroy_at(_VSTD::addressof(*__first));
3218}
3219
3220template <class _ForwardIterator, class _Size>
3221inline _LIBCPP_INLINE_VISIBILITY
3222_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3223 for (; __n > 0; (void)++__first, --__n)
3224 _VSTD::destroy_at(_VSTD::addressof(*__first));
3225 return __first;
3226}
3227
Eric Fiselier290c07c2016-10-11 21:13:44 +00003228template <class _ForwardIterator>
3229inline _LIBCPP_INLINE_VISIBILITY
3230void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3231 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3232 auto __idx = __first;
3233#ifndef _LIBCPP_NO_EXCEPTIONS
3234 try {
3235#endif
3236 for (; __idx != __last; ++__idx)
3237 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3238#ifndef _LIBCPP_NO_EXCEPTIONS
3239 } catch (...) {
3240 _VSTD::destroy(__first, __idx);
3241 throw;
3242 }
3243#endif
3244}
3245
3246template <class _ForwardIterator, class _Size>
3247inline _LIBCPP_INLINE_VISIBILITY
3248_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3249 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3250 auto __idx = __first;
3251#ifndef _LIBCPP_NO_EXCEPTIONS
3252 try {
3253#endif
3254 for (; __n > 0; (void)++__idx, --__n)
3255 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3256 return __idx;
3257#ifndef _LIBCPP_NO_EXCEPTIONS
3258 } catch (...) {
3259 _VSTD::destroy(__first, __idx);
3260 throw;
3261 }
3262#endif
3263}
3264
3265
3266template <class _ForwardIterator>
3267inline _LIBCPP_INLINE_VISIBILITY
3268void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3269 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3270 auto __idx = __first;
3271#ifndef _LIBCPP_NO_EXCEPTIONS
3272 try {
3273#endif
3274 for (; __idx != __last; ++__idx)
3275 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3276#ifndef _LIBCPP_NO_EXCEPTIONS
3277 } catch (...) {
3278 _VSTD::destroy(__first, __idx);
3279 throw;
3280 }
3281#endif
3282}
3283
3284template <class _ForwardIterator, class _Size>
3285inline _LIBCPP_INLINE_VISIBILITY
3286_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3287 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3288 auto __idx = __first;
3289#ifndef _LIBCPP_NO_EXCEPTIONS
3290 try {
3291#endif
3292 for (; __n > 0; (void)++__idx, --__n)
3293 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3294 return __idx;
3295#ifndef _LIBCPP_NO_EXCEPTIONS
3296 } catch (...) {
3297 _VSTD::destroy(__first, __idx);
3298 throw;
3299 }
3300#endif
3301}
3302
3303
3304template <class _InputIt, class _ForwardIt>
3305inline _LIBCPP_INLINE_VISIBILITY
3306_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3307 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3308 auto __idx = __first_res;
3309#ifndef _LIBCPP_NO_EXCEPTIONS
3310 try {
3311#endif
3312 for (; __first != __last; (void)++__idx, ++__first)
3313 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3314 return __idx;
3315#ifndef _LIBCPP_NO_EXCEPTIONS
3316 } catch (...) {
3317 _VSTD::destroy(__first_res, __idx);
3318 throw;
3319 }
3320#endif
3321}
3322
3323template <class _InputIt, class _Size, class _ForwardIt>
3324inline _LIBCPP_INLINE_VISIBILITY
3325pair<_InputIt, _ForwardIt>
3326uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3327 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3328 auto __idx = __first_res;
3329#ifndef _LIBCPP_NO_EXCEPTIONS
3330 try {
3331#endif
3332 for (; __n > 0; ++__idx, (void)++__first, --__n)
3333 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3334 return {__first, __idx};
3335#ifndef _LIBCPP_NO_EXCEPTIONS
3336 } catch (...) {
3337 _VSTD::destroy(__first_res, __idx);
3338 throw;
3339 }
3340#endif
3341}
3342
3343
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003344#endif // _LIBCPP_STD_VER > 14
3345
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003346// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3347// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003348// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003349#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3350 && defined(__ATOMIC_RELAXED) \
3351 && defined(__ATOMIC_ACQ_REL)
3352# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003353#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003354# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3355#endif
3356
3357template <class _Tp>
3358inline _LIBCPP_INLINE_VISIBILITY _Tp
3359__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3360{
3361#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3362 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3363#else
3364 return __t += 1;
3365#endif
3366}
3367
3368template <class _Tp>
3369inline _LIBCPP_INLINE_VISIBILITY _Tp
3370__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3371{
3372#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3373 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3374#else
3375 return __t -= 1;
3376#endif
3377}
3378
Howard Hinnant756c69b2010-09-22 16:48:34 +00003379class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380 : public std::exception
3381{
3382public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003383 virtual ~bad_weak_ptr() _NOEXCEPT;
3384 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385};
3386
Louis Dionne16fe2952018-07-11 23:14:33 +00003387_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003388void __throw_bad_weak_ptr()
3389{
3390#ifndef _LIBCPP_NO_EXCEPTIONS
3391 throw bad_weak_ptr();
3392#else
3393 _VSTD::abort();
3394#endif
3395}
3396
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003397template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003399class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400{
3401 __shared_count(const __shared_count&);
3402 __shared_count& operator=(const __shared_count&);
3403
3404protected:
3405 long __shared_owners_;
3406 virtual ~__shared_count();
3407private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003408 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409
3410public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003412 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413 : __shared_owners_(__refs) {}
3414
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003415#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003416 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003417 void __add_shared() _NOEXCEPT;
3418 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003419#else
3420 _LIBCPP_INLINE_VISIBILITY
3421 void __add_shared() _NOEXCEPT {
3422 __libcpp_atomic_refcount_increment(__shared_owners_);
3423 }
3424 _LIBCPP_INLINE_VISIBILITY
3425 bool __release_shared() _NOEXCEPT {
3426 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3427 __on_zero_shared();
3428 return true;
3429 }
3430 return false;
3431 }
3432#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003433 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003434 long use_count() const _NOEXCEPT {
3435 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3436 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437};
3438
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003439class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440 : private __shared_count
3441{
3442 long __shared_weak_owners_;
3443
3444public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003446 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447 : __shared_count(__refs),
3448 __shared_weak_owners_(__refs) {}
3449protected:
3450 virtual ~__shared_weak_count();
3451
3452public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003453#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003454 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003455 void __add_shared() _NOEXCEPT;
3456 void __add_weak() _NOEXCEPT;
3457 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003458#else
3459 _LIBCPP_INLINE_VISIBILITY
3460 void __add_shared() _NOEXCEPT {
3461 __shared_count::__add_shared();
3462 }
3463 _LIBCPP_INLINE_VISIBILITY
3464 void __add_weak() _NOEXCEPT {
3465 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3466 }
3467 _LIBCPP_INLINE_VISIBILITY
3468 void __release_shared() _NOEXCEPT {
3469 if (__shared_count::__release_shared())
3470 __release_weak();
3471 }
3472#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003473 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003475 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3476 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477
Howard Hinnant807d6332013-02-25 15:50:36 +00003478 // Define the function out only if we build static libc++ without RTTI.
3479 // Otherwise we may break clients who need to compile their projects with
3480 // -fno-rtti and yet link against a libc++.dylib compiled
3481 // without -fno-rtti.
3482#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003483 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003484#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003486 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487};
3488
3489template <class _Tp, class _Dp, class _Alloc>
3490class __shared_ptr_pointer
3491 : public __shared_weak_count
3492{
3493 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3494public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003497 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498
Howard Hinnant72f73582010-08-11 17:04:31 +00003499#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003500 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003501#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502
3503private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003504 virtual void __on_zero_shared() _NOEXCEPT;
3505 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506};
3507
Howard Hinnant72f73582010-08-11 17:04:31 +00003508#ifndef _LIBCPP_NO_RTTI
3509
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510template <class _Tp, class _Dp, class _Alloc>
3511const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003512__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003514 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515}
3516
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003517#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003518
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519template <class _Tp, class _Dp, class _Alloc>
3520void
Howard Hinnant719bda32011-05-28 14:41:13 +00003521__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522{
3523 __data_.first().second()(__data_.first().first());
3524 __data_.first().second().~_Dp();
3525}
3526
3527template <class _Tp, class _Dp, class _Alloc>
3528void
Howard Hinnant719bda32011-05-28 14:41:13 +00003529__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003531 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3532 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003533 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3534
Eric Fiselierf8898c82015-02-05 23:01:40 +00003535 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003537 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538}
3539
3540template <class _Tp, class _Alloc>
3541class __shared_ptr_emplace
3542 : public __shared_weak_count
3543{
3544 __compressed_pair<_Alloc, _Tp> __data_;
3545public:
3546#ifndef _LIBCPP_HAS_NO_VARIADICS
3547
Howard Hinnant756c69b2010-09-22 16:48:34 +00003548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003550 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551
3552 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003555 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3556 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557
3558#else // _LIBCPP_HAS_NO_VARIADICS
3559
Howard Hinnant756c69b2010-09-22 16:48:34 +00003560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561 __shared_ptr_emplace(_Alloc __a)
3562 : __data_(__a) {}
3563
3564 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3567 : __data_(__a, _Tp(__a0)) {}
3568
3569 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3572 : __data_(__a, _Tp(__a0, __a1)) {}
3573
3574 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3577 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3578
3579#endif // _LIBCPP_HAS_NO_VARIADICS
3580
3581private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003582 virtual void __on_zero_shared() _NOEXCEPT;
3583 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003585 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003586 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587};
3588
3589template <class _Tp, class _Alloc>
3590void
Howard Hinnant719bda32011-05-28 14:41:13 +00003591__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592{
3593 __data_.second().~_Tp();
3594}
3595
3596template <class _Tp, class _Alloc>
3597void
Howard Hinnant719bda32011-05-28 14:41:13 +00003598__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003600 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3601 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003602 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003603 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003605 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606}
3607
Erik Pilkington2a398762017-05-25 15:43:31 +00003608struct __shared_ptr_dummy_rebind_allocator_type;
3609template <>
3610class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3611{
3612public:
3613 template <class _Other>
3614 struct rebind
3615 {
3616 typedef allocator<_Other> other;
3617 };
3618};
3619
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003620template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621
3622template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003623class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003625public:
3626 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003627
Eric Fiselierae5b6672016-06-27 01:02:43 +00003628#if _LIBCPP_STD_VER > 14
3629 typedef weak_ptr<_Tp> weak_type;
3630#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631private:
3632 element_type* __ptr_;
3633 __shared_weak_count* __cntrl_;
3634
3635 struct __nat {int __for_bool_;};
3636public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003638 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003640 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003641 template<class _Yp>
3642 explicit shared_ptr(_Yp* __p,
3643 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3644 template<class _Yp, class _Dp>
3645 shared_ptr(_Yp* __p, _Dp __d,
3646 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3647 template<class _Yp, class _Dp, class _Alloc>
3648 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3649 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3651 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003652 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003654 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003658 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003659 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003660#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003662 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003663 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003664 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003665 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003666#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003668 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003669#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003670#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003671 template<class _Yp>
3672 shared_ptr(auto_ptr<_Yp>&& __r,
3673 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003674#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003675 template<class _Yp>
3676 shared_ptr(auto_ptr<_Yp> __r,
3677 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003679#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003680#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003681 template <class _Yp, class _Dp>
3682 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3683 typename enable_if
3684 <
3685 !is_lvalue_reference<_Dp>::value &&
3686 !is_array<_Yp>::value &&
3687 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3688 __nat
3689 >::type = __nat());
3690 template <class _Yp, class _Dp>
3691 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3692 typename enable_if
3693 <
3694 is_lvalue_reference<_Dp>::value &&
3695 !is_array<_Yp>::value &&
3696 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3697 __nat
3698 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003699#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003700 template <class _Yp, class _Dp>
3701 shared_ptr(unique_ptr<_Yp, _Dp>,
3702 typename enable_if
3703 <
3704 !is_lvalue_reference<_Dp>::value &&
3705 !is_array<_Yp>::value &&
3706 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3707 __nat
3708 >::type = __nat());
3709 template <class _Yp, class _Dp>
3710 shared_ptr(unique_ptr<_Yp, _Dp>,
3711 typename enable_if
3712 <
3713 is_lvalue_reference<_Dp>::value &&
3714 !is_array<_Yp>::value &&
3715 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3716 __nat
3717 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003718#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719
3720 ~shared_ptr();
3721
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003723 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003724 template<class _Yp>
3725 typename enable_if
3726 <
3727 is_convertible<_Yp*, element_type*>::value,
3728 shared_ptr&
3729 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003731 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003732#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003734 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003735 template<class _Yp>
3736 typename enable_if
3737 <
3738 is_convertible<_Yp*, element_type*>::value,
3739 shared_ptr<_Tp>&
3740 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003742 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003743#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003744 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003746 typename enable_if
3747 <
3748 !is_array<_Yp>::value &&
3749 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003750 shared_ptr
3751 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003752 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003753#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003754#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003755#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003756 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003758 typename enable_if
3759 <
3760 !is_array<_Yp>::value &&
3761 is_convertible<_Yp*, element_type*>::value,
3762 shared_ptr&
3763 >::type
3764 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003766#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003767 template <class _Yp, class _Dp>
3768 typename enable_if
3769 <
3770 !is_array<_Yp>::value &&
3771 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3772 shared_ptr&
3773 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003774#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003776 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003777#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003779 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780#endif
3781
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003783 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003785 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003786 template<class _Yp>
3787 typename enable_if
3788 <
3789 is_convertible<_Yp*, element_type*>::value,
3790 void
3791 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003793 reset(_Yp* __p);
3794 template<class _Yp, class _Dp>
3795 typename enable_if
3796 <
3797 is_convertible<_Yp*, element_type*>::value,
3798 void
3799 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003801 reset(_Yp* __p, _Dp __d);
3802 template<class _Yp, class _Dp, class _Alloc>
3803 typename enable_if
3804 <
3805 is_convertible<_Yp*, element_type*>::value,
3806 void
3807 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003809 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810
Howard Hinnant756c69b2010-09-22 16:48:34 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003812 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003814 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3815 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003817 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003819 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003821 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003823 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003824 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003825 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003826 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003828 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003829 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003830 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003831 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003832 _LIBCPP_INLINE_VISIBILITY
3833 bool
3834 __owner_equivalent(const shared_ptr& __p) const
3835 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836
Howard Hinnant72f73582010-08-11 17:04:31 +00003837#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003840 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003841 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003842 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003843 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003844#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845
Zoe Carver6cd05c32019-08-19 15:47:16 +00003846#ifndef _LIBCPP_HAS_NO_VARIADICS
3847
3848 template<class ..._Args>
3849 static
3850 shared_ptr<_Tp>
3851 make_shared(_Args&& ...__args);
3852
3853 template<class _Alloc, class ..._Args>
3854 static
3855 shared_ptr<_Tp>
3856 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3857
3858#else // _LIBCPP_HAS_NO_VARIADICS
3859
3860 static shared_ptr<_Tp> make_shared();
3861
3862 template<class _A0>
3863 static shared_ptr<_Tp> make_shared(_A0&);
3864
3865 template<class _A0, class _A1>
3866 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3867
3868 template<class _A0, class _A1, class _A2>
3869 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3870
3871 template<class _Alloc>
3872 static shared_ptr<_Tp>
3873 allocate_shared(const _Alloc& __a);
3874
3875 template<class _Alloc, class _A0>
3876 static shared_ptr<_Tp>
3877 allocate_shared(const _Alloc& __a, _A0& __a0);
3878
3879 template<class _Alloc, class _A0, class _A1>
3880 static shared_ptr<_Tp>
3881 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3882
3883 template<class _Alloc, class _A0, class _A1, class _A2>
3884 static shared_ptr<_Tp>
3885 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3886
3887#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888
3889private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003890 template <class _Yp, bool = is_function<_Yp>::value>
3891 struct __shared_ptr_default_allocator
3892 {
3893 typedef allocator<_Yp> type;
3894 };
3895
3896 template <class _Yp>
3897 struct __shared_ptr_default_allocator<_Yp, true>
3898 {
3899 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3900 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003902 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003903 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003904 typename enable_if<is_convertible<_OrigPtr*,
3905 const enable_shared_from_this<_Yp>*
3906 >::value,
3907 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003908 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3909 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003910 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003911 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003912 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003913 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003914 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3915 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003916 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917 }
3918
Erik Pilkington2a398762017-05-25 15:43:31 +00003919 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003921 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3922 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003923};
3924
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003925
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003927inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003928_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003929shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930 : __ptr_(0),
3931 __cntrl_(0)
3932{
3933}
3934
3935template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003936inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003937_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003938shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939 : __ptr_(0),
3940 __cntrl_(0)
3941{
3942}
3943
3944template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003945template<class _Yp>
3946shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3947 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948 : __ptr_(__p)
3949{
3950 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003951 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3952 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3953 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003955 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956}
3957
3958template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003959template<class _Yp, class _Dp>
3960shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3961 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962 : __ptr_(__p)
3963{
3964#ifndef _LIBCPP_NO_EXCEPTIONS
3965 try
3966 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003967#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003968 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3969 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3970 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003971 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972#ifndef _LIBCPP_NO_EXCEPTIONS
3973 }
3974 catch (...)
3975 {
3976 __d(__p);
3977 throw;
3978 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003979#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980}
3981
3982template<class _Tp>
3983template<class _Dp>
3984shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3985 : __ptr_(0)
3986{
3987#ifndef _LIBCPP_NO_EXCEPTIONS
3988 try
3989 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003990#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003991 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3992 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3993 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994#ifndef _LIBCPP_NO_EXCEPTIONS
3995 }
3996 catch (...)
3997 {
3998 __d(__p);
3999 throw;
4000 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004001#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002}
4003
4004template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004005template<class _Yp, class _Dp, class _Alloc>
4006shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4007 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008 : __ptr_(__p)
4009{
4010#ifndef _LIBCPP_NO_EXCEPTIONS
4011 try
4012 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004013#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004015 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016 typedef __allocator_destructor<_A2> _D2;
4017 _A2 __a2(__a);
4018 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004019 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4020 _CntrlBlk(__p, __d, __a);
4021 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004022 __enable_weak_this(__p, __p);
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>
4034template<class _Dp, class _Alloc>
4035shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4036 : __ptr_(0)
4037{
4038#ifndef _LIBCPP_NO_EXCEPTIONS
4039 try
4040 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004041#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004043 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044 typedef __allocator_destructor<_A2> _D2;
4045 _A2 __a2(__a);
4046 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004047 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4048 _CntrlBlk(__p, __d, __a);
4049 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050#ifndef _LIBCPP_NO_EXCEPTIONS
4051 }
4052 catch (...)
4053 {
4054 __d(__p);
4055 throw;
4056 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004057#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058}
4059
4060template<class _Tp>
4061template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004062inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004063shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064 : __ptr_(__p),
4065 __cntrl_(__r.__cntrl_)
4066{
4067 if (__cntrl_)
4068 __cntrl_->__add_shared();
4069}
4070
4071template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004072inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004073shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074 : __ptr_(__r.__ptr_),
4075 __cntrl_(__r.__cntrl_)
4076{
4077 if (__cntrl_)
4078 __cntrl_->__add_shared();
4079}
4080
4081template<class _Tp>
4082template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004083inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004085 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004086 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087 : __ptr_(__r.__ptr_),
4088 __cntrl_(__r.__cntrl_)
4089{
4090 if (__cntrl_)
4091 __cntrl_->__add_shared();
4092}
4093
Howard Hinnant74279a52010-09-04 23:28:19 +00004094#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095
4096template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004097inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004098shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099 : __ptr_(__r.__ptr_),
4100 __cntrl_(__r.__cntrl_)
4101{
4102 __r.__ptr_ = 0;
4103 __r.__cntrl_ = 0;
4104}
4105
4106template<class _Tp>
4107template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004108inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004110 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004111 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112 : __ptr_(__r.__ptr_),
4113 __cntrl_(__r.__cntrl_)
4114{
4115 __r.__ptr_ = 0;
4116 __r.__cntrl_ = 0;
4117}
4118
Howard Hinnant74279a52010-09-04 23:28:19 +00004119#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120
Marshall Clowb22274f2017-01-24 22:22:33 +00004121#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004123template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004124#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004125shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004127shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004129 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130 : __ptr_(__r.get())
4131{
4132 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4133 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004134 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135 __r.release();
4136}
Marshall Clowb22274f2017-01-24 22:22:33 +00004137#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138
4139template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004140template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004141#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4143#else
4144shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4145#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004146 typename enable_if
4147 <
4148 !is_lvalue_reference<_Dp>::value &&
4149 !is_array<_Yp>::value &&
4150 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4151 __nat
4152 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153 : __ptr_(__r.get())
4154{
Marshall Clow35cde742015-05-10 13:59:45 +00004155#if _LIBCPP_STD_VER > 11
4156 if (__ptr_ == nullptr)
4157 __cntrl_ = nullptr;
4158 else
4159#endif
4160 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004161 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4162 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4163 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004164 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004165 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166 __r.release();
4167}
4168
4169template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004170template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004171#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4173#else
4174shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4175#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004176 typename enable_if
4177 <
4178 is_lvalue_reference<_Dp>::value &&
4179 !is_array<_Yp>::value &&
4180 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4181 __nat
4182 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183 : __ptr_(__r.get())
4184{
Marshall Clow35cde742015-05-10 13:59:45 +00004185#if _LIBCPP_STD_VER > 11
4186 if (__ptr_ == nullptr)
4187 __cntrl_ = nullptr;
4188 else
4189#endif
4190 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004191 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004192 typedef __shared_ptr_pointer<_Yp*,
4193 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004194 _AllocT > _CntrlBlk;
4195 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004196 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004197 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198 __r.release();
4199}
4200
Zoe Carver6cd05c32019-08-19 15:47:16 +00004201#ifndef _LIBCPP_HAS_NO_VARIADICS
4202
4203template<class _Tp>
4204template<class ..._Args>
4205shared_ptr<_Tp>
4206shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4207{
4208 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared" );
4209 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4210 typedef allocator<_CntrlBlk> _A2;
4211 typedef __allocator_destructor<_A2> _D2;
4212 _A2 __a2;
4213 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4214 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4215 shared_ptr<_Tp> __r;
4216 __r.__ptr_ = __hold2.get()->get();
4217 __r.__cntrl_ = __hold2.release();
4218 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4219 return __r;
4220}
4221
4222template<class _Tp>
4223template<class _Alloc, class ..._Args>
4224shared_ptr<_Tp>
4225shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4226{
4227 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
4228 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4229 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4230 typedef __allocator_destructor<_A2> _D2;
4231 _A2 __a2(__a);
4232 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4233 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4234 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4235 shared_ptr<_Tp> __r;
4236 __r.__ptr_ = __hold2.get()->get();
4237 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4238 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4239 return __r;
4240}
4241
4242#else // _LIBCPP_HAS_NO_VARIADICS
4243
4244template<class _Tp>
4245shared_ptr<_Tp>
4246shared_ptr<_Tp>::make_shared()
4247{
4248 static_assert((is_constructible<_Tp>::value), "Can't construct object in make_shared" );
4249 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4250 typedef allocator<_CntrlBlk> _Alloc2;
4251 typedef __allocator_destructor<_Alloc2> _D2;
4252 _Alloc2 __alloc2;
4253 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4254 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4255 shared_ptr<_Tp> __r;
4256 __r.__ptr_ = __hold2.get()->get();
4257 __r.__cntrl_ = __hold2.release();
4258 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4259 return __r;
4260}
4261
4262template<class _Tp>
4263template<class _A0>
4264shared_ptr<_Tp>
4265shared_ptr<_Tp>::make_shared(_A0& __a0)
4266{
4267 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in make_shared" );
4268 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4269 typedef allocator<_CntrlBlk> _Alloc2;
4270 typedef __allocator_destructor<_Alloc2> _D2;
4271 _Alloc2 __alloc2;
4272 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4273 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4274 shared_ptr<_Tp> __r;
4275 __r.__ptr_ = __hold2.get()->get();
4276 __r.__cntrl_ = __hold2.release();
4277 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4278 return __r;
4279}
4280
4281template<class _Tp>
4282template<class _A0, class _A1>
4283shared_ptr<_Tp>
4284shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4285{
4286 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in make_shared" );
4287 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4288 typedef allocator<_CntrlBlk> _Alloc2;
4289 typedef __allocator_destructor<_Alloc2> _D2;
4290 _Alloc2 __alloc2;
4291 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4292 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4293 shared_ptr<_Tp> __r;
4294 __r.__ptr_ = __hold2.get()->get();
4295 __r.__cntrl_ = __hold2.release();
4296 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4297 return __r;
4298}
4299
4300template<class _Tp>
4301template<class _A0, class _A1, class _A2>
4302shared_ptr<_Tp>
4303shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4304{
4305 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in make_shared" );
4306 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4307 typedef allocator<_CntrlBlk> _Alloc2;
4308 typedef __allocator_destructor<_Alloc2> _D2;
4309 _Alloc2 __alloc2;
4310 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4311 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4312 shared_ptr<_Tp> __r;
4313 __r.__ptr_ = __hold2.get()->get();
4314 __r.__cntrl_ = __hold2.release();
4315 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4316 return __r;
4317}
4318
4319template<class _Tp>
4320template<class _Alloc>
4321shared_ptr<_Tp>
4322shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4323{
4324 static_assert((is_constructible<_Tp>::value), "Can't construct object in allocate_shared" );
4325 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4326 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4327 typedef __allocator_destructor<_Alloc2> _D2;
4328 _Alloc2 __alloc2(__a);
4329 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4330 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4331 _CntrlBlk(__a);
4332 shared_ptr<_Tp> __r;
4333 __r.__ptr_ = __hold2.get()->get();
4334 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4335 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4336 return __r;
4337}
4338
4339template<class _Tp>
4340template<class _Alloc, class _A0>
4341shared_ptr<_Tp>
4342shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4343{
4344 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in allocate_shared" );
4345 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4346 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4347 typedef __allocator_destructor<_Alloc2> _D2;
4348 _Alloc2 __alloc2(__a);
4349 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4350 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4351 _CntrlBlk(__a, __a0);
4352 shared_ptr<_Tp> __r;
4353 __r.__ptr_ = __hold2.get()->get();
4354 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4355 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4356 return __r;
4357}
4358
4359template<class _Tp>
4360template<class _Alloc, class _A0, class _A1>
4361shared_ptr<_Tp>
4362shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4363{
4364 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in allocate_shared" );
4365 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4366 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4367 typedef __allocator_destructor<_Alloc2> _D2;
4368 _Alloc2 __alloc2(__a);
4369 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4370 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4371 _CntrlBlk(__a, __a0, __a1);
4372 shared_ptr<_Tp> __r;
4373 __r.__ptr_ = __hold2.get()->get();
4374 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4375 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4376 return __r;
4377}
4378
4379template<class _Tp>
4380template<class _Alloc, class _A0, class _A1, class _A2>
4381shared_ptr<_Tp>
4382shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4383{
4384 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in allocate_shared" );
4385 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4386 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4387 typedef __allocator_destructor<_Alloc2> _D2;
4388 _Alloc2 __alloc2(__a);
4389 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4390 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4391 _CntrlBlk(__a, __a0, __a1, __a2);
4392 shared_ptr<_Tp> __r;
4393 __r.__ptr_ = __hold2.get()->get();
4394 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4395 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4396 return __r;
4397}
4398
4399#endif // _LIBCPP_HAS_NO_VARIADICS
4400
Howard Hinnantc51e1022010-05-11 19:42:16 +00004401template<class _Tp>
4402shared_ptr<_Tp>::~shared_ptr()
4403{
4404 if (__cntrl_)
4405 __cntrl_->__release_shared();
4406}
4407
4408template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004409inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004410shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004411shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004412{
4413 shared_ptr(__r).swap(*this);
4414 return *this;
4415}
4416
4417template<class _Tp>
4418template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004419inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004420typename enable_if
4421<
Marshall Clow7e384b72017-01-10 16:59:33 +00004422 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004423 shared_ptr<_Tp>&
4424>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004425shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004426{
4427 shared_ptr(__r).swap(*this);
4428 return *this;
4429}
4430
Howard Hinnant74279a52010-09-04 23:28:19 +00004431#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432
4433template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004434inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004436shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004438 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004439 return *this;
4440}
4441
4442template<class _Tp>
4443template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004444inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004445typename enable_if
4446<
Marshall Clow7e384b72017-01-10 16:59:33 +00004447 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004448 shared_ptr<_Tp>&
4449>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4451{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004452 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004453 return *this;
4454}
4455
Marshall Clowb22274f2017-01-24 22:22:33 +00004456#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004457template<class _Tp>
4458template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004459inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004460typename enable_if
4461<
4462 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004463 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004464 shared_ptr<_Tp>
4465>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4467{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004468 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004469 return *this;
4470}
Marshall Clowb22274f2017-01-24 22:22:33 +00004471#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004472
4473template<class _Tp>
4474template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004475inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004476typename enable_if
4477<
4478 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004479 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004480 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004481 shared_ptr<_Tp>&
4482>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004483shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4484{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004485 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486 return *this;
4487}
4488
Howard Hinnant74279a52010-09-04 23:28:19 +00004489#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490
Marshall Clowb22274f2017-01-24 22:22:33 +00004491#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004492template<class _Tp>
4493template<class _Yp>
4494inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004495typename enable_if
4496<
4497 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004498 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004499 shared_ptr<_Tp>&
4500>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004501shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004502{
4503 shared_ptr(__r).swap(*this);
4504 return *this;
4505}
Marshall Clowb22274f2017-01-24 22:22:33 +00004506#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507
4508template<class _Tp>
4509template <class _Yp, class _Dp>
4510inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004511typename enable_if
4512<
4513 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004514 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004515 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004516 shared_ptr<_Tp>&
4517>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004518shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4519{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004520 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521 return *this;
4522}
4523
Howard Hinnant74279a52010-09-04 23:28:19 +00004524#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004525
4526template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004527inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528void
Howard Hinnant719bda32011-05-28 14:41:13 +00004529shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004531 _VSTD::swap(__ptr_, __r.__ptr_);
4532 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533}
4534
4535template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004536inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004537void
Howard Hinnant719bda32011-05-28 14:41:13 +00004538shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539{
4540 shared_ptr().swap(*this);
4541}
4542
4543template<class _Tp>
4544template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004545inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004546typename enable_if
4547<
Marshall Clow7e384b72017-01-10 16:59:33 +00004548 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004549 void
4550>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004551shared_ptr<_Tp>::reset(_Yp* __p)
4552{
4553 shared_ptr(__p).swap(*this);
4554}
4555
4556template<class _Tp>
4557template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004558inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004559typename enable_if
4560<
Marshall Clow7e384b72017-01-10 16:59:33 +00004561 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004562 void
4563>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004564shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4565{
4566 shared_ptr(__p, __d).swap(*this);
4567}
4568
4569template<class _Tp>
4570template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004571inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004572typename enable_if
4573<
Marshall Clow7e384b72017-01-10 16:59:33 +00004574 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004575 void
4576>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4578{
4579 shared_ptr(__p, __d, __a).swap(*this);
4580}
4581
4582#ifndef _LIBCPP_HAS_NO_VARIADICS
4583
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004584template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004585inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004586typename enable_if
4587<
4588 !is_array<_Tp>::value,
4589 shared_ptr<_Tp>
4590>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004591make_shared(_Args&& ...__args)
4592{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004593 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004594}
4595
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004596template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004598typename enable_if
4599<
4600 !is_array<_Tp>::value,
4601 shared_ptr<_Tp>
4602>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004603allocate_shared(const _Alloc& __a, _Args&& ...__args)
4604{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004605 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004606}
4607
4608#else // _LIBCPP_HAS_NO_VARIADICS
4609
4610template<class _Tp>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004612shared_ptr<_Tp>
4613make_shared()
4614{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004615 return shared_ptr<_Tp>::make_shared();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004616}
4617
4618template<class _Tp, class _A0>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004619inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004620shared_ptr<_Tp>
4621make_shared(_A0& __a0)
4622{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004623 return shared_ptr<_Tp>::make_shared(__a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004624}
4625
4626template<class _Tp, class _A0, class _A1>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004627inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004628shared_ptr<_Tp>
4629make_shared(_A0& __a0, _A1& __a1)
4630{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004631 return shared_ptr<_Tp>::make_shared(__a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632}
4633
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004634template<class _Tp, class _A0, class _A1, class _A2>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004636shared_ptr<_Tp>
4637make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4638{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004639 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004640}
4641
4642template<class _Tp, class _Alloc>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004643inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644shared_ptr<_Tp>
4645allocate_shared(const _Alloc& __a)
4646{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004647 return shared_ptr<_Tp>::allocate_shared(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004648}
4649
4650template<class _Tp, class _Alloc, class _A0>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004651inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004652shared_ptr<_Tp>
4653allocate_shared(const _Alloc& __a, _A0& __a0)
4654{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004655 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004656}
4657
4658template<class _Tp, class _Alloc, class _A0, class _A1>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004659inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004660shared_ptr<_Tp>
4661allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4662{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004663 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004664}
4665
4666template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004667inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004668shared_ptr<_Tp>
4669allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4670{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004671 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004672}
4673
4674#endif // _LIBCPP_HAS_NO_VARIADICS
4675
4676template<class _Tp, class _Up>
4677inline _LIBCPP_INLINE_VISIBILITY
4678bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004679operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004680{
4681 return __x.get() == __y.get();
4682}
4683
4684template<class _Tp, class _Up>
4685inline _LIBCPP_INLINE_VISIBILITY
4686bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004687operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004688{
4689 return !(__x == __y);
4690}
4691
4692template<class _Tp, class _Up>
4693inline _LIBCPP_INLINE_VISIBILITY
4694bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004695operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004696{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004697#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004698 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4699 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004700#else
4701 return less<>()(__x.get(), __y.get());
4702#endif
4703
Howard Hinnantb17caf92012-02-21 21:02:58 +00004704}
4705
4706template<class _Tp, class _Up>
4707inline _LIBCPP_INLINE_VISIBILITY
4708bool
4709operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4710{
4711 return __y < __x;
4712}
4713
4714template<class _Tp, class _Up>
4715inline _LIBCPP_INLINE_VISIBILITY
4716bool
4717operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4718{
4719 return !(__y < __x);
4720}
4721
4722template<class _Tp, class _Up>
4723inline _LIBCPP_INLINE_VISIBILITY
4724bool
4725operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4726{
4727 return !(__x < __y);
4728}
4729
4730template<class _Tp>
4731inline _LIBCPP_INLINE_VISIBILITY
4732bool
4733operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4734{
4735 return !__x;
4736}
4737
4738template<class _Tp>
4739inline _LIBCPP_INLINE_VISIBILITY
4740bool
4741operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4742{
4743 return !__x;
4744}
4745
4746template<class _Tp>
4747inline _LIBCPP_INLINE_VISIBILITY
4748bool
4749operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4750{
4751 return static_cast<bool>(__x);
4752}
4753
4754template<class _Tp>
4755inline _LIBCPP_INLINE_VISIBILITY
4756bool
4757operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4758{
4759 return static_cast<bool>(__x);
4760}
4761
4762template<class _Tp>
4763inline _LIBCPP_INLINE_VISIBILITY
4764bool
4765operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4766{
4767 return less<_Tp*>()(__x.get(), nullptr);
4768}
4769
4770template<class _Tp>
4771inline _LIBCPP_INLINE_VISIBILITY
4772bool
4773operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4774{
4775 return less<_Tp*>()(nullptr, __x.get());
4776}
4777
4778template<class _Tp>
4779inline _LIBCPP_INLINE_VISIBILITY
4780bool
4781operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4782{
4783 return nullptr < __x;
4784}
4785
4786template<class _Tp>
4787inline _LIBCPP_INLINE_VISIBILITY
4788bool
4789operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4790{
4791 return __x < nullptr;
4792}
4793
4794template<class _Tp>
4795inline _LIBCPP_INLINE_VISIBILITY
4796bool
4797operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4798{
4799 return !(nullptr < __x);
4800}
4801
4802template<class _Tp>
4803inline _LIBCPP_INLINE_VISIBILITY
4804bool
4805operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4806{
4807 return !(__x < nullptr);
4808}
4809
4810template<class _Tp>
4811inline _LIBCPP_INLINE_VISIBILITY
4812bool
4813operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4814{
4815 return !(__x < nullptr);
4816}
4817
4818template<class _Tp>
4819inline _LIBCPP_INLINE_VISIBILITY
4820bool
4821operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4822{
4823 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004824}
4825
4826template<class _Tp>
4827inline _LIBCPP_INLINE_VISIBILITY
4828void
Howard Hinnant719bda32011-05-28 14:41:13 +00004829swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004830{
4831 __x.swap(__y);
4832}
4833
4834template<class _Tp, class _Up>
4835inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004836typename enable_if
4837<
4838 !is_array<_Tp>::value && !is_array<_Up>::value,
4839 shared_ptr<_Tp>
4840>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004841static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004842{
4843 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4844}
4845
4846template<class _Tp, class _Up>
4847inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004848typename enable_if
4849<
4850 !is_array<_Tp>::value && !is_array<_Up>::value,
4851 shared_ptr<_Tp>
4852>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004853dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004854{
4855 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4856 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4857}
4858
4859template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004860typename enable_if
4861<
4862 is_array<_Tp>::value == is_array<_Up>::value,
4863 shared_ptr<_Tp>
4864>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004865const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004866{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004867 typedef typename remove_extent<_Tp>::type _RTp;
4868 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869}
4870
Howard Hinnant72f73582010-08-11 17:04:31 +00004871#ifndef _LIBCPP_NO_RTTI
4872
Howard Hinnantc51e1022010-05-11 19:42:16 +00004873template<class _Dp, class _Tp>
4874inline _LIBCPP_INLINE_VISIBILITY
4875_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004876get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004877{
4878 return __p.template __get_deleter<_Dp>();
4879}
4880
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004881#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004882
Howard Hinnantc51e1022010-05-11 19:42:16 +00004883template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004884class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004885{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004886public:
4887 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004888private:
4889 element_type* __ptr_;
4890 __shared_weak_count* __cntrl_;
4891
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004892public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004894 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004895 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004896 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4897 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004899 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004900 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004901 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4902 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004903
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004904#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004906 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004907 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004908 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4909 _NOEXCEPT;
4910#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004911 ~weak_ptr();
4912
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004914 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004915 template<class _Yp>
4916 typename enable_if
4917 <
4918 is_convertible<_Yp*, element_type*>::value,
4919 weak_ptr&
4920 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004922 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4923
4924#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4925
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004927 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4928 template<class _Yp>
4929 typename enable_if
4930 <
4931 is_convertible<_Yp*, element_type*>::value,
4932 weak_ptr&
4933 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004935 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4936
4937#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4938
4939 template<class _Yp>
4940 typename enable_if
4941 <
4942 is_convertible<_Yp*, element_type*>::value,
4943 weak_ptr&
4944 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004946 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004947
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004949 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004951 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004952
Howard Hinnant756c69b2010-09-22 16:48:34 +00004953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004954 long use_count() const _NOEXCEPT
4955 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004957 bool expired() const _NOEXCEPT
4958 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4959 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004960 template<class _Up>
4961 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004962 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004964 template<class _Up>
4965 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004966 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004967 {return __cntrl_ < __r.__cntrl_;}
4968
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004969 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4970 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004971};
4972
4973template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004974inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004975_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004976weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004977 : __ptr_(0),
4978 __cntrl_(0)
4979{
4980}
4981
4982template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004983inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004984weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985 : __ptr_(__r.__ptr_),
4986 __cntrl_(__r.__cntrl_)
4987{
4988 if (__cntrl_)
4989 __cntrl_->__add_weak();
4990}
4991
4992template<class _Tp>
4993template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004994inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004995weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004996 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004997 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004998 : __ptr_(__r.__ptr_),
4999 __cntrl_(__r.__cntrl_)
5000{
5001 if (__cntrl_)
5002 __cntrl_->__add_weak();
5003}
5004
5005template<class _Tp>
5006template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005007inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005008weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005009 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005010 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005011 : __ptr_(__r.__ptr_),
5012 __cntrl_(__r.__cntrl_)
5013{
5014 if (__cntrl_)
5015 __cntrl_->__add_weak();
5016}
5017
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005018#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5019
5020template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005021inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005022weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5023 : __ptr_(__r.__ptr_),
5024 __cntrl_(__r.__cntrl_)
5025{
5026 __r.__ptr_ = 0;
5027 __r.__cntrl_ = 0;
5028}
5029
5030template<class _Tp>
5031template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005032inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005033weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5034 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5035 _NOEXCEPT
5036 : __ptr_(__r.__ptr_),
5037 __cntrl_(__r.__cntrl_)
5038{
5039 __r.__ptr_ = 0;
5040 __r.__cntrl_ = 0;
5041}
5042
5043#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5044
Howard Hinnantc51e1022010-05-11 19:42:16 +00005045template<class _Tp>
5046weak_ptr<_Tp>::~weak_ptr()
5047{
5048 if (__cntrl_)
5049 __cntrl_->__release_weak();
5050}
5051
5052template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005053inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005054weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005055weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005056{
5057 weak_ptr(__r).swap(*this);
5058 return *this;
5059}
5060
5061template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005062template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005063inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005064typename enable_if
5065<
5066 is_convertible<_Yp*, _Tp*>::value,
5067 weak_ptr<_Tp>&
5068>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005069weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005070{
5071 weak_ptr(__r).swap(*this);
5072 return *this;
5073}
5074
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005075#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5076
5077template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005078inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005079weak_ptr<_Tp>&
5080weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5081{
5082 weak_ptr(_VSTD::move(__r)).swap(*this);
5083 return *this;
5084}
5085
Howard Hinnantc51e1022010-05-11 19:42:16 +00005086template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005087template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005088inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005089typename enable_if
5090<
5091 is_convertible<_Yp*, _Tp*>::value,
5092 weak_ptr<_Tp>&
5093>::type
5094weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5095{
5096 weak_ptr(_VSTD::move(__r)).swap(*this);
5097 return *this;
5098}
5099
5100#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5101
5102template<class _Tp>
5103template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005104inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005105typename enable_if
5106<
5107 is_convertible<_Yp*, _Tp*>::value,
5108 weak_ptr<_Tp>&
5109>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005110weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005111{
5112 weak_ptr(__r).swap(*this);
5113 return *this;
5114}
5115
5116template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005117inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005118void
Howard Hinnant719bda32011-05-28 14:41:13 +00005119weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005120{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005121 _VSTD::swap(__ptr_, __r.__ptr_);
5122 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005123}
5124
5125template<class _Tp>
5126inline _LIBCPP_INLINE_VISIBILITY
5127void
Howard Hinnant719bda32011-05-28 14:41:13 +00005128swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005129{
5130 __x.swap(__y);
5131}
5132
5133template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005134inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005135void
Howard Hinnant719bda32011-05-28 14:41:13 +00005136weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005137{
5138 weak_ptr().swap(*this);
5139}
5140
5141template<class _Tp>
5142template<class _Yp>
5143shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005144 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005145 : __ptr_(__r.__ptr_),
5146 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5147{
5148 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005149 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005150}
5151
5152template<class _Tp>
5153shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005154weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005155{
5156 shared_ptr<_Tp> __r;
5157 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5158 if (__r.__cntrl_)
5159 __r.__ptr_ = __ptr_;
5160 return __r;
5161}
5162
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005163#if _LIBCPP_STD_VER > 14
5164template <class _Tp = void> struct owner_less;
5165#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005166template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005167#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005168
5169template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005170struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005171 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005172{
5173 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005174 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005175 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005176 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005177 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005178 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005179 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005180 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005181 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005182 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005183};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005184
5185template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005186struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005187 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5188{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005189 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005190 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005191 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005192 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005193 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005194 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005195 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005196 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005197 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005198 {return __x.owner_before(__y);}
5199};
5200
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005201#if _LIBCPP_STD_VER > 14
5202template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005203struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005204{
5205 template <class _Tp, class _Up>
5206 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005207 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005208 {return __x.owner_before(__y);}
5209 template <class _Tp, class _Up>
5210 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005211 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005212 {return __x.owner_before(__y);}
5213 template <class _Tp, class _Up>
5214 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005215 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005216 {return __x.owner_before(__y);}
5217 template <class _Tp, class _Up>
5218 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005219 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005220 {return __x.owner_before(__y);}
5221 typedef void is_transparent;
5222};
5223#endif
5224
Howard Hinnantc51e1022010-05-11 19:42:16 +00005225template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005226class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227{
5228 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005229protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005230 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005231 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005233 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005235 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5236 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005238 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005239public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005241 shared_ptr<_Tp> shared_from_this()
5242 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005244 shared_ptr<_Tp const> shared_from_this() const
5245 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005246
Eric Fiselier84006862016-06-02 00:15:35 +00005247#if _LIBCPP_STD_VER > 14
5248 _LIBCPP_INLINE_VISIBILITY
5249 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5250 { return __weak_this_; }
5251
5252 _LIBCPP_INLINE_VISIBILITY
5253 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5254 { return __weak_this_; }
5255#endif // _LIBCPP_STD_VER > 14
5256
Howard Hinnantc51e1022010-05-11 19:42:16 +00005257 template <class _Up> friend class shared_ptr;
5258};
5259
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005260template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005261struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005262{
5263 typedef shared_ptr<_Tp> argument_type;
5264 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005265
Howard Hinnant756c69b2010-09-22 16:48:34 +00005266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005267 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005268 {
5269 return hash<_Tp*>()(__ptr.get());
5270 }
5271};
5272
Howard Hinnantc834c512011-11-29 18:15:50 +00005273template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005274inline _LIBCPP_INLINE_VISIBILITY
5275basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005276operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005277
Eric Fiselier9b492672016-06-18 02:12:53 +00005278
5279#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005280
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005281class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005282{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005283 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005284public:
5285 void lock() _NOEXCEPT;
5286 void unlock() _NOEXCEPT;
5287
5288private:
5289 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5290 __sp_mut(const __sp_mut&);
5291 __sp_mut& operator=(const __sp_mut&);
5292
Howard Hinnant8331b762013-03-06 23:30:19 +00005293 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005294};
5295
Mehdi Amini228053d2017-05-04 17:08:54 +00005296_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5297__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005298
5299template <class _Tp>
5300inline _LIBCPP_INLINE_VISIBILITY
5301bool
5302atomic_is_lock_free(const shared_ptr<_Tp>*)
5303{
5304 return false;
5305}
5306
5307template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005308_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005309shared_ptr<_Tp>
5310atomic_load(const shared_ptr<_Tp>* __p)
5311{
5312 __sp_mut& __m = __get_sp_mut(__p);
5313 __m.lock();
5314 shared_ptr<_Tp> __q = *__p;
5315 __m.unlock();
5316 return __q;
5317}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005318
Howard Hinnant9fa30202012-07-30 01:40:57 +00005319template <class _Tp>
5320inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005321_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005322shared_ptr<_Tp>
5323atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5324{
5325 return atomic_load(__p);
5326}
5327
5328template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005329_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005330void
5331atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5332{
5333 __sp_mut& __m = __get_sp_mut(__p);
5334 __m.lock();
5335 __p->swap(__r);
5336 __m.unlock();
5337}
5338
5339template <class _Tp>
5340inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005341_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005342void
5343atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5344{
5345 atomic_store(__p, __r);
5346}
5347
5348template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005349_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005350shared_ptr<_Tp>
5351atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5352{
5353 __sp_mut& __m = __get_sp_mut(__p);
5354 __m.lock();
5355 __p->swap(__r);
5356 __m.unlock();
5357 return __r;
5358}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005359
Howard Hinnant9fa30202012-07-30 01:40:57 +00005360template <class _Tp>
5361inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005362_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005363shared_ptr<_Tp>
5364atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5365{
5366 return atomic_exchange(__p, __r);
5367}
5368
5369template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005370_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005371bool
5372atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5373{
Marshall Clow4201ee82016-05-18 17:50:13 +00005374 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005375 __sp_mut& __m = __get_sp_mut(__p);
5376 __m.lock();
5377 if (__p->__owner_equivalent(*__v))
5378 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005379 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005380 *__p = __w;
5381 __m.unlock();
5382 return true;
5383 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005384 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005385 *__v = *__p;
5386 __m.unlock();
5387 return false;
5388}
5389
5390template <class _Tp>
5391inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005392_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005393bool
5394atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5395{
5396 return atomic_compare_exchange_strong(__p, __v, __w);
5397}
5398
5399template <class _Tp>
5400inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005401_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005402bool
5403atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5404 shared_ptr<_Tp> __w, memory_order, memory_order)
5405{
5406 return atomic_compare_exchange_strong(__p, __v, __w);
5407}
5408
5409template <class _Tp>
5410inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005411_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005412bool
5413atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5414 shared_ptr<_Tp> __w, memory_order, memory_order)
5415{
5416 return atomic_compare_exchange_weak(__p, __v, __w);
5417}
5418
Eric Fiselier9b492672016-06-18 02:12:53 +00005419#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005420
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005421//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005422#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5423# ifndef _LIBCPP_CXX03_LANG
5424enum class pointer_safety : unsigned char {
5425 relaxed,
5426 preferred,
5427 strict
5428};
5429# endif
5430#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005431struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005432{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005433 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005434 {
5435 relaxed,
5436 preferred,
5437 strict
5438 };
5439
Howard Hinnant49e145e2012-10-30 19:06:59 +00005440 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005441
Howard Hinnant756c69b2010-09-22 16:48:34 +00005442 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005443 pointer_safety() : __v_() {}
5444
5445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005446 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005448 operator int() const {return __v_;}
5449};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005450#endif
5451
5452#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005453 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005454_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5455#else
5456// This function is only offered in C++03 under ABI v1.
5457# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5458inline _LIBCPP_INLINE_VISIBILITY
5459pointer_safety get_pointer_safety() _NOEXCEPT {
5460 return pointer_safety::relaxed;
5461}
5462# endif
5463#endif
5464
Howard Hinnantc51e1022010-05-11 19:42:16 +00005465
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005466_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5467_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5468_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005469_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005470
5471template <class _Tp>
5472inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005473_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005474undeclare_reachable(_Tp* __p)
5475{
5476 return static_cast<_Tp*>(__undeclare_reachable(__p));
5477}
5478
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005479_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005480
Marshall Clow8982dcd2015-07-13 20:04:56 +00005481// --- Helper for container swap --
5482template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005483inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005484void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5485#if _LIBCPP_STD_VER >= 14
5486 _NOEXCEPT
5487#else
5488 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5489#endif
5490{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005491 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005492 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5493}
5494
5495template <typename _Alloc>
5496_LIBCPP_INLINE_VISIBILITY
5497void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5498#if _LIBCPP_STD_VER >= 14
5499 _NOEXCEPT
5500#else
5501 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5502#endif
5503{
5504 using _VSTD::swap;
5505 swap(__a1, __a2);
5506}
5507
5508template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005509inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005510void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5511
Marshall Clowff91de82015-08-18 19:51:37 +00005512template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005513struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005514 _Traits::propagate_on_container_move_assignment::value
5515#if _LIBCPP_STD_VER > 14
5516 || _Traits::is_always_equal::value
5517#else
5518 && is_nothrow_move_assignable<_Alloc>::value
5519#endif
5520 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005521
Marshall Clowa591b9a2016-07-11 21:38:08 +00005522
5523#ifndef _LIBCPP_HAS_NO_VARIADICS
5524template <class _Tp, class _Alloc>
5525struct __temp_value {
5526 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005527
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005528 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005529 _Alloc &__a;
5530
5531 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5532 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005533
Marshall Clowa591b9a2016-07-11 21:38:08 +00005534 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005535 _LIBCPP_NO_CFI
5536 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5537 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5538 _VSTD::forward<_Args>(__args)...);
5539 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005540
Marshall Clowa591b9a2016-07-11 21:38:08 +00005541 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5542 };
5543#endif
5544
Marshall Clowe46031a2018-07-02 18:41:15 +00005545template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005546struct __is_allocator : false_type {};
5547
5548template<typename _Alloc>
5549struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005550 typename __void_t<typename _Alloc::value_type>::type,
5551 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5552 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005553 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005554
Eric Fiselier74ebee62019-06-08 01:31:19 +00005555// __builtin_new_allocator -- A non-templated helper for allocating and
5556// deallocating memory using __builtin_operator_new and
5557// __builtin_operator_delete. It should be used in preference to
5558// `std::allocator<T>` to avoid additional instantiations.
5559struct __builtin_new_allocator {
5560 struct __builtin_new_deleter {
5561 typedef void* pointer_type;
5562
5563 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5564 : __size_(__size), __align_(__align) {}
5565
5566 void operator()(void* p) const _NOEXCEPT {
5567 std::__libcpp_deallocate(p, __size_, __align_);
5568 }
5569
5570 private:
5571 size_t __size_;
5572 size_t __align_;
5573 };
5574
5575 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5576
5577 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5578 return __holder_t(std::__libcpp_allocate(__s, __align),
5579 __builtin_new_deleter(__s, __align));
5580 }
5581
5582 static void __deallocate_bytes(void* __p, size_t __s,
5583 size_t __align) _NOEXCEPT {
5584 std::__libcpp_deallocate(__p, __s, __align);
5585 }
5586
5587 template <class _Tp>
5588 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5589 static __holder_t __allocate_type(size_t __n) {
5590 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5591 }
5592
5593 template <class _Tp>
5594 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5595 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5596 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5597 }
5598};
5599
5600
Howard Hinnantc51e1022010-05-11 19:42:16 +00005601_LIBCPP_END_NAMESPACE_STD
5602
Eric Fiselierf4433a32017-05-31 22:07:49 +00005603_LIBCPP_POP_MACROS
5604
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005605#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005606# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005607#endif
5608
Howard Hinnantc51e1022010-05-11 19:42:16 +00005609#endif // _LIBCPP_MEMORY