blob: dc5902da9ef5f370ee0c41e1323d02f01a713d5e [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;
1790
1791 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1792
1793 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1794 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1795 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1796 {return _VSTD::addressof(__x);}
1797 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Richard Smith73c1fce2014-06-04 19:54:15 +00001798 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant57199402012-01-02 17:56:02 +00001799 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001800 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant57199402012-01-02 17:56:02 +00001801 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1802 {return size_type(~0) / sizeof(_Tp);}
1803#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1804 template <class _Up, class... _Args>
1805 _LIBCPP_INLINE_VISIBILITY
1806 void
1807 construct(_Up* __p, _Args&&... __args)
1808 {
1809 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1810 }
1811#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1812 _LIBCPP_INLINE_VISIBILITY
1813 void
1814 construct(pointer __p)
1815 {
1816 ::new((void*)__p) _Tp();
1817 }
1818# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001819
Howard Hinnant57199402012-01-02 17:56:02 +00001820 template <class _A0>
1821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001822 void
Howard Hinnant57199402012-01-02 17:56:02 +00001823 construct(pointer __p, _A0& __a0)
1824 {
1825 ::new((void*)__p) _Tp(__a0);
1826 }
1827 template <class _A0>
1828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001829 void
Howard Hinnant57199402012-01-02 17:56:02 +00001830 construct(pointer __p, const _A0& __a0)
1831 {
1832 ::new((void*)__p) _Tp(__a0);
1833 }
Howard Hinnant57199402012-01-02 17:56:02 +00001834# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1835 template <class _A0, class _A1>
1836 _LIBCPP_INLINE_VISIBILITY
1837 void
1838 construct(pointer __p, _A0& __a0, _A1& __a1)
1839 {
1840 ::new((void*)__p) _Tp(__a0, __a1);
1841 }
1842 template <class _A0, class _A1>
1843 _LIBCPP_INLINE_VISIBILITY
1844 void
1845 construct(pointer __p, const _A0& __a0, _A1& __a1)
1846 {
1847 ::new((void*)__p) _Tp(__a0, __a1);
1848 }
1849 template <class _A0, class _A1>
1850 _LIBCPP_INLINE_VISIBILITY
1851 void
1852 construct(pointer __p, _A0& __a0, const _A1& __a1)
1853 {
1854 ::new((void*)__p) _Tp(__a0, __a1);
1855 }
1856 template <class _A0, class _A1>
1857 _LIBCPP_INLINE_VISIBILITY
1858 void
1859 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1860 {
1861 ::new((void*)__p) _Tp(__a0, __a1);
1862 }
1863#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1864 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1865};
1866
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867template <class _Tp, class _Up>
1868inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001869bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870
1871template <class _Tp, class _Up>
1872inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001873bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874
1875template <class _OutputIterator, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001876class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 : public iterator<output_iterator_tag,
1878 _Tp, // purposefully not C++03
1879 ptrdiff_t, // purposefully not C++03
1880 _Tp*, // purposefully not C++03
1881 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1882{
1883private:
1884 _OutputIterator __x_;
1885public:
1886 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1887 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1888 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1889 {::new(&*__x_) _Tp(__element); return *this;}
1890 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1891 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1892 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00001893#if _LIBCPP_STD_VER >= 14
1894 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1895#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896};
1897
1898template <class _Tp>
1899pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001900get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901{
1902 pair<_Tp*, ptrdiff_t> __r(0, 0);
1903 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1904 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1905 / sizeof(_Tp);
1906 if (__n > __m)
1907 __n = __m;
1908 while (__n > 0)
1909 {
1910 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1911 if (__r.first)
1912 {
1913 __r.second = __n;
1914 break;
1915 }
1916 __n /= 2;
1917 }
1918 return __r;
1919}
1920
1921template <class _Tp>
1922inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001923void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924
1925template <class _Tp>
1926struct auto_ptr_ref
1927{
1928 _Tp* __ptr_;
1929};
1930
1931template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001932class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933{
1934private:
1935 _Tp* __ptr_;
1936public:
1937 typedef _Tp element_type;
1938
1939 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1940 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1941 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1942 : __ptr_(__p.release()) {}
1943 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1944 {reset(__p.release()); return *this;}
1945 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1946 {reset(__p.release()); return *this;}
1947 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1948 {reset(__p.__ptr_); return *this;}
1949 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1950
1951 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1952 {return *__ptr_;}
1953 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1954 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1955 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1956 {
1957 _Tp* __t = __ptr_;
1958 __ptr_ = 0;
1959 return __t;
1960 }
1961 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1962 {
1963 if (__ptr_ != __p)
1964 delete __ptr_;
1965 __ptr_ = __p;
1966 }
1967
1968 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1969 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1970 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1971 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1972 {return auto_ptr<_Up>(release());}
1973};
1974
1975template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001976class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001977{
1978public:
1979 typedef void element_type;
1980};
1981
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1983 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001984 bool = is_empty<_T1>::value
1985#if __has_feature(is_final)
1986 && !__is_final(_T1)
1987#endif
1988 ,
1989 bool = is_empty<_T2>::value
1990#if __has_feature(is_final)
1991 && !__is_final(_T2)
1992#endif
1993 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994struct __libcpp_compressed_pair_switch;
1995
1996template <class _T1, class _T2, bool IsSame>
1997struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1998
1999template <class _T1, class _T2, bool IsSame>
2000struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2001
2002template <class _T1, class _T2, bool IsSame>
2003struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2004
2005template <class _T1, class _T2>
2006struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2007
2008template <class _T1, class _T2>
2009struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2010
2011template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2012class __libcpp_compressed_pair_imp;
2013
2014template <class _T1, class _T2>
2015class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2016{
2017private:
2018 _T1 __first_;
2019 _T2 __second_;
2020public:
2021 typedef _T1 _T1_param;
2022 typedef _T2 _T2_param;
2023
2024 typedef typename remove_reference<_T1>::type& _T1_reference;
2025 typedef typename remove_reference<_T2>::type& _T2_reference;
2026
2027 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2028 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2029
2030 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002031 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002032 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002033 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002034 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002036 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002038#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002039
2040 _LIBCPP_INLINE_VISIBILITY
2041 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2042 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2043 is_nothrow_copy_constructible<_T2>::value)
2044 : __first_(__p.first()),
2045 __second_(__p.second()) {}
2046
2047 _LIBCPP_INLINE_VISIBILITY
2048 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2049 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2050 is_nothrow_copy_assignable<_T2>::value)
2051 {
2052 __first_ = __p.first();
2053 __second_ = __p.second();
2054 return *this;
2055 }
2056
Howard Hinnant61aa6012011-07-01 19:24:36 +00002057 _LIBCPP_INLINE_VISIBILITY
2058 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002059 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2060 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002061 : __first_(_VSTD::forward<_T1>(__p.first())),
2062 __second_(_VSTD::forward<_T2>(__p.second())) {}
2063
2064 _LIBCPP_INLINE_VISIBILITY
2065 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2066 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2067 is_nothrow_move_assignable<_T2>::value)
2068 {
2069 __first_ = _VSTD::forward<_T1>(__p.first());
2070 __second_ = _VSTD::forward<_T2>(__p.second());
2071 return *this;
2072 }
2073
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002074#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002075
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002076#ifndef _LIBCPP_HAS_NO_VARIADICS
2077
2078 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2079 _LIBCPP_INLINE_VISIBILITY
2080 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2081 tuple<_Args1...> __first_args,
2082 tuple<_Args2...> __second_args,
2083 __tuple_indices<_I1...>,
2084 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002085 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2086 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002087 {}
2088
2089#endif // _LIBCPP_HAS_NO_VARIADICS
2090
Howard Hinnant1694d232011-05-28 14:41:13 +00002091 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2092 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093
Howard Hinnant1694d232011-05-28 14:41:13 +00002094 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2095 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096
2097 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002098 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002099 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002101 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 swap(__first_, __x.__first_);
2103 swap(__second_, __x.__second_);
2104 }
2105};
2106
2107template <class _T1, class _T2>
2108class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2109 : private _T1
2110{
2111private:
2112 _T2 __second_;
2113public:
2114 typedef _T1 _T1_param;
2115 typedef _T2 _T2_param;
2116
2117 typedef _T1& _T1_reference;
2118 typedef typename remove_reference<_T2>::type& _T2_reference;
2119
2120 typedef const _T1& _T1_const_reference;
2121 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2122
2123 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002124 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002125 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002126 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002127 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002129 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002131#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002132
2133 _LIBCPP_INLINE_VISIBILITY
2134 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2135 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2136 is_nothrow_copy_constructible<_T2>::value)
2137 : _T1(__p.first()), __second_(__p.second()) {}
2138
2139 _LIBCPP_INLINE_VISIBILITY
2140 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2141 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2142 is_nothrow_copy_assignable<_T2>::value)
2143 {
2144 _T1::operator=(__p.first());
2145 __second_ = __p.second();
2146 return *this;
2147 }
2148
Howard Hinnant61aa6012011-07-01 19:24:36 +00002149 _LIBCPP_INLINE_VISIBILITY
2150 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002151 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2152 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002153 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002154
2155 _LIBCPP_INLINE_VISIBILITY
2156 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2157 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2158 is_nothrow_move_assignable<_T2>::value)
2159 {
2160 _T1::operator=(_VSTD::move(__p.first()));
2161 __second_ = _VSTD::forward<_T2>(__p.second());
2162 return *this;
2163 }
2164
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002165#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002166
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002167#ifndef _LIBCPP_HAS_NO_VARIADICS
2168
2169 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2170 _LIBCPP_INLINE_VISIBILITY
2171 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2172 tuple<_Args1...> __first_args,
2173 tuple<_Args2...> __second_args,
2174 __tuple_indices<_I1...>,
2175 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002176 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2177 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002178 {}
2179
2180#endif // _LIBCPP_HAS_NO_VARIADICS
2181
Howard Hinnant1694d232011-05-28 14:41:13 +00002182 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2183 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184
Howard Hinnant1694d232011-05-28 14:41:13 +00002185 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2186 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187
2188 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002189 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002190 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002192 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 swap(__second_, __x.__second_);
2194 }
2195};
2196
2197template <class _T1, class _T2>
2198class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2199 : private _T2
2200{
2201private:
2202 _T1 __first_;
2203public:
2204 typedef _T1 _T1_param;
2205 typedef _T2 _T2_param;
2206
2207 typedef typename remove_reference<_T1>::type& _T1_reference;
2208 typedef _T2& _T2_reference;
2209
2210 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2211 typedef const _T2& _T2_const_reference;
2212
2213 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2214 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002215 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002217 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002219 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2220 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002221 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002223#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002224
2225 _LIBCPP_INLINE_VISIBILITY
2226 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2227 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2228 is_nothrow_copy_constructible<_T2>::value)
2229 : _T2(__p.second()), __first_(__p.first()) {}
2230
2231 _LIBCPP_INLINE_VISIBILITY
2232 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2233 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2234 is_nothrow_copy_assignable<_T2>::value)
2235 {
2236 _T2::operator=(__p.second());
2237 __first_ = __p.first();
2238 return *this;
2239 }
2240
Howard Hinnant61aa6012011-07-01 19:24:36 +00002241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002242 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002243 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2244 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002245 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002246
2247 _LIBCPP_INLINE_VISIBILITY
2248 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2249 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2250 is_nothrow_move_assignable<_T2>::value)
2251 {
2252 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2253 __first_ = _VSTD::move(__p.first());
2254 return *this;
2255 }
2256
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002257#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002258
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002259#ifndef _LIBCPP_HAS_NO_VARIADICS
2260
2261 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2262 _LIBCPP_INLINE_VISIBILITY
2263 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2264 tuple<_Args1...> __first_args,
2265 tuple<_Args2...> __second_args,
2266 __tuple_indices<_I1...>,
2267 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002268 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2269 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002270
2271 {}
2272
2273#endif // _LIBCPP_HAS_NO_VARIADICS
2274
Howard Hinnant1694d232011-05-28 14:41:13 +00002275 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2276 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277
Howard Hinnant1694d232011-05-28 14:41:13 +00002278 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2279 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280
2281 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002282 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002283 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002285 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286 swap(__first_, __x.__first_);
2287 }
2288};
2289
2290template <class _T1, class _T2>
2291class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2292 : private _T1,
2293 private _T2
2294{
2295public:
2296 typedef _T1 _T1_param;
2297 typedef _T2 _T2_param;
2298
2299 typedef _T1& _T1_reference;
2300 typedef _T2& _T2_reference;
2301
2302 typedef const _T1& _T1_const_reference;
2303 typedef const _T2& _T2_const_reference;
2304
2305 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2306 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002307 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002309 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002311 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002313#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002314
2315 _LIBCPP_INLINE_VISIBILITY
2316 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2317 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2318 is_nothrow_copy_constructible<_T2>::value)
2319 : _T1(__p.first()), _T2(__p.second()) {}
2320
2321 _LIBCPP_INLINE_VISIBILITY
2322 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2323 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2324 is_nothrow_copy_assignable<_T2>::value)
2325 {
2326 _T1::operator=(__p.first());
2327 _T2::operator=(__p.second());
2328 return *this;
2329 }
2330
Howard Hinnant61aa6012011-07-01 19:24:36 +00002331 _LIBCPP_INLINE_VISIBILITY
2332 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002333 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2334 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002335 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002336
2337 _LIBCPP_INLINE_VISIBILITY
2338 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2339 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2340 is_nothrow_move_assignable<_T2>::value)
2341 {
2342 _T1::operator=(_VSTD::move(__p.first()));
2343 _T2::operator=(_VSTD::move(__p.second()));
2344 return *this;
2345 }
2346
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002347#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002348
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002349#ifndef _LIBCPP_HAS_NO_VARIADICS
2350
2351 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2352 _LIBCPP_INLINE_VISIBILITY
2353 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2354 tuple<_Args1...> __first_args,
2355 tuple<_Args2...> __second_args,
2356 __tuple_indices<_I1...>,
2357 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002358 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2359 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002360 {}
2361
2362#endif // _LIBCPP_HAS_NO_VARIADICS
2363
Howard Hinnant1694d232011-05-28 14:41:13 +00002364 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2365 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366
Howard Hinnant1694d232011-05-28 14:41:13 +00002367 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2368 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369
Howard Hinnantec3773c2011-12-01 20:21:04 +00002370 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002371 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002372 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373 {
2374 }
2375};
2376
2377template <class _T1, class _T2>
2378class __compressed_pair
2379 : private __libcpp_compressed_pair_imp<_T1, _T2>
2380{
2381 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2382public:
2383 typedef typename base::_T1_param _T1_param;
2384 typedef typename base::_T2_param _T2_param;
2385
2386 typedef typename base::_T1_reference _T1_reference;
2387 typedef typename base::_T2_reference _T2_reference;
2388
2389 typedef typename base::_T1_const_reference _T1_const_reference;
2390 typedef typename base::_T2_const_reference _T2_const_reference;
2391
2392 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002393 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002394 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002395 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002396 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002398 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002399
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002400#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002401
2402 _LIBCPP_INLINE_VISIBILITY
2403 __compressed_pair(const __compressed_pair& __p)
2404 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2405 is_nothrow_copy_constructible<_T2>::value)
2406 : base(__p) {}
2407
2408 _LIBCPP_INLINE_VISIBILITY
2409 __compressed_pair& operator=(const __compressed_pair& __p)
2410 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2411 is_nothrow_copy_assignable<_T2>::value)
2412 {
2413 base::operator=(__p);
2414 return *this;
2415 }
2416
Howard Hinnant61aa6012011-07-01 19:24:36 +00002417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002418 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002419 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2420 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002421 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002422
2423 _LIBCPP_INLINE_VISIBILITY
2424 __compressed_pair& operator=(__compressed_pair&& __p)
2425 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2426 is_nothrow_move_assignable<_T2>::value)
2427 {
2428 base::operator=(_VSTD::move(__p));
2429 return *this;
2430 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002431
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002432#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002433
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002434#ifndef _LIBCPP_HAS_NO_VARIADICS
2435
2436 template <class... _Args1, class... _Args2>
2437 _LIBCPP_INLINE_VISIBILITY
2438 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2439 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002440 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002441 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2442 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2443 {}
2444
2445#endif // _LIBCPP_HAS_NO_VARIADICS
2446
Howard Hinnant1694d232011-05-28 14:41:13 +00002447 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2448 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449
Howard Hinnant1694d232011-05-28 14:41:13 +00002450 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2451 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452
Howard Hinnant1694d232011-05-28 14:41:13 +00002453 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2454 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002455 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002456 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457};
2458
2459template <class _T1, class _T2>
2460inline _LIBCPP_INLINE_VISIBILITY
2461void
2462swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002463 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002464 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 {__x.swap(__y);}
2466
Howard Hinnant57199402012-01-02 17:56:02 +00002467// __same_or_less_cv_qualified
2468
2469template <class _Ptr1, class _Ptr2,
2470 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2471 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2472 >::value
2473 >
2474struct __same_or_less_cv_qualified_imp
2475 : is_convertible<_Ptr1, _Ptr2> {};
2476
2477template <class _Ptr1, class _Ptr2>
2478struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2479 : false_type {};
2480
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002481template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2482 is_same<_Ptr1, _Ptr2>::value ||
2483 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002484struct __same_or_less_cv_qualified
2485 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2486
2487template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002488struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002489 : false_type {};
2490
2491// default_delete
2492
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002493template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002494struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495{
Howard Hinnant46e94932012-07-07 20:56:04 +00002496#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2497 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2498#else
2499 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2500#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501 template <class _Up>
2502 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002503 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2504 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002505 {
2506 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002507 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002508 delete __ptr;
2509 }
2510};
2511
2512template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002513struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514{
Howard Hinnant8e843502011-12-18 21:19:44 +00002515public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002516#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2517 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2518#else
2519 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2520#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002521 template <class _Up>
2522 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002523 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002524 template <class _Up>
2525 _LIBCPP_INLINE_VISIBILITY
2526 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002527 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528 {
2529 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002530 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 delete [] __ptr;
2532 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533};
2534
2535template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002536class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537{
2538public:
2539 typedef _Tp element_type;
2540 typedef _Dp deleter_type;
2541 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2542private:
2543 __compressed_pair<pointer, deleter_type> __ptr_;
2544
Howard Hinnant57199402012-01-02 17:56:02 +00002545#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546 unique_ptr(unique_ptr&);
2547 template <class _Up, class _Ep>
2548 unique_ptr(unique_ptr<_Up, _Ep>&);
2549 unique_ptr& operator=(unique_ptr&);
2550 template <class _Up, class _Ep>
2551 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002552#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553
2554 struct __nat {int __for_bool_;};
2555
2556 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2557 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2558public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002559 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560 : __ptr_(pointer())
2561 {
2562 static_assert(!is_pointer<deleter_type>::value,
2563 "unique_ptr constructed with null function pointer deleter");
2564 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002565 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 : __ptr_(pointer())
2567 {
2568 static_assert(!is_pointer<deleter_type>::value,
2569 "unique_ptr constructed with null function pointer deleter");
2570 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002571 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002572 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002573 {
2574 static_assert(!is_pointer<deleter_type>::value,
2575 "unique_ptr constructed with null function pointer deleter");
2576 }
2577
Howard Hinnant73d21a42010-09-04 23:28:19 +00002578#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2580 is_reference<deleter_type>::value,
2581 deleter_type,
2582 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002583 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584 : __ptr_(__p, __d) {}
2585
2586 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002587 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002588 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589 {
2590 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2591 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002592 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002593 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594 template <class _Up, class _Ep>
2595 _LIBCPP_INLINE_VISIBILITY
2596 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2597 typename enable_if
2598 <
2599 !is_array<_Up>::value &&
2600 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2601 is_convertible<_Ep, deleter_type>::value &&
2602 (
2603 !is_reference<deleter_type>::value ||
2604 is_same<deleter_type, _Ep>::value
2605 ),
2606 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002607 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002608 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609
2610 template <class _Up>
2611 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2612 typename enable_if<
2613 is_convertible<_Up*, _Tp*>::value &&
2614 is_same<_Dp, default_delete<_Tp> >::value,
2615 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002616 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617 : __ptr_(__p.release())
2618 {
2619 }
2620
Howard Hinnant1694d232011-05-28 14:41:13 +00002621 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 {
2623 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002624 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625 return *this;
2626 }
2627
2628 template <class _Up, class _Ep>
2629 _LIBCPP_INLINE_VISIBILITY
2630 typename enable_if
2631 <
Howard Hinnant57199402012-01-02 17:56:02 +00002632 !is_array<_Up>::value &&
2633 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2634 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002635 unique_ptr&
2636 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002637 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002638 {
2639 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002640 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641 return *this;
2642 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002643#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644
2645 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2646 {
2647 return __rv<unique_ptr>(*this);
2648 }
2649
2650 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002651 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652
2653 template <class _Up, class _Ep>
2654 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2655 {
2656 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002657 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658 return *this;
2659 }
2660
2661 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002662 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663
2664 template <class _Up>
2665 _LIBCPP_INLINE_VISIBILITY
2666 typename enable_if<
2667 is_convertible<_Up*, _Tp*>::value &&
2668 is_same<_Dp, default_delete<_Tp> >::value,
2669 unique_ptr&
2670 >::type
2671 operator=(auto_ptr<_Up> __p)
2672 {reset(__p.release()); return *this;}
2673
Howard Hinnant73d21a42010-09-04 23:28:19 +00002674#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2676
Howard Hinnant1694d232011-05-28 14:41:13 +00002677 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002678 {
2679 reset();
2680 return *this;
2681 }
2682
2683 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2684 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002685 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2686 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2687 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2688 {return __ptr_.second();}
2689 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2690 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002691 _LIBCPP_INLINE_VISIBILITY
2692 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2693 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002694
Howard Hinnant1694d232011-05-28 14:41:13 +00002695 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002696 {
2697 pointer __t = __ptr_.first();
2698 __ptr_.first() = pointer();
2699 return __t;
2700 }
2701
Howard Hinnant1694d232011-05-28 14:41:13 +00002702 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 {
2704 pointer __tmp = __ptr_.first();
2705 __ptr_.first() = __p;
2706 if (__tmp)
2707 __ptr_.second()(__tmp);
2708 }
2709
Howard Hinnant1694d232011-05-28 14:41:13 +00002710 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2711 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712};
2713
2714template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002715class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716{
2717public:
2718 typedef _Tp element_type;
2719 typedef _Dp deleter_type;
2720 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2721private:
2722 __compressed_pair<pointer, deleter_type> __ptr_;
2723
Howard Hinnant57199402012-01-02 17:56:02 +00002724#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725 unique_ptr(unique_ptr&);
2726 template <class _Up>
2727 unique_ptr(unique_ptr<_Up>&);
2728 unique_ptr& operator=(unique_ptr&);
2729 template <class _Up>
2730 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002731#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002732
2733 struct __nat {int __for_bool_;};
2734
2735 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2736 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2737public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002738 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739 : __ptr_(pointer())
2740 {
2741 static_assert(!is_pointer<deleter_type>::value,
2742 "unique_ptr constructed with null function pointer deleter");
2743 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002744 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002745 : __ptr_(pointer())
2746 {
2747 static_assert(!is_pointer<deleter_type>::value,
2748 "unique_ptr constructed with null function pointer deleter");
2749 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002750#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002751 template <class _Pp>
2752 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2753 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 : __ptr_(__p)
2755 {
2756 static_assert(!is_pointer<deleter_type>::value,
2757 "unique_ptr constructed with null function pointer deleter");
2758 }
2759
Logan Chiene1678a12014-01-31 09:30:46 +00002760 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002761 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 is_reference<deleter_type>::value,
2763 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002764 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2765 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002766 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767 : __ptr_(__p, __d) {}
2768
2769 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2770 is_reference<deleter_type>::value,
2771 deleter_type,
2772 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002773 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002774 : __ptr_(pointer(), __d) {}
2775
Logan Chiene1678a12014-01-31 09:30:46 +00002776 template <class _Pp>
2777 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2778 typename remove_reference<deleter_type>::type&& __d,
2779 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002780 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002781 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782 {
2783 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2784 }
2785
2786 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002787 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002788 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 {
2790 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2791 }
2792
Howard Hinnant1694d232011-05-28 14:41:13 +00002793 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002794 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795
Howard Hinnant1694d232011-05-28 14:41:13 +00002796 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797 {
2798 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002799 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800 return *this;
2801 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002802
2803 template <class _Up, class _Ep>
2804 _LIBCPP_INLINE_VISIBILITY
2805 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2806 typename enable_if
2807 <
2808 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002809 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002810 && is_convertible<_Ep, deleter_type>::value &&
2811 (
2812 !is_reference<deleter_type>::value ||
2813 is_same<deleter_type, _Ep>::value
2814 ),
2815 __nat
2816 >::type = __nat()
2817 ) _NOEXCEPT
2818 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2819
2820
2821 template <class _Up, class _Ep>
2822 _LIBCPP_INLINE_VISIBILITY
2823 typename enable_if
2824 <
2825 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002826 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2827 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002828 unique_ptr&
2829 >::type
2830 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2831 {
2832 reset(__u.release());
2833 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2834 return *this;
2835 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002836#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837
2838 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2839 : __ptr_(__p)
2840 {
2841 static_assert(!is_pointer<deleter_type>::value,
2842 "unique_ptr constructed with null function pointer deleter");
2843 }
2844
2845 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002846 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002847
2848 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002849 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850
2851 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2852 {
2853 return __rv<unique_ptr>(*this);
2854 }
2855
2856 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002857 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858
2859 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2860 {
2861 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002862 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002863 return *this;
2864 }
2865
Howard Hinnant73d21a42010-09-04 23:28:19 +00002866#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2868
Howard Hinnant1694d232011-05-28 14:41:13 +00002869 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002870 {
2871 reset();
2872 return *this;
2873 }
2874
2875 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2876 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002877 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2878 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2879 {return __ptr_.second();}
2880 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2881 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002882 _LIBCPP_INLINE_VISIBILITY
2883 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2884 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002885
Howard Hinnant1694d232011-05-28 14:41:13 +00002886 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002887 {
2888 pointer __t = __ptr_.first();
2889 __ptr_.first() = pointer();
2890 return __t;
2891 }
2892
Howard Hinnant73d21a42010-09-04 23:28:19 +00002893#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002894 template <class _Pp>
2895 _LIBCPP_INLINE_VISIBILITY
2896 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2897 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002898 {
2899 pointer __tmp = __ptr_.first();
2900 __ptr_.first() = __p;
2901 if (__tmp)
2902 __ptr_.second()(__tmp);
2903 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002904 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002905 {
2906 pointer __tmp = __ptr_.first();
2907 __ptr_.first() = nullptr;
2908 if (__tmp)
2909 __ptr_.second()(__tmp);
2910 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002911 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002912 {
2913 pointer __tmp = __ptr_.first();
2914 __ptr_.first() = nullptr;
2915 if (__tmp)
2916 __ptr_.second()(__tmp);
2917 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002918#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2920 {
2921 pointer __tmp = __ptr_.first();
2922 __ptr_.first() = __p;
2923 if (__tmp)
2924 __ptr_.second()(__tmp);
2925 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002926#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002927
2928 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2929private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002930
Howard Hinnant73d21a42010-09-04 23:28:19 +00002931#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002932 template <class _Up>
2933 explicit unique_ptr(_Up);
2934 template <class _Up>
2935 unique_ptr(_Up __u,
2936 typename conditional<
2937 is_reference<deleter_type>::value,
2938 deleter_type,
2939 typename add_lvalue_reference<const deleter_type>::type>::type,
2940 typename enable_if
2941 <
2942 is_convertible<_Up, pointer>::value,
2943 __nat
2944 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002945#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002946};
2947
2948template <class _Tp, class _Dp>
2949inline _LIBCPP_INLINE_VISIBILITY
2950void
Howard Hinnant1694d232011-05-28 14:41:13 +00002951swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002952
2953template <class _T1, class _D1, class _T2, class _D2>
2954inline _LIBCPP_INLINE_VISIBILITY
2955bool
2956operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2957
2958template <class _T1, class _D1, class _T2, class _D2>
2959inline _LIBCPP_INLINE_VISIBILITY
2960bool
2961operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2962
2963template <class _T1, class _D1, class _T2, class _D2>
2964inline _LIBCPP_INLINE_VISIBILITY
2965bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002966operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2967{
2968 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2969 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002970 typedef typename common_type<_P1, _P2>::type _Vp;
2971 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002972}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002973
2974template <class _T1, class _D1, class _T2, class _D2>
2975inline _LIBCPP_INLINE_VISIBILITY
2976bool
2977operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2978
2979template <class _T1, class _D1, class _T2, class _D2>
2980inline _LIBCPP_INLINE_VISIBILITY
2981bool
2982operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2983
2984template <class _T1, class _D1, class _T2, class _D2>
2985inline _LIBCPP_INLINE_VISIBILITY
2986bool
2987operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2988
Howard Hinnant3fadda32012-02-21 21:02:58 +00002989template <class _T1, class _D1>
2990inline _LIBCPP_INLINE_VISIBILITY
2991bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002992operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002993{
2994 return !__x;
2995}
2996
2997template <class _T1, class _D1>
2998inline _LIBCPP_INLINE_VISIBILITY
2999bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003000operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003001{
3002 return !__x;
3003}
3004
3005template <class _T1, class _D1>
3006inline _LIBCPP_INLINE_VISIBILITY
3007bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003008operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003009{
3010 return static_cast<bool>(__x);
3011}
3012
3013template <class _T1, class _D1>
3014inline _LIBCPP_INLINE_VISIBILITY
3015bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003016operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003017{
3018 return static_cast<bool>(__x);
3019}
3020
3021template <class _T1, class _D1>
3022inline _LIBCPP_INLINE_VISIBILITY
3023bool
3024operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3025{
3026 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3027 return less<_P1>()(__x.get(), nullptr);
3028}
3029
3030template <class _T1, class _D1>
3031inline _LIBCPP_INLINE_VISIBILITY
3032bool
3033operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3034{
3035 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3036 return less<_P1>()(nullptr, __x.get());
3037}
3038
3039template <class _T1, class _D1>
3040inline _LIBCPP_INLINE_VISIBILITY
3041bool
3042operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3043{
3044 return nullptr < __x;
3045}
3046
3047template <class _T1, class _D1>
3048inline _LIBCPP_INLINE_VISIBILITY
3049bool
3050operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3051{
3052 return __x < nullptr;
3053}
3054
3055template <class _T1, class _D1>
3056inline _LIBCPP_INLINE_VISIBILITY
3057bool
3058operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3059{
3060 return !(nullptr < __x);
3061}
3062
3063template <class _T1, class _D1>
3064inline _LIBCPP_INLINE_VISIBILITY
3065bool
3066operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3067{
3068 return !(__x < nullptr);
3069}
3070
3071template <class _T1, class _D1>
3072inline _LIBCPP_INLINE_VISIBILITY
3073bool
3074operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3075{
3076 return !(__x < nullptr);
3077}
3078
3079template <class _T1, class _D1>
3080inline _LIBCPP_INLINE_VISIBILITY
3081bool
3082operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3083{
3084 return !(nullptr < __x);
3085}
3086
Howard Hinnant87073e42012-05-01 15:37:54 +00003087#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3088
3089template <class _Tp, class _Dp>
3090inline _LIBCPP_INLINE_VISIBILITY
3091unique_ptr<_Tp, _Dp>
3092move(unique_ptr<_Tp, _Dp>& __t)
3093{
3094 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3095}
3096
3097#endif
3098
Marshall Clowfd7481e2013-07-01 18:16:03 +00003099#if _LIBCPP_STD_VER > 11
3100
3101template<class _Tp>
3102struct __unique_if
3103{
3104 typedef unique_ptr<_Tp> __unique_single;
3105};
3106
3107template<class _Tp>
3108struct __unique_if<_Tp[]>
3109{
3110 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3111};
3112
3113template<class _Tp, size_t _Np>
3114struct __unique_if<_Tp[_Np]>
3115{
3116 typedef void __unique_array_known_bound;
3117};
3118
3119template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003120inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003121typename __unique_if<_Tp>::__unique_single
3122make_unique(_Args&&... __args)
3123{
3124 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3125}
3126
3127template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003128inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003129typename __unique_if<_Tp>::__unique_array_unknown_bound
3130make_unique(size_t __n)
3131{
3132 typedef typename remove_extent<_Tp>::type _Up;
3133 return unique_ptr<_Tp>(new _Up[__n]());
3134}
3135
3136template<class _Tp, class... _Args>
3137 typename __unique_if<_Tp>::__unique_array_known_bound
3138 make_unique(_Args&&...) = delete;
3139
3140#endif // _LIBCPP_STD_VER > 11
3141
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003142template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003143
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003144template <class _Size>
3145inline _LIBCPP_INLINE_VISIBILITY
3146_Size
3147__loadword(const void* __p)
3148{
3149 _Size __r;
3150 std::memcpy(&__r, __p, sizeof(__r));
3151 return __r;
3152}
3153
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003154// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3155// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3156// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003157template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003158struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003159
3160template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003161struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003162{
3163 _Size operator()(const void* __key, _Size __len);
3164};
3165
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003166// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003167template <class _Size>
3168_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003169__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003170{
3171 const _Size __m = 0x5bd1e995;
3172 const _Size __r = 24;
3173 _Size __h = __len;
3174 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3175 for (; __len >= 4; __data += 4, __len -= 4)
3176 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003177 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003178 __k *= __m;
3179 __k ^= __k >> __r;
3180 __k *= __m;
3181 __h *= __m;
3182 __h ^= __k;
3183 }
3184 switch (__len)
3185 {
3186 case 3:
3187 __h ^= __data[2] << 16;
3188 case 2:
3189 __h ^= __data[1] << 8;
3190 case 1:
3191 __h ^= __data[0];
3192 __h *= __m;
3193 }
3194 __h ^= __h >> 13;
3195 __h *= __m;
3196 __h ^= __h >> 15;
3197 return __h;
3198}
3199
3200template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003201struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003202{
3203 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003204
3205 private:
3206 // Some primes between 2^63 and 2^64.
3207 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3208 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3209 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3210 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3211
3212 static _Size __rotate(_Size __val, int __shift) {
3213 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3214 }
3215
3216 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3217 return (__val >> __shift) | (__val << (64 - __shift));
3218 }
3219
3220 static _Size __shift_mix(_Size __val) {
3221 return __val ^ (__val >> 47);
3222 }
3223
3224 static _Size __hash_len_16(_Size __u, _Size __v) {
3225 const _Size __mul = 0x9ddfea08eb382d69ULL;
3226 _Size __a = (__u ^ __v) * __mul;
3227 __a ^= (__a >> 47);
3228 _Size __b = (__v ^ __a) * __mul;
3229 __b ^= (__b >> 47);
3230 __b *= __mul;
3231 return __b;
3232 }
3233
3234 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3235 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003236 const _Size __a = __loadword<_Size>(__s);
3237 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003238 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3239 }
3240 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003241 const uint32_t __a = __loadword<uint32_t>(__s);
3242 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003243 return __hash_len_16(__len + (__a << 3), __b);
3244 }
3245 if (__len > 0) {
3246 const unsigned char __a = __s[0];
3247 const unsigned char __b = __s[__len >> 1];
3248 const unsigned char __c = __s[__len - 1];
3249 const uint32_t __y = static_cast<uint32_t>(__a) +
3250 (static_cast<uint32_t>(__b) << 8);
3251 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3252 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3253 }
3254 return __k2;
3255 }
3256
3257 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003258 const _Size __a = __loadword<_Size>(__s) * __k1;
3259 const _Size __b = __loadword<_Size>(__s + 8);
3260 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3261 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003262 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3263 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3264 }
3265
3266 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3267 // Callers do best to use "random-looking" values for a and b.
3268 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3269 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3270 __a += __w;
3271 __b = __rotate(__b + __a + __z, 21);
3272 const _Size __c = __a;
3273 __a += __x;
3274 __a += __y;
3275 __b += __rotate(__a, 44);
3276 return pair<_Size, _Size>(__a + __z, __b + __c);
3277 }
3278
3279 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3280 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3281 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003282 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3283 __loadword<_Size>(__s + 8),
3284 __loadword<_Size>(__s + 16),
3285 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003286 __a,
3287 __b);
3288 }
3289
3290 // Return an 8-byte hash for 33 to 64 bytes.
3291 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003292 _Size __z = __loadword<_Size>(__s + 24);
3293 _Size __a = __loadword<_Size>(__s) +
3294 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003295 _Size __b = __rotate(__a + __z, 52);
3296 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003297 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003298 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003299 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003300 _Size __vf = __a + __z;
3301 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003302 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3303 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003304 __b = __rotate(__a + __z, 52);
3305 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003306 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003307 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003308 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003309 _Size __wf = __a + __z;
3310 _Size __ws = __b + __rotate(__a, 31) + __c;
3311 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3312 return __shift_mix(__r * __k0 + __vs) * __k2;
3313 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003314};
3315
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003316// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003317template <class _Size>
3318_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003319__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003320{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003321 const char* __s = static_cast<const char*>(__key);
3322 if (__len <= 32) {
3323 if (__len <= 16) {
3324 return __hash_len_0_to_16(__s, __len);
3325 } else {
3326 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003327 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003328 } else if (__len <= 64) {
3329 return __hash_len_33_to_64(__s, __len);
3330 }
3331
3332 // For strings over 64 bytes we hash the end first, and then as we
3333 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003334 _Size __x = __loadword<_Size>(__s + __len - 40);
3335 _Size __y = __loadword<_Size>(__s + __len - 16) +
3336 __loadword<_Size>(__s + __len - 56);
3337 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3338 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003339 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3340 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003341 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003342
3343 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3344 __len = (__len - 1) & ~static_cast<_Size>(63);
3345 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003346 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3347 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003348 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003349 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003350 __z = __rotate(__z + __w.first, 33) * __k1;
3351 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3352 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003353 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003354 std::swap(__z, __x);
3355 __s += 64;
3356 __len -= 64;
3357 } while (__len != 0);
3358 return __hash_len_16(
3359 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3360 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003361}
3362
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003363template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3364struct __scalar_hash;
3365
3366template <class _Tp>
3367struct __scalar_hash<_Tp, 0>
3368 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003369{
Howard Hinnant82894812010-09-22 16:48:34 +00003370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003371 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003372 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003373 union
3374 {
3375 _Tp __t;
3376 size_t __a;
3377 } __u;
3378 __u.__a = 0;
3379 __u.__t = __v;
3380 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003381 }
3382};
3383
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003384template <class _Tp>
3385struct __scalar_hash<_Tp, 1>
3386 : public unary_function<_Tp, size_t>
3387{
3388 _LIBCPP_INLINE_VISIBILITY
3389 size_t operator()(_Tp __v) const _NOEXCEPT
3390 {
3391 union
3392 {
3393 _Tp __t;
3394 size_t __a;
3395 } __u;
3396 __u.__t = __v;
3397 return __u.__a;
3398 }
3399};
3400
3401template <class _Tp>
3402struct __scalar_hash<_Tp, 2>
3403 : public unary_function<_Tp, size_t>
3404{
3405 _LIBCPP_INLINE_VISIBILITY
3406 size_t operator()(_Tp __v) const _NOEXCEPT
3407 {
3408 union
3409 {
3410 _Tp __t;
3411 struct
3412 {
3413 size_t __a;
3414 size_t __b;
3415 };
3416 } __u;
3417 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003418 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003419 }
3420};
3421
3422template <class _Tp>
3423struct __scalar_hash<_Tp, 3>
3424 : public unary_function<_Tp, size_t>
3425{
3426 _LIBCPP_INLINE_VISIBILITY
3427 size_t operator()(_Tp __v) const _NOEXCEPT
3428 {
3429 union
3430 {
3431 _Tp __t;
3432 struct
3433 {
3434 size_t __a;
3435 size_t __b;
3436 size_t __c;
3437 };
3438 } __u;
3439 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003440 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003441 }
3442};
3443
3444template <class _Tp>
3445struct __scalar_hash<_Tp, 4>
3446 : public unary_function<_Tp, size_t>
3447{
3448 _LIBCPP_INLINE_VISIBILITY
3449 size_t operator()(_Tp __v) const _NOEXCEPT
3450 {
3451 union
3452 {
3453 _Tp __t;
3454 struct
3455 {
3456 size_t __a;
3457 size_t __b;
3458 size_t __c;
3459 size_t __d;
3460 };
3461 } __u;
3462 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003463 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003464 }
3465};
3466
3467template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003468struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003469 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003470{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003471 _LIBCPP_INLINE_VISIBILITY
3472 size_t operator()(_Tp* __v) const _NOEXCEPT
3473 {
3474 union
3475 {
3476 _Tp* __t;
3477 size_t __a;
3478 } __u;
3479 __u.__t = __v;
3480 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3481 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003482};
3483
Howard Hinnant21aefc32010-06-03 16:42:57 +00003484template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003485struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003486{
3487 typedef unique_ptr<_Tp, _Dp> argument_type;
3488 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003490 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003491 {
3492 typedef typename argument_type::pointer pointer;
3493 return hash<pointer>()(__ptr.get());
3494 }
3495};
3496
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003497struct __destruct_n
3498{
3499private:
3500 size_t size;
3501
3502 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003503 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003504 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3505
3506 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003507 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003508 {}
3509
Howard Hinnant1694d232011-05-28 14:41:13 +00003510 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003511 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003512 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513 {}
3514
Howard Hinnant1694d232011-05-28 14:41:13 +00003515 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003516 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003517 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003518 {}
3519public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003520 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3521 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003522
3523 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003524 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003525 {__incr(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 __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003529 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530
3531 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003532 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003533 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003534};
3535
3536template <class _Alloc>
3537class __allocator_destructor
3538{
3539 typedef allocator_traits<_Alloc> __alloc_traits;
3540public:
3541 typedef typename __alloc_traits::pointer pointer;
3542 typedef typename __alloc_traits::size_type size_type;
3543private:
3544 _Alloc& __alloc_;
3545 size_type __s_;
3546public:
3547 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003548 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003549 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003551 void operator()(pointer __p) _NOEXCEPT
3552 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553};
3554
3555template <class _InputIterator, class _ForwardIterator>
3556_ForwardIterator
3557uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3558{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003559 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003560#ifndef _LIBCPP_NO_EXCEPTIONS
3561 _ForwardIterator __s = __r;
3562 try
3563 {
3564#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003565 for (; __f != __l; ++__f, (void) ++__r)
3566 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003567#ifndef _LIBCPP_NO_EXCEPTIONS
3568 }
3569 catch (...)
3570 {
3571 for (; __s != __r; ++__s)
3572 __s->~value_type();
3573 throw;
3574 }
3575#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576 return __r;
3577}
3578
3579template <class _InputIterator, class _Size, class _ForwardIterator>
3580_ForwardIterator
3581uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3582{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003583 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003584#ifndef _LIBCPP_NO_EXCEPTIONS
3585 _ForwardIterator __s = __r;
3586 try
3587 {
3588#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003589 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3590 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003591#ifndef _LIBCPP_NO_EXCEPTIONS
3592 }
3593 catch (...)
3594 {
3595 for (; __s != __r; ++__s)
3596 __s->~value_type();
3597 throw;
3598 }
3599#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600 return __r;
3601}
3602
3603template <class _ForwardIterator, class _Tp>
3604void
3605uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3606{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003607 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003608#ifndef _LIBCPP_NO_EXCEPTIONS
3609 _ForwardIterator __s = __f;
3610 try
3611 {
3612#endif
3613 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003614 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003615#ifndef _LIBCPP_NO_EXCEPTIONS
3616 }
3617 catch (...)
3618 {
3619 for (; __s != __f; ++__s)
3620 __s->~value_type();
3621 throw;
3622 }
3623#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624}
3625
3626template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003627_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003628uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3629{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003631#ifndef _LIBCPP_NO_EXCEPTIONS
3632 _ForwardIterator __s = __f;
3633 try
3634 {
3635#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003636 for (; __n > 0; ++__f, (void) --__n)
3637 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003638#ifndef _LIBCPP_NO_EXCEPTIONS
3639 }
3640 catch (...)
3641 {
3642 for (; __s != __f; ++__s)
3643 __s->~value_type();
3644 throw;
3645 }
3646#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003647 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003648}
3649
Howard Hinnant82894812010-09-22 16:48:34 +00003650class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003651 : public std::exception
3652{
3653public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003654 virtual ~bad_weak_ptr() _NOEXCEPT;
3655 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656};
3657
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003658template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003659
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003660class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003661{
3662 __shared_count(const __shared_count&);
3663 __shared_count& operator=(const __shared_count&);
3664
3665protected:
3666 long __shared_owners_;
3667 virtual ~__shared_count();
3668private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003669 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003670
3671public:
Howard Hinnant82894812010-09-22 16:48:34 +00003672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003673 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674 : __shared_owners_(__refs) {}
3675
Howard Hinnant1694d232011-05-28 14:41:13 +00003676 void __add_shared() _NOEXCEPT;
3677 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003679 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003680};
3681
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003682class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003683 : private __shared_count
3684{
3685 long __shared_weak_owners_;
3686
3687public:
Howard Hinnant82894812010-09-22 16:48:34 +00003688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003689 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690 : __shared_count(__refs),
3691 __shared_weak_owners_(__refs) {}
3692protected:
3693 virtual ~__shared_weak_count();
3694
3695public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003696 void __add_shared() _NOEXCEPT;
3697 void __add_weak() _NOEXCEPT;
3698 void __release_shared() _NOEXCEPT;
3699 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003701 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3702 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003703
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003704 // Define the function out only if we build static libc++ without RTTI.
3705 // Otherwise we may break clients who need to compile their projects with
3706 // -fno-rtti and yet link against a libc++.dylib compiled
3707 // without -fno-rtti.
3708#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003709 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003710#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003712 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713};
3714
3715template <class _Tp, class _Dp, class _Alloc>
3716class __shared_ptr_pointer
3717 : public __shared_weak_count
3718{
3719 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3720public:
Howard Hinnant82894812010-09-22 16:48:34 +00003721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003722 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003723 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003724
Howard Hinnantd4444702010-08-11 17:04:31 +00003725#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003726 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003727#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728
3729private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003730 virtual void __on_zero_shared() _NOEXCEPT;
3731 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732};
3733
Howard Hinnantd4444702010-08-11 17:04:31 +00003734#ifndef _LIBCPP_NO_RTTI
3735
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736template <class _Tp, class _Dp, class _Alloc>
3737const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003738__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003739{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003740 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741}
3742
Howard Hinnant324bb032010-08-22 00:02:43 +00003743#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003744
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003745template <class _Tp, class _Dp, class _Alloc>
3746void
Howard Hinnant1694d232011-05-28 14:41:13 +00003747__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003748{
3749 __data_.first().second()(__data_.first().first());
3750 __data_.first().second().~_Dp();
3751}
3752
3753template <class _Tp, class _Dp, class _Alloc>
3754void
Howard Hinnant1694d232011-05-28 14:41:13 +00003755__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003756{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003757 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3758 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003759 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3760
Eric Fiselier8492cd82015-02-05 23:01:40 +00003761 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003762 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003763 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003764}
3765
3766template <class _Tp, class _Alloc>
3767class __shared_ptr_emplace
3768 : public __shared_weak_count
3769{
3770 __compressed_pair<_Alloc, _Tp> __data_;
3771public:
3772#ifndef _LIBCPP_HAS_NO_VARIADICS
3773
Howard Hinnant82894812010-09-22 16:48:34 +00003774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003776 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777
3778 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003780 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003781 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3782 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783
3784#else // _LIBCPP_HAS_NO_VARIADICS
3785
Howard Hinnant82894812010-09-22 16:48:34 +00003786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787 __shared_ptr_emplace(_Alloc __a)
3788 : __data_(__a) {}
3789
3790 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3793 : __data_(__a, _Tp(__a0)) {}
3794
3795 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3798 : __data_(__a, _Tp(__a0, __a1)) {}
3799
3800 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003802 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3803 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3804
3805#endif // _LIBCPP_HAS_NO_VARIADICS
3806
3807private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003808 virtual void __on_zero_shared() _NOEXCEPT;
3809 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003810public:
Howard Hinnant82894812010-09-22 16:48:34 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003812 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813};
3814
3815template <class _Tp, class _Alloc>
3816void
Howard Hinnant1694d232011-05-28 14:41:13 +00003817__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003818{
3819 __data_.second().~_Tp();
3820}
3821
3822template <class _Tp, class _Alloc>
3823void
Howard Hinnant1694d232011-05-28 14:41:13 +00003824__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003826 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3827 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003828 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003829 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003830 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003831 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003832}
3833
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003834template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835
3836template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003837class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838{
Howard Hinnant324bb032010-08-22 00:02:43 +00003839public:
3840 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003841private:
3842 element_type* __ptr_;
3843 __shared_weak_count* __cntrl_;
3844
3845 struct __nat {int __for_bool_;};
3846public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003847 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3848 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003849 template<class _Yp>
3850 explicit shared_ptr(_Yp* __p,
3851 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3852 template<class _Yp, class _Dp>
3853 shared_ptr(_Yp* __p, _Dp __d,
3854 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3855 template<class _Yp, class _Dp, class _Alloc>
3856 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3857 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003858 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3859 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003860 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3861 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862 template<class _Yp>
3863 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003864 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3865 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003866#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003867 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003869 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3870 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003871#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003872 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003873 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003874#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
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 Hinnant324bb032010-08-22 00:02:43 +00003878#else
Logan Chiene1678a12014-01-31 09:30:46 +00003879 template<class _Yp>
3880 shared_ptr(auto_ptr<_Yp> __r,
3881 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003882#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003883#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003884 template <class _Yp, class _Dp>
3885 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3886 typename enable_if
3887 <
3888 !is_lvalue_reference<_Dp>::value &&
3889 !is_array<_Yp>::value &&
3890 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3891 __nat
3892 >::type = __nat());
3893 template <class _Yp, class _Dp>
3894 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3895 typename enable_if
3896 <
3897 is_lvalue_reference<_Dp>::value &&
3898 !is_array<_Yp>::value &&
3899 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3900 __nat
3901 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003902#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003903 template <class _Yp, class _Dp>
3904 shared_ptr(unique_ptr<_Yp, _Dp>,
3905 typename enable_if
3906 <
3907 !is_lvalue_reference<_Dp>::value &&
3908 !is_array<_Yp>::value &&
3909 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3910 __nat
3911 >::type = __nat());
3912 template <class _Yp, class _Dp>
3913 shared_ptr(unique_ptr<_Yp, _Dp>,
3914 typename enable_if
3915 <
3916 is_lvalue_reference<_Dp>::value &&
3917 !is_array<_Yp>::value &&
3918 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3919 __nat
3920 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003921#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003922
3923 ~shared_ptr();
3924
Howard Hinnant1694d232011-05-28 14:41:13 +00003925 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003926 template<class _Yp>
3927 typename enable_if
3928 <
3929 is_convertible<_Yp*, element_type*>::value,
3930 shared_ptr&
3931 >::type
3932 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003933#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003934 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003935 template<class _Yp>
3936 typename enable_if
3937 <
3938 is_convertible<_Yp*, element_type*>::value,
3939 shared_ptr<_Tp>&
3940 >::type
3941 operator=(shared_ptr<_Yp>&& __r);
3942 template<class _Yp>
3943 typename enable_if
3944 <
3945 !is_array<_Yp>::value &&
3946 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003947 shared_ptr
3948 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003949 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003950#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003951 template<class _Yp>
3952 typename enable_if
3953 <
3954 !is_array<_Yp>::value &&
3955 is_convertible<_Yp*, element_type*>::value,
3956 shared_ptr&
3957 >::type
3958 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003959#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003960 template <class _Yp, class _Dp>
3961 typename enable_if
3962 <
3963 !is_array<_Yp>::value &&
3964 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3965 shared_ptr&
3966 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003967#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003968 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003969#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003970 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003971#endif
3972
Howard Hinnant1694d232011-05-28 14:41:13 +00003973 void swap(shared_ptr& __r) _NOEXCEPT;
3974 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003975 template<class _Yp>
3976 typename enable_if
3977 <
3978 is_convertible<_Yp*, element_type*>::value,
3979 void
3980 >::type
3981 reset(_Yp* __p);
3982 template<class _Yp, class _Dp>
3983 typename enable_if
3984 <
3985 is_convertible<_Yp*, element_type*>::value,
3986 void
3987 >::type
3988 reset(_Yp* __p, _Dp __d);
3989 template<class _Yp, class _Dp, class _Alloc>
3990 typename enable_if
3991 <
3992 is_convertible<_Yp*, element_type*>::value,
3993 void
3994 >::type
3995 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003996
Howard Hinnant82894812010-09-22 16:48:34 +00003997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003998 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003999 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004000 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4001 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004003 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004005 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004007 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004009 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
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(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004013 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004014 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004016 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004017 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004018 _LIBCPP_INLINE_VISIBILITY
4019 bool
4020 __owner_equivalent(const shared_ptr& __p) const
4021 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004022
Howard Hinnantd4444702010-08-11 17:04:31 +00004023#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004024 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004026 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004027 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004028#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004029
4030#ifndef _LIBCPP_HAS_NO_VARIADICS
4031
4032 template<class ..._Args>
4033 static
4034 shared_ptr<_Tp>
4035 make_shared(_Args&& ...__args);
4036
4037 template<class _Alloc, class ..._Args>
4038 static
4039 shared_ptr<_Tp>
4040 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4041
4042#else // _LIBCPP_HAS_NO_VARIADICS
4043
4044 static shared_ptr<_Tp> make_shared();
4045
4046 template<class _A0>
4047 static shared_ptr<_Tp> make_shared(_A0&);
4048
4049 template<class _A0, class _A1>
4050 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4051
4052 template<class _A0, class _A1, class _A2>
4053 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4054
4055 template<class _Alloc>
4056 static shared_ptr<_Tp>
4057 allocate_shared(const _Alloc& __a);
4058
4059 template<class _Alloc, class _A0>
4060 static shared_ptr<_Tp>
4061 allocate_shared(const _Alloc& __a, _A0& __a0);
4062
4063 template<class _Alloc, class _A0, class _A1>
4064 static shared_ptr<_Tp>
4065 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4066
4067 template<class _Alloc, class _A0, class _A1, class _A2>
4068 static shared_ptr<_Tp>
4069 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4070
4071#endif // _LIBCPP_HAS_NO_VARIADICS
4072
4073private:
4074
4075 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004077 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004078 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004079 {
4080 if (__e)
4081 __e->__weak_this_ = *this;
4082 }
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