blob: b12b46ab4fdb97c9823883451d4481a4820f9657 [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
723template <class _Tp>
724struct __has_element_type
725{
726private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000727 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728 template <class _Up> static __two __test(...);
729 template <class _Up> static char __test(typename _Up::element_type* = 0);
730public:
731 static const bool value = sizeof(__test<_Tp>(0)) == 1;
732};
733
734template <class _Ptr, bool = __has_element_type<_Ptr>::value>
735struct __pointer_traits_element_type;
736
737template <class _Ptr>
738struct __pointer_traits_element_type<_Ptr, true>
739{
740 typedef typename _Ptr::element_type type;
741};
742
743#ifndef _LIBCPP_HAS_NO_VARIADICS
744
745template <template <class, class...> class _Sp, class _Tp, class ..._Args>
746struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
747{
748 typedef typename _Sp<_Tp, _Args...>::element_type type;
749};
750
751template <template <class, class...> class _Sp, class _Tp, class ..._Args>
752struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
753{
754 typedef _Tp type;
755};
756
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000757#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758
759template <template <class> class _Sp, class _Tp>
760struct __pointer_traits_element_type<_Sp<_Tp>, true>
761{
762 typedef typename _Sp<_Tp>::element_type type;
763};
764
765template <template <class> class _Sp, class _Tp>
766struct __pointer_traits_element_type<_Sp<_Tp>, false>
767{
768 typedef _Tp type;
769};
770
771template <template <class, class> class _Sp, class _Tp, class _A0>
772struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
773{
774 typedef typename _Sp<_Tp, _A0>::element_type type;
775};
776
777template <template <class, class> class _Sp, class _Tp, class _A0>
778struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
779{
780 typedef _Tp type;
781};
782
783template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
784struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
785{
786 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
787};
788
789template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
790struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
791{
792 typedef _Tp type;
793};
794
795template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
796 class _A1, class _A2>
797struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
798{
799 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
800};
801
802template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
803 class _A1, class _A2>
804struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
805{
806 typedef _Tp type;
807};
808
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000809#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810
811template <class _Tp>
812struct __has_difference_type
813{
814private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000815 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 template <class _Up> static __two __test(...);
817 template <class _Up> static char __test(typename _Up::difference_type* = 0);
818public:
819 static const bool value = sizeof(__test<_Tp>(0)) == 1;
820};
821
822template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
823struct __pointer_traits_difference_type
824{
825 typedef ptrdiff_t type;
826};
827
828template <class _Ptr>
829struct __pointer_traits_difference_type<_Ptr, true>
830{
831 typedef typename _Ptr::difference_type type;
832};
833
834template <class _Tp, class _Up>
835struct __has_rebind
836{
837private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000838 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839 template <class _Xp> static __two __test(...);
840 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
841public:
842 static const bool value = sizeof(__test<_Tp>(0)) == 1;
843};
844
845template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
846struct __pointer_traits_rebind
847{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000848#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849 typedef typename _Tp::template rebind<_Up> type;
850#else
851 typedef typename _Tp::template rebind<_Up>::other type;
852#endif
853};
854
855#ifndef _LIBCPP_HAS_NO_VARIADICS
856
857template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
858struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
859{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000860#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
862#else
863 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
864#endif
865};
866
867template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
868struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
869{
870 typedef _Sp<_Up, _Args...> type;
871};
872
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000873#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874
875template <template <class> class _Sp, class _Tp, class _Up>
876struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
877{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000878#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 typedef typename _Sp<_Tp>::template rebind<_Up> type;
880#else
881 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
882#endif
883};
884
885template <template <class> class _Sp, class _Tp, class _Up>
886struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
887{
888 typedef _Sp<_Up> type;
889};
890
891template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
892struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
893{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000894#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000895 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
896#else
897 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
898#endif
899};
900
901template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
902struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
903{
904 typedef _Sp<_Up, _A0> type;
905};
906
907template <template <class, class, class> class _Sp, class _Tp, class _A0,
908 class _A1, class _Up>
909struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
910{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000911#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
913#else
914 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
915#endif
916};
917
918template <template <class, class, class> class _Sp, class _Tp, class _A0,
919 class _A1, class _Up>
920struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
921{
922 typedef _Sp<_Up, _A0, _A1> type;
923};
924
925template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
926 class _A1, class _A2, class _Up>
927struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
928{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000929#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
931#else
932 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
933#endif
934};
935
936template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
937 class _A1, class _A2, class _Up>
938struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
939{
940 typedef _Sp<_Up, _A0, _A1, _A2> type;
941};
942
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000943#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944
945template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000946struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947{
948 typedef _Ptr pointer;
949 typedef typename __pointer_traits_element_type<pointer>::type element_type;
950 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
951
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000952#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000953 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954#else
955 template <class _Up> struct rebind
956 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000957#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958
959private:
960 struct __nat {};
961public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963 static pointer pointer_to(typename conditional<is_void<element_type>::value,
964 __nat, element_type>::type& __r)
965 {return pointer::pointer_to(__r);}
966};
967
968template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000969struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970{
971 typedef _Tp* pointer;
972 typedef _Tp element_type;
973 typedef ptrdiff_t difference_type;
974
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000975#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 template <class _Up> using rebind = _Up*;
977#else
978 template <class _Up> struct rebind {typedef _Up* other;};
979#endif
980
981private:
982 struct __nat {};
983public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000986 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000987 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988};
989
Eric Fiselierae3ab842015-08-23 02:56:05 +0000990template <class _From, class _To>
991struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000992#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000993 typedef typename pointer_traits<_From>::template rebind<_To> type;
994#else
995 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
996#endif
997};
998
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999// allocator_traits
1000
Marshall Clow41eb88c2017-05-11 14:00:54 +00001001struct __has_pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002{
Howard Hinnant6e260172013-09-21 01:45:05 +00001003 template <class _Up> static __two __test(...);
1004 template <class _Up> static char __test(typename _Up::pointer* = 0);
Marshall Clow41eb88c2017-05-11 14:00:54 +00001005};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006
1007template <class _Tp>
1008struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +00001009 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010{
1011};
1012
1013namespace __pointer_type_imp
1014{
1015
1016template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1017struct __pointer_type
1018{
1019 typedef typename _Dp::pointer type;
1020};
1021
1022template <class _Tp, class _Dp>
1023struct __pointer_type<_Tp, _Dp, false>
1024{
1025 typedef _Tp* type;
1026};
1027
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001028} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029
1030template <class _Tp, class _Dp>
1031struct __pointer_type
1032{
1033 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1034};
1035
1036template <class _Tp>
1037struct __has_const_pointer
1038{
1039private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001040 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 template <class _Up> static __two __test(...);
1042 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1043public:
1044 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1045};
1046
1047template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1048struct __const_pointer
1049{
1050 typedef typename _Alloc::const_pointer type;
1051};
1052
1053template <class _Tp, class _Ptr, class _Alloc>
1054struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1055{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001056#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1058#else
1059 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1060#endif
1061};
1062
1063template <class _Tp>
1064struct __has_void_pointer
1065{
1066private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001067 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 template <class _Up> static __two __test(...);
1069 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1070public:
1071 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1072};
1073
1074template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1075struct __void_pointer
1076{
1077 typedef typename _Alloc::void_pointer type;
1078};
1079
1080template <class _Ptr, class _Alloc>
1081struct __void_pointer<_Ptr, _Alloc, false>
1082{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001083#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1085#else
1086 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1087#endif
1088};
1089
1090template <class _Tp>
1091struct __has_const_void_pointer
1092{
1093private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001094 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 template <class _Up> static __two __test(...);
1096 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1097public:
1098 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1099};
1100
1101template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1102struct __const_void_pointer
1103{
1104 typedef typename _Alloc::const_void_pointer type;
1105};
1106
1107template <class _Ptr, class _Alloc>
1108struct __const_void_pointer<_Ptr, _Alloc, false>
1109{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001110#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1112#else
1113 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1114#endif
1115};
1116
Howard Hinnantc834c512011-11-29 18:15:50 +00001117template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001119_Tp*
1120__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121{
1122 return __p;
1123}
1124
1125template <class _Pointer>
1126inline _LIBCPP_INLINE_VISIBILITY
1127typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001128__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001130 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131}
1132
1133template <class _Tp>
1134struct __has_size_type
1135{
1136private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001137 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138 template <class _Up> static __two __test(...);
1139 template <class _Up> static char __test(typename _Up::size_type* = 0);
1140public:
1141 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1142};
1143
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001144template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145struct __size_type
1146{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001147 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148};
1149
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001150template <class _Alloc, class _DiffType>
1151struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152{
1153 typedef typename _Alloc::size_type type;
1154};
1155
1156template <class _Tp>
1157struct __has_propagate_on_container_copy_assignment
1158{
1159private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001160 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 template <class _Up> static __two __test(...);
1162 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1163public:
1164 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1165};
1166
1167template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1168struct __propagate_on_container_copy_assignment
1169{
1170 typedef false_type type;
1171};
1172
1173template <class _Alloc>
1174struct __propagate_on_container_copy_assignment<_Alloc, true>
1175{
1176 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1177};
1178
1179template <class _Tp>
1180struct __has_propagate_on_container_move_assignment
1181{
1182private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001183 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 template <class _Up> static __two __test(...);
1185 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1186public:
1187 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1188};
1189
1190template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1191struct __propagate_on_container_move_assignment
1192{
1193 typedef false_type type;
1194};
1195
1196template <class _Alloc>
1197struct __propagate_on_container_move_assignment<_Alloc, true>
1198{
1199 typedef typename _Alloc::propagate_on_container_move_assignment type;
1200};
1201
1202template <class _Tp>
1203struct __has_propagate_on_container_swap
1204{
1205private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001206 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207 template <class _Up> static __two __test(...);
1208 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1209public:
1210 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1211};
1212
1213template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1214struct __propagate_on_container_swap
1215{
1216 typedef false_type type;
1217};
1218
1219template <class _Alloc>
1220struct __propagate_on_container_swap<_Alloc, true>
1221{
1222 typedef typename _Alloc::propagate_on_container_swap type;
1223};
1224
Marshall Clow0b587562015-06-02 16:34:03 +00001225template <class _Tp>
1226struct __has_is_always_equal
1227{
1228private:
1229 struct __two {char __lx; char __lxx;};
1230 template <class _Up> static __two __test(...);
1231 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1232public:
1233 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1234};
1235
1236template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1237struct __is_always_equal
1238{
1239 typedef typename _VSTD::is_empty<_Alloc>::type type;
1240};
1241
1242template <class _Alloc>
1243struct __is_always_equal<_Alloc, true>
1244{
1245 typedef typename _Alloc::is_always_equal type;
1246};
1247
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1249struct __has_rebind_other
1250{
1251private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001252 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253 template <class _Xp> static __two __test(...);
1254 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1255public:
1256 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1257};
1258
1259template <class _Tp, class _Up>
1260struct __has_rebind_other<_Tp, _Up, false>
1261{
1262 static const bool value = false;
1263};
1264
1265template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1266struct __allocator_traits_rebind
1267{
1268 typedef typename _Tp::template rebind<_Up>::other type;
1269};
1270
1271#ifndef _LIBCPP_HAS_NO_VARIADICS
1272
1273template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1274struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1275{
1276 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1277};
1278
1279template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1280struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1281{
1282 typedef _Alloc<_Up, _Args...> type;
1283};
1284
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001285#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286
1287template <template <class> class _Alloc, class _Tp, class _Up>
1288struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1289{
1290 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1291};
1292
1293template <template <class> class _Alloc, class _Tp, class _Up>
1294struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1295{
1296 typedef _Alloc<_Up> type;
1297};
1298
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1300struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1301{
1302 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1303};
1304
1305template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1306struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1307{
1308 typedef _Alloc<_Up, _A0> type;
1309};
1310
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1312 class _A1, class _Up>
1313struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1314{
1315 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1316};
1317
1318template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1319 class _A1, class _Up>
1320struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1321{
1322 typedef _Alloc<_Up, _A0, _A1> type;
1323};
1324
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1326 class _A1, class _A2, class _Up>
1327struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1328{
1329 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1330};
1331
1332template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1333 class _A1, class _A2, class _Up>
1334struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1335{
1336 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1337};
1338
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001339#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001341#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342
1343template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1344auto
1345__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1346 -> decltype(__a.allocate(__sz, __p), true_type());
1347
1348template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1349auto
1350__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1351 -> false_type;
1352
1353template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1354struct __has_allocate_hint
1355 : integral_constant<bool,
1356 is_same<
1357 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1358 declval<_SizeType>(),
1359 declval<_ConstVoidPtr>())),
1360 true_type>::value>
1361{
1362};
1363
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001364#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365
1366template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1367struct __has_allocate_hint
1368 : true_type
1369{
1370};
1371
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001372#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001374#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375
1376template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001377decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1378 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 true_type())
1380__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1381
1382template <class _Alloc, class _Pointer, class ..._Args>
1383false_type
1384__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1385
1386template <class _Alloc, class _Pointer, class ..._Args>
1387struct __has_construct
1388 : integral_constant<bool,
1389 is_same<
1390 decltype(__has_construct_test(declval<_Alloc>(),
1391 declval<_Pointer>(),
1392 declval<_Args>()...)),
1393 true_type>::value>
1394{
1395};
1396
1397template <class _Alloc, class _Pointer>
1398auto
1399__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1400 -> decltype(__a.destroy(__p), true_type());
1401
1402template <class _Alloc, class _Pointer>
1403auto
1404__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1405 -> false_type;
1406
1407template <class _Alloc, class _Pointer>
1408struct __has_destroy
1409 : integral_constant<bool,
1410 is_same<
1411 decltype(__has_destroy_test(declval<_Alloc>(),
1412 declval<_Pointer>())),
1413 true_type>::value>
1414{
1415};
1416
1417template <class _Alloc>
1418auto
1419__has_max_size_test(_Alloc&& __a)
1420 -> decltype(__a.max_size(), true_type());
1421
1422template <class _Alloc>
1423auto
1424__has_max_size_test(const volatile _Alloc& __a)
1425 -> false_type;
1426
1427template <class _Alloc>
1428struct __has_max_size
1429 : integral_constant<bool,
1430 is_same<
1431 decltype(__has_max_size_test(declval<_Alloc&>())),
1432 true_type>::value>
1433{
1434};
1435
1436template <class _Alloc>
1437auto
1438__has_select_on_container_copy_construction_test(_Alloc&& __a)
1439 -> decltype(__a.select_on_container_copy_construction(), true_type());
1440
1441template <class _Alloc>
1442auto
1443__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1444 -> false_type;
1445
1446template <class _Alloc>
1447struct __has_select_on_container_copy_construction
1448 : integral_constant<bool,
1449 is_same<
1450 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1451 true_type>::value>
1452{
1453};
1454
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001455#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456
1457#ifndef _LIBCPP_HAS_NO_VARIADICS
1458
1459template <class _Alloc, class _Pointer, class ..._Args>
1460struct __has_construct
1461 : false_type
1462{
1463};
1464
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001465#else // _LIBCPP_HAS_NO_VARIADICS
1466
1467template <class _Alloc, class _Pointer, class _Args>
1468struct __has_construct
1469 : false_type
1470{
1471};
1472
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001473#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474
1475template <class _Alloc, class _Pointer>
1476struct __has_destroy
1477 : false_type
1478{
1479};
1480
1481template <class _Alloc>
1482struct __has_max_size
1483 : true_type
1484{
1485};
1486
1487template <class _Alloc>
1488struct __has_select_on_container_copy_construction
1489 : false_type
1490{
1491};
1492
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001493#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001495template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1496struct __alloc_traits_difference_type
1497{
1498 typedef typename pointer_traits<_Ptr>::difference_type type;
1499};
1500
1501template <class _Alloc, class _Ptr>
1502struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1503{
1504 typedef typename _Alloc::difference_type type;
1505};
1506
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001508struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509{
1510 typedef _Alloc allocator_type;
1511 typedef typename allocator_type::value_type value_type;
1512
1513 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1514 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1515 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1516 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1517
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001518 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1519 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520
1521 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1522 propagate_on_container_copy_assignment;
1523 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1524 propagate_on_container_move_assignment;
1525 typedef typename __propagate_on_container_swap<allocator_type>::type
1526 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001527 typedef typename __is_always_equal<allocator_type>::type
1528 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001530#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001532 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001534#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535 template <class _Tp> struct rebind_alloc
1536 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1537 template <class _Tp> struct rebind_traits
1538 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001539#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540
Howard Hinnant756c69b2010-09-22 16:48:34 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 static pointer allocate(allocator_type& __a, size_type __n)
1543 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1546 {return allocate(__a, __n, __hint,
1547 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1548
Howard Hinnant756c69b2010-09-22 16:48:34 +00001549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001550 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 {__a.deallocate(__p, __n);}
1552
1553#ifndef _LIBCPP_HAS_NO_VARIADICS
1554 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001557 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001558 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001559#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001561 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001562 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 {
1564 ::new ((void*)__p) _Tp();
1565 }
1566 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001567 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001568 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 {
1570 ::new ((void*)__p) _Tp(__a0);
1571 }
1572 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001573 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001574 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 const _A1& __a1)
1576 {
1577 ::new ((void*)__p) _Tp(__a0, __a1);
1578 }
1579 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001580 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001581 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 const _A1& __a1, const _A2& __a2)
1583 {
1584 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1585 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001586#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587
1588 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 static void destroy(allocator_type& __a, _Tp* __p)
1591 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1592
Howard Hinnant756c69b2010-09-22 16:48:34 +00001593 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001594 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1596
Howard Hinnant756c69b2010-09-22 16:48:34 +00001597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 static allocator_type
1599 select_on_container_copy_construction(const allocator_type& __a)
1600 {return select_on_container_copy_construction(
1601 __has_select_on_container_copy_construction<const allocator_type>(),
1602 __a);}
1603
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001604 template <class _Ptr>
1605 _LIBCPP_INLINE_VISIBILITY
1606 static
1607 void
1608 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1609 {
1610 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1611 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1612 }
1613
1614 template <class _Tp>
1615 _LIBCPP_INLINE_VISIBILITY
1616 static
1617 typename enable_if
1618 <
1619 (is_same<allocator_type, allocator<_Tp> >::value
1620 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1621 is_trivially_move_constructible<_Tp>::value,
1622 void
1623 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001624 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001625 {
1626 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001627 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001628 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001629 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001630 __begin2 += _Np;
1631 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001632 }
1633
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001634 template <class _Iter, class _Ptr>
1635 _LIBCPP_INLINE_VISIBILITY
1636 static
1637 void
1638 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1639 {
1640 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1641 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1642 }
1643
1644 template <class _Tp>
1645 _LIBCPP_INLINE_VISIBILITY
1646 static
1647 typename enable_if
1648 <
1649 (is_same<allocator_type, allocator<_Tp> >::value
1650 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1651 is_trivially_move_constructible<_Tp>::value,
1652 void
1653 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001654 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001655 {
1656 typedef typename remove_const<_Tp>::type _Vp;
1657 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001658 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001659 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001660 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001661 __begin2 += _Np;
1662 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001663 }
1664
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001665 template <class _Ptr>
1666 _LIBCPP_INLINE_VISIBILITY
1667 static
1668 void
1669 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1670 {
1671 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001672 {
1673 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1674 --__end2;
1675 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001676 }
1677
1678 template <class _Tp>
1679 _LIBCPP_INLINE_VISIBILITY
1680 static
1681 typename enable_if
1682 <
1683 (is_same<allocator_type, allocator<_Tp> >::value
1684 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1685 is_trivially_move_constructible<_Tp>::value,
1686 void
1687 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001688 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001689 {
1690 ptrdiff_t _Np = __end1 - __begin1;
1691 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001692 if (_Np > 0)
1693 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001694 }
1695
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696private:
1697
Howard Hinnant756c69b2010-09-22 16:48:34 +00001698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699 static pointer allocate(allocator_type& __a, size_type __n,
1700 const_void_pointer __hint, true_type)
1701 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001704 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 {return __a.allocate(__n);}
1706
1707#ifndef _LIBCPP_HAS_NO_VARIADICS
1708 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001711 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1715 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001716 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001718#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719
1720 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1723 {__a.destroy(__p);}
1724 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 static void __destroy(false_type, allocator_type&, _Tp* __p)
1727 {
1728 __p->~_Tp();
1729 }
1730
Howard Hinnant756c69b2010-09-22 16:48:34 +00001731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732 static size_type __max_size(true_type, const allocator_type& __a)
1733 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001736 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737
Howard Hinnant756c69b2010-09-22 16:48:34 +00001738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739 static allocator_type
1740 select_on_container_copy_construction(true_type, const allocator_type& __a)
1741 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743 static allocator_type
1744 select_on_container_copy_construction(false_type, const allocator_type& __a)
1745 {return __a;}
1746};
1747
Marshall Clow940e01c2015-04-07 05:21:38 +00001748template <class _Traits, class _Tp>
1749struct __rebind_alloc_helper
1750{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001751#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001752 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001753#else
1754 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1755#endif
1756};
1757
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758// allocator
1759
1760template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001761class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762{
1763public:
1764 typedef size_t size_type;
1765 typedef ptrdiff_t difference_type;
1766 typedef _Tp* pointer;
1767 typedef const _Tp* const_pointer;
1768 typedef _Tp& reference;
1769 typedef const _Tp& const_reference;
1770 typedef _Tp value_type;
1771
Howard Hinnant4931e092011-06-02 21:38:57 +00001772 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001773 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001774
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1776
Howard Hinnant719bda32011-05-28 14:41:13 +00001777 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1778 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1779 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001781 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001782 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001784 {
1785 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001786 __throw_length_error("allocator<T>::allocate(size_t n)"
1787 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001788 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1789 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001790 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001791 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001792 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1793 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001794#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 template <class _Up, class... _Args>
1796 _LIBCPP_INLINE_VISIBILITY
1797 void
1798 construct(_Up* __p, _Args&&... __args)
1799 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001800 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001802#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 _LIBCPP_INLINE_VISIBILITY
1804 void
1805 construct(pointer __p)
1806 {
1807 ::new((void*)__p) _Tp();
1808 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001809# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001810
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 template <class _A0>
1812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001813 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 construct(pointer __p, _A0& __a0)
1815 {
1816 ::new((void*)__p) _Tp(__a0);
1817 }
1818 template <class _A0>
1819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001820 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 construct(pointer __p, const _A0& __a0)
1822 {
1823 ::new((void*)__p) _Tp(__a0);
1824 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001825# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 template <class _A0, class _A1>
1827 _LIBCPP_INLINE_VISIBILITY
1828 void
1829 construct(pointer __p, _A0& __a0, _A1& __a1)
1830 {
1831 ::new((void*)__p) _Tp(__a0, __a1);
1832 }
1833 template <class _A0, class _A1>
1834 _LIBCPP_INLINE_VISIBILITY
1835 void
1836 construct(pointer __p, const _A0& __a0, _A1& __a1)
1837 {
1838 ::new((void*)__p) _Tp(__a0, __a1);
1839 }
1840 template <class _A0, class _A1>
1841 _LIBCPP_INLINE_VISIBILITY
1842 void
1843 construct(pointer __p, _A0& __a0, const _A1& __a1)
1844 {
1845 ::new((void*)__p) _Tp(__a0, __a1);
1846 }
1847 template <class _A0, class _A1>
1848 _LIBCPP_INLINE_VISIBILITY
1849 void
1850 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1851 {
1852 ::new((void*)__p) _Tp(__a0, __a1);
1853 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001854#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1856};
1857
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001858template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001859class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001860{
1861public:
1862 typedef size_t size_type;
1863 typedef ptrdiff_t difference_type;
1864 typedef const _Tp* pointer;
1865 typedef const _Tp* const_pointer;
1866 typedef const _Tp& reference;
1867 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001868 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001869
1870 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001871 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001872
1873 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1874
1875 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1876 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1877 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1878 {return _VSTD::addressof(__x);}
1879 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001880 {
1881 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001882 __throw_length_error("allocator<const T>::allocate(size_t n)"
1883 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001884 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001885 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001886 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001887 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001888 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1889 {return size_type(~0) / sizeof(_Tp);}
1890#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1891 template <class _Up, class... _Args>
1892 _LIBCPP_INLINE_VISIBILITY
1893 void
1894 construct(_Up* __p, _Args&&... __args)
1895 {
1896 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1897 }
1898#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1899 _LIBCPP_INLINE_VISIBILITY
1900 void
1901 construct(pointer __p)
1902 {
1903 ::new((void*)__p) _Tp();
1904 }
1905# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001906
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001907 template <class _A0>
1908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001909 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001910 construct(pointer __p, _A0& __a0)
1911 {
1912 ::new((void*)__p) _Tp(__a0);
1913 }
1914 template <class _A0>
1915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001916 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001917 construct(pointer __p, const _A0& __a0)
1918 {
1919 ::new((void*)__p) _Tp(__a0);
1920 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001921# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1922 template <class _A0, class _A1>
1923 _LIBCPP_INLINE_VISIBILITY
1924 void
1925 construct(pointer __p, _A0& __a0, _A1& __a1)
1926 {
1927 ::new((void*)__p) _Tp(__a0, __a1);
1928 }
1929 template <class _A0, class _A1>
1930 _LIBCPP_INLINE_VISIBILITY
1931 void
1932 construct(pointer __p, const _A0& __a0, _A1& __a1)
1933 {
1934 ::new((void*)__p) _Tp(__a0, __a1);
1935 }
1936 template <class _A0, class _A1>
1937 _LIBCPP_INLINE_VISIBILITY
1938 void
1939 construct(pointer __p, _A0& __a0, const _A1& __a1)
1940 {
1941 ::new((void*)__p) _Tp(__a0, __a1);
1942 }
1943 template <class _A0, class _A1>
1944 _LIBCPP_INLINE_VISIBILITY
1945 void
1946 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1947 {
1948 ::new((void*)__p) _Tp(__a0, __a1);
1949 }
1950#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1951 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1952};
1953
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954template <class _Tp, class _Up>
1955inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001956bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001957
1958template <class _Tp, class _Up>
1959inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001960bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961
1962template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001963class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964 : public iterator<output_iterator_tag,
1965 _Tp, // purposefully not C++03
1966 ptrdiff_t, // purposefully not C++03
1967 _Tp*, // purposefully not C++03
1968 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1969{
1970private:
1971 _OutputIterator __x_;
1972public:
1973 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1974 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1975 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1976 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001977#if _LIBCPP_STD_VER >= 14
1978 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1979 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1980#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1982 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1983 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001984#if _LIBCPP_STD_VER >= 14
1985 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1986#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987};
1988
1989template <class _Tp>
1990pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001991get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992{
1993 pair<_Tp*, ptrdiff_t> __r(0, 0);
1994 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1995 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1996 / sizeof(_Tp);
1997 if (__n > __m)
1998 __n = __m;
1999 while (__n > 0)
2000 {
2001 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2002 if (__r.first)
2003 {
2004 __r.second = __n;
2005 break;
2006 }
2007 __n /= 2;
2008 }
2009 return __r;
2010}
2011
2012template <class _Tp>
2013inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002014void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015
Marshall Clowb22274f2017-01-24 22:22:33 +00002016#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017template <class _Tp>
2018struct auto_ptr_ref
2019{
2020 _Tp* __ptr_;
2021};
2022
2023template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002024class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025{
2026private:
2027 _Tp* __ptr_;
2028public:
2029 typedef _Tp element_type;
2030
2031 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2032 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2033 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2034 : __ptr_(__p.release()) {}
2035 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2036 {reset(__p.release()); return *this;}
2037 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2038 {reset(__p.release()); return *this;}
2039 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2040 {reset(__p.__ptr_); return *this;}
2041 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2042
2043 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2044 {return *__ptr_;}
2045 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2046 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2047 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2048 {
2049 _Tp* __t = __ptr_;
2050 __ptr_ = 0;
2051 return __t;
2052 }
2053 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2054 {
2055 if (__ptr_ != __p)
2056 delete __ptr_;
2057 __ptr_ = __p;
2058 }
2059
2060 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2061 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2062 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2063 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2064 {return auto_ptr<_Up>(release());}
2065};
2066
2067template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002068class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069{
2070public:
2071 typedef void element_type;
2072};
Marshall Clowb22274f2017-01-24 22:22:33 +00002073#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074
Eric Fiselier9d355982017-04-12 23:45:53 +00002075template <class _Tp, int _Idx,
2076 bool _CanBeEmptyBase =
2077 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2078struct __compressed_pair_elem {
2079 typedef _Tp _ParamT;
2080 typedef _Tp& reference;
2081 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082
Eric Fiselier9d355982017-04-12 23:45:53 +00002083#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002084 constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085
Eric Fiselier9d355982017-04-12 23:45:53 +00002086 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002087 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2088 >::type>
2089 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002090 __compressed_pair_elem(_Up&& __u)
2091 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092
Eric Fiselier9d355982017-04-12 23:45:53 +00002093 template <class... _Args, size_t... _Indexes>
2094 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2095 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2096 __tuple_indices<_Indexes...>)
2097 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2098#else
2099 __compressed_pair_elem() : __value_() {}
2100 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2101#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102
Eric Fiselier9d355982017-04-12 23:45:53 +00002103 reference __get() _NOEXCEPT { return __value_; }
2104 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002107 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108};
2109
Eric Fiselier9d355982017-04-12 23:45:53 +00002110template <class _Tp, int _Idx>
2111struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2112 typedef _Tp _ParamT;
2113 typedef _Tp& reference;
2114 typedef const _Tp& const_reference;
2115 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116
Eric Fiselier9d355982017-04-12 23:45:53 +00002117#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002118 constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119
Eric Fiselier9d355982017-04-12 23:45:53 +00002120 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002121 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2122 >::type>
2123 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002124 __compressed_pair_elem(_Up&& __u)
2125 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126
Eric Fiselier9d355982017-04-12 23:45:53 +00002127 template <class... _Args, size_t... _Indexes>
2128 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2129 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2130 __tuple_indices<_Indexes...>)
2131 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2132#else
2133 __compressed_pair_elem() : __value_type() {}
2134 __compressed_pair_elem(_ParamT __p)
2135 : __value_type(std::forward<_ParamT>(__p)) {}
2136#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137
Eric Fiselier9d355982017-04-12 23:45:53 +00002138 reference __get() _NOEXCEPT { return *this; }
2139 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140};
2141
Eric Fiselier9d355982017-04-12 23:45:53 +00002142// Tag used to construct the second element of the compressed pair.
2143struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144
2145template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002146class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2147 private __compressed_pair_elem<_T2, 1> {
2148 typedef __compressed_pair_elem<_T1, 0> _Base1;
2149 typedef __compressed_pair_elem<_T2, 1> _Base2;
2150
2151 // NOTE: This static assert should never fire because __compressed_pair
2152 // is *almost never* used in a scenario where it's possible for T1 == T2.
2153 // (The exception is std::function where it is possible that the function
2154 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002155 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002156 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2157 "The current implementation is NOT ABI-compatible with the previous "
2158 "implementation for this configuration");
2159
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002161#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002162 template <bool _Dummy = true,
2163 class = typename enable_if<
2164 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2165 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2166 >::type
2167 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002168 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002169 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170
Eric Fiselier9d355982017-04-12 23:45:53 +00002171 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2172 __compressed_pair>::value,
2173 bool>::type = true>
2174 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2175 __compressed_pair(_Tp&& __t)
2176 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177
Eric Fiselier9d355982017-04-12 23:45:53 +00002178 template <class _Tp>
2179 _LIBCPP_INLINE_VISIBILITY constexpr
2180 __compressed_pair(__second_tag, _Tp&& __t)
2181 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182
Eric Fiselier9d355982017-04-12 23:45:53 +00002183 template <class _U1, class _U2>
2184 _LIBCPP_INLINE_VISIBILITY constexpr
2185 __compressed_pair(_U1&& __t1, _U2&& __t2)
2186 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187
Eric Fiselier9d355982017-04-12 23:45:53 +00002188 template <class... _Args1, class... _Args2>
2189 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2190 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2191 tuple<_Args2...> __second_args)
2192 : _Base1(__pc, _VSTD::move(__first_args),
2193 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2194 _Base2(__pc, _VSTD::move(__second_args),
2195 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002196
Eric Fiselier9d355982017-04-12 23:45:53 +00002197#else
2198 _LIBCPP_INLINE_VISIBILITY
2199 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002200
Eric Fiselier9d355982017-04-12 23:45:53 +00002201 _LIBCPP_INLINE_VISIBILITY explicit
2202 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002203
Eric Fiselier9d355982017-04-12 23:45:53 +00002204 _LIBCPP_INLINE_VISIBILITY
2205 __compressed_pair(__second_tag, _T2 __t2)
2206 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207
Eric Fiselier9d355982017-04-12 23:45:53 +00002208 _LIBCPP_INLINE_VISIBILITY
2209 __compressed_pair(_T1 __t1, _T2 __t2)
2210 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2211#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212
Eric Fiselier9d355982017-04-12 23:45:53 +00002213 _LIBCPP_INLINE_VISIBILITY
2214 typename _Base1::reference first() _NOEXCEPT {
2215 return static_cast<_Base1&>(*this).__get();
2216 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217
Eric Fiselier9d355982017-04-12 23:45:53 +00002218 _LIBCPP_INLINE_VISIBILITY
2219 typename _Base1::const_reference first() const _NOEXCEPT {
2220 return static_cast<_Base1 const&>(*this).__get();
2221 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222
Eric Fiselier9d355982017-04-12 23:45:53 +00002223 _LIBCPP_INLINE_VISIBILITY
2224 typename _Base2::reference second() _NOEXCEPT {
2225 return static_cast<_Base2&>(*this).__get();
2226 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227
Eric Fiselier9d355982017-04-12 23:45:53 +00002228 _LIBCPP_INLINE_VISIBILITY
2229 typename _Base2::const_reference second() const _NOEXCEPT {
2230 return static_cast<_Base2 const&>(*this).__get();
2231 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232
Eric Fiselier9d355982017-04-12 23:45:53 +00002233 _LIBCPP_INLINE_VISIBILITY
2234 void swap(__compressed_pair& __x)
2235 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2236 __is_nothrow_swappable<_T2>::value)
2237 {
2238 using std::swap;
2239 swap(first(), __x.first());
2240 swap(second(), __x.second());
2241 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242};
2243
2244template <class _T1, class _T2>
2245inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002246void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2247 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2248 __is_nothrow_swappable<_T2>::value) {
2249 __x.swap(__y);
2250}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002252// default_delete
2253
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002255struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002256 static_assert(!is_function<_Tp>::value,
2257 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002258#ifndef _LIBCPP_CXX03_LANG
2259 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002260#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002261 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002262#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002263 template <class _Up>
2264 _LIBCPP_INLINE_VISIBILITY
2265 default_delete(const default_delete<_Up>&,
2266 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2267 0) _NOEXCEPT {}
2268
2269 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2270 static_assert(sizeof(_Tp) > 0,
2271 "default_delete can not delete incomplete type");
2272 static_assert(!is_void<_Tp>::value,
2273 "default_delete can not delete incomplete type");
2274 delete __ptr;
2275 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276};
2277
2278template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002279struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2280private:
2281 template <class _Up>
2282 struct _EnableIfConvertible
2283 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2284
Howard Hinnant4500ca52011-12-18 21:19:44 +00002285public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002286#ifndef _LIBCPP_CXX03_LANG
2287 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002288#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002289 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002290#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002291
2292 template <class _Up>
2293 _LIBCPP_INLINE_VISIBILITY
2294 default_delete(const default_delete<_Up[]>&,
2295 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2296
2297 template <class _Up>
2298 _LIBCPP_INLINE_VISIBILITY
2299 typename _EnableIfConvertible<_Up>::type
2300 operator()(_Up* __ptr) const _NOEXCEPT {
2301 static_assert(sizeof(_Tp) > 0,
2302 "default_delete can not delete incomplete type");
2303 static_assert(!is_void<_Tp>::value,
2304 "default_delete can not delete void type");
2305 delete[] __ptr;
2306 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307};
2308
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309
Eric Fiselier6779b062017-04-16 02:06:25 +00002310
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002311#ifndef _LIBCPP_CXX03_LANG
2312template <class _Deleter>
2313struct __unique_ptr_deleter_sfinae {
2314 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2315 typedef const _Deleter& __lval_ref_type;
2316 typedef _Deleter&& __good_rval_ref_type;
2317 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318};
2319
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002320template <class _Deleter>
2321struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2322 typedef const _Deleter& __lval_ref_type;
2323 typedef const _Deleter&& __bad_rval_ref_type;
2324 typedef false_type __enable_rval_overload;
2325};
2326
2327template <class _Deleter>
2328struct __unique_ptr_deleter_sfinae<_Deleter&> {
2329 typedef _Deleter& __lval_ref_type;
2330 typedef _Deleter&& __bad_rval_ref_type;
2331 typedef false_type __enable_rval_overload;
2332};
2333#endif // !defined(_LIBCPP_CXX03_LANG)
2334
2335template <class _Tp, class _Dp = default_delete<_Tp> >
2336class _LIBCPP_TEMPLATE_VIS unique_ptr {
2337public:
2338 typedef _Tp element_type;
2339 typedef _Dp deleter_type;
2340 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2341
2342 static_assert(!is_rvalue_reference<deleter_type>::value,
2343 "the specified deleter type cannot be an rvalue reference");
2344
2345private:
2346 __compressed_pair<pointer, deleter_type> __ptr_;
2347
2348 struct __nat { int __for_bool_; };
2349
2350#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002351 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002352
2353 template <bool _Dummy>
2354 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002355 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002356
2357 template <bool _Dummy>
2358 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002359 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002360
2361 template <bool _Dummy>
2362 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002363 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002364
2365 template <bool _Dummy, class _Deleter = typename __dependent_type<
2366 __identity<deleter_type>, _Dummy>::type>
2367 using _EnableIfDeleterDefaultConstructible =
2368 typename enable_if<is_default_constructible<_Deleter>::value &&
2369 !is_pointer<_Deleter>::value>::type;
2370
2371 template <class _ArgType>
2372 using _EnableIfDeleterConstructible =
2373 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2374
2375 template <class _UPtr, class _Up>
2376 using _EnableIfMoveConvertible = typename enable_if<
2377 is_convertible<typename _UPtr::pointer, pointer>::value &&
2378 !is_array<_Up>::value
2379 >::type;
2380
2381 template <class _UDel>
2382 using _EnableIfDeleterConvertible = typename enable_if<
2383 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2384 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2385 >::type;
2386
2387 template <class _UDel>
2388 using _EnableIfDeleterAssignable = typename enable_if<
2389 is_assignable<_Dp&, _UDel&&>::value
2390 >::type;
2391
2392public:
2393 template <bool _Dummy = true,
2394 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2395 _LIBCPP_INLINE_VISIBILITY
2396 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2397
2398 template <bool _Dummy = true,
2399 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2400 _LIBCPP_INLINE_VISIBILITY
2401 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2402
2403 template <bool _Dummy = true,
2404 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2405 _LIBCPP_INLINE_VISIBILITY
2406 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2407
2408 template <bool _Dummy = true,
2409 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2410 _LIBCPP_INLINE_VISIBILITY
2411 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2412 : __ptr_(__p, __d) {}
2413
2414 template <bool _Dummy = true,
2415 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2416 _LIBCPP_INLINE_VISIBILITY
2417 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2418 : __ptr_(__p, _VSTD::move(__d)) {
2419 static_assert(!is_reference<deleter_type>::value,
2420 "rvalue deleter bound to reference");
2421 }
2422
2423 template <bool _Dummy = true,
2424 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2425 _LIBCPP_INLINE_VISIBILITY
2426 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2427
2428 _LIBCPP_INLINE_VISIBILITY
2429 unique_ptr(unique_ptr&& __u) noexcept
2430 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2431 }
2432
2433 template <class _Up, class _Ep,
2434 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2435 class = _EnableIfDeleterConvertible<_Ep>
2436 >
2437 _LIBCPP_INLINE_VISIBILITY
2438 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2439 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2440
2441#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2442 template <class _Up>
2443 _LIBCPP_INLINE_VISIBILITY
2444 unique_ptr(auto_ptr<_Up>&& __p,
2445 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2446 is_same<_Dp, default_delete<_Tp>>::value,
2447 __nat>::type = __nat()) _NOEXCEPT
2448 : __ptr_(__p.release()) {}
2449#endif
2450
2451 _LIBCPP_INLINE_VISIBILITY
2452 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2453 reset(__u.release());
2454 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2455 return *this;
2456 }
2457
2458 template <class _Up, class _Ep,
2459 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2460 class = _EnableIfDeleterAssignable<_Ep>
2461 >
2462 _LIBCPP_INLINE_VISIBILITY
2463 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2464 reset(__u.release());
2465 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2466 return *this;
2467 }
2468
2469#else // _LIBCPP_CXX03_LANG
2470private:
2471 unique_ptr(unique_ptr&);
2472 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2473
2474 unique_ptr& operator=(unique_ptr&);
2475 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2476
2477public:
2478 _LIBCPP_INLINE_VISIBILITY
2479 unique_ptr() : __ptr_(pointer())
2480 {
2481 static_assert(!is_pointer<deleter_type>::value,
2482 "unique_ptr constructed with null function pointer deleter");
2483 static_assert(is_default_constructible<deleter_type>::value,
2484 "unique_ptr::deleter_type is not default constructible");
2485 }
2486 _LIBCPP_INLINE_VISIBILITY
2487 unique_ptr(nullptr_t) : __ptr_(pointer())
2488 {
2489 static_assert(!is_pointer<deleter_type>::value,
2490 "unique_ptr constructed with null function pointer deleter");
2491 }
2492 _LIBCPP_INLINE_VISIBILITY
2493 explicit unique_ptr(pointer __p)
2494 : __ptr_(_VSTD::move(__p)) {
2495 static_assert(!is_pointer<deleter_type>::value,
2496 "unique_ptr constructed with null function pointer deleter");
2497 }
2498
2499 _LIBCPP_INLINE_VISIBILITY
2500 operator __rv<unique_ptr>() {
2501 return __rv<unique_ptr>(*this);
2502 }
2503
2504 _LIBCPP_INLINE_VISIBILITY
2505 unique_ptr(__rv<unique_ptr> __u)
2506 : __ptr_(__u->release(),
2507 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2508
2509 template <class _Up, class _Ep>
2510 _LIBCPP_INLINE_VISIBILITY
2511 typename enable_if<
2512 !is_array<_Up>::value &&
2513 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2514 pointer>::value &&
2515 is_assignable<deleter_type&, _Ep&>::value,
2516 unique_ptr&>::type
2517 operator=(unique_ptr<_Up, _Ep> __u) {
2518 reset(__u.release());
2519 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2520 return *this;
2521 }
2522
2523 _LIBCPP_INLINE_VISIBILITY
2524 unique_ptr(pointer __p, deleter_type __d)
2525 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2526#endif // _LIBCPP_CXX03_LANG
2527
2528#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2529 template <class _Up>
2530 _LIBCPP_INLINE_VISIBILITY
2531 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2532 is_same<_Dp, default_delete<_Tp> >::value,
2533 unique_ptr&>::type
2534 operator=(auto_ptr<_Up> __p) {
2535 reset(__p.release());
2536 return *this;
2537 }
2538#endif
2539
2540 _LIBCPP_INLINE_VISIBILITY
2541 ~unique_ptr() { reset(); }
2542
2543 _LIBCPP_INLINE_VISIBILITY
2544 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2545 reset();
2546 return *this;
2547 }
2548
2549 _LIBCPP_INLINE_VISIBILITY
2550 typename add_lvalue_reference<_Tp>::type
2551 operator*() const {
2552 return *__ptr_.first();
2553 }
2554 _LIBCPP_INLINE_VISIBILITY
2555 pointer operator->() const _NOEXCEPT {
2556 return __ptr_.first();
2557 }
2558 _LIBCPP_INLINE_VISIBILITY
2559 pointer get() const _NOEXCEPT {
2560 return __ptr_.first();
2561 }
2562 _LIBCPP_INLINE_VISIBILITY
2563 deleter_type& get_deleter() _NOEXCEPT {
2564 return __ptr_.second();
2565 }
2566 _LIBCPP_INLINE_VISIBILITY
2567 const deleter_type& get_deleter() const _NOEXCEPT {
2568 return __ptr_.second();
2569 }
2570 _LIBCPP_INLINE_VISIBILITY
2571 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2572 return __ptr_.first() != nullptr;
2573 }
2574
2575 _LIBCPP_INLINE_VISIBILITY
2576 pointer release() _NOEXCEPT {
2577 pointer __t = __ptr_.first();
2578 __ptr_.first() = pointer();
2579 return __t;
2580 }
2581
2582 _LIBCPP_INLINE_VISIBILITY
2583 void reset(pointer __p = pointer()) _NOEXCEPT {
2584 pointer __tmp = __ptr_.first();
2585 __ptr_.first() = __p;
2586 if (__tmp)
2587 __ptr_.second()(__tmp);
2588 }
2589
2590 _LIBCPP_INLINE_VISIBILITY
2591 void swap(unique_ptr& __u) _NOEXCEPT {
2592 __ptr_.swap(__u.__ptr_);
2593 }
2594};
2595
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002596
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002598class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002600 typedef _Tp element_type;
2601 typedef _Dp deleter_type;
2602 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2603
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002605 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606
Eric Fiselier31127cd2017-04-16 02:14:31 +00002607 template <class _From>
2608 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609
Eric Fiselier31127cd2017-04-16 02:14:31 +00002610 template <class _FromElem>
2611 struct _CheckArrayPointerConversion<_FromElem*>
2612 : integral_constant<bool,
2613 is_same<_FromElem*, pointer>::value ||
2614 (is_same<pointer, element_type*>::value &&
2615 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2616 >
2617 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002619#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002620 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002621
2622 template <bool _Dummy>
2623 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002624 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002625
2626 template <bool _Dummy>
2627 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002628 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002629
2630 template <bool _Dummy>
2631 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002632 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002633
2634 template <bool _Dummy, class _Deleter = typename __dependent_type<
2635 __identity<deleter_type>, _Dummy>::type>
2636 using _EnableIfDeleterDefaultConstructible =
2637 typename enable_if<is_default_constructible<_Deleter>::value &&
2638 !is_pointer<_Deleter>::value>::type;
2639
2640 template <class _ArgType>
2641 using _EnableIfDeleterConstructible =
2642 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2643
2644 template <class _Pp>
2645 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002646 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002647 >::type;
2648
2649 template <class _UPtr, class _Up,
2650 class _ElemT = typename _UPtr::element_type>
2651 using _EnableIfMoveConvertible = typename enable_if<
2652 is_array<_Up>::value &&
2653 is_same<pointer, element_type*>::value &&
2654 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2655 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2656 >::type;
2657
2658 template <class _UDel>
2659 using _EnableIfDeleterConvertible = typename enable_if<
2660 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2661 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2662 >::type;
2663
2664 template <class _UDel>
2665 using _EnableIfDeleterAssignable = typename enable_if<
2666 is_assignable<_Dp&, _UDel&&>::value
2667 >::type;
2668
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002670 template <bool _Dummy = true,
2671 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2672 _LIBCPP_INLINE_VISIBILITY
2673 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002675 template <bool _Dummy = true,
2676 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2677 _LIBCPP_INLINE_VISIBILITY
2678 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002680 template <class _Pp, bool _Dummy = true,
2681 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2682 class = _EnableIfPointerConvertible<_Pp>>
2683 _LIBCPP_INLINE_VISIBILITY
2684 explicit unique_ptr(_Pp __p) noexcept
2685 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002687 template <class _Pp, bool _Dummy = true,
2688 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2689 class = _EnableIfPointerConvertible<_Pp>>
2690 _LIBCPP_INLINE_VISIBILITY
2691 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2692 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002694 template <bool _Dummy = true,
2695 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2696 _LIBCPP_INLINE_VISIBILITY
2697 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2698 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002700 template <class _Pp, bool _Dummy = true,
2701 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2702 class = _EnableIfPointerConvertible<_Pp>>
2703 _LIBCPP_INLINE_VISIBILITY
2704 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2705 : __ptr_(__p, _VSTD::move(__d)) {
2706 static_assert(!is_reference<deleter_type>::value,
2707 "rvalue deleter bound to reference");
2708 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002710 template <bool _Dummy = true,
2711 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2712 _LIBCPP_INLINE_VISIBILITY
2713 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2714 : __ptr_(nullptr, _VSTD::move(__d)) {
2715 static_assert(!is_reference<deleter_type>::value,
2716 "rvalue deleter bound to reference");
2717 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002718
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002719 template <class _Pp, bool _Dummy = true,
2720 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2721 class = _EnableIfPointerConvertible<_Pp>>
2722 _LIBCPP_INLINE_VISIBILITY
2723 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002724
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002725 _LIBCPP_INLINE_VISIBILITY
2726 unique_ptr(unique_ptr&& __u) noexcept
2727 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2728 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002729
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002730 _LIBCPP_INLINE_VISIBILITY
2731 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2732 reset(__u.release());
2733 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2734 return *this;
2735 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002737 template <class _Up, class _Ep,
2738 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2739 class = _EnableIfDeleterConvertible<_Ep>
2740 >
2741 _LIBCPP_INLINE_VISIBILITY
2742 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002743 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002746 template <class _Up, class _Ep,
2747 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2748 class = _EnableIfDeleterAssignable<_Ep>
2749 >
2750 _LIBCPP_INLINE_VISIBILITY
2751 unique_ptr&
2752 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2753 reset(__u.release());
2754 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2755 return *this;
2756 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002758#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002760 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002761
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002762 unique_ptr(unique_ptr&);
2763 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2764
2765 unique_ptr& operator=(unique_ptr&);
2766 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2767
2768 template <class _Up>
2769 unique_ptr(_Up __u,
2770 typename conditional<
2771 is_reference<deleter_type>::value, deleter_type,
2772 typename add_lvalue_reference<const deleter_type>::type>::type,
2773 typename enable_if<is_convertible<_Up, pointer>::value,
2774 __nat>::type = __nat());
2775public:
2776 _LIBCPP_INLINE_VISIBILITY
2777 unique_ptr() : __ptr_(pointer()) {
2778 static_assert(!is_pointer<deleter_type>::value,
2779 "unique_ptr constructed with null function pointer deleter");
2780 }
2781 _LIBCPP_INLINE_VISIBILITY
2782 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2783 static_assert(!is_pointer<deleter_type>::value,
2784 "unique_ptr constructed with null function pointer deleter");
2785 }
2786
2787 _LIBCPP_INLINE_VISIBILITY
2788 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2789 static_assert(!is_pointer<deleter_type>::value,
2790 "unique_ptr constructed with null function pointer deleter");
2791 }
2792
2793 _LIBCPP_INLINE_VISIBILITY
2794 unique_ptr(pointer __p, deleter_type __d)
2795 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2796
2797 _LIBCPP_INLINE_VISIBILITY
2798 unique_ptr(nullptr_t, deleter_type __d)
2799 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2800
2801 _LIBCPP_INLINE_VISIBILITY
2802 operator __rv<unique_ptr>() {
2803 return __rv<unique_ptr>(*this);
2804 }
2805
2806 _LIBCPP_INLINE_VISIBILITY
2807 unique_ptr(__rv<unique_ptr> __u)
2808 : __ptr_(__u->release(),
2809 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2810
2811 _LIBCPP_INLINE_VISIBILITY
2812 unique_ptr& operator=(__rv<unique_ptr> __u) {
2813 reset(__u->release());
2814 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2815 return *this;
2816 }
2817
2818#endif // _LIBCPP_CXX03_LANG
2819
2820public:
2821 _LIBCPP_INLINE_VISIBILITY
2822 ~unique_ptr() { reset(); }
2823
2824 _LIBCPP_INLINE_VISIBILITY
2825 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2826 reset();
2827 return *this;
2828 }
2829
2830 _LIBCPP_INLINE_VISIBILITY
2831 typename add_lvalue_reference<_Tp>::type
2832 operator[](size_t __i) const {
2833 return __ptr_.first()[__i];
2834 }
2835 _LIBCPP_INLINE_VISIBILITY
2836 pointer get() const _NOEXCEPT {
2837 return __ptr_.first();
2838 }
2839
2840 _LIBCPP_INLINE_VISIBILITY
2841 deleter_type& get_deleter() _NOEXCEPT {
2842 return __ptr_.second();
2843 }
2844
2845 _LIBCPP_INLINE_VISIBILITY
2846 const deleter_type& get_deleter() const _NOEXCEPT {
2847 return __ptr_.second();
2848 }
2849 _LIBCPP_INLINE_VISIBILITY
2850 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2851 return __ptr_.first() != nullptr;
2852 }
2853
2854 _LIBCPP_INLINE_VISIBILITY
2855 pointer release() _NOEXCEPT {
2856 pointer __t = __ptr_.first();
2857 __ptr_.first() = pointer();
2858 return __t;
2859 }
2860
2861 template <class _Pp>
2862 _LIBCPP_INLINE_VISIBILITY
2863 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002864 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002865 >::type
2866 reset(_Pp __p) _NOEXCEPT {
2867 pointer __tmp = __ptr_.first();
2868 __ptr_.first() = __p;
2869 if (__tmp)
2870 __ptr_.second()(__tmp);
2871 }
2872
2873 _LIBCPP_INLINE_VISIBILITY
2874 void reset(nullptr_t = nullptr) _NOEXCEPT {
2875 pointer __tmp = __ptr_.first();
2876 __ptr_.first() = nullptr;
2877 if (__tmp)
2878 __ptr_.second()(__tmp);
2879 }
2880
2881 _LIBCPP_INLINE_VISIBILITY
2882 void swap(unique_ptr& __u) _NOEXCEPT {
2883 __ptr_.swap(__u.__ptr_);
2884 }
2885
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886};
2887
2888template <class _Tp, class _Dp>
2889inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002890typename enable_if<
2891 __is_swappable<_Dp>::value,
2892 void
2893>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002894swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002895
2896template <class _T1, class _D1, class _T2, class _D2>
2897inline _LIBCPP_INLINE_VISIBILITY
2898bool
2899operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2900
2901template <class _T1, class _D1, class _T2, class _D2>
2902inline _LIBCPP_INLINE_VISIBILITY
2903bool
2904operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2905
2906template <class _T1, class _D1, class _T2, class _D2>
2907inline _LIBCPP_INLINE_VISIBILITY
2908bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002909operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2910{
2911 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2912 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002913 typedef typename common_type<_P1, _P2>::type _Vp;
2914 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002915}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916
2917template <class _T1, class _D1, class _T2, class _D2>
2918inline _LIBCPP_INLINE_VISIBILITY
2919bool
2920operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2921
2922template <class _T1, class _D1, class _T2, class _D2>
2923inline _LIBCPP_INLINE_VISIBILITY
2924bool
2925operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2926
2927template <class _T1, class _D1, class _T2, class _D2>
2928inline _LIBCPP_INLINE_VISIBILITY
2929bool
2930operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2931
Howard Hinnantb17caf92012-02-21 21:02:58 +00002932template <class _T1, class _D1>
2933inline _LIBCPP_INLINE_VISIBILITY
2934bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002935operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002936{
2937 return !__x;
2938}
2939
2940template <class _T1, class _D1>
2941inline _LIBCPP_INLINE_VISIBILITY
2942bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002943operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002944{
2945 return !__x;
2946}
2947
2948template <class _T1, class _D1>
2949inline _LIBCPP_INLINE_VISIBILITY
2950bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002951operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002952{
2953 return static_cast<bool>(__x);
2954}
2955
2956template <class _T1, class _D1>
2957inline _LIBCPP_INLINE_VISIBILITY
2958bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002959operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002960{
2961 return static_cast<bool>(__x);
2962}
2963
2964template <class _T1, class _D1>
2965inline _LIBCPP_INLINE_VISIBILITY
2966bool
2967operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2968{
2969 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2970 return less<_P1>()(__x.get(), nullptr);
2971}
2972
2973template <class _T1, class _D1>
2974inline _LIBCPP_INLINE_VISIBILITY
2975bool
2976operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2977{
2978 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2979 return less<_P1>()(nullptr, __x.get());
2980}
2981
2982template <class _T1, class _D1>
2983inline _LIBCPP_INLINE_VISIBILITY
2984bool
2985operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2986{
2987 return nullptr < __x;
2988}
2989
2990template <class _T1, class _D1>
2991inline _LIBCPP_INLINE_VISIBILITY
2992bool
2993operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2994{
2995 return __x < nullptr;
2996}
2997
2998template <class _T1, class _D1>
2999inline _LIBCPP_INLINE_VISIBILITY
3000bool
3001operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3002{
3003 return !(nullptr < __x);
3004}
3005
3006template <class _T1, class _D1>
3007inline _LIBCPP_INLINE_VISIBILITY
3008bool
3009operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3010{
3011 return !(__x < nullptr);
3012}
3013
3014template <class _T1, class _D1>
3015inline _LIBCPP_INLINE_VISIBILITY
3016bool
3017operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3018{
3019 return !(__x < nullptr);
3020}
3021
3022template <class _T1, class _D1>
3023inline _LIBCPP_INLINE_VISIBILITY
3024bool
3025operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3026{
3027 return !(nullptr < __x);
3028}
3029
Howard Hinnant9e028f12012-05-01 15:37:54 +00003030#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3031
3032template <class _Tp, class _Dp>
3033inline _LIBCPP_INLINE_VISIBILITY
3034unique_ptr<_Tp, _Dp>
3035move(unique_ptr<_Tp, _Dp>& __t)
3036{
3037 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3038}
3039
3040#endif
3041
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003042#if _LIBCPP_STD_VER > 11
3043
3044template<class _Tp>
3045struct __unique_if
3046{
3047 typedef unique_ptr<_Tp> __unique_single;
3048};
3049
3050template<class _Tp>
3051struct __unique_if<_Tp[]>
3052{
3053 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3054};
3055
3056template<class _Tp, size_t _Np>
3057struct __unique_if<_Tp[_Np]>
3058{
3059 typedef void __unique_array_known_bound;
3060};
3061
3062template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003063inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003064typename __unique_if<_Tp>::__unique_single
3065make_unique(_Args&&... __args)
3066{
3067 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3068}
3069
3070template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003071inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003072typename __unique_if<_Tp>::__unique_array_unknown_bound
3073make_unique(size_t __n)
3074{
3075 typedef typename remove_extent<_Tp>::type _Up;
3076 return unique_ptr<_Tp>(new _Up[__n]());
3077}
3078
3079template<class _Tp, class... _Args>
3080 typename __unique_if<_Tp>::__unique_array_known_bound
3081 make_unique(_Args&&...) = delete;
3082
3083#endif // _LIBCPP_STD_VER > 11
3084
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003085template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003086#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003087struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003088#else
3089struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3090 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3091#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003092{
3093 typedef unique_ptr<_Tp, _Dp> argument_type;
3094 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003095 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003096 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003097 {
3098 typedef typename argument_type::pointer pointer;
3099 return hash<pointer>()(__ptr.get());
3100 }
3101};
3102
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103struct __destruct_n
3104{
3105private:
3106 size_t size;
3107
3108 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003109 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3111
3112 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003113 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114 {}
3115
Howard Hinnant719bda32011-05-28 14:41:13 +00003116 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003118 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 {}
3120
Howard Hinnant719bda32011-05-28 14:41:13 +00003121 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003123 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124 {}
3125public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003126 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3127 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128
3129 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003130 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003131 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132
3133 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003134 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003135 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136
3137 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003138 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003139 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140};
3141
3142template <class _Alloc>
3143class __allocator_destructor
3144{
3145 typedef allocator_traits<_Alloc> __alloc_traits;
3146public:
3147 typedef typename __alloc_traits::pointer pointer;
3148 typedef typename __alloc_traits::size_type size_type;
3149private:
3150 _Alloc& __alloc_;
3151 size_type __s_;
3152public:
3153 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003154 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003157 void operator()(pointer __p) _NOEXCEPT
3158 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159};
3160
3161template <class _InputIterator, class _ForwardIterator>
3162_ForwardIterator
3163uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3164{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003166#ifndef _LIBCPP_NO_EXCEPTIONS
3167 _ForwardIterator __s = __r;
3168 try
3169 {
3170#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003171 for (; __f != __l; ++__f, (void) ++__r)
3172 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003173#ifndef _LIBCPP_NO_EXCEPTIONS
3174 }
3175 catch (...)
3176 {
3177 for (; __s != __r; ++__s)
3178 __s->~value_type();
3179 throw;
3180 }
3181#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182 return __r;
3183}
3184
3185template <class _InputIterator, class _Size, class _ForwardIterator>
3186_ForwardIterator
3187uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3188{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003190#ifndef _LIBCPP_NO_EXCEPTIONS
3191 _ForwardIterator __s = __r;
3192 try
3193 {
3194#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003195 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3196 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003197#ifndef _LIBCPP_NO_EXCEPTIONS
3198 }
3199 catch (...)
3200 {
3201 for (; __s != __r; ++__s)
3202 __s->~value_type();
3203 throw;
3204 }
3205#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206 return __r;
3207}
3208
3209template <class _ForwardIterator, class _Tp>
3210void
3211uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3212{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003214#ifndef _LIBCPP_NO_EXCEPTIONS
3215 _ForwardIterator __s = __f;
3216 try
3217 {
3218#endif
3219 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003220 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003221#ifndef _LIBCPP_NO_EXCEPTIONS
3222 }
3223 catch (...)
3224 {
3225 for (; __s != __f; ++__s)
3226 __s->~value_type();
3227 throw;
3228 }
3229#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230}
3231
3232template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003233_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3235{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003236 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003237#ifndef _LIBCPP_NO_EXCEPTIONS
3238 _ForwardIterator __s = __f;
3239 try
3240 {
3241#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003242 for (; __n > 0; ++__f, (void) --__n)
3243 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003244#ifndef _LIBCPP_NO_EXCEPTIONS
3245 }
3246 catch (...)
3247 {
3248 for (; __s != __f; ++__s)
3249 __s->~value_type();
3250 throw;
3251 }
3252#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003253 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254}
3255
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003256#if _LIBCPP_STD_VER > 14
3257
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003258template <class _Tp>
3259inline _LIBCPP_INLINE_VISIBILITY
3260void destroy_at(_Tp* __loc) {
3261 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3262 __loc->~_Tp();
3263}
3264
3265template <class _ForwardIterator>
3266inline _LIBCPP_INLINE_VISIBILITY
3267void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3268 for (; __first != __last; ++__first)
3269 _VSTD::destroy_at(_VSTD::addressof(*__first));
3270}
3271
3272template <class _ForwardIterator, class _Size>
3273inline _LIBCPP_INLINE_VISIBILITY
3274_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3275 for (; __n > 0; (void)++__first, --__n)
3276 _VSTD::destroy_at(_VSTD::addressof(*__first));
3277 return __first;
3278}
3279
Eric Fiselier290c07c2016-10-11 21:13:44 +00003280template <class _ForwardIterator>
3281inline _LIBCPP_INLINE_VISIBILITY
3282void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3283 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3284 auto __idx = __first;
3285#ifndef _LIBCPP_NO_EXCEPTIONS
3286 try {
3287#endif
3288 for (; __idx != __last; ++__idx)
3289 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3290#ifndef _LIBCPP_NO_EXCEPTIONS
3291 } catch (...) {
3292 _VSTD::destroy(__first, __idx);
3293 throw;
3294 }
3295#endif
3296}
3297
3298template <class _ForwardIterator, class _Size>
3299inline _LIBCPP_INLINE_VISIBILITY
3300_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3301 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3302 auto __idx = __first;
3303#ifndef _LIBCPP_NO_EXCEPTIONS
3304 try {
3305#endif
3306 for (; __n > 0; (void)++__idx, --__n)
3307 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3308 return __idx;
3309#ifndef _LIBCPP_NO_EXCEPTIONS
3310 } catch (...) {
3311 _VSTD::destroy(__first, __idx);
3312 throw;
3313 }
3314#endif
3315}
3316
3317
3318template <class _ForwardIterator>
3319inline _LIBCPP_INLINE_VISIBILITY
3320void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3321 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3322 auto __idx = __first;
3323#ifndef _LIBCPP_NO_EXCEPTIONS
3324 try {
3325#endif
3326 for (; __idx != __last; ++__idx)
3327 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3328#ifndef _LIBCPP_NO_EXCEPTIONS
3329 } catch (...) {
3330 _VSTD::destroy(__first, __idx);
3331 throw;
3332 }
3333#endif
3334}
3335
3336template <class _ForwardIterator, class _Size>
3337inline _LIBCPP_INLINE_VISIBILITY
3338_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3339 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3340 auto __idx = __first;
3341#ifndef _LIBCPP_NO_EXCEPTIONS
3342 try {
3343#endif
3344 for (; __n > 0; (void)++__idx, --__n)
3345 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3346 return __idx;
3347#ifndef _LIBCPP_NO_EXCEPTIONS
3348 } catch (...) {
3349 _VSTD::destroy(__first, __idx);
3350 throw;
3351 }
3352#endif
3353}
3354
3355
3356template <class _InputIt, class _ForwardIt>
3357inline _LIBCPP_INLINE_VISIBILITY
3358_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3359 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3360 auto __idx = __first_res;
3361#ifndef _LIBCPP_NO_EXCEPTIONS
3362 try {
3363#endif
3364 for (; __first != __last; (void)++__idx, ++__first)
3365 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3366 return __idx;
3367#ifndef _LIBCPP_NO_EXCEPTIONS
3368 } catch (...) {
3369 _VSTD::destroy(__first_res, __idx);
3370 throw;
3371 }
3372#endif
3373}
3374
3375template <class _InputIt, class _Size, class _ForwardIt>
3376inline _LIBCPP_INLINE_VISIBILITY
3377pair<_InputIt, _ForwardIt>
3378uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3379 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3380 auto __idx = __first_res;
3381#ifndef _LIBCPP_NO_EXCEPTIONS
3382 try {
3383#endif
3384 for (; __n > 0; ++__idx, (void)++__first, --__n)
3385 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3386 return {__first, __idx};
3387#ifndef _LIBCPP_NO_EXCEPTIONS
3388 } catch (...) {
3389 _VSTD::destroy(__first_res, __idx);
3390 throw;
3391 }
3392#endif
3393}
3394
3395
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003396#endif // _LIBCPP_STD_VER > 14
3397
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003398// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3399// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003400// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003401#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3402 && defined(__ATOMIC_RELAXED) \
3403 && defined(__ATOMIC_ACQ_REL)
3404# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3405#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3406# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3407#endif
3408
3409template <class _Tp>
3410inline _LIBCPP_INLINE_VISIBILITY _Tp
3411__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3412{
3413#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3414 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3415#else
3416 return __t += 1;
3417#endif
3418}
3419
3420template <class _Tp>
3421inline _LIBCPP_INLINE_VISIBILITY _Tp
3422__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3423{
3424#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3425 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3426#else
3427 return __t -= 1;
3428#endif
3429}
3430
Howard Hinnant756c69b2010-09-22 16:48:34 +00003431class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432 : public std::exception
3433{
3434public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003435 virtual ~bad_weak_ptr() _NOEXCEPT;
3436 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437};
3438
Marshall Clow8fea1612016-08-25 15:09:01 +00003439_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3440void __throw_bad_weak_ptr()
3441{
3442#ifndef _LIBCPP_NO_EXCEPTIONS
3443 throw bad_weak_ptr();
3444#else
3445 _VSTD::abort();
3446#endif
3447}
3448
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003449template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003451class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452{
3453 __shared_count(const __shared_count&);
3454 __shared_count& operator=(const __shared_count&);
3455
3456protected:
3457 long __shared_owners_;
3458 virtual ~__shared_count();
3459private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003460 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461
3462public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003464 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465 : __shared_owners_(__refs) {}
3466
Eric Fiselier98848572017-01-17 03:16:26 +00003467#if defined(_LIBCPP_BUILDING_MEMORY) && \
3468 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003469 void __add_shared() _NOEXCEPT;
3470 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003471#else
3472 _LIBCPP_INLINE_VISIBILITY
3473 void __add_shared() _NOEXCEPT {
3474 __libcpp_atomic_refcount_increment(__shared_owners_);
3475 }
3476 _LIBCPP_INLINE_VISIBILITY
3477 bool __release_shared() _NOEXCEPT {
3478 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3479 __on_zero_shared();
3480 return true;
3481 }
3482 return false;
3483 }
3484#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003485 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003486 long use_count() const _NOEXCEPT {
3487 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3488 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489};
3490
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003491class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492 : private __shared_count
3493{
3494 long __shared_weak_owners_;
3495
3496public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003498 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499 : __shared_count(__refs),
3500 __shared_weak_owners_(__refs) {}
3501protected:
3502 virtual ~__shared_weak_count();
3503
3504public:
Eric Fiselier98848572017-01-17 03:16:26 +00003505#if defined(_LIBCPP_BUILDING_MEMORY) && \
3506 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003507 void __add_shared() _NOEXCEPT;
3508 void __add_weak() _NOEXCEPT;
3509 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003510#else
3511 _LIBCPP_INLINE_VISIBILITY
3512 void __add_shared() _NOEXCEPT {
3513 __shared_count::__add_shared();
3514 }
3515 _LIBCPP_INLINE_VISIBILITY
3516 void __add_weak() _NOEXCEPT {
3517 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3518 }
3519 _LIBCPP_INLINE_VISIBILITY
3520 void __release_shared() _NOEXCEPT {
3521 if (__shared_count::__release_shared())
3522 __release_weak();
3523 }
3524#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003525 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003526 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003527 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3528 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529
Howard Hinnant807d6332013-02-25 15:50:36 +00003530 // Define the function out only if we build static libc++ without RTTI.
3531 // Otherwise we may break clients who need to compile their projects with
3532 // -fno-rtti and yet link against a libc++.dylib compiled
3533 // without -fno-rtti.
3534#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003535 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003536#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003538 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539};
3540
3541template <class _Tp, class _Dp, class _Alloc>
3542class __shared_ptr_pointer
3543 : public __shared_weak_count
3544{
3545 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3546public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003549 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550
Howard Hinnant72f73582010-08-11 17:04:31 +00003551#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003552 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003553#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554
3555private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003556 virtual void __on_zero_shared() _NOEXCEPT;
3557 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558};
3559
Howard Hinnant72f73582010-08-11 17:04:31 +00003560#ifndef _LIBCPP_NO_RTTI
3561
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562template <class _Tp, class _Dp, class _Alloc>
3563const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003564__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003566 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567}
3568
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003569#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003570
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571template <class _Tp, class _Dp, class _Alloc>
3572void
Howard Hinnant719bda32011-05-28 14:41:13 +00003573__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574{
3575 __data_.first().second()(__data_.first().first());
3576 __data_.first().second().~_Dp();
3577}
3578
3579template <class _Tp, class _Dp, class _Alloc>
3580void
Howard Hinnant719bda32011-05-28 14:41:13 +00003581__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003583 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3584 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003585 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3586
Eric Fiselierf8898c82015-02-05 23:01:40 +00003587 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003589 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590}
3591
3592template <class _Tp, class _Alloc>
3593class __shared_ptr_emplace
3594 : public __shared_weak_count
3595{
3596 __compressed_pair<_Alloc, _Tp> __data_;
3597public:
3598#ifndef _LIBCPP_HAS_NO_VARIADICS
3599
Howard Hinnant756c69b2010-09-22 16:48:34 +00003600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003602 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603
3604 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003607 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3608 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609
3610#else // _LIBCPP_HAS_NO_VARIADICS
3611
Howard Hinnant756c69b2010-09-22 16:48:34 +00003612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613 __shared_ptr_emplace(_Alloc __a)
3614 : __data_(__a) {}
3615
3616 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3619 : __data_(__a, _Tp(__a0)) {}
3620
3621 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3624 : __data_(__a, _Tp(__a0, __a1)) {}
3625
3626 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3629 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3630
3631#endif // _LIBCPP_HAS_NO_VARIADICS
3632
3633private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003634 virtual void __on_zero_shared() _NOEXCEPT;
3635 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003638 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639};
3640
3641template <class _Tp, class _Alloc>
3642void
Howard Hinnant719bda32011-05-28 14:41:13 +00003643__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644{
3645 __data_.second().~_Tp();
3646}
3647
3648template <class _Tp, class _Alloc>
3649void
Howard Hinnant719bda32011-05-28 14:41:13 +00003650__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003652 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3653 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003654 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003655 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003657 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658}
3659
Erik Pilkington2a398762017-05-25 15:43:31 +00003660struct __shared_ptr_dummy_rebind_allocator_type;
3661template <>
3662class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3663{
3664public:
3665 template <class _Other>
3666 struct rebind
3667 {
3668 typedef allocator<_Other> other;
3669 };
3670};
3671
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003672template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673
3674template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003675class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003676{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003677public:
3678 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003679
Eric Fiselierae5b6672016-06-27 01:02:43 +00003680#if _LIBCPP_STD_VER > 14
3681 typedef weak_ptr<_Tp> weak_type;
3682#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683private:
3684 element_type* __ptr_;
3685 __shared_weak_count* __cntrl_;
3686
3687 struct __nat {int __for_bool_;};
3688public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003690 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003692 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003693 template<class _Yp>
3694 explicit shared_ptr(_Yp* __p,
3695 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3696 template<class _Yp, class _Dp>
3697 shared_ptr(_Yp* __p, _Dp __d,
3698 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3699 template<class _Yp, class _Dp, class _Alloc>
3700 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3701 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3703 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003704 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003706 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003709 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003710 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003711 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003712#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003714 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003715 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003716 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003717 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003718#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003720 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003721#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003722#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003723 template<class _Yp>
3724 shared_ptr(auto_ptr<_Yp>&& __r,
3725 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003726#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003727 template<class _Yp>
3728 shared_ptr(auto_ptr<_Yp> __r,
3729 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003731#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003732#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003733 template <class _Yp, class _Dp>
3734 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3735 typename enable_if
3736 <
3737 !is_lvalue_reference<_Dp>::value &&
3738 !is_array<_Yp>::value &&
3739 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3740 __nat
3741 >::type = __nat());
3742 template <class _Yp, class _Dp>
3743 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3744 typename enable_if
3745 <
3746 is_lvalue_reference<_Dp>::value &&
3747 !is_array<_Yp>::value &&
3748 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3749 __nat
3750 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003751#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003752 template <class _Yp, class _Dp>
3753 shared_ptr(unique_ptr<_Yp, _Dp>,
3754 typename enable_if
3755 <
3756 !is_lvalue_reference<_Dp>::value &&
3757 !is_array<_Yp>::value &&
3758 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3759 __nat
3760 >::type = __nat());
3761 template <class _Yp, class _Dp>
3762 shared_ptr(unique_ptr<_Yp, _Dp>,
3763 typename enable_if
3764 <
3765 is_lvalue_reference<_Dp>::value &&
3766 !is_array<_Yp>::value &&
3767 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3768 __nat
3769 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003770#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771
3772 ~shared_ptr();
3773
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003775 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003776 template<class _Yp>
3777 typename enable_if
3778 <
3779 is_convertible<_Yp*, element_type*>::value,
3780 shared_ptr&
3781 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003783 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003784#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003786 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003787 template<class _Yp>
3788 typename enable_if
3789 <
3790 is_convertible<_Yp*, element_type*>::value,
3791 shared_ptr<_Tp>&
3792 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003794 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003795#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003796 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003798 typename enable_if
3799 <
3800 !is_array<_Yp>::value &&
3801 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003802 shared_ptr
3803 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003804 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003805#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003806#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003807#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003808 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003810 typename enable_if
3811 <
3812 !is_array<_Yp>::value &&
3813 is_convertible<_Yp*, element_type*>::value,
3814 shared_ptr&
3815 >::type
3816 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003818#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003819 template <class _Yp, class _Dp>
3820 typename enable_if
3821 <
3822 !is_array<_Yp>::value &&
3823 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3824 shared_ptr&
3825 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003826#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003828 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003829#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003831 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832#endif
3833
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003835 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003837 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003838 template<class _Yp>
3839 typename enable_if
3840 <
3841 is_convertible<_Yp*, element_type*>::value,
3842 void
3843 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003845 reset(_Yp* __p);
3846 template<class _Yp, class _Dp>
3847 typename enable_if
3848 <
3849 is_convertible<_Yp*, element_type*>::value,
3850 void
3851 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003853 reset(_Yp* __p, _Dp __d);
3854 template<class _Yp, class _Dp, class _Alloc>
3855 typename enable_if
3856 <
3857 is_convertible<_Yp*, element_type*>::value,
3858 void
3859 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003861 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003862
Howard Hinnant756c69b2010-09-22 16:48:34 +00003863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003864 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003866 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3867 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003869 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003871 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003873 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003875 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003876 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003877 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003878 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003880 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003881 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003882 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003883 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003884 _LIBCPP_INLINE_VISIBILITY
3885 bool
3886 __owner_equivalent(const shared_ptr& __p) const
3887 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888
Howard Hinnant72f73582010-08-11 17:04:31 +00003889#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003890 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003892 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003894#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895
3896#ifndef _LIBCPP_HAS_NO_VARIADICS
3897
3898 template<class ..._Args>
3899 static
3900 shared_ptr<_Tp>
3901 make_shared(_Args&& ...__args);
3902
3903 template<class _Alloc, class ..._Args>
3904 static
3905 shared_ptr<_Tp>
3906 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3907
3908#else // _LIBCPP_HAS_NO_VARIADICS
3909
3910 static shared_ptr<_Tp> make_shared();
3911
3912 template<class _A0>
3913 static shared_ptr<_Tp> make_shared(_A0&);
3914
3915 template<class _A0, class _A1>
3916 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3917
3918 template<class _A0, class _A1, class _A2>
3919 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3920
3921 template<class _Alloc>
3922 static shared_ptr<_Tp>
3923 allocate_shared(const _Alloc& __a);
3924
3925 template<class _Alloc, class _A0>
3926 static shared_ptr<_Tp>
3927 allocate_shared(const _Alloc& __a, _A0& __a0);
3928
3929 template<class _Alloc, class _A0, class _A1>
3930 static shared_ptr<_Tp>
3931 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3932
3933 template<class _Alloc, class _A0, class _A1, class _A2>
3934 static shared_ptr<_Tp>
3935 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3936
3937#endif // _LIBCPP_HAS_NO_VARIADICS
3938
3939private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003940 template <class _Yp, bool = is_function<_Yp>::value>
3941 struct __shared_ptr_default_allocator
3942 {
3943 typedef allocator<_Yp> type;
3944 };
3945
3946 template <class _Yp>
3947 struct __shared_ptr_default_allocator<_Yp, true>
3948 {
3949 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3950 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003952 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003953 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003954 typename enable_if<is_convertible<_OrigPtr*,
3955 const enable_shared_from_this<_Yp>*
3956 >::value,
3957 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003958 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3959 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003961 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003962 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003963 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003964 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3965 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003966 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967 }
3968
Erik Pilkington2a398762017-05-25 15:43:31 +00003969 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003970
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003971 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3972 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973};
3974
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003975
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003977inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003978_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003979shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980 : __ptr_(0),
3981 __cntrl_(0)
3982{
3983}
3984
3985template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003986inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003987_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003988shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989 : __ptr_(0),
3990 __cntrl_(0)
3991{
3992}
3993
3994template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003995template<class _Yp>
3996shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3997 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998 : __ptr_(__p)
3999{
4000 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004001 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4002 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4003 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004005 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006}
4007
4008template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004009template<class _Yp, class _Dp>
4010shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4011 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012 : __ptr_(__p)
4013{
4014#ifndef _LIBCPP_NO_EXCEPTIONS
4015 try
4016 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004017#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004018 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4019 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4020 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004021 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022#ifndef _LIBCPP_NO_EXCEPTIONS
4023 }
4024 catch (...)
4025 {
4026 __d(__p);
4027 throw;
4028 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004029#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030}
4031
4032template<class _Tp>
4033template<class _Dp>
4034shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4035 : __ptr_(0)
4036{
4037#ifndef _LIBCPP_NO_EXCEPTIONS
4038 try
4039 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004040#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004041 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4042 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4043 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044#ifndef _LIBCPP_NO_EXCEPTIONS
4045 }
4046 catch (...)
4047 {
4048 __d(__p);
4049 throw;
4050 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004051#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052}
4053
4054template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004055template<class _Yp, class _Dp, class _Alloc>
4056shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4057 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058 : __ptr_(__p)
4059{
4060#ifndef _LIBCPP_NO_EXCEPTIONS
4061 try
4062 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004063#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004065 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066 typedef __allocator_destructor<_A2> _D2;
4067 _A2 __a2(__a);
4068 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004069 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4070 _CntrlBlk(__p, __d, __a);
4071 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004072 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073#ifndef _LIBCPP_NO_EXCEPTIONS
4074 }
4075 catch (...)
4076 {
4077 __d(__p);
4078 throw;
4079 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004080#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004081}
4082
4083template<class _Tp>
4084template<class _Dp, class _Alloc>
4085shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4086 : __ptr_(0)
4087{
4088#ifndef _LIBCPP_NO_EXCEPTIONS
4089 try
4090 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004091#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004093 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094 typedef __allocator_destructor<_A2> _D2;
4095 _A2 __a2(__a);
4096 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004097 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4098 _CntrlBlk(__p, __d, __a);
4099 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100#ifndef _LIBCPP_NO_EXCEPTIONS
4101 }
4102 catch (...)
4103 {
4104 __d(__p);
4105 throw;
4106 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004107#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108}
4109
4110template<class _Tp>
4111template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004112inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004113shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114 : __ptr_(__p),
4115 __cntrl_(__r.__cntrl_)
4116{
4117 if (__cntrl_)
4118 __cntrl_->__add_shared();
4119}
4120
4121template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004122inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004123shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124 : __ptr_(__r.__ptr_),
4125 __cntrl_(__r.__cntrl_)
4126{
4127 if (__cntrl_)
4128 __cntrl_->__add_shared();
4129}
4130
4131template<class _Tp>
4132template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004133inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004135 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004136 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137 : __ptr_(__r.__ptr_),
4138 __cntrl_(__r.__cntrl_)
4139{
4140 if (__cntrl_)
4141 __cntrl_->__add_shared();
4142}
4143
Howard Hinnant74279a52010-09-04 23:28:19 +00004144#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145
4146template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004147inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004148shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149 : __ptr_(__r.__ptr_),
4150 __cntrl_(__r.__cntrl_)
4151{
4152 __r.__ptr_ = 0;
4153 __r.__cntrl_ = 0;
4154}
4155
4156template<class _Tp>
4157template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004158inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004160 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004161 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162 : __ptr_(__r.__ptr_),
4163 __cntrl_(__r.__cntrl_)
4164{
4165 __r.__ptr_ = 0;
4166 __r.__cntrl_ = 0;
4167}
4168
Howard Hinnant74279a52010-09-04 23:28:19 +00004169#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170
Marshall Clowb22274f2017-01-24 22:22:33 +00004171#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004173template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004174#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004175shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004177shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004179 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180 : __ptr_(__r.get())
4181{
4182 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4183 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004184 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185 __r.release();
4186}
Marshall Clowb22274f2017-01-24 22:22:33 +00004187#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188
4189template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004190template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004191#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4193#else
4194shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4195#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004196 typename enable_if
4197 <
4198 !is_lvalue_reference<_Dp>::value &&
4199 !is_array<_Yp>::value &&
4200 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4201 __nat
4202 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203 : __ptr_(__r.get())
4204{
Marshall Clow35cde742015-05-10 13:59:45 +00004205#if _LIBCPP_STD_VER > 11
4206 if (__ptr_ == nullptr)
4207 __cntrl_ = nullptr;
4208 else
4209#endif
4210 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004211 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4212 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4213 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004214 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004215 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004216 __r.release();
4217}
4218
4219template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004220template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004221#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4223#else
4224shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4225#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004226 typename enable_if
4227 <
4228 is_lvalue_reference<_Dp>::value &&
4229 !is_array<_Yp>::value &&
4230 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4231 __nat
4232 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233 : __ptr_(__r.get())
4234{
Marshall Clow35cde742015-05-10 13:59:45 +00004235#if _LIBCPP_STD_VER > 11
4236 if (__ptr_ == nullptr)
4237 __cntrl_ = nullptr;
4238 else
4239#endif
4240 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004241 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004242 typedef __shared_ptr_pointer<_Yp*,
4243 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004244 _AllocT > _CntrlBlk;
4245 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004246 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004247 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248 __r.release();
4249}
4250
4251#ifndef _LIBCPP_HAS_NO_VARIADICS
4252
4253template<class _Tp>
4254template<class ..._Args>
4255shared_ptr<_Tp>
4256shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4257{
4258 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4259 typedef allocator<_CntrlBlk> _A2;
4260 typedef __allocator_destructor<_A2> _D2;
4261 _A2 __a2;
4262 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004263 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264 shared_ptr<_Tp> __r;
4265 __r.__ptr_ = __hold2.get()->get();
4266 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004267 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268 return __r;
4269}
4270
4271template<class _Tp>
4272template<class _Alloc, class ..._Args>
4273shared_ptr<_Tp>
4274shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4275{
4276 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004277 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 typedef __allocator_destructor<_A2> _D2;
4279 _A2 __a2(__a);
4280 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004281 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4282 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283 shared_ptr<_Tp> __r;
4284 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004285 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004286 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287 return __r;
4288}
4289
4290#else // _LIBCPP_HAS_NO_VARIADICS
4291
4292template<class _Tp>
4293shared_ptr<_Tp>
4294shared_ptr<_Tp>::make_shared()
4295{
4296 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4297 typedef allocator<_CntrlBlk> _Alloc2;
4298 typedef __allocator_destructor<_Alloc2> _D2;
4299 _Alloc2 __alloc2;
4300 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4301 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4302 shared_ptr<_Tp> __r;
4303 __r.__ptr_ = __hold2.get()->get();
4304 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004305 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306 return __r;
4307}
4308
4309template<class _Tp>
4310template<class _A0>
4311shared_ptr<_Tp>
4312shared_ptr<_Tp>::make_shared(_A0& __a0)
4313{
4314 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4315 typedef allocator<_CntrlBlk> _Alloc2;
4316 typedef __allocator_destructor<_Alloc2> _D2;
4317 _Alloc2 __alloc2;
4318 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4319 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4320 shared_ptr<_Tp> __r;
4321 __r.__ptr_ = __hold2.get()->get();
4322 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004323 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324 return __r;
4325}
4326
4327template<class _Tp>
4328template<class _A0, class _A1>
4329shared_ptr<_Tp>
4330shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4331{
4332 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4333 typedef allocator<_CntrlBlk> _Alloc2;
4334 typedef __allocator_destructor<_Alloc2> _D2;
4335 _Alloc2 __alloc2;
4336 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4337 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4338 shared_ptr<_Tp> __r;
4339 __r.__ptr_ = __hold2.get()->get();
4340 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004341 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342 return __r;
4343}
4344
4345template<class _Tp>
4346template<class _A0, class _A1, class _A2>
4347shared_ptr<_Tp>
4348shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4349{
4350 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4351 typedef allocator<_CntrlBlk> _Alloc2;
4352 typedef __allocator_destructor<_Alloc2> _D2;
4353 _Alloc2 __alloc2;
4354 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4355 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4356 shared_ptr<_Tp> __r;
4357 __r.__ptr_ = __hold2.get()->get();
4358 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004359 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360 return __r;
4361}
4362
4363template<class _Tp>
4364template<class _Alloc>
4365shared_ptr<_Tp>
4366shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4367{
4368 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004369 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004370 typedef __allocator_destructor<_Alloc2> _D2;
4371 _Alloc2 __alloc2(__a);
4372 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004373 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4374 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004375 shared_ptr<_Tp> __r;
4376 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004377 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004378 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379 return __r;
4380}
4381
4382template<class _Tp>
4383template<class _Alloc, class _A0>
4384shared_ptr<_Tp>
4385shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4386{
4387 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004388 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004389 typedef __allocator_destructor<_Alloc2> _D2;
4390 _Alloc2 __alloc2(__a);
4391 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004392 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4393 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394 shared_ptr<_Tp> __r;
4395 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004396 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004397 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398 return __r;
4399}
4400
4401template<class _Tp>
4402template<class _Alloc, class _A0, class _A1>
4403shared_ptr<_Tp>
4404shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4405{
4406 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004407 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004408 typedef __allocator_destructor<_Alloc2> _D2;
4409 _Alloc2 __alloc2(__a);
4410 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004411 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4412 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413 shared_ptr<_Tp> __r;
4414 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004415 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004416 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417 return __r;
4418}
4419
4420template<class _Tp>
4421template<class _Alloc, class _A0, class _A1, class _A2>
4422shared_ptr<_Tp>
4423shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4424{
4425 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004426 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004427 typedef __allocator_destructor<_Alloc2> _D2;
4428 _Alloc2 __alloc2(__a);
4429 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004430 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4431 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432 shared_ptr<_Tp> __r;
4433 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004434 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004435 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436 return __r;
4437}
4438
4439#endif // _LIBCPP_HAS_NO_VARIADICS
4440
4441template<class _Tp>
4442shared_ptr<_Tp>::~shared_ptr()
4443{
4444 if (__cntrl_)
4445 __cntrl_->__release_shared();
4446}
4447
4448template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004449inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004451shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452{
4453 shared_ptr(__r).swap(*this);
4454 return *this;
4455}
4456
4457template<class _Tp>
4458template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004459inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004460typename enable_if
4461<
Marshall Clow7e384b72017-01-10 16:59:33 +00004462 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004463 shared_ptr<_Tp>&
4464>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004465shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466{
4467 shared_ptr(__r).swap(*this);
4468 return *this;
4469}
4470
Howard Hinnant74279a52010-09-04 23:28:19 +00004471#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004472
4473template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004474inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004476shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004477{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004478 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479 return *this;
4480}
4481
4482template<class _Tp>
4483template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004484inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004485typename enable_if
4486<
Marshall Clow7e384b72017-01-10 16:59:33 +00004487 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004488 shared_ptr<_Tp>&
4489>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4491{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004492 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004493 return *this;
4494}
4495
Marshall Clowb22274f2017-01-24 22:22:33 +00004496#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497template<class _Tp>
4498template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004499inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004500typename enable_if
4501<
4502 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004503 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004504 shared_ptr<_Tp>
4505>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004506shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4507{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004508 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509 return *this;
4510}
Marshall Clowb22274f2017-01-24 22:22:33 +00004511#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004512
4513template<class _Tp>
4514template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004515inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004516typename enable_if
4517<
4518 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004519 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4520 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004521 shared_ptr<_Tp>&
4522>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4524{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004525 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004526 return *this;
4527}
4528
Howard Hinnant74279a52010-09-04 23:28:19 +00004529#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530
Marshall Clowb22274f2017-01-24 22:22:33 +00004531#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004532template<class _Tp>
4533template<class _Yp>
4534inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004535typename enable_if
4536<
4537 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004538 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004539 shared_ptr<_Tp>&
4540>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004541shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004542{
4543 shared_ptr(__r).swap(*this);
4544 return *this;
4545}
Marshall Clowb22274f2017-01-24 22:22:33 +00004546#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004547
4548template<class _Tp>
4549template <class _Yp, class _Dp>
4550inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004551typename enable_if
4552<
4553 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004554 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4555 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004556 shared_ptr<_Tp>&
4557>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004558shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4559{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004560 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004561 return *this;
4562}
4563
Howard Hinnant74279a52010-09-04 23:28:19 +00004564#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565
4566template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004567inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568void
Howard Hinnant719bda32011-05-28 14:41:13 +00004569shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004570{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004571 _VSTD::swap(__ptr_, __r.__ptr_);
4572 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573}
4574
4575template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004576inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577void
Howard Hinnant719bda32011-05-28 14:41:13 +00004578shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004579{
4580 shared_ptr().swap(*this);
4581}
4582
4583template<class _Tp>
4584template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004585inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004586typename enable_if
4587<
Marshall Clow7e384b72017-01-10 16:59:33 +00004588 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004589 void
4590>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004591shared_ptr<_Tp>::reset(_Yp* __p)
4592{
4593 shared_ptr(__p).swap(*this);
4594}
4595
4596template<class _Tp>
4597template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004598inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004599typename enable_if
4600<
Marshall Clow7e384b72017-01-10 16:59:33 +00004601 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004602 void
4603>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004604shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4605{
4606 shared_ptr(__p, __d).swap(*this);
4607}
4608
4609template<class _Tp>
4610template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004611inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004612typename enable_if
4613<
Marshall Clow7e384b72017-01-10 16:59:33 +00004614 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004615 void
4616>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004617shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4618{
4619 shared_ptr(__p, __d, __a).swap(*this);
4620}
4621
4622#ifndef _LIBCPP_HAS_NO_VARIADICS
4623
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004624template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004625inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004626typename enable_if
4627<
4628 !is_array<_Tp>::value,
4629 shared_ptr<_Tp>
4630>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004631make_shared(_Args&& ...__args)
4632{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004633 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004634}
4635
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004636template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004637inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004638typename enable_if
4639<
4640 !is_array<_Tp>::value,
4641 shared_ptr<_Tp>
4642>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004643allocate_shared(const _Alloc& __a, _Args&& ...__args)
4644{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004645 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004646}
4647
4648#else // _LIBCPP_HAS_NO_VARIADICS
4649
4650template<class _Tp>
4651inline _LIBCPP_INLINE_VISIBILITY
4652shared_ptr<_Tp>
4653make_shared()
4654{
4655 return shared_ptr<_Tp>::make_shared();
4656}
4657
4658template<class _Tp, class _A0>
4659inline _LIBCPP_INLINE_VISIBILITY
4660shared_ptr<_Tp>
4661make_shared(_A0& __a0)
4662{
4663 return shared_ptr<_Tp>::make_shared(__a0);
4664}
4665
4666template<class _Tp, class _A0, class _A1>
4667inline _LIBCPP_INLINE_VISIBILITY
4668shared_ptr<_Tp>
4669make_shared(_A0& __a0, _A1& __a1)
4670{
4671 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4672}
4673
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004674template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004675inline _LIBCPP_INLINE_VISIBILITY
4676shared_ptr<_Tp>
4677make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4678{
4679 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4680}
4681
4682template<class _Tp, class _Alloc>
4683inline _LIBCPP_INLINE_VISIBILITY
4684shared_ptr<_Tp>
4685allocate_shared(const _Alloc& __a)
4686{
4687 return shared_ptr<_Tp>::allocate_shared(__a);
4688}
4689
4690template<class _Tp, class _Alloc, class _A0>
4691inline _LIBCPP_INLINE_VISIBILITY
4692shared_ptr<_Tp>
4693allocate_shared(const _Alloc& __a, _A0& __a0)
4694{
4695 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4696}
4697
4698template<class _Tp, class _Alloc, class _A0, class _A1>
4699inline _LIBCPP_INLINE_VISIBILITY
4700shared_ptr<_Tp>
4701allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4702{
4703 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4704}
4705
4706template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4707inline _LIBCPP_INLINE_VISIBILITY
4708shared_ptr<_Tp>
4709allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4710{
4711 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4712}
4713
4714#endif // _LIBCPP_HAS_NO_VARIADICS
4715
4716template<class _Tp, class _Up>
4717inline _LIBCPP_INLINE_VISIBILITY
4718bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004719operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004720{
4721 return __x.get() == __y.get();
4722}
4723
4724template<class _Tp, class _Up>
4725inline _LIBCPP_INLINE_VISIBILITY
4726bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004727operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004728{
4729 return !(__x == __y);
4730}
4731
4732template<class _Tp, class _Up>
4733inline _LIBCPP_INLINE_VISIBILITY
4734bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004735operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004736{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004737 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4738 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004739}
4740
4741template<class _Tp, class _Up>
4742inline _LIBCPP_INLINE_VISIBILITY
4743bool
4744operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4745{
4746 return __y < __x;
4747}
4748
4749template<class _Tp, class _Up>
4750inline _LIBCPP_INLINE_VISIBILITY
4751bool
4752operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4753{
4754 return !(__y < __x);
4755}
4756
4757template<class _Tp, class _Up>
4758inline _LIBCPP_INLINE_VISIBILITY
4759bool
4760operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4761{
4762 return !(__x < __y);
4763}
4764
4765template<class _Tp>
4766inline _LIBCPP_INLINE_VISIBILITY
4767bool
4768operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4769{
4770 return !__x;
4771}
4772
4773template<class _Tp>
4774inline _LIBCPP_INLINE_VISIBILITY
4775bool
4776operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4777{
4778 return !__x;
4779}
4780
4781template<class _Tp>
4782inline _LIBCPP_INLINE_VISIBILITY
4783bool
4784operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4785{
4786 return static_cast<bool>(__x);
4787}
4788
4789template<class _Tp>
4790inline _LIBCPP_INLINE_VISIBILITY
4791bool
4792operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4793{
4794 return static_cast<bool>(__x);
4795}
4796
4797template<class _Tp>
4798inline _LIBCPP_INLINE_VISIBILITY
4799bool
4800operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4801{
4802 return less<_Tp*>()(__x.get(), nullptr);
4803}
4804
4805template<class _Tp>
4806inline _LIBCPP_INLINE_VISIBILITY
4807bool
4808operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4809{
4810 return less<_Tp*>()(nullptr, __x.get());
4811}
4812
4813template<class _Tp>
4814inline _LIBCPP_INLINE_VISIBILITY
4815bool
4816operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4817{
4818 return nullptr < __x;
4819}
4820
4821template<class _Tp>
4822inline _LIBCPP_INLINE_VISIBILITY
4823bool
4824operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4825{
4826 return __x < nullptr;
4827}
4828
4829template<class _Tp>
4830inline _LIBCPP_INLINE_VISIBILITY
4831bool
4832operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4833{
4834 return !(nullptr < __x);
4835}
4836
4837template<class _Tp>
4838inline _LIBCPP_INLINE_VISIBILITY
4839bool
4840operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4841{
4842 return !(__x < nullptr);
4843}
4844
4845template<class _Tp>
4846inline _LIBCPP_INLINE_VISIBILITY
4847bool
4848operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4849{
4850 return !(__x < nullptr);
4851}
4852
4853template<class _Tp>
4854inline _LIBCPP_INLINE_VISIBILITY
4855bool
4856operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4857{
4858 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004859}
4860
4861template<class _Tp>
4862inline _LIBCPP_INLINE_VISIBILITY
4863void
Howard Hinnant719bda32011-05-28 14:41:13 +00004864swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004865{
4866 __x.swap(__y);
4867}
4868
4869template<class _Tp, class _Up>
4870inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004871typename enable_if
4872<
4873 !is_array<_Tp>::value && !is_array<_Up>::value,
4874 shared_ptr<_Tp>
4875>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004876static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004877{
4878 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4879}
4880
4881template<class _Tp, class _Up>
4882inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004883typename enable_if
4884<
4885 !is_array<_Tp>::value && !is_array<_Up>::value,
4886 shared_ptr<_Tp>
4887>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004888dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004889{
4890 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4891 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4892}
4893
4894template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004895typename enable_if
4896<
4897 is_array<_Tp>::value == is_array<_Up>::value,
4898 shared_ptr<_Tp>
4899>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004900const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004901{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004902 typedef typename remove_extent<_Tp>::type _RTp;
4903 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004904}
4905
Howard Hinnant72f73582010-08-11 17:04:31 +00004906#ifndef _LIBCPP_NO_RTTI
4907
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908template<class _Dp, class _Tp>
4909inline _LIBCPP_INLINE_VISIBILITY
4910_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004911get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004912{
4913 return __p.template __get_deleter<_Dp>();
4914}
4915
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004916#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004917
Howard Hinnantc51e1022010-05-11 19:42:16 +00004918template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004919class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004920{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004921public:
4922 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004923private:
4924 element_type* __ptr_;
4925 __shared_weak_count* __cntrl_;
4926
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004927public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004929 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004930 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004931 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4932 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004934 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004935 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004936 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4937 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004938
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004939#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004941 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004942 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004943 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4944 _NOEXCEPT;
4945#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004946 ~weak_ptr();
4947
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004949 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004950 template<class _Yp>
4951 typename enable_if
4952 <
4953 is_convertible<_Yp*, element_type*>::value,
4954 weak_ptr&
4955 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004957 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4958
4959#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4960
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004962 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4963 template<class _Yp>
4964 typename enable_if
4965 <
4966 is_convertible<_Yp*, element_type*>::value,
4967 weak_ptr&
4968 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004970 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4971
4972#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4973
4974 template<class _Yp>
4975 typename enable_if
4976 <
4977 is_convertible<_Yp*, element_type*>::value,
4978 weak_ptr&
4979 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004981 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004982
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004984 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004986 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004987
Howard Hinnant756c69b2010-09-22 16:48:34 +00004988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004989 long use_count() const _NOEXCEPT
4990 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004992 bool expired() const _NOEXCEPT
4993 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4994 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004995 template<class _Up>
4996 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004997 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004998 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004999 template<class _Up>
5000 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005001 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005002 {return __cntrl_ < __r.__cntrl_;}
5003
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005004 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5005 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005006};
5007
5008template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005009inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005010_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005011weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012 : __ptr_(0),
5013 __cntrl_(0)
5014{
5015}
5016
5017template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005018inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005019weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005020 : __ptr_(__r.__ptr_),
5021 __cntrl_(__r.__cntrl_)
5022{
5023 if (__cntrl_)
5024 __cntrl_->__add_weak();
5025}
5026
5027template<class _Tp>
5028template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005029inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005030weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005031 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005032 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005033 : __ptr_(__r.__ptr_),
5034 __cntrl_(__r.__cntrl_)
5035{
5036 if (__cntrl_)
5037 __cntrl_->__add_weak();
5038}
5039
5040template<class _Tp>
5041template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005042inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005043weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005044 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005045 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005046 : __ptr_(__r.__ptr_),
5047 __cntrl_(__r.__cntrl_)
5048{
5049 if (__cntrl_)
5050 __cntrl_->__add_weak();
5051}
5052
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005053#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5054
5055template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005056inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005057weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5058 : __ptr_(__r.__ptr_),
5059 __cntrl_(__r.__cntrl_)
5060{
5061 __r.__ptr_ = 0;
5062 __r.__cntrl_ = 0;
5063}
5064
5065template<class _Tp>
5066template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005067inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005068weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5069 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5070 _NOEXCEPT
5071 : __ptr_(__r.__ptr_),
5072 __cntrl_(__r.__cntrl_)
5073{
5074 __r.__ptr_ = 0;
5075 __r.__cntrl_ = 0;
5076}
5077
5078#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5079
Howard Hinnantc51e1022010-05-11 19:42:16 +00005080template<class _Tp>
5081weak_ptr<_Tp>::~weak_ptr()
5082{
5083 if (__cntrl_)
5084 __cntrl_->__release_weak();
5085}
5086
5087template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005088inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005089weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005090weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005091{
5092 weak_ptr(__r).swap(*this);
5093 return *this;
5094}
5095
5096template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005097template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005098inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005099typename enable_if
5100<
5101 is_convertible<_Yp*, _Tp*>::value,
5102 weak_ptr<_Tp>&
5103>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005104weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005105{
5106 weak_ptr(__r).swap(*this);
5107 return *this;
5108}
5109
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005110#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5111
5112template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005113inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005114weak_ptr<_Tp>&
5115weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5116{
5117 weak_ptr(_VSTD::move(__r)).swap(*this);
5118 return *this;
5119}
5120
Howard Hinnantc51e1022010-05-11 19:42:16 +00005121template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005122template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005123inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005124typename enable_if
5125<
5126 is_convertible<_Yp*, _Tp*>::value,
5127 weak_ptr<_Tp>&
5128>::type
5129weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5130{
5131 weak_ptr(_VSTD::move(__r)).swap(*this);
5132 return *this;
5133}
5134
5135#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5136
5137template<class _Tp>
5138template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005139inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005140typename enable_if
5141<
5142 is_convertible<_Yp*, _Tp*>::value,
5143 weak_ptr<_Tp>&
5144>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005145weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005146{
5147 weak_ptr(__r).swap(*this);
5148 return *this;
5149}
5150
5151template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005152inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005153void
Howard Hinnant719bda32011-05-28 14:41:13 +00005154weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005155{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005156 _VSTD::swap(__ptr_, __r.__ptr_);
5157 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005158}
5159
5160template<class _Tp>
5161inline _LIBCPP_INLINE_VISIBILITY
5162void
Howard Hinnant719bda32011-05-28 14:41:13 +00005163swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005164{
5165 __x.swap(__y);
5166}
5167
5168template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005169inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005170void
Howard Hinnant719bda32011-05-28 14:41:13 +00005171weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005172{
5173 weak_ptr().swap(*this);
5174}
5175
5176template<class _Tp>
5177template<class _Yp>
5178shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005179 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005180 : __ptr_(__r.__ptr_),
5181 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5182{
5183 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005184 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005185}
5186
5187template<class _Tp>
5188shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005189weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005190{
5191 shared_ptr<_Tp> __r;
5192 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5193 if (__r.__cntrl_)
5194 __r.__ptr_ = __ptr_;
5195 return __r;
5196}
5197
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005198#if _LIBCPP_STD_VER > 14
5199template <class _Tp = void> struct owner_less;
5200#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005201template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005202#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005203
5204template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005205struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005206 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005207{
5208 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005209 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005210 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005211 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005212 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005213 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005214 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005215 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005216 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005217 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005218};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005219
5220template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005221struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005222 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5223{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005224 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005225 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005226 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005228 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005229 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005230 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005231 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005232 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005233 {return __x.owner_before(__y);}
5234};
5235
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005236#if _LIBCPP_STD_VER > 14
5237template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005238struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005239{
5240 template <class _Tp, class _Up>
5241 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005242 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005243 {return __x.owner_before(__y);}
5244 template <class _Tp, class _Up>
5245 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005246 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005247 {return __x.owner_before(__y);}
5248 template <class _Tp, class _Up>
5249 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005250 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005251 {return __x.owner_before(__y);}
5252 template <class _Tp, class _Up>
5253 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005254 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005255 {return __x.owner_before(__y);}
5256 typedef void is_transparent;
5257};
5258#endif
5259
Howard Hinnantc51e1022010-05-11 19:42:16 +00005260template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005261class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005262{
5263 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005264protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005266 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005268 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005270 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5271 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005273 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005274public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005276 shared_ptr<_Tp> shared_from_this()
5277 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005279 shared_ptr<_Tp const> shared_from_this() const
5280 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005281
Eric Fiselier84006862016-06-02 00:15:35 +00005282#if _LIBCPP_STD_VER > 14
5283 _LIBCPP_INLINE_VISIBILITY
5284 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5285 { return __weak_this_; }
5286
5287 _LIBCPP_INLINE_VISIBILITY
5288 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5289 { return __weak_this_; }
5290#endif // _LIBCPP_STD_VER > 14
5291
Howard Hinnantc51e1022010-05-11 19:42:16 +00005292 template <class _Up> friend class shared_ptr;
5293};
5294
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005295template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005296struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005297{
5298 typedef shared_ptr<_Tp> argument_type;
5299 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005300
Howard Hinnant756c69b2010-09-22 16:48:34 +00005301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005302 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005303 {
5304 return hash<_Tp*>()(__ptr.get());
5305 }
5306};
5307
Howard Hinnantc834c512011-11-29 18:15:50 +00005308template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005309inline _LIBCPP_INLINE_VISIBILITY
5310basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005311operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005312
Eric Fiselier9b492672016-06-18 02:12:53 +00005313
5314#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005315
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005316class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005317{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005318 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005319public:
5320 void lock() _NOEXCEPT;
5321 void unlock() _NOEXCEPT;
5322
5323private:
5324 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5325 __sp_mut(const __sp_mut&);
5326 __sp_mut& operator=(const __sp_mut&);
5327
Howard Hinnant8331b762013-03-06 23:30:19 +00005328 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005329};
5330
Mehdi Amini228053d2017-05-04 17:08:54 +00005331_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5332__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005333
5334template <class _Tp>
5335inline _LIBCPP_INLINE_VISIBILITY
5336bool
5337atomic_is_lock_free(const shared_ptr<_Tp>*)
5338{
5339 return false;
5340}
5341
5342template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005343_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005344shared_ptr<_Tp>
5345atomic_load(const shared_ptr<_Tp>* __p)
5346{
5347 __sp_mut& __m = __get_sp_mut(__p);
5348 __m.lock();
5349 shared_ptr<_Tp> __q = *__p;
5350 __m.unlock();
5351 return __q;
5352}
5353
5354template <class _Tp>
5355inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005356_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005357shared_ptr<_Tp>
5358atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5359{
5360 return atomic_load(__p);
5361}
5362
5363template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005364_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005365void
5366atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5367{
5368 __sp_mut& __m = __get_sp_mut(__p);
5369 __m.lock();
5370 __p->swap(__r);
5371 __m.unlock();
5372}
5373
5374template <class _Tp>
5375inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005376_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005377void
5378atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5379{
5380 atomic_store(__p, __r);
5381}
5382
5383template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005384_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005385shared_ptr<_Tp>
5386atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5387{
5388 __sp_mut& __m = __get_sp_mut(__p);
5389 __m.lock();
5390 __p->swap(__r);
5391 __m.unlock();
5392 return __r;
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 +00005398shared_ptr<_Tp>
5399atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5400{
5401 return atomic_exchange(__p, __r);
5402}
5403
5404template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005405_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005406bool
5407atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5408{
Marshall Clow4201ee82016-05-18 17:50:13 +00005409 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005410 __sp_mut& __m = __get_sp_mut(__p);
5411 __m.lock();
5412 if (__p->__owner_equivalent(*__v))
5413 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005414 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005415 *__p = __w;
5416 __m.unlock();
5417 return true;
5418 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005419 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005420 *__v = *__p;
5421 __m.unlock();
5422 return false;
5423}
5424
5425template <class _Tp>
5426inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005427_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005428bool
5429atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5430{
5431 return atomic_compare_exchange_strong(__p, __v, __w);
5432}
5433
5434template <class _Tp>
5435inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005436_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005437bool
5438atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5439 shared_ptr<_Tp> __w, memory_order, memory_order)
5440{
5441 return atomic_compare_exchange_strong(__p, __v, __w);
5442}
5443
5444template <class _Tp>
5445inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005446_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005447bool
5448atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5449 shared_ptr<_Tp> __w, memory_order, memory_order)
5450{
5451 return atomic_compare_exchange_weak(__p, __v, __w);
5452}
5453
Eric Fiselier9b492672016-06-18 02:12:53 +00005454#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005455
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005456//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005457#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5458# ifndef _LIBCPP_CXX03_LANG
5459enum class pointer_safety : unsigned char {
5460 relaxed,
5461 preferred,
5462 strict
5463};
5464# endif
5465#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005466struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005467{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005468 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005469 {
5470 relaxed,
5471 preferred,
5472 strict
5473 };
5474
Howard Hinnant49e145e2012-10-30 19:06:59 +00005475 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005476
Howard Hinnant756c69b2010-09-22 16:48:34 +00005477 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005478 pointer_safety() : __v_() {}
5479
5480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005481 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005483 operator int() const {return __v_;}
5484};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005485#endif
5486
5487#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5488 defined(_LIBCPP_BUILDING_MEMORY)
5489_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5490#else
5491// This function is only offered in C++03 under ABI v1.
5492# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5493inline _LIBCPP_INLINE_VISIBILITY
5494pointer_safety get_pointer_safety() _NOEXCEPT {
5495 return pointer_safety::relaxed;
5496}
5497# endif
5498#endif
5499
Howard Hinnantc51e1022010-05-11 19:42:16 +00005500
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005501_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5502_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5503_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005504_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005505
5506template <class _Tp>
5507inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005508_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005509undeclare_reachable(_Tp* __p)
5510{
5511 return static_cast<_Tp*>(__undeclare_reachable(__p));
5512}
5513
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005514_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005515
Marshall Clow8982dcd2015-07-13 20:04:56 +00005516// --- Helper for container swap --
5517template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005518inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005519void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5520#if _LIBCPP_STD_VER >= 14
5521 _NOEXCEPT
5522#else
5523 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5524#endif
5525{
5526 __swap_allocator(__a1, __a2,
5527 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5528}
5529
5530template <typename _Alloc>
5531_LIBCPP_INLINE_VISIBILITY
5532void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5533#if _LIBCPP_STD_VER >= 14
5534 _NOEXCEPT
5535#else
5536 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5537#endif
5538{
5539 using _VSTD::swap;
5540 swap(__a1, __a2);
5541}
5542
5543template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005544inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005545void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5546
Marshall Clowff91de82015-08-18 19:51:37 +00005547template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005548struct __noexcept_move_assign_container : public integral_constant<bool,
5549 _Traits::propagate_on_container_move_assignment::value
5550#if _LIBCPP_STD_VER > 14
5551 || _Traits::is_always_equal::value
5552#else
5553 && is_nothrow_move_assignable<_Alloc>::value
5554#endif
5555 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005556
Marshall Clowa591b9a2016-07-11 21:38:08 +00005557
5558#ifndef _LIBCPP_HAS_NO_VARIADICS
5559template <class _Tp, class _Alloc>
5560struct __temp_value {
5561 typedef allocator_traits<_Alloc> _Traits;
5562
5563 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5564 _Alloc &__a;
5565
5566 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5567 _Tp & get() { return *__addr(); }
5568
5569 template<class... _Args>
5570 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5571 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5572
5573 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5574 };
5575#endif
5576
Howard Hinnantc51e1022010-05-11 19:42:16 +00005577_LIBCPP_END_NAMESPACE_STD
5578
Eric Fiselierf4433a32017-05-31 22:07:49 +00005579_LIBCPP_POP_MACROS
5580
Howard Hinnantc51e1022010-05-11 19:42:16 +00005581#endif // _LIBCPP_MEMORY