blob: 913bc85c8565fcbbed1c50997b48ad6fc729288d [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
Eric Fiselier45c9aac2017-11-22 19:49:21 +000049template <class T> constexpr T* to_address(T* p) noexcept; // C++20
50template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
51
Howard Hinnantc51e1022010-05-11 19:42:16 +000052template <class Alloc>
53struct allocator_traits
54{
55 typedef Alloc allocator_type;
56 typedef typename allocator_type::value_type
57 value_type;
58
59 typedef Alloc::pointer | value_type* pointer;
60 typedef Alloc::const_pointer
61 | pointer_traits<pointer>::rebind<const value_type>
62 const_pointer;
63 typedef Alloc::void_pointer
64 | pointer_traits<pointer>::rebind<void>
65 void_pointer;
66 typedef Alloc::const_void_pointer
67 | pointer_traits<pointer>::rebind<const void>
68 const_void_pointer;
69 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000070 | pointer_traits<pointer>::difference_type
71 difference_type;
72 typedef Alloc::size_type
73 | make_unsigned<difference_type>::type
74 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075 typedef Alloc::propagate_on_container_copy_assignment
76 | false_type propagate_on_container_copy_assignment;
77 typedef Alloc::propagate_on_container_move_assignment
78 | false_type propagate_on_container_move_assignment;
79 typedef Alloc::propagate_on_container_swap
80 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000081 typedef Alloc::is_always_equal
82 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083
84 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
85 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
86
87 static pointer allocate(allocator_type& a, size_type n);
88 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
89
Howard Hinnant719bda32011-05-28 14:41:13 +000090 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000091
92 template <class T, class... Args>
93 static void construct(allocator_type& a, T* p, Args&&... args);
94
95 template <class T>
96 static void destroy(allocator_type& a, T* p);
97
Marshall Clow4f834b52013-08-27 20:22:15 +000098 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000099
100 static allocator_type
101 select_on_container_copy_construction(const allocator_type& a);
102};
103
104template <>
105class allocator<void>
106{
107public:
108 typedef void* pointer;
109 typedef const void* const_pointer;
110 typedef void value_type;
111
112 template <class _Up> struct rebind {typedef allocator<_Up> other;};
113};
114
115template <class T>
116class allocator
117{
118public:
119 typedef size_t size_type;
120 typedef ptrdiff_t difference_type;
121 typedef T* pointer;
122 typedef const T* const_pointer;
123 typedef typename add_lvalue_reference<T>::type reference;
124 typedef typename add_lvalue_reference<const T>::type const_reference;
125 typedef T value_type;
126
127 template <class U> struct rebind {typedef allocator<U> other;};
128
Howard Hinnant719bda32011-05-28 14:41:13 +0000129 allocator() noexcept;
130 allocator(const allocator&) noexcept;
131 template <class U> allocator(const allocator<U>&) noexcept;
132 ~allocator();
133 pointer address(reference x) const noexcept;
134 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000136 void deallocate(pointer p, size_type n) noexcept;
137 size_type max_size() const noexcept;
138 template<class U, class... Args>
139 void construct(U* p, Args&&... args);
140 template <class U>
141 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142};
143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000148bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149
150template <class OutputIterator, class T>
151class raw_storage_iterator
152 : public iterator<output_iterator_tag,
153 T, // purposefully not C++03
154 ptrdiff_t, // purposefully not C++03
155 T*, // purposefully not C++03
156 raw_storage_iterator&> // purposefully not C++03
157{
158public:
159 explicit raw_storage_iterator(OutputIterator x);
160 raw_storage_iterator& operator*();
161 raw_storage_iterator& operator=(const T& element);
162 raw_storage_iterator& operator++();
163 raw_storage_iterator operator++(int);
164};
165
Howard Hinnant719bda32011-05-28 14:41:13 +0000166template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
167template <class T> void return_temporary_buffer(T* p) noexcept;
168
169template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000170template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
172template <class InputIterator, class ForwardIterator>
173ForwardIterator
174uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
175
Howard Hinnant719bda32011-05-28 14:41:13 +0000176template <class InputIterator, class Size, class ForwardIterator>
177ForwardIterator
178uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
179
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180template <class ForwardIterator, class T>
181void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
182
183template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000184ForwardIterator
185uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000187template <class T>
188void destroy_at(T* location);
189
190template <class ForwardIterator>
191 void destroy(ForwardIterator first, ForwardIterator last);
192
193template <class ForwardIterator, class Size>
194 ForwardIterator destroy_n(ForwardIterator first, Size n);
195
196template <class InputIterator, class ForwardIterator>
197 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
198
199template <class InputIterator, class Size, class ForwardIterator>
200 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
201
202template <class ForwardIterator>
203 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
204
205template <class ForwardIterator, class Size>
206 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
207
208template <class ForwardIterator>
209 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
210
211template <class ForwardIterator, class Size>
212 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
213
Marshall Clowb22274f2017-01-24 22:22:33 +0000214template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
216template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000217class auto_ptr // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218{
219public:
220 typedef X element_type;
221
222 explicit auto_ptr(X* p =0) throw();
223 auto_ptr(auto_ptr&) throw();
224 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
225 auto_ptr& operator=(auto_ptr&) throw();
226 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
227 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
228 ~auto_ptr() throw();
229
230 typename add_lvalue_reference<X>::type operator*() const throw();
231 X* operator->() const throw();
232 X* get() const throw();
233 X* release() throw();
234 void reset(X* p =0) throw();
235
236 auto_ptr(auto_ptr_ref<X>) throw();
237 template<class Y> operator auto_ptr_ref<Y>() throw();
238 template<class Y> operator auto_ptr<Y>() throw();
239};
240
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000241template <class T>
242struct default_delete
243{
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 constexpr default_delete() noexcept = default;
245 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000246
Howard Hinnant719bda32011-05-28 14:41:13 +0000247 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248};
249
250template <class T>
251struct default_delete<T[]>
252{
Howard Hinnant719bda32011-05-28 14:41:13 +0000253 constexpr default_delete() noexcept = default;
254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255 template <class U> void operator()(U*) const = delete;
256};
257
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000258template <class T, class D = default_delete<T>>
259class unique_ptr
260{
261public:
262 typedef see below pointer;
263 typedef T element_type;
264 typedef D deleter_type;
265
266 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000267 constexpr unique_ptr() noexcept;
268 explicit unique_ptr(pointer p) noexcept;
269 unique_ptr(pointer p, see below d1) noexcept;
270 unique_ptr(pointer p, see below d2) noexcept;
271 unique_ptr(unique_ptr&& u) noexcept;
272 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000273 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000275 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000276 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000277
278 // destructor
279 ~unique_ptr();
280
281 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr& operator=(unique_ptr&& u) noexcept;
283 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
284 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // observers
287 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000288 pointer operator->() const noexcept;
289 pointer get() const noexcept;
290 deleter_type& get_deleter() noexcept;
291 const deleter_type& get_deleter() const noexcept;
292 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer release() noexcept;
296 void reset(pointer p = pointer()) noexcept;
297 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000298};
299
300template <class T, class D>
301class unique_ptr<T[], D>
302{
303public:
304 typedef implementation-defined pointer;
305 typedef T element_type;
306 typedef D deleter_type;
307
308 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000309 constexpr unique_ptr() noexcept;
310 explicit unique_ptr(pointer p) noexcept;
311 unique_ptr(pointer p, see below d) noexcept;
312 unique_ptr(pointer p, see below d) noexcept;
313 unique_ptr(unique_ptr&& u) noexcept;
314 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000317 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000318
319 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000320 unique_ptr& operator=(unique_ptr&& u) noexcept;
321 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // observers
324 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000325 pointer get() const noexcept;
326 deleter_type& get_deleter() noexcept;
327 const deleter_type& get_deleter() const noexcept;
328 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000331 pointer release() noexcept;
332 void reset(pointer p = pointer()) noexcept;
333 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000334 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000335 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336};
337
338template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000340
341template <class T1, class D1, class T2, class D2>
342 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
343template <class T1, class D1, class T2, class D2>
344 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
345template <class T1, class D1, class T2, class D2>
346 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
347template <class T1, class D1, class T2, class D2>
348 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
349template <class T1, class D1, class T2, class D2>
350 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353
Howard Hinnant719bda32011-05-28 14:41:13 +0000354template <class T, class D>
355 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
356template <class T, class D>
357 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
358template <class T, class D>
359 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
360template <class T, class D>
361 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
362
363template <class T, class D>
364 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
365template <class T, class D>
366 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
367template <class T, class D>
368 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
369template <class T, class D>
370 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
371template <class T, class D>
372 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
379
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000380class bad_weak_ptr
381 : public std::exception
382{
Howard Hinnant719bda32011-05-28 14:41:13 +0000383 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000384};
385
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000386template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
387template<class T> unique_ptr<T> make_unique(size_t n); // C++14
388template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
389
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000390template<class T>
391class shared_ptr
392{
393public:
394 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000395 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396
397 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000398 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000399 template<class Y> explicit shared_ptr(Y* p);
400 template<class Y, class D> shared_ptr(Y* p, D d);
401 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
402 template <class D> shared_ptr(nullptr_t p, D d);
403 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000404 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
405 shared_ptr(const shared_ptr& r) noexcept;
406 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
407 shared_ptr(shared_ptr&& r) noexcept;
408 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000409 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000410 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000411 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
412 shared_ptr(nullptr_t) : shared_ptr() { }
413
414 // destructor:
415 ~shared_ptr();
416
417 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000418 shared_ptr& operator=(const shared_ptr& r) noexcept;
419 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
420 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000421 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000422 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000423 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
424
425 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000426 void swap(shared_ptr& r) noexcept;
427 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000428 template<class Y> void reset(Y* p);
429 template<class Y, class D> void reset(Y* p, D d);
430 template<class Y, class D, class A> void reset(Y* p, D d, A a);
431
Howard Hinnant719bda32011-05-28 14:41:13 +0000432 // observers:
433 T* get() const noexcept;
434 T& operator*() const noexcept;
435 T* operator->() const noexcept;
436 long use_count() const noexcept;
437 bool unique() const noexcept;
438 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000439 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
440 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000441};
442
443// shared_ptr comparisons:
444template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000445 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000446template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000447 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000448template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000449 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000451 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000453 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000454template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000455 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
456
457template <class T>
458 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
459template <class T>
460 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
461template <class T>
462 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
463template <class T>
464 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
465template <class T>
466 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
467template <class T>
468bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
469template <class T>
470 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
471template <class T>
472 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
473template <class T>
474 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000483template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000484
485// shared_ptr casts:
486template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000487 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000488template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000489 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000490template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000491 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000492
493// shared_ptr I/O:
494template<class E, class T, class Y>
495 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
496
497// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000498template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000499
500template<class T, class... Args>
501 shared_ptr<T> make_shared(Args&&... args);
502template<class T, class A, class... Args>
503 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
504
505template<class T>
506class weak_ptr
507{
508public:
509 typedef T element_type;
510
511 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000512 constexpr weak_ptr() noexcept;
513 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
514 weak_ptr(weak_ptr const& r) noexcept;
515 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000516 weak_ptr(weak_ptr&& r) noexcept; // C++14
517 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000518
519 // destructor
520 ~weak_ptr();
521
522 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000523 weak_ptr& operator=(weak_ptr const& r) noexcept;
524 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
525 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000526 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
527 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000528
529 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000530 void swap(weak_ptr& r) noexcept;
531 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000532
533 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000534 long use_count() const noexcept;
535 bool expired() const noexcept;
536 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000537 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
538 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000539};
540
541// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000542template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000543
544// class owner_less:
545template<class T> struct owner_less;
546
547template<class T>
548struct owner_less<shared_ptr<T>>
549 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
550{
551 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000552 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
553 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
554 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000555};
556
557template<class T>
558struct owner_less<weak_ptr<T>>
559 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
560{
561 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000562 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
563 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
564 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
565};
566
567template <> // Added in C++14
568struct owner_less<void>
569{
570 template <class _Tp, class _Up>
571 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
572 template <class _Tp, class _Up>
573 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
574 template <class _Tp, class _Up>
575 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
576 template <class _Tp, class _Up>
577 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
578
579 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000580};
581
582template<class T>
583class enable_shared_from_this
584{
585protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000586 constexpr enable_shared_from_this() noexcept;
587 enable_shared_from_this(enable_shared_from_this const&) noexcept;
588 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000589 ~enable_shared_from_this();
590public:
591 shared_ptr<T> shared_from_this();
592 shared_ptr<T const> shared_from_this() const;
593};
594
595template<class T>
596 bool atomic_is_lock_free(const shared_ptr<T>* p);
597template<class T>
598 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
599template<class T>
600 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
601template<class T>
602 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
603template<class T>
604 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
605template<class T>
606 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
607template<class T>
608 shared_ptr<T>
609 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
610template<class T>
611 bool
612 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
613template<class T>
614 bool
615 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
616template<class T>
617 bool
618 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
619 shared_ptr<T> w, memory_order success,
620 memory_order failure);
621template<class T>
622 bool
623 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
624 shared_ptr<T> w, memory_order success,
625 memory_order failure);
626// Hash support
627template <class T> struct hash;
628template <class T, class D> struct hash<unique_ptr<T, D> >;
629template <class T> struct hash<shared_ptr<T> >;
630
631// Pointer safety
632enum class pointer_safety { relaxed, preferred, strict };
633void declare_reachable(void *p);
634template <class T> T *undeclare_reachable(T *p);
635void declare_no_pointers(char *p, size_t n);
636void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000637pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000638
Howard Hinnantc51e1022010-05-11 19:42:16 +0000639void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
640
641} // std
642
643*/
644
645#include <__config>
646#include <type_traits>
647#include <typeinfo>
648#include <cstddef>
649#include <cstdint>
650#include <new>
651#include <utility>
652#include <limits>
653#include <iterator>
654#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000655#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000656#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000657#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000658#include <cstring>
Eric Fiselier9d355982017-04-12 23:45:53 +0000659#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000660#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000661# include <atomic>
662#endif
663
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000664#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000666#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667
Eric Fiselierf4433a32017-05-31 22:07:49 +0000668_LIBCPP_PUSH_MACROS
669#include <__undef_macros>
670
671
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672_LIBCPP_BEGIN_NAMESPACE_STD
673
Eric Fiselier89659d12015-07-07 00:27:16 +0000674template <class _ValueType>
675inline _LIBCPP_ALWAYS_INLINE
676_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
677#if !defined(_LIBCPP_HAS_NO_THREADS) && \
678 defined(__ATOMIC_RELAXED) && \
679 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
680 return __atomic_load_n(__value, __ATOMIC_RELAXED);
681#else
682 return *__value;
683#endif
684}
685
Kuba Breckade9d6792016-09-04 09:55:12 +0000686template <class _ValueType>
687inline _LIBCPP_ALWAYS_INLINE
688_ValueType __libcpp_acquire_load(_ValueType const* __value) {
689#if !defined(_LIBCPP_HAS_NO_THREADS) && \
690 defined(__ATOMIC_ACQUIRE) && \
691 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
692 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
693#else
694 return *__value;
695#endif
696}
697
Marshall Clow78dbe462016-11-14 18:22:19 +0000698// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000699
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700template <class _Tp> class allocator;
701
702template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000703class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704{
705public:
706 typedef void* pointer;
707 typedef const void* const_pointer;
708 typedef void value_type;
709
710 template <class _Up> struct rebind {typedef allocator<_Up> other;};
711};
712
Howard Hinnant9f771052012-01-19 23:15:22 +0000713template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000714class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000715{
716public:
717 typedef const void* pointer;
718 typedef const void* const_pointer;
719 typedef const void value_type;
720
721 template <class _Up> struct rebind {typedef allocator<_Up> other;};
722};
723
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724// pointer_traits
725
Marshall Clow0be70c32017-06-14 21:23:57 +0000726template <class _Tp, class = void>
727struct __has_element_type : false_type {};
728
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000730struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000731 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732
733template <class _Ptr, bool = __has_element_type<_Ptr>::value>
734struct __pointer_traits_element_type;
735
736template <class _Ptr>
737struct __pointer_traits_element_type<_Ptr, true>
738{
739 typedef typename _Ptr::element_type type;
740};
741
742#ifndef _LIBCPP_HAS_NO_VARIADICS
743
744template <template <class, class...> class _Sp, class _Tp, class ..._Args>
745struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
746{
747 typedef typename _Sp<_Tp, _Args...>::element_type type;
748};
749
750template <template <class, class...> class _Sp, class _Tp, class ..._Args>
751struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
752{
753 typedef _Tp type;
754};
755
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000756#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757
758template <template <class> class _Sp, class _Tp>
759struct __pointer_traits_element_type<_Sp<_Tp>, true>
760{
761 typedef typename _Sp<_Tp>::element_type type;
762};
763
764template <template <class> class _Sp, class _Tp>
765struct __pointer_traits_element_type<_Sp<_Tp>, false>
766{
767 typedef _Tp type;
768};
769
770template <template <class, class> class _Sp, class _Tp, class _A0>
771struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
772{
773 typedef typename _Sp<_Tp, _A0>::element_type type;
774};
775
776template <template <class, class> class _Sp, class _Tp, class _A0>
777struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
778{
779 typedef _Tp type;
780};
781
782template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
783struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
784{
785 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
786};
787
788template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
789struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
790{
791 typedef _Tp type;
792};
793
794template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
795 class _A1, class _A2>
796struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
797{
798 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
799};
800
801template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
802 class _A1, class _A2>
803struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
804{
805 typedef _Tp type;
806};
807
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000808#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809
Marshall Clow0be70c32017-06-14 21:23:57 +0000810template <class _Tp, class = void>
811struct __has_difference_type : false_type {};
812
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000814struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000815 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816
817template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
818struct __pointer_traits_difference_type
819{
820 typedef ptrdiff_t type;
821};
822
823template <class _Ptr>
824struct __pointer_traits_difference_type<_Ptr, true>
825{
826 typedef typename _Ptr::difference_type type;
827};
828
829template <class _Tp, class _Up>
830struct __has_rebind
831{
832private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000833 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834 template <class _Xp> static __two __test(...);
835 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
836public:
837 static const bool value = sizeof(__test<_Tp>(0)) == 1;
838};
839
840template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
841struct __pointer_traits_rebind
842{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000843#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 typedef typename _Tp::template rebind<_Up> type;
845#else
846 typedef typename _Tp::template rebind<_Up>::other type;
847#endif
848};
849
850#ifndef _LIBCPP_HAS_NO_VARIADICS
851
852template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
853struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
854{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000855#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
857#else
858 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
859#endif
860};
861
862template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
863struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
864{
865 typedef _Sp<_Up, _Args...> type;
866};
867
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000868#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869
870template <template <class> class _Sp, class _Tp, class _Up>
871struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
872{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000873#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874 typedef typename _Sp<_Tp>::template rebind<_Up> type;
875#else
876 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
877#endif
878};
879
880template <template <class> class _Sp, class _Tp, class _Up>
881struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
882{
883 typedef _Sp<_Up> type;
884};
885
886template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
887struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
888{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000889#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
891#else
892 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
893#endif
894};
895
896template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
897struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
898{
899 typedef _Sp<_Up, _A0> type;
900};
901
902template <template <class, class, class> class _Sp, class _Tp, class _A0,
903 class _A1, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
905{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000906#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
908#else
909 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
910#endif
911};
912
913template <template <class, class, class> class _Sp, class _Tp, class _A0,
914 class _A1, class _Up>
915struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
916{
917 typedef _Sp<_Up, _A0, _A1> type;
918};
919
920template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
921 class _A1, class _A2, class _Up>
922struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
923{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000924#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
926#else
927 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
928#endif
929};
930
931template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
932 class _A1, class _A2, class _Up>
933struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
934{
935 typedef _Sp<_Up, _A0, _A1, _A2> type;
936};
937
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000938#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939
940template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000941struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942{
943 typedef _Ptr pointer;
944 typedef typename __pointer_traits_element_type<pointer>::type element_type;
945 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
946
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000947#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000948 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949#else
950 template <class _Up> struct rebind
951 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000952#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953
954private:
955 struct __nat {};
956public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 static pointer pointer_to(typename conditional<is_void<element_type>::value,
959 __nat, element_type>::type& __r)
960 {return pointer::pointer_to(__r);}
961};
962
963template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000964struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965{
966 typedef _Tp* pointer;
967 typedef _Tp element_type;
968 typedef ptrdiff_t difference_type;
969
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000970#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 template <class _Up> using rebind = _Up*;
972#else
973 template <class _Up> struct rebind {typedef _Up* other;};
974#endif
975
976private:
977 struct __nat {};
978public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000981 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000982 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983};
984
Eric Fiselierae3ab842015-08-23 02:56:05 +0000985template <class _From, class _To>
986struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000987#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000988 typedef typename pointer_traits<_From>::template rebind<_To> type;
989#else
990 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
991#endif
992};
993
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994// allocator_traits
995
Marshall Clow0be70c32017-06-14 21:23:57 +0000996template <class _Tp, class = void>
997struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998
999template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001000struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001001 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002
1003namespace __pointer_type_imp
1004{
1005
1006template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1007struct __pointer_type
1008{
1009 typedef typename _Dp::pointer type;
1010};
1011
1012template <class _Tp, class _Dp>
1013struct __pointer_type<_Tp, _Dp, false>
1014{
1015 typedef _Tp* type;
1016};
1017
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001018} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019
1020template <class _Tp, class _Dp>
1021struct __pointer_type
1022{
1023 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1024};
1025
Marshall Clow0be70c32017-06-14 21:23:57 +00001026template <class _Tp, class = void>
1027struct __has_const_pointer : false_type {};
1028
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001030struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001031 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032
1033template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1034struct __const_pointer
1035{
1036 typedef typename _Alloc::const_pointer type;
1037};
1038
1039template <class _Tp, class _Ptr, class _Alloc>
1040struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1041{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001042#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1044#else
1045 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1046#endif
1047};
1048
Marshall Clow0be70c32017-06-14 21:23:57 +00001049template <class _Tp, class = void>
1050struct __has_void_pointer : false_type {};
1051
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001053struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001054 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055
1056template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1057struct __void_pointer
1058{
1059 typedef typename _Alloc::void_pointer type;
1060};
1061
1062template <class _Ptr, class _Alloc>
1063struct __void_pointer<_Ptr, _Alloc, false>
1064{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001065#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1067#else
1068 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1069#endif
1070};
1071
Marshall Clow0be70c32017-06-14 21:23:57 +00001072template <class _Tp, class = void>
1073struct __has_const_void_pointer : false_type {};
1074
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001076struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001077 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078
1079template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1080struct __const_void_pointer
1081{
1082 typedef typename _Alloc::const_void_pointer type;
1083};
1084
1085template <class _Ptr, class _Alloc>
1086struct __const_void_pointer<_Ptr, _Alloc, false>
1087{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001088#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1090#else
1091 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1092#endif
1093};
1094
Howard Hinnantc834c512011-11-29 18:15:50 +00001095template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001096inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001097_Tp*
1098__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099{
1100 return __p;
1101}
1102
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001103#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104template <class _Pointer>
1105inline _LIBCPP_INLINE_VISIBILITY
1106typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001107__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001109 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110}
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001111#else
1112template <class _Pointer>
1113inline _LIBCPP_INLINE_VISIBILITY
1114auto
1115__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1116-> decltype(pointer_traits<_Pointer>::to_address(__p))
1117{
1118 return pointer_traits<_Pointer>::to_address(__p);
1119}
1120
1121template <class _Pointer, class... _None>
1122inline _LIBCPP_INLINE_VISIBILITY
1123auto
1124__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1125{
1126 return _VSTD::__to_raw_pointer(__p.operator->());
1127}
1128
1129template <class _Tp>
1130inline _LIBCPP_INLINE_VISIBILITY constexpr
1131_Tp*
1132to_address(_Tp* __p) _NOEXCEPT
1133{
1134 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1135 return __p;
1136}
1137
1138template <class _Pointer>
1139inline _LIBCPP_INLINE_VISIBILITY
1140auto
1141to_address(const _Pointer& __p) _NOEXCEPT
1142{
1143 return _VSTD::__to_raw_pointer(__p);
1144}
1145#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146
Marshall Clow0be70c32017-06-14 21:23:57 +00001147template <class _Tp, class = void>
1148struct __has_size_type : false_type {};
1149
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001151struct __has_size_type<_Tp,
1152 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001154template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155struct __size_type
1156{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001157 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158};
1159
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001160template <class _Alloc, class _DiffType>
1161struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162{
1163 typedef typename _Alloc::size_type type;
1164};
1165
Marshall Clow0be70c32017-06-14 21:23:57 +00001166template <class _Tp, class = void>
1167struct __has_propagate_on_container_copy_assignment : false_type {};
1168
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001170struct __has_propagate_on_container_copy_assignment<_Tp,
1171 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1172 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173
1174template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1175struct __propagate_on_container_copy_assignment
1176{
1177 typedef false_type type;
1178};
1179
1180template <class _Alloc>
1181struct __propagate_on_container_copy_assignment<_Alloc, true>
1182{
1183 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1184};
1185
Marshall Clow0be70c32017-06-14 21:23:57 +00001186template <class _Tp, class = void>
1187struct __has_propagate_on_container_move_assignment : false_type {};
1188
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001190struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001191 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1192 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193
1194template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1195struct __propagate_on_container_move_assignment
1196{
1197 typedef false_type type;
1198};
1199
1200template <class _Alloc>
1201struct __propagate_on_container_move_assignment<_Alloc, true>
1202{
1203 typedef typename _Alloc::propagate_on_container_move_assignment type;
1204};
1205
Marshall Clow0be70c32017-06-14 21:23:57 +00001206template <class _Tp, class = void>
1207struct __has_propagate_on_container_swap : false_type {};
1208
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001210struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001211 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1212 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213
1214template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1215struct __propagate_on_container_swap
1216{
1217 typedef false_type type;
1218};
1219
1220template <class _Alloc>
1221struct __propagate_on_container_swap<_Alloc, true>
1222{
1223 typedef typename _Alloc::propagate_on_container_swap type;
1224};
1225
Marshall Clow0be70c32017-06-14 21:23:57 +00001226template <class _Tp, class = void>
1227struct __has_is_always_equal : false_type {};
1228
Marshall Clow0b587562015-06-02 16:34:03 +00001229template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001230struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001231 typename __void_t<typename _Tp::is_always_equal>::type>
1232 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001233
1234template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1235struct __is_always_equal
1236{
1237 typedef typename _VSTD::is_empty<_Alloc>::type type;
1238};
1239
1240template <class _Alloc>
1241struct __is_always_equal<_Alloc, true>
1242{
1243 typedef typename _Alloc::is_always_equal type;
1244};
1245
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1247struct __has_rebind_other
1248{
1249private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001250 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 template <class _Xp> static __two __test(...);
1252 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1253public:
1254 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1255};
1256
1257template <class _Tp, class _Up>
1258struct __has_rebind_other<_Tp, _Up, false>
1259{
1260 static const bool value = false;
1261};
1262
1263template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1264struct __allocator_traits_rebind
1265{
1266 typedef typename _Tp::template rebind<_Up>::other type;
1267};
1268
1269#ifndef _LIBCPP_HAS_NO_VARIADICS
1270
1271template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1278struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1279{
1280 typedef _Alloc<_Up, _Args...> type;
1281};
1282
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001283#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
1285template <template <class> class _Alloc, class _Tp, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1287{
1288 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1289};
1290
1291template <template <class> class _Alloc, class _Tp, class _Up>
1292struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1293{
1294 typedef _Alloc<_Up> type;
1295};
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1298struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1299{
1300 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1301};
1302
1303template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1305{
1306 typedef _Alloc<_Up, _A0> type;
1307};
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1310 class _A1, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1312{
1313 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1314};
1315
1316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1319{
1320 typedef _Alloc<_Up, _A0, _A1> type;
1321};
1322
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _A2, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1326{
1327 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1328};
1329
1330template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1331 class _A1, class _A2, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1333{
1334 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1335};
1336
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001337#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001339#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
1341template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1342auto
1343__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001344 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
1346template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1347auto
1348__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1349 -> false_type;
1350
1351template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1352struct __has_allocate_hint
1353 : integral_constant<bool,
1354 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001355 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356 declval<_SizeType>(),
1357 declval<_ConstVoidPtr>())),
1358 true_type>::value>
1359{
1360};
1361
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001362#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363
1364template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1365struct __has_allocate_hint
1366 : true_type
1367{
1368};
1369
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001370#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001372#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
1374template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001375decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1376 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 true_type())
1378__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1379
1380template <class _Alloc, class _Pointer, class ..._Args>
1381false_type
1382__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1383
1384template <class _Alloc, class _Pointer, class ..._Args>
1385struct __has_construct
1386 : integral_constant<bool,
1387 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001388 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 declval<_Pointer>(),
1390 declval<_Args>()...)),
1391 true_type>::value>
1392{
1393};
1394
1395template <class _Alloc, class _Pointer>
1396auto
1397__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1398 -> decltype(__a.destroy(__p), true_type());
1399
1400template <class _Alloc, class _Pointer>
1401auto
1402__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1403 -> false_type;
1404
1405template <class _Alloc, class _Pointer>
1406struct __has_destroy
1407 : integral_constant<bool,
1408 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001409 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 declval<_Pointer>())),
1411 true_type>::value>
1412{
1413};
1414
1415template <class _Alloc>
1416auto
1417__has_max_size_test(_Alloc&& __a)
1418 -> decltype(__a.max_size(), true_type());
1419
1420template <class _Alloc>
1421auto
1422__has_max_size_test(const volatile _Alloc& __a)
1423 -> false_type;
1424
1425template <class _Alloc>
1426struct __has_max_size
1427 : integral_constant<bool,
1428 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001429 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430 true_type>::value>
1431{
1432};
1433
1434template <class _Alloc>
1435auto
1436__has_select_on_container_copy_construction_test(_Alloc&& __a)
1437 -> decltype(__a.select_on_container_copy_construction(), true_type());
1438
1439template <class _Alloc>
1440auto
1441__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1442 -> false_type;
1443
1444template <class _Alloc>
1445struct __has_select_on_container_copy_construction
1446 : integral_constant<bool,
1447 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001448 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 true_type>::value>
1450{
1451};
1452
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001453#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
1455#ifndef _LIBCPP_HAS_NO_VARIADICS
1456
1457template <class _Alloc, class _Pointer, class ..._Args>
1458struct __has_construct
1459 : false_type
1460{
1461};
1462
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001463#else // _LIBCPP_HAS_NO_VARIADICS
1464
1465template <class _Alloc, class _Pointer, class _Args>
1466struct __has_construct
1467 : false_type
1468{
1469};
1470
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001471#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Alloc, class _Pointer>
1474struct __has_destroy
1475 : false_type
1476{
1477};
1478
1479template <class _Alloc>
1480struct __has_max_size
1481 : true_type
1482{
1483};
1484
1485template <class _Alloc>
1486struct __has_select_on_container_copy_construction
1487 : false_type
1488{
1489};
1490
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001491#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001493template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1494struct __alloc_traits_difference_type
1495{
1496 typedef typename pointer_traits<_Ptr>::difference_type type;
1497};
1498
1499template <class _Alloc, class _Ptr>
1500struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1501{
1502 typedef typename _Alloc::difference_type type;
1503};
1504
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001506struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 typedef _Alloc allocator_type;
1509 typedef typename allocator_type::value_type value_type;
1510
1511 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1512 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1513 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1514 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1515
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1517 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1520 propagate_on_container_copy_assignment;
1521 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1522 propagate_on_container_move_assignment;
1523 typedef typename __propagate_on_container_swap<allocator_type>::type
1524 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001525 typedef typename __is_always_equal<allocator_type>::type
1526 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001528#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001530 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001532#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> struct rebind_alloc
1534 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1535 template <class _Tp> struct rebind_traits
1536 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static pointer allocate(allocator_type& __a, size_type __n)
1541 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001544 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001548 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {__a.deallocate(__p, __n);}
1550
1551#ifndef _LIBCPP_HAS_NO_VARIADICS
1552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001555 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001556 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001557#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001560 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {
1562 ::new ((void*)__p) _Tp();
1563 }
1564 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001566 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {
1568 ::new ((void*)__p) _Tp(__a0);
1569 }
1570 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001571 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001572 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 const _A1& __a1)
1574 {
1575 ::new ((void*)__p) _Tp(__a0, __a1);
1576 }
1577 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001579 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 const _A1& __a1, const _A2& __a2)
1581 {
1582 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1583 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001584#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
1586 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 static void destroy(allocator_type& __a, _Tp* __p)
1589 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1590
Howard Hinnant756c69b2010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001592 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1594
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 static allocator_type
1597 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001598 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 __has_select_on_container_copy_construction<const allocator_type>(),
1600 __a);}
1601
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001602 template <class _Ptr>
1603 _LIBCPP_INLINE_VISIBILITY
1604 static
1605 void
1606 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1607 {
1608 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1609 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1610 }
1611
1612 template <class _Tp>
1613 _LIBCPP_INLINE_VISIBILITY
1614 static
1615 typename enable_if
1616 <
1617 (is_same<allocator_type, allocator<_Tp> >::value
1618 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1619 is_trivially_move_constructible<_Tp>::value,
1620 void
1621 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001622 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001623 {
1624 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001625 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001626 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001627 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001628 __begin2 += _Np;
1629 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 }
1631
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001632 template <class _Iter, class _Ptr>
1633 _LIBCPP_INLINE_VISIBILITY
1634 static
1635 void
1636 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1637 {
1638 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1639 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1640 }
1641
1642 template <class _Tp>
1643 _LIBCPP_INLINE_VISIBILITY
1644 static
1645 typename enable_if
1646 <
1647 (is_same<allocator_type, allocator<_Tp> >::value
1648 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1649 is_trivially_move_constructible<_Tp>::value,
1650 void
1651 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001652 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001653 {
1654 typedef typename remove_const<_Tp>::type _Vp;
1655 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001656 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001657 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001658 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001659 __begin2 += _Np;
1660 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001661 }
1662
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 template <class _Ptr>
1664 _LIBCPP_INLINE_VISIBILITY
1665 static
1666 void
1667 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1668 {
1669 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001670 {
1671 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1672 --__end2;
1673 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001674 }
1675
1676 template <class _Tp>
1677 _LIBCPP_INLINE_VISIBILITY
1678 static
1679 typename enable_if
1680 <
1681 (is_same<allocator_type, allocator<_Tp> >::value
1682 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1683 is_trivially_move_constructible<_Tp>::value,
1684 void
1685 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001686 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001687 {
1688 ptrdiff_t _Np = __end1 - __begin1;
1689 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001690 if (_Np > 0)
1691 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001692 }
1693
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694private:
1695
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001697 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698 const_void_pointer __hint, true_type)
1699 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001700 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001701 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001702 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 {return __a.allocate(__n);}
1704
1705#ifndef _LIBCPP_HAS_NO_VARIADICS
1706 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001709 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1713 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001714 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001716#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717
1718 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1721 {__a.destroy(__p);}
1722 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 static void __destroy(false_type, allocator_type&, _Tp* __p)
1725 {
1726 __p->~_Tp();
1727 }
1728
Howard Hinnant756c69b2010-09-22 16:48:34 +00001729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 static size_type __max_size(true_type, const allocator_type& __a)
1731 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001734 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735
Howard Hinnant756c69b2010-09-22 16:48:34 +00001736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001738 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001742 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743 {return __a;}
1744};
1745
Marshall Clow940e01c2015-04-07 05:21:38 +00001746template <class _Traits, class _Tp>
1747struct __rebind_alloc_helper
1748{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001749#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001750 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001751#else
1752 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1753#endif
1754};
1755
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756// allocator
1757
1758template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001759class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760{
1761public:
1762 typedef size_t size_type;
1763 typedef ptrdiff_t difference_type;
1764 typedef _Tp* pointer;
1765 typedef const _Tp* const_pointer;
1766 typedef _Tp& reference;
1767 typedef const _Tp& const_reference;
1768 typedef _Tp value_type;
1769
Howard Hinnant4931e092011-06-02 21:38:57 +00001770 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001771 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001772
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1774
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1776 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1777 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001778 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001779 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001782 {
1783 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001784 __throw_length_error("allocator<T>::allocate(size_t n)"
1785 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001786 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1787 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001788 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001789 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001790 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1791 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001792#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 template <class _Up, class... _Args>
1794 _LIBCPP_INLINE_VISIBILITY
1795 void
1796 construct(_Up* __p, _Args&&... __args)
1797 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001798 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001800#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 _LIBCPP_INLINE_VISIBILITY
1802 void
1803 construct(pointer __p)
1804 {
1805 ::new((void*)__p) _Tp();
1806 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001807# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001808
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0>
1810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001811 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 construct(pointer __p, _A0& __a0)
1813 {
1814 ::new((void*)__p) _Tp(__a0);
1815 }
1816 template <class _A0>
1817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001818 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 construct(pointer __p, const _A0& __a0)
1820 {
1821 ::new((void*)__p) _Tp(__a0);
1822 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001823# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 template <class _A0, class _A1>
1825 _LIBCPP_INLINE_VISIBILITY
1826 void
1827 construct(pointer __p, _A0& __a0, _A1& __a1)
1828 {
1829 ::new((void*)__p) _Tp(__a0, __a1);
1830 }
1831 template <class _A0, class _A1>
1832 _LIBCPP_INLINE_VISIBILITY
1833 void
1834 construct(pointer __p, const _A0& __a0, _A1& __a1)
1835 {
1836 ::new((void*)__p) _Tp(__a0, __a1);
1837 }
1838 template <class _A0, class _A1>
1839 _LIBCPP_INLINE_VISIBILITY
1840 void
1841 construct(pointer __p, _A0& __a0, const _A1& __a1)
1842 {
1843 ::new((void*)__p) _Tp(__a0, __a1);
1844 }
1845 template <class _A0, class _A1>
1846 _LIBCPP_INLINE_VISIBILITY
1847 void
1848 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1849 {
1850 ::new((void*)__p) _Tp(__a0, __a1);
1851 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001852#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1854};
1855
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001856template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001857class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001858{
1859public:
1860 typedef size_t size_type;
1861 typedef ptrdiff_t difference_type;
1862 typedef const _Tp* pointer;
1863 typedef const _Tp* const_pointer;
1864 typedef const _Tp& reference;
1865 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001866 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001867
1868 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001869 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001870
1871 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1872
1873 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1874 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1875 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1876 {return _VSTD::addressof(__x);}
1877 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001878 {
1879 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001880 __throw_length_error("allocator<const T>::allocate(size_t n)"
1881 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001882 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001883 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001884 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00001885 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001886 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1887 {return size_type(~0) / sizeof(_Tp);}
1888#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1889 template <class _Up, class... _Args>
1890 _LIBCPP_INLINE_VISIBILITY
1891 void
1892 construct(_Up* __p, _Args&&... __args)
1893 {
1894 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1895 }
1896#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1897 _LIBCPP_INLINE_VISIBILITY
1898 void
1899 construct(pointer __p)
1900 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001901 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001902 }
1903# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001904
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001905 template <class _A0>
1906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001907 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001908 construct(pointer __p, _A0& __a0)
1909 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001910 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001911 }
1912 template <class _A0>
1913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001914 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001915 construct(pointer __p, const _A0& __a0)
1916 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001917 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001918 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1920 template <class _A0, class _A1>
1921 _LIBCPP_INLINE_VISIBILITY
1922 void
1923 construct(pointer __p, _A0& __a0, _A1& __a1)
1924 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001925 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001926 }
1927 template <class _A0, class _A1>
1928 _LIBCPP_INLINE_VISIBILITY
1929 void
1930 construct(pointer __p, const _A0& __a0, _A1& __a1)
1931 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001932 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001933 }
1934 template <class _A0, class _A1>
1935 _LIBCPP_INLINE_VISIBILITY
1936 void
1937 construct(pointer __p, _A0& __a0, const _A1& __a1)
1938 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001939 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001940 }
1941 template <class _A0, class _A1>
1942 _LIBCPP_INLINE_VISIBILITY
1943 void
1944 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1945 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001946 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001947 }
1948#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1949 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1950};
1951
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952template <class _Tp, class _Up>
1953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001954bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955
1956template <class _Tp, class _Up>
1957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001958bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959
1960template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001961class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 : public iterator<output_iterator_tag,
1963 _Tp, // purposefully not C++03
1964 ptrdiff_t, // purposefully not C++03
1965 _Tp*, // purposefully not C++03
1966 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1967{
1968private:
1969 _OutputIterator __x_;
1970public:
1971 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1972 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1973 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1974 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001975#if _LIBCPP_STD_VER >= 14
1976 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1977 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1978#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1980 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1981 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001982#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001983 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985};
1986
1987template <class _Tp>
1988pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001989get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990{
1991 pair<_Tp*, ptrdiff_t> __r(0, 0);
1992 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1993 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1994 / sizeof(_Tp);
1995 if (__n > __m)
1996 __n = __m;
1997 while (__n > 0)
1998 {
1999 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2000 if (__r.first)
2001 {
2002 __r.second = __n;
2003 break;
2004 }
2005 __n /= 2;
2006 }
2007 return __r;
2008}
2009
2010template <class _Tp>
2011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002012void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013
Marshall Clowb22274f2017-01-24 22:22:33 +00002014#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015template <class _Tp>
2016struct auto_ptr_ref
2017{
2018 _Tp* __ptr_;
2019};
2020
2021template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002022class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024private:
2025 _Tp* __ptr_;
2026public:
2027 typedef _Tp element_type;
2028
2029 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2030 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2031 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2032 : __ptr_(__p.release()) {}
2033 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2034 {reset(__p.release()); return *this;}
2035 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2036 {reset(__p.release()); return *this;}
2037 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2038 {reset(__p.__ptr_); return *this;}
2039 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2040
2041 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2042 {return *__ptr_;}
2043 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2044 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2045 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2046 {
2047 _Tp* __t = __ptr_;
2048 __ptr_ = 0;
2049 return __t;
2050 }
2051 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2052 {
2053 if (__ptr_ != __p)
2054 delete __ptr_;
2055 __ptr_ = __p;
2056 }
2057
2058 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2059 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2060 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2061 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2062 {return auto_ptr<_Up>(release());}
2063};
2064
2065template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002066class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
2068public:
2069 typedef void element_type;
2070};
Marshall Clowb22274f2017-01-24 22:22:33 +00002071#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
Eric Fiselier9d355982017-04-12 23:45:53 +00002073template <class _Tp, int _Idx,
2074 bool _CanBeEmptyBase =
2075 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2076struct __compressed_pair_elem {
2077 typedef _Tp _ParamT;
2078 typedef _Tp& reference;
2079 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080
Eric Fiselier9d355982017-04-12 23:45:53 +00002081#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002082 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083
Eric Fiselier9d355982017-04-12 23:45:53 +00002084 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002085 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2086 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002087 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002088 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002089 __compressed_pair_elem(_Up&& __u)
2090 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091
Eric Fiselier9d355982017-04-12 23:45:53 +00002092 template <class... _Args, size_t... _Indexes>
2093 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2094 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2095 __tuple_indices<_Indexes...>)
2096 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2097#else
Alex Lorenz76132112017-11-09 17:54:49 +00002098 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2099 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002100 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2101#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102
Alex Lorenz76132112017-11-09 17:54:49 +00002103 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2104 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002105 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002108 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109};
2110
Eric Fiselier9d355982017-04-12 23:45:53 +00002111template <class _Tp, int _Idx>
2112struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2113 typedef _Tp _ParamT;
2114 typedef _Tp& reference;
2115 typedef const _Tp& const_reference;
2116 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117
Eric Fiselier9d355982017-04-12 23:45:53 +00002118#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002119 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120
Eric Fiselier9d355982017-04-12 23:45:53 +00002121 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002122 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2123 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002124 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002125 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002126 __compressed_pair_elem(_Up&& __u)
2127 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128
Eric Fiselier9d355982017-04-12 23:45:53 +00002129 template <class... _Args, size_t... _Indexes>
2130 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2131 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2132 __tuple_indices<_Indexes...>)
2133 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2134#else
Alex Lorenz76132112017-11-09 17:54:49 +00002135 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2136 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002137 __compressed_pair_elem(_ParamT __p)
2138 : __value_type(std::forward<_ParamT>(__p)) {}
2139#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140
Alex Lorenz76132112017-11-09 17:54:49 +00002141 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2142 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002143 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144};
2145
Eric Fiselier9d355982017-04-12 23:45:53 +00002146// Tag used to construct the second element of the compressed pair.
2147struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148
2149template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002150class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2151 private __compressed_pair_elem<_T2, 1> {
2152 typedef __compressed_pair_elem<_T1, 0> _Base1;
2153 typedef __compressed_pair_elem<_T2, 1> _Base2;
2154
2155 // NOTE: This static assert should never fire because __compressed_pair
2156 // is *almost never* used in a scenario where it's possible for T1 == T2.
2157 // (The exception is std::function where it is possible that the function
2158 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002159 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002160 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2161 "The current implementation is NOT ABI-compatible with the previous "
2162 "implementation for this configuration");
2163
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002165#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002166 template <bool _Dummy = true,
2167 class = typename enable_if<
2168 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2169 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2170 >::type
2171 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002172 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002173 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174
Eric Fiselier9d355982017-04-12 23:45:53 +00002175 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2176 __compressed_pair>::value,
2177 bool>::type = true>
2178 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2179 __compressed_pair(_Tp&& __t)
2180 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181
Eric Fiselier9d355982017-04-12 23:45:53 +00002182 template <class _Tp>
2183 _LIBCPP_INLINE_VISIBILITY constexpr
2184 __compressed_pair(__second_tag, _Tp&& __t)
2185 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186
Eric Fiselier9d355982017-04-12 23:45:53 +00002187 template <class _U1, class _U2>
2188 _LIBCPP_INLINE_VISIBILITY constexpr
2189 __compressed_pair(_U1&& __t1, _U2&& __t2)
2190 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191
Eric Fiselier9d355982017-04-12 23:45:53 +00002192 template <class... _Args1, class... _Args2>
2193 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2194 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2195 tuple<_Args2...> __second_args)
2196 : _Base1(__pc, _VSTD::move(__first_args),
2197 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2198 _Base2(__pc, _VSTD::move(__second_args),
2199 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002200
Eric Fiselier9d355982017-04-12 23:45:53 +00002201#else
2202 _LIBCPP_INLINE_VISIBILITY
2203 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002204
Eric Fiselier9d355982017-04-12 23:45:53 +00002205 _LIBCPP_INLINE_VISIBILITY explicit
2206 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002207
Eric Fiselier9d355982017-04-12 23:45:53 +00002208 _LIBCPP_INLINE_VISIBILITY
2209 __compressed_pair(__second_tag, _T2 __t2)
2210 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211
Eric Fiselier9d355982017-04-12 23:45:53 +00002212 _LIBCPP_INLINE_VISIBILITY
2213 __compressed_pair(_T1 __t1, _T2 __t2)
2214 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216
Eric Fiselier9d355982017-04-12 23:45:53 +00002217 _LIBCPP_INLINE_VISIBILITY
2218 typename _Base1::reference first() _NOEXCEPT {
2219 return static_cast<_Base1&>(*this).__get();
2220 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221
Eric Fiselier9d355982017-04-12 23:45:53 +00002222 _LIBCPP_INLINE_VISIBILITY
2223 typename _Base1::const_reference first() const _NOEXCEPT {
2224 return static_cast<_Base1 const&>(*this).__get();
2225 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226
Eric Fiselier9d355982017-04-12 23:45:53 +00002227 _LIBCPP_INLINE_VISIBILITY
2228 typename _Base2::reference second() _NOEXCEPT {
2229 return static_cast<_Base2&>(*this).__get();
2230 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231
Eric Fiselier9d355982017-04-12 23:45:53 +00002232 _LIBCPP_INLINE_VISIBILITY
2233 typename _Base2::const_reference second() const _NOEXCEPT {
2234 return static_cast<_Base2 const&>(*this).__get();
2235 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236
Eric Fiselier9d355982017-04-12 23:45:53 +00002237 _LIBCPP_INLINE_VISIBILITY
2238 void swap(__compressed_pair& __x)
2239 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2240 __is_nothrow_swappable<_T2>::value)
2241 {
2242 using std::swap;
2243 swap(first(), __x.first());
2244 swap(second(), __x.second());
2245 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246};
2247
2248template <class _T1, class _T2>
2249inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002250void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2251 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2252 __is_nothrow_swappable<_T2>::value) {
2253 __x.swap(__y);
2254}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002256// default_delete
2257
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002259struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002260 static_assert(!is_function<_Tp>::value,
2261 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002262#ifndef _LIBCPP_CXX03_LANG
2263 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002264#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002265 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002266#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002267 template <class _Up>
2268 _LIBCPP_INLINE_VISIBILITY
2269 default_delete(const default_delete<_Up>&,
2270 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2271 0) _NOEXCEPT {}
2272
2273 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2274 static_assert(sizeof(_Tp) > 0,
2275 "default_delete can not delete incomplete type");
2276 static_assert(!is_void<_Tp>::value,
2277 "default_delete can not delete incomplete type");
2278 delete __ptr;
2279 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280};
2281
2282template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002283struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2284private:
2285 template <class _Up>
2286 struct _EnableIfConvertible
2287 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2288
Howard Hinnant4500ca52011-12-18 21:19:44 +00002289public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002290#ifndef _LIBCPP_CXX03_LANG
2291 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002292#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002293 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002294#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002295
2296 template <class _Up>
2297 _LIBCPP_INLINE_VISIBILITY
2298 default_delete(const default_delete<_Up[]>&,
2299 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2300
2301 template <class _Up>
2302 _LIBCPP_INLINE_VISIBILITY
2303 typename _EnableIfConvertible<_Up>::type
2304 operator()(_Up* __ptr) const _NOEXCEPT {
2305 static_assert(sizeof(_Tp) > 0,
2306 "default_delete can not delete incomplete type");
2307 static_assert(!is_void<_Tp>::value,
2308 "default_delete can not delete void type");
2309 delete[] __ptr;
2310 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311};
2312
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313
Eric Fiselier6779b062017-04-16 02:06:25 +00002314
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002315#ifndef _LIBCPP_CXX03_LANG
2316template <class _Deleter>
2317struct __unique_ptr_deleter_sfinae {
2318 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2319 typedef const _Deleter& __lval_ref_type;
2320 typedef _Deleter&& __good_rval_ref_type;
2321 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322};
2323
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002324template <class _Deleter>
2325struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2326 typedef const _Deleter& __lval_ref_type;
2327 typedef const _Deleter&& __bad_rval_ref_type;
2328 typedef false_type __enable_rval_overload;
2329};
2330
2331template <class _Deleter>
2332struct __unique_ptr_deleter_sfinae<_Deleter&> {
2333 typedef _Deleter& __lval_ref_type;
2334 typedef _Deleter&& __bad_rval_ref_type;
2335 typedef false_type __enable_rval_overload;
2336};
2337#endif // !defined(_LIBCPP_CXX03_LANG)
2338
2339template <class _Tp, class _Dp = default_delete<_Tp> >
2340class _LIBCPP_TEMPLATE_VIS unique_ptr {
2341public:
2342 typedef _Tp element_type;
2343 typedef _Dp deleter_type;
2344 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2345
2346 static_assert(!is_rvalue_reference<deleter_type>::value,
2347 "the specified deleter type cannot be an rvalue reference");
2348
2349private:
2350 __compressed_pair<pointer, deleter_type> __ptr_;
2351
2352 struct __nat { int __for_bool_; };
2353
2354#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002355 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002356
2357 template <bool _Dummy>
2358 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002359 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002360
2361 template <bool _Dummy>
2362 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002363 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002364
2365 template <bool _Dummy>
2366 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002367 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002368
2369 template <bool _Dummy, class _Deleter = typename __dependent_type<
2370 __identity<deleter_type>, _Dummy>::type>
2371 using _EnableIfDeleterDefaultConstructible =
2372 typename enable_if<is_default_constructible<_Deleter>::value &&
2373 !is_pointer<_Deleter>::value>::type;
2374
2375 template <class _ArgType>
2376 using _EnableIfDeleterConstructible =
2377 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2378
2379 template <class _UPtr, class _Up>
2380 using _EnableIfMoveConvertible = typename enable_if<
2381 is_convertible<typename _UPtr::pointer, pointer>::value &&
2382 !is_array<_Up>::value
2383 >::type;
2384
2385 template <class _UDel>
2386 using _EnableIfDeleterConvertible = typename enable_if<
2387 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2388 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2389 >::type;
2390
2391 template <class _UDel>
2392 using _EnableIfDeleterAssignable = typename enable_if<
2393 is_assignable<_Dp&, _UDel&&>::value
2394 >::type;
2395
2396public:
2397 template <bool _Dummy = true,
2398 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2399 _LIBCPP_INLINE_VISIBILITY
2400 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2401
2402 template <bool _Dummy = true,
2403 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2404 _LIBCPP_INLINE_VISIBILITY
2405 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2406
2407 template <bool _Dummy = true,
2408 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2409 _LIBCPP_INLINE_VISIBILITY
2410 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2411
2412 template <bool _Dummy = true,
2413 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2414 _LIBCPP_INLINE_VISIBILITY
2415 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2416 : __ptr_(__p, __d) {}
2417
2418 template <bool _Dummy = true,
2419 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2420 _LIBCPP_INLINE_VISIBILITY
2421 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2422 : __ptr_(__p, _VSTD::move(__d)) {
2423 static_assert(!is_reference<deleter_type>::value,
2424 "rvalue deleter bound to reference");
2425 }
2426
2427 template <bool _Dummy = true,
2428 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2429 _LIBCPP_INLINE_VISIBILITY
2430 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2431
2432 _LIBCPP_INLINE_VISIBILITY
2433 unique_ptr(unique_ptr&& __u) noexcept
2434 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2435 }
2436
2437 template <class _Up, class _Ep,
2438 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2439 class = _EnableIfDeleterConvertible<_Ep>
2440 >
2441 _LIBCPP_INLINE_VISIBILITY
2442 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2443 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2444
2445#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2446 template <class _Up>
2447 _LIBCPP_INLINE_VISIBILITY
2448 unique_ptr(auto_ptr<_Up>&& __p,
2449 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2450 is_same<_Dp, default_delete<_Tp>>::value,
2451 __nat>::type = __nat()) _NOEXCEPT
2452 : __ptr_(__p.release()) {}
2453#endif
2454
2455 _LIBCPP_INLINE_VISIBILITY
2456 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2457 reset(__u.release());
2458 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2459 return *this;
2460 }
2461
2462 template <class _Up, class _Ep,
2463 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2464 class = _EnableIfDeleterAssignable<_Ep>
2465 >
2466 _LIBCPP_INLINE_VISIBILITY
2467 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2468 reset(__u.release());
2469 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2470 return *this;
2471 }
2472
2473#else // _LIBCPP_CXX03_LANG
2474private:
2475 unique_ptr(unique_ptr&);
2476 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2477
2478 unique_ptr& operator=(unique_ptr&);
2479 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2480
2481public:
2482 _LIBCPP_INLINE_VISIBILITY
2483 unique_ptr() : __ptr_(pointer())
2484 {
2485 static_assert(!is_pointer<deleter_type>::value,
2486 "unique_ptr constructed with null function pointer deleter");
2487 static_assert(is_default_constructible<deleter_type>::value,
2488 "unique_ptr::deleter_type is not default constructible");
2489 }
2490 _LIBCPP_INLINE_VISIBILITY
2491 unique_ptr(nullptr_t) : __ptr_(pointer())
2492 {
2493 static_assert(!is_pointer<deleter_type>::value,
2494 "unique_ptr constructed with null function pointer deleter");
2495 }
2496 _LIBCPP_INLINE_VISIBILITY
2497 explicit unique_ptr(pointer __p)
2498 : __ptr_(_VSTD::move(__p)) {
2499 static_assert(!is_pointer<deleter_type>::value,
2500 "unique_ptr constructed with null function pointer deleter");
2501 }
2502
2503 _LIBCPP_INLINE_VISIBILITY
2504 operator __rv<unique_ptr>() {
2505 return __rv<unique_ptr>(*this);
2506 }
2507
2508 _LIBCPP_INLINE_VISIBILITY
2509 unique_ptr(__rv<unique_ptr> __u)
2510 : __ptr_(__u->release(),
2511 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2512
2513 template <class _Up, class _Ep>
2514 _LIBCPP_INLINE_VISIBILITY
2515 typename enable_if<
2516 !is_array<_Up>::value &&
2517 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2518 pointer>::value &&
2519 is_assignable<deleter_type&, _Ep&>::value,
2520 unique_ptr&>::type
2521 operator=(unique_ptr<_Up, _Ep> __u) {
2522 reset(__u.release());
2523 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2524 return *this;
2525 }
2526
2527 _LIBCPP_INLINE_VISIBILITY
2528 unique_ptr(pointer __p, deleter_type __d)
2529 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2530#endif // _LIBCPP_CXX03_LANG
2531
2532#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2533 template <class _Up>
2534 _LIBCPP_INLINE_VISIBILITY
2535 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2536 is_same<_Dp, default_delete<_Tp> >::value,
2537 unique_ptr&>::type
2538 operator=(auto_ptr<_Up> __p) {
2539 reset(__p.release());
2540 return *this;
2541 }
2542#endif
2543
2544 _LIBCPP_INLINE_VISIBILITY
2545 ~unique_ptr() { reset(); }
2546
2547 _LIBCPP_INLINE_VISIBILITY
2548 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2549 reset();
2550 return *this;
2551 }
2552
2553 _LIBCPP_INLINE_VISIBILITY
2554 typename add_lvalue_reference<_Tp>::type
2555 operator*() const {
2556 return *__ptr_.first();
2557 }
2558 _LIBCPP_INLINE_VISIBILITY
2559 pointer operator->() const _NOEXCEPT {
2560 return __ptr_.first();
2561 }
2562 _LIBCPP_INLINE_VISIBILITY
2563 pointer get() const _NOEXCEPT {
2564 return __ptr_.first();
2565 }
2566 _LIBCPP_INLINE_VISIBILITY
2567 deleter_type& get_deleter() _NOEXCEPT {
2568 return __ptr_.second();
2569 }
2570 _LIBCPP_INLINE_VISIBILITY
2571 const deleter_type& get_deleter() const _NOEXCEPT {
2572 return __ptr_.second();
2573 }
2574 _LIBCPP_INLINE_VISIBILITY
2575 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2576 return __ptr_.first() != nullptr;
2577 }
2578
2579 _LIBCPP_INLINE_VISIBILITY
2580 pointer release() _NOEXCEPT {
2581 pointer __t = __ptr_.first();
2582 __ptr_.first() = pointer();
2583 return __t;
2584 }
2585
2586 _LIBCPP_INLINE_VISIBILITY
2587 void reset(pointer __p = pointer()) _NOEXCEPT {
2588 pointer __tmp = __ptr_.first();
2589 __ptr_.first() = __p;
2590 if (__tmp)
2591 __ptr_.second()(__tmp);
2592 }
2593
2594 _LIBCPP_INLINE_VISIBILITY
2595 void swap(unique_ptr& __u) _NOEXCEPT {
2596 __ptr_.swap(__u.__ptr_);
2597 }
2598};
2599
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002600
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002602class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002604 typedef _Tp element_type;
2605 typedef _Dp deleter_type;
2606 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2607
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002609 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610
Eric Fiselier31127cd2017-04-16 02:14:31 +00002611 template <class _From>
2612 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613
Eric Fiselier31127cd2017-04-16 02:14:31 +00002614 template <class _FromElem>
2615 struct _CheckArrayPointerConversion<_FromElem*>
2616 : integral_constant<bool,
2617 is_same<_FromElem*, pointer>::value ||
2618 (is_same<pointer, element_type*>::value &&
2619 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2620 >
2621 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002623#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002624 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002625
2626 template <bool _Dummy>
2627 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002628 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002629
2630 template <bool _Dummy>
2631 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002632 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002633
2634 template <bool _Dummy>
2635 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002636 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002637
2638 template <bool _Dummy, class _Deleter = typename __dependent_type<
2639 __identity<deleter_type>, _Dummy>::type>
2640 using _EnableIfDeleterDefaultConstructible =
2641 typename enable_if<is_default_constructible<_Deleter>::value &&
2642 !is_pointer<_Deleter>::value>::type;
2643
2644 template <class _ArgType>
2645 using _EnableIfDeleterConstructible =
2646 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2647
2648 template <class _Pp>
2649 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002650 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002651 >::type;
2652
2653 template <class _UPtr, class _Up,
2654 class _ElemT = typename _UPtr::element_type>
2655 using _EnableIfMoveConvertible = typename enable_if<
2656 is_array<_Up>::value &&
2657 is_same<pointer, element_type*>::value &&
2658 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2659 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2660 >::type;
2661
2662 template <class _UDel>
2663 using _EnableIfDeleterConvertible = typename enable_if<
2664 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2665 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2666 >::type;
2667
2668 template <class _UDel>
2669 using _EnableIfDeleterAssignable = typename enable_if<
2670 is_assignable<_Dp&, _UDel&&>::value
2671 >::type;
2672
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002674 template <bool _Dummy = true,
2675 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2676 _LIBCPP_INLINE_VISIBILITY
2677 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002679 template <bool _Dummy = true,
2680 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2681 _LIBCPP_INLINE_VISIBILITY
2682 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002684 template <class _Pp, bool _Dummy = true,
2685 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2686 class = _EnableIfPointerConvertible<_Pp>>
2687 _LIBCPP_INLINE_VISIBILITY
2688 explicit unique_ptr(_Pp __p) noexcept
2689 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002691 template <class _Pp, bool _Dummy = true,
2692 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2693 class = _EnableIfPointerConvertible<_Pp>>
2694 _LIBCPP_INLINE_VISIBILITY
2695 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2696 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002698 template <bool _Dummy = true,
2699 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2700 _LIBCPP_INLINE_VISIBILITY
2701 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2702 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002704 template <class _Pp, bool _Dummy = true,
2705 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2706 class = _EnableIfPointerConvertible<_Pp>>
2707 _LIBCPP_INLINE_VISIBILITY
2708 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2709 : __ptr_(__p, _VSTD::move(__d)) {
2710 static_assert(!is_reference<deleter_type>::value,
2711 "rvalue deleter bound to reference");
2712 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002714 template <bool _Dummy = true,
2715 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2716 _LIBCPP_INLINE_VISIBILITY
2717 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2718 : __ptr_(nullptr, _VSTD::move(__d)) {
2719 static_assert(!is_reference<deleter_type>::value,
2720 "rvalue deleter bound to reference");
2721 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002722
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002723 template <class _Pp, bool _Dummy = true,
2724 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2725 class = _EnableIfPointerConvertible<_Pp>>
2726 _LIBCPP_INLINE_VISIBILITY
2727 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002728
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002729 _LIBCPP_INLINE_VISIBILITY
2730 unique_ptr(unique_ptr&& __u) noexcept
2731 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2732 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002733
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002734 _LIBCPP_INLINE_VISIBILITY
2735 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2736 reset(__u.release());
2737 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2738 return *this;
2739 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002741 template <class _Up, class _Ep,
2742 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2743 class = _EnableIfDeleterConvertible<_Ep>
2744 >
2745 _LIBCPP_INLINE_VISIBILITY
2746 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002747 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002748 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002750 template <class _Up, class _Ep,
2751 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2752 class = _EnableIfDeleterAssignable<_Ep>
2753 >
2754 _LIBCPP_INLINE_VISIBILITY
2755 unique_ptr&
2756 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2757 reset(__u.release());
2758 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2759 return *this;
2760 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002762#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002764 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002765
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002766 unique_ptr(unique_ptr&);
2767 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2768
2769 unique_ptr& operator=(unique_ptr&);
2770 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2771
2772 template <class _Up>
2773 unique_ptr(_Up __u,
2774 typename conditional<
2775 is_reference<deleter_type>::value, deleter_type,
2776 typename add_lvalue_reference<const deleter_type>::type>::type,
2777 typename enable_if<is_convertible<_Up, pointer>::value,
2778 __nat>::type = __nat());
2779public:
2780 _LIBCPP_INLINE_VISIBILITY
2781 unique_ptr() : __ptr_(pointer()) {
2782 static_assert(!is_pointer<deleter_type>::value,
2783 "unique_ptr constructed with null function pointer deleter");
2784 }
2785 _LIBCPP_INLINE_VISIBILITY
2786 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2787 static_assert(!is_pointer<deleter_type>::value,
2788 "unique_ptr constructed with null function pointer deleter");
2789 }
2790
2791 _LIBCPP_INLINE_VISIBILITY
2792 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2793 static_assert(!is_pointer<deleter_type>::value,
2794 "unique_ptr constructed with null function pointer deleter");
2795 }
2796
2797 _LIBCPP_INLINE_VISIBILITY
2798 unique_ptr(pointer __p, deleter_type __d)
2799 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2800
2801 _LIBCPP_INLINE_VISIBILITY
2802 unique_ptr(nullptr_t, deleter_type __d)
2803 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2804
2805 _LIBCPP_INLINE_VISIBILITY
2806 operator __rv<unique_ptr>() {
2807 return __rv<unique_ptr>(*this);
2808 }
2809
2810 _LIBCPP_INLINE_VISIBILITY
2811 unique_ptr(__rv<unique_ptr> __u)
2812 : __ptr_(__u->release(),
2813 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2814
2815 _LIBCPP_INLINE_VISIBILITY
2816 unique_ptr& operator=(__rv<unique_ptr> __u) {
2817 reset(__u->release());
2818 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2819 return *this;
2820 }
2821
2822#endif // _LIBCPP_CXX03_LANG
2823
2824public:
2825 _LIBCPP_INLINE_VISIBILITY
2826 ~unique_ptr() { reset(); }
2827
2828 _LIBCPP_INLINE_VISIBILITY
2829 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2830 reset();
2831 return *this;
2832 }
2833
2834 _LIBCPP_INLINE_VISIBILITY
2835 typename add_lvalue_reference<_Tp>::type
2836 operator[](size_t __i) const {
2837 return __ptr_.first()[__i];
2838 }
2839 _LIBCPP_INLINE_VISIBILITY
2840 pointer get() const _NOEXCEPT {
2841 return __ptr_.first();
2842 }
2843
2844 _LIBCPP_INLINE_VISIBILITY
2845 deleter_type& get_deleter() _NOEXCEPT {
2846 return __ptr_.second();
2847 }
2848
2849 _LIBCPP_INLINE_VISIBILITY
2850 const deleter_type& get_deleter() const _NOEXCEPT {
2851 return __ptr_.second();
2852 }
2853 _LIBCPP_INLINE_VISIBILITY
2854 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2855 return __ptr_.first() != nullptr;
2856 }
2857
2858 _LIBCPP_INLINE_VISIBILITY
2859 pointer release() _NOEXCEPT {
2860 pointer __t = __ptr_.first();
2861 __ptr_.first() = pointer();
2862 return __t;
2863 }
2864
2865 template <class _Pp>
2866 _LIBCPP_INLINE_VISIBILITY
2867 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002868 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002869 >::type
2870 reset(_Pp __p) _NOEXCEPT {
2871 pointer __tmp = __ptr_.first();
2872 __ptr_.first() = __p;
2873 if (__tmp)
2874 __ptr_.second()(__tmp);
2875 }
2876
2877 _LIBCPP_INLINE_VISIBILITY
2878 void reset(nullptr_t = nullptr) _NOEXCEPT {
2879 pointer __tmp = __ptr_.first();
2880 __ptr_.first() = nullptr;
2881 if (__tmp)
2882 __ptr_.second()(__tmp);
2883 }
2884
2885 _LIBCPP_INLINE_VISIBILITY
2886 void swap(unique_ptr& __u) _NOEXCEPT {
2887 __ptr_.swap(__u.__ptr_);
2888 }
2889
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890};
2891
2892template <class _Tp, class _Dp>
2893inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002894typename enable_if<
2895 __is_swappable<_Dp>::value,
2896 void
2897>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002898swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899
2900template <class _T1, class _D1, class _T2, class _D2>
2901inline _LIBCPP_INLINE_VISIBILITY
2902bool
2903operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2904
2905template <class _T1, class _D1, class _T2, class _D2>
2906inline _LIBCPP_INLINE_VISIBILITY
2907bool
2908operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2909
2910template <class _T1, class _D1, class _T2, class _D2>
2911inline _LIBCPP_INLINE_VISIBILITY
2912bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002913operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2914{
2915 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2916 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002917 typedef typename common_type<_P1, _P2>::type _Vp;
2918 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002919}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920
2921template <class _T1, class _D1, class _T2, class _D2>
2922inline _LIBCPP_INLINE_VISIBILITY
2923bool
2924operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2925
2926template <class _T1, class _D1, class _T2, class _D2>
2927inline _LIBCPP_INLINE_VISIBILITY
2928bool
2929operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2930
2931template <class _T1, class _D1, class _T2, class _D2>
2932inline _LIBCPP_INLINE_VISIBILITY
2933bool
2934operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2935
Howard Hinnantb17caf92012-02-21 21:02:58 +00002936template <class _T1, class _D1>
2937inline _LIBCPP_INLINE_VISIBILITY
2938bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002939operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002940{
2941 return !__x;
2942}
2943
2944template <class _T1, class _D1>
2945inline _LIBCPP_INLINE_VISIBILITY
2946bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002947operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002948{
2949 return !__x;
2950}
2951
2952template <class _T1, class _D1>
2953inline _LIBCPP_INLINE_VISIBILITY
2954bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002955operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002956{
2957 return static_cast<bool>(__x);
2958}
2959
2960template <class _T1, class _D1>
2961inline _LIBCPP_INLINE_VISIBILITY
2962bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002963operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002964{
2965 return static_cast<bool>(__x);
2966}
2967
2968template <class _T1, class _D1>
2969inline _LIBCPP_INLINE_VISIBILITY
2970bool
2971operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2972{
2973 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2974 return less<_P1>()(__x.get(), nullptr);
2975}
2976
2977template <class _T1, class _D1>
2978inline _LIBCPP_INLINE_VISIBILITY
2979bool
2980operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2981{
2982 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2983 return less<_P1>()(nullptr, __x.get());
2984}
2985
2986template <class _T1, class _D1>
2987inline _LIBCPP_INLINE_VISIBILITY
2988bool
2989operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2990{
2991 return nullptr < __x;
2992}
2993
2994template <class _T1, class _D1>
2995inline _LIBCPP_INLINE_VISIBILITY
2996bool
2997operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2998{
2999 return __x < nullptr;
3000}
3001
3002template <class _T1, class _D1>
3003inline _LIBCPP_INLINE_VISIBILITY
3004bool
3005operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3006{
3007 return !(nullptr < __x);
3008}
3009
3010template <class _T1, class _D1>
3011inline _LIBCPP_INLINE_VISIBILITY
3012bool
3013operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3014{
3015 return !(__x < nullptr);
3016}
3017
3018template <class _T1, class _D1>
3019inline _LIBCPP_INLINE_VISIBILITY
3020bool
3021operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3022{
3023 return !(__x < nullptr);
3024}
3025
3026template <class _T1, class _D1>
3027inline _LIBCPP_INLINE_VISIBILITY
3028bool
3029operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3030{
3031 return !(nullptr < __x);
3032}
3033
Howard Hinnant9e028f12012-05-01 15:37:54 +00003034#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3035
3036template <class _Tp, class _Dp>
3037inline _LIBCPP_INLINE_VISIBILITY
3038unique_ptr<_Tp, _Dp>
3039move(unique_ptr<_Tp, _Dp>& __t)
3040{
3041 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3042}
3043
3044#endif
3045
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003046#if _LIBCPP_STD_VER > 11
3047
3048template<class _Tp>
3049struct __unique_if
3050{
3051 typedef unique_ptr<_Tp> __unique_single;
3052};
3053
3054template<class _Tp>
3055struct __unique_if<_Tp[]>
3056{
3057 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3058};
3059
3060template<class _Tp, size_t _Np>
3061struct __unique_if<_Tp[_Np]>
3062{
3063 typedef void __unique_array_known_bound;
3064};
3065
3066template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003067inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003068typename __unique_if<_Tp>::__unique_single
3069make_unique(_Args&&... __args)
3070{
3071 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3072}
3073
3074template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003075inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003076typename __unique_if<_Tp>::__unique_array_unknown_bound
3077make_unique(size_t __n)
3078{
3079 typedef typename remove_extent<_Tp>::type _Up;
3080 return unique_ptr<_Tp>(new _Up[__n]());
3081}
3082
3083template<class _Tp, class... _Args>
3084 typename __unique_if<_Tp>::__unique_array_known_bound
3085 make_unique(_Args&&...) = delete;
3086
3087#endif // _LIBCPP_STD_VER > 11
3088
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003089template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003090#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003091struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003092#else
3093struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3094 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3095#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003096{
3097 typedef unique_ptr<_Tp, _Dp> argument_type;
3098 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003099 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003100 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003101 {
3102 typedef typename argument_type::pointer pointer;
3103 return hash<pointer>()(__ptr.get());
3104 }
3105};
3106
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107struct __destruct_n
3108{
3109private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003110 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111
3112 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003113 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003114 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115
3116 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003117 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 {}
3119
Howard Hinnant719bda32011-05-28 14:41:13 +00003120 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003121 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003122 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123 {}
3124
Howard Hinnant719bda32011-05-28 14:41:13 +00003125 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003126 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003127 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 {}
3129public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003130 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003131 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132
3133 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003134 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003135 {__incr(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 __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003139 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140
3141 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003142 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003143 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144};
3145
3146template <class _Alloc>
3147class __allocator_destructor
3148{
3149 typedef allocator_traits<_Alloc> __alloc_traits;
3150public:
3151 typedef typename __alloc_traits::pointer pointer;
3152 typedef typename __alloc_traits::size_type size_type;
3153private:
3154 _Alloc& __alloc_;
3155 size_type __s_;
3156public:
3157 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003158 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003161 void operator()(pointer __p) _NOEXCEPT
3162 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163};
3164
3165template <class _InputIterator, class _ForwardIterator>
3166_ForwardIterator
3167uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3168{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003170#ifndef _LIBCPP_NO_EXCEPTIONS
3171 _ForwardIterator __s = __r;
3172 try
3173 {
3174#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003175 for (; __f != __l; ++__f, (void) ++__r)
3176 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003177#ifndef _LIBCPP_NO_EXCEPTIONS
3178 }
3179 catch (...)
3180 {
3181 for (; __s != __r; ++__s)
3182 __s->~value_type();
3183 throw;
3184 }
3185#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186 return __r;
3187}
3188
3189template <class _InputIterator, class _Size, class _ForwardIterator>
3190_ForwardIterator
3191uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3192{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003194#ifndef _LIBCPP_NO_EXCEPTIONS
3195 _ForwardIterator __s = __r;
3196 try
3197 {
3198#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003199 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3200 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003201#ifndef _LIBCPP_NO_EXCEPTIONS
3202 }
3203 catch (...)
3204 {
3205 for (; __s != __r; ++__s)
3206 __s->~value_type();
3207 throw;
3208 }
3209#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210 return __r;
3211}
3212
3213template <class _ForwardIterator, class _Tp>
3214void
3215uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3216{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003218#ifndef _LIBCPP_NO_EXCEPTIONS
3219 _ForwardIterator __s = __f;
3220 try
3221 {
3222#endif
3223 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003224 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003225#ifndef _LIBCPP_NO_EXCEPTIONS
3226 }
3227 catch (...)
3228 {
3229 for (; __s != __f; ++__s)
3230 __s->~value_type();
3231 throw;
3232 }
3233#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234}
3235
3236template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003237_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3239{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003241#ifndef _LIBCPP_NO_EXCEPTIONS
3242 _ForwardIterator __s = __f;
3243 try
3244 {
3245#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003246 for (; __n > 0; ++__f, (void) --__n)
3247 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003248#ifndef _LIBCPP_NO_EXCEPTIONS
3249 }
3250 catch (...)
3251 {
3252 for (; __s != __f; ++__s)
3253 __s->~value_type();
3254 throw;
3255 }
3256#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003257 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258}
3259
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003260#if _LIBCPP_STD_VER > 14
3261
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003262template <class _Tp>
3263inline _LIBCPP_INLINE_VISIBILITY
3264void destroy_at(_Tp* __loc) {
3265 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3266 __loc->~_Tp();
3267}
3268
3269template <class _ForwardIterator>
3270inline _LIBCPP_INLINE_VISIBILITY
3271void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3272 for (; __first != __last; ++__first)
3273 _VSTD::destroy_at(_VSTD::addressof(*__first));
3274}
3275
3276template <class _ForwardIterator, class _Size>
3277inline _LIBCPP_INLINE_VISIBILITY
3278_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3279 for (; __n > 0; (void)++__first, --__n)
3280 _VSTD::destroy_at(_VSTD::addressof(*__first));
3281 return __first;
3282}
3283
Eric Fiselier290c07c2016-10-11 21:13:44 +00003284template <class _ForwardIterator>
3285inline _LIBCPP_INLINE_VISIBILITY
3286void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3287 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3288 auto __idx = __first;
3289#ifndef _LIBCPP_NO_EXCEPTIONS
3290 try {
3291#endif
3292 for (; __idx != __last; ++__idx)
3293 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3294#ifndef _LIBCPP_NO_EXCEPTIONS
3295 } catch (...) {
3296 _VSTD::destroy(__first, __idx);
3297 throw;
3298 }
3299#endif
3300}
3301
3302template <class _ForwardIterator, class _Size>
3303inline _LIBCPP_INLINE_VISIBILITY
3304_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3305 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3306 auto __idx = __first;
3307#ifndef _LIBCPP_NO_EXCEPTIONS
3308 try {
3309#endif
3310 for (; __n > 0; (void)++__idx, --__n)
3311 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3312 return __idx;
3313#ifndef _LIBCPP_NO_EXCEPTIONS
3314 } catch (...) {
3315 _VSTD::destroy(__first, __idx);
3316 throw;
3317 }
3318#endif
3319}
3320
3321
3322template <class _ForwardIterator>
3323inline _LIBCPP_INLINE_VISIBILITY
3324void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3325 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3326 auto __idx = __first;
3327#ifndef _LIBCPP_NO_EXCEPTIONS
3328 try {
3329#endif
3330 for (; __idx != __last; ++__idx)
3331 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3332#ifndef _LIBCPP_NO_EXCEPTIONS
3333 } catch (...) {
3334 _VSTD::destroy(__first, __idx);
3335 throw;
3336 }
3337#endif
3338}
3339
3340template <class _ForwardIterator, class _Size>
3341inline _LIBCPP_INLINE_VISIBILITY
3342_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3343 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3344 auto __idx = __first;
3345#ifndef _LIBCPP_NO_EXCEPTIONS
3346 try {
3347#endif
3348 for (; __n > 0; (void)++__idx, --__n)
3349 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3350 return __idx;
3351#ifndef _LIBCPP_NO_EXCEPTIONS
3352 } catch (...) {
3353 _VSTD::destroy(__first, __idx);
3354 throw;
3355 }
3356#endif
3357}
3358
3359
3360template <class _InputIt, class _ForwardIt>
3361inline _LIBCPP_INLINE_VISIBILITY
3362_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3363 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3364 auto __idx = __first_res;
3365#ifndef _LIBCPP_NO_EXCEPTIONS
3366 try {
3367#endif
3368 for (; __first != __last; (void)++__idx, ++__first)
3369 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3370 return __idx;
3371#ifndef _LIBCPP_NO_EXCEPTIONS
3372 } catch (...) {
3373 _VSTD::destroy(__first_res, __idx);
3374 throw;
3375 }
3376#endif
3377}
3378
3379template <class _InputIt, class _Size, class _ForwardIt>
3380inline _LIBCPP_INLINE_VISIBILITY
3381pair<_InputIt, _ForwardIt>
3382uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3383 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3384 auto __idx = __first_res;
3385#ifndef _LIBCPP_NO_EXCEPTIONS
3386 try {
3387#endif
3388 for (; __n > 0; ++__idx, (void)++__first, --__n)
3389 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3390 return {__first, __idx};
3391#ifndef _LIBCPP_NO_EXCEPTIONS
3392 } catch (...) {
3393 _VSTD::destroy(__first_res, __idx);
3394 throw;
3395 }
3396#endif
3397}
3398
3399
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003400#endif // _LIBCPP_STD_VER > 14
3401
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003402// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3403// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003404// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003405#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3406 && defined(__ATOMIC_RELAXED) \
3407 && defined(__ATOMIC_ACQ_REL)
3408# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3409#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3410# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3411#endif
3412
3413template <class _Tp>
3414inline _LIBCPP_INLINE_VISIBILITY _Tp
3415__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3416{
3417#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3418 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3419#else
3420 return __t += 1;
3421#endif
3422}
3423
3424template <class _Tp>
3425inline _LIBCPP_INLINE_VISIBILITY _Tp
3426__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3427{
3428#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3429 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3430#else
3431 return __t -= 1;
3432#endif
3433}
3434
Howard Hinnant756c69b2010-09-22 16:48:34 +00003435class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436 : public std::exception
3437{
3438public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003439 virtual ~bad_weak_ptr() _NOEXCEPT;
3440 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441};
3442
Marshall Clow8fea1612016-08-25 15:09:01 +00003443_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3444void __throw_bad_weak_ptr()
3445{
3446#ifndef _LIBCPP_NO_EXCEPTIONS
3447 throw bad_weak_ptr();
3448#else
3449 _VSTD::abort();
3450#endif
3451}
3452
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003453template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003455class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456{
3457 __shared_count(const __shared_count&);
3458 __shared_count& operator=(const __shared_count&);
3459
3460protected:
3461 long __shared_owners_;
3462 virtual ~__shared_count();
3463private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003464 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465
3466public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003468 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469 : __shared_owners_(__refs) {}
3470
Eric Fiselier98848572017-01-17 03:16:26 +00003471#if defined(_LIBCPP_BUILDING_MEMORY) && \
3472 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003473 void __add_shared() _NOEXCEPT;
3474 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003475#else
3476 _LIBCPP_INLINE_VISIBILITY
3477 void __add_shared() _NOEXCEPT {
3478 __libcpp_atomic_refcount_increment(__shared_owners_);
3479 }
3480 _LIBCPP_INLINE_VISIBILITY
3481 bool __release_shared() _NOEXCEPT {
3482 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3483 __on_zero_shared();
3484 return true;
3485 }
3486 return false;
3487 }
3488#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003489 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003490 long use_count() const _NOEXCEPT {
3491 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3492 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493};
3494
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003495class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496 : private __shared_count
3497{
3498 long __shared_weak_owners_;
3499
3500public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003502 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503 : __shared_count(__refs),
3504 __shared_weak_owners_(__refs) {}
3505protected:
3506 virtual ~__shared_weak_count();
3507
3508public:
Eric Fiselier98848572017-01-17 03:16:26 +00003509#if defined(_LIBCPP_BUILDING_MEMORY) && \
3510 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003511 void __add_shared() _NOEXCEPT;
3512 void __add_weak() _NOEXCEPT;
3513 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003514#else
3515 _LIBCPP_INLINE_VISIBILITY
3516 void __add_shared() _NOEXCEPT {
3517 __shared_count::__add_shared();
3518 }
3519 _LIBCPP_INLINE_VISIBILITY
3520 void __add_weak() _NOEXCEPT {
3521 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3522 }
3523 _LIBCPP_INLINE_VISIBILITY
3524 void __release_shared() _NOEXCEPT {
3525 if (__shared_count::__release_shared())
3526 __release_weak();
3527 }
3528#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003529 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003531 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3532 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533
Howard Hinnant807d6332013-02-25 15:50:36 +00003534 // Define the function out only if we build static libc++ without RTTI.
3535 // Otherwise we may break clients who need to compile their projects with
3536 // -fno-rtti and yet link against a libc++.dylib compiled
3537 // without -fno-rtti.
3538#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003539 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003540#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003542 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543};
3544
3545template <class _Tp, class _Dp, class _Alloc>
3546class __shared_ptr_pointer
3547 : public __shared_weak_count
3548{
3549 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3550public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003553 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554
Howard Hinnant72f73582010-08-11 17:04:31 +00003555#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003556 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003557#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558
3559private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003560 virtual void __on_zero_shared() _NOEXCEPT;
3561 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562};
3563
Howard Hinnant72f73582010-08-11 17:04:31 +00003564#ifndef _LIBCPP_NO_RTTI
3565
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566template <class _Tp, class _Dp, class _Alloc>
3567const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003568__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003570 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571}
3572
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003573#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003574
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575template <class _Tp, class _Dp, class _Alloc>
3576void
Howard Hinnant719bda32011-05-28 14:41:13 +00003577__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578{
3579 __data_.first().second()(__data_.first().first());
3580 __data_.first().second().~_Dp();
3581}
3582
3583template <class _Tp, class _Dp, class _Alloc>
3584void
Howard Hinnant719bda32011-05-28 14:41:13 +00003585__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003587 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3588 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003589 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3590
Eric Fiselierf8898c82015-02-05 23:01:40 +00003591 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003593 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594}
3595
3596template <class _Tp, class _Alloc>
3597class __shared_ptr_emplace
3598 : public __shared_weak_count
3599{
3600 __compressed_pair<_Alloc, _Tp> __data_;
3601public:
3602#ifndef _LIBCPP_HAS_NO_VARIADICS
3603
Howard Hinnant756c69b2010-09-22 16:48:34 +00003604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003606 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607
3608 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003611 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3612 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613
3614#else // _LIBCPP_HAS_NO_VARIADICS
3615
Howard Hinnant756c69b2010-09-22 16:48:34 +00003616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617 __shared_ptr_emplace(_Alloc __a)
3618 : __data_(__a) {}
3619
3620 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3623 : __data_(__a, _Tp(__a0)) {}
3624
3625 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3628 : __data_(__a, _Tp(__a0, __a1)) {}
3629
3630 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3633 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3634
3635#endif // _LIBCPP_HAS_NO_VARIADICS
3636
3637private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003638 virtual void __on_zero_shared() _NOEXCEPT;
3639 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003640public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003642 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643};
3644
3645template <class _Tp, class _Alloc>
3646void
Howard Hinnant719bda32011-05-28 14:41:13 +00003647__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648{
3649 __data_.second().~_Tp();
3650}
3651
3652template <class _Tp, class _Alloc>
3653void
Howard Hinnant719bda32011-05-28 14:41:13 +00003654__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003656 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3657 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003658 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003659 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003661 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662}
3663
Erik Pilkington2a398762017-05-25 15:43:31 +00003664struct __shared_ptr_dummy_rebind_allocator_type;
3665template <>
3666class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3667{
3668public:
3669 template <class _Other>
3670 struct rebind
3671 {
3672 typedef allocator<_Other> other;
3673 };
3674};
3675
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003676template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677
3678template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003679class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003681public:
3682 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003683
Eric Fiselierae5b6672016-06-27 01:02:43 +00003684#if _LIBCPP_STD_VER > 14
3685 typedef weak_ptr<_Tp> weak_type;
3686#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687private:
3688 element_type* __ptr_;
3689 __shared_weak_count* __cntrl_;
3690
3691 struct __nat {int __for_bool_;};
3692public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003694 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003696 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003697 template<class _Yp>
3698 explicit shared_ptr(_Yp* __p,
3699 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3700 template<class _Yp, class _Dp>
3701 shared_ptr(_Yp* __p, _Dp __d,
3702 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3703 template<class _Yp, class _Dp, class _Alloc>
3704 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3705 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3707 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003708 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003710 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003714 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003715 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003716#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003718 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003719 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003720 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003721 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003722#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003724 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003725#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003726#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
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 Hinnant3b6579a2010-08-22 00:02:43 +00003730#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003731 template<class _Yp>
3732 shared_ptr(auto_ptr<_Yp> __r,
3733 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003735#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003736#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003737 template <class _Yp, class _Dp>
3738 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3739 typename enable_if
3740 <
3741 !is_lvalue_reference<_Dp>::value &&
3742 !is_array<_Yp>::value &&
3743 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3744 __nat
3745 >::type = __nat());
3746 template <class _Yp, class _Dp>
3747 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3748 typename enable_if
3749 <
3750 is_lvalue_reference<_Dp>::value &&
3751 !is_array<_Yp>::value &&
3752 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3753 __nat
3754 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003755#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003756 template <class _Yp, class _Dp>
3757 shared_ptr(unique_ptr<_Yp, _Dp>,
3758 typename enable_if
3759 <
3760 !is_lvalue_reference<_Dp>::value &&
3761 !is_array<_Yp>::value &&
3762 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3763 __nat
3764 >::type = __nat());
3765 template <class _Yp, class _Dp>
3766 shared_ptr(unique_ptr<_Yp, _Dp>,
3767 typename enable_if
3768 <
3769 is_lvalue_reference<_Dp>::value &&
3770 !is_array<_Yp>::value &&
3771 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3772 __nat
3773 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003774#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775
3776 ~shared_ptr();
3777
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003779 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003780 template<class _Yp>
3781 typename enable_if
3782 <
3783 is_convertible<_Yp*, element_type*>::value,
3784 shared_ptr&
3785 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003787 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003788#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003790 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003791 template<class _Yp>
3792 typename enable_if
3793 <
3794 is_convertible<_Yp*, element_type*>::value,
3795 shared_ptr<_Tp>&
3796 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003798 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003799#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003800 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003802 typename enable_if
3803 <
3804 !is_array<_Yp>::value &&
3805 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003806 shared_ptr
3807 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003808 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003809#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003810#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003811#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003812 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003814 typename enable_if
3815 <
3816 !is_array<_Yp>::value &&
3817 is_convertible<_Yp*, element_type*>::value,
3818 shared_ptr&
3819 >::type
3820 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003822#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003823 template <class _Yp, class _Dp>
3824 typename enable_if
3825 <
3826 !is_array<_Yp>::value &&
3827 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3828 shared_ptr&
3829 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003830#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003832 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003833#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003835 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836#endif
3837
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003839 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003841 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003842 template<class _Yp>
3843 typename enable_if
3844 <
3845 is_convertible<_Yp*, element_type*>::value,
3846 void
3847 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003849 reset(_Yp* __p);
3850 template<class _Yp, class _Dp>
3851 typename enable_if
3852 <
3853 is_convertible<_Yp*, element_type*>::value,
3854 void
3855 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003857 reset(_Yp* __p, _Dp __d);
3858 template<class _Yp, class _Dp, class _Alloc>
3859 typename enable_if
3860 <
3861 is_convertible<_Yp*, element_type*>::value,
3862 void
3863 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003865 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866
Howard Hinnant756c69b2010-09-22 16:48:34 +00003867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003868 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003870 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3871 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003873 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003875 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003877 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003879 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
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(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003883 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003884 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003885 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003886 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003888 _LIBCPP_INLINE_VISIBILITY
3889 bool
3890 __owner_equivalent(const shared_ptr& __p) const
3891 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892
Howard Hinnant72f73582010-08-11 17:04:31 +00003893#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003896 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003897 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003898 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003899 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003900#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901
3902#ifndef _LIBCPP_HAS_NO_VARIADICS
3903
3904 template<class ..._Args>
3905 static
3906 shared_ptr<_Tp>
3907 make_shared(_Args&& ...__args);
3908
3909 template<class _Alloc, class ..._Args>
3910 static
3911 shared_ptr<_Tp>
3912 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3913
3914#else // _LIBCPP_HAS_NO_VARIADICS
3915
3916 static shared_ptr<_Tp> make_shared();
3917
3918 template<class _A0>
3919 static shared_ptr<_Tp> make_shared(_A0&);
3920
3921 template<class _A0, class _A1>
3922 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3923
3924 template<class _A0, class _A1, class _A2>
3925 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3926
3927 template<class _Alloc>
3928 static shared_ptr<_Tp>
3929 allocate_shared(const _Alloc& __a);
3930
3931 template<class _Alloc, class _A0>
3932 static shared_ptr<_Tp>
3933 allocate_shared(const _Alloc& __a, _A0& __a0);
3934
3935 template<class _Alloc, class _A0, class _A1>
3936 static shared_ptr<_Tp>
3937 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3938
3939 template<class _Alloc, class _A0, class _A1, class _A2>
3940 static shared_ptr<_Tp>
3941 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3942
3943#endif // _LIBCPP_HAS_NO_VARIADICS
3944
3945private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003946 template <class _Yp, bool = is_function<_Yp>::value>
3947 struct __shared_ptr_default_allocator
3948 {
3949 typedef allocator<_Yp> type;
3950 };
3951
3952 template <class _Yp>
3953 struct __shared_ptr_default_allocator<_Yp, true>
3954 {
3955 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3956 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003958 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003959 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003960 typename enable_if<is_convertible<_OrigPtr*,
3961 const enable_shared_from_this<_Yp>*
3962 >::value,
3963 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003964 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3965 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003967 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003968 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003969 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003970 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3971 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003972 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973 }
3974
Erik Pilkington2a398762017-05-25 15:43:31 +00003975 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003977 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3978 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979};
3980
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003981
Howard Hinnantc51e1022010-05-11 19:42:16 +00003982template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003983inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003984_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003985shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986 : __ptr_(0),
3987 __cntrl_(0)
3988{
3989}
3990
3991template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003992inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003993_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003994shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995 : __ptr_(0),
3996 __cntrl_(0)
3997{
3998}
3999
4000template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004001template<class _Yp>
4002shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4003 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004 : __ptr_(__p)
4005{
4006 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004007 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4008 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4009 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004010 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004011 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012}
4013
4014template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004015template<class _Yp, class _Dp>
4016shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4017 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018 : __ptr_(__p)
4019{
4020#ifndef _LIBCPP_NO_EXCEPTIONS
4021 try
4022 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004023#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004024 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4025 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4026 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004027 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028#ifndef _LIBCPP_NO_EXCEPTIONS
4029 }
4030 catch (...)
4031 {
4032 __d(__p);
4033 throw;
4034 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004035#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036}
4037
4038template<class _Tp>
4039template<class _Dp>
4040shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4041 : __ptr_(0)
4042{
4043#ifndef _LIBCPP_NO_EXCEPTIONS
4044 try
4045 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004046#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004047 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4048 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4049 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050#ifndef _LIBCPP_NO_EXCEPTIONS
4051 }
4052 catch (...)
4053 {
4054 __d(__p);
4055 throw;
4056 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004057#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058}
4059
4060template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004061template<class _Yp, class _Dp, class _Alloc>
4062shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4063 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064 : __ptr_(__p)
4065{
4066#ifndef _LIBCPP_NO_EXCEPTIONS
4067 try
4068 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004069#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004071 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072 typedef __allocator_destructor<_A2> _D2;
4073 _A2 __a2(__a);
4074 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004075 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4076 _CntrlBlk(__p, __d, __a);
4077 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004078 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079#ifndef _LIBCPP_NO_EXCEPTIONS
4080 }
4081 catch (...)
4082 {
4083 __d(__p);
4084 throw;
4085 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004086#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087}
4088
4089template<class _Tp>
4090template<class _Dp, class _Alloc>
4091shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4092 : __ptr_(0)
4093{
4094#ifndef _LIBCPP_NO_EXCEPTIONS
4095 try
4096 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004097#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004099 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100 typedef __allocator_destructor<_A2> _D2;
4101 _A2 __a2(__a);
4102 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004103 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4104 _CntrlBlk(__p, __d, __a);
4105 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106#ifndef _LIBCPP_NO_EXCEPTIONS
4107 }
4108 catch (...)
4109 {
4110 __d(__p);
4111 throw;
4112 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004113#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114}
4115
4116template<class _Tp>
4117template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004118inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004119shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120 : __ptr_(__p),
4121 __cntrl_(__r.__cntrl_)
4122{
4123 if (__cntrl_)
4124 __cntrl_->__add_shared();
4125}
4126
4127template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004128inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004129shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130 : __ptr_(__r.__ptr_),
4131 __cntrl_(__r.__cntrl_)
4132{
4133 if (__cntrl_)
4134 __cntrl_->__add_shared();
4135}
4136
4137template<class _Tp>
4138template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004139inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004141 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004142 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143 : __ptr_(__r.__ptr_),
4144 __cntrl_(__r.__cntrl_)
4145{
4146 if (__cntrl_)
4147 __cntrl_->__add_shared();
4148}
4149
Howard Hinnant74279a52010-09-04 23:28:19 +00004150#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151
4152template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004153inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004154shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155 : __ptr_(__r.__ptr_),
4156 __cntrl_(__r.__cntrl_)
4157{
4158 __r.__ptr_ = 0;
4159 __r.__cntrl_ = 0;
4160}
4161
4162template<class _Tp>
4163template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004164inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004166 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004167 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168 : __ptr_(__r.__ptr_),
4169 __cntrl_(__r.__cntrl_)
4170{
4171 __r.__ptr_ = 0;
4172 __r.__cntrl_ = 0;
4173}
4174
Howard Hinnant74279a52010-09-04 23:28:19 +00004175#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176
Marshall Clowb22274f2017-01-24 22:22:33 +00004177#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004179template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004180#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004181shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004183shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004185 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186 : __ptr_(__r.get())
4187{
4188 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4189 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004190 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191 __r.release();
4192}
Marshall Clowb22274f2017-01-24 22:22:33 +00004193#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194
4195template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004196template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004197#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4199#else
4200shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4201#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004202 typename enable_if
4203 <
4204 !is_lvalue_reference<_Dp>::value &&
4205 !is_array<_Yp>::value &&
4206 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4207 __nat
4208 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209 : __ptr_(__r.get())
4210{
Marshall Clow35cde742015-05-10 13:59:45 +00004211#if _LIBCPP_STD_VER > 11
4212 if (__ptr_ == nullptr)
4213 __cntrl_ = nullptr;
4214 else
4215#endif
4216 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004217 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4218 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4219 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004220 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004221 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222 __r.release();
4223}
4224
4225template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004226template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004227#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004228shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4229#else
4230shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4231#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004232 typename enable_if
4233 <
4234 is_lvalue_reference<_Dp>::value &&
4235 !is_array<_Yp>::value &&
4236 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4237 __nat
4238 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239 : __ptr_(__r.get())
4240{
Marshall Clow35cde742015-05-10 13:59:45 +00004241#if _LIBCPP_STD_VER > 11
4242 if (__ptr_ == nullptr)
4243 __cntrl_ = nullptr;
4244 else
4245#endif
4246 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004247 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004248 typedef __shared_ptr_pointer<_Yp*,
4249 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004250 _AllocT > _CntrlBlk;
4251 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004252 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004253 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254 __r.release();
4255}
4256
4257#ifndef _LIBCPP_HAS_NO_VARIADICS
4258
4259template<class _Tp>
4260template<class ..._Args>
4261shared_ptr<_Tp>
4262shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4263{
4264 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4265 typedef allocator<_CntrlBlk> _A2;
4266 typedef __allocator_destructor<_A2> _D2;
4267 _A2 __a2;
4268 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004269 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270 shared_ptr<_Tp> __r;
4271 __r.__ptr_ = __hold2.get()->get();
4272 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004273 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274 return __r;
4275}
4276
4277template<class _Tp>
4278template<class _Alloc, class ..._Args>
4279shared_ptr<_Tp>
4280shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4281{
4282 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004283 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284 typedef __allocator_destructor<_A2> _D2;
4285 _A2 __a2(__a);
4286 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004287 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4288 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289 shared_ptr<_Tp> __r;
4290 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004291 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004292 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293 return __r;
4294}
4295
4296#else // _LIBCPP_HAS_NO_VARIADICS
4297
4298template<class _Tp>
4299shared_ptr<_Tp>
4300shared_ptr<_Tp>::make_shared()
4301{
4302 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4303 typedef allocator<_CntrlBlk> _Alloc2;
4304 typedef __allocator_destructor<_Alloc2> _D2;
4305 _Alloc2 __alloc2;
4306 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4307 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4308 shared_ptr<_Tp> __r;
4309 __r.__ptr_ = __hold2.get()->get();
4310 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004311 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004312 return __r;
4313}
4314
4315template<class _Tp>
4316template<class _A0>
4317shared_ptr<_Tp>
4318shared_ptr<_Tp>::make_shared(_A0& __a0)
4319{
4320 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4321 typedef allocator<_CntrlBlk> _Alloc2;
4322 typedef __allocator_destructor<_Alloc2> _D2;
4323 _Alloc2 __alloc2;
4324 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4325 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4326 shared_ptr<_Tp> __r;
4327 __r.__ptr_ = __hold2.get()->get();
4328 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004329 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330 return __r;
4331}
4332
4333template<class _Tp>
4334template<class _A0, class _A1>
4335shared_ptr<_Tp>
4336shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4337{
4338 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4339 typedef allocator<_CntrlBlk> _Alloc2;
4340 typedef __allocator_destructor<_Alloc2> _D2;
4341 _Alloc2 __alloc2;
4342 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4343 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4344 shared_ptr<_Tp> __r;
4345 __r.__ptr_ = __hold2.get()->get();
4346 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004347 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348 return __r;
4349}
4350
4351template<class _Tp>
4352template<class _A0, class _A1, class _A2>
4353shared_ptr<_Tp>
4354shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4355{
4356 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4357 typedef allocator<_CntrlBlk> _Alloc2;
4358 typedef __allocator_destructor<_Alloc2> _D2;
4359 _Alloc2 __alloc2;
4360 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4361 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4362 shared_ptr<_Tp> __r;
4363 __r.__ptr_ = __hold2.get()->get();
4364 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004365 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004366 return __r;
4367}
4368
4369template<class _Tp>
4370template<class _Alloc>
4371shared_ptr<_Tp>
4372shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4373{
4374 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004375 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376 typedef __allocator_destructor<_Alloc2> _D2;
4377 _Alloc2 __alloc2(__a);
4378 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004379 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4380 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004381 shared_ptr<_Tp> __r;
4382 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004383 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004384 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385 return __r;
4386}
4387
4388template<class _Tp>
4389template<class _Alloc, class _A0>
4390shared_ptr<_Tp>
4391shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4392{
4393 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004394 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004395 typedef __allocator_destructor<_Alloc2> _D2;
4396 _Alloc2 __alloc2(__a);
4397 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004398 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4399 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004400 shared_ptr<_Tp> __r;
4401 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004402 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004403 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404 return __r;
4405}
4406
4407template<class _Tp>
4408template<class _Alloc, class _A0, class _A1>
4409shared_ptr<_Tp>
4410shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4411{
4412 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004413 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004414 typedef __allocator_destructor<_Alloc2> _D2;
4415 _Alloc2 __alloc2(__a);
4416 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004417 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4418 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004419 shared_ptr<_Tp> __r;
4420 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004421 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004422 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423 return __r;
4424}
4425
4426template<class _Tp>
4427template<class _Alloc, class _A0, class _A1, class _A2>
4428shared_ptr<_Tp>
4429shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4430{
4431 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004432 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004433 typedef __allocator_destructor<_Alloc2> _D2;
4434 _Alloc2 __alloc2(__a);
4435 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004436 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4437 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438 shared_ptr<_Tp> __r;
4439 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004440 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004441 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442 return __r;
4443}
4444
4445#endif // _LIBCPP_HAS_NO_VARIADICS
4446
4447template<class _Tp>
4448shared_ptr<_Tp>::~shared_ptr()
4449{
4450 if (__cntrl_)
4451 __cntrl_->__release_shared();
4452}
4453
4454template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004455inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004456shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004457shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004458{
4459 shared_ptr(__r).swap(*this);
4460 return *this;
4461}
4462
4463template<class _Tp>
4464template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004465inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004466typename enable_if
4467<
Marshall Clow7e384b72017-01-10 16:59:33 +00004468 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004469 shared_ptr<_Tp>&
4470>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004471shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004472{
4473 shared_ptr(__r).swap(*this);
4474 return *this;
4475}
4476
Howard Hinnant74279a52010-09-04 23:28:19 +00004477#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004478
4479template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004480inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004482shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004483{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004484 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004485 return *this;
4486}
4487
4488template<class _Tp>
4489template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004490inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004491typename enable_if
4492<
Marshall Clow7e384b72017-01-10 16:59:33 +00004493 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004494 shared_ptr<_Tp>&
4495>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4497{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004498 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004499 return *this;
4500}
4501
Marshall Clowb22274f2017-01-24 22:22:33 +00004502#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503template<class _Tp>
4504template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004505inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004506typename enable_if
4507<
4508 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004509 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004510 shared_ptr<_Tp>
4511>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004512shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4513{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004514 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004515 return *this;
4516}
Marshall Clowb22274f2017-01-24 22:22:33 +00004517#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004518
4519template<class _Tp>
4520template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004521inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004522typename enable_if
4523<
4524 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004525 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004526 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004527 shared_ptr<_Tp>&
4528>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004529shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4530{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004531 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004532 return *this;
4533}
4534
Howard Hinnant74279a52010-09-04 23:28:19 +00004535#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536
Marshall Clowb22274f2017-01-24 22:22:33 +00004537#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538template<class _Tp>
4539template<class _Yp>
4540inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004541typename enable_if
4542<
4543 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004544 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004545 shared_ptr<_Tp>&
4546>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004547shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004548{
4549 shared_ptr(__r).swap(*this);
4550 return *this;
4551}
Marshall Clowb22274f2017-01-24 22:22:33 +00004552#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004553
4554template<class _Tp>
4555template <class _Yp, class _Dp>
4556inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004557typename enable_if
4558<
4559 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004560 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004561 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004562 shared_ptr<_Tp>&
4563>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004564shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4565{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004566 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004567 return *this;
4568}
4569
Howard Hinnant74279a52010-09-04 23:28:19 +00004570#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004571
4572template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004573inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004574void
Howard Hinnant719bda32011-05-28 14:41:13 +00004575shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004576{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004577 _VSTD::swap(__ptr_, __r.__ptr_);
4578 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004579}
4580
4581template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004582inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004583void
Howard Hinnant719bda32011-05-28 14:41:13 +00004584shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004585{
4586 shared_ptr().swap(*this);
4587}
4588
4589template<class _Tp>
4590template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004591inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004592typename enable_if
4593<
Marshall Clow7e384b72017-01-10 16:59:33 +00004594 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004595 void
4596>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597shared_ptr<_Tp>::reset(_Yp* __p)
4598{
4599 shared_ptr(__p).swap(*this);
4600}
4601
4602template<class _Tp>
4603template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004604inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004605typename enable_if
4606<
Marshall Clow7e384b72017-01-10 16:59:33 +00004607 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004608 void
4609>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004610shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4611{
4612 shared_ptr(__p, __d).swap(*this);
4613}
4614
4615template<class _Tp>
4616template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004617inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004618typename enable_if
4619<
Marshall Clow7e384b72017-01-10 16:59:33 +00004620 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004621 void
4622>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004623shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4624{
4625 shared_ptr(__p, __d, __a).swap(*this);
4626}
4627
4628#ifndef _LIBCPP_HAS_NO_VARIADICS
4629
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004630template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004631inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004632typename enable_if
4633<
4634 !is_array<_Tp>::value,
4635 shared_ptr<_Tp>
4636>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004637make_shared(_Args&& ...__args)
4638{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004639 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004640}
4641
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004642template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004643inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004644typename enable_if
4645<
4646 !is_array<_Tp>::value,
4647 shared_ptr<_Tp>
4648>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004649allocate_shared(const _Alloc& __a, _Args&& ...__args)
4650{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004651 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004652}
4653
4654#else // _LIBCPP_HAS_NO_VARIADICS
4655
4656template<class _Tp>
4657inline _LIBCPP_INLINE_VISIBILITY
4658shared_ptr<_Tp>
4659make_shared()
4660{
4661 return shared_ptr<_Tp>::make_shared();
4662}
4663
4664template<class _Tp, class _A0>
4665inline _LIBCPP_INLINE_VISIBILITY
4666shared_ptr<_Tp>
4667make_shared(_A0& __a0)
4668{
4669 return shared_ptr<_Tp>::make_shared(__a0);
4670}
4671
4672template<class _Tp, class _A0, class _A1>
4673inline _LIBCPP_INLINE_VISIBILITY
4674shared_ptr<_Tp>
4675make_shared(_A0& __a0, _A1& __a1)
4676{
4677 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4678}
4679
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004680template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004681inline _LIBCPP_INLINE_VISIBILITY
4682shared_ptr<_Tp>
4683make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4684{
4685 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4686}
4687
4688template<class _Tp, class _Alloc>
4689inline _LIBCPP_INLINE_VISIBILITY
4690shared_ptr<_Tp>
4691allocate_shared(const _Alloc& __a)
4692{
4693 return shared_ptr<_Tp>::allocate_shared(__a);
4694}
4695
4696template<class _Tp, class _Alloc, class _A0>
4697inline _LIBCPP_INLINE_VISIBILITY
4698shared_ptr<_Tp>
4699allocate_shared(const _Alloc& __a, _A0& __a0)
4700{
4701 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4702}
4703
4704template<class _Tp, class _Alloc, class _A0, class _A1>
4705inline _LIBCPP_INLINE_VISIBILITY
4706shared_ptr<_Tp>
4707allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4708{
4709 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4710}
4711
4712template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4713inline _LIBCPP_INLINE_VISIBILITY
4714shared_ptr<_Tp>
4715allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4716{
4717 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4718}
4719
4720#endif // _LIBCPP_HAS_NO_VARIADICS
4721
4722template<class _Tp, class _Up>
4723inline _LIBCPP_INLINE_VISIBILITY
4724bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004725operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004726{
4727 return __x.get() == __y.get();
4728}
4729
4730template<class _Tp, class _Up>
4731inline _LIBCPP_INLINE_VISIBILITY
4732bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004733operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004734{
4735 return !(__x == __y);
4736}
4737
4738template<class _Tp, class _Up>
4739inline _LIBCPP_INLINE_VISIBILITY
4740bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004741operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004742{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004743 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4744 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004745}
4746
4747template<class _Tp, class _Up>
4748inline _LIBCPP_INLINE_VISIBILITY
4749bool
4750operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4751{
4752 return __y < __x;
4753}
4754
4755template<class _Tp, class _Up>
4756inline _LIBCPP_INLINE_VISIBILITY
4757bool
4758operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4759{
4760 return !(__y < __x);
4761}
4762
4763template<class _Tp, class _Up>
4764inline _LIBCPP_INLINE_VISIBILITY
4765bool
4766operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4767{
4768 return !(__x < __y);
4769}
4770
4771template<class _Tp>
4772inline _LIBCPP_INLINE_VISIBILITY
4773bool
4774operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4775{
4776 return !__x;
4777}
4778
4779template<class _Tp>
4780inline _LIBCPP_INLINE_VISIBILITY
4781bool
4782operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4783{
4784 return !__x;
4785}
4786
4787template<class _Tp>
4788inline _LIBCPP_INLINE_VISIBILITY
4789bool
4790operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4791{
4792 return static_cast<bool>(__x);
4793}
4794
4795template<class _Tp>
4796inline _LIBCPP_INLINE_VISIBILITY
4797bool
4798operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4799{
4800 return static_cast<bool>(__x);
4801}
4802
4803template<class _Tp>
4804inline _LIBCPP_INLINE_VISIBILITY
4805bool
4806operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4807{
4808 return less<_Tp*>()(__x.get(), nullptr);
4809}
4810
4811template<class _Tp>
4812inline _LIBCPP_INLINE_VISIBILITY
4813bool
4814operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4815{
4816 return less<_Tp*>()(nullptr, __x.get());
4817}
4818
4819template<class _Tp>
4820inline _LIBCPP_INLINE_VISIBILITY
4821bool
4822operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4823{
4824 return nullptr < __x;
4825}
4826
4827template<class _Tp>
4828inline _LIBCPP_INLINE_VISIBILITY
4829bool
4830operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4831{
4832 return __x < nullptr;
4833}
4834
4835template<class _Tp>
4836inline _LIBCPP_INLINE_VISIBILITY
4837bool
4838operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4839{
4840 return !(nullptr < __x);
4841}
4842
4843template<class _Tp>
4844inline _LIBCPP_INLINE_VISIBILITY
4845bool
4846operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4847{
4848 return !(__x < nullptr);
4849}
4850
4851template<class _Tp>
4852inline _LIBCPP_INLINE_VISIBILITY
4853bool
4854operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4855{
4856 return !(__x < nullptr);
4857}
4858
4859template<class _Tp>
4860inline _LIBCPP_INLINE_VISIBILITY
4861bool
4862operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4863{
4864 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004865}
4866
4867template<class _Tp>
4868inline _LIBCPP_INLINE_VISIBILITY
4869void
Howard Hinnant719bda32011-05-28 14:41:13 +00004870swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004871{
4872 __x.swap(__y);
4873}
4874
4875template<class _Tp, class _Up>
4876inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004877typename enable_if
4878<
4879 !is_array<_Tp>::value && !is_array<_Up>::value,
4880 shared_ptr<_Tp>
4881>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004882static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004883{
4884 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4885}
4886
4887template<class _Tp, class _Up>
4888inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004889typename enable_if
4890<
4891 !is_array<_Tp>::value && !is_array<_Up>::value,
4892 shared_ptr<_Tp>
4893>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004894dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004895{
4896 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4897 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4898}
4899
4900template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004901typename enable_if
4902<
4903 is_array<_Tp>::value == is_array<_Up>::value,
4904 shared_ptr<_Tp>
4905>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004906const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004907{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004908 typedef typename remove_extent<_Tp>::type _RTp;
4909 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004910}
4911
Howard Hinnant72f73582010-08-11 17:04:31 +00004912#ifndef _LIBCPP_NO_RTTI
4913
Howard Hinnantc51e1022010-05-11 19:42:16 +00004914template<class _Dp, class _Tp>
4915inline _LIBCPP_INLINE_VISIBILITY
4916_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004917get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004918{
4919 return __p.template __get_deleter<_Dp>();
4920}
4921
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004922#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004923
Howard Hinnantc51e1022010-05-11 19:42:16 +00004924template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004925class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004926{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004927public:
4928 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004929private:
4930 element_type* __ptr_;
4931 __shared_weak_count* __cntrl_;
4932
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004933public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004935 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004936 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004937 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4938 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004940 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004941 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004942 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4943 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004944
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004945#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004947 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004948 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004949 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4950 _NOEXCEPT;
4951#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004952 ~weak_ptr();
4953
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004955 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004956 template<class _Yp>
4957 typename enable_if
4958 <
4959 is_convertible<_Yp*, element_type*>::value,
4960 weak_ptr&
4961 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004963 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4964
4965#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4966
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004968 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4969 template<class _Yp>
4970 typename enable_if
4971 <
4972 is_convertible<_Yp*, element_type*>::value,
4973 weak_ptr&
4974 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004976 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4977
4978#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4979
4980 template<class _Yp>
4981 typename enable_if
4982 <
4983 is_convertible<_Yp*, element_type*>::value,
4984 weak_ptr&
4985 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004987 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004988
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004990 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004992 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004993
Howard Hinnant756c69b2010-09-22 16:48:34 +00004994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004995 long use_count() const _NOEXCEPT
4996 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004998 bool expired() const _NOEXCEPT
4999 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5000 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005001 template<class _Up>
5002 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005003 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005004 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005005 template<class _Up>
5006 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005007 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005008 {return __cntrl_ < __r.__cntrl_;}
5009
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005010 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5011 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012};
5013
5014template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005015inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005016_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005017weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005018 : __ptr_(0),
5019 __cntrl_(0)
5020{
5021}
5022
5023template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005024inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005025weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005026 : __ptr_(__r.__ptr_),
5027 __cntrl_(__r.__cntrl_)
5028{
5029 if (__cntrl_)
5030 __cntrl_->__add_weak();
5031}
5032
5033template<class _Tp>
5034template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005035inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005036weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005037 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005038 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005039 : __ptr_(__r.__ptr_),
5040 __cntrl_(__r.__cntrl_)
5041{
5042 if (__cntrl_)
5043 __cntrl_->__add_weak();
5044}
5045
5046template<class _Tp>
5047template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005048inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005049weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005050 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005051 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005052 : __ptr_(__r.__ptr_),
5053 __cntrl_(__r.__cntrl_)
5054{
5055 if (__cntrl_)
5056 __cntrl_->__add_weak();
5057}
5058
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005059#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5060
5061template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005062inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005063weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5064 : __ptr_(__r.__ptr_),
5065 __cntrl_(__r.__cntrl_)
5066{
5067 __r.__ptr_ = 0;
5068 __r.__cntrl_ = 0;
5069}
5070
5071template<class _Tp>
5072template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005073inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005074weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5075 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5076 _NOEXCEPT
5077 : __ptr_(__r.__ptr_),
5078 __cntrl_(__r.__cntrl_)
5079{
5080 __r.__ptr_ = 0;
5081 __r.__cntrl_ = 0;
5082}
5083
5084#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5085
Howard Hinnantc51e1022010-05-11 19:42:16 +00005086template<class _Tp>
5087weak_ptr<_Tp>::~weak_ptr()
5088{
5089 if (__cntrl_)
5090 __cntrl_->__release_weak();
5091}
5092
5093template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005094inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005095weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005096weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005097{
5098 weak_ptr(__r).swap(*this);
5099 return *this;
5100}
5101
5102template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005103template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005104inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005105typename enable_if
5106<
5107 is_convertible<_Yp*, _Tp*>::value,
5108 weak_ptr<_Tp>&
5109>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005110weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005111{
5112 weak_ptr(__r).swap(*this);
5113 return *this;
5114}
5115
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005116#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5117
5118template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005119inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005120weak_ptr<_Tp>&
5121weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5122{
5123 weak_ptr(_VSTD::move(__r)).swap(*this);
5124 return *this;
5125}
5126
Howard Hinnantc51e1022010-05-11 19:42:16 +00005127template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005128template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005129inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005130typename enable_if
5131<
5132 is_convertible<_Yp*, _Tp*>::value,
5133 weak_ptr<_Tp>&
5134>::type
5135weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5136{
5137 weak_ptr(_VSTD::move(__r)).swap(*this);
5138 return *this;
5139}
5140
5141#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5142
5143template<class _Tp>
5144template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005145inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005146typename enable_if
5147<
5148 is_convertible<_Yp*, _Tp*>::value,
5149 weak_ptr<_Tp>&
5150>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005151weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005152{
5153 weak_ptr(__r).swap(*this);
5154 return *this;
5155}
5156
5157template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005158inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005159void
Howard Hinnant719bda32011-05-28 14:41:13 +00005160weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005161{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005162 _VSTD::swap(__ptr_, __r.__ptr_);
5163 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005164}
5165
5166template<class _Tp>
5167inline _LIBCPP_INLINE_VISIBILITY
5168void
Howard Hinnant719bda32011-05-28 14:41:13 +00005169swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005170{
5171 __x.swap(__y);
5172}
5173
5174template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005175inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005176void
Howard Hinnant719bda32011-05-28 14:41:13 +00005177weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005178{
5179 weak_ptr().swap(*this);
5180}
5181
5182template<class _Tp>
5183template<class _Yp>
5184shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005185 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005186 : __ptr_(__r.__ptr_),
5187 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5188{
5189 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005190 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005191}
5192
5193template<class _Tp>
5194shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005195weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005196{
5197 shared_ptr<_Tp> __r;
5198 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5199 if (__r.__cntrl_)
5200 __r.__ptr_ = __ptr_;
5201 return __r;
5202}
5203
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005204#if _LIBCPP_STD_VER > 14
5205template <class _Tp = void> struct owner_less;
5206#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005207template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005208#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005209
5210template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005211struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005212 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005213{
5214 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005215 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005216 bool operator()(shared_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 Hinnant756c69b2010-09-22 16:48:34 +00005218 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005219 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005220 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005221 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005222 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005223 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005224};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005225
5226template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005227struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005228 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5229{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005230 typedef bool result_type;
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, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005233 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005234 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005235 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005236 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005237 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005238 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005239 {return __x.owner_before(__y);}
5240};
5241
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005242#if _LIBCPP_STD_VER > 14
5243template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005244struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005245{
5246 template <class _Tp, class _Up>
5247 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005248 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005249 {return __x.owner_before(__y);}
5250 template <class _Tp, class _Up>
5251 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005252 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005253 {return __x.owner_before(__y);}
5254 template <class _Tp, class _Up>
5255 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005256 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005257 {return __x.owner_before(__y);}
5258 template <class _Tp, class _Up>
5259 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005260 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005261 {return __x.owner_before(__y);}
5262 typedef void is_transparent;
5263};
5264#endif
5265
Howard Hinnantc51e1022010-05-11 19:42:16 +00005266template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005267class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005268{
5269 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005270protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005271 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005272 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005274 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005276 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5277 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005279 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005280public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005282 shared_ptr<_Tp> shared_from_this()
5283 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005285 shared_ptr<_Tp const> shared_from_this() const
5286 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005287
Eric Fiselier84006862016-06-02 00:15:35 +00005288#if _LIBCPP_STD_VER > 14
5289 _LIBCPP_INLINE_VISIBILITY
5290 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5291 { return __weak_this_; }
5292
5293 _LIBCPP_INLINE_VISIBILITY
5294 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5295 { return __weak_this_; }
5296#endif // _LIBCPP_STD_VER > 14
5297
Howard Hinnantc51e1022010-05-11 19:42:16 +00005298 template <class _Up> friend class shared_ptr;
5299};
5300
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005301template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005302struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005303{
5304 typedef shared_ptr<_Tp> argument_type;
5305 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005306
Howard Hinnant756c69b2010-09-22 16:48:34 +00005307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005308 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005309 {
5310 return hash<_Tp*>()(__ptr.get());
5311 }
5312};
5313
Howard Hinnantc834c512011-11-29 18:15:50 +00005314template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005315inline _LIBCPP_INLINE_VISIBILITY
5316basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005317operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005318
Eric Fiselier9b492672016-06-18 02:12:53 +00005319
5320#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005321
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005322class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005323{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005324 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005325public:
5326 void lock() _NOEXCEPT;
5327 void unlock() _NOEXCEPT;
5328
5329private:
5330 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5331 __sp_mut(const __sp_mut&);
5332 __sp_mut& operator=(const __sp_mut&);
5333
Howard Hinnant8331b762013-03-06 23:30:19 +00005334 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005335};
5336
Mehdi Amini228053d2017-05-04 17:08:54 +00005337_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5338__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005339
5340template <class _Tp>
5341inline _LIBCPP_INLINE_VISIBILITY
5342bool
5343atomic_is_lock_free(const shared_ptr<_Tp>*)
5344{
5345 return false;
5346}
5347
5348template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005349_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005350shared_ptr<_Tp>
5351atomic_load(const shared_ptr<_Tp>* __p)
5352{
5353 __sp_mut& __m = __get_sp_mut(__p);
5354 __m.lock();
5355 shared_ptr<_Tp> __q = *__p;
5356 __m.unlock();
5357 return __q;
5358}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005359
Howard Hinnant9fa30202012-07-30 01:40:57 +00005360template <class _Tp>
5361inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005362_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005363shared_ptr<_Tp>
5364atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5365{
5366 return atomic_load(__p);
5367}
5368
5369template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005370_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005371void
5372atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5373{
5374 __sp_mut& __m = __get_sp_mut(__p);
5375 __m.lock();
5376 __p->swap(__r);
5377 __m.unlock();
5378}
5379
5380template <class _Tp>
5381inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005382_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005383void
5384atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5385{
5386 atomic_store(__p, __r);
5387}
5388
5389template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005390_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005391shared_ptr<_Tp>
5392atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5393{
5394 __sp_mut& __m = __get_sp_mut(__p);
5395 __m.lock();
5396 __p->swap(__r);
5397 __m.unlock();
5398 return __r;
5399}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005400
Howard Hinnant9fa30202012-07-30 01:40:57 +00005401template <class _Tp>
5402inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005403_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005404shared_ptr<_Tp>
5405atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5406{
5407 return atomic_exchange(__p, __r);
5408}
5409
5410template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005411_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005412bool
5413atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5414{
Marshall Clow4201ee82016-05-18 17:50:13 +00005415 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005416 __sp_mut& __m = __get_sp_mut(__p);
5417 __m.lock();
5418 if (__p->__owner_equivalent(*__v))
5419 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005420 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005421 *__p = __w;
5422 __m.unlock();
5423 return true;
5424 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005425 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005426 *__v = *__p;
5427 __m.unlock();
5428 return false;
5429}
5430
5431template <class _Tp>
5432inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005433_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005434bool
5435atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5436{
5437 return atomic_compare_exchange_strong(__p, __v, __w);
5438}
5439
5440template <class _Tp>
5441inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005442_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005443bool
5444atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5445 shared_ptr<_Tp> __w, memory_order, memory_order)
5446{
5447 return atomic_compare_exchange_strong(__p, __v, __w);
5448}
5449
5450template <class _Tp>
5451inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005452_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005453bool
5454atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5455 shared_ptr<_Tp> __w, memory_order, memory_order)
5456{
5457 return atomic_compare_exchange_weak(__p, __v, __w);
5458}
5459
Eric Fiselier9b492672016-06-18 02:12:53 +00005460#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005461
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005462//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005463#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5464# ifndef _LIBCPP_CXX03_LANG
5465enum class pointer_safety : unsigned char {
5466 relaxed,
5467 preferred,
5468 strict
5469};
5470# endif
5471#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005472struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005473{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005474 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005475 {
5476 relaxed,
5477 preferred,
5478 strict
5479 };
5480
Howard Hinnant49e145e2012-10-30 19:06:59 +00005481 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005482
Howard Hinnant756c69b2010-09-22 16:48:34 +00005483 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005484 pointer_safety() : __v_() {}
5485
5486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005487 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005489 operator int() const {return __v_;}
5490};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005491#endif
5492
5493#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5494 defined(_LIBCPP_BUILDING_MEMORY)
5495_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5496#else
5497// This function is only offered in C++03 under ABI v1.
5498# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5499inline _LIBCPP_INLINE_VISIBILITY
5500pointer_safety get_pointer_safety() _NOEXCEPT {
5501 return pointer_safety::relaxed;
5502}
5503# endif
5504#endif
5505
Howard Hinnantc51e1022010-05-11 19:42:16 +00005506
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005507_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5508_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5509_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005510_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005511
5512template <class _Tp>
5513inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005514_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005515undeclare_reachable(_Tp* __p)
5516{
5517 return static_cast<_Tp*>(__undeclare_reachable(__p));
5518}
5519
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005520_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005521
Marshall Clow8982dcd2015-07-13 20:04:56 +00005522// --- Helper for container swap --
5523template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005524inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005525void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5526#if _LIBCPP_STD_VER >= 14
5527 _NOEXCEPT
5528#else
5529 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5530#endif
5531{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005532 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005533 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5534}
5535
5536template <typename _Alloc>
5537_LIBCPP_INLINE_VISIBILITY
5538void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5539#if _LIBCPP_STD_VER >= 14
5540 _NOEXCEPT
5541#else
5542 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5543#endif
5544{
5545 using _VSTD::swap;
5546 swap(__a1, __a2);
5547}
5548
5549template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005550inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005551void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5552
Marshall Clowff91de82015-08-18 19:51:37 +00005553template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005554struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005555 _Traits::propagate_on_container_move_assignment::value
5556#if _LIBCPP_STD_VER > 14
5557 || _Traits::is_always_equal::value
5558#else
5559 && is_nothrow_move_assignable<_Alloc>::value
5560#endif
5561 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005562
Marshall Clowa591b9a2016-07-11 21:38:08 +00005563
5564#ifndef _LIBCPP_HAS_NO_VARIADICS
5565template <class _Tp, class _Alloc>
5566struct __temp_value {
5567 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005568
Marshall Clowa591b9a2016-07-11 21:38:08 +00005569 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5570 _Alloc &__a;
5571
5572 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5573 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005574
Marshall Clowa591b9a2016-07-11 21:38:08 +00005575 template<class... _Args>
5576 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5577 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005578
Marshall Clowa591b9a2016-07-11 21:38:08 +00005579 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5580 };
5581#endif
5582
Howard Hinnantc51e1022010-05-11 19:42:16 +00005583_LIBCPP_END_NAMESPACE_STD
5584
Eric Fiselierf4433a32017-05-31 22:07:49 +00005585_LIBCPP_POP_MACROS
5586
Howard Hinnantc51e1022010-05-11 19:42:16 +00005587#endif // _LIBCPP_MEMORY