blob: 22706d029d5fa32a6af218e8ada4589bd3bdcc56 [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 Hinnantaaaa52b2011-10-17 20:05:10 +0000661#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000663#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664
Eric Fiselierf4433a32017-05-31 22:07:49 +0000665_LIBCPP_PUSH_MACROS
666#include <__undef_macros>
667
668
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669_LIBCPP_BEGIN_NAMESPACE_STD
670
Eric Fiselier89659d12015-07-07 00:27:16 +0000671template <class _ValueType>
672inline _LIBCPP_ALWAYS_INLINE
673_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
674#if !defined(_LIBCPP_HAS_NO_THREADS) && \
675 defined(__ATOMIC_RELAXED) && \
676 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
677 return __atomic_load_n(__value, __ATOMIC_RELAXED);
678#else
679 return *__value;
680#endif
681}
682
Kuba Breckade9d6792016-09-04 09:55:12 +0000683template <class _ValueType>
684inline _LIBCPP_ALWAYS_INLINE
685_ValueType __libcpp_acquire_load(_ValueType const* __value) {
686#if !defined(_LIBCPP_HAS_NO_THREADS) && \
687 defined(__ATOMIC_ACQUIRE) && \
688 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
689 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
690#else
691 return *__value;
692#endif
693}
694
Marshall Clow78dbe462016-11-14 18:22:19 +0000695// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000696
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697template <class _Tp> class allocator;
698
699template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000700class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701{
702public:
703 typedef void* pointer;
704 typedef const void* const_pointer;
705 typedef void value_type;
706
707 template <class _Up> struct rebind {typedef allocator<_Up> other;};
708};
709
Howard Hinnant9f771052012-01-19 23:15:22 +0000710template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000711class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000712{
713public:
714 typedef const void* pointer;
715 typedef const void* const_pointer;
716 typedef const void value_type;
717
718 template <class _Up> struct rebind {typedef allocator<_Up> other;};
719};
720
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721// pointer_traits
722
Marshall Clow0be70c32017-06-14 21:23:57 +0000723template <class _Tp, class = void>
724struct __has_element_type : false_type {};
725
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +0000727struct __has_element_type<_Tp,
728 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729
730template <class _Ptr, bool = __has_element_type<_Ptr>::value>
731struct __pointer_traits_element_type;
732
733template <class _Ptr>
734struct __pointer_traits_element_type<_Ptr, true>
735{
736 typedef typename _Ptr::element_type type;
737};
738
739#ifndef _LIBCPP_HAS_NO_VARIADICS
740
741template <template <class, class...> class _Sp, class _Tp, class ..._Args>
742struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
743{
744 typedef typename _Sp<_Tp, _Args...>::element_type type;
745};
746
747template <template <class, class...> class _Sp, class _Tp, class ..._Args>
748struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
749{
750 typedef _Tp type;
751};
752
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000753#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754
755template <template <class> class _Sp, class _Tp>
756struct __pointer_traits_element_type<_Sp<_Tp>, true>
757{
758 typedef typename _Sp<_Tp>::element_type type;
759};
760
761template <template <class> class _Sp, class _Tp>
762struct __pointer_traits_element_type<_Sp<_Tp>, false>
763{
764 typedef _Tp type;
765};
766
767template <template <class, class> class _Sp, class _Tp, class _A0>
768struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
769{
770 typedef typename _Sp<_Tp, _A0>::element_type type;
771};
772
773template <template <class, class> class _Sp, class _Tp, class _A0>
774struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
775{
776 typedef _Tp type;
777};
778
779template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
780struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
781{
782 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
783};
784
785template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
786struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
787{
788 typedef _Tp type;
789};
790
791template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
792 class _A1, class _A2>
793struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
794{
795 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
796};
797
798template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
799 class _A1, class _A2>
800struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
801{
802 typedef _Tp type;
803};
804
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000805#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000806
Marshall Clow0be70c32017-06-14 21:23:57 +0000807template <class _Tp, class = void>
808struct __has_difference_type : false_type {};
809
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +0000811struct __has_difference_type<_Tp,
812 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813
814template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
815struct __pointer_traits_difference_type
816{
817 typedef ptrdiff_t type;
818};
819
820template <class _Ptr>
821struct __pointer_traits_difference_type<_Ptr, true>
822{
823 typedef typename _Ptr::difference_type type;
824};
825
826template <class _Tp, class _Up>
827struct __has_rebind
828{
829private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000830 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831 template <class _Xp> static __two __test(...);
832 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
833public:
834 static const bool value = sizeof(__test<_Tp>(0)) == 1;
835};
836
837template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
838struct __pointer_traits_rebind
839{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000840#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841 typedef typename _Tp::template rebind<_Up> type;
842#else
843 typedef typename _Tp::template rebind<_Up>::other type;
844#endif
845};
846
847#ifndef _LIBCPP_HAS_NO_VARIADICS
848
849template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
850struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
851{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000852#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
854#else
855 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
856#endif
857};
858
859template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
861{
862 typedef _Sp<_Up, _Args...> type;
863};
864
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000865#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866
867template <template <class> class _Sp, class _Tp, class _Up>
868struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
869{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000870#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 typedef typename _Sp<_Tp>::template rebind<_Up> type;
872#else
873 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
874#endif
875};
876
877template <template <class> class _Sp, class _Tp, class _Up>
878struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
879{
880 typedef _Sp<_Up> type;
881};
882
883template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
884struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
885{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000886#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
888#else
889 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
890#endif
891};
892
893template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
894struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
895{
896 typedef _Sp<_Up, _A0> type;
897};
898
899template <template <class, class, class> class _Sp, class _Tp, class _A0,
900 class _A1, class _Up>
901struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
902{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000903#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
905#else
906 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
907#endif
908};
909
910template <template <class, class, class> class _Sp, class _Tp, class _A0,
911 class _A1, class _Up>
912struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
913{
914 typedef _Sp<_Up, _A0, _A1> type;
915};
916
917template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
918 class _A1, class _A2, class _Up>
919struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
920{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000921#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
923#else
924 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
925#endif
926};
927
928template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
929 class _A1, class _A2, class _Up>
930struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
931{
932 typedef _Sp<_Up, _A0, _A1, _A2> type;
933};
934
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000935#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936
937template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000938struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939{
940 typedef _Ptr pointer;
941 typedef typename __pointer_traits_element_type<pointer>::type element_type;
942 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
943
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000944#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000945 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946#else
947 template <class _Up> struct rebind
948 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000949#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
951private:
952 struct __nat {};
953public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955 static pointer pointer_to(typename conditional<is_void<element_type>::value,
956 __nat, element_type>::type& __r)
957 {return pointer::pointer_to(__r);}
958};
959
960template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000961struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962{
963 typedef _Tp* pointer;
964 typedef _Tp element_type;
965 typedef ptrdiff_t difference_type;
966
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000967#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 template <class _Up> using rebind = _Up*;
969#else
970 template <class _Up> struct rebind {typedef _Up* other;};
971#endif
972
973private:
974 struct __nat {};
975public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000978 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000979 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980};
981
Eric Fiselierae3ab842015-08-23 02:56:05 +0000982template <class _From, class _To>
983struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000984#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000985 typedef typename pointer_traits<_From>::template rebind<_To> type;
986#else
987 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
988#endif
989};
990
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991// allocator_traits
992
Marshall Clow0be70c32017-06-14 21:23:57 +0000993template <class _Tp, class = void>
994struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995
996template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +0000997struct __has_pointer_type<_Tp,
998 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999
1000namespace __pointer_type_imp
1001{
1002
1003template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1004struct __pointer_type
1005{
1006 typedef typename _Dp::pointer type;
1007};
1008
1009template <class _Tp, class _Dp>
1010struct __pointer_type<_Tp, _Dp, false>
1011{
1012 typedef _Tp* type;
1013};
1014
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001015} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016
1017template <class _Tp, class _Dp>
1018struct __pointer_type
1019{
1020 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1021};
1022
Marshall Clow0be70c32017-06-14 21:23:57 +00001023template <class _Tp, class = void>
1024struct __has_const_pointer : false_type {};
1025
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001027struct __has_const_pointer<_Tp,
1028 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029
1030template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1031struct __const_pointer
1032{
1033 typedef typename _Alloc::const_pointer type;
1034};
1035
1036template <class _Tp, class _Ptr, class _Alloc>
1037struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1038{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001039#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1041#else
1042 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1043#endif
1044};
1045
Marshall Clow0be70c32017-06-14 21:23:57 +00001046template <class _Tp, class = void>
1047struct __has_void_pointer : false_type {};
1048
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001050struct __has_void_pointer<_Tp,
1051 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052
1053template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1054struct __void_pointer
1055{
1056 typedef typename _Alloc::void_pointer type;
1057};
1058
1059template <class _Ptr, class _Alloc>
1060struct __void_pointer<_Ptr, _Alloc, false>
1061{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001062#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1064#else
1065 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1066#endif
1067};
1068
Marshall Clow0be70c32017-06-14 21:23:57 +00001069template <class _Tp, class = void>
1070struct __has_const_void_pointer : false_type {};
1071
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001073struct __has_const_void_pointer<_Tp,
1074 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075
1076template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1077struct __const_void_pointer
1078{
1079 typedef typename _Alloc::const_void_pointer type;
1080};
1081
1082template <class _Ptr, class _Alloc>
1083struct __const_void_pointer<_Ptr, _Alloc, false>
1084{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001085#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1087#else
1088 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1089#endif
1090};
1091
Howard Hinnantc834c512011-11-29 18:15:50 +00001092template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001094_Tp*
1095__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096{
1097 return __p;
1098}
1099
1100template <class _Pointer>
1101inline _LIBCPP_INLINE_VISIBILITY
1102typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001103__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001105 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106}
1107
Marshall Clow0be70c32017-06-14 21:23:57 +00001108template <class _Tp, class = void>
1109struct __has_size_type : false_type {};
1110
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001112struct __has_size_type<_Tp,
1113 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001115template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116struct __size_type
1117{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001118 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119};
1120
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001121template <class _Alloc, class _DiffType>
1122struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123{
1124 typedef typename _Alloc::size_type type;
1125};
1126
Marshall Clow0be70c32017-06-14 21:23:57 +00001127template <class _Tp, class = void>
1128struct __has_propagate_on_container_copy_assignment : false_type {};
1129
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001131struct __has_propagate_on_container_copy_assignment<_Tp,
1132 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1133 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134
1135template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1136struct __propagate_on_container_copy_assignment
1137{
1138 typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_copy_assignment<_Alloc, true>
1143{
1144 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1145};
1146
Marshall Clow0be70c32017-06-14 21:23:57 +00001147template <class _Tp, class = void>
1148struct __has_propagate_on_container_move_assignment : false_type {};
1149
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001151struct __has_propagate_on_container_move_assignment<_Tp,
1152 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1153 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154
1155template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1156struct __propagate_on_container_move_assignment
1157{
1158 typedef false_type type;
1159};
1160
1161template <class _Alloc>
1162struct __propagate_on_container_move_assignment<_Alloc, true>
1163{
1164 typedef typename _Alloc::propagate_on_container_move_assignment type;
1165};
1166
Marshall Clow0be70c32017-06-14 21:23:57 +00001167template <class _Tp, class = void>
1168struct __has_propagate_on_container_swap : false_type {};
1169
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001171struct __has_propagate_on_container_swap<_Tp,
1172 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1173 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174
1175template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1176struct __propagate_on_container_swap
1177{
1178 typedef false_type type;
1179};
1180
1181template <class _Alloc>
1182struct __propagate_on_container_swap<_Alloc, true>
1183{
1184 typedef typename _Alloc::propagate_on_container_swap type;
1185};
1186
Marshall Clow0be70c32017-06-14 21:23:57 +00001187template <class _Tp, class = void>
1188struct __has_is_always_equal : false_type {};
1189
Marshall Clow0b587562015-06-02 16:34:03 +00001190template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001191struct __has_is_always_equal<_Tp,
1192 typename __void_t<typename _Tp::is_always_equal>::type>
1193 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001194
1195template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1196struct __is_always_equal
1197{
1198 typedef typename _VSTD::is_empty<_Alloc>::type type;
1199};
1200
1201template <class _Alloc>
1202struct __is_always_equal<_Alloc, true>
1203{
1204 typedef typename _Alloc::is_always_equal type;
1205};
1206
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1208struct __has_rebind_other
1209{
1210private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001211 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212 template <class _Xp> static __two __test(...);
1213 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1214public:
1215 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1216};
1217
1218template <class _Tp, class _Up>
1219struct __has_rebind_other<_Tp, _Up, false>
1220{
1221 static const bool value = false;
1222};
1223
1224template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1225struct __allocator_traits_rebind
1226{
1227 typedef typename _Tp::template rebind<_Up>::other type;
1228};
1229
1230#ifndef _LIBCPP_HAS_NO_VARIADICS
1231
1232template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1233struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1234{
1235 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1236};
1237
1238template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1239struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1240{
1241 typedef _Alloc<_Up, _Args...> type;
1242};
1243
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001244#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
1246template <template <class> class _Alloc, class _Tp, class _Up>
1247struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1248{
1249 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1250};
1251
1252template <template <class> class _Alloc, class _Tp, class _Up>
1253struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1254{
1255 typedef _Alloc<_Up> type;
1256};
1257
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1259struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1260{
1261 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1262};
1263
1264template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1265struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1266{
1267 typedef _Alloc<_Up, _A0> type;
1268};
1269
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1271 class _A1, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1278 class _A1, class _Up>
1279struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1280{
1281 typedef _Alloc<_Up, _A0, _A1> type;
1282};
1283
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1285 class _A1, class _A2, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1287{
1288 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1289};
1290
1291template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1292 class _A1, class _A2, class _Up>
1293struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1294{
1295 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1296};
1297
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001298#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001300#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301
1302template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1303auto
1304__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1305 -> decltype(__a.allocate(__sz, __p), true_type());
1306
1307template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1308auto
1309__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1310 -> false_type;
1311
1312template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1313struct __has_allocate_hint
1314 : integral_constant<bool,
1315 is_same<
1316 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1317 declval<_SizeType>(),
1318 declval<_ConstVoidPtr>())),
1319 true_type>::value>
1320{
1321};
1322
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001323#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324
1325template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1326struct __has_allocate_hint
1327 : true_type
1328{
1329};
1330
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001331#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001333#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334
1335template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001336decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1337 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338 true_type())
1339__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1340
1341template <class _Alloc, class _Pointer, class ..._Args>
1342false_type
1343__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1344
1345template <class _Alloc, class _Pointer, class ..._Args>
1346struct __has_construct
1347 : integral_constant<bool,
1348 is_same<
1349 decltype(__has_construct_test(declval<_Alloc>(),
1350 declval<_Pointer>(),
1351 declval<_Args>()...)),
1352 true_type>::value>
1353{
1354};
1355
1356template <class _Alloc, class _Pointer>
1357auto
1358__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1359 -> decltype(__a.destroy(__p), true_type());
1360
1361template <class _Alloc, class _Pointer>
1362auto
1363__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1364 -> false_type;
1365
1366template <class _Alloc, class _Pointer>
1367struct __has_destroy
1368 : integral_constant<bool,
1369 is_same<
1370 decltype(__has_destroy_test(declval<_Alloc>(),
1371 declval<_Pointer>())),
1372 true_type>::value>
1373{
1374};
1375
1376template <class _Alloc>
1377auto
1378__has_max_size_test(_Alloc&& __a)
1379 -> decltype(__a.max_size(), true_type());
1380
1381template <class _Alloc>
1382auto
1383__has_max_size_test(const volatile _Alloc& __a)
1384 -> false_type;
1385
1386template <class _Alloc>
1387struct __has_max_size
1388 : integral_constant<bool,
1389 is_same<
1390 decltype(__has_max_size_test(declval<_Alloc&>())),
1391 true_type>::value>
1392{
1393};
1394
1395template <class _Alloc>
1396auto
1397__has_select_on_container_copy_construction_test(_Alloc&& __a)
1398 -> decltype(__a.select_on_container_copy_construction(), true_type());
1399
1400template <class _Alloc>
1401auto
1402__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1403 -> false_type;
1404
1405template <class _Alloc>
1406struct __has_select_on_container_copy_construction
1407 : integral_constant<bool,
1408 is_same<
1409 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1410 true_type>::value>
1411{
1412};
1413
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001414#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415
1416#ifndef _LIBCPP_HAS_NO_VARIADICS
1417
1418template <class _Alloc, class _Pointer, class ..._Args>
1419struct __has_construct
1420 : false_type
1421{
1422};
1423
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001424#else // _LIBCPP_HAS_NO_VARIADICS
1425
1426template <class _Alloc, class _Pointer, class _Args>
1427struct __has_construct
1428 : false_type
1429{
1430};
1431
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001432#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433
1434template <class _Alloc, class _Pointer>
1435struct __has_destroy
1436 : false_type
1437{
1438};
1439
1440template <class _Alloc>
1441struct __has_max_size
1442 : true_type
1443{
1444};
1445
1446template <class _Alloc>
1447struct __has_select_on_container_copy_construction
1448 : false_type
1449{
1450};
1451
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001452#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001454template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1455struct __alloc_traits_difference_type
1456{
1457 typedef typename pointer_traits<_Ptr>::difference_type type;
1458};
1459
1460template <class _Alloc, class _Ptr>
1461struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1462{
1463 typedef typename _Alloc::difference_type type;
1464};
1465
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001467struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468{
1469 typedef _Alloc allocator_type;
1470 typedef typename allocator_type::value_type value_type;
1471
1472 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1473 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1474 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1475 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1476
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001477 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1478 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479
1480 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1481 propagate_on_container_copy_assignment;
1482 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1483 propagate_on_container_move_assignment;
1484 typedef typename __propagate_on_container_swap<allocator_type>::type
1485 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001486 typedef typename __is_always_equal<allocator_type>::type
1487 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001489#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001491 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001493#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 template <class _Tp> struct rebind_alloc
1495 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1496 template <class _Tp> struct rebind_traits
1497 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001498#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499
Howard Hinnant756c69b2010-09-22 16:48:34 +00001500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501 static pointer allocate(allocator_type& __a, size_type __n)
1502 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001505 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1507
Howard Hinnant756c69b2010-09-22 16:48:34 +00001508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001509 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510 {__a.deallocate(__p, __n);}
1511
1512#ifndef _LIBCPP_HAS_NO_VARIADICS
1513 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001516 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001517 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001518#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001520 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001521 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522 {
1523 ::new ((void*)__p) _Tp();
1524 }
1525 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001526 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001527 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 {
1529 ::new ((void*)__p) _Tp(__a0);
1530 }
1531 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001532 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001533 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 const _A1& __a1)
1535 {
1536 ::new ((void*)__p) _Tp(__a0, __a1);
1537 }
1538 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001540 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 const _A1& __a1, const _A2& __a2)
1542 {
1543 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1544 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001545#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546
1547 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 static void destroy(allocator_type& __a, _Tp* __p)
1550 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1551
Howard Hinnant756c69b2010-09-22 16:48:34 +00001552 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001553 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1555
Howard Hinnant756c69b2010-09-22 16:48:34 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 static allocator_type
1558 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001559 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 __has_select_on_container_copy_construction<const allocator_type>(),
1561 __a);}
1562
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001563 template <class _Ptr>
1564 _LIBCPP_INLINE_VISIBILITY
1565 static
1566 void
1567 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1568 {
1569 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1570 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1571 }
1572
1573 template <class _Tp>
1574 _LIBCPP_INLINE_VISIBILITY
1575 static
1576 typename enable_if
1577 <
1578 (is_same<allocator_type, allocator<_Tp> >::value
1579 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1580 is_trivially_move_constructible<_Tp>::value,
1581 void
1582 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001583 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001584 {
1585 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001586 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001587 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001588 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001589 __begin2 += _Np;
1590 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001591 }
1592
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001593 template <class _Iter, class _Ptr>
1594 _LIBCPP_INLINE_VISIBILITY
1595 static
1596 void
1597 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1598 {
1599 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1600 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1601 }
1602
1603 template <class _Tp>
1604 _LIBCPP_INLINE_VISIBILITY
1605 static
1606 typename enable_if
1607 <
1608 (is_same<allocator_type, allocator<_Tp> >::value
1609 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1610 is_trivially_move_constructible<_Tp>::value,
1611 void
1612 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001613 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001614 {
1615 typedef typename remove_const<_Tp>::type _Vp;
1616 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001617 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001618 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001619 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001620 __begin2 += _Np;
1621 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001622 }
1623
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001624 template <class _Ptr>
1625 _LIBCPP_INLINE_VISIBILITY
1626 static
1627 void
1628 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1629 {
1630 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001631 {
1632 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1633 --__end2;
1634 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001635 }
1636
1637 template <class _Tp>
1638 _LIBCPP_INLINE_VISIBILITY
1639 static
1640 typename enable_if
1641 <
1642 (is_same<allocator_type, allocator<_Tp> >::value
1643 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1644 is_trivially_move_constructible<_Tp>::value,
1645 void
1646 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001647 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001648 {
1649 ptrdiff_t _Np = __end1 - __begin1;
1650 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001651 if (_Np > 0)
1652 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001653 }
1654
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655private:
1656
Howard Hinnant756c69b2010-09-22 16:48:34 +00001657 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001658 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659 const_void_pointer __hint, true_type)
1660 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001661 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001662 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001663 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 {return __a.allocate(__n);}
1665
1666#ifndef _LIBCPP_HAS_NO_VARIADICS
1667 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001670 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001671 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1674 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001675 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001677#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678
1679 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1682 {__a.destroy(__p);}
1683 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685 static void __destroy(false_type, allocator_type&, _Tp* __p)
1686 {
1687 __p->~_Tp();
1688 }
1689
Howard Hinnant756c69b2010-09-22 16:48:34 +00001690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691 static size_type __max_size(true_type, const allocator_type& __a)
1692 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001695 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696
Howard Hinnant756c69b2010-09-22 16:48:34 +00001697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001699 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001703 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704 {return __a;}
1705};
1706
Marshall Clow940e01c2015-04-07 05:21:38 +00001707template <class _Traits, class _Tp>
1708struct __rebind_alloc_helper
1709{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001710#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001711 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001712#else
1713 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1714#endif
1715};
1716
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717// allocator
1718
1719template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001720class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721{
1722public:
1723 typedef size_t size_type;
1724 typedef ptrdiff_t difference_type;
1725 typedef _Tp* pointer;
1726 typedef const _Tp* const_pointer;
1727 typedef _Tp& reference;
1728 typedef const _Tp& const_reference;
1729 typedef _Tp value_type;
1730
Howard Hinnant4931e092011-06-02 21:38:57 +00001731 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001732 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001733
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1735
Howard Hinnant719bda32011-05-28 14:41:13 +00001736 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1737 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1738 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001739 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001740 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001741 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001743 {
1744 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001745 __throw_length_error("allocator<T>::allocate(size_t n)"
1746 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001747 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1748 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001749 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001750 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001751 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1752 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001753#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754 template <class _Up, class... _Args>
1755 _LIBCPP_INLINE_VISIBILITY
1756 void
1757 construct(_Up* __p, _Args&&... __args)
1758 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001759 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001761#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762 _LIBCPP_INLINE_VISIBILITY
1763 void
1764 construct(pointer __p)
1765 {
1766 ::new((void*)__p) _Tp();
1767 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001768# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001769
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770 template <class _A0>
1771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001772 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 construct(pointer __p, _A0& __a0)
1774 {
1775 ::new((void*)__p) _Tp(__a0);
1776 }
1777 template <class _A0>
1778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001779 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001780 construct(pointer __p, const _A0& __a0)
1781 {
1782 ::new((void*)__p) _Tp(__a0);
1783 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001784# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 template <class _A0, class _A1>
1786 _LIBCPP_INLINE_VISIBILITY
1787 void
1788 construct(pointer __p, _A0& __a0, _A1& __a1)
1789 {
1790 ::new((void*)__p) _Tp(__a0, __a1);
1791 }
1792 template <class _A0, class _A1>
1793 _LIBCPP_INLINE_VISIBILITY
1794 void
1795 construct(pointer __p, const _A0& __a0, _A1& __a1)
1796 {
1797 ::new((void*)__p) _Tp(__a0, __a1);
1798 }
1799 template <class _A0, class _A1>
1800 _LIBCPP_INLINE_VISIBILITY
1801 void
1802 construct(pointer __p, _A0& __a0, const _A1& __a1)
1803 {
1804 ::new((void*)__p) _Tp(__a0, __a1);
1805 }
1806 template <class _A0, class _A1>
1807 _LIBCPP_INLINE_VISIBILITY
1808 void
1809 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1810 {
1811 ::new((void*)__p) _Tp(__a0, __a1);
1812 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001813#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1815};
1816
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001817template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001818class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001819{
1820public:
1821 typedef size_t size_type;
1822 typedef ptrdiff_t difference_type;
1823 typedef const _Tp* pointer;
1824 typedef const _Tp* const_pointer;
1825 typedef const _Tp& reference;
1826 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001827 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001828
1829 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001830 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001831
1832 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1833
1834 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1835 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1836 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1837 {return _VSTD::addressof(__x);}
1838 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001839 {
1840 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001841 __throw_length_error("allocator<const T>::allocate(size_t n)"
1842 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001843 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001844 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001845 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00001846 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001847 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1848 {return size_type(~0) / sizeof(_Tp);}
1849#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1850 template <class _Up, class... _Args>
1851 _LIBCPP_INLINE_VISIBILITY
1852 void
1853 construct(_Up* __p, _Args&&... __args)
1854 {
1855 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1856 }
1857#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1858 _LIBCPP_INLINE_VISIBILITY
1859 void
1860 construct(pointer __p)
1861 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001862 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001863 }
1864# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001865
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001866 template <class _A0>
1867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001868 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001869 construct(pointer __p, _A0& __a0)
1870 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001871 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001872 }
1873 template <class _A0>
1874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001875 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001876 construct(pointer __p, const _A0& __a0)
1877 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001878 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001879 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001880# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1881 template <class _A0, class _A1>
1882 _LIBCPP_INLINE_VISIBILITY
1883 void
1884 construct(pointer __p, _A0& __a0, _A1& __a1)
1885 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001886 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001887 }
1888 template <class _A0, class _A1>
1889 _LIBCPP_INLINE_VISIBILITY
1890 void
1891 construct(pointer __p, const _A0& __a0, _A1& __a1)
1892 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001893 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001894 }
1895 template <class _A0, class _A1>
1896 _LIBCPP_INLINE_VISIBILITY
1897 void
1898 construct(pointer __p, _A0& __a0, const _A1& __a1)
1899 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001900 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001901 }
1902 template <class _A0, class _A1>
1903 _LIBCPP_INLINE_VISIBILITY
1904 void
1905 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1906 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001907 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001908 }
1909#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1910 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1911};
1912
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913template <class _Tp, class _Up>
1914inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001915bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916
1917template <class _Tp, class _Up>
1918inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001919bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920
1921template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001922class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923 : public iterator<output_iterator_tag,
1924 _Tp, // purposefully not C++03
1925 ptrdiff_t, // purposefully not C++03
1926 _Tp*, // purposefully not C++03
1927 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1928{
1929private:
1930 _OutputIterator __x_;
1931public:
1932 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1933 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1934 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1935 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001936#if _LIBCPP_STD_VER >= 14
1937 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1938 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1939#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1941 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1942 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001943#if _LIBCPP_STD_VER >= 14
1944 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1945#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946};
1947
1948template <class _Tp>
1949pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001950get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951{
1952 pair<_Tp*, ptrdiff_t> __r(0, 0);
1953 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1954 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1955 / sizeof(_Tp);
1956 if (__n > __m)
1957 __n = __m;
1958 while (__n > 0)
1959 {
1960 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1961 if (__r.first)
1962 {
1963 __r.second = __n;
1964 break;
1965 }
1966 __n /= 2;
1967 }
1968 return __r;
1969}
1970
1971template <class _Tp>
1972inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001973void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974
Marshall Clowb22274f2017-01-24 22:22:33 +00001975#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976template <class _Tp>
1977struct auto_ptr_ref
1978{
1979 _Tp* __ptr_;
1980};
1981
1982template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001983class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984{
1985private:
1986 _Tp* __ptr_;
1987public:
1988 typedef _Tp element_type;
1989
1990 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1991 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1992 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1993 : __ptr_(__p.release()) {}
1994 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1995 {reset(__p.release()); return *this;}
1996 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1997 {reset(__p.release()); return *this;}
1998 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1999 {reset(__p.__ptr_); return *this;}
2000 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2001
2002 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2003 {return *__ptr_;}
2004 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2005 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2006 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2007 {
2008 _Tp* __t = __ptr_;
2009 __ptr_ = 0;
2010 return __t;
2011 }
2012 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2013 {
2014 if (__ptr_ != __p)
2015 delete __ptr_;
2016 __ptr_ = __p;
2017 }
2018
2019 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2020 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2021 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2022 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2023 {return auto_ptr<_Up>(release());}
2024};
2025
2026template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002027class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028{
2029public:
2030 typedef void element_type;
2031};
Marshall Clowb22274f2017-01-24 22:22:33 +00002032#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033
Eric Fiselier9d355982017-04-12 23:45:53 +00002034template <class _Tp, int _Idx,
2035 bool _CanBeEmptyBase =
2036 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2037struct __compressed_pair_elem {
2038 typedef _Tp _ParamT;
2039 typedef _Tp& reference;
2040 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041
Eric Fiselier9d355982017-04-12 23:45:53 +00002042#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002043 constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044
Eric Fiselier9d355982017-04-12 23:45:53 +00002045 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002046 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2047 >::type>
2048 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002049 __compressed_pair_elem(_Up&& __u)
2050 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051
Eric Fiselier9d355982017-04-12 23:45:53 +00002052 template <class... _Args, size_t... _Indexes>
2053 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2054 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2055 __tuple_indices<_Indexes...>)
2056 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2057#else
2058 __compressed_pair_elem() : __value_() {}
2059 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2060#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002061
Eric Fiselier9d355982017-04-12 23:45:53 +00002062 reference __get() _NOEXCEPT { return __value_; }
2063 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002066 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067};
2068
Eric Fiselier9d355982017-04-12 23:45:53 +00002069template <class _Tp, int _Idx>
2070struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2071 typedef _Tp _ParamT;
2072 typedef _Tp& reference;
2073 typedef const _Tp& const_reference;
2074 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075
Eric Fiselier9d355982017-04-12 23:45:53 +00002076#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002077 constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078
Eric Fiselier9d355982017-04-12 23:45:53 +00002079 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002080 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2081 >::type>
2082 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002083 __compressed_pair_elem(_Up&& __u)
2084 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085
Eric Fiselier9d355982017-04-12 23:45:53 +00002086 template <class... _Args, size_t... _Indexes>
2087 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2088 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2089 __tuple_indices<_Indexes...>)
2090 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2091#else
2092 __compressed_pair_elem() : __value_type() {}
2093 __compressed_pair_elem(_ParamT __p)
2094 : __value_type(std::forward<_ParamT>(__p)) {}
2095#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096
Eric Fiselier9d355982017-04-12 23:45:53 +00002097 reference __get() _NOEXCEPT { return *this; }
2098 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099};
2100
Eric Fiselier9d355982017-04-12 23:45:53 +00002101// Tag used to construct the second element of the compressed pair.
2102struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103
2104template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002105class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2106 private __compressed_pair_elem<_T2, 1> {
2107 typedef __compressed_pair_elem<_T1, 0> _Base1;
2108 typedef __compressed_pair_elem<_T2, 1> _Base2;
2109
2110 // NOTE: This static assert should never fire because __compressed_pair
2111 // is *almost never* used in a scenario where it's possible for T1 == T2.
2112 // (The exception is std::function where it is possible that the function
2113 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002114 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002115 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2116 "The current implementation is NOT ABI-compatible with the previous "
2117 "implementation for this configuration");
2118
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002120#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002121 template <bool _Dummy = true,
2122 class = typename enable_if<
2123 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2124 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2125 >::type
2126 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002127 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002128 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129
Eric Fiselier9d355982017-04-12 23:45:53 +00002130 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2131 __compressed_pair>::value,
2132 bool>::type = true>
2133 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2134 __compressed_pair(_Tp&& __t)
2135 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136
Eric Fiselier9d355982017-04-12 23:45:53 +00002137 template <class _Tp>
2138 _LIBCPP_INLINE_VISIBILITY constexpr
2139 __compressed_pair(__second_tag, _Tp&& __t)
2140 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141
Eric Fiselier9d355982017-04-12 23:45:53 +00002142 template <class _U1, class _U2>
2143 _LIBCPP_INLINE_VISIBILITY constexpr
2144 __compressed_pair(_U1&& __t1, _U2&& __t2)
2145 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146
Eric Fiselier9d355982017-04-12 23:45:53 +00002147 template <class... _Args1, class... _Args2>
2148 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2149 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2150 tuple<_Args2...> __second_args)
2151 : _Base1(__pc, _VSTD::move(__first_args),
2152 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2153 _Base2(__pc, _VSTD::move(__second_args),
2154 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002155
Eric Fiselier9d355982017-04-12 23:45:53 +00002156#else
2157 _LIBCPP_INLINE_VISIBILITY
2158 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002159
Eric Fiselier9d355982017-04-12 23:45:53 +00002160 _LIBCPP_INLINE_VISIBILITY explicit
2161 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002162
Eric Fiselier9d355982017-04-12 23:45:53 +00002163 _LIBCPP_INLINE_VISIBILITY
2164 __compressed_pair(__second_tag, _T2 __t2)
2165 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166
Eric Fiselier9d355982017-04-12 23:45:53 +00002167 _LIBCPP_INLINE_VISIBILITY
2168 __compressed_pair(_T1 __t1, _T2 __t2)
2169 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2170#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002171
Eric Fiselier9d355982017-04-12 23:45:53 +00002172 _LIBCPP_INLINE_VISIBILITY
2173 typename _Base1::reference first() _NOEXCEPT {
2174 return static_cast<_Base1&>(*this).__get();
2175 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176
Eric Fiselier9d355982017-04-12 23:45:53 +00002177 _LIBCPP_INLINE_VISIBILITY
2178 typename _Base1::const_reference first() const _NOEXCEPT {
2179 return static_cast<_Base1 const&>(*this).__get();
2180 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181
Eric Fiselier9d355982017-04-12 23:45:53 +00002182 _LIBCPP_INLINE_VISIBILITY
2183 typename _Base2::reference second() _NOEXCEPT {
2184 return static_cast<_Base2&>(*this).__get();
2185 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186
Eric Fiselier9d355982017-04-12 23:45:53 +00002187 _LIBCPP_INLINE_VISIBILITY
2188 typename _Base2::const_reference second() const _NOEXCEPT {
2189 return static_cast<_Base2 const&>(*this).__get();
2190 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191
Eric Fiselier9d355982017-04-12 23:45:53 +00002192 _LIBCPP_INLINE_VISIBILITY
2193 void swap(__compressed_pair& __x)
2194 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2195 __is_nothrow_swappable<_T2>::value)
2196 {
2197 using std::swap;
2198 swap(first(), __x.first());
2199 swap(second(), __x.second());
2200 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201};
2202
2203template <class _T1, class _T2>
2204inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002205void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2206 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2207 __is_nothrow_swappable<_T2>::value) {
2208 __x.swap(__y);
2209}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002211// default_delete
2212
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002214struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002215 static_assert(!is_function<_Tp>::value,
2216 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002217#ifndef _LIBCPP_CXX03_LANG
2218 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002219#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002220 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002221#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002222 template <class _Up>
2223 _LIBCPP_INLINE_VISIBILITY
2224 default_delete(const default_delete<_Up>&,
2225 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2226 0) _NOEXCEPT {}
2227
2228 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2229 static_assert(sizeof(_Tp) > 0,
2230 "default_delete can not delete incomplete type");
2231 static_assert(!is_void<_Tp>::value,
2232 "default_delete can not delete incomplete type");
2233 delete __ptr;
2234 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235};
2236
2237template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002238struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2239private:
2240 template <class _Up>
2241 struct _EnableIfConvertible
2242 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2243
Howard Hinnant4500ca52011-12-18 21:19:44 +00002244public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002245#ifndef _LIBCPP_CXX03_LANG
2246 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002247#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002248 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002249#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002250
2251 template <class _Up>
2252 _LIBCPP_INLINE_VISIBILITY
2253 default_delete(const default_delete<_Up[]>&,
2254 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2255
2256 template <class _Up>
2257 _LIBCPP_INLINE_VISIBILITY
2258 typename _EnableIfConvertible<_Up>::type
2259 operator()(_Up* __ptr) const _NOEXCEPT {
2260 static_assert(sizeof(_Tp) > 0,
2261 "default_delete can not delete incomplete type");
2262 static_assert(!is_void<_Tp>::value,
2263 "default_delete can not delete void type");
2264 delete[] __ptr;
2265 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266};
2267
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268
Eric Fiselier6779b062017-04-16 02:06:25 +00002269
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002270#ifndef _LIBCPP_CXX03_LANG
2271template <class _Deleter>
2272struct __unique_ptr_deleter_sfinae {
2273 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2274 typedef const _Deleter& __lval_ref_type;
2275 typedef _Deleter&& __good_rval_ref_type;
2276 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277};
2278
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002279template <class _Deleter>
2280struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2281 typedef const _Deleter& __lval_ref_type;
2282 typedef const _Deleter&& __bad_rval_ref_type;
2283 typedef false_type __enable_rval_overload;
2284};
2285
2286template <class _Deleter>
2287struct __unique_ptr_deleter_sfinae<_Deleter&> {
2288 typedef _Deleter& __lval_ref_type;
2289 typedef _Deleter&& __bad_rval_ref_type;
2290 typedef false_type __enable_rval_overload;
2291};
2292#endif // !defined(_LIBCPP_CXX03_LANG)
2293
2294template <class _Tp, class _Dp = default_delete<_Tp> >
2295class _LIBCPP_TEMPLATE_VIS unique_ptr {
2296public:
2297 typedef _Tp element_type;
2298 typedef _Dp deleter_type;
2299 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2300
2301 static_assert(!is_rvalue_reference<deleter_type>::value,
2302 "the specified deleter type cannot be an rvalue reference");
2303
2304private:
2305 __compressed_pair<pointer, deleter_type> __ptr_;
2306
2307 struct __nat { int __for_bool_; };
2308
2309#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002310 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002311
2312 template <bool _Dummy>
2313 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002314 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002315
2316 template <bool _Dummy>
2317 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002318 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002319
2320 template <bool _Dummy>
2321 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002322 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002323
2324 template <bool _Dummy, class _Deleter = typename __dependent_type<
2325 __identity<deleter_type>, _Dummy>::type>
2326 using _EnableIfDeleterDefaultConstructible =
2327 typename enable_if<is_default_constructible<_Deleter>::value &&
2328 !is_pointer<_Deleter>::value>::type;
2329
2330 template <class _ArgType>
2331 using _EnableIfDeleterConstructible =
2332 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2333
2334 template <class _UPtr, class _Up>
2335 using _EnableIfMoveConvertible = typename enable_if<
2336 is_convertible<typename _UPtr::pointer, pointer>::value &&
2337 !is_array<_Up>::value
2338 >::type;
2339
2340 template <class _UDel>
2341 using _EnableIfDeleterConvertible = typename enable_if<
2342 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2343 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2344 >::type;
2345
2346 template <class _UDel>
2347 using _EnableIfDeleterAssignable = typename enable_if<
2348 is_assignable<_Dp&, _UDel&&>::value
2349 >::type;
2350
2351public:
2352 template <bool _Dummy = true,
2353 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2354 _LIBCPP_INLINE_VISIBILITY
2355 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2356
2357 template <bool _Dummy = true,
2358 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2359 _LIBCPP_INLINE_VISIBILITY
2360 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2361
2362 template <bool _Dummy = true,
2363 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2364 _LIBCPP_INLINE_VISIBILITY
2365 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2366
2367 template <bool _Dummy = true,
2368 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2369 _LIBCPP_INLINE_VISIBILITY
2370 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2371 : __ptr_(__p, __d) {}
2372
2373 template <bool _Dummy = true,
2374 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2375 _LIBCPP_INLINE_VISIBILITY
2376 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2377 : __ptr_(__p, _VSTD::move(__d)) {
2378 static_assert(!is_reference<deleter_type>::value,
2379 "rvalue deleter bound to reference");
2380 }
2381
2382 template <bool _Dummy = true,
2383 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2384 _LIBCPP_INLINE_VISIBILITY
2385 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2386
2387 _LIBCPP_INLINE_VISIBILITY
2388 unique_ptr(unique_ptr&& __u) noexcept
2389 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2390 }
2391
2392 template <class _Up, class _Ep,
2393 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2394 class = _EnableIfDeleterConvertible<_Ep>
2395 >
2396 _LIBCPP_INLINE_VISIBILITY
2397 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2398 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2399
2400#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2401 template <class _Up>
2402 _LIBCPP_INLINE_VISIBILITY
2403 unique_ptr(auto_ptr<_Up>&& __p,
2404 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2405 is_same<_Dp, default_delete<_Tp>>::value,
2406 __nat>::type = __nat()) _NOEXCEPT
2407 : __ptr_(__p.release()) {}
2408#endif
2409
2410 _LIBCPP_INLINE_VISIBILITY
2411 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2412 reset(__u.release());
2413 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2414 return *this;
2415 }
2416
2417 template <class _Up, class _Ep,
2418 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2419 class = _EnableIfDeleterAssignable<_Ep>
2420 >
2421 _LIBCPP_INLINE_VISIBILITY
2422 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2423 reset(__u.release());
2424 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2425 return *this;
2426 }
2427
2428#else // _LIBCPP_CXX03_LANG
2429private:
2430 unique_ptr(unique_ptr&);
2431 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2432
2433 unique_ptr& operator=(unique_ptr&);
2434 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2435
2436public:
2437 _LIBCPP_INLINE_VISIBILITY
2438 unique_ptr() : __ptr_(pointer())
2439 {
2440 static_assert(!is_pointer<deleter_type>::value,
2441 "unique_ptr constructed with null function pointer deleter");
2442 static_assert(is_default_constructible<deleter_type>::value,
2443 "unique_ptr::deleter_type is not default constructible");
2444 }
2445 _LIBCPP_INLINE_VISIBILITY
2446 unique_ptr(nullptr_t) : __ptr_(pointer())
2447 {
2448 static_assert(!is_pointer<deleter_type>::value,
2449 "unique_ptr constructed with null function pointer deleter");
2450 }
2451 _LIBCPP_INLINE_VISIBILITY
2452 explicit unique_ptr(pointer __p)
2453 : __ptr_(_VSTD::move(__p)) {
2454 static_assert(!is_pointer<deleter_type>::value,
2455 "unique_ptr constructed with null function pointer deleter");
2456 }
2457
2458 _LIBCPP_INLINE_VISIBILITY
2459 operator __rv<unique_ptr>() {
2460 return __rv<unique_ptr>(*this);
2461 }
2462
2463 _LIBCPP_INLINE_VISIBILITY
2464 unique_ptr(__rv<unique_ptr> __u)
2465 : __ptr_(__u->release(),
2466 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2467
2468 template <class _Up, class _Ep>
2469 _LIBCPP_INLINE_VISIBILITY
2470 typename enable_if<
2471 !is_array<_Up>::value &&
2472 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2473 pointer>::value &&
2474 is_assignable<deleter_type&, _Ep&>::value,
2475 unique_ptr&>::type
2476 operator=(unique_ptr<_Up, _Ep> __u) {
2477 reset(__u.release());
2478 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2479 return *this;
2480 }
2481
2482 _LIBCPP_INLINE_VISIBILITY
2483 unique_ptr(pointer __p, deleter_type __d)
2484 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2485#endif // _LIBCPP_CXX03_LANG
2486
2487#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2488 template <class _Up>
2489 _LIBCPP_INLINE_VISIBILITY
2490 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2491 is_same<_Dp, default_delete<_Tp> >::value,
2492 unique_ptr&>::type
2493 operator=(auto_ptr<_Up> __p) {
2494 reset(__p.release());
2495 return *this;
2496 }
2497#endif
2498
2499 _LIBCPP_INLINE_VISIBILITY
2500 ~unique_ptr() { reset(); }
2501
2502 _LIBCPP_INLINE_VISIBILITY
2503 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2504 reset();
2505 return *this;
2506 }
2507
2508 _LIBCPP_INLINE_VISIBILITY
2509 typename add_lvalue_reference<_Tp>::type
2510 operator*() const {
2511 return *__ptr_.first();
2512 }
2513 _LIBCPP_INLINE_VISIBILITY
2514 pointer operator->() const _NOEXCEPT {
2515 return __ptr_.first();
2516 }
2517 _LIBCPP_INLINE_VISIBILITY
2518 pointer get() const _NOEXCEPT {
2519 return __ptr_.first();
2520 }
2521 _LIBCPP_INLINE_VISIBILITY
2522 deleter_type& get_deleter() _NOEXCEPT {
2523 return __ptr_.second();
2524 }
2525 _LIBCPP_INLINE_VISIBILITY
2526 const deleter_type& get_deleter() const _NOEXCEPT {
2527 return __ptr_.second();
2528 }
2529 _LIBCPP_INLINE_VISIBILITY
2530 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2531 return __ptr_.first() != nullptr;
2532 }
2533
2534 _LIBCPP_INLINE_VISIBILITY
2535 pointer release() _NOEXCEPT {
2536 pointer __t = __ptr_.first();
2537 __ptr_.first() = pointer();
2538 return __t;
2539 }
2540
2541 _LIBCPP_INLINE_VISIBILITY
2542 void reset(pointer __p = pointer()) _NOEXCEPT {
2543 pointer __tmp = __ptr_.first();
2544 __ptr_.first() = __p;
2545 if (__tmp)
2546 __ptr_.second()(__tmp);
2547 }
2548
2549 _LIBCPP_INLINE_VISIBILITY
2550 void swap(unique_ptr& __u) _NOEXCEPT {
2551 __ptr_.swap(__u.__ptr_);
2552 }
2553};
2554
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002555
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002557class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002559 typedef _Tp element_type;
2560 typedef _Dp deleter_type;
2561 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2562
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002564 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565
Eric Fiselier31127cd2017-04-16 02:14:31 +00002566 template <class _From>
2567 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568
Eric Fiselier31127cd2017-04-16 02:14:31 +00002569 template <class _FromElem>
2570 struct _CheckArrayPointerConversion<_FromElem*>
2571 : integral_constant<bool,
2572 is_same<_FromElem*, pointer>::value ||
2573 (is_same<pointer, element_type*>::value &&
2574 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2575 >
2576 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002578#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002579 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002580
2581 template <bool _Dummy>
2582 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002583 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002584
2585 template <bool _Dummy>
2586 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002587 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002588
2589 template <bool _Dummy>
2590 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002591 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002592
2593 template <bool _Dummy, class _Deleter = typename __dependent_type<
2594 __identity<deleter_type>, _Dummy>::type>
2595 using _EnableIfDeleterDefaultConstructible =
2596 typename enable_if<is_default_constructible<_Deleter>::value &&
2597 !is_pointer<_Deleter>::value>::type;
2598
2599 template <class _ArgType>
2600 using _EnableIfDeleterConstructible =
2601 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2602
2603 template <class _Pp>
2604 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002605 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002606 >::type;
2607
2608 template <class _UPtr, class _Up,
2609 class _ElemT = typename _UPtr::element_type>
2610 using _EnableIfMoveConvertible = typename enable_if<
2611 is_array<_Up>::value &&
2612 is_same<pointer, element_type*>::value &&
2613 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2614 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2615 >::type;
2616
2617 template <class _UDel>
2618 using _EnableIfDeleterConvertible = typename enable_if<
2619 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2620 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2621 >::type;
2622
2623 template <class _UDel>
2624 using _EnableIfDeleterAssignable = typename enable_if<
2625 is_assignable<_Dp&, _UDel&&>::value
2626 >::type;
2627
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002629 template <bool _Dummy = true,
2630 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2631 _LIBCPP_INLINE_VISIBILITY
2632 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002634 template <bool _Dummy = true,
2635 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2636 _LIBCPP_INLINE_VISIBILITY
2637 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002639 template <class _Pp, bool _Dummy = true,
2640 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2641 class = _EnableIfPointerConvertible<_Pp>>
2642 _LIBCPP_INLINE_VISIBILITY
2643 explicit unique_ptr(_Pp __p) noexcept
2644 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002646 template <class _Pp, bool _Dummy = true,
2647 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2648 class = _EnableIfPointerConvertible<_Pp>>
2649 _LIBCPP_INLINE_VISIBILITY
2650 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2651 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002653 template <bool _Dummy = true,
2654 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2655 _LIBCPP_INLINE_VISIBILITY
2656 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2657 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002659 template <class _Pp, bool _Dummy = true,
2660 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2661 class = _EnableIfPointerConvertible<_Pp>>
2662 _LIBCPP_INLINE_VISIBILITY
2663 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2664 : __ptr_(__p, _VSTD::move(__d)) {
2665 static_assert(!is_reference<deleter_type>::value,
2666 "rvalue deleter bound to reference");
2667 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002669 template <bool _Dummy = true,
2670 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2671 _LIBCPP_INLINE_VISIBILITY
2672 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2673 : __ptr_(nullptr, _VSTD::move(__d)) {
2674 static_assert(!is_reference<deleter_type>::value,
2675 "rvalue deleter bound to reference");
2676 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002677
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002678 template <class _Pp, bool _Dummy = true,
2679 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2680 class = _EnableIfPointerConvertible<_Pp>>
2681 _LIBCPP_INLINE_VISIBILITY
2682 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002683
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002684 _LIBCPP_INLINE_VISIBILITY
2685 unique_ptr(unique_ptr&& __u) noexcept
2686 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2687 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002688
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002689 _LIBCPP_INLINE_VISIBILITY
2690 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2691 reset(__u.release());
2692 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2693 return *this;
2694 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002696 template <class _Up, class _Ep,
2697 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2698 class = _EnableIfDeleterConvertible<_Ep>
2699 >
2700 _LIBCPP_INLINE_VISIBILITY
2701 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002702 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002703 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002705 template <class _Up, class _Ep,
2706 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2707 class = _EnableIfDeleterAssignable<_Ep>
2708 >
2709 _LIBCPP_INLINE_VISIBILITY
2710 unique_ptr&
2711 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2712 reset(__u.release());
2713 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2714 return *this;
2715 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002719 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002720
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002721 unique_ptr(unique_ptr&);
2722 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2723
2724 unique_ptr& operator=(unique_ptr&);
2725 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2726
2727 template <class _Up>
2728 unique_ptr(_Up __u,
2729 typename conditional<
2730 is_reference<deleter_type>::value, deleter_type,
2731 typename add_lvalue_reference<const deleter_type>::type>::type,
2732 typename enable_if<is_convertible<_Up, pointer>::value,
2733 __nat>::type = __nat());
2734public:
2735 _LIBCPP_INLINE_VISIBILITY
2736 unique_ptr() : __ptr_(pointer()) {
2737 static_assert(!is_pointer<deleter_type>::value,
2738 "unique_ptr constructed with null function pointer deleter");
2739 }
2740 _LIBCPP_INLINE_VISIBILITY
2741 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2742 static_assert(!is_pointer<deleter_type>::value,
2743 "unique_ptr constructed with null function pointer deleter");
2744 }
2745
2746 _LIBCPP_INLINE_VISIBILITY
2747 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2748 static_assert(!is_pointer<deleter_type>::value,
2749 "unique_ptr constructed with null function pointer deleter");
2750 }
2751
2752 _LIBCPP_INLINE_VISIBILITY
2753 unique_ptr(pointer __p, deleter_type __d)
2754 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2755
2756 _LIBCPP_INLINE_VISIBILITY
2757 unique_ptr(nullptr_t, deleter_type __d)
2758 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2759
2760 _LIBCPP_INLINE_VISIBILITY
2761 operator __rv<unique_ptr>() {
2762 return __rv<unique_ptr>(*this);
2763 }
2764
2765 _LIBCPP_INLINE_VISIBILITY
2766 unique_ptr(__rv<unique_ptr> __u)
2767 : __ptr_(__u->release(),
2768 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2769
2770 _LIBCPP_INLINE_VISIBILITY
2771 unique_ptr& operator=(__rv<unique_ptr> __u) {
2772 reset(__u->release());
2773 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2774 return *this;
2775 }
2776
2777#endif // _LIBCPP_CXX03_LANG
2778
2779public:
2780 _LIBCPP_INLINE_VISIBILITY
2781 ~unique_ptr() { reset(); }
2782
2783 _LIBCPP_INLINE_VISIBILITY
2784 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2785 reset();
2786 return *this;
2787 }
2788
2789 _LIBCPP_INLINE_VISIBILITY
2790 typename add_lvalue_reference<_Tp>::type
2791 operator[](size_t __i) const {
2792 return __ptr_.first()[__i];
2793 }
2794 _LIBCPP_INLINE_VISIBILITY
2795 pointer get() const _NOEXCEPT {
2796 return __ptr_.first();
2797 }
2798
2799 _LIBCPP_INLINE_VISIBILITY
2800 deleter_type& get_deleter() _NOEXCEPT {
2801 return __ptr_.second();
2802 }
2803
2804 _LIBCPP_INLINE_VISIBILITY
2805 const deleter_type& get_deleter() const _NOEXCEPT {
2806 return __ptr_.second();
2807 }
2808 _LIBCPP_INLINE_VISIBILITY
2809 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2810 return __ptr_.first() != nullptr;
2811 }
2812
2813 _LIBCPP_INLINE_VISIBILITY
2814 pointer release() _NOEXCEPT {
2815 pointer __t = __ptr_.first();
2816 __ptr_.first() = pointer();
2817 return __t;
2818 }
2819
2820 template <class _Pp>
2821 _LIBCPP_INLINE_VISIBILITY
2822 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002823 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002824 >::type
2825 reset(_Pp __p) _NOEXCEPT {
2826 pointer __tmp = __ptr_.first();
2827 __ptr_.first() = __p;
2828 if (__tmp)
2829 __ptr_.second()(__tmp);
2830 }
2831
2832 _LIBCPP_INLINE_VISIBILITY
2833 void reset(nullptr_t = nullptr) _NOEXCEPT {
2834 pointer __tmp = __ptr_.first();
2835 __ptr_.first() = nullptr;
2836 if (__tmp)
2837 __ptr_.second()(__tmp);
2838 }
2839
2840 _LIBCPP_INLINE_VISIBILITY
2841 void swap(unique_ptr& __u) _NOEXCEPT {
2842 __ptr_.swap(__u.__ptr_);
2843 }
2844
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845};
2846
2847template <class _Tp, class _Dp>
2848inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002849typename enable_if<
2850 __is_swappable<_Dp>::value,
2851 void
2852>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002853swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854
2855template <class _T1, class _D1, class _T2, class _D2>
2856inline _LIBCPP_INLINE_VISIBILITY
2857bool
2858operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2859
2860template <class _T1, class _D1, class _T2, class _D2>
2861inline _LIBCPP_INLINE_VISIBILITY
2862bool
2863operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2864
2865template <class _T1, class _D1, class _T2, class _D2>
2866inline _LIBCPP_INLINE_VISIBILITY
2867bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002868operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2869{
2870 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2871 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002872 typedef typename common_type<_P1, _P2>::type _Vp;
2873 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002874}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875
2876template <class _T1, class _D1, class _T2, class _D2>
2877inline _LIBCPP_INLINE_VISIBILITY
2878bool
2879operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2880
2881template <class _T1, class _D1, class _T2, class _D2>
2882inline _LIBCPP_INLINE_VISIBILITY
2883bool
2884operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2885
2886template <class _T1, class _D1, class _T2, class _D2>
2887inline _LIBCPP_INLINE_VISIBILITY
2888bool
2889operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2890
Howard Hinnantb17caf92012-02-21 21:02:58 +00002891template <class _T1, class _D1>
2892inline _LIBCPP_INLINE_VISIBILITY
2893bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002894operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002895{
2896 return !__x;
2897}
2898
2899template <class _T1, class _D1>
2900inline _LIBCPP_INLINE_VISIBILITY
2901bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002902operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002903{
2904 return !__x;
2905}
2906
2907template <class _T1, class _D1>
2908inline _LIBCPP_INLINE_VISIBILITY
2909bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002910operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002911{
2912 return static_cast<bool>(__x);
2913}
2914
2915template <class _T1, class _D1>
2916inline _LIBCPP_INLINE_VISIBILITY
2917bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002918operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002919{
2920 return static_cast<bool>(__x);
2921}
2922
2923template <class _T1, class _D1>
2924inline _LIBCPP_INLINE_VISIBILITY
2925bool
2926operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2927{
2928 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2929 return less<_P1>()(__x.get(), nullptr);
2930}
2931
2932template <class _T1, class _D1>
2933inline _LIBCPP_INLINE_VISIBILITY
2934bool
2935operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2936{
2937 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2938 return less<_P1>()(nullptr, __x.get());
2939}
2940
2941template <class _T1, class _D1>
2942inline _LIBCPP_INLINE_VISIBILITY
2943bool
2944operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2945{
2946 return nullptr < __x;
2947}
2948
2949template <class _T1, class _D1>
2950inline _LIBCPP_INLINE_VISIBILITY
2951bool
2952operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2953{
2954 return __x < nullptr;
2955}
2956
2957template <class _T1, class _D1>
2958inline _LIBCPP_INLINE_VISIBILITY
2959bool
2960operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2961{
2962 return !(nullptr < __x);
2963}
2964
2965template <class _T1, class _D1>
2966inline _LIBCPP_INLINE_VISIBILITY
2967bool
2968operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2969{
2970 return !(__x < nullptr);
2971}
2972
2973template <class _T1, class _D1>
2974inline _LIBCPP_INLINE_VISIBILITY
2975bool
2976operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2977{
2978 return !(__x < nullptr);
2979}
2980
2981template <class _T1, class _D1>
2982inline _LIBCPP_INLINE_VISIBILITY
2983bool
2984operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2985{
2986 return !(nullptr < __x);
2987}
2988
Howard Hinnant9e028f12012-05-01 15:37:54 +00002989#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2990
2991template <class _Tp, class _Dp>
2992inline _LIBCPP_INLINE_VISIBILITY
2993unique_ptr<_Tp, _Dp>
2994move(unique_ptr<_Tp, _Dp>& __t)
2995{
2996 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
2997}
2998
2999#endif
3000
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003001#if _LIBCPP_STD_VER > 11
3002
3003template<class _Tp>
3004struct __unique_if
3005{
3006 typedef unique_ptr<_Tp> __unique_single;
3007};
3008
3009template<class _Tp>
3010struct __unique_if<_Tp[]>
3011{
3012 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3013};
3014
3015template<class _Tp, size_t _Np>
3016struct __unique_if<_Tp[_Np]>
3017{
3018 typedef void __unique_array_known_bound;
3019};
3020
3021template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003022inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003023typename __unique_if<_Tp>::__unique_single
3024make_unique(_Args&&... __args)
3025{
3026 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3027}
3028
3029template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003030inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003031typename __unique_if<_Tp>::__unique_array_unknown_bound
3032make_unique(size_t __n)
3033{
3034 typedef typename remove_extent<_Tp>::type _Up;
3035 return unique_ptr<_Tp>(new _Up[__n]());
3036}
3037
3038template<class _Tp, class... _Args>
3039 typename __unique_if<_Tp>::__unique_array_known_bound
3040 make_unique(_Args&&...) = delete;
3041
3042#endif // _LIBCPP_STD_VER > 11
3043
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003044template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003045#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003046struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003047#else
3048struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3049 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3050#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003051{
3052 typedef unique_ptr<_Tp, _Dp> argument_type;
3053 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003054 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003055 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003056 {
3057 typedef typename argument_type::pointer pointer;
3058 return hash<pointer>()(__ptr.get());
3059 }
3060};
3061
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062struct __destruct_n
3063{
3064private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003065 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066
3067 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003068 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003069 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070
3071 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003072 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 {}
3074
Howard Hinnant719bda32011-05-28 14:41:13 +00003075 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003076 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003077 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078 {}
3079
Howard Hinnant719bda32011-05-28 14:41:13 +00003080 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003081 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003082 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083 {}
3084public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003085 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003086 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087
3088 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003089 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003090 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091
3092 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003093 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003094 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095
3096 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003097 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003098 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099};
3100
3101template <class _Alloc>
3102class __allocator_destructor
3103{
3104 typedef allocator_traits<_Alloc> __alloc_traits;
3105public:
3106 typedef typename __alloc_traits::pointer pointer;
3107 typedef typename __alloc_traits::size_type size_type;
3108private:
3109 _Alloc& __alloc_;
3110 size_type __s_;
3111public:
3112 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003113 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003116 void operator()(pointer __p) _NOEXCEPT
3117 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118};
3119
3120template <class _InputIterator, class _ForwardIterator>
3121_ForwardIterator
3122uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3123{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003125#ifndef _LIBCPP_NO_EXCEPTIONS
3126 _ForwardIterator __s = __r;
3127 try
3128 {
3129#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003130 for (; __f != __l; ++__f, (void) ++__r)
3131 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003132#ifndef _LIBCPP_NO_EXCEPTIONS
3133 }
3134 catch (...)
3135 {
3136 for (; __s != __r; ++__s)
3137 __s->~value_type();
3138 throw;
3139 }
3140#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 return __r;
3142}
3143
3144template <class _InputIterator, class _Size, class _ForwardIterator>
3145_ForwardIterator
3146uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3147{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003149#ifndef _LIBCPP_NO_EXCEPTIONS
3150 _ForwardIterator __s = __r;
3151 try
3152 {
3153#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003154 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3155 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003156#ifndef _LIBCPP_NO_EXCEPTIONS
3157 }
3158 catch (...)
3159 {
3160 for (; __s != __r; ++__s)
3161 __s->~value_type();
3162 throw;
3163 }
3164#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 return __r;
3166}
3167
3168template <class _ForwardIterator, class _Tp>
3169void
3170uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3171{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003173#ifndef _LIBCPP_NO_EXCEPTIONS
3174 _ForwardIterator __s = __f;
3175 try
3176 {
3177#endif
3178 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003179 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003180#ifndef _LIBCPP_NO_EXCEPTIONS
3181 }
3182 catch (...)
3183 {
3184 for (; __s != __f; ++__s)
3185 __s->~value_type();
3186 throw;
3187 }
3188#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189}
3190
3191template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003192_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3194{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003196#ifndef _LIBCPP_NO_EXCEPTIONS
3197 _ForwardIterator __s = __f;
3198 try
3199 {
3200#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003201 for (; __n > 0; ++__f, (void) --__n)
3202 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003203#ifndef _LIBCPP_NO_EXCEPTIONS
3204 }
3205 catch (...)
3206 {
3207 for (; __s != __f; ++__s)
3208 __s->~value_type();
3209 throw;
3210 }
3211#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003212 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213}
3214
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003215#if _LIBCPP_STD_VER > 14
3216
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003217template <class _Tp>
3218inline _LIBCPP_INLINE_VISIBILITY
3219void destroy_at(_Tp* __loc) {
3220 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3221 __loc->~_Tp();
3222}
3223
3224template <class _ForwardIterator>
3225inline _LIBCPP_INLINE_VISIBILITY
3226void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3227 for (; __first != __last; ++__first)
3228 _VSTD::destroy_at(_VSTD::addressof(*__first));
3229}
3230
3231template <class _ForwardIterator, class _Size>
3232inline _LIBCPP_INLINE_VISIBILITY
3233_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3234 for (; __n > 0; (void)++__first, --__n)
3235 _VSTD::destroy_at(_VSTD::addressof(*__first));
3236 return __first;
3237}
3238
Eric Fiselier290c07c2016-10-11 21:13:44 +00003239template <class _ForwardIterator>
3240inline _LIBCPP_INLINE_VISIBILITY
3241void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3242 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3243 auto __idx = __first;
3244#ifndef _LIBCPP_NO_EXCEPTIONS
3245 try {
3246#endif
3247 for (; __idx != __last; ++__idx)
3248 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3249#ifndef _LIBCPP_NO_EXCEPTIONS
3250 } catch (...) {
3251 _VSTD::destroy(__first, __idx);
3252 throw;
3253 }
3254#endif
3255}
3256
3257template <class _ForwardIterator, class _Size>
3258inline _LIBCPP_INLINE_VISIBILITY
3259_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3260 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3261 auto __idx = __first;
3262#ifndef _LIBCPP_NO_EXCEPTIONS
3263 try {
3264#endif
3265 for (; __n > 0; (void)++__idx, --__n)
3266 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3267 return __idx;
3268#ifndef _LIBCPP_NO_EXCEPTIONS
3269 } catch (...) {
3270 _VSTD::destroy(__first, __idx);
3271 throw;
3272 }
3273#endif
3274}
3275
3276
3277template <class _ForwardIterator>
3278inline _LIBCPP_INLINE_VISIBILITY
3279void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3280 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3281 auto __idx = __first;
3282#ifndef _LIBCPP_NO_EXCEPTIONS
3283 try {
3284#endif
3285 for (; __idx != __last; ++__idx)
3286 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3287#ifndef _LIBCPP_NO_EXCEPTIONS
3288 } catch (...) {
3289 _VSTD::destroy(__first, __idx);
3290 throw;
3291 }
3292#endif
3293}
3294
3295template <class _ForwardIterator, class _Size>
3296inline _LIBCPP_INLINE_VISIBILITY
3297_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3298 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3299 auto __idx = __first;
3300#ifndef _LIBCPP_NO_EXCEPTIONS
3301 try {
3302#endif
3303 for (; __n > 0; (void)++__idx, --__n)
3304 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3305 return __idx;
3306#ifndef _LIBCPP_NO_EXCEPTIONS
3307 } catch (...) {
3308 _VSTD::destroy(__first, __idx);
3309 throw;
3310 }
3311#endif
3312}
3313
3314
3315template <class _InputIt, class _ForwardIt>
3316inline _LIBCPP_INLINE_VISIBILITY
3317_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3318 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3319 auto __idx = __first_res;
3320#ifndef _LIBCPP_NO_EXCEPTIONS
3321 try {
3322#endif
3323 for (; __first != __last; (void)++__idx, ++__first)
3324 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3325 return __idx;
3326#ifndef _LIBCPP_NO_EXCEPTIONS
3327 } catch (...) {
3328 _VSTD::destroy(__first_res, __idx);
3329 throw;
3330 }
3331#endif
3332}
3333
3334template <class _InputIt, class _Size, class _ForwardIt>
3335inline _LIBCPP_INLINE_VISIBILITY
3336pair<_InputIt, _ForwardIt>
3337uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3338 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3339 auto __idx = __first_res;
3340#ifndef _LIBCPP_NO_EXCEPTIONS
3341 try {
3342#endif
3343 for (; __n > 0; ++__idx, (void)++__first, --__n)
3344 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3345 return {__first, __idx};
3346#ifndef _LIBCPP_NO_EXCEPTIONS
3347 } catch (...) {
3348 _VSTD::destroy(__first_res, __idx);
3349 throw;
3350 }
3351#endif
3352}
3353
3354
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003355#endif // _LIBCPP_STD_VER > 14
3356
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003357// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3358// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003359// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003360#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3361 && defined(__ATOMIC_RELAXED) \
3362 && defined(__ATOMIC_ACQ_REL)
3363# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3364#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3365# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3366#endif
3367
3368template <class _Tp>
3369inline _LIBCPP_INLINE_VISIBILITY _Tp
3370__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3371{
3372#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3373 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3374#else
3375 return __t += 1;
3376#endif
3377}
3378
3379template <class _Tp>
3380inline _LIBCPP_INLINE_VISIBILITY _Tp
3381__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3382{
3383#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3384 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3385#else
3386 return __t -= 1;
3387#endif
3388}
3389
Howard Hinnant756c69b2010-09-22 16:48:34 +00003390class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391 : public std::exception
3392{
3393public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003394 virtual ~bad_weak_ptr() _NOEXCEPT;
3395 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396};
3397
Marshall Clow8fea1612016-08-25 15:09:01 +00003398_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3399void __throw_bad_weak_ptr()
3400{
3401#ifndef _LIBCPP_NO_EXCEPTIONS
3402 throw bad_weak_ptr();
3403#else
3404 _VSTD::abort();
3405#endif
3406}
3407
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003408template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003410class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411{
3412 __shared_count(const __shared_count&);
3413 __shared_count& operator=(const __shared_count&);
3414
3415protected:
3416 long __shared_owners_;
3417 virtual ~__shared_count();
3418private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003419 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420
3421public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003423 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003424 : __shared_owners_(__refs) {}
3425
Eric Fiselier98848572017-01-17 03:16:26 +00003426#if defined(_LIBCPP_BUILDING_MEMORY) && \
3427 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003428 void __add_shared() _NOEXCEPT;
3429 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003430#else
3431 _LIBCPP_INLINE_VISIBILITY
3432 void __add_shared() _NOEXCEPT {
3433 __libcpp_atomic_refcount_increment(__shared_owners_);
3434 }
3435 _LIBCPP_INLINE_VISIBILITY
3436 bool __release_shared() _NOEXCEPT {
3437 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3438 __on_zero_shared();
3439 return true;
3440 }
3441 return false;
3442 }
3443#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003444 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003445 long use_count() const _NOEXCEPT {
3446 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3447 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448};
3449
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003450class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003451 : private __shared_count
3452{
3453 long __shared_weak_owners_;
3454
3455public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003457 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458 : __shared_count(__refs),
3459 __shared_weak_owners_(__refs) {}
3460protected:
3461 virtual ~__shared_weak_count();
3462
3463public:
Eric Fiselier98848572017-01-17 03:16:26 +00003464#if defined(_LIBCPP_BUILDING_MEMORY) && \
3465 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003466 void __add_shared() _NOEXCEPT;
3467 void __add_weak() _NOEXCEPT;
3468 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003469#else
3470 _LIBCPP_INLINE_VISIBILITY
3471 void __add_shared() _NOEXCEPT {
3472 __shared_count::__add_shared();
3473 }
3474 _LIBCPP_INLINE_VISIBILITY
3475 void __add_weak() _NOEXCEPT {
3476 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3477 }
3478 _LIBCPP_INLINE_VISIBILITY
3479 void __release_shared() _NOEXCEPT {
3480 if (__shared_count::__release_shared())
3481 __release_weak();
3482 }
3483#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003484 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003486 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3487 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488
Howard Hinnant807d6332013-02-25 15:50:36 +00003489 // Define the function out only if we build static libc++ without RTTI.
3490 // Otherwise we may break clients who need to compile their projects with
3491 // -fno-rtti and yet link against a libc++.dylib compiled
3492 // without -fno-rtti.
3493#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003494 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003495#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003497 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498};
3499
3500template <class _Tp, class _Dp, class _Alloc>
3501class __shared_ptr_pointer
3502 : public __shared_weak_count
3503{
3504 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3505public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003508 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509
Howard Hinnant72f73582010-08-11 17:04:31 +00003510#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003511 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003512#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513
3514private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003515 virtual void __on_zero_shared() _NOEXCEPT;
3516 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517};
3518
Howard Hinnant72f73582010-08-11 17:04:31 +00003519#ifndef _LIBCPP_NO_RTTI
3520
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521template <class _Tp, class _Dp, class _Alloc>
3522const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003523__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003525 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526}
3527
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003528#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003529
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530template <class _Tp, class _Dp, class _Alloc>
3531void
Howard Hinnant719bda32011-05-28 14:41:13 +00003532__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533{
3534 __data_.first().second()(__data_.first().first());
3535 __data_.first().second().~_Dp();
3536}
3537
3538template <class _Tp, class _Dp, class _Alloc>
3539void
Howard Hinnant719bda32011-05-28 14:41:13 +00003540__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003542 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3543 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003544 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3545
Eric Fiselierf8898c82015-02-05 23:01:40 +00003546 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003548 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549}
3550
3551template <class _Tp, class _Alloc>
3552class __shared_ptr_emplace
3553 : public __shared_weak_count
3554{
3555 __compressed_pair<_Alloc, _Tp> __data_;
3556public:
3557#ifndef _LIBCPP_HAS_NO_VARIADICS
3558
Howard Hinnant756c69b2010-09-22 16:48:34 +00003559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003561 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562
3563 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003566 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3567 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568
3569#else // _LIBCPP_HAS_NO_VARIADICS
3570
Howard Hinnant756c69b2010-09-22 16:48:34 +00003571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572 __shared_ptr_emplace(_Alloc __a)
3573 : __data_(__a) {}
3574
3575 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3578 : __data_(__a, _Tp(__a0)) {}
3579
3580 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3583 : __data_(__a, _Tp(__a0, __a1)) {}
3584
3585 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3588 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3589
3590#endif // _LIBCPP_HAS_NO_VARIADICS
3591
3592private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003593 virtual void __on_zero_shared() _NOEXCEPT;
3594 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003597 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598};
3599
3600template <class _Tp, class _Alloc>
3601void
Howard Hinnant719bda32011-05-28 14:41:13 +00003602__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603{
3604 __data_.second().~_Tp();
3605}
3606
3607template <class _Tp, class _Alloc>
3608void
Howard Hinnant719bda32011-05-28 14:41:13 +00003609__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003611 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3612 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003613 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003614 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003616 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617}
3618
Erik Pilkington2a398762017-05-25 15:43:31 +00003619struct __shared_ptr_dummy_rebind_allocator_type;
3620template <>
3621class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3622{
3623public:
3624 template <class _Other>
3625 struct rebind
3626 {
3627 typedef allocator<_Other> other;
3628 };
3629};
3630
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003631template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632
3633template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003634class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003636public:
3637 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003638
Eric Fiselierae5b6672016-06-27 01:02:43 +00003639#if _LIBCPP_STD_VER > 14
3640 typedef weak_ptr<_Tp> weak_type;
3641#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642private:
3643 element_type* __ptr_;
3644 __shared_weak_count* __cntrl_;
3645
3646 struct __nat {int __for_bool_;};
3647public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003649 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003651 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003652 template<class _Yp>
3653 explicit shared_ptr(_Yp* __p,
3654 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3655 template<class _Yp, class _Dp>
3656 shared_ptr(_Yp* __p, _Dp __d,
3657 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3658 template<class _Yp, class _Dp, class _Alloc>
3659 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3660 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3662 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003663 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003665 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003669 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003670 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003671#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003673 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003674 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003675 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003676 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003677#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003679 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003680#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003681#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003682 template<class _Yp>
3683 shared_ptr(auto_ptr<_Yp>&& __r,
3684 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003685#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003686 template<class _Yp>
3687 shared_ptr(auto_ptr<_Yp> __r,
3688 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003690#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003691#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003692 template <class _Yp, class _Dp>
3693 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3694 typename enable_if
3695 <
3696 !is_lvalue_reference<_Dp>::value &&
3697 !is_array<_Yp>::value &&
3698 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3699 __nat
3700 >::type = __nat());
3701 template <class _Yp, class _Dp>
3702 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3703 typename enable_if
3704 <
3705 is_lvalue_reference<_Dp>::value &&
3706 !is_array<_Yp>::value &&
3707 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3708 __nat
3709 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003710#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003711 template <class _Yp, class _Dp>
3712 shared_ptr(unique_ptr<_Yp, _Dp>,
3713 typename enable_if
3714 <
3715 !is_lvalue_reference<_Dp>::value &&
3716 !is_array<_Yp>::value &&
3717 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3718 __nat
3719 >::type = __nat());
3720 template <class _Yp, class _Dp>
3721 shared_ptr(unique_ptr<_Yp, _Dp>,
3722 typename enable_if
3723 <
3724 is_lvalue_reference<_Dp>::value &&
3725 !is_array<_Yp>::value &&
3726 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3727 __nat
3728 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003729#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730
3731 ~shared_ptr();
3732
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003734 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003735 template<class _Yp>
3736 typename enable_if
3737 <
3738 is_convertible<_Yp*, element_type*>::value,
3739 shared_ptr&
3740 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003742 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003743#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003745 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003746 template<class _Yp>
3747 typename enable_if
3748 <
3749 is_convertible<_Yp*, element_type*>::value,
3750 shared_ptr<_Tp>&
3751 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003753 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003754#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003755 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003757 typename enable_if
3758 <
3759 !is_array<_Yp>::value &&
3760 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003761 shared_ptr
3762 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003763 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003764#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003765#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003766#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003767 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003769 typename enable_if
3770 <
3771 !is_array<_Yp>::value &&
3772 is_convertible<_Yp*, element_type*>::value,
3773 shared_ptr&
3774 >::type
3775 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003777#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003778 template <class _Yp, class _Dp>
3779 typename enable_if
3780 <
3781 !is_array<_Yp>::value &&
3782 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3783 shared_ptr&
3784 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003785#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003787 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003788#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003790 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791#endif
3792
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003794 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003796 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003797 template<class _Yp>
3798 typename enable_if
3799 <
3800 is_convertible<_Yp*, element_type*>::value,
3801 void
3802 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003804 reset(_Yp* __p);
3805 template<class _Yp, class _Dp>
3806 typename enable_if
3807 <
3808 is_convertible<_Yp*, element_type*>::value,
3809 void
3810 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003812 reset(_Yp* __p, _Dp __d);
3813 template<class _Yp, class _Dp, class _Alloc>
3814 typename enable_if
3815 <
3816 is_convertible<_Yp*, element_type*>::value,
3817 void
3818 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003820 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821
Howard Hinnant756c69b2010-09-22 16:48:34 +00003822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003823 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003825 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3826 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003828 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003830 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003832 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003834 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003835 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003836 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003837 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003839 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003840 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003841 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003843 _LIBCPP_INLINE_VISIBILITY
3844 bool
3845 __owner_equivalent(const shared_ptr& __p) const
3846 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847
Howard Hinnant72f73582010-08-11 17:04:31 +00003848#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003851 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003852 {return static_cast<_Dp*>(__cntrl_
3853 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
3854 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003855#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856
3857#ifndef _LIBCPP_HAS_NO_VARIADICS
3858
3859 template<class ..._Args>
3860 static
3861 shared_ptr<_Tp>
3862 make_shared(_Args&& ...__args);
3863
3864 template<class _Alloc, class ..._Args>
3865 static
3866 shared_ptr<_Tp>
3867 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3868
3869#else // _LIBCPP_HAS_NO_VARIADICS
3870
3871 static shared_ptr<_Tp> make_shared();
3872
3873 template<class _A0>
3874 static shared_ptr<_Tp> make_shared(_A0&);
3875
3876 template<class _A0, class _A1>
3877 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3878
3879 template<class _A0, class _A1, class _A2>
3880 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3881
3882 template<class _Alloc>
3883 static shared_ptr<_Tp>
3884 allocate_shared(const _Alloc& __a);
3885
3886 template<class _Alloc, class _A0>
3887 static shared_ptr<_Tp>
3888 allocate_shared(const _Alloc& __a, _A0& __a0);
3889
3890 template<class _Alloc, class _A0, class _A1>
3891 static shared_ptr<_Tp>
3892 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3893
3894 template<class _Alloc, class _A0, class _A1, class _A2>
3895 static shared_ptr<_Tp>
3896 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3897
3898#endif // _LIBCPP_HAS_NO_VARIADICS
3899
3900private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003901 template <class _Yp, bool = is_function<_Yp>::value>
3902 struct __shared_ptr_default_allocator
3903 {
3904 typedef allocator<_Yp> type;
3905 };
3906
3907 template <class _Yp>
3908 struct __shared_ptr_default_allocator<_Yp, true>
3909 {
3910 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3911 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003913 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003914 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003915 typename enable_if<is_convertible<_OrigPtr*,
3916 const enable_shared_from_this<_Yp>*
3917 >::value,
3918 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003919 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3920 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003922 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003923 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003924 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003925 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3926 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003927 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928 }
3929
Erik Pilkington2a398762017-05-25 15:43:31 +00003930 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003931
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003932 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3933 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003934};
3935
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003936
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003938inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003939_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003940shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941 : __ptr_(0),
3942 __cntrl_(0)
3943{
3944}
3945
3946template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003947inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003948_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003949shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950 : __ptr_(0),
3951 __cntrl_(0)
3952{
3953}
3954
3955template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003956template<class _Yp>
3957shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3958 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959 : __ptr_(__p)
3960{
3961 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003962 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3963 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3964 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003966 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967}
3968
3969template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003970template<class _Yp, class _Dp>
3971shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3972 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973 : __ptr_(__p)
3974{
3975#ifndef _LIBCPP_NO_EXCEPTIONS
3976 try
3977 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003978#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003979 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3980 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3981 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003982 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983#ifndef _LIBCPP_NO_EXCEPTIONS
3984 }
3985 catch (...)
3986 {
3987 __d(__p);
3988 throw;
3989 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003990#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991}
3992
3993template<class _Tp>
3994template<class _Dp>
3995shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3996 : __ptr_(0)
3997{
3998#ifndef _LIBCPP_NO_EXCEPTIONS
3999 try
4000 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004001#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004002 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4003 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4004 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005#ifndef _LIBCPP_NO_EXCEPTIONS
4006 }
4007 catch (...)
4008 {
4009 __d(__p);
4010 throw;
4011 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004012#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013}
4014
4015template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004016template<class _Yp, class _Dp, class _Alloc>
4017shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4018 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019 : __ptr_(__p)
4020{
4021#ifndef _LIBCPP_NO_EXCEPTIONS
4022 try
4023 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004024#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004026 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027 typedef __allocator_destructor<_A2> _D2;
4028 _A2 __a2(__a);
4029 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004030 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4031 _CntrlBlk(__p, __d, __a);
4032 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004033 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034#ifndef _LIBCPP_NO_EXCEPTIONS
4035 }
4036 catch (...)
4037 {
4038 __d(__p);
4039 throw;
4040 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004041#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042}
4043
4044template<class _Tp>
4045template<class _Dp, class _Alloc>
4046shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4047 : __ptr_(0)
4048{
4049#ifndef _LIBCPP_NO_EXCEPTIONS
4050 try
4051 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004052#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004054 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055 typedef __allocator_destructor<_A2> _D2;
4056 _A2 __a2(__a);
4057 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004058 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4059 _CntrlBlk(__p, __d, __a);
4060 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061#ifndef _LIBCPP_NO_EXCEPTIONS
4062 }
4063 catch (...)
4064 {
4065 __d(__p);
4066 throw;
4067 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004068#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069}
4070
4071template<class _Tp>
4072template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004073inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004074shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075 : __ptr_(__p),
4076 __cntrl_(__r.__cntrl_)
4077{
4078 if (__cntrl_)
4079 __cntrl_->__add_shared();
4080}
4081
4082template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004083inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004084shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085 : __ptr_(__r.__ptr_),
4086 __cntrl_(__r.__cntrl_)
4087{
4088 if (__cntrl_)
4089 __cntrl_->__add_shared();
4090}
4091
4092template<class _Tp>
4093template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004094inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004096 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004097 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098 : __ptr_(__r.__ptr_),
4099 __cntrl_(__r.__cntrl_)
4100{
4101 if (__cntrl_)
4102 __cntrl_->__add_shared();
4103}
4104
Howard Hinnant74279a52010-09-04 23:28:19 +00004105#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106
4107template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004108inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004109shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110 : __ptr_(__r.__ptr_),
4111 __cntrl_(__r.__cntrl_)
4112{
4113 __r.__ptr_ = 0;
4114 __r.__cntrl_ = 0;
4115}
4116
4117template<class _Tp>
4118template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004119inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004121 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004122 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123 : __ptr_(__r.__ptr_),
4124 __cntrl_(__r.__cntrl_)
4125{
4126 __r.__ptr_ = 0;
4127 __r.__cntrl_ = 0;
4128}
4129
Howard Hinnant74279a52010-09-04 23:28:19 +00004130#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131
Marshall Clowb22274f2017-01-24 22:22:33 +00004132#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004134template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004135#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004136shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004138shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004140 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141 : __ptr_(__r.get())
4142{
4143 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4144 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004145 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146 __r.release();
4147}
Marshall Clowb22274f2017-01-24 22:22:33 +00004148#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149
4150template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004151template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004152#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4154#else
4155shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4156#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004157 typename enable_if
4158 <
4159 !is_lvalue_reference<_Dp>::value &&
4160 !is_array<_Yp>::value &&
4161 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4162 __nat
4163 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004164 : __ptr_(__r.get())
4165{
Marshall Clow35cde742015-05-10 13:59:45 +00004166#if _LIBCPP_STD_VER > 11
4167 if (__ptr_ == nullptr)
4168 __cntrl_ = nullptr;
4169 else
4170#endif
4171 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004172 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4173 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4174 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004175 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004176 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177 __r.release();
4178}
4179
4180template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004181template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004182#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4184#else
4185shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4186#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004187 typename enable_if
4188 <
4189 is_lvalue_reference<_Dp>::value &&
4190 !is_array<_Yp>::value &&
4191 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4192 __nat
4193 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194 : __ptr_(__r.get())
4195{
Marshall Clow35cde742015-05-10 13:59:45 +00004196#if _LIBCPP_STD_VER > 11
4197 if (__ptr_ == nullptr)
4198 __cntrl_ = nullptr;
4199 else
4200#endif
4201 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004202 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004203 typedef __shared_ptr_pointer<_Yp*,
4204 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004205 _AllocT > _CntrlBlk;
4206 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004207 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004208 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209 __r.release();
4210}
4211
4212#ifndef _LIBCPP_HAS_NO_VARIADICS
4213
4214template<class _Tp>
4215template<class ..._Args>
4216shared_ptr<_Tp>
4217shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4218{
4219 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4220 typedef allocator<_CntrlBlk> _A2;
4221 typedef __allocator_destructor<_A2> _D2;
4222 _A2 __a2;
4223 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004224 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225 shared_ptr<_Tp> __r;
4226 __r.__ptr_ = __hold2.get()->get();
4227 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004228 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229 return __r;
4230}
4231
4232template<class _Tp>
4233template<class _Alloc, class ..._Args>
4234shared_ptr<_Tp>
4235shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4236{
4237 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004238 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239 typedef __allocator_destructor<_A2> _D2;
4240 _A2 __a2(__a);
4241 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004242 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4243 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244 shared_ptr<_Tp> __r;
4245 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004246 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004247 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248 return __r;
4249}
4250
4251#else // _LIBCPP_HAS_NO_VARIADICS
4252
4253template<class _Tp>
4254shared_ptr<_Tp>
4255shared_ptr<_Tp>::make_shared()
4256{
4257 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4258 typedef allocator<_CntrlBlk> _Alloc2;
4259 typedef __allocator_destructor<_Alloc2> _D2;
4260 _Alloc2 __alloc2;
4261 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4262 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4263 shared_ptr<_Tp> __r;
4264 __r.__ptr_ = __hold2.get()->get();
4265 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004266 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267 return __r;
4268}
4269
4270template<class _Tp>
4271template<class _A0>
4272shared_ptr<_Tp>
4273shared_ptr<_Tp>::make_shared(_A0& __a0)
4274{
4275 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4276 typedef allocator<_CntrlBlk> _Alloc2;
4277 typedef __allocator_destructor<_Alloc2> _D2;
4278 _Alloc2 __alloc2;
4279 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4280 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4281 shared_ptr<_Tp> __r;
4282 __r.__ptr_ = __hold2.get()->get();
4283 __r.__cntrl_ = __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
4288template<class _Tp>
4289template<class _A0, class _A1>
4290shared_ptr<_Tp>
4291shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4292{
4293 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4294 typedef allocator<_CntrlBlk> _Alloc2;
4295 typedef __allocator_destructor<_Alloc2> _D2;
4296 _Alloc2 __alloc2;
4297 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4298 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4299 shared_ptr<_Tp> __r;
4300 __r.__ptr_ = __hold2.get()->get();
4301 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004302 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004303 return __r;
4304}
4305
4306template<class _Tp>
4307template<class _A0, class _A1, class _A2>
4308shared_ptr<_Tp>
4309shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4310{
4311 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4312 typedef allocator<_CntrlBlk> _Alloc2;
4313 typedef __allocator_destructor<_Alloc2> _D2;
4314 _Alloc2 __alloc2;
4315 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4316 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4317 shared_ptr<_Tp> __r;
4318 __r.__ptr_ = __hold2.get()->get();
4319 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004320 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321 return __r;
4322}
4323
4324template<class _Tp>
4325template<class _Alloc>
4326shared_ptr<_Tp>
4327shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4328{
4329 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004330 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331 typedef __allocator_destructor<_Alloc2> _D2;
4332 _Alloc2 __alloc2(__a);
4333 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004334 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4335 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004336 shared_ptr<_Tp> __r;
4337 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004338 __r.__cntrl_ = _VSTD::addressof(*__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 _Alloc, class _A0>
4345shared_ptr<_Tp>
4346shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4347{
4348 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004349 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350 typedef __allocator_destructor<_Alloc2> _D2;
4351 _Alloc2 __alloc2(__a);
4352 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004353 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4354 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355 shared_ptr<_Tp> __r;
4356 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004357 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004358 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359 return __r;
4360}
4361
4362template<class _Tp>
4363template<class _Alloc, class _A0, class _A1>
4364shared_ptr<_Tp>
4365shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4366{
4367 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004368 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369 typedef __allocator_destructor<_Alloc2> _D2;
4370 _Alloc2 __alloc2(__a);
4371 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004372 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4373 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004374 shared_ptr<_Tp> __r;
4375 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004376 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004377 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378 return __r;
4379}
4380
4381template<class _Tp>
4382template<class _Alloc, class _A0, class _A1, class _A2>
4383shared_ptr<_Tp>
4384shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4385{
4386 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004387 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388 typedef __allocator_destructor<_Alloc2> _D2;
4389 _Alloc2 __alloc2(__a);
4390 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004391 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4392 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004393 shared_ptr<_Tp> __r;
4394 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004395 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004396 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004397 return __r;
4398}
4399
4400#endif // _LIBCPP_HAS_NO_VARIADICS
4401
4402template<class _Tp>
4403shared_ptr<_Tp>::~shared_ptr()
4404{
4405 if (__cntrl_)
4406 __cntrl_->__release_shared();
4407}
4408
4409template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004410inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004411shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004412shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413{
4414 shared_ptr(__r).swap(*this);
4415 return *this;
4416}
4417
4418template<class _Tp>
4419template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004420inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004421typename enable_if
4422<
Marshall Clow7e384b72017-01-10 16:59:33 +00004423 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004424 shared_ptr<_Tp>&
4425>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004426shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004427{
4428 shared_ptr(__r).swap(*this);
4429 return *this;
4430}
4431
Howard Hinnant74279a52010-09-04 23:28:19 +00004432#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004433
4434template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004435inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004437shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004439 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440 return *this;
4441}
4442
4443template<class _Tp>
4444template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004445inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004446typename enable_if
4447<
Marshall Clow7e384b72017-01-10 16:59:33 +00004448 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004449 shared_ptr<_Tp>&
4450>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004451shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4452{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004453 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004454 return *this;
4455}
4456
Marshall Clowb22274f2017-01-24 22:22:33 +00004457#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004458template<class _Tp>
4459template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004460inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004461typename enable_if
4462<
4463 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004464 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004465 shared_ptr<_Tp>
4466>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4468{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004469 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470 return *this;
4471}
Marshall Clowb22274f2017-01-24 22:22:33 +00004472#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473
4474template<class _Tp>
4475template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004476inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004477typename enable_if
4478<
4479 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004480 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4481 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004482 shared_ptr<_Tp>&
4483>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4485{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004486 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004487 return *this;
4488}
4489
Howard Hinnant74279a52010-09-04 23:28:19 +00004490#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004491
Marshall Clowb22274f2017-01-24 22:22:33 +00004492#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004493template<class _Tp>
4494template<class _Yp>
4495inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004496typename enable_if
4497<
4498 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004499 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004500 shared_ptr<_Tp>&
4501>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004502shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503{
4504 shared_ptr(__r).swap(*this);
4505 return *this;
4506}
Marshall Clowb22274f2017-01-24 22:22:33 +00004507#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004508
4509template<class _Tp>
4510template <class _Yp, class _Dp>
4511inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004512typename enable_if
4513<
4514 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004515 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4516 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004517 shared_ptr<_Tp>&
4518>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4520{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004521 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004522 return *this;
4523}
4524
Howard Hinnant74279a52010-09-04 23:28:19 +00004525#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004526
4527template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004528inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004529void
Howard Hinnant719bda32011-05-28 14:41:13 +00004530shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004531{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004532 _VSTD::swap(__ptr_, __r.__ptr_);
4533 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004534}
4535
4536template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004537inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538void
Howard Hinnant719bda32011-05-28 14:41:13 +00004539shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004540{
4541 shared_ptr().swap(*this);
4542}
4543
4544template<class _Tp>
4545template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004546inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004547typename enable_if
4548<
Marshall Clow7e384b72017-01-10 16:59:33 +00004549 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004550 void
4551>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004552shared_ptr<_Tp>::reset(_Yp* __p)
4553{
4554 shared_ptr(__p).swap(*this);
4555}
4556
4557template<class _Tp>
4558template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004559inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004560typename enable_if
4561<
Marshall Clow7e384b72017-01-10 16:59:33 +00004562 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004563 void
4564>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4566{
4567 shared_ptr(__p, __d).swap(*this);
4568}
4569
4570template<class _Tp>
4571template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004572inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004573typename enable_if
4574<
Marshall Clow7e384b72017-01-10 16:59:33 +00004575 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004576 void
4577>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4579{
4580 shared_ptr(__p, __d, __a).swap(*this);
4581}
4582
4583#ifndef _LIBCPP_HAS_NO_VARIADICS
4584
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004585template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004587typename enable_if
4588<
4589 !is_array<_Tp>::value,
4590 shared_ptr<_Tp>
4591>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592make_shared(_Args&& ...__args)
4593{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004594 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004595}
4596
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004597template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004598inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004599typename enable_if
4600<
4601 !is_array<_Tp>::value,
4602 shared_ptr<_Tp>
4603>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004604allocate_shared(const _Alloc& __a, _Args&& ...__args)
4605{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004606 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004607}
4608
4609#else // _LIBCPP_HAS_NO_VARIADICS
4610
4611template<class _Tp>
4612inline _LIBCPP_INLINE_VISIBILITY
4613shared_ptr<_Tp>
4614make_shared()
4615{
4616 return shared_ptr<_Tp>::make_shared();
4617}
4618
4619template<class _Tp, class _A0>
4620inline _LIBCPP_INLINE_VISIBILITY
4621shared_ptr<_Tp>
4622make_shared(_A0& __a0)
4623{
4624 return shared_ptr<_Tp>::make_shared(__a0);
4625}
4626
4627template<class _Tp, class _A0, class _A1>
4628inline _LIBCPP_INLINE_VISIBILITY
4629shared_ptr<_Tp>
4630make_shared(_A0& __a0, _A1& __a1)
4631{
4632 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4633}
4634
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004635template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004636inline _LIBCPP_INLINE_VISIBILITY
4637shared_ptr<_Tp>
4638make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4639{
4640 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4641}
4642
4643template<class _Tp, class _Alloc>
4644inline _LIBCPP_INLINE_VISIBILITY
4645shared_ptr<_Tp>
4646allocate_shared(const _Alloc& __a)
4647{
4648 return shared_ptr<_Tp>::allocate_shared(__a);
4649}
4650
4651template<class _Tp, class _Alloc, class _A0>
4652inline _LIBCPP_INLINE_VISIBILITY
4653shared_ptr<_Tp>
4654allocate_shared(const _Alloc& __a, _A0& __a0)
4655{
4656 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4657}
4658
4659template<class _Tp, class _Alloc, class _A0, class _A1>
4660inline _LIBCPP_INLINE_VISIBILITY
4661shared_ptr<_Tp>
4662allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4663{
4664 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4665}
4666
4667template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4668inline _LIBCPP_INLINE_VISIBILITY
4669shared_ptr<_Tp>
4670allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4671{
4672 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4673}
4674
4675#endif // _LIBCPP_HAS_NO_VARIADICS
4676
4677template<class _Tp, class _Up>
4678inline _LIBCPP_INLINE_VISIBILITY
4679bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004680operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004681{
4682 return __x.get() == __y.get();
4683}
4684
4685template<class _Tp, class _Up>
4686inline _LIBCPP_INLINE_VISIBILITY
4687bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004688operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004689{
4690 return !(__x == __y);
4691}
4692
4693template<class _Tp, class _Up>
4694inline _LIBCPP_INLINE_VISIBILITY
4695bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004696operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004697{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004698 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4699 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004700}
4701
4702template<class _Tp, class _Up>
4703inline _LIBCPP_INLINE_VISIBILITY
4704bool
4705operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4706{
4707 return __y < __x;
4708}
4709
4710template<class _Tp, class _Up>
4711inline _LIBCPP_INLINE_VISIBILITY
4712bool
4713operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4714{
4715 return !(__y < __x);
4716}
4717
4718template<class _Tp, class _Up>
4719inline _LIBCPP_INLINE_VISIBILITY
4720bool
4721operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4722{
4723 return !(__x < __y);
4724}
4725
4726template<class _Tp>
4727inline _LIBCPP_INLINE_VISIBILITY
4728bool
4729operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4730{
4731 return !__x;
4732}
4733
4734template<class _Tp>
4735inline _LIBCPP_INLINE_VISIBILITY
4736bool
4737operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4738{
4739 return !__x;
4740}
4741
4742template<class _Tp>
4743inline _LIBCPP_INLINE_VISIBILITY
4744bool
4745operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4746{
4747 return static_cast<bool>(__x);
4748}
4749
4750template<class _Tp>
4751inline _LIBCPP_INLINE_VISIBILITY
4752bool
4753operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4754{
4755 return static_cast<bool>(__x);
4756}
4757
4758template<class _Tp>
4759inline _LIBCPP_INLINE_VISIBILITY
4760bool
4761operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4762{
4763 return less<_Tp*>()(__x.get(), nullptr);
4764}
4765
4766template<class _Tp>
4767inline _LIBCPP_INLINE_VISIBILITY
4768bool
4769operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4770{
4771 return less<_Tp*>()(nullptr, __x.get());
4772}
4773
4774template<class _Tp>
4775inline _LIBCPP_INLINE_VISIBILITY
4776bool
4777operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4778{
4779 return nullptr < __x;
4780}
4781
4782template<class _Tp>
4783inline _LIBCPP_INLINE_VISIBILITY
4784bool
4785operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4786{
4787 return __x < nullptr;
4788}
4789
4790template<class _Tp>
4791inline _LIBCPP_INLINE_VISIBILITY
4792bool
4793operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4794{
4795 return !(nullptr < __x);
4796}
4797
4798template<class _Tp>
4799inline _LIBCPP_INLINE_VISIBILITY
4800bool
4801operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4802{
4803 return !(__x < nullptr);
4804}
4805
4806template<class _Tp>
4807inline _LIBCPP_INLINE_VISIBILITY
4808bool
4809operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4810{
4811 return !(__x < nullptr);
4812}
4813
4814template<class _Tp>
4815inline _LIBCPP_INLINE_VISIBILITY
4816bool
4817operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4818{
4819 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004820}
4821
4822template<class _Tp>
4823inline _LIBCPP_INLINE_VISIBILITY
4824void
Howard Hinnant719bda32011-05-28 14:41:13 +00004825swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004826{
4827 __x.swap(__y);
4828}
4829
4830template<class _Tp, class _Up>
4831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004832typename enable_if
4833<
4834 !is_array<_Tp>::value && !is_array<_Up>::value,
4835 shared_ptr<_Tp>
4836>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004837static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004838{
4839 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4840}
4841
4842template<class _Tp, class _Up>
4843inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004844typename enable_if
4845<
4846 !is_array<_Tp>::value && !is_array<_Up>::value,
4847 shared_ptr<_Tp>
4848>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004849dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004850{
4851 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4852 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4853}
4854
4855template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004856typename enable_if
4857<
4858 is_array<_Tp>::value == is_array<_Up>::value,
4859 shared_ptr<_Tp>
4860>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004861const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004862{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004863 typedef typename remove_extent<_Tp>::type _RTp;
4864 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004865}
4866
Howard Hinnant72f73582010-08-11 17:04:31 +00004867#ifndef _LIBCPP_NO_RTTI
4868
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869template<class _Dp, class _Tp>
4870inline _LIBCPP_INLINE_VISIBILITY
4871_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004872get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004873{
4874 return __p.template __get_deleter<_Dp>();
4875}
4876
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004877#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004878
Howard Hinnantc51e1022010-05-11 19:42:16 +00004879template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004880class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004881{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004882public:
4883 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004884private:
4885 element_type* __ptr_;
4886 __shared_weak_count* __cntrl_;
4887
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004888public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004890 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004891 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004892 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4893 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004895 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004896 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004897 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4898 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004899
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004900#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004902 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004903 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004904 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4905 _NOEXCEPT;
4906#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004907 ~weak_ptr();
4908
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004910 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004911 template<class _Yp>
4912 typename enable_if
4913 <
4914 is_convertible<_Yp*, element_type*>::value,
4915 weak_ptr&
4916 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004918 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4919
4920#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4921
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004923 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4924 template<class _Yp>
4925 typename enable_if
4926 <
4927 is_convertible<_Yp*, element_type*>::value,
4928 weak_ptr&
4929 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004931 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4932
4933#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4934
4935 template<class _Yp>
4936 typename enable_if
4937 <
4938 is_convertible<_Yp*, element_type*>::value,
4939 weak_ptr&
4940 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004942 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004943
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004945 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004947 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004948
Howard Hinnant756c69b2010-09-22 16:48:34 +00004949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004950 long use_count() const _NOEXCEPT
4951 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004953 bool expired() const _NOEXCEPT
4954 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4955 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004956 template<class _Up>
4957 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004958 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004959 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004960 template<class _Up>
4961 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004962 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963 {return __cntrl_ < __r.__cntrl_;}
4964
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004965 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4966 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004967};
4968
4969template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004970inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004971_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004972weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004973 : __ptr_(0),
4974 __cntrl_(0)
4975{
4976}
4977
4978template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004979inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004980weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004981 : __ptr_(__r.__ptr_),
4982 __cntrl_(__r.__cntrl_)
4983{
4984 if (__cntrl_)
4985 __cntrl_->__add_weak();
4986}
4987
4988template<class _Tp>
4989template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004990inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004991weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004992 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004993 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004994 : __ptr_(__r.__ptr_),
4995 __cntrl_(__r.__cntrl_)
4996{
4997 if (__cntrl_)
4998 __cntrl_->__add_weak();
4999}
5000
5001template<class _Tp>
5002template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005003inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005004weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005005 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005006 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005007 : __ptr_(__r.__ptr_),
5008 __cntrl_(__r.__cntrl_)
5009{
5010 if (__cntrl_)
5011 __cntrl_->__add_weak();
5012}
5013
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005014#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5015
5016template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005017inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005018weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5019 : __ptr_(__r.__ptr_),
5020 __cntrl_(__r.__cntrl_)
5021{
5022 __r.__ptr_ = 0;
5023 __r.__cntrl_ = 0;
5024}
5025
5026template<class _Tp>
5027template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005028inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005029weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5030 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5031 _NOEXCEPT
5032 : __ptr_(__r.__ptr_),
5033 __cntrl_(__r.__cntrl_)
5034{
5035 __r.__ptr_ = 0;
5036 __r.__cntrl_ = 0;
5037}
5038
5039#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5040
Howard Hinnantc51e1022010-05-11 19:42:16 +00005041template<class _Tp>
5042weak_ptr<_Tp>::~weak_ptr()
5043{
5044 if (__cntrl_)
5045 __cntrl_->__release_weak();
5046}
5047
5048template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005049inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005050weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005051weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005052{
5053 weak_ptr(__r).swap(*this);
5054 return *this;
5055}
5056
5057template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005058template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005059inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005060typename enable_if
5061<
5062 is_convertible<_Yp*, _Tp*>::value,
5063 weak_ptr<_Tp>&
5064>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005065weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005066{
5067 weak_ptr(__r).swap(*this);
5068 return *this;
5069}
5070
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005071#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5072
5073template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005074inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005075weak_ptr<_Tp>&
5076weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5077{
5078 weak_ptr(_VSTD::move(__r)).swap(*this);
5079 return *this;
5080}
5081
Howard Hinnantc51e1022010-05-11 19:42:16 +00005082template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005083template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005084inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005085typename enable_if
5086<
5087 is_convertible<_Yp*, _Tp*>::value,
5088 weak_ptr<_Tp>&
5089>::type
5090weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5091{
5092 weak_ptr(_VSTD::move(__r)).swap(*this);
5093 return *this;
5094}
5095
5096#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5097
5098template<class _Tp>
5099template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005100inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005101typename enable_if
5102<
5103 is_convertible<_Yp*, _Tp*>::value,
5104 weak_ptr<_Tp>&
5105>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005106weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005107{
5108 weak_ptr(__r).swap(*this);
5109 return *this;
5110}
5111
5112template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005113inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005114void
Howard Hinnant719bda32011-05-28 14:41:13 +00005115weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005116{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005117 _VSTD::swap(__ptr_, __r.__ptr_);
5118 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005119}
5120
5121template<class _Tp>
5122inline _LIBCPP_INLINE_VISIBILITY
5123void
Howard Hinnant719bda32011-05-28 14:41:13 +00005124swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005125{
5126 __x.swap(__y);
5127}
5128
5129template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005130inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005131void
Howard Hinnant719bda32011-05-28 14:41:13 +00005132weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005133{
5134 weak_ptr().swap(*this);
5135}
5136
5137template<class _Tp>
5138template<class _Yp>
5139shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005140 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005141 : __ptr_(__r.__ptr_),
5142 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5143{
5144 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005145 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005146}
5147
5148template<class _Tp>
5149shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005150weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005151{
5152 shared_ptr<_Tp> __r;
5153 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5154 if (__r.__cntrl_)
5155 __r.__ptr_ = __ptr_;
5156 return __r;
5157}
5158
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005159#if _LIBCPP_STD_VER > 14
5160template <class _Tp = void> struct owner_less;
5161#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005162template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005163#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005164
5165template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005166struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005167 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005168{
5169 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005170 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005171 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005172 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005173 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005174 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005175 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005176 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005177 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005178 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005179};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005180
5181template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005182struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005183 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5184{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005185 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005186 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005187 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005188 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005189 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005190 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005191 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005192 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005193 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005194 {return __x.owner_before(__y);}
5195};
5196
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005197#if _LIBCPP_STD_VER > 14
5198template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005199struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005200{
5201 template <class _Tp, class _Up>
5202 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005203 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005204 {return __x.owner_before(__y);}
5205 template <class _Tp, class _Up>
5206 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005207 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005208 {return __x.owner_before(__y);}
5209 template <class _Tp, class _Up>
5210 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005211 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005212 {return __x.owner_before(__y);}
5213 template <class _Tp, class _Up>
5214 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005215 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005216 {return __x.owner_before(__y);}
5217 typedef void is_transparent;
5218};
5219#endif
5220
Howard Hinnantc51e1022010-05-11 19:42:16 +00005221template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005222class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005223{
5224 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005225protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005226 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005227 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005229 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005231 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5232 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005234 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005235public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005237 shared_ptr<_Tp> shared_from_this()
5238 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005240 shared_ptr<_Tp const> shared_from_this() const
5241 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005242
Eric Fiselier84006862016-06-02 00:15:35 +00005243#if _LIBCPP_STD_VER > 14
5244 _LIBCPP_INLINE_VISIBILITY
5245 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5246 { return __weak_this_; }
5247
5248 _LIBCPP_INLINE_VISIBILITY
5249 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5250 { return __weak_this_; }
5251#endif // _LIBCPP_STD_VER > 14
5252
Howard Hinnantc51e1022010-05-11 19:42:16 +00005253 template <class _Up> friend class shared_ptr;
5254};
5255
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005256template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005257struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005258{
5259 typedef shared_ptr<_Tp> argument_type;
5260 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005261
Howard Hinnant756c69b2010-09-22 16:48:34 +00005262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005263 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005264 {
5265 return hash<_Tp*>()(__ptr.get());
5266 }
5267};
5268
Howard Hinnantc834c512011-11-29 18:15:50 +00005269template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005270inline _LIBCPP_INLINE_VISIBILITY
5271basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005272operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005273
Eric Fiselier9b492672016-06-18 02:12:53 +00005274
5275#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005276
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005277class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005278{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005279 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005280public:
5281 void lock() _NOEXCEPT;
5282 void unlock() _NOEXCEPT;
5283
5284private:
5285 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5286 __sp_mut(const __sp_mut&);
5287 __sp_mut& operator=(const __sp_mut&);
5288
Howard Hinnant8331b762013-03-06 23:30:19 +00005289 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005290};
5291
Mehdi Amini228053d2017-05-04 17:08:54 +00005292_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5293__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005294
5295template <class _Tp>
5296inline _LIBCPP_INLINE_VISIBILITY
5297bool
5298atomic_is_lock_free(const shared_ptr<_Tp>*)
5299{
5300 return false;
5301}
5302
5303template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005304_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005305shared_ptr<_Tp>
5306atomic_load(const shared_ptr<_Tp>* __p)
5307{
5308 __sp_mut& __m = __get_sp_mut(__p);
5309 __m.lock();
5310 shared_ptr<_Tp> __q = *__p;
5311 __m.unlock();
5312 return __q;
5313}
5314
5315template <class _Tp>
5316inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005317_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005318shared_ptr<_Tp>
5319atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5320{
5321 return atomic_load(__p);
5322}
5323
5324template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005325_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005326void
5327atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5328{
5329 __sp_mut& __m = __get_sp_mut(__p);
5330 __m.lock();
5331 __p->swap(__r);
5332 __m.unlock();
5333}
5334
5335template <class _Tp>
5336inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005337_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005338void
5339atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5340{
5341 atomic_store(__p, __r);
5342}
5343
5344template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005345_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005346shared_ptr<_Tp>
5347atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5348{
5349 __sp_mut& __m = __get_sp_mut(__p);
5350 __m.lock();
5351 __p->swap(__r);
5352 __m.unlock();
5353 return __r;
5354}
5355
5356template <class _Tp>
5357inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005358_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005359shared_ptr<_Tp>
5360atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5361{
5362 return atomic_exchange(__p, __r);
5363}
5364
5365template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005366_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005367bool
5368atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5369{
Marshall Clow4201ee82016-05-18 17:50:13 +00005370 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005371 __sp_mut& __m = __get_sp_mut(__p);
5372 __m.lock();
5373 if (__p->__owner_equivalent(*__v))
5374 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005375 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005376 *__p = __w;
5377 __m.unlock();
5378 return true;
5379 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005380 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005381 *__v = *__p;
5382 __m.unlock();
5383 return false;
5384}
5385
5386template <class _Tp>
5387inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005388_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005389bool
5390atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5391{
5392 return atomic_compare_exchange_strong(__p, __v, __w);
5393}
5394
5395template <class _Tp>
5396inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005397_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005398bool
5399atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5400 shared_ptr<_Tp> __w, memory_order, memory_order)
5401{
5402 return atomic_compare_exchange_strong(__p, __v, __w);
5403}
5404
5405template <class _Tp>
5406inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005407_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005408bool
5409atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5410 shared_ptr<_Tp> __w, memory_order, memory_order)
5411{
5412 return atomic_compare_exchange_weak(__p, __v, __w);
5413}
5414
Eric Fiselier9b492672016-06-18 02:12:53 +00005415#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005416
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005417//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005418#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5419# ifndef _LIBCPP_CXX03_LANG
5420enum class pointer_safety : unsigned char {
5421 relaxed,
5422 preferred,
5423 strict
5424};
5425# endif
5426#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005427struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005428{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005429 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005430 {
5431 relaxed,
5432 preferred,
5433 strict
5434 };
5435
Howard Hinnant49e145e2012-10-30 19:06:59 +00005436 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005437
Howard Hinnant756c69b2010-09-22 16:48:34 +00005438 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005439 pointer_safety() : __v_() {}
5440
5441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005442 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005444 operator int() const {return __v_;}
5445};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005446#endif
5447
5448#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5449 defined(_LIBCPP_BUILDING_MEMORY)
5450_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5451#else
5452// This function is only offered in C++03 under ABI v1.
5453# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5454inline _LIBCPP_INLINE_VISIBILITY
5455pointer_safety get_pointer_safety() _NOEXCEPT {
5456 return pointer_safety::relaxed;
5457}
5458# endif
5459#endif
5460
Howard Hinnantc51e1022010-05-11 19:42:16 +00005461
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005462_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5463_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5464_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005465_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005466
5467template <class _Tp>
5468inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005469_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005470undeclare_reachable(_Tp* __p)
5471{
5472 return static_cast<_Tp*>(__undeclare_reachable(__p));
5473}
5474
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005475_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005476
Marshall Clow8982dcd2015-07-13 20:04:56 +00005477// --- Helper for container swap --
5478template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005479inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005480void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5481#if _LIBCPP_STD_VER >= 14
5482 _NOEXCEPT
5483#else
5484 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5485#endif
5486{
5487 __swap_allocator(__a1, __a2,
5488 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5489}
5490
5491template <typename _Alloc>
5492_LIBCPP_INLINE_VISIBILITY
5493void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5494#if _LIBCPP_STD_VER >= 14
5495 _NOEXCEPT
5496#else
5497 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5498#endif
5499{
5500 using _VSTD::swap;
5501 swap(__a1, __a2);
5502}
5503
5504template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005505inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005506void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5507
Marshall Clowff91de82015-08-18 19:51:37 +00005508template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005509struct __noexcept_move_assign_container : public integral_constant<bool,
5510 _Traits::propagate_on_container_move_assignment::value
5511#if _LIBCPP_STD_VER > 14
5512 || _Traits::is_always_equal::value
5513#else
5514 && is_nothrow_move_assignable<_Alloc>::value
5515#endif
5516 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005517
Marshall Clowa591b9a2016-07-11 21:38:08 +00005518
5519#ifndef _LIBCPP_HAS_NO_VARIADICS
5520template <class _Tp, class _Alloc>
5521struct __temp_value {
5522 typedef allocator_traits<_Alloc> _Traits;
5523
5524 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5525 _Alloc &__a;
5526
5527 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5528 _Tp & get() { return *__addr(); }
5529
5530 template<class... _Args>
5531 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5532 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5533
5534 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5535 };
5536#endif
5537
Howard Hinnantc51e1022010-05-11 19:42:16 +00005538_LIBCPP_END_NAMESPACE_STD
5539
Eric Fiselierf4433a32017-05-31 22:07:49 +00005540_LIBCPP_POP_MACROS
5541
Howard Hinnantc51e1022010-05-11 19:42:16 +00005542#endif // _LIBCPP_MEMORY