blob: c7f540bb860064217d79b5e36e6fdf42d8c41e7f [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000031
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000033
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant719bda32011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
46 static pointer pointer_to(<details>) noexcept;
47};
48
Howard Hinnantc51e1022010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
81 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
82 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
83
84 static pointer allocate(allocator_type& a, size_type n);
85 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
86
Howard Hinnant719bda32011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 template <class T, class... Args>
90 static void construct(allocator_type& a, T* p, Args&&... args);
91
92 template <class T>
93 static void destroy(allocator_type& a, T* p);
94
Marshall Clow4f834b52013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 static allocator_type
98 select_on_container_copy_construction(const allocator_type& a);
99};
100
101template <>
102class allocator<void>
103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef T* pointer;
119 typedef const T* const_pointer;
120 typedef typename add_lvalue_reference<T>::type reference;
121 typedef typename add_lvalue_reference<const T>::type const_reference;
122 typedef T value_type;
123
124 template <class U> struct rebind {typedef allocator<U> other;};
125
Howard Hinnant719bda32011-05-28 14:41:13 +0000126 allocator() noexcept;
127 allocator(const allocator&) noexcept;
128 template <class U> allocator(const allocator<U>&) noexcept;
129 ~allocator();
130 pointer address(reference x) const noexcept;
131 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 void deallocate(pointer p, size_type n) noexcept;
134 size_type max_size() const noexcept;
135 template<class U, class... Args>
136 void construct(U* p, Args&&... args);
137 template <class U>
138 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class OutputIterator, class T>
148class raw_storage_iterator
149 : public iterator<output_iterator_tag,
150 T, // purposefully not C++03
151 ptrdiff_t, // purposefully not C++03
152 T*, // purposefully not C++03
153 raw_storage_iterator&> // purposefully not C++03
154{
155public:
156 explicit raw_storage_iterator(OutputIterator x);
157 raw_storage_iterator& operator*();
158 raw_storage_iterator& operator=(const T& element);
159 raw_storage_iterator& operator++();
160 raw_storage_iterator operator++(int);
161};
162
Howard Hinnant719bda32011-05-28 14:41:13 +0000163template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
164template <class T> void return_temporary_buffer(T* p) noexcept;
165
166template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000167template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169template <class InputIterator, class ForwardIterator>
170ForwardIterator
171uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class InputIterator, class Size, class ForwardIterator>
174ForwardIterator
175uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
176
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177template <class ForwardIterator, class T>
178void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
179
180template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000181ForwardIterator
182uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000184template <class T>
185void destroy_at(T* location);
186
187template <class ForwardIterator>
188 void destroy(ForwardIterator first, ForwardIterator last);
189
190template <class ForwardIterator, class Size>
191 ForwardIterator destroy_n(ForwardIterator first, Size n);
192
193template <class InputIterator, class ForwardIterator>
194 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
195
196template <class InputIterator, class Size, class ForwardIterator>
197 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
198
199template <class ForwardIterator>
200 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
201
202template <class ForwardIterator, class Size>
203 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
204
205template <class ForwardIterator>
206 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
207
208template <class ForwardIterator, class Size>
209 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
210
Marshall Clowb22274f2017-01-24 22:22:33 +0000211template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
213template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000214class auto_ptr // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215{
216public:
217 typedef X element_type;
218
219 explicit auto_ptr(X* p =0) throw();
220 auto_ptr(auto_ptr&) throw();
221 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
222 auto_ptr& operator=(auto_ptr&) throw();
223 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
224 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
225 ~auto_ptr() throw();
226
227 typename add_lvalue_reference<X>::type operator*() const throw();
228 X* operator->() const throw();
229 X* get() const throw();
230 X* release() throw();
231 void reset(X* p =0) throw();
232
233 auto_ptr(auto_ptr_ref<X>) throw();
234 template<class Y> operator auto_ptr_ref<Y>() throw();
235 template<class Y> operator auto_ptr<Y>() throw();
236};
237
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000238template <class T>
239struct default_delete
240{
Howard Hinnant719bda32011-05-28 14:41:13 +0000241 constexpr default_delete() noexcept = default;
242 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000243
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000245};
246
247template <class T>
248struct default_delete<T[]>
249{
Howard Hinnant719bda32011-05-28 14:41:13 +0000250 constexpr default_delete() noexcept = default;
251 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000252 template <class U> void operator()(U*) const = delete;
253};
254
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255template <class T, class D = default_delete<T>>
256class unique_ptr
257{
258public:
259 typedef see below pointer;
260 typedef T element_type;
261 typedef D deleter_type;
262
263 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 constexpr unique_ptr() noexcept;
265 explicit unique_ptr(pointer p) noexcept;
266 unique_ptr(pointer p, see below d1) noexcept;
267 unique_ptr(pointer p, see below d2) noexcept;
268 unique_ptr(unique_ptr&& u) noexcept;
269 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000270 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000271 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000272 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000273 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000274
275 // destructor
276 ~unique_ptr();
277
278 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000279 unique_ptr& operator=(unique_ptr&& u) noexcept;
280 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
281 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282
283 // observers
284 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000285 pointer operator->() const noexcept;
286 pointer get() const noexcept;
287 deleter_type& get_deleter() noexcept;
288 const deleter_type& get_deleter() const noexcept;
289 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000290
291 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000292 pointer release() noexcept;
293 void reset(pointer p = pointer()) noexcept;
294 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000295};
296
297template <class T, class D>
298class unique_ptr<T[], D>
299{
300public:
301 typedef implementation-defined pointer;
302 typedef T element_type;
303 typedef D deleter_type;
304
305 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 constexpr unique_ptr() noexcept;
307 explicit unique_ptr(pointer p) noexcept;
308 unique_ptr(pointer p, see below d) noexcept;
309 unique_ptr(pointer p, see below d) noexcept;
310 unique_ptr(unique_ptr&& u) noexcept;
311 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000312
313 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000314 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 unique_ptr& operator=(unique_ptr&& u) noexcept;
318 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000319
320 // observers
321 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000322 pointer get() const noexcept;
323 deleter_type& get_deleter() noexcept;
324 const deleter_type& get_deleter() const noexcept;
325 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 pointer release() noexcept;
329 void reset(pointer p = pointer()) noexcept;
330 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000331 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000333};
334
335template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338template <class T1, class D1, class T2, class D2>
339 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
340template <class T1, class D1, class T2, class D2>
341 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
342template <class T1, class D1, class T2, class D2>
343 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350
Howard Hinnant719bda32011-05-28 14:41:13 +0000351template <class T, class D>
352 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
353template <class T, class D>
354 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
355template <class T, class D>
356 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359
360template <class T, class D>
361 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
362template <class T, class D>
363 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
364template <class T, class D>
365 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
376
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000377class bad_weak_ptr
378 : public std::exception
379{
Howard Hinnant719bda32011-05-28 14:41:13 +0000380 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000381};
382
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000383template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
384template<class T> unique_ptr<T> make_unique(size_t n); // C++14
385template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387template<class T>
388class shared_ptr
389{
390public:
391 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000392 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393
394 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000395 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396 template<class Y> explicit shared_ptr(Y* p);
397 template<class Y, class D> shared_ptr(Y* p, D d);
398 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
399 template <class D> shared_ptr(nullptr_t p, D d);
400 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
402 shared_ptr(const shared_ptr& r) noexcept;
403 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
404 shared_ptr(shared_ptr&& r) noexcept;
405 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000407 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000408 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
409 shared_ptr(nullptr_t) : shared_ptr() { }
410
411 // destructor:
412 ~shared_ptr();
413
414 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 shared_ptr& operator=(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
417 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000418 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000419 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
421
422 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000423 void swap(shared_ptr& r) noexcept;
424 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000425 template<class Y> void reset(Y* p);
426 template<class Y, class D> void reset(Y* p, D d);
427 template<class Y, class D, class A> void reset(Y* p, D d, A a);
428
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 // observers:
430 T* get() const noexcept;
431 T& operator*() const noexcept;
432 T* operator->() const noexcept;
433 long use_count() const noexcept;
434 bool unique() const noexcept;
435 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000436 template<class U> bool owner_before(shared_ptr<U> const& b) const;
437 template<class U> bool owner_before(weak_ptr<U> const& b) const;
438};
439
440// shared_ptr comparisons:
441template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000443template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000444 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000445template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000446 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
453
454template <class T>
455 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
456template <class T>
457 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
458template <class T>
459 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
460template <class T>
461 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
462template <class T>
463 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
464template <class T>
465bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
466template <class T>
467 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
468template <class T>
469 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
470template <class T>
471 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
472template <class T>
473 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
474template <class T>
475 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
476template <class T>
477 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000478
479// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000480template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr casts:
483template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000484 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000485template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000486 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000488 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000489
490// shared_ptr I/O:
491template<class E, class T, class Y>
492 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
493
494// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000495template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497template<class T, class... Args>
498 shared_ptr<T> make_shared(Args&&... args);
499template<class T, class A, class... Args>
500 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
501
502template<class T>
503class weak_ptr
504{
505public:
506 typedef T element_type;
507
508 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000509 constexpr weak_ptr() noexcept;
510 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
511 weak_ptr(weak_ptr const& r) noexcept;
512 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000513 weak_ptr(weak_ptr&& r) noexcept; // C++14
514 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516 // destructor
517 ~weak_ptr();
518
519 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000520 weak_ptr& operator=(weak_ptr const& r) noexcept;
521 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
522 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000523 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
524 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000525
526 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 void swap(weak_ptr& r) noexcept;
528 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000529
530 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000531 long use_count() const noexcept;
532 bool expired() const noexcept;
533 shared_ptr<T> lock() const noexcept;
Marshall Clow495e4a62013-09-03 14:37:50 +0000534 template<class U> bool owner_before(shared_ptr<U> const& b) const;
535 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000536};
537
538// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000539template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000540
541// class owner_less:
542template<class T> struct owner_less;
543
544template<class T>
545struct owner_less<shared_ptr<T>>
546 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
547{
548 typedef bool result_type;
549 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
550 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
551 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
552};
553
554template<class T>
555struct owner_less<weak_ptr<T>>
556 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
557{
558 typedef bool result_type;
559 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
560 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
561 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
562};
563
564template<class T>
565class enable_shared_from_this
566{
567protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000568 constexpr enable_shared_from_this() noexcept;
569 enable_shared_from_this(enable_shared_from_this const&) noexcept;
570 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000571 ~enable_shared_from_this();
572public:
573 shared_ptr<T> shared_from_this();
574 shared_ptr<T const> shared_from_this() const;
575};
576
577template<class T>
578 bool atomic_is_lock_free(const shared_ptr<T>* p);
579template<class T>
580 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
581template<class T>
582 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
583template<class T>
584 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
585template<class T>
586 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
587template<class T>
588 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
589template<class T>
590 shared_ptr<T>
591 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
592template<class T>
593 bool
594 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
595template<class T>
596 bool
597 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
598template<class T>
599 bool
600 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
601 shared_ptr<T> w, memory_order success,
602 memory_order failure);
603template<class T>
604 bool
605 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
606 shared_ptr<T> w, memory_order success,
607 memory_order failure);
608// Hash support
609template <class T> struct hash;
610template <class T, class D> struct hash<unique_ptr<T, D> >;
611template <class T> struct hash<shared_ptr<T> >;
612
613// Pointer safety
614enum class pointer_safety { relaxed, preferred, strict };
615void declare_reachable(void *p);
616template <class T> T *undeclare_reachable(T *p);
617void declare_no_pointers(char *p, size_t n);
618void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000619pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000620
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
622
623} // std
624
625*/
626
627#include <__config>
628#include <type_traits>
629#include <typeinfo>
630#include <cstddef>
631#include <cstdint>
632#include <new>
633#include <utility>
634#include <limits>
635#include <iterator>
636#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000637#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000638#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000639#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000640#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000642#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000643# include <atomic>
644#endif
645
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000646#include <__undef_min_max>
647
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000648#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000650#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651
652_LIBCPP_BEGIN_NAMESPACE_STD
653
Eric Fiselier89659d12015-07-07 00:27:16 +0000654template <class _ValueType>
655inline _LIBCPP_ALWAYS_INLINE
656_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
657#if !defined(_LIBCPP_HAS_NO_THREADS) && \
658 defined(__ATOMIC_RELAXED) && \
659 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
660 return __atomic_load_n(__value, __ATOMIC_RELAXED);
661#else
662 return *__value;
663#endif
664}
665
Kuba Breckade9d6792016-09-04 09:55:12 +0000666template <class _ValueType>
667inline _LIBCPP_ALWAYS_INLINE
668_ValueType __libcpp_acquire_load(_ValueType const* __value) {
669#if !defined(_LIBCPP_HAS_NO_THREADS) && \
670 defined(__ATOMIC_ACQUIRE) && \
671 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
672 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
673#else
674 return *__value;
675#endif
676}
677
Marshall Clow78dbe462016-11-14 18:22:19 +0000678// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000679
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680template <class _Tp> class allocator;
681
682template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000683class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684{
685public:
686 typedef void* pointer;
687 typedef const void* const_pointer;
688 typedef void value_type;
689
690 template <class _Up> struct rebind {typedef allocator<_Up> other;};
691};
692
Howard Hinnant9f771052012-01-19 23:15:22 +0000693template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000694class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000695{
696public:
697 typedef const void* pointer;
698 typedef const void* const_pointer;
699 typedef const void value_type;
700
701 template <class _Up> struct rebind {typedef allocator<_Up> other;};
702};
703
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704// pointer_traits
705
706template <class _Tp>
707struct __has_element_type
708{
709private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000710 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 template <class _Up> static __two __test(...);
712 template <class _Up> static char __test(typename _Up::element_type* = 0);
713public:
714 static const bool value = sizeof(__test<_Tp>(0)) == 1;
715};
716
717template <class _Ptr, bool = __has_element_type<_Ptr>::value>
718struct __pointer_traits_element_type;
719
720template <class _Ptr>
721struct __pointer_traits_element_type<_Ptr, true>
722{
723 typedef typename _Ptr::element_type type;
724};
725
726#ifndef _LIBCPP_HAS_NO_VARIADICS
727
728template <template <class, class...> class _Sp, class _Tp, class ..._Args>
729struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
730{
731 typedef typename _Sp<_Tp, _Args...>::element_type type;
732};
733
734template <template <class, class...> class _Sp, class _Tp, class ..._Args>
735struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
736{
737 typedef _Tp type;
738};
739
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000740#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
742template <template <class> class _Sp, class _Tp>
743struct __pointer_traits_element_type<_Sp<_Tp>, true>
744{
745 typedef typename _Sp<_Tp>::element_type type;
746};
747
748template <template <class> class _Sp, class _Tp>
749struct __pointer_traits_element_type<_Sp<_Tp>, false>
750{
751 typedef _Tp type;
752};
753
754template <template <class, class> class _Sp, class _Tp, class _A0>
755struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
756{
757 typedef typename _Sp<_Tp, _A0>::element_type type;
758};
759
760template <template <class, class> class _Sp, class _Tp, class _A0>
761struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
762{
763 typedef _Tp type;
764};
765
766template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
767struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
768{
769 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
770};
771
772template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
773struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
774{
775 typedef _Tp type;
776};
777
778template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
779 class _A1, class _A2>
780struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
781{
782 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
783};
784
785template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
786 class _A1, class _A2>
787struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
788{
789 typedef _Tp type;
790};
791
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000792#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793
794template <class _Tp>
795struct __has_difference_type
796{
797private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000798 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 template <class _Up> static __two __test(...);
800 template <class _Up> static char __test(typename _Up::difference_type* = 0);
801public:
802 static const bool value = sizeof(__test<_Tp>(0)) == 1;
803};
804
805template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
806struct __pointer_traits_difference_type
807{
808 typedef ptrdiff_t type;
809};
810
811template <class _Ptr>
812struct __pointer_traits_difference_type<_Ptr, true>
813{
814 typedef typename _Ptr::difference_type type;
815};
816
817template <class _Tp, class _Up>
818struct __has_rebind
819{
820private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000821 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 template <class _Xp> static __two __test(...);
823 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
824public:
825 static const bool value = sizeof(__test<_Tp>(0)) == 1;
826};
827
828template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
829struct __pointer_traits_rebind
830{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000831#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 typedef typename _Tp::template rebind<_Up> type;
833#else
834 typedef typename _Tp::template rebind<_Up>::other type;
835#endif
836};
837
838#ifndef _LIBCPP_HAS_NO_VARIADICS
839
840template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
841struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
842{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000843#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
845#else
846 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
847#endif
848};
849
850template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
851struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
852{
853 typedef _Sp<_Up, _Args...> type;
854};
855
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000856#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857
858template <template <class> class _Sp, class _Tp, class _Up>
859struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
860{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000861#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 typedef typename _Sp<_Tp>::template rebind<_Up> type;
863#else
864 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
865#endif
866};
867
868template <template <class> class _Sp, class _Tp, class _Up>
869struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
870{
871 typedef _Sp<_Up> type;
872};
873
874template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
875struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
876{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000877#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
879#else
880 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
881#endif
882};
883
884template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
885struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
886{
887 typedef _Sp<_Up, _A0> type;
888};
889
890template <template <class, class, class> class _Sp, class _Tp, class _A0,
891 class _A1, class _Up>
892struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
893{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000894#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000895 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
896#else
897 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
898#endif
899};
900
901template <template <class, class, class> class _Sp, class _Tp, class _A0,
902 class _A1, class _Up>
903struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
904{
905 typedef _Sp<_Up, _A0, _A1> type;
906};
907
908template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
909 class _A1, class _A2, class _Up>
910struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
911{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000912#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
914#else
915 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
916#endif
917};
918
919template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
920 class _A1, class _A2, class _Up>
921struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
922{
923 typedef _Sp<_Up, _A0, _A1, _A2> type;
924};
925
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000926#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927
928template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000929struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930{
931 typedef _Ptr pointer;
932 typedef typename __pointer_traits_element_type<pointer>::type element_type;
933 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
934
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000935#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000936 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937#else
938 template <class _Up> struct rebind
939 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000940#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941
942private:
943 struct __nat {};
944public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946 static pointer pointer_to(typename conditional<is_void<element_type>::value,
947 __nat, element_type>::type& __r)
948 {return pointer::pointer_to(__r);}
949};
950
951template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000952struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953{
954 typedef _Tp* pointer;
955 typedef _Tp element_type;
956 typedef ptrdiff_t difference_type;
957
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000958#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 template <class _Up> using rebind = _Up*;
960#else
961 template <class _Up> struct rebind {typedef _Up* other;};
962#endif
963
964private:
965 struct __nat {};
966public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000969 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000970 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971};
972
Eric Fiselierae3ab842015-08-23 02:56:05 +0000973template <class _From, class _To>
974struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000975#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000976 typedef typename pointer_traits<_From>::template rebind<_To> type;
977#else
978 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
979#endif
980};
981
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982// allocator_traits
983
984namespace __has_pointer_type_imp
985{
Howard Hinnant6e260172013-09-21 01:45:05 +0000986 template <class _Up> static __two __test(...);
987 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988}
989
990template <class _Tp>
991struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +0000992 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993{
994};
995
996namespace __pointer_type_imp
997{
998
999template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1000struct __pointer_type
1001{
1002 typedef typename _Dp::pointer type;
1003};
1004
1005template <class _Tp, class _Dp>
1006struct __pointer_type<_Tp, _Dp, false>
1007{
1008 typedef _Tp* type;
1009};
1010
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001011} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012
1013template <class _Tp, class _Dp>
1014struct __pointer_type
1015{
1016 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1017};
1018
1019template <class _Tp>
1020struct __has_const_pointer
1021{
1022private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001023 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 template <class _Up> static __two __test(...);
1025 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1026public:
1027 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1028};
1029
1030template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1031struct __const_pointer
1032{
1033 typedef typename _Alloc::const_pointer type;
1034};
1035
1036template <class _Tp, class _Ptr, class _Alloc>
1037struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1038{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001039#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1041#else
1042 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1043#endif
1044};
1045
1046template <class _Tp>
1047struct __has_void_pointer
1048{
1049private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001050 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 template <class _Up> static __two __test(...);
1052 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1053public:
1054 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1055};
1056
1057template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1058struct __void_pointer
1059{
1060 typedef typename _Alloc::void_pointer type;
1061};
1062
1063template <class _Ptr, class _Alloc>
1064struct __void_pointer<_Ptr, _Alloc, false>
1065{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001066#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1068#else
1069 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1070#endif
1071};
1072
1073template <class _Tp>
1074struct __has_const_void_pointer
1075{
1076private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001077 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 template <class _Up> static __two __test(...);
1079 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1080public:
1081 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1082};
1083
1084template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1085struct __const_void_pointer
1086{
1087 typedef typename _Alloc::const_void_pointer type;
1088};
1089
1090template <class _Ptr, class _Alloc>
1091struct __const_void_pointer<_Ptr, _Alloc, false>
1092{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001093#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1095#else
1096 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1097#endif
1098};
1099
Howard Hinnantc834c512011-11-29 18:15:50 +00001100template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001102_Tp*
1103__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104{
1105 return __p;
1106}
1107
1108template <class _Pointer>
1109inline _LIBCPP_INLINE_VISIBILITY
1110typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001111__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001113 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114}
1115
1116template <class _Tp>
1117struct __has_size_type
1118{
1119private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001120 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 template <class _Up> static __two __test(...);
1122 template <class _Up> static char __test(typename _Up::size_type* = 0);
1123public:
1124 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1125};
1126
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001127template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128struct __size_type
1129{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001130 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131};
1132
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001133template <class _Alloc, class _DiffType>
1134struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135{
1136 typedef typename _Alloc::size_type type;
1137};
1138
1139template <class _Tp>
1140struct __has_propagate_on_container_copy_assignment
1141{
1142private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001143 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 template <class _Up> static __two __test(...);
1145 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1146public:
1147 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1148};
1149
1150template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1151struct __propagate_on_container_copy_assignment
1152{
1153 typedef false_type type;
1154};
1155
1156template <class _Alloc>
1157struct __propagate_on_container_copy_assignment<_Alloc, true>
1158{
1159 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1160};
1161
1162template <class _Tp>
1163struct __has_propagate_on_container_move_assignment
1164{
1165private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001166 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 template <class _Up> static __two __test(...);
1168 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1169public:
1170 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1171};
1172
1173template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1174struct __propagate_on_container_move_assignment
1175{
1176 typedef false_type type;
1177};
1178
1179template <class _Alloc>
1180struct __propagate_on_container_move_assignment<_Alloc, true>
1181{
1182 typedef typename _Alloc::propagate_on_container_move_assignment type;
1183};
1184
1185template <class _Tp>
1186struct __has_propagate_on_container_swap
1187{
1188private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001189 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 template <class _Up> static __two __test(...);
1191 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1192public:
1193 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1194};
1195
1196template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1197struct __propagate_on_container_swap
1198{
1199 typedef false_type type;
1200};
1201
1202template <class _Alloc>
1203struct __propagate_on_container_swap<_Alloc, true>
1204{
1205 typedef typename _Alloc::propagate_on_container_swap type;
1206};
1207
Marshall Clow0b587562015-06-02 16:34:03 +00001208template <class _Tp>
1209struct __has_is_always_equal
1210{
1211private:
1212 struct __two {char __lx; char __lxx;};
1213 template <class _Up> static __two __test(...);
1214 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1215public:
1216 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1217};
1218
1219template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1220struct __is_always_equal
1221{
1222 typedef typename _VSTD::is_empty<_Alloc>::type type;
1223};
1224
1225template <class _Alloc>
1226struct __is_always_equal<_Alloc, true>
1227{
1228 typedef typename _Alloc::is_always_equal type;
1229};
1230
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1232struct __has_rebind_other
1233{
1234private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001235 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 template <class _Xp> static __two __test(...);
1237 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1238public:
1239 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1240};
1241
1242template <class _Tp, class _Up>
1243struct __has_rebind_other<_Tp, _Up, false>
1244{
1245 static const bool value = false;
1246};
1247
1248template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1249struct __allocator_traits_rebind
1250{
1251 typedef typename _Tp::template rebind<_Up>::other type;
1252};
1253
1254#ifndef _LIBCPP_HAS_NO_VARIADICS
1255
1256template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1257struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1258{
1259 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1260};
1261
1262template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1263struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1264{
1265 typedef _Alloc<_Up, _Args...> type;
1266};
1267
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001268#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
1270template <template <class> class _Alloc, class _Tp, class _Up>
1271struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1272{
1273 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1274};
1275
1276template <template <class> class _Alloc, class _Tp, class _Up>
1277struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1278{
1279 typedef _Alloc<_Up> type;
1280};
1281
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1283struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1284{
1285 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1286};
1287
1288template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1289struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1290{
1291 typedef _Alloc<_Up, _A0> type;
1292};
1293
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1295 class _A1, class _Up>
1296struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1297{
1298 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1299};
1300
1301template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1302 class _A1, class _Up>
1303struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1304{
1305 typedef _Alloc<_Up, _A0, _A1> type;
1306};
1307
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1309 class _A1, class _A2, class _Up>
1310struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1311{
1312 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1313};
1314
1315template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1316 class _A1, class _A2, class _Up>
1317struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1318{
1319 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1320};
1321
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001322#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001324#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
1326template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1327auto
1328__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1329 -> decltype(__a.allocate(__sz, __p), true_type());
1330
1331template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1332auto
1333__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1334 -> false_type;
1335
1336template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1337struct __has_allocate_hint
1338 : integral_constant<bool,
1339 is_same<
1340 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1341 declval<_SizeType>(),
1342 declval<_ConstVoidPtr>())),
1343 true_type>::value>
1344{
1345};
1346
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001347#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348
1349template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1350struct __has_allocate_hint
1351 : true_type
1352{
1353};
1354
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001355#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001357#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358
1359template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001360decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1361 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 true_type())
1363__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1364
1365template <class _Alloc, class _Pointer, class ..._Args>
1366false_type
1367__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1368
1369template <class _Alloc, class _Pointer, class ..._Args>
1370struct __has_construct
1371 : integral_constant<bool,
1372 is_same<
1373 decltype(__has_construct_test(declval<_Alloc>(),
1374 declval<_Pointer>(),
1375 declval<_Args>()...)),
1376 true_type>::value>
1377{
1378};
1379
1380template <class _Alloc, class _Pointer>
1381auto
1382__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1383 -> decltype(__a.destroy(__p), true_type());
1384
1385template <class _Alloc, class _Pointer>
1386auto
1387__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1388 -> false_type;
1389
1390template <class _Alloc, class _Pointer>
1391struct __has_destroy
1392 : integral_constant<bool,
1393 is_same<
1394 decltype(__has_destroy_test(declval<_Alloc>(),
1395 declval<_Pointer>())),
1396 true_type>::value>
1397{
1398};
1399
1400template <class _Alloc>
1401auto
1402__has_max_size_test(_Alloc&& __a)
1403 -> decltype(__a.max_size(), true_type());
1404
1405template <class _Alloc>
1406auto
1407__has_max_size_test(const volatile _Alloc& __a)
1408 -> false_type;
1409
1410template <class _Alloc>
1411struct __has_max_size
1412 : integral_constant<bool,
1413 is_same<
1414 decltype(__has_max_size_test(declval<_Alloc&>())),
1415 true_type>::value>
1416{
1417};
1418
1419template <class _Alloc>
1420auto
1421__has_select_on_container_copy_construction_test(_Alloc&& __a)
1422 -> decltype(__a.select_on_container_copy_construction(), true_type());
1423
1424template <class _Alloc>
1425auto
1426__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1427 -> false_type;
1428
1429template <class _Alloc>
1430struct __has_select_on_container_copy_construction
1431 : integral_constant<bool,
1432 is_same<
1433 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1434 true_type>::value>
1435{
1436};
1437
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001438#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439
1440#ifndef _LIBCPP_HAS_NO_VARIADICS
1441
1442template <class _Alloc, class _Pointer, class ..._Args>
1443struct __has_construct
1444 : false_type
1445{
1446};
1447
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001448#else // _LIBCPP_HAS_NO_VARIADICS
1449
1450template <class _Alloc, class _Pointer, class _Args>
1451struct __has_construct
1452 : false_type
1453{
1454};
1455
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001456#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457
1458template <class _Alloc, class _Pointer>
1459struct __has_destroy
1460 : false_type
1461{
1462};
1463
1464template <class _Alloc>
1465struct __has_max_size
1466 : true_type
1467{
1468};
1469
1470template <class _Alloc>
1471struct __has_select_on_container_copy_construction
1472 : false_type
1473{
1474};
1475
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001476#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001478template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1479struct __alloc_traits_difference_type
1480{
1481 typedef typename pointer_traits<_Ptr>::difference_type type;
1482};
1483
1484template <class _Alloc, class _Ptr>
1485struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1486{
1487 typedef typename _Alloc::difference_type type;
1488};
1489
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001491struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492{
1493 typedef _Alloc allocator_type;
1494 typedef typename allocator_type::value_type value_type;
1495
1496 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1497 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1498 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1499 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1500
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001501 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1502 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503
1504 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1505 propagate_on_container_copy_assignment;
1506 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1507 propagate_on_container_move_assignment;
1508 typedef typename __propagate_on_container_swap<allocator_type>::type
1509 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001510 typedef typename __is_always_equal<allocator_type>::type
1511 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001513#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001515 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001517#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518 template <class _Tp> struct rebind_alloc
1519 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1520 template <class _Tp> struct rebind_traits
1521 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001522#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
Howard Hinnant756c69b2010-09-22 16:48:34 +00001524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 static pointer allocate(allocator_type& __a, size_type __n)
1526 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1529 {return allocate(__a, __n, __hint,
1530 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1531
Howard Hinnant756c69b2010-09-22 16:48:34 +00001532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001533 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 {__a.deallocate(__p, __n);}
1535
1536#ifndef _LIBCPP_HAS_NO_VARIADICS
1537 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001540 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001541 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001542#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001544 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001545 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 {
1547 ::new ((void*)__p) _Tp();
1548 }
1549 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001550 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001551 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 {
1553 ::new ((void*)__p) _Tp(__a0);
1554 }
1555 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001556 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001557 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 const _A1& __a1)
1559 {
1560 ::new ((void*)__p) _Tp(__a0, __a1);
1561 }
1562 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001563 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001564 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 const _A1& __a1, const _A2& __a2)
1566 {
1567 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1568 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001569#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570
1571 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 static void destroy(allocator_type& __a, _Tp* __p)
1574 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1575
Howard Hinnant756c69b2010-09-22 16:48:34 +00001576 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001577 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1579
Howard Hinnant756c69b2010-09-22 16:48:34 +00001580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 static allocator_type
1582 select_on_container_copy_construction(const allocator_type& __a)
1583 {return select_on_container_copy_construction(
1584 __has_select_on_container_copy_construction<const allocator_type>(),
1585 __a);}
1586
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001587 template <class _Ptr>
1588 _LIBCPP_INLINE_VISIBILITY
1589 static
1590 void
1591 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1592 {
1593 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1594 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1595 }
1596
1597 template <class _Tp>
1598 _LIBCPP_INLINE_VISIBILITY
1599 static
1600 typename enable_if
1601 <
1602 (is_same<allocator_type, allocator<_Tp> >::value
1603 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1604 is_trivially_move_constructible<_Tp>::value,
1605 void
1606 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001607 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001608 {
1609 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001610 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001611 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001612 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001613 __begin2 += _Np;
1614 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001615 }
1616
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001617 template <class _Iter, class _Ptr>
1618 _LIBCPP_INLINE_VISIBILITY
1619 static
1620 void
1621 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1622 {
1623 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1624 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1625 }
1626
1627 template <class _Tp>
1628 _LIBCPP_INLINE_VISIBILITY
1629 static
1630 typename enable_if
1631 <
1632 (is_same<allocator_type, allocator<_Tp> >::value
1633 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1634 is_trivially_move_constructible<_Tp>::value,
1635 void
1636 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001637 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001638 {
1639 typedef typename remove_const<_Tp>::type _Vp;
1640 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001641 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001642 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001643 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001644 __begin2 += _Np;
1645 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001646 }
1647
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001648 template <class _Ptr>
1649 _LIBCPP_INLINE_VISIBILITY
1650 static
1651 void
1652 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1653 {
1654 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001655 {
1656 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1657 --__end2;
1658 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001659 }
1660
1661 template <class _Tp>
1662 _LIBCPP_INLINE_VISIBILITY
1663 static
1664 typename enable_if
1665 <
1666 (is_same<allocator_type, allocator<_Tp> >::value
1667 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1668 is_trivially_move_constructible<_Tp>::value,
1669 void
1670 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001671 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001672 {
1673 ptrdiff_t _Np = __end1 - __begin1;
1674 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001675 if (_Np > 0)
1676 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 }
1678
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679private:
1680
Howard Hinnant756c69b2010-09-22 16:48:34 +00001681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 static pointer allocate(allocator_type& __a, size_type __n,
1683 const_void_pointer __hint, true_type)
1684 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001687 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 {return __a.allocate(__n);}
1689
1690#ifndef _LIBCPP_HAS_NO_VARIADICS
1691 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001694 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1698 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001699 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001701#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702
1703 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1706 {__a.destroy(__p);}
1707 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709 static void __destroy(false_type, allocator_type&, _Tp* __p)
1710 {
1711 __p->~_Tp();
1712 }
1713
Howard Hinnant756c69b2010-09-22 16:48:34 +00001714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 static size_type __max_size(true_type, const allocator_type& __a)
1716 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001719 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720
Howard Hinnant756c69b2010-09-22 16:48:34 +00001721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 static allocator_type
1723 select_on_container_copy_construction(true_type, const allocator_type& __a)
1724 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 static allocator_type
1727 select_on_container_copy_construction(false_type, const allocator_type& __a)
1728 {return __a;}
1729};
1730
Marshall Clow940e01c2015-04-07 05:21:38 +00001731template <class _Traits, class _Tp>
1732struct __rebind_alloc_helper
1733{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001734#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001735 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001736#else
1737 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1738#endif
1739};
1740
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741// allocator
1742
1743template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001744class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745{
1746public:
1747 typedef size_t size_type;
1748 typedef ptrdiff_t difference_type;
1749 typedef _Tp* pointer;
1750 typedef const _Tp* const_pointer;
1751 typedef _Tp& reference;
1752 typedef const _Tp& const_reference;
1753 typedef _Tp value_type;
1754
Howard Hinnant4931e092011-06-02 21:38:57 +00001755 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001756 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001757
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1759
Howard Hinnant719bda32011-05-28 14:41:13 +00001760 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1761 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1762 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001763 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001764 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001765 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001767 {
1768 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001769 __throw_length_error("allocator<T>::allocate(size_t n)"
1770 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001771 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1772 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001773 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001774 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1776 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001777#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 template <class _Up, class... _Args>
1779 _LIBCPP_INLINE_VISIBILITY
1780 void
1781 construct(_Up* __p, _Args&&... __args)
1782 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001783 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001785#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 _LIBCPP_INLINE_VISIBILITY
1787 void
1788 construct(pointer __p)
1789 {
1790 ::new((void*)__p) _Tp();
1791 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001792# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001793
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 template <class _A0>
1795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001796 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 construct(pointer __p, _A0& __a0)
1798 {
1799 ::new((void*)__p) _Tp(__a0);
1800 }
1801 template <class _A0>
1802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001803 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 construct(pointer __p, const _A0& __a0)
1805 {
1806 ::new((void*)__p) _Tp(__a0);
1807 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001808# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0, class _A1>
1810 _LIBCPP_INLINE_VISIBILITY
1811 void
1812 construct(pointer __p, _A0& __a0, _A1& __a1)
1813 {
1814 ::new((void*)__p) _Tp(__a0, __a1);
1815 }
1816 template <class _A0, class _A1>
1817 _LIBCPP_INLINE_VISIBILITY
1818 void
1819 construct(pointer __p, const _A0& __a0, _A1& __a1)
1820 {
1821 ::new((void*)__p) _Tp(__a0, __a1);
1822 }
1823 template <class _A0, class _A1>
1824 _LIBCPP_INLINE_VISIBILITY
1825 void
1826 construct(pointer __p, _A0& __a0, const _A1& __a1)
1827 {
1828 ::new((void*)__p) _Tp(__a0, __a1);
1829 }
1830 template <class _A0, class _A1>
1831 _LIBCPP_INLINE_VISIBILITY
1832 void
1833 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1834 {
1835 ::new((void*)__p) _Tp(__a0, __a1);
1836 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001837#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1839};
1840
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001841template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001842class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001843{
1844public:
1845 typedef size_t size_type;
1846 typedef ptrdiff_t difference_type;
1847 typedef const _Tp* pointer;
1848 typedef const _Tp* const_pointer;
1849 typedef const _Tp& reference;
1850 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001851 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001852
1853 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001854 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001855
1856 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1857
1858 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1859 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1860 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1861 {return _VSTD::addressof(__x);}
1862 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001863 {
1864 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001865 __throw_length_error("allocator<const T>::allocate(size_t n)"
1866 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001867 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001868 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001869 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001870 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001871 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1872 {return size_type(~0) / sizeof(_Tp);}
1873#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1874 template <class _Up, class... _Args>
1875 _LIBCPP_INLINE_VISIBILITY
1876 void
1877 construct(_Up* __p, _Args&&... __args)
1878 {
1879 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1880 }
1881#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1882 _LIBCPP_INLINE_VISIBILITY
1883 void
1884 construct(pointer __p)
1885 {
1886 ::new((void*)__p) _Tp();
1887 }
1888# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001889
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001890 template <class _A0>
1891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001892 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001893 construct(pointer __p, _A0& __a0)
1894 {
1895 ::new((void*)__p) _Tp(__a0);
1896 }
1897 template <class _A0>
1898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001899 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001900 construct(pointer __p, const _A0& __a0)
1901 {
1902 ::new((void*)__p) _Tp(__a0);
1903 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001904# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1905 template <class _A0, class _A1>
1906 _LIBCPP_INLINE_VISIBILITY
1907 void
1908 construct(pointer __p, _A0& __a0, _A1& __a1)
1909 {
1910 ::new((void*)__p) _Tp(__a0, __a1);
1911 }
1912 template <class _A0, class _A1>
1913 _LIBCPP_INLINE_VISIBILITY
1914 void
1915 construct(pointer __p, const _A0& __a0, _A1& __a1)
1916 {
1917 ::new((void*)__p) _Tp(__a0, __a1);
1918 }
1919 template <class _A0, class _A1>
1920 _LIBCPP_INLINE_VISIBILITY
1921 void
1922 construct(pointer __p, _A0& __a0, const _A1& __a1)
1923 {
1924 ::new((void*)__p) _Tp(__a0, __a1);
1925 }
1926 template <class _A0, class _A1>
1927 _LIBCPP_INLINE_VISIBILITY
1928 void
1929 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1930 {
1931 ::new((void*)__p) _Tp(__a0, __a1);
1932 }
1933#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1934 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1935};
1936
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937template <class _Tp, class _Up>
1938inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001939bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940
1941template <class _Tp, class _Up>
1942inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001943bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944
1945template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001946class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947 : public iterator<output_iterator_tag,
1948 _Tp, // purposefully not C++03
1949 ptrdiff_t, // purposefully not C++03
1950 _Tp*, // purposefully not C++03
1951 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1952{
1953private:
1954 _OutputIterator __x_;
1955public:
1956 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1957 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1958 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1959 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001960#if _LIBCPP_STD_VER >= 14
1961 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1962 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1963#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1965 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1966 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001967#if _LIBCPP_STD_VER >= 14
1968 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1969#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970};
1971
1972template <class _Tp>
1973pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001974get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975{
1976 pair<_Tp*, ptrdiff_t> __r(0, 0);
1977 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1978 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1979 / sizeof(_Tp);
1980 if (__n > __m)
1981 __n = __m;
1982 while (__n > 0)
1983 {
1984 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1985 if (__r.first)
1986 {
1987 __r.second = __n;
1988 break;
1989 }
1990 __n /= 2;
1991 }
1992 return __r;
1993}
1994
1995template <class _Tp>
1996inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001997void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998
Marshall Clowb22274f2017-01-24 22:22:33 +00001999#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000template <class _Tp>
2001struct auto_ptr_ref
2002{
2003 _Tp* __ptr_;
2004};
2005
2006template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002007class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008{
2009private:
2010 _Tp* __ptr_;
2011public:
2012 typedef _Tp element_type;
2013
2014 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2015 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2016 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2017 : __ptr_(__p.release()) {}
2018 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2019 {reset(__p.release()); return *this;}
2020 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2021 {reset(__p.release()); return *this;}
2022 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2023 {reset(__p.__ptr_); return *this;}
2024 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2025
2026 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2027 {return *__ptr_;}
2028 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2029 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2030 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2031 {
2032 _Tp* __t = __ptr_;
2033 __ptr_ = 0;
2034 return __t;
2035 }
2036 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2037 {
2038 if (__ptr_ != __p)
2039 delete __ptr_;
2040 __ptr_ = __p;
2041 }
2042
2043 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2044 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2045 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2046 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2047 {return auto_ptr<_Up>(release());}
2048};
2049
2050template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002051class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052{
2053public:
2054 typedef void element_type;
2055};
Marshall Clowb22274f2017-01-24 22:22:33 +00002056#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2059 typename remove_cv<_T2>::type>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002060 bool = is_empty<_T1>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002061 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002062 bool = is_empty<_T2>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002063 && !__libcpp_is_final<_T2>::value
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002064 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065struct __libcpp_compressed_pair_switch;
2066
2067template <class _T1, class _T2, bool IsSame>
2068struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2069
2070template <class _T1, class _T2, bool IsSame>
2071struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2072
2073template <class _T1, class _T2, bool IsSame>
2074struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2075
2076template <class _T1, class _T2>
2077struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2078
2079template <class _T1, class _T2>
2080struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2081
2082template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2083class __libcpp_compressed_pair_imp;
2084
2085template <class _T1, class _T2>
2086class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2087{
2088private:
2089 _T1 __first_;
2090 _T2 __second_;
2091public:
2092 typedef _T1 _T1_param;
2093 typedef _T2 _T2_param;
2094
2095 typedef typename remove_reference<_T1>::type& _T1_reference;
2096 typedef typename remove_reference<_T2>::type& _T2_reference;
2097
2098 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2099 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2100
Marshall Clowac92bfe2015-07-16 03:05:06 +00002101 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002102 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002103 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002104 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002105 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002107 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108
Howard Hinnant83b1c052011-12-19 17:58:44 +00002109#ifndef _LIBCPP_HAS_NO_VARIADICS
2110
2111 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2112 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002113 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002114 tuple<_Args1...> __first_args,
2115 tuple<_Args2...> __second_args,
2116 __tuple_indices<_I1...>,
2117 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002118 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2119 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002120 {}
2121
2122#endif // _LIBCPP_HAS_NO_VARIADICS
2123
Howard Hinnant719bda32011-05-28 14:41:13 +00002124 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2125 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126
Howard Hinnant719bda32011-05-28 14:41:13 +00002127 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2128 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129
2130 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002131 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002132 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002134 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135 swap(__first_, __x.__first_);
2136 swap(__second_, __x.__second_);
2137 }
2138};
2139
2140template <class _T1, class _T2>
2141class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2142 : private _T1
2143{
2144private:
2145 _T2 __second_;
2146public:
2147 typedef _T1 _T1_param;
2148 typedef _T2 _T2_param;
2149
2150 typedef _T1& _T1_reference;
2151 typedef typename remove_reference<_T2>::type& _T2_reference;
2152
2153 typedef const _T1& _T1_const_reference;
2154 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2155
Marshall Clowac92bfe2015-07-16 03:05:06 +00002156 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002157 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002158 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002159 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002160 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002162 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163
Howard Hinnant83b1c052011-12-19 17:58:44 +00002164#ifndef _LIBCPP_HAS_NO_VARIADICS
2165
2166 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2167 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002168 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002169 tuple<_Args1...> __first_args,
2170 tuple<_Args2...> __second_args,
2171 __tuple_indices<_I1...>,
2172 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002173 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2174 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002175 {}
2176
2177#endif // _LIBCPP_HAS_NO_VARIADICS
2178
Howard Hinnant719bda32011-05-28 14:41:13 +00002179 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2180 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181
Howard Hinnant719bda32011-05-28 14:41:13 +00002182 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2183 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184
2185 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002186 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002187 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002189 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190 swap(__second_, __x.__second_);
2191 }
2192};
2193
2194template <class _T1, class _T2>
2195class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2196 : private _T2
2197{
2198private:
2199 _T1 __first_;
2200public:
2201 typedef _T1 _T1_param;
2202 typedef _T2 _T2_param;
2203
2204 typedef typename remove_reference<_T1>::type& _T1_reference;
2205 typedef _T2& _T2_reference;
2206
2207 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2208 typedef const _T2& _T2_const_reference;
2209
Marshall Clowac92bfe2015-07-16 03:05:06 +00002210 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002212 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002214 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002216 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2217 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002218 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219
Howard Hinnant83b1c052011-12-19 17:58:44 +00002220#ifndef _LIBCPP_HAS_NO_VARIADICS
2221
2222 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2223 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002224 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002225 tuple<_Args1...> __first_args,
2226 tuple<_Args2...> __second_args,
2227 __tuple_indices<_I1...>,
2228 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002229 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2230 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002231
2232 {}
2233
2234#endif // _LIBCPP_HAS_NO_VARIADICS
2235
Howard Hinnant719bda32011-05-28 14:41:13 +00002236 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2237 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238
Howard Hinnant719bda32011-05-28 14:41:13 +00002239 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2240 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241
2242 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002243 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002244 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002246 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 swap(__first_, __x.__first_);
2248 }
2249};
2250
2251template <class _T1, class _T2>
2252class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2253 : private _T1,
2254 private _T2
2255{
2256public:
2257 typedef _T1 _T1_param;
2258 typedef _T2 _T2_param;
2259
2260 typedef _T1& _T1_reference;
2261 typedef _T2& _T2_reference;
2262
2263 typedef const _T1& _T1_const_reference;
2264 typedef const _T2& _T2_const_reference;
2265
2266 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2267 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002268 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002270 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002272 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273
Howard Hinnant83b1c052011-12-19 17:58:44 +00002274#ifndef _LIBCPP_HAS_NO_VARIADICS
2275
2276 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2277 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002278 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002279 tuple<_Args1...> __first_args,
2280 tuple<_Args2...> __second_args,
2281 __tuple_indices<_I1...>,
2282 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002283 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2284 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002285 {}
2286
2287#endif // _LIBCPP_HAS_NO_VARIADICS
2288
Howard Hinnant719bda32011-05-28 14:41:13 +00002289 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2290 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291
Howard Hinnant719bda32011-05-28 14:41:13 +00002292 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2293 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294
Howard Hinnant28b24882011-12-01 20:21:04 +00002295 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002296 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002297 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298 {
2299 }
2300};
2301
2302template <class _T1, class _T2>
2303class __compressed_pair
2304 : private __libcpp_compressed_pair_imp<_T1, _T2>
2305{
2306 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2307public:
2308 typedef typename base::_T1_param _T1_param;
2309 typedef typename base::_T2_param _T2_param;
2310
2311 typedef typename base::_T1_reference _T1_reference;
2312 typedef typename base::_T2_reference _T2_reference;
2313
2314 typedef typename base::_T1_const_reference _T1_const_reference;
2315 typedef typename base::_T2_const_reference _T2_const_reference;
2316
2317 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002318 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002319 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002320 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002321 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002323 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324
Howard Hinnant83b1c052011-12-19 17:58:44 +00002325#ifndef _LIBCPP_HAS_NO_VARIADICS
2326
2327 template <class... _Args1, class... _Args2>
2328 _LIBCPP_INLINE_VISIBILITY
2329 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2330 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002331 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002332 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2333 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2334 {}
2335
2336#endif // _LIBCPP_HAS_NO_VARIADICS
2337
Howard Hinnant719bda32011-05-28 14:41:13 +00002338 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2339 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340
Howard Hinnant719bda32011-05-28 14:41:13 +00002341 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2342 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343
Howard Hinnant719bda32011-05-28 14:41:13 +00002344 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2345 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002346 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002347 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348};
2349
2350template <class _T1, class _T2>
2351inline _LIBCPP_INLINE_VISIBILITY
2352void
2353swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002354 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002355 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356 {__x.swap(__y);}
2357
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002358// __same_or_less_cv_qualified
2359
2360template <class _Ptr1, class _Ptr2,
2361 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2362 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2363 >::value
2364 >
2365struct __same_or_less_cv_qualified_imp
2366 : is_convertible<_Ptr1, _Ptr2> {};
2367
2368template <class _Ptr1, class _Ptr2>
2369struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2370 : false_type {};
2371
Marshall Clowdf296162014-04-26 05:19:48 +00002372template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2373 is_same<_Ptr1, _Ptr2>::value ||
2374 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002375struct __same_or_less_cv_qualified
2376 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2377
2378template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002379struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002380 : false_type {};
2381
2382// default_delete
2383
Howard Hinnantc51e1022010-05-11 19:42:16 +00002384template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002385struct _LIBCPP_TEMPLATE_VIS default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386{
Eric Fiselier07b2b552016-11-18 06:42:17 +00002387#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002388 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2389#else
2390 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2391#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 template <class _Up>
2393 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002394 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2395 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 {
2397 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002398 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399 delete __ptr;
2400 }
2401};
2402
2403template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002404struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002406public:
Eric Fiselier07b2b552016-11-18 06:42:17 +00002407#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002408 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2409#else
2410 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2411#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002412 template <class _Up>
2413 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002414 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002415 template <class _Up>
2416 _LIBCPP_INLINE_VISIBILITY
2417 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002418 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419 {
2420 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002421 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422 delete [] __ptr;
2423 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424};
2425
2426template <class _Tp, class _Dp = default_delete<_Tp> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002427class _LIBCPP_TEMPLATE_VIS unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428{
2429public:
2430 typedef _Tp element_type;
2431 typedef _Dp deleter_type;
2432 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2433private:
2434 __compressed_pair<pointer, deleter_type> __ptr_;
2435
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002436#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437 unique_ptr(unique_ptr&);
2438 template <class _Up, class _Ep>
2439 unique_ptr(unique_ptr<_Up, _Ep>&);
2440 unique_ptr& operator=(unique_ptr&);
2441 template <class _Up, class _Ep>
2442 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002443#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444
2445 struct __nat {int __for_bool_;};
2446
2447 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2448 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2449public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002450 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 : __ptr_(pointer())
2452 {
2453 static_assert(!is_pointer<deleter_type>::value,
2454 "unique_ptr constructed with null function pointer deleter");
2455 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002456 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457 : __ptr_(pointer())
2458 {
2459 static_assert(!is_pointer<deleter_type>::value,
2460 "unique_ptr constructed with null function pointer deleter");
2461 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002462 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002463 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 {
2465 static_assert(!is_pointer<deleter_type>::value,
2466 "unique_ptr constructed with null function pointer deleter");
2467 }
2468
Howard Hinnant74279a52010-09-04 23:28:19 +00002469#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2471 is_reference<deleter_type>::value,
2472 deleter_type,
2473 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002474 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 : __ptr_(__p, __d) {}
2476
2477 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002478 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002479 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 {
2481 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2482 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002483 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002484 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485 template <class _Up, class _Ep>
2486 _LIBCPP_INLINE_VISIBILITY
2487 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2488 typename enable_if
2489 <
2490 !is_array<_Up>::value &&
2491 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2492 is_convertible<_Ep, deleter_type>::value &&
2493 (
2494 !is_reference<deleter_type>::value ||
2495 is_same<deleter_type, _Ep>::value
2496 ),
2497 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002498 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002499 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500
Marshall Clowb22274f2017-01-24 22:22:33 +00002501#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502 template <class _Up>
2503 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2504 typename enable_if<
2505 is_convertible<_Up*, _Tp*>::value &&
2506 is_same<_Dp, default_delete<_Tp> >::value,
2507 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002508 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509 : __ptr_(__p.release())
2510 {
2511 }
Marshall Clowb22274f2017-01-24 22:22:33 +00002512#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513
Howard Hinnant719bda32011-05-28 14:41:13 +00002514 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515 {
2516 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002517 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518 return *this;
2519 }
2520
2521 template <class _Up, class _Ep>
2522 _LIBCPP_INLINE_VISIBILITY
2523 typename enable_if
2524 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002525 !is_array<_Up>::value &&
2526 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2527 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528 unique_ptr&
2529 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002530 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531 {
2532 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002533 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534 return *this;
2535 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002536#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537
2538 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2539 {
2540 return __rv<unique_ptr>(*this);
2541 }
2542
2543 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002544 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545
2546 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002547 _LIBCPP_INLINE_VISIBILITY
2548 typename enable_if<
2549 !is_array<_Up>::value &&
2550 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2551 is_assignable<deleter_type&, _Ep&>::value,
2552 unique_ptr&
2553 >::type
2554 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555 {
2556 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002557 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558 return *this;
2559 }
2560
2561 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002562 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563
Marshall Clowb22274f2017-01-24 22:22:33 +00002564#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565 template <class _Up>
2566 _LIBCPP_INLINE_VISIBILITY
2567 typename enable_if<
2568 is_convertible<_Up*, _Tp*>::value &&
2569 is_same<_Dp, default_delete<_Tp> >::value,
2570 unique_ptr&
2571 >::type
2572 operator=(auto_ptr<_Up> __p)
2573 {reset(__p.release()); return *this;}
Marshall Clowb22274f2017-01-24 22:22:33 +00002574#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00002575#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2577
Howard Hinnant719bda32011-05-28 14:41:13 +00002578 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579 {
2580 reset();
2581 return *this;
2582 }
2583
2584 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2585 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002586 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2587 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2588 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2589 {return __ptr_.second();}
2590 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2591 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002592 _LIBCPP_INLINE_VISIBILITY
2593 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2594 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595
Howard Hinnant719bda32011-05-28 14:41:13 +00002596 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 {
2598 pointer __t = __ptr_.first();
2599 __ptr_.first() = pointer();
2600 return __t;
2601 }
2602
Howard Hinnant719bda32011-05-28 14:41:13 +00002603 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604 {
2605 pointer __tmp = __ptr_.first();
2606 __ptr_.first() = __p;
2607 if (__tmp)
2608 __ptr_.second()(__tmp);
2609 }
2610
Howard Hinnant719bda32011-05-28 14:41:13 +00002611 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2612 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613};
2614
2615template <class _Tp, class _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002616class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617{
2618public:
2619 typedef _Tp element_type;
2620 typedef _Dp deleter_type;
2621 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2622private:
2623 __compressed_pair<pointer, deleter_type> __ptr_;
2624
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002625#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626 unique_ptr(unique_ptr&);
2627 template <class _Up>
2628 unique_ptr(unique_ptr<_Up>&);
2629 unique_ptr& operator=(unique_ptr&);
2630 template <class _Up>
2631 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002632#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633
2634 struct __nat {int __for_bool_;};
2635
2636 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2637 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2638public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002639 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 : __ptr_(pointer())
2641 {
2642 static_assert(!is_pointer<deleter_type>::value,
2643 "unique_ptr constructed with null function pointer deleter");
2644 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002645 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646 : __ptr_(pointer())
2647 {
2648 static_assert(!is_pointer<deleter_type>::value,
2649 "unique_ptr constructed with null function pointer deleter");
2650 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002651#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002652 template <class _Pp>
2653 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2654 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 : __ptr_(__p)
2656 {
2657 static_assert(!is_pointer<deleter_type>::value,
2658 "unique_ptr constructed with null function pointer deleter");
2659 }
2660
Logan Chiend435f8b2014-01-31 09:30:46 +00002661 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002662 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 is_reference<deleter_type>::value,
2664 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002665 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2666 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002667 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668 : __ptr_(__p, __d) {}
2669
2670 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2671 is_reference<deleter_type>::value,
2672 deleter_type,
2673 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002674 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675 : __ptr_(pointer(), __d) {}
2676
Logan Chiend435f8b2014-01-31 09:30:46 +00002677 template <class _Pp>
2678 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2679 typename remove_reference<deleter_type>::type&& __d,
2680 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002681 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002682 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 {
2684 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2685 }
2686
2687 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002688 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002689 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690 {
2691 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2692 }
2693
Howard Hinnant719bda32011-05-28 14:41:13 +00002694 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002695 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696
Howard Hinnant719bda32011-05-28 14:41:13 +00002697 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 {
2699 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002700 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 return *this;
2702 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002703
2704 template <class _Up, class _Ep>
2705 _LIBCPP_INLINE_VISIBILITY
2706 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2707 typename enable_if
2708 <
2709 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002710 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002711 && is_convertible<_Ep, deleter_type>::value &&
2712 (
2713 !is_reference<deleter_type>::value ||
2714 is_same<deleter_type, _Ep>::value
2715 ),
2716 __nat
2717 >::type = __nat()
2718 ) _NOEXCEPT
2719 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2720
2721
2722 template <class _Up, class _Ep>
2723 _LIBCPP_INLINE_VISIBILITY
2724 typename enable_if
2725 <
2726 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002727 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2728 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002729 unique_ptr&
2730 >::type
2731 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2732 {
2733 reset(__u.release());
2734 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2735 return *this;
2736 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002737#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738
2739 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2740 : __ptr_(__p)
2741 {
2742 static_assert(!is_pointer<deleter_type>::value,
2743 "unique_ptr constructed with null function pointer deleter");
2744 }
2745
2746 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002747 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748
2749 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002750 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751
2752 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2753 {
2754 return __rv<unique_ptr>(*this);
2755 }
2756
2757 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002758 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759
2760 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2761 {
2762 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002763 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 return *this;
2765 }
2766
Howard Hinnant74279a52010-09-04 23:28:19 +00002767#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2769
Howard Hinnant719bda32011-05-28 14:41:13 +00002770 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 {
2772 reset();
2773 return *this;
2774 }
2775
2776 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2777 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002778 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2779 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2780 {return __ptr_.second();}
2781 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2782 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002783 _LIBCPP_INLINE_VISIBILITY
2784 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2785 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786
Howard Hinnant719bda32011-05-28 14:41:13 +00002787 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 {
2789 pointer __t = __ptr_.first();
2790 __ptr_.first() = pointer();
2791 return __t;
2792 }
2793
Logan Chiend435f8b2014-01-31 09:30:46 +00002794 template <class _Pp>
2795 _LIBCPP_INLINE_VISIBILITY
2796 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2797 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 {
2799 pointer __tmp = __ptr_.first();
2800 __ptr_.first() = __p;
2801 if (__tmp)
2802 __ptr_.second()(__tmp);
2803 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002804 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805 {
2806 pointer __tmp = __ptr_.first();
2807 __ptr_.first() = nullptr;
2808 if (__tmp)
2809 __ptr_.second()(__tmp);
2810 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811
2812 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2813private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002814
Howard Hinnant74279a52010-09-04 23:28:19 +00002815#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 template <class _Up>
2817 explicit unique_ptr(_Up);
2818 template <class _Up>
2819 unique_ptr(_Up __u,
2820 typename conditional<
2821 is_reference<deleter_type>::value,
2822 deleter_type,
2823 typename add_lvalue_reference<const deleter_type>::type>::type,
2824 typename enable_if
2825 <
2826 is_convertible<_Up, pointer>::value,
2827 __nat
2828 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00002829#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830};
2831
2832template <class _Tp, class _Dp>
2833inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002834typename enable_if<
2835 __is_swappable<_Dp>::value,
2836 void
2837>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002838swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839
2840template <class _T1, class _D1, class _T2, class _D2>
2841inline _LIBCPP_INLINE_VISIBILITY
2842bool
2843operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2844
2845template <class _T1, class _D1, class _T2, class _D2>
2846inline _LIBCPP_INLINE_VISIBILITY
2847bool
2848operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2849
2850template <class _T1, class _D1, class _T2, class _D2>
2851inline _LIBCPP_INLINE_VISIBILITY
2852bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002853operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2854{
2855 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2856 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002857 typedef typename common_type<_P1, _P2>::type _Vp;
2858 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002859}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860
2861template <class _T1, class _D1, class _T2, class _D2>
2862inline _LIBCPP_INLINE_VISIBILITY
2863bool
2864operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2865
2866template <class _T1, class _D1, class _T2, class _D2>
2867inline _LIBCPP_INLINE_VISIBILITY
2868bool
2869operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2870
2871template <class _T1, class _D1, class _T2, class _D2>
2872inline _LIBCPP_INLINE_VISIBILITY
2873bool
2874operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2875
Howard Hinnantb17caf92012-02-21 21:02:58 +00002876template <class _T1, class _D1>
2877inline _LIBCPP_INLINE_VISIBILITY
2878bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002879operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002880{
2881 return !__x;
2882}
2883
2884template <class _T1, class _D1>
2885inline _LIBCPP_INLINE_VISIBILITY
2886bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002887operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002888{
2889 return !__x;
2890}
2891
2892template <class _T1, class _D1>
2893inline _LIBCPP_INLINE_VISIBILITY
2894bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002895operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002896{
2897 return static_cast<bool>(__x);
2898}
2899
2900template <class _T1, class _D1>
2901inline _LIBCPP_INLINE_VISIBILITY
2902bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002903operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002904{
2905 return static_cast<bool>(__x);
2906}
2907
2908template <class _T1, class _D1>
2909inline _LIBCPP_INLINE_VISIBILITY
2910bool
2911operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2912{
2913 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2914 return less<_P1>()(__x.get(), nullptr);
2915}
2916
2917template <class _T1, class _D1>
2918inline _LIBCPP_INLINE_VISIBILITY
2919bool
2920operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2921{
2922 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2923 return less<_P1>()(nullptr, __x.get());
2924}
2925
2926template <class _T1, class _D1>
2927inline _LIBCPP_INLINE_VISIBILITY
2928bool
2929operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2930{
2931 return nullptr < __x;
2932}
2933
2934template <class _T1, class _D1>
2935inline _LIBCPP_INLINE_VISIBILITY
2936bool
2937operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2938{
2939 return __x < nullptr;
2940}
2941
2942template <class _T1, class _D1>
2943inline _LIBCPP_INLINE_VISIBILITY
2944bool
2945operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2946{
2947 return !(nullptr < __x);
2948}
2949
2950template <class _T1, class _D1>
2951inline _LIBCPP_INLINE_VISIBILITY
2952bool
2953operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2954{
2955 return !(__x < nullptr);
2956}
2957
2958template <class _T1, class _D1>
2959inline _LIBCPP_INLINE_VISIBILITY
2960bool
2961operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2962{
2963 return !(__x < nullptr);
2964}
2965
2966template <class _T1, class _D1>
2967inline _LIBCPP_INLINE_VISIBILITY
2968bool
2969operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2970{
2971 return !(nullptr < __x);
2972}
2973
Howard Hinnant9e028f12012-05-01 15:37:54 +00002974#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2975
2976template <class _Tp, class _Dp>
2977inline _LIBCPP_INLINE_VISIBILITY
2978unique_ptr<_Tp, _Dp>
2979move(unique_ptr<_Tp, _Dp>& __t)
2980{
2981 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
2982}
2983
2984#endif
2985
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002986#if _LIBCPP_STD_VER > 11
2987
2988template<class _Tp>
2989struct __unique_if
2990{
2991 typedef unique_ptr<_Tp> __unique_single;
2992};
2993
2994template<class _Tp>
2995struct __unique_if<_Tp[]>
2996{
2997 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2998};
2999
3000template<class _Tp, size_t _Np>
3001struct __unique_if<_Tp[_Np]>
3002{
3003 typedef void __unique_array_known_bound;
3004};
3005
3006template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003007inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003008typename __unique_if<_Tp>::__unique_single
3009make_unique(_Args&&... __args)
3010{
3011 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3012}
3013
3014template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003015inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003016typename __unique_if<_Tp>::__unique_array_unknown_bound
3017make_unique(size_t __n)
3018{
3019 typedef typename remove_extent<_Tp>::type _Up;
3020 return unique_ptr<_Tp>(new _Up[__n]());
3021}
3022
3023template<class _Tp, class... _Args>
3024 typename __unique_if<_Tp>::__unique_array_known_bound
3025 make_unique(_Args&&...) = delete;
3026
3027#endif // _LIBCPP_STD_VER > 11
3028
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003029template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003030#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003031struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003032#else
3033struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3034 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3035#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003036{
3037 typedef unique_ptr<_Tp, _Dp> argument_type;
3038 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003040 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003041 {
3042 typedef typename argument_type::pointer pointer;
3043 return hash<pointer>()(__ptr.get());
3044 }
3045};
3046
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047struct __destruct_n
3048{
3049private:
3050 size_t size;
3051
3052 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003053 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3055
3056 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003057 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058 {}
3059
Howard Hinnant719bda32011-05-28 14:41:13 +00003060 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003062 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 {}
3064
Howard Hinnant719bda32011-05-28 14:41:13 +00003065 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003067 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068 {}
3069public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003070 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3071 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072
3073 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003074 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003075 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076
3077 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003078 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003079 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080
3081 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003082 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003083 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084};
3085
3086template <class _Alloc>
3087class __allocator_destructor
3088{
3089 typedef allocator_traits<_Alloc> __alloc_traits;
3090public:
3091 typedef typename __alloc_traits::pointer pointer;
3092 typedef typename __alloc_traits::size_type size_type;
3093private:
3094 _Alloc& __alloc_;
3095 size_type __s_;
3096public:
3097 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003098 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003101 void operator()(pointer __p) _NOEXCEPT
3102 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103};
3104
3105template <class _InputIterator, class _ForwardIterator>
3106_ForwardIterator
3107uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3108{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003110#ifndef _LIBCPP_NO_EXCEPTIONS
3111 _ForwardIterator __s = __r;
3112 try
3113 {
3114#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003115 for (; __f != __l; ++__f, (void) ++__r)
3116 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003117#ifndef _LIBCPP_NO_EXCEPTIONS
3118 }
3119 catch (...)
3120 {
3121 for (; __s != __r; ++__s)
3122 __s->~value_type();
3123 throw;
3124 }
3125#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126 return __r;
3127}
3128
3129template <class _InputIterator, class _Size, class _ForwardIterator>
3130_ForwardIterator
3131uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3132{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003134#ifndef _LIBCPP_NO_EXCEPTIONS
3135 _ForwardIterator __s = __r;
3136 try
3137 {
3138#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003139 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3140 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003141#ifndef _LIBCPP_NO_EXCEPTIONS
3142 }
3143 catch (...)
3144 {
3145 for (; __s != __r; ++__s)
3146 __s->~value_type();
3147 throw;
3148 }
3149#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150 return __r;
3151}
3152
3153template <class _ForwardIterator, class _Tp>
3154void
3155uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3156{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003158#ifndef _LIBCPP_NO_EXCEPTIONS
3159 _ForwardIterator __s = __f;
3160 try
3161 {
3162#endif
3163 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003164 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003165#ifndef _LIBCPP_NO_EXCEPTIONS
3166 }
3167 catch (...)
3168 {
3169 for (; __s != __f; ++__s)
3170 __s->~value_type();
3171 throw;
3172 }
3173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174}
3175
3176template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003177_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3179{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003181#ifndef _LIBCPP_NO_EXCEPTIONS
3182 _ForwardIterator __s = __f;
3183 try
3184 {
3185#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003186 for (; __n > 0; ++__f, (void) --__n)
3187 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003188#ifndef _LIBCPP_NO_EXCEPTIONS
3189 }
3190 catch (...)
3191 {
3192 for (; __s != __f; ++__s)
3193 __s->~value_type();
3194 throw;
3195 }
3196#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003197 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198}
3199
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003200#if _LIBCPP_STD_VER > 14
3201
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003202template <class _Tp>
3203inline _LIBCPP_INLINE_VISIBILITY
3204void destroy_at(_Tp* __loc) {
3205 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3206 __loc->~_Tp();
3207}
3208
3209template <class _ForwardIterator>
3210inline _LIBCPP_INLINE_VISIBILITY
3211void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3212 for (; __first != __last; ++__first)
3213 _VSTD::destroy_at(_VSTD::addressof(*__first));
3214}
3215
3216template <class _ForwardIterator, class _Size>
3217inline _LIBCPP_INLINE_VISIBILITY
3218_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3219 for (; __n > 0; (void)++__first, --__n)
3220 _VSTD::destroy_at(_VSTD::addressof(*__first));
3221 return __first;
3222}
3223
Eric Fiselier290c07c2016-10-11 21:13:44 +00003224template <class _ForwardIterator>
3225inline _LIBCPP_INLINE_VISIBILITY
3226void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3227 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3228 auto __idx = __first;
3229#ifndef _LIBCPP_NO_EXCEPTIONS
3230 try {
3231#endif
3232 for (; __idx != __last; ++__idx)
3233 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3234#ifndef _LIBCPP_NO_EXCEPTIONS
3235 } catch (...) {
3236 _VSTD::destroy(__first, __idx);
3237 throw;
3238 }
3239#endif
3240}
3241
3242template <class _ForwardIterator, class _Size>
3243inline _LIBCPP_INLINE_VISIBILITY
3244_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3245 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3246 auto __idx = __first;
3247#ifndef _LIBCPP_NO_EXCEPTIONS
3248 try {
3249#endif
3250 for (; __n > 0; (void)++__idx, --__n)
3251 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3252 return __idx;
3253#ifndef _LIBCPP_NO_EXCEPTIONS
3254 } catch (...) {
3255 _VSTD::destroy(__first, __idx);
3256 throw;
3257 }
3258#endif
3259}
3260
3261
3262template <class _ForwardIterator>
3263inline _LIBCPP_INLINE_VISIBILITY
3264void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3265 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3266 auto __idx = __first;
3267#ifndef _LIBCPP_NO_EXCEPTIONS
3268 try {
3269#endif
3270 for (; __idx != __last; ++__idx)
3271 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3272#ifndef _LIBCPP_NO_EXCEPTIONS
3273 } catch (...) {
3274 _VSTD::destroy(__first, __idx);
3275 throw;
3276 }
3277#endif
3278}
3279
3280template <class _ForwardIterator, class _Size>
3281inline _LIBCPP_INLINE_VISIBILITY
3282_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3283 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3284 auto __idx = __first;
3285#ifndef _LIBCPP_NO_EXCEPTIONS
3286 try {
3287#endif
3288 for (; __n > 0; (void)++__idx, --__n)
3289 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3290 return __idx;
3291#ifndef _LIBCPP_NO_EXCEPTIONS
3292 } catch (...) {
3293 _VSTD::destroy(__first, __idx);
3294 throw;
3295 }
3296#endif
3297}
3298
3299
3300template <class _InputIt, class _ForwardIt>
3301inline _LIBCPP_INLINE_VISIBILITY
3302_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3303 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3304 auto __idx = __first_res;
3305#ifndef _LIBCPP_NO_EXCEPTIONS
3306 try {
3307#endif
3308 for (; __first != __last; (void)++__idx, ++__first)
3309 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3310 return __idx;
3311#ifndef _LIBCPP_NO_EXCEPTIONS
3312 } catch (...) {
3313 _VSTD::destroy(__first_res, __idx);
3314 throw;
3315 }
3316#endif
3317}
3318
3319template <class _InputIt, class _Size, class _ForwardIt>
3320inline _LIBCPP_INLINE_VISIBILITY
3321pair<_InputIt, _ForwardIt>
3322uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3323 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3324 auto __idx = __first_res;
3325#ifndef _LIBCPP_NO_EXCEPTIONS
3326 try {
3327#endif
3328 for (; __n > 0; ++__idx, (void)++__first, --__n)
3329 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3330 return {__first, __idx};
3331#ifndef _LIBCPP_NO_EXCEPTIONS
3332 } catch (...) {
3333 _VSTD::destroy(__first_res, __idx);
3334 throw;
3335 }
3336#endif
3337}
3338
3339
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003340#endif // _LIBCPP_STD_VER > 14
3341
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003342// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3343// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003344// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003345#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3346 && defined(__ATOMIC_RELAXED) \
3347 && defined(__ATOMIC_ACQ_REL)
3348# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3349#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3350# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3351#endif
3352
3353template <class _Tp>
3354inline _LIBCPP_INLINE_VISIBILITY _Tp
3355__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3356{
3357#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3358 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3359#else
3360 return __t += 1;
3361#endif
3362}
3363
3364template <class _Tp>
3365inline _LIBCPP_INLINE_VISIBILITY _Tp
3366__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3367{
3368#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3369 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3370#else
3371 return __t -= 1;
3372#endif
3373}
3374
Howard Hinnant756c69b2010-09-22 16:48:34 +00003375class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376 : public std::exception
3377{
3378public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003379 virtual ~bad_weak_ptr() _NOEXCEPT;
3380 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003381};
3382
Marshall Clow8fea1612016-08-25 15:09:01 +00003383_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3384void __throw_bad_weak_ptr()
3385{
3386#ifndef _LIBCPP_NO_EXCEPTIONS
3387 throw bad_weak_ptr();
3388#else
3389 _VSTD::abort();
3390#endif
3391}
3392
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003393template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003395class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396{
3397 __shared_count(const __shared_count&);
3398 __shared_count& operator=(const __shared_count&);
3399
3400protected:
3401 long __shared_owners_;
3402 virtual ~__shared_count();
3403private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003404 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405
3406public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003408 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409 : __shared_owners_(__refs) {}
3410
Eric Fiselier98848572017-01-17 03:16:26 +00003411#if defined(_LIBCPP_BUILDING_MEMORY) && \
3412 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003413 void __add_shared() _NOEXCEPT;
3414 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003415#else
3416 _LIBCPP_INLINE_VISIBILITY
3417 void __add_shared() _NOEXCEPT {
3418 __libcpp_atomic_refcount_increment(__shared_owners_);
3419 }
3420 _LIBCPP_INLINE_VISIBILITY
3421 bool __release_shared() _NOEXCEPT {
3422 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3423 __on_zero_shared();
3424 return true;
3425 }
3426 return false;
3427 }
3428#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003429 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003430 long use_count() const _NOEXCEPT {
3431 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3432 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433};
3434
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003435class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436 : private __shared_count
3437{
3438 long __shared_weak_owners_;
3439
3440public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003442 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443 : __shared_count(__refs),
3444 __shared_weak_owners_(__refs) {}
3445protected:
3446 virtual ~__shared_weak_count();
3447
3448public:
Eric Fiselier98848572017-01-17 03:16:26 +00003449#if defined(_LIBCPP_BUILDING_MEMORY) && \
3450 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003451 void __add_shared() _NOEXCEPT;
3452 void __add_weak() _NOEXCEPT;
3453 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003454#else
3455 _LIBCPP_INLINE_VISIBILITY
3456 void __add_shared() _NOEXCEPT {
3457 __shared_count::__add_shared();
3458 }
3459 _LIBCPP_INLINE_VISIBILITY
3460 void __add_weak() _NOEXCEPT {
3461 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3462 }
3463 _LIBCPP_INLINE_VISIBILITY
3464 void __release_shared() _NOEXCEPT {
3465 if (__shared_count::__release_shared())
3466 __release_weak();
3467 }
3468#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003469 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003471 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3472 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473
Howard Hinnant807d6332013-02-25 15:50:36 +00003474 // Define the function out only if we build static libc++ without RTTI.
3475 // Otherwise we may break clients who need to compile their projects with
3476 // -fno-rtti and yet link against a libc++.dylib compiled
3477 // without -fno-rtti.
3478#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003479 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003480#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003482 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483};
3484
3485template <class _Tp, class _Dp, class _Alloc>
3486class __shared_ptr_pointer
3487 : public __shared_weak_count
3488{
3489 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3490public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003493 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494
Howard Hinnant72f73582010-08-11 17:04:31 +00003495#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003496 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003497#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498
3499private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003500 virtual void __on_zero_shared() _NOEXCEPT;
3501 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502};
3503
Howard Hinnant72f73582010-08-11 17:04:31 +00003504#ifndef _LIBCPP_NO_RTTI
3505
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506template <class _Tp, class _Dp, class _Alloc>
3507const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003508__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003510 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511}
3512
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003513#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003514
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515template <class _Tp, class _Dp, class _Alloc>
3516void
Howard Hinnant719bda32011-05-28 14:41:13 +00003517__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518{
3519 __data_.first().second()(__data_.first().first());
3520 __data_.first().second().~_Dp();
3521}
3522
3523template <class _Tp, class _Dp, class _Alloc>
3524void
Howard Hinnant719bda32011-05-28 14:41:13 +00003525__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003527 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3528 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003529 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3530
Eric Fiselierf8898c82015-02-05 23:01:40 +00003531 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003533 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534}
3535
3536template <class _Tp, class _Alloc>
3537class __shared_ptr_emplace
3538 : public __shared_weak_count
3539{
3540 __compressed_pair<_Alloc, _Tp> __data_;
3541public:
3542#ifndef _LIBCPP_HAS_NO_VARIADICS
3543
Howard Hinnant756c69b2010-09-22 16:48:34 +00003544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003546 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547
3548 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003551 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3552 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553
3554#else // _LIBCPP_HAS_NO_VARIADICS
3555
Howard Hinnant756c69b2010-09-22 16:48:34 +00003556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557 __shared_ptr_emplace(_Alloc __a)
3558 : __data_(__a) {}
3559
3560 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3563 : __data_(__a, _Tp(__a0)) {}
3564
3565 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3568 : __data_(__a, _Tp(__a0, __a1)) {}
3569
3570 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3573 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3574
3575#endif // _LIBCPP_HAS_NO_VARIADICS
3576
3577private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003578 virtual void __on_zero_shared() _NOEXCEPT;
3579 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003582 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583};
3584
3585template <class _Tp, class _Alloc>
3586void
Howard Hinnant719bda32011-05-28 14:41:13 +00003587__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588{
3589 __data_.second().~_Tp();
3590}
3591
3592template <class _Tp, class _Alloc>
3593void
Howard Hinnant719bda32011-05-28 14:41:13 +00003594__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003596 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3597 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003598 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003599 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003601 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602}
3603
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003604template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605
3606template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003607class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003609public:
3610 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003611
Eric Fiselierae5b6672016-06-27 01:02:43 +00003612#if _LIBCPP_STD_VER > 14
3613 typedef weak_ptr<_Tp> weak_type;
3614#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615private:
3616 element_type* __ptr_;
3617 __shared_weak_count* __cntrl_;
3618
3619 struct __nat {int __for_bool_;};
3620public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003622 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003624 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003625 template<class _Yp>
3626 explicit shared_ptr(_Yp* __p,
3627 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3628 template<class _Yp, class _Dp>
3629 shared_ptr(_Yp* __p, _Dp __d,
3630 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3631 template<class _Yp, class _Dp, class _Alloc>
3632 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3633 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3635 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003636 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003638 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003642 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003643 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003644#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003646 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003647 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003648 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003649 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003650#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003652 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003653#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003654#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003655 template<class _Yp>
3656 shared_ptr(auto_ptr<_Yp>&& __r,
3657 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003658#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003659 template<class _Yp>
3660 shared_ptr(auto_ptr<_Yp> __r,
3661 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003663#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003664#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003665 template <class _Yp, class _Dp>
3666 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3667 typename enable_if
3668 <
3669 !is_lvalue_reference<_Dp>::value &&
3670 !is_array<_Yp>::value &&
3671 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3672 __nat
3673 >::type = __nat());
3674 template <class _Yp, class _Dp>
3675 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3676 typename enable_if
3677 <
3678 is_lvalue_reference<_Dp>::value &&
3679 !is_array<_Yp>::value &&
3680 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3681 __nat
3682 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003683#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003684 template <class _Yp, class _Dp>
3685 shared_ptr(unique_ptr<_Yp, _Dp>,
3686 typename enable_if
3687 <
3688 !is_lvalue_reference<_Dp>::value &&
3689 !is_array<_Yp>::value &&
3690 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3691 __nat
3692 >::type = __nat());
3693 template <class _Yp, class _Dp>
3694 shared_ptr(unique_ptr<_Yp, _Dp>,
3695 typename enable_if
3696 <
3697 is_lvalue_reference<_Dp>::value &&
3698 !is_array<_Yp>::value &&
3699 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3700 __nat
3701 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703
3704 ~shared_ptr();
3705
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003707 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003708 template<class _Yp>
3709 typename enable_if
3710 <
3711 is_convertible<_Yp*, element_type*>::value,
3712 shared_ptr&
3713 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003715 operator=(const shared_ptr<_Yp>& __r) _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& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003719 template<class _Yp>
3720 typename enable_if
3721 <
3722 is_convertible<_Yp*, element_type*>::value,
3723 shared_ptr<_Tp>&
3724 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003726 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003727#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003728 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003730 typename enable_if
3731 <
3732 !is_array<_Yp>::value &&
3733 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003734 shared_ptr
3735 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003736 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003737#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003738#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003739#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003740 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003742 typename enable_if
3743 <
3744 !is_array<_Yp>::value &&
3745 is_convertible<_Yp*, element_type*>::value,
3746 shared_ptr&
3747 >::type
3748 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003750#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003751 template <class _Yp, class _Dp>
3752 typename enable_if
3753 <
3754 !is_array<_Yp>::value &&
3755 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3756 shared_ptr&
3757 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003760 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003761#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003763 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764#endif
3765
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003767 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003769 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003770 template<class _Yp>
3771 typename enable_if
3772 <
3773 is_convertible<_Yp*, element_type*>::value,
3774 void
3775 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003777 reset(_Yp* __p);
3778 template<class _Yp, class _Dp>
3779 typename enable_if
3780 <
3781 is_convertible<_Yp*, element_type*>::value,
3782 void
3783 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003785 reset(_Yp* __p, _Dp __d);
3786 template<class _Yp, class _Dp, class _Alloc>
3787 typename enable_if
3788 <
3789 is_convertible<_Yp*, element_type*>::value,
3790 void
3791 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003793 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794
Howard Hinnant756c69b2010-09-22 16:48:34 +00003795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003796 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003798 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3799 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003801 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003803 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003805 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003807 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003808 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003810 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003812 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003814 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003816 _LIBCPP_INLINE_VISIBILITY
3817 bool
3818 __owner_equivalent(const shared_ptr& __p) const
3819 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820
Howard Hinnant72f73582010-08-11 17:04:31 +00003821#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003824 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003826#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827
3828#ifndef _LIBCPP_HAS_NO_VARIADICS
3829
3830 template<class ..._Args>
3831 static
3832 shared_ptr<_Tp>
3833 make_shared(_Args&& ...__args);
3834
3835 template<class _Alloc, class ..._Args>
3836 static
3837 shared_ptr<_Tp>
3838 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3839
3840#else // _LIBCPP_HAS_NO_VARIADICS
3841
3842 static shared_ptr<_Tp> make_shared();
3843
3844 template<class _A0>
3845 static shared_ptr<_Tp> make_shared(_A0&);
3846
3847 template<class _A0, class _A1>
3848 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3849
3850 template<class _A0, class _A1, class _A2>
3851 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3852
3853 template<class _Alloc>
3854 static shared_ptr<_Tp>
3855 allocate_shared(const _Alloc& __a);
3856
3857 template<class _Alloc, class _A0>
3858 static shared_ptr<_Tp>
3859 allocate_shared(const _Alloc& __a, _A0& __a0);
3860
3861 template<class _Alloc, class _A0, class _A1>
3862 static shared_ptr<_Tp>
3863 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3864
3865 template<class _Alloc, class _A0, class _A1, class _A2>
3866 static shared_ptr<_Tp>
3867 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3868
3869#endif // _LIBCPP_HAS_NO_VARIADICS
3870
3871private:
3872
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003873 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003876 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3877 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003879 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003880 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003881 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003882 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3883 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003884 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885 }
3886
Howard Hinnant756c69b2010-09-22 16:48:34 +00003887 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003888 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003890 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3891 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892};
3893
3894template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003895inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003896_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003897shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898 : __ptr_(0),
3899 __cntrl_(0)
3900{
3901}
3902
3903template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003904inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003905_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003906shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907 : __ptr_(0),
3908 __cntrl_(0)
3909{
3910}
3911
3912template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003913template<class _Yp>
3914shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3915 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916 : __ptr_(__p)
3917{
3918 unique_ptr<_Yp> __hold(__p);
3919 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3920 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3921 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003922 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003923}
3924
3925template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003926template<class _Yp, class _Dp>
3927shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3928 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929 : __ptr_(__p)
3930{
3931#ifndef _LIBCPP_NO_EXCEPTIONS
3932 try
3933 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003934#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3936 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003937 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003938#ifndef _LIBCPP_NO_EXCEPTIONS
3939 }
3940 catch (...)
3941 {
3942 __d(__p);
3943 throw;
3944 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003945#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003946}
3947
3948template<class _Tp>
3949template<class _Dp>
3950shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3951 : __ptr_(0)
3952{
3953#ifndef _LIBCPP_NO_EXCEPTIONS
3954 try
3955 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003956#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3958 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3959#ifndef _LIBCPP_NO_EXCEPTIONS
3960 }
3961 catch (...)
3962 {
3963 __d(__p);
3964 throw;
3965 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003966#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967}
3968
3969template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003970template<class _Yp, class _Dp, class _Alloc>
3971shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3972 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973 : __ptr_(__p)
3974{
3975#ifndef _LIBCPP_NO_EXCEPTIONS
3976 try
3977 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003978#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003980 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981 typedef __allocator_destructor<_A2> _D2;
3982 _A2 __a2(__a);
3983 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003984 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3985 _CntrlBlk(__p, __d, __a);
3986 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003987 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988#ifndef _LIBCPP_NO_EXCEPTIONS
3989 }
3990 catch (...)
3991 {
3992 __d(__p);
3993 throw;
3994 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003995#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996}
3997
3998template<class _Tp>
3999template<class _Dp, class _Alloc>
4000shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4001 : __ptr_(0)
4002{
4003#ifndef _LIBCPP_NO_EXCEPTIONS
4004 try
4005 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004006#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004008 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009 typedef __allocator_destructor<_A2> _D2;
4010 _A2 __a2(__a);
4011 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004012 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4013 _CntrlBlk(__p, __d, __a);
4014 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015#ifndef _LIBCPP_NO_EXCEPTIONS
4016 }
4017 catch (...)
4018 {
4019 __d(__p);
4020 throw;
4021 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004022#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023}
4024
4025template<class _Tp>
4026template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004027inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004028shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029 : __ptr_(__p),
4030 __cntrl_(__r.__cntrl_)
4031{
4032 if (__cntrl_)
4033 __cntrl_->__add_shared();
4034}
4035
4036template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004037inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004038shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039 : __ptr_(__r.__ptr_),
4040 __cntrl_(__r.__cntrl_)
4041{
4042 if (__cntrl_)
4043 __cntrl_->__add_shared();
4044}
4045
4046template<class _Tp>
4047template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004048inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004050 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004051 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052 : __ptr_(__r.__ptr_),
4053 __cntrl_(__r.__cntrl_)
4054{
4055 if (__cntrl_)
4056 __cntrl_->__add_shared();
4057}
4058
Howard Hinnant74279a52010-09-04 23:28:19 +00004059#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060
4061template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004062inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004063shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064 : __ptr_(__r.__ptr_),
4065 __cntrl_(__r.__cntrl_)
4066{
4067 __r.__ptr_ = 0;
4068 __r.__cntrl_ = 0;
4069}
4070
4071template<class _Tp>
4072template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004073inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004075 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004076 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077 : __ptr_(__r.__ptr_),
4078 __cntrl_(__r.__cntrl_)
4079{
4080 __r.__ptr_ = 0;
4081 __r.__cntrl_ = 0;
4082}
4083
Howard Hinnant74279a52010-09-04 23:28:19 +00004084#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085
Marshall Clowb22274f2017-01-24 22:22:33 +00004086#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004088template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004089#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004090shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004092shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004094 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095 : __ptr_(__r.get())
4096{
4097 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4098 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004099 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100 __r.release();
4101}
Marshall Clowb22274f2017-01-24 22:22:33 +00004102#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103
4104template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004105template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004106#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4108#else
4109shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4110#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004111 typename enable_if
4112 <
4113 !is_lvalue_reference<_Dp>::value &&
4114 !is_array<_Yp>::value &&
4115 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4116 __nat
4117 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118 : __ptr_(__r.get())
4119{
Marshall Clow35cde742015-05-10 13:59:45 +00004120#if _LIBCPP_STD_VER > 11
4121 if (__ptr_ == nullptr)
4122 __cntrl_ = nullptr;
4123 else
4124#endif
4125 {
4126 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4127 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004128 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004129 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130 __r.release();
4131}
4132
4133template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004134template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004135#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4137#else
4138shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4139#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004140 typename enable_if
4141 <
4142 is_lvalue_reference<_Dp>::value &&
4143 !is_array<_Yp>::value &&
4144 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4145 __nat
4146 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147 : __ptr_(__r.get())
4148{
Marshall Clow35cde742015-05-10 13:59:45 +00004149#if _LIBCPP_STD_VER > 11
4150 if (__ptr_ == nullptr)
4151 __cntrl_ = nullptr;
4152 else
4153#endif
4154 {
4155 typedef __shared_ptr_pointer<_Yp*,
4156 reference_wrapper<typename remove_reference<_Dp>::type>,
4157 allocator<_Yp> > _CntrlBlk;
4158 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004159 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004160 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161 __r.release();
4162}
4163
4164#ifndef _LIBCPP_HAS_NO_VARIADICS
4165
4166template<class _Tp>
4167template<class ..._Args>
4168shared_ptr<_Tp>
4169shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4170{
4171 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4172 typedef allocator<_CntrlBlk> _A2;
4173 typedef __allocator_destructor<_A2> _D2;
4174 _A2 __a2;
4175 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004176 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177 shared_ptr<_Tp> __r;
4178 __r.__ptr_ = __hold2.get()->get();
4179 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004180 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181 return __r;
4182}
4183
4184template<class _Tp>
4185template<class _Alloc, class ..._Args>
4186shared_ptr<_Tp>
4187shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4188{
4189 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004190 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191 typedef __allocator_destructor<_A2> _D2;
4192 _A2 __a2(__a);
4193 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004194 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4195 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196 shared_ptr<_Tp> __r;
4197 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004198 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004199 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200 return __r;
4201}
4202
4203#else // _LIBCPP_HAS_NO_VARIADICS
4204
4205template<class _Tp>
4206shared_ptr<_Tp>
4207shared_ptr<_Tp>::make_shared()
4208{
4209 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4210 typedef allocator<_CntrlBlk> _Alloc2;
4211 typedef __allocator_destructor<_Alloc2> _D2;
4212 _Alloc2 __alloc2;
4213 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4214 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4215 shared_ptr<_Tp> __r;
4216 __r.__ptr_ = __hold2.get()->get();
4217 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004218 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219 return __r;
4220}
4221
4222template<class _Tp>
4223template<class _A0>
4224shared_ptr<_Tp>
4225shared_ptr<_Tp>::make_shared(_A0& __a0)
4226{
4227 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4228 typedef allocator<_CntrlBlk> _Alloc2;
4229 typedef __allocator_destructor<_Alloc2> _D2;
4230 _Alloc2 __alloc2;
4231 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4232 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4233 shared_ptr<_Tp> __r;
4234 __r.__ptr_ = __hold2.get()->get();
4235 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004236 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237 return __r;
4238}
4239
4240template<class _Tp>
4241template<class _A0, class _A1>
4242shared_ptr<_Tp>
4243shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4244{
4245 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4246 typedef allocator<_CntrlBlk> _Alloc2;
4247 typedef __allocator_destructor<_Alloc2> _D2;
4248 _Alloc2 __alloc2;
4249 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4250 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4251 shared_ptr<_Tp> __r;
4252 __r.__ptr_ = __hold2.get()->get();
4253 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004254 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255 return __r;
4256}
4257
4258template<class _Tp>
4259template<class _A0, class _A1, class _A2>
4260shared_ptr<_Tp>
4261shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4262{
4263 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4264 typedef allocator<_CntrlBlk> _Alloc2;
4265 typedef __allocator_destructor<_Alloc2> _D2;
4266 _Alloc2 __alloc2;
4267 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4268 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4269 shared_ptr<_Tp> __r;
4270 __r.__ptr_ = __hold2.get()->get();
4271 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004272 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273 return __r;
4274}
4275
4276template<class _Tp>
4277template<class _Alloc>
4278shared_ptr<_Tp>
4279shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4280{
4281 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004282 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283 typedef __allocator_destructor<_Alloc2> _D2;
4284 _Alloc2 __alloc2(__a);
4285 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004286 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4287 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288 shared_ptr<_Tp> __r;
4289 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004290 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004291 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292 return __r;
4293}
4294
4295template<class _Tp>
4296template<class _Alloc, class _A0>
4297shared_ptr<_Tp>
4298shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4299{
4300 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004301 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302 typedef __allocator_destructor<_Alloc2> _D2;
4303 _Alloc2 __alloc2(__a);
4304 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004305 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4306 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307 shared_ptr<_Tp> __r;
4308 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004309 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004310 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311 return __r;
4312}
4313
4314template<class _Tp>
4315template<class _Alloc, class _A0, class _A1>
4316shared_ptr<_Tp>
4317shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4318{
4319 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004320 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321 typedef __allocator_destructor<_Alloc2> _D2;
4322 _Alloc2 __alloc2(__a);
4323 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004324 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4325 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326 shared_ptr<_Tp> __r;
4327 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004328 __r.__cntrl_ = _VSTD::addressof(*__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 _Alloc, class _A0, class _A1, class _A2>
4335shared_ptr<_Tp>
4336shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4337{
4338 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004339 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340 typedef __allocator_destructor<_Alloc2> _D2;
4341 _Alloc2 __alloc2(__a);
4342 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004343 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4344 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345 shared_ptr<_Tp> __r;
4346 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004347 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004348 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349 return __r;
4350}
4351
4352#endif // _LIBCPP_HAS_NO_VARIADICS
4353
4354template<class _Tp>
4355shared_ptr<_Tp>::~shared_ptr()
4356{
4357 if (__cntrl_)
4358 __cntrl_->__release_shared();
4359}
4360
4361template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004362inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004364shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365{
4366 shared_ptr(__r).swap(*this);
4367 return *this;
4368}
4369
4370template<class _Tp>
4371template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004372inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004373typename enable_if
4374<
Marshall Clow7e384b72017-01-10 16:59:33 +00004375 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004376 shared_ptr<_Tp>&
4377>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004378shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379{
4380 shared_ptr(__r).swap(*this);
4381 return *this;
4382}
4383
Howard Hinnant74279a52010-09-04 23:28:19 +00004384#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385
4386template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004387inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004389shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004391 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392 return *this;
4393}
4394
4395template<class _Tp>
4396template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004397inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004398typename enable_if
4399<
Marshall Clow7e384b72017-01-10 16:59:33 +00004400 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004401 shared_ptr<_Tp>&
4402>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4404{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004405 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004406 return *this;
4407}
4408
Marshall Clowb22274f2017-01-24 22:22:33 +00004409#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004410template<class _Tp>
4411template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004412inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004413typename enable_if
4414<
4415 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004416 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004417 shared_ptr<_Tp>
4418>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004419shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4420{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004421 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422 return *this;
4423}
Marshall Clowb22274f2017-01-24 22:22:33 +00004424#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425
4426template<class _Tp>
4427template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004428inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004429typename enable_if
4430<
4431 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004432 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4433 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004434 shared_ptr<_Tp>&
4435>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4437{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004438 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004439 return *this;
4440}
4441
Howard Hinnant74279a52010-09-04 23:28:19 +00004442#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443
Marshall Clowb22274f2017-01-24 22:22:33 +00004444#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445template<class _Tp>
4446template<class _Yp>
4447inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004448typename enable_if
4449<
4450 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004451 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004452 shared_ptr<_Tp>&
4453>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004454shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004455{
4456 shared_ptr(__r).swap(*this);
4457 return *this;
4458}
Marshall Clowb22274f2017-01-24 22:22:33 +00004459#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004460
4461template<class _Tp>
4462template <class _Yp, class _Dp>
4463inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004464typename enable_if
4465<
4466 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004467 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4468 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004469 shared_ptr<_Tp>&
4470>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004471shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4472{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004473 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474 return *this;
4475}
4476
Howard Hinnant74279a52010-09-04 23:28:19 +00004477#endif // _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 +00004481void
Howard Hinnant719bda32011-05-28 14:41:13 +00004482shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004483{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004484 _VSTD::swap(__ptr_, __r.__ptr_);
4485 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486}
4487
4488template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004489inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490void
Howard Hinnant719bda32011-05-28 14:41:13 +00004491shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004492{
4493 shared_ptr().swap(*this);
4494}
4495
4496template<class _Tp>
4497template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004498inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004499typename enable_if
4500<
Marshall Clow7e384b72017-01-10 16:59:33 +00004501 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004502 void
4503>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004504shared_ptr<_Tp>::reset(_Yp* __p)
4505{
4506 shared_ptr(__p).swap(*this);
4507}
4508
4509template<class _Tp>
4510template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004511inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004512typename enable_if
4513<
Marshall Clow7e384b72017-01-10 16:59:33 +00004514 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004515 void
4516>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004517shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4518{
4519 shared_ptr(__p, __d).swap(*this);
4520}
4521
4522template<class _Tp>
4523template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004524inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004525typename enable_if
4526<
Marshall Clow7e384b72017-01-10 16:59:33 +00004527 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004528 void
4529>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4531{
4532 shared_ptr(__p, __d, __a).swap(*this);
4533}
4534
4535#ifndef _LIBCPP_HAS_NO_VARIADICS
4536
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004537template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004539typename enable_if
4540<
4541 !is_array<_Tp>::value,
4542 shared_ptr<_Tp>
4543>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004544make_shared(_Args&& ...__args)
4545{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004546 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004547}
4548
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004549template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004550inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004551typename enable_if
4552<
4553 !is_array<_Tp>::value,
4554 shared_ptr<_Tp>
4555>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556allocate_shared(const _Alloc& __a, _Args&& ...__args)
4557{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004558 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004559}
4560
4561#else // _LIBCPP_HAS_NO_VARIADICS
4562
4563template<class _Tp>
4564inline _LIBCPP_INLINE_VISIBILITY
4565shared_ptr<_Tp>
4566make_shared()
4567{
4568 return shared_ptr<_Tp>::make_shared();
4569}
4570
4571template<class _Tp, class _A0>
4572inline _LIBCPP_INLINE_VISIBILITY
4573shared_ptr<_Tp>
4574make_shared(_A0& __a0)
4575{
4576 return shared_ptr<_Tp>::make_shared(__a0);
4577}
4578
4579template<class _Tp, class _A0, class _A1>
4580inline _LIBCPP_INLINE_VISIBILITY
4581shared_ptr<_Tp>
4582make_shared(_A0& __a0, _A1& __a1)
4583{
4584 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4585}
4586
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004587template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004588inline _LIBCPP_INLINE_VISIBILITY
4589shared_ptr<_Tp>
4590make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4591{
4592 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4593}
4594
4595template<class _Tp, class _Alloc>
4596inline _LIBCPP_INLINE_VISIBILITY
4597shared_ptr<_Tp>
4598allocate_shared(const _Alloc& __a)
4599{
4600 return shared_ptr<_Tp>::allocate_shared(__a);
4601}
4602
4603template<class _Tp, class _Alloc, class _A0>
4604inline _LIBCPP_INLINE_VISIBILITY
4605shared_ptr<_Tp>
4606allocate_shared(const _Alloc& __a, _A0& __a0)
4607{
4608 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4609}
4610
4611template<class _Tp, class _Alloc, class _A0, class _A1>
4612inline _LIBCPP_INLINE_VISIBILITY
4613shared_ptr<_Tp>
4614allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4615{
4616 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4617}
4618
4619template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4620inline _LIBCPP_INLINE_VISIBILITY
4621shared_ptr<_Tp>
4622allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4623{
4624 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4625}
4626
4627#endif // _LIBCPP_HAS_NO_VARIADICS
4628
4629template<class _Tp, class _Up>
4630inline _LIBCPP_INLINE_VISIBILITY
4631bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004632operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004633{
4634 return __x.get() == __y.get();
4635}
4636
4637template<class _Tp, class _Up>
4638inline _LIBCPP_INLINE_VISIBILITY
4639bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004640operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641{
4642 return !(__x == __y);
4643}
4644
4645template<class _Tp, class _Up>
4646inline _LIBCPP_INLINE_VISIBILITY
4647bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004648operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004649{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004650 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4651 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004652}
4653
4654template<class _Tp, class _Up>
4655inline _LIBCPP_INLINE_VISIBILITY
4656bool
4657operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4658{
4659 return __y < __x;
4660}
4661
4662template<class _Tp, class _Up>
4663inline _LIBCPP_INLINE_VISIBILITY
4664bool
4665operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4666{
4667 return !(__y < __x);
4668}
4669
4670template<class _Tp, class _Up>
4671inline _LIBCPP_INLINE_VISIBILITY
4672bool
4673operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4674{
4675 return !(__x < __y);
4676}
4677
4678template<class _Tp>
4679inline _LIBCPP_INLINE_VISIBILITY
4680bool
4681operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4682{
4683 return !__x;
4684}
4685
4686template<class _Tp>
4687inline _LIBCPP_INLINE_VISIBILITY
4688bool
4689operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4690{
4691 return !__x;
4692}
4693
4694template<class _Tp>
4695inline _LIBCPP_INLINE_VISIBILITY
4696bool
4697operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4698{
4699 return static_cast<bool>(__x);
4700}
4701
4702template<class _Tp>
4703inline _LIBCPP_INLINE_VISIBILITY
4704bool
4705operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4706{
4707 return static_cast<bool>(__x);
4708}
4709
4710template<class _Tp>
4711inline _LIBCPP_INLINE_VISIBILITY
4712bool
4713operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4714{
4715 return less<_Tp*>()(__x.get(), nullptr);
4716}
4717
4718template<class _Tp>
4719inline _LIBCPP_INLINE_VISIBILITY
4720bool
4721operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4722{
4723 return less<_Tp*>()(nullptr, __x.get());
4724}
4725
4726template<class _Tp>
4727inline _LIBCPP_INLINE_VISIBILITY
4728bool
4729operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4730{
4731 return nullptr < __x;
4732}
4733
4734template<class _Tp>
4735inline _LIBCPP_INLINE_VISIBILITY
4736bool
4737operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4738{
4739 return __x < nullptr;
4740}
4741
4742template<class _Tp>
4743inline _LIBCPP_INLINE_VISIBILITY
4744bool
4745operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4746{
4747 return !(nullptr < __x);
4748}
4749
4750template<class _Tp>
4751inline _LIBCPP_INLINE_VISIBILITY
4752bool
4753operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4754{
4755 return !(__x < nullptr);
4756}
4757
4758template<class _Tp>
4759inline _LIBCPP_INLINE_VISIBILITY
4760bool
4761operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4762{
4763 return !(__x < nullptr);
4764}
4765
4766template<class _Tp>
4767inline _LIBCPP_INLINE_VISIBILITY
4768bool
4769operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4770{
4771 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004772}
4773
4774template<class _Tp>
4775inline _LIBCPP_INLINE_VISIBILITY
4776void
Howard Hinnant719bda32011-05-28 14:41:13 +00004777swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004778{
4779 __x.swap(__y);
4780}
4781
4782template<class _Tp, class _Up>
4783inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004784typename enable_if
4785<
4786 !is_array<_Tp>::value && !is_array<_Up>::value,
4787 shared_ptr<_Tp>
4788>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004789static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004790{
4791 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4792}
4793
4794template<class _Tp, class _Up>
4795inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004796typename enable_if
4797<
4798 !is_array<_Tp>::value && !is_array<_Up>::value,
4799 shared_ptr<_Tp>
4800>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004801dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004802{
4803 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4804 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4805}
4806
4807template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004808typename enable_if
4809<
4810 is_array<_Tp>::value == is_array<_Up>::value,
4811 shared_ptr<_Tp>
4812>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004813const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004814{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004815 typedef typename remove_extent<_Tp>::type _RTp;
4816 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004817}
4818
Howard Hinnant72f73582010-08-11 17:04:31 +00004819#ifndef _LIBCPP_NO_RTTI
4820
Howard Hinnantc51e1022010-05-11 19:42:16 +00004821template<class _Dp, class _Tp>
4822inline _LIBCPP_INLINE_VISIBILITY
4823_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004824get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004825{
4826 return __p.template __get_deleter<_Dp>();
4827}
4828
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004829#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004830
Howard Hinnantc51e1022010-05-11 19:42:16 +00004831template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004832class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004833{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004834public:
4835 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004836private:
4837 element_type* __ptr_;
4838 __shared_weak_count* __cntrl_;
4839
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004840public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004842 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004843 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004844 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4845 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004847 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004848 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004849 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4850 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004851
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004852#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004854 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004855 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004856 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4857 _NOEXCEPT;
4858#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004859 ~weak_ptr();
4860
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004862 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004863 template<class _Yp>
4864 typename enable_if
4865 <
4866 is_convertible<_Yp*, element_type*>::value,
4867 weak_ptr&
4868 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004870 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4871
4872#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4873
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004875 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4876 template<class _Yp>
4877 typename enable_if
4878 <
4879 is_convertible<_Yp*, element_type*>::value,
4880 weak_ptr&
4881 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004883 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4884
4885#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4886
4887 template<class _Yp>
4888 typename enable_if
4889 <
4890 is_convertible<_Yp*, element_type*>::value,
4891 weak_ptr&
4892 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004894 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004895
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004897 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004899 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004900
Howard Hinnant756c69b2010-09-22 16:48:34 +00004901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004902 long use_count() const _NOEXCEPT
4903 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004905 bool expired() const _NOEXCEPT
4906 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4907 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004908 template<class _Up>
4909 _LIBCPP_INLINE_VISIBILITY
4910 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004911 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004912 template<class _Up>
4913 _LIBCPP_INLINE_VISIBILITY
4914 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004915 {return __cntrl_ < __r.__cntrl_;}
4916
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004917 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4918 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004919};
4920
4921template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004922inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004923_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004924weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004925 : __ptr_(0),
4926 __cntrl_(0)
4927{
4928}
4929
4930template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004931inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004932weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004933 : __ptr_(__r.__ptr_),
4934 __cntrl_(__r.__cntrl_)
4935{
4936 if (__cntrl_)
4937 __cntrl_->__add_weak();
4938}
4939
4940template<class _Tp>
4941template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004942inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004943weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004944 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004945 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004946 : __ptr_(__r.__ptr_),
4947 __cntrl_(__r.__cntrl_)
4948{
4949 if (__cntrl_)
4950 __cntrl_->__add_weak();
4951}
4952
4953template<class _Tp>
4954template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004955inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004956weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004957 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004958 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004959 : __ptr_(__r.__ptr_),
4960 __cntrl_(__r.__cntrl_)
4961{
4962 if (__cntrl_)
4963 __cntrl_->__add_weak();
4964}
4965
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4967
4968template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004969inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004970weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4971 : __ptr_(__r.__ptr_),
4972 __cntrl_(__r.__cntrl_)
4973{
4974 __r.__ptr_ = 0;
4975 __r.__cntrl_ = 0;
4976}
4977
4978template<class _Tp>
4979template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004980inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004981weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4982 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4983 _NOEXCEPT
4984 : __ptr_(__r.__ptr_),
4985 __cntrl_(__r.__cntrl_)
4986{
4987 __r.__ptr_ = 0;
4988 __r.__cntrl_ = 0;
4989}
4990
4991#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4992
Howard Hinnantc51e1022010-05-11 19:42:16 +00004993template<class _Tp>
4994weak_ptr<_Tp>::~weak_ptr()
4995{
4996 if (__cntrl_)
4997 __cntrl_->__release_weak();
4998}
4999
5000template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005001inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005002weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005003weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005004{
5005 weak_ptr(__r).swap(*this);
5006 return *this;
5007}
5008
5009template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005010template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005011inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005012typename enable_if
5013<
5014 is_convertible<_Yp*, _Tp*>::value,
5015 weak_ptr<_Tp>&
5016>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005017weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005018{
5019 weak_ptr(__r).swap(*this);
5020 return *this;
5021}
5022
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005023#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5024
5025template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005026inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005027weak_ptr<_Tp>&
5028weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5029{
5030 weak_ptr(_VSTD::move(__r)).swap(*this);
5031 return *this;
5032}
5033
Howard Hinnantc51e1022010-05-11 19:42:16 +00005034template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005035template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005036inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005037typename enable_if
5038<
5039 is_convertible<_Yp*, _Tp*>::value,
5040 weak_ptr<_Tp>&
5041>::type
5042weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5043{
5044 weak_ptr(_VSTD::move(__r)).swap(*this);
5045 return *this;
5046}
5047
5048#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5049
5050template<class _Tp>
5051template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005052inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005053typename enable_if
5054<
5055 is_convertible<_Yp*, _Tp*>::value,
5056 weak_ptr<_Tp>&
5057>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005058weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005059{
5060 weak_ptr(__r).swap(*this);
5061 return *this;
5062}
5063
5064template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005065inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005066void
Howard Hinnant719bda32011-05-28 14:41:13 +00005067weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005068{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005069 _VSTD::swap(__ptr_, __r.__ptr_);
5070 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005071}
5072
5073template<class _Tp>
5074inline _LIBCPP_INLINE_VISIBILITY
5075void
Howard Hinnant719bda32011-05-28 14:41:13 +00005076swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005077{
5078 __x.swap(__y);
5079}
5080
5081template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005082inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005083void
Howard Hinnant719bda32011-05-28 14:41:13 +00005084weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005085{
5086 weak_ptr().swap(*this);
5087}
5088
5089template<class _Tp>
5090template<class _Yp>
5091shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005092 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005093 : __ptr_(__r.__ptr_),
5094 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5095{
5096 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005097 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005098}
5099
5100template<class _Tp>
5101shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005102weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005103{
5104 shared_ptr<_Tp> __r;
5105 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5106 if (__r.__cntrl_)
5107 __r.__ptr_ = __ptr_;
5108 return __r;
5109}
5110
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005111#if _LIBCPP_STD_VER > 14
5112template <class _Tp = void> struct owner_less;
5113#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005114template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005115#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005116
5117template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005118struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005119 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005120{
5121 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005123 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5124 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005126 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5127 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005129 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5130 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005131};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005132
5133template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005134struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005135 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5136{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005137 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005139 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5140 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005142 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5143 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005145 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5146 {return __x.owner_before(__y);}
5147};
5148
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005149#if _LIBCPP_STD_VER > 14
5150template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005151struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005152{
5153 template <class _Tp, class _Up>
5154 _LIBCPP_INLINE_VISIBILITY
5155 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5156 {return __x.owner_before(__y);}
5157 template <class _Tp, class _Up>
5158 _LIBCPP_INLINE_VISIBILITY
5159 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5160 {return __x.owner_before(__y);}
5161 template <class _Tp, class _Up>
5162 _LIBCPP_INLINE_VISIBILITY
5163 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5164 {return __x.owner_before(__y);}
5165 template <class _Tp, class _Up>
5166 _LIBCPP_INLINE_VISIBILITY
5167 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5168 {return __x.owner_before(__y);}
5169 typedef void is_transparent;
5170};
5171#endif
5172
Howard Hinnantc51e1022010-05-11 19:42:16 +00005173template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005174class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005175{
5176 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005177protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005178 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005179 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005181 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005183 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5184 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005186 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005187public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005189 shared_ptr<_Tp> shared_from_this()
5190 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005192 shared_ptr<_Tp const> shared_from_this() const
5193 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005194
Eric Fiselier84006862016-06-02 00:15:35 +00005195#if _LIBCPP_STD_VER > 14
5196 _LIBCPP_INLINE_VISIBILITY
5197 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5198 { return __weak_this_; }
5199
5200 _LIBCPP_INLINE_VISIBILITY
5201 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5202 { return __weak_this_; }
5203#endif // _LIBCPP_STD_VER > 14
5204
Howard Hinnantc51e1022010-05-11 19:42:16 +00005205 template <class _Up> friend class shared_ptr;
5206};
5207
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005208template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005209struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005210{
5211 typedef shared_ptr<_Tp> argument_type;
5212 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005213
Howard Hinnant756c69b2010-09-22 16:48:34 +00005214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005215 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005216 {
5217 return hash<_Tp*>()(__ptr.get());
5218 }
5219};
5220
Howard Hinnantc834c512011-11-29 18:15:50 +00005221template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005222inline _LIBCPP_INLINE_VISIBILITY
5223basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005224operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005225
Eric Fiselier9b492672016-06-18 02:12:53 +00005226
5227#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005228
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005229class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005230{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005231 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005232public:
5233 void lock() _NOEXCEPT;
5234 void unlock() _NOEXCEPT;
5235
5236private:
5237 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5238 __sp_mut(const __sp_mut&);
5239 __sp_mut& operator=(const __sp_mut&);
5240
Howard Hinnant8331b762013-03-06 23:30:19 +00005241 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005242};
5243
Howard Hinnant8331b762013-03-06 23:30:19 +00005244_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005245
5246template <class _Tp>
5247inline _LIBCPP_INLINE_VISIBILITY
5248bool
5249atomic_is_lock_free(const shared_ptr<_Tp>*)
5250{
5251 return false;
5252}
5253
5254template <class _Tp>
5255shared_ptr<_Tp>
5256atomic_load(const shared_ptr<_Tp>* __p)
5257{
5258 __sp_mut& __m = __get_sp_mut(__p);
5259 __m.lock();
5260 shared_ptr<_Tp> __q = *__p;
5261 __m.unlock();
5262 return __q;
5263}
5264
5265template <class _Tp>
5266inline _LIBCPP_INLINE_VISIBILITY
5267shared_ptr<_Tp>
5268atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5269{
5270 return atomic_load(__p);
5271}
5272
5273template <class _Tp>
5274void
5275atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5276{
5277 __sp_mut& __m = __get_sp_mut(__p);
5278 __m.lock();
5279 __p->swap(__r);
5280 __m.unlock();
5281}
5282
5283template <class _Tp>
5284inline _LIBCPP_INLINE_VISIBILITY
5285void
5286atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5287{
5288 atomic_store(__p, __r);
5289}
5290
5291template <class _Tp>
5292shared_ptr<_Tp>
5293atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5294{
5295 __sp_mut& __m = __get_sp_mut(__p);
5296 __m.lock();
5297 __p->swap(__r);
5298 __m.unlock();
5299 return __r;
5300}
5301
5302template <class _Tp>
5303inline _LIBCPP_INLINE_VISIBILITY
5304shared_ptr<_Tp>
5305atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5306{
5307 return atomic_exchange(__p, __r);
5308}
5309
5310template <class _Tp>
5311bool
5312atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5313{
Marshall Clow4201ee82016-05-18 17:50:13 +00005314 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005315 __sp_mut& __m = __get_sp_mut(__p);
5316 __m.lock();
5317 if (__p->__owner_equivalent(*__v))
5318 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005319 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005320 *__p = __w;
5321 __m.unlock();
5322 return true;
5323 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005324 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005325 *__v = *__p;
5326 __m.unlock();
5327 return false;
5328}
5329
5330template <class _Tp>
5331inline _LIBCPP_INLINE_VISIBILITY
5332bool
5333atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5334{
5335 return atomic_compare_exchange_strong(__p, __v, __w);
5336}
5337
5338template <class _Tp>
5339inline _LIBCPP_INLINE_VISIBILITY
5340bool
5341atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5342 shared_ptr<_Tp> __w, memory_order, memory_order)
5343{
5344 return atomic_compare_exchange_strong(__p, __v, __w);
5345}
5346
5347template <class _Tp>
5348inline _LIBCPP_INLINE_VISIBILITY
5349bool
5350atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5351 shared_ptr<_Tp> __w, memory_order, memory_order)
5352{
5353 return atomic_compare_exchange_weak(__p, __v, __w);
5354}
5355
Eric Fiselier9b492672016-06-18 02:12:53 +00005356#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005357
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005358//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005359#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5360# ifndef _LIBCPP_CXX03_LANG
5361enum class pointer_safety : unsigned char {
5362 relaxed,
5363 preferred,
5364 strict
5365};
5366# endif
5367#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005368struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005369{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005370 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005371 {
5372 relaxed,
5373 preferred,
5374 strict
5375 };
5376
Howard Hinnant49e145e2012-10-30 19:06:59 +00005377 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005378
Howard Hinnant756c69b2010-09-22 16:48:34 +00005379 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005380 pointer_safety() : __v_() {}
5381
5382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005383 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005385 operator int() const {return __v_;}
5386};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005387#endif
5388
5389#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5390 defined(_LIBCPP_BUILDING_MEMORY)
5391_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5392#else
5393// This function is only offered in C++03 under ABI v1.
5394# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5395inline _LIBCPP_INLINE_VISIBILITY
5396pointer_safety get_pointer_safety() _NOEXCEPT {
5397 return pointer_safety::relaxed;
5398}
5399# endif
5400#endif
5401
Howard Hinnantc51e1022010-05-11 19:42:16 +00005402
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005403_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5404_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5405_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005406_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005407
5408template <class _Tp>
5409inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005410_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005411undeclare_reachable(_Tp* __p)
5412{
5413 return static_cast<_Tp*>(__undeclare_reachable(__p));
5414}
5415
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005416_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005417
Marshall Clow8982dcd2015-07-13 20:04:56 +00005418// --- Helper for container swap --
5419template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005420inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005421void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5422#if _LIBCPP_STD_VER >= 14
5423 _NOEXCEPT
5424#else
5425 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5426#endif
5427{
5428 __swap_allocator(__a1, __a2,
5429 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5430}
5431
5432template <typename _Alloc>
5433_LIBCPP_INLINE_VISIBILITY
5434void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5435#if _LIBCPP_STD_VER >= 14
5436 _NOEXCEPT
5437#else
5438 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5439#endif
5440{
5441 using _VSTD::swap;
5442 swap(__a1, __a2);
5443}
5444
5445template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005446inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005447void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5448
Marshall Clowff91de82015-08-18 19:51:37 +00005449template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005450struct __noexcept_move_assign_container : public integral_constant<bool,
5451 _Traits::propagate_on_container_move_assignment::value
5452#if _LIBCPP_STD_VER > 14
5453 || _Traits::is_always_equal::value
5454#else
5455 && is_nothrow_move_assignable<_Alloc>::value
5456#endif
5457 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005458
Marshall Clowa591b9a2016-07-11 21:38:08 +00005459
5460#ifndef _LIBCPP_HAS_NO_VARIADICS
5461template <class _Tp, class _Alloc>
5462struct __temp_value {
5463 typedef allocator_traits<_Alloc> _Traits;
5464
5465 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5466 _Alloc &__a;
5467
5468 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5469 _Tp & get() { return *__addr(); }
5470
5471 template<class... _Args>
5472 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5473 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5474
5475 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5476 };
5477#endif
5478
Howard Hinnantc51e1022010-05-11 19:42:16 +00005479_LIBCPP_END_NAMESPACE_STD
5480
5481#endif // _LIBCPP_MEMORY