blob: df35a07143ce28f7d371c00b5e6e262a52da9777 [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>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000610#include <cstring>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611#if defined(_LIBCPP_NO_EXCEPTIONS)
612 #include <cassert>
613#endif
614
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700615#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000616# include <atomic>
617#endif
618
Howard Hinnant66c6f972011-11-29 16:45:27 +0000619#include <__undef_min_max>
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +0000620#include <__undef___deallocate>
Howard Hinnant66c6f972011-11-29 16:45:27 +0000621
Howard Hinnant08e17472011-10-17 20:05:10 +0000622#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000624#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625
626_LIBCPP_BEGIN_NAMESPACE_STD
627
Eric Fiselierc6e46692015-07-07 00:27:16 +0000628template <class _ValueType>
629inline _LIBCPP_ALWAYS_INLINE
630_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
631#if !defined(_LIBCPP_HAS_NO_THREADS) && \
632 defined(__ATOMIC_RELAXED) && \
633 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
634 return __atomic_load_n(__value, __ATOMIC_RELAXED);
635#else
636 return *__value;
637#endif
638}
639
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000640// addressof moved to <__functional_base>
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000641
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642template <class _Tp> class allocator;
643
644template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000645class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646{
647public:
648 typedef void* pointer;
649 typedef const void* const_pointer;
650 typedef void value_type;
651
652 template <class _Up> struct rebind {typedef allocator<_Up> other;};
653};
654
Howard Hinnanta1877872012-01-19 23:15:22 +0000655template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000656class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000657{
658public:
659 typedef const void* pointer;
660 typedef const void* const_pointer;
661 typedef const void value_type;
662
663 template <class _Up> struct rebind {typedef allocator<_Up> other;};
664};
665
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666// pointer_traits
667
668template <class _Tp>
669struct __has_element_type
670{
671private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000672 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673 template <class _Up> static __two __test(...);
674 template <class _Up> static char __test(typename _Up::element_type* = 0);
675public:
676 static const bool value = sizeof(__test<_Tp>(0)) == 1;
677};
678
679template <class _Ptr, bool = __has_element_type<_Ptr>::value>
680struct __pointer_traits_element_type;
681
682template <class _Ptr>
683struct __pointer_traits_element_type<_Ptr, true>
684{
685 typedef typename _Ptr::element_type type;
686};
687
688#ifndef _LIBCPP_HAS_NO_VARIADICS
689
690template <template <class, class...> class _Sp, class _Tp, class ..._Args>
691struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
692{
693 typedef typename _Sp<_Tp, _Args...>::element_type type;
694};
695
696template <template <class, class...> class _Sp, class _Tp, class ..._Args>
697struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
698{
699 typedef _Tp type;
700};
701
Howard Hinnant324bb032010-08-22 00:02:43 +0000702#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703
704template <template <class> class _Sp, class _Tp>
705struct __pointer_traits_element_type<_Sp<_Tp>, true>
706{
707 typedef typename _Sp<_Tp>::element_type type;
708};
709
710template <template <class> class _Sp, class _Tp>
711struct __pointer_traits_element_type<_Sp<_Tp>, false>
712{
713 typedef _Tp type;
714};
715
716template <template <class, class> class _Sp, class _Tp, class _A0>
717struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
718{
719 typedef typename _Sp<_Tp, _A0>::element_type type;
720};
721
722template <template <class, class> class _Sp, class _Tp, class _A0>
723struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
724{
725 typedef _Tp type;
726};
727
728template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
729struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
730{
731 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
732};
733
734template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
735struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
736{
737 typedef _Tp type;
738};
739
740template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
741 class _A1, class _A2>
742struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
743{
744 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
745};
746
747template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
748 class _A1, class _A2>
749struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
750{
751 typedef _Tp type;
752};
753
Howard Hinnant324bb032010-08-22 00:02:43 +0000754#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755
756template <class _Tp>
757struct __has_difference_type
758{
759private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000760 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761 template <class _Up> static __two __test(...);
762 template <class _Up> static char __test(typename _Up::difference_type* = 0);
763public:
764 static const bool value = sizeof(__test<_Tp>(0)) == 1;
765};
766
767template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
768struct __pointer_traits_difference_type
769{
770 typedef ptrdiff_t type;
771};
772
773template <class _Ptr>
774struct __pointer_traits_difference_type<_Ptr, true>
775{
776 typedef typename _Ptr::difference_type type;
777};
778
779template <class _Tp, class _Up>
780struct __has_rebind
781{
782private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000783 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784 template <class _Xp> static __two __test(...);
785 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
786public:
787 static const bool value = sizeof(__test<_Tp>(0)) == 1;
788};
789
790template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
791struct __pointer_traits_rebind
792{
793#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794 typedef typename _Tp::template rebind<_Up> type;
795#else
796 typedef typename _Tp::template rebind<_Up>::other type;
797#endif
798};
799
800#ifndef _LIBCPP_HAS_NO_VARIADICS
801
802template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
803struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
804{
805#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
806 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
807#else
808 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
809#endif
810};
811
812template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
813struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
814{
815 typedef _Sp<_Up, _Args...> type;
816};
817
Howard Hinnant324bb032010-08-22 00:02:43 +0000818#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819
820template <template <class> class _Sp, class _Tp, class _Up>
821struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
822{
823#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
824 typedef typename _Sp<_Tp>::template rebind<_Up> type;
825#else
826 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
827#endif
828};
829
830template <template <class> class _Sp, class _Tp, class _Up>
831struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
832{
833 typedef _Sp<_Up> type;
834};
835
836template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
837struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
838{
839#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
840 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
841#else
842 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
843#endif
844};
845
846template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
847struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
848{
849 typedef _Sp<_Up, _A0> type;
850};
851
852template <template <class, class, class> class _Sp, class _Tp, class _A0,
853 class _A1, class _Up>
854struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
855{
856#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
857 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
858#else
859 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
860#endif
861};
862
863template <template <class, class, class> class _Sp, class _Tp, class _A0,
864 class _A1, class _Up>
865struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
866{
867 typedef _Sp<_Up, _A0, _A1> type;
868};
869
870template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
871 class _A1, class _A2, class _Up>
872struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
873{
874#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
875 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
876#else
877 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
878#endif
879};
880
881template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
882 class _A1, class _A2, class _Up>
883struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
884{
885 typedef _Sp<_Up, _A0, _A1, _A2> type;
886};
887
Howard Hinnant324bb032010-08-22 00:02:43 +0000888#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889
890template <class _Ptr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000891struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892{
893 typedef _Ptr pointer;
894 typedef typename __pointer_traits_element_type<pointer>::type element_type;
895 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
896
897#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000898 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899#else
900 template <class _Up> struct rebind
901 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000902#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903
904private:
905 struct __nat {};
906public:
Howard Hinnant82894812010-09-22 16:48:34 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908 static pointer pointer_to(typename conditional<is_void<element_type>::value,
909 __nat, element_type>::type& __r)
910 {return pointer::pointer_to(__r);}
911};
912
913template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000914struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915{
916 typedef _Tp* pointer;
917 typedef _Tp element_type;
918 typedef ptrdiff_t difference_type;
919
920#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
921 template <class _Up> using rebind = _Up*;
922#else
923 template <class _Up> struct rebind {typedef _Up* other;};
924#endif
925
926private:
927 struct __nat {};
928public:
Howard Hinnant82894812010-09-22 16:48:34 +0000929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000931 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000932 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933};
934
935// allocator_traits
936
937namespace __has_pointer_type_imp
938{
Howard Hinnant81277582013-09-21 01:45:05 +0000939 template <class _Up> static __two __test(...);
940 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941}
942
943template <class _Tp>
944struct __has_pointer_type
Howard Hinnant81277582013-09-21 01:45:05 +0000945 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946{
947};
948
949namespace __pointer_type_imp
950{
951
952template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
953struct __pointer_type
954{
955 typedef typename _Dp::pointer type;
956};
957
958template <class _Tp, class _Dp>
959struct __pointer_type<_Tp, _Dp, false>
960{
961 typedef _Tp* type;
962};
963
Howard Hinnant47761072010-11-18 01:40:00 +0000964} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965
966template <class _Tp, class _Dp>
967struct __pointer_type
968{
969 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
970};
971
972template <class _Tp>
973struct __has_const_pointer
974{
975private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000976 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977 template <class _Up> static __two __test(...);
978 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
979public:
980 static const bool value = sizeof(__test<_Tp>(0)) == 1;
981};
982
983template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
984struct __const_pointer
985{
986 typedef typename _Alloc::const_pointer type;
987};
988
989template <class _Tp, class _Ptr, class _Alloc>
990struct __const_pointer<_Tp, _Ptr, _Alloc, false>
991{
992#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
993 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
994#else
995 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
996#endif
997};
998
999template <class _Tp>
1000struct __has_void_pointer
1001{
1002private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001003 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004 template <class _Up> static __two __test(...);
1005 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1006public:
1007 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1008};
1009
1010template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1011struct __void_pointer
1012{
1013 typedef typename _Alloc::void_pointer type;
1014};
1015
1016template <class _Ptr, class _Alloc>
1017struct __void_pointer<_Ptr, _Alloc, false>
1018{
1019#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1020 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1021#else
1022 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1023#endif
1024};
1025
1026template <class _Tp>
1027struct __has_const_void_pointer
1028{
1029private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001030 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031 template <class _Up> static __two __test(...);
1032 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1033public:
1034 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1035};
1036
1037template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1038struct __const_void_pointer
1039{
1040 typedef typename _Alloc::const_void_pointer type;
1041};
1042
1043template <class _Ptr, class _Alloc>
1044struct __const_void_pointer<_Ptr, _Alloc, false>
1045{
1046#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1047 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1048#else
1049 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1050#endif
1051};
1052
Howard Hinnant99968442011-11-29 18:15:50 +00001053template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001055_Tp*
1056__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057{
1058 return __p;
1059}
1060
1061template <class _Pointer>
1062inline _LIBCPP_INLINE_VISIBILITY
1063typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001064__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001066 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067}
1068
1069template <class _Tp>
1070struct __has_size_type
1071{
1072private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001073 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 template <class _Up> static __two __test(...);
1075 template <class _Up> static char __test(typename _Up::size_type* = 0);
1076public:
1077 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1078};
1079
Howard Hinnant47761072010-11-18 01:40:00 +00001080template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081struct __size_type
1082{
Howard Hinnant47761072010-11-18 01:40:00 +00001083 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084};
1085
Howard Hinnant47761072010-11-18 01:40:00 +00001086template <class _Alloc, class _DiffType>
1087struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088{
1089 typedef typename _Alloc::size_type type;
1090};
1091
1092template <class _Tp>
1093struct __has_propagate_on_container_copy_assignment
1094{
1095private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001096 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 template <class _Up> static __two __test(...);
1098 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1099public:
1100 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1101};
1102
1103template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1104struct __propagate_on_container_copy_assignment
1105{
1106 typedef false_type type;
1107};
1108
1109template <class _Alloc>
1110struct __propagate_on_container_copy_assignment<_Alloc, true>
1111{
1112 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1113};
1114
1115template <class _Tp>
1116struct __has_propagate_on_container_move_assignment
1117{
1118private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001119 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001120 template <class _Up> static __two __test(...);
1121 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1122public:
1123 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1124};
1125
1126template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1127struct __propagate_on_container_move_assignment
1128{
1129 typedef false_type type;
1130};
1131
1132template <class _Alloc>
1133struct __propagate_on_container_move_assignment<_Alloc, true>
1134{
1135 typedef typename _Alloc::propagate_on_container_move_assignment type;
1136};
1137
1138template <class _Tp>
1139struct __has_propagate_on_container_swap
1140{
1141private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001142 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143 template <class _Up> static __two __test(...);
1144 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1145public:
1146 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1147};
1148
1149template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1150struct __propagate_on_container_swap
1151{
1152 typedef false_type type;
1153};
1154
1155template <class _Alloc>
1156struct __propagate_on_container_swap<_Alloc, true>
1157{
1158 typedef typename _Alloc::propagate_on_container_swap type;
1159};
1160
Marshall Clowf0324bc2015-06-02 16:34:03 +00001161template <class _Tp>
1162struct __has_is_always_equal
1163{
1164private:
1165 struct __two {char __lx; char __lxx;};
1166 template <class _Up> static __two __test(...);
1167 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1168public:
1169 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1170};
1171
1172template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1173struct __is_always_equal
1174{
1175 typedef typename _VSTD::is_empty<_Alloc>::type type;
1176};
1177
1178template <class _Alloc>
1179struct __is_always_equal<_Alloc, true>
1180{
1181 typedef typename _Alloc::is_always_equal type;
1182};
1183
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001184template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1185struct __has_rebind_other
1186{
1187private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001188 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189 template <class _Xp> static __two __test(...);
1190 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1191public:
1192 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1193};
1194
1195template <class _Tp, class _Up>
1196struct __has_rebind_other<_Tp, _Up, false>
1197{
1198 static const bool value = false;
1199};
1200
1201template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1202struct __allocator_traits_rebind
1203{
1204 typedef typename _Tp::template rebind<_Up>::other type;
1205};
1206
1207#ifndef _LIBCPP_HAS_NO_VARIADICS
1208
1209template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1210struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1211{
1212 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1213};
1214
1215template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1216struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1217{
1218 typedef _Alloc<_Up, _Args...> type;
1219};
1220
Howard Hinnant324bb032010-08-22 00:02:43 +00001221#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222
1223template <template <class> class _Alloc, class _Tp, class _Up>
1224struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1225{
1226 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1227};
1228
1229template <template <class> class _Alloc, class _Tp, class _Up>
1230struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1231{
1232 typedef _Alloc<_Up> type;
1233};
1234
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1236struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1237{
1238 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1239};
1240
1241template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1242struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1243{
1244 typedef _Alloc<_Up, _A0> type;
1245};
1246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1248 class _A1, class _Up>
1249struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1250{
1251 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1252};
1253
1254template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1255 class _A1, class _Up>
1256struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1257{
1258 typedef _Alloc<_Up, _A0, _A1> type;
1259};
1260
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1262 class _A1, class _A2, class _Up>
1263struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1264{
1265 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1266};
1267
1268template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1269 class _A1, class _A2, class _Up>
1270struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1271{
1272 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1273};
1274
Howard Hinnant324bb032010-08-22 00:02:43 +00001275#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276
1277#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1278
1279template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1280auto
1281__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1282 -> decltype(__a.allocate(__sz, __p), true_type());
1283
1284template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1285auto
1286__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1287 -> false_type;
1288
1289template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1290struct __has_allocate_hint
1291 : integral_constant<bool,
1292 is_same<
1293 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1294 declval<_SizeType>(),
1295 declval<_ConstVoidPtr>())),
1296 true_type>::value>
1297{
1298};
1299
Howard Hinnant324bb032010-08-22 00:02:43 +00001300#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301
1302template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1303struct __has_allocate_hint
1304 : true_type
1305{
1306};
1307
Howard Hinnant324bb032010-08-22 00:02:43 +00001308#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309
Howard Hinnant23369ee2011-07-29 21:35:53 +00001310#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311
1312template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001313decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1314 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 true_type())
1316__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1317
1318template <class _Alloc, class _Pointer, class ..._Args>
1319false_type
1320__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1321
1322template <class _Alloc, class _Pointer, class ..._Args>
1323struct __has_construct
1324 : integral_constant<bool,
1325 is_same<
1326 decltype(__has_construct_test(declval<_Alloc>(),
1327 declval<_Pointer>(),
1328 declval<_Args>()...)),
1329 true_type>::value>
1330{
1331};
1332
1333template <class _Alloc, class _Pointer>
1334auto
1335__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1336 -> decltype(__a.destroy(__p), true_type());
1337
1338template <class _Alloc, class _Pointer>
1339auto
1340__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1341 -> false_type;
1342
1343template <class _Alloc, class _Pointer>
1344struct __has_destroy
1345 : integral_constant<bool,
1346 is_same<
1347 decltype(__has_destroy_test(declval<_Alloc>(),
1348 declval<_Pointer>())),
1349 true_type>::value>
1350{
1351};
1352
1353template <class _Alloc>
1354auto
1355__has_max_size_test(_Alloc&& __a)
1356 -> decltype(__a.max_size(), true_type());
1357
1358template <class _Alloc>
1359auto
1360__has_max_size_test(const volatile _Alloc& __a)
1361 -> false_type;
1362
1363template <class _Alloc>
1364struct __has_max_size
1365 : integral_constant<bool,
1366 is_same<
1367 decltype(__has_max_size_test(declval<_Alloc&>())),
1368 true_type>::value>
1369{
1370};
1371
1372template <class _Alloc>
1373auto
1374__has_select_on_container_copy_construction_test(_Alloc&& __a)
1375 -> decltype(__a.select_on_container_copy_construction(), true_type());
1376
1377template <class _Alloc>
1378auto
1379__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1380 -> false_type;
1381
1382template <class _Alloc>
1383struct __has_select_on_container_copy_construction
1384 : integral_constant<bool,
1385 is_same<
1386 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1387 true_type>::value>
1388{
1389};
1390
Howard Hinnant324bb032010-08-22 00:02:43 +00001391#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392
1393#ifndef _LIBCPP_HAS_NO_VARIADICS
1394
1395template <class _Alloc, class _Pointer, class ..._Args>
1396struct __has_construct
1397 : false_type
1398{
1399};
1400
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001401#else // _LIBCPP_HAS_NO_VARIADICS
1402
1403template <class _Alloc, class _Pointer, class _Args>
1404struct __has_construct
1405 : false_type
1406{
1407};
1408
Howard Hinnant324bb032010-08-22 00:02:43 +00001409#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410
1411template <class _Alloc, class _Pointer>
1412struct __has_destroy
1413 : false_type
1414{
1415};
1416
1417template <class _Alloc>
1418struct __has_max_size
1419 : true_type
1420{
1421};
1422
1423template <class _Alloc>
1424struct __has_select_on_container_copy_construction
1425 : false_type
1426{
1427};
1428
Howard Hinnant324bb032010-08-22 00:02:43 +00001429#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430
Howard Hinnant47761072010-11-18 01:40:00 +00001431template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1432struct __alloc_traits_difference_type
1433{
1434 typedef typename pointer_traits<_Ptr>::difference_type type;
1435};
1436
1437template <class _Alloc, class _Ptr>
1438struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1439{
1440 typedef typename _Alloc::difference_type type;
1441};
1442
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443template <class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001444struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445{
1446 typedef _Alloc allocator_type;
1447 typedef typename allocator_type::value_type value_type;
1448
1449 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1450 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1451 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1452 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1453
Howard Hinnant47761072010-11-18 01:40:00 +00001454 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1455 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001456
1457 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1458 propagate_on_container_copy_assignment;
1459 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1460 propagate_on_container_move_assignment;
1461 typedef typename __propagate_on_container_swap<allocator_type>::type
1462 propagate_on_container_swap;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001463 typedef typename __is_always_equal<allocator_type>::type
1464 is_always_equal;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465
1466#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1467 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001468 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001470#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471 template <class _Tp> struct rebind_alloc
1472 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1473 template <class _Tp> struct rebind_traits
1474 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001475#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476
Howard Hinnant82894812010-09-22 16:48:34 +00001477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 static pointer allocate(allocator_type& __a, size_type __n)
1479 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1482 {return allocate(__a, __n, __hint,
1483 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1484
Howard Hinnant82894812010-09-22 16:48:34 +00001485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001486 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 {__a.deallocate(__p, __n);}
1488
1489#ifndef _LIBCPP_HAS_NO_VARIADICS
1490 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3f5579f2014-11-11 19:22:33 +00001493 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001494 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001495#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 static void construct(allocator_type& __a, _Tp* __p)
1499 {
1500 ::new ((void*)__p) _Tp();
1501 }
1502 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1505 {
1506 ::new ((void*)__p) _Tp(__a0);
1507 }
1508 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1511 const _A1& __a1)
1512 {
1513 ::new ((void*)__p) _Tp(__a0, __a1);
1514 }
1515 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1518 const _A1& __a1, const _A2& __a2)
1519 {
1520 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1521 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001522#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523
1524 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 static void destroy(allocator_type& __a, _Tp* __p)
1527 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1528
Howard Hinnant82894812010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Marshall Clow08b4f3f2013-08-27 20:22:15 +00001530 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1532
Howard Hinnant82894812010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 static allocator_type
1535 select_on_container_copy_construction(const allocator_type& __a)
1536 {return select_on_container_copy_construction(
1537 __has_select_on_container_copy_construction<const allocator_type>(),
1538 __a);}
1539
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001540 template <class _Ptr>
1541 _LIBCPP_INLINE_VISIBILITY
1542 static
1543 void
1544 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1545 {
1546 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1547 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1548 }
1549
1550 template <class _Tp>
1551 _LIBCPP_INLINE_VISIBILITY
1552 static
1553 typename enable_if
1554 <
1555 (is_same<allocator_type, allocator<_Tp> >::value
1556 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1557 is_trivially_move_constructible<_Tp>::value,
1558 void
1559 >::type
1560 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1561 {
1562 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001563 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001564 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001565 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001566 __begin2 += _Np;
1567 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001568 }
1569
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001570 template <class _Iter, class _Ptr>
1571 _LIBCPP_INLINE_VISIBILITY
1572 static
1573 void
1574 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1575 {
1576 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1577 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1578 }
1579
1580 template <class _Tp>
1581 _LIBCPP_INLINE_VISIBILITY
1582 static
1583 typename enable_if
1584 <
1585 (is_same<allocator_type, allocator<_Tp> >::value
1586 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1587 is_trivially_move_constructible<_Tp>::value,
1588 void
1589 >::type
1590 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1591 {
1592 typedef typename remove_const<_Tp>::type _Vp;
1593 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001594 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001595 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001596 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001597 __begin2 += _Np;
1598 }
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001599 }
1600
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001601 template <class _Ptr>
1602 _LIBCPP_INLINE_VISIBILITY
1603 static
1604 void
1605 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1606 {
1607 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001608 {
1609 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1610 --__end2;
1611 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001612 }
1613
1614 template <class _Tp>
1615 _LIBCPP_INLINE_VISIBILITY
1616 static
1617 typename enable_if
1618 <
1619 (is_same<allocator_type, allocator<_Tp> >::value
1620 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1621 is_trivially_move_constructible<_Tp>::value,
1622 void
1623 >::type
1624 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1625 {
1626 ptrdiff_t _Np = __end1 - __begin1;
1627 __end2 -= _Np;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001628 if (_Np > 0)
1629 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001630 }
1631
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632private:
1633
Howard Hinnant82894812010-09-22 16:48:34 +00001634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001635 static pointer allocate(allocator_type& __a, size_type __n,
1636 const_void_pointer __hint, true_type)
1637 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001640 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 {return __a.allocate(__n);}
1642
1643#ifndef _LIBCPP_HAS_NO_VARIADICS
1644 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001647 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1651 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001652 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001654#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655
1656 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1659 {__a.destroy(__p);}
1660 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662 static void __destroy(false_type, allocator_type&, _Tp* __p)
1663 {
1664 __p->~_Tp();
1665 }
1666
Howard Hinnant82894812010-09-22 16:48:34 +00001667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668 static size_type __max_size(true_type, const allocator_type& __a)
1669 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671 static size_type __max_size(false_type, const allocator_type&)
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001672 {return numeric_limits<size_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673
Howard Hinnant82894812010-09-22 16:48:34 +00001674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 static allocator_type
1676 select_on_container_copy_construction(true_type, const allocator_type& __a)
1677 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679 static allocator_type
1680 select_on_container_copy_construction(false_type, const allocator_type& __a)
1681 {return __a;}
1682};
1683
Marshall Clow66302c62015-04-07 05:21:38 +00001684template <class _Traits, class _Tp>
1685struct __rebind_alloc_helper
1686{
1687#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Marshall Clow0ad232a2015-05-10 13:59:45 +00001688 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow66302c62015-04-07 05:21:38 +00001689#else
1690 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1691#endif
1692};
1693
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694// allocator
1695
1696template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001697class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698{
1699public:
1700 typedef size_t size_type;
1701 typedef ptrdiff_t difference_type;
1702 typedef _Tp* pointer;
1703 typedef const _Tp* const_pointer;
1704 typedef _Tp& reference;
1705 typedef const _Tp& const_reference;
1706 typedef _Tp value_type;
1707
Howard Hinnant18884f42011-06-02 21:38:57 +00001708 typedef true_type propagate_on_container_move_assignment;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001709 typedef true_type is_always_equal;
Howard Hinnant18884f42011-06-02 21:38:57 +00001710
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1712
Howard Hinnant1694d232011-05-28 14:41:13 +00001713 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1714 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1715 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001716 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001717 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001718 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001720 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001721 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001722 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001723 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1724 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001725#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726 template <class _Up, class... _Args>
1727 _LIBCPP_INLINE_VISIBILITY
1728 void
1729 construct(_Up* __p, _Args&&... __args)
1730 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001731 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001733#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734 _LIBCPP_INLINE_VISIBILITY
1735 void
1736 construct(pointer __p)
1737 {
1738 ::new((void*)__p) _Tp();
1739 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001740# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001741
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001742 template <class _A0>
1743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001744 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745 construct(pointer __p, _A0& __a0)
1746 {
1747 ::new((void*)__p) _Tp(__a0);
1748 }
1749 template <class _A0>
1750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001751 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752 construct(pointer __p, const _A0& __a0)
1753 {
1754 ::new((void*)__p) _Tp(__a0);
1755 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001756# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757 template <class _A0, class _A1>
1758 _LIBCPP_INLINE_VISIBILITY
1759 void
1760 construct(pointer __p, _A0& __a0, _A1& __a1)
1761 {
1762 ::new((void*)__p) _Tp(__a0, __a1);
1763 }
1764 template <class _A0, class _A1>
1765 _LIBCPP_INLINE_VISIBILITY
1766 void
1767 construct(pointer __p, const _A0& __a0, _A1& __a1)
1768 {
1769 ::new((void*)__p) _Tp(__a0, __a1);
1770 }
1771 template <class _A0, class _A1>
1772 _LIBCPP_INLINE_VISIBILITY
1773 void
1774 construct(pointer __p, _A0& __a0, const _A1& __a1)
1775 {
1776 ::new((void*)__p) _Tp(__a0, __a1);
1777 }
1778 template <class _A0, class _A1>
1779 _LIBCPP_INLINE_VISIBILITY
1780 void
1781 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1782 {
1783 ::new((void*)__p) _Tp(__a0, __a1);
1784 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001785#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1787};
1788
Howard Hinnant57199402012-01-02 17:56:02 +00001789template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001790class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001791{
1792public:
1793 typedef size_t size_type;
1794 typedef ptrdiff_t difference_type;
1795 typedef const _Tp* pointer;
1796 typedef const _Tp* const_pointer;
1797 typedef const _Tp& reference;
1798 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001799 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001800
1801 typedef true_type propagate_on_container_move_assignment;
Marshall Clowb81d6f52015-07-01 21:23:40 +00001802 typedef true_type is_always_equal;
Howard Hinnant57199402012-01-02 17:56:02 +00001803
1804 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1805
1806 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1807 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1808 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1809 {return _VSTD::addressof(__x);}
1810 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001811 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant57199402012-01-02 17:56:02 +00001812 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001813 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant57199402012-01-02 17:56:02 +00001814 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1815 {return size_type(~0) / sizeof(_Tp);}
1816#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1817 template <class _Up, class... _Args>
1818 _LIBCPP_INLINE_VISIBILITY
1819 void
1820 construct(_Up* __p, _Args&&... __args)
1821 {
1822 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1823 }
1824#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1825 _LIBCPP_INLINE_VISIBILITY
1826 void
1827 construct(pointer __p)
1828 {
1829 ::new((void*)__p) _Tp();
1830 }
1831# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001832
Howard Hinnant57199402012-01-02 17:56:02 +00001833 template <class _A0>
1834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001835 void
Howard Hinnant57199402012-01-02 17:56:02 +00001836 construct(pointer __p, _A0& __a0)
1837 {
1838 ::new((void*)__p) _Tp(__a0);
1839 }
1840 template <class _A0>
1841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001842 void
Howard Hinnant57199402012-01-02 17:56:02 +00001843 construct(pointer __p, const _A0& __a0)
1844 {
1845 ::new((void*)__p) _Tp(__a0);
1846 }
Howard Hinnant57199402012-01-02 17:56:02 +00001847# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1848 template <class _A0, class _A1>
1849 _LIBCPP_INLINE_VISIBILITY
1850 void
1851 construct(pointer __p, _A0& __a0, _A1& __a1)
1852 {
1853 ::new((void*)__p) _Tp(__a0, __a1);
1854 }
1855 template <class _A0, class _A1>
1856 _LIBCPP_INLINE_VISIBILITY
1857 void
1858 construct(pointer __p, const _A0& __a0, _A1& __a1)
1859 {
1860 ::new((void*)__p) _Tp(__a0, __a1);
1861 }
1862 template <class _A0, class _A1>
1863 _LIBCPP_INLINE_VISIBILITY
1864 void
1865 construct(pointer __p, _A0& __a0, const _A1& __a1)
1866 {
1867 ::new((void*)__p) _Tp(__a0, __a1);
1868 }
1869 template <class _A0, class _A1>
1870 _LIBCPP_INLINE_VISIBILITY
1871 void
1872 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1873 {
1874 ::new((void*)__p) _Tp(__a0, __a1);
1875 }
1876#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1877 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1878};
1879
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880template <class _Tp, class _Up>
1881inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001882bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883
1884template <class _Tp, class _Up>
1885inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001886bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887
1888template <class _OutputIterator, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001889class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 : public iterator<output_iterator_tag,
1891 _Tp, // purposefully not C++03
1892 ptrdiff_t, // purposefully not C++03
1893 _Tp*, // purposefully not C++03
1894 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1895{
1896private:
1897 _OutputIterator __x_;
1898public:
1899 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1900 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1901 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1902 {::new(&*__x_) _Tp(__element); return *this;}
1903 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1904 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1905 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00001906#if _LIBCPP_STD_VER >= 14
1907 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1908#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909};
1910
1911template <class _Tp>
1912pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001913get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914{
1915 pair<_Tp*, ptrdiff_t> __r(0, 0);
1916 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1917 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1918 / sizeof(_Tp);
1919 if (__n > __m)
1920 __n = __m;
1921 while (__n > 0)
1922 {
1923 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1924 if (__r.first)
1925 {
1926 __r.second = __n;
1927 break;
1928 }
1929 __n /= 2;
1930 }
1931 return __r;
1932}
1933
1934template <class _Tp>
1935inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001936void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937
1938template <class _Tp>
1939struct auto_ptr_ref
1940{
1941 _Tp* __ptr_;
1942};
1943
1944template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001945class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946{
1947private:
1948 _Tp* __ptr_;
1949public:
1950 typedef _Tp element_type;
1951
1952 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1953 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1954 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1955 : __ptr_(__p.release()) {}
1956 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1957 {reset(__p.release()); return *this;}
1958 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1959 {reset(__p.release()); return *this;}
1960 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1961 {reset(__p.__ptr_); return *this;}
1962 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1963
1964 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1965 {return *__ptr_;}
1966 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1967 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1968 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1969 {
1970 _Tp* __t = __ptr_;
1971 __ptr_ = 0;
1972 return __t;
1973 }
1974 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1975 {
1976 if (__ptr_ != __p)
1977 delete __ptr_;
1978 __ptr_ = __p;
1979 }
1980
1981 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1982 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1983 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1984 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1985 {return auto_ptr<_Up>(release());}
1986};
1987
1988template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001989class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990{
1991public:
1992 typedef void element_type;
1993};
1994
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1996 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001997 bool = is_empty<_T1>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00001998 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001999 bool = is_empty<_T2>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002000 && !__libcpp_is_final<_T2>::value
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002001 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002struct __libcpp_compressed_pair_switch;
2003
2004template <class _T1, class _T2, bool IsSame>
2005struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2006
2007template <class _T1, class _T2, bool IsSame>
2008struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2009
2010template <class _T1, class _T2, bool IsSame>
2011struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2012
2013template <class _T1, class _T2>
2014struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2015
2016template <class _T1, class _T2>
2017struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2018
2019template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2020class __libcpp_compressed_pair_imp;
2021
2022template <class _T1, class _T2>
2023class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2024{
2025private:
2026 _T1 __first_;
2027 _T2 __second_;
2028public:
2029 typedef _T1 _T1_param;
2030 typedef _T2 _T2_param;
2031
2032 typedef typename remove_reference<_T1>::type& _T1_reference;
2033 typedef typename remove_reference<_T2>::type& _T2_reference;
2034
2035 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2036 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2037
Marshall Clowcd6ed542015-07-16 03:05:06 +00002038 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002039 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002040 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002041 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002042 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002044 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002046#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002047
2048 _LIBCPP_INLINE_VISIBILITY
2049 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2050 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2051 is_nothrow_copy_constructible<_T2>::value)
2052 : __first_(__p.first()),
2053 __second_(__p.second()) {}
2054
2055 _LIBCPP_INLINE_VISIBILITY
2056 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2057 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2058 is_nothrow_copy_assignable<_T2>::value)
2059 {
2060 __first_ = __p.first();
2061 __second_ = __p.second();
2062 return *this;
2063 }
2064
Howard Hinnant61aa6012011-07-01 19:24:36 +00002065 _LIBCPP_INLINE_VISIBILITY
2066 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002067 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2068 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002069 : __first_(_VSTD::forward<_T1>(__p.first())),
2070 __second_(_VSTD::forward<_T2>(__p.second())) {}
2071
2072 _LIBCPP_INLINE_VISIBILITY
2073 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2074 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2075 is_nothrow_move_assignable<_T2>::value)
2076 {
2077 __first_ = _VSTD::forward<_T1>(__p.first());
2078 __second_ = _VSTD::forward<_T2>(__p.second());
2079 return *this;
2080 }
2081
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002082#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002083
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002084#ifndef _LIBCPP_HAS_NO_VARIADICS
2085
2086 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2087 _LIBCPP_INLINE_VISIBILITY
2088 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2089 tuple<_Args1...> __first_args,
2090 tuple<_Args2...> __second_args,
2091 __tuple_indices<_I1...>,
2092 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002093 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2094 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002095 {}
2096
2097#endif // _LIBCPP_HAS_NO_VARIADICS
2098
Howard Hinnant1694d232011-05-28 14:41:13 +00002099 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2100 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002101
Howard Hinnant1694d232011-05-28 14:41:13 +00002102 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2103 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104
2105 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002106 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002107 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002109 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110 swap(__first_, __x.__first_);
2111 swap(__second_, __x.__second_);
2112 }
2113};
2114
2115template <class _T1, class _T2>
2116class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2117 : private _T1
2118{
2119private:
2120 _T2 __second_;
2121public:
2122 typedef _T1 _T1_param;
2123 typedef _T2 _T2_param;
2124
2125 typedef _T1& _T1_reference;
2126 typedef typename remove_reference<_T2>::type& _T2_reference;
2127
2128 typedef const _T1& _T1_const_reference;
2129 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2130
Marshall Clowcd6ed542015-07-16 03:05:06 +00002131 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002132 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002133 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002134 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002135 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002137 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002138
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002139#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002140
2141 _LIBCPP_INLINE_VISIBILITY
2142 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2143 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2144 is_nothrow_copy_constructible<_T2>::value)
2145 : _T1(__p.first()), __second_(__p.second()) {}
2146
2147 _LIBCPP_INLINE_VISIBILITY
2148 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2149 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2150 is_nothrow_copy_assignable<_T2>::value)
2151 {
2152 _T1::operator=(__p.first());
2153 __second_ = __p.second();
2154 return *this;
2155 }
2156
Howard Hinnant61aa6012011-07-01 19:24:36 +00002157 _LIBCPP_INLINE_VISIBILITY
2158 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002159 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2160 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002161 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002162
2163 _LIBCPP_INLINE_VISIBILITY
2164 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2165 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2166 is_nothrow_move_assignable<_T2>::value)
2167 {
2168 _T1::operator=(_VSTD::move(__p.first()));
2169 __second_ = _VSTD::forward<_T2>(__p.second());
2170 return *this;
2171 }
2172
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002173#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002174
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002175#ifndef _LIBCPP_HAS_NO_VARIADICS
2176
2177 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2178 _LIBCPP_INLINE_VISIBILITY
2179 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2180 tuple<_Args1...> __first_args,
2181 tuple<_Args2...> __second_args,
2182 __tuple_indices<_I1...>,
2183 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002184 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2185 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002186 {}
2187
2188#endif // _LIBCPP_HAS_NO_VARIADICS
2189
Howard Hinnant1694d232011-05-28 14:41:13 +00002190 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2191 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002192
Howard Hinnant1694d232011-05-28 14:41:13 +00002193 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2194 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002195
2196 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002197 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002198 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002200 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201 swap(__second_, __x.__second_);
2202 }
2203};
2204
2205template <class _T1, class _T2>
2206class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2207 : private _T2
2208{
2209private:
2210 _T1 __first_;
2211public:
2212 typedef _T1 _T1_param;
2213 typedef _T2 _T2_param;
2214
2215 typedef typename remove_reference<_T1>::type& _T1_reference;
2216 typedef _T2& _T2_reference;
2217
2218 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2219 typedef const _T2& _T2_const_reference;
2220
Marshall Clowcd6ed542015-07-16 03:05:06 +00002221 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002223 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002225 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002227 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2228 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002229 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002230
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002231#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002232
2233 _LIBCPP_INLINE_VISIBILITY
2234 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2235 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2236 is_nothrow_copy_constructible<_T2>::value)
2237 : _T2(__p.second()), __first_(__p.first()) {}
2238
2239 _LIBCPP_INLINE_VISIBILITY
2240 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2241 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2242 is_nothrow_copy_assignable<_T2>::value)
2243 {
2244 _T2::operator=(__p.second());
2245 __first_ = __p.first();
2246 return *this;
2247 }
2248
Howard Hinnant61aa6012011-07-01 19:24:36 +00002249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +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>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002254
2255 _LIBCPP_INLINE_VISIBILITY
2256 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2257 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2258 is_nothrow_move_assignable<_T2>::value)
2259 {
2260 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2261 __first_ = _VSTD::move(__p.first());
2262 return *this;
2263 }
2264
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002265#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002266
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002267#ifndef _LIBCPP_HAS_NO_VARIADICS
2268
2269 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2270 _LIBCPP_INLINE_VISIBILITY
2271 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2272 tuple<_Args1...> __first_args,
2273 tuple<_Args2...> __second_args,
2274 __tuple_indices<_I1...>,
2275 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002276 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2277 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002278
2279 {}
2280
2281#endif // _LIBCPP_HAS_NO_VARIADICS
2282
Howard Hinnant1694d232011-05-28 14:41:13 +00002283 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2284 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285
Howard Hinnant1694d232011-05-28 14:41:13 +00002286 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2287 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288
2289 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002290 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002291 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002293 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 swap(__first_, __x.__first_);
2295 }
2296};
2297
2298template <class _T1, class _T2>
2299class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2300 : private _T1,
2301 private _T2
2302{
2303public:
2304 typedef _T1 _T1_param;
2305 typedef _T2 _T2_param;
2306
2307 typedef _T1& _T1_reference;
2308 typedef _T2& _T2_reference;
2309
2310 typedef const _T1& _T1_const_reference;
2311 typedef const _T2& _T2_const_reference;
2312
2313 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2314 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002315 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002317 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002318 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002319 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002321#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002322
2323 _LIBCPP_INLINE_VISIBILITY
2324 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2325 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2326 is_nothrow_copy_constructible<_T2>::value)
2327 : _T1(__p.first()), _T2(__p.second()) {}
2328
2329 _LIBCPP_INLINE_VISIBILITY
2330 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2331 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2332 is_nothrow_copy_assignable<_T2>::value)
2333 {
2334 _T1::operator=(__p.first());
2335 _T2::operator=(__p.second());
2336 return *this;
2337 }
2338
Howard Hinnant61aa6012011-07-01 19:24:36 +00002339 _LIBCPP_INLINE_VISIBILITY
2340 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002341 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2342 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002343 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002344
2345 _LIBCPP_INLINE_VISIBILITY
2346 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2347 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2348 is_nothrow_move_assignable<_T2>::value)
2349 {
2350 _T1::operator=(_VSTD::move(__p.first()));
2351 _T2::operator=(_VSTD::move(__p.second()));
2352 return *this;
2353 }
2354
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002355#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002356
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002357#ifndef _LIBCPP_HAS_NO_VARIADICS
2358
2359 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2360 _LIBCPP_INLINE_VISIBILITY
2361 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2362 tuple<_Args1...> __first_args,
2363 tuple<_Args2...> __second_args,
2364 __tuple_indices<_I1...>,
2365 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002366 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2367 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002368 {}
2369
2370#endif // _LIBCPP_HAS_NO_VARIADICS
2371
Howard Hinnant1694d232011-05-28 14:41:13 +00002372 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2373 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002374
Howard Hinnant1694d232011-05-28 14:41:13 +00002375 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2376 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377
Howard Hinnantec3773c2011-12-01 20:21:04 +00002378 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002379 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002380 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 {
2382 }
2383};
2384
2385template <class _T1, class _T2>
2386class __compressed_pair
2387 : private __libcpp_compressed_pair_imp<_T1, _T2>
2388{
2389 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2390public:
2391 typedef typename base::_T1_param _T1_param;
2392 typedef typename base::_T2_param _T2_param;
2393
2394 typedef typename base::_T1_reference _T1_reference;
2395 typedef typename base::_T2_reference _T2_reference;
2396
2397 typedef typename base::_T1_const_reference _T1_const_reference;
2398 typedef typename base::_T2_const_reference _T2_const_reference;
2399
2400 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002401 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002402 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002403 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002404 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002406 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002408#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002409
2410 _LIBCPP_INLINE_VISIBILITY
2411 __compressed_pair(const __compressed_pair& __p)
2412 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2413 is_nothrow_copy_constructible<_T2>::value)
2414 : base(__p) {}
2415
2416 _LIBCPP_INLINE_VISIBILITY
2417 __compressed_pair& operator=(const __compressed_pair& __p)
2418 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2419 is_nothrow_copy_assignable<_T2>::value)
2420 {
2421 base::operator=(__p);
2422 return *this;
2423 }
2424
Howard Hinnant61aa6012011-07-01 19:24:36 +00002425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002427 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2428 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002429 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002430
2431 _LIBCPP_INLINE_VISIBILITY
2432 __compressed_pair& operator=(__compressed_pair&& __p)
2433 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2434 is_nothrow_move_assignable<_T2>::value)
2435 {
2436 base::operator=(_VSTD::move(__p));
2437 return *this;
2438 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002439
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002440#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002441
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002442#ifndef _LIBCPP_HAS_NO_VARIADICS
2443
2444 template <class... _Args1, class... _Args2>
2445 _LIBCPP_INLINE_VISIBILITY
2446 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2447 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002448 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002449 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2450 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2451 {}
2452
2453#endif // _LIBCPP_HAS_NO_VARIADICS
2454
Howard Hinnant1694d232011-05-28 14:41:13 +00002455 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2456 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457
Howard Hinnant1694d232011-05-28 14:41:13 +00002458 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2459 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460
Howard Hinnant1694d232011-05-28 14:41:13 +00002461 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2462 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002463 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002464 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465};
2466
2467template <class _T1, class _T2>
2468inline _LIBCPP_INLINE_VISIBILITY
2469void
2470swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002471 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002472 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473 {__x.swap(__y);}
2474
Howard Hinnant57199402012-01-02 17:56:02 +00002475// __same_or_less_cv_qualified
2476
2477template <class _Ptr1, class _Ptr2,
2478 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2479 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2480 >::value
2481 >
2482struct __same_or_less_cv_qualified_imp
2483 : is_convertible<_Ptr1, _Ptr2> {};
2484
2485template <class _Ptr1, class _Ptr2>
2486struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2487 : false_type {};
2488
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002489template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2490 is_same<_Ptr1, _Ptr2>::value ||
2491 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002492struct __same_or_less_cv_qualified
2493 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2494
2495template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002496struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002497 : false_type {};
2498
2499// default_delete
2500
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002502struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503{
Howard Hinnant46e94932012-07-07 20:56:04 +00002504#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2505 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2506#else
2507 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2508#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509 template <class _Up>
2510 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002511 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2512 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002513 {
2514 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002515 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 delete __ptr;
2517 }
2518};
2519
2520template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002521struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522{
Howard Hinnant8e843502011-12-18 21:19:44 +00002523public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002524#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2525 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2526#else
2527 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2528#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002529 template <class _Up>
2530 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002531 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002532 template <class _Up>
2533 _LIBCPP_INLINE_VISIBILITY
2534 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002535 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536 {
2537 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002538 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539 delete [] __ptr;
2540 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002541};
2542
2543template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002544class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545{
2546public:
2547 typedef _Tp element_type;
2548 typedef _Dp deleter_type;
2549 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2550private:
2551 __compressed_pair<pointer, deleter_type> __ptr_;
2552
Howard Hinnant57199402012-01-02 17:56:02 +00002553#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554 unique_ptr(unique_ptr&);
2555 template <class _Up, class _Ep>
2556 unique_ptr(unique_ptr<_Up, _Ep>&);
2557 unique_ptr& operator=(unique_ptr&);
2558 template <class _Up, class _Ep>
2559 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002560#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002561
2562 struct __nat {int __for_bool_;};
2563
2564 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2565 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2566public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002567 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 : __ptr_(pointer())
2569 {
2570 static_assert(!is_pointer<deleter_type>::value,
2571 "unique_ptr constructed with null function pointer deleter");
2572 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002573 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 : __ptr_(pointer())
2575 {
2576 static_assert(!is_pointer<deleter_type>::value,
2577 "unique_ptr constructed with null function pointer deleter");
2578 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002579 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002580 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581 {
2582 static_assert(!is_pointer<deleter_type>::value,
2583 "unique_ptr constructed with null function pointer deleter");
2584 }
2585
Howard Hinnant73d21a42010-09-04 23:28:19 +00002586#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002587 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2588 is_reference<deleter_type>::value,
2589 deleter_type,
2590 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002591 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002592 : __ptr_(__p, __d) {}
2593
2594 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002595 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002596 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 {
2598 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2599 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002600 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002601 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 template <class _Up, class _Ep>
2603 _LIBCPP_INLINE_VISIBILITY
2604 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2605 typename enable_if
2606 <
2607 !is_array<_Up>::value &&
2608 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2609 is_convertible<_Ep, deleter_type>::value &&
2610 (
2611 !is_reference<deleter_type>::value ||
2612 is_same<deleter_type, _Ep>::value
2613 ),
2614 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002615 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002616 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617
2618 template <class _Up>
2619 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2620 typename enable_if<
2621 is_convertible<_Up*, _Tp*>::value &&
2622 is_same<_Dp, default_delete<_Tp> >::value,
2623 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002624 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625 : __ptr_(__p.release())
2626 {
2627 }
2628
Howard Hinnant1694d232011-05-28 14:41:13 +00002629 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 {
2631 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002632 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633 return *this;
2634 }
2635
2636 template <class _Up, class _Ep>
2637 _LIBCPP_INLINE_VISIBILITY
2638 typename enable_if
2639 <
Howard Hinnant57199402012-01-02 17:56:02 +00002640 !is_array<_Up>::value &&
2641 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2642 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 unique_ptr&
2644 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002645 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002646 {
2647 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002648 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 return *this;
2650 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002651#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652
2653 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2654 {
2655 return __rv<unique_ptr>(*this);
2656 }
2657
2658 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002659 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660
2661 template <class _Up, class _Ep>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002662 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 {
2664 reset(__u.release());
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002665 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 return *this;
2667 }
2668
2669 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002670 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671
2672 template <class _Up>
2673 _LIBCPP_INLINE_VISIBILITY
2674 typename enable_if<
2675 is_convertible<_Up*, _Tp*>::value &&
2676 is_same<_Dp, default_delete<_Tp> >::value,
2677 unique_ptr&
2678 >::type
2679 operator=(auto_ptr<_Up> __p)
2680 {reset(__p.release()); return *this;}
2681
Howard Hinnant73d21a42010-09-04 23:28:19 +00002682#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2684
Howard Hinnant1694d232011-05-28 14:41:13 +00002685 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686 {
2687 reset();
2688 return *this;
2689 }
2690
2691 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2692 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002693 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2694 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2695 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2696 {return __ptr_.second();}
2697 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2698 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002699 _LIBCPP_INLINE_VISIBILITY
2700 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2701 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702
Howard Hinnant1694d232011-05-28 14:41:13 +00002703 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002704 {
2705 pointer __t = __ptr_.first();
2706 __ptr_.first() = pointer();
2707 return __t;
2708 }
2709
Howard Hinnant1694d232011-05-28 14:41:13 +00002710 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711 {
2712 pointer __tmp = __ptr_.first();
2713 __ptr_.first() = __p;
2714 if (__tmp)
2715 __ptr_.second()(__tmp);
2716 }
2717
Howard Hinnant1694d232011-05-28 14:41:13 +00002718 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2719 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720};
2721
2722template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002723class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724{
2725public:
2726 typedef _Tp element_type;
2727 typedef _Dp deleter_type;
2728 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2729private:
2730 __compressed_pair<pointer, deleter_type> __ptr_;
2731
Howard Hinnant57199402012-01-02 17:56:02 +00002732#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733 unique_ptr(unique_ptr&);
2734 template <class _Up>
2735 unique_ptr(unique_ptr<_Up>&);
2736 unique_ptr& operator=(unique_ptr&);
2737 template <class _Up>
2738 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002739#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740
2741 struct __nat {int __for_bool_;};
2742
2743 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2744 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2745public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002746 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747 : __ptr_(pointer())
2748 {
2749 static_assert(!is_pointer<deleter_type>::value,
2750 "unique_ptr constructed with null function pointer deleter");
2751 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002752 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 : __ptr_(pointer())
2754 {
2755 static_assert(!is_pointer<deleter_type>::value,
2756 "unique_ptr constructed with null function pointer deleter");
2757 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002759 template <class _Pp>
2760 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2761 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 : __ptr_(__p)
2763 {
2764 static_assert(!is_pointer<deleter_type>::value,
2765 "unique_ptr constructed with null function pointer deleter");
2766 }
2767
Logan Chiene1678a12014-01-31 09:30:46 +00002768 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002769 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770 is_reference<deleter_type>::value,
2771 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002772 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2773 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002774 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775 : __ptr_(__p, __d) {}
2776
2777 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2778 is_reference<deleter_type>::value,
2779 deleter_type,
2780 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002781 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782 : __ptr_(pointer(), __d) {}
2783
Logan Chiene1678a12014-01-31 09:30:46 +00002784 template <class _Pp>
2785 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2786 typename remove_reference<deleter_type>::type&& __d,
2787 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002788 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002789 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790 {
2791 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2792 }
2793
2794 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002795 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002796 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797 {
2798 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2799 }
2800
Howard Hinnant1694d232011-05-28 14:41:13 +00002801 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002802 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803
Howard Hinnant1694d232011-05-28 14:41:13 +00002804 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805 {
2806 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002807 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002808 return *this;
2809 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002810
2811 template <class _Up, class _Ep>
2812 _LIBCPP_INLINE_VISIBILITY
2813 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2814 typename enable_if
2815 <
2816 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002817 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002818 && is_convertible<_Ep, deleter_type>::value &&
2819 (
2820 !is_reference<deleter_type>::value ||
2821 is_same<deleter_type, _Ep>::value
2822 ),
2823 __nat
2824 >::type = __nat()
2825 ) _NOEXCEPT
2826 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2827
2828
2829 template <class _Up, class _Ep>
2830 _LIBCPP_INLINE_VISIBILITY
2831 typename enable_if
2832 <
2833 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002834 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2835 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002836 unique_ptr&
2837 >::type
2838 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2839 {
2840 reset(__u.release());
2841 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2842 return *this;
2843 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002844#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845
2846 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2847 : __ptr_(__p)
2848 {
2849 static_assert(!is_pointer<deleter_type>::value,
2850 "unique_ptr constructed with null function pointer deleter");
2851 }
2852
2853 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002854 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002855
2856 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002857 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858
2859 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2860 {
2861 return __rv<unique_ptr>(*this);
2862 }
2863
2864 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002865 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866
2867 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2868 {
2869 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002870 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871 return *this;
2872 }
2873
Howard Hinnant73d21a42010-09-04 23:28:19 +00002874#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2876
Howard Hinnant1694d232011-05-28 14:41:13 +00002877 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878 {
2879 reset();
2880 return *this;
2881 }
2882
2883 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2884 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002885 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2886 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2887 {return __ptr_.second();}
2888 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2889 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002890 _LIBCPP_INLINE_VISIBILITY
2891 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2892 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893
Howard Hinnant1694d232011-05-28 14:41:13 +00002894 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895 {
2896 pointer __t = __ptr_.first();
2897 __ptr_.first() = pointer();
2898 return __t;
2899 }
2900
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002901#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002902 template <class _Pp>
2903 _LIBCPP_INLINE_VISIBILITY
2904 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2905 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 {
2907 pointer __tmp = __ptr_.first();
2908 __ptr_.first() = __p;
2909 if (__tmp)
2910 __ptr_.second()(__tmp);
2911 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002912 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002913 {
2914 pointer __tmp = __ptr_.first();
2915 __ptr_.first() = nullptr;
2916 if (__tmp)
2917 __ptr_.second()(__tmp);
2918 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002919 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
2920 {
2921 pointer __tmp = __ptr_.first();
2922 __ptr_.first() = nullptr;
2923 if (__tmp)
2924 __ptr_.second()(__tmp);
2925 }
2926#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2927 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2928 {
2929 pointer __tmp = __ptr_.first();
2930 __ptr_.first() = __p;
2931 if (__tmp)
2932 __ptr_.second()(__tmp);
2933 }
2934#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002935
2936 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2937private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002938
Howard Hinnant73d21a42010-09-04 23:28:19 +00002939#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002940 template <class _Up>
2941 explicit unique_ptr(_Up);
2942 template <class _Up>
2943 unique_ptr(_Up __u,
2944 typename conditional<
2945 is_reference<deleter_type>::value,
2946 deleter_type,
2947 typename add_lvalue_reference<const deleter_type>::type>::type,
2948 typename enable_if
2949 <
2950 is_convertible<_Up, pointer>::value,
2951 __nat
2952 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002953#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954};
2955
2956template <class _Tp, class _Dp>
2957inline _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002958void
Howard Hinnant1694d232011-05-28 14:41:13 +00002959swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002960
2961template <class _T1, class _D1, class _T2, class _D2>
2962inline _LIBCPP_INLINE_VISIBILITY
2963bool
2964operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2965
2966template <class _T1, class _D1, class _T2, class _D2>
2967inline _LIBCPP_INLINE_VISIBILITY
2968bool
2969operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2970
2971template <class _T1, class _D1, class _T2, class _D2>
2972inline _LIBCPP_INLINE_VISIBILITY
2973bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002974operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2975{
2976 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2977 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002978 typedef typename common_type<_P1, _P2>::type _Vp;
2979 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002980}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981
2982template <class _T1, class _D1, class _T2, class _D2>
2983inline _LIBCPP_INLINE_VISIBILITY
2984bool
2985operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2986
2987template <class _T1, class _D1, class _T2, class _D2>
2988inline _LIBCPP_INLINE_VISIBILITY
2989bool
2990operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2991
2992template <class _T1, class _D1, class _T2, class _D2>
2993inline _LIBCPP_INLINE_VISIBILITY
2994bool
2995operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2996
Howard Hinnant3fadda32012-02-21 21:02:58 +00002997template <class _T1, class _D1>
2998inline _LIBCPP_INLINE_VISIBILITY
2999bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003000operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003001{
3002 return !__x;
3003}
3004
3005template <class _T1, class _D1>
3006inline _LIBCPP_INLINE_VISIBILITY
3007bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003008operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003009{
3010 return !__x;
3011}
3012
3013template <class _T1, class _D1>
3014inline _LIBCPP_INLINE_VISIBILITY
3015bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003016operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003017{
3018 return static_cast<bool>(__x);
3019}
3020
3021template <class _T1, class _D1>
3022inline _LIBCPP_INLINE_VISIBILITY
3023bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003024operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003025{
3026 return static_cast<bool>(__x);
3027}
3028
3029template <class _T1, class _D1>
3030inline _LIBCPP_INLINE_VISIBILITY
3031bool
3032operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3033{
3034 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3035 return less<_P1>()(__x.get(), nullptr);
3036}
3037
3038template <class _T1, class _D1>
3039inline _LIBCPP_INLINE_VISIBILITY
3040bool
3041operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3042{
3043 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3044 return less<_P1>()(nullptr, __x.get());
3045}
3046
3047template <class _T1, class _D1>
3048inline _LIBCPP_INLINE_VISIBILITY
3049bool
3050operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3051{
3052 return nullptr < __x;
3053}
3054
3055template <class _T1, class _D1>
3056inline _LIBCPP_INLINE_VISIBILITY
3057bool
3058operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3059{
3060 return __x < nullptr;
3061}
3062
3063template <class _T1, class _D1>
3064inline _LIBCPP_INLINE_VISIBILITY
3065bool
3066operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3067{
3068 return !(nullptr < __x);
3069}
3070
3071template <class _T1, class _D1>
3072inline _LIBCPP_INLINE_VISIBILITY
3073bool
3074operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3075{
3076 return !(__x < nullptr);
3077}
3078
3079template <class _T1, class _D1>
3080inline _LIBCPP_INLINE_VISIBILITY
3081bool
3082operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3083{
3084 return !(__x < nullptr);
3085}
3086
3087template <class _T1, class _D1>
3088inline _LIBCPP_INLINE_VISIBILITY
3089bool
3090operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3091{
3092 return !(nullptr < __x);
3093}
3094
Howard Hinnant87073e42012-05-01 15:37:54 +00003095#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3096
3097template <class _Tp, class _Dp>
3098inline _LIBCPP_INLINE_VISIBILITY
3099unique_ptr<_Tp, _Dp>
3100move(unique_ptr<_Tp, _Dp>& __t)
3101{
3102 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3103}
3104
3105#endif
3106
Marshall Clowfd7481e2013-07-01 18:16:03 +00003107#if _LIBCPP_STD_VER > 11
3108
3109template<class _Tp>
3110struct __unique_if
3111{
3112 typedef unique_ptr<_Tp> __unique_single;
3113};
3114
3115template<class _Tp>
3116struct __unique_if<_Tp[]>
3117{
3118 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3119};
3120
3121template<class _Tp, size_t _Np>
3122struct __unique_if<_Tp[_Np]>
3123{
3124 typedef void __unique_array_known_bound;
3125};
3126
3127template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003128inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003129typename __unique_if<_Tp>::__unique_single
3130make_unique(_Args&&... __args)
3131{
3132 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3133}
3134
3135template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003136inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003137typename __unique_if<_Tp>::__unique_array_unknown_bound
3138make_unique(size_t __n)
3139{
3140 typedef typename remove_extent<_Tp>::type _Up;
3141 return unique_ptr<_Tp>(new _Up[__n]());
3142}
3143
3144template<class _Tp, class... _Args>
3145 typename __unique_if<_Tp>::__unique_array_known_bound
3146 make_unique(_Args&&...) = delete;
3147
3148#endif // _LIBCPP_STD_VER > 11
3149
Dan Albert1d4a1ed2016-05-25 22:36:09 -07003150template <class _Tp> struct hash;
3151
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003152template <class _Size>
3153inline _LIBCPP_INLINE_VISIBILITY
3154_Size
3155__loadword(const void* __p)
3156{
3157 _Size __r;
3158 std::memcpy(&__r, __p, sizeof(__r));
3159 return __r;
3160}
3161
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003162// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3163// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3164// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003165template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003166struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003167
3168template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003169struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003170{
3171 _Size operator()(const void* __key, _Size __len);
3172};
3173
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003174// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003175template <class _Size>
3176_Size
Dan Albert1d4a1ed2016-05-25 22:36:09 -07003177__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003178{
3179 const _Size __m = 0x5bd1e995;
3180 const _Size __r = 24;
3181 _Size __h = __len;
3182 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3183 for (; __len >= 4; __data += 4, __len -= 4)
3184 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003185 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003186 __k *= __m;
3187 __k ^= __k >> __r;
3188 __k *= __m;
3189 __h *= __m;
3190 __h ^= __k;
3191 }
3192 switch (__len)
3193 {
3194 case 3:
3195 __h ^= __data[2] << 16;
3196 case 2:
3197 __h ^= __data[1] << 8;
3198 case 1:
3199 __h ^= __data[0];
3200 __h *= __m;
3201 }
3202 __h ^= __h >> 13;
3203 __h *= __m;
3204 __h ^= __h >> 15;
3205 return __h;
3206}
3207
3208template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003209struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003210{
3211 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003212
3213 private:
3214 // Some primes between 2^63 and 2^64.
3215 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3216 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3217 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3218 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3219
3220 static _Size __rotate(_Size __val, int __shift) {
3221 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3222 }
3223
3224 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3225 return (__val >> __shift) | (__val << (64 - __shift));
3226 }
3227
3228 static _Size __shift_mix(_Size __val) {
3229 return __val ^ (__val >> 47);
3230 }
3231
3232 static _Size __hash_len_16(_Size __u, _Size __v) {
3233 const _Size __mul = 0x9ddfea08eb382d69ULL;
3234 _Size __a = (__u ^ __v) * __mul;
3235 __a ^= (__a >> 47);
3236 _Size __b = (__v ^ __a) * __mul;
3237 __b ^= (__b >> 47);
3238 __b *= __mul;
3239 return __b;
3240 }
3241
3242 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3243 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003244 const _Size __a = __loadword<_Size>(__s);
3245 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003246 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3247 }
3248 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003249 const uint32_t __a = __loadword<uint32_t>(__s);
3250 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003251 return __hash_len_16(__len + (__a << 3), __b);
3252 }
3253 if (__len > 0) {
3254 const unsigned char __a = __s[0];
3255 const unsigned char __b = __s[__len >> 1];
3256 const unsigned char __c = __s[__len - 1];
3257 const uint32_t __y = static_cast<uint32_t>(__a) +
3258 (static_cast<uint32_t>(__b) << 8);
3259 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3260 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3261 }
3262 return __k2;
3263 }
3264
3265 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003266 const _Size __a = __loadword<_Size>(__s) * __k1;
3267 const _Size __b = __loadword<_Size>(__s + 8);
3268 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3269 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003270 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3271 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3272 }
3273
3274 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3275 // Callers do best to use "random-looking" values for a and b.
3276 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3277 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3278 __a += __w;
3279 __b = __rotate(__b + __a + __z, 21);
3280 const _Size __c = __a;
3281 __a += __x;
3282 __a += __y;
3283 __b += __rotate(__a, 44);
3284 return pair<_Size, _Size>(__a + __z, __b + __c);
3285 }
3286
3287 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3288 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3289 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003290 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3291 __loadword<_Size>(__s + 8),
3292 __loadword<_Size>(__s + 16),
3293 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003294 __a,
3295 __b);
3296 }
3297
3298 // Return an 8-byte hash for 33 to 64 bytes.
3299 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003300 _Size __z = __loadword<_Size>(__s + 24);
3301 _Size __a = __loadword<_Size>(__s) +
3302 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003303 _Size __b = __rotate(__a + __z, 52);
3304 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003305 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003306 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003307 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003308 _Size __vf = __a + __z;
3309 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003310 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3311 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003312 __b = __rotate(__a + __z, 52);
3313 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003314 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003315 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003316 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003317 _Size __wf = __a + __z;
3318 _Size __ws = __b + __rotate(__a, 31) + __c;
3319 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3320 return __shift_mix(__r * __k0 + __vs) * __k2;
3321 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003322};
3323
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003324// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003325template <class _Size>
3326_Size
Dan Albert1d4a1ed2016-05-25 22:36:09 -07003327__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003328{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003329 const char* __s = static_cast<const char*>(__key);
3330 if (__len <= 32) {
3331 if (__len <= 16) {
3332 return __hash_len_0_to_16(__s, __len);
3333 } else {
3334 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003335 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003336 } else if (__len <= 64) {
3337 return __hash_len_33_to_64(__s, __len);
3338 }
3339
3340 // For strings over 64 bytes we hash the end first, and then as we
3341 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003342 _Size __x = __loadword<_Size>(__s + __len - 40);
3343 _Size __y = __loadword<_Size>(__s + __len - 16) +
3344 __loadword<_Size>(__s + __len - 56);
3345 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3346 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003347 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3348 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003349 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003350
3351 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3352 __len = (__len - 1) & ~static_cast<_Size>(63);
3353 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003354 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3355 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003356 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003357 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003358 __z = __rotate(__z + __w.first, 33) * __k1;
3359 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3360 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003361 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003362 std::swap(__z, __x);
3363 __s += 64;
3364 __len -= 64;
3365 } while (__len != 0);
3366 return __hash_len_16(
3367 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3368 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003369}
3370
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003371template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3372struct __scalar_hash;
3373
3374template <class _Tp>
3375struct __scalar_hash<_Tp, 0>
3376 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003377{
Howard Hinnant82894812010-09-22 16:48:34 +00003378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003379 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003380 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003381 union
3382 {
3383 _Tp __t;
3384 size_t __a;
3385 } __u;
3386 __u.__a = 0;
3387 __u.__t = __v;
3388 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003389 }
3390};
3391
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003392template <class _Tp>
3393struct __scalar_hash<_Tp, 1>
3394 : public unary_function<_Tp, size_t>
3395{
3396 _LIBCPP_INLINE_VISIBILITY
3397 size_t operator()(_Tp __v) const _NOEXCEPT
3398 {
3399 union
3400 {
3401 _Tp __t;
3402 size_t __a;
3403 } __u;
3404 __u.__t = __v;
3405 return __u.__a;
3406 }
3407};
3408
3409template <class _Tp>
3410struct __scalar_hash<_Tp, 2>
3411 : public unary_function<_Tp, size_t>
3412{
3413 _LIBCPP_INLINE_VISIBILITY
3414 size_t operator()(_Tp __v) const _NOEXCEPT
3415 {
3416 union
3417 {
3418 _Tp __t;
3419 struct
3420 {
3421 size_t __a;
3422 size_t __b;
Eric Fiselier692177d2015-07-18 20:40:46 +00003423 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003424 } __u;
3425 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003426 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003427 }
3428};
3429
3430template <class _Tp>
3431struct __scalar_hash<_Tp, 3>
3432 : public unary_function<_Tp, size_t>
3433{
3434 _LIBCPP_INLINE_VISIBILITY
3435 size_t operator()(_Tp __v) const _NOEXCEPT
3436 {
3437 union
3438 {
3439 _Tp __t;
3440 struct
3441 {
3442 size_t __a;
3443 size_t __b;
3444 size_t __c;
Eric Fiselier692177d2015-07-18 20:40:46 +00003445 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003446 } __u;
3447 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003448 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003449 }
3450};
3451
3452template <class _Tp>
3453struct __scalar_hash<_Tp, 4>
3454 : public unary_function<_Tp, size_t>
3455{
3456 _LIBCPP_INLINE_VISIBILITY
3457 size_t operator()(_Tp __v) const _NOEXCEPT
3458 {
3459 union
3460 {
3461 _Tp __t;
3462 struct
3463 {
3464 size_t __a;
3465 size_t __b;
3466 size_t __c;
3467 size_t __d;
Eric Fiselier692177d2015-07-18 20:40:46 +00003468 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003469 } __u;
3470 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003471 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003472 }
3473};
3474
3475template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003476struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003477 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003478{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003479 _LIBCPP_INLINE_VISIBILITY
3480 size_t operator()(_Tp* __v) const _NOEXCEPT
3481 {
3482 union
3483 {
3484 _Tp* __t;
3485 size_t __a;
3486 } __u;
3487 __u.__t = __v;
3488 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3489 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003490};
3491
Howard Hinnant21aefc32010-06-03 16:42:57 +00003492template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003493struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003494{
3495 typedef unique_ptr<_Tp, _Dp> argument_type;
3496 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003498 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003499 {
3500 typedef typename argument_type::pointer pointer;
3501 return hash<pointer>()(__ptr.get());
3502 }
3503};
3504
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003505struct __destruct_n
3506{
3507private:
3508 size_t size;
3509
3510 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003511 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3513
3514 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003515 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003516 {}
3517
Howard Hinnant1694d232011-05-28 14:41:13 +00003518 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003519 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003520 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003521 {}
3522
Howard Hinnant1694d232011-05-28 14:41:13 +00003523 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003524 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003525 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003526 {}
3527public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003528 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3529 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530
3531 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003532 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003533 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003534
3535 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003536 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003537 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003538
3539 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003540 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003541 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003542};
3543
3544template <class _Alloc>
3545class __allocator_destructor
3546{
3547 typedef allocator_traits<_Alloc> __alloc_traits;
3548public:
3549 typedef typename __alloc_traits::pointer pointer;
3550 typedef typename __alloc_traits::size_type size_type;
3551private:
3552 _Alloc& __alloc_;
3553 size_type __s_;
3554public:
3555 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003556 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003557 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003559 void operator()(pointer __p) _NOEXCEPT
3560 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003561};
3562
3563template <class _InputIterator, class _ForwardIterator>
3564_ForwardIterator
3565uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3566{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003567 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003568#ifndef _LIBCPP_NO_EXCEPTIONS
3569 _ForwardIterator __s = __r;
3570 try
3571 {
3572#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003573 for (; __f != __l; ++__f, (void) ++__r)
3574 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003575#ifndef _LIBCPP_NO_EXCEPTIONS
3576 }
3577 catch (...)
3578 {
3579 for (; __s != __r; ++__s)
3580 __s->~value_type();
3581 throw;
3582 }
3583#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003584 return __r;
3585}
3586
3587template <class _InputIterator, class _Size, class _ForwardIterator>
3588_ForwardIterator
3589uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3590{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003591 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003592#ifndef _LIBCPP_NO_EXCEPTIONS
3593 _ForwardIterator __s = __r;
3594 try
3595 {
3596#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003597 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3598 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003599#ifndef _LIBCPP_NO_EXCEPTIONS
3600 }
3601 catch (...)
3602 {
3603 for (; __s != __r; ++__s)
3604 __s->~value_type();
3605 throw;
3606 }
3607#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608 return __r;
3609}
3610
3611template <class _ForwardIterator, class _Tp>
3612void
3613uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3614{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003616#ifndef _LIBCPP_NO_EXCEPTIONS
3617 _ForwardIterator __s = __f;
3618 try
3619 {
3620#endif
3621 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003622 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003623#ifndef _LIBCPP_NO_EXCEPTIONS
3624 }
3625 catch (...)
3626 {
3627 for (; __s != __f; ++__s)
3628 __s->~value_type();
3629 throw;
3630 }
3631#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632}
3633
3634template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003635_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003636uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3637{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003639#ifndef _LIBCPP_NO_EXCEPTIONS
3640 _ForwardIterator __s = __f;
3641 try
3642 {
3643#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003644 for (; __n > 0; ++__f, (void) --__n)
3645 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003646#ifndef _LIBCPP_NO_EXCEPTIONS
3647 }
3648 catch (...)
3649 {
3650 for (; __s != __f; ++__s)
3651 __s->~value_type();
3652 throw;
3653 }
3654#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003655 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656}
3657
Howard Hinnant82894812010-09-22 16:48:34 +00003658class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003659 : public std::exception
3660{
3661public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003662 virtual ~bad_weak_ptr() _NOEXCEPT;
3663 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003664};
3665
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003666template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003667
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003668class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003669{
3670 __shared_count(const __shared_count&);
3671 __shared_count& operator=(const __shared_count&);
3672
3673protected:
3674 long __shared_owners_;
3675 virtual ~__shared_count();
3676private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003677 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678
3679public:
Howard Hinnant82894812010-09-22 16:48:34 +00003680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003681 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003682 : __shared_owners_(__refs) {}
3683
Howard Hinnant1694d232011-05-28 14:41:13 +00003684 void __add_shared() _NOEXCEPT;
3685 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003686 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc6e46692015-07-07 00:27:16 +00003687 long use_count() const _NOEXCEPT {
3688 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3689 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690};
3691
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003692class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003693 : private __shared_count
3694{
3695 long __shared_weak_owners_;
3696
3697public:
Howard Hinnant82894812010-09-22 16:48:34 +00003698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003699 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003700 : __shared_count(__refs),
3701 __shared_weak_owners_(__refs) {}
3702protected:
3703 virtual ~__shared_weak_count();
3704
3705public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003706 void __add_shared() _NOEXCEPT;
3707 void __add_weak() _NOEXCEPT;
3708 void __release_shared() _NOEXCEPT;
3709 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003711 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3712 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003714 // Define the function out only if we build static libc++ without RTTI.
3715 // Otherwise we may break clients who need to compile their projects with
3716 // -fno-rtti and yet link against a libc++.dylib compiled
3717 // without -fno-rtti.
3718#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003719 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003720#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003721private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003722 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723};
3724
3725template <class _Tp, class _Dp, class _Alloc>
3726class __shared_ptr_pointer
3727 : public __shared_weak_count
3728{
3729 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3730public:
Howard Hinnant82894812010-09-22 16:48:34 +00003731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003733 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003734
Howard Hinnantd4444702010-08-11 17:04:31 +00003735#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003736 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003737#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003738
3739private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003740 virtual void __on_zero_shared() _NOEXCEPT;
3741 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003742};
3743
Howard Hinnantd4444702010-08-11 17:04:31 +00003744#ifndef _LIBCPP_NO_RTTI
3745
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746template <class _Tp, class _Dp, class _Alloc>
3747const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003748__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003750 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751}
3752
Howard Hinnant324bb032010-08-22 00:02:43 +00003753#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003754
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755template <class _Tp, class _Dp, class _Alloc>
3756void
Howard Hinnant1694d232011-05-28 14:41:13 +00003757__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758{
3759 __data_.first().second()(__data_.first().first());
3760 __data_.first().second().~_Dp();
3761}
3762
3763template <class _Tp, class _Dp, class _Alloc>
3764void
Howard Hinnant1694d232011-05-28 14:41:13 +00003765__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003767 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3768 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003769 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3770
Eric Fiselier8492cd82015-02-05 23:01:40 +00003771 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003773 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774}
3775
3776template <class _Tp, class _Alloc>
3777class __shared_ptr_emplace
3778 : public __shared_weak_count
3779{
3780 __compressed_pair<_Alloc, _Tp> __data_;
3781public:
3782#ifndef _LIBCPP_HAS_NO_VARIADICS
3783
Howard Hinnant82894812010-09-22 16:48:34 +00003784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003785 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003786 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787
3788 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003791 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3792 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793
3794#else // _LIBCPP_HAS_NO_VARIADICS
3795
Howard Hinnant82894812010-09-22 16:48:34 +00003796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797 __shared_ptr_emplace(_Alloc __a)
3798 : __data_(__a) {}
3799
3800 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003802 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3803 : __data_(__a, _Tp(__a0)) {}
3804
3805 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003807 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3808 : __data_(__a, _Tp(__a0, __a1)) {}
3809
3810 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003812 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3813 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3814
3815#endif // _LIBCPP_HAS_NO_VARIADICS
3816
3817private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003818 virtual void __on_zero_shared() _NOEXCEPT;
3819 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003820public:
Howard Hinnant82894812010-09-22 16:48:34 +00003821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003822 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003823};
3824
3825template <class _Tp, class _Alloc>
3826void
Howard Hinnant1694d232011-05-28 14:41:13 +00003827__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003828{
3829 __data_.second().~_Tp();
3830}
3831
3832template <class _Tp, class _Alloc>
3833void
Howard Hinnant1694d232011-05-28 14:41:13 +00003834__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003836 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3837 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003838 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003839 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003840 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003841 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003842}
3843
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003844template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003845
3846template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003847class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003848{
Howard Hinnant324bb032010-08-22 00:02:43 +00003849public:
3850 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003851private:
3852 element_type* __ptr_;
3853 __shared_weak_count* __cntrl_;
3854
3855 struct __nat {int __for_bool_;};
3856public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003857 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3858 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003859 template<class _Yp>
3860 explicit shared_ptr(_Yp* __p,
3861 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3862 template<class _Yp, class _Dp>
3863 shared_ptr(_Yp* __p, _Dp __d,
3864 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3865 template<class _Yp, class _Dp, class _Alloc>
3866 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3867 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3869 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Dan Albert1d4a1ed2016-05-25 22:36:09 -07003870 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
Howard Hinnant1694d232011-05-28 14:41:13 +00003871 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003872 template<class _Yp>
3873 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003874 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3875 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003876#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003877 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07003878 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003879 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3880 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003881#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003882 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003883 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003884#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003885 template<class _Yp>
3886 shared_ptr(auto_ptr<_Yp>&& __r,
3887 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003888#else
Logan Chiene1678a12014-01-31 09:30:46 +00003889 template<class _Yp>
3890 shared_ptr(auto_ptr<_Yp> __r,
3891 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003893#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003894 template <class _Yp, class _Dp>
3895 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3896 typename enable_if
3897 <
3898 !is_lvalue_reference<_Dp>::value &&
3899 !is_array<_Yp>::value &&
3900 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3901 __nat
3902 >::type = __nat());
3903 template <class _Yp, class _Dp>
3904 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3905 typename enable_if
3906 <
3907 is_lvalue_reference<_Dp>::value &&
3908 !is_array<_Yp>::value &&
3909 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3910 __nat
3911 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003912#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003913 template <class _Yp, class _Dp>
3914 shared_ptr(unique_ptr<_Yp, _Dp>,
3915 typename enable_if
3916 <
3917 !is_lvalue_reference<_Dp>::value &&
3918 !is_array<_Yp>::value &&
3919 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3920 __nat
3921 >::type = __nat());
3922 template <class _Yp, class _Dp>
3923 shared_ptr(unique_ptr<_Yp, _Dp>,
3924 typename enable_if
3925 <
3926 is_lvalue_reference<_Dp>::value &&
3927 !is_array<_Yp>::value &&
3928 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3929 __nat
3930 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003931#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003932
3933 ~shared_ptr();
3934
Howard Hinnant1694d232011-05-28 14:41:13 +00003935 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003936 template<class _Yp>
3937 typename enable_if
3938 <
3939 is_convertible<_Yp*, element_type*>::value,
3940 shared_ptr&
3941 >::type
3942 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003943#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003944 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003945 template<class _Yp>
3946 typename enable_if
3947 <
3948 is_convertible<_Yp*, element_type*>::value,
3949 shared_ptr<_Tp>&
3950 >::type
3951 operator=(shared_ptr<_Yp>&& __r);
3952 template<class _Yp>
3953 typename enable_if
3954 <
3955 !is_array<_Yp>::value &&
3956 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003957 shared_ptr
3958 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003959 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003960#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003961 template<class _Yp>
3962 typename enable_if
3963 <
3964 !is_array<_Yp>::value &&
3965 is_convertible<_Yp*, element_type*>::value,
3966 shared_ptr&
3967 >::type
3968 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003969#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003970 template <class _Yp, class _Dp>
3971 typename enable_if
3972 <
3973 !is_array<_Yp>::value &&
3974 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3975 shared_ptr&
3976 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003977#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003978 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003979#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003980 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003981#endif
3982
Howard Hinnant1694d232011-05-28 14:41:13 +00003983 void swap(shared_ptr& __r) _NOEXCEPT;
3984 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003985 template<class _Yp>
3986 typename enable_if
3987 <
3988 is_convertible<_Yp*, element_type*>::value,
3989 void
3990 >::type
3991 reset(_Yp* __p);
3992 template<class _Yp, class _Dp>
3993 typename enable_if
3994 <
3995 is_convertible<_Yp*, element_type*>::value,
3996 void
3997 >::type
3998 reset(_Yp* __p, _Dp __d);
3999 template<class _Yp, class _Dp, class _Alloc>
4000 typename enable_if
4001 <
4002 is_convertible<_Yp*, element_type*>::value,
4003 void
4004 >::type
4005 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004006
Howard Hinnant82894812010-09-22 16:48:34 +00004007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004008 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004010 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4011 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004013 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004015 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004017 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004019 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00004020 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004022 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004023 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004024 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004026 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004027 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004028 _LIBCPP_INLINE_VISIBILITY
4029 bool
4030 __owner_equivalent(const shared_ptr& __p) const
4031 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004032
Howard Hinnantd4444702010-08-11 17:04:31 +00004033#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004034 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004036 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004037 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004038#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004039
4040#ifndef _LIBCPP_HAS_NO_VARIADICS
4041
4042 template<class ..._Args>
4043 static
4044 shared_ptr<_Tp>
4045 make_shared(_Args&& ...__args);
4046
4047 template<class _Alloc, class ..._Args>
4048 static
4049 shared_ptr<_Tp>
4050 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4051
4052#else // _LIBCPP_HAS_NO_VARIADICS
4053
4054 static shared_ptr<_Tp> make_shared();
4055
4056 template<class _A0>
4057 static shared_ptr<_Tp> make_shared(_A0&);
4058
4059 template<class _A0, class _A1>
4060 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4061
4062 template<class _A0, class _A1, class _A2>
4063 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4064
4065 template<class _Alloc>
4066 static shared_ptr<_Tp>
4067 allocate_shared(const _Alloc& __a);
4068
4069 template<class _Alloc, class _A0>
4070 static shared_ptr<_Tp>
4071 allocate_shared(const _Alloc& __a, _A0& __a0);
4072
4073 template<class _Alloc, class _A0, class _A1>
4074 static shared_ptr<_Tp>
4075 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4076
4077 template<class _Alloc, class _A0, class _A1, class _A2>
4078 static shared_ptr<_Tp>
4079 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4080
4081#endif // _LIBCPP_HAS_NO_VARIADICS
4082
4083private:
4084
4085 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004087 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004088 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004089 {
4090 if (__e)
Marshall Clowc4113372015-06-19 15:54:13 +00004091 {
4092 __e->__weak_this_.__ptr_ = const_cast<_Yp*>(static_cast<const _Yp*>(__e));
4093 __e->__weak_this_.__cntrl_ = __cntrl_;
Marshall Clow46d06b92015-06-19 19:32:06 +00004094 __cntrl_->__add_weak();
Marshall Clowc4113372015-06-19 15:54:13 +00004095 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004096 }
4097
Howard Hinnant82894812010-09-22 16:48:34 +00004098 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004099 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004100
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004101 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4102 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004103};
4104
4105template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004106inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004107_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004108shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004109 : __ptr_(0),
4110 __cntrl_(0)
4111{
4112}
4113
4114template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004115inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004116_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004117shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004118 : __ptr_(0),
4119 __cntrl_(0)
4120{
4121}
4122
4123template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004124template<class _Yp>
4125shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4126 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004127 : __ptr_(__p)
4128{
4129 unique_ptr<_Yp> __hold(__p);
4130 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4131 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4132 __hold.release();
4133 __enable_weak_this(__p);
4134}
4135
4136template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004137template<class _Yp, class _Dp>
4138shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4139 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004140 : __ptr_(__p)
4141{
4142#ifndef _LIBCPP_NO_EXCEPTIONS
4143 try
4144 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004145#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004146 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4147 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4148 __enable_weak_this(__p);
4149#ifndef _LIBCPP_NO_EXCEPTIONS
4150 }
4151 catch (...)
4152 {
4153 __d(__p);
4154 throw;
4155 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004156#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004157}
4158
4159template<class _Tp>
4160template<class _Dp>
4161shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4162 : __ptr_(0)
4163{
4164#ifndef _LIBCPP_NO_EXCEPTIONS
4165 try
4166 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004167#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004168 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4169 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4170#ifndef _LIBCPP_NO_EXCEPTIONS
4171 }
4172 catch (...)
4173 {
4174 __d(__p);
4175 throw;
4176 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004177#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004178}
4179
4180template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004181template<class _Yp, class _Dp, class _Alloc>
4182shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4183 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004184 : __ptr_(__p)
4185{
4186#ifndef _LIBCPP_NO_EXCEPTIONS
4187 try
4188 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004189#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004190 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004191 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004192 typedef __allocator_destructor<_A2> _D2;
4193 _A2 __a2(__a);
4194 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004195 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4196 _CntrlBlk(__p, __d, __a);
4197 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004198 __enable_weak_this(__p);
4199#ifndef _LIBCPP_NO_EXCEPTIONS
4200 }
4201 catch (...)
4202 {
4203 __d(__p);
4204 throw;
4205 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004206#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004207}
4208
4209template<class _Tp>
4210template<class _Dp, class _Alloc>
4211shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4212 : __ptr_(0)
4213{
4214#ifndef _LIBCPP_NO_EXCEPTIONS
4215 try
4216 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004217#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004218 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004219 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004220 typedef __allocator_destructor<_A2> _D2;
4221 _A2 __a2(__a);
4222 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004223 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4224 _CntrlBlk(__p, __d, __a);
4225 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004226#ifndef _LIBCPP_NO_EXCEPTIONS
4227 }
4228 catch (...)
4229 {
4230 __d(__p);
4231 throw;
4232 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004233#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004234}
4235
4236template<class _Tp>
4237template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004238inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004239shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004240 : __ptr_(__p),
4241 __cntrl_(__r.__cntrl_)
4242{
4243 if (__cntrl_)
4244 __cntrl_->__add_shared();
4245}
4246
4247template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004249shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004250 : __ptr_(__r.__ptr_),
4251 __cntrl_(__r.__cntrl_)
4252{
4253 if (__cntrl_)
4254 __cntrl_->__add_shared();
4255}
4256
4257template<class _Tp>
4258template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004260shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4261 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004262 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004263 : __ptr_(__r.__ptr_),
4264 __cntrl_(__r.__cntrl_)
4265{
4266 if (__cntrl_)
4267 __cntrl_->__add_shared();
4268}
4269
Howard Hinnant73d21a42010-09-04 23:28:19 +00004270#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004271
4272template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004274shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004275 : __ptr_(__r.__ptr_),
4276 __cntrl_(__r.__cntrl_)
4277{
4278 __r.__ptr_ = 0;
4279 __r.__cntrl_ = 0;
4280}
4281
4282template<class _Tp>
4283template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004284inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004285shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4286 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004287 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004288 : __ptr_(__r.__ptr_),
4289 __cntrl_(__r.__cntrl_)
4290{
4291 __r.__ptr_ = 0;
4292 __r.__cntrl_ = 0;
4293}
4294
Howard Hinnant73d21a42010-09-04 23:28:19 +00004295#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004296
4297template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004298template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004299#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004300shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004301#else
Logan Chiene1678a12014-01-31 09:30:46 +00004302shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004303#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004304 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004305 : __ptr_(__r.get())
4306{
4307 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4308 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4309 __enable_weak_this(__r.get());
4310 __r.release();
4311}
4312
4313template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004314template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004315#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004316shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4317#else
4318shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4319#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004320 typename enable_if
4321 <
4322 !is_lvalue_reference<_Dp>::value &&
4323 !is_array<_Yp>::value &&
4324 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4325 __nat
4326 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004327 : __ptr_(__r.get())
4328{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004329#if _LIBCPP_STD_VER > 11
4330 if (__ptr_ == nullptr)
4331 __cntrl_ = nullptr;
4332 else
4333#endif
4334 {
4335 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4336 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4337 __enable_weak_this(__r.get());
4338 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004339 __r.release();
4340}
4341
4342template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004343template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004344#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004345shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4346#else
4347shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4348#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004349 typename enable_if
4350 <
4351 is_lvalue_reference<_Dp>::value &&
4352 !is_array<_Yp>::value &&
4353 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4354 __nat
4355 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004356 : __ptr_(__r.get())
4357{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004358#if _LIBCPP_STD_VER > 11
4359 if (__ptr_ == nullptr)
4360 __cntrl_ = nullptr;
4361 else
4362#endif
4363 {
4364 typedef __shared_ptr_pointer<_Yp*,
4365 reference_wrapper<typename remove_reference<_Dp>::type>,
4366 allocator<_Yp> > _CntrlBlk;
4367 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4368 __enable_weak_this(__r.get());
4369 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004370 __r.release();
4371}
4372
4373#ifndef _LIBCPP_HAS_NO_VARIADICS
4374
4375template<class _Tp>
4376template<class ..._Args>
4377shared_ptr<_Tp>
4378shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4379{
4380 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4381 typedef allocator<_CntrlBlk> _A2;
4382 typedef __allocator_destructor<_A2> _D2;
4383 _A2 __a2;
4384 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004385 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004386 shared_ptr<_Tp> __r;
4387 __r.__ptr_ = __hold2.get()->get();
4388 __r.__cntrl_ = __hold2.release();
4389 __r.__enable_weak_this(__r.__ptr_);
4390 return __r;
4391}
4392
4393template<class _Tp>
4394template<class _Alloc, class ..._Args>
4395shared_ptr<_Tp>
4396shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4397{
4398 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004399 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004400 typedef __allocator_destructor<_A2> _D2;
4401 _A2 __a2(__a);
4402 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004403 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4404 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004405 shared_ptr<_Tp> __r;
4406 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004407 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004408 __r.__enable_weak_this(__r.__ptr_);
4409 return __r;
4410}
4411
4412#else // _LIBCPP_HAS_NO_VARIADICS
4413
4414template<class _Tp>
4415shared_ptr<_Tp>
4416shared_ptr<_Tp>::make_shared()
4417{
4418 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4419 typedef allocator<_CntrlBlk> _Alloc2;
4420 typedef __allocator_destructor<_Alloc2> _D2;
4421 _Alloc2 __alloc2;
4422 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4423 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4424 shared_ptr<_Tp> __r;
4425 __r.__ptr_ = __hold2.get()->get();
4426 __r.__cntrl_ = __hold2.release();
4427 __r.__enable_weak_this(__r.__ptr_);
4428 return __r;
4429}
4430
4431template<class _Tp>
4432template<class _A0>
4433shared_ptr<_Tp>
4434shared_ptr<_Tp>::make_shared(_A0& __a0)
4435{
4436 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4437 typedef allocator<_CntrlBlk> _Alloc2;
4438 typedef __allocator_destructor<_Alloc2> _D2;
4439 _Alloc2 __alloc2;
4440 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4441 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4442 shared_ptr<_Tp> __r;
4443 __r.__ptr_ = __hold2.get()->get();
4444 __r.__cntrl_ = __hold2.release();
4445 __r.__enable_weak_this(__r.__ptr_);
4446 return __r;
4447}
4448
4449template<class _Tp>
4450template<class _A0, class _A1>
4451shared_ptr<_Tp>
4452shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4453{
4454 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4455 typedef allocator<_CntrlBlk> _Alloc2;
4456 typedef __allocator_destructor<_Alloc2> _D2;
4457 _Alloc2 __alloc2;
4458 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4459 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4460 shared_ptr<_Tp> __r;
4461 __r.__ptr_ = __hold2.get()->get();
4462 __r.__cntrl_ = __hold2.release();
4463 __r.__enable_weak_this(__r.__ptr_);
4464 return __r;
4465}
4466
4467template<class _Tp>
4468template<class _A0, class _A1, class _A2>
4469shared_ptr<_Tp>
4470shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4471{
4472 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4473 typedef allocator<_CntrlBlk> _Alloc2;
4474 typedef __allocator_destructor<_Alloc2> _D2;
4475 _Alloc2 __alloc2;
4476 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4477 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4478 shared_ptr<_Tp> __r;
4479 __r.__ptr_ = __hold2.get()->get();
4480 __r.__cntrl_ = __hold2.release();
4481 __r.__enable_weak_this(__r.__ptr_);
4482 return __r;
4483}
4484
4485template<class _Tp>
4486template<class _Alloc>
4487shared_ptr<_Tp>
4488shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4489{
4490 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004491 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004492 typedef __allocator_destructor<_Alloc2> _D2;
4493 _Alloc2 __alloc2(__a);
4494 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004495 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4496 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004497 shared_ptr<_Tp> __r;
4498 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004499 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004500 __r.__enable_weak_this(__r.__ptr_);
4501 return __r;
4502}
4503
4504template<class _Tp>
4505template<class _Alloc, class _A0>
4506shared_ptr<_Tp>
4507shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4508{
4509 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004510 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004511 typedef __allocator_destructor<_Alloc2> _D2;
4512 _Alloc2 __alloc2(__a);
4513 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004514 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4515 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004516 shared_ptr<_Tp> __r;
4517 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004518 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004519 __r.__enable_weak_this(__r.__ptr_);
4520 return __r;
4521}
4522
4523template<class _Tp>
4524template<class _Alloc, class _A0, class _A1>
4525shared_ptr<_Tp>
4526shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4527{
4528 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004529 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004530 typedef __allocator_destructor<_Alloc2> _D2;
4531 _Alloc2 __alloc2(__a);
4532 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004533 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4534 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004535 shared_ptr<_Tp> __r;
4536 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004537 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004538 __r.__enable_weak_this(__r.__ptr_);
4539 return __r;
4540}
4541
4542template<class _Tp>
4543template<class _Alloc, class _A0, class _A1, class _A2>
4544shared_ptr<_Tp>
4545shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4546{
4547 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004548 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004549 typedef __allocator_destructor<_Alloc2> _D2;
4550 _Alloc2 __alloc2(__a);
4551 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004552 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4553 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004554 shared_ptr<_Tp> __r;
4555 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004556 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004557 __r.__enable_weak_this(__r.__ptr_);
4558 return __r;
4559}
4560
4561#endif // _LIBCPP_HAS_NO_VARIADICS
4562
4563template<class _Tp>
4564shared_ptr<_Tp>::~shared_ptr()
4565{
4566 if (__cntrl_)
4567 __cntrl_->__release_shared();
4568}
4569
4570template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004571inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004572shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004573shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004574{
4575 shared_ptr(__r).swap(*this);
4576 return *this;
4577}
4578
4579template<class _Tp>
4580template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004581inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004582typename enable_if
4583<
4584 is_convertible<_Yp*, _Tp*>::value,
4585 shared_ptr<_Tp>&
4586>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004587shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004588{
4589 shared_ptr(__r).swap(*this);
4590 return *this;
4591}
4592
Howard Hinnant73d21a42010-09-04 23:28:19 +00004593#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004594
4595template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004596inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004597shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004598shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004599{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004600 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004601 return *this;
4602}
4603
4604template<class _Tp>
4605template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004606inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004607typename enable_if
4608<
4609 is_convertible<_Yp*, _Tp*>::value,
4610 shared_ptr<_Tp>&
4611>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004612shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4613{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004614 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004615 return *this;
4616}
4617
4618template<class _Tp>
4619template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004620inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004621typename enable_if
4622<
4623 !is_array<_Yp>::value &&
4624 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004625 shared_ptr<_Tp>
4626>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004627shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4628{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004629 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004630 return *this;
4631}
4632
4633template<class _Tp>
4634template <class _Yp, class _Dp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004636typename enable_if
4637<
4638 !is_array<_Yp>::value &&
4639 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4640 shared_ptr<_Tp>&
4641>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004642shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4643{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004644 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645 return *this;
4646}
4647
Howard Hinnant73d21a42010-09-04 23:28:19 +00004648#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004649
4650template<class _Tp>
4651template<class _Yp>
4652inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004653typename enable_if
4654<
4655 !is_array<_Yp>::value &&
4656 is_convertible<_Yp*, _Tp*>::value,
4657 shared_ptr<_Tp>&
4658>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004659shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004660{
4661 shared_ptr(__r).swap(*this);
4662 return *this;
4663}
4664
4665template<class _Tp>
4666template <class _Yp, class _Dp>
4667inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004668typename enable_if
4669<
4670 !is_array<_Yp>::value &&
4671 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4672 shared_ptr<_Tp>&
4673>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004674shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4675{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004676 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004677 return *this;
4678}
4679
Howard Hinnant73d21a42010-09-04 23:28:19 +00004680#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004681
4682template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004683inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004684void
Howard Hinnant1694d232011-05-28 14:41:13 +00004685shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004686{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004687 _VSTD::swap(__ptr_, __r.__ptr_);
4688 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004689}
4690
4691template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004692inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004693void
Howard Hinnant1694d232011-05-28 14:41:13 +00004694shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004695{
4696 shared_ptr().swap(*this);
4697}
4698
4699template<class _Tp>
4700template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004701inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004702typename enable_if
4703<
4704 is_convertible<_Yp*, _Tp*>::value,
4705 void
4706>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004707shared_ptr<_Tp>::reset(_Yp* __p)
4708{
4709 shared_ptr(__p).swap(*this);
4710}
4711
4712template<class _Tp>
4713template<class _Yp, class _Dp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004714inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004715typename enable_if
4716<
4717 is_convertible<_Yp*, _Tp*>::value,
4718 void
4719>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004720shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4721{
4722 shared_ptr(__p, __d).swap(*this);
4723}
4724
4725template<class _Tp>
4726template<class _Yp, class _Dp, class _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07004727inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004728typename enable_if
4729<
4730 is_convertible<_Yp*, _Tp*>::value,
4731 void
4732>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004733shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4734{
4735 shared_ptr(__p, __d, __a).swap(*this);
4736}
4737
4738#ifndef _LIBCPP_HAS_NO_VARIADICS
4739
Howard Hinnant324bb032010-08-22 00:02:43 +00004740template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004741inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004742typename enable_if
4743<
4744 !is_array<_Tp>::value,
4745 shared_ptr<_Tp>
4746>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004747make_shared(_Args&& ...__args)
4748{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004749 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004750}
4751
Howard Hinnant324bb032010-08-22 00:02:43 +00004752template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004753inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004754typename enable_if
4755<
4756 !is_array<_Tp>::value,
4757 shared_ptr<_Tp>
4758>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004759allocate_shared(const _Alloc& __a, _Args&& ...__args)
4760{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004761 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004762}
4763
4764#else // _LIBCPP_HAS_NO_VARIADICS
4765
4766template<class _Tp>
4767inline _LIBCPP_INLINE_VISIBILITY
4768shared_ptr<_Tp>
4769make_shared()
4770{
4771 return shared_ptr<_Tp>::make_shared();
4772}
4773
4774template<class _Tp, class _A0>
4775inline _LIBCPP_INLINE_VISIBILITY
4776shared_ptr<_Tp>
4777make_shared(_A0& __a0)
4778{
4779 return shared_ptr<_Tp>::make_shared(__a0);
4780}
4781
4782template<class _Tp, class _A0, class _A1>
4783inline _LIBCPP_INLINE_VISIBILITY
4784shared_ptr<_Tp>
4785make_shared(_A0& __a0, _A1& __a1)
4786{
4787 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4788}
4789
Howard Hinnant324bb032010-08-22 00:02:43 +00004790template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004791inline _LIBCPP_INLINE_VISIBILITY
4792shared_ptr<_Tp>
4793make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4794{
4795 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4796}
4797
4798template<class _Tp, class _Alloc>
4799inline _LIBCPP_INLINE_VISIBILITY
4800shared_ptr<_Tp>
4801allocate_shared(const _Alloc& __a)
4802{
4803 return shared_ptr<_Tp>::allocate_shared(__a);
4804}
4805
4806template<class _Tp, class _Alloc, class _A0>
4807inline _LIBCPP_INLINE_VISIBILITY
4808shared_ptr<_Tp>
4809allocate_shared(const _Alloc& __a, _A0& __a0)
4810{
4811 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4812}
4813
4814template<class _Tp, class _Alloc, class _A0, class _A1>
4815inline _LIBCPP_INLINE_VISIBILITY
4816shared_ptr<_Tp>
4817allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4818{
4819 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4820}
4821
4822template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4823inline _LIBCPP_INLINE_VISIBILITY
4824shared_ptr<_Tp>
4825allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4826{
4827 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4828}
4829
4830#endif // _LIBCPP_HAS_NO_VARIADICS
4831
4832template<class _Tp, class _Up>
4833inline _LIBCPP_INLINE_VISIBILITY
4834bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004835operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004836{
4837 return __x.get() == __y.get();
4838}
4839
4840template<class _Tp, class _Up>
4841inline _LIBCPP_INLINE_VISIBILITY
4842bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004843operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004844{
4845 return !(__x == __y);
4846}
4847
4848template<class _Tp, class _Up>
4849inline _LIBCPP_INLINE_VISIBILITY
4850bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004851operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004852{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004853 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4854 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004855}
4856
4857template<class _Tp, class _Up>
4858inline _LIBCPP_INLINE_VISIBILITY
4859bool
4860operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4861{
4862 return __y < __x;
4863}
4864
4865template<class _Tp, class _Up>
4866inline _LIBCPP_INLINE_VISIBILITY
4867bool
4868operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4869{
4870 return !(__y < __x);
4871}
4872
4873template<class _Tp, class _Up>
4874inline _LIBCPP_INLINE_VISIBILITY
4875bool
4876operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4877{
4878 return !(__x < __y);
4879}
4880
4881template<class _Tp>
4882inline _LIBCPP_INLINE_VISIBILITY
4883bool
4884operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4885{
4886 return !__x;
4887}
4888
4889template<class _Tp>
4890inline _LIBCPP_INLINE_VISIBILITY
4891bool
4892operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4893{
4894 return !__x;
4895}
4896
4897template<class _Tp>
4898inline _LIBCPP_INLINE_VISIBILITY
4899bool
4900operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4901{
4902 return static_cast<bool>(__x);
4903}
4904
4905template<class _Tp>
4906inline _LIBCPP_INLINE_VISIBILITY
4907bool
4908operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4909{
4910 return static_cast<bool>(__x);
4911}
4912
4913template<class _Tp>
4914inline _LIBCPP_INLINE_VISIBILITY
4915bool
4916operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4917{
4918 return less<_Tp*>()(__x.get(), nullptr);
4919}
4920
4921template<class _Tp>
4922inline _LIBCPP_INLINE_VISIBILITY
4923bool
4924operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4925{
4926 return less<_Tp*>()(nullptr, __x.get());
4927}
4928
4929template<class _Tp>
4930inline _LIBCPP_INLINE_VISIBILITY
4931bool
4932operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4933{
4934 return nullptr < __x;
4935}
4936
4937template<class _Tp>
4938inline _LIBCPP_INLINE_VISIBILITY
4939bool
4940operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4941{
4942 return __x < nullptr;
4943}
4944
4945template<class _Tp>
4946inline _LIBCPP_INLINE_VISIBILITY
4947bool
4948operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4949{
4950 return !(nullptr < __x);
4951}
4952
4953template<class _Tp>
4954inline _LIBCPP_INLINE_VISIBILITY
4955bool
4956operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4957{
4958 return !(__x < nullptr);
4959}
4960
4961template<class _Tp>
4962inline _LIBCPP_INLINE_VISIBILITY
4963bool
4964operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4965{
4966 return !(__x < nullptr);
4967}
4968
4969template<class _Tp>
4970inline _LIBCPP_INLINE_VISIBILITY
4971bool
4972operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4973{
4974 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004975}
4976
4977template<class _Tp>
4978inline _LIBCPP_INLINE_VISIBILITY
4979void
Howard Hinnant1694d232011-05-28 14:41:13 +00004980swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004981{
4982 __x.swap(__y);
4983}
4984
4985template<class _Tp, class _Up>
4986inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004987typename enable_if
4988<
4989 !is_array<_Tp>::value && !is_array<_Up>::value,
4990 shared_ptr<_Tp>
4991>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004992static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004993{
4994 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4995}
4996
4997template<class _Tp, class _Up>
4998inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004999typename enable_if
5000<
5001 !is_array<_Tp>::value && !is_array<_Up>::value,
5002 shared_ptr<_Tp>
5003>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005004dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005005{
5006 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5007 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5008}
5009
5010template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00005011typename enable_if
5012<
5013 is_array<_Tp>::value == is_array<_Up>::value,
5014 shared_ptr<_Tp>
5015>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005016const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005017{
Howard Hinnant57199402012-01-02 17:56:02 +00005018 typedef typename remove_extent<_Tp>::type _RTp;
5019 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005020}
5021
Howard Hinnantd4444702010-08-11 17:04:31 +00005022#ifndef _LIBCPP_NO_RTTI
5023
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005024template<class _Dp, class _Tp>
5025inline _LIBCPP_INLINE_VISIBILITY
5026_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00005027get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005028{
5029 return __p.template __get_deleter<_Dp>();
5030}
5031
Howard Hinnant324bb032010-08-22 00:02:43 +00005032#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00005033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005034template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005035class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005036{
Howard Hinnant324bb032010-08-22 00:02:43 +00005037public:
5038 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005039private:
5040 element_type* __ptr_;
5041 __shared_weak_count* __cntrl_;
5042
Howard Hinnant324bb032010-08-22 00:02:43 +00005043public:
Howard Hinnant46e94932012-07-07 20:56:04 +00005044 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005045 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005046 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5047 _NOEXCEPT;
5048 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005049 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005050 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5051 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005052
Howard Hinnant57199402012-01-02 17:56:02 +00005053#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5054 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005055 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant57199402012-01-02 17:56:02 +00005056 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5057 _NOEXCEPT;
5058#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005059 ~weak_ptr();
5060
Howard Hinnant1694d232011-05-28 14:41:13 +00005061 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005062 template<class _Yp>
5063 typename enable_if
5064 <
5065 is_convertible<_Yp*, element_type*>::value,
5066 weak_ptr&
5067 >::type
5068 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5069
5070#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5071
5072 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5073 template<class _Yp>
5074 typename enable_if
5075 <
5076 is_convertible<_Yp*, element_type*>::value,
5077 weak_ptr&
5078 >::type
5079 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5080
5081#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5082
5083 template<class _Yp>
5084 typename enable_if
5085 <
5086 is_convertible<_Yp*, element_type*>::value,
5087 weak_ptr&
5088 >::type
5089 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005090
Howard Hinnant1694d232011-05-28 14:41:13 +00005091 void swap(weak_ptr& __r) _NOEXCEPT;
5092 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005093
Howard Hinnant82894812010-09-22 16:48:34 +00005094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005095 long use_count() const _NOEXCEPT
5096 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005098 bool expired() const _NOEXCEPT
5099 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5100 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005101 template<class _Up>
5102 _LIBCPP_INLINE_VISIBILITY
5103 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005104 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005105 template<class _Up>
5106 _LIBCPP_INLINE_VISIBILITY
5107 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005108 {return __cntrl_ < __r.__cntrl_;}
5109
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005110 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5111 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005112};
5113
5114template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005115inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005116_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005117weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005118 : __ptr_(0),
5119 __cntrl_(0)
5120{
5121}
5122
5123template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005124inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005125weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005126 : __ptr_(__r.__ptr_),
5127 __cntrl_(__r.__cntrl_)
5128{
5129 if (__cntrl_)
5130 __cntrl_->__add_weak();
5131}
5132
5133template<class _Tp>
5134template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005136weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005137 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005138 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005139 : __ptr_(__r.__ptr_),
5140 __cntrl_(__r.__cntrl_)
5141{
5142 if (__cntrl_)
5143 __cntrl_->__add_weak();
5144}
5145
5146template<class _Tp>
5147template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005148inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005149weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005150 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005151 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005152 : __ptr_(__r.__ptr_),
5153 __cntrl_(__r.__cntrl_)
5154{
5155 if (__cntrl_)
5156 __cntrl_->__add_weak();
5157}
5158
Howard Hinnant57199402012-01-02 17:56:02 +00005159#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5160
5161template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005162inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005163weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5164 : __ptr_(__r.__ptr_),
5165 __cntrl_(__r.__cntrl_)
5166{
5167 __r.__ptr_ = 0;
5168 __r.__cntrl_ = 0;
5169}
5170
5171template<class _Tp>
5172template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005173inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005174weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5175 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5176 _NOEXCEPT
5177 : __ptr_(__r.__ptr_),
5178 __cntrl_(__r.__cntrl_)
5179{
5180 __r.__ptr_ = 0;
5181 __r.__cntrl_ = 0;
5182}
5183
5184#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5185
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005186template<class _Tp>
5187weak_ptr<_Tp>::~weak_ptr()
5188{
5189 if (__cntrl_)
5190 __cntrl_->__release_weak();
5191}
5192
5193template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005194inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005195weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005196weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005197{
5198 weak_ptr(__r).swap(*this);
5199 return *this;
5200}
5201
5202template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005203template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005205typename enable_if
5206<
5207 is_convertible<_Yp*, _Tp*>::value,
5208 weak_ptr<_Tp>&
5209>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005210weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005211{
5212 weak_ptr(__r).swap(*this);
5213 return *this;
5214}
5215
Howard Hinnant57199402012-01-02 17:56:02 +00005216#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5217
5218template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005219inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005220weak_ptr<_Tp>&
5221weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5222{
5223 weak_ptr(_VSTD::move(__r)).swap(*this);
5224 return *this;
5225}
5226
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005227template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005228template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005230typename enable_if
5231<
5232 is_convertible<_Yp*, _Tp*>::value,
5233 weak_ptr<_Tp>&
5234>::type
5235weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5236{
5237 weak_ptr(_VSTD::move(__r)).swap(*this);
5238 return *this;
5239}
5240
5241#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5242
5243template<class _Tp>
5244template<class _Yp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005246typename enable_if
5247<
5248 is_convertible<_Yp*, _Tp*>::value,
5249 weak_ptr<_Tp>&
5250>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005251weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005252{
5253 weak_ptr(__r).swap(*this);
5254 return *this;
5255}
5256
5257template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005259void
Howard Hinnant1694d232011-05-28 14:41:13 +00005260weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005261{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005262 _VSTD::swap(__ptr_, __r.__ptr_);
5263 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005264}
5265
5266template<class _Tp>
5267inline _LIBCPP_INLINE_VISIBILITY
5268void
Howard Hinnant1694d232011-05-28 14:41:13 +00005269swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005270{
5271 __x.swap(__y);
5272}
5273
5274template<class _Tp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005276void
Howard Hinnant1694d232011-05-28 14:41:13 +00005277weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005278{
5279 weak_ptr().swap(*this);
5280}
5281
5282template<class _Tp>
5283template<class _Yp>
5284shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5285 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5286 : __ptr_(__r.__ptr_),
5287 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5288{
5289 if (__cntrl_ == 0)
5290#ifndef _LIBCPP_NO_EXCEPTIONS
5291 throw bad_weak_ptr();
5292#else
5293 assert(!"bad_weak_ptr");
5294#endif
5295}
5296
5297template<class _Tp>
5298shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005299weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005300{
5301 shared_ptr<_Tp> __r;
5302 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5303 if (__r.__cntrl_)
5304 __r.__ptr_ = __ptr_;
5305 return __r;
5306}
5307
Howard Hinnant324bb032010-08-22 00:02:43 +00005308template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005309
5310template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005311struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005312 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005313{
5314 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005316 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5317 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005319 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5320 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005322 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5323 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005324};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005325
5326template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005327struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005328 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5329{
Howard Hinnant324bb032010-08-22 00:02:43 +00005330 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005332 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5333 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005335 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5336 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005338 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5339 {return __x.owner_before(__y);}
5340};
5341
5342template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005343class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005344{
5345 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005346protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005347 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005348 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005350 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005352 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5353 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005355 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005356public:
Howard Hinnant82894812010-09-22 16:48:34 +00005357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005358 shared_ptr<_Tp> shared_from_this()
5359 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005361 shared_ptr<_Tp const> shared_from_this() const
5362 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005363
5364 template <class _Up> friend class shared_ptr;
5365};
5366
Howard Hinnant21aefc32010-06-03 16:42:57 +00005367template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005368struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005369{
5370 typedef shared_ptr<_Tp> argument_type;
5371 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005373 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005374 {
5375 return hash<_Tp*>()(__ptr.get());
5376 }
5377};
5378
Howard Hinnant99968442011-11-29 18:15:50 +00005379template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005380inline _LIBCPP_INLINE_VISIBILITY
5381basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005382operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005383
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005384#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005385
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005386class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005387{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005388 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005389public:
5390 void lock() _NOEXCEPT;
5391 void unlock() _NOEXCEPT;
5392
5393private:
5394 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5395 __sp_mut(const __sp_mut&);
5396 __sp_mut& operator=(const __sp_mut&);
5397
Howard Hinnant83eade62013-03-06 23:30:19 +00005398 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005399};
5400
Howard Hinnant83eade62013-03-06 23:30:19 +00005401_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005402
5403template <class _Tp>
5404inline _LIBCPP_INLINE_VISIBILITY
5405bool
5406atomic_is_lock_free(const shared_ptr<_Tp>*)
5407{
5408 return false;
5409}
5410
5411template <class _Tp>
5412shared_ptr<_Tp>
5413atomic_load(const shared_ptr<_Tp>* __p)
5414{
5415 __sp_mut& __m = __get_sp_mut(__p);
5416 __m.lock();
5417 shared_ptr<_Tp> __q = *__p;
5418 __m.unlock();
5419 return __q;
5420}
5421
5422template <class _Tp>
5423inline _LIBCPP_INLINE_VISIBILITY
5424shared_ptr<_Tp>
5425atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5426{
5427 return atomic_load(__p);
5428}
5429
5430template <class _Tp>
5431void
5432atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5433{
5434 __sp_mut& __m = __get_sp_mut(__p);
5435 __m.lock();
5436 __p->swap(__r);
5437 __m.unlock();
5438}
5439
5440template <class _Tp>
5441inline _LIBCPP_INLINE_VISIBILITY
5442void
5443atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5444{
5445 atomic_store(__p, __r);
5446}
5447
5448template <class _Tp>
5449shared_ptr<_Tp>
5450atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5451{
5452 __sp_mut& __m = __get_sp_mut(__p);
5453 __m.lock();
5454 __p->swap(__r);
5455 __m.unlock();
5456 return __r;
5457}
5458
5459template <class _Tp>
5460inline _LIBCPP_INLINE_VISIBILITY
5461shared_ptr<_Tp>
5462atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5463{
5464 return atomic_exchange(__p, __r);
5465}
5466
5467template <class _Tp>
5468bool
5469atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5470{
5471 __sp_mut& __m = __get_sp_mut(__p);
5472 __m.lock();
5473 if (__p->__owner_equivalent(*__v))
5474 {
5475 *__p = __w;
5476 __m.unlock();
5477 return true;
5478 }
5479 *__v = *__p;
5480 __m.unlock();
5481 return false;
5482}
5483
5484template <class _Tp>
5485inline _LIBCPP_INLINE_VISIBILITY
5486bool
5487atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5488{
5489 return atomic_compare_exchange_strong(__p, __v, __w);
5490}
5491
5492template <class _Tp>
5493inline _LIBCPP_INLINE_VISIBILITY
5494bool
5495atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5496 shared_ptr<_Tp> __w, memory_order, memory_order)
5497{
5498 return atomic_compare_exchange_strong(__p, __v, __w);
5499}
5500
5501template <class _Tp>
5502inline _LIBCPP_INLINE_VISIBILITY
5503bool
5504atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5505 shared_ptr<_Tp> __w, memory_order, memory_order)
5506{
5507 return atomic_compare_exchange_weak(__p, __v, __w);
5508}
5509
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005510#endif // __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005511
Howard Hinnant324bb032010-08-22 00:02:43 +00005512//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005513struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005514{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005515 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005516 {
5517 relaxed,
5518 preferred,
5519 strict
5520 };
5521
Howard Hinnant9c0df142012-10-30 19:06:59 +00005522 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005523
Howard Hinnant82894812010-09-22 16:48:34 +00005524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005525 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005526 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005527 operator int() const {return __v_;}
5528};
5529
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005530_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5531_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5532_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5533_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5534_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005535
5536template <class _Tp>
5537inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005538_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005539undeclare_reachable(_Tp* __p)
5540{
5541 return static_cast<_Tp*>(__undeclare_reachable(__p));
5542}
5543
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005544_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005545
Marshall Clow7d914d12015-07-13 20:04:56 +00005546// --- Helper for container swap --
5547template <typename _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005548_LIBCPP_INLINE_VISIBILITY
Marshall Clow7d914d12015-07-13 20:04:56 +00005549void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5550#if _LIBCPP_STD_VER >= 14
5551 _NOEXCEPT
5552#else
5553 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5554#endif
5555{
5556 __swap_allocator(__a1, __a2,
5557 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5558}
5559
5560template <typename _Alloc>
5561_LIBCPP_INLINE_VISIBILITY
5562void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5563#if _LIBCPP_STD_VER >= 14
5564 _NOEXCEPT
5565#else
5566 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5567#endif
5568{
5569 using _VSTD::swap;
5570 swap(__a1, __a2);
5571}
5572
5573template <typename _Alloc>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07005574_LIBCPP_INLINE_VISIBILITY
Marshall Clow7d914d12015-07-13 20:04:56 +00005575void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5576
5577
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005578_LIBCPP_END_NAMESPACE_STD
5579
5580#endif // _LIBCPP_MEMORY