blob: 7627248a40539d8b7813f453a4a82a5e77dcff16 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnant324bb032010-08-22 00:02:43 +000031
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant324bb032010-08-22 00:02:43 +000033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant1694d232011-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 Hinnantbc8d3f92010-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 Hinnant47761072010-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 Hinnantbc8d3f92010-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 Clowf0324bc2015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantbc8d3f92010-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 Hinnant1694d232011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantbc8d3f92010-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 Clow08b4f3f2013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantbc8d3f92010-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 Hinnant1694d232011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant1694d232011-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143
144template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000145bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-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 Hinnant1694d232011-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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167
168template <class InputIterator, class ForwardIterator>
169ForwardIterator
170uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
171
Howard Hinnant1694d232011-05-28 14:41:13 +0000172template <class InputIterator, class Size, class ForwardIterator>
173ForwardIterator
174uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
175
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176template <class ForwardIterator, class T>
177void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
178
179template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +0000180ForwardIterator
181uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182
183template <class Y> struct auto_ptr_ref {};
184
185template<class X>
186class auto_ptr
187{
188public:
189 typedef X element_type;
190
191 explicit auto_ptr(X* p =0) throw();
192 auto_ptr(auto_ptr&) throw();
193 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
194 auto_ptr& operator=(auto_ptr&) throw();
195 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
196 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
197 ~auto_ptr() throw();
198
199 typename add_lvalue_reference<X>::type operator*() const throw();
200 X* operator->() const throw();
201 X* get() const throw();
202 X* release() throw();
203 void reset(X* p =0) throw();
204
205 auto_ptr(auto_ptr_ref<X>) throw();
206 template<class Y> operator auto_ptr_ref<Y>() throw();
207 template<class Y> operator auto_ptr<Y>() throw();
208};
209
Howard Hinnante92c3d72010-08-19 18:39:17 +0000210template <class T>
211struct default_delete
212{
Howard Hinnant1694d232011-05-28 14:41:13 +0000213 constexpr default_delete() noexcept = default;
214 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000215
Howard Hinnant1694d232011-05-28 14:41:13 +0000216 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000217};
218
219template <class T>
220struct default_delete<T[]>
221{
Howard Hinnant1694d232011-05-28 14:41:13 +0000222 constexpr default_delete() noexcept = default;
223 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000224 template <class U> void operator()(U*) const = delete;
225};
226
Howard Hinnante92c3d72010-08-19 18:39:17 +0000227template <class T, class D = default_delete<T>>
228class unique_ptr
229{
230public:
231 typedef see below pointer;
232 typedef T element_type;
233 typedef D deleter_type;
234
235 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000236 constexpr unique_ptr() noexcept;
237 explicit unique_ptr(pointer p) noexcept;
238 unique_ptr(pointer p, see below d1) noexcept;
239 unique_ptr(pointer p, see below d2) noexcept;
240 unique_ptr(unique_ptr&& u) noexcept;
241 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000242 template <class U, class E>
Howard Hinnant1694d232011-05-28 14:41:13 +0000243 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000244 template <class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000245 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000246
247 // destructor
248 ~unique_ptr();
249
250 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000251 unique_ptr& operator=(unique_ptr&& u) noexcept;
252 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
253 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000254
255 // observers
256 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000257 pointer operator->() const noexcept;
258 pointer get() const noexcept;
259 deleter_type& get_deleter() noexcept;
260 const deleter_type& get_deleter() const noexcept;
261 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000262
263 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000264 pointer release() noexcept;
265 void reset(pointer p = pointer()) noexcept;
266 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000267};
268
269template <class T, class D>
270class unique_ptr<T[], D>
271{
272public:
273 typedef implementation-defined pointer;
274 typedef T element_type;
275 typedef D deleter_type;
276
277 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000278 constexpr unique_ptr() noexcept;
279 explicit unique_ptr(pointer p) noexcept;
280 unique_ptr(pointer p, see below d) noexcept;
281 unique_ptr(pointer p, see below d) noexcept;
282 unique_ptr(unique_ptr&& u) noexcept;
283 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000284
285 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000286 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000287
288 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000289 unique_ptr& operator=(unique_ptr&& u) noexcept;
290 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000291
292 // observers
293 T& operator[](size_t i) const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000294 pointer get() const noexcept;
295 deleter_type& get_deleter() noexcept;
296 const deleter_type& get_deleter() const noexcept;
297 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000298
299 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000300 pointer release() noexcept;
301 void reset(pointer p = pointer()) noexcept;
302 void reset(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000303 template <class U> void reset(U) = delete;
Howard Hinnant1694d232011-05-28 14:41:13 +0000304 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000305};
306
307template <class T, class D>
Howard Hinnant1694d232011-05-28 14:41:13 +0000308 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000309
310template <class T1, class D1, class T2, class D2>
311 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
312template <class T1, class D1, class T2, class D2>
313 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
314template <class T1, class D1, class T2, class D2>
315 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
316template <class T1, class D1, class T2, class D2>
317 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
318template <class T1, class D1, class T2, class D2>
319 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
320template <class T1, class D1, class T2, class D2>
321 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
322
Howard Hinnant1694d232011-05-28 14:41:13 +0000323template <class T, class D>
324 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
325template <class T, class D>
326 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
327template <class T, class D>
328 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
329template <class T, class D>
330 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
331
332template <class T, class D>
333 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
334template <class T, class D>
335 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
336template <class T, class D>
337 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
338template <class T, class D>
339 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
340template <class T, class D>
341 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
342template <class T, class D>
343 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
344template <class T, class D>
345 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
346template <class T, class D>
347 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
348
Howard Hinnante92c3d72010-08-19 18:39:17 +0000349class bad_weak_ptr
350 : public std::exception
351{
Howard Hinnant1694d232011-05-28 14:41:13 +0000352 bad_weak_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000353};
354
Marshall Clowfd7481e2013-07-01 18:16:03 +0000355template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
356template<class T> unique_ptr<T> make_unique(size_t n); // C++14
357template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
358
Howard Hinnante92c3d72010-08-19 18:39:17 +0000359template<class T>
360class shared_ptr
361{
362public:
363 typedef T element_type;
364
365 // constructors:
Howard Hinnant1694d232011-05-28 14:41:13 +0000366 constexpr shared_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000367 template<class Y> explicit shared_ptr(Y* p);
368 template<class Y, class D> shared_ptr(Y* p, D d);
369 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
370 template <class D> shared_ptr(nullptr_t p, D d);
371 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant1694d232011-05-28 14:41:13 +0000372 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
373 shared_ptr(const shared_ptr& r) noexcept;
374 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
375 shared_ptr(shared_ptr&& r) noexcept;
376 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000377 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
378 template<class Y> shared_ptr(auto_ptr<Y>&& r);
379 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
380 shared_ptr(nullptr_t) : shared_ptr() { }
381
382 // destructor:
383 ~shared_ptr();
384
385 // assignment:
Howard Hinnant1694d232011-05-28 14:41:13 +0000386 shared_ptr& operator=(const shared_ptr& r) noexcept;
387 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
388 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000389 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
390 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
391 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
392
393 // modifiers:
Howard Hinnant1694d232011-05-28 14:41:13 +0000394 void swap(shared_ptr& r) noexcept;
395 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000396 template<class Y> void reset(Y* p);
397 template<class Y, class D> void reset(Y* p, D d);
398 template<class Y, class D, class A> void reset(Y* p, D d, A a);
399
Howard Hinnant1694d232011-05-28 14:41:13 +0000400 // observers:
401 T* get() const noexcept;
402 T& operator*() const noexcept;
403 T* operator->() const noexcept;
404 long use_count() const noexcept;
405 bool unique() const noexcept;
406 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000407 template<class U> bool owner_before(shared_ptr<U> const& b) const;
408 template<class U> bool owner_before(weak_ptr<U> const& b) const;
409};
410
411// shared_ptr comparisons:
412template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000413 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000414template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000415 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000416template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000417 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000418template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000419 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000420template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000421 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000422template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000423 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
424
425template <class T>
426 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
427template <class T>
428 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
429template <class T>
430 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
431template <class T>
432 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
433template <class T>
434 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
435template <class T>
436bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
437template <class T>
438 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
439template <class T>
440 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
441template <class T>
442 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
443template <class T>
444 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
445template <class T>
446 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
447template <class T>
448 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000449
450// shared_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000451template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000452
453// shared_ptr casts:
454template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000455 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000456template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000457 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000458template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000459 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000460
461// shared_ptr I/O:
462template<class E, class T, class Y>
463 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
464
465// shared_ptr get_deleter:
Howard Hinnant1694d232011-05-28 14:41:13 +0000466template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000467
468template<class T, class... Args>
469 shared_ptr<T> make_shared(Args&&... args);
470template<class T, class A, class... Args>
471 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
472
473template<class T>
474class weak_ptr
475{
476public:
477 typedef T element_type;
478
479 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000480 constexpr weak_ptr() noexcept;
481 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
482 weak_ptr(weak_ptr const& r) noexcept;
483 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clow23ef1512014-03-05 03:12:04 +0000484 weak_ptr(weak_ptr&& r) noexcept; // C++14
485 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnante92c3d72010-08-19 18:39:17 +0000486
487 // destructor
488 ~weak_ptr();
489
490 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000491 weak_ptr& operator=(weak_ptr const& r) noexcept;
492 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
493 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clow23ef1512014-03-05 03:12:04 +0000494 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
495 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnante92c3d72010-08-19 18:39:17 +0000496
497 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000498 void swap(weak_ptr& r) noexcept;
499 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000500
501 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000502 long use_count() const noexcept;
503 bool expired() const noexcept;
504 shared_ptr<T> lock() const noexcept;
Marshall Clow1b5f3ad2013-09-03 14:37:50 +0000505 template<class U> bool owner_before(shared_ptr<U> const& b) const;
506 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000507};
508
509// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000510template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000511
512// class owner_less:
513template<class T> struct owner_less;
514
515template<class T>
516struct owner_less<shared_ptr<T>>
517 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
518{
519 typedef bool result_type;
520 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
521 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
522 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
523};
524
525template<class T>
526struct owner_less<weak_ptr<T>>
527 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
528{
529 typedef bool result_type;
530 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
531 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
532 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
533};
534
535template<class T>
536class enable_shared_from_this
537{
538protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000539 constexpr enable_shared_from_this() noexcept;
540 enable_shared_from_this(enable_shared_from_this const&) noexcept;
541 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000542 ~enable_shared_from_this();
543public:
544 shared_ptr<T> shared_from_this();
545 shared_ptr<T const> shared_from_this() const;
546};
547
548template<class T>
549 bool atomic_is_lock_free(const shared_ptr<T>* p);
550template<class T>
551 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
552template<class T>
553 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
554template<class T>
555 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
556template<class T>
557 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
558template<class T>
559 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
560template<class T>
561 shared_ptr<T>
562 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
563template<class T>
564 bool
565 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
566template<class T>
567 bool
568 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
569template<class T>
570 bool
571 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
572 shared_ptr<T> w, memory_order success,
573 memory_order failure);
574template<class T>
575 bool
576 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
577 shared_ptr<T> w, memory_order success,
578 memory_order failure);
579// Hash support
580template <class T> struct hash;
581template <class T, class D> struct hash<unique_ptr<T, D> >;
582template <class T> struct hash<shared_ptr<T> >;
583
584// Pointer safety
585enum class pointer_safety { relaxed, preferred, strict };
586void declare_reachable(void *p);
587template <class T> T *undeclare_reachable(T *p);
588void declare_no_pointers(char *p, size_t n);
589void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000590pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000591
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
593
594} // std
595
596*/
597
598#include <__config>
599#include <type_traits>
600#include <typeinfo>
601#include <cstddef>
602#include <cstdint>
603#include <new>
604#include <utility>
605#include <limits>
606#include <iterator>
607#include <__functional_base>
Howard Hinnant464aa5c2011-07-18 15:51:59 +0000608#include <iosfwd>
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000609#include <tuple>
Eric Fiselier4db388b2016-05-07 03:12:24 +0000610#include <stdexcept>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000611#include <cstring>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612#if defined(_LIBCPP_NO_EXCEPTIONS)
613 #include <cassert>
614#endif
615
Eric Fiselier00f4a492015-08-19 17:21:46 +0000616#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000617# include <atomic>
618#endif
619
Howard Hinnant66c6f972011-11-29 16:45:27 +0000620#include <__undef_min_max>
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +0000621#include <__undef___deallocate>
Howard Hinnant66c6f972011-11-29 16:45:27 +0000622
Howard Hinnant08e17472011-10-17 20:05:10 +0000623#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000625#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626
627_LIBCPP_BEGIN_NAMESPACE_STD
628
Eric Fiselierc6e46692015-07-07 00:27:16 +0000629template <class _ValueType>
630inline _LIBCPP_ALWAYS_INLINE
631_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
632#if !defined(_LIBCPP_HAS_NO_THREADS) && \
633 defined(__ATOMIC_RELAXED) && \
634 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
635 return __atomic_load_n(__value, __ATOMIC_RELAXED);
636#else
637 return *__value;
638#endif
639}
640
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000641// addressof moved to <__functional_base>
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000642
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643template <class _Tp> class allocator;
644
645template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000646class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647{
648public:
649 typedef void* pointer;
650 typedef const void* const_pointer;
651 typedef void value_type;
652
653 template <class _Up> struct rebind {typedef allocator<_Up> other;};
654};
655
Howard Hinnanta1877872012-01-19 23:15:22 +0000656template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000657class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000658{
659public:
660 typedef const void* pointer;
661 typedef const void* const_pointer;
662 typedef const void value_type;
663
664 template <class _Up> struct rebind {typedef allocator<_Up> other;};
665};
666
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667// pointer_traits
668
669template <class _Tp>
670struct __has_element_type
671{
672private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000673 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674 template <class _Up> static __two __test(...);
675 template <class _Up> static char __test(typename _Up::element_type* = 0);
676public:
677 static const bool value = sizeof(__test<_Tp>(0)) == 1;
678};
679
680template <class _Ptr, bool = __has_element_type<_Ptr>::value>
681struct __pointer_traits_element_type;
682
683template <class _Ptr>
684struct __pointer_traits_element_type<_Ptr, true>
685{
686 typedef typename _Ptr::element_type type;
687};
688
689#ifndef _LIBCPP_HAS_NO_VARIADICS
690
691template <template <class, class...> class _Sp, class _Tp, class ..._Args>
692struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
693{
694 typedef typename _Sp<_Tp, _Args...>::element_type type;
695};
696
697template <template <class, class...> class _Sp, class _Tp, class ..._Args>
698struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
699{
700 typedef _Tp type;
701};
702
Howard Hinnant324bb032010-08-22 00:02:43 +0000703#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704
705template <template <class> class _Sp, class _Tp>
706struct __pointer_traits_element_type<_Sp<_Tp>, true>
707{
708 typedef typename _Sp<_Tp>::element_type type;
709};
710
711template <template <class> class _Sp, class _Tp>
712struct __pointer_traits_element_type<_Sp<_Tp>, false>
713{
714 typedef _Tp type;
715};
716
717template <template <class, class> class _Sp, class _Tp, class _A0>
718struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
719{
720 typedef typename _Sp<_Tp, _A0>::element_type type;
721};
722
723template <template <class, class> class _Sp, class _Tp, class _A0>
724struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
725{
726 typedef _Tp type;
727};
728
729template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
730struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
731{
732 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
733};
734
735template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
736struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
737{
738 typedef _Tp type;
739};
740
741template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
742 class _A1, class _A2>
743struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
744{
745 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
746};
747
748template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
749 class _A1, class _A2>
750struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
751{
752 typedef _Tp type;
753};
754
Howard Hinnant324bb032010-08-22 00:02:43 +0000755#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000756
757template <class _Tp>
758struct __has_difference_type
759{
760private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000761 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000762 template <class _Up> static __two __test(...);
763 template <class _Up> static char __test(typename _Up::difference_type* = 0);
764public:
765 static const bool value = sizeof(__test<_Tp>(0)) == 1;
766};
767
768template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
769struct __pointer_traits_difference_type
770{
771 typedef ptrdiff_t type;
772};
773
774template <class _Ptr>
775struct __pointer_traits_difference_type<_Ptr, true>
776{
777 typedef typename _Ptr::difference_type type;
778};
779
780template <class _Tp, class _Up>
781struct __has_rebind
782{
783private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000784 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785 template <class _Xp> static __two __test(...);
786 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
787public:
788 static const bool value = sizeof(__test<_Tp>(0)) == 1;
789};
790
791template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
792struct __pointer_traits_rebind
793{
794#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
795 typedef typename _Tp::template rebind<_Up> type;
796#else
797 typedef typename _Tp::template rebind<_Up>::other type;
798#endif
799};
800
801#ifndef _LIBCPP_HAS_NO_VARIADICS
802
803template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
804struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
805{
806#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
807 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
808#else
809 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
810#endif
811};
812
813template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
814struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
815{
816 typedef _Sp<_Up, _Args...> type;
817};
818
Howard Hinnant324bb032010-08-22 00:02:43 +0000819#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000820
821template <template <class> class _Sp, class _Tp, class _Up>
822struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
823{
824#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
825 typedef typename _Sp<_Tp>::template rebind<_Up> type;
826#else
827 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
828#endif
829};
830
831template <template <class> class _Sp, class _Tp, class _Up>
832struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
833{
834 typedef _Sp<_Up> type;
835};
836
837template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
838struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
839{
840#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
841 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
842#else
843 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
844#endif
845};
846
847template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
848struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
849{
850 typedef _Sp<_Up, _A0> type;
851};
852
853template <template <class, class, class> class _Sp, class _Tp, class _A0,
854 class _A1, class _Up>
855struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
856{
857#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
858 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
859#else
860 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
861#endif
862};
863
864template <template <class, class, class> class _Sp, class _Tp, class _A0,
865 class _A1, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
867{
868 typedef _Sp<_Up, _A0, _A1> type;
869};
870
871template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
872 class _A1, class _A2, class _Up>
873struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
874{
875#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
876 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
877#else
878 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
879#endif
880};
881
882template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
883 class _A1, class _A2, class _Up>
884struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
885{
886 typedef _Sp<_Up, _A0, _A1, _A2> type;
887};
888
Howard Hinnant324bb032010-08-22 00:02:43 +0000889#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890
891template <class _Ptr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000892struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893{
894 typedef _Ptr pointer;
895 typedef typename __pointer_traits_element_type<pointer>::type element_type;
896 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
897
898#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000899 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900#else
901 template <class _Up> struct rebind
902 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000903#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904
905private:
906 struct __nat {};
907public:
Howard Hinnant82894812010-09-22 16:48:34 +0000908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909 static pointer pointer_to(typename conditional<is_void<element_type>::value,
910 __nat, element_type>::type& __r)
911 {return pointer::pointer_to(__r);}
912};
913
914template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000915struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916{
917 typedef _Tp* pointer;
918 typedef _Tp element_type;
919 typedef ptrdiff_t difference_type;
920
921#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
922 template <class _Up> using rebind = _Up*;
923#else
924 template <class _Up> struct rebind {typedef _Up* other;};
925#endif
926
927private:
928 struct __nat {};
929public:
Howard Hinnant82894812010-09-22 16:48:34 +0000930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000932 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000933 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934};
935
Eric Fiselierbb2f28e2015-08-23 02:56:05 +0000936template <class _From, class _To>
937struct __rebind_pointer {
938#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
939 typedef typename pointer_traits<_From>::template rebind<_To> type;
940#else
941 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
942#endif
943};
944
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945// allocator_traits
946
947namespace __has_pointer_type_imp
948{
Howard Hinnant81277582013-09-21 01:45:05 +0000949 template <class _Up> static __two __test(...);
950 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951}
952
953template <class _Tp>
954struct __has_pointer_type
Howard Hinnant81277582013-09-21 01:45:05 +0000955 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956{
957};
958
959namespace __pointer_type_imp
960{
961
962template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
963struct __pointer_type
964{
965 typedef typename _Dp::pointer type;
966};
967
968template <class _Tp, class _Dp>
969struct __pointer_type<_Tp, _Dp, false>
970{
971 typedef _Tp* type;
972};
973
Howard Hinnant47761072010-11-18 01:40:00 +0000974} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975
976template <class _Tp, class _Dp>
977struct __pointer_type
978{
979 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
980};
981
982template <class _Tp>
983struct __has_const_pointer
984{
985private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000986 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 template <class _Up> static __two __test(...);
988 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
989public:
990 static const bool value = sizeof(__test<_Tp>(0)) == 1;
991};
992
993template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
994struct __const_pointer
995{
996 typedef typename _Alloc::const_pointer type;
997};
998
999template <class _Tp, class _Ptr, class _Alloc>
1000struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1001{
1002#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1003 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1004#else
1005 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1006#endif
1007};
1008
1009template <class _Tp>
1010struct __has_void_pointer
1011{
1012private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001013 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001014 template <class _Up> static __two __test(...);
1015 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1016public:
1017 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1018};
1019
1020template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1021struct __void_pointer
1022{
1023 typedef typename _Alloc::void_pointer type;
1024};
1025
1026template <class _Ptr, class _Alloc>
1027struct __void_pointer<_Ptr, _Alloc, false>
1028{
1029#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1030 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1031#else
1032 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1033#endif
1034};
1035
1036template <class _Tp>
1037struct __has_const_void_pointer
1038{
1039private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001040 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041 template <class _Up> static __two __test(...);
1042 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1043public:
1044 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1045};
1046
1047template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1048struct __const_void_pointer
1049{
1050 typedef typename _Alloc::const_void_pointer type;
1051};
1052
1053template <class _Ptr, class _Alloc>
1054struct __const_void_pointer<_Ptr, _Alloc, false>
1055{
1056#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1057 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1058#else
1059 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1060#endif
1061};
1062
Howard Hinnant99968442011-11-29 18:15:50 +00001063template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001065_Tp*
1066__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067{
1068 return __p;
1069}
1070
1071template <class _Pointer>
1072inline _LIBCPP_INLINE_VISIBILITY
1073typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001074__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001076 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077}
1078
1079template <class _Tp>
1080struct __has_size_type
1081{
1082private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001083 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084 template <class _Up> static __two __test(...);
1085 template <class _Up> static char __test(typename _Up::size_type* = 0);
1086public:
1087 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1088};
1089
Howard Hinnant47761072010-11-18 01:40:00 +00001090template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091struct __size_type
1092{
Howard Hinnant47761072010-11-18 01:40:00 +00001093 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094};
1095
Howard Hinnant47761072010-11-18 01:40:00 +00001096template <class _Alloc, class _DiffType>
1097struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001098{
1099 typedef typename _Alloc::size_type type;
1100};
1101
1102template <class _Tp>
1103struct __has_propagate_on_container_copy_assignment
1104{
1105private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001106 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107 template <class _Up> static __two __test(...);
1108 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1109public:
1110 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1111};
1112
1113template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1114struct __propagate_on_container_copy_assignment
1115{
1116 typedef false_type type;
1117};
1118
1119template <class _Alloc>
1120struct __propagate_on_container_copy_assignment<_Alloc, true>
1121{
1122 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1123};
1124
1125template <class _Tp>
1126struct __has_propagate_on_container_move_assignment
1127{
1128private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001129 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130 template <class _Up> static __two __test(...);
1131 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1132public:
1133 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1134};
1135
1136template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1137struct __propagate_on_container_move_assignment
1138{
1139 typedef false_type type;
1140};
1141
1142template <class _Alloc>
1143struct __propagate_on_container_move_assignment<_Alloc, true>
1144{
1145 typedef typename _Alloc::propagate_on_container_move_assignment type;
1146};
1147
1148template <class _Tp>
1149struct __has_propagate_on_container_swap
1150{
1151private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001152 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153 template <class _Up> static __two __test(...);
1154 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1155public:
1156 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1157};
1158
1159template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1160struct __propagate_on_container_swap
1161{
1162 typedef false_type type;
1163};
1164
1165template <class _Alloc>
1166struct __propagate_on_container_swap<_Alloc, true>
1167{
1168 typedef typename _Alloc::propagate_on_container_swap type;
1169};
1170
Marshall Clowf0324bc2015-06-02 16:34:03 +00001171template <class _Tp>
1172struct __has_is_always_equal
1173{
1174private:
1175 struct __two {char __lx; char __lxx;};
1176 template <class _Up> static __two __test(...);
1177 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1178public:
1179 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1180};
1181
1182template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1183struct __is_always_equal
1184{
1185 typedef typename _VSTD::is_empty<_Alloc>::type type;
1186};
1187
1188template <class _Alloc>
1189struct __is_always_equal<_Alloc, true>
1190{
1191 typedef typename _Alloc::is_always_equal type;
1192};
1193
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1195struct __has_rebind_other
1196{
1197private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001198 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199 template <class _Xp> static __two __test(...);
1200 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1201public:
1202 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1203};
1204
1205template <class _Tp, class _Up>
1206struct __has_rebind_other<_Tp, _Up, false>
1207{
1208 static const bool value = false;
1209};
1210
1211template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1212struct __allocator_traits_rebind
1213{
1214 typedef typename _Tp::template rebind<_Up>::other type;
1215};
1216
1217#ifndef _LIBCPP_HAS_NO_VARIADICS
1218
1219template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1220struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1221{
1222 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1223};
1224
1225template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1226struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1227{
1228 typedef _Alloc<_Up, _Args...> type;
1229};
1230
Howard Hinnant324bb032010-08-22 00:02:43 +00001231#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232
1233template <template <class> class _Alloc, class _Tp, class _Up>
1234struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1235{
1236 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1237};
1238
1239template <template <class> class _Alloc, class _Tp, class _Up>
1240struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1241{
1242 typedef _Alloc<_Up> type;
1243};
1244
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1246struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1247{
1248 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1249};
1250
1251template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1252struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1253{
1254 typedef _Alloc<_Up, _A0> type;
1255};
1256
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1258 class _A1, class _Up>
1259struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1260{
1261 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1262};
1263
1264template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1265 class _A1, class _Up>
1266struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1267{
1268 typedef _Alloc<_Up, _A0, _A1> type;
1269};
1270
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1272 class _A1, class _A2, class _Up>
1273struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1274{
1275 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1276};
1277
1278template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1279 class _A1, class _A2, class _Up>
1280struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1281{
1282 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1283};
1284
Howard Hinnant324bb032010-08-22 00:02:43 +00001285#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286
1287#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1288
1289template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1290auto
1291__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1292 -> decltype(__a.allocate(__sz, __p), true_type());
1293
1294template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1295auto
1296__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1297 -> false_type;
1298
1299template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1300struct __has_allocate_hint
1301 : integral_constant<bool,
1302 is_same<
1303 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1304 declval<_SizeType>(),
1305 declval<_ConstVoidPtr>())),
1306 true_type>::value>
1307{
1308};
1309
Howard Hinnant324bb032010-08-22 00:02:43 +00001310#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311
1312template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1313struct __has_allocate_hint
1314 : true_type
1315{
1316};
1317
Howard Hinnant324bb032010-08-22 00:02:43 +00001318#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319
Howard Hinnant23369ee2011-07-29 21:35:53 +00001320#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321
1322template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001323decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1324 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 true_type())
1326__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1327
1328template <class _Alloc, class _Pointer, class ..._Args>
1329false_type
1330__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1331
1332template <class _Alloc, class _Pointer, class ..._Args>
1333struct __has_construct
1334 : integral_constant<bool,
1335 is_same<
1336 decltype(__has_construct_test(declval<_Alloc>(),
1337 declval<_Pointer>(),
1338 declval<_Args>()...)),
1339 true_type>::value>
1340{
1341};
1342
1343template <class _Alloc, class _Pointer>
1344auto
1345__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1346 -> decltype(__a.destroy(__p), true_type());
1347
1348template <class _Alloc, class _Pointer>
1349auto
1350__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1351 -> false_type;
1352
1353template <class _Alloc, class _Pointer>
1354struct __has_destroy
1355 : integral_constant<bool,
1356 is_same<
1357 decltype(__has_destroy_test(declval<_Alloc>(),
1358 declval<_Pointer>())),
1359 true_type>::value>
1360{
1361};
1362
1363template <class _Alloc>
1364auto
1365__has_max_size_test(_Alloc&& __a)
1366 -> decltype(__a.max_size(), true_type());
1367
1368template <class _Alloc>
1369auto
1370__has_max_size_test(const volatile _Alloc& __a)
1371 -> false_type;
1372
1373template <class _Alloc>
1374struct __has_max_size
1375 : integral_constant<bool,
1376 is_same<
1377 decltype(__has_max_size_test(declval<_Alloc&>())),
1378 true_type>::value>
1379{
1380};
1381
1382template <class _Alloc>
1383auto
1384__has_select_on_container_copy_construction_test(_Alloc&& __a)
1385 -> decltype(__a.select_on_container_copy_construction(), true_type());
1386
1387template <class _Alloc>
1388auto
1389__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1390 -> false_type;
1391
1392template <class _Alloc>
1393struct __has_select_on_container_copy_construction
1394 : integral_constant<bool,
1395 is_same<
1396 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1397 true_type>::value>
1398{
1399};
1400
Howard Hinnant324bb032010-08-22 00:02:43 +00001401#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402
1403#ifndef _LIBCPP_HAS_NO_VARIADICS
1404
1405template <class _Alloc, class _Pointer, class ..._Args>
1406struct __has_construct
1407 : false_type
1408{
1409};
1410
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001411#else // _LIBCPP_HAS_NO_VARIADICS
1412
1413template <class _Alloc, class _Pointer, class _Args>
1414struct __has_construct
1415 : false_type
1416{
1417};
1418
Howard Hinnant324bb032010-08-22 00:02:43 +00001419#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420
1421template <class _Alloc, class _Pointer>
1422struct __has_destroy
1423 : false_type
1424{
1425};
1426
1427template <class _Alloc>
1428struct __has_max_size
1429 : true_type
1430{
1431};
1432
1433template <class _Alloc>
1434struct __has_select_on_container_copy_construction
1435 : false_type
1436{
1437};
1438
Howard Hinnant324bb032010-08-22 00:02:43 +00001439#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440
Howard Hinnant47761072010-11-18 01:40:00 +00001441template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1442struct __alloc_traits_difference_type
1443{
1444 typedef typename pointer_traits<_Ptr>::difference_type type;
1445};
1446
1447template <class _Alloc, class _Ptr>
1448struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1449{
1450 typedef typename _Alloc::difference_type type;
1451};
1452
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453template <class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001454struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455{
1456 typedef _Alloc allocator_type;
1457 typedef typename allocator_type::value_type value_type;
1458
1459 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1460 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1461 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1462 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1463
Howard Hinnant47761072010-11-18 01:40:00 +00001464 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1465 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466
1467 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1468 propagate_on_container_copy_assignment;
1469 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1470 propagate_on_container_move_assignment;
1471 typedef typename __propagate_on_container_swap<allocator_type>::type
1472 propagate_on_container_swap;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001473 typedef typename __is_always_equal<allocator_type>::type
1474 is_always_equal;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475
1476#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1477 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001478 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001480#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481 template <class _Tp> struct rebind_alloc
1482 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1483 template <class _Tp> struct rebind_traits
1484 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001485#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486
Howard Hinnant82894812010-09-22 16:48:34 +00001487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 static pointer allocate(allocator_type& __a, size_type __n)
1489 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1492 {return allocate(__a, __n, __hint,
1493 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1494
Howard Hinnant82894812010-09-22 16:48:34 +00001495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001496 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497 {__a.deallocate(__p, __n);}
1498
1499#ifndef _LIBCPP_HAS_NO_VARIADICS
1500 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3f5579f2014-11-11 19:22:33 +00001503 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001504 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001505#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508 static void construct(allocator_type& __a, _Tp* __p)
1509 {
1510 ::new ((void*)__p) _Tp();
1511 }
1512 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1515 {
1516 ::new ((void*)__p) _Tp(__a0);
1517 }
1518 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1521 const _A1& __a1)
1522 {
1523 ::new ((void*)__p) _Tp(__a0, __a1);
1524 }
1525 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001526 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1528 const _A1& __a1, const _A2& __a2)
1529 {
1530 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1531 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001532#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533
1534 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536 static void destroy(allocator_type& __a, _Tp* __p)
1537 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1538
Howard Hinnant82894812010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Marshall Clow08b4f3f2013-08-27 20:22:15 +00001540 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1542
Howard Hinnant82894812010-09-22 16:48:34 +00001543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544 static allocator_type
1545 select_on_container_copy_construction(const allocator_type& __a)
1546 {return select_on_container_copy_construction(
1547 __has_select_on_container_copy_construction<const allocator_type>(),
1548 __a);}
1549
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001550 template <class _Ptr>
1551 _LIBCPP_INLINE_VISIBILITY
1552 static
1553 void
1554 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1555 {
1556 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1557 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1558 }
1559
1560 template <class _Tp>
1561 _LIBCPP_INLINE_VISIBILITY
1562 static
1563 typename enable_if
1564 <
1565 (is_same<allocator_type, allocator<_Tp> >::value
1566 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1567 is_trivially_move_constructible<_Tp>::value,
1568 void
1569 >::type
1570 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1571 {
1572 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001573 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001574 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001575 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001576 __begin2 += _Np;
1577 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001578 }
1579
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001580 template <class _Iter, class _Ptr>
1581 _LIBCPP_INLINE_VISIBILITY
1582 static
1583 void
1584 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1585 {
1586 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1587 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1588 }
1589
1590 template <class _Tp>
1591 _LIBCPP_INLINE_VISIBILITY
1592 static
1593 typename enable_if
1594 <
1595 (is_same<allocator_type, allocator<_Tp> >::value
1596 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1597 is_trivially_move_constructible<_Tp>::value,
1598 void
1599 >::type
1600 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1601 {
1602 typedef typename remove_const<_Tp>::type _Vp;
1603 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001604 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001605 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001606 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001607 __begin2 += _Np;
1608 }
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001609 }
1610
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001611 template <class _Ptr>
1612 _LIBCPP_INLINE_VISIBILITY
1613 static
1614 void
1615 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1616 {
1617 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001618 {
1619 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1620 --__end2;
1621 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001622 }
1623
1624 template <class _Tp>
1625 _LIBCPP_INLINE_VISIBILITY
1626 static
1627 typename enable_if
1628 <
1629 (is_same<allocator_type, allocator<_Tp> >::value
1630 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1631 is_trivially_move_constructible<_Tp>::value,
1632 void
1633 >::type
1634 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1635 {
1636 ptrdiff_t _Np = __end1 - __begin1;
1637 __end2 -= _Np;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001638 if (_Np > 0)
1639 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001640 }
1641
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642private:
1643
Howard Hinnant82894812010-09-22 16:48:34 +00001644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 static pointer allocate(allocator_type& __a, size_type __n,
1646 const_void_pointer __hint, true_type)
1647 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001650 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651 {return __a.allocate(__n);}
1652
1653#ifndef _LIBCPP_HAS_NO_VARIADICS
1654 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001657 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1661 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001662 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001664#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665
1666 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1669 {__a.destroy(__p);}
1670 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672 static void __destroy(false_type, allocator_type&, _Tp* __p)
1673 {
1674 __p->~_Tp();
1675 }
1676
Howard Hinnant82894812010-09-22 16:48:34 +00001677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678 static size_type __max_size(true_type, const allocator_type& __a)
1679 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow88fa03a2015-10-25 19:34:04 +00001682 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001683
Howard Hinnant82894812010-09-22 16:48:34 +00001684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685 static allocator_type
1686 select_on_container_copy_construction(true_type, const allocator_type& __a)
1687 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 static allocator_type
1690 select_on_container_copy_construction(false_type, const allocator_type& __a)
1691 {return __a;}
1692};
1693
Marshall Clow66302c62015-04-07 05:21:38 +00001694template <class _Traits, class _Tp>
1695struct __rebind_alloc_helper
1696{
1697#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Marshall Clow0ad232a2015-05-10 13:59:45 +00001698 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow66302c62015-04-07 05:21:38 +00001699#else
1700 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1701#endif
1702};
1703
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704// allocator
1705
1706template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001707class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708{
1709public:
1710 typedef size_t size_type;
1711 typedef ptrdiff_t difference_type;
1712 typedef _Tp* pointer;
1713 typedef const _Tp* const_pointer;
1714 typedef _Tp& reference;
1715 typedef const _Tp& const_reference;
1716 typedef _Tp value_type;
1717
Howard Hinnant18884f42011-06-02 21:38:57 +00001718 typedef true_type propagate_on_container_move_assignment;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001719 typedef true_type is_always_equal;
Howard Hinnant18884f42011-06-02 21:38:57 +00001720
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1722
Howard Hinnant1694d232011-05-28 14:41:13 +00001723 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1724 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1725 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001726 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001727 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001728 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow4951a482016-03-03 12:04:39 +00001730 {
1731 if (__n > max_size())
Eric Fiselier4db388b2016-05-07 03:12:24 +00001732 __libcpp_throw(length_error("allocator<T>::allocate(size_t n)"
1733 " 'n' exceeds maximum supported size"));
Marshall Clow4951a482016-03-03 12:04:39 +00001734 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1735 }
Howard Hinnant1694d232011-05-28 14:41:13 +00001736 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001737 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001738 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1739 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001740#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741 template <class _Up, class... _Args>
1742 _LIBCPP_INLINE_VISIBILITY
1743 void
1744 construct(_Up* __p, _Args&&... __args)
1745 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001746 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001748#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749 _LIBCPP_INLINE_VISIBILITY
1750 void
1751 construct(pointer __p)
1752 {
1753 ::new((void*)__p) _Tp();
1754 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001755# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001756
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757 template <class _A0>
1758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001759 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760 construct(pointer __p, _A0& __a0)
1761 {
1762 ::new((void*)__p) _Tp(__a0);
1763 }
1764 template <class _A0>
1765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001766 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001767 construct(pointer __p, const _A0& __a0)
1768 {
1769 ::new((void*)__p) _Tp(__a0);
1770 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001771# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772 template <class _A0, class _A1>
1773 _LIBCPP_INLINE_VISIBILITY
1774 void
1775 construct(pointer __p, _A0& __a0, _A1& __a1)
1776 {
1777 ::new((void*)__p) _Tp(__a0, __a1);
1778 }
1779 template <class _A0, class _A1>
1780 _LIBCPP_INLINE_VISIBILITY
1781 void
1782 construct(pointer __p, const _A0& __a0, _A1& __a1)
1783 {
1784 ::new((void*)__p) _Tp(__a0, __a1);
1785 }
1786 template <class _A0, class _A1>
1787 _LIBCPP_INLINE_VISIBILITY
1788 void
1789 construct(pointer __p, _A0& __a0, const _A1& __a1)
1790 {
1791 ::new((void*)__p) _Tp(__a0, __a1);
1792 }
1793 template <class _A0, class _A1>
1794 _LIBCPP_INLINE_VISIBILITY
1795 void
1796 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1797 {
1798 ::new((void*)__p) _Tp(__a0, __a1);
1799 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001800#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1802};
1803
Howard Hinnant57199402012-01-02 17:56:02 +00001804template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001805class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001806{
1807public:
1808 typedef size_t size_type;
1809 typedef ptrdiff_t difference_type;
1810 typedef const _Tp* pointer;
1811 typedef const _Tp* const_pointer;
1812 typedef const _Tp& reference;
1813 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001814 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001815
1816 typedef true_type propagate_on_container_move_assignment;
Marshall Clowb81d6f52015-07-01 21:23:40 +00001817 typedef true_type is_always_equal;
Howard Hinnant57199402012-01-02 17:56:02 +00001818
1819 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1820
1821 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1822 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1823 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1824 {return _VSTD::addressof(__x);}
1825 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow4951a482016-03-03 12:04:39 +00001826 {
1827 if (__n > max_size())
Eric Fiselier4db388b2016-05-07 03:12:24 +00001828 __libcpp_throw(length_error("allocator<const T>::allocate(size_t n)"
1829 " 'n' exceeds maximum supported size"));
Marshall Clow4951a482016-03-03 12:04:39 +00001830 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier4db388b2016-05-07 03:12:24 +00001831 }
Howard Hinnant57199402012-01-02 17:56:02 +00001832 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001833 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant57199402012-01-02 17:56:02 +00001834 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1835 {return size_type(~0) / sizeof(_Tp);}
1836#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1837 template <class _Up, class... _Args>
1838 _LIBCPP_INLINE_VISIBILITY
1839 void
1840 construct(_Up* __p, _Args&&... __args)
1841 {
1842 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1843 }
1844#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1845 _LIBCPP_INLINE_VISIBILITY
1846 void
1847 construct(pointer __p)
1848 {
1849 ::new((void*)__p) _Tp();
1850 }
1851# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001852
Howard Hinnant57199402012-01-02 17:56:02 +00001853 template <class _A0>
1854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001855 void
Howard Hinnant57199402012-01-02 17:56:02 +00001856 construct(pointer __p, _A0& __a0)
1857 {
1858 ::new((void*)__p) _Tp(__a0);
1859 }
1860 template <class _A0>
1861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001862 void
Howard Hinnant57199402012-01-02 17:56:02 +00001863 construct(pointer __p, const _A0& __a0)
1864 {
1865 ::new((void*)__p) _Tp(__a0);
1866 }
Howard Hinnant57199402012-01-02 17:56:02 +00001867# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1868 template <class _A0, class _A1>
1869 _LIBCPP_INLINE_VISIBILITY
1870 void
1871 construct(pointer __p, _A0& __a0, _A1& __a1)
1872 {
1873 ::new((void*)__p) _Tp(__a0, __a1);
1874 }
1875 template <class _A0, class _A1>
1876 _LIBCPP_INLINE_VISIBILITY
1877 void
1878 construct(pointer __p, const _A0& __a0, _A1& __a1)
1879 {
1880 ::new((void*)__p) _Tp(__a0, __a1);
1881 }
1882 template <class _A0, class _A1>
1883 _LIBCPP_INLINE_VISIBILITY
1884 void
1885 construct(pointer __p, _A0& __a0, const _A1& __a1)
1886 {
1887 ::new((void*)__p) _Tp(__a0, __a1);
1888 }
1889 template <class _A0, class _A1>
1890 _LIBCPP_INLINE_VISIBILITY
1891 void
1892 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1893 {
1894 ::new((void*)__p) _Tp(__a0, __a1);
1895 }
1896#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1897 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1898};
1899
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900template <class _Tp, class _Up>
1901inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001902bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903
1904template <class _Tp, class _Up>
1905inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001906bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907
1908template <class _OutputIterator, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001909class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910 : public iterator<output_iterator_tag,
1911 _Tp, // purposefully not C++03
1912 ptrdiff_t, // purposefully not C++03
1913 _Tp*, // purposefully not C++03
1914 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1915{
1916private:
1917 _OutputIterator __x_;
1918public:
1919 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1920 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1921 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1922 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow332ab912015-10-25 18:58:07 +00001923#if _LIBCPP_STD_VER >= 14
1924 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1925 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1926#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1928 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1929 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00001930#if _LIBCPP_STD_VER >= 14
1931 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1932#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933};
1934
1935template <class _Tp>
1936pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001937get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938{
1939 pair<_Tp*, ptrdiff_t> __r(0, 0);
1940 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1941 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1942 / sizeof(_Tp);
1943 if (__n > __m)
1944 __n = __m;
1945 while (__n > 0)
1946 {
1947 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1948 if (__r.first)
1949 {
1950 __r.second = __n;
1951 break;
1952 }
1953 __n /= 2;
1954 }
1955 return __r;
1956}
1957
1958template <class _Tp>
1959inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001960void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961
1962template <class _Tp>
1963struct auto_ptr_ref
1964{
1965 _Tp* __ptr_;
1966};
1967
1968template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001969class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970{
1971private:
1972 _Tp* __ptr_;
1973public:
1974 typedef _Tp element_type;
1975
1976 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1977 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1978 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1979 : __ptr_(__p.release()) {}
1980 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1981 {reset(__p.release()); return *this;}
1982 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1983 {reset(__p.release()); return *this;}
1984 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1985 {reset(__p.__ptr_); return *this;}
1986 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1987
1988 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1989 {return *__ptr_;}
1990 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1991 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1992 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1993 {
1994 _Tp* __t = __ptr_;
1995 __ptr_ = 0;
1996 return __t;
1997 }
1998 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1999 {
2000 if (__ptr_ != __p)
2001 delete __ptr_;
2002 __ptr_ = __p;
2003 }
2004
2005 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2006 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2007 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2008 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2009 {return auto_ptr<_Up>(release());}
2010};
2011
2012template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002013class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014{
2015public:
2016 typedef void element_type;
2017};
2018
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2020 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002021 bool = is_empty<_T1>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002022 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002023 bool = is_empty<_T2>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002024 && !__libcpp_is_final<_T2>::value
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002025 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026struct __libcpp_compressed_pair_switch;
2027
2028template <class _T1, class _T2, bool IsSame>
2029struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2030
2031template <class _T1, class _T2, bool IsSame>
2032struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2033
2034template <class _T1, class _T2, bool IsSame>
2035struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2036
2037template <class _T1, class _T2>
2038struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2039
2040template <class _T1, class _T2>
2041struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2042
2043template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2044class __libcpp_compressed_pair_imp;
2045
2046template <class _T1, class _T2>
2047class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2048{
2049private:
2050 _T1 __first_;
2051 _T2 __second_;
2052public:
2053 typedef _T1 _T1_param;
2054 typedef _T2 _T2_param;
2055
2056 typedef typename remove_reference<_T1>::type& _T1_reference;
2057 typedef typename remove_reference<_T2>::type& _T2_reference;
2058
2059 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2060 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2061
Marshall Clowcd6ed542015-07-16 03:05:06 +00002062 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002063 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002064 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002065 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002066 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002068 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002069
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002070#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002071
2072 _LIBCPP_INLINE_VISIBILITY
2073 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2074 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2075 is_nothrow_copy_constructible<_T2>::value)
2076 : __first_(__p.first()),
2077 __second_(__p.second()) {}
2078
2079 _LIBCPP_INLINE_VISIBILITY
2080 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2081 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2082 is_nothrow_copy_assignable<_T2>::value)
2083 {
2084 __first_ = __p.first();
2085 __second_ = __p.second();
2086 return *this;
2087 }
2088
Howard Hinnant61aa6012011-07-01 19:24:36 +00002089 _LIBCPP_INLINE_VISIBILITY
2090 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002091 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2092 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002093 : __first_(_VSTD::forward<_T1>(__p.first())),
2094 __second_(_VSTD::forward<_T2>(__p.second())) {}
2095
2096 _LIBCPP_INLINE_VISIBILITY
2097 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2098 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2099 is_nothrow_move_assignable<_T2>::value)
2100 {
2101 __first_ = _VSTD::forward<_T1>(__p.first());
2102 __second_ = _VSTD::forward<_T2>(__p.second());
2103 return *this;
2104 }
2105
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002106#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002107
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002108#ifndef _LIBCPP_HAS_NO_VARIADICS
2109
2110 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2111 _LIBCPP_INLINE_VISIBILITY
2112 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2113 tuple<_Args1...> __first_args,
2114 tuple<_Args2...> __second_args,
2115 __tuple_indices<_I1...>,
2116 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002117 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2118 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002119 {}
2120
2121#endif // _LIBCPP_HAS_NO_VARIADICS
2122
Howard Hinnant1694d232011-05-28 14:41:13 +00002123 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2124 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125
Howard Hinnant1694d232011-05-28 14:41:13 +00002126 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2127 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128
2129 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002130 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002131 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002133 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134 swap(__first_, __x.__first_);
2135 swap(__second_, __x.__second_);
2136 }
2137};
2138
2139template <class _T1, class _T2>
2140class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2141 : private _T1
2142{
2143private:
2144 _T2 __second_;
2145public:
2146 typedef _T1 _T1_param;
2147 typedef _T2 _T2_param;
2148
2149 typedef _T1& _T1_reference;
2150 typedef typename remove_reference<_T2>::type& _T2_reference;
2151
2152 typedef const _T1& _T1_const_reference;
2153 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2154
Marshall Clowcd6ed542015-07-16 03:05:06 +00002155 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002156 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002157 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002158 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002159 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002161 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002163#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002164
2165 _LIBCPP_INLINE_VISIBILITY
2166 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2167 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2168 is_nothrow_copy_constructible<_T2>::value)
2169 : _T1(__p.first()), __second_(__p.second()) {}
2170
2171 _LIBCPP_INLINE_VISIBILITY
2172 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2173 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2174 is_nothrow_copy_assignable<_T2>::value)
2175 {
2176 _T1::operator=(__p.first());
2177 __second_ = __p.second();
2178 return *this;
2179 }
2180
Howard Hinnant61aa6012011-07-01 19:24:36 +00002181 _LIBCPP_INLINE_VISIBILITY
2182 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002183 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2184 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002185 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002186
2187 _LIBCPP_INLINE_VISIBILITY
2188 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2189 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2190 is_nothrow_move_assignable<_T2>::value)
2191 {
2192 _T1::operator=(_VSTD::move(__p.first()));
2193 __second_ = _VSTD::forward<_T2>(__p.second());
2194 return *this;
2195 }
2196
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002197#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002198
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002199#ifndef _LIBCPP_HAS_NO_VARIADICS
2200
2201 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2202 _LIBCPP_INLINE_VISIBILITY
2203 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2204 tuple<_Args1...> __first_args,
2205 tuple<_Args2...> __second_args,
2206 __tuple_indices<_I1...>,
2207 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002208 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2209 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002210 {}
2211
2212#endif // _LIBCPP_HAS_NO_VARIADICS
2213
Howard Hinnant1694d232011-05-28 14:41:13 +00002214 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2215 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216
Howard Hinnant1694d232011-05-28 14:41:13 +00002217 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2218 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219
2220 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002221 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002222 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002224 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225 swap(__second_, __x.__second_);
2226 }
2227};
2228
2229template <class _T1, class _T2>
2230class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2231 : private _T2
2232{
2233private:
2234 _T1 __first_;
2235public:
2236 typedef _T1 _T1_param;
2237 typedef _T2 _T2_param;
2238
2239 typedef typename remove_reference<_T1>::type& _T1_reference;
2240 typedef _T2& _T2_reference;
2241
2242 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2243 typedef const _T2& _T2_const_reference;
2244
Marshall Clowcd6ed542015-07-16 03:05:06 +00002245 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002247 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002249 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002251 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2252 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002253 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002254
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002255#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002256
2257 _LIBCPP_INLINE_VISIBILITY
2258 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2259 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2260 is_nothrow_copy_constructible<_T2>::value)
2261 : _T2(__p.second()), __first_(__p.first()) {}
2262
2263 _LIBCPP_INLINE_VISIBILITY
2264 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2265 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2266 is_nothrow_copy_assignable<_T2>::value)
2267 {
2268 _T2::operator=(__p.second());
2269 __first_ = __p.first();
2270 return *this;
2271 }
2272
Howard Hinnant61aa6012011-07-01 19:24:36 +00002273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002275 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2276 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002277 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002278
2279 _LIBCPP_INLINE_VISIBILITY
2280 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2281 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2282 is_nothrow_move_assignable<_T2>::value)
2283 {
2284 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2285 __first_ = _VSTD::move(__p.first());
2286 return *this;
2287 }
2288
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002289#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002290
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002291#ifndef _LIBCPP_HAS_NO_VARIADICS
2292
2293 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2294 _LIBCPP_INLINE_VISIBILITY
2295 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2296 tuple<_Args1...> __first_args,
2297 tuple<_Args2...> __second_args,
2298 __tuple_indices<_I1...>,
2299 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002300 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2301 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002302
2303 {}
2304
2305#endif // _LIBCPP_HAS_NO_VARIADICS
2306
Howard Hinnant1694d232011-05-28 14:41:13 +00002307 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2308 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002309
Howard Hinnant1694d232011-05-28 14:41:13 +00002310 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2311 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312
2313 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002314 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002315 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002317 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002318 swap(__first_, __x.__first_);
2319 }
2320};
2321
2322template <class _T1, class _T2>
2323class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2324 : private _T1,
2325 private _T2
2326{
2327public:
2328 typedef _T1 _T1_param;
2329 typedef _T2 _T2_param;
2330
2331 typedef _T1& _T1_reference;
2332 typedef _T2& _T2_reference;
2333
2334 typedef const _T1& _T1_const_reference;
2335 typedef const _T2& _T2_const_reference;
2336
2337 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2338 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002339 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002341 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002342 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002343 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002345#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002346
2347 _LIBCPP_INLINE_VISIBILITY
2348 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2349 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2350 is_nothrow_copy_constructible<_T2>::value)
2351 : _T1(__p.first()), _T2(__p.second()) {}
2352
2353 _LIBCPP_INLINE_VISIBILITY
2354 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2355 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2356 is_nothrow_copy_assignable<_T2>::value)
2357 {
2358 _T1::operator=(__p.first());
2359 _T2::operator=(__p.second());
2360 return *this;
2361 }
2362
Howard Hinnant61aa6012011-07-01 19:24:36 +00002363 _LIBCPP_INLINE_VISIBILITY
2364 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002365 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2366 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002367 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002368
2369 _LIBCPP_INLINE_VISIBILITY
2370 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2371 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2372 is_nothrow_move_assignable<_T2>::value)
2373 {
2374 _T1::operator=(_VSTD::move(__p.first()));
2375 _T2::operator=(_VSTD::move(__p.second()));
2376 return *this;
2377 }
2378
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002379#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002380
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002381#ifndef _LIBCPP_HAS_NO_VARIADICS
2382
2383 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2384 _LIBCPP_INLINE_VISIBILITY
2385 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2386 tuple<_Args1...> __first_args,
2387 tuple<_Args2...> __second_args,
2388 __tuple_indices<_I1...>,
2389 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002390 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2391 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002392 {}
2393
2394#endif // _LIBCPP_HAS_NO_VARIADICS
2395
Howard Hinnant1694d232011-05-28 14:41:13 +00002396 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2397 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398
Howard Hinnant1694d232011-05-28 14:41:13 +00002399 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2400 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002401
Howard Hinnantec3773c2011-12-01 20:21:04 +00002402 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002403 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002404 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405 {
2406 }
2407};
2408
2409template <class _T1, class _T2>
2410class __compressed_pair
2411 : private __libcpp_compressed_pair_imp<_T1, _T2>
2412{
2413 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2414public:
2415 typedef typename base::_T1_param _T1_param;
2416 typedef typename base::_T2_param _T2_param;
2417
2418 typedef typename base::_T1_reference _T1_reference;
2419 typedef typename base::_T2_reference _T2_reference;
2420
2421 typedef typename base::_T1_const_reference _T1_const_reference;
2422 typedef typename base::_T2_const_reference _T2_const_reference;
2423
2424 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002425 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002426 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002427 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002428 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002430 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002431
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002432#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002433
2434 _LIBCPP_INLINE_VISIBILITY
2435 __compressed_pair(const __compressed_pair& __p)
2436 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2437 is_nothrow_copy_constructible<_T2>::value)
2438 : base(__p) {}
2439
2440 _LIBCPP_INLINE_VISIBILITY
2441 __compressed_pair& operator=(const __compressed_pair& __p)
2442 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2443 is_nothrow_copy_assignable<_T2>::value)
2444 {
2445 base::operator=(__p);
2446 return *this;
2447 }
2448
Howard Hinnant61aa6012011-07-01 19:24:36 +00002449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002450 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002451 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2452 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002453 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002454
2455 _LIBCPP_INLINE_VISIBILITY
2456 __compressed_pair& operator=(__compressed_pair&& __p)
2457 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2458 is_nothrow_move_assignable<_T2>::value)
2459 {
2460 base::operator=(_VSTD::move(__p));
2461 return *this;
2462 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002463
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002464#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002465
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002466#ifndef _LIBCPP_HAS_NO_VARIADICS
2467
2468 template <class... _Args1, class... _Args2>
2469 _LIBCPP_INLINE_VISIBILITY
2470 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2471 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002472 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002473 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2474 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2475 {}
2476
2477#endif // _LIBCPP_HAS_NO_VARIADICS
2478
Howard Hinnant1694d232011-05-28 14:41:13 +00002479 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2480 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002481
Howard Hinnant1694d232011-05-28 14:41:13 +00002482 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2483 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002484
Howard Hinnant1694d232011-05-28 14:41:13 +00002485 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2486 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002487 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002488 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489};
2490
2491template <class _T1, class _T2>
2492inline _LIBCPP_INLINE_VISIBILITY
2493void
2494swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002495 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002496 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497 {__x.swap(__y);}
2498
Howard Hinnant57199402012-01-02 17:56:02 +00002499// __same_or_less_cv_qualified
2500
2501template <class _Ptr1, class _Ptr2,
2502 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2503 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2504 >::value
2505 >
2506struct __same_or_less_cv_qualified_imp
2507 : is_convertible<_Ptr1, _Ptr2> {};
2508
2509template <class _Ptr1, class _Ptr2>
2510struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2511 : false_type {};
2512
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002513template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2514 is_same<_Ptr1, _Ptr2>::value ||
2515 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002516struct __same_or_less_cv_qualified
2517 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2518
2519template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002520struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002521 : false_type {};
2522
2523// default_delete
2524
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002525template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002526struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527{
Howard Hinnant46e94932012-07-07 20:56:04 +00002528#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2529 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2530#else
2531 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2532#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533 template <class _Up>
2534 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002535 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2536 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537 {
2538 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002539 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540 delete __ptr;
2541 }
2542};
2543
2544template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002545struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546{
Howard Hinnant8e843502011-12-18 21:19:44 +00002547public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002548#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2549 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2550#else
2551 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2552#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002553 template <class _Up>
2554 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002555 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002556 template <class _Up>
2557 _LIBCPP_INLINE_VISIBILITY
2558 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002559 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560 {
2561 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clow61d4dd02016-02-25 16:50:51 +00002562 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563 delete [] __ptr;
2564 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565};
2566
2567template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002568class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569{
2570public:
2571 typedef _Tp element_type;
2572 typedef _Dp deleter_type;
2573 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2574private:
2575 __compressed_pair<pointer, deleter_type> __ptr_;
2576
Howard Hinnant57199402012-01-02 17:56:02 +00002577#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002578 unique_ptr(unique_ptr&);
2579 template <class _Up, class _Ep>
2580 unique_ptr(unique_ptr<_Up, _Ep>&);
2581 unique_ptr& operator=(unique_ptr&);
2582 template <class _Up, class _Ep>
2583 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002584#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585
2586 struct __nat {int __for_bool_;};
2587
2588 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2589 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2590public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002591 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002592 : __ptr_(pointer())
2593 {
2594 static_assert(!is_pointer<deleter_type>::value,
2595 "unique_ptr constructed with null function pointer deleter");
2596 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002597 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002598 : __ptr_(pointer())
2599 {
2600 static_assert(!is_pointer<deleter_type>::value,
2601 "unique_ptr constructed with null function pointer deleter");
2602 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002603 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002604 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 {
2606 static_assert(!is_pointer<deleter_type>::value,
2607 "unique_ptr constructed with null function pointer deleter");
2608 }
2609
Howard Hinnant73d21a42010-09-04 23:28:19 +00002610#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002611 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2612 is_reference<deleter_type>::value,
2613 deleter_type,
2614 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002615 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002616 : __ptr_(__p, __d) {}
2617
2618 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002619 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002620 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621 {
2622 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2623 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002624 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002625 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 template <class _Up, class _Ep>
2627 _LIBCPP_INLINE_VISIBILITY
2628 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2629 typename enable_if
2630 <
2631 !is_array<_Up>::value &&
2632 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2633 is_convertible<_Ep, deleter_type>::value &&
2634 (
2635 !is_reference<deleter_type>::value ||
2636 is_same<deleter_type, _Ep>::value
2637 ),
2638 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002639 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002640 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641
2642 template <class _Up>
2643 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2644 typename enable_if<
2645 is_convertible<_Up*, _Tp*>::value &&
2646 is_same<_Dp, default_delete<_Tp> >::value,
2647 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002648 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 : __ptr_(__p.release())
2650 {
2651 }
2652
Howard Hinnant1694d232011-05-28 14:41:13 +00002653 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002654 {
2655 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002656 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002657 return *this;
2658 }
2659
2660 template <class _Up, class _Ep>
2661 _LIBCPP_INLINE_VISIBILITY
2662 typename enable_if
2663 <
Howard Hinnant57199402012-01-02 17:56:02 +00002664 !is_array<_Up>::value &&
2665 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2666 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667 unique_ptr&
2668 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002669 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670 {
2671 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002672 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673 return *this;
2674 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002675#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676
2677 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2678 {
2679 return __rv<unique_ptr>(*this);
2680 }
2681
2682 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002683 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684
2685 template <class _Up, class _Ep>
Eric Fiselieraff153a2015-08-28 05:07:06 +00002686 _LIBCPP_INLINE_VISIBILITY
2687 typename enable_if<
2688 !is_array<_Up>::value &&
2689 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2690 is_assignable<deleter_type&, _Ep&>::value,
2691 unique_ptr&
2692 >::type
2693 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002694 {
2695 reset(__u.release());
Eric Fiselieraff153a2015-08-28 05:07:06 +00002696 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697 return *this;
2698 }
2699
2700 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002701 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702
2703 template <class _Up>
2704 _LIBCPP_INLINE_VISIBILITY
2705 typename enable_if<
2706 is_convertible<_Up*, _Tp*>::value &&
2707 is_same<_Dp, default_delete<_Tp> >::value,
2708 unique_ptr&
2709 >::type
2710 operator=(auto_ptr<_Up> __p)
2711 {reset(__p.release()); return *this;}
2712
Howard Hinnant73d21a42010-09-04 23:28:19 +00002713#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002714 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2715
Howard Hinnant1694d232011-05-28 14:41:13 +00002716 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717 {
2718 reset();
2719 return *this;
2720 }
2721
2722 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2723 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002724 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2725 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2726 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2727 {return __ptr_.second();}
2728 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2729 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002730 _LIBCPP_INLINE_VISIBILITY
2731 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2732 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733
Howard Hinnant1694d232011-05-28 14:41:13 +00002734 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735 {
2736 pointer __t = __ptr_.first();
2737 __ptr_.first() = pointer();
2738 return __t;
2739 }
2740
Howard Hinnant1694d232011-05-28 14:41:13 +00002741 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742 {
2743 pointer __tmp = __ptr_.first();
2744 __ptr_.first() = __p;
2745 if (__tmp)
2746 __ptr_.second()(__tmp);
2747 }
2748
Howard Hinnant1694d232011-05-28 14:41:13 +00002749 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2750 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751};
2752
2753template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002754class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755{
2756public:
2757 typedef _Tp element_type;
2758 typedef _Dp deleter_type;
2759 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2760private:
2761 __compressed_pair<pointer, deleter_type> __ptr_;
2762
Howard Hinnant57199402012-01-02 17:56:02 +00002763#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764 unique_ptr(unique_ptr&);
2765 template <class _Up>
2766 unique_ptr(unique_ptr<_Up>&);
2767 unique_ptr& operator=(unique_ptr&);
2768 template <class _Up>
2769 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002770#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002771
2772 struct __nat {int __for_bool_;};
2773
2774 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2775 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2776public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002777 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778 : __ptr_(pointer())
2779 {
2780 static_assert(!is_pointer<deleter_type>::value,
2781 "unique_ptr constructed with null function pointer deleter");
2782 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002783 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784 : __ptr_(pointer())
2785 {
2786 static_assert(!is_pointer<deleter_type>::value,
2787 "unique_ptr constructed with null function pointer deleter");
2788 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002789#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002790 template <class _Pp>
2791 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2792 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793 : __ptr_(__p)
2794 {
2795 static_assert(!is_pointer<deleter_type>::value,
2796 "unique_ptr constructed with null function pointer deleter");
2797 }
2798
Logan Chiene1678a12014-01-31 09:30:46 +00002799 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002800 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801 is_reference<deleter_type>::value,
2802 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002803 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2804 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002805 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806 : __ptr_(__p, __d) {}
2807
2808 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2809 is_reference<deleter_type>::value,
2810 deleter_type,
2811 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002812 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002813 : __ptr_(pointer(), __d) {}
2814
Logan Chiene1678a12014-01-31 09:30:46 +00002815 template <class _Pp>
2816 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2817 typename remove_reference<deleter_type>::type&& __d,
2818 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002819 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002820 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002821 {
2822 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2823 }
2824
2825 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002826 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002827 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002828 {
2829 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2830 }
2831
Howard Hinnant1694d232011-05-28 14:41:13 +00002832 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002833 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834
Howard Hinnant1694d232011-05-28 14:41:13 +00002835 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836 {
2837 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002838 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839 return *this;
2840 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002841
2842 template <class _Up, class _Ep>
2843 _LIBCPP_INLINE_VISIBILITY
2844 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2845 typename enable_if
2846 <
2847 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002848 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002849 && is_convertible<_Ep, deleter_type>::value &&
2850 (
2851 !is_reference<deleter_type>::value ||
2852 is_same<deleter_type, _Ep>::value
2853 ),
2854 __nat
2855 >::type = __nat()
2856 ) _NOEXCEPT
2857 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2858
2859
2860 template <class _Up, class _Ep>
2861 _LIBCPP_INLINE_VISIBILITY
2862 typename enable_if
2863 <
2864 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002865 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2866 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002867 unique_ptr&
2868 >::type
2869 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2870 {
2871 reset(__u.release());
2872 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2873 return *this;
2874 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002875#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002876
2877 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2878 : __ptr_(__p)
2879 {
2880 static_assert(!is_pointer<deleter_type>::value,
2881 "unique_ptr constructed with null function pointer deleter");
2882 }
2883
2884 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002885 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886
2887 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002888 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002889
2890 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2891 {
2892 return __rv<unique_ptr>(*this);
2893 }
2894
2895 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002896 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002897
2898 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2899 {
2900 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002901 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002902 return *this;
2903 }
2904
Howard Hinnant73d21a42010-09-04 23:28:19 +00002905#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2907
Howard Hinnant1694d232011-05-28 14:41:13 +00002908 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002909 {
2910 reset();
2911 return *this;
2912 }
2913
2914 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2915 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002916 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2917 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2918 {return __ptr_.second();}
2919 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2920 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002921 _LIBCPP_INLINE_VISIBILITY
2922 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2923 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002924
Howard Hinnant1694d232011-05-28 14:41:13 +00002925 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002926 {
2927 pointer __t = __ptr_.first();
2928 __ptr_.first() = pointer();
2929 return __t;
2930 }
2931
Logan Chiene1678a12014-01-31 09:30:46 +00002932 template <class _Pp>
2933 _LIBCPP_INLINE_VISIBILITY
2934 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2935 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936 {
2937 pointer __tmp = __ptr_.first();
2938 __ptr_.first() = __p;
2939 if (__tmp)
2940 __ptr_.second()(__tmp);
2941 }
Marshall Clow61d4dd02016-02-25 16:50:51 +00002942 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002943 {
2944 pointer __tmp = __ptr_.first();
2945 __ptr_.first() = nullptr;
2946 if (__tmp)
2947 __ptr_.second()(__tmp);
2948 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002949
2950 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2951private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002952
Howard Hinnant73d21a42010-09-04 23:28:19 +00002953#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954 template <class _Up>
2955 explicit unique_ptr(_Up);
2956 template <class _Up>
2957 unique_ptr(_Up __u,
2958 typename conditional<
2959 is_reference<deleter_type>::value,
2960 deleter_type,
2961 typename add_lvalue_reference<const deleter_type>::type>::type,
2962 typename enable_if
2963 <
2964 is_convertible<_Up, pointer>::value,
2965 __nat
2966 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002967#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002968};
2969
2970template <class _Tp, class _Dp>
2971inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier8f1e73d2016-04-21 23:38:59 +00002972typename enable_if<
2973 __is_swappable<_Dp>::value,
2974 void
2975>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002976swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002977
2978template <class _T1, class _D1, class _T2, class _D2>
2979inline _LIBCPP_INLINE_VISIBILITY
2980bool
2981operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2982
2983template <class _T1, class _D1, class _T2, class _D2>
2984inline _LIBCPP_INLINE_VISIBILITY
2985bool
2986operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2987
2988template <class _T1, class _D1, class _T2, class _D2>
2989inline _LIBCPP_INLINE_VISIBILITY
2990bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002991operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2992{
2993 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2994 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002995 typedef typename common_type<_P1, _P2>::type _Vp;
2996 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002997}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002998
2999template <class _T1, class _D1, class _T2, class _D2>
3000inline _LIBCPP_INLINE_VISIBILITY
3001bool
3002operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
3003
3004template <class _T1, class _D1, class _T2, class _D2>
3005inline _LIBCPP_INLINE_VISIBILITY
3006bool
3007operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
3008
3009template <class _T1, class _D1, class _T2, class _D2>
3010inline _LIBCPP_INLINE_VISIBILITY
3011bool
3012operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
3013
Howard Hinnant3fadda32012-02-21 21:02:58 +00003014template <class _T1, class _D1>
3015inline _LIBCPP_INLINE_VISIBILITY
3016bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003017operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003018{
3019 return !__x;
3020}
3021
3022template <class _T1, class _D1>
3023inline _LIBCPP_INLINE_VISIBILITY
3024bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003025operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003026{
3027 return !__x;
3028}
3029
3030template <class _T1, class _D1>
3031inline _LIBCPP_INLINE_VISIBILITY
3032bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003033operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003034{
3035 return static_cast<bool>(__x);
3036}
3037
3038template <class _T1, class _D1>
3039inline _LIBCPP_INLINE_VISIBILITY
3040bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003041operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003042{
3043 return static_cast<bool>(__x);
3044}
3045
3046template <class _T1, class _D1>
3047inline _LIBCPP_INLINE_VISIBILITY
3048bool
3049operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3050{
3051 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3052 return less<_P1>()(__x.get(), nullptr);
3053}
3054
3055template <class _T1, class _D1>
3056inline _LIBCPP_INLINE_VISIBILITY
3057bool
3058operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3059{
3060 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3061 return less<_P1>()(nullptr, __x.get());
3062}
3063
3064template <class _T1, class _D1>
3065inline _LIBCPP_INLINE_VISIBILITY
3066bool
3067operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3068{
3069 return nullptr < __x;
3070}
3071
3072template <class _T1, class _D1>
3073inline _LIBCPP_INLINE_VISIBILITY
3074bool
3075operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3076{
3077 return __x < nullptr;
3078}
3079
3080template <class _T1, class _D1>
3081inline _LIBCPP_INLINE_VISIBILITY
3082bool
3083operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3084{
3085 return !(nullptr < __x);
3086}
3087
3088template <class _T1, class _D1>
3089inline _LIBCPP_INLINE_VISIBILITY
3090bool
3091operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3092{
3093 return !(__x < nullptr);
3094}
3095
3096template <class _T1, class _D1>
3097inline _LIBCPP_INLINE_VISIBILITY
3098bool
3099operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3100{
3101 return !(__x < nullptr);
3102}
3103
3104template <class _T1, class _D1>
3105inline _LIBCPP_INLINE_VISIBILITY
3106bool
3107operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3108{
3109 return !(nullptr < __x);
3110}
3111
Howard Hinnant87073e42012-05-01 15:37:54 +00003112#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3113
3114template <class _Tp, class _Dp>
3115inline _LIBCPP_INLINE_VISIBILITY
3116unique_ptr<_Tp, _Dp>
3117move(unique_ptr<_Tp, _Dp>& __t)
3118{
3119 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3120}
3121
3122#endif
3123
Marshall Clowfd7481e2013-07-01 18:16:03 +00003124#if _LIBCPP_STD_VER > 11
3125
3126template<class _Tp>
3127struct __unique_if
3128{
3129 typedef unique_ptr<_Tp> __unique_single;
3130};
3131
3132template<class _Tp>
3133struct __unique_if<_Tp[]>
3134{
3135 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3136};
3137
3138template<class _Tp, size_t _Np>
3139struct __unique_if<_Tp[_Np]>
3140{
3141 typedef void __unique_array_known_bound;
3142};
3143
3144template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003145inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003146typename __unique_if<_Tp>::__unique_single
3147make_unique(_Args&&... __args)
3148{
3149 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3150}
3151
3152template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003153inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003154typename __unique_if<_Tp>::__unique_array_unknown_bound
3155make_unique(size_t __n)
3156{
3157 typedef typename remove_extent<_Tp>::type _Up;
3158 return unique_ptr<_Tp>(new _Up[__n]());
3159}
3160
3161template<class _Tp, class... _Args>
3162 typename __unique_if<_Tp>::__unique_array_known_bound
3163 make_unique(_Args&&...) = delete;
3164
3165#endif // _LIBCPP_STD_VER > 11
3166
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003167template <class _Size>
3168inline _LIBCPP_INLINE_VISIBILITY
3169_Size
3170__loadword(const void* __p)
3171{
3172 _Size __r;
3173 std::memcpy(&__r, __p, sizeof(__r));
3174 return __r;
3175}
3176
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003177// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3178// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3179// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003180template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003181struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003182
3183template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003184struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003185{
3186 _Size operator()(const void* __key, _Size __len);
3187};
3188
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003189// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003190template <class _Size>
3191_Size
Marshall Clow7a3731f2016-01-11 19:27:10 +00003192__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnant40c13d32011-12-05 00:08:45 +00003193{
3194 const _Size __m = 0x5bd1e995;
3195 const _Size __r = 24;
3196 _Size __h = __len;
3197 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3198 for (; __len >= 4; __data += 4, __len -= 4)
3199 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003200 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003201 __k *= __m;
3202 __k ^= __k >> __r;
3203 __k *= __m;
3204 __h *= __m;
3205 __h ^= __k;
3206 }
3207 switch (__len)
3208 {
3209 case 3:
3210 __h ^= __data[2] << 16;
3211 case 2:
3212 __h ^= __data[1] << 8;
3213 case 1:
3214 __h ^= __data[0];
3215 __h *= __m;
3216 }
3217 __h ^= __h >> 13;
3218 __h *= __m;
3219 __h ^= __h >> 15;
3220 return __h;
3221}
3222
3223template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003224struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003225{
3226 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003227
3228 private:
3229 // Some primes between 2^63 and 2^64.
3230 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3231 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3232 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3233 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3234
3235 static _Size __rotate(_Size __val, int __shift) {
3236 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3237 }
3238
3239 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3240 return (__val >> __shift) | (__val << (64 - __shift));
3241 }
3242
3243 static _Size __shift_mix(_Size __val) {
3244 return __val ^ (__val >> 47);
3245 }
3246
3247 static _Size __hash_len_16(_Size __u, _Size __v) {
3248 const _Size __mul = 0x9ddfea08eb382d69ULL;
3249 _Size __a = (__u ^ __v) * __mul;
3250 __a ^= (__a >> 47);
3251 _Size __b = (__v ^ __a) * __mul;
3252 __b ^= (__b >> 47);
3253 __b *= __mul;
3254 return __b;
3255 }
3256
3257 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3258 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003259 const _Size __a = __loadword<_Size>(__s);
3260 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003261 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3262 }
3263 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003264 const uint32_t __a = __loadword<uint32_t>(__s);
3265 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003266 return __hash_len_16(__len + (__a << 3), __b);
3267 }
3268 if (__len > 0) {
3269 const unsigned char __a = __s[0];
3270 const unsigned char __b = __s[__len >> 1];
3271 const unsigned char __c = __s[__len - 1];
3272 const uint32_t __y = static_cast<uint32_t>(__a) +
3273 (static_cast<uint32_t>(__b) << 8);
3274 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3275 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3276 }
3277 return __k2;
3278 }
3279
3280 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003281 const _Size __a = __loadword<_Size>(__s) * __k1;
3282 const _Size __b = __loadword<_Size>(__s + 8);
3283 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3284 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003285 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3286 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3287 }
3288
3289 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3290 // Callers do best to use "random-looking" values for a and b.
3291 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3292 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3293 __a += __w;
3294 __b = __rotate(__b + __a + __z, 21);
3295 const _Size __c = __a;
3296 __a += __x;
3297 __a += __y;
3298 __b += __rotate(__a, 44);
3299 return pair<_Size, _Size>(__a + __z, __b + __c);
3300 }
3301
3302 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3303 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3304 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003305 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3306 __loadword<_Size>(__s + 8),
3307 __loadword<_Size>(__s + 16),
3308 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003309 __a,
3310 __b);
3311 }
3312
3313 // Return an 8-byte hash for 33 to 64 bytes.
3314 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003315 _Size __z = __loadword<_Size>(__s + 24);
3316 _Size __a = __loadword<_Size>(__s) +
3317 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003318 _Size __b = __rotate(__a + __z, 52);
3319 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003320 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003321 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003322 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003323 _Size __vf = __a + __z;
3324 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003325 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3326 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003327 __b = __rotate(__a + __z, 52);
3328 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003329 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003330 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003331 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003332 _Size __wf = __a + __z;
3333 _Size __ws = __b + __rotate(__a, 31) + __c;
3334 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3335 return __shift_mix(__r * __k0 + __vs) * __k2;
3336 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003337};
3338
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003339// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003340template <class _Size>
3341_Size
Marshall Clow7a3731f2016-01-11 19:27:10 +00003342__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnant40c13d32011-12-05 00:08:45 +00003343{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003344 const char* __s = static_cast<const char*>(__key);
3345 if (__len <= 32) {
3346 if (__len <= 16) {
3347 return __hash_len_0_to_16(__s, __len);
3348 } else {
3349 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003350 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003351 } else if (__len <= 64) {
3352 return __hash_len_33_to_64(__s, __len);
3353 }
3354
3355 // For strings over 64 bytes we hash the end first, and then as we
3356 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003357 _Size __x = __loadword<_Size>(__s + __len - 40);
3358 _Size __y = __loadword<_Size>(__s + __len - 16) +
3359 __loadword<_Size>(__s + __len - 56);
3360 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3361 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003362 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3363 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003364 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003365
3366 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3367 __len = (__len - 1) & ~static_cast<_Size>(63);
3368 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003369 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3370 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003371 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003372 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003373 __z = __rotate(__z + __w.first, 33) * __k1;
3374 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3375 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003376 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003377 std::swap(__z, __x);
3378 __s += 64;
3379 __len -= 64;
3380 } while (__len != 0);
3381 return __hash_len_16(
3382 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3383 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003384}
3385
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003386template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3387struct __scalar_hash;
3388
3389template <class _Tp>
3390struct __scalar_hash<_Tp, 0>
3391 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003392{
Howard Hinnant82894812010-09-22 16:48:34 +00003393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003394 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003395 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003396 union
3397 {
3398 _Tp __t;
3399 size_t __a;
3400 } __u;
3401 __u.__a = 0;
3402 __u.__t = __v;
3403 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003404 }
3405};
3406
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003407template <class _Tp>
3408struct __scalar_hash<_Tp, 1>
3409 : public unary_function<_Tp, size_t>
3410{
3411 _LIBCPP_INLINE_VISIBILITY
3412 size_t operator()(_Tp __v) const _NOEXCEPT
3413 {
3414 union
3415 {
3416 _Tp __t;
3417 size_t __a;
3418 } __u;
3419 __u.__t = __v;
3420 return __u.__a;
3421 }
3422};
3423
3424template <class _Tp>
3425struct __scalar_hash<_Tp, 2>
3426 : public unary_function<_Tp, size_t>
3427{
3428 _LIBCPP_INLINE_VISIBILITY
3429 size_t operator()(_Tp __v) const _NOEXCEPT
3430 {
3431 union
3432 {
3433 _Tp __t;
3434 struct
3435 {
3436 size_t __a;
3437 size_t __b;
Eric Fiselier692177d2015-07-18 20:40:46 +00003438 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003439 } __u;
3440 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003441 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003442 }
3443};
3444
3445template <class _Tp>
3446struct __scalar_hash<_Tp, 3>
3447 : public unary_function<_Tp, size_t>
3448{
3449 _LIBCPP_INLINE_VISIBILITY
3450 size_t operator()(_Tp __v) const _NOEXCEPT
3451 {
3452 union
3453 {
3454 _Tp __t;
3455 struct
3456 {
3457 size_t __a;
3458 size_t __b;
3459 size_t __c;
Eric Fiselier692177d2015-07-18 20:40:46 +00003460 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003461 } __u;
3462 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003463 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003464 }
3465};
3466
3467template <class _Tp>
3468struct __scalar_hash<_Tp, 4>
3469 : public unary_function<_Tp, size_t>
3470{
3471 _LIBCPP_INLINE_VISIBILITY
3472 size_t operator()(_Tp __v) const _NOEXCEPT
3473 {
3474 union
3475 {
3476 _Tp __t;
3477 struct
3478 {
3479 size_t __a;
3480 size_t __b;
3481 size_t __c;
3482 size_t __d;
Eric Fiselier692177d2015-07-18 20:40:46 +00003483 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003484 } __u;
3485 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003486 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003487 }
3488};
3489
3490template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003491struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003492 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003493{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003494 _LIBCPP_INLINE_VISIBILITY
3495 size_t operator()(_Tp* __v) const _NOEXCEPT
3496 {
3497 union
3498 {
3499 _Tp* __t;
3500 size_t __a;
3501 } __u;
3502 __u.__t = __v;
3503 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3504 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003505};
3506
Howard Hinnant21aefc32010-06-03 16:42:57 +00003507template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003508struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003509{
3510 typedef unique_ptr<_Tp, _Dp> argument_type;
3511 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003513 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003514 {
3515 typedef typename argument_type::pointer pointer;
3516 return hash<pointer>()(__ptr.get());
3517 }
3518};
3519
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003520struct __destruct_n
3521{
3522private:
3523 size_t size;
3524
3525 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003526 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003527 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3528
3529 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003530 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003531 {}
3532
Howard Hinnant1694d232011-05-28 14:41:13 +00003533 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003534 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003535 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 {}
3537
Howard Hinnant1694d232011-05-28 14:41:13 +00003538 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003539 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003540 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003541 {}
3542public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003543 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3544 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003545
3546 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003547 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003548 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003549
3550 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003551 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003552 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553
3554 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003555 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003556 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003557};
3558
3559template <class _Alloc>
3560class __allocator_destructor
3561{
3562 typedef allocator_traits<_Alloc> __alloc_traits;
3563public:
3564 typedef typename __alloc_traits::pointer pointer;
3565 typedef typename __alloc_traits::size_type size_type;
3566private:
3567 _Alloc& __alloc_;
3568 size_type __s_;
3569public:
3570 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003571 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003574 void operator()(pointer __p) _NOEXCEPT
3575 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576};
3577
3578template <class _InputIterator, class _ForwardIterator>
3579_ForwardIterator
3580uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3581{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003583#ifndef _LIBCPP_NO_EXCEPTIONS
3584 _ForwardIterator __s = __r;
3585 try
3586 {
3587#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003588 for (; __f != __l; ++__f, (void) ++__r)
3589 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003590#ifndef _LIBCPP_NO_EXCEPTIONS
3591 }
3592 catch (...)
3593 {
3594 for (; __s != __r; ++__s)
3595 __s->~value_type();
3596 throw;
3597 }
3598#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599 return __r;
3600}
3601
3602template <class _InputIterator, class _Size, class _ForwardIterator>
3603_ForwardIterator
3604uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3605{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003606 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003607#ifndef _LIBCPP_NO_EXCEPTIONS
3608 _ForwardIterator __s = __r;
3609 try
3610 {
3611#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003612 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3613 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003614#ifndef _LIBCPP_NO_EXCEPTIONS
3615 }
3616 catch (...)
3617 {
3618 for (; __s != __r; ++__s)
3619 __s->~value_type();
3620 throw;
3621 }
3622#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623 return __r;
3624}
3625
3626template <class _ForwardIterator, class _Tp>
3627void
3628uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3629{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003631#ifndef _LIBCPP_NO_EXCEPTIONS
3632 _ForwardIterator __s = __f;
3633 try
3634 {
3635#endif
3636 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003637 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003638#ifndef _LIBCPP_NO_EXCEPTIONS
3639 }
3640 catch (...)
3641 {
3642 for (; __s != __f; ++__s)
3643 __s->~value_type();
3644 throw;
3645 }
3646#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647}
3648
3649template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003650_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003651uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3652{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003653 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003654#ifndef _LIBCPP_NO_EXCEPTIONS
3655 _ForwardIterator __s = __f;
3656 try
3657 {
3658#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003659 for (; __n > 0; ++__f, (void) --__n)
3660 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003661#ifndef _LIBCPP_NO_EXCEPTIONS
3662 }
3663 catch (...)
3664 {
3665 for (; __s != __f; ++__s)
3666 __s->~value_type();
3667 throw;
3668 }
3669#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003670 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003671}
3672
Howard Hinnant82894812010-09-22 16:48:34 +00003673class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674 : public std::exception
3675{
3676public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003677 virtual ~bad_weak_ptr() _NOEXCEPT;
3678 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003679};
3680
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003681template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003682
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003683class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003684{
3685 __shared_count(const __shared_count&);
3686 __shared_count& operator=(const __shared_count&);
3687
3688protected:
3689 long __shared_owners_;
3690 virtual ~__shared_count();
3691private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003692 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003693
3694public:
Howard Hinnant82894812010-09-22 16:48:34 +00003695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003696 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003697 : __shared_owners_(__refs) {}
3698
Howard Hinnant1694d232011-05-28 14:41:13 +00003699 void __add_shared() _NOEXCEPT;
3700 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003701 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc6e46692015-07-07 00:27:16 +00003702 long use_count() const _NOEXCEPT {
3703 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3704 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003705};
3706
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003707class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003708 : private __shared_count
3709{
3710 long __shared_weak_owners_;
3711
3712public:
Howard Hinnant82894812010-09-22 16:48:34 +00003713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003714 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003715 : __shared_count(__refs),
3716 __shared_weak_owners_(__refs) {}
3717protected:
3718 virtual ~__shared_weak_count();
3719
3720public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003721 void __add_shared() _NOEXCEPT;
3722 void __add_weak() _NOEXCEPT;
3723 void __release_shared() _NOEXCEPT;
3724 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003726 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3727 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003729 // Define the function out only if we build static libc++ without RTTI.
3730 // Otherwise we may break clients who need to compile their projects with
3731 // -fno-rtti and yet link against a libc++.dylib compiled
3732 // without -fno-rtti.
3733#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003734 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003735#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003737 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003738};
3739
3740template <class _Tp, class _Dp, class _Alloc>
3741class __shared_ptr_pointer
3742 : public __shared_weak_count
3743{
3744 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3745public:
Howard Hinnant82894812010-09-22 16:48:34 +00003746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003748 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749
Howard Hinnantd4444702010-08-11 17:04:31 +00003750#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003751 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003752#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003753
3754private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003755 virtual void __on_zero_shared() _NOEXCEPT;
3756 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757};
3758
Howard Hinnantd4444702010-08-11 17:04:31 +00003759#ifndef _LIBCPP_NO_RTTI
3760
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003761template <class _Tp, class _Dp, class _Alloc>
3762const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003763__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003764{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003765 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766}
3767
Howard Hinnant324bb032010-08-22 00:02:43 +00003768#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003769
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003770template <class _Tp, class _Dp, class _Alloc>
3771void
Howard Hinnant1694d232011-05-28 14:41:13 +00003772__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003773{
3774 __data_.first().second()(__data_.first().first());
3775 __data_.first().second().~_Dp();
3776}
3777
3778template <class _Tp, class _Dp, class _Alloc>
3779void
Howard Hinnant1694d232011-05-28 14:41:13 +00003780__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003781{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003782 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3783 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003784 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3785
Eric Fiselier8492cd82015-02-05 23:01:40 +00003786 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003788 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003789}
3790
3791template <class _Tp, class _Alloc>
3792class __shared_ptr_emplace
3793 : public __shared_weak_count
3794{
3795 __compressed_pair<_Alloc, _Tp> __data_;
3796public:
3797#ifndef _LIBCPP_HAS_NO_VARIADICS
3798
Howard Hinnant82894812010-09-22 16:48:34 +00003799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003800 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003801 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003802
3803 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003805 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003806 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3807 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003808
3809#else // _LIBCPP_HAS_NO_VARIADICS
3810
Howard Hinnant82894812010-09-22 16:48:34 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003812 __shared_ptr_emplace(_Alloc __a)
3813 : __data_(__a) {}
3814
3815 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3818 : __data_(__a, _Tp(__a0)) {}
3819
3820 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003822 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3823 : __data_(__a, _Tp(__a0, __a1)) {}
3824
3825 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3828 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3829
3830#endif // _LIBCPP_HAS_NO_VARIADICS
3831
3832private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003833 virtual void __on_zero_shared() _NOEXCEPT;
3834 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835public:
Howard Hinnant82894812010-09-22 16:48:34 +00003836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003837 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838};
3839
3840template <class _Tp, class _Alloc>
3841void
Howard Hinnant1694d232011-05-28 14:41:13 +00003842__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003843{
3844 __data_.second().~_Tp();
3845}
3846
3847template <class _Tp, class _Alloc>
3848void
Howard Hinnant1694d232011-05-28 14:41:13 +00003849__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003850{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003851 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3852 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003853 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003854 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003855 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003856 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003857}
3858
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003859template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003860
3861template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003862class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003863{
Howard Hinnant324bb032010-08-22 00:02:43 +00003864public:
3865 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003866private:
3867 element_type* __ptr_;
3868 __shared_weak_count* __cntrl_;
3869
3870 struct __nat {int __for_bool_;};
3871public:
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00003873 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00003875 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003876 template<class _Yp>
3877 explicit shared_ptr(_Yp* __p,
3878 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3879 template<class _Yp, class _Dp>
3880 shared_ptr(_Yp* __p, _Dp __d,
3881 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3882 template<class _Yp, class _Dp, class _Alloc>
3883 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3884 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003885 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3886 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003887 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003889 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003890 template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003893 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3894 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003895#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003897 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003898 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003899 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3900 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003901#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003902 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003903 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003904#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003905 template<class _Yp>
3906 shared_ptr(auto_ptr<_Yp>&& __r,
3907 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003908#else
Logan Chiene1678a12014-01-31 09:30:46 +00003909 template<class _Yp>
3910 shared_ptr(auto_ptr<_Yp> __r,
3911 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003912#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003913#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003914 template <class _Yp, class _Dp>
3915 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3916 typename enable_if
3917 <
3918 !is_lvalue_reference<_Dp>::value &&
3919 !is_array<_Yp>::value &&
3920 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3921 __nat
3922 >::type = __nat());
3923 template <class _Yp, class _Dp>
3924 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3925 typename enable_if
3926 <
3927 is_lvalue_reference<_Dp>::value &&
3928 !is_array<_Yp>::value &&
3929 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3930 __nat
3931 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003932#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003933 template <class _Yp, class _Dp>
3934 shared_ptr(unique_ptr<_Yp, _Dp>,
3935 typename enable_if
3936 <
3937 !is_lvalue_reference<_Dp>::value &&
3938 !is_array<_Yp>::value &&
3939 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3940 __nat
3941 >::type = __nat());
3942 template <class _Yp, class _Dp>
3943 shared_ptr(unique_ptr<_Yp, _Dp>,
3944 typename enable_if
3945 <
3946 is_lvalue_reference<_Dp>::value &&
3947 !is_array<_Yp>::value &&
3948 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3949 __nat
3950 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003951#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003952
3953 ~shared_ptr();
3954
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003956 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003957 template<class _Yp>
3958 typename enable_if
3959 <
3960 is_convertible<_Yp*, element_type*>::value,
3961 shared_ptr&
3962 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003964 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003965#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003967 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003968 template<class _Yp>
3969 typename enable_if
3970 <
3971 is_convertible<_Yp*, element_type*>::value,
3972 shared_ptr<_Tp>&
3973 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003975 operator=(shared_ptr<_Yp>&& __r);
3976 template<class _Yp>
Eric Fiselier566bcb42016-04-21 22:54:21 +00003977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003978 typename enable_if
3979 <
3980 !is_array<_Yp>::value &&
3981 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003982 shared_ptr
3983 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003984 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003985#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003986 template<class _Yp>
Eric Fiselier566bcb42016-04-21 22:54:21 +00003987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003988 typename enable_if
3989 <
3990 !is_array<_Yp>::value &&
3991 is_convertible<_Yp*, element_type*>::value,
3992 shared_ptr&
3993 >::type
3994 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003995#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003996 template <class _Yp, class _Dp>
3997 typename enable_if
3998 <
3999 !is_array<_Yp>::value &&
4000 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4001 shared_ptr&
4002 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00004003#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004005 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00004006#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov28c02db2015-12-09 22:32:36 +00004007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004008 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004009#endif
4010
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004012 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004014 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004015 template<class _Yp>
4016 typename enable_if
4017 <
4018 is_convertible<_Yp*, element_type*>::value,
4019 void
4020 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004022 reset(_Yp* __p);
4023 template<class _Yp, class _Dp>
4024 typename enable_if
4025 <
4026 is_convertible<_Yp*, element_type*>::value,
4027 void
4028 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004030 reset(_Yp* __p, _Dp __d);
4031 template<class _Yp, class _Dp, class _Alloc>
4032 typename enable_if
4033 <
4034 is_convertible<_Yp*, element_type*>::value,
4035 void
4036 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004038 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004039
Howard Hinnant82894812010-09-22 16:48:34 +00004040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004041 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004043 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4044 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004046 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004048 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004050 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004052 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00004053 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004055 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004056 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004057 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004059 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004060 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004061 _LIBCPP_INLINE_VISIBILITY
4062 bool
4063 __owner_equivalent(const shared_ptr& __p) const
4064 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004065
Howard Hinnantd4444702010-08-11 17:04:31 +00004066#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004067 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004069 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004070 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004071#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004072
4073#ifndef _LIBCPP_HAS_NO_VARIADICS
4074
4075 template<class ..._Args>
4076 static
4077 shared_ptr<_Tp>
4078 make_shared(_Args&& ...__args);
4079
4080 template<class _Alloc, class ..._Args>
4081 static
4082 shared_ptr<_Tp>
4083 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4084
4085#else // _LIBCPP_HAS_NO_VARIADICS
4086
4087 static shared_ptr<_Tp> make_shared();
4088
4089 template<class _A0>
4090 static shared_ptr<_Tp> make_shared(_A0&);
4091
4092 template<class _A0, class _A1>
4093 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4094
4095 template<class _A0, class _A1, class _A2>
4096 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4097
4098 template<class _Alloc>
4099 static shared_ptr<_Tp>
4100 allocate_shared(const _Alloc& __a);
4101
4102 template<class _Alloc, class _A0>
4103 static shared_ptr<_Tp>
4104 allocate_shared(const _Alloc& __a, _A0& __a0);
4105
4106 template<class _Alloc, class _A0, class _A1>
4107 static shared_ptr<_Tp>
4108 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4109
4110 template<class _Alloc, class _A0, class _A1, class _A2>
4111 static shared_ptr<_Tp>
4112 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4113
4114#endif // _LIBCPP_HAS_NO_VARIADICS
4115
4116private:
4117
4118 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004120 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004121 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004122 {
4123 if (__e)
Marshall Clowc4113372015-06-19 15:54:13 +00004124 {
4125 __e->__weak_this_.__ptr_ = const_cast<_Yp*>(static_cast<const _Yp*>(__e));
4126 __e->__weak_this_.__cntrl_ = __cntrl_;
Marshall Clow46d06b92015-06-19 19:32:06 +00004127 __cntrl_->__add_weak();
Marshall Clowc4113372015-06-19 15:54:13 +00004128 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004129 }
4130
Howard Hinnant82894812010-09-22 16:48:34 +00004131 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004132 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004133
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004134 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4135 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004136};
4137
4138template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004139inline
Howard Hinnant46e94932012-07-07 20:56:04 +00004140_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004141shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004142 : __ptr_(0),
4143 __cntrl_(0)
4144{
4145}
4146
4147template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004148inline
Howard Hinnant46e94932012-07-07 20:56:04 +00004149_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004150shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004151 : __ptr_(0),
4152 __cntrl_(0)
4153{
4154}
4155
4156template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004157template<class _Yp>
4158shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4159 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004160 : __ptr_(__p)
4161{
4162 unique_ptr<_Yp> __hold(__p);
4163 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4164 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4165 __hold.release();
4166 __enable_weak_this(__p);
4167}
4168
4169template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004170template<class _Yp, class _Dp>
4171shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4172 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004173 : __ptr_(__p)
4174{
4175#ifndef _LIBCPP_NO_EXCEPTIONS
4176 try
4177 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004178#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004179 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4180 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4181 __enable_weak_this(__p);
4182#ifndef _LIBCPP_NO_EXCEPTIONS
4183 }
4184 catch (...)
4185 {
4186 __d(__p);
4187 throw;
4188 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004189#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004190}
4191
4192template<class _Tp>
4193template<class _Dp>
4194shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4195 : __ptr_(0)
4196{
4197#ifndef _LIBCPP_NO_EXCEPTIONS
4198 try
4199 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004200#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4202 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4203#ifndef _LIBCPP_NO_EXCEPTIONS
4204 }
4205 catch (...)
4206 {
4207 __d(__p);
4208 throw;
4209 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004210#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004211}
4212
4213template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004214template<class _Yp, class _Dp, class _Alloc>
4215shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4216 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004217 : __ptr_(__p)
4218{
4219#ifndef _LIBCPP_NO_EXCEPTIONS
4220 try
4221 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004222#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004223 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004224 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004225 typedef __allocator_destructor<_A2> _D2;
4226 _A2 __a2(__a);
4227 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004228 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4229 _CntrlBlk(__p, __d, __a);
4230 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004231 __enable_weak_this(__p);
4232#ifndef _LIBCPP_NO_EXCEPTIONS
4233 }
4234 catch (...)
4235 {
4236 __d(__p);
4237 throw;
4238 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004239#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004240}
4241
4242template<class _Tp>
4243template<class _Dp, class _Alloc>
4244shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4245 : __ptr_(0)
4246{
4247#ifndef _LIBCPP_NO_EXCEPTIONS
4248 try
4249 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004250#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004251 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004252 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004253 typedef __allocator_destructor<_A2> _D2;
4254 _A2 __a2(__a);
4255 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004256 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4257 _CntrlBlk(__p, __d, __a);
4258 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004259#ifndef _LIBCPP_NO_EXCEPTIONS
4260 }
4261 catch (...)
4262 {
4263 __d(__p);
4264 throw;
4265 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004266#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004267}
4268
4269template<class _Tp>
4270template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004271inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004272shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004273 : __ptr_(__p),
4274 __cntrl_(__r.__cntrl_)
4275{
4276 if (__cntrl_)
4277 __cntrl_->__add_shared();
4278}
4279
4280template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004281inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004282shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004283 : __ptr_(__r.__ptr_),
4284 __cntrl_(__r.__cntrl_)
4285{
4286 if (__cntrl_)
4287 __cntrl_->__add_shared();
4288}
4289
4290template<class _Tp>
4291template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004292inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004293shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4294 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004295 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004296 : __ptr_(__r.__ptr_),
4297 __cntrl_(__r.__cntrl_)
4298{
4299 if (__cntrl_)
4300 __cntrl_->__add_shared();
4301}
4302
Howard Hinnant73d21a42010-09-04 23:28:19 +00004303#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004304
4305template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004306inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004307shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004308 : __ptr_(__r.__ptr_),
4309 __cntrl_(__r.__cntrl_)
4310{
4311 __r.__ptr_ = 0;
4312 __r.__cntrl_ = 0;
4313}
4314
4315template<class _Tp>
4316template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004317inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004318shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4319 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004320 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004321 : __ptr_(__r.__ptr_),
4322 __cntrl_(__r.__cntrl_)
4323{
4324 __r.__ptr_ = 0;
4325 __r.__cntrl_ = 0;
4326}
4327
Howard Hinnant73d21a42010-09-04 23:28:19 +00004328#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004329
4330template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004331template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004332#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004333shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004334#else
Logan Chiene1678a12014-01-31 09:30:46 +00004335shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004336#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004337 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004338 : __ptr_(__r.get())
4339{
4340 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4341 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4342 __enable_weak_this(__r.get());
4343 __r.release();
4344}
4345
4346template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004347template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004348#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004349shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4350#else
4351shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4352#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004353 typename enable_if
4354 <
4355 !is_lvalue_reference<_Dp>::value &&
4356 !is_array<_Yp>::value &&
4357 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4358 __nat
4359 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004360 : __ptr_(__r.get())
4361{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004362#if _LIBCPP_STD_VER > 11
4363 if (__ptr_ == nullptr)
4364 __cntrl_ = nullptr;
4365 else
4366#endif
4367 {
4368 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4369 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4370 __enable_weak_this(__r.get());
4371 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004372 __r.release();
4373}
4374
4375template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004376template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004377#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004378shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4379#else
4380shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4381#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004382 typename enable_if
4383 <
4384 is_lvalue_reference<_Dp>::value &&
4385 !is_array<_Yp>::value &&
4386 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4387 __nat
4388 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004389 : __ptr_(__r.get())
4390{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004391#if _LIBCPP_STD_VER > 11
4392 if (__ptr_ == nullptr)
4393 __cntrl_ = nullptr;
4394 else
4395#endif
4396 {
4397 typedef __shared_ptr_pointer<_Yp*,
4398 reference_wrapper<typename remove_reference<_Dp>::type>,
4399 allocator<_Yp> > _CntrlBlk;
4400 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4401 __enable_weak_this(__r.get());
4402 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004403 __r.release();
4404}
4405
4406#ifndef _LIBCPP_HAS_NO_VARIADICS
4407
4408template<class _Tp>
4409template<class ..._Args>
4410shared_ptr<_Tp>
4411shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4412{
4413 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4414 typedef allocator<_CntrlBlk> _A2;
4415 typedef __allocator_destructor<_A2> _D2;
4416 _A2 __a2;
4417 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004418 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004419 shared_ptr<_Tp> __r;
4420 __r.__ptr_ = __hold2.get()->get();
4421 __r.__cntrl_ = __hold2.release();
4422 __r.__enable_weak_this(__r.__ptr_);
4423 return __r;
4424}
4425
4426template<class _Tp>
4427template<class _Alloc, class ..._Args>
4428shared_ptr<_Tp>
4429shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4430{
4431 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004432 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004433 typedef __allocator_destructor<_A2> _D2;
4434 _A2 __a2(__a);
4435 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004436 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4437 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004438 shared_ptr<_Tp> __r;
4439 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004440 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004441 __r.__enable_weak_this(__r.__ptr_);
4442 return __r;
4443}
4444
4445#else // _LIBCPP_HAS_NO_VARIADICS
4446
4447template<class _Tp>
4448shared_ptr<_Tp>
4449shared_ptr<_Tp>::make_shared()
4450{
4451 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4452 typedef allocator<_CntrlBlk> _Alloc2;
4453 typedef __allocator_destructor<_Alloc2> _D2;
4454 _Alloc2 __alloc2;
4455 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4456 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4457 shared_ptr<_Tp> __r;
4458 __r.__ptr_ = __hold2.get()->get();
4459 __r.__cntrl_ = __hold2.release();
4460 __r.__enable_weak_this(__r.__ptr_);
4461 return __r;
4462}
4463
4464template<class _Tp>
4465template<class _A0>
4466shared_ptr<_Tp>
4467shared_ptr<_Tp>::make_shared(_A0& __a0)
4468{
4469 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4470 typedef allocator<_CntrlBlk> _Alloc2;
4471 typedef __allocator_destructor<_Alloc2> _D2;
4472 _Alloc2 __alloc2;
4473 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4474 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4475 shared_ptr<_Tp> __r;
4476 __r.__ptr_ = __hold2.get()->get();
4477 __r.__cntrl_ = __hold2.release();
4478 __r.__enable_weak_this(__r.__ptr_);
4479 return __r;
4480}
4481
4482template<class _Tp>
4483template<class _A0, class _A1>
4484shared_ptr<_Tp>
4485shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4486{
4487 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4488 typedef allocator<_CntrlBlk> _Alloc2;
4489 typedef __allocator_destructor<_Alloc2> _D2;
4490 _Alloc2 __alloc2;
4491 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4492 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4493 shared_ptr<_Tp> __r;
4494 __r.__ptr_ = __hold2.get()->get();
4495 __r.__cntrl_ = __hold2.release();
4496 __r.__enable_weak_this(__r.__ptr_);
4497 return __r;
4498}
4499
4500template<class _Tp>
4501template<class _A0, class _A1, class _A2>
4502shared_ptr<_Tp>
4503shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4504{
4505 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4506 typedef allocator<_CntrlBlk> _Alloc2;
4507 typedef __allocator_destructor<_Alloc2> _D2;
4508 _Alloc2 __alloc2;
4509 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4510 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4511 shared_ptr<_Tp> __r;
4512 __r.__ptr_ = __hold2.get()->get();
4513 __r.__cntrl_ = __hold2.release();
4514 __r.__enable_weak_this(__r.__ptr_);
4515 return __r;
4516}
4517
4518template<class _Tp>
4519template<class _Alloc>
4520shared_ptr<_Tp>
4521shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4522{
4523 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004524 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004525 typedef __allocator_destructor<_Alloc2> _D2;
4526 _Alloc2 __alloc2(__a);
4527 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004528 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4529 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004530 shared_ptr<_Tp> __r;
4531 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004532 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004533 __r.__enable_weak_this(__r.__ptr_);
4534 return __r;
4535}
4536
4537template<class _Tp>
4538template<class _Alloc, class _A0>
4539shared_ptr<_Tp>
4540shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4541{
4542 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004543 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004544 typedef __allocator_destructor<_Alloc2> _D2;
4545 _Alloc2 __alloc2(__a);
4546 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004547 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4548 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004549 shared_ptr<_Tp> __r;
4550 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004551 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004552 __r.__enable_weak_this(__r.__ptr_);
4553 return __r;
4554}
4555
4556template<class _Tp>
4557template<class _Alloc, class _A0, class _A1>
4558shared_ptr<_Tp>
4559shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4560{
4561 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004562 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004563 typedef __allocator_destructor<_Alloc2> _D2;
4564 _Alloc2 __alloc2(__a);
4565 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004566 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4567 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004568 shared_ptr<_Tp> __r;
4569 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004570 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004571 __r.__enable_weak_this(__r.__ptr_);
4572 return __r;
4573}
4574
4575template<class _Tp>
4576template<class _Alloc, class _A0, class _A1, class _A2>
4577shared_ptr<_Tp>
4578shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4579{
4580 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004581 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004582 typedef __allocator_destructor<_Alloc2> _D2;
4583 _Alloc2 __alloc2(__a);
4584 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004585 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4586 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004587 shared_ptr<_Tp> __r;
4588 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004589 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004590 __r.__enable_weak_this(__r.__ptr_);
4591 return __r;
4592}
4593
4594#endif // _LIBCPP_HAS_NO_VARIADICS
4595
4596template<class _Tp>
4597shared_ptr<_Tp>::~shared_ptr()
4598{
4599 if (__cntrl_)
4600 __cntrl_->__release_shared();
4601}
4602
4603template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004604inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004605shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004606shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004607{
4608 shared_ptr(__r).swap(*this);
4609 return *this;
4610}
4611
4612template<class _Tp>
4613template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004614inline
Howard Hinnant57199402012-01-02 17:56:02 +00004615typename enable_if
4616<
4617 is_convertible<_Yp*, _Tp*>::value,
4618 shared_ptr<_Tp>&
4619>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004620shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004621{
4622 shared_ptr(__r).swap(*this);
4623 return *this;
4624}
4625
Howard Hinnant73d21a42010-09-04 23:28:19 +00004626#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004627
4628template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004629inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004630shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004631shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004632{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004633 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004634 return *this;
4635}
4636
4637template<class _Tp>
4638template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004639inline
Howard Hinnant57199402012-01-02 17:56:02 +00004640typename enable_if
4641<
4642 is_convertible<_Yp*, _Tp*>::value,
4643 shared_ptr<_Tp>&
4644>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4646{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004647 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004648 return *this;
4649}
4650
4651template<class _Tp>
4652template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004653inline
Howard Hinnant57199402012-01-02 17:56:02 +00004654typename enable_if
4655<
4656 !is_array<_Yp>::value &&
4657 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004658 shared_ptr<_Tp>
4659>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004660shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4661{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004662 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004663 return *this;
4664}
4665
4666template<class _Tp>
4667template <class _Yp, class _Dp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004668inline
Howard Hinnant57199402012-01-02 17:56:02 +00004669typename enable_if
4670<
4671 !is_array<_Yp>::value &&
4672 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4673 shared_ptr<_Tp>&
4674>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004675shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4676{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004677 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004678 return *this;
4679}
4680
Howard Hinnant73d21a42010-09-04 23:28:19 +00004681#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004682
4683template<class _Tp>
4684template<class _Yp>
4685inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004686typename enable_if
4687<
4688 !is_array<_Yp>::value &&
4689 is_convertible<_Yp*, _Tp*>::value,
4690 shared_ptr<_Tp>&
4691>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004692shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004693{
4694 shared_ptr(__r).swap(*this);
4695 return *this;
4696}
4697
4698template<class _Tp>
4699template <class _Yp, class _Dp>
4700inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004701typename enable_if
4702<
4703 !is_array<_Yp>::value &&
4704 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4705 shared_ptr<_Tp>&
4706>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004707shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4708{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004709 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004710 return *this;
4711}
4712
Howard Hinnant73d21a42010-09-04 23:28:19 +00004713#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004714
4715template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004716inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004717void
Howard Hinnant1694d232011-05-28 14:41:13 +00004718shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004719{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004720 _VSTD::swap(__ptr_, __r.__ptr_);
4721 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004722}
4723
4724template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004725inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004726void
Howard Hinnant1694d232011-05-28 14:41:13 +00004727shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004728{
4729 shared_ptr().swap(*this);
4730}
4731
4732template<class _Tp>
4733template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004734inline
Howard Hinnant57199402012-01-02 17:56:02 +00004735typename enable_if
4736<
4737 is_convertible<_Yp*, _Tp*>::value,
4738 void
4739>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004740shared_ptr<_Tp>::reset(_Yp* __p)
4741{
4742 shared_ptr(__p).swap(*this);
4743}
4744
4745template<class _Tp>
4746template<class _Yp, class _Dp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004747inline
Howard Hinnant57199402012-01-02 17:56:02 +00004748typename enable_if
4749<
4750 is_convertible<_Yp*, _Tp*>::value,
4751 void
4752>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004753shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4754{
4755 shared_ptr(__p, __d).swap(*this);
4756}
4757
4758template<class _Tp>
4759template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004760inline
Howard Hinnant57199402012-01-02 17:56:02 +00004761typename enable_if
4762<
4763 is_convertible<_Yp*, _Tp*>::value,
4764 void
4765>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004766shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4767{
4768 shared_ptr(__p, __d, __a).swap(*this);
4769}
4770
4771#ifndef _LIBCPP_HAS_NO_VARIADICS
4772
Howard Hinnant324bb032010-08-22 00:02:43 +00004773template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004774inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004775typename enable_if
4776<
4777 !is_array<_Tp>::value,
4778 shared_ptr<_Tp>
4779>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004780make_shared(_Args&& ...__args)
4781{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004782 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004783}
4784
Howard Hinnant324bb032010-08-22 00:02:43 +00004785template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004786inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004787typename enable_if
4788<
4789 !is_array<_Tp>::value,
4790 shared_ptr<_Tp>
4791>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004792allocate_shared(const _Alloc& __a, _Args&& ...__args)
4793{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004794 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004795}
4796
4797#else // _LIBCPP_HAS_NO_VARIADICS
4798
4799template<class _Tp>
4800inline _LIBCPP_INLINE_VISIBILITY
4801shared_ptr<_Tp>
4802make_shared()
4803{
4804 return shared_ptr<_Tp>::make_shared();
4805}
4806
4807template<class _Tp, class _A0>
4808inline _LIBCPP_INLINE_VISIBILITY
4809shared_ptr<_Tp>
4810make_shared(_A0& __a0)
4811{
4812 return shared_ptr<_Tp>::make_shared(__a0);
4813}
4814
4815template<class _Tp, class _A0, class _A1>
4816inline _LIBCPP_INLINE_VISIBILITY
4817shared_ptr<_Tp>
4818make_shared(_A0& __a0, _A1& __a1)
4819{
4820 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4821}
4822
Howard Hinnant324bb032010-08-22 00:02:43 +00004823template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004824inline _LIBCPP_INLINE_VISIBILITY
4825shared_ptr<_Tp>
4826make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4827{
4828 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4829}
4830
4831template<class _Tp, class _Alloc>
4832inline _LIBCPP_INLINE_VISIBILITY
4833shared_ptr<_Tp>
4834allocate_shared(const _Alloc& __a)
4835{
4836 return shared_ptr<_Tp>::allocate_shared(__a);
4837}
4838
4839template<class _Tp, class _Alloc, class _A0>
4840inline _LIBCPP_INLINE_VISIBILITY
4841shared_ptr<_Tp>
4842allocate_shared(const _Alloc& __a, _A0& __a0)
4843{
4844 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4845}
4846
4847template<class _Tp, class _Alloc, class _A0, class _A1>
4848inline _LIBCPP_INLINE_VISIBILITY
4849shared_ptr<_Tp>
4850allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4851{
4852 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4853}
4854
4855template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4856inline _LIBCPP_INLINE_VISIBILITY
4857shared_ptr<_Tp>
4858allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4859{
4860 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4861}
4862
4863#endif // _LIBCPP_HAS_NO_VARIADICS
4864
4865template<class _Tp, class _Up>
4866inline _LIBCPP_INLINE_VISIBILITY
4867bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004868operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004869{
4870 return __x.get() == __y.get();
4871}
4872
4873template<class _Tp, class _Up>
4874inline _LIBCPP_INLINE_VISIBILITY
4875bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004876operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004877{
4878 return !(__x == __y);
4879}
4880
4881template<class _Tp, class _Up>
4882inline _LIBCPP_INLINE_VISIBILITY
4883bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004884operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004885{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004886 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4887 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004888}
4889
4890template<class _Tp, class _Up>
4891inline _LIBCPP_INLINE_VISIBILITY
4892bool
4893operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4894{
4895 return __y < __x;
4896}
4897
4898template<class _Tp, class _Up>
4899inline _LIBCPP_INLINE_VISIBILITY
4900bool
4901operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4902{
4903 return !(__y < __x);
4904}
4905
4906template<class _Tp, class _Up>
4907inline _LIBCPP_INLINE_VISIBILITY
4908bool
4909operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4910{
4911 return !(__x < __y);
4912}
4913
4914template<class _Tp>
4915inline _LIBCPP_INLINE_VISIBILITY
4916bool
4917operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4918{
4919 return !__x;
4920}
4921
4922template<class _Tp>
4923inline _LIBCPP_INLINE_VISIBILITY
4924bool
4925operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4926{
4927 return !__x;
4928}
4929
4930template<class _Tp>
4931inline _LIBCPP_INLINE_VISIBILITY
4932bool
4933operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4934{
4935 return static_cast<bool>(__x);
4936}
4937
4938template<class _Tp>
4939inline _LIBCPP_INLINE_VISIBILITY
4940bool
4941operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4942{
4943 return static_cast<bool>(__x);
4944}
4945
4946template<class _Tp>
4947inline _LIBCPP_INLINE_VISIBILITY
4948bool
4949operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4950{
4951 return less<_Tp*>()(__x.get(), nullptr);
4952}
4953
4954template<class _Tp>
4955inline _LIBCPP_INLINE_VISIBILITY
4956bool
4957operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4958{
4959 return less<_Tp*>()(nullptr, __x.get());
4960}
4961
4962template<class _Tp>
4963inline _LIBCPP_INLINE_VISIBILITY
4964bool
4965operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4966{
4967 return nullptr < __x;
4968}
4969
4970template<class _Tp>
4971inline _LIBCPP_INLINE_VISIBILITY
4972bool
4973operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4974{
4975 return __x < nullptr;
4976}
4977
4978template<class _Tp>
4979inline _LIBCPP_INLINE_VISIBILITY
4980bool
4981operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4982{
4983 return !(nullptr < __x);
4984}
4985
4986template<class _Tp>
4987inline _LIBCPP_INLINE_VISIBILITY
4988bool
4989operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4990{
4991 return !(__x < nullptr);
4992}
4993
4994template<class _Tp>
4995inline _LIBCPP_INLINE_VISIBILITY
4996bool
4997operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4998{
4999 return !(__x < nullptr);
5000}
5001
5002template<class _Tp>
5003inline _LIBCPP_INLINE_VISIBILITY
5004bool
5005operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5006{
5007 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005008}
5009
5010template<class _Tp>
5011inline _LIBCPP_INLINE_VISIBILITY
5012void
Howard Hinnant1694d232011-05-28 14:41:13 +00005013swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005014{
5015 __x.swap(__y);
5016}
5017
5018template<class _Tp, class _Up>
5019inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005020typename enable_if
5021<
5022 !is_array<_Tp>::value && !is_array<_Up>::value,
5023 shared_ptr<_Tp>
5024>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005025static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005026{
5027 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5028}
5029
5030template<class _Tp, class _Up>
5031inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005032typename enable_if
5033<
5034 !is_array<_Tp>::value && !is_array<_Up>::value,
5035 shared_ptr<_Tp>
5036>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005037dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005038{
5039 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5040 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5041}
5042
5043template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00005044typename enable_if
5045<
5046 is_array<_Tp>::value == is_array<_Up>::value,
5047 shared_ptr<_Tp>
5048>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005049const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005050{
Howard Hinnant57199402012-01-02 17:56:02 +00005051 typedef typename remove_extent<_Tp>::type _RTp;
5052 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005053}
5054
Howard Hinnantd4444702010-08-11 17:04:31 +00005055#ifndef _LIBCPP_NO_RTTI
5056
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005057template<class _Dp, class _Tp>
5058inline _LIBCPP_INLINE_VISIBILITY
5059_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00005060get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005061{
5062 return __p.template __get_deleter<_Dp>();
5063}
5064
Howard Hinnant324bb032010-08-22 00:02:43 +00005065#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00005066
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005067template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005068class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005069{
Howard Hinnant324bb032010-08-22 00:02:43 +00005070public:
5071 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005072private:
5073 element_type* __ptr_;
5074 __shared_weak_count* __cntrl_;
5075
Howard Hinnant324bb032010-08-22 00:02:43 +00005076public:
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005078 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005079 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005080 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5081 _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005083 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005084 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005085 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5086 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005087
Howard Hinnant57199402012-01-02 17:56:02 +00005088#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005090 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005091 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant57199402012-01-02 17:56:02 +00005092 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5093 _NOEXCEPT;
5094#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005095 ~weak_ptr();
5096
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005098 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005099 template<class _Yp>
5100 typename enable_if
5101 <
5102 is_convertible<_Yp*, element_type*>::value,
5103 weak_ptr&
5104 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005106 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5107
5108#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5109
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005111 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5112 template<class _Yp>
5113 typename enable_if
5114 <
5115 is_convertible<_Yp*, element_type*>::value,
5116 weak_ptr&
5117 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005119 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5120
5121#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5122
5123 template<class _Yp>
5124 typename enable_if
5125 <
5126 is_convertible<_Yp*, element_type*>::value,
5127 weak_ptr&
5128 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005130 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005131
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005133 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005135 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005136
Howard Hinnant82894812010-09-22 16:48:34 +00005137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005138 long use_count() const _NOEXCEPT
5139 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005141 bool expired() const _NOEXCEPT
5142 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5143 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005144 template<class _Up>
5145 _LIBCPP_INLINE_VISIBILITY
5146 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005147 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005148 template<class _Up>
5149 _LIBCPP_INLINE_VISIBILITY
5150 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005151 {return __cntrl_ < __r.__cntrl_;}
5152
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005153 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5154 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005155};
5156
5157template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005158inline
Howard Hinnant46e94932012-07-07 20:56:04 +00005159_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005160weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005161 : __ptr_(0),
5162 __cntrl_(0)
5163{
5164}
5165
5166template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005167inline
Howard Hinnant1694d232011-05-28 14:41:13 +00005168weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005169 : __ptr_(__r.__ptr_),
5170 __cntrl_(__r.__cntrl_)
5171{
5172 if (__cntrl_)
5173 __cntrl_->__add_weak();
5174}
5175
5176template<class _Tp>
5177template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005178inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005179weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005180 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005181 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005182 : __ptr_(__r.__ptr_),
5183 __cntrl_(__r.__cntrl_)
5184{
5185 if (__cntrl_)
5186 __cntrl_->__add_weak();
5187}
5188
5189template<class _Tp>
5190template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005191inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005192weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005193 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005194 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005195 : __ptr_(__r.__ptr_),
5196 __cntrl_(__r.__cntrl_)
5197{
5198 if (__cntrl_)
5199 __cntrl_->__add_weak();
5200}
5201
Howard Hinnant57199402012-01-02 17:56:02 +00005202#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5203
5204template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005205inline
Howard Hinnant57199402012-01-02 17:56:02 +00005206weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5207 : __ptr_(__r.__ptr_),
5208 __cntrl_(__r.__cntrl_)
5209{
5210 __r.__ptr_ = 0;
5211 __r.__cntrl_ = 0;
5212}
5213
5214template<class _Tp>
5215template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005216inline
Howard Hinnant57199402012-01-02 17:56:02 +00005217weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5218 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5219 _NOEXCEPT
5220 : __ptr_(__r.__ptr_),
5221 __cntrl_(__r.__cntrl_)
5222{
5223 __r.__ptr_ = 0;
5224 __r.__cntrl_ = 0;
5225}
5226
5227#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5228
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005229template<class _Tp>
5230weak_ptr<_Tp>::~weak_ptr()
5231{
5232 if (__cntrl_)
5233 __cntrl_->__release_weak();
5234}
5235
5236template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005237inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005238weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005239weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005240{
5241 weak_ptr(__r).swap(*this);
5242 return *this;
5243}
5244
5245template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005246template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005247inline
Howard Hinnant57199402012-01-02 17:56:02 +00005248typename enable_if
5249<
5250 is_convertible<_Yp*, _Tp*>::value,
5251 weak_ptr<_Tp>&
5252>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005253weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005254{
5255 weak_ptr(__r).swap(*this);
5256 return *this;
5257}
5258
Howard Hinnant57199402012-01-02 17:56:02 +00005259#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5260
5261template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005262inline
Howard Hinnant57199402012-01-02 17:56:02 +00005263weak_ptr<_Tp>&
5264weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5265{
5266 weak_ptr(_VSTD::move(__r)).swap(*this);
5267 return *this;
5268}
5269
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005270template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005271template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005272inline
Howard Hinnant57199402012-01-02 17:56:02 +00005273typename enable_if
5274<
5275 is_convertible<_Yp*, _Tp*>::value,
5276 weak_ptr<_Tp>&
5277>::type
5278weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5279{
5280 weak_ptr(_VSTD::move(__r)).swap(*this);
5281 return *this;
5282}
5283
5284#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5285
5286template<class _Tp>
5287template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005288inline
Howard Hinnant57199402012-01-02 17:56:02 +00005289typename enable_if
5290<
5291 is_convertible<_Yp*, _Tp*>::value,
5292 weak_ptr<_Tp>&
5293>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005294weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005295{
5296 weak_ptr(__r).swap(*this);
5297 return *this;
5298}
5299
5300template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005301inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005302void
Howard Hinnant1694d232011-05-28 14:41:13 +00005303weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005304{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005305 _VSTD::swap(__ptr_, __r.__ptr_);
5306 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005307}
5308
5309template<class _Tp>
5310inline _LIBCPP_INLINE_VISIBILITY
5311void
Howard Hinnant1694d232011-05-28 14:41:13 +00005312swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005313{
5314 __x.swap(__y);
5315}
5316
5317template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005318inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005319void
Howard Hinnant1694d232011-05-28 14:41:13 +00005320weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005321{
5322 weak_ptr().swap(*this);
5323}
5324
5325template<class _Tp>
5326template<class _Yp>
5327shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5328 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5329 : __ptr_(__r.__ptr_),
5330 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5331{
5332 if (__cntrl_ == 0)
5333#ifndef _LIBCPP_NO_EXCEPTIONS
5334 throw bad_weak_ptr();
5335#else
5336 assert(!"bad_weak_ptr");
5337#endif
5338}
5339
5340template<class _Tp>
5341shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005342weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005343{
5344 shared_ptr<_Tp> __r;
5345 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5346 if (__r.__cntrl_)
5347 __r.__ptr_ = __ptr_;
5348 return __r;
5349}
5350
Marshall Clow3f159e82015-11-12 15:56:44 +00005351#if _LIBCPP_STD_VER > 14
5352template <class _Tp = void> struct owner_less;
5353#else
Howard Hinnant324bb032010-08-22 00:02:43 +00005354template <class _Tp> struct owner_less;
Marshall Clow3f159e82015-11-12 15:56:44 +00005355#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005356
5357template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005358struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005359 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005360{
5361 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005363 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5364 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005366 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5367 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005369 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5370 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005371};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005372
5373template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005374struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005375 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5376{
Howard Hinnant324bb032010-08-22 00:02:43 +00005377 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005379 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5380 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005382 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5383 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005385 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5386 {return __x.owner_before(__y);}
5387};
5388
Marshall Clow3f159e82015-11-12 15:56:44 +00005389#if _LIBCPP_STD_VER > 14
5390template <>
5391struct _LIBCPP_TYPE_VIS_ONLY owner_less<void>
5392{
5393 template <class _Tp, class _Up>
5394 _LIBCPP_INLINE_VISIBILITY
5395 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5396 {return __x.owner_before(__y);}
5397 template <class _Tp, class _Up>
5398 _LIBCPP_INLINE_VISIBILITY
5399 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5400 {return __x.owner_before(__y);}
5401 template <class _Tp, class _Up>
5402 _LIBCPP_INLINE_VISIBILITY
5403 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5404 {return __x.owner_before(__y);}
5405 template <class _Tp, class _Up>
5406 _LIBCPP_INLINE_VISIBILITY
5407 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5408 {return __x.owner_before(__y);}
5409 typedef void is_transparent;
5410};
5411#endif
5412
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005413template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005414class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005415{
5416 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005417protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005418 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005419 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005421 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005423 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5424 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005426 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005427public:
Howard Hinnant82894812010-09-22 16:48:34 +00005428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005429 shared_ptr<_Tp> shared_from_this()
5430 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005432 shared_ptr<_Tp const> shared_from_this() const
5433 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005434
5435 template <class _Up> friend class shared_ptr;
5436};
5437
Howard Hinnant21aefc32010-06-03 16:42:57 +00005438template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005439struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005440{
5441 typedef shared_ptr<_Tp> argument_type;
5442 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005444 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005445 {
5446 return hash<_Tp*>()(__ptr.get());
5447 }
5448};
5449
Howard Hinnant99968442011-11-29 18:15:50 +00005450template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005451inline _LIBCPP_INLINE_VISIBILITY
5452basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005453operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005454
Eric Fiselier00f4a492015-08-19 17:21:46 +00005455// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
5456// enabled with clang.
5457#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005458
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005459class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005460{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005461 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005462public:
5463 void lock() _NOEXCEPT;
5464 void unlock() _NOEXCEPT;
5465
5466private:
5467 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5468 __sp_mut(const __sp_mut&);
5469 __sp_mut& operator=(const __sp_mut&);
5470
Howard Hinnant83eade62013-03-06 23:30:19 +00005471 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005472};
5473
Howard Hinnant83eade62013-03-06 23:30:19 +00005474_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005475
5476template <class _Tp>
5477inline _LIBCPP_INLINE_VISIBILITY
5478bool
5479atomic_is_lock_free(const shared_ptr<_Tp>*)
5480{
5481 return false;
5482}
5483
5484template <class _Tp>
5485shared_ptr<_Tp>
5486atomic_load(const shared_ptr<_Tp>* __p)
5487{
5488 __sp_mut& __m = __get_sp_mut(__p);
5489 __m.lock();
5490 shared_ptr<_Tp> __q = *__p;
5491 __m.unlock();
5492 return __q;
5493}
5494
5495template <class _Tp>
5496inline _LIBCPP_INLINE_VISIBILITY
5497shared_ptr<_Tp>
5498atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5499{
5500 return atomic_load(__p);
5501}
5502
5503template <class _Tp>
5504void
5505atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5506{
5507 __sp_mut& __m = __get_sp_mut(__p);
5508 __m.lock();
5509 __p->swap(__r);
5510 __m.unlock();
5511}
5512
5513template <class _Tp>
5514inline _LIBCPP_INLINE_VISIBILITY
5515void
5516atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5517{
5518 atomic_store(__p, __r);
5519}
5520
5521template <class _Tp>
5522shared_ptr<_Tp>
5523atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5524{
5525 __sp_mut& __m = __get_sp_mut(__p);
5526 __m.lock();
5527 __p->swap(__r);
5528 __m.unlock();
5529 return __r;
5530}
5531
5532template <class _Tp>
5533inline _LIBCPP_INLINE_VISIBILITY
5534shared_ptr<_Tp>
5535atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5536{
5537 return atomic_exchange(__p, __r);
5538}
5539
5540template <class _Tp>
5541bool
5542atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5543{
Marshall Clow2241cf02016-05-18 17:50:13 +00005544 shared_ptr<_Tp> __temp;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005545 __sp_mut& __m = __get_sp_mut(__p);
5546 __m.lock();
5547 if (__p->__owner_equivalent(*__v))
5548 {
Marshall Clow2241cf02016-05-18 17:50:13 +00005549 _VSTD::swap(__temp, *__p);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005550 *__p = __w;
5551 __m.unlock();
5552 return true;
5553 }
Marshall Clow2241cf02016-05-18 17:50:13 +00005554 _VSTD::swap(__temp, *__v);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005555 *__v = *__p;
5556 __m.unlock();
5557 return false;
5558}
5559
5560template <class _Tp>
5561inline _LIBCPP_INLINE_VISIBILITY
5562bool
5563atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5564{
5565 return atomic_compare_exchange_strong(__p, __v, __w);
5566}
5567
5568template <class _Tp>
5569inline _LIBCPP_INLINE_VISIBILITY
5570bool
5571atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5572 shared_ptr<_Tp> __w, memory_order, memory_order)
5573{
5574 return atomic_compare_exchange_strong(__p, __v, __w);
5575}
5576
5577template <class _Tp>
5578inline _LIBCPP_INLINE_VISIBILITY
5579bool
5580atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5581 shared_ptr<_Tp> __w, memory_order, memory_order)
5582{
5583 return atomic_compare_exchange_weak(__p, __v, __w);
5584}
5585
Eric Fiselier00f4a492015-08-19 17:21:46 +00005586#endif // defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005587
Howard Hinnant324bb032010-08-22 00:02:43 +00005588//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005589struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005590{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005591 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005592 {
5593 relaxed,
5594 preferred,
5595 strict
5596 };
5597
Howard Hinnant9c0df142012-10-30 19:06:59 +00005598 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005599
Howard Hinnant82894812010-09-22 16:48:34 +00005600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005601 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005603 operator int() const {return __v_;}
5604};
5605
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005606_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5607_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5608_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5609_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5610_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005611
5612template <class _Tp>
5613inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005614_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005615undeclare_reachable(_Tp* __p)
5616{
5617 return static_cast<_Tp*>(__undeclare_reachable(__p));
5618}
5619
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005620_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005621
Marshall Clow7d914d12015-07-13 20:04:56 +00005622// --- Helper for container swap --
5623template <typename _Alloc>
Eric Fiselier566bcb42016-04-21 22:54:21 +00005624inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow7d914d12015-07-13 20:04:56 +00005625void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5626#if _LIBCPP_STD_VER >= 14
5627 _NOEXCEPT
5628#else
5629 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5630#endif
5631{
5632 __swap_allocator(__a1, __a2,
5633 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5634}
5635
5636template <typename _Alloc>
5637_LIBCPP_INLINE_VISIBILITY
5638void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5639#if _LIBCPP_STD_VER >= 14
5640 _NOEXCEPT
5641#else
5642 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5643#endif
5644{
5645 using _VSTD::swap;
5646 swap(__a1, __a2);
5647}
5648
5649template <typename _Alloc>
Eric Fiselier566bcb42016-04-21 22:54:21 +00005650inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow7d914d12015-07-13 20:04:56 +00005651void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5652
Marshall Clowd434e2a2015-08-18 19:51:37 +00005653template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clowaf961ed2015-08-18 18:57:00 +00005654struct __noexcept_move_assign_container : public integral_constant<bool,
5655 _Traits::propagate_on_container_move_assignment::value
5656#if _LIBCPP_STD_VER > 14
5657 || _Traits::is_always_equal::value
5658#else
5659 && is_nothrow_move_assignable<_Alloc>::value
5660#endif
5661 > {};
Marshall Clow7d914d12015-07-13 20:04:56 +00005662
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005663_LIBCPP_END_NAMESPACE_STD
5664
5665#endif // _LIBCPP_MEMORY