blob: a9a5cc93084caece6345ba3b22bc8d6ee8d49ae7 [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
1612 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1613 {
Louis Dionne301a6772018-12-07 16:42:28 +00001614 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001615 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1616 }
1617
1618 template <class _Tp>
1619 _LIBCPP_INLINE_VISIBILITY
1620 static
1621 typename enable_if
1622 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001623 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001624 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1625 is_trivially_move_constructible<_Tp>::value,
1626 void
1627 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001628 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001629 {
1630 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001631 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001632 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001633 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001634 __begin2 += _Np;
1635 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001636 }
1637
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001638 template <class _Iter, class _Ptr>
1639 _LIBCPP_INLINE_VISIBILITY
1640 static
1641 void
1642 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1643 {
1644 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1645 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1646 }
1647
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001648 template <class _SourceTp, class _DestTp,
1649 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1650 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001651 _LIBCPP_INLINE_VISIBILITY
1652 static
1653 typename enable_if
1654 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001655 is_trivially_move_constructible<_DestTp>::value &&
1656 is_same<_RawSourceTp, _RawDestTp>::value &&
1657 (__is_default_allocator<allocator_type>::value ||
1658 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001659 void
1660 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001661 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001662 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001663 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001664 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001665 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001666 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001667 __begin2 += _Np;
1668 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001669 }
1670
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001671 template <class _Ptr>
1672 _LIBCPP_INLINE_VISIBILITY
1673 static
1674 void
1675 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1676 {
1677 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001678 {
1679 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1680 --__end2;
1681 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001682 }
1683
1684 template <class _Tp>
1685 _LIBCPP_INLINE_VISIBILITY
1686 static
1687 typename enable_if
1688 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001689 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001690 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1691 is_trivially_move_constructible<_Tp>::value,
1692 void
1693 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001694 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001695 {
1696 ptrdiff_t _Np = __end1 - __begin1;
1697 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001698 if (_Np > 0)
1699 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001700 }
1701
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702private:
1703
Howard Hinnant756c69b2010-09-22 16:48:34 +00001704 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001705 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 const_void_pointer __hint, true_type)
1707 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001708 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001709 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001710 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711 {return __a.allocate(__n);}
1712
1713#ifndef _LIBCPP_HAS_NO_VARIADICS
1714 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001717 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1721 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001722 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001724#else // _LIBCPP_HAS_NO_VARIADICS
1725 template <class _Tp, class _A0>
1726 _LIBCPP_INLINE_VISIBILITY
1727 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1728 const _A0& __a0)
1729 {__a.construct(__p, __a0);}
1730 template <class _Tp, class _A0>
1731 _LIBCPP_INLINE_VISIBILITY
1732 static void __construct(false_type, allocator_type&, _Tp* __p,
1733 const _A0& __a0)
1734 {
1735 ::new ((void*)__p) _Tp(__a0);
1736 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001737#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738
1739 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1742 {__a.destroy(__p);}
1743 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745 static void __destroy(false_type, allocator_type&, _Tp* __p)
1746 {
1747 __p->~_Tp();
1748 }
1749
Howard Hinnant756c69b2010-09-22 16:48:34 +00001750 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001751 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001753 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001754 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001755 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756
Howard Hinnant756c69b2010-09-22 16:48:34 +00001757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001759 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001763 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764 {return __a;}
1765};
1766
Marshall Clow940e01c2015-04-07 05:21:38 +00001767template <class _Traits, class _Tp>
1768struct __rebind_alloc_helper
1769{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001770#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001771 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001772#else
1773 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1774#endif
1775};
1776
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777// allocator
1778
1779template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001780class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781{
1782public:
1783 typedef size_t size_type;
1784 typedef ptrdiff_t difference_type;
1785 typedef _Tp* pointer;
1786 typedef const _Tp* const_pointer;
1787 typedef _Tp& reference;
1788 typedef const _Tp& const_reference;
1789 typedef _Tp value_type;
1790
Howard Hinnant4931e092011-06-02 21:38:57 +00001791 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001792 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001793
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1795
Marshall Clowbc759762018-03-20 23:02:53 +00001796 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1797 allocator() _NOEXCEPT {}
1798
Louis Dionne481a2662018-09-23 18:35:00 +00001799 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001800 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1801 allocator(const allocator<_Up>&) _NOEXCEPT {}
1802
Howard Hinnant719bda32011-05-28 14:41:13 +00001803 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001804 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001805 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001806 {return _VSTD::addressof(__x);}
Louis Dionne481a2662018-09-23 18:35:00 +00001807 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0e58cae2017-11-26 02:55:38 +00001808 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001809 {
1810 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001811 __throw_length_error("allocator<T>::allocate(size_t n)"
1812 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001813 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001814 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001815 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001816 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001817 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1818 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001819#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 template <class _Up, class... _Args>
1821 _LIBCPP_INLINE_VISIBILITY
1822 void
1823 construct(_Up* __p, _Args&&... __args)
1824 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001825 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001827#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 _LIBCPP_INLINE_VISIBILITY
1829 void
1830 construct(pointer __p)
1831 {
1832 ::new((void*)__p) _Tp();
1833 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001834# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001835
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 template <class _A0>
1837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001838 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 construct(pointer __p, _A0& __a0)
1840 {
1841 ::new((void*)__p) _Tp(__a0);
1842 }
1843 template <class _A0>
1844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001845 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846 construct(pointer __p, const _A0& __a0)
1847 {
1848 ::new((void*)__p) _Tp(__a0);
1849 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001850# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 template <class _A0, class _A1>
1852 _LIBCPP_INLINE_VISIBILITY
1853 void
1854 construct(pointer __p, _A0& __a0, _A1& __a1)
1855 {
1856 ::new((void*)__p) _Tp(__a0, __a1);
1857 }
1858 template <class _A0, class _A1>
1859 _LIBCPP_INLINE_VISIBILITY
1860 void
1861 construct(pointer __p, const _A0& __a0, _A1& __a1)
1862 {
1863 ::new((void*)__p) _Tp(__a0, __a1);
1864 }
1865 template <class _A0, class _A1>
1866 _LIBCPP_INLINE_VISIBILITY
1867 void
1868 construct(pointer __p, _A0& __a0, const _A1& __a1)
1869 {
1870 ::new((void*)__p) _Tp(__a0, __a1);
1871 }
1872 template <class _A0, class _A1>
1873 _LIBCPP_INLINE_VISIBILITY
1874 void
1875 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1876 {
1877 ::new((void*)__p) _Tp(__a0, __a1);
1878 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001879#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1881};
1882
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001883template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001884class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001885{
1886public:
1887 typedef size_t size_type;
1888 typedef ptrdiff_t difference_type;
1889 typedef const _Tp* pointer;
1890 typedef const _Tp* const_pointer;
1891 typedef const _Tp& reference;
1892 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001893 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001894
1895 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001896 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001897
1898 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1899
Marshall Clowbc759762018-03-20 23:02:53 +00001900 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1901 allocator() _NOEXCEPT {}
1902
1903 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001904 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001905 allocator(const allocator<_Up>&) _NOEXCEPT {}
1906
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001907 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1908 {return _VSTD::addressof(__x);}
1909 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001910 {
1911 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001912 __throw_length_error("allocator<const T>::allocate(size_t n)"
1913 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001914 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001915 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001916 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001917 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001918 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1919 {return size_type(~0) / sizeof(_Tp);}
1920#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1921 template <class _Up, class... _Args>
1922 _LIBCPP_INLINE_VISIBILITY
1923 void
1924 construct(_Up* __p, _Args&&... __args)
1925 {
1926 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1927 }
1928#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1929 _LIBCPP_INLINE_VISIBILITY
1930 void
1931 construct(pointer __p)
1932 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001933 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001934 }
1935# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001936
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001937 template <class _A0>
1938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001939 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001940 construct(pointer __p, _A0& __a0)
1941 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001942 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001943 }
1944 template <class _A0>
1945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001946 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001947 construct(pointer __p, const _A0& __a0)
1948 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001949 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001950 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001951# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1952 template <class _A0, class _A1>
1953 _LIBCPP_INLINE_VISIBILITY
1954 void
1955 construct(pointer __p, _A0& __a0, _A1& __a1)
1956 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001957 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001958 }
1959 template <class _A0, class _A1>
1960 _LIBCPP_INLINE_VISIBILITY
1961 void
1962 construct(pointer __p, const _A0& __a0, _A1& __a1)
1963 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001964 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001965 }
1966 template <class _A0, class _A1>
1967 _LIBCPP_INLINE_VISIBILITY
1968 void
1969 construct(pointer __p, _A0& __a0, const _A1& __a1)
1970 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001971 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001972 }
1973 template <class _A0, class _A1>
1974 _LIBCPP_INLINE_VISIBILITY
1975 void
1976 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1977 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001978 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001979 }
1980#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1981 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1982};
1983
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984template <class _Tp, class _Up>
1985inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001986bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987
1988template <class _Tp, class _Up>
1989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001990bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991
1992template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001993class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994 : public iterator<output_iterator_tag,
1995 _Tp, // purposefully not C++03
1996 ptrdiff_t, // purposefully not C++03
1997 _Tp*, // purposefully not C++03
1998 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1999{
2000private:
2001 _OutputIterator __x_;
2002public:
2003 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2004 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2005 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002006 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002007#if _LIBCPP_STD_VER >= 14
2008 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002009 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002010#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2012 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2013 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002014#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002015 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002016#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017};
2018
2019template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002020_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002022get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024 pair<_Tp*, ptrdiff_t> __r(0, 0);
2025 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2026 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2027 / sizeof(_Tp);
2028 if (__n > __m)
2029 __n = __m;
2030 while (__n > 0)
2031 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002032#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002033 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002034 {
2035 std::align_val_t __al =
2036 std::align_val_t(std::alignment_of<_Tp>::value);
2037 __r.first = static_cast<_Tp*>(::operator new(
2038 __n * sizeof(_Tp), __al, nothrow));
2039 } else {
2040 __r.first = static_cast<_Tp*>(::operator new(
2041 __n * sizeof(_Tp), nothrow));
2042 }
2043#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002044 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002045 {
2046 // Since aligned operator new is unavailable, return an empty
2047 // buffer rather than one with invalid alignment.
2048 return __r;
2049 }
2050
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002052#endif
2053
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054 if (__r.first)
2055 {
2056 __r.second = __n;
2057 break;
2058 }
2059 __n /= 2;
2060 }
2061 return __r;
2062}
2063
2064template <class _Tp>
2065inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002066void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2067{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002068 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002069}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070
Marshall Clowb22274f2017-01-24 22:22:33 +00002071#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002073struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074{
2075 _Tp* __ptr_;
2076};
2077
2078template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002079class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080{
2081private:
2082 _Tp* __ptr_;
2083public:
2084 typedef _Tp element_type;
2085
2086 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2087 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2088 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2089 : __ptr_(__p.release()) {}
2090 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2091 {reset(__p.release()); return *this;}
2092 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2093 {reset(__p.release()); return *this;}
2094 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2095 {reset(__p.__ptr_); return *this;}
2096 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2097
2098 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2099 {return *__ptr_;}
2100 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2101 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2102 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2103 {
2104 _Tp* __t = __ptr_;
2105 __ptr_ = 0;
2106 return __t;
2107 }
2108 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2109 {
2110 if (__ptr_ != __p)
2111 delete __ptr_;
2112 __ptr_ = __p;
2113 }
2114
2115 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2116 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2117 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2118 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2119 {return auto_ptr<_Up>(release());}
2120};
2121
2122template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002123class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124{
2125public:
2126 typedef void element_type;
2127};
Marshall Clowb22274f2017-01-24 22:22:33 +00002128#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129
Eric Fiselier9d355982017-04-12 23:45:53 +00002130template <class _Tp, int _Idx,
2131 bool _CanBeEmptyBase =
2132 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2133struct __compressed_pair_elem {
2134 typedef _Tp _ParamT;
2135 typedef _Tp& reference;
2136 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137
Eric Fiselier9d355982017-04-12 23:45:53 +00002138#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002139 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140
Eric Fiselier9d355982017-04-12 23:45:53 +00002141 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002142 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2143 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002144 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002145 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002146 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002147 : __value_(_VSTD::forward<_Up>(__u))
2148 {
2149 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150
Eric Fiselier9d355982017-04-12 23:45:53 +00002151 template <class... _Args, size_t... _Indexes>
2152 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2153 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2154 __tuple_indices<_Indexes...>)
2155 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2156#else
Alex Lorenz76132112017-11-09 17:54:49 +00002157 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2158 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002159 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2160#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161
Alex Lorenz76132112017-11-09 17:54:49 +00002162 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2163 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002164 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002167 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168};
2169
Eric Fiselier9d355982017-04-12 23:45:53 +00002170template <class _Tp, int _Idx>
2171struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2172 typedef _Tp _ParamT;
2173 typedef _Tp& reference;
2174 typedef const _Tp& const_reference;
2175 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176
Eric Fiselier9d355982017-04-12 23:45:53 +00002177#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002178 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179
Eric Fiselier9d355982017-04-12 23:45:53 +00002180 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002181 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2182 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002183 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002184 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002185 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002186 : __value_type(_VSTD::forward<_Up>(__u))
2187 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188
Eric Fiselier9d355982017-04-12 23:45:53 +00002189 template <class... _Args, size_t... _Indexes>
2190 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2191 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2192 __tuple_indices<_Indexes...>)
2193 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2194#else
Alex Lorenz76132112017-11-09 17:54:49 +00002195 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2196 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002197 __compressed_pair_elem(_ParamT __p)
2198 : __value_type(std::forward<_ParamT>(__p)) {}
2199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200
Alex Lorenz76132112017-11-09 17:54:49 +00002201 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2202 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002203 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204};
2205
Eric Fiselier9d355982017-04-12 23:45:53 +00002206// Tag used to construct the second element of the compressed pair.
2207struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208
2209template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002210class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2211 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002212 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2213 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002214
2215 // NOTE: This static assert should never fire because __compressed_pair
2216 // is *almost never* used in a scenario where it's possible for T1 == T2.
2217 // (The exception is std::function where it is possible that the function
2218 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002219 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002220 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2221 "The current implementation is NOT ABI-compatible with the previous "
2222 "implementation for this configuration");
2223
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002225#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002226 template <bool _Dummy = true,
2227 class = typename enable_if<
2228 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2229 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2230 >::type
2231 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002232 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002233 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234
Eric Fiselier9d355982017-04-12 23:45:53 +00002235 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2236 __compressed_pair>::value,
2237 bool>::type = true>
2238 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2239 __compressed_pair(_Tp&& __t)
2240 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241
Eric Fiselier9d355982017-04-12 23:45:53 +00002242 template <class _Tp>
2243 _LIBCPP_INLINE_VISIBILITY constexpr
2244 __compressed_pair(__second_tag, _Tp&& __t)
2245 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246
Eric Fiselier9d355982017-04-12 23:45:53 +00002247 template <class _U1, class _U2>
2248 _LIBCPP_INLINE_VISIBILITY constexpr
2249 __compressed_pair(_U1&& __t1, _U2&& __t2)
2250 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251
Eric Fiselier9d355982017-04-12 23:45:53 +00002252 template <class... _Args1, class... _Args2>
2253 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2254 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2255 tuple<_Args2...> __second_args)
2256 : _Base1(__pc, _VSTD::move(__first_args),
2257 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2258 _Base2(__pc, _VSTD::move(__second_args),
2259 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002260
Eric Fiselier9d355982017-04-12 23:45:53 +00002261#else
2262 _LIBCPP_INLINE_VISIBILITY
2263 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002264
Eric Fiselier9d355982017-04-12 23:45:53 +00002265 _LIBCPP_INLINE_VISIBILITY explicit
2266 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002267
Eric Fiselier9d355982017-04-12 23:45:53 +00002268 _LIBCPP_INLINE_VISIBILITY
2269 __compressed_pair(__second_tag, _T2 __t2)
2270 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271
Eric Fiselier9d355982017-04-12 23:45:53 +00002272 _LIBCPP_INLINE_VISIBILITY
2273 __compressed_pair(_T1 __t1, _T2 __t2)
2274 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2275#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276
Eric Fiselier9d355982017-04-12 23:45:53 +00002277 _LIBCPP_INLINE_VISIBILITY
2278 typename _Base1::reference first() _NOEXCEPT {
2279 return static_cast<_Base1&>(*this).__get();
2280 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281
Eric Fiselier9d355982017-04-12 23:45:53 +00002282 _LIBCPP_INLINE_VISIBILITY
2283 typename _Base1::const_reference first() const _NOEXCEPT {
2284 return static_cast<_Base1 const&>(*this).__get();
2285 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286
Eric Fiselier9d355982017-04-12 23:45:53 +00002287 _LIBCPP_INLINE_VISIBILITY
2288 typename _Base2::reference second() _NOEXCEPT {
2289 return static_cast<_Base2&>(*this).__get();
2290 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291
Eric Fiselier9d355982017-04-12 23:45:53 +00002292 _LIBCPP_INLINE_VISIBILITY
2293 typename _Base2::const_reference second() const _NOEXCEPT {
2294 return static_cast<_Base2 const&>(*this).__get();
2295 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296
Eric Fiselier9d355982017-04-12 23:45:53 +00002297 _LIBCPP_INLINE_VISIBILITY
2298 void swap(__compressed_pair& __x)
2299 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2300 __is_nothrow_swappable<_T2>::value)
2301 {
2302 using std::swap;
2303 swap(first(), __x.first());
2304 swap(second(), __x.second());
2305 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306};
2307
2308template <class _T1, class _T2>
2309inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002310void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2311 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2312 __is_nothrow_swappable<_T2>::value) {
2313 __x.swap(__y);
2314}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002316// default_delete
2317
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002319struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002320 static_assert(!is_function<_Tp>::value,
2321 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002322#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002323 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002324#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002325 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002326#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002327 template <class _Up>
2328 _LIBCPP_INLINE_VISIBILITY
2329 default_delete(const default_delete<_Up>&,
2330 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2331 0) _NOEXCEPT {}
2332
2333 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2334 static_assert(sizeof(_Tp) > 0,
2335 "default_delete can not delete incomplete type");
2336 static_assert(!is_void<_Tp>::value,
2337 "default_delete can not delete incomplete type");
2338 delete __ptr;
2339 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340};
2341
2342template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002343struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2344private:
2345 template <class _Up>
2346 struct _EnableIfConvertible
2347 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2348
Howard Hinnant4500ca52011-12-18 21:19:44 +00002349public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002350#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002351 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002352#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002353 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002354#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002355
2356 template <class _Up>
2357 _LIBCPP_INLINE_VISIBILITY
2358 default_delete(const default_delete<_Up[]>&,
2359 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2360
2361 template <class _Up>
2362 _LIBCPP_INLINE_VISIBILITY
2363 typename _EnableIfConvertible<_Up>::type
2364 operator()(_Up* __ptr) const _NOEXCEPT {
2365 static_assert(sizeof(_Tp) > 0,
2366 "default_delete can not delete incomplete type");
2367 static_assert(!is_void<_Tp>::value,
2368 "default_delete can not delete void type");
2369 delete[] __ptr;
2370 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371};
2372
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002373template <class _Deleter>
2374struct __unique_ptr_deleter_sfinae {
2375 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2376 typedef const _Deleter& __lval_ref_type;
2377 typedef _Deleter&& __good_rval_ref_type;
2378 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379};
2380
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002381template <class _Deleter>
2382struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2383 typedef const _Deleter& __lval_ref_type;
2384 typedef const _Deleter&& __bad_rval_ref_type;
2385 typedef false_type __enable_rval_overload;
2386};
2387
2388template <class _Deleter>
2389struct __unique_ptr_deleter_sfinae<_Deleter&> {
2390 typedef _Deleter& __lval_ref_type;
2391 typedef _Deleter&& __bad_rval_ref_type;
2392 typedef false_type __enable_rval_overload;
2393};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002394
2395template <class _Tp, class _Dp = default_delete<_Tp> >
2396class _LIBCPP_TEMPLATE_VIS unique_ptr {
2397public:
2398 typedef _Tp element_type;
2399 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002400 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002401
2402 static_assert(!is_rvalue_reference<deleter_type>::value,
2403 "the specified deleter type cannot be an rvalue reference");
2404
2405private:
2406 __compressed_pair<pointer, deleter_type> __ptr_;
2407
2408 struct __nat { int __for_bool_; };
2409
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002410 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002411
2412 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002413 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002414 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002415
2416 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002417 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002418 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002419
2420 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002421 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002422 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002423
2424 template <bool _Dummy, class _Deleter = typename __dependent_type<
2425 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002426 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002427 typename enable_if<is_default_constructible<_Deleter>::value &&
2428 !is_pointer<_Deleter>::value>::type;
2429
2430 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002431 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002432 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2433
2434 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002435 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002436 is_convertible<typename _UPtr::pointer, pointer>::value &&
2437 !is_array<_Up>::value
2438 >::type;
2439
2440 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002441 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002442 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2443 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2444 >::type;
2445
2446 template <class _UDel>
2447 using _EnableIfDeleterAssignable = typename enable_if<
2448 is_assignable<_Dp&, _UDel&&>::value
2449 >::type;
2450
2451public:
2452 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002453 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002454 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002455 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002456
2457 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002458 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002459 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002460 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002461
2462 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002463 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002464 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002465 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002466
2467 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002468 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002469 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002470 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002471 : __ptr_(__p, __d) {}
2472
2473 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002474 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002475 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002476 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002477 : __ptr_(__p, _VSTD::move(__d)) {
2478 static_assert(!is_reference<deleter_type>::value,
2479 "rvalue deleter bound to reference");
2480 }
2481
2482 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002483 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002484 _LIBCPP_INLINE_VISIBILITY
2485 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2486
2487 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002488 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002489 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2490 }
2491
2492 template <class _Up, class _Ep,
2493 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2494 class = _EnableIfDeleterConvertible<_Ep>
2495 >
2496 _LIBCPP_INLINE_VISIBILITY
2497 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2498 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2499
2500#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2501 template <class _Up>
2502 _LIBCPP_INLINE_VISIBILITY
2503 unique_ptr(auto_ptr<_Up>&& __p,
2504 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002505 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002506 __nat>::type = __nat()) _NOEXCEPT
2507 : __ptr_(__p.release()) {}
2508#endif
2509
2510 _LIBCPP_INLINE_VISIBILITY
2511 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2512 reset(__u.release());
2513 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2514 return *this;
2515 }
2516
2517 template <class _Up, class _Ep,
2518 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2519 class = _EnableIfDeleterAssignable<_Ep>
2520 >
2521 _LIBCPP_INLINE_VISIBILITY
2522 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2523 reset(__u.release());
2524 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2525 return *this;
2526 }
2527
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002528#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2529 template <class _Up>
2530 _LIBCPP_INLINE_VISIBILITY
2531 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2532 is_same<_Dp, default_delete<_Tp> >::value,
2533 unique_ptr&>::type
2534 operator=(auto_ptr<_Up> __p) {
2535 reset(__p.release());
2536 return *this;
2537 }
2538#endif
2539
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002540#ifdef _LIBCPP_CXX03_LANG
2541 unique_ptr(unique_ptr const&) = delete;
2542 unique_ptr& operator=(unique_ptr const&) = delete;
2543#endif
2544
2545
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002546 _LIBCPP_INLINE_VISIBILITY
2547 ~unique_ptr() { reset(); }
2548
2549 _LIBCPP_INLINE_VISIBILITY
2550 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2551 reset();
2552 return *this;
2553 }
2554
2555 _LIBCPP_INLINE_VISIBILITY
2556 typename add_lvalue_reference<_Tp>::type
2557 operator*() const {
2558 return *__ptr_.first();
2559 }
2560 _LIBCPP_INLINE_VISIBILITY
2561 pointer operator->() const _NOEXCEPT {
2562 return __ptr_.first();
2563 }
2564 _LIBCPP_INLINE_VISIBILITY
2565 pointer get() const _NOEXCEPT {
2566 return __ptr_.first();
2567 }
2568 _LIBCPP_INLINE_VISIBILITY
2569 deleter_type& get_deleter() _NOEXCEPT {
2570 return __ptr_.second();
2571 }
2572 _LIBCPP_INLINE_VISIBILITY
2573 const deleter_type& get_deleter() const _NOEXCEPT {
2574 return __ptr_.second();
2575 }
2576 _LIBCPP_INLINE_VISIBILITY
2577 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2578 return __ptr_.first() != nullptr;
2579 }
2580
2581 _LIBCPP_INLINE_VISIBILITY
2582 pointer release() _NOEXCEPT {
2583 pointer __t = __ptr_.first();
2584 __ptr_.first() = pointer();
2585 return __t;
2586 }
2587
2588 _LIBCPP_INLINE_VISIBILITY
2589 void reset(pointer __p = pointer()) _NOEXCEPT {
2590 pointer __tmp = __ptr_.first();
2591 __ptr_.first() = __p;
2592 if (__tmp)
2593 __ptr_.second()(__tmp);
2594 }
2595
2596 _LIBCPP_INLINE_VISIBILITY
2597 void swap(unique_ptr& __u) _NOEXCEPT {
2598 __ptr_.swap(__u.__ptr_);
2599 }
2600};
2601
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002602
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002604class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002606 typedef _Tp element_type;
2607 typedef _Dp deleter_type;
2608 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2609
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002611 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612
Eric Fiselier31127cd2017-04-16 02:14:31 +00002613 template <class _From>
2614 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615
Eric Fiselier31127cd2017-04-16 02:14:31 +00002616 template <class _FromElem>
2617 struct _CheckArrayPointerConversion<_FromElem*>
2618 : integral_constant<bool,
2619 is_same<_FromElem*, pointer>::value ||
2620 (is_same<pointer, element_type*>::value &&
2621 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2622 >
2623 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624
Eric Fiselier31127cd2017-04-16 02:14:31 +00002625 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002626
2627 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002628 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002629 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002630
2631 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002632 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002633 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002634
2635 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002636 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002637 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002638
2639 template <bool _Dummy, class _Deleter = typename __dependent_type<
2640 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002641 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002642 typename enable_if<is_default_constructible<_Deleter>::value &&
2643 !is_pointer<_Deleter>::value>::type;
2644
2645 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002646 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002647 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2648
2649 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002650 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002651 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002652 >::type;
2653
2654 template <class _UPtr, class _Up,
2655 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002656 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002657 is_array<_Up>::value &&
2658 is_same<pointer, element_type*>::value &&
2659 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2660 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2661 >::type;
2662
2663 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002664 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002665 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2666 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2667 >::type;
2668
2669 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002670 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002671 is_assignable<_Dp&, _UDel&&>::value
2672 >::type;
2673
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002675 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002676 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002677 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002678 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002680 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002681 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002682 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002683 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002685 template <class _Pp, bool _Dummy = true,
2686 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002687 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002688 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002689 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002690 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002692 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002693 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2694 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002695 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002696 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002697 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002699 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002700 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002701 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002702 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002703 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002705 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002706 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2707 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002708 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002709 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002710 : __ptr_(__p, _VSTD::move(__d)) {
2711 static_assert(!is_reference<deleter_type>::value,
2712 "rvalue deleter bound to reference");
2713 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002716 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002718 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002719 : __ptr_(nullptr, _VSTD::move(__d)) {
2720 static_assert(!is_reference<deleter_type>::value,
2721 "rvalue deleter bound to reference");
2722 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002723
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002724 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002725 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2726 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727 _LIBCPP_INLINE_VISIBILITY
2728 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002729
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002730 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002731 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002732 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2733 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002734
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002735 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002736 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002737 reset(__u.release());
2738 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2739 return *this;
2740 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002742 template <class _Up, class _Ep,
2743 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2744 class = _EnableIfDeleterConvertible<_Ep>
2745 >
2746 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002747 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002748 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002749 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002751 template <class _Up, class _Ep,
2752 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2753 class = _EnableIfDeleterAssignable<_Ep>
2754 >
2755 _LIBCPP_INLINE_VISIBILITY
2756 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002757 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002758 reset(__u.release());
2759 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2760 return *this;
2761 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002763#ifdef _LIBCPP_CXX03_LANG
2764 unique_ptr(unique_ptr const&) = delete;
2765 unique_ptr& operator=(unique_ptr const&) = delete;
2766#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002767
2768public:
2769 _LIBCPP_INLINE_VISIBILITY
2770 ~unique_ptr() { reset(); }
2771
2772 _LIBCPP_INLINE_VISIBILITY
2773 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2774 reset();
2775 return *this;
2776 }
2777
2778 _LIBCPP_INLINE_VISIBILITY
2779 typename add_lvalue_reference<_Tp>::type
2780 operator[](size_t __i) const {
2781 return __ptr_.first()[__i];
2782 }
2783 _LIBCPP_INLINE_VISIBILITY
2784 pointer get() const _NOEXCEPT {
2785 return __ptr_.first();
2786 }
2787
2788 _LIBCPP_INLINE_VISIBILITY
2789 deleter_type& get_deleter() _NOEXCEPT {
2790 return __ptr_.second();
2791 }
2792
2793 _LIBCPP_INLINE_VISIBILITY
2794 const deleter_type& get_deleter() const _NOEXCEPT {
2795 return __ptr_.second();
2796 }
2797 _LIBCPP_INLINE_VISIBILITY
2798 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2799 return __ptr_.first() != nullptr;
2800 }
2801
2802 _LIBCPP_INLINE_VISIBILITY
2803 pointer release() _NOEXCEPT {
2804 pointer __t = __ptr_.first();
2805 __ptr_.first() = pointer();
2806 return __t;
2807 }
2808
2809 template <class _Pp>
2810 _LIBCPP_INLINE_VISIBILITY
2811 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002812 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002813 >::type
2814 reset(_Pp __p) _NOEXCEPT {
2815 pointer __tmp = __ptr_.first();
2816 __ptr_.first() = __p;
2817 if (__tmp)
2818 __ptr_.second()(__tmp);
2819 }
2820
2821 _LIBCPP_INLINE_VISIBILITY
2822 void reset(nullptr_t = nullptr) _NOEXCEPT {
2823 pointer __tmp = __ptr_.first();
2824 __ptr_.first() = nullptr;
2825 if (__tmp)
2826 __ptr_.second()(__tmp);
2827 }
2828
2829 _LIBCPP_INLINE_VISIBILITY
2830 void swap(unique_ptr& __u) _NOEXCEPT {
2831 __ptr_.swap(__u.__ptr_);
2832 }
2833
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834};
2835
2836template <class _Tp, class _Dp>
2837inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002838typename enable_if<
2839 __is_swappable<_Dp>::value,
2840 void
2841>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002842swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843
2844template <class _T1, class _D1, class _T2, class _D2>
2845inline _LIBCPP_INLINE_VISIBILITY
2846bool
2847operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2848
2849template <class _T1, class _D1, class _T2, class _D2>
2850inline _LIBCPP_INLINE_VISIBILITY
2851bool
2852operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2853
2854template <class _T1, class _D1, class _T2, class _D2>
2855inline _LIBCPP_INLINE_VISIBILITY
2856bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002857operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2858{
2859 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2860 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002861 typedef typename common_type<_P1, _P2>::type _Vp;
2862 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002863}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864
2865template <class _T1, class _D1, class _T2, class _D2>
2866inline _LIBCPP_INLINE_VISIBILITY
2867bool
2868operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2869
2870template <class _T1, class _D1, class _T2, class _D2>
2871inline _LIBCPP_INLINE_VISIBILITY
2872bool
2873operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2874
2875template <class _T1, class _D1, class _T2, class _D2>
2876inline _LIBCPP_INLINE_VISIBILITY
2877bool
2878operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2879
Howard Hinnantb17caf92012-02-21 21:02:58 +00002880template <class _T1, class _D1>
2881inline _LIBCPP_INLINE_VISIBILITY
2882bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002883operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002884{
2885 return !__x;
2886}
2887
2888template <class _T1, class _D1>
2889inline _LIBCPP_INLINE_VISIBILITY
2890bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002891operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002892{
2893 return !__x;
2894}
2895
2896template <class _T1, class _D1>
2897inline _LIBCPP_INLINE_VISIBILITY
2898bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002899operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002900{
2901 return static_cast<bool>(__x);
2902}
2903
2904template <class _T1, class _D1>
2905inline _LIBCPP_INLINE_VISIBILITY
2906bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002907operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002908{
2909 return static_cast<bool>(__x);
2910}
2911
2912template <class _T1, class _D1>
2913inline _LIBCPP_INLINE_VISIBILITY
2914bool
2915operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2916{
2917 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2918 return less<_P1>()(__x.get(), nullptr);
2919}
2920
2921template <class _T1, class _D1>
2922inline _LIBCPP_INLINE_VISIBILITY
2923bool
2924operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2925{
2926 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2927 return less<_P1>()(nullptr, __x.get());
2928}
2929
2930template <class _T1, class _D1>
2931inline _LIBCPP_INLINE_VISIBILITY
2932bool
2933operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2934{
2935 return nullptr < __x;
2936}
2937
2938template <class _T1, class _D1>
2939inline _LIBCPP_INLINE_VISIBILITY
2940bool
2941operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2942{
2943 return __x < nullptr;
2944}
2945
2946template <class _T1, class _D1>
2947inline _LIBCPP_INLINE_VISIBILITY
2948bool
2949operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2950{
2951 return !(nullptr < __x);
2952}
2953
2954template <class _T1, class _D1>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
2957operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2958{
2959 return !(__x < nullptr);
2960}
2961
2962template <class _T1, class _D1>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2966{
2967 return !(__x < nullptr);
2968}
2969
2970template <class _T1, class _D1>
2971inline _LIBCPP_INLINE_VISIBILITY
2972bool
2973operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2974{
2975 return !(nullptr < __x);
2976}
2977
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002978#if _LIBCPP_STD_VER > 11
2979
2980template<class _Tp>
2981struct __unique_if
2982{
2983 typedef unique_ptr<_Tp> __unique_single;
2984};
2985
2986template<class _Tp>
2987struct __unique_if<_Tp[]>
2988{
2989 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2990};
2991
2992template<class _Tp, size_t _Np>
2993struct __unique_if<_Tp[_Np]>
2994{
2995 typedef void __unique_array_known_bound;
2996};
2997
2998template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002999inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003000typename __unique_if<_Tp>::__unique_single
3001make_unique(_Args&&... __args)
3002{
3003 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3004}
3005
3006template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003007inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003008typename __unique_if<_Tp>::__unique_array_unknown_bound
3009make_unique(size_t __n)
3010{
3011 typedef typename remove_extent<_Tp>::type _Up;
3012 return unique_ptr<_Tp>(new _Up[__n]());
3013}
3014
3015template<class _Tp, class... _Args>
3016 typename __unique_if<_Tp>::__unique_array_known_bound
3017 make_unique(_Args&&...) = delete;
3018
3019#endif // _LIBCPP_STD_VER > 11
3020
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003021template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003022#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003023struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003024#else
3025struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003026 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003027#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003028{
3029 typedef unique_ptr<_Tp, _Dp> argument_type;
3030 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003031 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003032 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003033 {
3034 typedef typename argument_type::pointer pointer;
3035 return hash<pointer>()(__ptr.get());
3036 }
3037};
3038
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039struct __destruct_n
3040{
3041private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003042 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043
3044 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003045 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003046 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047
3048 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003049 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050 {}
3051
Howard Hinnant719bda32011-05-28 14:41:13 +00003052 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003053 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003054 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055 {}
3056
Howard Hinnant719bda32011-05-28 14:41:13 +00003057 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003058 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003059 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060 {}
3061public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003062 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003063 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064
3065 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003066 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003067 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068
3069 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003070 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003071 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072
3073 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003074 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003075 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076};
3077
3078template <class _Alloc>
3079class __allocator_destructor
3080{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003081 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003083 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3084 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085private:
3086 _Alloc& __alloc_;
3087 size_type __s_;
3088public:
3089 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003090 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003093 void operator()(pointer __p) _NOEXCEPT
3094 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095};
3096
3097template <class _InputIterator, class _ForwardIterator>
3098_ForwardIterator
3099uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3100{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003102#ifndef _LIBCPP_NO_EXCEPTIONS
3103 _ForwardIterator __s = __r;
3104 try
3105 {
3106#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003107 for (; __f != __l; ++__f, (void) ++__r)
3108 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003109#ifndef _LIBCPP_NO_EXCEPTIONS
3110 }
3111 catch (...)
3112 {
3113 for (; __s != __r; ++__s)
3114 __s->~value_type();
3115 throw;
3116 }
3117#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 return __r;
3119}
3120
3121template <class _InputIterator, class _Size, class _ForwardIterator>
3122_ForwardIterator
3123uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3124{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003126#ifndef _LIBCPP_NO_EXCEPTIONS
3127 _ForwardIterator __s = __r;
3128 try
3129 {
3130#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003131 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3132 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003133#ifndef _LIBCPP_NO_EXCEPTIONS
3134 }
3135 catch (...)
3136 {
3137 for (; __s != __r; ++__s)
3138 __s->~value_type();
3139 throw;
3140 }
3141#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142 return __r;
3143}
3144
3145template <class _ForwardIterator, class _Tp>
3146void
3147uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3148{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003150#ifndef _LIBCPP_NO_EXCEPTIONS
3151 _ForwardIterator __s = __f;
3152 try
3153 {
3154#endif
3155 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003156 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003157#ifndef _LIBCPP_NO_EXCEPTIONS
3158 }
3159 catch (...)
3160 {
3161 for (; __s != __f; ++__s)
3162 __s->~value_type();
3163 throw;
3164 }
3165#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166}
3167
3168template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003169_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3171{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003173#ifndef _LIBCPP_NO_EXCEPTIONS
3174 _ForwardIterator __s = __f;
3175 try
3176 {
3177#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003178 for (; __n > 0; ++__f, (void) --__n)
3179 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003180#ifndef _LIBCPP_NO_EXCEPTIONS
3181 }
3182 catch (...)
3183 {
3184 for (; __s != __f; ++__s)
3185 __s->~value_type();
3186 throw;
3187 }
3188#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003189 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190}
3191
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003192#if _LIBCPP_STD_VER > 14
3193
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003194template <class _Tp>
3195inline _LIBCPP_INLINE_VISIBILITY
3196void destroy_at(_Tp* __loc) {
3197 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3198 __loc->~_Tp();
3199}
3200
3201template <class _ForwardIterator>
3202inline _LIBCPP_INLINE_VISIBILITY
3203void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3204 for (; __first != __last; ++__first)
3205 _VSTD::destroy_at(_VSTD::addressof(*__first));
3206}
3207
3208template <class _ForwardIterator, class _Size>
3209inline _LIBCPP_INLINE_VISIBILITY
3210_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3211 for (; __n > 0; (void)++__first, --__n)
3212 _VSTD::destroy_at(_VSTD::addressof(*__first));
3213 return __first;
3214}
3215
Eric Fiselier290c07c2016-10-11 21:13:44 +00003216template <class _ForwardIterator>
3217inline _LIBCPP_INLINE_VISIBILITY
3218void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3219 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3220 auto __idx = __first;
3221#ifndef _LIBCPP_NO_EXCEPTIONS
3222 try {
3223#endif
3224 for (; __idx != __last; ++__idx)
3225 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3226#ifndef _LIBCPP_NO_EXCEPTIONS
3227 } catch (...) {
3228 _VSTD::destroy(__first, __idx);
3229 throw;
3230 }
3231#endif
3232}
3233
3234template <class _ForwardIterator, class _Size>
3235inline _LIBCPP_INLINE_VISIBILITY
3236_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3237 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3238 auto __idx = __first;
3239#ifndef _LIBCPP_NO_EXCEPTIONS
3240 try {
3241#endif
3242 for (; __n > 0; (void)++__idx, --__n)
3243 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3244 return __idx;
3245#ifndef _LIBCPP_NO_EXCEPTIONS
3246 } catch (...) {
3247 _VSTD::destroy(__first, __idx);
3248 throw;
3249 }
3250#endif
3251}
3252
3253
3254template <class _ForwardIterator>
3255inline _LIBCPP_INLINE_VISIBILITY
3256void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3257 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3258 auto __idx = __first;
3259#ifndef _LIBCPP_NO_EXCEPTIONS
3260 try {
3261#endif
3262 for (; __idx != __last; ++__idx)
3263 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3264#ifndef _LIBCPP_NO_EXCEPTIONS
3265 } catch (...) {
3266 _VSTD::destroy(__first, __idx);
3267 throw;
3268 }
3269#endif
3270}
3271
3272template <class _ForwardIterator, class _Size>
3273inline _LIBCPP_INLINE_VISIBILITY
3274_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3275 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3276 auto __idx = __first;
3277#ifndef _LIBCPP_NO_EXCEPTIONS
3278 try {
3279#endif
3280 for (; __n > 0; (void)++__idx, --__n)
3281 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3282 return __idx;
3283#ifndef _LIBCPP_NO_EXCEPTIONS
3284 } catch (...) {
3285 _VSTD::destroy(__first, __idx);
3286 throw;
3287 }
3288#endif
3289}
3290
3291
3292template <class _InputIt, class _ForwardIt>
3293inline _LIBCPP_INLINE_VISIBILITY
3294_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3295 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3296 auto __idx = __first_res;
3297#ifndef _LIBCPP_NO_EXCEPTIONS
3298 try {
3299#endif
3300 for (; __first != __last; (void)++__idx, ++__first)
3301 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3302 return __idx;
3303#ifndef _LIBCPP_NO_EXCEPTIONS
3304 } catch (...) {
3305 _VSTD::destroy(__first_res, __idx);
3306 throw;
3307 }
3308#endif
3309}
3310
3311template <class _InputIt, class _Size, class _ForwardIt>
3312inline _LIBCPP_INLINE_VISIBILITY
3313pair<_InputIt, _ForwardIt>
3314uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3315 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3316 auto __idx = __first_res;
3317#ifndef _LIBCPP_NO_EXCEPTIONS
3318 try {
3319#endif
3320 for (; __n > 0; ++__idx, (void)++__first, --__n)
3321 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3322 return {__first, __idx};
3323#ifndef _LIBCPP_NO_EXCEPTIONS
3324 } catch (...) {
3325 _VSTD::destroy(__first_res, __idx);
3326 throw;
3327 }
3328#endif
3329}
3330
3331
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003332#endif // _LIBCPP_STD_VER > 14
3333
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003334// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3335// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003336// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003337#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3338 && defined(__ATOMIC_RELAXED) \
3339 && defined(__ATOMIC_ACQ_REL)
3340# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003341#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003342# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3343#endif
3344
3345template <class _Tp>
3346inline _LIBCPP_INLINE_VISIBILITY _Tp
3347__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3348{
3349#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3350 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3351#else
3352 return __t += 1;
3353#endif
3354}
3355
3356template <class _Tp>
3357inline _LIBCPP_INLINE_VISIBILITY _Tp
3358__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3359{
3360#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3361 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3362#else
3363 return __t -= 1;
3364#endif
3365}
3366
Howard Hinnant756c69b2010-09-22 16:48:34 +00003367class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368 : public std::exception
3369{
3370public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003371 virtual ~bad_weak_ptr() _NOEXCEPT;
3372 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373};
3374
Louis Dionne16fe2952018-07-11 23:14:33 +00003375_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003376void __throw_bad_weak_ptr()
3377{
3378#ifndef _LIBCPP_NO_EXCEPTIONS
3379 throw bad_weak_ptr();
3380#else
3381 _VSTD::abort();
3382#endif
3383}
3384
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003385template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003387class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388{
3389 __shared_count(const __shared_count&);
3390 __shared_count& operator=(const __shared_count&);
3391
3392protected:
3393 long __shared_owners_;
3394 virtual ~__shared_count();
3395private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003396 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397
3398public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003400 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401 : __shared_owners_(__refs) {}
3402
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003403#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003404 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003405 void __add_shared() _NOEXCEPT;
3406 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003407#else
3408 _LIBCPP_INLINE_VISIBILITY
3409 void __add_shared() _NOEXCEPT {
3410 __libcpp_atomic_refcount_increment(__shared_owners_);
3411 }
3412 _LIBCPP_INLINE_VISIBILITY
3413 bool __release_shared() _NOEXCEPT {
3414 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3415 __on_zero_shared();
3416 return true;
3417 }
3418 return false;
3419 }
3420#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003421 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003422 long use_count() const _NOEXCEPT {
3423 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3424 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425};
3426
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003427class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428 : private __shared_count
3429{
3430 long __shared_weak_owners_;
3431
3432public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003434 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435 : __shared_count(__refs),
3436 __shared_weak_owners_(__refs) {}
3437protected:
3438 virtual ~__shared_weak_count();
3439
3440public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003441#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003442 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003443 void __add_shared() _NOEXCEPT;
3444 void __add_weak() _NOEXCEPT;
3445 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003446#else
3447 _LIBCPP_INLINE_VISIBILITY
3448 void __add_shared() _NOEXCEPT {
3449 __shared_count::__add_shared();
3450 }
3451 _LIBCPP_INLINE_VISIBILITY
3452 void __add_weak() _NOEXCEPT {
3453 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3454 }
3455 _LIBCPP_INLINE_VISIBILITY
3456 void __release_shared() _NOEXCEPT {
3457 if (__shared_count::__release_shared())
3458 __release_weak();
3459 }
3460#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003461 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003463 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3464 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465
Howard Hinnant807d6332013-02-25 15:50:36 +00003466 // Define the function out only if we build static libc++ without RTTI.
3467 // Otherwise we may break clients who need to compile their projects with
3468 // -fno-rtti and yet link against a libc++.dylib compiled
3469 // without -fno-rtti.
3470#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003471 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003472#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003474 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475};
3476
3477template <class _Tp, class _Dp, class _Alloc>
3478class __shared_ptr_pointer
3479 : public __shared_weak_count
3480{
3481 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3482public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003485 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486
Howard Hinnant72f73582010-08-11 17:04:31 +00003487#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003488 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003489#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490
3491private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003492 virtual void __on_zero_shared() _NOEXCEPT;
3493 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494};
3495
Howard Hinnant72f73582010-08-11 17:04:31 +00003496#ifndef _LIBCPP_NO_RTTI
3497
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498template <class _Tp, class _Dp, class _Alloc>
3499const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003500__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003502 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503}
3504
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003505#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003506
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507template <class _Tp, class _Dp, class _Alloc>
3508void
Howard Hinnant719bda32011-05-28 14:41:13 +00003509__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510{
3511 __data_.first().second()(__data_.first().first());
3512 __data_.first().second().~_Dp();
3513}
3514
3515template <class _Tp, class _Dp, class _Alloc>
3516void
Howard Hinnant719bda32011-05-28 14:41:13 +00003517__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003519 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3520 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003521 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3522
Eric Fiselierf8898c82015-02-05 23:01:40 +00003523 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003525 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526}
3527
3528template <class _Tp, class _Alloc>
3529class __shared_ptr_emplace
3530 : public __shared_weak_count
3531{
3532 __compressed_pair<_Alloc, _Tp> __data_;
3533public:
3534#ifndef _LIBCPP_HAS_NO_VARIADICS
3535
Howard Hinnant756c69b2010-09-22 16:48:34 +00003536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003538 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539
3540 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003543 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3544 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545
3546#else // _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)
3550 : __data_(__a) {}
3551
3552 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3555 : __data_(__a, _Tp(__a0)) {}
3556
3557 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3560 : __data_(__a, _Tp(__a0, __a1)) {}
3561
3562 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3565 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3566
3567#endif // _LIBCPP_HAS_NO_VARIADICS
3568
3569private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003570 virtual void __on_zero_shared() _NOEXCEPT;
3571 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003573 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003574 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575};
3576
3577template <class _Tp, class _Alloc>
3578void
Howard Hinnant719bda32011-05-28 14:41:13 +00003579__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580{
3581 __data_.second().~_Tp();
3582}
3583
3584template <class _Tp, class _Alloc>
3585void
Howard Hinnant719bda32011-05-28 14:41:13 +00003586__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003588 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3589 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003590 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003591 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003593 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594}
3595
Erik Pilkington2a398762017-05-25 15:43:31 +00003596struct __shared_ptr_dummy_rebind_allocator_type;
3597template <>
3598class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3599{
3600public:
3601 template <class _Other>
3602 struct rebind
3603 {
3604 typedef allocator<_Other> other;
3605 };
3606};
3607
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003608template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609
3610template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003611class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003613public:
3614 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003615
Eric Fiselierae5b6672016-06-27 01:02:43 +00003616#if _LIBCPP_STD_VER > 14
3617 typedef weak_ptr<_Tp> weak_type;
3618#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619private:
3620 element_type* __ptr_;
3621 __shared_weak_count* __cntrl_;
3622
3623 struct __nat {int __for_bool_;};
3624public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003626 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003628 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003629 template<class _Yp>
3630 explicit shared_ptr(_Yp* __p,
3631 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3632 template<class _Yp, class _Dp>
3633 shared_ptr(_Yp* __p, _Dp __d,
3634 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3635 template<class _Yp, class _Dp, class _Alloc>
3636 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3637 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3639 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003640 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003642 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003646 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003647 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003648#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003650 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003651 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003652 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003653 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003654#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003656 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003657#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003658#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003659 template<class _Yp>
3660 shared_ptr(auto_ptr<_Yp>&& __r,
3661 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003662#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003663 template<class _Yp>
3664 shared_ptr(auto_ptr<_Yp> __r,
3665 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003667#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003668#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003669 template <class _Yp, class _Dp>
3670 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3671 typename enable_if
3672 <
3673 !is_lvalue_reference<_Dp>::value &&
3674 !is_array<_Yp>::value &&
3675 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3676 __nat
3677 >::type = __nat());
3678 template <class _Yp, class _Dp>
3679 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3680 typename enable_if
3681 <
3682 is_lvalue_reference<_Dp>::value &&
3683 !is_array<_Yp>::value &&
3684 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3685 __nat
3686 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003687#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003688 template <class _Yp, class _Dp>
3689 shared_ptr(unique_ptr<_Yp, _Dp>,
3690 typename enable_if
3691 <
3692 !is_lvalue_reference<_Dp>::value &&
3693 !is_array<_Yp>::value &&
3694 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3695 __nat
3696 >::type = __nat());
3697 template <class _Yp, class _Dp>
3698 shared_ptr(unique_ptr<_Yp, _Dp>,
3699 typename enable_if
3700 <
3701 is_lvalue_reference<_Dp>::value &&
3702 !is_array<_Yp>::value &&
3703 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3704 __nat
3705 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003706#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707
3708 ~shared_ptr();
3709
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003711 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003712 template<class _Yp>
3713 typename enable_if
3714 <
3715 is_convertible<_Yp*, element_type*>::value,
3716 shared_ptr&
3717 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003719 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003720#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003722 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003723 template<class _Yp>
3724 typename enable_if
3725 <
3726 is_convertible<_Yp*, element_type*>::value,
3727 shared_ptr<_Tp>&
3728 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003730 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003731#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003732 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003734 typename enable_if
3735 <
3736 !is_array<_Yp>::value &&
3737 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003738 shared_ptr
3739 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003740 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003741#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003742#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
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,
3750 shared_ptr&
3751 >::type
3752 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003754#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003755 template <class _Yp, class _Dp>
3756 typename enable_if
3757 <
3758 !is_array<_Yp>::value &&
3759 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3760 shared_ptr&
3761 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003762#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003764 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003765#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003767 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768#endif
3769
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003771 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003773 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003774 template<class _Yp>
3775 typename enable_if
3776 <
3777 is_convertible<_Yp*, element_type*>::value,
3778 void
3779 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003781 reset(_Yp* __p);
3782 template<class _Yp, class _Dp>
3783 typename enable_if
3784 <
3785 is_convertible<_Yp*, element_type*>::value,
3786 void
3787 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003789 reset(_Yp* __p, _Dp __d);
3790 template<class _Yp, class _Dp, class _Alloc>
3791 typename enable_if
3792 <
3793 is_convertible<_Yp*, element_type*>::value,
3794 void
3795 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003797 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798
Howard Hinnant756c69b2010-09-22 16:48:34 +00003799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003800 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003802 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3803 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003805 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003807 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003809 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003811 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003812 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003813 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003814 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003816 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003817 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003818 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003820 _LIBCPP_INLINE_VISIBILITY
3821 bool
3822 __owner_equivalent(const shared_ptr& __p) const
3823 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824
Howard Hinnant72f73582010-08-11 17:04:31 +00003825#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003828 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003829 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003830 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003831 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003832#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003833
Zoe Carver0934c932019-08-14 17:19:25 +00003834 template<class _Yp, class _CntrlBlk>
3835 static shared_ptr<_Tp>
3836 __create_with_cntrl_block(_Yp* __p, _CntrlBlk* __cntrl)
3837 {
3838 shared_ptr<_Tp> __r;
3839 __r.__ptr_ = __p;
3840 __r.__cntrl_ = __cntrl;
3841 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3842 return __r;
3843 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844
3845private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003846 template <class _Yp, bool = is_function<_Yp>::value>
3847 struct __shared_ptr_default_allocator
3848 {
3849 typedef allocator<_Yp> type;
3850 };
3851
3852 template <class _Yp>
3853 struct __shared_ptr_default_allocator<_Yp, true>
3854 {
3855 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3856 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003858 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003859 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003860 typename enable_if<is_convertible<_OrigPtr*,
3861 const enable_shared_from_this<_Yp>*
3862 >::value,
3863 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003864 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3865 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003867 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003868 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003869 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003870 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3871 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003872 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873 }
3874
Erik Pilkington2a398762017-05-25 15:43:31 +00003875 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003877 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3878 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879};
3880
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003881
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003883inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003884_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003885shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886 : __ptr_(0),
3887 __cntrl_(0)
3888{
3889}
3890
3891template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003892inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003893_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003894shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895 : __ptr_(0),
3896 __cntrl_(0)
3897{
3898}
3899
3900template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003901template<class _Yp>
3902shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3903 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904 : __ptr_(__p)
3905{
3906 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003907 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3908 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3909 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003910 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003911 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912}
3913
3914template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003915template<class _Yp, class _Dp>
3916shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3917 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918 : __ptr_(__p)
3919{
3920#ifndef _LIBCPP_NO_EXCEPTIONS
3921 try
3922 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003923#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003924 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3925 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3926 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003927 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928#ifndef _LIBCPP_NO_EXCEPTIONS
3929 }
3930 catch (...)
3931 {
3932 __d(__p);
3933 throw;
3934 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003935#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936}
3937
3938template<class _Tp>
3939template<class _Dp>
3940shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3941 : __ptr_(0)
3942{
3943#ifndef _LIBCPP_NO_EXCEPTIONS
3944 try
3945 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003946#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003947 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3948 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3949 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950#ifndef _LIBCPP_NO_EXCEPTIONS
3951 }
3952 catch (...)
3953 {
3954 __d(__p);
3955 throw;
3956 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003957#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958}
3959
3960template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003961template<class _Yp, class _Dp, class _Alloc>
3962shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3963 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003964 : __ptr_(__p)
3965{
3966#ifndef _LIBCPP_NO_EXCEPTIONS
3967 try
3968 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003969#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003970 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003971 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972 typedef __allocator_destructor<_A2> _D2;
3973 _A2 __a2(__a);
3974 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003975 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3976 _CntrlBlk(__p, __d, __a);
3977 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003978 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979#ifndef _LIBCPP_NO_EXCEPTIONS
3980 }
3981 catch (...)
3982 {
3983 __d(__p);
3984 throw;
3985 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003986#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987}
3988
3989template<class _Tp>
3990template<class _Dp, class _Alloc>
3991shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3992 : __ptr_(0)
3993{
3994#ifndef _LIBCPP_NO_EXCEPTIONS
3995 try
3996 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003997#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003999 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000 typedef __allocator_destructor<_A2> _D2;
4001 _A2 __a2(__a);
4002 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004003 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4004 _CntrlBlk(__p, __d, __a);
4005 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006#ifndef _LIBCPP_NO_EXCEPTIONS
4007 }
4008 catch (...)
4009 {
4010 __d(__p);
4011 throw;
4012 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004013#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014}
4015
4016template<class _Tp>
4017template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004018inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004019shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020 : __ptr_(__p),
4021 __cntrl_(__r.__cntrl_)
4022{
4023 if (__cntrl_)
4024 __cntrl_->__add_shared();
4025}
4026
4027template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004028inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004029shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030 : __ptr_(__r.__ptr_),
4031 __cntrl_(__r.__cntrl_)
4032{
4033 if (__cntrl_)
4034 __cntrl_->__add_shared();
4035}
4036
4037template<class _Tp>
4038template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004039inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004041 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004042 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043 : __ptr_(__r.__ptr_),
4044 __cntrl_(__r.__cntrl_)
4045{
4046 if (__cntrl_)
4047 __cntrl_->__add_shared();
4048}
4049
Howard Hinnant74279a52010-09-04 23:28:19 +00004050#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051
4052template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004053inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004054shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055 : __ptr_(__r.__ptr_),
4056 __cntrl_(__r.__cntrl_)
4057{
4058 __r.__ptr_ = 0;
4059 __r.__cntrl_ = 0;
4060}
4061
4062template<class _Tp>
4063template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004064inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004066 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004067 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068 : __ptr_(__r.__ptr_),
4069 __cntrl_(__r.__cntrl_)
4070{
4071 __r.__ptr_ = 0;
4072 __r.__cntrl_ = 0;
4073}
4074
Howard Hinnant74279a52010-09-04 23:28:19 +00004075#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076
Marshall Clowb22274f2017-01-24 22:22:33 +00004077#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004079template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004080#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004081shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004083shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004085 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086 : __ptr_(__r.get())
4087{
4088 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4089 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004090 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091 __r.release();
4092}
Marshall Clowb22274f2017-01-24 22:22:33 +00004093#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094
4095template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004096template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004097#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4099#else
4100shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4101#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004102 typename enable_if
4103 <
4104 !is_lvalue_reference<_Dp>::value &&
4105 !is_array<_Yp>::value &&
4106 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4107 __nat
4108 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109 : __ptr_(__r.get())
4110{
Marshall Clow35cde742015-05-10 13:59:45 +00004111#if _LIBCPP_STD_VER > 11
4112 if (__ptr_ == nullptr)
4113 __cntrl_ = nullptr;
4114 else
4115#endif
4116 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004117 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4118 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4119 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004120 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004121 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122 __r.release();
4123}
4124
4125template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004126template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004127#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4129#else
4130shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4131#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004132 typename enable_if
4133 <
4134 is_lvalue_reference<_Dp>::value &&
4135 !is_array<_Yp>::value &&
4136 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4137 __nat
4138 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139 : __ptr_(__r.get())
4140{
Marshall Clow35cde742015-05-10 13:59:45 +00004141#if _LIBCPP_STD_VER > 11
4142 if (__ptr_ == nullptr)
4143 __cntrl_ = nullptr;
4144 else
4145#endif
4146 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004147 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004148 typedef __shared_ptr_pointer<_Yp*,
4149 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004150 _AllocT > _CntrlBlk;
4151 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004152 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004153 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154 __r.release();
4155}
4156
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157template<class _Tp>
4158shared_ptr<_Tp>::~shared_ptr()
4159{
4160 if (__cntrl_)
4161 __cntrl_->__release_shared();
4162}
4163
4164template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004165inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004167shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168{
4169 shared_ptr(__r).swap(*this);
4170 return *this;
4171}
4172
4173template<class _Tp>
4174template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004175inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004176typename enable_if
4177<
Marshall Clow7e384b72017-01-10 16:59:33 +00004178 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004179 shared_ptr<_Tp>&
4180>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004181shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182{
4183 shared_ptr(__r).swap(*this);
4184 return *this;
4185}
4186
Howard Hinnant74279a52010-09-04 23:28:19 +00004187#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188
4189template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004190inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004192shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004194 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195 return *this;
4196}
4197
4198template<class _Tp>
4199template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004200inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004201typename enable_if
4202<
Marshall Clow7e384b72017-01-10 16:59:33 +00004203 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004204 shared_ptr<_Tp>&
4205>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4207{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004208 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209 return *this;
4210}
4211
Marshall Clowb22274f2017-01-24 22:22:33 +00004212#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213template<class _Tp>
4214template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004215inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004216typename enable_if
4217<
4218 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004219 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004220 shared_ptr<_Tp>
4221>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4223{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004224 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225 return *this;
4226}
Marshall Clowb22274f2017-01-24 22:22:33 +00004227#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004228
4229template<class _Tp>
4230template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004231inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004232typename enable_if
4233<
4234 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004235 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004236 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004237 shared_ptr<_Tp>&
4238>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4240{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004241 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242 return *this;
4243}
4244
Howard Hinnant74279a52010-09-04 23:28:19 +00004245#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246
Marshall Clowb22274f2017-01-24 22:22:33 +00004247#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248template<class _Tp>
4249template<class _Yp>
4250inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004251typename enable_if
4252<
4253 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004254 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004255 shared_ptr<_Tp>&
4256>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004257shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258{
4259 shared_ptr(__r).swap(*this);
4260 return *this;
4261}
Marshall Clowb22274f2017-01-24 22:22:33 +00004262#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263
4264template<class _Tp>
4265template <class _Yp, class _Dp>
4266inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004267typename enable_if
4268<
4269 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004270 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004271 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004272 shared_ptr<_Tp>&
4273>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4275{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004276 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277 return *this;
4278}
4279
Howard Hinnant74279a52010-09-04 23:28:19 +00004280#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281
4282template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004283inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284void
Howard Hinnant719bda32011-05-28 14:41:13 +00004285shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004287 _VSTD::swap(__ptr_, __r.__ptr_);
4288 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289}
4290
4291template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004292inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293void
Howard Hinnant719bda32011-05-28 14:41:13 +00004294shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004295{
4296 shared_ptr().swap(*this);
4297}
4298
4299template<class _Tp>
4300template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004301inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004302typename enable_if
4303<
Marshall Clow7e384b72017-01-10 16:59:33 +00004304 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004305 void
4306>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307shared_ptr<_Tp>::reset(_Yp* __p)
4308{
4309 shared_ptr(__p).swap(*this);
4310}
4311
4312template<class _Tp>
4313template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004314inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004315typename enable_if
4316<
Marshall Clow7e384b72017-01-10 16:59:33 +00004317 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004318 void
4319>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004320shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4321{
4322 shared_ptr(__p, __d).swap(*this);
4323}
4324
4325template<class _Tp>
4326template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004327inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004328typename enable_if
4329<
Marshall Clow7e384b72017-01-10 16:59:33 +00004330 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004331 void
4332>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4334{
4335 shared_ptr(__p, __d, __a).swap(*this);
4336}
4337
4338#ifndef _LIBCPP_HAS_NO_VARIADICS
4339
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004340template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004342typename enable_if
4343<
4344 !is_array<_Tp>::value,
4345 shared_ptr<_Tp>
4346>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347make_shared(_Args&& ...__args)
4348{
Zoe Carver0934c932019-08-14 17:19:25 +00004349 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4350 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4351 typedef allocator<_CntrlBlk> _A2;
4352 typedef __allocator_destructor<_A2> _D2;
4353
4354 _A2 __a2;
4355 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4356 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4357
4358 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4359 __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360}
4361
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004362template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004364typename enable_if
4365<
4366 !is_array<_Tp>::value,
4367 shared_ptr<_Tp>
4368>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369allocate_shared(const _Alloc& __a, _Args&& ...__args)
4370{
Zoe Carver0934c932019-08-14 17:19:25 +00004371 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4372
4373 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4374 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4375 typedef __allocator_destructor<_A2> _D2;
4376
4377 _A2 __a2(__a);
4378 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4379 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4380 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4381
4382 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4383 _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384}
4385
4386#else // _LIBCPP_HAS_NO_VARIADICS
4387
4388template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004389shared_ptr<_Tp>
4390make_shared()
4391{
Zoe Carver0934c932019-08-14 17:19:25 +00004392 static_assert((is_constructible<_Tp>::value), "Can't construct object in make_shared" );
4393 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4394 typedef allocator<_CntrlBlk> _Alloc2;
4395 typedef __allocator_destructor<_Alloc2> _D2;
4396 _Alloc2 __alloc2;
4397 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4398 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4399 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4400 __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004401}
4402
4403template<class _Tp, class _A0>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404shared_ptr<_Tp>
4405make_shared(_A0& __a0)
4406{
Zoe Carver0934c932019-08-14 17:19:25 +00004407 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in make_shared" );
4408 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4409 typedef allocator<_CntrlBlk> _Alloc2;
4410 typedef __allocator_destructor<_Alloc2> _D2;
4411 _Alloc2 __alloc2;
4412 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4413 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4414 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4415 __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416}
4417
4418template<class _Tp, class _A0, class _A1>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004419shared_ptr<_Tp>
4420make_shared(_A0& __a0, _A1& __a1)
4421{
Zoe Carver0934c932019-08-14 17:19:25 +00004422 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in make_shared" );
4423 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4424 typedef allocator<_CntrlBlk> _Alloc2;
4425 typedef __allocator_destructor<_Alloc2> _D2;
4426 _Alloc2 __alloc2;
4427 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4428 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4429 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4430 __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004431}
4432
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004433template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434shared_ptr<_Tp>
4435make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4436{
Zoe Carver0934c932019-08-14 17:19:25 +00004437 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in make_shared" );
4438 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4439 typedef allocator<_CntrlBlk> _Alloc2;
4440 typedef __allocator_destructor<_Alloc2> _D2;
4441 _Alloc2 __alloc2;
4442 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4443 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4444 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4445 __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446}
4447
4448template<class _Tp, class _Alloc>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004449shared_ptr<_Tp>
4450allocate_shared(const _Alloc& __a)
4451{
Zoe Carver0934c932019-08-14 17:19:25 +00004452 static_assert((is_constructible<_Tp>::value), "Can't construct object in allocate_shared" );
4453 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4454 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4455 typedef __allocator_destructor<_Alloc2> _D2;
4456 _Alloc2 __alloc2(__a);
4457 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4458 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4459 _CntrlBlk(__a);
4460 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4461 _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462}
4463
4464template<class _Tp, class _Alloc, class _A0>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004465shared_ptr<_Tp>
4466allocate_shared(const _Alloc& __a, _A0& __a0)
4467{
Zoe Carver0934c932019-08-14 17:19:25 +00004468 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in allocate_shared" );
4469 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4470 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4471 typedef __allocator_destructor<_Alloc2> _D2;
4472 _Alloc2 __alloc2(__a);
4473 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4474 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4475 _CntrlBlk(__a, __a0);
4476 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4477 _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004478}
4479
4480template<class _Tp, class _Alloc, class _A0, class _A1>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481shared_ptr<_Tp>
4482allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4483{
Zoe Carver0934c932019-08-14 17:19:25 +00004484 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in allocate_shared" );
4485 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4486 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4487 typedef __allocator_destructor<_Alloc2> _D2;
4488 _Alloc2 __alloc2(__a);
4489 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4490 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4491 _CntrlBlk(__a, __a0, __a1);
4492 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4493 _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004494}
4495
4496template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497shared_ptr<_Tp>
4498allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4499{
Zoe Carver0934c932019-08-14 17:19:25 +00004500 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in allocate_shared" );
4501 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4502 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4503 typedef __allocator_destructor<_Alloc2> _D2;
4504 _Alloc2 __alloc2(__a);
4505 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4506 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4507 _CntrlBlk(__a, __a0, __a1, __a2);
4508 return shared_ptr<_Tp>::__create_with_cntrl_block(__hold2.get()->get(),
4509 _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004510}
4511
4512#endif // _LIBCPP_HAS_NO_VARIADICS
4513
4514template<class _Tp, class _Up>
4515inline _LIBCPP_INLINE_VISIBILITY
4516bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004517operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004518{
4519 return __x.get() == __y.get();
4520}
4521
4522template<class _Tp, class _Up>
4523inline _LIBCPP_INLINE_VISIBILITY
4524bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004525operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004526{
4527 return !(__x == __y);
4528}
4529
4530template<class _Tp, class _Up>
4531inline _LIBCPP_INLINE_VISIBILITY
4532bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004533operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004534{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004535#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004536 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4537 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004538#else
4539 return less<>()(__x.get(), __y.get());
4540#endif
4541
Howard Hinnantb17caf92012-02-21 21:02:58 +00004542}
4543
4544template<class _Tp, class _Up>
4545inline _LIBCPP_INLINE_VISIBILITY
4546bool
4547operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4548{
4549 return __y < __x;
4550}
4551
4552template<class _Tp, class _Up>
4553inline _LIBCPP_INLINE_VISIBILITY
4554bool
4555operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4556{
4557 return !(__y < __x);
4558}
4559
4560template<class _Tp, class _Up>
4561inline _LIBCPP_INLINE_VISIBILITY
4562bool
4563operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4564{
4565 return !(__x < __y);
4566}
4567
4568template<class _Tp>
4569inline _LIBCPP_INLINE_VISIBILITY
4570bool
4571operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4572{
4573 return !__x;
4574}
4575
4576template<class _Tp>
4577inline _LIBCPP_INLINE_VISIBILITY
4578bool
4579operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4580{
4581 return !__x;
4582}
4583
4584template<class _Tp>
4585inline _LIBCPP_INLINE_VISIBILITY
4586bool
4587operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4588{
4589 return static_cast<bool>(__x);
4590}
4591
4592template<class _Tp>
4593inline _LIBCPP_INLINE_VISIBILITY
4594bool
4595operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4596{
4597 return static_cast<bool>(__x);
4598}
4599
4600template<class _Tp>
4601inline _LIBCPP_INLINE_VISIBILITY
4602bool
4603operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4604{
4605 return less<_Tp*>()(__x.get(), nullptr);
4606}
4607
4608template<class _Tp>
4609inline _LIBCPP_INLINE_VISIBILITY
4610bool
4611operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4612{
4613 return less<_Tp*>()(nullptr, __x.get());
4614}
4615
4616template<class _Tp>
4617inline _LIBCPP_INLINE_VISIBILITY
4618bool
4619operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4620{
4621 return nullptr < __x;
4622}
4623
4624template<class _Tp>
4625inline _LIBCPP_INLINE_VISIBILITY
4626bool
4627operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4628{
4629 return __x < nullptr;
4630}
4631
4632template<class _Tp>
4633inline _LIBCPP_INLINE_VISIBILITY
4634bool
4635operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4636{
4637 return !(nullptr < __x);
4638}
4639
4640template<class _Tp>
4641inline _LIBCPP_INLINE_VISIBILITY
4642bool
4643operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4644{
4645 return !(__x < nullptr);
4646}
4647
4648template<class _Tp>
4649inline _LIBCPP_INLINE_VISIBILITY
4650bool
4651operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4652{
4653 return !(__x < nullptr);
4654}
4655
4656template<class _Tp>
4657inline _LIBCPP_INLINE_VISIBILITY
4658bool
4659operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4660{
4661 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004662}
4663
4664template<class _Tp>
4665inline _LIBCPP_INLINE_VISIBILITY
4666void
Howard Hinnant719bda32011-05-28 14:41:13 +00004667swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004668{
4669 __x.swap(__y);
4670}
4671
4672template<class _Tp, class _Up>
4673inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004674typename enable_if
4675<
4676 !is_array<_Tp>::value && !is_array<_Up>::value,
4677 shared_ptr<_Tp>
4678>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004679static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004680{
4681 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4682}
4683
4684template<class _Tp, class _Up>
4685inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004686typename enable_if
4687<
4688 !is_array<_Tp>::value && !is_array<_Up>::value,
4689 shared_ptr<_Tp>
4690>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004691dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004692{
4693 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4694 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4695}
4696
4697template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004698typename enable_if
4699<
4700 is_array<_Tp>::value == is_array<_Up>::value,
4701 shared_ptr<_Tp>
4702>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004703const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004704{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004705 typedef typename remove_extent<_Tp>::type _RTp;
4706 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004707}
4708
Howard Hinnant72f73582010-08-11 17:04:31 +00004709#ifndef _LIBCPP_NO_RTTI
4710
Howard Hinnantc51e1022010-05-11 19:42:16 +00004711template<class _Dp, class _Tp>
4712inline _LIBCPP_INLINE_VISIBILITY
4713_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004714get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004715{
4716 return __p.template __get_deleter<_Dp>();
4717}
4718
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004719#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004720
Howard Hinnantc51e1022010-05-11 19:42:16 +00004721template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004722class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004723{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004724public:
4725 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004726private:
4727 element_type* __ptr_;
4728 __shared_weak_count* __cntrl_;
4729
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004730public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004732 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004733 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004734 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4735 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004737 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004738 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004739 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4740 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004741
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004742#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004744 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004745 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004746 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4747 _NOEXCEPT;
4748#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004749 ~weak_ptr();
4750
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004752 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004753 template<class _Yp>
4754 typename enable_if
4755 <
4756 is_convertible<_Yp*, element_type*>::value,
4757 weak_ptr&
4758 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004760 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4761
4762#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4763
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004765 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4766 template<class _Yp>
4767 typename enable_if
4768 <
4769 is_convertible<_Yp*, element_type*>::value,
4770 weak_ptr&
4771 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004773 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4774
4775#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4776
4777 template<class _Yp>
4778 typename enable_if
4779 <
4780 is_convertible<_Yp*, element_type*>::value,
4781 weak_ptr&
4782 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004784 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004785
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004787 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004789 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004790
Howard Hinnant756c69b2010-09-22 16:48:34 +00004791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004792 long use_count() const _NOEXCEPT
4793 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004795 bool expired() const _NOEXCEPT
4796 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4797 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004798 template<class _Up>
4799 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004800 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004801 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004802 template<class _Up>
4803 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004804 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004805 {return __cntrl_ < __r.__cntrl_;}
4806
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004807 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4808 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004809};
4810
4811template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004812inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004813_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004814weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004815 : __ptr_(0),
4816 __cntrl_(0)
4817{
4818}
4819
4820template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004821inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004822weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004823 : __ptr_(__r.__ptr_),
4824 __cntrl_(__r.__cntrl_)
4825{
4826 if (__cntrl_)
4827 __cntrl_->__add_weak();
4828}
4829
4830template<class _Tp>
4831template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004832inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004833weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004834 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004835 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004836 : __ptr_(__r.__ptr_),
4837 __cntrl_(__r.__cntrl_)
4838{
4839 if (__cntrl_)
4840 __cntrl_->__add_weak();
4841}
4842
4843template<class _Tp>
4844template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004845inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004846weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004847 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004848 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004849 : __ptr_(__r.__ptr_),
4850 __cntrl_(__r.__cntrl_)
4851{
4852 if (__cntrl_)
4853 __cntrl_->__add_weak();
4854}
4855
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004856#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4857
4858template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004859inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004860weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4861 : __ptr_(__r.__ptr_),
4862 __cntrl_(__r.__cntrl_)
4863{
4864 __r.__ptr_ = 0;
4865 __r.__cntrl_ = 0;
4866}
4867
4868template<class _Tp>
4869template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004870inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004871weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4872 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4873 _NOEXCEPT
4874 : __ptr_(__r.__ptr_),
4875 __cntrl_(__r.__cntrl_)
4876{
4877 __r.__ptr_ = 0;
4878 __r.__cntrl_ = 0;
4879}
4880
4881#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4882
Howard Hinnantc51e1022010-05-11 19:42:16 +00004883template<class _Tp>
4884weak_ptr<_Tp>::~weak_ptr()
4885{
4886 if (__cntrl_)
4887 __cntrl_->__release_weak();
4888}
4889
4890template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004891inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004892weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004893weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004894{
4895 weak_ptr(__r).swap(*this);
4896 return *this;
4897}
4898
4899template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004900template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004901inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004902typename enable_if
4903<
4904 is_convertible<_Yp*, _Tp*>::value,
4905 weak_ptr<_Tp>&
4906>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004907weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908{
4909 weak_ptr(__r).swap(*this);
4910 return *this;
4911}
4912
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004913#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4914
4915template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004916inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004917weak_ptr<_Tp>&
4918weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4919{
4920 weak_ptr(_VSTD::move(__r)).swap(*this);
4921 return *this;
4922}
4923
Howard Hinnantc51e1022010-05-11 19:42:16 +00004924template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004925template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004926inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004927typename enable_if
4928<
4929 is_convertible<_Yp*, _Tp*>::value,
4930 weak_ptr<_Tp>&
4931>::type
4932weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4933{
4934 weak_ptr(_VSTD::move(__r)).swap(*this);
4935 return *this;
4936}
4937
4938#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4939
4940template<class _Tp>
4941template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004942inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004943typename enable_if
4944<
4945 is_convertible<_Yp*, _Tp*>::value,
4946 weak_ptr<_Tp>&
4947>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004948weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004949{
4950 weak_ptr(__r).swap(*this);
4951 return *this;
4952}
4953
4954template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004955inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004956void
Howard Hinnant719bda32011-05-28 14:41:13 +00004957weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004958{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004959 _VSTD::swap(__ptr_, __r.__ptr_);
4960 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004961}
4962
4963template<class _Tp>
4964inline _LIBCPP_INLINE_VISIBILITY
4965void
Howard Hinnant719bda32011-05-28 14:41:13 +00004966swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004967{
4968 __x.swap(__y);
4969}
4970
4971template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004972inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004973void
Howard Hinnant719bda32011-05-28 14:41:13 +00004974weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975{
4976 weak_ptr().swap(*this);
4977}
4978
4979template<class _Tp>
4980template<class _Yp>
4981shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004982 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004983 : __ptr_(__r.__ptr_),
4984 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4985{
4986 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004987 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004988}
4989
4990template<class _Tp>
4991shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004992weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004993{
4994 shared_ptr<_Tp> __r;
4995 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4996 if (__r.__cntrl_)
4997 __r.__ptr_ = __ptr_;
4998 return __r;
4999}
5000
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005001#if _LIBCPP_STD_VER > 14
5002template <class _Tp = void> struct owner_less;
5003#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005004template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005005#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005006
5007template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005008struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005009 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005010{
5011 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005012 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005013 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005014 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005015 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005016 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005017 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005018 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005019 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005020 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005021};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005022
5023template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005024struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005025 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5026{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005027 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005028 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005029 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005030 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005031 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005032 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005033 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005034 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005035 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005036 {return __x.owner_before(__y);}
5037};
5038
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005039#if _LIBCPP_STD_VER > 14
5040template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005041struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005042{
5043 template <class _Tp, class _Up>
5044 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005045 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005046 {return __x.owner_before(__y);}
5047 template <class _Tp, class _Up>
5048 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005049 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005050 {return __x.owner_before(__y);}
5051 template <class _Tp, class _Up>
5052 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005053 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005054 {return __x.owner_before(__y);}
5055 template <class _Tp, class _Up>
5056 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005057 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005058 {return __x.owner_before(__y);}
5059 typedef void is_transparent;
5060};
5061#endif
5062
Howard Hinnantc51e1022010-05-11 19:42:16 +00005063template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005064class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005065{
5066 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005067protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005068 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005069 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005071 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005073 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5074 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005076 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005077public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005079 shared_ptr<_Tp> shared_from_this()
5080 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005082 shared_ptr<_Tp const> shared_from_this() const
5083 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005084
Eric Fiselier84006862016-06-02 00:15:35 +00005085#if _LIBCPP_STD_VER > 14
5086 _LIBCPP_INLINE_VISIBILITY
5087 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5088 { return __weak_this_; }
5089
5090 _LIBCPP_INLINE_VISIBILITY
5091 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5092 { return __weak_this_; }
5093#endif // _LIBCPP_STD_VER > 14
5094
Howard Hinnantc51e1022010-05-11 19:42:16 +00005095 template <class _Up> friend class shared_ptr;
5096};
5097
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005098template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005099struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005100{
5101 typedef shared_ptr<_Tp> argument_type;
5102 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005103
Howard Hinnant756c69b2010-09-22 16:48:34 +00005104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005105 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005106 {
5107 return hash<_Tp*>()(__ptr.get());
5108 }
5109};
5110
Howard Hinnantc834c512011-11-29 18:15:50 +00005111template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005112inline _LIBCPP_INLINE_VISIBILITY
5113basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005114operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005115
Eric Fiselier9b492672016-06-18 02:12:53 +00005116
5117#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005118
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005119class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005120{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005121 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005122public:
5123 void lock() _NOEXCEPT;
5124 void unlock() _NOEXCEPT;
5125
5126private:
5127 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5128 __sp_mut(const __sp_mut&);
5129 __sp_mut& operator=(const __sp_mut&);
5130
Howard Hinnant8331b762013-03-06 23:30:19 +00005131 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005132};
5133
Mehdi Amini228053d2017-05-04 17:08:54 +00005134_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5135__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005136
5137template <class _Tp>
5138inline _LIBCPP_INLINE_VISIBILITY
5139bool
5140atomic_is_lock_free(const shared_ptr<_Tp>*)
5141{
5142 return false;
5143}
5144
5145template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005146_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005147shared_ptr<_Tp>
5148atomic_load(const shared_ptr<_Tp>* __p)
5149{
5150 __sp_mut& __m = __get_sp_mut(__p);
5151 __m.lock();
5152 shared_ptr<_Tp> __q = *__p;
5153 __m.unlock();
5154 return __q;
5155}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005156
Howard Hinnant9fa30202012-07-30 01:40:57 +00005157template <class _Tp>
5158inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005159_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005160shared_ptr<_Tp>
5161atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5162{
5163 return atomic_load(__p);
5164}
5165
5166template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005167_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005168void
5169atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5170{
5171 __sp_mut& __m = __get_sp_mut(__p);
5172 __m.lock();
5173 __p->swap(__r);
5174 __m.unlock();
5175}
5176
5177template <class _Tp>
5178inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005179_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005180void
5181atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5182{
5183 atomic_store(__p, __r);
5184}
5185
5186template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005187_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005188shared_ptr<_Tp>
5189atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5190{
5191 __sp_mut& __m = __get_sp_mut(__p);
5192 __m.lock();
5193 __p->swap(__r);
5194 __m.unlock();
5195 return __r;
5196}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005197
Howard Hinnant9fa30202012-07-30 01:40:57 +00005198template <class _Tp>
5199inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005200_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005201shared_ptr<_Tp>
5202atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5203{
5204 return atomic_exchange(__p, __r);
5205}
5206
5207template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005208_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005209bool
5210atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5211{
Marshall Clow4201ee82016-05-18 17:50:13 +00005212 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005213 __sp_mut& __m = __get_sp_mut(__p);
5214 __m.lock();
5215 if (__p->__owner_equivalent(*__v))
5216 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005217 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005218 *__p = __w;
5219 __m.unlock();
5220 return true;
5221 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005222 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005223 *__v = *__p;
5224 __m.unlock();
5225 return false;
5226}
5227
5228template <class _Tp>
5229inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005230_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005231bool
5232atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5233{
5234 return atomic_compare_exchange_strong(__p, __v, __w);
5235}
5236
5237template <class _Tp>
5238inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005239_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005240bool
5241atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5242 shared_ptr<_Tp> __w, memory_order, memory_order)
5243{
5244 return atomic_compare_exchange_strong(__p, __v, __w);
5245}
5246
5247template <class _Tp>
5248inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005249_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005250bool
5251atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5252 shared_ptr<_Tp> __w, memory_order, memory_order)
5253{
5254 return atomic_compare_exchange_weak(__p, __v, __w);
5255}
5256
Eric Fiselier9b492672016-06-18 02:12:53 +00005257#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005258
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005259//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005260#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5261# ifndef _LIBCPP_CXX03_LANG
5262enum class pointer_safety : unsigned char {
5263 relaxed,
5264 preferred,
5265 strict
5266};
5267# endif
5268#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005269struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005270{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005271 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005272 {
5273 relaxed,
5274 preferred,
5275 strict
5276 };
5277
Howard Hinnant49e145e2012-10-30 19:06:59 +00005278 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005279
Howard Hinnant756c69b2010-09-22 16:48:34 +00005280 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005281 pointer_safety() : __v_() {}
5282
5283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005284 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005286 operator int() const {return __v_;}
5287};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005288#endif
5289
5290#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005291 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005292_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5293#else
5294// This function is only offered in C++03 under ABI v1.
5295# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5296inline _LIBCPP_INLINE_VISIBILITY
5297pointer_safety get_pointer_safety() _NOEXCEPT {
5298 return pointer_safety::relaxed;
5299}
5300# endif
5301#endif
5302
Howard Hinnantc51e1022010-05-11 19:42:16 +00005303
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005304_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5305_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5306_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005307_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005308
5309template <class _Tp>
5310inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005311_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005312undeclare_reachable(_Tp* __p)
5313{
5314 return static_cast<_Tp*>(__undeclare_reachable(__p));
5315}
5316
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005317_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005318
Marshall Clow8982dcd2015-07-13 20:04:56 +00005319// --- Helper for container swap --
5320template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005321inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005322void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5323#if _LIBCPP_STD_VER >= 14
5324 _NOEXCEPT
5325#else
5326 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5327#endif
5328{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005329 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005330 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5331}
5332
5333template <typename _Alloc>
5334_LIBCPP_INLINE_VISIBILITY
5335void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5336#if _LIBCPP_STD_VER >= 14
5337 _NOEXCEPT
5338#else
5339 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5340#endif
5341{
5342 using _VSTD::swap;
5343 swap(__a1, __a2);
5344}
5345
5346template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005347inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005348void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5349
Marshall Clowff91de82015-08-18 19:51:37 +00005350template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005351struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005352 _Traits::propagate_on_container_move_assignment::value
5353#if _LIBCPP_STD_VER > 14
5354 || _Traits::is_always_equal::value
5355#else
5356 && is_nothrow_move_assignable<_Alloc>::value
5357#endif
5358 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005359
Marshall Clowa591b9a2016-07-11 21:38:08 +00005360
5361#ifndef _LIBCPP_HAS_NO_VARIADICS
5362template <class _Tp, class _Alloc>
5363struct __temp_value {
5364 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005365
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005366 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005367 _Alloc &__a;
5368
5369 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5370 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005371
Marshall Clowa591b9a2016-07-11 21:38:08 +00005372 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005373 _LIBCPP_NO_CFI
5374 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5375 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5376 _VSTD::forward<_Args>(__args)...);
5377 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005378
Marshall Clowa591b9a2016-07-11 21:38:08 +00005379 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5380 };
5381#endif
5382
Marshall Clowe46031a2018-07-02 18:41:15 +00005383template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005384struct __is_allocator : false_type {};
5385
5386template<typename _Alloc>
5387struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005388 typename __void_t<typename _Alloc::value_type>::type,
5389 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5390 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005391 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005392
Eric Fiselier74ebee62019-06-08 01:31:19 +00005393// __builtin_new_allocator -- A non-templated helper for allocating and
5394// deallocating memory using __builtin_operator_new and
5395// __builtin_operator_delete. It should be used in preference to
5396// `std::allocator<T>` to avoid additional instantiations.
5397struct __builtin_new_allocator {
5398 struct __builtin_new_deleter {
5399 typedef void* pointer_type;
5400
5401 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5402 : __size_(__size), __align_(__align) {}
5403
5404 void operator()(void* p) const _NOEXCEPT {
5405 std::__libcpp_deallocate(p, __size_, __align_);
5406 }
5407
5408 private:
5409 size_t __size_;
5410 size_t __align_;
5411 };
5412
5413 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5414
5415 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5416 return __holder_t(std::__libcpp_allocate(__s, __align),
5417 __builtin_new_deleter(__s, __align));
5418 }
5419
5420 static void __deallocate_bytes(void* __p, size_t __s,
5421 size_t __align) _NOEXCEPT {
5422 std::__libcpp_deallocate(__p, __s, __align);
5423 }
5424
5425 template <class _Tp>
5426 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5427 static __holder_t __allocate_type(size_t __n) {
5428 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5429 }
5430
5431 template <class _Tp>
5432 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5433 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5434 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5435 }
5436};
5437
5438
Howard Hinnantc51e1022010-05-11 19:42:16 +00005439_LIBCPP_END_NAMESPACE_STD
5440
Eric Fiselierf4433a32017-05-31 22:07:49 +00005441_LIBCPP_POP_MACROS
5442
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005443#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005444# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005445#endif
5446
Howard Hinnantc51e1022010-05-11 19:42:16 +00005447#endif // _LIBCPP_MEMORY