blob: 31dc73d8b14224c40f82148e8bcb376589d42bff [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000031
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000033
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant719bda32011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
46 static pointer pointer_to(<details>) noexcept;
47};
48
Howard Hinnantc51e1022010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
81 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
82 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
83
84 static pointer allocate(allocator_type& a, size_type n);
85 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
86
Howard Hinnant719bda32011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 template <class T, class... Args>
90 static void construct(allocator_type& a, T* p, Args&&... args);
91
92 template <class T>
93 static void destroy(allocator_type& a, T* p);
94
Marshall Clow4f834b52013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 static allocator_type
98 select_on_container_copy_construction(const allocator_type& a);
99};
100
101template <>
102class allocator<void>
103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef T* pointer;
119 typedef const T* const_pointer;
120 typedef typename add_lvalue_reference<T>::type reference;
121 typedef typename add_lvalue_reference<const T>::type const_reference;
122 typedef T value_type;
123
124 template <class U> struct rebind {typedef allocator<U> other;};
125
Howard Hinnant719bda32011-05-28 14:41:13 +0000126 allocator() noexcept;
127 allocator(const allocator&) noexcept;
128 template <class U> allocator(const allocator<U>&) noexcept;
129 ~allocator();
130 pointer address(reference x) const noexcept;
131 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 void deallocate(pointer p, size_type n) noexcept;
134 size_type max_size() const noexcept;
135 template<class U, class... Args>
136 void construct(U* p, Args&&... args);
137 template <class U>
138 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
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 OutputIterator, class T>
148class raw_storage_iterator
149 : public iterator<output_iterator_tag,
150 T, // purposefully not C++03
151 ptrdiff_t, // purposefully not C++03
152 T*, // purposefully not C++03
153 raw_storage_iterator&> // purposefully not C++03
154{
155public:
156 explicit raw_storage_iterator(OutputIterator x);
157 raw_storage_iterator& operator*();
158 raw_storage_iterator& operator=(const T& element);
159 raw_storage_iterator& operator++();
160 raw_storage_iterator operator++(int);
161};
162
Howard Hinnant719bda32011-05-28 14:41:13 +0000163template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
164template <class T> void return_temporary_buffer(T* p) noexcept;
165
166template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000167template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169template <class InputIterator, class ForwardIterator>
170ForwardIterator
171uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class InputIterator, class Size, class ForwardIterator>
174ForwardIterator
175uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
176
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177template <class ForwardIterator, class T>
178void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
179
180template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000181ForwardIterator
182uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000184template <class T>
185void destroy_at(T* location);
186
187template <class ForwardIterator>
188 void destroy(ForwardIterator first, ForwardIterator last);
189
190template <class ForwardIterator, class Size>
191 ForwardIterator destroy_n(ForwardIterator first, Size n);
192
193template <class InputIterator, class ForwardIterator>
194 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
195
196template <class InputIterator, class Size, class ForwardIterator>
197 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
198
199template <class ForwardIterator>
200 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
201
202template <class ForwardIterator, class Size>
203 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
204
205template <class ForwardIterator>
206 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
207
208template <class ForwardIterator, class Size>
209 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
210
Marshall Clowb22274f2017-01-24 22:22:33 +0000211template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
213template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000214class auto_ptr // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215{
216public:
217 typedef X element_type;
218
219 explicit auto_ptr(X* p =0) throw();
220 auto_ptr(auto_ptr&) throw();
221 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
222 auto_ptr& operator=(auto_ptr&) throw();
223 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
224 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
225 ~auto_ptr() throw();
226
227 typename add_lvalue_reference<X>::type operator*() const throw();
228 X* operator->() const throw();
229 X* get() const throw();
230 X* release() throw();
231 void reset(X* p =0) throw();
232
233 auto_ptr(auto_ptr_ref<X>) throw();
234 template<class Y> operator auto_ptr_ref<Y>() throw();
235 template<class Y> operator auto_ptr<Y>() throw();
236};
237
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000238template <class T>
239struct default_delete
240{
Howard Hinnant719bda32011-05-28 14:41:13 +0000241 constexpr default_delete() noexcept = default;
242 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000243
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000245};
246
247template <class T>
248struct default_delete<T[]>
249{
Howard Hinnant719bda32011-05-28 14:41:13 +0000250 constexpr default_delete() noexcept = default;
251 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000252 template <class U> void operator()(U*) const = delete;
253};
254
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255template <class T, class D = default_delete<T>>
256class unique_ptr
257{
258public:
259 typedef see below pointer;
260 typedef T element_type;
261 typedef D deleter_type;
262
263 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 constexpr unique_ptr() noexcept;
265 explicit unique_ptr(pointer p) noexcept;
266 unique_ptr(pointer p, see below d1) noexcept;
267 unique_ptr(pointer p, see below d2) noexcept;
268 unique_ptr(unique_ptr&& u) noexcept;
269 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000270 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000271 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000272 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000273 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000274
275 // destructor
276 ~unique_ptr();
277
278 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000279 unique_ptr& operator=(unique_ptr&& u) noexcept;
280 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
281 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282
283 // observers
284 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000285 pointer operator->() const noexcept;
286 pointer get() const noexcept;
287 deleter_type& get_deleter() noexcept;
288 const deleter_type& get_deleter() const noexcept;
289 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000290
291 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000292 pointer release() noexcept;
293 void reset(pointer p = pointer()) noexcept;
294 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000295};
296
297template <class T, class D>
298class unique_ptr<T[], D>
299{
300public:
301 typedef implementation-defined pointer;
302 typedef T element_type;
303 typedef D deleter_type;
304
305 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 constexpr unique_ptr() noexcept;
307 explicit unique_ptr(pointer p) noexcept;
308 unique_ptr(pointer p, see below d) noexcept;
309 unique_ptr(pointer p, see below d) noexcept;
310 unique_ptr(unique_ptr&& u) noexcept;
311 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000312
313 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000314 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 unique_ptr& operator=(unique_ptr&& u) noexcept;
318 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000319
320 // observers
321 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000322 pointer get() const noexcept;
323 deleter_type& get_deleter() noexcept;
324 const deleter_type& get_deleter() const noexcept;
325 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 pointer release() noexcept;
329 void reset(pointer p = pointer()) noexcept;
330 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000331 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000333};
334
335template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338template <class T1, class D1, class T2, class D2>
339 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
340template <class T1, class D1, class T2, class D2>
341 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
342template <class T1, class D1, class T2, class D2>
343 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350
Howard Hinnant719bda32011-05-28 14:41:13 +0000351template <class T, class D>
352 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
353template <class T, class D>
354 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
355template <class T, class D>
356 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359
360template <class T, class D>
361 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
362template <class T, class D>
363 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
364template <class T, class D>
365 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
376
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000377class bad_weak_ptr
378 : public std::exception
379{
Howard Hinnant719bda32011-05-28 14:41:13 +0000380 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000381};
382
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000383template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
384template<class T> unique_ptr<T> make_unique(size_t n); // C++14
385template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387template<class T>
388class shared_ptr
389{
390public:
391 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000392 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393
394 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000395 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396 template<class Y> explicit shared_ptr(Y* p);
397 template<class Y, class D> shared_ptr(Y* p, D d);
398 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
399 template <class D> shared_ptr(nullptr_t p, D d);
400 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
402 shared_ptr(const shared_ptr& r) noexcept;
403 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
404 shared_ptr(shared_ptr&& r) noexcept;
405 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000407 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000408 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
409 shared_ptr(nullptr_t) : shared_ptr() { }
410
411 // destructor:
412 ~shared_ptr();
413
414 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 shared_ptr& operator=(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
417 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000418 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000419 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
421
422 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000423 void swap(shared_ptr& r) noexcept;
424 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000425 template<class Y> void reset(Y* p);
426 template<class Y, class D> void reset(Y* p, D d);
427 template<class Y, class D, class A> void reset(Y* p, D d, A a);
428
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 // observers:
430 T* get() const noexcept;
431 T& operator*() const noexcept;
432 T* operator->() const noexcept;
433 long use_count() const noexcept;
434 bool unique() const noexcept;
435 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000436 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
437 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438};
439
440// shared_ptr comparisons:
441template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000443template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000444 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000445template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000446 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000447template<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;
453
454template <class T>
455 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
456template <class T>
457 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
458template <class T>
459 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
460template <class T>
461 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
462template <class T>
463 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
464template <class T>
465bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
466template <class T>
467 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
468template <class T>
469 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
470template <class T>
471 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
472template <class T>
473 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
474template <class T>
475 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
476template <class T>
477 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000478
479// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000480template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr casts:
483template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000484 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000485template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000486 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000488 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000489
490// shared_ptr I/O:
491template<class E, class T, class Y>
492 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
493
494// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000495template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497template<class T, class... Args>
498 shared_ptr<T> make_shared(Args&&... args);
499template<class T, class A, class... Args>
500 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
501
502template<class T>
503class weak_ptr
504{
505public:
506 typedef T element_type;
507
508 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000509 constexpr weak_ptr() noexcept;
510 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
511 weak_ptr(weak_ptr const& r) noexcept;
512 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000513 weak_ptr(weak_ptr&& r) noexcept; // C++14
514 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516 // destructor
517 ~weak_ptr();
518
519 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000520 weak_ptr& operator=(weak_ptr const& r) noexcept;
521 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
522 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000523 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
524 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000525
526 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 void swap(weak_ptr& r) noexcept;
528 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000529
530 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000531 long use_count() const noexcept;
532 bool expired() const noexcept;
533 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000534 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
535 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000536};
537
538// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000539template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000540
541// class owner_less:
542template<class T> struct owner_less;
543
544template<class T>
545struct owner_less<shared_ptr<T>>
546 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
547{
548 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000549 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
550 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
551 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000552};
553
554template<class T>
555struct owner_less<weak_ptr<T>>
556 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
557{
558 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000559 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
560 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
561 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
562};
563
564template <> // Added in C++14
565struct owner_less<void>
566{
567 template <class _Tp, class _Up>
568 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
569 template <class _Tp, class _Up>
570 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
571 template <class _Tp, class _Up>
572 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
573 template <class _Tp, class _Up>
574 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
575
576 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000577};
578
579template<class T>
580class enable_shared_from_this
581{
582protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000583 constexpr enable_shared_from_this() noexcept;
584 enable_shared_from_this(enable_shared_from_this const&) noexcept;
585 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000586 ~enable_shared_from_this();
587public:
588 shared_ptr<T> shared_from_this();
589 shared_ptr<T const> shared_from_this() const;
590};
591
592template<class T>
593 bool atomic_is_lock_free(const shared_ptr<T>* p);
594template<class T>
595 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
596template<class T>
597 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
598template<class T>
599 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
600template<class T>
601 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
602template<class T>
603 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
604template<class T>
605 shared_ptr<T>
606 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
607template<class T>
608 bool
609 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
610template<class T>
611 bool
612 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
613template<class T>
614 bool
615 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
616 shared_ptr<T> w, memory_order success,
617 memory_order failure);
618template<class T>
619 bool
620 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
621 shared_ptr<T> w, memory_order success,
622 memory_order failure);
623// Hash support
624template <class T> struct hash;
625template <class T, class D> struct hash<unique_ptr<T, D> >;
626template <class T> struct hash<shared_ptr<T> >;
627
628// Pointer safety
629enum class pointer_safety { relaxed, preferred, strict };
630void declare_reachable(void *p);
631template <class T> T *undeclare_reachable(T *p);
632void declare_no_pointers(char *p, size_t n);
633void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000634pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000635
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
637
638} // std
639
640*/
641
642#include <__config>
643#include <type_traits>
644#include <typeinfo>
645#include <cstddef>
646#include <cstdint>
647#include <new>
648#include <utility>
649#include <limits>
650#include <iterator>
651#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000652#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000653#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000654#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000655#include <cstring>
Eric Fiselier9d355982017-04-12 23:45:53 +0000656#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000657#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000658# include <atomic>
659#endif
660
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000661#include <__undef_min_max>
662
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000663#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000665#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666
667_LIBCPP_BEGIN_NAMESPACE_STD
668
Eric Fiselier89659d12015-07-07 00:27:16 +0000669template <class _ValueType>
670inline _LIBCPP_ALWAYS_INLINE
671_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
672#if !defined(_LIBCPP_HAS_NO_THREADS) && \
673 defined(__ATOMIC_RELAXED) && \
674 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
675 return __atomic_load_n(__value, __ATOMIC_RELAXED);
676#else
677 return *__value;
678#endif
679}
680
Kuba Breckade9d6792016-09-04 09:55:12 +0000681template <class _ValueType>
682inline _LIBCPP_ALWAYS_INLINE
683_ValueType __libcpp_acquire_load(_ValueType const* __value) {
684#if !defined(_LIBCPP_HAS_NO_THREADS) && \
685 defined(__ATOMIC_ACQUIRE) && \
686 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
687 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
688#else
689 return *__value;
690#endif
691}
692
Marshall Clow78dbe462016-11-14 18:22:19 +0000693// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695template <class _Tp> class allocator;
696
697template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000698class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699{
700public:
701 typedef void* pointer;
702 typedef const void* const_pointer;
703 typedef void value_type;
704
705 template <class _Up> struct rebind {typedef allocator<_Up> other;};
706};
707
Howard Hinnant9f771052012-01-19 23:15:22 +0000708template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000709class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000710{
711public:
712 typedef const void* pointer;
713 typedef const void* const_pointer;
714 typedef const void value_type;
715
716 template <class _Up> struct rebind {typedef allocator<_Up> other;};
717};
718
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719// pointer_traits
720
721template <class _Tp>
722struct __has_element_type
723{
724private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000725 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 template <class _Up> static __two __test(...);
727 template <class _Up> static char __test(typename _Up::element_type* = 0);
728public:
729 static const bool value = sizeof(__test<_Tp>(0)) == 1;
730};
731
732template <class _Ptr, bool = __has_element_type<_Ptr>::value>
733struct __pointer_traits_element_type;
734
735template <class _Ptr>
736struct __pointer_traits_element_type<_Ptr, true>
737{
738 typedef typename _Ptr::element_type type;
739};
740
741#ifndef _LIBCPP_HAS_NO_VARIADICS
742
743template <template <class, class...> class _Sp, class _Tp, class ..._Args>
744struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
745{
746 typedef typename _Sp<_Tp, _Args...>::element_type type;
747};
748
749template <template <class, class...> class _Sp, class _Tp, class ..._Args>
750struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
751{
752 typedef _Tp type;
753};
754
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000755#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756
757template <template <class> class _Sp, class _Tp>
758struct __pointer_traits_element_type<_Sp<_Tp>, true>
759{
760 typedef typename _Sp<_Tp>::element_type type;
761};
762
763template <template <class> class _Sp, class _Tp>
764struct __pointer_traits_element_type<_Sp<_Tp>, false>
765{
766 typedef _Tp type;
767};
768
769template <template <class, class> class _Sp, class _Tp, class _A0>
770struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
771{
772 typedef typename _Sp<_Tp, _A0>::element_type type;
773};
774
775template <template <class, class> class _Sp, class _Tp, class _A0>
776struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
777{
778 typedef _Tp type;
779};
780
781template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
782struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
783{
784 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
785};
786
787template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
788struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
789{
790 typedef _Tp type;
791};
792
793template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
794 class _A1, class _A2>
795struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
796{
797 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
798};
799
800template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
801 class _A1, class _A2>
802struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
803{
804 typedef _Tp type;
805};
806
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000807#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808
809template <class _Tp>
810struct __has_difference_type
811{
812private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000813 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 template <class _Up> static __two __test(...);
815 template <class _Up> static char __test(typename _Up::difference_type* = 0);
816public:
817 static const bool value = sizeof(__test<_Tp>(0)) == 1;
818};
819
820template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
821struct __pointer_traits_difference_type
822{
823 typedef ptrdiff_t type;
824};
825
826template <class _Ptr>
827struct __pointer_traits_difference_type<_Ptr, true>
828{
829 typedef typename _Ptr::difference_type type;
830};
831
832template <class _Tp, class _Up>
833struct __has_rebind
834{
835private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000836 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837 template <class _Xp> static __two __test(...);
838 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
839public:
840 static const bool value = sizeof(__test<_Tp>(0)) == 1;
841};
842
843template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
844struct __pointer_traits_rebind
845{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000846#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847 typedef typename _Tp::template rebind<_Up> type;
848#else
849 typedef typename _Tp::template rebind<_Up>::other type;
850#endif
851};
852
853#ifndef _LIBCPP_HAS_NO_VARIADICS
854
855template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
857{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000858#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
860#else
861 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
862#endif
863};
864
865template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
867{
868 typedef _Sp<_Up, _Args...> type;
869};
870
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000871#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
873template <template <class> class _Sp, class _Tp, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
875{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000876#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 typedef typename _Sp<_Tp>::template rebind<_Up> type;
878#else
879 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
880#endif
881};
882
883template <template <class> class _Sp, class _Tp, class _Up>
884struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
885{
886 typedef _Sp<_Up> type;
887};
888
889template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
890struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
891{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000892#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
894#else
895 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
896#endif
897};
898
899template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
900struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
901{
902 typedef _Sp<_Up, _A0> type;
903};
904
905template <template <class, class, class> class _Sp, class _Tp, class _A0,
906 class _A1, class _Up>
907struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
908{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000909#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
911#else
912 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
913#endif
914};
915
916template <template <class, class, class> class _Sp, class _Tp, class _A0,
917 class _A1, class _Up>
918struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
919{
920 typedef _Sp<_Up, _A0, _A1> type;
921};
922
923template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
924 class _A1, class _A2, class _Up>
925struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
926{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000927#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
929#else
930 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
931#endif
932};
933
934template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
935 class _A1, class _A2, class _Up>
936struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
937{
938 typedef _Sp<_Up, _A0, _A1, _A2> type;
939};
940
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000941#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942
943template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000944struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945{
946 typedef _Ptr pointer;
947 typedef typename __pointer_traits_element_type<pointer>::type element_type;
948 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
949
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000950#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000951 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952#else
953 template <class _Up> struct rebind
954 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000955#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
957private:
958 struct __nat {};
959public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 static pointer pointer_to(typename conditional<is_void<element_type>::value,
962 __nat, element_type>::type& __r)
963 {return pointer::pointer_to(__r);}
964};
965
966template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968{
969 typedef _Tp* pointer;
970 typedef _Tp element_type;
971 typedef ptrdiff_t difference_type;
972
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000973#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 template <class _Up> using rebind = _Up*;
975#else
976 template <class _Up> struct rebind {typedef _Up* other;};
977#endif
978
979private:
980 struct __nat {};
981public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000984 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000985 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986};
987
Eric Fiselierae3ab842015-08-23 02:56:05 +0000988template <class _From, class _To>
989struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000990#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000991 typedef typename pointer_traits<_From>::template rebind<_To> type;
992#else
993 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
994#endif
995};
996
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997// allocator_traits
998
Marshall Clow41eb88c2017-05-11 14:00:54 +0000999struct __has_pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000{
Howard Hinnant6e260172013-09-21 01:45:05 +00001001 template <class _Up> static __two __test(...);
1002 template <class _Up> static char __test(typename _Up::pointer* = 0);
Marshall Clow41eb88c2017-05-11 14:00:54 +00001003};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004
1005template <class _Tp>
1006struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +00001007 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008{
1009};
1010
1011namespace __pointer_type_imp
1012{
1013
1014template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1015struct __pointer_type
1016{
1017 typedef typename _Dp::pointer type;
1018};
1019
1020template <class _Tp, class _Dp>
1021struct __pointer_type<_Tp, _Dp, false>
1022{
1023 typedef _Tp* type;
1024};
1025
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001026} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028template <class _Tp, class _Dp>
1029struct __pointer_type
1030{
1031 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1032};
1033
1034template <class _Tp>
1035struct __has_const_pointer
1036{
1037private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001038 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 template <class _Up> static __two __test(...);
1040 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1041public:
1042 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1043};
1044
1045template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1046struct __const_pointer
1047{
1048 typedef typename _Alloc::const_pointer type;
1049};
1050
1051template <class _Tp, class _Ptr, class _Alloc>
1052struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1053{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001054#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1056#else
1057 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1058#endif
1059};
1060
1061template <class _Tp>
1062struct __has_void_pointer
1063{
1064private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001065 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 template <class _Up> static __two __test(...);
1067 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1068public:
1069 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1070};
1071
1072template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1073struct __void_pointer
1074{
1075 typedef typename _Alloc::void_pointer type;
1076};
1077
1078template <class _Ptr, class _Alloc>
1079struct __void_pointer<_Ptr, _Alloc, false>
1080{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001081#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1083#else
1084 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1085#endif
1086};
1087
1088template <class _Tp>
1089struct __has_const_void_pointer
1090{
1091private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001092 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 template <class _Up> static __two __test(...);
1094 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1095public:
1096 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1097};
1098
1099template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1100struct __const_void_pointer
1101{
1102 typedef typename _Alloc::const_void_pointer type;
1103};
1104
1105template <class _Ptr, class _Alloc>
1106struct __const_void_pointer<_Ptr, _Alloc, false>
1107{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001108#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1110#else
1111 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1112#endif
1113};
1114
Howard Hinnantc834c512011-11-29 18:15:50 +00001115template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001117_Tp*
1118__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119{
1120 return __p;
1121}
1122
1123template <class _Pointer>
1124inline _LIBCPP_INLINE_VISIBILITY
1125typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001126__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001128 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129}
1130
1131template <class _Tp>
1132struct __has_size_type
1133{
1134private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001135 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 template <class _Up> static __two __test(...);
1137 template <class _Up> static char __test(typename _Up::size_type* = 0);
1138public:
1139 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1140};
1141
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001142template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143struct __size_type
1144{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001145 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146};
1147
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001148template <class _Alloc, class _DiffType>
1149struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
1151 typedef typename _Alloc::size_type type;
1152};
1153
1154template <class _Tp>
1155struct __has_propagate_on_container_copy_assignment
1156{
1157private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001158 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 template <class _Up> static __two __test(...);
1160 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1161public:
1162 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1163};
1164
1165template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1166struct __propagate_on_container_copy_assignment
1167{
1168 typedef false_type type;
1169};
1170
1171template <class _Alloc>
1172struct __propagate_on_container_copy_assignment<_Alloc, true>
1173{
1174 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1175};
1176
1177template <class _Tp>
1178struct __has_propagate_on_container_move_assignment
1179{
1180private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001181 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 template <class _Up> static __two __test(...);
1183 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1184public:
1185 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1186};
1187
1188template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1189struct __propagate_on_container_move_assignment
1190{
1191 typedef false_type type;
1192};
1193
1194template <class _Alloc>
1195struct __propagate_on_container_move_assignment<_Alloc, true>
1196{
1197 typedef typename _Alloc::propagate_on_container_move_assignment type;
1198};
1199
1200template <class _Tp>
1201struct __has_propagate_on_container_swap
1202{
1203private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001204 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 template <class _Up> static __two __test(...);
1206 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1207public:
1208 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1209};
1210
1211template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1212struct __propagate_on_container_swap
1213{
1214 typedef false_type type;
1215};
1216
1217template <class _Alloc>
1218struct __propagate_on_container_swap<_Alloc, true>
1219{
1220 typedef typename _Alloc::propagate_on_container_swap type;
1221};
1222
Marshall Clow0b587562015-06-02 16:34:03 +00001223template <class _Tp>
1224struct __has_is_always_equal
1225{
1226private:
1227 struct __two {char __lx; char __lxx;};
1228 template <class _Up> static __two __test(...);
1229 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1230public:
1231 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1232};
1233
1234template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1235struct __is_always_equal
1236{
1237 typedef typename _VSTD::is_empty<_Alloc>::type type;
1238};
1239
1240template <class _Alloc>
1241struct __is_always_equal<_Alloc, true>
1242{
1243 typedef typename _Alloc::is_always_equal type;
1244};
1245
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1247struct __has_rebind_other
1248{
1249private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001250 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 template <class _Xp> static __two __test(...);
1252 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1253public:
1254 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1255};
1256
1257template <class _Tp, class _Up>
1258struct __has_rebind_other<_Tp, _Up, false>
1259{
1260 static const bool value = false;
1261};
1262
1263template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1264struct __allocator_traits_rebind
1265{
1266 typedef typename _Tp::template rebind<_Up>::other type;
1267};
1268
1269#ifndef _LIBCPP_HAS_NO_VARIADICS
1270
1271template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1278struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1279{
1280 typedef _Alloc<_Up, _Args...> type;
1281};
1282
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001283#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
1285template <template <class> class _Alloc, class _Tp, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1287{
1288 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1289};
1290
1291template <template <class> class _Alloc, class _Tp, class _Up>
1292struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1293{
1294 typedef _Alloc<_Up> type;
1295};
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1298struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1299{
1300 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1301};
1302
1303template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1305{
1306 typedef _Alloc<_Up, _A0> type;
1307};
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1310 class _A1, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1312{
1313 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1314};
1315
1316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1319{
1320 typedef _Alloc<_Up, _A0, _A1> type;
1321};
1322
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _A2, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1326{
1327 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1328};
1329
1330template <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, false>
1333{
1334 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1335};
1336
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001337#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001339#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
1341template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1342auto
1343__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1344 -> decltype(__a.allocate(__sz, __p), true_type());
1345
1346template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1347auto
1348__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1349 -> false_type;
1350
1351template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1352struct __has_allocate_hint
1353 : integral_constant<bool,
1354 is_same<
1355 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1356 declval<_SizeType>(),
1357 declval<_ConstVoidPtr>())),
1358 true_type>::value>
1359{
1360};
1361
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001362#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363
1364template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1365struct __has_allocate_hint
1366 : true_type
1367{
1368};
1369
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001370#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001372#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
1374template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001375decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1376 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 true_type())
1378__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1379
1380template <class _Alloc, class _Pointer, class ..._Args>
1381false_type
1382__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1383
1384template <class _Alloc, class _Pointer, class ..._Args>
1385struct __has_construct
1386 : integral_constant<bool,
1387 is_same<
1388 decltype(__has_construct_test(declval<_Alloc>(),
1389 declval<_Pointer>(),
1390 declval<_Args>()...)),
1391 true_type>::value>
1392{
1393};
1394
1395template <class _Alloc, class _Pointer>
1396auto
1397__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1398 -> decltype(__a.destroy(__p), true_type());
1399
1400template <class _Alloc, class _Pointer>
1401auto
1402__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1403 -> false_type;
1404
1405template <class _Alloc, class _Pointer>
1406struct __has_destroy
1407 : integral_constant<bool,
1408 is_same<
1409 decltype(__has_destroy_test(declval<_Alloc>(),
1410 declval<_Pointer>())),
1411 true_type>::value>
1412{
1413};
1414
1415template <class _Alloc>
1416auto
1417__has_max_size_test(_Alloc&& __a)
1418 -> decltype(__a.max_size(), true_type());
1419
1420template <class _Alloc>
1421auto
1422__has_max_size_test(const volatile _Alloc& __a)
1423 -> false_type;
1424
1425template <class _Alloc>
1426struct __has_max_size
1427 : integral_constant<bool,
1428 is_same<
1429 decltype(__has_max_size_test(declval<_Alloc&>())),
1430 true_type>::value>
1431{
1432};
1433
1434template <class _Alloc>
1435auto
1436__has_select_on_container_copy_construction_test(_Alloc&& __a)
1437 -> decltype(__a.select_on_container_copy_construction(), true_type());
1438
1439template <class _Alloc>
1440auto
1441__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1442 -> false_type;
1443
1444template <class _Alloc>
1445struct __has_select_on_container_copy_construction
1446 : integral_constant<bool,
1447 is_same<
1448 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1449 true_type>::value>
1450{
1451};
1452
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001453#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
1455#ifndef _LIBCPP_HAS_NO_VARIADICS
1456
1457template <class _Alloc, class _Pointer, class ..._Args>
1458struct __has_construct
1459 : false_type
1460{
1461};
1462
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001463#else // _LIBCPP_HAS_NO_VARIADICS
1464
1465template <class _Alloc, class _Pointer, class _Args>
1466struct __has_construct
1467 : false_type
1468{
1469};
1470
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001471#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Alloc, class _Pointer>
1474struct __has_destroy
1475 : false_type
1476{
1477};
1478
1479template <class _Alloc>
1480struct __has_max_size
1481 : true_type
1482{
1483};
1484
1485template <class _Alloc>
1486struct __has_select_on_container_copy_construction
1487 : false_type
1488{
1489};
1490
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001491#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001493template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1494struct __alloc_traits_difference_type
1495{
1496 typedef typename pointer_traits<_Ptr>::difference_type type;
1497};
1498
1499template <class _Alloc, class _Ptr>
1500struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1501{
1502 typedef typename _Alloc::difference_type type;
1503};
1504
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001506struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 typedef _Alloc allocator_type;
1509 typedef typename allocator_type::value_type value_type;
1510
1511 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1512 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1513 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1514 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1515
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1517 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1520 propagate_on_container_copy_assignment;
1521 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1522 propagate_on_container_move_assignment;
1523 typedef typename __propagate_on_container_swap<allocator_type>::type
1524 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001525 typedef typename __is_always_equal<allocator_type>::type
1526 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001528#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001530 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001532#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> struct rebind_alloc
1534 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1535 template <class _Tp> struct rebind_traits
1536 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static pointer allocate(allocator_type& __a, size_type __n)
1541 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1544 {return allocate(__a, __n, __hint,
1545 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001548 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {__a.deallocate(__p, __n);}
1550
1551#ifndef _LIBCPP_HAS_NO_VARIADICS
1552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001555 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001556 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001557#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001560 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {
1562 ::new ((void*)__p) _Tp();
1563 }
1564 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001566 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {
1568 ::new ((void*)__p) _Tp(__a0);
1569 }
1570 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001571 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001572 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 const _A1& __a1)
1574 {
1575 ::new ((void*)__p) _Tp(__a0, __a1);
1576 }
1577 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001579 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 const _A1& __a1, const _A2& __a2)
1581 {
1582 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1583 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001584#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
1586 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 static void destroy(allocator_type& __a, _Tp* __p)
1589 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1590
Howard Hinnant756c69b2010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001592 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1594
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 static allocator_type
1597 select_on_container_copy_construction(const allocator_type& __a)
1598 {return select_on_container_copy_construction(
1599 __has_select_on_container_copy_construction<const allocator_type>(),
1600 __a);}
1601
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001602 template <class _Ptr>
1603 _LIBCPP_INLINE_VISIBILITY
1604 static
1605 void
1606 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1607 {
1608 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1609 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1610 }
1611
1612 template <class _Tp>
1613 _LIBCPP_INLINE_VISIBILITY
1614 static
1615 typename enable_if
1616 <
1617 (is_same<allocator_type, allocator<_Tp> >::value
1618 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1619 is_trivially_move_constructible<_Tp>::value,
1620 void
1621 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001622 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001623 {
1624 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001625 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001626 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001627 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001628 __begin2 += _Np;
1629 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 }
1631
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001632 template <class _Iter, class _Ptr>
1633 _LIBCPP_INLINE_VISIBILITY
1634 static
1635 void
1636 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1637 {
1638 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1639 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1640 }
1641
1642 template <class _Tp>
1643 _LIBCPP_INLINE_VISIBILITY
1644 static
1645 typename enable_if
1646 <
1647 (is_same<allocator_type, allocator<_Tp> >::value
1648 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1649 is_trivially_move_constructible<_Tp>::value,
1650 void
1651 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001652 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001653 {
1654 typedef typename remove_const<_Tp>::type _Vp;
1655 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001656 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001657 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001658 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001659 __begin2 += _Np;
1660 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001661 }
1662
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 template <class _Ptr>
1664 _LIBCPP_INLINE_VISIBILITY
1665 static
1666 void
1667 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1668 {
1669 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001670 {
1671 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1672 --__end2;
1673 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001674 }
1675
1676 template <class _Tp>
1677 _LIBCPP_INLINE_VISIBILITY
1678 static
1679 typename enable_if
1680 <
1681 (is_same<allocator_type, allocator<_Tp> >::value
1682 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1683 is_trivially_move_constructible<_Tp>::value,
1684 void
1685 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001686 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001687 {
1688 ptrdiff_t _Np = __end1 - __begin1;
1689 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001690 if (_Np > 0)
1691 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001692 }
1693
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694private:
1695
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static pointer allocate(allocator_type& __a, size_type __n,
1698 const_void_pointer __hint, true_type)
1699 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001702 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 {return __a.allocate(__n);}
1704
1705#ifndef _LIBCPP_HAS_NO_VARIADICS
1706 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001709 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1713 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001714 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001716#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717
1718 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1721 {__a.destroy(__p);}
1722 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 static void __destroy(false_type, allocator_type&, _Tp* __p)
1725 {
1726 __p->~_Tp();
1727 }
1728
Howard Hinnant756c69b2010-09-22 16:48:34 +00001729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 static size_type __max_size(true_type, const allocator_type& __a)
1731 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001734 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735
Howard Hinnant756c69b2010-09-22 16:48:34 +00001736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 static allocator_type
1738 select_on_container_copy_construction(true_type, const allocator_type& __a)
1739 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static allocator_type
1742 select_on_container_copy_construction(false_type, const allocator_type& __a)
1743 {return __a;}
1744};
1745
Marshall Clow940e01c2015-04-07 05:21:38 +00001746template <class _Traits, class _Tp>
1747struct __rebind_alloc_helper
1748{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001749#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001750 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001751#else
1752 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1753#endif
1754};
1755
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756// allocator
1757
1758template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001759class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760{
1761public:
1762 typedef size_t size_type;
1763 typedef ptrdiff_t difference_type;
1764 typedef _Tp* pointer;
1765 typedef const _Tp* const_pointer;
1766 typedef _Tp& reference;
1767 typedef const _Tp& const_reference;
1768 typedef _Tp value_type;
1769
Howard Hinnant4931e092011-06-02 21:38:57 +00001770 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001771 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001772
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1774
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1776 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1777 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001778 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001779 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001782 {
1783 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001784 __throw_length_error("allocator<T>::allocate(size_t n)"
1785 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001786 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1787 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001788 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001789 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001790 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1791 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001792#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 template <class _Up, class... _Args>
1794 _LIBCPP_INLINE_VISIBILITY
1795 void
1796 construct(_Up* __p, _Args&&... __args)
1797 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001798 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001800#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 _LIBCPP_INLINE_VISIBILITY
1802 void
1803 construct(pointer __p)
1804 {
1805 ::new((void*)__p) _Tp();
1806 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001807# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001808
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0>
1810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001811 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 construct(pointer __p, _A0& __a0)
1813 {
1814 ::new((void*)__p) _Tp(__a0);
1815 }
1816 template <class _A0>
1817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001818 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 construct(pointer __p, const _A0& __a0)
1820 {
1821 ::new((void*)__p) _Tp(__a0);
1822 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001823# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 template <class _A0, class _A1>
1825 _LIBCPP_INLINE_VISIBILITY
1826 void
1827 construct(pointer __p, _A0& __a0, _A1& __a1)
1828 {
1829 ::new((void*)__p) _Tp(__a0, __a1);
1830 }
1831 template <class _A0, class _A1>
1832 _LIBCPP_INLINE_VISIBILITY
1833 void
1834 construct(pointer __p, const _A0& __a0, _A1& __a1)
1835 {
1836 ::new((void*)__p) _Tp(__a0, __a1);
1837 }
1838 template <class _A0, class _A1>
1839 _LIBCPP_INLINE_VISIBILITY
1840 void
1841 construct(pointer __p, _A0& __a0, const _A1& __a1)
1842 {
1843 ::new((void*)__p) _Tp(__a0, __a1);
1844 }
1845 template <class _A0, class _A1>
1846 _LIBCPP_INLINE_VISIBILITY
1847 void
1848 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1849 {
1850 ::new((void*)__p) _Tp(__a0, __a1);
1851 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001852#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1854};
1855
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001856template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001857class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001858{
1859public:
1860 typedef size_t size_type;
1861 typedef ptrdiff_t difference_type;
1862 typedef const _Tp* pointer;
1863 typedef const _Tp* const_pointer;
1864 typedef const _Tp& reference;
1865 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001866 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001867
1868 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001869 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001870
1871 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1872
1873 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1874 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1875 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1876 {return _VSTD::addressof(__x);}
1877 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001878 {
1879 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001880 __throw_length_error("allocator<const T>::allocate(size_t n)"
1881 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001882 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001883 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001884 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001885 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001886 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1887 {return size_type(~0) / sizeof(_Tp);}
1888#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1889 template <class _Up, class... _Args>
1890 _LIBCPP_INLINE_VISIBILITY
1891 void
1892 construct(_Up* __p, _Args&&... __args)
1893 {
1894 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1895 }
1896#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1897 _LIBCPP_INLINE_VISIBILITY
1898 void
1899 construct(pointer __p)
1900 {
1901 ::new((void*)__p) _Tp();
1902 }
1903# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001904
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001905 template <class _A0>
1906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001907 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001908 construct(pointer __p, _A0& __a0)
1909 {
1910 ::new((void*)__p) _Tp(__a0);
1911 }
1912 template <class _A0>
1913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001914 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001915 construct(pointer __p, const _A0& __a0)
1916 {
1917 ::new((void*)__p) _Tp(__a0);
1918 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1920 template <class _A0, class _A1>
1921 _LIBCPP_INLINE_VISIBILITY
1922 void
1923 construct(pointer __p, _A0& __a0, _A1& __a1)
1924 {
1925 ::new((void*)__p) _Tp(__a0, __a1);
1926 }
1927 template <class _A0, class _A1>
1928 _LIBCPP_INLINE_VISIBILITY
1929 void
1930 construct(pointer __p, const _A0& __a0, _A1& __a1)
1931 {
1932 ::new((void*)__p) _Tp(__a0, __a1);
1933 }
1934 template <class _A0, class _A1>
1935 _LIBCPP_INLINE_VISIBILITY
1936 void
1937 construct(pointer __p, _A0& __a0, const _A1& __a1)
1938 {
1939 ::new((void*)__p) _Tp(__a0, __a1);
1940 }
1941 template <class _A0, class _A1>
1942 _LIBCPP_INLINE_VISIBILITY
1943 void
1944 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1945 {
1946 ::new((void*)__p) _Tp(__a0, __a1);
1947 }
1948#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1949 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1950};
1951
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952template <class _Tp, class _Up>
1953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001954bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955
1956template <class _Tp, class _Up>
1957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001958bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959
1960template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001961class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 : public iterator<output_iterator_tag,
1963 _Tp, // purposefully not C++03
1964 ptrdiff_t, // purposefully not C++03
1965 _Tp*, // purposefully not C++03
1966 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1967{
1968private:
1969 _OutputIterator __x_;
1970public:
1971 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1972 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1973 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1974 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001975#if _LIBCPP_STD_VER >= 14
1976 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1977 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1978#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1980 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1981 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001982#if _LIBCPP_STD_VER >= 14
1983 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985};
1986
1987template <class _Tp>
1988pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001989get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990{
1991 pair<_Tp*, ptrdiff_t> __r(0, 0);
1992 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1993 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1994 / sizeof(_Tp);
1995 if (__n > __m)
1996 __n = __m;
1997 while (__n > 0)
1998 {
1999 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2000 if (__r.first)
2001 {
2002 __r.second = __n;
2003 break;
2004 }
2005 __n /= 2;
2006 }
2007 return __r;
2008}
2009
2010template <class _Tp>
2011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002012void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013
Marshall Clowb22274f2017-01-24 22:22:33 +00002014#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015template <class _Tp>
2016struct auto_ptr_ref
2017{
2018 _Tp* __ptr_;
2019};
2020
2021template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002022class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024private:
2025 _Tp* __ptr_;
2026public:
2027 typedef _Tp element_type;
2028
2029 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2030 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2031 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2032 : __ptr_(__p.release()) {}
2033 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2034 {reset(__p.release()); return *this;}
2035 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2036 {reset(__p.release()); return *this;}
2037 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2038 {reset(__p.__ptr_); return *this;}
2039 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2040
2041 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2042 {return *__ptr_;}
2043 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2044 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2045 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2046 {
2047 _Tp* __t = __ptr_;
2048 __ptr_ = 0;
2049 return __t;
2050 }
2051 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2052 {
2053 if (__ptr_ != __p)
2054 delete __ptr_;
2055 __ptr_ = __p;
2056 }
2057
2058 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2059 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2060 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2061 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2062 {return auto_ptr<_Up>(release());}
2063};
2064
2065template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002066class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
2068public:
2069 typedef void element_type;
2070};
Marshall Clowb22274f2017-01-24 22:22:33 +00002071#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
Eric Fiselier9d355982017-04-12 23:45:53 +00002073template <class _Tp, int _Idx,
2074 bool _CanBeEmptyBase =
2075 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2076struct __compressed_pair_elem {
2077 typedef _Tp _ParamT;
2078 typedef _Tp& reference;
2079 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080
Eric Fiselier9d355982017-04-12 23:45:53 +00002081#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002082 constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083
Eric Fiselier9d355982017-04-12 23:45:53 +00002084 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002085 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2086 >::type>
2087 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002088 __compressed_pair_elem(_Up&& __u)
2089 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090
Eric Fiselier9d355982017-04-12 23:45:53 +00002091 template <class... _Args, size_t... _Indexes>
2092 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2093 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2094 __tuple_indices<_Indexes...>)
2095 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2096#else
2097 __compressed_pair_elem() : __value_() {}
2098 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2099#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100
Eric Fiselier9d355982017-04-12 23:45:53 +00002101 reference __get() _NOEXCEPT { return __value_; }
2102 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002105 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106};
2107
Eric Fiselier9d355982017-04-12 23:45:53 +00002108template <class _Tp, int _Idx>
2109struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2110 typedef _Tp _ParamT;
2111 typedef _Tp& reference;
2112 typedef const _Tp& const_reference;
2113 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114
Eric Fiselier9d355982017-04-12 23:45:53 +00002115#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002116 constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117
Eric Fiselier9d355982017-04-12 23:45:53 +00002118 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002119 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2120 >::type>
2121 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002122 __compressed_pair_elem(_Up&& __u)
2123 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124
Eric Fiselier9d355982017-04-12 23:45:53 +00002125 template <class... _Args, size_t... _Indexes>
2126 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2127 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2128 __tuple_indices<_Indexes...>)
2129 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2130#else
2131 __compressed_pair_elem() : __value_type() {}
2132 __compressed_pair_elem(_ParamT __p)
2133 : __value_type(std::forward<_ParamT>(__p)) {}
2134#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135
Eric Fiselier9d355982017-04-12 23:45:53 +00002136 reference __get() _NOEXCEPT { return *this; }
2137 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138};
2139
Eric Fiselier9d355982017-04-12 23:45:53 +00002140// Tag used to construct the second element of the compressed pair.
2141struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142
2143template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002144class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2145 private __compressed_pair_elem<_T2, 1> {
2146 typedef __compressed_pair_elem<_T1, 0> _Base1;
2147 typedef __compressed_pair_elem<_T2, 1> _Base2;
2148
2149 // NOTE: This static assert should never fire because __compressed_pair
2150 // is *almost never* used in a scenario where it's possible for T1 == T2.
2151 // (The exception is std::function where it is possible that the function
2152 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002153 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002154 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2155 "The current implementation is NOT ABI-compatible with the previous "
2156 "implementation for this configuration");
2157
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002159#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002160 template <bool _Dummy = true,
2161 class = typename enable_if<
2162 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2163 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2164 >::type
2165 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002166 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002167 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168
Eric Fiselier9d355982017-04-12 23:45:53 +00002169 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2170 __compressed_pair>::value,
2171 bool>::type = true>
2172 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2173 __compressed_pair(_Tp&& __t)
2174 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175
Eric Fiselier9d355982017-04-12 23:45:53 +00002176 template <class _Tp>
2177 _LIBCPP_INLINE_VISIBILITY constexpr
2178 __compressed_pair(__second_tag, _Tp&& __t)
2179 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180
Eric Fiselier9d355982017-04-12 23:45:53 +00002181 template <class _U1, class _U2>
2182 _LIBCPP_INLINE_VISIBILITY constexpr
2183 __compressed_pair(_U1&& __t1, _U2&& __t2)
2184 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185
Eric Fiselier9d355982017-04-12 23:45:53 +00002186 template <class... _Args1, class... _Args2>
2187 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2188 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2189 tuple<_Args2...> __second_args)
2190 : _Base1(__pc, _VSTD::move(__first_args),
2191 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2192 _Base2(__pc, _VSTD::move(__second_args),
2193 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002194
Eric Fiselier9d355982017-04-12 23:45:53 +00002195#else
2196 _LIBCPP_INLINE_VISIBILITY
2197 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002198
Eric Fiselier9d355982017-04-12 23:45:53 +00002199 _LIBCPP_INLINE_VISIBILITY explicit
2200 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002201
Eric Fiselier9d355982017-04-12 23:45:53 +00002202 _LIBCPP_INLINE_VISIBILITY
2203 __compressed_pair(__second_tag, _T2 __t2)
2204 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205
Eric Fiselier9d355982017-04-12 23:45:53 +00002206 _LIBCPP_INLINE_VISIBILITY
2207 __compressed_pair(_T1 __t1, _T2 __t2)
2208 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2209#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210
Eric Fiselier9d355982017-04-12 23:45:53 +00002211 _LIBCPP_INLINE_VISIBILITY
2212 typename _Base1::reference first() _NOEXCEPT {
2213 return static_cast<_Base1&>(*this).__get();
2214 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215
Eric Fiselier9d355982017-04-12 23:45:53 +00002216 _LIBCPP_INLINE_VISIBILITY
2217 typename _Base1::const_reference first() const _NOEXCEPT {
2218 return static_cast<_Base1 const&>(*this).__get();
2219 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
Eric Fiselier9d355982017-04-12 23:45:53 +00002221 _LIBCPP_INLINE_VISIBILITY
2222 typename _Base2::reference second() _NOEXCEPT {
2223 return static_cast<_Base2&>(*this).__get();
2224 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225
Eric Fiselier9d355982017-04-12 23:45:53 +00002226 _LIBCPP_INLINE_VISIBILITY
2227 typename _Base2::const_reference second() const _NOEXCEPT {
2228 return static_cast<_Base2 const&>(*this).__get();
2229 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230
Eric Fiselier9d355982017-04-12 23:45:53 +00002231 _LIBCPP_INLINE_VISIBILITY
2232 void swap(__compressed_pair& __x)
2233 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2234 __is_nothrow_swappable<_T2>::value)
2235 {
2236 using std::swap;
2237 swap(first(), __x.first());
2238 swap(second(), __x.second());
2239 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240};
2241
2242template <class _T1, class _T2>
2243inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002244void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2245 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2246 __is_nothrow_swappable<_T2>::value) {
2247 __x.swap(__y);
2248}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002250// default_delete
2251
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002253struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002254 static_assert(!is_function<_Tp>::value,
2255 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002256#ifndef _LIBCPP_CXX03_LANG
2257 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002258#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002259 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002260#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002261 template <class _Up>
2262 _LIBCPP_INLINE_VISIBILITY
2263 default_delete(const default_delete<_Up>&,
2264 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2265 0) _NOEXCEPT {}
2266
2267 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2268 static_assert(sizeof(_Tp) > 0,
2269 "default_delete can not delete incomplete type");
2270 static_assert(!is_void<_Tp>::value,
2271 "default_delete can not delete incomplete type");
2272 delete __ptr;
2273 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274};
2275
2276template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002277struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2278private:
2279 template <class _Up>
2280 struct _EnableIfConvertible
2281 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2282
Howard Hinnant4500ca52011-12-18 21:19:44 +00002283public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002284#ifndef _LIBCPP_CXX03_LANG
2285 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002286#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002287 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002288#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002289
2290 template <class _Up>
2291 _LIBCPP_INLINE_VISIBILITY
2292 default_delete(const default_delete<_Up[]>&,
2293 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2294
2295 template <class _Up>
2296 _LIBCPP_INLINE_VISIBILITY
2297 typename _EnableIfConvertible<_Up>::type
2298 operator()(_Up* __ptr) const _NOEXCEPT {
2299 static_assert(sizeof(_Tp) > 0,
2300 "default_delete can not delete incomplete type");
2301 static_assert(!is_void<_Tp>::value,
2302 "default_delete can not delete void type");
2303 delete[] __ptr;
2304 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305};
2306
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307
Eric Fiselier6779b062017-04-16 02:06:25 +00002308
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002309#ifndef _LIBCPP_CXX03_LANG
2310template <class _Deleter>
2311struct __unique_ptr_deleter_sfinae {
2312 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2313 typedef const _Deleter& __lval_ref_type;
2314 typedef _Deleter&& __good_rval_ref_type;
2315 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316};
2317
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002318template <class _Deleter>
2319struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2320 typedef const _Deleter& __lval_ref_type;
2321 typedef const _Deleter&& __bad_rval_ref_type;
2322 typedef false_type __enable_rval_overload;
2323};
2324
2325template <class _Deleter>
2326struct __unique_ptr_deleter_sfinae<_Deleter&> {
2327 typedef _Deleter& __lval_ref_type;
2328 typedef _Deleter&& __bad_rval_ref_type;
2329 typedef false_type __enable_rval_overload;
2330};
2331#endif // !defined(_LIBCPP_CXX03_LANG)
2332
2333template <class _Tp, class _Dp = default_delete<_Tp> >
2334class _LIBCPP_TEMPLATE_VIS unique_ptr {
2335public:
2336 typedef _Tp element_type;
2337 typedef _Dp deleter_type;
2338 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2339
2340 static_assert(!is_rvalue_reference<deleter_type>::value,
2341 "the specified deleter type cannot be an rvalue reference");
2342
2343private:
2344 __compressed_pair<pointer, deleter_type> __ptr_;
2345
2346 struct __nat { int __for_bool_; };
2347
2348#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002349 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002350
2351 template <bool _Dummy>
2352 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002353 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002354
2355 template <bool _Dummy>
2356 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002357 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002358
2359 template <bool _Dummy>
2360 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002361 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002362
2363 template <bool _Dummy, class _Deleter = typename __dependent_type<
2364 __identity<deleter_type>, _Dummy>::type>
2365 using _EnableIfDeleterDefaultConstructible =
2366 typename enable_if<is_default_constructible<_Deleter>::value &&
2367 !is_pointer<_Deleter>::value>::type;
2368
2369 template <class _ArgType>
2370 using _EnableIfDeleterConstructible =
2371 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2372
2373 template <class _UPtr, class _Up>
2374 using _EnableIfMoveConvertible = typename enable_if<
2375 is_convertible<typename _UPtr::pointer, pointer>::value &&
2376 !is_array<_Up>::value
2377 >::type;
2378
2379 template <class _UDel>
2380 using _EnableIfDeleterConvertible = typename enable_if<
2381 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2382 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2383 >::type;
2384
2385 template <class _UDel>
2386 using _EnableIfDeleterAssignable = typename enable_if<
2387 is_assignable<_Dp&, _UDel&&>::value
2388 >::type;
2389
2390public:
2391 template <bool _Dummy = true,
2392 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2393 _LIBCPP_INLINE_VISIBILITY
2394 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2395
2396 template <bool _Dummy = true,
2397 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2398 _LIBCPP_INLINE_VISIBILITY
2399 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2400
2401 template <bool _Dummy = true,
2402 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2403 _LIBCPP_INLINE_VISIBILITY
2404 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2405
2406 template <bool _Dummy = true,
2407 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2408 _LIBCPP_INLINE_VISIBILITY
2409 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2410 : __ptr_(__p, __d) {}
2411
2412 template <bool _Dummy = true,
2413 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2414 _LIBCPP_INLINE_VISIBILITY
2415 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2416 : __ptr_(__p, _VSTD::move(__d)) {
2417 static_assert(!is_reference<deleter_type>::value,
2418 "rvalue deleter bound to reference");
2419 }
2420
2421 template <bool _Dummy = true,
2422 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2423 _LIBCPP_INLINE_VISIBILITY
2424 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2425
2426 _LIBCPP_INLINE_VISIBILITY
2427 unique_ptr(unique_ptr&& __u) noexcept
2428 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2429 }
2430
2431 template <class _Up, class _Ep,
2432 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2433 class = _EnableIfDeleterConvertible<_Ep>
2434 >
2435 _LIBCPP_INLINE_VISIBILITY
2436 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2437 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2438
2439#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2440 template <class _Up>
2441 _LIBCPP_INLINE_VISIBILITY
2442 unique_ptr(auto_ptr<_Up>&& __p,
2443 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2444 is_same<_Dp, default_delete<_Tp>>::value,
2445 __nat>::type = __nat()) _NOEXCEPT
2446 : __ptr_(__p.release()) {}
2447#endif
2448
2449 _LIBCPP_INLINE_VISIBILITY
2450 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2451 reset(__u.release());
2452 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2453 return *this;
2454 }
2455
2456 template <class _Up, class _Ep,
2457 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2458 class = _EnableIfDeleterAssignable<_Ep>
2459 >
2460 _LIBCPP_INLINE_VISIBILITY
2461 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2462 reset(__u.release());
2463 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2464 return *this;
2465 }
2466
2467#else // _LIBCPP_CXX03_LANG
2468private:
2469 unique_ptr(unique_ptr&);
2470 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2471
2472 unique_ptr& operator=(unique_ptr&);
2473 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2474
2475public:
2476 _LIBCPP_INLINE_VISIBILITY
2477 unique_ptr() : __ptr_(pointer())
2478 {
2479 static_assert(!is_pointer<deleter_type>::value,
2480 "unique_ptr constructed with null function pointer deleter");
2481 static_assert(is_default_constructible<deleter_type>::value,
2482 "unique_ptr::deleter_type is not default constructible");
2483 }
2484 _LIBCPP_INLINE_VISIBILITY
2485 unique_ptr(nullptr_t) : __ptr_(pointer())
2486 {
2487 static_assert(!is_pointer<deleter_type>::value,
2488 "unique_ptr constructed with null function pointer deleter");
2489 }
2490 _LIBCPP_INLINE_VISIBILITY
2491 explicit unique_ptr(pointer __p)
2492 : __ptr_(_VSTD::move(__p)) {
2493 static_assert(!is_pointer<deleter_type>::value,
2494 "unique_ptr constructed with null function pointer deleter");
2495 }
2496
2497 _LIBCPP_INLINE_VISIBILITY
2498 operator __rv<unique_ptr>() {
2499 return __rv<unique_ptr>(*this);
2500 }
2501
2502 _LIBCPP_INLINE_VISIBILITY
2503 unique_ptr(__rv<unique_ptr> __u)
2504 : __ptr_(__u->release(),
2505 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2506
2507 template <class _Up, class _Ep>
2508 _LIBCPP_INLINE_VISIBILITY
2509 typename enable_if<
2510 !is_array<_Up>::value &&
2511 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2512 pointer>::value &&
2513 is_assignable<deleter_type&, _Ep&>::value,
2514 unique_ptr&>::type
2515 operator=(unique_ptr<_Up, _Ep> __u) {
2516 reset(__u.release());
2517 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2518 return *this;
2519 }
2520
2521 _LIBCPP_INLINE_VISIBILITY
2522 unique_ptr(pointer __p, deleter_type __d)
2523 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2524#endif // _LIBCPP_CXX03_LANG
2525
2526#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2527 template <class _Up>
2528 _LIBCPP_INLINE_VISIBILITY
2529 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2530 is_same<_Dp, default_delete<_Tp> >::value,
2531 unique_ptr&>::type
2532 operator=(auto_ptr<_Up> __p) {
2533 reset(__p.release());
2534 return *this;
2535 }
2536#endif
2537
2538 _LIBCPP_INLINE_VISIBILITY
2539 ~unique_ptr() { reset(); }
2540
2541 _LIBCPP_INLINE_VISIBILITY
2542 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2543 reset();
2544 return *this;
2545 }
2546
2547 _LIBCPP_INLINE_VISIBILITY
2548 typename add_lvalue_reference<_Tp>::type
2549 operator*() const {
2550 return *__ptr_.first();
2551 }
2552 _LIBCPP_INLINE_VISIBILITY
2553 pointer operator->() const _NOEXCEPT {
2554 return __ptr_.first();
2555 }
2556 _LIBCPP_INLINE_VISIBILITY
2557 pointer get() const _NOEXCEPT {
2558 return __ptr_.first();
2559 }
2560 _LIBCPP_INLINE_VISIBILITY
2561 deleter_type& get_deleter() _NOEXCEPT {
2562 return __ptr_.second();
2563 }
2564 _LIBCPP_INLINE_VISIBILITY
2565 const deleter_type& get_deleter() const _NOEXCEPT {
2566 return __ptr_.second();
2567 }
2568 _LIBCPP_INLINE_VISIBILITY
2569 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2570 return __ptr_.first() != nullptr;
2571 }
2572
2573 _LIBCPP_INLINE_VISIBILITY
2574 pointer release() _NOEXCEPT {
2575 pointer __t = __ptr_.first();
2576 __ptr_.first() = pointer();
2577 return __t;
2578 }
2579
2580 _LIBCPP_INLINE_VISIBILITY
2581 void reset(pointer __p = pointer()) _NOEXCEPT {
2582 pointer __tmp = __ptr_.first();
2583 __ptr_.first() = __p;
2584 if (__tmp)
2585 __ptr_.second()(__tmp);
2586 }
2587
2588 _LIBCPP_INLINE_VISIBILITY
2589 void swap(unique_ptr& __u) _NOEXCEPT {
2590 __ptr_.swap(__u.__ptr_);
2591 }
2592};
2593
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002594
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002596class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002598 typedef _Tp element_type;
2599 typedef _Dp deleter_type;
2600 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2601
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002603 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604
Eric Fiselier31127cd2017-04-16 02:14:31 +00002605 template <class _From>
2606 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607
Eric Fiselier31127cd2017-04-16 02:14:31 +00002608 template <class _FromElem>
2609 struct _CheckArrayPointerConversion<_FromElem*>
2610 : integral_constant<bool,
2611 is_same<_FromElem*, pointer>::value ||
2612 (is_same<pointer, element_type*>::value &&
2613 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2614 >
2615 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002617#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002618 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002619
2620 template <bool _Dummy>
2621 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002622 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002623
2624 template <bool _Dummy>
2625 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002626 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002627
2628 template <bool _Dummy>
2629 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002630 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002631
2632 template <bool _Dummy, class _Deleter = typename __dependent_type<
2633 __identity<deleter_type>, _Dummy>::type>
2634 using _EnableIfDeleterDefaultConstructible =
2635 typename enable_if<is_default_constructible<_Deleter>::value &&
2636 !is_pointer<_Deleter>::value>::type;
2637
2638 template <class _ArgType>
2639 using _EnableIfDeleterConstructible =
2640 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2641
2642 template <class _Pp>
2643 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002644 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002645 >::type;
2646
2647 template <class _UPtr, class _Up,
2648 class _ElemT = typename _UPtr::element_type>
2649 using _EnableIfMoveConvertible = typename enable_if<
2650 is_array<_Up>::value &&
2651 is_same<pointer, element_type*>::value &&
2652 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2653 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2654 >::type;
2655
2656 template <class _UDel>
2657 using _EnableIfDeleterConvertible = typename enable_if<
2658 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2659 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2660 >::type;
2661
2662 template <class _UDel>
2663 using _EnableIfDeleterAssignable = typename enable_if<
2664 is_assignable<_Dp&, _UDel&&>::value
2665 >::type;
2666
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002668 template <bool _Dummy = true,
2669 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2670 _LIBCPP_INLINE_VISIBILITY
2671 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002673 template <bool _Dummy = true,
2674 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2675 _LIBCPP_INLINE_VISIBILITY
2676 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002678 template <class _Pp, bool _Dummy = true,
2679 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2680 class = _EnableIfPointerConvertible<_Pp>>
2681 _LIBCPP_INLINE_VISIBILITY
2682 explicit unique_ptr(_Pp __p) noexcept
2683 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002685 template <class _Pp, bool _Dummy = true,
2686 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2687 class = _EnableIfPointerConvertible<_Pp>>
2688 _LIBCPP_INLINE_VISIBILITY
2689 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2690 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002692 template <bool _Dummy = true,
2693 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2694 _LIBCPP_INLINE_VISIBILITY
2695 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2696 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002698 template <class _Pp, bool _Dummy = true,
2699 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2700 class = _EnableIfPointerConvertible<_Pp>>
2701 _LIBCPP_INLINE_VISIBILITY
2702 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2703 : __ptr_(__p, _VSTD::move(__d)) {
2704 static_assert(!is_reference<deleter_type>::value,
2705 "rvalue deleter bound to reference");
2706 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002708 template <bool _Dummy = true,
2709 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2710 _LIBCPP_INLINE_VISIBILITY
2711 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2712 : __ptr_(nullptr, _VSTD::move(__d)) {
2713 static_assert(!is_reference<deleter_type>::value,
2714 "rvalue deleter bound to reference");
2715 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002716
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717 template <class _Pp, bool _Dummy = true,
2718 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2719 class = _EnableIfPointerConvertible<_Pp>>
2720 _LIBCPP_INLINE_VISIBILITY
2721 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002722
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002723 _LIBCPP_INLINE_VISIBILITY
2724 unique_ptr(unique_ptr&& __u) noexcept
2725 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2726 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002727
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002728 _LIBCPP_INLINE_VISIBILITY
2729 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2730 reset(__u.release());
2731 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2732 return *this;
2733 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002735 template <class _Up, class _Ep,
2736 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2737 class = _EnableIfDeleterConvertible<_Ep>
2738 >
2739 _LIBCPP_INLINE_VISIBILITY
2740 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002741 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002742 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 template <class _Up, class _Ep,
2745 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2746 class = _EnableIfDeleterAssignable<_Ep>
2747 >
2748 _LIBCPP_INLINE_VISIBILITY
2749 unique_ptr&
2750 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2751 reset(__u.release());
2752 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2753 return *this;
2754 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002756#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002758 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002759
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002760 unique_ptr(unique_ptr&);
2761 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2762
2763 unique_ptr& operator=(unique_ptr&);
2764 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2765
2766 template <class _Up>
2767 unique_ptr(_Up __u,
2768 typename conditional<
2769 is_reference<deleter_type>::value, deleter_type,
2770 typename add_lvalue_reference<const deleter_type>::type>::type,
2771 typename enable_if<is_convertible<_Up, pointer>::value,
2772 __nat>::type = __nat());
2773public:
2774 _LIBCPP_INLINE_VISIBILITY
2775 unique_ptr() : __ptr_(pointer()) {
2776 static_assert(!is_pointer<deleter_type>::value,
2777 "unique_ptr constructed with null function pointer deleter");
2778 }
2779 _LIBCPP_INLINE_VISIBILITY
2780 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2781 static_assert(!is_pointer<deleter_type>::value,
2782 "unique_ptr constructed with null function pointer deleter");
2783 }
2784
2785 _LIBCPP_INLINE_VISIBILITY
2786 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2787 static_assert(!is_pointer<deleter_type>::value,
2788 "unique_ptr constructed with null function pointer deleter");
2789 }
2790
2791 _LIBCPP_INLINE_VISIBILITY
2792 unique_ptr(pointer __p, deleter_type __d)
2793 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2794
2795 _LIBCPP_INLINE_VISIBILITY
2796 unique_ptr(nullptr_t, deleter_type __d)
2797 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2798
2799 _LIBCPP_INLINE_VISIBILITY
2800 operator __rv<unique_ptr>() {
2801 return __rv<unique_ptr>(*this);
2802 }
2803
2804 _LIBCPP_INLINE_VISIBILITY
2805 unique_ptr(__rv<unique_ptr> __u)
2806 : __ptr_(__u->release(),
2807 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2808
2809 _LIBCPP_INLINE_VISIBILITY
2810 unique_ptr& operator=(__rv<unique_ptr> __u) {
2811 reset(__u->release());
2812 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2813 return *this;
2814 }
2815
2816#endif // _LIBCPP_CXX03_LANG
2817
2818public:
2819 _LIBCPP_INLINE_VISIBILITY
2820 ~unique_ptr() { reset(); }
2821
2822 _LIBCPP_INLINE_VISIBILITY
2823 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2824 reset();
2825 return *this;
2826 }
2827
2828 _LIBCPP_INLINE_VISIBILITY
2829 typename add_lvalue_reference<_Tp>::type
2830 operator[](size_t __i) const {
2831 return __ptr_.first()[__i];
2832 }
2833 _LIBCPP_INLINE_VISIBILITY
2834 pointer get() const _NOEXCEPT {
2835 return __ptr_.first();
2836 }
2837
2838 _LIBCPP_INLINE_VISIBILITY
2839 deleter_type& get_deleter() _NOEXCEPT {
2840 return __ptr_.second();
2841 }
2842
2843 _LIBCPP_INLINE_VISIBILITY
2844 const deleter_type& get_deleter() const _NOEXCEPT {
2845 return __ptr_.second();
2846 }
2847 _LIBCPP_INLINE_VISIBILITY
2848 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2849 return __ptr_.first() != nullptr;
2850 }
2851
2852 _LIBCPP_INLINE_VISIBILITY
2853 pointer release() _NOEXCEPT {
2854 pointer __t = __ptr_.first();
2855 __ptr_.first() = pointer();
2856 return __t;
2857 }
2858
2859 template <class _Pp>
2860 _LIBCPP_INLINE_VISIBILITY
2861 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002862 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002863 >::type
2864 reset(_Pp __p) _NOEXCEPT {
2865 pointer __tmp = __ptr_.first();
2866 __ptr_.first() = __p;
2867 if (__tmp)
2868 __ptr_.second()(__tmp);
2869 }
2870
2871 _LIBCPP_INLINE_VISIBILITY
2872 void reset(nullptr_t = nullptr) _NOEXCEPT {
2873 pointer __tmp = __ptr_.first();
2874 __ptr_.first() = nullptr;
2875 if (__tmp)
2876 __ptr_.second()(__tmp);
2877 }
2878
2879 _LIBCPP_INLINE_VISIBILITY
2880 void swap(unique_ptr& __u) _NOEXCEPT {
2881 __ptr_.swap(__u.__ptr_);
2882 }
2883
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884};
2885
2886template <class _Tp, class _Dp>
2887inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002888typename enable_if<
2889 __is_swappable<_Dp>::value,
2890 void
2891>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002892swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893
2894template <class _T1, class _D1, class _T2, class _D2>
2895inline _LIBCPP_INLINE_VISIBILITY
2896bool
2897operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2898
2899template <class _T1, class _D1, class _T2, class _D2>
2900inline _LIBCPP_INLINE_VISIBILITY
2901bool
2902operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2903
2904template <class _T1, class _D1, class _T2, class _D2>
2905inline _LIBCPP_INLINE_VISIBILITY
2906bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002907operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2908{
2909 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2910 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002911 typedef typename common_type<_P1, _P2>::type _Vp;
2912 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002913}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914
2915template <class _T1, class _D1, class _T2, class _D2>
2916inline _LIBCPP_INLINE_VISIBILITY
2917bool
2918operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2919
2920template <class _T1, class _D1, class _T2, class _D2>
2921inline _LIBCPP_INLINE_VISIBILITY
2922bool
2923operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2924
2925template <class _T1, class _D1, class _T2, class _D2>
2926inline _LIBCPP_INLINE_VISIBILITY
2927bool
2928operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2929
Howard Hinnantb17caf92012-02-21 21:02:58 +00002930template <class _T1, class _D1>
2931inline _LIBCPP_INLINE_VISIBILITY
2932bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002933operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002934{
2935 return !__x;
2936}
2937
2938template <class _T1, class _D1>
2939inline _LIBCPP_INLINE_VISIBILITY
2940bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002941operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002942{
2943 return !__x;
2944}
2945
2946template <class _T1, class _D1>
2947inline _LIBCPP_INLINE_VISIBILITY
2948bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002949operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002950{
2951 return static_cast<bool>(__x);
2952}
2953
2954template <class _T1, class _D1>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002957operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002958{
2959 return static_cast<bool>(__x);
2960}
2961
2962template <class _T1, class _D1>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2966{
2967 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2968 return less<_P1>()(__x.get(), nullptr);
2969}
2970
2971template <class _T1, class _D1>
2972inline _LIBCPP_INLINE_VISIBILITY
2973bool
2974operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2975{
2976 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2977 return less<_P1>()(nullptr, __x.get());
2978}
2979
2980template <class _T1, class _D1>
2981inline _LIBCPP_INLINE_VISIBILITY
2982bool
2983operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2984{
2985 return nullptr < __x;
2986}
2987
2988template <class _T1, class _D1>
2989inline _LIBCPP_INLINE_VISIBILITY
2990bool
2991operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2992{
2993 return __x < nullptr;
2994}
2995
2996template <class _T1, class _D1>
2997inline _LIBCPP_INLINE_VISIBILITY
2998bool
2999operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3000{
3001 return !(nullptr < __x);
3002}
3003
3004template <class _T1, class _D1>
3005inline _LIBCPP_INLINE_VISIBILITY
3006bool
3007operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3008{
3009 return !(__x < nullptr);
3010}
3011
3012template <class _T1, class _D1>
3013inline _LIBCPP_INLINE_VISIBILITY
3014bool
3015operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3016{
3017 return !(__x < nullptr);
3018}
3019
3020template <class _T1, class _D1>
3021inline _LIBCPP_INLINE_VISIBILITY
3022bool
3023operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3024{
3025 return !(nullptr < __x);
3026}
3027
Howard Hinnant9e028f12012-05-01 15:37:54 +00003028#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3029
3030template <class _Tp, class _Dp>
3031inline _LIBCPP_INLINE_VISIBILITY
3032unique_ptr<_Tp, _Dp>
3033move(unique_ptr<_Tp, _Dp>& __t)
3034{
3035 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3036}
3037
3038#endif
3039
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003040#if _LIBCPP_STD_VER > 11
3041
3042template<class _Tp>
3043struct __unique_if
3044{
3045 typedef unique_ptr<_Tp> __unique_single;
3046};
3047
3048template<class _Tp>
3049struct __unique_if<_Tp[]>
3050{
3051 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3052};
3053
3054template<class _Tp, size_t _Np>
3055struct __unique_if<_Tp[_Np]>
3056{
3057 typedef void __unique_array_known_bound;
3058};
3059
3060template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003061inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003062typename __unique_if<_Tp>::__unique_single
3063make_unique(_Args&&... __args)
3064{
3065 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3066}
3067
3068template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003069inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003070typename __unique_if<_Tp>::__unique_array_unknown_bound
3071make_unique(size_t __n)
3072{
3073 typedef typename remove_extent<_Tp>::type _Up;
3074 return unique_ptr<_Tp>(new _Up[__n]());
3075}
3076
3077template<class _Tp, class... _Args>
3078 typename __unique_if<_Tp>::__unique_array_known_bound
3079 make_unique(_Args&&...) = delete;
3080
3081#endif // _LIBCPP_STD_VER > 11
3082
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003083template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003084#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003085struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003086#else
3087struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3088 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3089#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003090{
3091 typedef unique_ptr<_Tp, _Dp> argument_type;
3092 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003093 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003094 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003095 {
3096 typedef typename argument_type::pointer pointer;
3097 return hash<pointer>()(__ptr.get());
3098 }
3099};
3100
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101struct __destruct_n
3102{
3103private:
3104 size_t size;
3105
3106 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003107 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3109
3110 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003111 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112 {}
3113
Howard Hinnant719bda32011-05-28 14:41:13 +00003114 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003116 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 {}
3118
Howard Hinnant719bda32011-05-28 14:41:13 +00003119 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003121 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122 {}
3123public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003124 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3125 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126
3127 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003128 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003129 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130
3131 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003132 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003133 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134
3135 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003136 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003137 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138};
3139
3140template <class _Alloc>
3141class __allocator_destructor
3142{
3143 typedef allocator_traits<_Alloc> __alloc_traits;
3144public:
3145 typedef typename __alloc_traits::pointer pointer;
3146 typedef typename __alloc_traits::size_type size_type;
3147private:
3148 _Alloc& __alloc_;
3149 size_type __s_;
3150public:
3151 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003152 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003155 void operator()(pointer __p) _NOEXCEPT
3156 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157};
3158
3159template <class _InputIterator, class _ForwardIterator>
3160_ForwardIterator
3161uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3162{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003164#ifndef _LIBCPP_NO_EXCEPTIONS
3165 _ForwardIterator __s = __r;
3166 try
3167 {
3168#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003169 for (; __f != __l; ++__f, (void) ++__r)
3170 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003171#ifndef _LIBCPP_NO_EXCEPTIONS
3172 }
3173 catch (...)
3174 {
3175 for (; __s != __r; ++__s)
3176 __s->~value_type();
3177 throw;
3178 }
3179#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180 return __r;
3181}
3182
3183template <class _InputIterator, class _Size, class _ForwardIterator>
3184_ForwardIterator
3185uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3186{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003188#ifndef _LIBCPP_NO_EXCEPTIONS
3189 _ForwardIterator __s = __r;
3190 try
3191 {
3192#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003193 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3194 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003195#ifndef _LIBCPP_NO_EXCEPTIONS
3196 }
3197 catch (...)
3198 {
3199 for (; __s != __r; ++__s)
3200 __s->~value_type();
3201 throw;
3202 }
3203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204 return __r;
3205}
3206
3207template <class _ForwardIterator, class _Tp>
3208void
3209uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3210{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003212#ifndef _LIBCPP_NO_EXCEPTIONS
3213 _ForwardIterator __s = __f;
3214 try
3215 {
3216#endif
3217 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003218 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003219#ifndef _LIBCPP_NO_EXCEPTIONS
3220 }
3221 catch (...)
3222 {
3223 for (; __s != __f; ++__s)
3224 __s->~value_type();
3225 throw;
3226 }
3227#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003228}
3229
3230template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003231_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3233{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003235#ifndef _LIBCPP_NO_EXCEPTIONS
3236 _ForwardIterator __s = __f;
3237 try
3238 {
3239#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003240 for (; __n > 0; ++__f, (void) --__n)
3241 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003242#ifndef _LIBCPP_NO_EXCEPTIONS
3243 }
3244 catch (...)
3245 {
3246 for (; __s != __f; ++__s)
3247 __s->~value_type();
3248 throw;
3249 }
3250#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003251 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252}
3253
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003254#if _LIBCPP_STD_VER > 14
3255
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003256template <class _Tp>
3257inline _LIBCPP_INLINE_VISIBILITY
3258void destroy_at(_Tp* __loc) {
3259 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3260 __loc->~_Tp();
3261}
3262
3263template <class _ForwardIterator>
3264inline _LIBCPP_INLINE_VISIBILITY
3265void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3266 for (; __first != __last; ++__first)
3267 _VSTD::destroy_at(_VSTD::addressof(*__first));
3268}
3269
3270template <class _ForwardIterator, class _Size>
3271inline _LIBCPP_INLINE_VISIBILITY
3272_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3273 for (; __n > 0; (void)++__first, --__n)
3274 _VSTD::destroy_at(_VSTD::addressof(*__first));
3275 return __first;
3276}
3277
Eric Fiselier290c07c2016-10-11 21:13:44 +00003278template <class _ForwardIterator>
3279inline _LIBCPP_INLINE_VISIBILITY
3280void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3281 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3282 auto __idx = __first;
3283#ifndef _LIBCPP_NO_EXCEPTIONS
3284 try {
3285#endif
3286 for (; __idx != __last; ++__idx)
3287 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3288#ifndef _LIBCPP_NO_EXCEPTIONS
3289 } catch (...) {
3290 _VSTD::destroy(__first, __idx);
3291 throw;
3292 }
3293#endif
3294}
3295
3296template <class _ForwardIterator, class _Size>
3297inline _LIBCPP_INLINE_VISIBILITY
3298_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3299 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3300 auto __idx = __first;
3301#ifndef _LIBCPP_NO_EXCEPTIONS
3302 try {
3303#endif
3304 for (; __n > 0; (void)++__idx, --__n)
3305 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3306 return __idx;
3307#ifndef _LIBCPP_NO_EXCEPTIONS
3308 } catch (...) {
3309 _VSTD::destroy(__first, __idx);
3310 throw;
3311 }
3312#endif
3313}
3314
3315
3316template <class _ForwardIterator>
3317inline _LIBCPP_INLINE_VISIBILITY
3318void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3319 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3320 auto __idx = __first;
3321#ifndef _LIBCPP_NO_EXCEPTIONS
3322 try {
3323#endif
3324 for (; __idx != __last; ++__idx)
3325 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3326#ifndef _LIBCPP_NO_EXCEPTIONS
3327 } catch (...) {
3328 _VSTD::destroy(__first, __idx);
3329 throw;
3330 }
3331#endif
3332}
3333
3334template <class _ForwardIterator, class _Size>
3335inline _LIBCPP_INLINE_VISIBILITY
3336_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3337 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3338 auto __idx = __first;
3339#ifndef _LIBCPP_NO_EXCEPTIONS
3340 try {
3341#endif
3342 for (; __n > 0; (void)++__idx, --__n)
3343 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3344 return __idx;
3345#ifndef _LIBCPP_NO_EXCEPTIONS
3346 } catch (...) {
3347 _VSTD::destroy(__first, __idx);
3348 throw;
3349 }
3350#endif
3351}
3352
3353
3354template <class _InputIt, class _ForwardIt>
3355inline _LIBCPP_INLINE_VISIBILITY
3356_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3357 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3358 auto __idx = __first_res;
3359#ifndef _LIBCPP_NO_EXCEPTIONS
3360 try {
3361#endif
3362 for (; __first != __last; (void)++__idx, ++__first)
3363 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3364 return __idx;
3365#ifndef _LIBCPP_NO_EXCEPTIONS
3366 } catch (...) {
3367 _VSTD::destroy(__first_res, __idx);
3368 throw;
3369 }
3370#endif
3371}
3372
3373template <class _InputIt, class _Size, class _ForwardIt>
3374inline _LIBCPP_INLINE_VISIBILITY
3375pair<_InputIt, _ForwardIt>
3376uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3377 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3378 auto __idx = __first_res;
3379#ifndef _LIBCPP_NO_EXCEPTIONS
3380 try {
3381#endif
3382 for (; __n > 0; ++__idx, (void)++__first, --__n)
3383 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3384 return {__first, __idx};
3385#ifndef _LIBCPP_NO_EXCEPTIONS
3386 } catch (...) {
3387 _VSTD::destroy(__first_res, __idx);
3388 throw;
3389 }
3390#endif
3391}
3392
3393
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003394#endif // _LIBCPP_STD_VER > 14
3395
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003396// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3397// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003398// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003399#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3400 && defined(__ATOMIC_RELAXED) \
3401 && defined(__ATOMIC_ACQ_REL)
3402# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3403#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3404# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3405#endif
3406
3407template <class _Tp>
3408inline _LIBCPP_INLINE_VISIBILITY _Tp
3409__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3410{
3411#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3412 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3413#else
3414 return __t += 1;
3415#endif
3416}
3417
3418template <class _Tp>
3419inline _LIBCPP_INLINE_VISIBILITY _Tp
3420__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3421{
3422#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3423 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3424#else
3425 return __t -= 1;
3426#endif
3427}
3428
Howard Hinnant756c69b2010-09-22 16:48:34 +00003429class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430 : public std::exception
3431{
3432public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003433 virtual ~bad_weak_ptr() _NOEXCEPT;
3434 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435};
3436
Marshall Clow8fea1612016-08-25 15:09:01 +00003437_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3438void __throw_bad_weak_ptr()
3439{
3440#ifndef _LIBCPP_NO_EXCEPTIONS
3441 throw bad_weak_ptr();
3442#else
3443 _VSTD::abort();
3444#endif
3445}
3446
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003447template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003449class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450{
3451 __shared_count(const __shared_count&);
3452 __shared_count& operator=(const __shared_count&);
3453
3454protected:
3455 long __shared_owners_;
3456 virtual ~__shared_count();
3457private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003458 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459
3460public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003462 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463 : __shared_owners_(__refs) {}
3464
Eric Fiselier98848572017-01-17 03:16:26 +00003465#if defined(_LIBCPP_BUILDING_MEMORY) && \
3466 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003467 void __add_shared() _NOEXCEPT;
3468 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003469#else
3470 _LIBCPP_INLINE_VISIBILITY
3471 void __add_shared() _NOEXCEPT {
3472 __libcpp_atomic_refcount_increment(__shared_owners_);
3473 }
3474 _LIBCPP_INLINE_VISIBILITY
3475 bool __release_shared() _NOEXCEPT {
3476 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3477 __on_zero_shared();
3478 return true;
3479 }
3480 return false;
3481 }
3482#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003483 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003484 long use_count() const _NOEXCEPT {
3485 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3486 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487};
3488
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003489class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490 : private __shared_count
3491{
3492 long __shared_weak_owners_;
3493
3494public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003496 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497 : __shared_count(__refs),
3498 __shared_weak_owners_(__refs) {}
3499protected:
3500 virtual ~__shared_weak_count();
3501
3502public:
Eric Fiselier98848572017-01-17 03:16:26 +00003503#if defined(_LIBCPP_BUILDING_MEMORY) && \
3504 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003505 void __add_shared() _NOEXCEPT;
3506 void __add_weak() _NOEXCEPT;
3507 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003508#else
3509 _LIBCPP_INLINE_VISIBILITY
3510 void __add_shared() _NOEXCEPT {
3511 __shared_count::__add_shared();
3512 }
3513 _LIBCPP_INLINE_VISIBILITY
3514 void __add_weak() _NOEXCEPT {
3515 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3516 }
3517 _LIBCPP_INLINE_VISIBILITY
3518 void __release_shared() _NOEXCEPT {
3519 if (__shared_count::__release_shared())
3520 __release_weak();
3521 }
3522#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003523 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003525 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3526 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527
Howard Hinnant807d6332013-02-25 15:50:36 +00003528 // Define the function out only if we build static libc++ without RTTI.
3529 // Otherwise we may break clients who need to compile their projects with
3530 // -fno-rtti and yet link against a libc++.dylib compiled
3531 // without -fno-rtti.
3532#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003533 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003534#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003536 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537};
3538
3539template <class _Tp, class _Dp, class _Alloc>
3540class __shared_ptr_pointer
3541 : public __shared_weak_count
3542{
3543 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3544public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003547 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548
Howard Hinnant72f73582010-08-11 17:04:31 +00003549#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003550 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003551#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552
3553private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003554 virtual void __on_zero_shared() _NOEXCEPT;
3555 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556};
3557
Howard Hinnant72f73582010-08-11 17:04:31 +00003558#ifndef _LIBCPP_NO_RTTI
3559
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560template <class _Tp, class _Dp, class _Alloc>
3561const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003562__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003564 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565}
3566
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003567#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003568
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569template <class _Tp, class _Dp, class _Alloc>
3570void
Howard Hinnant719bda32011-05-28 14:41:13 +00003571__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572{
3573 __data_.first().second()(__data_.first().first());
3574 __data_.first().second().~_Dp();
3575}
3576
3577template <class _Tp, class _Dp, class _Alloc>
3578void
Howard Hinnant719bda32011-05-28 14:41:13 +00003579__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003581 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3582 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003583 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3584
Eric Fiselierf8898c82015-02-05 23:01:40 +00003585 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003587 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588}
3589
3590template <class _Tp, class _Alloc>
3591class __shared_ptr_emplace
3592 : public __shared_weak_count
3593{
3594 __compressed_pair<_Alloc, _Tp> __data_;
3595public:
3596#ifndef _LIBCPP_HAS_NO_VARIADICS
3597
Howard Hinnant756c69b2010-09-22 16:48:34 +00003598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003600 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601
3602 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003605 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3606 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607
3608#else // _LIBCPP_HAS_NO_VARIADICS
3609
Howard Hinnant756c69b2010-09-22 16:48:34 +00003610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611 __shared_ptr_emplace(_Alloc __a)
3612 : __data_(__a) {}
3613
3614 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3617 : __data_(__a, _Tp(__a0)) {}
3618
3619 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3622 : __data_(__a, _Tp(__a0, __a1)) {}
3623
3624 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3627 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3628
3629#endif // _LIBCPP_HAS_NO_VARIADICS
3630
3631private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003632 virtual void __on_zero_shared() _NOEXCEPT;
3633 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003636 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637};
3638
3639template <class _Tp, class _Alloc>
3640void
Howard Hinnant719bda32011-05-28 14:41:13 +00003641__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642{
3643 __data_.second().~_Tp();
3644}
3645
3646template <class _Tp, class _Alloc>
3647void
Howard Hinnant719bda32011-05-28 14:41:13 +00003648__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003650 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3651 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003652 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003653 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003655 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656}
3657
Erik Pilkington2a398762017-05-25 15:43:31 +00003658struct __shared_ptr_dummy_rebind_allocator_type;
3659template <>
3660class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3661{
3662public:
3663 template <class _Other>
3664 struct rebind
3665 {
3666 typedef allocator<_Other> other;
3667 };
3668};
3669
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003670template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671
3672template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003673class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003675public:
3676 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003677
Eric Fiselierae5b6672016-06-27 01:02:43 +00003678#if _LIBCPP_STD_VER > 14
3679 typedef weak_ptr<_Tp> weak_type;
3680#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681private:
3682 element_type* __ptr_;
3683 __shared_weak_count* __cntrl_;
3684
3685 struct __nat {int __for_bool_;};
3686public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003688 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003690 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003691 template<class _Yp>
3692 explicit shared_ptr(_Yp* __p,
3693 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3694 template<class _Yp, class _Dp>
3695 shared_ptr(_Yp* __p, _Dp __d,
3696 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3697 template<class _Yp, class _Dp, class _Alloc>
3698 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3699 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3701 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003702 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003704 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003708 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003709 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003710#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003712 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003713 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003714 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003715 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003716#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003718 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003719#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003720#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003721 template<class _Yp>
3722 shared_ptr(auto_ptr<_Yp>&& __r,
3723 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003724#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003725 template<class _Yp>
3726 shared_ptr(auto_ptr<_Yp> __r,
3727 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003729#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003730#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003731 template <class _Yp, class _Dp>
3732 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3733 typename enable_if
3734 <
3735 !is_lvalue_reference<_Dp>::value &&
3736 !is_array<_Yp>::value &&
3737 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3738 __nat
3739 >::type = __nat());
3740 template <class _Yp, class _Dp>
3741 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3742 typename enable_if
3743 <
3744 is_lvalue_reference<_Dp>::value &&
3745 !is_array<_Yp>::value &&
3746 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3747 __nat
3748 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003749#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003750 template <class _Yp, class _Dp>
3751 shared_ptr(unique_ptr<_Yp, _Dp>,
3752 typename enable_if
3753 <
3754 !is_lvalue_reference<_Dp>::value &&
3755 !is_array<_Yp>::value &&
3756 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3757 __nat
3758 >::type = __nat());
3759 template <class _Yp, class _Dp>
3760 shared_ptr(unique_ptr<_Yp, _Dp>,
3761 typename enable_if
3762 <
3763 is_lvalue_reference<_Dp>::value &&
3764 !is_array<_Yp>::value &&
3765 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3766 __nat
3767 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003768#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769
3770 ~shared_ptr();
3771
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003773 shared_ptr& operator=(const shared_ptr& __r) _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 shared_ptr&
3779 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003781 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003782#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003784 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003785 template<class _Yp>
3786 typename enable_if
3787 <
3788 is_convertible<_Yp*, element_type*>::value,
3789 shared_ptr<_Tp>&
3790 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003792 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003793#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003794 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003796 typename enable_if
3797 <
3798 !is_array<_Yp>::value &&
3799 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003800 shared_ptr
3801 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003802 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003803#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003804#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003805#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003806 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003808 typename enable_if
3809 <
3810 !is_array<_Yp>::value &&
3811 is_convertible<_Yp*, element_type*>::value,
3812 shared_ptr&
3813 >::type
3814 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003816#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003817 template <class _Yp, class _Dp>
3818 typename enable_if
3819 <
3820 !is_array<_Yp>::value &&
3821 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3822 shared_ptr&
3823 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003824#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003826 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003827#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003829 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830#endif
3831
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003833 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003835 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003836 template<class _Yp>
3837 typename enable_if
3838 <
3839 is_convertible<_Yp*, element_type*>::value,
3840 void
3841 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003843 reset(_Yp* __p);
3844 template<class _Yp, class _Dp>
3845 typename enable_if
3846 <
3847 is_convertible<_Yp*, element_type*>::value,
3848 void
3849 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003851 reset(_Yp* __p, _Dp __d);
3852 template<class _Yp, class _Dp, class _Alloc>
3853 typename enable_if
3854 <
3855 is_convertible<_Yp*, element_type*>::value,
3856 void
3857 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003859 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860
Howard Hinnant756c69b2010-09-22 16:48:34 +00003861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003862 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003864 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3865 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003867 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003869 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003871 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003873 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003874 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003875 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003876 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003878 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003879 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003880 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003882 _LIBCPP_INLINE_VISIBILITY
3883 bool
3884 __owner_equivalent(const shared_ptr& __p) const
3885 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886
Howard Hinnant72f73582010-08-11 17:04:31 +00003887#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003890 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003892#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893
3894#ifndef _LIBCPP_HAS_NO_VARIADICS
3895
3896 template<class ..._Args>
3897 static
3898 shared_ptr<_Tp>
3899 make_shared(_Args&& ...__args);
3900
3901 template<class _Alloc, class ..._Args>
3902 static
3903 shared_ptr<_Tp>
3904 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3905
3906#else // _LIBCPP_HAS_NO_VARIADICS
3907
3908 static shared_ptr<_Tp> make_shared();
3909
3910 template<class _A0>
3911 static shared_ptr<_Tp> make_shared(_A0&);
3912
3913 template<class _A0, class _A1>
3914 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3915
3916 template<class _A0, class _A1, class _A2>
3917 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3918
3919 template<class _Alloc>
3920 static shared_ptr<_Tp>
3921 allocate_shared(const _Alloc& __a);
3922
3923 template<class _Alloc, class _A0>
3924 static shared_ptr<_Tp>
3925 allocate_shared(const _Alloc& __a, _A0& __a0);
3926
3927 template<class _Alloc, class _A0, class _A1>
3928 static shared_ptr<_Tp>
3929 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3930
3931 template<class _Alloc, class _A0, class _A1, class _A2>
3932 static shared_ptr<_Tp>
3933 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3934
3935#endif // _LIBCPP_HAS_NO_VARIADICS
3936
3937private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003938 template <class _Yp, bool = is_function<_Yp>::value>
3939 struct __shared_ptr_default_allocator
3940 {
3941 typedef allocator<_Yp> type;
3942 };
3943
3944 template <class _Yp>
3945 struct __shared_ptr_default_allocator<_Yp, true>
3946 {
3947 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3948 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003950 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003951 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003952 typename enable_if<is_convertible<_OrigPtr*,
3953 const enable_shared_from_this<_Yp>*
3954 >::value,
3955 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003956 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3957 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003959 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003960 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003961 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003962 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3963 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003964 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965 }
3966
Erik Pilkington2a398762017-05-25 15:43:31 +00003967 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003969 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3970 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971};
3972
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003973
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003975inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003976_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003977shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978 : __ptr_(0),
3979 __cntrl_(0)
3980{
3981}
3982
3983template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003984inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003985_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003986shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987 : __ptr_(0),
3988 __cntrl_(0)
3989{
3990}
3991
3992template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003993template<class _Yp>
3994shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3995 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996 : __ptr_(__p)
3997{
3998 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003999 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4000 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4001 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004003 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004}
4005
4006template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004007template<class _Yp, class _Dp>
4008shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4009 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004010 : __ptr_(__p)
4011{
4012#ifndef _LIBCPP_NO_EXCEPTIONS
4013 try
4014 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004015#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004016 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4017 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4018 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004019 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020#ifndef _LIBCPP_NO_EXCEPTIONS
4021 }
4022 catch (...)
4023 {
4024 __d(__p);
4025 throw;
4026 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004027#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028}
4029
4030template<class _Tp>
4031template<class _Dp>
4032shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4033 : __ptr_(0)
4034{
4035#ifndef _LIBCPP_NO_EXCEPTIONS
4036 try
4037 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004038#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004039 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4040 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4041 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042#ifndef _LIBCPP_NO_EXCEPTIONS
4043 }
4044 catch (...)
4045 {
4046 __d(__p);
4047 throw;
4048 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004049#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050}
4051
4052template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004053template<class _Yp, class _Dp, class _Alloc>
4054shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4055 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056 : __ptr_(__p)
4057{
4058#ifndef _LIBCPP_NO_EXCEPTIONS
4059 try
4060 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004061#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004063 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064 typedef __allocator_destructor<_A2> _D2;
4065 _A2 __a2(__a);
4066 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004067 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4068 _CntrlBlk(__p, __d, __a);
4069 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004070 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071#ifndef _LIBCPP_NO_EXCEPTIONS
4072 }
4073 catch (...)
4074 {
4075 __d(__p);
4076 throw;
4077 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004078#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079}
4080
4081template<class _Tp>
4082template<class _Dp, class _Alloc>
4083shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4084 : __ptr_(0)
4085{
4086#ifndef _LIBCPP_NO_EXCEPTIONS
4087 try
4088 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004089#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004091 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092 typedef __allocator_destructor<_A2> _D2;
4093 _A2 __a2(__a);
4094 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004095 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4096 _CntrlBlk(__p, __d, __a);
4097 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098#ifndef _LIBCPP_NO_EXCEPTIONS
4099 }
4100 catch (...)
4101 {
4102 __d(__p);
4103 throw;
4104 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004105#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106}
4107
4108template<class _Tp>
4109template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004110inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004111shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112 : __ptr_(__p),
4113 __cntrl_(__r.__cntrl_)
4114{
4115 if (__cntrl_)
4116 __cntrl_->__add_shared();
4117}
4118
4119template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004120inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004121shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122 : __ptr_(__r.__ptr_),
4123 __cntrl_(__r.__cntrl_)
4124{
4125 if (__cntrl_)
4126 __cntrl_->__add_shared();
4127}
4128
4129template<class _Tp>
4130template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004131inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004133 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004134 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135 : __ptr_(__r.__ptr_),
4136 __cntrl_(__r.__cntrl_)
4137{
4138 if (__cntrl_)
4139 __cntrl_->__add_shared();
4140}
4141
Howard Hinnant74279a52010-09-04 23:28:19 +00004142#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143
4144template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004145inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004146shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147 : __ptr_(__r.__ptr_),
4148 __cntrl_(__r.__cntrl_)
4149{
4150 __r.__ptr_ = 0;
4151 __r.__cntrl_ = 0;
4152}
4153
4154template<class _Tp>
4155template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004156inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004158 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004159 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160 : __ptr_(__r.__ptr_),
4161 __cntrl_(__r.__cntrl_)
4162{
4163 __r.__ptr_ = 0;
4164 __r.__cntrl_ = 0;
4165}
4166
Howard Hinnant74279a52010-09-04 23:28:19 +00004167#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168
Marshall Clowb22274f2017-01-24 22:22:33 +00004169#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004171template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004172#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004173shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004175shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004177 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178 : __ptr_(__r.get())
4179{
4180 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4181 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004182 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183 __r.release();
4184}
Marshall Clowb22274f2017-01-24 22:22:33 +00004185#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186
4187template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004188template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004189#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4191#else
4192shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4193#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004194 typename enable_if
4195 <
4196 !is_lvalue_reference<_Dp>::value &&
4197 !is_array<_Yp>::value &&
4198 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4199 __nat
4200 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201 : __ptr_(__r.get())
4202{
Marshall Clow35cde742015-05-10 13:59:45 +00004203#if _LIBCPP_STD_VER > 11
4204 if (__ptr_ == nullptr)
4205 __cntrl_ = nullptr;
4206 else
4207#endif
4208 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004209 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4210 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4211 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004212 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004213 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214 __r.release();
4215}
4216
4217template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004218template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004219#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4221#else
4222shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4223#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004224 typename enable_if
4225 <
4226 is_lvalue_reference<_Dp>::value &&
4227 !is_array<_Yp>::value &&
4228 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4229 __nat
4230 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231 : __ptr_(__r.get())
4232{
Marshall Clow35cde742015-05-10 13:59:45 +00004233#if _LIBCPP_STD_VER > 11
4234 if (__ptr_ == nullptr)
4235 __cntrl_ = nullptr;
4236 else
4237#endif
4238 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004239 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004240 typedef __shared_ptr_pointer<_Yp*,
4241 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004242 _AllocT > _CntrlBlk;
4243 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004244 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004245 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246 __r.release();
4247}
4248
4249#ifndef _LIBCPP_HAS_NO_VARIADICS
4250
4251template<class _Tp>
4252template<class ..._Args>
4253shared_ptr<_Tp>
4254shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4255{
4256 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4257 typedef allocator<_CntrlBlk> _A2;
4258 typedef __allocator_destructor<_A2> _D2;
4259 _A2 __a2;
4260 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004261 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004262 shared_ptr<_Tp> __r;
4263 __r.__ptr_ = __hold2.get()->get();
4264 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004265 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004266 return __r;
4267}
4268
4269template<class _Tp>
4270template<class _Alloc, class ..._Args>
4271shared_ptr<_Tp>
4272shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4273{
4274 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004275 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276 typedef __allocator_destructor<_A2> _D2;
4277 _A2 __a2(__a);
4278 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004279 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4280 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281 shared_ptr<_Tp> __r;
4282 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004283 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004284 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285 return __r;
4286}
4287
4288#else // _LIBCPP_HAS_NO_VARIADICS
4289
4290template<class _Tp>
4291shared_ptr<_Tp>
4292shared_ptr<_Tp>::make_shared()
4293{
4294 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4295 typedef allocator<_CntrlBlk> _Alloc2;
4296 typedef __allocator_destructor<_Alloc2> _D2;
4297 _Alloc2 __alloc2;
4298 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4299 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4300 shared_ptr<_Tp> __r;
4301 __r.__ptr_ = __hold2.get()->get();
4302 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004303 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304 return __r;
4305}
4306
4307template<class _Tp>
4308template<class _A0>
4309shared_ptr<_Tp>
4310shared_ptr<_Tp>::make_shared(_A0& __a0)
4311{
4312 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4313 typedef allocator<_CntrlBlk> _Alloc2;
4314 typedef __allocator_destructor<_Alloc2> _D2;
4315 _Alloc2 __alloc2;
4316 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4317 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4318 shared_ptr<_Tp> __r;
4319 __r.__ptr_ = __hold2.get()->get();
4320 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004321 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322 return __r;
4323}
4324
4325template<class _Tp>
4326template<class _A0, class _A1>
4327shared_ptr<_Tp>
4328shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4329{
4330 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4331 typedef allocator<_CntrlBlk> _Alloc2;
4332 typedef __allocator_destructor<_Alloc2> _D2;
4333 _Alloc2 __alloc2;
4334 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4335 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4336 shared_ptr<_Tp> __r;
4337 __r.__ptr_ = __hold2.get()->get();
4338 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004339 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340 return __r;
4341}
4342
4343template<class _Tp>
4344template<class _A0, class _A1, class _A2>
4345shared_ptr<_Tp>
4346shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4347{
4348 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4349 typedef allocator<_CntrlBlk> _Alloc2;
4350 typedef __allocator_destructor<_Alloc2> _D2;
4351 _Alloc2 __alloc2;
4352 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4353 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4354 shared_ptr<_Tp> __r;
4355 __r.__ptr_ = __hold2.get()->get();
4356 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004357 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358 return __r;
4359}
4360
4361template<class _Tp>
4362template<class _Alloc>
4363shared_ptr<_Tp>
4364shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4365{
4366 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004367 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368 typedef __allocator_destructor<_Alloc2> _D2;
4369 _Alloc2 __alloc2(__a);
4370 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004371 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4372 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373 shared_ptr<_Tp> __r;
4374 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004375 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004376 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377 return __r;
4378}
4379
4380template<class _Tp>
4381template<class _Alloc, class _A0>
4382shared_ptr<_Tp>
4383shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4384{
4385 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004386 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387 typedef __allocator_destructor<_Alloc2> _D2;
4388 _Alloc2 __alloc2(__a);
4389 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004390 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4391 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392 shared_ptr<_Tp> __r;
4393 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004394 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004395 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004396 return __r;
4397}
4398
4399template<class _Tp>
4400template<class _Alloc, class _A0, class _A1>
4401shared_ptr<_Tp>
4402shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4403{
4404 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004405 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004406 typedef __allocator_destructor<_Alloc2> _D2;
4407 _Alloc2 __alloc2(__a);
4408 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004409 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4410 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004411 shared_ptr<_Tp> __r;
4412 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004413 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004414 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415 return __r;
4416}
4417
4418template<class _Tp>
4419template<class _Alloc, class _A0, class _A1, class _A2>
4420shared_ptr<_Tp>
4421shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4422{
4423 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004424 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425 typedef __allocator_destructor<_Alloc2> _D2;
4426 _Alloc2 __alloc2(__a);
4427 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004428 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4429 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004430 shared_ptr<_Tp> __r;
4431 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004432 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004433 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434 return __r;
4435}
4436
4437#endif // _LIBCPP_HAS_NO_VARIADICS
4438
4439template<class _Tp>
4440shared_ptr<_Tp>::~shared_ptr()
4441{
4442 if (__cntrl_)
4443 __cntrl_->__release_shared();
4444}
4445
4446template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004447inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004448shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004449shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450{
4451 shared_ptr(__r).swap(*this);
4452 return *this;
4453}
4454
4455template<class _Tp>
4456template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004457inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004458typename enable_if
4459<
Marshall Clow7e384b72017-01-10 16:59:33 +00004460 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004461 shared_ptr<_Tp>&
4462>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004463shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004464{
4465 shared_ptr(__r).swap(*this);
4466 return *this;
4467}
4468
Howard Hinnant74279a52010-09-04 23:28:19 +00004469#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470
4471template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004472inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004474shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004476 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004477 return *this;
4478}
4479
4480template<class _Tp>
4481template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004482inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004483typename enable_if
4484<
Marshall Clow7e384b72017-01-10 16:59:33 +00004485 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004486 shared_ptr<_Tp>&
4487>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004488shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4489{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004490 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004491 return *this;
4492}
4493
Marshall Clowb22274f2017-01-24 22:22:33 +00004494#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495template<class _Tp>
4496template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004497inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004498typename enable_if
4499<
4500 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004501 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004502 shared_ptr<_Tp>
4503>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004504shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4505{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004506 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507 return *this;
4508}
Marshall Clowb22274f2017-01-24 22:22:33 +00004509#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004510
4511template<class _Tp>
4512template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004513inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004514typename enable_if
4515<
4516 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004517 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4518 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004519 shared_ptr<_Tp>&
4520>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4522{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004523 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004524 return *this;
4525}
4526
Howard Hinnant74279a52010-09-04 23:28:19 +00004527#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528
Marshall Clowb22274f2017-01-24 22:22:33 +00004529#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530template<class _Tp>
4531template<class _Yp>
4532inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004533typename enable_if
4534<
4535 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004536 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004537 shared_ptr<_Tp>&
4538>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004539shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004540{
4541 shared_ptr(__r).swap(*this);
4542 return *this;
4543}
Marshall Clowb22274f2017-01-24 22:22:33 +00004544#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004545
4546template<class _Tp>
4547template <class _Yp, class _Dp>
4548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004549typename enable_if
4550<
4551 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004552 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4553 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004554 shared_ptr<_Tp>&
4555>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4557{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004558 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004559 return *this;
4560}
4561
Howard Hinnant74279a52010-09-04 23:28:19 +00004562#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004563
4564template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004565inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566void
Howard Hinnant719bda32011-05-28 14:41:13 +00004567shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004569 _VSTD::swap(__ptr_, __r.__ptr_);
4570 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004571}
4572
4573template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004574inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004575void
Howard Hinnant719bda32011-05-28 14:41:13 +00004576shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577{
4578 shared_ptr().swap(*this);
4579}
4580
4581template<class _Tp>
4582template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004583inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004584typename enable_if
4585<
Marshall Clow7e384b72017-01-10 16:59:33 +00004586 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004587 void
4588>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004589shared_ptr<_Tp>::reset(_Yp* __p)
4590{
4591 shared_ptr(__p).swap(*this);
4592}
4593
4594template<class _Tp>
4595template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004596inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004597typename enable_if
4598<
Marshall Clow7e384b72017-01-10 16:59:33 +00004599 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004600 void
4601>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004602shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4603{
4604 shared_ptr(__p, __d).swap(*this);
4605}
4606
4607template<class _Tp>
4608template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004609inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004610typename enable_if
4611<
Marshall Clow7e384b72017-01-10 16:59:33 +00004612 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004613 void
4614>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004615shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4616{
4617 shared_ptr(__p, __d, __a).swap(*this);
4618}
4619
4620#ifndef _LIBCPP_HAS_NO_VARIADICS
4621
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004622template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004623inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004624typename enable_if
4625<
4626 !is_array<_Tp>::value,
4627 shared_ptr<_Tp>
4628>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004629make_shared(_Args&& ...__args)
4630{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004631 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632}
4633
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004634template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004636typename enable_if
4637<
4638 !is_array<_Tp>::value,
4639 shared_ptr<_Tp>
4640>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641allocate_shared(const _Alloc& __a, _Args&& ...__args)
4642{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004643 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644}
4645
4646#else // _LIBCPP_HAS_NO_VARIADICS
4647
4648template<class _Tp>
4649inline _LIBCPP_INLINE_VISIBILITY
4650shared_ptr<_Tp>
4651make_shared()
4652{
4653 return shared_ptr<_Tp>::make_shared();
4654}
4655
4656template<class _Tp, class _A0>
4657inline _LIBCPP_INLINE_VISIBILITY
4658shared_ptr<_Tp>
4659make_shared(_A0& __a0)
4660{
4661 return shared_ptr<_Tp>::make_shared(__a0);
4662}
4663
4664template<class _Tp, class _A0, class _A1>
4665inline _LIBCPP_INLINE_VISIBILITY
4666shared_ptr<_Tp>
4667make_shared(_A0& __a0, _A1& __a1)
4668{
4669 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4670}
4671
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004672template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004673inline _LIBCPP_INLINE_VISIBILITY
4674shared_ptr<_Tp>
4675make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4676{
4677 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4678}
4679
4680template<class _Tp, class _Alloc>
4681inline _LIBCPP_INLINE_VISIBILITY
4682shared_ptr<_Tp>
4683allocate_shared(const _Alloc& __a)
4684{
4685 return shared_ptr<_Tp>::allocate_shared(__a);
4686}
4687
4688template<class _Tp, class _Alloc, class _A0>
4689inline _LIBCPP_INLINE_VISIBILITY
4690shared_ptr<_Tp>
4691allocate_shared(const _Alloc& __a, _A0& __a0)
4692{
4693 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4694}
4695
4696template<class _Tp, class _Alloc, class _A0, class _A1>
4697inline _LIBCPP_INLINE_VISIBILITY
4698shared_ptr<_Tp>
4699allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4700{
4701 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4702}
4703
4704template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4705inline _LIBCPP_INLINE_VISIBILITY
4706shared_ptr<_Tp>
4707allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4708{
4709 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4710}
4711
4712#endif // _LIBCPP_HAS_NO_VARIADICS
4713
4714template<class _Tp, class _Up>
4715inline _LIBCPP_INLINE_VISIBILITY
4716bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004717operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004718{
4719 return __x.get() == __y.get();
4720}
4721
4722template<class _Tp, class _Up>
4723inline _LIBCPP_INLINE_VISIBILITY
4724bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004725operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004726{
4727 return !(__x == __y);
4728}
4729
4730template<class _Tp, class _Up>
4731inline _LIBCPP_INLINE_VISIBILITY
4732bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004733operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004734{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004735 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4736 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004737}
4738
4739template<class _Tp, class _Up>
4740inline _LIBCPP_INLINE_VISIBILITY
4741bool
4742operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4743{
4744 return __y < __x;
4745}
4746
4747template<class _Tp, class _Up>
4748inline _LIBCPP_INLINE_VISIBILITY
4749bool
4750operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4751{
4752 return !(__y < __x);
4753}
4754
4755template<class _Tp, class _Up>
4756inline _LIBCPP_INLINE_VISIBILITY
4757bool
4758operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4759{
4760 return !(__x < __y);
4761}
4762
4763template<class _Tp>
4764inline _LIBCPP_INLINE_VISIBILITY
4765bool
4766operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4767{
4768 return !__x;
4769}
4770
4771template<class _Tp>
4772inline _LIBCPP_INLINE_VISIBILITY
4773bool
4774operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4775{
4776 return !__x;
4777}
4778
4779template<class _Tp>
4780inline _LIBCPP_INLINE_VISIBILITY
4781bool
4782operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4783{
4784 return static_cast<bool>(__x);
4785}
4786
4787template<class _Tp>
4788inline _LIBCPP_INLINE_VISIBILITY
4789bool
4790operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4791{
4792 return static_cast<bool>(__x);
4793}
4794
4795template<class _Tp>
4796inline _LIBCPP_INLINE_VISIBILITY
4797bool
4798operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4799{
4800 return less<_Tp*>()(__x.get(), nullptr);
4801}
4802
4803template<class _Tp>
4804inline _LIBCPP_INLINE_VISIBILITY
4805bool
4806operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4807{
4808 return less<_Tp*>()(nullptr, __x.get());
4809}
4810
4811template<class _Tp>
4812inline _LIBCPP_INLINE_VISIBILITY
4813bool
4814operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4815{
4816 return nullptr < __x;
4817}
4818
4819template<class _Tp>
4820inline _LIBCPP_INLINE_VISIBILITY
4821bool
4822operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4823{
4824 return __x < nullptr;
4825}
4826
4827template<class _Tp>
4828inline _LIBCPP_INLINE_VISIBILITY
4829bool
4830operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4831{
4832 return !(nullptr < __x);
4833}
4834
4835template<class _Tp>
4836inline _LIBCPP_INLINE_VISIBILITY
4837bool
4838operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4839{
4840 return !(__x < nullptr);
4841}
4842
4843template<class _Tp>
4844inline _LIBCPP_INLINE_VISIBILITY
4845bool
4846operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4847{
4848 return !(__x < nullptr);
4849}
4850
4851template<class _Tp>
4852inline _LIBCPP_INLINE_VISIBILITY
4853bool
4854operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4855{
4856 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004857}
4858
4859template<class _Tp>
4860inline _LIBCPP_INLINE_VISIBILITY
4861void
Howard Hinnant719bda32011-05-28 14:41:13 +00004862swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004863{
4864 __x.swap(__y);
4865}
4866
4867template<class _Tp, class _Up>
4868inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004869typename enable_if
4870<
4871 !is_array<_Tp>::value && !is_array<_Up>::value,
4872 shared_ptr<_Tp>
4873>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004874static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004875{
4876 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4877}
4878
4879template<class _Tp, class _Up>
4880inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004881typename enable_if
4882<
4883 !is_array<_Tp>::value && !is_array<_Up>::value,
4884 shared_ptr<_Tp>
4885>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004886dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004887{
4888 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4889 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4890}
4891
4892template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004893typename enable_if
4894<
4895 is_array<_Tp>::value == is_array<_Up>::value,
4896 shared_ptr<_Tp>
4897>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004898const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004899{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004900 typedef typename remove_extent<_Tp>::type _RTp;
4901 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004902}
4903
Howard Hinnant72f73582010-08-11 17:04:31 +00004904#ifndef _LIBCPP_NO_RTTI
4905
Howard Hinnantc51e1022010-05-11 19:42:16 +00004906template<class _Dp, class _Tp>
4907inline _LIBCPP_INLINE_VISIBILITY
4908_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004909get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004910{
4911 return __p.template __get_deleter<_Dp>();
4912}
4913
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004914#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004915
Howard Hinnantc51e1022010-05-11 19:42:16 +00004916template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004917class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004918{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004919public:
4920 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004921private:
4922 element_type* __ptr_;
4923 __shared_weak_count* __cntrl_;
4924
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004925public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004927 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004928 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004929 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4930 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004932 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004933 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004934 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4935 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004936
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004937#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004939 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004940 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004941 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4942 _NOEXCEPT;
4943#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004944 ~weak_ptr();
4945
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004947 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004948 template<class _Yp>
4949 typename enable_if
4950 <
4951 is_convertible<_Yp*, element_type*>::value,
4952 weak_ptr&
4953 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004955 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4956
4957#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4958
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004960 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4961 template<class _Yp>
4962 typename enable_if
4963 <
4964 is_convertible<_Yp*, element_type*>::value,
4965 weak_ptr&
4966 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004968 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4969
4970#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4971
4972 template<class _Yp>
4973 typename enable_if
4974 <
4975 is_convertible<_Yp*, element_type*>::value,
4976 weak_ptr&
4977 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004979 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004980
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004982 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004984 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004985
Howard Hinnant756c69b2010-09-22 16:48:34 +00004986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004987 long use_count() const _NOEXCEPT
4988 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004990 bool expired() const _NOEXCEPT
4991 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4992 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004993 template<class _Up>
4994 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004995 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004996 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004997 template<class _Up>
4998 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004999 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005000 {return __cntrl_ < __r.__cntrl_;}
5001
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005002 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5003 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005004};
5005
5006template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005007inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005008_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005009weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005010 : __ptr_(0),
5011 __cntrl_(0)
5012{
5013}
5014
5015template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005016inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005017weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005018 : __ptr_(__r.__ptr_),
5019 __cntrl_(__r.__cntrl_)
5020{
5021 if (__cntrl_)
5022 __cntrl_->__add_weak();
5023}
5024
5025template<class _Tp>
5026template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005027inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005028weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005029 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005030 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005031 : __ptr_(__r.__ptr_),
5032 __cntrl_(__r.__cntrl_)
5033{
5034 if (__cntrl_)
5035 __cntrl_->__add_weak();
5036}
5037
5038template<class _Tp>
5039template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005040inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005041weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005042 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005043 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005044 : __ptr_(__r.__ptr_),
5045 __cntrl_(__r.__cntrl_)
5046{
5047 if (__cntrl_)
5048 __cntrl_->__add_weak();
5049}
5050
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005051#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5052
5053template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005054inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005055weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5056 : __ptr_(__r.__ptr_),
5057 __cntrl_(__r.__cntrl_)
5058{
5059 __r.__ptr_ = 0;
5060 __r.__cntrl_ = 0;
5061}
5062
5063template<class _Tp>
5064template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005065inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005066weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5067 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5068 _NOEXCEPT
5069 : __ptr_(__r.__ptr_),
5070 __cntrl_(__r.__cntrl_)
5071{
5072 __r.__ptr_ = 0;
5073 __r.__cntrl_ = 0;
5074}
5075
5076#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5077
Howard Hinnantc51e1022010-05-11 19:42:16 +00005078template<class _Tp>
5079weak_ptr<_Tp>::~weak_ptr()
5080{
5081 if (__cntrl_)
5082 __cntrl_->__release_weak();
5083}
5084
5085template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005086inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005087weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005088weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005089{
5090 weak_ptr(__r).swap(*this);
5091 return *this;
5092}
5093
5094template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005095template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005096inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005097typename enable_if
5098<
5099 is_convertible<_Yp*, _Tp*>::value,
5100 weak_ptr<_Tp>&
5101>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005102weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005103{
5104 weak_ptr(__r).swap(*this);
5105 return *this;
5106}
5107
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005108#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5109
5110template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005111inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005112weak_ptr<_Tp>&
5113weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5114{
5115 weak_ptr(_VSTD::move(__r)).swap(*this);
5116 return *this;
5117}
5118
Howard Hinnantc51e1022010-05-11 19:42:16 +00005119template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005120template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005121inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005122typename enable_if
5123<
5124 is_convertible<_Yp*, _Tp*>::value,
5125 weak_ptr<_Tp>&
5126>::type
5127weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5128{
5129 weak_ptr(_VSTD::move(__r)).swap(*this);
5130 return *this;
5131}
5132
5133#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5134
5135template<class _Tp>
5136template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005137inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005138typename enable_if
5139<
5140 is_convertible<_Yp*, _Tp*>::value,
5141 weak_ptr<_Tp>&
5142>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005143weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005144{
5145 weak_ptr(__r).swap(*this);
5146 return *this;
5147}
5148
5149template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005150inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005151void
Howard Hinnant719bda32011-05-28 14:41:13 +00005152weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005153{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005154 _VSTD::swap(__ptr_, __r.__ptr_);
5155 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005156}
5157
5158template<class _Tp>
5159inline _LIBCPP_INLINE_VISIBILITY
5160void
Howard Hinnant719bda32011-05-28 14:41:13 +00005161swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005162{
5163 __x.swap(__y);
5164}
5165
5166template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005167inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005168void
Howard Hinnant719bda32011-05-28 14:41:13 +00005169weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005170{
5171 weak_ptr().swap(*this);
5172}
5173
5174template<class _Tp>
5175template<class _Yp>
5176shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005177 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005178 : __ptr_(__r.__ptr_),
5179 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5180{
5181 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005182 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005183}
5184
5185template<class _Tp>
5186shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005187weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005188{
5189 shared_ptr<_Tp> __r;
5190 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5191 if (__r.__cntrl_)
5192 __r.__ptr_ = __ptr_;
5193 return __r;
5194}
5195
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005196#if _LIBCPP_STD_VER > 14
5197template <class _Tp = void> struct owner_less;
5198#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005199template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005200#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005201
5202template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005203struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005204 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005205{
5206 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005207 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005208 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005209 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005210 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005211 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005212 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005213 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005214 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005215 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005216};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005217
5218template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005219struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005220 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5221{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005222 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005223 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005224 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005225 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005226 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005227 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005228 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005229 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005230 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005231 {return __x.owner_before(__y);}
5232};
5233
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005234#if _LIBCPP_STD_VER > 14
5235template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005236struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005237{
5238 template <class _Tp, class _Up>
5239 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005240 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005241 {return __x.owner_before(__y);}
5242 template <class _Tp, class _Up>
5243 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005244 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005245 {return __x.owner_before(__y);}
5246 template <class _Tp, class _Up>
5247 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005248 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005249 {return __x.owner_before(__y);}
5250 template <class _Tp, class _Up>
5251 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005252 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005253 {return __x.owner_before(__y);}
5254 typedef void is_transparent;
5255};
5256#endif
5257
Howard Hinnantc51e1022010-05-11 19:42:16 +00005258template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005259class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005260{
5261 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005262protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005263 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005264 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005266 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005268 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5269 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005271 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005272public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005274 shared_ptr<_Tp> shared_from_this()
5275 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005277 shared_ptr<_Tp const> shared_from_this() const
5278 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005279
Eric Fiselier84006862016-06-02 00:15:35 +00005280#if _LIBCPP_STD_VER > 14
5281 _LIBCPP_INLINE_VISIBILITY
5282 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5283 { return __weak_this_; }
5284
5285 _LIBCPP_INLINE_VISIBILITY
5286 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5287 { return __weak_this_; }
5288#endif // _LIBCPP_STD_VER > 14
5289
Howard Hinnantc51e1022010-05-11 19:42:16 +00005290 template <class _Up> friend class shared_ptr;
5291};
5292
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005293template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005294struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005295{
5296 typedef shared_ptr<_Tp> argument_type;
5297 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005298
Howard Hinnant756c69b2010-09-22 16:48:34 +00005299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005300 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005301 {
5302 return hash<_Tp*>()(__ptr.get());
5303 }
5304};
5305
Howard Hinnantc834c512011-11-29 18:15:50 +00005306template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005307inline _LIBCPP_INLINE_VISIBILITY
5308basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005309operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005310
Eric Fiselier9b492672016-06-18 02:12:53 +00005311
5312#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005313
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005314class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005315{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005316 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005317public:
5318 void lock() _NOEXCEPT;
5319 void unlock() _NOEXCEPT;
5320
5321private:
5322 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5323 __sp_mut(const __sp_mut&);
5324 __sp_mut& operator=(const __sp_mut&);
5325
Howard Hinnant8331b762013-03-06 23:30:19 +00005326 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005327};
5328
Mehdi Amini228053d2017-05-04 17:08:54 +00005329_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5330__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005331
5332template <class _Tp>
5333inline _LIBCPP_INLINE_VISIBILITY
5334bool
5335atomic_is_lock_free(const shared_ptr<_Tp>*)
5336{
5337 return false;
5338}
5339
5340template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005341_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005342shared_ptr<_Tp>
5343atomic_load(const shared_ptr<_Tp>* __p)
5344{
5345 __sp_mut& __m = __get_sp_mut(__p);
5346 __m.lock();
5347 shared_ptr<_Tp> __q = *__p;
5348 __m.unlock();
5349 return __q;
5350}
5351
5352template <class _Tp>
5353inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005354_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005355shared_ptr<_Tp>
5356atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5357{
5358 return atomic_load(__p);
5359}
5360
5361template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005362_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005363void
5364atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5365{
5366 __sp_mut& __m = __get_sp_mut(__p);
5367 __m.lock();
5368 __p->swap(__r);
5369 __m.unlock();
5370}
5371
5372template <class _Tp>
5373inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005374_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005375void
5376atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5377{
5378 atomic_store(__p, __r);
5379}
5380
5381template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005382_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005383shared_ptr<_Tp>
5384atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5385{
5386 __sp_mut& __m = __get_sp_mut(__p);
5387 __m.lock();
5388 __p->swap(__r);
5389 __m.unlock();
5390 return __r;
5391}
5392
5393template <class _Tp>
5394inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005395_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005396shared_ptr<_Tp>
5397atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5398{
5399 return atomic_exchange(__p, __r);
5400}
5401
5402template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005403_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005404bool
5405atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5406{
Marshall Clow4201ee82016-05-18 17:50:13 +00005407 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005408 __sp_mut& __m = __get_sp_mut(__p);
5409 __m.lock();
5410 if (__p->__owner_equivalent(*__v))
5411 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005412 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005413 *__p = __w;
5414 __m.unlock();
5415 return true;
5416 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005417 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005418 *__v = *__p;
5419 __m.unlock();
5420 return false;
5421}
5422
5423template <class _Tp>
5424inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005425_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005426bool
5427atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5428{
5429 return atomic_compare_exchange_strong(__p, __v, __w);
5430}
5431
5432template <class _Tp>
5433inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005434_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005435bool
5436atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5437 shared_ptr<_Tp> __w, memory_order, memory_order)
5438{
5439 return atomic_compare_exchange_strong(__p, __v, __w);
5440}
5441
5442template <class _Tp>
5443inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005444_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005445bool
5446atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5447 shared_ptr<_Tp> __w, memory_order, memory_order)
5448{
5449 return atomic_compare_exchange_weak(__p, __v, __w);
5450}
5451
Eric Fiselier9b492672016-06-18 02:12:53 +00005452#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005453
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005454//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005455#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5456# ifndef _LIBCPP_CXX03_LANG
5457enum class pointer_safety : unsigned char {
5458 relaxed,
5459 preferred,
5460 strict
5461};
5462# endif
5463#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005464struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005465{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005466 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005467 {
5468 relaxed,
5469 preferred,
5470 strict
5471 };
5472
Howard Hinnant49e145e2012-10-30 19:06:59 +00005473 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005474
Howard Hinnant756c69b2010-09-22 16:48:34 +00005475 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005476 pointer_safety() : __v_() {}
5477
5478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005479 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005481 operator int() const {return __v_;}
5482};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005483#endif
5484
5485#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5486 defined(_LIBCPP_BUILDING_MEMORY)
5487_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5488#else
5489// This function is only offered in C++03 under ABI v1.
5490# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5491inline _LIBCPP_INLINE_VISIBILITY
5492pointer_safety get_pointer_safety() _NOEXCEPT {
5493 return pointer_safety::relaxed;
5494}
5495# endif
5496#endif
5497
Howard Hinnantc51e1022010-05-11 19:42:16 +00005498
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005499_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5500_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5501_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005502_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005503
5504template <class _Tp>
5505inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005506_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005507undeclare_reachable(_Tp* __p)
5508{
5509 return static_cast<_Tp*>(__undeclare_reachable(__p));
5510}
5511
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005512_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005513
Marshall Clow8982dcd2015-07-13 20:04:56 +00005514// --- Helper for container swap --
5515template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005516inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005517void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5518#if _LIBCPP_STD_VER >= 14
5519 _NOEXCEPT
5520#else
5521 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5522#endif
5523{
5524 __swap_allocator(__a1, __a2,
5525 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5526}
5527
5528template <typename _Alloc>
5529_LIBCPP_INLINE_VISIBILITY
5530void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5531#if _LIBCPP_STD_VER >= 14
5532 _NOEXCEPT
5533#else
5534 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5535#endif
5536{
5537 using _VSTD::swap;
5538 swap(__a1, __a2);
5539}
5540
5541template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005542inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005543void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5544
Marshall Clowff91de82015-08-18 19:51:37 +00005545template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005546struct __noexcept_move_assign_container : public integral_constant<bool,
5547 _Traits::propagate_on_container_move_assignment::value
5548#if _LIBCPP_STD_VER > 14
5549 || _Traits::is_always_equal::value
5550#else
5551 && is_nothrow_move_assignable<_Alloc>::value
5552#endif
5553 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005554
Marshall Clowa591b9a2016-07-11 21:38:08 +00005555
5556#ifndef _LIBCPP_HAS_NO_VARIADICS
5557template <class _Tp, class _Alloc>
5558struct __temp_value {
5559 typedef allocator_traits<_Alloc> _Traits;
5560
5561 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5562 _Alloc &__a;
5563
5564 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5565 _Tp & get() { return *__addr(); }
5566
5567 template<class... _Args>
5568 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5569 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5570
5571 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5572 };
5573#endif
5574
Howard Hinnantc51e1022010-05-11 19:42:16 +00005575_LIBCPP_END_NAMESPACE_STD
5576
5577#endif // _LIBCPP_MEMORY