blob: 26ba5642ce13d9156b158a4f48da0d56db985854 [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
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000615#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
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000628// addressof moved to <__functional_base>
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000629
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630template <class _Tp> class allocator;
631
632template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000633class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634{
635public:
636 typedef void* pointer;
637 typedef const void* const_pointer;
638 typedef void value_type;
639
640 template <class _Up> struct rebind {typedef allocator<_Up> other;};
641};
642
Howard Hinnanta1877872012-01-19 23:15:22 +0000643template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000644class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000645{
646public:
647 typedef const void* pointer;
648 typedef const void* const_pointer;
649 typedef const void value_type;
650
651 template <class _Up> struct rebind {typedef allocator<_Up> other;};
652};
653
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654// pointer_traits
655
656template <class _Tp>
657struct __has_element_type
658{
659private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000660 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 template <class _Up> static __two __test(...);
662 template <class _Up> static char __test(typename _Up::element_type* = 0);
663public:
664 static const bool value = sizeof(__test<_Tp>(0)) == 1;
665};
666
667template <class _Ptr, bool = __has_element_type<_Ptr>::value>
668struct __pointer_traits_element_type;
669
670template <class _Ptr>
671struct __pointer_traits_element_type<_Ptr, true>
672{
673 typedef typename _Ptr::element_type type;
674};
675
676#ifndef _LIBCPP_HAS_NO_VARIADICS
677
678template <template <class, class...> class _Sp, class _Tp, class ..._Args>
679struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
680{
681 typedef typename _Sp<_Tp, _Args...>::element_type type;
682};
683
684template <template <class, class...> class _Sp, class _Tp, class ..._Args>
685struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
686{
687 typedef _Tp type;
688};
689
Howard Hinnant324bb032010-08-22 00:02:43 +0000690#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691
692template <template <class> class _Sp, class _Tp>
693struct __pointer_traits_element_type<_Sp<_Tp>, true>
694{
695 typedef typename _Sp<_Tp>::element_type type;
696};
697
698template <template <class> class _Sp, class _Tp>
699struct __pointer_traits_element_type<_Sp<_Tp>, false>
700{
701 typedef _Tp type;
702};
703
704template <template <class, class> class _Sp, class _Tp, class _A0>
705struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
706{
707 typedef typename _Sp<_Tp, _A0>::element_type type;
708};
709
710template <template <class, class> class _Sp, class _Tp, class _A0>
711struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
712{
713 typedef _Tp type;
714};
715
716template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
717struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
718{
719 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
720};
721
722template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
723struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
724{
725 typedef _Tp type;
726};
727
728template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
729 class _A1, class _A2>
730struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
731{
732 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
733};
734
735template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
736 class _A1, class _A2>
737struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
738{
739 typedef _Tp type;
740};
741
Howard Hinnant324bb032010-08-22 00:02:43 +0000742#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
744template <class _Tp>
745struct __has_difference_type
746{
747private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000748 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000749 template <class _Up> static __two __test(...);
750 template <class _Up> static char __test(typename _Up::difference_type* = 0);
751public:
752 static const bool value = sizeof(__test<_Tp>(0)) == 1;
753};
754
755template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
756struct __pointer_traits_difference_type
757{
758 typedef ptrdiff_t type;
759};
760
761template <class _Ptr>
762struct __pointer_traits_difference_type<_Ptr, true>
763{
764 typedef typename _Ptr::difference_type type;
765};
766
767template <class _Tp, class _Up>
768struct __has_rebind
769{
770private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000771 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772 template <class _Xp> static __two __test(...);
773 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
774public:
775 static const bool value = sizeof(__test<_Tp>(0)) == 1;
776};
777
778template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
779struct __pointer_traits_rebind
780{
781#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
782 typedef typename _Tp::template rebind<_Up> type;
783#else
784 typedef typename _Tp::template rebind<_Up>::other type;
785#endif
786};
787
788#ifndef _LIBCPP_HAS_NO_VARIADICS
789
790template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
791struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
792{
793#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
795#else
796 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
797#endif
798};
799
800template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
801struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
802{
803 typedef _Sp<_Up, _Args...> type;
804};
805
Howard Hinnant324bb032010-08-22 00:02:43 +0000806#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807
808template <template <class> class _Sp, class _Tp, class _Up>
809struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
810{
811#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
812 typedef typename _Sp<_Tp>::template rebind<_Up> type;
813#else
814 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
815#endif
816};
817
818template <template <class> class _Sp, class _Tp, class _Up>
819struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
820{
821 typedef _Sp<_Up> type;
822};
823
824template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
825struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
826{
827#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
828 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
829#else
830 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
831#endif
832};
833
834template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
835struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
836{
837 typedef _Sp<_Up, _A0> type;
838};
839
840template <template <class, class, class> class _Sp, class _Tp, class _A0,
841 class _A1, class _Up>
842struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
843{
844#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
845 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
846#else
847 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
848#endif
849};
850
851template <template <class, class, class> class _Sp, class _Tp, class _A0,
852 class _A1, class _Up>
853struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
854{
855 typedef _Sp<_Up, _A0, _A1> type;
856};
857
858template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
859 class _A1, class _A2, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
861{
862#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
863 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
864#else
865 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
866#endif
867};
868
869template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
870 class _A1, class _A2, class _Up>
871struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
872{
873 typedef _Sp<_Up, _A0, _A1, _A2> type;
874};
875
Howard Hinnant324bb032010-08-22 00:02:43 +0000876#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877
878template <class _Ptr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000879struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880{
881 typedef _Ptr pointer;
882 typedef typename __pointer_traits_element_type<pointer>::type element_type;
883 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
884
885#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000886 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887#else
888 template <class _Up> struct rebind
889 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000890#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891
892private:
893 struct __nat {};
894public:
Howard Hinnant82894812010-09-22 16:48:34 +0000895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 static pointer pointer_to(typename conditional<is_void<element_type>::value,
897 __nat, element_type>::type& __r)
898 {return pointer::pointer_to(__r);}
899};
900
901template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000902struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903{
904 typedef _Tp* pointer;
905 typedef _Tp element_type;
906 typedef ptrdiff_t difference_type;
907
908#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
909 template <class _Up> using rebind = _Up*;
910#else
911 template <class _Up> struct rebind {typedef _Up* other;};
912#endif
913
914private:
915 struct __nat {};
916public:
Howard Hinnant82894812010-09-22 16:48:34 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000919 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000920 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921};
922
923// allocator_traits
924
925namespace __has_pointer_type_imp
926{
Howard Hinnant81277582013-09-21 01:45:05 +0000927 template <class _Up> static __two __test(...);
928 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929}
930
931template <class _Tp>
932struct __has_pointer_type
Howard Hinnant81277582013-09-21 01:45:05 +0000933 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934{
935};
936
937namespace __pointer_type_imp
938{
939
940template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
941struct __pointer_type
942{
943 typedef typename _Dp::pointer type;
944};
945
946template <class _Tp, class _Dp>
947struct __pointer_type<_Tp, _Dp, false>
948{
949 typedef _Tp* type;
950};
951
Howard Hinnant47761072010-11-18 01:40:00 +0000952} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953
954template <class _Tp, class _Dp>
955struct __pointer_type
956{
957 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
958};
959
960template <class _Tp>
961struct __has_const_pointer
962{
963private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000964 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 template <class _Up> static __two __test(...);
966 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
967public:
968 static const bool value = sizeof(__test<_Tp>(0)) == 1;
969};
970
971template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
972struct __const_pointer
973{
974 typedef typename _Alloc::const_pointer type;
975};
976
977template <class _Tp, class _Ptr, class _Alloc>
978struct __const_pointer<_Tp, _Ptr, _Alloc, false>
979{
980#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
981 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
982#else
983 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
984#endif
985};
986
987template <class _Tp>
988struct __has_void_pointer
989{
990private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000991 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000992 template <class _Up> static __two __test(...);
993 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
994public:
995 static const bool value = sizeof(__test<_Tp>(0)) == 1;
996};
997
998template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
999struct __void_pointer
1000{
1001 typedef typename _Alloc::void_pointer type;
1002};
1003
1004template <class _Ptr, class _Alloc>
1005struct __void_pointer<_Ptr, _Alloc, false>
1006{
1007#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1008 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1009#else
1010 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1011#endif
1012};
1013
1014template <class _Tp>
1015struct __has_const_void_pointer
1016{
1017private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001018 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 template <class _Up> static __two __test(...);
1020 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1021public:
1022 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1023};
1024
1025template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1026struct __const_void_pointer
1027{
1028 typedef typename _Alloc::const_void_pointer type;
1029};
1030
1031template <class _Ptr, class _Alloc>
1032struct __const_void_pointer<_Ptr, _Alloc, false>
1033{
1034#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1035 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1036#else
1037 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1038#endif
1039};
1040
Howard Hinnant99968442011-11-29 18:15:50 +00001041template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001043_Tp*
1044__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045{
1046 return __p;
1047}
1048
1049template <class _Pointer>
1050inline _LIBCPP_INLINE_VISIBILITY
1051typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001052__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001054 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055}
1056
1057template <class _Tp>
1058struct __has_size_type
1059{
1060private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001061 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062 template <class _Up> static __two __test(...);
1063 template <class _Up> static char __test(typename _Up::size_type* = 0);
1064public:
1065 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1066};
1067
Howard Hinnant47761072010-11-18 01:40:00 +00001068template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069struct __size_type
1070{
Howard Hinnant47761072010-11-18 01:40:00 +00001071 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072};
1073
Howard Hinnant47761072010-11-18 01:40:00 +00001074template <class _Alloc, class _DiffType>
1075struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076{
1077 typedef typename _Alloc::size_type type;
1078};
1079
1080template <class _Tp>
1081struct __has_propagate_on_container_copy_assignment
1082{
1083private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001084 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001085 template <class _Up> static __two __test(...);
1086 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1087public:
1088 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1089};
1090
1091template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1092struct __propagate_on_container_copy_assignment
1093{
1094 typedef false_type type;
1095};
1096
1097template <class _Alloc>
1098struct __propagate_on_container_copy_assignment<_Alloc, true>
1099{
1100 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1101};
1102
1103template <class _Tp>
1104struct __has_propagate_on_container_move_assignment
1105{
1106private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001107 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001108 template <class _Up> static __two __test(...);
1109 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1110public:
1111 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1112};
1113
1114template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1115struct __propagate_on_container_move_assignment
1116{
1117 typedef false_type type;
1118};
1119
1120template <class _Alloc>
1121struct __propagate_on_container_move_assignment<_Alloc, true>
1122{
1123 typedef typename _Alloc::propagate_on_container_move_assignment type;
1124};
1125
1126template <class _Tp>
1127struct __has_propagate_on_container_swap
1128{
1129private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001130 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131 template <class _Up> static __two __test(...);
1132 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1133public:
1134 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1135};
1136
1137template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1138struct __propagate_on_container_swap
1139{
1140 typedef false_type type;
1141};
1142
1143template <class _Alloc>
1144struct __propagate_on_container_swap<_Alloc, true>
1145{
1146 typedef typename _Alloc::propagate_on_container_swap type;
1147};
1148
Marshall Clowf0324bc2015-06-02 16:34:03 +00001149template <class _Tp>
1150struct __has_is_always_equal
1151{
1152private:
1153 struct __two {char __lx; char __lxx;};
1154 template <class _Up> static __two __test(...);
1155 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1156public:
1157 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1158};
1159
1160template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1161struct __is_always_equal
1162{
1163 typedef typename _VSTD::is_empty<_Alloc>::type type;
1164};
1165
1166template <class _Alloc>
1167struct __is_always_equal<_Alloc, true>
1168{
1169 typedef typename _Alloc::is_always_equal type;
1170};
1171
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1173struct __has_rebind_other
1174{
1175private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001176 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177 template <class _Xp> static __two __test(...);
1178 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1179public:
1180 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1181};
1182
1183template <class _Tp, class _Up>
1184struct __has_rebind_other<_Tp, _Up, false>
1185{
1186 static const bool value = false;
1187};
1188
1189template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1190struct __allocator_traits_rebind
1191{
1192 typedef typename _Tp::template rebind<_Up>::other type;
1193};
1194
1195#ifndef _LIBCPP_HAS_NO_VARIADICS
1196
1197template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1198struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1199{
1200 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1201};
1202
1203template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1204struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1205{
1206 typedef _Alloc<_Up, _Args...> type;
1207};
1208
Howard Hinnant324bb032010-08-22 00:02:43 +00001209#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210
1211template <template <class> class _Alloc, class _Tp, class _Up>
1212struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1213{
1214 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1215};
1216
1217template <template <class> class _Alloc, class _Tp, class _Up>
1218struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1219{
1220 typedef _Alloc<_Up> type;
1221};
1222
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001223template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1224struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1225{
1226 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1227};
1228
1229template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1230struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1231{
1232 typedef _Alloc<_Up, _A0> type;
1233};
1234
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1236 class _A1, class _Up>
1237struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1238{
1239 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1240};
1241
1242template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1243 class _A1, class _Up>
1244struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1245{
1246 typedef _Alloc<_Up, _A0, _A1> type;
1247};
1248
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1250 class _A1, class _A2, class _Up>
1251struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1252{
1253 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1254};
1255
1256template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1257 class _A1, class _A2, class _Up>
1258struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1259{
1260 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1261};
1262
Howard Hinnant324bb032010-08-22 00:02:43 +00001263#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264
1265#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1266
1267template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1268auto
1269__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1270 -> decltype(__a.allocate(__sz, __p), true_type());
1271
1272template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1273auto
1274__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1275 -> false_type;
1276
1277template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1278struct __has_allocate_hint
1279 : integral_constant<bool,
1280 is_same<
1281 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1282 declval<_SizeType>(),
1283 declval<_ConstVoidPtr>())),
1284 true_type>::value>
1285{
1286};
1287
Howard Hinnant324bb032010-08-22 00:02:43 +00001288#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001289
1290template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1291struct __has_allocate_hint
1292 : true_type
1293{
1294};
1295
Howard Hinnant324bb032010-08-22 00:02:43 +00001296#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297
Howard Hinnant23369ee2011-07-29 21:35:53 +00001298#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299
1300template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001301decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1302 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303 true_type())
1304__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1305
1306template <class _Alloc, class _Pointer, class ..._Args>
1307false_type
1308__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1309
1310template <class _Alloc, class _Pointer, class ..._Args>
1311struct __has_construct
1312 : integral_constant<bool,
1313 is_same<
1314 decltype(__has_construct_test(declval<_Alloc>(),
1315 declval<_Pointer>(),
1316 declval<_Args>()...)),
1317 true_type>::value>
1318{
1319};
1320
1321template <class _Alloc, class _Pointer>
1322auto
1323__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1324 -> decltype(__a.destroy(__p), true_type());
1325
1326template <class _Alloc, class _Pointer>
1327auto
1328__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1329 -> false_type;
1330
1331template <class _Alloc, class _Pointer>
1332struct __has_destroy
1333 : integral_constant<bool,
1334 is_same<
1335 decltype(__has_destroy_test(declval<_Alloc>(),
1336 declval<_Pointer>())),
1337 true_type>::value>
1338{
1339};
1340
1341template <class _Alloc>
1342auto
1343__has_max_size_test(_Alloc&& __a)
1344 -> decltype(__a.max_size(), true_type());
1345
1346template <class _Alloc>
1347auto
1348__has_max_size_test(const volatile _Alloc& __a)
1349 -> false_type;
1350
1351template <class _Alloc>
1352struct __has_max_size
1353 : integral_constant<bool,
1354 is_same<
1355 decltype(__has_max_size_test(declval<_Alloc&>())),
1356 true_type>::value>
1357{
1358};
1359
1360template <class _Alloc>
1361auto
1362__has_select_on_container_copy_construction_test(_Alloc&& __a)
1363 -> decltype(__a.select_on_container_copy_construction(), true_type());
1364
1365template <class _Alloc>
1366auto
1367__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1368 -> false_type;
1369
1370template <class _Alloc>
1371struct __has_select_on_container_copy_construction
1372 : integral_constant<bool,
1373 is_same<
1374 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1375 true_type>::value>
1376{
1377};
1378
Howard Hinnant324bb032010-08-22 00:02:43 +00001379#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380
1381#ifndef _LIBCPP_HAS_NO_VARIADICS
1382
1383template <class _Alloc, class _Pointer, class ..._Args>
1384struct __has_construct
1385 : false_type
1386{
1387};
1388
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001389#else // _LIBCPP_HAS_NO_VARIADICS
1390
1391template <class _Alloc, class _Pointer, class _Args>
1392struct __has_construct
1393 : false_type
1394{
1395};
1396
Howard Hinnant324bb032010-08-22 00:02:43 +00001397#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001398
1399template <class _Alloc, class _Pointer>
1400struct __has_destroy
1401 : false_type
1402{
1403};
1404
1405template <class _Alloc>
1406struct __has_max_size
1407 : true_type
1408{
1409};
1410
1411template <class _Alloc>
1412struct __has_select_on_container_copy_construction
1413 : false_type
1414{
1415};
1416
Howard Hinnant324bb032010-08-22 00:02:43 +00001417#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418
Howard Hinnant47761072010-11-18 01:40:00 +00001419template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1420struct __alloc_traits_difference_type
1421{
1422 typedef typename pointer_traits<_Ptr>::difference_type type;
1423};
1424
1425template <class _Alloc, class _Ptr>
1426struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1427{
1428 typedef typename _Alloc::difference_type type;
1429};
1430
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431template <class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001432struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433{
1434 typedef _Alloc allocator_type;
1435 typedef typename allocator_type::value_type value_type;
1436
1437 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1438 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1439 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1440 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1441
Howard Hinnant47761072010-11-18 01:40:00 +00001442 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1443 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444
1445 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1446 propagate_on_container_copy_assignment;
1447 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1448 propagate_on_container_move_assignment;
1449 typedef typename __propagate_on_container_swap<allocator_type>::type
1450 propagate_on_container_swap;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001451 typedef typename __is_always_equal<allocator_type>::type
1452 is_always_equal;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453
1454#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1455 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001456 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001458#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459 template <class _Tp> struct rebind_alloc
1460 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1461 template <class _Tp> struct rebind_traits
1462 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001463#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001464
Howard Hinnant82894812010-09-22 16:48:34 +00001465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466 static pointer allocate(allocator_type& __a, size_type __n)
1467 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1470 {return allocate(__a, __n, __hint,
1471 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1472
Howard Hinnant82894812010-09-22 16:48:34 +00001473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001474 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 {__a.deallocate(__p, __n);}
1476
1477#ifndef _LIBCPP_HAS_NO_VARIADICS
1478 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3f5579f2014-11-11 19:22:33 +00001481 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001482 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001483#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 static void construct(allocator_type& __a, _Tp* __p)
1487 {
1488 ::new ((void*)__p) _Tp();
1489 }
1490 template <class _Tp, class _A0>
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, const _A0& __a0)
1493 {
1494 ::new ((void*)__p) _Tp(__a0);
1495 }
1496 template <class _Tp, class _A0, class _A1>
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, const _A0& __a0,
1499 const _A1& __a1)
1500 {
1501 ::new ((void*)__p) _Tp(__a0, __a1);
1502 }
1503 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001505 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1506 const _A1& __a1, const _A2& __a2)
1507 {
1508 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1509 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001510#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511
1512 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514 static void destroy(allocator_type& __a, _Tp* __p)
1515 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1516
Howard Hinnant82894812010-09-22 16:48:34 +00001517 _LIBCPP_INLINE_VISIBILITY
Marshall Clow08b4f3f2013-08-27 20:22:15 +00001518 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1520
Howard Hinnant82894812010-09-22 16:48:34 +00001521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001522 static allocator_type
1523 select_on_container_copy_construction(const allocator_type& __a)
1524 {return select_on_container_copy_construction(
1525 __has_select_on_container_copy_construction<const allocator_type>(),
1526 __a);}
1527
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001528 template <class _Ptr>
1529 _LIBCPP_INLINE_VISIBILITY
1530 static
1531 void
1532 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1533 {
1534 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1535 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1536 }
1537
1538 template <class _Tp>
1539 _LIBCPP_INLINE_VISIBILITY
1540 static
1541 typename enable_if
1542 <
1543 (is_same<allocator_type, allocator<_Tp> >::value
1544 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1545 is_trivially_move_constructible<_Tp>::value,
1546 void
1547 >::type
1548 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1549 {
1550 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001551 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001552 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001553 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001554 __begin2 += _Np;
1555 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001556 }
1557
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001558 template <class _Iter, class _Ptr>
1559 _LIBCPP_INLINE_VISIBILITY
1560 static
1561 void
1562 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1563 {
1564 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1565 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1566 }
1567
1568 template <class _Tp>
1569 _LIBCPP_INLINE_VISIBILITY
1570 static
1571 typename enable_if
1572 <
1573 (is_same<allocator_type, allocator<_Tp> >::value
1574 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1575 is_trivially_move_constructible<_Tp>::value,
1576 void
1577 >::type
1578 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1579 {
1580 typedef typename remove_const<_Tp>::type _Vp;
1581 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001582 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001583 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001584 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001585 __begin2 += _Np;
1586 }
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001587 }
1588
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001589 template <class _Ptr>
1590 _LIBCPP_INLINE_VISIBILITY
1591 static
1592 void
1593 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1594 {
1595 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001596 {
1597 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1598 --__end2;
1599 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001600 }
1601
1602 template <class _Tp>
1603 _LIBCPP_INLINE_VISIBILITY
1604 static
1605 typename enable_if
1606 <
1607 (is_same<allocator_type, allocator<_Tp> >::value
1608 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1609 is_trivially_move_constructible<_Tp>::value,
1610 void
1611 >::type
1612 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1613 {
1614 ptrdiff_t _Np = __end1 - __begin1;
1615 __end2 -= _Np;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001616 if (_Np > 0)
1617 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001618 }
1619
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620private:
1621
Howard Hinnant82894812010-09-22 16:48:34 +00001622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623 static pointer allocate(allocator_type& __a, size_type __n,
1624 const_void_pointer __hint, true_type)
1625 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001628 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 {return __a.allocate(__n);}
1630
1631#ifndef _LIBCPP_HAS_NO_VARIADICS
1632 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001635 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1639 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001640 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001642#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643
1644 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1647 {__a.destroy(__p);}
1648 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 static void __destroy(false_type, allocator_type&, _Tp* __p)
1651 {
1652 __p->~_Tp();
1653 }
1654
Howard Hinnant82894812010-09-22 16:48:34 +00001655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656 static size_type __max_size(true_type, const allocator_type& __a)
1657 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 static size_type __max_size(false_type, const allocator_type&)
1660 {return numeric_limits<size_type>::max();}
1661
Howard Hinnant82894812010-09-22 16:48:34 +00001662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663 static allocator_type
1664 select_on_container_copy_construction(true_type, const allocator_type& __a)
1665 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 static allocator_type
1668 select_on_container_copy_construction(false_type, const allocator_type& __a)
1669 {return __a;}
1670};
1671
Marshall Clow66302c62015-04-07 05:21:38 +00001672template <class _Traits, class _Tp>
1673struct __rebind_alloc_helper
1674{
1675#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Marshall Clow0ad232a2015-05-10 13:59:45 +00001676 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow66302c62015-04-07 05:21:38 +00001677#else
1678 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1679#endif
1680};
1681
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682// allocator
1683
1684template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001685class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686{
1687public:
1688 typedef size_t size_type;
1689 typedef ptrdiff_t difference_type;
1690 typedef _Tp* pointer;
1691 typedef const _Tp* const_pointer;
1692 typedef _Tp& reference;
1693 typedef const _Tp& const_reference;
1694 typedef _Tp value_type;
1695
Howard Hinnant18884f42011-06-02 21:38:57 +00001696 typedef true_type propagate_on_container_move_assignment;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001697 typedef true_type is_always_equal;
Howard Hinnant18884f42011-06-02 21:38:57 +00001698
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1700
Howard Hinnant1694d232011-05-28 14:41:13 +00001701 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1702 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1703 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001704 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001705 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001706 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Richard Smith73c1fce2014-06-04 19:54:15 +00001708 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001709 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001710 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001711 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1712 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001713#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714 template <class _Up, class... _Args>
1715 _LIBCPP_INLINE_VISIBILITY
1716 void
1717 construct(_Up* __p, _Args&&... __args)
1718 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001719 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001721#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 _LIBCPP_INLINE_VISIBILITY
1723 void
1724 construct(pointer __p)
1725 {
1726 ::new((void*)__p) _Tp();
1727 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001728# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001729
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 template <class _A0>
1731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001732 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733 construct(pointer __p, _A0& __a0)
1734 {
1735 ::new((void*)__p) _Tp(__a0);
1736 }
1737 template <class _A0>
1738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001739 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 construct(pointer __p, const _A0& __a0)
1741 {
1742 ::new((void*)__p) _Tp(__a0);
1743 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001744# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745 template <class _A0, class _A1>
1746 _LIBCPP_INLINE_VISIBILITY
1747 void
1748 construct(pointer __p, _A0& __a0, _A1& __a1)
1749 {
1750 ::new((void*)__p) _Tp(__a0, __a1);
1751 }
1752 template <class _A0, class _A1>
1753 _LIBCPP_INLINE_VISIBILITY
1754 void
1755 construct(pointer __p, const _A0& __a0, _A1& __a1)
1756 {
1757 ::new((void*)__p) _Tp(__a0, __a1);
1758 }
1759 template <class _A0, class _A1>
1760 _LIBCPP_INLINE_VISIBILITY
1761 void
1762 construct(pointer __p, _A0& __a0, const _A1& __a1)
1763 {
1764 ::new((void*)__p) _Tp(__a0, __a1);
1765 }
1766 template <class _A0, class _A1>
1767 _LIBCPP_INLINE_VISIBILITY
1768 void
1769 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1770 {
1771 ::new((void*)__p) _Tp(__a0, __a1);
1772 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001773#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1775};
1776
Howard Hinnant57199402012-01-02 17:56:02 +00001777template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001778class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001779{
1780public:
1781 typedef size_t size_type;
1782 typedef ptrdiff_t difference_type;
1783 typedef const _Tp* pointer;
1784 typedef const _Tp* const_pointer;
1785 typedef const _Tp& reference;
1786 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001787 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001788
1789 typedef true_type propagate_on_container_move_assignment;
Marshall Clowb81d6f52015-07-01 21:23:40 +00001790 typedef true_type is_always_equal;
Howard Hinnant57199402012-01-02 17:56:02 +00001791
1792 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1793
1794 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1795 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1796 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1797 {return _VSTD::addressof(__x);}
1798 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Richard Smith73c1fce2014-06-04 19:54:15 +00001799 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant57199402012-01-02 17:56:02 +00001800 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001801 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant57199402012-01-02 17:56:02 +00001802 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1803 {return size_type(~0) / sizeof(_Tp);}
1804#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1805 template <class _Up, class... _Args>
1806 _LIBCPP_INLINE_VISIBILITY
1807 void
1808 construct(_Up* __p, _Args&&... __args)
1809 {
1810 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1811 }
1812#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1813 _LIBCPP_INLINE_VISIBILITY
1814 void
1815 construct(pointer __p)
1816 {
1817 ::new((void*)__p) _Tp();
1818 }
1819# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001820
Howard Hinnant57199402012-01-02 17:56:02 +00001821 template <class _A0>
1822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001823 void
Howard Hinnant57199402012-01-02 17:56:02 +00001824 construct(pointer __p, _A0& __a0)
1825 {
1826 ::new((void*)__p) _Tp(__a0);
1827 }
1828 template <class _A0>
1829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001830 void
Howard Hinnant57199402012-01-02 17:56:02 +00001831 construct(pointer __p, const _A0& __a0)
1832 {
1833 ::new((void*)__p) _Tp(__a0);
1834 }
Howard Hinnant57199402012-01-02 17:56:02 +00001835# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1836 template <class _A0, class _A1>
1837 _LIBCPP_INLINE_VISIBILITY
1838 void
1839 construct(pointer __p, _A0& __a0, _A1& __a1)
1840 {
1841 ::new((void*)__p) _Tp(__a0, __a1);
1842 }
1843 template <class _A0, class _A1>
1844 _LIBCPP_INLINE_VISIBILITY
1845 void
1846 construct(pointer __p, const _A0& __a0, _A1& __a1)
1847 {
1848 ::new((void*)__p) _Tp(__a0, __a1);
1849 }
1850 template <class _A0, class _A1>
1851 _LIBCPP_INLINE_VISIBILITY
1852 void
1853 construct(pointer __p, _A0& __a0, const _A1& __a1)
1854 {
1855 ::new((void*)__p) _Tp(__a0, __a1);
1856 }
1857 template <class _A0, class _A1>
1858 _LIBCPP_INLINE_VISIBILITY
1859 void
1860 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1861 {
1862 ::new((void*)__p) _Tp(__a0, __a1);
1863 }
1864#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1865 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1866};
1867
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868template <class _Tp, class _Up>
1869inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001870bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871
1872template <class _Tp, class _Up>
1873inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001874bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875
1876template <class _OutputIterator, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001877class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878 : public iterator<output_iterator_tag,
1879 _Tp, // purposefully not C++03
1880 ptrdiff_t, // purposefully not C++03
1881 _Tp*, // purposefully not C++03
1882 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1883{
1884private:
1885 _OutputIterator __x_;
1886public:
1887 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1888 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1889 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1890 {::new(&*__x_) _Tp(__element); return *this;}
1891 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1892 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1893 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00001894#if _LIBCPP_STD_VER >= 14
1895 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1896#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897};
1898
1899template <class _Tp>
1900pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001901get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902{
1903 pair<_Tp*, ptrdiff_t> __r(0, 0);
1904 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1905 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1906 / sizeof(_Tp);
1907 if (__n > __m)
1908 __n = __m;
1909 while (__n > 0)
1910 {
1911 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1912 if (__r.first)
1913 {
1914 __r.second = __n;
1915 break;
1916 }
1917 __n /= 2;
1918 }
1919 return __r;
1920}
1921
1922template <class _Tp>
1923inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001924void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925
1926template <class _Tp>
1927struct auto_ptr_ref
1928{
1929 _Tp* __ptr_;
1930};
1931
1932template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001933class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934{
1935private:
1936 _Tp* __ptr_;
1937public:
1938 typedef _Tp element_type;
1939
1940 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1941 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1942 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1943 : __ptr_(__p.release()) {}
1944 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1945 {reset(__p.release()); return *this;}
1946 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1947 {reset(__p.release()); return *this;}
1948 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1949 {reset(__p.__ptr_); return *this;}
1950 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1951
1952 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1953 {return *__ptr_;}
1954 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1955 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1956 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1957 {
1958 _Tp* __t = __ptr_;
1959 __ptr_ = 0;
1960 return __t;
1961 }
1962 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1963 {
1964 if (__ptr_ != __p)
1965 delete __ptr_;
1966 __ptr_ = __p;
1967 }
1968
1969 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1970 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1971 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1972 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1973 {return auto_ptr<_Up>(release());}
1974};
1975
1976template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001977class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978{
1979public:
1980 typedef void element_type;
1981};
1982
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1984 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001985 bool = is_empty<_T1>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00001986 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001987 bool = is_empty<_T2>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00001988 && !__libcpp_is_final<_T2>::value
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001989 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990struct __libcpp_compressed_pair_switch;
1991
1992template <class _T1, class _T2, bool IsSame>
1993struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1994
1995template <class _T1, class _T2, bool IsSame>
1996struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1997
1998template <class _T1, class _T2, bool IsSame>
1999struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2000
2001template <class _T1, class _T2>
2002struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2003
2004template <class _T1, class _T2>
2005struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2006
2007template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2008class __libcpp_compressed_pair_imp;
2009
2010template <class _T1, class _T2>
2011class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2012{
2013private:
2014 _T1 __first_;
2015 _T2 __second_;
2016public:
2017 typedef _T1 _T1_param;
2018 typedef _T2 _T2_param;
2019
2020 typedef typename remove_reference<_T1>::type& _T1_reference;
2021 typedef typename remove_reference<_T2>::type& _T2_reference;
2022
2023 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2024 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2025
2026 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002027 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002028 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002029 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002030 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002032 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002034#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002035
2036 _LIBCPP_INLINE_VISIBILITY
2037 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2038 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2039 is_nothrow_copy_constructible<_T2>::value)
2040 : __first_(__p.first()),
2041 __second_(__p.second()) {}
2042
2043 _LIBCPP_INLINE_VISIBILITY
2044 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2045 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2046 is_nothrow_copy_assignable<_T2>::value)
2047 {
2048 __first_ = __p.first();
2049 __second_ = __p.second();
2050 return *this;
2051 }
2052
Howard Hinnant61aa6012011-07-01 19:24:36 +00002053 _LIBCPP_INLINE_VISIBILITY
2054 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002055 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2056 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002057 : __first_(_VSTD::forward<_T1>(__p.first())),
2058 __second_(_VSTD::forward<_T2>(__p.second())) {}
2059
2060 _LIBCPP_INLINE_VISIBILITY
2061 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2062 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2063 is_nothrow_move_assignable<_T2>::value)
2064 {
2065 __first_ = _VSTD::forward<_T1>(__p.first());
2066 __second_ = _VSTD::forward<_T2>(__p.second());
2067 return *this;
2068 }
2069
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002070#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002071
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002072#ifndef _LIBCPP_HAS_NO_VARIADICS
2073
2074 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2075 _LIBCPP_INLINE_VISIBILITY
2076 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2077 tuple<_Args1...> __first_args,
2078 tuple<_Args2...> __second_args,
2079 __tuple_indices<_I1...>,
2080 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002081 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2082 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002083 {}
2084
2085#endif // _LIBCPP_HAS_NO_VARIADICS
2086
Howard Hinnant1694d232011-05-28 14:41:13 +00002087 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2088 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089
Howard Hinnant1694d232011-05-28 14:41:13 +00002090 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2091 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092
2093 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002094 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002095 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002097 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098 swap(__first_, __x.__first_);
2099 swap(__second_, __x.__second_);
2100 }
2101};
2102
2103template <class _T1, class _T2>
2104class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2105 : private _T1
2106{
2107private:
2108 _T2 __second_;
2109public:
2110 typedef _T1 _T1_param;
2111 typedef _T2 _T2_param;
2112
2113 typedef _T1& _T1_reference;
2114 typedef typename remove_reference<_T2>::type& _T2_reference;
2115
2116 typedef const _T1& _T1_const_reference;
2117 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2118
2119 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002120 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002121 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002122 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002123 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002125 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002127#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002128
2129 _LIBCPP_INLINE_VISIBILITY
2130 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2131 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2132 is_nothrow_copy_constructible<_T2>::value)
2133 : _T1(__p.first()), __second_(__p.second()) {}
2134
2135 _LIBCPP_INLINE_VISIBILITY
2136 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2137 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2138 is_nothrow_copy_assignable<_T2>::value)
2139 {
2140 _T1::operator=(__p.first());
2141 __second_ = __p.second();
2142 return *this;
2143 }
2144
Howard Hinnant61aa6012011-07-01 19:24:36 +00002145 _LIBCPP_INLINE_VISIBILITY
2146 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002147 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2148 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002149 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002150
2151 _LIBCPP_INLINE_VISIBILITY
2152 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2153 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2154 is_nothrow_move_assignable<_T2>::value)
2155 {
2156 _T1::operator=(_VSTD::move(__p.first()));
2157 __second_ = _VSTD::forward<_T2>(__p.second());
2158 return *this;
2159 }
2160
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002161#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002162
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002163#ifndef _LIBCPP_HAS_NO_VARIADICS
2164
2165 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2166 _LIBCPP_INLINE_VISIBILITY
2167 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2168 tuple<_Args1...> __first_args,
2169 tuple<_Args2...> __second_args,
2170 __tuple_indices<_I1...>,
2171 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002172 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2173 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002174 {}
2175
2176#endif // _LIBCPP_HAS_NO_VARIADICS
2177
Howard Hinnant1694d232011-05-28 14:41:13 +00002178 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2179 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180
Howard Hinnant1694d232011-05-28 14:41:13 +00002181 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2182 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183
2184 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002185 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002186 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002188 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189 swap(__second_, __x.__second_);
2190 }
2191};
2192
2193template <class _T1, class _T2>
2194class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2195 : private _T2
2196{
2197private:
2198 _T1 __first_;
2199public:
2200 typedef _T1 _T1_param;
2201 typedef _T2 _T2_param;
2202
2203 typedef typename remove_reference<_T1>::type& _T1_reference;
2204 typedef _T2& _T2_reference;
2205
2206 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2207 typedef const _T2& _T2_const_reference;
2208
2209 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2210 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002211 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002213 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002215 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2216 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002217 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002219#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002220
2221 _LIBCPP_INLINE_VISIBILITY
2222 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2223 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2224 is_nothrow_copy_constructible<_T2>::value)
2225 : _T2(__p.second()), __first_(__p.first()) {}
2226
2227 _LIBCPP_INLINE_VISIBILITY
2228 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2229 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2230 is_nothrow_copy_assignable<_T2>::value)
2231 {
2232 _T2::operator=(__p.second());
2233 __first_ = __p.first();
2234 return *this;
2235 }
2236
Howard Hinnant61aa6012011-07-01 19:24:36 +00002237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002238 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002239 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2240 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002241 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002242
2243 _LIBCPP_INLINE_VISIBILITY
2244 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2245 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2246 is_nothrow_move_assignable<_T2>::value)
2247 {
2248 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2249 __first_ = _VSTD::move(__p.first());
2250 return *this;
2251 }
2252
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002253#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002254
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002255#ifndef _LIBCPP_HAS_NO_VARIADICS
2256
2257 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2258 _LIBCPP_INLINE_VISIBILITY
2259 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2260 tuple<_Args1...> __first_args,
2261 tuple<_Args2...> __second_args,
2262 __tuple_indices<_I1...>,
2263 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002264 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2265 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002266
2267 {}
2268
2269#endif // _LIBCPP_HAS_NO_VARIADICS
2270
Howard Hinnant1694d232011-05-28 14:41:13 +00002271 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2272 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273
Howard Hinnant1694d232011-05-28 14:41:13 +00002274 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2275 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276
2277 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002278 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002279 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002281 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282 swap(__first_, __x.__first_);
2283 }
2284};
2285
2286template <class _T1, class _T2>
2287class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2288 : private _T1,
2289 private _T2
2290{
2291public:
2292 typedef _T1 _T1_param;
2293 typedef _T2 _T2_param;
2294
2295 typedef _T1& _T1_reference;
2296 typedef _T2& _T2_reference;
2297
2298 typedef const _T1& _T1_const_reference;
2299 typedef const _T2& _T2_const_reference;
2300
2301 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2302 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002303 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002304 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002305 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002307 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002309#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002310
2311 _LIBCPP_INLINE_VISIBILITY
2312 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2313 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2314 is_nothrow_copy_constructible<_T2>::value)
2315 : _T1(__p.first()), _T2(__p.second()) {}
2316
2317 _LIBCPP_INLINE_VISIBILITY
2318 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2319 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2320 is_nothrow_copy_assignable<_T2>::value)
2321 {
2322 _T1::operator=(__p.first());
2323 _T2::operator=(__p.second());
2324 return *this;
2325 }
2326
Howard Hinnant61aa6012011-07-01 19:24:36 +00002327 _LIBCPP_INLINE_VISIBILITY
2328 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002329 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2330 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002331 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002332
2333 _LIBCPP_INLINE_VISIBILITY
2334 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2335 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2336 is_nothrow_move_assignable<_T2>::value)
2337 {
2338 _T1::operator=(_VSTD::move(__p.first()));
2339 _T2::operator=(_VSTD::move(__p.second()));
2340 return *this;
2341 }
2342
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002343#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002344
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002345#ifndef _LIBCPP_HAS_NO_VARIADICS
2346
2347 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2348 _LIBCPP_INLINE_VISIBILITY
2349 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2350 tuple<_Args1...> __first_args,
2351 tuple<_Args2...> __second_args,
2352 __tuple_indices<_I1...>,
2353 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002354 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2355 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002356 {}
2357
2358#endif // _LIBCPP_HAS_NO_VARIADICS
2359
Howard Hinnant1694d232011-05-28 14:41:13 +00002360 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2361 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362
Howard Hinnant1694d232011-05-28 14:41:13 +00002363 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2364 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365
Howard Hinnantec3773c2011-12-01 20:21:04 +00002366 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002367 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002368 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369 {
2370 }
2371};
2372
2373template <class _T1, class _T2>
2374class __compressed_pair
2375 : private __libcpp_compressed_pair_imp<_T1, _T2>
2376{
2377 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2378public:
2379 typedef typename base::_T1_param _T1_param;
2380 typedef typename base::_T2_param _T2_param;
2381
2382 typedef typename base::_T1_reference _T1_reference;
2383 typedef typename base::_T2_reference _T2_reference;
2384
2385 typedef typename base::_T1_const_reference _T1_const_reference;
2386 typedef typename base::_T2_const_reference _T2_const_reference;
2387
2388 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002389 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002390 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002391 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002392 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002394 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002396#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002397
2398 _LIBCPP_INLINE_VISIBILITY
2399 __compressed_pair(const __compressed_pair& __p)
2400 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2401 is_nothrow_copy_constructible<_T2>::value)
2402 : base(__p) {}
2403
2404 _LIBCPP_INLINE_VISIBILITY
2405 __compressed_pair& operator=(const __compressed_pair& __p)
2406 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2407 is_nothrow_copy_assignable<_T2>::value)
2408 {
2409 base::operator=(__p);
2410 return *this;
2411 }
2412
Howard Hinnant61aa6012011-07-01 19:24:36 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002414 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002415 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2416 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002417 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002418
2419 _LIBCPP_INLINE_VISIBILITY
2420 __compressed_pair& operator=(__compressed_pair&& __p)
2421 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2422 is_nothrow_move_assignable<_T2>::value)
2423 {
2424 base::operator=(_VSTD::move(__p));
2425 return *this;
2426 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002427
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002428#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002429
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002430#ifndef _LIBCPP_HAS_NO_VARIADICS
2431
2432 template <class... _Args1, class... _Args2>
2433 _LIBCPP_INLINE_VISIBILITY
2434 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2435 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002436 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002437 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2438 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2439 {}
2440
2441#endif // _LIBCPP_HAS_NO_VARIADICS
2442
Howard Hinnant1694d232011-05-28 14:41:13 +00002443 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2444 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445
Howard Hinnant1694d232011-05-28 14:41:13 +00002446 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2447 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002448
Howard Hinnant1694d232011-05-28 14:41:13 +00002449 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2450 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002451 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002452 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002453};
2454
2455template <class _T1, class _T2>
2456inline _LIBCPP_INLINE_VISIBILITY
2457void
2458swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002459 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002460 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002461 {__x.swap(__y);}
2462
Howard Hinnant57199402012-01-02 17:56:02 +00002463// __same_or_less_cv_qualified
2464
2465template <class _Ptr1, class _Ptr2,
2466 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2467 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2468 >::value
2469 >
2470struct __same_or_less_cv_qualified_imp
2471 : is_convertible<_Ptr1, _Ptr2> {};
2472
2473template <class _Ptr1, class _Ptr2>
2474struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2475 : false_type {};
2476
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002477template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2478 is_same<_Ptr1, _Ptr2>::value ||
2479 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002480struct __same_or_less_cv_qualified
2481 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2482
2483template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002484struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002485 : false_type {};
2486
2487// default_delete
2488
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002490struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491{
Howard Hinnant46e94932012-07-07 20:56:04 +00002492#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2493 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2494#else
2495 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2496#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497 template <class _Up>
2498 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002499 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2500 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501 {
2502 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002503 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002504 delete __ptr;
2505 }
2506};
2507
2508template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002509struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510{
Howard Hinnant8e843502011-12-18 21:19:44 +00002511public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002512#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2513 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2514#else
2515 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2516#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002517 template <class _Up>
2518 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002519 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002520 template <class _Up>
2521 _LIBCPP_INLINE_VISIBILITY
2522 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002523 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524 {
2525 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002526 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527 delete [] __ptr;
2528 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529};
2530
2531template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002532class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533{
2534public:
2535 typedef _Tp element_type;
2536 typedef _Dp deleter_type;
2537 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2538private:
2539 __compressed_pair<pointer, deleter_type> __ptr_;
2540
Howard Hinnant57199402012-01-02 17:56:02 +00002541#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002542 unique_ptr(unique_ptr&);
2543 template <class _Up, class _Ep>
2544 unique_ptr(unique_ptr<_Up, _Ep>&);
2545 unique_ptr& operator=(unique_ptr&);
2546 template <class _Up, class _Ep>
2547 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002548#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549
2550 struct __nat {int __for_bool_;};
2551
2552 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2553 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2554public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002555 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556 : __ptr_(pointer())
2557 {
2558 static_assert(!is_pointer<deleter_type>::value,
2559 "unique_ptr constructed with null function pointer deleter");
2560 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002561 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562 : __ptr_(pointer())
2563 {
2564 static_assert(!is_pointer<deleter_type>::value,
2565 "unique_ptr constructed with null function pointer deleter");
2566 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002567 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002568 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 {
2570 static_assert(!is_pointer<deleter_type>::value,
2571 "unique_ptr constructed with null function pointer deleter");
2572 }
2573
Howard Hinnant73d21a42010-09-04 23:28:19 +00002574#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002575 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2576 is_reference<deleter_type>::value,
2577 deleter_type,
2578 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002579 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002580 : __ptr_(__p, __d) {}
2581
2582 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002583 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002584 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585 {
2586 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2587 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002588 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002589 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590 template <class _Up, class _Ep>
2591 _LIBCPP_INLINE_VISIBILITY
2592 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2593 typename enable_if
2594 <
2595 !is_array<_Up>::value &&
2596 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2597 is_convertible<_Ep, deleter_type>::value &&
2598 (
2599 !is_reference<deleter_type>::value ||
2600 is_same<deleter_type, _Ep>::value
2601 ),
2602 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002603 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002604 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605
2606 template <class _Up>
2607 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2608 typename enable_if<
2609 is_convertible<_Up*, _Tp*>::value &&
2610 is_same<_Dp, default_delete<_Tp> >::value,
2611 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002612 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002613 : __ptr_(__p.release())
2614 {
2615 }
2616
Howard Hinnant1694d232011-05-28 14:41:13 +00002617 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618 {
2619 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002620 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621 return *this;
2622 }
2623
2624 template <class _Up, class _Ep>
2625 _LIBCPP_INLINE_VISIBILITY
2626 typename enable_if
2627 <
Howard Hinnant57199402012-01-02 17:56:02 +00002628 !is_array<_Up>::value &&
2629 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2630 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002631 unique_ptr&
2632 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002633 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002634 {
2635 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002636 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 return *this;
2638 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002639#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640
2641 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2642 {
2643 return __rv<unique_ptr>(*this);
2644 }
2645
2646 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002647 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648
2649 template <class _Up, class _Ep>
2650 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2651 {
2652 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002653 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002654 return *this;
2655 }
2656
2657 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002658 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659
2660 template <class _Up>
2661 _LIBCPP_INLINE_VISIBILITY
2662 typename enable_if<
2663 is_convertible<_Up*, _Tp*>::value &&
2664 is_same<_Dp, default_delete<_Tp> >::value,
2665 unique_ptr&
2666 >::type
2667 operator=(auto_ptr<_Up> __p)
2668 {reset(__p.release()); return *this;}
2669
Howard Hinnant73d21a42010-09-04 23:28:19 +00002670#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2672
Howard Hinnant1694d232011-05-28 14:41:13 +00002673 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002674 {
2675 reset();
2676 return *this;
2677 }
2678
2679 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2680 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002681 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2682 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2683 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2684 {return __ptr_.second();}
2685 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2686 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002687 _LIBCPP_INLINE_VISIBILITY
2688 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2689 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690
Howard Hinnant1694d232011-05-28 14:41:13 +00002691 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692 {
2693 pointer __t = __ptr_.first();
2694 __ptr_.first() = pointer();
2695 return __t;
2696 }
2697
Howard Hinnant1694d232011-05-28 14:41:13 +00002698 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 {
2700 pointer __tmp = __ptr_.first();
2701 __ptr_.first() = __p;
2702 if (__tmp)
2703 __ptr_.second()(__tmp);
2704 }
2705
Howard Hinnant1694d232011-05-28 14:41:13 +00002706 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2707 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002708};
2709
2710template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002711class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712{
2713public:
2714 typedef _Tp element_type;
2715 typedef _Dp deleter_type;
2716 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2717private:
2718 __compressed_pair<pointer, deleter_type> __ptr_;
2719
Howard Hinnant57199402012-01-02 17:56:02 +00002720#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002721 unique_ptr(unique_ptr&);
2722 template <class _Up>
2723 unique_ptr(unique_ptr<_Up>&);
2724 unique_ptr& operator=(unique_ptr&);
2725 template <class _Up>
2726 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002727#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728
2729 struct __nat {int __for_bool_;};
2730
2731 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2732 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2733public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002734 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735 : __ptr_(pointer())
2736 {
2737 static_assert(!is_pointer<deleter_type>::value,
2738 "unique_ptr constructed with null function pointer deleter");
2739 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002740 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002741 : __ptr_(pointer())
2742 {
2743 static_assert(!is_pointer<deleter_type>::value,
2744 "unique_ptr constructed with null function pointer deleter");
2745 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002746#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002747 template <class _Pp>
2748 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2749 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002750 : __ptr_(__p)
2751 {
2752 static_assert(!is_pointer<deleter_type>::value,
2753 "unique_ptr constructed with null function pointer deleter");
2754 }
2755
Logan Chiene1678a12014-01-31 09:30:46 +00002756 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002757 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002758 is_reference<deleter_type>::value,
2759 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002760 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2761 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002762 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763 : __ptr_(__p, __d) {}
2764
2765 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2766 is_reference<deleter_type>::value,
2767 deleter_type,
2768 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002769 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770 : __ptr_(pointer(), __d) {}
2771
Logan Chiene1678a12014-01-31 09:30:46 +00002772 template <class _Pp>
2773 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2774 typename remove_reference<deleter_type>::type&& __d,
2775 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002776 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002777 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778 {
2779 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2780 }
2781
2782 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002783 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002784 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785 {
2786 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2787 }
2788
Howard Hinnant1694d232011-05-28 14:41:13 +00002789 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002790 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791
Howard Hinnant1694d232011-05-28 14:41:13 +00002792 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793 {
2794 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002795 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002796 return *this;
2797 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002798
2799 template <class _Up, class _Ep>
2800 _LIBCPP_INLINE_VISIBILITY
2801 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2802 typename enable_if
2803 <
2804 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002805 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002806 && is_convertible<_Ep, deleter_type>::value &&
2807 (
2808 !is_reference<deleter_type>::value ||
2809 is_same<deleter_type, _Ep>::value
2810 ),
2811 __nat
2812 >::type = __nat()
2813 ) _NOEXCEPT
2814 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2815
2816
2817 template <class _Up, class _Ep>
2818 _LIBCPP_INLINE_VISIBILITY
2819 typename enable_if
2820 <
2821 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002822 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2823 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002824 unique_ptr&
2825 >::type
2826 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2827 {
2828 reset(__u.release());
2829 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2830 return *this;
2831 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002832#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833
2834 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2835 : __ptr_(__p)
2836 {
2837 static_assert(!is_pointer<deleter_type>::value,
2838 "unique_ptr constructed with null function pointer deleter");
2839 }
2840
2841 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002842 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843
2844 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002845 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846
2847 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2848 {
2849 return __rv<unique_ptr>(*this);
2850 }
2851
2852 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002853 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854
2855 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2856 {
2857 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002858 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002859 return *this;
2860 }
2861
Howard Hinnant73d21a42010-09-04 23:28:19 +00002862#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002863 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2864
Howard Hinnant1694d232011-05-28 14:41:13 +00002865 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866 {
2867 reset();
2868 return *this;
2869 }
2870
2871 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2872 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002873 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2874 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2875 {return __ptr_.second();}
2876 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2877 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002878 _LIBCPP_INLINE_VISIBILITY
2879 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2880 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002881
Howard Hinnant1694d232011-05-28 14:41:13 +00002882 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002883 {
2884 pointer __t = __ptr_.first();
2885 __ptr_.first() = pointer();
2886 return __t;
2887 }
2888
Howard Hinnant73d21a42010-09-04 23:28:19 +00002889#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002890 template <class _Pp>
2891 _LIBCPP_INLINE_VISIBILITY
2892 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2893 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002894 {
2895 pointer __tmp = __ptr_.first();
2896 __ptr_.first() = __p;
2897 if (__tmp)
2898 __ptr_.second()(__tmp);
2899 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002900 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002901 {
2902 pointer __tmp = __ptr_.first();
2903 __ptr_.first() = nullptr;
2904 if (__tmp)
2905 __ptr_.second()(__tmp);
2906 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002907 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002908 {
2909 pointer __tmp = __ptr_.first();
2910 __ptr_.first() = nullptr;
2911 if (__tmp)
2912 __ptr_.second()(__tmp);
2913 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002914#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2916 {
2917 pointer __tmp = __ptr_.first();
2918 __ptr_.first() = __p;
2919 if (__tmp)
2920 __ptr_.second()(__tmp);
2921 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002922#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002923
2924 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2925private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002926
Howard Hinnant73d21a42010-09-04 23:28:19 +00002927#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002928 template <class _Up>
2929 explicit unique_ptr(_Up);
2930 template <class _Up>
2931 unique_ptr(_Up __u,
2932 typename conditional<
2933 is_reference<deleter_type>::value,
2934 deleter_type,
2935 typename add_lvalue_reference<const deleter_type>::type>::type,
2936 typename enable_if
2937 <
2938 is_convertible<_Up, pointer>::value,
2939 __nat
2940 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002941#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002942};
2943
2944template <class _Tp, class _Dp>
2945inline _LIBCPP_INLINE_VISIBILITY
2946void
Howard Hinnant1694d232011-05-28 14:41:13 +00002947swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948
2949template <class _T1, class _D1, class _T2, class _D2>
2950inline _LIBCPP_INLINE_VISIBILITY
2951bool
2952operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2953
2954template <class _T1, class _D1, class _T2, class _D2>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
2957operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2958
2959template <class _T1, class _D1, class _T2, class _D2>
2960inline _LIBCPP_INLINE_VISIBILITY
2961bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002962operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2963{
2964 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2965 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002966 typedef typename common_type<_P1, _P2>::type _Vp;
2967 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002968}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002969
2970template <class _T1, class _D1, class _T2, class _D2>
2971inline _LIBCPP_INLINE_VISIBILITY
2972bool
2973operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2974
2975template <class _T1, class _D1, class _T2, class _D2>
2976inline _LIBCPP_INLINE_VISIBILITY
2977bool
2978operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2979
2980template <class _T1, class _D1, class _T2, class _D2>
2981inline _LIBCPP_INLINE_VISIBILITY
2982bool
2983operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2984
Howard Hinnant3fadda32012-02-21 21:02:58 +00002985template <class _T1, class _D1>
2986inline _LIBCPP_INLINE_VISIBILITY
2987bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002988operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002989{
2990 return !__x;
2991}
2992
2993template <class _T1, class _D1>
2994inline _LIBCPP_INLINE_VISIBILITY
2995bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002996operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002997{
2998 return !__x;
2999}
3000
3001template <class _T1, class _D1>
3002inline _LIBCPP_INLINE_VISIBILITY
3003bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003004operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003005{
3006 return static_cast<bool>(__x);
3007}
3008
3009template <class _T1, class _D1>
3010inline _LIBCPP_INLINE_VISIBILITY
3011bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003012operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003013{
3014 return static_cast<bool>(__x);
3015}
3016
3017template <class _T1, class _D1>
3018inline _LIBCPP_INLINE_VISIBILITY
3019bool
3020operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3021{
3022 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3023 return less<_P1>()(__x.get(), nullptr);
3024}
3025
3026template <class _T1, class _D1>
3027inline _LIBCPP_INLINE_VISIBILITY
3028bool
3029operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3030{
3031 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3032 return less<_P1>()(nullptr, __x.get());
3033}
3034
3035template <class _T1, class _D1>
3036inline _LIBCPP_INLINE_VISIBILITY
3037bool
3038operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3039{
3040 return nullptr < __x;
3041}
3042
3043template <class _T1, class _D1>
3044inline _LIBCPP_INLINE_VISIBILITY
3045bool
3046operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3047{
3048 return __x < nullptr;
3049}
3050
3051template <class _T1, class _D1>
3052inline _LIBCPP_INLINE_VISIBILITY
3053bool
3054operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3055{
3056 return !(nullptr < __x);
3057}
3058
3059template <class _T1, class _D1>
3060inline _LIBCPP_INLINE_VISIBILITY
3061bool
3062operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3063{
3064 return !(__x < nullptr);
3065}
3066
3067template <class _T1, class _D1>
3068inline _LIBCPP_INLINE_VISIBILITY
3069bool
3070operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3071{
3072 return !(__x < nullptr);
3073}
3074
3075template <class _T1, class _D1>
3076inline _LIBCPP_INLINE_VISIBILITY
3077bool
3078operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3079{
3080 return !(nullptr < __x);
3081}
3082
Howard Hinnant87073e42012-05-01 15:37:54 +00003083#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3084
3085template <class _Tp, class _Dp>
3086inline _LIBCPP_INLINE_VISIBILITY
3087unique_ptr<_Tp, _Dp>
3088move(unique_ptr<_Tp, _Dp>& __t)
3089{
3090 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3091}
3092
3093#endif
3094
Marshall Clowfd7481e2013-07-01 18:16:03 +00003095#if _LIBCPP_STD_VER > 11
3096
3097template<class _Tp>
3098struct __unique_if
3099{
3100 typedef unique_ptr<_Tp> __unique_single;
3101};
3102
3103template<class _Tp>
3104struct __unique_if<_Tp[]>
3105{
3106 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3107};
3108
3109template<class _Tp, size_t _Np>
3110struct __unique_if<_Tp[_Np]>
3111{
3112 typedef void __unique_array_known_bound;
3113};
3114
3115template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003116inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003117typename __unique_if<_Tp>::__unique_single
3118make_unique(_Args&&... __args)
3119{
3120 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3121}
3122
3123template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003124inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003125typename __unique_if<_Tp>::__unique_array_unknown_bound
3126make_unique(size_t __n)
3127{
3128 typedef typename remove_extent<_Tp>::type _Up;
3129 return unique_ptr<_Tp>(new _Up[__n]());
3130}
3131
3132template<class _Tp, class... _Args>
3133 typename __unique_if<_Tp>::__unique_array_known_bound
3134 make_unique(_Args&&...) = delete;
3135
3136#endif // _LIBCPP_STD_VER > 11
3137
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003138template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003139
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003140template <class _Size>
3141inline _LIBCPP_INLINE_VISIBILITY
3142_Size
3143__loadword(const void* __p)
3144{
3145 _Size __r;
3146 std::memcpy(&__r, __p, sizeof(__r));
3147 return __r;
3148}
3149
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003150// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3151// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3152// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003153template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003154struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003155
3156template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003157struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003158{
3159 _Size operator()(const void* __key, _Size __len);
3160};
3161
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003162// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003163template <class _Size>
3164_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003165__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003166{
3167 const _Size __m = 0x5bd1e995;
3168 const _Size __r = 24;
3169 _Size __h = __len;
3170 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3171 for (; __len >= 4; __data += 4, __len -= 4)
3172 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003173 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003174 __k *= __m;
3175 __k ^= __k >> __r;
3176 __k *= __m;
3177 __h *= __m;
3178 __h ^= __k;
3179 }
3180 switch (__len)
3181 {
3182 case 3:
3183 __h ^= __data[2] << 16;
3184 case 2:
3185 __h ^= __data[1] << 8;
3186 case 1:
3187 __h ^= __data[0];
3188 __h *= __m;
3189 }
3190 __h ^= __h >> 13;
3191 __h *= __m;
3192 __h ^= __h >> 15;
3193 return __h;
3194}
3195
3196template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003197struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003198{
3199 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003200
3201 private:
3202 // Some primes between 2^63 and 2^64.
3203 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3204 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3205 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3206 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3207
3208 static _Size __rotate(_Size __val, int __shift) {
3209 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3210 }
3211
3212 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3213 return (__val >> __shift) | (__val << (64 - __shift));
3214 }
3215
3216 static _Size __shift_mix(_Size __val) {
3217 return __val ^ (__val >> 47);
3218 }
3219
3220 static _Size __hash_len_16(_Size __u, _Size __v) {
3221 const _Size __mul = 0x9ddfea08eb382d69ULL;
3222 _Size __a = (__u ^ __v) * __mul;
3223 __a ^= (__a >> 47);
3224 _Size __b = (__v ^ __a) * __mul;
3225 __b ^= (__b >> 47);
3226 __b *= __mul;
3227 return __b;
3228 }
3229
3230 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3231 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003232 const _Size __a = __loadword<_Size>(__s);
3233 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003234 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3235 }
3236 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003237 const uint32_t __a = __loadword<uint32_t>(__s);
3238 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003239 return __hash_len_16(__len + (__a << 3), __b);
3240 }
3241 if (__len > 0) {
3242 const unsigned char __a = __s[0];
3243 const unsigned char __b = __s[__len >> 1];
3244 const unsigned char __c = __s[__len - 1];
3245 const uint32_t __y = static_cast<uint32_t>(__a) +
3246 (static_cast<uint32_t>(__b) << 8);
3247 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3248 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3249 }
3250 return __k2;
3251 }
3252
3253 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003254 const _Size __a = __loadword<_Size>(__s) * __k1;
3255 const _Size __b = __loadword<_Size>(__s + 8);
3256 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3257 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003258 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3259 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3260 }
3261
3262 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3263 // Callers do best to use "random-looking" values for a and b.
3264 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3265 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3266 __a += __w;
3267 __b = __rotate(__b + __a + __z, 21);
3268 const _Size __c = __a;
3269 __a += __x;
3270 __a += __y;
3271 __b += __rotate(__a, 44);
3272 return pair<_Size, _Size>(__a + __z, __b + __c);
3273 }
3274
3275 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3276 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3277 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003278 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3279 __loadword<_Size>(__s + 8),
3280 __loadword<_Size>(__s + 16),
3281 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003282 __a,
3283 __b);
3284 }
3285
3286 // Return an 8-byte hash for 33 to 64 bytes.
3287 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003288 _Size __z = __loadword<_Size>(__s + 24);
3289 _Size __a = __loadword<_Size>(__s) +
3290 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003291 _Size __b = __rotate(__a + __z, 52);
3292 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003293 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003294 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003295 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003296 _Size __vf = __a + __z;
3297 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003298 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3299 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003300 __b = __rotate(__a + __z, 52);
3301 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003302 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003303 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003304 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003305 _Size __wf = __a + __z;
3306 _Size __ws = __b + __rotate(__a, 31) + __c;
3307 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3308 return __shift_mix(__r * __k0 + __vs) * __k2;
3309 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003310};
3311
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003312// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003313template <class _Size>
3314_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003315__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003316{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003317 const char* __s = static_cast<const char*>(__key);
3318 if (__len <= 32) {
3319 if (__len <= 16) {
3320 return __hash_len_0_to_16(__s, __len);
3321 } else {
3322 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003323 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003324 } else if (__len <= 64) {
3325 return __hash_len_33_to_64(__s, __len);
3326 }
3327
3328 // For strings over 64 bytes we hash the end first, and then as we
3329 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003330 _Size __x = __loadword<_Size>(__s + __len - 40);
3331 _Size __y = __loadword<_Size>(__s + __len - 16) +
3332 __loadword<_Size>(__s + __len - 56);
3333 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3334 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003335 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3336 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003337 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003338
3339 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3340 __len = (__len - 1) & ~static_cast<_Size>(63);
3341 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003342 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3343 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003344 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003345 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003346 __z = __rotate(__z + __w.first, 33) * __k1;
3347 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3348 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003349 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003350 std::swap(__z, __x);
3351 __s += 64;
3352 __len -= 64;
3353 } while (__len != 0);
3354 return __hash_len_16(
3355 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3356 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003357}
3358
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003359template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3360struct __scalar_hash;
3361
3362template <class _Tp>
3363struct __scalar_hash<_Tp, 0>
3364 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003365{
Howard Hinnant82894812010-09-22 16:48:34 +00003366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003367 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003368 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003369 union
3370 {
3371 _Tp __t;
3372 size_t __a;
3373 } __u;
3374 __u.__a = 0;
3375 __u.__t = __v;
3376 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003377 }
3378};
3379
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003380template <class _Tp>
3381struct __scalar_hash<_Tp, 1>
3382 : public unary_function<_Tp, size_t>
3383{
3384 _LIBCPP_INLINE_VISIBILITY
3385 size_t operator()(_Tp __v) const _NOEXCEPT
3386 {
3387 union
3388 {
3389 _Tp __t;
3390 size_t __a;
3391 } __u;
3392 __u.__t = __v;
3393 return __u.__a;
3394 }
3395};
3396
3397template <class _Tp>
3398struct __scalar_hash<_Tp, 2>
3399 : public unary_function<_Tp, size_t>
3400{
3401 _LIBCPP_INLINE_VISIBILITY
3402 size_t operator()(_Tp __v) const _NOEXCEPT
3403 {
3404 union
3405 {
3406 _Tp __t;
3407 struct
3408 {
3409 size_t __a;
3410 size_t __b;
3411 };
3412 } __u;
3413 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003414 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003415 }
3416};
3417
3418template <class _Tp>
3419struct __scalar_hash<_Tp, 3>
3420 : public unary_function<_Tp, size_t>
3421{
3422 _LIBCPP_INLINE_VISIBILITY
3423 size_t operator()(_Tp __v) const _NOEXCEPT
3424 {
3425 union
3426 {
3427 _Tp __t;
3428 struct
3429 {
3430 size_t __a;
3431 size_t __b;
3432 size_t __c;
3433 };
3434 } __u;
3435 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003436 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003437 }
3438};
3439
3440template <class _Tp>
3441struct __scalar_hash<_Tp, 4>
3442 : public unary_function<_Tp, size_t>
3443{
3444 _LIBCPP_INLINE_VISIBILITY
3445 size_t operator()(_Tp __v) const _NOEXCEPT
3446 {
3447 union
3448 {
3449 _Tp __t;
3450 struct
3451 {
3452 size_t __a;
3453 size_t __b;
3454 size_t __c;
3455 size_t __d;
3456 };
3457 } __u;
3458 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003459 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003460 }
3461};
3462
3463template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003464struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003465 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003466{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003467 _LIBCPP_INLINE_VISIBILITY
3468 size_t operator()(_Tp* __v) const _NOEXCEPT
3469 {
3470 union
3471 {
3472 _Tp* __t;
3473 size_t __a;
3474 } __u;
3475 __u.__t = __v;
3476 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3477 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003478};
3479
Howard Hinnant21aefc32010-06-03 16:42:57 +00003480template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003481struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003482{
3483 typedef unique_ptr<_Tp, _Dp> argument_type;
3484 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003486 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003487 {
3488 typedef typename argument_type::pointer pointer;
3489 return hash<pointer>()(__ptr.get());
3490 }
3491};
3492
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003493struct __destruct_n
3494{
3495private:
3496 size_t size;
3497
3498 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003499 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003500 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3501
3502 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003503 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003504 {}
3505
Howard Hinnant1694d232011-05-28 14:41:13 +00003506 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003507 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003508 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003509 {}
3510
Howard Hinnant1694d232011-05-28 14:41:13 +00003511 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003513 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003514 {}
3515public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003516 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3517 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003518
3519 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003520 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003521 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003522
3523 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003524 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003525 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003526
3527 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003528 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003529 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530};
3531
3532template <class _Alloc>
3533class __allocator_destructor
3534{
3535 typedef allocator_traits<_Alloc> __alloc_traits;
3536public:
3537 typedef typename __alloc_traits::pointer pointer;
3538 typedef typename __alloc_traits::size_type size_type;
3539private:
3540 _Alloc& __alloc_;
3541 size_type __s_;
3542public:
3543 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003544 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003545 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003547 void operator()(pointer __p) _NOEXCEPT
3548 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003549};
3550
3551template <class _InputIterator, class _ForwardIterator>
3552_ForwardIterator
3553uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3554{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003556#ifndef _LIBCPP_NO_EXCEPTIONS
3557 _ForwardIterator __s = __r;
3558 try
3559 {
3560#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003561 for (; __f != __l; ++__f, (void) ++__r)
3562 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003563#ifndef _LIBCPP_NO_EXCEPTIONS
3564 }
3565 catch (...)
3566 {
3567 for (; __s != __r; ++__s)
3568 __s->~value_type();
3569 throw;
3570 }
3571#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572 return __r;
3573}
3574
3575template <class _InputIterator, class _Size, class _ForwardIterator>
3576_ForwardIterator
3577uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3578{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003580#ifndef _LIBCPP_NO_EXCEPTIONS
3581 _ForwardIterator __s = __r;
3582 try
3583 {
3584#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003585 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3586 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003587#ifndef _LIBCPP_NO_EXCEPTIONS
3588 }
3589 catch (...)
3590 {
3591 for (; __s != __r; ++__s)
3592 __s->~value_type();
3593 throw;
3594 }
3595#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003596 return __r;
3597}
3598
3599template <class _ForwardIterator, class _Tp>
3600void
3601uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3602{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003603 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003604#ifndef _LIBCPP_NO_EXCEPTIONS
3605 _ForwardIterator __s = __f;
3606 try
3607 {
3608#endif
3609 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003610 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003611#ifndef _LIBCPP_NO_EXCEPTIONS
3612 }
3613 catch (...)
3614 {
3615 for (; __s != __f; ++__s)
3616 __s->~value_type();
3617 throw;
3618 }
3619#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003620}
3621
3622template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003623_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3625{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003626 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003627#ifndef _LIBCPP_NO_EXCEPTIONS
3628 _ForwardIterator __s = __f;
3629 try
3630 {
3631#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003632 for (; __n > 0; ++__f, (void) --__n)
3633 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003634#ifndef _LIBCPP_NO_EXCEPTIONS
3635 }
3636 catch (...)
3637 {
3638 for (; __s != __f; ++__s)
3639 __s->~value_type();
3640 throw;
3641 }
3642#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003643 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003644}
3645
Howard Hinnant82894812010-09-22 16:48:34 +00003646class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647 : public std::exception
3648{
3649public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003650 virtual ~bad_weak_ptr() _NOEXCEPT;
3651 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652};
3653
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003654template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003656class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657{
3658 __shared_count(const __shared_count&);
3659 __shared_count& operator=(const __shared_count&);
3660
3661protected:
3662 long __shared_owners_;
3663 virtual ~__shared_count();
3664private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003665 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003666
3667public:
Howard Hinnant82894812010-09-22 16:48:34 +00003668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003669 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003670 : __shared_owners_(__refs) {}
3671
Howard Hinnant1694d232011-05-28 14:41:13 +00003672 void __add_shared() _NOEXCEPT;
3673 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003675 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676};
3677
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003678class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003679 : private __shared_count
3680{
3681 long __shared_weak_owners_;
3682
3683public:
Howard Hinnant82894812010-09-22 16:48:34 +00003684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003685 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686 : __shared_count(__refs),
3687 __shared_weak_owners_(__refs) {}
3688protected:
3689 virtual ~__shared_weak_count();
3690
3691public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003692 void __add_shared() _NOEXCEPT;
3693 void __add_weak() _NOEXCEPT;
3694 void __release_shared() _NOEXCEPT;
3695 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003697 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3698 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003700 // Define the function out only if we build static libc++ without RTTI.
3701 // Otherwise we may break clients who need to compile their projects with
3702 // -fno-rtti and yet link against a libc++.dylib compiled
3703 // without -fno-rtti.
3704#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003705 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003706#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003707private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003708 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709};
3710
3711template <class _Tp, class _Dp, class _Alloc>
3712class __shared_ptr_pointer
3713 : public __shared_weak_count
3714{
3715 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3716public:
Howard Hinnant82894812010-09-22 16:48:34 +00003717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003718 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003719 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003720
Howard Hinnantd4444702010-08-11 17:04:31 +00003721#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003722 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003723#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003724
3725private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003726 virtual void __on_zero_shared() _NOEXCEPT;
3727 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728};
3729
Howard Hinnantd4444702010-08-11 17:04:31 +00003730#ifndef _LIBCPP_NO_RTTI
3731
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732template <class _Tp, class _Dp, class _Alloc>
3733const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003734__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003735{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003736 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003737}
3738
Howard Hinnant324bb032010-08-22 00:02:43 +00003739#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003740
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741template <class _Tp, class _Dp, class _Alloc>
3742void
Howard Hinnant1694d232011-05-28 14:41:13 +00003743__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003744{
3745 __data_.first().second()(__data_.first().first());
3746 __data_.first().second().~_Dp();
3747}
3748
3749template <class _Tp, class _Dp, class _Alloc>
3750void
Howard Hinnant1694d232011-05-28 14:41:13 +00003751__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003752{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003753 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3754 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003755 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3756
Eric Fiselier8492cd82015-02-05 23:01:40 +00003757 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003759 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003760}
3761
3762template <class _Tp, class _Alloc>
3763class __shared_ptr_emplace
3764 : public __shared_weak_count
3765{
3766 __compressed_pair<_Alloc, _Tp> __data_;
3767public:
3768#ifndef _LIBCPP_HAS_NO_VARIADICS
3769
Howard Hinnant82894812010-09-22 16:48:34 +00003770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003771 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003772 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003773
3774 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003777 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3778 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003779
3780#else // _LIBCPP_HAS_NO_VARIADICS
3781
Howard Hinnant82894812010-09-22 16:48:34 +00003782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783 __shared_ptr_emplace(_Alloc __a)
3784 : __data_(__a) {}
3785
3786 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003788 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3789 : __data_(__a, _Tp(__a0)) {}
3790
3791 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3794 : __data_(__a, _Tp(__a0, __a1)) {}
3795
3796 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003798 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3799 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3800
3801#endif // _LIBCPP_HAS_NO_VARIADICS
3802
3803private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003804 virtual void __on_zero_shared() _NOEXCEPT;
3805 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003806public:
Howard Hinnant82894812010-09-22 16:48:34 +00003807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003808 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809};
3810
3811template <class _Tp, class _Alloc>
3812void
Howard Hinnant1694d232011-05-28 14:41:13 +00003813__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003814{
3815 __data_.second().~_Tp();
3816}
3817
3818template <class _Tp, class _Alloc>
3819void
Howard Hinnant1694d232011-05-28 14:41:13 +00003820__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003822 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3823 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003824 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003825 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003826 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003827 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003828}
3829
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003830template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003831
3832template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003833class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003834{
Howard Hinnant324bb032010-08-22 00:02:43 +00003835public:
3836 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003837private:
3838 element_type* __ptr_;
3839 __shared_weak_count* __cntrl_;
3840
3841 struct __nat {int __for_bool_;};
3842public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003843 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3844 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003845 template<class _Yp>
3846 explicit shared_ptr(_Yp* __p,
3847 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3848 template<class _Yp, class _Dp>
3849 shared_ptr(_Yp* __p, _Dp __d,
3850 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3851 template<class _Yp, class _Dp, class _Alloc>
3852 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3853 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3855 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003856 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3857 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003858 template<class _Yp>
3859 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003860 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3861 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003863 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003864 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003865 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3866 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003867#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003869 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003870#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003871 template<class _Yp>
3872 shared_ptr(auto_ptr<_Yp>&& __r,
3873 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003874#else
Logan Chiene1678a12014-01-31 09:30:46 +00003875 template<class _Yp>
3876 shared_ptr(auto_ptr<_Yp> __r,
3877 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003878#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003879#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003880 template <class _Yp, class _Dp>
3881 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3882 typename enable_if
3883 <
3884 !is_lvalue_reference<_Dp>::value &&
3885 !is_array<_Yp>::value &&
3886 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3887 __nat
3888 >::type = __nat());
3889 template <class _Yp, class _Dp>
3890 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3891 typename enable_if
3892 <
3893 is_lvalue_reference<_Dp>::value &&
3894 !is_array<_Yp>::value &&
3895 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3896 __nat
3897 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003898#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003899 template <class _Yp, class _Dp>
3900 shared_ptr(unique_ptr<_Yp, _Dp>,
3901 typename enable_if
3902 <
3903 !is_lvalue_reference<_Dp>::value &&
3904 !is_array<_Yp>::value &&
3905 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3906 __nat
3907 >::type = __nat());
3908 template <class _Yp, class _Dp>
3909 shared_ptr(unique_ptr<_Yp, _Dp>,
3910 typename enable_if
3911 <
3912 is_lvalue_reference<_Dp>::value &&
3913 !is_array<_Yp>::value &&
3914 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3915 __nat
3916 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003917#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003918
3919 ~shared_ptr();
3920
Howard Hinnant1694d232011-05-28 14:41:13 +00003921 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003922 template<class _Yp>
3923 typename enable_if
3924 <
3925 is_convertible<_Yp*, element_type*>::value,
3926 shared_ptr&
3927 >::type
3928 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003929#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003930 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003931 template<class _Yp>
3932 typename enable_if
3933 <
3934 is_convertible<_Yp*, element_type*>::value,
3935 shared_ptr<_Tp>&
3936 >::type
3937 operator=(shared_ptr<_Yp>&& __r);
3938 template<class _Yp>
3939 typename enable_if
3940 <
3941 !is_array<_Yp>::value &&
3942 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003943 shared_ptr
3944 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003945 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003946#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003947 template<class _Yp>
3948 typename enable_if
3949 <
3950 !is_array<_Yp>::value &&
3951 is_convertible<_Yp*, element_type*>::value,
3952 shared_ptr&
3953 >::type
3954 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003955#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003956 template <class _Yp, class _Dp>
3957 typename enable_if
3958 <
3959 !is_array<_Yp>::value &&
3960 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3961 shared_ptr&
3962 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003963#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003964 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003965#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003966 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003967#endif
3968
Howard Hinnant1694d232011-05-28 14:41:13 +00003969 void swap(shared_ptr& __r) _NOEXCEPT;
3970 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003971 template<class _Yp>
3972 typename enable_if
3973 <
3974 is_convertible<_Yp*, element_type*>::value,
3975 void
3976 >::type
3977 reset(_Yp* __p);
3978 template<class _Yp, class _Dp>
3979 typename enable_if
3980 <
3981 is_convertible<_Yp*, element_type*>::value,
3982 void
3983 >::type
3984 reset(_Yp* __p, _Dp __d);
3985 template<class _Yp, class _Dp, class _Alloc>
3986 typename enable_if
3987 <
3988 is_convertible<_Yp*, element_type*>::value,
3989 void
3990 >::type
3991 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003992
Howard Hinnant82894812010-09-22 16:48:34 +00003993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003994 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003996 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3997 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003999 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004001 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004003 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004005 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00004006 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004008 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004009 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004010 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004012 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004013 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004014 _LIBCPP_INLINE_VISIBILITY
4015 bool
4016 __owner_equivalent(const shared_ptr& __p) const
4017 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004018
Howard Hinnantd4444702010-08-11 17:04:31 +00004019#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004020 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004022 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004023 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004024#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004025
4026#ifndef _LIBCPP_HAS_NO_VARIADICS
4027
4028 template<class ..._Args>
4029 static
4030 shared_ptr<_Tp>
4031 make_shared(_Args&& ...__args);
4032
4033 template<class _Alloc, class ..._Args>
4034 static
4035 shared_ptr<_Tp>
4036 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4037
4038#else // _LIBCPP_HAS_NO_VARIADICS
4039
4040 static shared_ptr<_Tp> make_shared();
4041
4042 template<class _A0>
4043 static shared_ptr<_Tp> make_shared(_A0&);
4044
4045 template<class _A0, class _A1>
4046 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4047
4048 template<class _A0, class _A1, class _A2>
4049 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4050
4051 template<class _Alloc>
4052 static shared_ptr<_Tp>
4053 allocate_shared(const _Alloc& __a);
4054
4055 template<class _Alloc, class _A0>
4056 static shared_ptr<_Tp>
4057 allocate_shared(const _Alloc& __a, _A0& __a0);
4058
4059 template<class _Alloc, class _A0, class _A1>
4060 static shared_ptr<_Tp>
4061 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4062
4063 template<class _Alloc, class _A0, class _A1, class _A2>
4064 static shared_ptr<_Tp>
4065 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4066
4067#endif // _LIBCPP_HAS_NO_VARIADICS
4068
4069private:
4070
4071 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004073 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004074 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004075 {
4076 if (__e)
Marshall Clowc4113372015-06-19 15:54:13 +00004077 {
4078 __e->__weak_this_.__ptr_ = const_cast<_Yp*>(static_cast<const _Yp*>(__e));
4079 __e->__weak_this_.__cntrl_ = __cntrl_;
Marshall Clow46d06b92015-06-19 19:32:06 +00004080 __cntrl_->__add_weak();
Marshall Clowc4113372015-06-19 15:54:13 +00004081 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004082 }
4083
Howard Hinnant82894812010-09-22 16:48:34 +00004084 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004085 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004086
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004087 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4088 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004089};
4090
4091template<class _Tp>
4092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004093_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004094shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004095 : __ptr_(0),
4096 __cntrl_(0)
4097{
4098}
4099
4100template<class _Tp>
4101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004102_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004103shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004104 : __ptr_(0),
4105 __cntrl_(0)
4106{
4107}
4108
4109template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004110template<class _Yp>
4111shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4112 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004113 : __ptr_(__p)
4114{
4115 unique_ptr<_Yp> __hold(__p);
4116 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4117 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4118 __hold.release();
4119 __enable_weak_this(__p);
4120}
4121
4122template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004123template<class _Yp, class _Dp>
4124shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4125 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004126 : __ptr_(__p)
4127{
4128#ifndef _LIBCPP_NO_EXCEPTIONS
4129 try
4130 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004131#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004132 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4133 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4134 __enable_weak_this(__p);
4135#ifndef _LIBCPP_NO_EXCEPTIONS
4136 }
4137 catch (...)
4138 {
4139 __d(__p);
4140 throw;
4141 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004142#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004143}
4144
4145template<class _Tp>
4146template<class _Dp>
4147shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4148 : __ptr_(0)
4149{
4150#ifndef _LIBCPP_NO_EXCEPTIONS
4151 try
4152 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004153#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004154 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4155 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4156#ifndef _LIBCPP_NO_EXCEPTIONS
4157 }
4158 catch (...)
4159 {
4160 __d(__p);
4161 throw;
4162 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004163#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004164}
4165
4166template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004167template<class _Yp, class _Dp, class _Alloc>
4168shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4169 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004170 : __ptr_(__p)
4171{
4172#ifndef _LIBCPP_NO_EXCEPTIONS
4173 try
4174 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004175#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004176 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004177 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004178 typedef __allocator_destructor<_A2> _D2;
4179 _A2 __a2(__a);
4180 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004181 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4182 _CntrlBlk(__p, __d, __a);
4183 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004184 __enable_weak_this(__p);
4185#ifndef _LIBCPP_NO_EXCEPTIONS
4186 }
4187 catch (...)
4188 {
4189 __d(__p);
4190 throw;
4191 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004192#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004193}
4194
4195template<class _Tp>
4196template<class _Dp, class _Alloc>
4197shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4198 : __ptr_(0)
4199{
4200#ifndef _LIBCPP_NO_EXCEPTIONS
4201 try
4202 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004203#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004204 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004205 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004206 typedef __allocator_destructor<_A2> _D2;
4207 _A2 __a2(__a);
4208 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004209 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4210 _CntrlBlk(__p, __d, __a);
4211 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004212#ifndef _LIBCPP_NO_EXCEPTIONS
4213 }
4214 catch (...)
4215 {
4216 __d(__p);
4217 throw;
4218 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004219#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004220}
4221
4222template<class _Tp>
4223template<class _Yp>
4224inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004225shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004226 : __ptr_(__p),
4227 __cntrl_(__r.__cntrl_)
4228{
4229 if (__cntrl_)
4230 __cntrl_->__add_shared();
4231}
4232
4233template<class _Tp>
4234inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004235shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004236 : __ptr_(__r.__ptr_),
4237 __cntrl_(__r.__cntrl_)
4238{
4239 if (__cntrl_)
4240 __cntrl_->__add_shared();
4241}
4242
4243template<class _Tp>
4244template<class _Yp>
4245inline _LIBCPP_INLINE_VISIBILITY
4246shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4247 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004248 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004249 : __ptr_(__r.__ptr_),
4250 __cntrl_(__r.__cntrl_)
4251{
4252 if (__cntrl_)
4253 __cntrl_->__add_shared();
4254}
4255
Howard Hinnant73d21a42010-09-04 23:28:19 +00004256#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004257
4258template<class _Tp>
4259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004260shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004261 : __ptr_(__r.__ptr_),
4262 __cntrl_(__r.__cntrl_)
4263{
4264 __r.__ptr_ = 0;
4265 __r.__cntrl_ = 0;
4266}
4267
4268template<class _Tp>
4269template<class _Yp>
4270inline _LIBCPP_INLINE_VISIBILITY
4271shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4272 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004273 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004274 : __ptr_(__r.__ptr_),
4275 __cntrl_(__r.__cntrl_)
4276{
4277 __r.__ptr_ = 0;
4278 __r.__cntrl_ = 0;
4279}
4280
Howard Hinnant73d21a42010-09-04 23:28:19 +00004281#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004282
4283template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004284template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004285#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004286shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004287#else
Logan Chiene1678a12014-01-31 09:30:46 +00004288shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004289#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004290 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004291 : __ptr_(__r.get())
4292{
4293 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4294 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4295 __enable_weak_this(__r.get());
4296 __r.release();
4297}
4298
4299template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004300template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004301#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004302shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4303#else
4304shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4305#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004306 typename enable_if
4307 <
4308 !is_lvalue_reference<_Dp>::value &&
4309 !is_array<_Yp>::value &&
4310 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4311 __nat
4312 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004313 : __ptr_(__r.get())
4314{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004315#if _LIBCPP_STD_VER > 11
4316 if (__ptr_ == nullptr)
4317 __cntrl_ = nullptr;
4318 else
4319#endif
4320 {
4321 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4322 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4323 __enable_weak_this(__r.get());
4324 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004325 __r.release();
4326}
4327
4328template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004329template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004330#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004331shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4332#else
4333shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4334#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004335 typename enable_if
4336 <
4337 is_lvalue_reference<_Dp>::value &&
4338 !is_array<_Yp>::value &&
4339 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4340 __nat
4341 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004342 : __ptr_(__r.get())
4343{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004344#if _LIBCPP_STD_VER > 11
4345 if (__ptr_ == nullptr)
4346 __cntrl_ = nullptr;
4347 else
4348#endif
4349 {
4350 typedef __shared_ptr_pointer<_Yp*,
4351 reference_wrapper<typename remove_reference<_Dp>::type>,
4352 allocator<_Yp> > _CntrlBlk;
4353 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4354 __enable_weak_this(__r.get());
4355 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004356 __r.release();
4357}
4358
4359#ifndef _LIBCPP_HAS_NO_VARIADICS
4360
4361template<class _Tp>
4362template<class ..._Args>
4363shared_ptr<_Tp>
4364shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4365{
4366 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4367 typedef allocator<_CntrlBlk> _A2;
4368 typedef __allocator_destructor<_A2> _D2;
4369 _A2 __a2;
4370 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004371 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004372 shared_ptr<_Tp> __r;
4373 __r.__ptr_ = __hold2.get()->get();
4374 __r.__cntrl_ = __hold2.release();
4375 __r.__enable_weak_this(__r.__ptr_);
4376 return __r;
4377}
4378
4379template<class _Tp>
4380template<class _Alloc, class ..._Args>
4381shared_ptr<_Tp>
4382shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4383{
4384 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004385 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004386 typedef __allocator_destructor<_A2> _D2;
4387 _A2 __a2(__a);
4388 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004389 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4390 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004391 shared_ptr<_Tp> __r;
4392 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004393 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004394 __r.__enable_weak_this(__r.__ptr_);
4395 return __r;
4396}
4397
4398#else // _LIBCPP_HAS_NO_VARIADICS
4399
4400template<class _Tp>
4401shared_ptr<_Tp>
4402shared_ptr<_Tp>::make_shared()
4403{
4404 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4405 typedef allocator<_CntrlBlk> _Alloc2;
4406 typedef __allocator_destructor<_Alloc2> _D2;
4407 _Alloc2 __alloc2;
4408 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4409 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4410 shared_ptr<_Tp> __r;
4411 __r.__ptr_ = __hold2.get()->get();
4412 __r.__cntrl_ = __hold2.release();
4413 __r.__enable_weak_this(__r.__ptr_);
4414 return __r;
4415}
4416
4417template<class _Tp>
4418template<class _A0>
4419shared_ptr<_Tp>
4420shared_ptr<_Tp>::make_shared(_A0& __a0)
4421{
4422 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4423 typedef allocator<_CntrlBlk> _Alloc2;
4424 typedef __allocator_destructor<_Alloc2> _D2;
4425 _Alloc2 __alloc2;
4426 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4427 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4428 shared_ptr<_Tp> __r;
4429 __r.__ptr_ = __hold2.get()->get();
4430 __r.__cntrl_ = __hold2.release();
4431 __r.__enable_weak_this(__r.__ptr_);
4432 return __r;
4433}
4434
4435template<class _Tp>
4436template<class _A0, class _A1>
4437shared_ptr<_Tp>
4438shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4439{
4440 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4441 typedef allocator<_CntrlBlk> _Alloc2;
4442 typedef __allocator_destructor<_Alloc2> _D2;
4443 _Alloc2 __alloc2;
4444 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4445 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4446 shared_ptr<_Tp> __r;
4447 __r.__ptr_ = __hold2.get()->get();
4448 __r.__cntrl_ = __hold2.release();
4449 __r.__enable_weak_this(__r.__ptr_);
4450 return __r;
4451}
4452
4453template<class _Tp>
4454template<class _A0, class _A1, class _A2>
4455shared_ptr<_Tp>
4456shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4457{
4458 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4459 typedef allocator<_CntrlBlk> _Alloc2;
4460 typedef __allocator_destructor<_Alloc2> _D2;
4461 _Alloc2 __alloc2;
4462 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4463 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4464 shared_ptr<_Tp> __r;
4465 __r.__ptr_ = __hold2.get()->get();
4466 __r.__cntrl_ = __hold2.release();
4467 __r.__enable_weak_this(__r.__ptr_);
4468 return __r;
4469}
4470
4471template<class _Tp>
4472template<class _Alloc>
4473shared_ptr<_Tp>
4474shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4475{
4476 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004477 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004478 typedef __allocator_destructor<_Alloc2> _D2;
4479 _Alloc2 __alloc2(__a);
4480 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004481 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4482 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004483 shared_ptr<_Tp> __r;
4484 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004485 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004486 __r.__enable_weak_this(__r.__ptr_);
4487 return __r;
4488}
4489
4490template<class _Tp>
4491template<class _Alloc, class _A0>
4492shared_ptr<_Tp>
4493shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4494{
4495 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004496 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004497 typedef __allocator_destructor<_Alloc2> _D2;
4498 _Alloc2 __alloc2(__a);
4499 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004500 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4501 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004502 shared_ptr<_Tp> __r;
4503 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004504 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004505 __r.__enable_weak_this(__r.__ptr_);
4506 return __r;
4507}
4508
4509template<class _Tp>
4510template<class _Alloc, class _A0, class _A1>
4511shared_ptr<_Tp>
4512shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4513{
4514 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004515 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004516 typedef __allocator_destructor<_Alloc2> _D2;
4517 _Alloc2 __alloc2(__a);
4518 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004519 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4520 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004521 shared_ptr<_Tp> __r;
4522 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004523 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004524 __r.__enable_weak_this(__r.__ptr_);
4525 return __r;
4526}
4527
4528template<class _Tp>
4529template<class _Alloc, class _A0, class _A1, class _A2>
4530shared_ptr<_Tp>
4531shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4532{
4533 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004534 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004535 typedef __allocator_destructor<_Alloc2> _D2;
4536 _Alloc2 __alloc2(__a);
4537 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004538 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4539 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004540 shared_ptr<_Tp> __r;
4541 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004542 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004543 __r.__enable_weak_this(__r.__ptr_);
4544 return __r;
4545}
4546
4547#endif // _LIBCPP_HAS_NO_VARIADICS
4548
4549template<class _Tp>
4550shared_ptr<_Tp>::~shared_ptr()
4551{
4552 if (__cntrl_)
4553 __cntrl_->__release_shared();
4554}
4555
4556template<class _Tp>
4557inline _LIBCPP_INLINE_VISIBILITY
4558shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004559shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004560{
4561 shared_ptr(__r).swap(*this);
4562 return *this;
4563}
4564
4565template<class _Tp>
4566template<class _Yp>
4567inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004568typename enable_if
4569<
4570 is_convertible<_Yp*, _Tp*>::value,
4571 shared_ptr<_Tp>&
4572>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004573shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004574{
4575 shared_ptr(__r).swap(*this);
4576 return *this;
4577}
4578
Howard Hinnant73d21a42010-09-04 23:28:19 +00004579#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004580
4581template<class _Tp>
4582inline _LIBCPP_INLINE_VISIBILITY
4583shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004584shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004585{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004586 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004587 return *this;
4588}
4589
4590template<class _Tp>
4591template<class _Yp>
4592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004593typename enable_if
4594<
4595 is_convertible<_Yp*, _Tp*>::value,
4596 shared_ptr<_Tp>&
4597>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004598shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4599{
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>
4606inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004607typename enable_if
4608<
4609 !is_array<_Yp>::value &&
4610 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004611 shared_ptr<_Tp>
4612>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004613shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4614{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004615 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004616 return *this;
4617}
4618
4619template<class _Tp>
4620template <class _Yp, class _Dp>
4621inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004622typename enable_if
4623<
4624 !is_array<_Yp>::value &&
4625 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4626 shared_ptr<_Tp>&
4627>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004628shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4629{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004630 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004631 return *this;
4632}
4633
Howard Hinnant73d21a42010-09-04 23:28:19 +00004634#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004635
4636template<class _Tp>
4637template<class _Yp>
4638inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004639typename enable_if
4640<
4641 !is_array<_Yp>::value &&
4642 is_convertible<_Yp*, _Tp*>::value,
4643 shared_ptr<_Tp>&
4644>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004645shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004646{
4647 shared_ptr(__r).swap(*this);
4648 return *this;
4649}
4650
4651template<class _Tp>
4652template <class _Yp, class _Dp>
4653inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004654typename enable_if
4655<
4656 !is_array<_Yp>::value &&
4657 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4658 shared_ptr<_Tp>&
4659>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004660shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4661{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004662 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004663 return *this;
4664}
4665
Howard Hinnant73d21a42010-09-04 23:28:19 +00004666#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004667
4668template<class _Tp>
4669inline _LIBCPP_INLINE_VISIBILITY
4670void
Howard Hinnant1694d232011-05-28 14:41:13 +00004671shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004672{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004673 _VSTD::swap(__ptr_, __r.__ptr_);
4674 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004675}
4676
4677template<class _Tp>
4678inline _LIBCPP_INLINE_VISIBILITY
4679void
Howard Hinnant1694d232011-05-28 14:41:13 +00004680shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004681{
4682 shared_ptr().swap(*this);
4683}
4684
4685template<class _Tp>
4686template<class _Yp>
4687inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004688typename enable_if
4689<
4690 is_convertible<_Yp*, _Tp*>::value,
4691 void
4692>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004693shared_ptr<_Tp>::reset(_Yp* __p)
4694{
4695 shared_ptr(__p).swap(*this);
4696}
4697
4698template<class _Tp>
4699template<class _Yp, class _Dp>
4700inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004701typename enable_if
4702<
4703 is_convertible<_Yp*, _Tp*>::value,
4704 void
4705>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004706shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4707{
4708 shared_ptr(__p, __d).swap(*this);
4709}
4710
4711template<class _Tp>
4712template<class _Yp, class _Dp, class _Alloc>
4713inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004714typename enable_if
4715<
4716 is_convertible<_Yp*, _Tp*>::value,
4717 void
4718>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004719shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4720{
4721 shared_ptr(__p, __d, __a).swap(*this);
4722}
4723
4724#ifndef _LIBCPP_HAS_NO_VARIADICS
4725
Howard Hinnant324bb032010-08-22 00:02:43 +00004726template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004727inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004728typename enable_if
4729<
4730 !is_array<_Tp>::value,
4731 shared_ptr<_Tp>
4732>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004733make_shared(_Args&& ...__args)
4734{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004735 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004736}
4737
Howard Hinnant324bb032010-08-22 00:02:43 +00004738template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004739inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004740typename enable_if
4741<
4742 !is_array<_Tp>::value,
4743 shared_ptr<_Tp>
4744>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004745allocate_shared(const _Alloc& __a, _Args&& ...__args)
4746{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004747 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004748}
4749
4750#else // _LIBCPP_HAS_NO_VARIADICS
4751
4752template<class _Tp>
4753inline _LIBCPP_INLINE_VISIBILITY
4754shared_ptr<_Tp>
4755make_shared()
4756{
4757 return shared_ptr<_Tp>::make_shared();
4758}
4759
4760template<class _Tp, class _A0>
4761inline _LIBCPP_INLINE_VISIBILITY
4762shared_ptr<_Tp>
4763make_shared(_A0& __a0)
4764{
4765 return shared_ptr<_Tp>::make_shared(__a0);
4766}
4767
4768template<class _Tp, class _A0, class _A1>
4769inline _LIBCPP_INLINE_VISIBILITY
4770shared_ptr<_Tp>
4771make_shared(_A0& __a0, _A1& __a1)
4772{
4773 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4774}
4775
Howard Hinnant324bb032010-08-22 00:02:43 +00004776template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004777inline _LIBCPP_INLINE_VISIBILITY
4778shared_ptr<_Tp>
4779make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4780{
4781 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4782}
4783
4784template<class _Tp, class _Alloc>
4785inline _LIBCPP_INLINE_VISIBILITY
4786shared_ptr<_Tp>
4787allocate_shared(const _Alloc& __a)
4788{
4789 return shared_ptr<_Tp>::allocate_shared(__a);
4790}
4791
4792template<class _Tp, class _Alloc, class _A0>
4793inline _LIBCPP_INLINE_VISIBILITY
4794shared_ptr<_Tp>
4795allocate_shared(const _Alloc& __a, _A0& __a0)
4796{
4797 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4798}
4799
4800template<class _Tp, class _Alloc, class _A0, class _A1>
4801inline _LIBCPP_INLINE_VISIBILITY
4802shared_ptr<_Tp>
4803allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4804{
4805 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4806}
4807
4808template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4809inline _LIBCPP_INLINE_VISIBILITY
4810shared_ptr<_Tp>
4811allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4812{
4813 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4814}
4815
4816#endif // _LIBCPP_HAS_NO_VARIADICS
4817
4818template<class _Tp, class _Up>
4819inline _LIBCPP_INLINE_VISIBILITY
4820bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004821operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004822{
4823 return __x.get() == __y.get();
4824}
4825
4826template<class _Tp, class _Up>
4827inline _LIBCPP_INLINE_VISIBILITY
4828bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004829operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004830{
4831 return !(__x == __y);
4832}
4833
4834template<class _Tp, class _Up>
4835inline _LIBCPP_INLINE_VISIBILITY
4836bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004837operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004838{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004839 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4840 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004841}
4842
4843template<class _Tp, class _Up>
4844inline _LIBCPP_INLINE_VISIBILITY
4845bool
4846operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4847{
4848 return __y < __x;
4849}
4850
4851template<class _Tp, class _Up>
4852inline _LIBCPP_INLINE_VISIBILITY
4853bool
4854operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4855{
4856 return !(__y < __x);
4857}
4858
4859template<class _Tp, class _Up>
4860inline _LIBCPP_INLINE_VISIBILITY
4861bool
4862operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4863{
4864 return !(__x < __y);
4865}
4866
4867template<class _Tp>
4868inline _LIBCPP_INLINE_VISIBILITY
4869bool
4870operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4871{
4872 return !__x;
4873}
4874
4875template<class _Tp>
4876inline _LIBCPP_INLINE_VISIBILITY
4877bool
4878operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4879{
4880 return !__x;
4881}
4882
4883template<class _Tp>
4884inline _LIBCPP_INLINE_VISIBILITY
4885bool
4886operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4887{
4888 return static_cast<bool>(__x);
4889}
4890
4891template<class _Tp>
4892inline _LIBCPP_INLINE_VISIBILITY
4893bool
4894operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4895{
4896 return static_cast<bool>(__x);
4897}
4898
4899template<class _Tp>
4900inline _LIBCPP_INLINE_VISIBILITY
4901bool
4902operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4903{
4904 return less<_Tp*>()(__x.get(), nullptr);
4905}
4906
4907template<class _Tp>
4908inline _LIBCPP_INLINE_VISIBILITY
4909bool
4910operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4911{
4912 return less<_Tp*>()(nullptr, __x.get());
4913}
4914
4915template<class _Tp>
4916inline _LIBCPP_INLINE_VISIBILITY
4917bool
4918operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4919{
4920 return nullptr < __x;
4921}
4922
4923template<class _Tp>
4924inline _LIBCPP_INLINE_VISIBILITY
4925bool
4926operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4927{
4928 return __x < nullptr;
4929}
4930
4931template<class _Tp>
4932inline _LIBCPP_INLINE_VISIBILITY
4933bool
4934operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4935{
4936 return !(nullptr < __x);
4937}
4938
4939template<class _Tp>
4940inline _LIBCPP_INLINE_VISIBILITY
4941bool
4942operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4943{
4944 return !(__x < nullptr);
4945}
4946
4947template<class _Tp>
4948inline _LIBCPP_INLINE_VISIBILITY
4949bool
4950operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4951{
4952 return !(__x < nullptr);
4953}
4954
4955template<class _Tp>
4956inline _LIBCPP_INLINE_VISIBILITY
4957bool
4958operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4959{
4960 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004961}
4962
4963template<class _Tp>
4964inline _LIBCPP_INLINE_VISIBILITY
4965void
Howard Hinnant1694d232011-05-28 14:41:13 +00004966swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004967{
4968 __x.swap(__y);
4969}
4970
4971template<class _Tp, class _Up>
4972inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004973typename enable_if
4974<
4975 !is_array<_Tp>::value && !is_array<_Up>::value,
4976 shared_ptr<_Tp>
4977>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004978static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004979{
4980 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4981}
4982
4983template<class _Tp, class _Up>
4984inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004985typename enable_if
4986<
4987 !is_array<_Tp>::value && !is_array<_Up>::value,
4988 shared_ptr<_Tp>
4989>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004990dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004991{
4992 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4993 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4994}
4995
4996template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004997typename enable_if
4998<
4999 is_array<_Tp>::value == is_array<_Up>::value,
5000 shared_ptr<_Tp>
5001>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005002const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005003{
Howard Hinnant57199402012-01-02 17:56:02 +00005004 typedef typename remove_extent<_Tp>::type _RTp;
5005 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005006}
5007
Howard Hinnantd4444702010-08-11 17:04:31 +00005008#ifndef _LIBCPP_NO_RTTI
5009
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005010template<class _Dp, class _Tp>
5011inline _LIBCPP_INLINE_VISIBILITY
5012_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00005013get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005014{
5015 return __p.template __get_deleter<_Dp>();
5016}
5017
Howard Hinnant324bb032010-08-22 00:02:43 +00005018#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00005019
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005020template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005021class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005022{
Howard Hinnant324bb032010-08-22 00:02:43 +00005023public:
5024 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005025private:
5026 element_type* __ptr_;
5027 __shared_weak_count* __cntrl_;
5028
Howard Hinnant324bb032010-08-22 00:02:43 +00005029public:
Howard Hinnant46e94932012-07-07 20:56:04 +00005030 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005031 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005032 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5033 _NOEXCEPT;
5034 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005035 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005036 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5037 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005038
Howard Hinnant57199402012-01-02 17:56:02 +00005039#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5040 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
5041 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
5042 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5043 _NOEXCEPT;
5044#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005045 ~weak_ptr();
5046
Howard Hinnant1694d232011-05-28 14:41:13 +00005047 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005048 template<class _Yp>
5049 typename enable_if
5050 <
5051 is_convertible<_Yp*, element_type*>::value,
5052 weak_ptr&
5053 >::type
5054 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5055
5056#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5057
5058 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5059 template<class _Yp>
5060 typename enable_if
5061 <
5062 is_convertible<_Yp*, element_type*>::value,
5063 weak_ptr&
5064 >::type
5065 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5066
5067#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5068
5069 template<class _Yp>
5070 typename enable_if
5071 <
5072 is_convertible<_Yp*, element_type*>::value,
5073 weak_ptr&
5074 >::type
5075 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005076
Howard Hinnant1694d232011-05-28 14:41:13 +00005077 void swap(weak_ptr& __r) _NOEXCEPT;
5078 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005079
Howard Hinnant82894812010-09-22 16:48:34 +00005080 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005081 long use_count() const _NOEXCEPT
5082 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005084 bool expired() const _NOEXCEPT
5085 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5086 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005087 template<class _Up>
5088 _LIBCPP_INLINE_VISIBILITY
5089 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005090 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005091 template<class _Up>
5092 _LIBCPP_INLINE_VISIBILITY
5093 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005094 {return __cntrl_ < __r.__cntrl_;}
5095
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005096 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5097 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005098};
5099
5100template<class _Tp>
5101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005102_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005103weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005104 : __ptr_(0),
5105 __cntrl_(0)
5106{
5107}
5108
5109template<class _Tp>
5110inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005111weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005112 : __ptr_(__r.__ptr_),
5113 __cntrl_(__r.__cntrl_)
5114{
5115 if (__cntrl_)
5116 __cntrl_->__add_weak();
5117}
5118
5119template<class _Tp>
5120template<class _Yp>
5121inline _LIBCPP_INLINE_VISIBILITY
5122weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005123 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005124 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005125 : __ptr_(__r.__ptr_),
5126 __cntrl_(__r.__cntrl_)
5127{
5128 if (__cntrl_)
5129 __cntrl_->__add_weak();
5130}
5131
5132template<class _Tp>
5133template<class _Yp>
5134inline _LIBCPP_INLINE_VISIBILITY
5135weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005136 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005137 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005138 : __ptr_(__r.__ptr_),
5139 __cntrl_(__r.__cntrl_)
5140{
5141 if (__cntrl_)
5142 __cntrl_->__add_weak();
5143}
5144
Howard Hinnant57199402012-01-02 17:56:02 +00005145#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5146
5147template<class _Tp>
5148inline _LIBCPP_INLINE_VISIBILITY
5149weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5150 : __ptr_(__r.__ptr_),
5151 __cntrl_(__r.__cntrl_)
5152{
5153 __r.__ptr_ = 0;
5154 __r.__cntrl_ = 0;
5155}
5156
5157template<class _Tp>
5158template<class _Yp>
5159inline _LIBCPP_INLINE_VISIBILITY
5160weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5161 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5162 _NOEXCEPT
5163 : __ptr_(__r.__ptr_),
5164 __cntrl_(__r.__cntrl_)
5165{
5166 __r.__ptr_ = 0;
5167 __r.__cntrl_ = 0;
5168}
5169
5170#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5171
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005172template<class _Tp>
5173weak_ptr<_Tp>::~weak_ptr()
5174{
5175 if (__cntrl_)
5176 __cntrl_->__release_weak();
5177}
5178
5179template<class _Tp>
5180inline _LIBCPP_INLINE_VISIBILITY
5181weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005182weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005183{
5184 weak_ptr(__r).swap(*this);
5185 return *this;
5186}
5187
5188template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005189template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005190inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005191typename enable_if
5192<
5193 is_convertible<_Yp*, _Tp*>::value,
5194 weak_ptr<_Tp>&
5195>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005196weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005197{
5198 weak_ptr(__r).swap(*this);
5199 return *this;
5200}
5201
Howard Hinnant57199402012-01-02 17:56:02 +00005202#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5203
5204template<class _Tp>
5205inline _LIBCPP_INLINE_VISIBILITY
5206weak_ptr<_Tp>&
5207weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5208{
5209 weak_ptr(_VSTD::move(__r)).swap(*this);
5210 return *this;
5211}
5212
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005213template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005214template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005215inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005216typename enable_if
5217<
5218 is_convertible<_Yp*, _Tp*>::value,
5219 weak_ptr<_Tp>&
5220>::type
5221weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5222{
5223 weak_ptr(_VSTD::move(__r)).swap(*this);
5224 return *this;
5225}
5226
5227#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5228
5229template<class _Tp>
5230template<class _Yp>
5231inline _LIBCPP_INLINE_VISIBILITY
5232typename enable_if
5233<
5234 is_convertible<_Yp*, _Tp*>::value,
5235 weak_ptr<_Tp>&
5236>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005237weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005238{
5239 weak_ptr(__r).swap(*this);
5240 return *this;
5241}
5242
5243template<class _Tp>
5244inline _LIBCPP_INLINE_VISIBILITY
5245void
Howard Hinnant1694d232011-05-28 14:41:13 +00005246weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005247{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005248 _VSTD::swap(__ptr_, __r.__ptr_);
5249 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005250}
5251
5252template<class _Tp>
5253inline _LIBCPP_INLINE_VISIBILITY
5254void
Howard Hinnant1694d232011-05-28 14:41:13 +00005255swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005256{
5257 __x.swap(__y);
5258}
5259
5260template<class _Tp>
5261inline _LIBCPP_INLINE_VISIBILITY
5262void
Howard Hinnant1694d232011-05-28 14:41:13 +00005263weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005264{
5265 weak_ptr().swap(*this);
5266}
5267
5268template<class _Tp>
5269template<class _Yp>
5270shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5271 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5272 : __ptr_(__r.__ptr_),
5273 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5274{
5275 if (__cntrl_ == 0)
5276#ifndef _LIBCPP_NO_EXCEPTIONS
5277 throw bad_weak_ptr();
5278#else
5279 assert(!"bad_weak_ptr");
5280#endif
5281}
5282
5283template<class _Tp>
5284shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005285weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005286{
5287 shared_ptr<_Tp> __r;
5288 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5289 if (__r.__cntrl_)
5290 __r.__ptr_ = __ptr_;
5291 return __r;
5292}
5293
Howard Hinnant324bb032010-08-22 00:02:43 +00005294template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005295
5296template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005297struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005298 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005299{
5300 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005302 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5303 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005305 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5306 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005308 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5309 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005310};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005311
5312template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005313struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005314 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5315{
Howard Hinnant324bb032010-08-22 00:02:43 +00005316 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005318 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5319 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005321 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5322 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005324 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5325 {return __x.owner_before(__y);}
5326};
5327
5328template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005329class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005330{
5331 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005332protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005333 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005334 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005336 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005338 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5339 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005341 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005342public:
Howard Hinnant82894812010-09-22 16:48:34 +00005343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005344 shared_ptr<_Tp> shared_from_this()
5345 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005347 shared_ptr<_Tp const> shared_from_this() const
5348 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005349
5350 template <class _Up> friend class shared_ptr;
5351};
5352
Howard Hinnant21aefc32010-06-03 16:42:57 +00005353template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005354struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005355{
5356 typedef shared_ptr<_Tp> argument_type;
5357 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005359 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005360 {
5361 return hash<_Tp*>()(__ptr.get());
5362 }
5363};
5364
Howard Hinnant99968442011-11-29 18:15:50 +00005365template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005366inline _LIBCPP_INLINE_VISIBILITY
5367basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005368operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005369
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00005370#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005371
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005372class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005373{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005374 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005375public:
5376 void lock() _NOEXCEPT;
5377 void unlock() _NOEXCEPT;
5378
5379private:
5380 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5381 __sp_mut(const __sp_mut&);
5382 __sp_mut& operator=(const __sp_mut&);
5383
Howard Hinnant83eade62013-03-06 23:30:19 +00005384 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005385};
5386
Howard Hinnant83eade62013-03-06 23:30:19 +00005387_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005388
5389template <class _Tp>
5390inline _LIBCPP_INLINE_VISIBILITY
5391bool
5392atomic_is_lock_free(const shared_ptr<_Tp>*)
5393{
5394 return false;
5395}
5396
5397template <class _Tp>
5398shared_ptr<_Tp>
5399atomic_load(const shared_ptr<_Tp>* __p)
5400{
5401 __sp_mut& __m = __get_sp_mut(__p);
5402 __m.lock();
5403 shared_ptr<_Tp> __q = *__p;
5404 __m.unlock();
5405 return __q;
5406}
5407
5408template <class _Tp>
5409inline _LIBCPP_INLINE_VISIBILITY
5410shared_ptr<_Tp>
5411atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5412{
5413 return atomic_load(__p);
5414}
5415
5416template <class _Tp>
5417void
5418atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5419{
5420 __sp_mut& __m = __get_sp_mut(__p);
5421 __m.lock();
5422 __p->swap(__r);
5423 __m.unlock();
5424}
5425
5426template <class _Tp>
5427inline _LIBCPP_INLINE_VISIBILITY
5428void
5429atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5430{
5431 atomic_store(__p, __r);
5432}
5433
5434template <class _Tp>
5435shared_ptr<_Tp>
5436atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5437{
5438 __sp_mut& __m = __get_sp_mut(__p);
5439 __m.lock();
5440 __p->swap(__r);
5441 __m.unlock();
5442 return __r;
5443}
5444
5445template <class _Tp>
5446inline _LIBCPP_INLINE_VISIBILITY
5447shared_ptr<_Tp>
5448atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5449{
5450 return atomic_exchange(__p, __r);
5451}
5452
5453template <class _Tp>
5454bool
5455atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5456{
5457 __sp_mut& __m = __get_sp_mut(__p);
5458 __m.lock();
5459 if (__p->__owner_equivalent(*__v))
5460 {
5461 *__p = __w;
5462 __m.unlock();
5463 return true;
5464 }
5465 *__v = *__p;
5466 __m.unlock();
5467 return false;
5468}
5469
5470template <class _Tp>
5471inline _LIBCPP_INLINE_VISIBILITY
5472bool
5473atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5474{
5475 return atomic_compare_exchange_strong(__p, __v, __w);
5476}
5477
5478template <class _Tp>
5479inline _LIBCPP_INLINE_VISIBILITY
5480bool
5481atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5482 shared_ptr<_Tp> __w, memory_order, memory_order)
5483{
5484 return atomic_compare_exchange_strong(__p, __v, __w);
5485}
5486
5487template <class _Tp>
5488inline _LIBCPP_INLINE_VISIBILITY
5489bool
5490atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5491 shared_ptr<_Tp> __w, memory_order, memory_order)
5492{
5493 return atomic_compare_exchange_weak(__p, __v, __w);
5494}
5495
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00005496#endif // __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005497
Howard Hinnant324bb032010-08-22 00:02:43 +00005498//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005499struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005500{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005501 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005502 {
5503 relaxed,
5504 preferred,
5505 strict
5506 };
5507
Howard Hinnant9c0df142012-10-30 19:06:59 +00005508 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005509
Howard Hinnant82894812010-09-22 16:48:34 +00005510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005511 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005513 operator int() const {return __v_;}
5514};
5515
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005516_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5517_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5518_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5519_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5520_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005521
5522template <class _Tp>
5523inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005524_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005525undeclare_reachable(_Tp* __p)
5526{
5527 return static_cast<_Tp*>(__undeclare_reachable(__p));
5528}
5529
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005530_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005531
5532_LIBCPP_END_NAMESPACE_STD
5533
5534#endif // _LIBCPP_MEMORY